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),
48 return pcmk_dbus_send(msg, systemd_proxy, done, user_data, timeout);
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?
132 name, callback, userdata, pending, timeout)
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 dbus_message_get_args (reply, NULL,
270 DBUS_TYPE_OBJECT_PATH, &path,
288 systemd_loadunit_cb(DBusPendingCall *pending,
void *user_data)
290 DBusMessage *reply = NULL;
294 reply = dbus_pending_call_steal_reply(pending);
297 crm_trace(
"Got result: %p for %p / %p for %s", reply, pending, op->
opaque->pending, op->
id);
300 services_set_op_pending(op, NULL);
302 systemd_loadunit_result(reply, user_data);
305 dbus_message_unref(reply);
310 systemd_unit_by_name(
const gchar * arg_name,
svc_action_t *op)
313 DBusMessage *reply = NULL;
314 DBusPendingCall* pending = NULL;
325 if (systemd_init() == FALSE) {
329 msg = systemd_new_method(
"LoadUnit");
332 name = systemd_service_name(arg_name);
333 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
337 const char *unit = NULL;
340 reply = systemd_send_recv(msg, NULL,
342 dbus_message_unref(msg);
344 unit = systemd_loadunit_result(reply, op);
346 munit = strdup(unit);
349 dbus_message_unref(reply);
354 pending = systemd_send(msg, systemd_loadunit_cb, op, op->
timeout);
356 services_set_op_pending(op, pending);
359 dbus_message_unref(msg);
376 sort_str(gconstpointer a, gconstpointer b)
385 return strcasecmp(a, b);
393 DBusMessageIter args;
394 DBusMessageIter unit;
395 DBusMessageIter elem;
396 DBusMessage *reply = NULL;
398 if (systemd_init() == FALSE) {
408 reply = systemd_call_simple_method(
"ListUnitFiles");
412 if (!dbus_message_iter_init(reply, &args)) {
413 crm_err(
"Could not list systemd unit files: systemd reply has no arguments");
414 dbus_message_unref(reply);
418 __FUNCTION__, __LINE__)) {
419 crm_err(
"Could not list systemd unit files: systemd reply has invalid arguments");
420 dbus_message_unref(reply);
424 dbus_message_iter_recurse(&args, &unit);
425 for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
426 dbus_message_iter_next(&unit)) {
428 DBusBasicValue value;
429 const char *match = NULL;
430 char *unit_name = NULL;
431 char *basename = NULL;
434 crm_debug(
"ListUnitFiles reply has unexpected type");
438 dbus_message_iter_recurse(&unit, &elem);
440 crm_debug(
"ListUnitFiles reply does not contain a string");
444 dbus_message_iter_get_basic(&elem, &value);
445 if (value.str == NULL) {
446 crm_debug(
"ListUnitFiles reply did not provide a string");
449 crm_trace(
"DBus ListUnitFiles listed: %s", value.str);
451 match = systemd_unit_extension(value.str);
454 crm_debug(
"ListUnitFiles entry '%s' is not supported as resource",
460 basename = strrchr(value.str,
'/');
462 basename = basename + 1;
464 basename = value.str;
467 if (!strcmp(match,
".service")) {
469 unit_name =
strndup(basename, match - basename);
471 unit_name = strdup(basename);
475 units = g_list_prepend(units, unit_name);
478 dbus_message_unref(reply);
480 crm_trace(
"Found %d manageable systemd unit files", nfiles);
481 units = g_list_sort(units, sort_str);
493 unit = systemd_unit_by_name(name, NULL);
502 systemd_unit_metadata(
const char *name,
int timeout)
506 char *path = systemd_unit_by_name(name, NULL);
510 desc = systemd_get_property(path,
"Description", NULL, NULL, NULL,
517 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
519 " <version>1.0</version>\n"
520 " <longdesc lang=\"en\">\n"
523 " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n"
527 " <action name=\"start\" timeout=\"100\" />\n"
528 " <action name=\"stop\" timeout=\"100\" />\n"
529 " <action name=\"status\" timeout=\"100\" />\n"
530 " <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n"
531 " <action name=\"meta-data\" timeout=\"5\" />\n"
533 " <special tag=\"systemd\">\n"
534 " </special>\n" "</resource-agent>\n", name, desc, name);
541 systemd_exec_result(DBusMessage *reply,
svc_action_t *op)
548 if (!systemd_mask_error(op, error.name)) {
549 crm_err(
"Could not issue %s for %s: %s", op->
action, op->
rsc, error.message);
551 dbus_error_free(&error);
555 crm_warn(
"Call to %s passed but return type was unexpected", op->
action);
559 const char *path = NULL;
561 dbus_message_get_args (reply, NULL,
562 DBUS_TYPE_OBJECT_PATH, &path,
573 systemd_async_dispatch(DBusPendingCall *pending,
void *user_data)
575 DBusMessage *reply = NULL;
579 reply = dbus_pending_call_steal_reply(pending);
585 services_set_op_pending(op, NULL);
586 systemd_exec_result(reply, op);
589 dbus_message_unref(reply);
593 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"
604 #define SYSTEMD_OVERRIDE_TEMPLATE \
606 "Description=Cluster Controlled %s\n" \
607 "Before=pacemaker.service pacemaker_remote.service\n" \
614 create_world_readable(
const char *filename)
616 mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
617 FILE *fp = fopen(filename,
"w");
624 create_override_dir(
const char *agent)
627 "/%s.service.d", agent);
634 get_override_filename(
const char *agent)
637 "/%s.service.d/50-pacemaker.conf", agent);
641 systemd_create_override(
const char *agent,
int timeout)
643 FILE *file_strm = NULL;
644 char *override_file = get_override_filename(agent);
646 create_override_dir(agent);
651 file_strm = create_world_readable(override_file);
652 if (file_strm == NULL) {
653 crm_err(
"Cannot open systemd override file %s for writing",
658 int rc = fprintf(file_strm,
"%s\n",
override);
662 crm_perror(LOG_WARNING,
"Cannot write to systemd override file %s",
667 systemd_daemon_reload(timeout);
674 systemd_remove_override(
const char *agent,
int timeout)
676 char *override_file = get_override_filename(agent);
677 int rc = unlink(override_file);
681 crm_perror(LOG_DEBUG,
"Cannot remove systemd override file %s",
684 systemd_daemon_reload(timeout);
690 systemd_unit_check(
const char *name,
const char *state,
void *userdata)
694 crm_trace(
"Resource %s has %s='%s'", op->
rsc, name, state);
699 }
else if (g_strcmp0(state,
"active") == 0) {
701 }
else if (g_strcmp0(state,
"reloading") == 0) {
703 }
else if (g_strcmp0(state,
"activating") == 0) {
705 }
else if (g_strcmp0(state,
"deactivating") == 0) {
712 services_set_op_pending(op, NULL);
720 const char *method = op->
action;
721 DBusMessage *msg = NULL;
722 DBusMessage *reply = NULL;
727 DBusPendingCall *pending = NULL;
730 state = systemd_get_property(unit,
"ActiveState",
735 systemd_unit_check(
"ActiveState", state, op);
738 }
else if (pending) {
739 services_set_op_pending(op, pending);
746 }
else if (g_strcmp0(method,
"start") == 0) {
747 method =
"StartUnit";
750 }
else if (g_strcmp0(method,
"stop") == 0) {
754 }
else if (g_strcmp0(method,
"restart") == 0) {
755 method =
"RestartUnit";
762 crm_debug(
"Calling %s for %s: %s", method, op->
rsc, unit);
764 msg = systemd_new_method(method);
769 const char *replace_s =
"replace";
770 char *name = systemd_service_name(op->
agent);
772 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
773 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
779 DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
782 dbus_message_unref(msg);
784 services_set_op_pending(op, pending);
792 reply = systemd_send_recv(msg, NULL, op->
timeout);
793 dbus_message_unref(msg);
794 systemd_exec_result(reply, op);
797 dbus_message_unref(reply);
811 systemd_timeout_callback(gpointer p)
832 crm_debug(
"Performing %ssynchronous %s op on systemd unit %s named '%s'",
846 unit = systemd_unit_by_name(op->
agent, op);
850 if (op->
opaque->pending) {
851 op->
opaque->timerid = g_timeout_add(op->
timeout + 5000, systemd_timeout_callback, op);
#define CRM_CHECK(expr, failure_action)
void crm_build_path(const char *path_c, mode_t mode)
Create a directory, including any parent directories needed.
void pcmk_dbus_disconnect(DBusConnection *connection)
#define DBUS_TIMEOUT_USE_DEFAULT
#define CRM_LOG_ASSERT(expr)
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 * 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)
#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)
#define safe_str_eq(a, b)
#define SYSTEMD_OVERRIDE_TEMPLATE
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
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...)