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",
   314                       error->name, pcmk__s(op->
rsc, 
"unknown resource"));
   321                                "systemd unit %s not found", op->
agent);
   324     crm_info(
"DBus request for %s of systemd unit %s%s%s failed: %s",
   326              ((op->
rsc == NULL)? 
"" : 
" for resource "), pcmk__s(op->
rsc, 
""),
   341 execute_after_loadunit(DBusMessage *reply, 
svc_action_t *op)
   343     const char *
path = NULL;
   351             set_result_from_method_error(op, &error);
   353         dbus_error_free(&error);
   356                                      __func__, __LINE__)) {
   359                                  "systemd DBus method had unexpected reply");
   360             crm_info(
"Could not load systemd unit %s for %s: "   361                      "DBus reply has unexpected type", op->
agent, op->
id);
   363             crm_info(
"Could not load systemd unit: "   364                      "DBus reply has unexpected type");
   368         dbus_message_get_args (reply, NULL,
   369                                DBUS_TYPE_OBJECT_PATH, &
path,
   375             invoke_unit_by_path(op, 
path);
   379                                     "No DBus object found for systemd unit %s",
   396 loadunit_completed(DBusPendingCall *pending, 
void *user_data)
   398     DBusMessage *reply = NULL;
   401     crm_trace(
"LoadUnit result for %s arrived", op->
id);
   404     if (pending != NULL) {
   405         reply = dbus_pending_call_steal_reply(pending);
   410     services_set_op_pending(op, NULL);
   413     execute_after_loadunit(reply, user_data);
   415         dbus_message_unref(reply);
   438     DBusMessage *reply = NULL;
   439     DBusPendingCall *pending = NULL;
   442     if (!systemd_init()) {
   445                                  "No DBus connection");
   458     msg = systemd_new_method(
"LoadUnit");
   469         const char *unit = NULL;
   472         reply = systemd_send_recv(msg, NULL,
   474         dbus_message_unref(msg);
   476         unit = execute_after_loadunit(reply, op);
   482         } 
else if (
path != NULL) {
   483             *
path = strdup(unit);
   490             dbus_message_unref(reply);
   496     pending = systemd_send(msg, loadunit_completed, op, op->
timeout);
   497     if (pending == NULL) {
   499                              "Unable to send DBus message");
   500         dbus_message_unref(msg);
   506     services_set_op_pending(op, pending);
   507     dbus_message_unref(msg);
   524 sort_str(gconstpointer a, gconstpointer b)
   533     return strcasecmp(a, b);
   541     DBusMessageIter args;
   542     DBusMessageIter unit;
   543     DBusMessageIter elem;
   544     DBusMessage *reply = NULL;
   546     if (systemd_init() == FALSE) {
   556     reply = systemd_call_simple_method(
"ListUnitFiles");
   560     if (!dbus_message_iter_init(reply, &args)) {
   561         crm_err(
"Could not list systemd unit files: systemd reply has no arguments");
   562         dbus_message_unref(reply);
   566                               __func__, __LINE__)) {
   567         crm_err(
"Could not list systemd unit files: systemd reply has invalid arguments");
   568         dbus_message_unref(reply);
   572     dbus_message_iter_recurse(&args, &unit);
   573     for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
   574         dbus_message_iter_next(&unit)) {
   576         DBusBasicValue value;
   577         const char *match = NULL;
   578         char *unit_name = NULL;
   579         char *basename = NULL;
   582             crm_warn(
"Skipping systemd reply argument with unexpected type");
   586         dbus_message_iter_recurse(&unit, &elem);
   588             crm_warn(
"Skipping systemd reply argument with no string");
   592         dbus_message_iter_get_basic(&elem, &value);
   593         if (value.str == NULL) {
   594             crm_debug(
"ListUnitFiles reply did not provide a string");
   597         crm_trace(
"DBus ListUnitFiles listed: %s", value.str);
   599         match = systemd_unit_extension(value.str);
   602             crm_debug(
"ListUnitFiles entry '%s' is not supported as resource",
   608         basename = strrchr(value.str, 
'/');
   610             basename = basename + 1;
   612             basename = value.str;
   615         if (!strcmp(match, 
".service")) {
   617             unit_name = 
strndup(basename, match - basename);
   619             unit_name = strdup(basename);
   623         units = g_list_prepend(units, unit_name);
   626     dbus_message_unref(reply);
   628     crm_trace(
"Found %d manageable systemd unit files", nfiles);
   629     units = g_list_sort(units, sort_str);
   651     state = systemd_get_property(
path, 
"LoadState", NULL, NULL, NULL,
   662 #define METADATA_FORMAT                                                     \   663     "<?xml version=\"1.0\"?>\n"                                             \   664     "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"                   \   665     "<resource-agent name=\"%s\" version=\"" PCMK_DEFAULT_AGENT_VERSION "\">\n" \   666     "  <version>1.1</version>\n"                                            \   667     "  <longdesc lang=\"en\">\n"                                            \   670     "  <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n"       \   673     "    <action name=\"start\"     timeout=\"100\" />\n"                   \   674     "    <action name=\"stop\"      timeout=\"100\" />\n"                   \   675     "    <action name=\"status\"    timeout=\"100\" />\n"                   \   676     "    <action name=\"monitor\"   timeout=\"100\" interval=\"60\"/>\n"    \   677     "    <action name=\"meta-data\" timeout=\"5\"   />\n"                   \   679     "  <special tag=\"systemd\"/>\n"                                        \   680     "</resource-agent>\n"   683 systemd_unit_metadata(
const char *
name, 
int timeout)
   689     char *escaped = NULL;
   693         desc = systemd_get_property(
path, 
"Description", NULL, NULL, NULL,
   716 process_unit_method_reply(DBusMessage *reply, 
svc_action_t *op)
   724         set_result_from_method_error(op, &error);
   725         dbus_error_free(&error);
   728                                      __func__, __LINE__)) {
   729         crm_info(
"DBus request for %s of %s succeeded but "   730                  "return type was unexpected",
   731                  op->
action, pcmk__s(op->
rsc, 
"unknown resource"));
   733                              "systemd DBus method had unexpected reply");
   736         const char *
path = NULL;
   738         dbus_message_get_args(reply, NULL,
   739                               DBUS_TYPE_OBJECT_PATH, &
path,
   741         crm_debug(
"DBus request for %s of %s using %s succeeded",
   755 unit_method_complete(DBusPendingCall *pending, 
void *user_data)
   757     DBusMessage *reply = NULL;
   763     if (pending != NULL) {
   764         reply = dbus_pending_call_steal_reply(pending);
   769     services_set_op_pending(op, NULL);
   772     process_unit_method_reply(reply, op);
   775         dbus_message_unref(reply);
   779 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"   790 #define SYSTEMD_OVERRIDE_TEMPLATE                           \   792     "Description=Cluster Controlled %s\n"                   \   793     "Before=pacemaker.service pacemaker_remote.service\n"   \   800 create_world_readable(
const char *filename)
   802     mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
   803     FILE *fp = fopen(filename, 
"w");
   810 create_override_dir(
const char *agent)
   813                                            "/%s.service.d", agent);
   817         crm_warn(
"Could not create systemd override directory %s: %s",
   824 get_override_filename(
const char *agent)
   827                              "/%s.service.d/50-pacemaker.conf", agent);
   831 systemd_create_override(
const char *agent, 
int timeout)
   833     FILE *file_strm = NULL;
   834     char *override_file = get_override_filename(agent);
   836     create_override_dir(agent);
   841     file_strm = create_world_readable(override_file);
   842     if (file_strm == NULL) {
   843         crm_err(
"Cannot open systemd override file %s for writing",
   848         int rc = fprintf(file_strm, 
"%s\n", 
override);
   852             crm_perror(LOG_WARNING, 
"Cannot write to systemd override file %s",
   857         systemd_daemon_reload(
timeout);
   864 systemd_remove_override(
const char *agent, 
int timeout)
   866     char *override_file = get_override_filename(agent);
   867     int rc = unlink(override_file);
   871         crm_perror(LOG_DEBUG, 
"Cannot remove systemd override file %s",
   874         systemd_daemon_reload(
timeout);
   891 parse_status_result(
const char *
name, 
const char *state, 
void *userdata)
   896               pcmk__s(op->
rsc, 
"(unspecified)"), 
name,
   897               pcmk__s(state, 
"<null>"));
   916         services_set_op_pending(op, NULL);
   931     const char *method = NULL;
   932     DBusMessage *msg = NULL;
   933     DBusMessage *reply = NULL;
   936         DBusPendingCall *pending = NULL;
   939         state = systemd_get_property(unit, 
"ActiveState",
   944             parse_status_result(
"ActiveState", state, op);
   947         } 
else if (pending == NULL) { 
   949                                     "Could not get state for unit %s from DBus",
   954             services_set_op_pending(op, pending);
   959         method = 
"StartUnit";
   967         method = 
"RestartUnit";
   972                                 "Action %s not implemented "   973                                 "for systemd resources",
   974                                 pcmk__s(op->
action, 
"(unspecified)"));
   981     crm_trace(
"Calling %s for unit path %s%s%s",
   983               ((op->
rsc == NULL)? 
"" : 
" for resource "), pcmk__s(op->
rsc, 
""));
   985     msg = systemd_new_method(method);
   990         const char *replace_s = 
"replace";
   993         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &
name, DBUS_TYPE_INVALID));
   994         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
  1000         reply = systemd_send_recv(msg, NULL, op->
timeout);
  1001         dbus_message_unref(msg);
  1002         process_unit_method_reply(reply, op);
  1003         if (reply != NULL) {
  1004             dbus_message_unref(reply);
  1008         DBusPendingCall *pending = systemd_send(msg, unit_method_complete, op,
  1011         dbus_message_unref(msg);
  1012         if (pending == NULL) {
  1014                                  "Unable to send DBus message");
  1018             services_set_op_pending(op, pending);
  1024 systemd_timeout_callback(gpointer p)
  1029     crm_info(
"%s action for systemd unit %s named '%s' timed out",
  1032                             "%s action for systemd unit %s "  1033                             "did not complete in time", op->
action, op->
agent);
  1059     if ((op->
action == NULL) || (op->
agent == NULL)) {
  1061                              "Bug in action caller");
  1065     if (!systemd_init()) {
  1067                              "No DBus connection");
  1071     crm_debug(
"Performing %ssynchronous %s op on systemd unit %s%s%s",
  1073               ((op->
rsc == NULL)? 
"" : 
" for resource "), pcmk__s(op->
rsc, 
""));
  1085                          "Bug in service library");
  1089                                             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)
 
int pcmk__build_path(const char *path_c, mode_t mode)
 
#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...)