This source file includes following definitions.
- time_is_set
- get_current_time
- time_diff_ms
- cmd_original_times
- action_matches
- log_finished
- log_execute
- normalize_action_name
- build_rsc_from_xml
- create_lrmd_cmd
- stop_recurring_timer
- free_lrmd_cmd
- stonith_recurring_op_helper
- start_recurring_timer
- start_delay_helper
- find_duplicate_action
- merge_recurring_duplicate
- schedule_lrmd_cmd
- create_lrmd_reply
- send_client_notify
- send_cmd_complete_notify
- send_generic_notify
- cmd_reset
- cmd_finalize
- notify_one_client
- notify_of_new_client
- client_disconnect_cleanup
- action_complete
- stonith_action_complete
- lrmd_stonith_callback
- stonith_connection_failed
- execd_stonith_start
- execd_stonith_stop
- execd_stonith_monitor
- execute_stonith_action
- execute_nonstonith_action
- execute_resource_action
- free_rsc
- process_lrmd_signon
- process_lrmd_rsc_register
- process_lrmd_get_rsc_info
- process_lrmd_rsc_unregister
- process_lrmd_rsc_exec
- cancel_op
- cancel_all_recurring
- process_lrmd_rsc_cancel
- add_recurring_op_xml
- process_lrmd_get_recurring
- process_lrmd_message
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11 #include <crm/fencing/internal.h>
12
13 #include <glib.h>
14
15
16 #undef PCMK__TIME_USE_CGT
17 #if HAVE_DECL_CLOCK_MONOTONIC && defined(CLOCK_MONOTONIC)
18 # define PCMK__TIME_USE_CGT
19 # include <time.h>
20 #endif
21
22 #include <unistd.h>
23
24 #include <crm/crm.h>
25 #include <crm/fencing/internal.h>
26 #include <crm/services.h>
27 #include <crm/services_internal.h>
28 #include <crm/common/mainloop.h>
29 #include <crm/common/ipc.h>
30 #include <crm/common/ipc_internal.h>
31 #include <crm/msg_xml.h>
32
33 #include "pacemaker-execd.h"
34
35 GHashTable *rsc_list = NULL;
36
37 typedef struct lrmd_cmd_s {
38 int timeout;
39 guint interval_ms;
40 int start_delay;
41 int timeout_orig;
42
43 int call_id;
44
45 int call_opts;
46
47 int delay_id;
48 int stonith_recurring_id;
49
50 int rsc_deleted;
51
52 int service_flags;
53
54 char *client_id;
55 char *origin;
56 char *rsc_id;
57 char *action;
58 char *real_action;
59 char *userdata_str;
60
61 pcmk__action_result_t result;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 #ifdef PCMK__TIME_USE_CGT
80
81
82
83
84 struct timespec t_first_run;
85 struct timespec t_run;
86 struct timespec t_first_queue;
87 struct timespec t_queue;
88 #endif
89 time_t epoch_last_run;
90 time_t epoch_rcchange;
91
92 bool first_notify_sent;
93 int last_notify_rc;
94 int last_notify_op_status;
95 int last_pid;
96
97 GHashTable *params;
98 } lrmd_cmd_t;
99
100 static void cmd_finalize(lrmd_cmd_t * cmd, lrmd_rsc_t * rsc);
101 static gboolean execute_resource_action(gpointer user_data);
102 static void cancel_all_recurring(lrmd_rsc_t * rsc, const char *client_id);
103
104 #ifdef PCMK__TIME_USE_CGT
105
106
107
108
109
110
111
112
113
114 static inline bool
115 time_is_set(const struct timespec *timespec)
116 {
117 return (timespec != NULL) &&
118 ((timespec->tv_sec != 0) || (timespec->tv_nsec != 0));
119 }
120
121
122
123
124
125
126
127
128 static void
129 get_current_time(struct timespec *t_current, struct timespec *t_orig)
130 {
131 clock_gettime(CLOCK_MONOTONIC, t_current);
132 if ((t_orig != NULL) && !time_is_set(t_orig)) {
133 *t_orig = *t_current;
134 }
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 static int
150 time_diff_ms(const struct timespec *now, const struct timespec *old)
151 {
152 int diff_ms = 0;
153
154 if (time_is_set(old)) {
155 struct timespec local_now = { 0, };
156
157 if (now == NULL) {
158 clock_gettime(CLOCK_MONOTONIC, &local_now);
159 now = &local_now;
160 }
161 diff_ms = (now->tv_sec - old->tv_sec) * 1000
162 + (now->tv_nsec - old->tv_nsec) / 1000000;
163 }
164 return diff_ms;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 static void
188 cmd_original_times(lrmd_cmd_t * cmd)
189 {
190 cmd->t_run = cmd->t_first_run;
191 cmd->t_queue = cmd->t_first_queue;
192 }
193 #endif
194
195 static inline bool
196 action_matches(const lrmd_cmd_t *cmd, const char *action, guint interval_ms)
197 {
198 return (cmd->interval_ms == interval_ms)
199 && pcmk__str_eq(cmd->action, action, pcmk__str_casei);
200 }
201
202
203
204
205
206
207
208
209
210 static void
211 log_finished(const lrmd_cmd_t *cmd, int exec_time_ms, int queue_time_ms)
212 {
213 int log_level = LOG_INFO;
214 GString *str = g_string_sized_new(100);
215
216 if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)) {
217 log_level = LOG_DEBUG;
218 }
219
220 g_string_append_printf(str, "%s %s (call %d",
221 cmd->rsc_id, cmd->action, cmd->call_id);
222 if (cmd->last_pid != 0) {
223 g_string_append_printf(str, ", PID %d", cmd->last_pid);
224 }
225 if (cmd->result.execution_status == PCMK_EXEC_DONE) {
226 g_string_append_printf(str, ") exited with status %d",
227 cmd->result.exit_status);
228 } else {
229 pcmk__g_strcat(str, ") could not be executed: ",
230 pcmk_exec_status_str(cmd->result.execution_status),
231 NULL);
232 }
233 if (cmd->result.exit_reason != NULL) {
234 pcmk__g_strcat(str, " (", cmd->result.exit_reason, ")", NULL);
235 }
236
237 #ifdef PCMK__TIME_USE_CGT
238 pcmk__g_strcat(str, " (execution time ",
239 pcmk__readable_interval(exec_time_ms), NULL);
240 if (queue_time_ms > 0) {
241 pcmk__g_strcat(str, " after being queued ",
242 pcmk__readable_interval(queue_time_ms), NULL);
243 }
244 g_string_append_c(str, ')');
245 #endif
246
247 do_crm_log(log_level, "%s", str->str);
248 g_string_free(str, TRUE);
249 }
250
251 static void
252 log_execute(lrmd_cmd_t * cmd)
253 {
254 int log_level = LOG_INFO;
255
256 if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)) {
257 log_level = LOG_DEBUG;
258 }
259
260 do_crm_log(log_level, "executing - rsc:%s action:%s call_id:%d",
261 cmd->rsc_id, cmd->action, cmd->call_id);
262 }
263
264 static const char *
265 normalize_action_name(lrmd_rsc_t * rsc, const char *action)
266 {
267 if (pcmk__str_eq(action, PCMK_ACTION_MONITOR, pcmk__str_casei) &&
268 pcmk_is_set(pcmk_get_ra_caps(rsc->class), pcmk_ra_cap_status)) {
269 return PCMK_ACTION_STATUS;
270 }
271 return action;
272 }
273
274 static lrmd_rsc_t *
275 build_rsc_from_xml(xmlNode * msg)
276 {
277 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, msg, LOG_ERR);
278 lrmd_rsc_t *rsc = NULL;
279
280 rsc = calloc(1, sizeof(lrmd_rsc_t));
281
282 crm_element_value_int(msg, F_LRMD_CALLOPTS, &rsc->call_opts);
283
284 rsc->rsc_id = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ID);
285 rsc->class = crm_element_value_copy(rsc_xml, F_LRMD_CLASS);
286 rsc->provider = crm_element_value_copy(rsc_xml, F_LRMD_PROVIDER);
287 rsc->type = crm_element_value_copy(rsc_xml, F_LRMD_TYPE);
288 rsc->work = mainloop_add_trigger(G_PRIORITY_HIGH, execute_resource_action,
289 rsc);
290
291
292 pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
293 PCMK_EXEC_NO_FENCE_DEVICE, NULL);
294 return rsc;
295 }
296
297 static lrmd_cmd_t *
298 create_lrmd_cmd(xmlNode *msg, pcmk__client_t *client)
299 {
300 int call_options = 0;
301 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, msg, LOG_ERR);
302 lrmd_cmd_t *cmd = NULL;
303
304 cmd = calloc(1, sizeof(lrmd_cmd_t));
305
306 crm_element_value_int(msg, F_LRMD_CALLOPTS, &call_options);
307 cmd->call_opts = call_options;
308 cmd->client_id = strdup(client->id);
309
310 crm_element_value_int(msg, F_LRMD_CALLID, &cmd->call_id);
311 crm_element_value_ms(rsc_xml, F_LRMD_RSC_INTERVAL, &cmd->interval_ms);
312 crm_element_value_int(rsc_xml, F_LRMD_TIMEOUT, &cmd->timeout);
313 crm_element_value_int(rsc_xml, F_LRMD_RSC_START_DELAY, &cmd->start_delay);
314 cmd->timeout_orig = cmd->timeout;
315
316 cmd->origin = crm_element_value_copy(rsc_xml, F_LRMD_ORIGIN);
317 cmd->action = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ACTION);
318 cmd->userdata_str = crm_element_value_copy(rsc_xml, F_LRMD_RSC_USERDATA_STR);
319 cmd->rsc_id = crm_element_value_copy(rsc_xml, F_LRMD_RSC_ID);
320
321 cmd->params = xml2list(rsc_xml);
322
323 if (pcmk__str_eq(g_hash_table_lookup(cmd->params, "CRM_meta_on_fail"), "block", pcmk__str_casei)) {
324 crm_debug("Setting flag to leave pid group on timeout and "
325 "only kill action pid for " PCMK__OP_FMT,
326 cmd->rsc_id, cmd->action, cmd->interval_ms);
327 cmd->service_flags = pcmk__set_flags_as(__func__, __LINE__,
328 LOG_TRACE, "Action",
329 cmd->action, 0,
330 SVC_ACTION_LEAVE_GROUP,
331 "SVC_ACTION_LEAVE_GROUP");
332 }
333 return cmd;
334 }
335
336 static void
337 stop_recurring_timer(lrmd_cmd_t *cmd)
338 {
339 if (cmd) {
340 if (cmd->stonith_recurring_id) {
341 g_source_remove(cmd->stonith_recurring_id);
342 }
343 cmd->stonith_recurring_id = 0;
344 }
345 }
346
347 static void
348 free_lrmd_cmd(lrmd_cmd_t * cmd)
349 {
350 stop_recurring_timer(cmd);
351 if (cmd->delay_id) {
352 g_source_remove(cmd->delay_id);
353 }
354 if (cmd->params) {
355 g_hash_table_destroy(cmd->params);
356 }
357 pcmk__reset_result(&(cmd->result));
358 free(cmd->origin);
359 free(cmd->action);
360 free(cmd->real_action);
361 free(cmd->userdata_str);
362 free(cmd->rsc_id);
363 free(cmd->client_id);
364 free(cmd);
365 }
366
367 static gboolean
368 stonith_recurring_op_helper(gpointer data)
369 {
370 lrmd_cmd_t *cmd = data;
371 lrmd_rsc_t *rsc;
372
373 cmd->stonith_recurring_id = 0;
374
375 if (!cmd->rsc_id) {
376 return FALSE;
377 }
378
379 rsc = g_hash_table_lookup(rsc_list, cmd->rsc_id);
380
381 CRM_ASSERT(rsc != NULL);
382
383
384 rsc->recurring_ops = g_list_remove(rsc->recurring_ops, cmd);
385 rsc->pending_ops = g_list_append(rsc->pending_ops, cmd);
386 #ifdef PCMK__TIME_USE_CGT
387 get_current_time(&(cmd->t_queue), &(cmd->t_first_queue));
388 #endif
389 mainloop_set_trigger(rsc->work);
390
391 return FALSE;
392 }
393
394 static inline void
395 start_recurring_timer(lrmd_cmd_t *cmd)
396 {
397 if (cmd && (cmd->interval_ms > 0)) {
398 cmd->stonith_recurring_id = g_timeout_add(cmd->interval_ms,
399 stonith_recurring_op_helper,
400 cmd);
401 }
402 }
403
404 static gboolean
405 start_delay_helper(gpointer data)
406 {
407 lrmd_cmd_t *cmd = data;
408 lrmd_rsc_t *rsc = NULL;
409
410 cmd->delay_id = 0;
411 rsc = cmd->rsc_id ? g_hash_table_lookup(rsc_list, cmd->rsc_id) : NULL;
412
413 if (rsc) {
414 mainloop_set_trigger(rsc->work);
415 }
416
417 return FALSE;
418 }
419
420
421
422
423
424
425
426
427 static lrmd_cmd_t *
428 find_duplicate_action(const GList *action_list, const lrmd_cmd_t *cmd)
429 {
430 for (const GList *item = action_list; item != NULL; item = item->next) {
431 lrmd_cmd_t *dup = item->data;
432
433 if (action_matches(cmd, dup->action, dup->interval_ms)) {
434 return dup;
435 }
436 }
437 return NULL;
438 }
439
440 static bool
441 merge_recurring_duplicate(lrmd_rsc_t * rsc, lrmd_cmd_t * cmd)
442 {
443 lrmd_cmd_t * dup = NULL;
444 bool dup_pending = true;
445
446 if (cmd->interval_ms == 0) {
447 return false;
448 }
449
450
451 dup = find_duplicate_action(rsc->pending_ops, cmd);
452 if (dup == NULL) {
453 dup_pending = false;
454 dup = find_duplicate_action(rsc->recurring_ops, cmd);
455 if (dup == NULL) {
456 return false;
457 }
458 }
459
460
461
462
463 if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
464 pcmk__str_casei)
465 && (dup->result.execution_status == PCMK_EXEC_CANCELLED)) {
466 return false;
467 }
468
469
470
471
472 crm_warn("Duplicate recurring op entry detected (" PCMK__OP_FMT
473 "), merging with previous op entry",
474 rsc->rsc_id, normalize_action_name(rsc, dup->action),
475 dup->interval_ms);
476
477
478 dup->first_notify_sent = false;
479 free(dup->userdata_str);
480 dup->userdata_str = cmd->userdata_str;
481 cmd->userdata_str = NULL;
482 dup->call_id = cmd->call_id;
483 free_lrmd_cmd(cmd);
484 cmd = NULL;
485
486
487
488
489
490 if (!dup_pending) {
491 if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
492 pcmk__str_casei)) {
493 stop_recurring_timer(dup);
494 stonith_recurring_op_helper(dup);
495 } else {
496 services_action_kick(rsc->rsc_id,
497 normalize_action_name(rsc, dup->action),
498 dup->interval_ms);
499 }
500 }
501 return true;
502 }
503
504 static void
505 schedule_lrmd_cmd(lrmd_rsc_t * rsc, lrmd_cmd_t * cmd)
506 {
507 CRM_CHECK(cmd != NULL, return);
508 CRM_CHECK(rsc != NULL, return);
509
510 crm_trace("Scheduling %s on %s", cmd->action, rsc->rsc_id);
511
512 if (merge_recurring_duplicate(rsc, cmd)) {
513
514 return;
515 }
516
517
518
519
520 if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
521 cancel_all_recurring(rsc, NULL);
522 }
523
524 rsc->pending_ops = g_list_append(rsc->pending_ops, cmd);
525 #ifdef PCMK__TIME_USE_CGT
526 get_current_time(&(cmd->t_queue), &(cmd->t_first_queue));
527 #endif
528 mainloop_set_trigger(rsc->work);
529
530 if (cmd->start_delay) {
531 cmd->delay_id = g_timeout_add(cmd->start_delay, start_delay_helper, cmd);
532 }
533 }
534
535 static xmlNode *
536 create_lrmd_reply(const char *origin, int rc, int call_id)
537 {
538 xmlNode *reply = create_xml_node(NULL, T_LRMD_REPLY);
539
540 crm_xml_add(reply, F_LRMD_ORIGIN, origin);
541 crm_xml_add_int(reply, F_LRMD_RC, rc);
542 crm_xml_add_int(reply, F_LRMD_CALLID, call_id);
543 return reply;
544 }
545
546 static void
547 send_client_notify(gpointer key, gpointer value, gpointer user_data)
548 {
549 xmlNode *update_msg = user_data;
550 pcmk__client_t *client = value;
551 int rc;
552 int log_level = LOG_WARNING;
553 const char *msg = NULL;
554
555 CRM_CHECK(client != NULL, return);
556 if (client->name == NULL) {
557 crm_trace("Skipping notification to client without name");
558 return;
559 }
560 if (pcmk_is_set(client->flags, pcmk__client_to_proxy)) {
561
562
563
564
565 crm_trace("Skipping executor API notification to client %s",
566 pcmk__client_name(client));
567 return;
568 }
569
570 rc = lrmd_server_send_notify(client, update_msg);
571 if (rc == pcmk_rc_ok) {
572 return;
573 }
574
575 switch (rc) {
576 case ENOTCONN:
577 case EPIPE:
578 log_level = LOG_INFO;
579 msg = "Disconnected";
580 break;
581
582 default:
583 msg = pcmk_rc_str(rc);
584 break;
585 }
586 do_crm_log(log_level, "Could not notify client %s: %s " CRM_XS " rc=%d",
587 pcmk__client_name(client), msg, rc);
588 }
589
590 static void
591 send_cmd_complete_notify(lrmd_cmd_t * cmd)
592 {
593 xmlNode *notify = NULL;
594 int exec_time = 0;
595 int queue_time = 0;
596
597 #ifdef PCMK__TIME_USE_CGT
598 exec_time = time_diff_ms(NULL, &(cmd->t_run));
599 queue_time = time_diff_ms(&cmd->t_run, &(cmd->t_queue));
600 #endif
601 log_finished(cmd, exec_time, queue_time);
602
603
604
605
606 if (cmd->first_notify_sent
607 && pcmk_is_set(cmd->call_opts, lrmd_opt_notify_changes_only)
608 && (cmd->last_notify_rc == cmd->result.exit_status)
609 && (cmd->last_notify_op_status == cmd->result.execution_status)) {
610 return;
611 }
612
613 cmd->first_notify_sent = true;
614 cmd->last_notify_rc = cmd->result.exit_status;
615 cmd->last_notify_op_status = cmd->result.execution_status;
616
617 notify = create_xml_node(NULL, T_LRMD_NOTIFY);
618
619 crm_xml_add(notify, F_LRMD_ORIGIN, __func__);
620 crm_xml_add_int(notify, F_LRMD_TIMEOUT, cmd->timeout);
621 crm_xml_add_ms(notify, F_LRMD_RSC_INTERVAL, cmd->interval_ms);
622 crm_xml_add_int(notify, F_LRMD_RSC_START_DELAY, cmd->start_delay);
623 crm_xml_add_int(notify, F_LRMD_EXEC_RC, cmd->result.exit_status);
624 crm_xml_add_int(notify, F_LRMD_OP_STATUS, cmd->result.execution_status);
625 crm_xml_add_int(notify, F_LRMD_CALLID, cmd->call_id);
626 crm_xml_add_int(notify, F_LRMD_RSC_DELETED, cmd->rsc_deleted);
627
628 crm_xml_add_ll(notify, F_LRMD_RSC_RUN_TIME,
629 (long long) cmd->epoch_last_run);
630 crm_xml_add_ll(notify, F_LRMD_RSC_RCCHANGE_TIME,
631 (long long) cmd->epoch_rcchange);
632 #ifdef PCMK__TIME_USE_CGT
633 crm_xml_add_int(notify, F_LRMD_RSC_EXEC_TIME, exec_time);
634 crm_xml_add_int(notify, F_LRMD_RSC_QUEUE_TIME, queue_time);
635 #endif
636
637 crm_xml_add(notify, F_LRMD_OPERATION, LRMD_OP_RSC_EXEC);
638 crm_xml_add(notify, F_LRMD_RSC_ID, cmd->rsc_id);
639 if(cmd->real_action) {
640 crm_xml_add(notify, F_LRMD_RSC_ACTION, cmd->real_action);
641 } else {
642 crm_xml_add(notify, F_LRMD_RSC_ACTION, cmd->action);
643 }
644 crm_xml_add(notify, F_LRMD_RSC_USERDATA_STR, cmd->userdata_str);
645 crm_xml_add(notify, F_LRMD_RSC_EXIT_REASON, cmd->result.exit_reason);
646
647 if (cmd->result.action_stderr != NULL) {
648 crm_xml_add(notify, F_LRMD_RSC_OUTPUT, cmd->result.action_stderr);
649
650 } else if (cmd->result.action_stdout != NULL) {
651 crm_xml_add(notify, F_LRMD_RSC_OUTPUT, cmd->result.action_stdout);
652 }
653
654 if (cmd->params) {
655 char *key = NULL;
656 char *value = NULL;
657 GHashTableIter iter;
658
659 xmlNode *args = create_xml_node(notify, XML_TAG_ATTRS);
660
661 g_hash_table_iter_init(&iter, cmd->params);
662 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
663 hash2smartfield((gpointer) key, (gpointer) value, args);
664 }
665 }
666 if ((cmd->client_id != NULL)
667 && pcmk_is_set(cmd->call_opts, lrmd_opt_notify_orig_only)) {
668
669 pcmk__client_t *client = pcmk__find_client_by_id(cmd->client_id);
670
671 if (client != NULL) {
672 send_client_notify(client->id, client, notify);
673 }
674 } else {
675 pcmk__foreach_ipc_client(send_client_notify, notify);
676 }
677
678 free_xml(notify);
679 }
680
681 static void
682 send_generic_notify(int rc, xmlNode * request)
683 {
684 if (pcmk__ipc_client_count() != 0) {
685 int call_id = 0;
686 xmlNode *notify = NULL;
687 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
688 const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
689 const char *op = crm_element_value(request, F_LRMD_OPERATION);
690
691 crm_element_value_int(request, F_LRMD_CALLID, &call_id);
692
693 notify = create_xml_node(NULL, T_LRMD_NOTIFY);
694 crm_xml_add(notify, F_LRMD_ORIGIN, __func__);
695 crm_xml_add_int(notify, F_LRMD_RC, rc);
696 crm_xml_add_int(notify, F_LRMD_CALLID, call_id);
697 crm_xml_add(notify, F_LRMD_OPERATION, op);
698 crm_xml_add(notify, F_LRMD_RSC_ID, rsc_id);
699
700 pcmk__foreach_ipc_client(send_client_notify, notify);
701
702 free_xml(notify);
703 }
704 }
705
706 static void
707 cmd_reset(lrmd_cmd_t * cmd)
708 {
709 cmd->last_pid = 0;
710 #ifdef PCMK__TIME_USE_CGT
711 memset(&cmd->t_run, 0, sizeof(cmd->t_run));
712 memset(&cmd->t_queue, 0, sizeof(cmd->t_queue));
713 #endif
714 cmd->epoch_last_run = 0;
715
716 pcmk__reset_result(&(cmd->result));
717 cmd->result.execution_status = PCMK_EXEC_DONE;
718 }
719
720 static void
721 cmd_finalize(lrmd_cmd_t * cmd, lrmd_rsc_t * rsc)
722 {
723 crm_trace("Resource operation rsc:%s action:%s completed (%p %p)", cmd->rsc_id, cmd->action,
724 rsc ? rsc->active : NULL, cmd);
725
726 if (rsc && (rsc->active == cmd)) {
727 rsc->active = NULL;
728 mainloop_set_trigger(rsc->work);
729 }
730
731 if (!rsc) {
732 cmd->rsc_deleted = 1;
733 }
734
735
736 cmd->timeout = cmd->timeout_orig;
737
738 send_cmd_complete_notify(cmd);
739
740 if ((cmd->interval_ms != 0)
741 && (cmd->result.execution_status == PCMK_EXEC_CANCELLED)) {
742
743 if (rsc) {
744 rsc->recurring_ops = g_list_remove(rsc->recurring_ops, cmd);
745 rsc->pending_ops = g_list_remove(rsc->pending_ops, cmd);
746 }
747 free_lrmd_cmd(cmd);
748 } else if (cmd->interval_ms == 0) {
749 if (rsc) {
750 rsc->pending_ops = g_list_remove(rsc->pending_ops, cmd);
751 }
752 free_lrmd_cmd(cmd);
753 } else {
754
755 cmd_reset(cmd);
756 }
757 }
758
759 struct notify_new_client_data {
760 xmlNode *notify;
761 pcmk__client_t *new_client;
762 };
763
764 static void
765 notify_one_client(gpointer key, gpointer value, gpointer user_data)
766 {
767 pcmk__client_t *client = value;
768 struct notify_new_client_data *data = user_data;
769
770 if (!pcmk__str_eq(client->id, data->new_client->id, pcmk__str_casei)) {
771 send_client_notify(key, (gpointer) client, (gpointer) data->notify);
772 }
773 }
774
775 void
776 notify_of_new_client(pcmk__client_t *new_client)
777 {
778 struct notify_new_client_data data;
779
780 data.new_client = new_client;
781 data.notify = create_xml_node(NULL, T_LRMD_NOTIFY);
782 crm_xml_add(data.notify, F_LRMD_ORIGIN, __func__);
783 crm_xml_add(data.notify, F_LRMD_OPERATION, LRMD_OP_NEW_CLIENT);
784 pcmk__foreach_ipc_client(notify_one_client, &data);
785 free_xml(data.notify);
786 }
787
788 void
789 client_disconnect_cleanup(const char *client_id)
790 {
791 GHashTableIter iter;
792 lrmd_rsc_t *rsc = NULL;
793 char *key = NULL;
794
795 g_hash_table_iter_init(&iter, rsc_list);
796 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & rsc)) {
797 if (pcmk_all_flags_set(rsc->call_opts, lrmd_opt_drop_recurring)) {
798
799
800 cancel_all_recurring(rsc, client_id);
801 }
802 }
803 }
804
805 static void
806 action_complete(svc_action_t * action)
807 {
808 lrmd_rsc_t *rsc;
809 lrmd_cmd_t *cmd = action->cb_data;
810 enum ocf_exitcode code;
811
812 #ifdef PCMK__TIME_USE_CGT
813 const char *rclass = NULL;
814 bool goagain = false;
815 #endif
816
817 if (!cmd) {
818 crm_err("Completed executor action (%s) does not match any known operations",
819 action->id);
820 return;
821 }
822
823 #ifdef PCMK__TIME_USE_CGT
824 if (cmd->result.exit_status != action->rc) {
825 cmd->epoch_rcchange = time(NULL);
826 }
827 #endif
828
829 cmd->last_pid = action->pid;
830
831
832 code = services_result2ocf(action->standard, cmd->action, action->rc);
833 pcmk__set_result(&(cmd->result), (int) code,
834 action->status, services__exit_reason(action));
835
836 rsc = cmd->rsc_id ? g_hash_table_lookup(rsc_list, cmd->rsc_id) : NULL;
837
838 #ifdef PCMK__TIME_USE_CGT
839 if (rsc && pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_SERVICE, pcmk__str_casei)) {
840 rclass = resources_find_service_class(rsc->type);
841 } else if(rsc) {
842 rclass = rsc->class;
843 }
844
845 if (pcmk__str_eq(rclass, PCMK_RESOURCE_CLASS_SYSTEMD, pcmk__str_casei)) {
846 if (pcmk__result_ok(&(cmd->result))
847 && pcmk__strcase_any_of(cmd->action, PCMK_ACTION_START,
848 PCMK_ACTION_STOP, NULL)) {
849
850
851
852
853
854 goagain = true;
855 cmd->real_action = cmd->action;
856 cmd->action = strdup(PCMK_ACTION_MONITOR);
857
858 } else if (cmd->real_action != NULL) {
859
860 if (cmd->result.execution_status == PCMK_EXEC_PENDING) {
861 goagain = true;
862
863 } else if (pcmk__result_ok(&(cmd->result))
864 && pcmk__str_eq(cmd->real_action, PCMK_ACTION_STOP,
865 pcmk__str_casei)) {
866 goagain = true;
867
868 } else {
869 int time_sum = time_diff_ms(NULL, &(cmd->t_first_run));
870 int timeout_left = cmd->timeout_orig - time_sum;
871
872 crm_debug("%s systemd %s is now complete (elapsed=%dms, "
873 "remaining=%dms): %s (%d)",
874 cmd->rsc_id, cmd->real_action, time_sum, timeout_left,
875 services_ocf_exitcode_str(cmd->result.exit_status),
876 cmd->result.exit_status);
877 cmd_original_times(cmd);
878
879
880 if ((cmd->result.execution_status == PCMK_EXEC_DONE)
881 && (cmd->result.exit_status == PCMK_OCF_NOT_RUNNING)) {
882
883 if (pcmk__str_eq(cmd->real_action, PCMK_ACTION_START,
884 pcmk__str_casei)) {
885 cmd->result.exit_status = PCMK_OCF_UNKNOWN_ERROR;
886 } else if (pcmk__str_eq(cmd->real_action, PCMK_ACTION_STOP,
887 pcmk__str_casei)) {
888 cmd->result.exit_status = PCMK_OCF_OK;
889 }
890 }
891 }
892 }
893 }
894 #endif
895
896 #if SUPPORT_NAGIOS
897 if (rsc && pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_NAGIOS, pcmk__str_casei)) {
898 if (action_matches(cmd, PCMK_ACTION_MONITOR, 0)
899 && pcmk__result_ok(&(cmd->result))) {
900
901 cmd->result.exit_status = PCMK_OCF_NOT_RUNNING;
902
903 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)
904 && !pcmk__result_ok(&(cmd->result))) {
905 #ifdef PCMK__TIME_USE_CGT
906 goagain = true;
907 #endif
908 }
909 }
910 #endif
911
912 #ifdef PCMK__TIME_USE_CGT
913 if (goagain) {
914 int time_sum = time_diff_ms(NULL, &(cmd->t_first_run));
915 int timeout_left = cmd->timeout_orig - time_sum;
916 int delay = cmd->timeout_orig / 10;
917
918 if(delay >= timeout_left && timeout_left > 20) {
919 delay = timeout_left/2;
920 }
921
922 delay = QB_MIN(2000, delay);
923 if (delay < timeout_left) {
924 cmd->start_delay = delay;
925 cmd->timeout = timeout_left;
926
927 if (pcmk__result_ok(&(cmd->result))) {
928 crm_debug("%s %s may still be in progress: re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
929 cmd->rsc_id, cmd->real_action, time_sum, timeout_left, delay);
930
931 } else if (cmd->result.execution_status == PCMK_EXEC_PENDING) {
932 crm_info("%s %s is still in progress: re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
933 cmd->rsc_id, cmd->action, time_sum, timeout_left, delay);
934
935 } else {
936 crm_notice("%s %s failed '%s' (%d): re-scheduling (elapsed=%dms, remaining=%dms, start_delay=%dms)",
937 cmd->rsc_id, cmd->action,
938 services_ocf_exitcode_str(cmd->result.exit_status),
939 cmd->result.exit_status, time_sum, timeout_left,
940 delay);
941 }
942
943 cmd_reset(cmd);
944 if(rsc) {
945 rsc->active = NULL;
946 }
947 schedule_lrmd_cmd(rsc, cmd);
948
949
950 return;
951
952 } else {
953 crm_notice("Giving up on %s %s (rc=%d): timeout (elapsed=%dms, remaining=%dms)",
954 cmd->rsc_id,
955 (cmd->real_action? cmd->real_action : cmd->action),
956 cmd->result.exit_status, time_sum, timeout_left);
957 pcmk__set_result(&(cmd->result), PCMK_OCF_UNKNOWN_ERROR,
958 PCMK_EXEC_TIMEOUT,
959 "Investigate reason for timeout, and adjust "
960 "configured operation timeout if necessary");
961 cmd_original_times(cmd);
962 }
963 }
964 #endif
965
966 pcmk__set_result_output(&(cmd->result), services__grab_stdout(action),
967 services__grab_stderr(action));
968 cmd_finalize(cmd, rsc);
969 }
970
971
972
973
974
975
976
977
978
979
980 static void
981 stonith_action_complete(lrmd_cmd_t *cmd, int exit_status,
982 enum pcmk_exec_status execution_status,
983 const char *exit_reason)
984 {
985
986 lrmd_rsc_t *rsc = g_hash_table_lookup(rsc_list, cmd->rsc_id);
987
988
989 if (exit_status != CRM_EX_OK) {
990 exit_status = PCMK_OCF_UNKNOWN_ERROR;
991 }
992
993 if (cmd->result.execution_status == PCMK_EXEC_CANCELLED) {
994
995
996
997 execution_status = PCMK_EXEC_CANCELLED;
998
999 } else {
1000
1001
1002
1003
1004 switch (execution_status) {
1005 case PCMK_EXEC_NOT_CONNECTED:
1006 case PCMK_EXEC_INVALID:
1007 execution_status = PCMK_EXEC_ERROR;
1008 break;
1009
1010 case PCMK_EXEC_NO_FENCE_DEVICE:
1011
1012
1013
1014 if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR,
1015 pcmk__str_none)) {
1016 exit_status = PCMK_OCF_NOT_RUNNING;
1017
1018 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP,
1019 pcmk__str_none)) {
1020 exit_status = PCMK_OCF_OK;
1021
1022 } else {
1023 exit_status = PCMK_OCF_NOT_INSTALLED;
1024 }
1025 execution_status = PCMK_EXEC_ERROR;
1026 break;
1027
1028 case PCMK_EXEC_NOT_SUPPORTED:
1029 exit_status = PCMK_OCF_UNIMPLEMENT_FEATURE;
1030 break;
1031
1032 default:
1033 break;
1034 }
1035 }
1036
1037 pcmk__set_result(&cmd->result, exit_status, execution_status, exit_reason);
1038
1039
1040 if ((rsc != NULL) && pcmk__result_ok(&(cmd->result))) {
1041
1042 if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)) {
1043 pcmk__set_result(&rsc->fence_probe_result, CRM_EX_OK,
1044 PCMK_EXEC_DONE, NULL);
1045
1046 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP,
1047 pcmk__str_casei)) {
1048 pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
1049 PCMK_EXEC_NO_FENCE_DEVICE, NULL);
1050 }
1051 }
1052
1053
1054
1055
1056 stop_recurring_timer(cmd);
1057
1058
1059
1060
1061
1062 if (rsc && (cmd->interval_ms > 0)
1063 && (cmd->result.execution_status != PCMK_EXEC_CANCELLED)) {
1064 start_recurring_timer(cmd);
1065 }
1066
1067 cmd_finalize(cmd, rsc);
1068 }
1069
1070 static void
1071 lrmd_stonith_callback(stonith_t * stonith, stonith_callback_data_t * data)
1072 {
1073 if ((data == NULL) || (data->userdata == NULL)) {
1074 crm_err("Ignoring fence action result: "
1075 "Invalid callback arguments (bug?)");
1076 } else {
1077 stonith_action_complete((lrmd_cmd_t *) data->userdata,
1078 stonith__exit_status(data),
1079 stonith__execution_status(data),
1080 stonith__exit_reason(data));
1081 }
1082 }
1083
1084 void
1085 stonith_connection_failed(void)
1086 {
1087 GHashTableIter iter;
1088 lrmd_rsc_t *rsc = NULL;
1089
1090 crm_warn("Connection to fencer lost (any pending operations for "
1091 "fence devices will be considered failed)");
1092
1093 g_hash_table_iter_init(&iter, rsc_list);
1094 while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &rsc)) {
1095 if (!pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
1096 pcmk__str_none)) {
1097 continue;
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107 if (rsc->fence_probe_result.execution_status == PCMK_EXEC_DONE) {
1108 pcmk__set_result(&rsc->fence_probe_result, CRM_EX_ERROR,
1109 PCMK_EXEC_NOT_CONNECTED,
1110 "Lost connection to fencer");
1111 }
1112
1113
1114
1115 for (GList *op = rsc->recurring_ops; op != NULL; op = op->next) {
1116 lrmd_cmd_t *cmd = op->data;
1117
1118
1119
1120
1121
1122 stonith_action_complete(cmd,
1123 CRM_EX_ERROR, PCMK_EXEC_NOT_CONNECTED,
1124 "Lost connection to fencer");
1125 }
1126
1127 if (rsc->active != NULL) {
1128 rsc->pending_ops = g_list_prepend(rsc->pending_ops, rsc->active);
1129 }
1130 while (rsc->pending_ops != NULL) {
1131
1132 stonith_action_complete((lrmd_cmd_t *) rsc->pending_ops->data,
1133 CRM_EX_ERROR, PCMK_EXEC_NOT_CONNECTED,
1134 "Lost connection to fencer");
1135 }
1136 }
1137 }
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152 static int
1153 execd_stonith_start(stonith_t *stonith_api, const lrmd_rsc_t *rsc,
1154 const lrmd_cmd_t *cmd)
1155 {
1156 char *key = NULL;
1157 char *value = NULL;
1158 stonith_key_value_t *device_params = NULL;
1159 int rc = pcmk_ok;
1160
1161
1162 if (cmd->params) {
1163 GHashTableIter iter;
1164
1165 g_hash_table_iter_init(&iter, cmd->params);
1166 while (g_hash_table_iter_next(&iter, (gpointer *) & key, (gpointer *) & value)) {
1167 device_params = stonith_key_value_add(device_params, key, value);
1168 }
1169 }
1170
1171
1172
1173
1174
1175
1176
1177 rc = stonith_api->cmds->register_device(stonith_api, st_opt_sync_call,
1178 cmd->rsc_id, rsc->provider,
1179 rsc->type, device_params);
1180
1181 stonith_key_value_freeall(device_params, 1, 1);
1182 return rc;
1183 }
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197 static inline int
1198 execd_stonith_stop(stonith_t *stonith_api, const lrmd_rsc_t *rsc)
1199 {
1200
1201
1202
1203 return stonith_api->cmds->remove_device(stonith_api, st_opt_sync_call,
1204 rsc->rsc_id);
1205 }
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217 static inline int
1218 execd_stonith_monitor(stonith_t *stonith_api, lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
1219 {
1220 int rc = stonith_api->cmds->monitor(stonith_api, 0, cmd->rsc_id,
1221 cmd->timeout / 1000);
1222
1223 rc = stonith_api->cmds->register_callback(stonith_api, rc, 0, 0, cmd,
1224 "lrmd_stonith_callback",
1225 lrmd_stonith_callback);
1226 if (rc == TRUE) {
1227 rsc->active = cmd;
1228 rc = pcmk_ok;
1229 } else {
1230 rc = -pcmk_err_generic;
1231 }
1232 return rc;
1233 }
1234
1235 static void
1236 execute_stonith_action(lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
1237 {
1238 int rc = 0;
1239 bool do_monitor = FALSE;
1240
1241 stonith_t *stonith_api = get_stonith_connection();
1242
1243 if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR, pcmk__str_casei)
1244 && (cmd->interval_ms == 0)) {
1245
1246 stonith_action_complete(cmd, rsc->fence_probe_result.exit_status,
1247 rsc->fence_probe_result.execution_status,
1248 rsc->fence_probe_result.exit_reason);
1249 return;
1250
1251 } else if (stonith_api == NULL) {
1252 stonith_action_complete(cmd, PCMK_OCF_UNKNOWN_ERROR,
1253 PCMK_EXEC_NOT_CONNECTED,
1254 "No connection to fencer");
1255 return;
1256
1257 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_START, pcmk__str_casei)) {
1258 rc = execd_stonith_start(stonith_api, rsc, cmd);
1259 if (rc == pcmk_ok) {
1260 do_monitor = TRUE;
1261 }
1262
1263 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
1264 rc = execd_stonith_stop(stonith_api, rsc);
1265
1266 } else if (pcmk__str_eq(cmd->action, PCMK_ACTION_MONITOR,
1267 pcmk__str_casei)) {
1268 do_monitor = TRUE;
1269
1270 } else {
1271 stonith_action_complete(cmd, PCMK_OCF_UNIMPLEMENT_FEATURE,
1272 PCMK_EXEC_ERROR,
1273 "Invalid fence device action (bug?)");
1274 return;
1275 }
1276
1277 if (do_monitor) {
1278 rc = execd_stonith_monitor(stonith_api, rsc, cmd);
1279 if (rc == pcmk_ok) {
1280
1281 return;
1282 }
1283 }
1284
1285 stonith_action_complete(cmd,
1286 ((rc == pcmk_ok)? CRM_EX_OK : CRM_EX_ERROR),
1287 stonith__legacy2status(rc),
1288 ((rc == -pcmk_err_generic)? NULL : pcmk_strerror(rc)));
1289 }
1290
1291 static void
1292 execute_nonstonith_action(lrmd_rsc_t *rsc, lrmd_cmd_t *cmd)
1293 {
1294 svc_action_t *action = NULL;
1295 GHashTable *params_copy = NULL;
1296
1297 CRM_ASSERT(rsc);
1298 CRM_ASSERT(cmd);
1299
1300 crm_trace("Creating action, resource:%s action:%s class:%s provider:%s agent:%s",
1301 rsc->rsc_id, cmd->action, rsc->class, rsc->provider, rsc->type);
1302
1303 #if SUPPORT_NAGIOS
1304
1305 if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_NAGIOS, pcmk__str_casei)
1306 && pcmk__str_eq(cmd->action, PCMK_ACTION_STOP, pcmk__str_casei)) {
1307
1308 cmd->result.exit_status = PCMK_OCF_OK;
1309 cmd_finalize(cmd, rsc);
1310 return;
1311 }
1312 #endif
1313
1314 params_copy = pcmk__str_table_dup(cmd->params);
1315
1316 action = services__create_resource_action(rsc->rsc_id, rsc->class, rsc->provider,
1317 rsc->type,
1318 normalize_action_name(rsc, cmd->action),
1319 cmd->interval_ms, cmd->timeout,
1320 params_copy, cmd->service_flags);
1321
1322 if (action == NULL) {
1323 pcmk__set_result(&(cmd->result), PCMK_OCF_UNKNOWN_ERROR,
1324 PCMK_EXEC_ERROR, strerror(ENOMEM));
1325 cmd_finalize(cmd, rsc);
1326 return;
1327 }
1328
1329 if (action->rc != PCMK_OCF_UNKNOWN) {
1330 pcmk__set_result(&(cmd->result), action->rc, action->status,
1331 services__exit_reason(action));
1332 services_action_free(action);
1333 cmd_finalize(cmd, rsc);
1334 return;
1335 }
1336
1337 action->cb_data = cmd;
1338
1339 if (services_action_async(action, action_complete)) {
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349 } else {
1350
1351
1352
1353
1354
1355
1356
1357 pcmk__set_result(&(cmd->result), action->rc, action->status,
1358 services__exit_reason(action));
1359 services_action_free(action);
1360 }
1361 }
1362
1363 static gboolean
1364 execute_resource_action(gpointer user_data)
1365 {
1366 lrmd_rsc_t *rsc = (lrmd_rsc_t *) user_data;
1367 lrmd_cmd_t *cmd = NULL;
1368
1369 CRM_CHECK(rsc != NULL, return FALSE);
1370
1371 if (rsc->active) {
1372 crm_trace("%s is still active", rsc->rsc_id);
1373 return TRUE;
1374 }
1375
1376 if (rsc->pending_ops) {
1377 GList *first = rsc->pending_ops;
1378
1379 cmd = first->data;
1380 if (cmd->delay_id) {
1381 crm_trace
1382 ("Command %s %s was asked to run too early, waiting for start_delay timeout of %dms",
1383 cmd->rsc_id, cmd->action, cmd->start_delay);
1384 return TRUE;
1385 }
1386 rsc->pending_ops = g_list_remove_link(rsc->pending_ops, first);
1387 g_list_free_1(first);
1388
1389 #ifdef PCMK__TIME_USE_CGT
1390 get_current_time(&(cmd->t_run), &(cmd->t_first_run));
1391 #endif
1392 cmd->epoch_last_run = time(NULL);
1393 }
1394
1395 if (!cmd) {
1396 crm_trace("Nothing further to do for %s", rsc->rsc_id);
1397 return TRUE;
1398 }
1399
1400 rsc->active = cmd;
1401 if (cmd->interval_ms) {
1402 rsc->recurring_ops = g_list_append(rsc->recurring_ops, cmd);
1403 }
1404
1405 log_execute(cmd);
1406
1407 if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
1408 execute_stonith_action(rsc, cmd);
1409 } else {
1410 execute_nonstonith_action(rsc, cmd);
1411 }
1412
1413 return TRUE;
1414 }
1415
1416 void
1417 free_rsc(gpointer data)
1418 {
1419 GList *gIter = NULL;
1420 lrmd_rsc_t *rsc = data;
1421 int is_stonith = pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH,
1422 pcmk__str_casei);
1423
1424 gIter = rsc->pending_ops;
1425 while (gIter != NULL) {
1426 GList *next = gIter->next;
1427 lrmd_cmd_t *cmd = gIter->data;
1428
1429
1430 cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1431 cmd_finalize(cmd, NULL);
1432
1433 gIter = next;
1434 }
1435
1436 g_list_free(rsc->pending_ops);
1437
1438 gIter = rsc->recurring_ops;
1439 while (gIter != NULL) {
1440 GList *next = gIter->next;
1441 lrmd_cmd_t *cmd = gIter->data;
1442
1443 if (is_stonith) {
1444 cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1445
1446
1447
1448
1449 if (rsc->active != cmd) {
1450 cmd_finalize(cmd, NULL);
1451 }
1452 } else {
1453
1454
1455
1456
1457 services_action_cancel(rsc->rsc_id,
1458 normalize_action_name(rsc, cmd->action),
1459 cmd->interval_ms);
1460 }
1461
1462 gIter = next;
1463 }
1464
1465 g_list_free(rsc->recurring_ops);
1466
1467 free(rsc->rsc_id);
1468 free(rsc->class);
1469 free(rsc->provider);
1470 free(rsc->type);
1471 mainloop_destroy_trigger(rsc->work);
1472
1473 free(rsc);
1474 }
1475
1476 static int
1477 process_lrmd_signon(pcmk__client_t *client, xmlNode *request, int call_id,
1478 xmlNode **reply)
1479 {
1480 int rc = pcmk_ok;
1481 time_t now = time(NULL);
1482 const char *protocol_version = crm_element_value(request, F_LRMD_PROTOCOL_VERSION);
1483 const char *start_state = pcmk__env_option(PCMK__ENV_NODE_START_STATE);
1484
1485 if (compare_version(protocol_version, LRMD_MIN_PROTOCOL_VERSION) < 0) {
1486 crm_err("Cluster API version must be greater than or equal to %s, not %s",
1487 LRMD_MIN_PROTOCOL_VERSION, protocol_version);
1488 rc = -EPROTO;
1489 }
1490
1491 if (pcmk__xe_attr_is_true(request, F_LRMD_IS_IPC_PROVIDER)) {
1492 #ifdef PCMK__COMPILE_REMOTE
1493 if ((client->remote != NULL)
1494 && pcmk_is_set(client->flags,
1495 pcmk__client_tls_handshake_complete)) {
1496
1497
1498 ipc_proxy_add_provider(client);
1499 } else {
1500 rc = -EACCES;
1501 }
1502 #else
1503 rc = -EPROTONOSUPPORT;
1504 #endif
1505 }
1506
1507 *reply = create_lrmd_reply(__func__, rc, call_id);
1508 crm_xml_add(*reply, F_LRMD_OPERATION, CRM_OP_REGISTER);
1509 crm_xml_add(*reply, F_LRMD_CLIENTID, client->id);
1510 crm_xml_add(*reply, F_LRMD_PROTOCOL_VERSION, LRMD_PROTOCOL_VERSION);
1511 crm_xml_add_ll(*reply, PCMK__XA_UPTIME, now - start_time);
1512
1513 if (start_state) {
1514 crm_xml_add(*reply, PCMK__XA_NODE_START_STATE, start_state);
1515 }
1516
1517 return rc;
1518 }
1519
1520 static int
1521 process_lrmd_rsc_register(pcmk__client_t *client, uint32_t id, xmlNode *request)
1522 {
1523 int rc = pcmk_ok;
1524 lrmd_rsc_t *rsc = build_rsc_from_xml(request);
1525 lrmd_rsc_t *dup = g_hash_table_lookup(rsc_list, rsc->rsc_id);
1526
1527 if (dup &&
1528 pcmk__str_eq(rsc->class, dup->class, pcmk__str_casei) &&
1529 pcmk__str_eq(rsc->provider, dup->provider, pcmk__str_casei) && pcmk__str_eq(rsc->type, dup->type, pcmk__str_casei)) {
1530
1531 crm_notice("Ignoring duplicate registration of '%s'", rsc->rsc_id);
1532 free_rsc(rsc);
1533 return rc;
1534 }
1535
1536 g_hash_table_replace(rsc_list, rsc->rsc_id, rsc);
1537 crm_info("Cached agent information for '%s'", rsc->rsc_id);
1538 return rc;
1539 }
1540
1541 static xmlNode *
1542 process_lrmd_get_rsc_info(xmlNode *request, int call_id)
1543 {
1544 int rc = pcmk_ok;
1545 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1546 const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1547 xmlNode *reply = NULL;
1548 lrmd_rsc_t *rsc = NULL;
1549
1550 if (rsc_id == NULL) {
1551 rc = -ENODEV;
1552 } else {
1553 rsc = g_hash_table_lookup(rsc_list, rsc_id);
1554 if (rsc == NULL) {
1555 crm_info("Agent information for '%s' not in cache", rsc_id);
1556 rc = -ENODEV;
1557 }
1558 }
1559
1560 reply = create_lrmd_reply(__func__, rc, call_id);
1561 if (rsc) {
1562 crm_xml_add(reply, F_LRMD_RSC_ID, rsc->rsc_id);
1563 crm_xml_add(reply, F_LRMD_CLASS, rsc->class);
1564 crm_xml_add(reply, F_LRMD_PROVIDER, rsc->provider);
1565 crm_xml_add(reply, F_LRMD_TYPE, rsc->type);
1566 }
1567 return reply;
1568 }
1569
1570 static int
1571 process_lrmd_rsc_unregister(pcmk__client_t *client, uint32_t id,
1572 xmlNode *request)
1573 {
1574 int rc = pcmk_ok;
1575 lrmd_rsc_t *rsc = NULL;
1576 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1577 const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1578
1579 if (!rsc_id) {
1580 return -ENODEV;
1581 }
1582
1583 rsc = g_hash_table_lookup(rsc_list, rsc_id);
1584 if (rsc == NULL) {
1585 crm_info("Ignoring unregistration of resource '%s', which is not registered",
1586 rsc_id);
1587 return pcmk_ok;
1588 }
1589
1590 if (rsc->active) {
1591
1592 crm_trace("Operation (%p) still in progress for unregistered resource %s",
1593 rsc->active, rsc_id);
1594 rc = -EINPROGRESS;
1595 }
1596
1597 g_hash_table_remove(rsc_list, rsc_id);
1598
1599 return rc;
1600 }
1601
1602 static int
1603 process_lrmd_rsc_exec(pcmk__client_t *client, uint32_t id, xmlNode *request)
1604 {
1605 lrmd_rsc_t *rsc = NULL;
1606 lrmd_cmd_t *cmd = NULL;
1607 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1608 const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1609 int call_id;
1610
1611 if (!rsc_id) {
1612 return -EINVAL;
1613 }
1614 if (!(rsc = g_hash_table_lookup(rsc_list, rsc_id))) {
1615 crm_info("Resource '%s' not found (%d active resources)",
1616 rsc_id, g_hash_table_size(rsc_list));
1617 return -ENODEV;
1618 }
1619
1620 cmd = create_lrmd_cmd(request, client);
1621 call_id = cmd->call_id;
1622
1623
1624
1625 schedule_lrmd_cmd(rsc, cmd);
1626
1627 return call_id;
1628 }
1629
1630 static int
1631 cancel_op(const char *rsc_id, const char *action, guint interval_ms)
1632 {
1633 GList *gIter = NULL;
1634 lrmd_rsc_t *rsc = g_hash_table_lookup(rsc_list, rsc_id);
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647 if (!rsc) {
1648 return -ENODEV;
1649 }
1650
1651 for (gIter = rsc->pending_ops; gIter != NULL; gIter = gIter->next) {
1652 lrmd_cmd_t *cmd = gIter->data;
1653
1654 if (action_matches(cmd, action, interval_ms)) {
1655 cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1656 cmd_finalize(cmd, rsc);
1657 return pcmk_ok;
1658 }
1659 }
1660
1661 if (pcmk__str_eq(rsc->class, PCMK_RESOURCE_CLASS_STONITH, pcmk__str_casei)) {
1662
1663
1664 for (gIter = rsc->recurring_ops; gIter != NULL; gIter = gIter->next) {
1665 lrmd_cmd_t *cmd = gIter->data;
1666
1667 if (action_matches(cmd, action, interval_ms)) {
1668 cmd->result.execution_status = PCMK_EXEC_CANCELLED;
1669 if (rsc->active != cmd) {
1670 cmd_finalize(cmd, rsc);
1671 }
1672 return pcmk_ok;
1673 }
1674 }
1675 } else if (services_action_cancel(rsc_id,
1676 normalize_action_name(rsc, action),
1677 interval_ms) == TRUE) {
1678
1679
1680
1681
1682 return pcmk_ok;
1683 }
1684
1685 return -EOPNOTSUPP;
1686 }
1687
1688 static void
1689 cancel_all_recurring(lrmd_rsc_t * rsc, const char *client_id)
1690 {
1691 GList *cmd_list = NULL;
1692 GList *cmd_iter = NULL;
1693
1694
1695
1696
1697
1698
1699
1700 if (rsc->recurring_ops) {
1701 cmd_list = g_list_concat(cmd_list, g_list_copy(rsc->recurring_ops));
1702 }
1703 if (rsc->pending_ops) {
1704 cmd_list = g_list_concat(cmd_list, g_list_copy(rsc->pending_ops));
1705 }
1706 if (!cmd_list) {
1707 return;
1708 }
1709
1710 for (cmd_iter = cmd_list; cmd_iter; cmd_iter = cmd_iter->next) {
1711 lrmd_cmd_t *cmd = cmd_iter->data;
1712
1713 if (cmd->interval_ms == 0) {
1714 continue;
1715 }
1716
1717 if (client_id && !pcmk__str_eq(cmd->client_id, client_id, pcmk__str_casei)) {
1718 continue;
1719 }
1720
1721 cancel_op(rsc->rsc_id, cmd->action, cmd->interval_ms);
1722 }
1723
1724 g_list_free(cmd_list);
1725 }
1726
1727 static int
1728 process_lrmd_rsc_cancel(pcmk__client_t *client, uint32_t id, xmlNode *request)
1729 {
1730 xmlNode *rsc_xml = get_xpath_object("//" F_LRMD_RSC, request, LOG_ERR);
1731 const char *rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1732 const char *action = crm_element_value(rsc_xml, F_LRMD_RSC_ACTION);
1733 guint interval_ms = 0;
1734
1735 crm_element_value_ms(rsc_xml, F_LRMD_RSC_INTERVAL, &interval_ms);
1736
1737 if (!rsc_id || !action) {
1738 return -EINVAL;
1739 }
1740
1741 return cancel_op(rsc_id, action, interval_ms);
1742 }
1743
1744 static void
1745 add_recurring_op_xml(xmlNode *reply, lrmd_rsc_t *rsc)
1746 {
1747 xmlNode *rsc_xml = create_xml_node(reply, F_LRMD_RSC);
1748
1749 crm_xml_add(rsc_xml, F_LRMD_RSC_ID, rsc->rsc_id);
1750 for (GList *item = rsc->recurring_ops; item != NULL; item = item->next) {
1751 lrmd_cmd_t *cmd = item->data;
1752 xmlNode *op_xml = create_xml_node(rsc_xml, T_LRMD_RSC_OP);
1753
1754 crm_xml_add(op_xml, F_LRMD_RSC_ACTION,
1755 (cmd->real_action? cmd->real_action : cmd->action));
1756 crm_xml_add_ms(op_xml, F_LRMD_RSC_INTERVAL, cmd->interval_ms);
1757 crm_xml_add_int(op_xml, F_LRMD_TIMEOUT, cmd->timeout_orig);
1758 }
1759 }
1760
1761 static xmlNode *
1762 process_lrmd_get_recurring(xmlNode *request, int call_id)
1763 {
1764 int rc = pcmk_ok;
1765 const char *rsc_id = NULL;
1766 lrmd_rsc_t *rsc = NULL;
1767 xmlNode *reply = NULL;
1768 xmlNode *rsc_xml = NULL;
1769
1770
1771 rsc_xml = first_named_child(request, F_LRMD_CALLDATA);
1772 if (rsc_xml) {
1773 rsc_xml = first_named_child(rsc_xml, F_LRMD_RSC);
1774 }
1775 if (rsc_xml) {
1776 rsc_id = crm_element_value(rsc_xml, F_LRMD_RSC_ID);
1777 }
1778
1779
1780 if (rsc_id != NULL) {
1781 rsc = g_hash_table_lookup(rsc_list, rsc_id);
1782 if (rsc == NULL) {
1783 crm_info("Resource '%s' not found (%d active resources)",
1784 rsc_id, g_hash_table_size(rsc_list));
1785 rc = -ENODEV;
1786 }
1787 }
1788
1789 reply = create_lrmd_reply(__func__, rc, call_id);
1790
1791
1792 if (rsc_id == NULL) {
1793 GHashTableIter iter;
1794 char *key = NULL;
1795
1796 g_hash_table_iter_init(&iter, rsc_list);
1797 while (g_hash_table_iter_next(&iter, (gpointer *) &key,
1798 (gpointer *) &rsc)) {
1799 add_recurring_op_xml(reply, rsc);
1800 }
1801 } else if (rsc) {
1802 add_recurring_op_xml(reply, rsc);
1803 }
1804 return reply;
1805 }
1806
1807 void
1808 process_lrmd_message(pcmk__client_t *client, uint32_t id, xmlNode *request)
1809 {
1810 int rc = pcmk_ok;
1811 int call_id = 0;
1812 const char *op = crm_element_value(request, F_LRMD_OPERATION);
1813 int do_reply = 0;
1814 int do_notify = 0;
1815 xmlNode *reply = NULL;
1816
1817
1818
1819
1820
1821 bool allowed = pcmk_is_set(client->flags, pcmk__client_privileged);
1822
1823 crm_trace("Processing %s operation from %s", op, client->id);
1824 crm_element_value_int(request, F_LRMD_CALLID, &call_id);
1825
1826 if (pcmk__str_eq(op, CRM_OP_IPC_FWD, pcmk__str_none)) {
1827 #ifdef PCMK__COMPILE_REMOTE
1828 if (allowed) {
1829 ipc_proxy_forward_client(client, request);
1830 } else {
1831 rc = -EACCES;
1832 }
1833 #else
1834 rc = -EPROTONOSUPPORT;
1835 #endif
1836 do_reply = 1;
1837 } else if (pcmk__str_eq(op, CRM_OP_REGISTER, pcmk__str_none)) {
1838 rc = process_lrmd_signon(client, request, call_id, &reply);
1839 do_reply = 1;
1840 } else if (pcmk__str_eq(op, LRMD_OP_RSC_REG, pcmk__str_none)) {
1841 if (allowed) {
1842 rc = process_lrmd_rsc_register(client, id, request);
1843 do_notify = 1;
1844 } else {
1845 rc = -EACCES;
1846 }
1847 do_reply = 1;
1848 } else if (pcmk__str_eq(op, LRMD_OP_RSC_INFO, pcmk__str_none)) {
1849 if (allowed) {
1850 reply = process_lrmd_get_rsc_info(request, call_id);
1851 } else {
1852 rc = -EACCES;
1853 }
1854 do_reply = 1;
1855 } else if (pcmk__str_eq(op, LRMD_OP_RSC_UNREG, pcmk__str_none)) {
1856 if (allowed) {
1857 rc = process_lrmd_rsc_unregister(client, id, request);
1858
1859 if (rc == pcmk_ok || rc == -EINPROGRESS) {
1860 do_notify = 1;
1861 }
1862 } else {
1863 rc = -EACCES;
1864 }
1865 do_reply = 1;
1866 } else if (pcmk__str_eq(op, LRMD_OP_RSC_EXEC, pcmk__str_none)) {
1867 if (allowed) {
1868 rc = process_lrmd_rsc_exec(client, id, request);
1869 } else {
1870 rc = -EACCES;
1871 }
1872 do_reply = 1;
1873 } else if (pcmk__str_eq(op, LRMD_OP_RSC_CANCEL, pcmk__str_none)) {
1874 if (allowed) {
1875 rc = process_lrmd_rsc_cancel(client, id, request);
1876 } else {
1877 rc = -EACCES;
1878 }
1879 do_reply = 1;
1880 } else if (pcmk__str_eq(op, LRMD_OP_POKE, pcmk__str_none)) {
1881 do_notify = 1;
1882 do_reply = 1;
1883 } else if (pcmk__str_eq(op, LRMD_OP_CHECK, pcmk__str_none)) {
1884 if (allowed) {
1885 xmlNode *data = get_message_xml(request, F_LRMD_CALLDATA);
1886
1887 CRM_LOG_ASSERT(data != NULL);
1888 pcmk__valid_sbd_timeout(crm_element_value(data, F_LRMD_WATCHDOG));
1889 } else {
1890 rc = -EACCES;
1891 }
1892 } else if (pcmk__str_eq(op, LRMD_OP_ALERT_EXEC, pcmk__str_none)) {
1893 if (allowed) {
1894 rc = process_lrmd_alert_exec(client, id, request);
1895 } else {
1896 rc = -EACCES;
1897 }
1898 do_reply = 1;
1899 } else if (pcmk__str_eq(op, LRMD_OP_GET_RECURRING, pcmk__str_none)) {
1900 if (allowed) {
1901 reply = process_lrmd_get_recurring(request, call_id);
1902 } else {
1903 rc = -EACCES;
1904 }
1905 do_reply = 1;
1906 } else {
1907 rc = -EOPNOTSUPP;
1908 do_reply = 1;
1909 crm_err("Unknown IPC request '%s' from client %s",
1910 op, pcmk__client_name(client));
1911 }
1912
1913 if (rc == -EACCES) {
1914 crm_warn("Rejecting IPC request '%s' from unprivileged client %s",
1915 op, pcmk__client_name(client));
1916 }
1917
1918 crm_debug("Processed %s operation from %s: rc=%d, reply=%d, notify=%d",
1919 op, client->id, rc, do_reply, do_notify);
1920
1921 if (do_reply) {
1922 int send_rc = pcmk_rc_ok;
1923
1924 if (reply == NULL) {
1925 reply = create_lrmd_reply(__func__, rc, call_id);
1926 }
1927 send_rc = lrmd_server_send_reply(client, id, reply);
1928 free_xml(reply);
1929 if (send_rc != pcmk_rc_ok) {
1930 crm_warn("Reply to client %s failed: %s " CRM_XS " rc=%d",
1931 pcmk__client_name(client), pcmk_rc_str(send_rc), send_rc);
1932 }
1933 }
1934
1935 if (do_notify) {
1936 send_generic_notify(rc, request);
1937 }
1938 }