14 #include <sys/types.h> 39 static int operations = 0;
40 static GHashTable *recurring_actions = NULL;
44 static GList *blocked_ops = NULL;
47 static GList *inflight_ops = NULL;
49 static void handle_blocked_ops(
void);
55 action, interval, timeout, NULL, 0);
72 if (rc > 0 && stat(path, &st) == 0) {
94 init_recurring_actions(
void)
96 if (recurring_actions == NULL) {
97 recurring_actions = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
110 static inline gboolean
115 && (g_list_find(inflight_ops, op) != NULL);
131 expand_resource_class(
const char *rsc,
const char *standard,
const char *agent)
133 char *expanded_class = NULL;
139 crm_debug(
"Found %s agent %s for %s", found_class, agent, rsc);
140 expanded_class = strdup(found_class);
142 crm_info(
"Assuming resource class lsb for agent %s for %s",
147 expanded_class = strdup(standard);
150 return expanded_class;
155 const char *agent,
const char *action,
int interval,
int timeout,
165 if (crm_strlen_zero(name)) {
166 crm_err(
"Cannot create operation without resource name");
170 if (crm_strlen_zero(standard)) {
171 crm_err(
"Cannot create operation for %s without resource class", name);
176 crm_err(
"Cannot create OCF operation for %s without provider", name);
180 if (crm_strlen_zero(agent)) {
181 crm_err(
"Cannot create operation for %s without agent name", name);
185 if (crm_strlen_zero(action)) {
186 crm_err(
"Cannot create operation for %s without operation name", name);
196 op->
rsc = strdup(name);
199 op->
standard = expand_resource_class(name, standard, agent);
200 op->
agent = strdup(agent);
212 op->
action = strdup(action);
220 crm_err(
"Internal error: cannot create agent path");
227 if (op->
agent[0] ==
'/') {
232 crm_err(
"Internal error: cannot create agent path");
238 #if SUPPORT_HEARTBEAT 245 if (op->
agent[0] ==
'/') {
249 }
else if (asprintf(&op->
opaque->
exec,
"%s/%s", HB_RA_DIR, op->
agent) == -1) {
250 crm_err(
"Internal error: cannot create agent path");
259 for (index = 1; index <=
MAX_ARGC - 3; index++ ) {
260 snprintf(buf_tmp,
sizeof(buf_tmp),
"%d", index);
261 value_tmp = g_hash_table_lookup(params, buf_tmp);
262 if (value_tmp == NULL) {
267 op->
opaque->
args[param_num++] = strdup(value_tmp);
288 if (op->
agent[0] ==
'/') {
294 crm_err(
"Internal error: cannot create agent path");
303 op->
opaque->
args[index] = strdup(
"--version");
310 static int args_size =
sizeof(op->
opaque->
args) /
sizeof(
char *);
312 g_hash_table_iter_init(&iter, params);
314 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value) &&
315 index <= args_size - 3) {
317 char *long_opt = NULL;
324 long_opt = calloc(1, len);
325 sprintf(long_opt,
"--%s", key);
326 long_opt[len - 1] = 0;
342 g_hash_table_destroy(params);
348 g_hash_table_destroy(params);
359 unsigned int cur_arg;
361 op = calloc(1,
sizeof(*op));
367 for (cur_arg = 1; args && args[cur_arg - 1]; cur_arg++) {
368 op->
opaque->
args[cur_arg] = strdup(args[cur_arg - 1]);
371 crm_err(
"svc_action_t args list not long enough for '%s' execution request.", exec);
395 GHashTable *params,
int sequence,
void *cb_data)
401 action->
id = strdup(
id);
426 CRM_CHECK((op != NULL) && (user != NULL),
return -EINVAL);
431 set_alert_env(gpointer key, gpointer value, gpointer user_data)
436 rc =
setenv(key, value, 1);
443 (
char*)key, (value? (
char*)value :
""));
445 crm_trace(
"setenv %s=%s", (
char*)key, (value? (
char*)value :
""));
450 unset_alert_env(gpointer key, gpointer value, gpointer user_data)
452 if (unsetenv(key) < 0) {
473 gboolean responsible;
478 g_hash_table_foreach(action->
params, set_alert_env, NULL);
482 g_hash_table_foreach(action->
params, unset_alert_env, NULL);
496 services_set_op_pending(
svc_action_t *op, DBusPendingCall *pending)
498 if (op->
opaque->pending && (op->
opaque->pending != pending)) {
504 dbus_pending_call_unref(op->
opaque->pending);
506 op->
opaque->pending = pending;
508 crm_trace(
"Updated pending %s DBus call (%p)", op->
id, pending);
523 if(op->
opaque->timerid != 0) {
525 g_source_remove(op->
opaque->timerid);
531 if(dbus_pending_call_get_completed(op->
opaque->pending)) {
534 dbus_pending_call_cancel(op->
opaque->pending);
535 dbus_pending_call_unref(op->
opaque->pending);
536 op->
opaque->pending = NULL;
564 CRM_CHECK(g_list_find(inflight_ops, op) == NULL,
return);
565 CRM_CHECK(g_list_find(blocked_ops, op) == NULL,
return);
567 || (g_hash_table_lookup(recurring_actions, op->
id) == NULL),
596 g_hash_table_destroy(op->
params);
608 if (recurring_actions) {
609 g_hash_table_remove(recurring_actions, op->
id);
632 gboolean cancelled = FALSE;
637 init_recurring_actions();
638 op = g_hash_table_lookup(recurring_actions,
id);
656 crm_info(
"Terminating in-flight op %s (pid %d) early because it was cancelled",
659 if (cancelled == FALSE) {
660 crm_err(
"Termination of %s (pid %d) failed",
id, op->
pid);
670 if (inflight_systemd_or_upstart(op)) {
671 crm_info(
"Will cancel %s op %s when in-flight instance completes",
683 blocked_ops = g_list_remove(blocked_ops, op);
698 init_recurring_actions();
699 op = g_hash_table_lookup(recurring_actions,
id);
707 if (op->
pid || inflight_systemd_or_upstart(op)) {
734 dup = g_hash_table_lookup(recurring_actions, op->
id);
736 if (dup && (dup != op)) {
759 inline static gboolean
794 inflight_ops = g_list_append(inflight_ops, op);
808 inflight_ops = g_list_remove(inflight_ops, op);
809 blocked_ops = g_list_remove(blocked_ops, op);
812 handle_blocked_ops();
819 if (action_callback) {
824 init_recurring_actions();
825 if (handle_duplicate_recurring(op) == TRUE) {
830 g_hash_table_replace(recurring_actions, op->
id, op);
834 blocked_ops = g_list_append(blocked_ops, op);
838 return action_exec_helper(op);
842 static gboolean processing_blocked_ops = FALSE;
850 for (gIter = inflight_ops; gIter != NULL; gIter = gIter->next) {
861 handle_blocked_ops(
void)
863 GList *executed_ops = NULL;
866 gboolean res = FALSE;
868 if (processing_blocked_ops) {
873 processing_blocked_ops = TRUE;
877 for (gIter = blocked_ops; gIter != NULL; gIter = gIter->next) {
882 executed_ops = g_list_append(executed_ops, op);
883 res = action_exec_helper(op);
892 for (gIter = executed_ops; gIter != NULL; gIter = gIter->next) {
894 blocked_ops = g_list_remove(blocked_ops, op);
896 g_list_free(executed_ops);
898 processing_blocked_ops = FALSE;
901 #define lsb_metadata_template \ 902 "<?xml version='1.0'?>\n" \ 903 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" \ 904 "<resource-agent name='%s' version='" PCMK_DEFAULT_AGENT_VERSION "'>\n" \ 905 " <version>1.0</version>\n" \ 906 " <longdesc lang='en'>\n" \ 909 " <shortdesc lang='en'>%s</shortdesc>\n" \ 913 " <action name='meta-data' timeout='5' />\n" \ 914 " <action name='start' timeout='15' />\n" \ 915 " <action name='stop' timeout='15' />\n" \ 916 " <action name='status' timeout='15' />\n" \ 917 " <action name='restart' timeout='15' />\n" \ 918 " <action name='force-reload' timeout='15' />\n" \ 919 " <action name='monitor' timeout='15' interval='15' />\n" \ 921 " <special tag='LSB'>\n" \ 922 " <Provides>%s</Provides>\n" \ 923 " <Required-Start>%s</Required-Start>\n" \ 924 " <Required-Stop>%s</Required-Stop>\n" \ 925 " <Should-Start>%s</Should-Start>\n" \ 926 " <Should-Stop>%s</Should-Stop>\n" \ 927 " <Default-Start>%s</Default-Start>\n" \ 928 " <Default-Stop>%s</Default-Stop>\n" \ 930 "</resource-agent>\n" 935 #define LSB_INITSCRIPT_INFOBEGIN_TAG "### BEGIN INIT INFO" 936 #define LSB_INITSCRIPT_INFOEND_TAG "### END INIT INFO" 937 #define PROVIDES "# Provides:" 938 #define REQ_START "# Required-Start:" 939 #define REQ_STOP "# Required-Stop:" 940 #define SHLD_START "# Should-Start:" 941 #define SHLD_STOP "# Should-Stop:" 942 #define DFLT_START "# Default-Start:" 943 #define DFLT_STOP "# Default-Stop:" 944 #define SHORT_DSCR "# Short-Description:" 945 #define DESCRIPTION "# Description:" 947 #define lsb_meta_helper_free_value(m) \ 965 static inline gboolean
966 lsb_meta_helper_get_value(
const char *line,
char **value,
const char *prefix)
969 *value = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST line+strlen(prefix));
975 #define DESC_MAX 2048 978 lsb_get_metadata(
const char *
type,
char **output)
980 char ra_pathname[PATH_MAX] = { 0, };
982 char buffer[1024] = { 0, };
983 char *provides = NULL;
984 char *req_start = NULL;
985 char *req_stop = NULL;
986 char *shld_start = NULL;
987 char *shld_stop = NULL;
988 char *dflt_start = NULL;
989 char *dflt_stop = NULL;
990 char *s_dscrpt = NULL;
991 char *xml_l_dscrpt = NULL;
993 bool in_header = FALSE;
994 char description[
DESC_MAX] = { 0, };
996 if (type[0] ==
'/') {
997 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s", type);
999 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s/%s",
1003 crm_trace(
"Looking into %s", ra_pathname);
1004 fp = fopen(ra_pathname,
"r");
1010 while (fgets(buffer,
sizeof(buffer), fp)) {
1022 if (lsb_meta_helper_get_value(buffer, &provides,
PROVIDES)) {
1025 if (lsb_meta_helper_get_value(buffer, &req_start,
REQ_START)) {
1028 if (lsb_meta_helper_get_value(buffer, &req_stop,
REQ_STOP)) {
1031 if (lsb_meta_helper_get_value(buffer, &shld_start,
SHLD_START)) {
1034 if (lsb_meta_helper_get_value(buffer, &shld_stop,
SHLD_STOP)) {
1037 if (lsb_meta_helper_get_value(buffer, &dflt_start,
DFLT_START)) {
1040 if (lsb_meta_helper_get_value(buffer, &dflt_stop,
DFLT_STOP)) {
1043 if (lsb_meta_helper_get_value(buffer, &s_dscrpt,
SHORT_DSCR)) {
1050 bool processed_line = TRUE;
1053 offset += snprintf(description,
DESC_MAX,
"%s",
1058 while (fgets(buffer,
sizeof(buffer), fp)) {
1064 offset += snprintf(description + offset,
DESC_MAX - offset,
1070 processed_line = FALSE;
1076 xml_l_dscrpt = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST(description));
1078 if (processed_line) {
1088 if (buffer[0] !=
'#') {
1095 (xml_l_dscrpt? xml_l_dscrpt : type),
1096 (s_dscrpt? s_dscrpt : type),
1097 (provides? provides :
""),
1098 (req_start? req_start :
""),
1099 (req_stop? req_stop :
""),
1100 (shld_start? shld_start :
""),
1101 (shld_stop? shld_stop :
""),
1102 (dflt_start? dflt_start :
""),
1103 (dflt_stop? dflt_stop :
""));
1115 crm_trace(
"Created fake metadata: %llu",
1116 (
unsigned long long) strlen(*output));
1122 nagios_get_metadata(
const char *
type,
char **output)
1125 FILE *file_strm = NULL;
1126 int start = 0, length = 0, read_len = 0;
1127 char *metadata_file = NULL;
1131 len += strlen(type);
1132 metadata_file = calloc(1, len);
1133 CRM_CHECK(metadata_file != NULL,
return -ENOMEM);
1136 file_strm = fopen(metadata_file,
"r");
1137 if (file_strm == NULL) {
1138 crm_err(
"Metadata file %s does not exist", metadata_file);
1139 free(metadata_file);
1144 start = ftell(file_strm);
1145 fseek(file_strm, 0L, SEEK_END);
1146 length = ftell(file_strm);
1147 fseek(file_strm, 0L, start);
1153 crm_info(
"%s was not valid", metadata_file);
1159 crm_trace(
"Reading %d bytes from file", length);
1160 *output = calloc(1, (length + 1));
1161 read_len = fread(*output, 1, length, file_strm);
1162 if (read_len != length) {
1163 crm_err(
"Calculated and read bytes differ: %d vs. %d",
1172 free(metadata_file);
1177 #if SUPPORT_HEARTBEAT 1193 static const char hb_metadata_template[] =
1194 "<?xml version='1.0'?>\n" 1195 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" 1197 "<version>1.0</version>\n" 1198 "<longdesc lang='en'>\n" 1201 "<shortdesc lang='en'>%s</shortdesc>\n" 1203 "<parameter name='1' unique='1' required='0'>\n" 1204 "<longdesc lang='en'>\n" 1205 "This argument will be passed as the first argument to the " 1206 "heartbeat resource agent (assuming it supports one)\n" 1208 "<shortdesc lang='en'>argv[1]</shortdesc>\n" 1209 "<content type='string' default=' ' />\n" 1211 "<parameter name='2' unique='1' required='0'>\n" 1212 "<longdesc lang='en'>\n" 1213 "This argument will be passed as the second argument to the " 1214 "heartbeat resource agent (assuming it supports one)\n" 1216 "<shortdesc lang='en'>argv[2]</shortdesc>\n" 1217 "<content type='string' default=' ' />\n" 1219 "<parameter name='3' unique='1' required='0'>\n" 1220 "<longdesc lang='en'>\n" 1221 "This argument will be passed as the third argument to the " 1222 "heartbeat resource agent (assuming it supports one)\n" 1224 "<shortdesc lang='en'>argv[3]</shortdesc>\n" 1225 "<content type='string' default=' ' />\n" 1227 "<parameter name='4' unique='1' required='0'>\n" 1228 "<longdesc lang='en'>\n" 1229 "This argument will be passed as the fourth argument to the " 1230 "heartbeat resource agent (assuming it supports one)\n" 1232 "<shortdesc lang='en'>argv[4]</shortdesc>\n" 1233 "<content type='string' default=' ' />\n" 1235 "<parameter name='5' unique='1' required='0'>\n" 1236 "<longdesc lang='en'>\n" 1237 "This argument will be passed as the fifth argument to the " 1238 "heartbeat resource agent (assuming it supports one)\n" 1240 "<shortdesc lang='en'>argv[5]</shortdesc>\n" 1241 "<content type='string' default=' ' />\n" 1245 "<action name='start' timeout='15' />\n" 1246 "<action name='stop' timeout='15' />\n" 1247 "<action name='status' timeout='15' />\n" 1248 "<action name='monitor' timeout='15' interval='15' start-delay='15' />\n" 1249 "<action name='meta-data' timeout='5' />\n" 1251 "<special tag='heartbeat'>\n" 1253 "</resource-agent>\n";
1256 heartbeat_get_metadata(
const char *type,
char **output)
1259 crm_trace(
"Created fake metadata: %llu",
1260 (
unsigned long long) strlen(*output));
1268 const char *
class = op->standard;
1270 if (op->
agent == NULL) {
1271 crm_err(
"meta-data requested without specifying agent");
1275 if (
class == NULL) {
1276 crm_err(
"meta-data requested for agent %s without specifying class",
1285 if (
class == NULL) {
1286 crm_err(
"meta-data requested for %s, but could not determine class",
1301 #if SUPPORT_HEARTBEAT 1307 return action_exec_helper(op);
1330 rc = action_get_metadata(op);
1332 rc = action_exec_helper(op);
1357 #if SUPPORT_HEARTBEAT 1359 resources_os_list_hb_agents(
void)
1368 GList *standards = NULL;
1369 GList *agents = NULL;
1378 standards = g_list_append(standards,
1380 g_list_free_full(agents, free);
1387 standards = g_list_append(standards,
1389 g_list_free_full(agents, free);
1396 standards = g_list_append(standards,
1398 g_list_free_full(agents, free);
1402 #if SUPPORT_HEARTBEAT 1422 if ((standard == NULL)
1429 if (standard == NULL) {
1433 result = g_list_concat(tmp1, tmp2);
1440 result = g_list_concat(tmp1, tmp2);
1448 result = g_list_concat(tmp1, tmp2);
1458 #if SUPPORT_HEARTBEAT 1460 return resources_os_list_hb_agents();
gboolean services_action_cancel(const char *name, const char *action, int interval)
Cancel a recurring action.
#define CRM_CHECK(expr, failure_action)
#define lsb_metadata_template
void(* callback)(svc_action_t *op)
GList * resources_list_providers(const char *standard)
Get a list of providers.
gboolean upstart_job_exists(const char *name)
gboolean mainloop_child_kill(pid_t pid)
svc_action_t * resources_action_create(const char *name, const char *standard, const char *provider, const char *agent, const char *action, int interval, int timeout, GHashTable *params, enum svc_action_flags flags)
Create a new resource action.
mainloop_io_t * stderr_gsource
GList * resources_os_list_ocf_agents(const char *provider)
GList * resources_os_list_ocf_providers(void)
#define PCMK_RESOURCE_CLASS_SYSTEMD
gboolean recurring_action_timer(gpointer data)
GList * services_os_get_directory_list(const char *root, gboolean files, gboolean executable)
gboolean services_action_async(svc_action_t *op, void(*action_callback)(svc_action_t *))
int crm_user_lookup(const char *name, uid_t *uid, gid_t *gid)
gboolean services_os_action_execute(svc_action_t *op)
G_GNUC_INTERNAL GList * resources_os_list_nagios_agents(void)
gboolean upstart_job_exec(svc_action_t *op)
gboolean is_op_blocked(const char *rsc)
Wrappers for and extensions to glib mainloop.
bool crm_starts_with(const char *str, const char *prefix)
Check whether a string starts with a certain sequence.
svc_action_t * services_action_create_generic(const char *exec, const char *args[])
GList * resources_os_list_lsb_agents(void)
enum svc_action_flags flags
#define crm_warn(fmt, args...)
GList * services_list(void)
#define PCMK_RESOURCE_CLASS_OCF
gboolean cancel_recurring_action(svc_action_t *op)
svc_action_private_t * opaque
GList * upstart_job_listall(void)
#define crm_debug(fmt, args...)
gboolean operation_finalize(svc_action_t *op)
svc_action_t * services_alert_create(const char *id, const char *exec, int timeout, GHashTable *params, int sequence, void *cb_data)
Create an alert agent action.
gboolean systemd_unit_exists(const char *name)
#define PCMK_RESOURCE_CLASS_SERVICE
#define LSB_INITSCRIPT_INFOEND_TAG
#define crm_trace(fmt, args...)
int setenv(const char *name, const char *value, int why)
gboolean services_alert_async(svc_action_t *action, void(*cb)(svc_action_t *op))
Execute an alert agent action.
#define LSB_INITSCRIPT_INFOBEGIN_TAG
gboolean services_action_sync(svc_action_t *op)
#define SUPPORT_HEARTBEAT
gboolean services_action_kick(const char *name, const char *action, int interval)
GList * systemd_unit_listall(void)
const char * resources_find_service_class(const char *agent)
GList * resources_list_agents(const char *standard, const char *provider)
Get a list of resource agents.
void services_add_inflight_op(svc_action_t *op)
GList * resources_list_standards(void)
void services_untrack_op(svc_action_t *op)
GList * get_directory_list(const char *root, gboolean files, gboolean executable)
Get a list of files or directories in a given path.
#define PCMK_RESOURCE_CLASS_NAGIOS
#define PCMK_RESOURCE_CLASS_LSB
#define crm_perror(level, fmt, args...)
Log a system error message.
#define NAGIOS_PLUGIN_DIR
#define crm_err(fmt, args...)
#define PCMK_RESOURCE_CLASS_UPSTART
#define PCMK_RESOURCE_CLASS_HB
int services_action_user(svc_action_t *op, const char *user)
Set the user and group that an action will execute as.
#define PCMK_DEFAULT_AGENT_VERSION
mainloop_io_t * stdout_gsource
#define XML_ATTR_CRM_VERSION
void mainloop_del_fd(mainloop_io_t *client)
void services_action_cleanup(svc_action_t *op)
bool crm_provider_required(const char *standard)
Check whether a resource standard requires a provider to be specified.
char * generate_op_key(const char *rsc_id, const char *op_type, int interval)
Generate an operation key.
#define safe_str_eq(a, b)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
void services_action_free(svc_action_t *op)
gboolean systemd_unit_exec(svc_action_t *op)
#define crm_info(fmt, args...)
#define NAGIOS_METADATA_DIR
svc_action_t * services_action_create(const char *name, const char *action, int interval, int timeout)
enum crm_ais_msg_types type
#define lsb_meta_helper_free_value(m)