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-2020 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 
 634     crm_build_path(override_dir, 0755);
 635     free(override_dir);
 636 }
 637 
 638 static char *
 639 get_override_filename(const char *agent)
     /* [previous][next][first][last][top][bottom][index][help] */
 640 {
 641     return crm_strdup_printf(SYSTEMD_OVERRIDE_ROOT
 642                              "/%s.service.d/50-pacemaker.conf", agent);
 643 }
 644 
 645 static void
 646 systemd_create_override(const char *agent, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 647 {
 648     FILE *file_strm = NULL;
 649     char *override_file = get_override_filename(agent);
 650 
 651     create_override_dir(agent);
 652 
 653     /* Ensure the override file is world-readable. This is not strictly
 654      * necessary, but it avoids a systemd warning in the logs.
 655      */
 656     file_strm = create_world_readable(override_file);
 657     if (file_strm == NULL) {
 658         crm_err("Cannot open systemd override file %s for writing",
 659                 override_file);
 660     } else {
 661         char *override = crm_strdup_printf(SYSTEMD_OVERRIDE_TEMPLATE, agent);
 662 
 663         int rc = fprintf(file_strm, "%s\n", override);
 664 
 665         free(override);
 666         if (rc < 0) {
 667             crm_perror(LOG_WARNING, "Cannot write to systemd override file %s",
 668                        override_file);
 669         }
 670         fflush(file_strm);
 671         fclose(file_strm);
 672         systemd_daemon_reload(timeout);
 673     }
 674 
 675     free(override_file);
 676 }
 677 
 678 static void
 679 systemd_remove_override(const char *agent, int timeout)
     /* [previous][next][first][last][top][bottom][index][help] */
 680 {
 681     char *override_file = get_override_filename(agent);
 682     int rc = unlink(override_file);
 683 
 684     if (rc < 0) {
 685         // Stop may be called when already stopped, which is fine
 686         crm_perror(LOG_DEBUG, "Cannot remove systemd override file %s",
 687                    override_file);
 688     } else {
 689         systemd_daemon_reload(timeout);
 690     }
 691     free(override_file);
 692 }
 693 
 694 static void
 695 systemd_unit_check(const char *name, const char *state, void *userdata)
     /* [previous][next][first][last][top][bottom][index][help] */
 696 {
 697     svc_action_t * op = userdata;
 698 
 699     crm_trace("Resource %s has %s='%s'", op->rsc, name, state);
 700 
 701     if(state == NULL) {
 702         op->rc = PCMK_OCF_NOT_RUNNING;
 703 
 704     } else if (g_strcmp0(state, "active") == 0) {
 705         op->rc = PCMK_OCF_OK;
 706     } else if (g_strcmp0(state, "reloading") == 0) {
 707         op->rc = PCMK_OCF_OK;
 708     } else if (g_strcmp0(state, "activating") == 0) {
 709         op->rc = PCMK_OCF_PENDING;
 710     } else if (g_strcmp0(state, "deactivating") == 0) {
 711         op->rc = PCMK_OCF_PENDING;
 712     } else {
 713         op->rc = PCMK_OCF_NOT_RUNNING;
 714     }
 715 
 716     if (op->synchronous == FALSE) {
 717         services_set_op_pending(op, NULL);
 718         operation_finalize(op);
 719     }
 720 }
 721 
 722 gboolean
 723 systemd_unit_exec_with_unit(svc_action_t * op, const char *unit)
     /* [previous][next][first][last][top][bottom][index][help] */
 724 {
 725     const char *method = op->action;
 726     DBusMessage *msg = NULL;
 727     DBusMessage *reply = NULL;
 728 
 729     CRM_ASSERT(unit);
 730 
 731     if (pcmk__str_eq(op->action, "monitor", pcmk__str_casei) || pcmk__str_eq(method, "status", pcmk__str_casei)) {
 732         DBusPendingCall *pending = NULL;
 733         char *state;
 734 
 735         state = systemd_get_property(unit, "ActiveState",
 736                                      (op->synchronous? NULL : systemd_unit_check),
 737                                      op, (op->synchronous? NULL : &pending),
 738                                      op->timeout);
 739         if (op->synchronous) {
 740             systemd_unit_check("ActiveState", state, op);
 741             free(state);
 742             return op->rc == PCMK_OCF_OK;
 743         } else if (pending) {
 744             services_set_op_pending(op, pending);
 745             return TRUE;
 746 
 747         } else {
 748             return operation_finalize(op);
 749         }
 750 
 751     } else if (g_strcmp0(method, "start") == 0) {
 752         method = "StartUnit";
 753         systemd_create_override(op->agent, op->timeout);
 754 
 755     } else if (g_strcmp0(method, "stop") == 0) {
 756         method = "StopUnit";
 757         systemd_remove_override(op->agent, op->timeout);
 758 
 759     } else if (g_strcmp0(method, "restart") == 0) {
 760         method = "RestartUnit";
 761 
 762     } else {
 763         op->rc = PCMK_OCF_UNIMPLEMENT_FEATURE;
 764         goto cleanup;
 765     }
 766 
 767     crm_debug("Calling %s for %s: %s", method, op->rsc, unit);
 768 
 769     msg = systemd_new_method(method);
 770     CRM_ASSERT(msg != NULL);
 771 
 772     /* (ss) */
 773     {
 774         const char *replace_s = "replace";
 775         char *name = systemd_service_name(op->agent);
 776 
 777         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID));
 778         CRM_LOG_ASSERT(dbus_message_append_args(msg, DBUS_TYPE_STRING, &replace_s, DBUS_TYPE_INVALID));
 779 
 780         free(name);
 781     }
 782 
 783     if (op->synchronous == FALSE) {
 784         DBusPendingCall *pending = systemd_send(msg, systemd_async_dispatch,
 785                                                 op, op->timeout);
 786 
 787         dbus_message_unref(msg);
 788         if(pending) {
 789             services_set_op_pending(op, pending);
 790             return TRUE;
 791 
 792         } else {
 793             return operation_finalize(op);
 794         }
 795 
 796     } else {
 797         reply = systemd_send_recv(msg, NULL, op->timeout);
 798         dbus_message_unref(msg);
 799         systemd_exec_result(reply, op);
 800 
 801         if(reply) {
 802             dbus_message_unref(reply);
 803         }
 804         return FALSE;
 805     }
 806 
 807   cleanup:
 808     if (op->synchronous == FALSE) {
 809         return operation_finalize(op);
 810     }
 811 
 812     return op->rc == PCMK_OCF_OK;
 813 }
 814 
 815 static gboolean
 816 systemd_timeout_callback(gpointer p)
     /* [previous][next][first][last][top][bottom][index][help] */
 817 {
 818     svc_action_t * op = p;
 819 
 820     op->opaque->timerid = 0;
 821     crm_warn("%s operation on systemd unit %s named '%s' timed out", op->action, op->agent, op->rsc);
 822     operation_finalize(op);
 823 
 824     return FALSE;
 825 }
 826 
 827 /* For an asynchronous 'op', returns FALSE if 'op' should be free'd by the caller */
 828 /* For a synchronous 'op', returns FALSE if 'op' fails */
 829 gboolean
 830 systemd_unit_exec(svc_action_t * op)
     /* [previous][next][first][last][top][bottom][index][help] */
 831 {
 832     char *unit = NULL;
 833 
 834     CRM_ASSERT(op);
 835     CRM_ASSERT(systemd_init());
 836     op->rc = PCMK_OCF_UNKNOWN_ERROR;
 837     crm_debug("Performing %ssynchronous %s op on systemd unit %s named '%s'",
 838               op->synchronous ? "" : "a", op->action, op->agent, op->rsc);
 839 
 840     if (pcmk__str_eq(op->action, "meta-data", pcmk__str_casei)) {
 841         // @TODO Implement an async meta-data call in executor API
 842         op->stdout_data = systemd_unit_metadata(op->agent, op->timeout);
 843         op->rc = PCMK_OCF_OK;
 844 
 845         if (op->synchronous == FALSE) {
 846             return operation_finalize(op);
 847         }
 848         return TRUE;
 849     }
 850 
 851     unit = systemd_unit_by_name(op->agent, op);
 852     free(unit);
 853 
 854     if (op->synchronous == FALSE) {
 855         if (op->opaque->pending) {
 856             op->opaque->timerid = g_timeout_add(op->timeout + 5000, systemd_timeout_callback, op);
 857             services_add_inflight_op(op);
 858             return TRUE;
 859 
 860         } else {
 861             return operation_finalize(op);
 862         }
 863     }
 864 
 865     return op->rc == PCMK_OCF_OK;
 866 }

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