19 #include <dbus/dbus.h> 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" 29 static inline DBusMessage *
30 systemd_new_method(
const char *method)
41 static DBusConnection* systemd_proxy = NULL;
43 static inline DBusPendingCall *
44 systemd_send(DBusMessage *msg,
45 void(*done)(DBusPendingCall *pending,
void *user_data),
51 static inline DBusMessage *
52 systemd_send_recv(DBusMessage *msg, DBusError *error,
int timeout)
69 systemd_call_simple_method(
const char *method)
71 DBusMessage *msg = systemd_new_method(method);
72 DBusMessage *reply = NULL;
79 crm_err(
"Could not create message to send %s to systemd", method);
83 dbus_error_init(&error);
85 dbus_message_unref(msg);
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);
93 }
else if (reply == NULL) {
94 crm_err(
"Could not send %s to systemd: no reply received", method);
104 static int need_init = 1;
108 && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
109 crm_warn(
"Connection to System DBus is closed. Reconnecting...");
111 systemd_proxy = NULL;
119 if (systemd_proxy == NULL) {
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)
130 return systemd_proxy?
141 systemd_proxy = NULL;
158 systemd_unit_extension(
const char *
name)
161 const char *dot = strrchr(
name,
'.');
163 if (dot && (!strcmp(dot,
".service")
164 || !strcmp(dot,
".socket")
165 || !strcmp(dot,
".mount")
166 || !strcmp(dot,
".timer")
167 || !strcmp(dot,
".path"))) {
175 systemd_service_name(
const char *
name)
181 if (systemd_unit_extension(
name)) {
189 systemd_daemon_reload_complete(DBusPendingCall *pending,
void *user_data)
192 DBusMessage *reply = NULL;
193 unsigned int reload_count = GPOINTER_TO_UINT(user_data);
195 dbus_error_init(&error);
197 reply = dbus_pending_call_steal_reply(pending);
201 crm_err(
"Could not issue systemd reload %d: %s", reload_count, error.message);
202 dbus_error_free(&error);
205 crm_trace(
"Reload %d complete", reload_count);
209 dbus_pending_call_unref(pending);
212 dbus_message_unref(reply);
217 systemd_daemon_reload(
int timeout)
219 static unsigned int reload_count = 0;
220 DBusMessage *msg = systemd_new_method(
"Reload");
224 systemd_send(msg, systemd_daemon_reload_complete,
225 GUINT_TO_POINTER(reload_count),
timeout);
226 dbus_message_unref(msg);
235 if(strstr(error,
"org.freedesktop.systemd1.InvalidName")
236 || strstr(error,
"org.freedesktop.systemd1.LoadFailed")
237 || strstr(error,
"org.freedesktop.systemd1.NoSuchUnit")) {
240 crm_trace(
"Masking %s failure for %s: unknown services are stopped", op->
action, op->
rsc);
245 crm_trace(
"Mapping %s failure for %s: unknown services are not installed", op->
action, op->
rsc);
256 systemd_loadunit_result(DBusMessage *reply,
svc_action_t * op)
258 const char *path = NULL;
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);
266 dbus_error_free(&error);
269 __func__, __LINE__)) {
270 crm_err(
"Could not load systemd unit %s for %s: " 271 "systemd reply has unexpected type", op->
agent, op->
id);
274 dbus_message_get_args (reply, NULL,
275 DBUS_TYPE_OBJECT_PATH, &path,
293 systemd_loadunit_cb(DBusPendingCall *pending,
void *user_data)
295 DBusMessage *reply = NULL;
299 reply = dbus_pending_call_steal_reply(pending);
302 crm_trace(
"Got result: %p for %p / %p for %s", reply, pending, op->
opaque->pending, op->
id);
305 services_set_op_pending(op, NULL);
307 systemd_loadunit_result(reply, user_data);
310 dbus_message_unref(reply);
315 systemd_unit_by_name(
const gchar * arg_name,
svc_action_t *op)
318 DBusMessage *reply = NULL;
319 DBusPendingCall* pending = NULL;
330 if (systemd_init() == FALSE) {
334 msg = systemd_new_method(
"LoadUnit");
337 name = systemd_service_name(arg_name);
338 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &
name, DBUS_TYPE_INVALID));
342 const char *unit = NULL;
345 reply = systemd_send_recv(msg, NULL,
347 dbus_message_unref(msg);
349 unit = systemd_loadunit_result(reply, op);
351 munit = strdup(unit);
354 dbus_message_unref(reply);
359 pending = systemd_send(msg, systemd_loadunit_cb, op, op->
timeout);
361 services_set_op_pending(op, pending);
364 dbus_message_unref(msg);
381 sort_str(gconstpointer a, gconstpointer b)
390 return strcasecmp(a, b);
398 DBusMessageIter args;
399 DBusMessageIter unit;
400 DBusMessageIter elem;
401 DBusMessage *reply = NULL;
403 if (systemd_init() == FALSE) {
413 reply = systemd_call_simple_method(
"ListUnitFiles");
417 if (!dbus_message_iter_init(reply, &args)) {
418 crm_err(
"Could not list systemd unit files: systemd reply has no arguments");
419 dbus_message_unref(reply);
423 __func__, __LINE__)) {
424 crm_err(
"Could not list systemd unit files: systemd reply has invalid arguments");
425 dbus_message_unref(reply);
429 dbus_message_iter_recurse(&args, &unit);
430 for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
431 dbus_message_iter_next(&unit)) {
433 DBusBasicValue value;
434 const char *match = NULL;
435 char *unit_name = NULL;
436 char *basename = NULL;
439 crm_warn(
"Skipping systemd reply argument with unexpected type");
443 dbus_message_iter_recurse(&unit, &elem);
445 crm_warn(
"Skipping systemd reply argument with no string");
449 dbus_message_iter_get_basic(&elem, &value);
450 if (value.str == NULL) {
451 crm_debug(
"ListUnitFiles reply did not provide a string");
454 crm_trace(
"DBus ListUnitFiles listed: %s", value.str);
456 match = systemd_unit_extension(value.str);
459 crm_debug(
"ListUnitFiles entry '%s' is not supported as resource",
465 basename = strrchr(value.str,
'/');
467 basename = basename + 1;
469 basename = value.str;
472 if (!strcmp(match,
".service")) {
474 unit_name =
strndup(basename, match - basename);
476 unit_name = strdup(basename);
480 units = g_list_prepend(units, unit_name);
483 dbus_message_unref(reply);
485 crm_trace(
"Found %d manageable systemd unit files", nfiles);
486 units = g_list_sort(units, sort_str);
498 unit = systemd_unit_by_name(
name, NULL);
507 systemd_unit_metadata(
const char *
name,
int timeout)
511 char *path = systemd_unit_by_name(
name, NULL);
515 desc = systemd_get_property(path,
"Description", NULL, NULL, NULL,
522 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n" 524 " <version>1.0</version>\n" 525 " <longdesc lang=\"en\">\n" 528 " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n" 532 " <action name=\"start\" timeout=\"100\" />\n" 533 " <action name=\"stop\" timeout=\"100\" />\n" 534 " <action name=\"status\" timeout=\"100\" />\n" 535 " <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n" 536 " <action name=\"meta-data\" timeout=\"5\" />\n" 538 " <special tag=\"systemd\">\n" 539 " </special>\n" "</resource-agent>\n",
name, desc,
name);
546 systemd_exec_result(DBusMessage *reply,
svc_action_t *op)
553 if (!systemd_mask_error(op, error.name)) {
554 crm_err(
"Could not issue %s for %s: %s", op->
action, op->
rsc, error.message);
556 dbus_error_free(&error);
560 crm_warn(
"Call to %s passed but return type was unexpected", op->
action);
564 const char *path = NULL;
566 dbus_message_get_args (reply, NULL,
567 DBUS_TYPE_OBJECT_PATH, &path,
578 systemd_async_dispatch(DBusPendingCall *pending,
void *user_data)
580 DBusMessage *reply = NULL;
584 reply = dbus_pending_call_steal_reply(pending);
590 services_set_op_pending(op, NULL);
591 systemd_exec_result(reply, op);
594 dbus_message_unref(reply);
598 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/" 609 #define SYSTEMD_OVERRIDE_TEMPLATE \ 611 "Description=Cluster Controlled %s\n" \ 612 "Before=pacemaker.service pacemaker_remote.service\n" \ 619 create_world_readable(
const char *filename)
621 mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
622 FILE *fp = fopen(filename,
"w");
629 create_override_dir(
const char *agent)
632 "/%s.service.d", agent);
636 crm_warn(
"Could not create systemd override directory %s: %s",
643 get_override_filename(
const char *agent)
646 "/%s.service.d/50-pacemaker.conf", agent);
650 systemd_create_override(
const char *agent,
int timeout)
652 FILE *file_strm = NULL;
653 char *override_file = get_override_filename(agent);
655 create_override_dir(agent);
660 file_strm = create_world_readable(override_file);
661 if (file_strm == NULL) {
662 crm_err(
"Cannot open systemd override file %s for writing",
667 int rc = fprintf(file_strm,
"%s\n",
override);
671 crm_perror(LOG_WARNING,
"Cannot write to systemd override file %s",
676 systemd_daemon_reload(
timeout);
683 systemd_remove_override(
const char *agent,
int timeout)
685 char *override_file = get_override_filename(agent);
686 int rc = unlink(override_file);
690 crm_perror(LOG_DEBUG,
"Cannot remove systemd override file %s",
693 systemd_daemon_reload(
timeout);
699 systemd_unit_check(
const char *
name,
const char *state,
void *userdata)
708 }
else if (g_strcmp0(state,
"active") == 0) {
710 }
else if (g_strcmp0(state,
"reloading") == 0) {
712 }
else if (g_strcmp0(state,
"activating") == 0) {
714 }
else if (g_strcmp0(state,
"deactivating") == 0) {
721 services_set_op_pending(op, NULL);
729 const char *method = op->
action;
730 DBusMessage *msg = NULL;
731 DBusMessage *reply = NULL;
736 DBusPendingCall *pending = NULL;
739 state = systemd_get_property(unit,
"ActiveState",
744 systemd_unit_check(
"ActiveState", state, op);
747 }
else if (pending) {
748 services_set_op_pending(op, pending);
755 }
else if (g_strcmp0(method,
"start") == 0) {
756 method =
"StartUnit";
759 }
else if (g_strcmp0(method,
"stop") == 0) {
763 }
else if (g_strcmp0(method,
"restart") == 0) {
764 method =
"RestartUnit";
771 crm_debug(
"Calling %s for %s: %s", method, op->
rsc, unit);
773 msg = systemd_new_method(method);
778 const char *replace_s =
"replace";
779 char *
name = systemd_service_name(op->
agent);
781 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &
name, DBUS_TYPE_INVALID));
782 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
788 DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
791 dbus_message_unref(msg);
793 services_set_op_pending(op, pending);
801 reply = systemd_send_recv(msg, NULL, op->
timeout);
802 dbus_message_unref(msg);
803 systemd_exec_result(reply, op);
806 dbus_message_unref(reply);
820 systemd_timeout_callback(gpointer p)
841 crm_debug(
"Performing %ssynchronous %s op on systemd unit %s named '%s'",
855 unit = systemd_unit_by_name(op->
agent, op);
859 if (op->
opaque->pending) {
860 op->
opaque->timerid = g_timeout_add(op->
timeout + 5000, systemd_timeout_callback, op);
#define CRM_CHECK(expr, failure_action)
void pcmk_dbus_disconnect(DBusConnection *connection)
#define DBUS_TIMEOUT_USE_DEFAULT
#define CRM_LOG_ASSERT(expr)
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
Wrappers for and extensions to glib mainloop.
bool pcmk_dbus_find_error(DBusPendingCall *pending, DBusMessage *reply, DBusError *ret)
char * strndup(const char *str, size_t len)
#define crm_warn(fmt, args...)
svc_action_private_t * opaque
#define crm_debug(fmt, args...)
gboolean operation_finalize(svc_action_t *op)
gboolean systemd_unit_exists(const char *name)
#define crm_trace(fmt, args...)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
#define SYSTEMD_OVERRIDE_ROOT
GList * systemd_unit_listall(void)
DBusPendingCall * pcmk_dbus_send(DBusMessage *msg, DBusConnection *connection, void(*done)(DBusPendingCall *pending, void *user_data), void *user_data, int timeout)
DBusConnection * pcmk_dbus_connect(void)
void services_add_inflight_op(svc_action_t *op)
void systemd_cleanup(void)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
#define PCMK_DEFAULT_AGENT_VERSION
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line)
gboolean systemd_unit_exec_with_unit(svc_action_t *op, const char *unit)
char * pcmk_dbus_get_property(DBusConnection *connection, const char *target, const char *obj, const gchar *iface, const char *name, property_callback_func callback, void *userdata, DBusPendingCall **pending, int timeout)
#define SYSTEMD_OVERRIDE_TEMPLATE
DBusMessage * pcmk_dbus_send_recv(DBusMessage *msg, DBusConnection *connection, DBusError *error, int timeout)
gboolean systemd_unit_exec(svc_action_t *op)
#define crm_info(fmt, args...)
int pcmk__build_path(const char *path_c, mode_t mode)