pacemaker  2.0.1-9e909a5bdd
Scalable High-Availability cluster resource manager
systemd.c
Go to the documentation of this file.
1 /*
2  * Copyright 2012-2018 Andrew Beekhof <andrew@beekhof.net>
3  *
4  * This source code is licensed under the GNU Lesser General Public License
5  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
6  */
7 
8 #include <crm_internal.h>
9 #include <crm/crm.h>
10 #include <crm/services.h>
11 #include <crm/common/mainloop.h>
12 
13 #include <sys/stat.h>
14 #include <gio/gio.h>
15 #include <services_private.h>
16 #include <systemd.h>
17 #include <dbus/dbus.h>
18 #include <pcmk-dbus.h>
19 
20 gboolean systemd_unit_exec_with_unit(svc_action_t * op, const char *unit);
21 
22 #define BUS_NAME "org.freedesktop.systemd1"
23 #define BUS_NAME_MANAGER BUS_NAME ".Manager"
24 #define BUS_NAME_UNIT BUS_NAME ".Unit"
25 #define BUS_PATH "/org/freedesktop/systemd1"
26 
27 static inline DBusMessage *
28 systemd_new_method(const char *method)
29 {
30  crm_trace("Calling: %s on " BUS_NAME_MANAGER, method);
31  return dbus_message_new_method_call(BUS_NAME, BUS_PATH, BUS_NAME_MANAGER,
32  method);
33 }
34 
35 /*
36  * Functions to manage a static DBus connection
37  */
38 
39 static DBusConnection* systemd_proxy = NULL;
40 
41 static inline DBusPendingCall *
42 systemd_send(DBusMessage *msg,
43  void(*done)(DBusPendingCall *pending, void *user_data),
44  void *user_data, int timeout)
45 {
46  return pcmk_dbus_send(msg, systemd_proxy, done, user_data, timeout);
47 }
48 
49 static inline DBusMessage *
50 systemd_send_recv(DBusMessage *msg, DBusError *error, int timeout)
51 {
52  return pcmk_dbus_send_recv(msg, systemd_proxy, error, timeout);
53 }
54 
66 static DBusMessage *
67 systemd_call_simple_method(const char *method)
68 {
69  DBusMessage *msg = systemd_new_method(method);
70  DBusMessage *reply = NULL;
71  DBusError error;
72 
73  /* Don't call systemd_init() here, because that calls this */
74  CRM_CHECK(systemd_proxy, return NULL);
75 
76  if (msg == NULL) {
77  crm_err("Could not create message to send %s to systemd", method);
78  return NULL;
79  }
80 
81  dbus_error_init(&error);
82  reply = systemd_send_recv(msg, &error, DBUS_TIMEOUT_USE_DEFAULT);
83  dbus_message_unref(msg);
84 
85  if (dbus_error_is_set(&error)) {
86  crm_err("Could not send %s to systemd: %s (%s)",
87  method, error.message, error.name);
88  dbus_error_free(&error);
89  return NULL;
90 
91  } else if (reply == NULL) {
92  crm_err("Could not send %s to systemd: no reply received", method);
93  return NULL;
94  }
95 
96  return reply;
97 }
98 
99 static gboolean
100 systemd_init(void)
101 {
102  static int need_init = 1;
103  /* http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html */
104 
105  if (systemd_proxy
106  && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
107  crm_warn("Connection to System DBus is closed. Reconnecting...");
108  pcmk_dbus_disconnect(systemd_proxy);
109  systemd_proxy = NULL;
110  need_init = 1;
111  }
112 
113  if (need_init) {
114  need_init = 0;
115  systemd_proxy = pcmk_dbus_connect();
116  }
117  if (systemd_proxy == NULL) {
118  return FALSE;
119  }
120  return TRUE;
121 }
122 
123 static inline char *
124 systemd_get_property(const char *unit, const char *name,
125  void (*callback)(const char *name, const char *value, void *userdata),
126  void *userdata, DBusPendingCall **pending, int timeout)
127 {
128  return systemd_proxy?
129  pcmk_dbus_get_property(systemd_proxy, BUS_NAME, unit, BUS_NAME_UNIT,
130  name, callback, userdata, pending, timeout)
131  : NULL;
132 }
133 
134 void
136 {
137  if (systemd_proxy) {
138  pcmk_dbus_disconnect(systemd_proxy);
139  systemd_proxy = NULL;
140  }
141 }
142 
143 /*
144  * end of systemd_proxy functions
145  */
146 
155 static const char *
156 systemd_unit_extension(const char *name)
157 {
158  if (name) {
159  const char *dot = strrchr(name, '.');
160 
161  if (dot && (!strcmp(dot, ".service")
162  || !strcmp(dot, ".socket")
163  || !strcmp(dot, ".mount")
164  || !strcmp(dot, ".timer")
165  || !strcmp(dot, ".path"))) {
166  return dot;
167  }
168  }
169  return NULL;
170 }
171 
172 static char *
173 systemd_service_name(const char *name)
174 {
175  if (name == NULL) {
176  return NULL;
177  }
178 
179  if (systemd_unit_extension(name)) {
180  return strdup(name);
181  }
182 
183  return crm_strdup_printf("%s.service", name);
184 }
185 
186 static void
187 systemd_daemon_reload_complete(DBusPendingCall *pending, void *user_data)
188 {
189  DBusError error;
190  DBusMessage *reply = NULL;
191  unsigned int reload_count = GPOINTER_TO_UINT(user_data);
192 
193  dbus_error_init(&error);
194  if(pending) {
195  reply = dbus_pending_call_steal_reply(pending);
196  }
197 
198  if (pcmk_dbus_find_error(pending, reply, &error)) {
199  crm_err("Could not issue systemd reload %d: %s", reload_count, error.message);
200  dbus_error_free(&error);
201 
202  } else {
203  crm_trace("Reload %d complete", reload_count);
204  }
205 
206  if(pending) {
207  dbus_pending_call_unref(pending);
208  }
209  if(reply) {
210  dbus_message_unref(reply);
211  }
212 }
213 
214 static bool
215 systemd_daemon_reload(int timeout)
216 {
217  static unsigned int reload_count = 0;
218  DBusMessage *msg = systemd_new_method("Reload");
219 
220  reload_count++;
221  CRM_ASSERT(msg != NULL);
222  systemd_send(msg, systemd_daemon_reload_complete,
223  GUINT_TO_POINTER(reload_count), timeout);
224  dbus_message_unref(msg);
225 
226  return TRUE;
227 }
228 
229 static bool
230 systemd_mask_error(svc_action_t *op, const char *error)
231 {
232  crm_trace("Could not issue %s for %s: %s", op->action, op->rsc, error);
233  if(strstr(error, "org.freedesktop.systemd1.InvalidName")
234  || strstr(error, "org.freedesktop.systemd1.LoadFailed")
235  || strstr(error, "org.freedesktop.systemd1.NoSuchUnit")) {
236 
237  if (safe_str_eq(op->action, "stop")) {
238  crm_trace("Masking %s failure for %s: unknown services are stopped", op->action, op->rsc);
239  op->rc = PCMK_OCF_OK;
240  return TRUE;
241 
242  } else {
243  crm_trace("Mapping %s failure for %s: unknown services are not installed", op->action, op->rsc);
246  return FALSE;
247  }
248  }
249 
250  return FALSE;
251 }
252 
253 static const char *
254 systemd_loadunit_result(DBusMessage *reply, svc_action_t * op)
255 {
256  const char *path = NULL;
257  DBusError error;
258 
259  if (pcmk_dbus_find_error((void*)&path, reply, &error)) {
260  if(op && !systemd_mask_error(op, error.name)) {
261  crm_err("Could not load systemd unit %s for %s: %s",
262  op->agent, op->id, error.message);
263  }
264  dbus_error_free(&error);
265 
266  } else if(pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
267  dbus_message_get_args (reply, NULL,
268  DBUS_TYPE_OBJECT_PATH, &path,
269  DBUS_TYPE_INVALID);
270  }
271 
272  if(op) {
273  if (path) {
274  systemd_unit_exec_with_unit(op, path);
275 
276  } else if (op->synchronous == FALSE) {
277  operation_finalize(op);
278  }
279  }
280 
281  return path;
282 }
283 
284 
285 static void
286 systemd_loadunit_cb(DBusPendingCall *pending, void *user_data)
287 {
288  DBusMessage *reply = NULL;
289  svc_action_t * op = user_data;
290 
291  if(pending) {
292  reply = dbus_pending_call_steal_reply(pending);
293  }
294 
295  crm_trace("Got result: %p for %p / %p for %s", reply, pending, op->opaque->pending, op->id);
296 
297  CRM_LOG_ASSERT(pending == op->opaque->pending);
298  services_set_op_pending(op, NULL);
299 
300  systemd_loadunit_result(reply, user_data);
301 
302  if(reply) {
303  dbus_message_unref(reply);
304  }
305 }
306 
307 static char *
308 systemd_unit_by_name(const gchar * arg_name, svc_action_t *op)
309 {
310  DBusMessage *msg;
311  DBusMessage *reply = NULL;
312  DBusPendingCall* pending = NULL;
313  char *name = NULL;
314 
315 /*
316  Equivalent to GetUnit if it's already loaded
317  <method name="LoadUnit">
318  <arg name="name" type="s" direction="in"/>
319  <arg name="unit" type="o" direction="out"/>
320  </method>
321  */
322 
323  if (systemd_init() == FALSE) {
324  return FALSE;
325  }
326 
327  msg = systemd_new_method("LoadUnit");
328  CRM_ASSERT(msg != NULL);
329 
330  name = systemd_service_name(arg_name);
331  CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
332  free(name);
333 
334  if(op == NULL || op->synchronous) {
335  const char *unit = NULL;
336  char *munit = NULL;
337 
338  reply = systemd_send_recv(msg, NULL,
339  (op? op->timeout : DBUS_TIMEOUT_USE_DEFAULT));
340  dbus_message_unref(msg);
341 
342  unit = systemd_loadunit_result(reply, op);
343  if(unit) {
344  munit = strdup(unit);
345  }
346  if(reply) {
347  dbus_message_unref(reply);
348  }
349  return munit;
350  }
351 
352  pending = systemd_send(msg, systemd_loadunit_cb, op, op->timeout);
353  if(pending) {
354  services_set_op_pending(op, pending);
355  }
356 
357  dbus_message_unref(msg);
358  return NULL;
359 }
360 
361 GList *
363 {
364  int nfiles = 0;
365  GList *units = NULL;
366  DBusMessageIter args;
367  DBusMessageIter unit;
368  DBusMessageIter elem;
369  DBusMessage *reply = NULL;
370 
371  if (systemd_init() == FALSE) {
372  return NULL;
373  }
374 
375 /*
376  " <method name=\"ListUnitFiles\">\n" \
377  " <arg name=\"files\" type=\"a(ss)\" direction=\"out\"/>\n" \
378  " </method>\n" \
379 */
380 
381  reply = systemd_call_simple_method("ListUnitFiles");
382  if (reply == NULL) {
383  return NULL;
384  }
385  if (!dbus_message_iter_init(reply, &args)) {
386  crm_err("Could not list systemd unit files: systemd reply has no arguments");
387  dbus_message_unref(reply);
388  return NULL;
389  }
390  if (!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_ARRAY,
391  __FUNCTION__, __LINE__)) {
392  crm_err("Could not list systemd unit files: systemd reply has invalid arguments");
393  dbus_message_unref(reply);
394  return NULL;
395  }
396 
397  dbus_message_iter_recurse(&args, &unit);
398  for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
399  dbus_message_iter_next(&unit)) {
400 
401  DBusBasicValue value;
402  const char *match = NULL;
403  char *unit_name = NULL;
404  char *basename = NULL;
405 
406  if(!pcmk_dbus_type_check(reply, &unit, DBUS_TYPE_STRUCT, __FUNCTION__, __LINE__)) {
407  crm_debug("ListUnitFiles reply has unexpected type");
408  continue;
409  }
410 
411  dbus_message_iter_recurse(&unit, &elem);
412  if(!pcmk_dbus_type_check(reply, &elem, DBUS_TYPE_STRING, __FUNCTION__, __LINE__)) {
413  crm_debug("ListUnitFiles reply does not contain a string");
414  continue;
415  }
416 
417  dbus_message_iter_get_basic(&elem, &value);
418  if (value.str == NULL) {
419  crm_debug("ListUnitFiles reply did not provide a string");
420  continue;
421  }
422  crm_trace("DBus ListUnitFiles listed: %s", value.str);
423 
424  match = systemd_unit_extension(value.str);
425  if (match == NULL) {
426  // This is not a unit file type we know how to manage
427  crm_debug("ListUnitFiles entry '%s' is not supported as resource",
428  value.str);
429  continue;
430  }
431 
432  // ListUnitFiles returns full path names, we just want base name
433  basename = strrchr(value.str, '/');
434  if (basename) {
435  basename = basename + 1;
436  } else {
437  basename = value.str;
438  }
439 
440  if (!strcmp(match, ".service")) {
441  // Service is the "default" unit type, so strip it
442  unit_name = strndup(basename, match - basename);
443  } else {
444  unit_name = strdup(basename);
445  }
446 
447  nfiles++;
448  units = g_list_prepend(units, unit_name);
449  }
450 
451  dbus_message_unref(reply);
452 
453  crm_trace("Found %d manageable systemd unit files", nfiles);
454  units = g_list_sort(units, crm_alpha_sort);
455  return units;
456 }
457 
458 gboolean
459 systemd_unit_exists(const char *name)
460 {
461  char *unit = NULL;
462 
463  /* Note: Makes a blocking dbus calls
464  * Used by resources_find_service_class() when resource class=service
465  */
466  unit = systemd_unit_by_name(name, NULL);
467  if(unit) {
468  free(unit);
469  return TRUE;
470  }
471  return FALSE;
472 }
473 
474 static char *
475 systemd_unit_metadata(const char *name, int timeout)
476 {
477  char *meta = NULL;
478  char *desc = NULL;
479  char *path = systemd_unit_by_name(name, NULL);
480 
481  if (path) {
482  /* TODO: Worth a making blocking call for? Probably not. Possibly if cached. */
483  desc = systemd_get_property(path, "Description", NULL, NULL, NULL,
484  timeout);
485  } else {
486  desc = crm_strdup_printf("Systemd unit file for %s", name);
487  }
488 
489  meta = crm_strdup_printf("<?xml version=\"1.0\"?>\n"
490  "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
491  "<resource-agent name=\"%s\" version=\"" PCMK_DEFAULT_AGENT_VERSION "\">\n"
492  " <version>1.0</version>\n"
493  " <longdesc lang=\"en\">\n"
494  " %s\n"
495  " </longdesc>\n"
496  " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n"
497  " <parameters>\n"
498  " </parameters>\n"
499  " <actions>\n"
500  " <action name=\"start\" timeout=\"100\" />\n"
501  " <action name=\"stop\" timeout=\"100\" />\n"
502  " <action name=\"status\" timeout=\"100\" />\n"
503  " <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n"
504  " <action name=\"meta-data\" timeout=\"5\" />\n"
505  " </actions>\n"
506  " <special tag=\"systemd\">\n"
507  " </special>\n" "</resource-agent>\n", name, desc, name);
508  free(desc);
509  free(path);
510  return meta;
511 }
512 
513 static void
514 systemd_exec_result(DBusMessage *reply, svc_action_t *op)
515 {
516  DBusError error;
517 
518  if (pcmk_dbus_find_error((void*)&error, reply, &error)) {
519 
520  /* ignore "already started" or "not running" errors */
521  if (!systemd_mask_error(op, error.name)) {
522  crm_err("Could not issue %s for %s: %s", op->action, op->rsc, error.message);
523  }
524  dbus_error_free(&error);
525 
526  } else {
527  if(!pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __FUNCTION__, __LINE__)) {
528  crm_warn("Call to %s passed but return type was unexpected", op->action);
529  op->rc = PCMK_OCF_OK;
530 
531  } else {
532  const char *path = NULL;
533 
534  dbus_message_get_args (reply, NULL,
535  DBUS_TYPE_OBJECT_PATH, &path,
536  DBUS_TYPE_INVALID);
537  crm_info("Call to %s passed: %s", op->action, path);
538  op->rc = PCMK_OCF_OK;
539  }
540  }
541 
542  operation_finalize(op);
543 }
544 
545 static void
546 systemd_async_dispatch(DBusPendingCall *pending, void *user_data)
547 {
548  DBusMessage *reply = NULL;
549  svc_action_t *op = user_data;
550 
551  if(pending) {
552  reply = dbus_pending_call_steal_reply(pending);
553  }
554 
555  crm_trace("Got result: %p for %p for %s, %s", reply, pending, op->rsc, op->action);
556 
557  CRM_LOG_ASSERT(pending == op->opaque->pending);
558  services_set_op_pending(op, NULL);
559  systemd_exec_result(reply, op);
560 
561  if(reply) {
562  dbus_message_unref(reply);
563  }
564 }
565 
566 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"
567 
568 /* When the cluster manages a systemd resource, we create a unit file override
569  * to order the service "before" pacemaker. The "before" relationship won't
570  * actually be used, since systemd won't ever start the resource -- we're
571  * interested in the reverse shutdown ordering it creates, to ensure that
572  * systemd doesn't stop the resource at shutdown while pacemaker is still
573  * running.
574  *
575  * @TODO Add start timeout
576  */
577 #define SYSTEMD_OVERRIDE_TEMPLATE \
578  "[Unit]\n" \
579  "Description=Cluster Controlled %s\n" \
580  "Before=pacemaker.service pacemaker_remote.service\n" \
581  "\n" \
582  "[Service]\n" \
583  "Restart=no\n"
584 
585 // Temporarily use rwxr-xr-x umask when opening a file for writing
586 static FILE *
587 create_world_readable(const char *filename)
588 {
589  mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
590  FILE *fp = fopen(filename, "w");
591 
592  umask(orig_umask);
593  return fp;
594 }
595 
596 static void
597 create_override_dir(const char *agent)
598 {
599  char *override_dir = crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
600  "/%s.service.d", agent);
601 
602  crm_build_path(override_dir, 0755);
603  free(override_dir);
604 }
605 
606 static char *
607 get_override_filename(const char *agent)
608 {
610  "/%s.service.d/50-pacemaker.conf", agent);
611 }
612 
613 static void
614 systemd_create_override(const char *agent, int timeout)
615 {
616  FILE *file_strm = NULL;
617  char *override_file = get_override_filename(agent);
618 
619  create_override_dir(agent);
620 
621  /* Ensure the override file is world-readable. This is not strictly
622  * necessary, but it avoids a systemd warning in the logs.
623  */
624  file_strm = create_world_readable(override_file);
625  if (file_strm == NULL) {
626  crm_err("Cannot open systemd override file %s for writing",
627  override_file);
628  } else {
629  char *override = crm_strdup_printf(SYSTEMD_OVERRIDE_TEMPLATE, agent);
630 
631  int rc = fprintf(file_strm, "%s\n", override);
632 
633  free(override);
634  if (rc < 0) {
635  crm_perror(LOG_WARNING, "Cannot write to systemd override file %s",
636  override_file);
637  }
638  fflush(file_strm);
639  fclose(file_strm);
640  systemd_daemon_reload(timeout);
641  }
642 
643  free(override_file);
644 }
645 
646 static void
647 systemd_remove_override(const char *agent, int timeout)
648 {
649  char *override_file = get_override_filename(agent);
650  int rc = unlink(override_file);
651 
652  if (rc < 0) {
653  // Stop may be called when already stopped, which is fine
654  crm_perror(LOG_DEBUG, "Cannot remove systemd override file %s",
655  override_file);
656  } else {
657  systemd_daemon_reload(timeout);
658  }
659  free(override_file);
660 }
661 
662 static void
663 systemd_unit_check(const char *name, const char *state, void *userdata)
664 {
665  svc_action_t * op = userdata;
666 
667  crm_trace("Resource %s has %s='%s'", op->rsc, name, state);
668 
669  if(state == NULL) {
670  op->rc = PCMK_OCF_NOT_RUNNING;
671 
672  } else if (g_strcmp0(state, "active") == 0) {
673  op->rc = PCMK_OCF_OK;
674  } else if (g_strcmp0(state, "reloading") == 0) {
675  op->rc = PCMK_OCF_OK;
676  } else if (g_strcmp0(state, "activating") == 0) {
677  op->rc = PCMK_OCF_PENDING;
678  } else if (g_strcmp0(state, "deactivating") == 0) {
679  op->rc = PCMK_OCF_PENDING;
680  } else {
681  op->rc = PCMK_OCF_NOT_RUNNING;
682  }
683 
684  if (op->synchronous == FALSE) {
685  services_set_op_pending(op, NULL);
686  operation_finalize(op);
687  }
688 }
689 
690 gboolean
692 {
693  const char *method = op->action;
694  DBusMessage *msg = NULL;
695  DBusMessage *reply = NULL;
696 
697  CRM_ASSERT(unit);
698 
699  if (safe_str_eq(op->action, "monitor") || safe_str_eq(method, "status")) {
700  DBusPendingCall *pending = NULL;
701  char *state;
702 
703  state = systemd_get_property(unit, "ActiveState",
704  (op->synchronous? NULL : systemd_unit_check),
705  op, (op->synchronous? NULL : &pending),
706  op->timeout);
707  if (op->synchronous) {
708  systemd_unit_check("ActiveState", state, op);
709  free(state);
710  return op->rc == PCMK_OCF_OK;
711  } else if (pending) {
712  services_set_op_pending(op, pending);
713  return TRUE;
714 
715  } else {
716  return operation_finalize(op);
717  }
718 
719  } else if (g_strcmp0(method, "start") == 0) {
720  method = "StartUnit";
721  systemd_create_override(op->agent, op->timeout);
722 
723  } else if (g_strcmp0(method, "stop") == 0) {
724  method = "StopUnit";
725  systemd_remove_override(op->agent, op->timeout);
726 
727  } else if (g_strcmp0(method, "restart") == 0) {
728  method = "RestartUnit";
729 
730  } else {
732  goto cleanup;
733  }
734 
735  crm_debug("Calling %s for %s: %s", method, op->rsc, unit);
736 
737  msg = systemd_new_method(method);
738  CRM_ASSERT(msg != NULL);
739 
740  /* (ss) */
741  {
742  const char *replace_s = "replace";
743  char *name = systemd_service_name(op->agent);
744 
745  CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
746  CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
747 
748  free(name);
749  }
750 
751  if (op->synchronous == FALSE) {
752  DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
753  op, op->timeout);
754 
755  dbus_message_unref(msg);
756  if(pending) {
757  services_set_op_pending(op, pending);
758  return TRUE;
759 
760  } else {
761  return operation_finalize(op);
762  }
763 
764  } else {
765  reply = systemd_send_recv(msg, NULL, op->timeout);
766  dbus_message_unref(msg);
767  systemd_exec_result(reply, op);
768 
769  if(reply) {
770  dbus_message_unref(reply);
771  }
772  return FALSE;
773  }
774 
775  cleanup:
776  if (op->synchronous == FALSE) {
777  return operation_finalize(op);
778  }
779 
780  return op->rc == PCMK_OCF_OK;
781 }
782 
783 static gboolean
784 systemd_timeout_callback(gpointer p)
785 {
786  svc_action_t * op = p;
787 
788  op->opaque->timerid = 0;
789  crm_warn("%s operation on systemd unit %s named '%s' timed out", op->action, op->agent, op->rsc);
790  operation_finalize(op);
791 
792  return FALSE;
793 }
794 
795 /* For an asynchronous 'op', returns FALSE if 'op' should be free'd by the caller */
796 /* For a synchronous 'op', returns FALSE if 'op' fails */
797 gboolean
799 {
800  char *unit = NULL;
801 
802  CRM_ASSERT(op);
803  CRM_ASSERT(systemd_init());
805  crm_debug("Performing %ssynchronous %s op on systemd unit %s named '%s'",
806  op->synchronous ? "" : "a", op->action, op->agent, op->rsc);
807 
808  if (safe_str_eq(op->action, "meta-data")) {
809  // @TODO Implement an async meta-data call in executor API
810  op->stdout_data = systemd_unit_metadata(op->agent, op->timeout);
811  op->rc = PCMK_OCF_OK;
812 
813  if (op->synchronous == FALSE) {
814  return operation_finalize(op);
815  }
816  return TRUE;
817  }
818 
819  unit = systemd_unit_by_name(op->agent, op);
820  free(unit);
821 
822  if (op->synchronous == FALSE) {
823  if (op->opaque->pending) {
824  op->opaque->timerid = g_timeout_add(op->timeout + 5000, systemd_timeout_callback, op);
826  return TRUE;
827 
828  } else {
829  return operation_finalize(op);
830  }
831  }
832 
833  return op->rc == PCMK_OCF_OK;
834 }
Services API.
#define CRM_CHECK(expr, failure_action)
Definition: logging.h:165
A dumping ground.
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
Definition: io.c:39
char * id
Definition: services.h:147
void pcmk_dbus_disconnect(DBusConnection *connection)
Definition: dbus.c:55
#define DBUS_TIMEOUT_USE_DEFAULT
Definition: pcmk-dbus.h:14
char * rsc
Definition: services.h:148
#define CRM_LOG_ASSERT(expr)
Definition: logging.h:151
Wrappers for and extensions to glib mainloop.
bool pcmk_dbus_find_error(DBusPendingCall *pending, DBusMessage *reply, DBusError *ret)
Definition: dbus.c:76
char * strndup(const char *str, size_t len)
#define crm_warn(fmt, args...)
Definition: logging.h:250
svc_action_private_t * opaque
Definition: services.h:179
#define crm_debug(fmt, args...)
Definition: logging.h:254
#define BUS_NAME_UNIT
Definition: systemd.c:24
gint crm_alpha_sort(gconstpointer a, gconstpointer b)
Compare two strings alphabetically (case-insensitive)
Definition: strings.c:473
gboolean operation_finalize(svc_action_t *op)
char * stdout_data
Definition: services.h:169
gboolean systemd_unit_exists(const char *name)
Definition: systemd.c:459
#define crm_trace(fmt, args...)
Definition: logging.h:255
#define BUS_NAME
Definition: systemd.c:22
char * agent
Definition: services.h:154
int synchronous
Definition: services.h:165
#define BUS_PATH
Definition: systemd.c:25
char * pcmk_dbus_get_property(DBusConnection *connection, const char *target, const char *obj, const gchar *iface, const char *name, void(*callback)(const char *name, const char *value, void *userdata), void *userdata, DBusPendingCall **pending, int timeout)
Definition: dbus.c:408
#define SYSTEMD_OVERRIDE_ROOT
Definition: systemd.c:566
GList * systemd_unit_listall(void)
Definition: systemd.c:362
DBusPendingCall * pcmk_dbus_send(DBusMessage *msg, DBusConnection *connection, void(*done)(DBusPendingCall *pending, void *user_data), void *user_data, int timeout)
Definition: dbus.c:223
DBusConnection * pcmk_dbus_connect(void)
Definition: dbus.c:36
void services_add_inflight_op(svc_action_t *op)
Definition: services.c:698
void systemd_cleanup(void)
Definition: systemd.c:135
char * action
Definition: services.h:149
#define crm_perror(level, fmt, args...)
Log a system error message.
Definition: logging.h:227
#define crm_err(fmt, args...)
Definition: logging.h:249
#define CRM_ASSERT(expr)
Definition: results.h:20
#define PCMK_DEFAULT_AGENT_VERSION
Definition: services.h:56
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line)
Definition: dbus.c:270
gboolean systemd_unit_exec_with_unit(svc_action_t *op, const char *unit)
Definition: systemd.c:691
#define safe_str_eq(a, b)
Definition: util.h:54
#define SYSTEMD_OVERRIDE_TEMPLATE
Definition: systemd.c:577
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
DBusMessage * pcmk_dbus_send_recv(DBusMessage *msg, DBusConnection *connection, DBusError *error, int timeout)
Definition: dbus.c:159
gboolean systemd_unit_exec(svc_action_t *op)
Definition: systemd.c:798
#define crm_info(fmt, args...)
Definition: logging.h:252
#define BUS_NAME_MANAGER
Definition: systemd.c:23