20 #include <dbus/dbus.h> 23 static void invoke_unit_by_path(
svc_action_t *op,
const char *unit);
25 #define BUS_NAME "org.freedesktop.systemd1" 26 #define BUS_NAME_MANAGER BUS_NAME ".Manager" 27 #define BUS_NAME_UNIT BUS_NAME ".Unit" 28 #define BUS_PATH "/org/freedesktop/systemd1" 63 static inline DBusMessage *
64 systemd_new_method(
const char *method)
75 static DBusConnection* systemd_proxy = NULL;
77 static inline DBusPendingCall *
78 systemd_send(DBusMessage *msg,
79 void(*done)(DBusPendingCall *pending,
void *user_data),
85 static inline DBusMessage *
86 systemd_send_recv(DBusMessage *msg, DBusError *error,
int timeout)
103 systemd_call_simple_method(
const char *method)
105 DBusMessage *msg = systemd_new_method(method);
106 DBusMessage *reply = NULL;
113 crm_err(
"Could not create message to send %s to systemd", method);
117 dbus_error_init(&error);
119 dbus_message_unref(msg);
121 if (dbus_error_is_set(&error)) {
122 crm_err(
"Could not send %s to systemd: %s (%s)",
123 method, error.message, error.name);
124 dbus_error_free(&error);
127 }
else if (reply == NULL) {
128 crm_err(
"Could not send %s to systemd: no reply received", method);
138 static int need_init = 1;
142 && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
143 crm_warn(
"Connection to System DBus is closed. Reconnecting...");
145 systemd_proxy = NULL;
153 if (systemd_proxy == NULL) {
160 systemd_get_property(
const char *unit,
const char *
name,
161 void (*callback)(
const char *
name,
const char *value,
void *userdata),
162 void *userdata, DBusPendingCall **pending,
int timeout)
164 return systemd_proxy?
175 systemd_proxy = NULL;
192 systemd_unit_extension(
const char *
name)
195 const char *dot = strrchr(
name,
'.');
197 if (dot && (!strcmp(dot,
".service")
198 || !strcmp(dot,
".socket")
199 || !strcmp(dot,
".mount")
200 || !strcmp(dot,
".timer")
201 || !strcmp(dot,
".path"))) {
209 systemd_service_name(
const char *
name,
bool add_instance_name)
211 const char *dot = NULL;
213 if (pcmk__str_empty(
name)) {
226 dot = systemd_unit_extension(
name);
229 if (dot !=
name && *(dot-1) ==
'@') {
232 if (asprintf(&s,
"%.*spacemaker%s", (
int) (dot-
name),
name, dot) == -1) {
242 }
else if (add_instance_name && *(
name+strlen(
name)-1) ==
'@') {
251 systemd_daemon_reload_complete(DBusPendingCall *pending,
void *user_data)
254 DBusMessage *reply = NULL;
255 unsigned int reload_count = GPOINTER_TO_UINT(user_data);
257 dbus_error_init(&error);
259 reply = dbus_pending_call_steal_reply(pending);
263 crm_warn(
"Could not issue systemd reload %d: %s",
264 reload_count, error.message);
265 dbus_error_free(&error);
268 crm_trace(
"Reload %d complete", reload_count);
272 dbus_pending_call_unref(pending);
275 dbus_message_unref(reply);
280 systemd_daemon_reload(
int timeout)
282 static unsigned int reload_count = 0;
283 DBusMessage *msg = systemd_new_method(
"Reload");
287 systemd_send(msg, systemd_daemon_reload_complete,
288 GUINT_TO_POINTER(reload_count),
timeout);
289 dbus_message_unref(msg);
302 set_result_from_method_error(
svc_action_t *op,
const DBusError *error)
305 "Unable to invoke systemd DBus method");
307 if (strstr(error->name,
"org.freedesktop.systemd1.InvalidName")
308 || strstr(error->name,
"org.freedesktop.systemd1.LoadFailed")
309 || strstr(error->name,
"org.freedesktop.systemd1.NoSuchUnit")) {
312 crm_trace(
"Masking systemd stop failure (%s) for %s " 313 "because unknown service can be considered stopped",
321 "systemd unit %s not found", op->
agent);
324 crm_info(
"DBus request for %s of systemd unit %s for resource %s failed: %s",
339 execute_after_loadunit(DBusMessage *reply,
svc_action_t *op)
341 const char *
path = NULL;
349 set_result_from_method_error(op, &error);
351 dbus_error_free(&error);
354 __func__, __LINE__)) {
357 "systemd DBus method had unexpected reply");
358 crm_info(
"Could not load systemd unit %s for %s: " 359 "DBus reply has unexpected type", op->
agent, op->
id);
361 crm_info(
"Could not load systemd unit: " 362 "DBus reply has unexpected type");
366 dbus_message_get_args (reply, NULL,
367 DBUS_TYPE_OBJECT_PATH, &
path,
373 invoke_unit_by_path(op,
path);
377 "No DBus object found for systemd unit %s",
394 loadunit_completed(DBusPendingCall *pending,
void *user_data)
396 DBusMessage *reply = NULL;
399 crm_trace(
"LoadUnit result for %s arrived", op->
id);
402 if (pending != NULL) {
403 reply = dbus_pending_call_steal_reply(pending);
408 services_set_op_pending(op, NULL);
411 execute_after_loadunit(reply, user_data);
413 dbus_message_unref(reply);
436 DBusMessage *reply = NULL;
437 DBusPendingCall *pending = NULL;
440 if (!systemd_init()) {
443 "No DBus connection");
456 msg = systemd_new_method(
"LoadUnit");
467 const char *unit = NULL;
470 reply = systemd_send_recv(msg, NULL,
472 dbus_message_unref(msg);
474 unit = execute_after_loadunit(reply, op);
480 }
else if (
path != NULL) {
481 *
path = strdup(unit);
488 dbus_message_unref(reply);
494 pending = systemd_send(msg, loadunit_completed, op, op->
timeout);
495 if (pending == NULL) {
497 "Unable to send DBus message");
498 dbus_message_unref(msg);
504 services_set_op_pending(op, pending);
505 dbus_message_unref(msg);
522 sort_str(gconstpointer a, gconstpointer b)
531 return strcasecmp(a, b);
539 DBusMessageIter args;
540 DBusMessageIter unit;
541 DBusMessageIter elem;
542 DBusMessage *reply = NULL;
544 if (systemd_init() == FALSE) {
554 reply = systemd_call_simple_method(
"ListUnitFiles");
558 if (!dbus_message_iter_init(reply, &args)) {
559 crm_err(
"Could not list systemd unit files: systemd reply has no arguments");
560 dbus_message_unref(reply);
564 __func__, __LINE__)) {
565 crm_err(
"Could not list systemd unit files: systemd reply has invalid arguments");
566 dbus_message_unref(reply);
570 dbus_message_iter_recurse(&args, &unit);
571 for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
572 dbus_message_iter_next(&unit)) {
574 DBusBasicValue value;
575 const char *match = NULL;
576 char *unit_name = NULL;
577 char *basename = NULL;
580 crm_warn(
"Skipping systemd reply argument with unexpected type");
584 dbus_message_iter_recurse(&unit, &elem);
586 crm_warn(
"Skipping systemd reply argument with no string");
590 dbus_message_iter_get_basic(&elem, &value);
591 if (value.str == NULL) {
592 crm_debug(
"ListUnitFiles reply did not provide a string");
595 crm_trace(
"DBus ListUnitFiles listed: %s", value.str);
597 match = systemd_unit_extension(value.str);
600 crm_debug(
"ListUnitFiles entry '%s' is not supported as resource",
606 basename = strrchr(value.str,
'/');
608 basename = basename + 1;
610 basename = value.str;
613 if (!strcmp(match,
".service")) {
615 unit_name =
strndup(basename, match - basename);
617 unit_name = strdup(basename);
621 units = g_list_prepend(units, unit_name);
624 dbus_message_unref(reply);
626 crm_trace(
"Found %d manageable systemd unit files", nfiles);
627 units = g_list_sort(units, sort_str);
649 state = systemd_get_property(
path,
"LoadState", NULL, NULL, NULL,
660 #define METADATA_FORMAT \ 661 "<?xml version=\"1.0\"?>\n" \ 662 "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n" \ 663 "<resource-agent name=\"%s\" version=\"" PCMK_DEFAULT_AGENT_VERSION "\">\n" \ 664 " <version>1.1</version>\n" \ 665 " <longdesc lang=\"en\">\n" \ 668 " <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n" \ 671 " <action name=\"start\" timeout=\"100\" />\n" \ 672 " <action name=\"stop\" timeout=\"100\" />\n" \ 673 " <action name=\"status\" timeout=\"100\" />\n" \ 674 " <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n" \ 675 " <action name=\"meta-data\" timeout=\"5\" />\n" \ 677 " <special tag=\"systemd\"/>\n" \ 678 "</resource-agent>\n" 681 systemd_unit_metadata(
const char *
name,
int timeout)
687 char *escaped = NULL;
691 desc = systemd_get_property(
path,
"Description", NULL, NULL, NULL,
714 process_unit_method_reply(DBusMessage *reply,
svc_action_t *op)
722 set_result_from_method_error(op, &error);
723 dbus_error_free(&error);
726 __func__, __LINE__)) {
727 crm_info(
"DBus request for %s of %s succeeded but " 730 "systemd DBus method had unexpected reply");
733 const char *
path = NULL;
735 dbus_message_get_args(reply, NULL,
736 DBUS_TYPE_OBJECT_PATH, &
path,
738 crm_debug(
"DBus request for %s of %s using %s succeeded",
752 unit_method_complete(DBusPendingCall *pending,
void *user_data)
754 DBusMessage *reply = NULL;
760 if (pending != NULL) {
761 reply = dbus_pending_call_steal_reply(pending);
766 services_set_op_pending(op, NULL);
769 process_unit_method_reply(reply, op);
772 dbus_message_unref(reply);
776 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/" 787 #define SYSTEMD_OVERRIDE_TEMPLATE \ 789 "Description=Cluster Controlled %s\n" \ 790 "Before=pacemaker.service pacemaker_remote.service\n" \ 797 create_world_readable(
const char *filename)
799 mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
800 FILE *fp = fopen(filename,
"w");
807 create_override_dir(
const char *agent)
810 "/%s.service.d", agent);
814 crm_warn(
"Could not create systemd override directory %s: %s",
821 get_override_filename(
const char *agent)
824 "/%s.service.d/50-pacemaker.conf", agent);
828 systemd_create_override(
const char *agent,
int timeout)
830 FILE *file_strm = NULL;
831 char *override_file = get_override_filename(agent);
833 create_override_dir(agent);
838 file_strm = create_world_readable(override_file);
839 if (file_strm == NULL) {
840 crm_err(
"Cannot open systemd override file %s for writing",
845 int rc = fprintf(file_strm,
"%s\n",
override);
849 crm_perror(LOG_WARNING,
"Cannot write to systemd override file %s",
854 systemd_daemon_reload(
timeout);
861 systemd_remove_override(
const char *agent,
int timeout)
863 char *override_file = get_override_filename(agent);
864 int rc = unlink(override_file);
868 crm_perror(LOG_DEBUG,
"Cannot remove systemd override file %s",
871 systemd_daemon_reload(
timeout);
888 parse_status_result(
const char *
name,
const char *state,
void *userdata)
912 services_set_op_pending(op, NULL);
927 const char *method = NULL;
928 DBusMessage *msg = NULL;
929 DBusMessage *reply = NULL;
932 DBusPendingCall *pending = NULL;
935 state = systemd_get_property(unit,
"ActiveState",
940 parse_status_result(
"ActiveState", state, op);
943 }
else if (pending == NULL) {
945 "Could not get state for unit %s from DBus",
950 services_set_op_pending(op, pending);
955 method =
"StartUnit";
963 method =
"RestartUnit";
968 "Action %s not implemented " 976 crm_trace(
"Calling %s for unit path %s named %s",
979 msg = systemd_new_method(method);
984 const char *replace_s =
"replace";
987 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &
name, DBUS_TYPE_INVALID));
988 CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
994 reply = systemd_send_recv(msg, NULL, op->
timeout);
995 dbus_message_unref(msg);
996 process_unit_method_reply(reply, op);
998 dbus_message_unref(reply);
1002 DBusPendingCall *pending = systemd_send(msg, unit_method_complete, op,
1005 dbus_message_unref(msg);
1006 if (pending == NULL) {
1008 "Unable to send DBus message");
1012 services_set_op_pending(op, pending);
1018 systemd_timeout_callback(gpointer p)
1023 crm_info(
"%s action for systemd unit %s named '%s' timed out",
1026 "%s action for systemd unit %s " 1027 "did not complete in time", op->
action, op->
agent);
1053 if ((op->
action == NULL) || (op->
agent == NULL)) {
1055 "Bug in action caller");
1059 if (!systemd_init()) {
1061 "No DBus connection");
1065 crm_debug(
"Performing %ssynchronous %s op on systemd unit %s named '%s'",
1079 "Bug in service library");
1083 systemd_timeout_callback, op);
int rc
Exit status of action (set by library upon completion)
#define CRM_CHECK(expr, failure_action)
int services__execute_systemd(svc_action_t *op)
ocf_exitcode
Exit status codes for resource agents.
void pcmk_dbus_disconnect(DBusConnection *connection)
#define DBUS_TIMEOUT_USE_DEFAULT
char * rsc
XML ID of resource being executed for resource actions, otherwise NULL.
#define CRM_LOG_ASSERT(expr)
int timeout
Action timeout (in milliseconds)
Action did not complete in time.
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...)
void services__format_result(svc_action_t *action, int agent_status, enum pcmk_exec_status exec_status, const char *format,...) G_GNUC_PRINTF(4
svc_action_private_t * opaque
This field should be treated as internal to Pacemaker.
#define crm_debug(fmt, args...)
char * stdout_data
Action stdout (set by library)
Parameter invalid (inherently)
gboolean systemd_unit_exists(const char *name)
#define crm_trace(fmt, args...)
int services__finalize_async_op(svc_action_t *op)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
Object for executing external actions.
char * agent
Resource agent name for resource actions, otherwise NULL.
void services__set_result(svc_action_t *action, int agent_status, enum pcmk_exec_status exec_status, const char *exit_reason)
Action completed, result is known.
Execution failed, do not retry anywhere.
Dependencies not available locally.
bool pcmk__str_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
#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)
Requested action not implemented.
int services__systemd_prepare(svc_action_t *op)
void systemd_cleanup(void)
char * action
Name of action being executed for resource actions, otherwise NULL.
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
#define crm_err(fmt, args...)
enum ocf_exitcode services__systemd2ocf(int exit_status)
bool pcmk_dbus_type_check(DBusMessage *msg, DBusMessageIter *field, int expected, const char *function, int line)
Agent or dependency not available locally.
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
char * crm_xml_escape(const char *text)
Replace special characters with their XML escape sequences.
DBusMessage * pcmk_dbus_send_recv(DBusMessage *msg, DBusConnection *connection, DBusError *error, int timeout)
Execution failed, may be retried.
#define crm_info(fmt, args...)
int pcmk__build_path(const char *path_c, mode_t mode)