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);
59 services__lsb_agent_path(
const char *agent)
61 return (*agent ==
'/')? strdup(agent)
66 services__lsb_agent_exists(
const char *agent)
70 char *path = services__lsb_agent_path(agent);
72 rc = (stat(path, &st) == 0);
96 if (services__lsb_agent_exists(agent)) {
115 init_recurring_actions(
void)
117 if (recurring_actions == NULL) {
118 recurring_actions = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
131 static inline gboolean
136 && (g_list_find(inflight_ops, op) != NULL);
152 expand_resource_class(
const char *rsc,
const char *standard,
const char *agent)
154 char *expanded_class = NULL;
160 crm_debug(
"Found %s agent %s for %s", found_class, agent, rsc);
161 expanded_class = strdup(found_class);
163 crm_info(
"Assuming resource class lsb for agent %s for %s",
168 expanded_class = strdup(standard);
171 return expanded_class;
176 const char *agent,
const char *action,
int interval,
int timeout,
187 if (crm_strlen_zero(name)) {
188 crm_err(
"Cannot create operation without resource name");
192 if (crm_strlen_zero(standard)) {
193 crm_err(
"Cannot create operation for %s without resource class", name);
199 crm_err(
"Cannot create operation for %s without provider", name);
203 if (crm_strlen_zero(agent)) {
204 crm_err(
"Cannot create operation for %s without agent name", name);
208 if (crm_strlen_zero(action)) {
209 crm_err(
"Cannot create operation for %s without operation name", name);
219 op->
rsc = strdup(name);
222 op->
standard = expand_resource_class(name, standard, agent);
223 op->
agent = strdup(agent);
229 op->
action = strdup(
"status");
231 op->
action = strdup(action);
245 crm_err(
"Internal error: cannot create agent path");
256 #if SUPPORT_HEARTBEAT 263 if (op->
agent[0] ==
'/') {
267 }
else if (asprintf(&op->
opaque->
exec,
"%s/%s", HB_RA_DIR, op->
agent) == -1) {
268 crm_err(
"Internal error: cannot create agent path");
277 for (index = 1; index <=
MAX_ARGC - 3; index++ ) {
278 snprintf(buf_tmp,
sizeof(buf_tmp),
"%d", index);
279 value_tmp = g_hash_table_lookup(op->
params, buf_tmp);
280 if (value_tmp == NULL) {
285 op->
opaque->
args[param_num++] = strdup(value_tmp);
289 g_hash_table_destroy(op->
params);
308 if (op->
agent[0] ==
'/') {
314 crm_err(
"Internal error: cannot create agent path");
329 static int args_size =
sizeof(op->
opaque->
args) /
sizeof(
char *);
331 g_hash_table_iter_init(&iter, op->
params);
333 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value) &&
334 index <= args_size - 3) {
336 char *long_opt = NULL;
343 long_opt = calloc(1, len);
344 sprintf(long_opt,
"--%s", key);
345 long_opt[len - 1] = 0;
355 g_hash_table_destroy(op->
params);
365 g_hash_table_destroy(params);
371 g_hash_table_destroy(params);
382 unsigned int cur_arg;
384 op = calloc(1,
sizeof(*op));
390 for (cur_arg = 1; args && args[cur_arg - 1]; cur_arg++) {
391 op->
opaque->
args[cur_arg] = strdup(args[cur_arg - 1]);
394 crm_err(
"svc_action_t args list not long enough for '%s' execution request.", exec);
418 GHashTable *params,
int sequence,
void *cb_data)
424 action->
id = strdup(
id);
449 CRM_CHECK((op != NULL) && (user != NULL),
return -EINVAL);
481 services_set_op_pending(
svc_action_t *op, DBusPendingCall *pending)
483 if (op->
opaque->pending && (op->
opaque->pending != pending)) {
489 dbus_pending_call_unref(op->
opaque->pending);
491 op->
opaque->pending = pending;
493 crm_trace(
"Updated pending %s DBus call (%p)", op->
id, pending);
503 if ((op == NULL) || (op->
opaque == NULL)) {
508 if(op->
opaque->timerid != 0) {
510 g_source_remove(op->
opaque->timerid);
515 if (dbus_pending_call_get_completed(op->
opaque->pending)) {
517 crm_warn(
"Result of %s op %s was unhandled",
520 crm_debug(
"Will ignore any result of canceled %s op %s",
523 dbus_pending_call_cancel(op->
opaque->pending);
524 services_set_op_pending(op, NULL);
552 CRM_CHECK(g_list_find(inflight_ops, op) == NULL,
return);
553 CRM_CHECK(g_list_find(blocked_ops, op) == NULL,
return);
555 || (g_hash_table_lookup(recurring_actions, op->
id) == NULL),
584 g_hash_table_destroy(op->
params);
596 if (recurring_actions) {
597 g_hash_table_remove(recurring_actions, op->
id);
620 gboolean cancelled = FALSE;
625 init_recurring_actions();
626 op = g_hash_table_lookup(recurring_actions,
id);
644 crm_info(
"Terminating in-flight op %s (pid %d) early because it was cancelled",
647 if (cancelled == FALSE) {
648 crm_err(
"Termination of %s (pid %d) failed",
id, op->
pid);
655 if (inflight_systemd_or_upstart(op)) {
656 inflight_ops = g_list_remove(inflight_ops, op);
674 blocked_ops = g_list_remove(blocked_ops, op);
690 init_recurring_actions();
691 op = g_hash_table_lookup(recurring_actions,
id);
699 if (op->
pid || inflight_systemd_or_upstart(op)) {
726 dup = g_hash_table_lookup(recurring_actions, op->
id);
728 if (dup && (dup != op)) {
751 inline static gboolean
786 inflight_ops = g_list_append(inflight_ops, op);
800 inflight_ops = g_list_remove(inflight_ops, op);
801 blocked_ops = g_list_remove(blocked_ops, op);
804 handle_blocked_ops();
813 if (action_callback) {
816 if (action_fork_callback) {
821 init_recurring_actions();
822 if (handle_duplicate_recurring(op) == TRUE) {
827 g_hash_table_replace(recurring_actions, op->
id, op);
832 blocked_ops = g_list_append(blocked_ops, op);
836 return action_exec_helper(op);
846 static gboolean processing_blocked_ops = FALSE;
854 for (gIter = inflight_ops; gIter != NULL; gIter = gIter->next) {
865 handle_blocked_ops(
void)
867 GList *executed_ops = NULL;
870 gboolean res = FALSE;
872 if (processing_blocked_ops) {
877 processing_blocked_ops = TRUE;
881 for (gIter = blocked_ops; gIter != NULL; gIter = gIter->next) {
886 executed_ops = g_list_append(executed_ops, op);
887 res = action_exec_helper(op);
896 for (gIter = executed_ops; gIter != NULL; gIter = gIter->next) {
898 blocked_ops = g_list_remove(blocked_ops, op);
900 g_list_free(executed_ops);
902 processing_blocked_ops = FALSE;
905 #define lsb_metadata_template \ 906 "<?xml version='1.0'?>\n" \ 907 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" \ 908 "<resource-agent name='%s' version='" PCMK_DEFAULT_AGENT_VERSION "'>\n" \ 909 " <version>1.0</version>\n" \ 910 " <longdesc lang='en'>\n" \ 913 " <shortdesc lang='en'>%s</shortdesc>\n" \ 917 " <action name='meta-data' timeout='5' />\n" \ 918 " <action name='start' timeout='15' />\n" \ 919 " <action name='stop' timeout='15' />\n" \ 920 " <action name='status' timeout='15' />\n" \ 921 " <action name='restart' timeout='15' />\n" \ 922 " <action name='force-reload' timeout='15' />\n" \ 923 " <action name='monitor' timeout='15' interval='15' />\n" \ 925 " <special tag='LSB'>\n" \ 926 " <Provides>%s</Provides>\n" \ 927 " <Required-Start>%s</Required-Start>\n" \ 928 " <Required-Stop>%s</Required-Stop>\n" \ 929 " <Should-Start>%s</Should-Start>\n" \ 930 " <Should-Stop>%s</Should-Stop>\n" \ 931 " <Default-Start>%s</Default-Start>\n" \ 932 " <Default-Stop>%s</Default-Stop>\n" \ 934 "</resource-agent>\n" 939 #define LSB_INITSCRIPT_INFOBEGIN_TAG "### BEGIN INIT INFO" 940 #define LSB_INITSCRIPT_INFOEND_TAG "### END INIT INFO" 941 #define PROVIDES "# Provides:" 942 #define REQ_START "# Required-Start:" 943 #define REQ_STOP "# Required-Stop:" 944 #define SHLD_START "# Should-Start:" 945 #define SHLD_STOP "# Should-Stop:" 946 #define DFLT_START "# Default-Start:" 947 #define DFLT_STOP "# Default-Stop:" 948 #define SHORT_DSCR "# Short-Description:" 949 #define DESCRIPTION "# Description:" 951 #define lsb_meta_helper_free_value(m) \ 969 static inline gboolean
970 lsb_meta_helper_get_value(
const char *line,
char **value,
const char *prefix)
973 *value = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST line+strlen(prefix));
979 #define DESC_MAX 2048 982 lsb_get_metadata(
const char *
type,
char **output)
984 char ra_pathname[PATH_MAX] = { 0, };
986 char buffer[1024] = { 0, };
987 char *provides = NULL;
988 char *req_start = NULL;
989 char *req_stop = NULL;
990 char *shld_start = NULL;
991 char *shld_stop = NULL;
992 char *dflt_start = NULL;
993 char *dflt_stop = NULL;
994 char *s_dscrpt = NULL;
995 char *xml_l_dscrpt = NULL;
997 bool in_header = FALSE;
998 char description[
DESC_MAX] = { 0, };
1000 if (type[0] ==
'/') {
1001 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s", type);
1003 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s/%s",
1007 crm_trace(
"Looking into %s", ra_pathname);
1008 fp = fopen(ra_pathname,
"r");
1014 while (fgets(buffer,
sizeof(buffer), fp)) {
1026 if (lsb_meta_helper_get_value(buffer, &provides,
PROVIDES)) {
1029 if (lsb_meta_helper_get_value(buffer, &req_start,
REQ_START)) {
1032 if (lsb_meta_helper_get_value(buffer, &req_stop,
REQ_STOP)) {
1035 if (lsb_meta_helper_get_value(buffer, &shld_start,
SHLD_START)) {
1038 if (lsb_meta_helper_get_value(buffer, &shld_stop,
SHLD_STOP)) {
1041 if (lsb_meta_helper_get_value(buffer, &dflt_start,
DFLT_START)) {
1044 if (lsb_meta_helper_get_value(buffer, &dflt_stop,
DFLT_STOP)) {
1047 if (lsb_meta_helper_get_value(buffer, &s_dscrpt,
SHORT_DSCR)) {
1054 bool processed_line = TRUE;
1057 offset += snprintf(description,
DESC_MAX,
"%s",
1062 while (fgets(buffer,
sizeof(buffer), fp)) {
1068 offset += snprintf(description + offset,
DESC_MAX - offset,
1074 processed_line = FALSE;
1080 xml_l_dscrpt = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST(description));
1082 if (processed_line) {
1092 if (buffer[0] !=
'#') {
1099 (xml_l_dscrpt? xml_l_dscrpt : type),
1100 (s_dscrpt? s_dscrpt : type),
1101 (provides? provides :
""),
1102 (req_start? req_start :
""),
1103 (req_stop? req_stop :
""),
1104 (shld_start? shld_start :
""),
1105 (shld_stop? shld_stop :
""),
1106 (dflt_start? dflt_start :
""),
1107 (dflt_stop? dflt_stop :
""));
1119 crm_trace(
"Created fake metadata: %llu",
1120 (
unsigned long long) strlen(*output));
1126 nagios_get_metadata(
const char *
type,
char **output)
1129 FILE *file_strm = NULL;
1130 int start = 0, length = 0, read_len = 0;
1134 file_strm = fopen(metadata_file,
"r");
1135 if (file_strm == NULL) {
1136 crm_err(
"Metadata file %s does not exist", metadata_file);
1137 free(metadata_file);
1142 start = ftell(file_strm);
1143 fseek(file_strm, 0L, SEEK_END);
1144 length = ftell(file_strm);
1145 fseek(file_strm, 0L, start);
1151 crm_info(
"%s was not valid", metadata_file);
1157 crm_trace(
"Reading %d bytes from file", length);
1158 *output = calloc(1, (length + 1));
1159 read_len = fread(*output, 1, length, file_strm);
1160 if (read_len != length) {
1161 crm_err(
"Calculated and read bytes differ: %d vs. %d",
1170 free(metadata_file);
1175 #if SUPPORT_HEARTBEAT 1191 static const char hb_metadata_template[] =
1192 "<?xml version='1.0'?>\n" 1193 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" 1195 "<version>1.0</version>\n" 1196 "<longdesc lang='en'>\n" 1199 "<shortdesc lang='en'>%s</shortdesc>\n" 1201 "<parameter name='1' unique='1' required='0'>\n" 1202 "<longdesc lang='en'>\n" 1203 "This argument will be passed as the first argument to the " 1204 "heartbeat resource agent (assuming it supports one)\n" 1206 "<shortdesc lang='en'>argv[1]</shortdesc>\n" 1207 "<content type='string' default=' ' />\n" 1209 "<parameter name='2' unique='1' required='0'>\n" 1210 "<longdesc lang='en'>\n" 1211 "This argument will be passed as the second argument to the " 1212 "heartbeat resource agent (assuming it supports one)\n" 1214 "<shortdesc lang='en'>argv[2]</shortdesc>\n" 1215 "<content type='string' default=' ' />\n" 1217 "<parameter name='3' unique='1' required='0'>\n" 1218 "<longdesc lang='en'>\n" 1219 "This argument will be passed as the third argument to the " 1220 "heartbeat resource agent (assuming it supports one)\n" 1222 "<shortdesc lang='en'>argv[3]</shortdesc>\n" 1223 "<content type='string' default=' ' />\n" 1225 "<parameter name='4' unique='1' required='0'>\n" 1226 "<longdesc lang='en'>\n" 1227 "This argument will be passed as the fourth argument to the " 1228 "heartbeat resource agent (assuming it supports one)\n" 1230 "<shortdesc lang='en'>argv[4]</shortdesc>\n" 1231 "<content type='string' default=' ' />\n" 1233 "<parameter name='5' unique='1' required='0'>\n" 1234 "<longdesc lang='en'>\n" 1235 "This argument will be passed as the fifth argument to the " 1236 "heartbeat resource agent (assuming it supports one)\n" 1238 "<shortdesc lang='en'>argv[5]</shortdesc>\n" 1239 "<content type='string' default=' ' />\n" 1243 "<action name='start' timeout='15' />\n" 1244 "<action name='stop' timeout='15' />\n" 1245 "<action name='status' timeout='15' />\n" 1246 "<action name='monitor' timeout='15' interval='15' start-delay='15' />\n" 1247 "<action name='meta-data' timeout='5' />\n" 1249 "<special tag='heartbeat'>\n" 1251 "</resource-agent>\n";
1254 heartbeat_get_metadata(
const char *type,
char **output)
1257 crm_trace(
"Created fake metadata: %llu",
1258 (
unsigned long long) strlen(*output));
1266 const char *
class = op->standard;
1268 if (op->
agent == NULL) {
1269 crm_err(
"meta-data requested without specifying agent");
1273 if (
class == NULL) {
1274 crm_err(
"meta-data requested for agent %s without specifying class",
1283 if (
class == NULL) {
1284 crm_err(
"meta-data requested for %s, but could not determine class",
1299 #if SUPPORT_HEARTBEAT 1305 return action_exec_helper(op);
1328 rc = action_get_metadata(op);
1330 rc = action_exec_helper(op);
1355 #if SUPPORT_HEARTBEAT 1357 resources_os_list_hb_agents(
void)
1366 GList *standards = NULL;
1367 GList *agents = NULL;
1376 standards = g_list_append(standards,
1378 g_list_free_full(agents, free);
1385 standards = g_list_append(standards,
1387 g_list_free_full(agents, free);
1394 standards = g_list_append(standards,
1396 g_list_free_full(agents, free);
1400 #if SUPPORT_HEARTBEAT 1420 if ((standard == NULL)
1427 if (standard == NULL) {
1431 result = g_list_concat(tmp1, tmp2);
1438 result = g_list_concat(tmp1, tmp2);
1446 result = g_list_concat(tmp1, tmp2);
1456 #if SUPPORT_HEARTBEAT 1458 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 services_action_async_fork_notify(svc_action_t *op, void(*action_callback)(svc_action_t *), void(*action_fork_callback)(svc_action_t *))
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...)
gboolean services_alert_async(svc_action_t *action, void(*cb)(svc_action_t *op))
Execute an alert agent action.
uint32_t pcmk_get_ra_caps(const char *standard)
Get capabilities of a resource agent standard.
#define LSB_INITSCRIPT_INFOBEGIN_TAG
gboolean services_action_sync(svc_action_t *op)
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)
Find first service class that can provide a specified 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 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)
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)
void(* fork_callback)(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)