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