root/lib/services/systemd.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. systemd_new_method
  2. systemd_send
  3. systemd_send_recv
  4. systemd_call_simple_method
  5. systemd_init
  6. systemd_get_property
  7. systemd_cleanup
  8. systemd_unit_extension
  9. systemd_service_name
  10. systemd_daemon_reload_complete
  11. systemd_daemon_reload
  12. systemd_mask_error
  13. systemd_loadunit_result
  14. systemd_loadunit_cb
  15. systemd_unit_by_name
  16. sort_str
  17. systemd_unit_listall
  18. systemd_unit_exists
  19. systemd_unit_metadata
  20. systemd_exec_result
  21. systemd_async_dispatch
  22. create_world_readable
  23. create_override_dir
  24. get_override_filename
  25. systemd_create_override
  26. systemd_remove_override
  27. systemd_unit_check
  28. systemd_unit_exec_with_unit
  29. systemd_timeout_callback
  30. systemd_unit_exec

   1 /*
   2  * Copyright 2012-2021 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include <crm/crm.h>
  12 #include <crm/services.h>
  13 #include <crm/common/mainloop.h>
  14 
  15 #include <sys/stat.h>
  16 #include <gio/gio.h>
  17 #include <services_private.h>
  18 #include <systemd.h>
  19 #include <dbus/dbus.h>
  20 #include <pcmk-dbus.h>
  21 
  22 gboolean systemd_unit_exec_with_unit(svc_action_t * op, const char *unit);
  23 
  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"
  28 
  29 static inline DBusMessage *
  30 systemd_new_method(const char *method)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32     crm_trace("Calling: %s on " BUS_NAME_MANAGER, method);
  33     return dbus_message_new_method_call(BUS_NAME, BUS_PATH, BUS_NAME_MANAGER,
  34                                         method);
  35 }
  36 
  37 /*
  38  * Functions to manage a static DBus connection
  39  */
  40 
  41 static DBusConnection* systemd_proxy = NULL;
  42 
  43 static inline DBusPendingCall *
  44 systemd_send(DBusMessage *msg,
     /* [previous][next][first][last][top][bottom][index][help] */
  45              void(*done)(DBusPendingCall *pending, void *user_data),
  46              void *user_data, int timeout)
  47 {
  48     return pcmk_dbus_send(msg, systemd_proxy, done, user_data, timeout);
  49 }
  50 
  51 static inline DBusMessage *
  52 systemd_send_recv(DBusMessage *msg, DBusError *error, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
  53 {
  54     return pcmk_dbus_send_recv(msg, systemd_proxy, error, timeout);
  55 }
  56 
  57 /*!
  58  * \internal
  59  * \brief Send a method to systemd without arguments, and wait for reply
  60  *
  61  * \param[in] method  Method to send
  62  *
  63  * \return Systemd reply on success, NULL (and error will be logged) otherwise
  64  *
  65  * \note The caller must call dbus_message_unref() on the reply after
  66  *       handling it.
  67  */
  68 static DBusMessage *
  69 systemd_call_simple_method(const char *method)
     /* [previous][next][first][last][top][bottom][index][help] */
  70 {
  71     DBusMessage *msg = systemd_new_method(method);
  72     DBusMessage *reply = NULL;
  73     DBusError error;
  74 
  75     /* Don't call systemd_init() here, because that calls this */
  76     CRM_CHECK(systemd_proxy, return NULL);
  77 
  78     if (msg == NULL) {
  79         crm_err("Could not create message to send %s to systemd", method);
  80         return NULL;
  81     }
  82 
  83     dbus_error_init(&error);
  84     reply = systemd_send_recv(msg, &error, DBUS_TIMEOUT_USE_DEFAULT);
  85     dbus_message_unref(msg);
  86 
  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);
  91         return NULL;
  92 
  93     } else if (reply == NULL) {
  94         crm_err("Could not send %s to systemd: no reply received", method);
  95         return NULL;
  96     }
  97 
  98     return reply;
  99 }
 100 
 101 static gboolean
 102 systemd_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 103 {
 104     static int need_init = 1;
 105     // https://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
 106 
 107     if (systemd_proxy
 108         && dbus_connection_get_is_connected(systemd_proxy) == FALSE) {
 109         crm_warn("Connection to System DBus is closed. Reconnecting...");
 110         pcmk_dbus_disconnect(systemd_proxy);
 111         systemd_proxy = NULL;
 112         need_init = 1;
 113     }
 114 
 115     if (need_init) {
 116         need_init = 0;
 117         systemd_proxy = pcmk_dbus_connect();
 118     }
 119     if (systemd_proxy == NULL) {
 120         return FALSE;
 121     }
 122     return TRUE;
 123 }
 124 
 125 static inline char *
 126 systemd_get_property(const char *unit, const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
 127                      void (*callback)(const char *name, const char *value, void *userdata),
 128                      void *userdata, DBusPendingCall **pending, int timeout)
 129 {
 130     return systemd_proxy?
 131            pcmk_dbus_get_property(systemd_proxy, BUS_NAME, unit, BUS_NAME_UNIT,
 132                                   name, callback, userdata, pending, timeout)
 133            : NULL;
 134 }
 135 
 136 void
 137 systemd_cleanup(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139     if (systemd_proxy) {
 140         pcmk_dbus_disconnect(systemd_proxy);
 141         systemd_proxy = NULL;
 142     }
 143 }
 144 
 145 /*
 146  * end of systemd_proxy functions
 147  */
 148 
 149 /*!
 150  * \internal
 151  * \brief Check whether a file name represents a manageable systemd unit
 152  *
 153  * \param[in] name  File name to check
 154  *
 155  * \return Pointer to "dot" before filename extension if so, NULL otherwise
 156  */
 157 static const char *
 158 systemd_unit_extension(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160     if (name) {
 161         const char *dot = strrchr(name, '.');
 162 
 163         if (dot && (!strcmp(dot, ".service")
 164                     || !strcmp(dot, ".socket")
 165                     || !strcmp(dot, ".mount")
 166                     || !strcmp(dot, ".timer")
 167                     || !strcmp(dot, ".path"))) {
 168             return dot;
 169         }
 170     }
 171     return NULL;
 172 }
 173 
 174 static char *
 175 systemd_service_name(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177     if (name == NULL) {
 178         return NULL;
 179     }
 180 
 181     if (systemd_unit_extension(name)) {
 182         return strdup(name);
 183     }
 184 
 185     return crm_strdup_printf("%s.service", name);
 186 }
 187 
 188 static void
 189 systemd_daemon_reload_complete(DBusPendingCall *pending, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191     DBusError error;
 192     DBusMessage *reply = NULL;
 193     unsigned int reload_count = GPOINTER_TO_UINT(user_data);
 194 
 195     dbus_error_init(&error);
 196     if(pending) {
 197         reply = dbus_pending_call_steal_reply(pending);
 198     }
 199 
 200     if (pcmk_dbus_find_error(pending, reply, &error)) {
 201         crm_err("Could not issue systemd reload %d: %s", reload_count, error.message);
 202         dbus_error_free(&error);
 203 
 204     } else {
 205         crm_trace("Reload %d complete", reload_count);
 206     }
 207 
 208     if(pending) {
 209         dbus_pending_call_unref(pending);
 210     }
 211     if(reply) {
 212         dbus_message_unref(reply);
 213     }
 214 }
 215 
 216 static bool
 217 systemd_daemon_reload(int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 218 {
 219     static unsigned int reload_count = 0;
 220     DBusMessage *msg = systemd_new_method("Reload");
 221 
 222     reload_count++;
 223     CRM_ASSERT(msg != NULL);
 224     systemd_send(msg, systemd_daemon_reload_complete,
 225                  GUINT_TO_POINTER(reload_count), timeout);
 226     dbus_message_unref(msg);
 227 
 228     return TRUE;
 229 }
 230 
 231 static bool
 232 systemd_mask_error(svc_action_t *op, const char *error)
     /* [previous][next][first][last][top][bottom][index][help] */
 233 {
 234     crm_trace("Could not issue %s for %s: %s", op->action, op->rsc, error);
 235     if(strstr(error, "org.freedesktop.systemd1.InvalidName")
 236        || strstr(error, "org.freedesktop.systemd1.LoadFailed")
 237        || strstr(error, "org.freedesktop.systemd1.NoSuchUnit")) {
 238 
 239         if (pcmk__str_eq(op->action, "stop", pcmk__str_casei)) {
 240             crm_trace("Masking %s failure for %s: unknown services are stopped", op->action, op->rsc);
 241             op->rc = PCMK_OCF_OK;
 242             return TRUE;
 243 
 244         } else {
 245             crm_trace("Mapping %s failure for %s: unknown services are not installed", op->action, op->rsc);
 246             op->rc = PCMK_OCF_NOT_INSTALLED;
 247             op->status = PCMK_LRM_OP_NOT_INSTALLED;
 248             return FALSE;
 249         }
 250     }
 251 
 252     return FALSE;
 253 }
 254 
 255 static const char *
 256 systemd_loadunit_result(DBusMessage *reply, svc_action_t * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 257 {
 258     const char *path = NULL;
 259     DBusError error;
 260 
 261     if (pcmk_dbus_find_error((void*)&path, reply, &error)) {
 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);
 265         }
 266         dbus_error_free(&error);
 267 
 268     } else if (!pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH,
 269                                      __func__, __LINE__)) {
 270         crm_err("Could not load systemd unit %s for %s: "
 271                 "systemd reply has unexpected type", op->agent, op->id);
 272 
 273     } else {
 274         dbus_message_get_args (reply, NULL,
 275                                DBUS_TYPE_OBJECT_PATH, &path,
 276                                DBUS_TYPE_INVALID);
 277     }
 278 
 279     if(op) {
 280         if (path) {
 281             systemd_unit_exec_with_unit(op, path);
 282 
 283         } else if (op->synchronous == FALSE) {
 284             operation_finalize(op);
 285         }
 286     }
 287 
 288     return path;
 289 }
 290 
 291 
 292 static void
 293 systemd_loadunit_cb(DBusPendingCall *pending, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 294 {
 295     DBusMessage *reply = NULL;
 296     svc_action_t * op = user_data;
 297 
 298     if(pending) {
 299         reply = dbus_pending_call_steal_reply(pending);
 300     }
 301 
 302     crm_trace("Got result: %p for %p / %p for %s", reply, pending, op->opaque->pending, op->id);
 303 
 304     CRM_LOG_ASSERT(pending == op->opaque->pending);
 305     services_set_op_pending(op, NULL);
 306 
 307     systemd_loadunit_result(reply, user_data);
 308 
 309     if(reply) {
 310         dbus_message_unref(reply);
 311     }
 312 }
 313 
 314 static char *
 315 systemd_unit_by_name(const gchar * arg_name, svc_action_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 316 {
 317     DBusMessage *msg;
 318     DBusMessage *reply = NULL;
 319     DBusPendingCall* pending = NULL;
 320     char *name = NULL;
 321 
 322 /*
 323   Equivalent to GetUnit if it's already loaded
 324   <method name="LoadUnit">
 325    <arg name="name" type="s" direction="in"/>
 326    <arg name="unit" type="o" direction="out"/>
 327   </method>
 328  */
 329 
 330     if (systemd_init() == FALSE) {
 331         return FALSE;
 332     }
 333 
 334     msg = systemd_new_method("LoadUnit");
 335     CRM_ASSERT(msg != NULL);
 336 
 337     name = systemd_service_name(arg_name);
 338     CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
 339     free(name);
 340 
 341     if(op == NULL || op->synchronous) {
 342         const char *unit = NULL;
 343         char *munit = NULL;
 344 
 345         reply = systemd_send_recv(msg, NULL,
 346                                   (op? op->timeout : DBUS_TIMEOUT_USE_DEFAULT));
 347         dbus_message_unref(msg);
 348 
 349         unit = systemd_loadunit_result(reply, op);
 350         if(unit) {
 351             munit = strdup(unit);
 352         }
 353         if(reply) {
 354             dbus_message_unref(reply);
 355         }
 356         return munit;
 357     }
 358 
 359     pending = systemd_send(msg, systemd_loadunit_cb, op, op->timeout);
 360     if(pending) {
 361         services_set_op_pending(op, pending);
 362     }
 363 
 364     dbus_message_unref(msg);
 365     return NULL;
 366 }
 367 
 368 /*!
 369  * \internal
 370  * \brief Compare two strings alphabetically (case-insensitive)
 371  *
 372  * \param[in] a  First string to compare
 373  * \param[in] b  Second string to compare
 374  *
 375  * \return 0 if strings are equal, -1 if a < b, 1 if a > b
 376  *
 377  * \note Usable as a GCompareFunc with g_list_sort().
 378  *       NULL is considered less than non-NULL.
 379  */
 380 static gint
 381 sort_str(gconstpointer a, gconstpointer b)
     /* [previous][next][first][last][top][bottom][index][help] */
 382 {
 383     if (!a && !b) {
 384         return 0;
 385     } else if (!a) {
 386         return -1;
 387     } else if (!b) {
 388         return 1;
 389     }
 390     return strcasecmp(a, b);
 391 }
 392 
 393 GList *
 394 systemd_unit_listall(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 395 {
 396     int nfiles = 0;
 397     GList *units = NULL;
 398     DBusMessageIter args;
 399     DBusMessageIter unit;
 400     DBusMessageIter elem;
 401     DBusMessage *reply = NULL;
 402 
 403     if (systemd_init() == FALSE) {
 404         return NULL;
 405     }
 406 
 407 /*
 408         "  <method name=\"ListUnitFiles\">\n"                               \
 409         "   <arg name=\"files\" type=\"a(ss)\" direction=\"out\"/>\n" \
 410         "  </method>\n"                                                 \
 411 */
 412 
 413     reply = systemd_call_simple_method("ListUnitFiles");
 414     if (reply == NULL) {
 415         return NULL;
 416     }
 417     if (!dbus_message_iter_init(reply, &args)) {
 418         crm_err("Could not list systemd unit files: systemd reply has no arguments");
 419         dbus_message_unref(reply);
 420         return NULL;
 421     }
 422     if (!pcmk_dbus_type_check(reply, &args, DBUS_TYPE_ARRAY,
 423                               __func__, __LINE__)) {
 424         crm_err("Could not list systemd unit files: systemd reply has invalid arguments");
 425         dbus_message_unref(reply);
 426         return NULL;
 427     }
 428 
 429     dbus_message_iter_recurse(&args, &unit);
 430     for (; dbus_message_iter_get_arg_type(&unit) != DBUS_TYPE_INVALID;
 431         dbus_message_iter_next(&unit)) {
 432 
 433         DBusBasicValue value;
 434         const char *match = NULL;
 435         char *unit_name = NULL;
 436         char *basename = NULL;
 437 
 438         if(!pcmk_dbus_type_check(reply, &unit, DBUS_TYPE_STRUCT, __func__, __LINE__)) {
 439             crm_warn("Skipping systemd reply argument with unexpected type");
 440             continue;
 441         }
 442 
 443         dbus_message_iter_recurse(&unit, &elem);
 444         if(!pcmk_dbus_type_check(reply, &elem, DBUS_TYPE_STRING, __func__, __LINE__)) {
 445             crm_warn("Skipping systemd reply argument with no string");
 446             continue;
 447         }
 448 
 449         dbus_message_iter_get_basic(&elem, &value);
 450         if (value.str == NULL) {
 451             crm_debug("ListUnitFiles reply did not provide a string");
 452             continue;
 453         }
 454         crm_trace("DBus ListUnitFiles listed: %s", value.str);
 455 
 456         match = systemd_unit_extension(value.str);
 457         if (match == NULL) {
 458             // This is not a unit file type we know how to manage
 459             crm_debug("ListUnitFiles entry '%s' is not supported as resource",
 460                       value.str);
 461             continue;
 462         }
 463 
 464         // ListUnitFiles returns full path names, we just want base name
 465         basename = strrchr(value.str, '/');
 466         if (basename) {
 467             basename = basename + 1;
 468         } else {
 469             basename = value.str;
 470         }
 471 
 472         if (!strcmp(match, ".service")) {
 473             // Service is the "default" unit type, so strip it
 474             unit_name = strndup(basename, match - basename);
 475         } else {
 476             unit_name = strdup(basename);
 477         }
 478 
 479         nfiles++;
 480         units = g_list_prepend(units, unit_name);
 481     }
 482 
 483     dbus_message_unref(reply);
 484 
 485     crm_trace("Found %d manageable systemd unit files", nfiles);
 486     units = g_list_sort(units, sort_str);
 487     return units;
 488 }
 489 
 490 gboolean
 491 systemd_unit_exists(const char *name)
     /* [previous][next][first][last][top][bottom][index][help] */
 492 {
 493     char *unit = NULL;
 494 
 495     /* Note: Makes a blocking dbus calls
 496      * Used by resources_find_service_class() when resource class=service
 497      */
 498     unit = systemd_unit_by_name(name, NULL);
 499     if(unit) {
 500         free(unit);
 501         return TRUE;
 502     }
 503     return FALSE;
 504 }
 505 
 506 static char *
 507 systemd_unit_metadata(const char *name, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 508 {
 509     char *meta = NULL;
 510     char *desc = NULL;
 511     char *path = systemd_unit_by_name(name, NULL);
 512 
 513     if (path) {
 514         /* TODO: Worth a making blocking call for? Probably not. Possibly if cached. */
 515         desc = systemd_get_property(path, "Description", NULL, NULL, NULL,
 516                                     timeout);
 517     } else {
 518         desc = crm_strdup_printf("Systemd unit file for %s", name);
 519     }
 520 
 521     meta = crm_strdup_printf("<?xml version=\"1.0\"?>\n"
 522                            "<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n"
 523                            "<resource-agent name=\"%s\" version=\"" PCMK_DEFAULT_AGENT_VERSION "\">\n"
 524                            "  <version>1.0</version>\n"
 525                            "  <longdesc lang=\"en\">\n"
 526                            "    %s\n"
 527                            "  </longdesc>\n"
 528                            "  <shortdesc lang=\"en\">systemd unit file for %s</shortdesc>\n"
 529                            "  <parameters>\n"
 530                            "  </parameters>\n"
 531                            "  <actions>\n"
 532                            "    <action name=\"start\"   timeout=\"100\" />\n"
 533                            "    <action name=\"stop\"    timeout=\"100\" />\n"
 534                            "    <action name=\"status\"  timeout=\"100\" />\n"
 535                            "    <action name=\"monitor\" timeout=\"100\" interval=\"60\"/>\n"
 536                            "    <action name=\"meta-data\"  timeout=\"5\" />\n"
 537                            "  </actions>\n"
 538                            "  <special tag=\"systemd\">\n"
 539                            "  </special>\n" "</resource-agent>\n", name, desc, name);
 540     free(desc);
 541     free(path);
 542     return meta;
 543 }
 544 
 545 static void
 546 systemd_exec_result(DBusMessage *reply, svc_action_t *op)
     /* [previous][next][first][last][top][bottom][index][help] */
 547 {
 548     DBusError error;
 549 
 550     if (pcmk_dbus_find_error((void*)&error, reply, &error)) {
 551 
 552         /* ignore "already started" or "not running" errors */
 553         if (!systemd_mask_error(op, error.name)) {
 554             crm_err("Could not issue %s for %s: %s", op->action, op->rsc, error.message);
 555         }
 556         dbus_error_free(&error);
 557 
 558     } else {
 559         if(!pcmk_dbus_type_check(reply, NULL, DBUS_TYPE_OBJECT_PATH, __func__, __LINE__)) {
 560             crm_warn("Call to %s passed but return type was unexpected", op->action);
 561             op->rc = PCMK_OCF_OK;
 562 
 563         } else {
 564             const char *path = NULL;
 565 
 566             dbus_message_get_args (reply, NULL,
 567                                    DBUS_TYPE_OBJECT_PATH, &path,
 568                                    DBUS_TYPE_INVALID);
 569             crm_info("Call to %s passed: %s", op->action, path);
 570             op->rc = PCMK_OCF_OK;
 571         }
 572     }
 573 
 574     operation_finalize(op);
 575 }
 576 
 577 static void
 578 systemd_async_dispatch(DBusPendingCall *pending, void *user_data)
     /* [previous][next][first][last][top][bottom][index][help] */
 579 {
 580     DBusMessage *reply = NULL;
 581     svc_action_t *op = user_data;
 582 
 583     if(pending) {
 584         reply = dbus_pending_call_steal_reply(pending);
 585     }
 586 
 587     crm_trace("Got result: %p for %p for %s, %s", reply, pending, op->rsc, op->action);
 588 
 589     CRM_LOG_ASSERT(pending == op->opaque->pending);
 590     services_set_op_pending(op, NULL);
 591     systemd_exec_result(reply, op);
 592 
 593     if(reply) {
 594         dbus_message_unref(reply);
 595     }
 596 }
 597 
 598 #define SYSTEMD_OVERRIDE_ROOT "/run/systemd/system/"
 599 
 600 /* When the cluster manages a systemd resource, we create a unit file override
 601  * to order the service "before" pacemaker. The "before" relationship won't
 602  * actually be used, since systemd won't ever start the resource -- we're
 603  * interested in the reverse shutdown ordering it creates, to ensure that
 604  * systemd doesn't stop the resource at shutdown while pacemaker is still
 605  * running.
 606  *
 607  * @TODO Add start timeout
 608  */
 609 #define SYSTEMD_OVERRIDE_TEMPLATE                           \
 610     "[Unit]\n"                                              \
 611     "Description=Cluster Controlled %s\n"                   \
 612     "Before=pacemaker.service pacemaker_remote.service\n"   \
 613     "\n"                                                    \
 614     "[Service]\n"                                           \
 615     "Restart=no\n"
 616 
 617 // Temporarily use rwxr-xr-x umask when opening a file for writing
 618 static FILE *
 619 create_world_readable(const char *filename)
     /* [previous][next][first][last][top][bottom][index][help] */
 620 {
 621     mode_t orig_umask = umask(S_IWGRP | S_IWOTH);
 622     FILE *fp = fopen(filename, "w");
 623 
 624     umask(orig_umask);
 625     return fp;
 626 }
 627 
 628 static void
 629 create_override_dir(const char *agent)
     /* [previous][next][first][last][top][bottom][index][help] */
 630 {
 631     char *override_dir = crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
 632                                            "/%s.service.d", agent);
 633     int rc = pcmk__build_path(override_dir, 0755);
 634 
 635     if (rc != pcmk_rc_ok) {
 636         crm_warn("Could not create systemd override directory %s: %s",
 637                  override_dir, pcmk_rc_str(rc));
 638     }
 639     free(override_dir);
 640 }
 641 
 642 static char *
 643 get_override_filename(const char *agent)
     /* [previous][next][first][last][top][bottom][index][help] */
 644 {
 645     return crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
 646                              "/%s.service.d/50-pacemaker.conf", agent);
 647 }
 648 
 649 static void
 650 systemd_create_override(const char *agent, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 651 {
 652     FILE *file_strm = NULL;
 653     char *override_file = get_override_filename(agent);
 654 
 655     create_override_dir(agent);
 656 
 657     /* Ensure the override file is world-readable. This is not strictly
 658      * necessary, but it avoids a systemd warning in the logs.
 659      */
 660     file_strm = create_world_readable(override_file);
 661     if (file_strm == NULL) {
 662         crm_err("Cannot open systemd override file %s for writing",
 663                 override_file);
 664     } else {
 665         char *override = crm_strdup_printf(SYSTEMD_OVERRIDE_TEMPLATE, agent);
 666 
 667         int rc = fprintf(file_strm, "%s\n", override);
 668 
 669         free(override);
 670         if (rc < 0) {
 671             crm_perror(LOG_WARNING, "Cannot write to systemd override file %s",
 672                        override_file);
 673         }
 674         fflush(file_strm);
 675         fclose(file_strm);
 676         systemd_daemon_reload(timeout);
 677     }
 678 
 679     free(override_file);
 680 }
 681 
 682 static void
 683 systemd_remove_override(const char *agent, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 684 {
 685     char *override_file = get_override_filename(agent);
 686     int rc = unlink(override_file);
 687 
 688     if (rc < 0) {
 689         // Stop may be called when already stopped, which is fine
 690         crm_perror(LOG_DEBUG, "Cannot remove systemd override file %s",
 691                    override_file);
 692     } else {
 693         systemd_daemon_reload(timeout);
 694     }
 695     free(override_file);
 696 }
 697 
 698 static void
 699 systemd_unit_check(const char *name, const char *state, void *userdata)
     /* [previous][next][first][last][top][bottom][index][help] */
 700 {
 701     svc_action_t * op = userdata;
 702 
 703     crm_trace("Resource %s has %s='%s'", op->rsc, name, state);
 704 
 705     if(state == NULL) {
 706         op->rc = PCMK_OCF_NOT_RUNNING;
 707 
 708     } else if (g_strcmp0(state, "active") == 0) {
 709         op->rc = PCMK_OCF_OK;
 710     } else if (g_strcmp0(state, "reloading") == 0) {
 711         op->rc = PCMK_OCF_OK;
 712     } else if (g_strcmp0(state, "activating") == 0) {
 713         op->rc = PCMK_OCF_PENDING;
 714     } else if (g_strcmp0(state, "deactivating") == 0) {
 715         op->rc = PCMK_OCF_PENDING;
 716     } else {
 717         op->rc = PCMK_OCF_NOT_RUNNING;
 718     }
 719 
 720     if (op->synchronous == FALSE) {
 721         services_set_op_pending(op, NULL);
 722         operation_finalize(op);
 723     }
 724 }
 725 
 726 gboolean
 727 systemd_unit_exec_with_unit(svc_action_t * op, const char *unit)
     /* [previous][next][first][last][top][bottom][index][help] */
 728 {
 729     const char *method = op->action;
 730     DBusMessage *msg = NULL;
 731     DBusMessage *reply = NULL;
 732 
 733     CRM_ASSERT(unit);
 734 
 735     if (pcmk__str_eq(op->action, "monitor", pcmk__str_casei) || pcmk__str_eq(method, "status", pcmk__str_casei)) {
 736         DBusPendingCall *pending = NULL;
 737         char *state;
 738 
 739         state = systemd_get_property(unit, "ActiveState",
 740                                      (op->synchronous? NULL : systemd_unit_check),
 741                                      op, (op->synchronous? NULL : &pending),
 742                                      op->timeout);
 743         if (op->synchronous) {
 744             systemd_unit_check("ActiveState", state, op);
 745             free(state);
 746             return op->rc == PCMK_OCF_OK;
 747         } else if (pending) {
 748             services_set_op_pending(op, pending);
 749             return TRUE;
 750 
 751         } else {
 752             return operation_finalize(op);
 753         }
 754 
 755     } else if (g_strcmp0(method, "start") == 0) {
 756         method = "StartUnit";
 757         systemd_create_override(op->agent, op->timeout);
 758 
 759     } else if (g_strcmp0(method, "stop") == 0) {
 760         method = "StopUnit";
 761         systemd_remove_override(op->agent, op->timeout);
 762 
 763     } else if (g_strcmp0(method, "restart") == 0) {
 764         method = "RestartUnit";
 765 
 766     } else {
 767         op->rc = PCMK_OCF_UNIMPLEMENT_FEATURE;
 768         goto cleanup;
 769     }
 770 
 771     crm_debug("Calling %s for %s: %s", method, op->rsc, unit);
 772 
 773     msg = systemd_new_method(method);
 774     CRM_ASSERT(msg != NULL);
 775 
 776     /* (ss) */
 777     {
 778         const char *replace_s = "replace";
 779         char *name = systemd_service_name(op->agent);
 780 
 781         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
 782         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
 783 
 784         free(name);
 785     }
 786 
 787     if (op->synchronous == FALSE) {
 788         DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
 789                                                 op, op->timeout);
 790 
 791         dbus_message_unref(msg);
 792         if(pending) {
 793             services_set_op_pending(op, pending);
 794             return TRUE;
 795 
 796         } else {
 797             return operation_finalize(op);
 798         }
 799 
 800     } else {
 801         reply = systemd_send_recv(msg, NULL, op->timeout);
 802         dbus_message_unref(msg);
 803         systemd_exec_result(reply, op);
 804 
 805         if(reply) {
 806             dbus_message_unref(reply);
 807         }
 808         return FALSE;
 809     }
 810 
 811   cleanup:
 812     if (op->synchronous == FALSE) {
 813         return operation_finalize(op);
 814     }
 815 
 816     return op->rc == PCMK_OCF_OK;
 817 }
 818 
 819 static gboolean
 820 systemd_timeout_callback(gpointer p)
     /* [previous][next][first][last][top][bottom][index][help] */
 821 {
 822     svc_action_t * op = p;
 823 
 824     op->opaque->timerid = 0;
 825     crm_warn("%s operation on systemd unit %s named '%s' timed out", op->action, op->agent, op->rsc);
 826     operation_finalize(op);
 827 
 828     return FALSE;
 829 }
 830 
 831 /* For an asynchronous 'op', returns FALSE if 'op' should be free'd by the caller */
 832 /* For a synchronous 'op', returns FALSE if 'op' fails */
 833 gboolean
 834 systemd_unit_exec(svc_action_t * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 835 {
 836     char *unit = NULL;
 837 
 838     CRM_ASSERT(op);
 839     CRM_ASSERT(systemd_init());
 840     op->rc = PCMK_OCF_UNKNOWN_ERROR;
 841     crm_debug("Performing %ssynchronous %s op on systemd unit %s named '%s'",
 842               op->synchronous ? "" : "a", op->action, op->agent, op->rsc);
 843 
 844     if (pcmk__str_eq(op->action, "meta-data", pcmk__str_casei)) {
 845         // @TODO Implement an async meta-data call in executor API
 846         op->stdout_data = systemd_unit_metadata(op->agent, op->timeout);
 847         op->rc = PCMK_OCF_OK;
 848 
 849         if (op->synchronous == FALSE) {
 850             return operation_finalize(op);
 851         }
 852         return TRUE;
 853     }
 854 
 855     unit = systemd_unit_by_name(op->agent, op);
 856     free(unit);
 857 
 858     if (op->synchronous == FALSE) {
 859         if (op->opaque->pending) {
 860             op->opaque->timerid = g_timeout_add(op->timeout + 5000, systemd_timeout_callback, op);
 861             services_add_inflight_op(op);
 862             return TRUE;
 863 
 864         } else {
 865             return operation_finalize(op);
 866         }
 867     }
 868 
 869     return op->rc == PCMK_OCF_OK;
 870 }

/* [previous][next][first][last][top][bottom][index][help] */