27 #include <dbus/dbus.h>
30 #define BUS_NAME "org.freedesktop.systemd1"
31 #define BUS_PATH "/org/freedesktop/systemd1"
33 #define BUS_PROPERTY_IFACE "org.freedesktop.DBus.Properties"
44 const char *description;
45 const char *load_state;
46 const char *active_state;
47 const char *sub_state;
48 const char *following;
49 const char *unit_path;
64 static DBusMessage *systemd_new_method(
const char *iface,
const char *method)
66 crm_trace(
"Calling: %s on %s", method, iface);
67 return dbus_message_new_method_call(
BUS_NAME,
74 static DBusConnection* systemd_proxy = NULL;
78 static int need_init = 1;
82 && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
83 crm_warn(
"Connection to System DBus is closed. Reconnecting...");
93 if (systemd_proxy == NULL) {
104 systemd_proxy = NULL;
109 systemd_service_name(
const char *name)
114 }
else if (strstr(name,
".service")) {
122 systemd_daemon_reload_complete(DBusPendingCall *pending,
void *user_data)
125 DBusMessage *reply = NULL;
126 unsigned int reload_count = GPOINTER_TO_UINT(user_data);
128 dbus_error_init(&error);
130 reply = dbus_pending_call_steal_reply(pending);
134 crm_err(
"Could not issue systemd reload %d: %s", reload_count, error.message);
137 crm_trace(
"Reload %d complete", reload_count);
141 dbus_pending_call_unref(pending);
144 dbus_message_unref(reply);
149 systemd_daemon_reload(
int timeout)
151 static unsigned int reload_count = 0;
152 const char *method =
"Reload";
156 if(reload_count % 10 == 0) {
157 DBusMessage *msg = systemd_new_method(
BUS_NAME".Manager", method);
160 pcmk_dbus_send(msg, systemd_proxy, systemd_daemon_reload_complete, GUINT_TO_POINTER(reload_count), timeout);
161 dbus_message_unref(msg);
170 if(strstr(error,
"org.freedesktop.systemd1.InvalidName")
171 || strstr(error,
"org.freedesktop.systemd1.LoadFailed")
172 || strstr(error,
"org.freedesktop.systemd1.NoSuchUnit")) {
175 crm_trace(
"Masking %s failure for %s: unknown services are stopped", op->
action, op->
rsc);
180 crm_trace(
"Mapping %s failure for %s: unknown services are not installed", op->
action, op->
rsc);
191 systemd_loadunit_result(DBusMessage *reply,
svc_action_t * op)
193 const char *path = NULL;
197 if(op && !systemd_mask_error(op, error.name)) {
198 crm_err(
"Could not find unit %s for %s: LoadUnit error '%s'",
199 op->
agent, op->
id, error.name);
203 dbus_message_get_args (reply, NULL,
204 DBUS_TYPE_OBJECT_PATH, &path,
222 systemd_loadunit_cb(DBusPendingCall *pending,
void *user_data)
224 DBusMessage *reply = NULL;
228 reply = dbus_pending_call_steal_reply(pending);
231 crm_trace(
"Got result: %p for %p / %p for %s", reply, pending, op->
opaque->pending, op->
id);
234 services_set_op_pending(op, NULL);
236 systemd_loadunit_result(reply, user_data);
239 dbus_message_unref(reply);
244 systemd_unit_by_name(
const gchar * arg_name,
svc_action_t *op)
247 DBusMessage *reply = NULL;
248 DBusPendingCall* pending = NULL;
259 if (systemd_init() == FALSE) {
263 msg = systemd_new_method(
BUS_NAME".Manager",
"LoadUnit");
266 name = systemd_service_name(arg_name);
267 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
271 const char *unit = NULL;
275 dbus_error_init(&error);
277 dbus_message_unref(msg);
279 unit = systemd_loadunit_result(reply, op);
281 munit = strdup(unit);
284 dbus_message_unref(reply);
291 services_set_op_pending(op, pending);
294 dbus_message_unref(msg);
303 DBusMessageIter args;
304 DBusMessageIter unit;
305 DBusMessageIter elem;
306 DBusMessage *msg = NULL;
307 DBusMessage *reply = NULL;
308 const char *method =
"ListUnits";
311 if (systemd_init() == FALSE) {
321 dbus_error_init(&error);
322 msg = systemd_new_method(
BUS_NAME".Manager", method);
326 dbus_message_unref(msg);
329 crm_err(
"Call to %s failed: %s", method, error.name);
332 }
else if (reply == NULL) {
333 crm_err(
"Call to %s failed: Message has no reply", method);
336 }
else if (!dbus_message_iter_init(reply, &args)) {
337 crm_err(
"Call to %s failed: Message has no arguments", method);
338 dbus_message_unref(reply);
343 crm_err(
"Call to %s failed: Message has invalid arguments", method);
344 dbus_message_unref(reply);
348 dbus_message_iter_recurse(&args, &unit);
349 while (dbus_message_iter_get_arg_type (&unit) != DBUS_TYPE_INVALID) {
350 DBusBasicValue value;
356 dbus_message_iter_recurse(&unit, &elem);
361 dbus_message_iter_get_basic(&elem, &value);
364 char *match = strstr(value.str,
".service");
370 units = g_list_append(units, strdup(value.str));
373 dbus_message_iter_next (&unit);
376 dbus_message_unref(reply);
378 crm_trace(
"Found %d systemd services", lpc);
390 unit = systemd_unit_by_name(name, NULL);
399 systemd_unit_metadata(
const char *name,
int timeout)
403 char *path = systemd_unit_by_name(name, NULL);
413 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
414 "<resource-agent name=\"%s\" version=\"0.1\">\n"
415 " <version>1.0</version>\n"
416 " <longdesc lang=\"en\">\n"
419 " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n"
423 " <action name=\"start\" timeout=\"100\" />\n"
424 " <action name=\"stop\" timeout=\"100\" />\n"
425 " <action name=\"status\" timeout=\"100\" />\n"
426 " <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n"
427 " <action name=\"meta-data\" timeout=\"5\" />\n"
429 " <special tag=\"systemd\">\n"
430 " </special>\n" "</resource-agent>\n", name, desc, name);
437 systemd_exec_result(DBusMessage *reply,
svc_action_t *op)
444 if (!systemd_mask_error(op, error.name)) {
445 crm_err(
"Could not issue %s for %s: %s", op->
action, op->
rsc, error.message);
450 crm_warn(
"Call to %s passed but return type was unexpected", op->
action);
454 const char *path = NULL;
456 dbus_message_get_args (reply, NULL,
457 DBUS_TYPE_OBJECT_PATH, &path,
468 systemd_async_dispatch(DBusPendingCall *pending,
void *user_data)
471 DBusMessage *reply = NULL;
474 dbus_error_init(&error);
476 reply = dbus_pending_call_steal_reply(pending);
482 services_set_op_pending(op, NULL);
483 systemd_exec_result(reply, op);
486 dbus_message_unref(reply);
490 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"
493 systemd_unit_check(
const char *name,
const char *state,
void *userdata)
497 crm_trace(
"Resource %s has %s='%s'", op->
rsc, name, state);
502 }
else if (g_strcmp0(state,
"active") == 0) {
504 }
else if (g_strcmp0(state,
"activating") == 0) {
506 }
else if (g_strcmp0(state,
"deactivating") == 0) {
513 services_set_op_pending(op, NULL);
521 const char *method = op->
action;
522 DBusMessage *msg = NULL;
523 DBusMessage *reply = NULL;
528 DBusPendingCall *pending = NULL;
536 systemd_unit_check(
"ActiveState", state, op);
539 }
else if (pending) {
540 services_set_op_pending(op, pending);
547 }
else if (g_strcmp0(method,
"start") == 0) {
548 FILE *file_strm = NULL;
552 method =
"StartUnit";
555 file_strm = fopen(override_file,
"w");
556 if (file_strm != NULL) {
560 "Description=Cluster Controlled %s\n"
561 "Before=pacemaker.service\n"
567 int rc = fprintf(file_strm,
"%s\n",
override);
571 crm_perror(LOG_ERR,
"Cannot write to systemd override file %s", override_file);
575 crm_err(
"Cannot open systemd override file %s for writing", override_file);
578 if (file_strm != NULL) {
582 systemd_daemon_reload(op->
timeout);
586 }
else if (g_strcmp0(method,
"stop") == 0) {
590 unlink(override_file);
592 systemd_daemon_reload(op->
timeout);
594 }
else if (g_strcmp0(method,
"restart") == 0) {
595 method =
"RestartUnit";
602 crm_debug(
"Calling %s for %s: %s", method, op->
rsc, unit);
604 msg = systemd_new_method(
BUS_NAME".Manager", method);
609 const char *replace_s =
"replace";
610 char *name = systemd_service_name(op->
agent);
612 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
613 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
619 DBusPendingCall* pending =
pcmk_dbus_send(msg, systemd_proxy, systemd_async_dispatch, op, op->
timeout);
621 dbus_message_unref(msg);
623 services_set_op_pending(op, pending);
634 dbus_message_unref(msg);
635 systemd_exec_result(reply, op);
638 dbus_message_unref(reply);
652 systemd_timeout_callback(gpointer p)
673 crm_debug(
"Performing %ssynchronous %s op on systemd unit %s named '%s'",
687 unit = systemd_unit_by_name(op->
agent, op);
691 if (op->
opaque->pending) {
692 op->
opaque->timerid = g_timeout_add(op->
timeout + 5000, systemd_timeout_callback, op);
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.
#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...)
Log a system error message.
#define crm_err(fmt, args...)
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__
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...)
bool pcmk_dbus_find_error(const char *method, DBusPendingCall *pending, DBusMessage *reply, DBusError *ret)