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);
83 if (rc > 0 && stat(path, &st) == 0) {
105 init_recurring_actions(
void)
107 if (recurring_actions == NULL) {
108 recurring_actions = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
121 static inline gboolean
126 && (g_list_find(inflight_ops, op) != NULL);
142 expand_resource_class(
const char *rsc,
const char *standard,
const char *agent)
144 char *expanded_class = NULL;
150 crm_debug(
"Found %s agent %s for %s", found_class, agent, rsc);
151 expanded_class = strdup(found_class);
153 crm_info(
"Assuming resource class lsb for agent %s for %s",
158 expanded_class = strdup(standard);
161 return expanded_class;
166 const char *agent,
const char *action,
int interval,
int timeout,
176 if (crm_strlen_zero(name)) {
177 crm_err(
"Cannot create operation without resource name");
181 if (crm_strlen_zero(standard)) {
182 crm_err(
"Cannot create operation for %s without resource class", name);
187 crm_err(
"Cannot create OCF operation for %s without provider", name);
191 if (crm_strlen_zero(agent)) {
192 crm_err(
"Cannot create operation for %s without agent name", name);
196 if (crm_strlen_zero(action)) {
197 crm_err(
"Cannot create operation for %s without operation name", name);
207 op->
rsc = strdup(name);
210 op->
standard = expand_resource_class(name, standard, agent);
211 op->
agent = strdup(agent);
223 op->
action = strdup(action);
231 crm_err(
"Internal error: cannot create agent path");
238 if (op->
agent[0] ==
'/') {
243 crm_err(
"Internal error: cannot create agent path");
249 #if SUPPORT_HEARTBEAT 256 if (op->
agent[0] ==
'/') {
260 }
else if (asprintf(&op->
opaque->
exec,
"%s/%s", HB_RA_DIR, op->
agent) == -1) {
261 crm_err(
"Internal error: cannot create agent path");
270 for (index = 1; index <=
MAX_ARGC - 3; index++ ) {
271 snprintf(buf_tmp,
sizeof(buf_tmp),
"%d", index);
272 value_tmp = g_hash_table_lookup(params, buf_tmp);
273 if (value_tmp == NULL) {
278 op->
opaque->
args[param_num++] = strdup(value_tmp);
299 if (op->
agent[0] ==
'/') {
305 crm_err(
"Internal error: cannot create agent path");
314 op->
opaque->
args[index] = strdup(
"--version");
321 static int args_size =
sizeof(op->
opaque->
args) /
sizeof(
char *);
323 g_hash_table_iter_init(&iter, params);
325 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value) &&
326 index <= args_size - 3) {
328 char *long_opt = NULL;
335 long_opt = calloc(1, len);
336 sprintf(long_opt,
"--%s", key);
337 long_opt[len - 1] = 0;
353 g_hash_table_destroy(params);
359 g_hash_table_destroy(params);
370 unsigned int cur_arg;
372 op = calloc(1,
sizeof(*op));
378 for (cur_arg = 1; args && args[cur_arg - 1]; cur_arg++) {
379 op->
opaque->
args[cur_arg] = strdup(args[cur_arg - 1]);
382 crm_err(
"svc_action_t args list not long enough for '%s' execution request.", exec);
406 GHashTable *params,
int sequence,
void *cb_data)
412 action->
id = strdup(
id);
437 CRM_CHECK((op != NULL) && (user != NULL),
return -EINVAL);
469 services_set_op_pending(
svc_action_t *op, DBusPendingCall *pending)
471 if (op->
opaque->pending && (op->
opaque->pending != pending)) {
477 dbus_pending_call_unref(op->
opaque->pending);
479 op->
opaque->pending = pending;
481 crm_trace(
"Updated pending %s DBus call (%p)", op->
id, pending);
496 if(op->
opaque->timerid != 0) {
498 g_source_remove(op->
opaque->timerid);
504 if(dbus_pending_call_get_completed(op->
opaque->pending)) {
507 dbus_pending_call_cancel(op->
opaque->pending);
508 dbus_pending_call_unref(op->
opaque->pending);
509 op->
opaque->pending = NULL;
537 CRM_CHECK(g_list_find(inflight_ops, op) == NULL,
return);
538 CRM_CHECK(g_list_find(blocked_ops, op) == NULL,
return);
540 || (g_hash_table_lookup(recurring_actions, op->
id) == NULL),
569 g_hash_table_destroy(op->
params);
581 if (recurring_actions) {
582 g_hash_table_remove(recurring_actions, op->
id);
605 gboolean cancelled = FALSE;
610 init_recurring_actions();
611 op = g_hash_table_lookup(recurring_actions,
id);
629 crm_info(
"Terminating in-flight op %s (pid %d) early because it was cancelled",
632 if (cancelled == FALSE) {
633 crm_err(
"Termination of %s (pid %d) failed",
id, op->
pid);
643 if (inflight_systemd_or_upstart(op)) {
644 crm_info(
"Will cancel %s op %s when in-flight instance completes",
656 blocked_ops = g_list_remove(blocked_ops, op);
671 init_recurring_actions();
672 op = g_hash_table_lookup(recurring_actions,
id);
680 if (op->
pid || inflight_systemd_or_upstart(op)) {
707 dup = g_hash_table_lookup(recurring_actions, op->
id);
709 if (dup && (dup != op)) {
732 inline static gboolean
767 inflight_ops = g_list_append(inflight_ops, op);
781 inflight_ops = g_list_remove(inflight_ops, op);
782 blocked_ops = g_list_remove(blocked_ops, op);
785 handle_blocked_ops();
792 if (action_callback) {
797 init_recurring_actions();
798 if (handle_duplicate_recurring(op) == TRUE) {
803 g_hash_table_replace(recurring_actions, op->
id, op);
807 blocked_ops = g_list_append(blocked_ops, op);
811 return action_exec_helper(op);
815 static gboolean processing_blocked_ops = FALSE;
823 for (gIter = inflight_ops; gIter != NULL; gIter = gIter->next) {
834 handle_blocked_ops(
void)
836 GList *executed_ops = NULL;
839 gboolean res = FALSE;
841 if (processing_blocked_ops) {
846 processing_blocked_ops = TRUE;
850 for (gIter = blocked_ops; gIter != NULL; gIter = gIter->next) {
855 executed_ops = g_list_append(executed_ops, op);
856 res = action_exec_helper(op);
865 for (gIter = executed_ops; gIter != NULL; gIter = gIter->next) {
867 blocked_ops = g_list_remove(blocked_ops, op);
869 g_list_free(executed_ops);
871 processing_blocked_ops = FALSE;
874 #define lsb_metadata_template \ 875 "<?xml version='1.0'?>\n" \ 876 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" \ 877 "<resource-agent name='%s' version='" PCMK_DEFAULT_AGENT_VERSION "'>\n" \ 878 " <version>1.0</version>\n" \ 879 " <longdesc lang='en'>\n" \ 882 " <shortdesc lang='en'>%s</shortdesc>\n" \ 886 " <action name='meta-data' timeout='5' />\n" \ 887 " <action name='start' timeout='15' />\n" \ 888 " <action name='stop' timeout='15' />\n" \ 889 " <action name='status' timeout='15' />\n" \ 890 " <action name='restart' timeout='15' />\n" \ 891 " <action name='force-reload' timeout='15' />\n" \ 892 " <action name='monitor' timeout='15' interval='15' />\n" \ 894 " <special tag='LSB'>\n" \ 895 " <Provides>%s</Provides>\n" \ 896 " <Required-Start>%s</Required-Start>\n" \ 897 " <Required-Stop>%s</Required-Stop>\n" \ 898 " <Should-Start>%s</Should-Start>\n" \ 899 " <Should-Stop>%s</Should-Stop>\n" \ 900 " <Default-Start>%s</Default-Start>\n" \ 901 " <Default-Stop>%s</Default-Stop>\n" \ 903 "</resource-agent>\n" 908 #define LSB_INITSCRIPT_INFOBEGIN_TAG "### BEGIN INIT INFO" 909 #define LSB_INITSCRIPT_INFOEND_TAG "### END INIT INFO" 910 #define PROVIDES "# Provides:" 911 #define REQ_START "# Required-Start:" 912 #define REQ_STOP "# Required-Stop:" 913 #define SHLD_START "# Should-Start:" 914 #define SHLD_STOP "# Should-Stop:" 915 #define DFLT_START "# Default-Start:" 916 #define DFLT_STOP "# Default-Stop:" 917 #define SHORT_DSCR "# Short-Description:" 918 #define DESCRIPTION "# Description:" 920 #define lsb_meta_helper_free_value(m) \ 938 static inline gboolean
939 lsb_meta_helper_get_value(
const char *line,
char **value,
const char *prefix)
942 *value = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST line+strlen(prefix));
948 #define DESC_MAX 2048 951 lsb_get_metadata(
const char *
type,
char **output)
953 char ra_pathname[PATH_MAX] = { 0, };
955 char buffer[1024] = { 0, };
956 char *provides = NULL;
957 char *req_start = NULL;
958 char *req_stop = NULL;
959 char *shld_start = NULL;
960 char *shld_stop = NULL;
961 char *dflt_start = NULL;
962 char *dflt_stop = NULL;
963 char *s_dscrpt = NULL;
964 char *xml_l_dscrpt = NULL;
966 bool in_header = FALSE;
967 char description[
DESC_MAX] = { 0, };
969 if (type[0] ==
'/') {
970 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s", type);
972 snprintf(ra_pathname,
sizeof(ra_pathname),
"%s/%s",
976 crm_trace(
"Looking into %s", ra_pathname);
977 fp = fopen(ra_pathname,
"r");
983 while (fgets(buffer,
sizeof(buffer), fp)) {
995 if (lsb_meta_helper_get_value(buffer, &provides,
PROVIDES)) {
998 if (lsb_meta_helper_get_value(buffer, &req_start,
REQ_START)) {
1001 if (lsb_meta_helper_get_value(buffer, &req_stop,
REQ_STOP)) {
1004 if (lsb_meta_helper_get_value(buffer, &shld_start,
SHLD_START)) {
1007 if (lsb_meta_helper_get_value(buffer, &shld_stop,
SHLD_STOP)) {
1010 if (lsb_meta_helper_get_value(buffer, &dflt_start,
DFLT_START)) {
1013 if (lsb_meta_helper_get_value(buffer, &dflt_stop,
DFLT_STOP)) {
1016 if (lsb_meta_helper_get_value(buffer, &s_dscrpt,
SHORT_DSCR)) {
1023 bool processed_line = TRUE;
1026 offset += snprintf(description,
DESC_MAX,
"%s",
1031 while (fgets(buffer,
sizeof(buffer), fp)) {
1037 offset += snprintf(description + offset,
DESC_MAX - offset,
1043 processed_line = FALSE;
1049 xml_l_dscrpt = (
char *)xmlEncodeEntitiesReentrant(NULL, BAD_CAST(description));
1051 if (processed_line) {
1061 if (buffer[0] !=
'#') {
1068 (xml_l_dscrpt? xml_l_dscrpt : type),
1069 (s_dscrpt? s_dscrpt : type),
1070 (provides? provides :
""),
1071 (req_start? req_start :
""),
1072 (req_stop? req_stop :
""),
1073 (shld_start? shld_start :
""),
1074 (shld_stop? shld_stop :
""),
1075 (dflt_start? dflt_start :
""),
1076 (dflt_stop? dflt_stop :
""));
1088 crm_trace(
"Created fake metadata: %llu",
1089 (
unsigned long long) strlen(*output));
1095 nagios_get_metadata(
const char *
type,
char **output)
1098 FILE *file_strm = NULL;
1099 int start = 0, length = 0, read_len = 0;
1103 file_strm = fopen(metadata_file,
"r");
1104 if (file_strm == NULL) {
1105 crm_err(
"Metadata file %s does not exist", metadata_file);
1106 free(metadata_file);
1111 start = ftell(file_strm);
1112 fseek(file_strm, 0L, SEEK_END);
1113 length = ftell(file_strm);
1114 fseek(file_strm, 0L, start);
1120 crm_info(
"%s was not valid", metadata_file);
1126 crm_trace(
"Reading %d bytes from file", length);
1127 *output = calloc(1, (length + 1));
1128 read_len = fread(*output, 1, length, file_strm);
1129 if (read_len != length) {
1130 crm_err(
"Calculated and read bytes differ: %d vs. %d",
1139 free(metadata_file);
1144 #if SUPPORT_HEARTBEAT 1160 static const char hb_metadata_template[] =
1161 "<?xml version='1.0'?>\n" 1162 "<!DOCTYPE resource-agent SYSTEM 'ra-api-1.dtd'>\n" 1164 "<version>1.0</version>\n" 1165 "<longdesc lang='en'>\n" 1168 "<shortdesc lang='en'>%s</shortdesc>\n" 1170 "<parameter name='1' unique='1' required='0'>\n" 1171 "<longdesc lang='en'>\n" 1172 "This argument will be passed as the first argument to the " 1173 "heartbeat resource agent (assuming it supports one)\n" 1175 "<shortdesc lang='en'>argv[1]</shortdesc>\n" 1176 "<content type='string' default=' ' />\n" 1178 "<parameter name='2' unique='1' required='0'>\n" 1179 "<longdesc lang='en'>\n" 1180 "This argument will be passed as the second argument to the " 1181 "heartbeat resource agent (assuming it supports one)\n" 1183 "<shortdesc lang='en'>argv[2]</shortdesc>\n" 1184 "<content type='string' default=' ' />\n" 1186 "<parameter name='3' unique='1' required='0'>\n" 1187 "<longdesc lang='en'>\n" 1188 "This argument will be passed as the third argument to the " 1189 "heartbeat resource agent (assuming it supports one)\n" 1191 "<shortdesc lang='en'>argv[3]</shortdesc>\n" 1192 "<content type='string' default=' ' />\n" 1194 "<parameter name='4' unique='1' required='0'>\n" 1195 "<longdesc lang='en'>\n" 1196 "This argument will be passed as the fourth argument to the " 1197 "heartbeat resource agent (assuming it supports one)\n" 1199 "<shortdesc lang='en'>argv[4]</shortdesc>\n" 1200 "<content type='string' default=' ' />\n" 1202 "<parameter name='5' unique='1' required='0'>\n" 1203 "<longdesc lang='en'>\n" 1204 "This argument will be passed as the fifth argument to the " 1205 "heartbeat resource agent (assuming it supports one)\n" 1207 "<shortdesc lang='en'>argv[5]</shortdesc>\n" 1208 "<content type='string' default=' ' />\n" 1212 "<action name='start' timeout='15' />\n" 1213 "<action name='stop' timeout='15' />\n" 1214 "<action name='status' timeout='15' />\n" 1215 "<action name='monitor' timeout='15' interval='15' start-delay='15' />\n" 1216 "<action name='meta-data' timeout='5' />\n" 1218 "<special tag='heartbeat'>\n" 1220 "</resource-agent>\n";
1223 heartbeat_get_metadata(
const char *type,
char **output)
1226 crm_trace(
"Created fake metadata: %llu",
1227 (
unsigned long long) strlen(*output));
1235 const char *
class = op->standard;
1237 if (op->
agent == NULL) {
1238 crm_err(
"meta-data requested without specifying agent");
1242 if (
class == NULL) {
1243 crm_err(
"meta-data requested for agent %s without specifying class",
1252 if (
class == NULL) {
1253 crm_err(
"meta-data requested for %s, but could not determine class",
1268 #if SUPPORT_HEARTBEAT 1274 return action_exec_helper(op);
1297 rc = action_get_metadata(op);
1299 rc = action_exec_helper(op);
1324 #if SUPPORT_HEARTBEAT 1326 resources_os_list_hb_agents(
void)
1335 GList *standards = NULL;
1336 GList *agents = NULL;
1345 standards = g_list_append(standards,
1347 g_list_free_full(agents, free);
1354 standards = g_list_append(standards,
1356 g_list_free_full(agents, free);
1363 standards = g_list_append(standards,
1365 g_list_free_full(agents, free);
1369 #if SUPPORT_HEARTBEAT 1389 if ((standard == NULL)
1396 if (standard == NULL) {
1400 result = g_list_concat(tmp1, tmp2);
1407 result = g_list_concat(tmp1, tmp2);
1415 result = g_list_concat(tmp1, tmp2);
1425 #if SUPPORT_HEARTBEAT 1427 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...)
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)
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)
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)