17 #include <dbus/dbus.h> 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" 27 static inline DBusMessage *
28 systemd_new_method(
const char *method)
39 static DBusConnection* systemd_proxy = NULL;
41 static inline DBusPendingCall *
42 systemd_send(DBusMessage *msg,
43 void(*done)(DBusPendingCall *pending,
void *user_data),
44 void *user_data,
int timeout)
46 return pcmk_dbus_send(msg, systemd_proxy, done, user_data, timeout);
49 static inline DBusMessage *
50 systemd_send_recv(DBusMessage *msg, DBusError *error,
int timeout)
67 systemd_call_simple_method(
const char *method)
69 DBusMessage *msg = systemd_new_method(method);
70 DBusMessage *reply = NULL;
77 crm_err(
"Could not create message to send %s to systemd", method);
81 dbus_error_init(&error);
83 dbus_message_unref(msg);
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);
91 }
else if (reply == NULL) {
92 crm_err(
"Could not send %s to systemd: no reply received", method);
102 static int need_init = 1;
106 && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
107 crm_warn(
"Connection to System DBus is closed. Reconnecting...");
109 systemd_proxy = NULL;
117 if (systemd_proxy == NULL) {
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)
128 return systemd_proxy?
130 name, callback, userdata, pending, timeout)
139 systemd_proxy = NULL;
156 systemd_unit_extension(
const char *name)
159 const char *dot = strrchr(name,
'.');
161 if (dot && (!strcmp(dot,
".service")
162 || !strcmp(dot,
".socket")
163 || !strcmp(dot,
".mount")
164 || !strcmp(dot,
".timer")
165 || !strcmp(dot,
".path"))) {
173 systemd_service_name(
const char *name)
179 if (systemd_unit_extension(name)) {
187 systemd_daemon_reload_complete(DBusPendingCall *pending,
void *user_data)
190 DBusMessage *reply = NULL;
191 unsigned int reload_count = GPOINTER_TO_UINT(user_data);
193 dbus_error_init(&error);
195 reply = dbus_pending_call_steal_reply(pending);
199 crm_err(
"Could not issue systemd reload %d: %s", reload_count, error.message);
200 dbus_error_free(&error);
203 crm_trace(
"Reload %d complete", reload_count);
207 dbus_pending_call_unref(pending);
210 dbus_message_unref(reply);
215 systemd_daemon_reload(
int timeout)
217 static unsigned int reload_count = 0;
218 DBusMessage *msg = systemd_new_method(
"Reload");
222 systemd_send(msg, systemd_daemon_reload_complete,
223 GUINT_TO_POINTER(reload_count), timeout);
224 dbus_message_unref(msg);
233 if(strstr(error,
"org.freedesktop.systemd1.InvalidName")
234 || strstr(error,
"org.freedesktop.systemd1.LoadFailed")
235 || strstr(error,
"org.freedesktop.systemd1.NoSuchUnit")) {
238 crm_trace(
"Masking %s failure for %s: unknown services are stopped", op->
action, op->
rsc);
243 crm_trace(
"Mapping %s failure for %s: unknown services are not installed", op->
action, op->
rsc);
254 systemd_loadunit_result(DBusMessage *reply,
svc_action_t * op)
256 const char *path = NULL;
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);
264 dbus_error_free(&error);
267 dbus_message_get_args (reply, NULL,
268 DBUS_TYPE_OBJECT_PATH, &path,
286 systemd_loadunit_cb(DBusPendingCall *pending,
void *user_data)
288 DBusMessage *reply = NULL;
292 reply = dbus_pending_call_steal_reply(pending);
295 crm_trace(
"Got result: %p for %p / %p for %s", reply, pending, op->
opaque->pending, op->
id);
298 services_set_op_pending(op, NULL);
300 systemd_loadunit_result(reply, user_data);
303 dbus_message_unref(reply);
308 systemd_unit_by_name(
const gchar * arg_name,
svc_action_t *op)
311 DBusMessage *reply = NULL;
312 DBusPendingCall* pending = NULL;
323 if (systemd_init() == FALSE) {
327 msg = systemd_new_method(
"LoadUnit");
330 name = systemd_service_name(arg_name);
331 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
335 const char *unit = NULL;
338 reply = systemd_send_recv(msg, NULL,
340 dbus_message_unref(msg);
342 unit = systemd_loadunit_result(reply, op);
344 munit = strdup(unit);
347 dbus_message_unref(reply);
352 pending = systemd_send(msg, systemd_loadunit_cb, op, op->
timeout);
354 services_set_op_pending(op, pending);
357 dbus_message_unref(msg);
366 DBusMessageIter args;
367 DBusMessageIter unit;
368 DBusMessageIter elem;
369 DBusMessage *reply = NULL;
371 if (systemd_init() == FALSE) {
381 reply = systemd_call_simple_method(
"ListUnitFiles");
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);
391 __FUNCTION__, __LINE__)) {
392 crm_err(
"Could not list systemd unit files: systemd reply has invalid arguments");
393 dbus_message_unref(reply);
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)) {
401 DBusBasicValue value;
402 const char *match = NULL;
403 char *unit_name = NULL;
404 char *basename = NULL;
407 crm_debug(
"ListUnitFiles reply has unexpected type");
411 dbus_message_iter_recurse(&unit, &elem);
413 crm_debug(
"ListUnitFiles reply does not contain a string");
417 dbus_message_iter_get_basic(&elem, &value);
418 if (value.str == NULL) {
419 crm_debug(
"ListUnitFiles reply did not provide a string");
422 crm_trace(
"DBus ListUnitFiles listed: %s", value.str);
424 match = systemd_unit_extension(value.str);
427 crm_debug(
"ListUnitFiles entry '%s' is not supported as resource",
433 basename = strrchr(value.str,
'/');
435 basename = basename + 1;
437 basename = value.str;
440 if (!strcmp(match,
".service")) {
442 unit_name =
strndup(basename, match - basename);
444 unit_name = strdup(basename);
448 units = g_list_prepend(units, unit_name);
451 dbus_message_unref(reply);
453 crm_trace(
"Found %d manageable systemd unit files", nfiles);
466 unit = systemd_unit_by_name(name, NULL);
475 systemd_unit_metadata(
const char *name,
int timeout)
479 char *path = systemd_unit_by_name(name, NULL);
483 desc = systemd_get_property(path,
"Description", NULL, NULL, NULL,
490 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n" 492 " <version>1.0</version>\n" 493 " <longdesc lang=\"en\">\n" 496 " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\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" 506 " <special tag=\"systemd\">\n" 507 " </special>\n" "</resource-agent>\n", name, desc, name);
514 systemd_exec_result(DBusMessage *reply,
svc_action_t *op)
521 if (!systemd_mask_error(op, error.name)) {
522 crm_err(
"Could not issue %s for %s: %s", op->
action, op->
rsc, error.message);
524 dbus_error_free(&error);
528 crm_warn(
"Call to %s passed but return type was unexpected", op->
action);
532 const char *path = NULL;
534 dbus_message_get_args (reply, NULL,
535 DBUS_TYPE_OBJECT_PATH, &path,
546 systemd_async_dispatch(DBusPendingCall *pending,
void *user_data)
548 DBusMessage *reply = NULL;
552 reply = dbus_pending_call_steal_reply(pending);
558 services_set_op_pending(op, NULL);
559 systemd_exec_result(reply, op);
562 dbus_message_unref(reply);
566 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/" 577 #define SYSTEMD_OVERRIDE_TEMPLATE \ 579 "Description=Cluster Controlled %s\n" \ 580 "Before=pacemaker.service pacemaker_remote.service\n" \ 587 create_world_readable(
const char *filename)
589 mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
590 FILE *fp = fopen(filename,
"w");
597 create_override_dir(
const char *agent)
600 "/%s.service.d", agent);
607 get_override_filename(
const char *agent)
610 "/%s.service.d/50-pacemaker.conf", agent);
614 systemd_create_override(
const char *agent,
int timeout)
616 FILE *file_strm = NULL;
617 char *override_file = get_override_filename(agent);
619 create_override_dir(agent);
624 file_strm = create_world_readable(override_file);
625 if (file_strm == NULL) {
626 crm_err(
"Cannot open systemd override file %s for writing",
631 int rc = fprintf(file_strm,
"%s\n",
override);
635 crm_perror(LOG_WARNING,
"Cannot write to systemd override file %s",
640 systemd_daemon_reload(timeout);
647 systemd_remove_override(
const char *agent,
int timeout)
649 char *override_file = get_override_filename(agent);
650 int rc = unlink(override_file);
654 crm_perror(LOG_DEBUG,
"Cannot remove systemd override file %s",
657 systemd_daemon_reload(timeout);
663 systemd_unit_check(
const char *name,
const char *state,
void *userdata)
667 crm_trace(
"Resource %s has %s='%s'", op->
rsc, name, state);
672 }
else if (g_strcmp0(state,
"active") == 0) {
674 }
else if (g_strcmp0(state,
"reloading") == 0) {
676 }
else if (g_strcmp0(state,
"activating") == 0) {
678 }
else if (g_strcmp0(state,
"deactivating") == 0) {
685 services_set_op_pending(op, NULL);
693 const char *method = op->
action;
694 DBusMessage *msg = NULL;
695 DBusMessage *reply = NULL;
700 DBusPendingCall *pending = NULL;
703 state = systemd_get_property(unit,
"ActiveState",
708 systemd_unit_check(
"ActiveState", state, op);
711 }
else if (pending) {
712 services_set_op_pending(op, pending);
719 }
else if (g_strcmp0(method,
"start") == 0) {
720 method =
"StartUnit";
723 }
else if (g_strcmp0(method,
"stop") == 0) {
727 }
else if (g_strcmp0(method,
"restart") == 0) {
728 method =
"RestartUnit";
735 crm_debug(
"Calling %s for %s: %s", method, op->
rsc, unit);
737 msg = systemd_new_method(method);
742 const char *replace_s =
"replace";
743 char *name = systemd_service_name(op->
agent);
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));
752 DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
755 dbus_message_unref(msg);
757 services_set_op_pending(op, pending);
765 reply = systemd_send_recv(msg, NULL, op->
timeout);
766 dbus_message_unref(msg);
767 systemd_exec_result(reply, op);
770 dbus_message_unref(reply);
784 systemd_timeout_callback(gpointer p)
805 crm_debug(
"Performing %ssynchronous %s op on systemd unit %s named '%s'",
819 unit = systemd_unit_by_name(op->
agent, op);
823 if (op->
opaque->pending) {
824 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...)
gint crm_alpha_sort(gconstpointer a, gconstpointer b)
Compare two strings alphabetically (case-insensitive)
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...)
Log a system error message.
#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)
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#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...)