root/daemons/pacemakerd/pacemakerd.c

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

DEFINITIONS

This source file includes following definitions.
  1. PCMK__OUTPUT_ARGS
  2. PCMK__OUTPUT_ARGS
  3. pid_cb
  4. standby_cb
  5. pcmk_ignore
  6. pcmk_sigquit
  7. mcp_chown
  8. create_pcmk_dirs
  9. remove_core_file_limit
  10. pacemakerd_event_cb
  11. build_arg_context
  12. main

   1 /*
   2  * Copyright 2010-2022 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 General Public License version 2
   7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <crm_internal.h>
  11 #include "pacemakerd.h"
  12 
  13 #include <pwd.h>
  14 #include <errno.h>
  15 #include <unistd.h>
  16 #include <stdio.h>
  17 #include <stdbool.h>
  18 #include <sys/stat.h>
  19 #include <sys/types.h>
  20 #include <sys/time.h>
  21 #include <sys/resource.h>
  22 
  23 #include <crm/crm.h>  /* indirectly: CRM_EX_* */
  24 #include <crm/msg_xml.h>
  25 #include <crm/common/mainloop.h>
  26 #include <crm/common/cmdline_internal.h>
  27 #include <crm/common/ipc_pacemakerd.h>
  28 #include <crm/common/output_internal.h>
  29 #include <crm/cluster/internal.h>
  30 #include <crm/cluster.h>
  31 
  32 #define SUMMARY "pacemakerd - primary Pacemaker daemon that launches and monitors all subsidiary Pacemaker daemons"
  33 
  34 struct {
  35     gboolean features;
  36     gboolean foreground;
  37     gboolean shutdown;
  38     gboolean standby;
  39 } options;
  40 
  41 static pcmk__output_t *out = NULL;
  42 
  43 static pcmk__supported_format_t formats[] = {
  44     PCMK__SUPPORTED_FORMAT_NONE,
  45     PCMK__SUPPORTED_FORMAT_TEXT,
  46     PCMK__SUPPORTED_FORMAT_XML,
  47     { NULL, NULL, NULL }
  48 };
  49 
  50 PCMK__OUTPUT_ARGS("features")
     /* [previous][next][first][last][top][bottom][index][help] */
  51 static int
  52 pacemakerd_features(pcmk__output_t *out, va_list args) {
  53     out->info(out, "Pacemaker %s (Build: %s)\n Supporting v%s: %s", PACEMAKER_VERSION,
  54               BUILD_VERSION, CRM_FEATURE_SET, CRM_FEATURES);
  55     return pcmk_rc_ok;
  56 }
  57 
  58 PCMK__OUTPUT_ARGS("features")
     /* [previous][next][first][last][top][bottom][index][help] */
  59 static int
  60 pacemakerd_features_xml(pcmk__output_t *out, va_list args) {
  61     gchar **feature_list = g_strsplit(CRM_FEATURES, " ", 0);
  62 
  63     pcmk__output_xml_create_parent(out, "pacemakerd",
  64                                    "version", PACEMAKER_VERSION,
  65                                    "build", BUILD_VERSION,
  66                                    "feature_set", CRM_FEATURE_SET,
  67                                    NULL);
  68     out->begin_list(out, NULL, NULL, "features");
  69 
  70     for (char **s = feature_list; *s != NULL; s++) {
  71         pcmk__output_create_xml_text_node(out, "feature", *s);
  72     }
  73 
  74     out->end_list(out);
  75 
  76     g_strfreev(feature_list);
  77     return pcmk_rc_ok;
  78 }
  79 
  80 static pcmk__message_entry_t fmt_functions[] = {
  81     { "features", "default", pacemakerd_features },
  82     { "features", "xml", pacemakerd_features_xml },
  83 
  84     { NULL, NULL, NULL }
  85 };
  86 
  87 static gboolean
  88 pid_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
     /* [previous][next][first][last][top][bottom][index][help] */
  89     return TRUE;
  90 }
  91 
  92 static gboolean
  93 standby_cb(const gchar *option_name, const gchar *optarg, gpointer data, GError **err) {
     /* [previous][next][first][last][top][bottom][index][help] */
  94     options.standby = TRUE;
  95     pcmk__set_env_option("node_start_state", "standby");
  96     return TRUE;
  97 }
  98 
  99 static GOptionEntry entries[] = {
 100     { "features", 'F', 0, G_OPTION_ARG_NONE, &options.features,
 101       "Display full version and list of features Pacemaker was built with",
 102       NULL },
 103     { "foreground", 'f', 0, G_OPTION_ARG_NONE, &options.foreground,
 104       "(Ignored) Pacemaker always runs in the foreground",
 105       NULL },
 106     { "pid-file", 'p', 0, G_OPTION_ARG_CALLBACK, pid_cb,
 107       "(Ignored) Daemon pid file location",
 108       "FILE" },
 109     { "shutdown", 'S', 0, G_OPTION_ARG_NONE, &options.shutdown,
 110       "Instruct Pacemaker to shutdown on this machine",
 111       NULL },
 112     { "standby", 's', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, standby_cb,
 113       "Start node in standby state",
 114       NULL },
 115 
 116     { NULL }
 117 };
 118 
 119 static void
 120 pcmk_ignore(int nsig)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122     crm_info("Ignoring signal %s (%d)", strsignal(nsig), nsig);
 123 }
 124 
 125 static void
 126 pcmk_sigquit(int nsig)
     /* [previous][next][first][last][top][bottom][index][help] */
 127 {
 128     pcmk__panic(__func__);
 129 }
 130 
 131 static void
 132 mcp_chown(const char *path, uid_t uid, gid_t gid)
     /* [previous][next][first][last][top][bottom][index][help] */
 133 {
 134     int rc = chown(path, uid, gid);
 135 
 136     if (rc < 0) {
 137         crm_warn("Cannot change the ownership of %s to user %s and gid %d: %s",
 138                  path, CRM_DAEMON_USER, gid, pcmk_rc_str(errno));
 139     }
 140 }
 141 
 142 static void
 143 create_pcmk_dirs(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145     uid_t pcmk_uid = 0;
 146     gid_t pcmk_gid = 0;
 147 
 148     const char *dirs[] = {
 149         CRM_PACEMAKER_DIR, // core/blackbox/scheduler/CIB files
 150         CRM_CORE_DIR,      // core files
 151         CRM_BLACKBOX_DIR,  // blackbox dumps
 152         PE_STATE_DIR,      // scheduler inputs
 153         CRM_CONFIG_DIR,    // the Cluster Information Base (CIB)
 154         // Don't build CRM_RSCTMP_DIR, pacemaker-execd will do it
 155         NULL
 156     };
 157 
 158     if (pcmk_daemon_user(&pcmk_uid, &pcmk_gid) < 0) {
 159         crm_err("Cluster user %s does not exist, aborting Pacemaker startup",
 160                 CRM_DAEMON_USER);
 161         crm_exit(CRM_EX_NOUSER);
 162     }
 163 
 164     // Used by some resource agents
 165     if ((mkdir(CRM_STATE_DIR, 0750) < 0) && (errno != EEXIST)) {
 166         crm_warn("Could not create directory " CRM_STATE_DIR ": %s",
 167                  pcmk_rc_str(errno));
 168     } else {
 169         mcp_chown(CRM_STATE_DIR, pcmk_uid, pcmk_gid);
 170     }
 171 
 172     for (int i = 0; dirs[i] != NULL; ++i) {
 173         int rc = pcmk__build_path(dirs[i], 0750);
 174 
 175         if (rc != pcmk_rc_ok) {
 176             crm_warn("Could not create directory %s: %s",
 177                      dirs[i], pcmk_rc_str(rc));
 178         } else {
 179             mcp_chown(dirs[i], pcmk_uid, pcmk_gid);
 180         }
 181     }
 182 }
 183 
 184 static void
 185 remove_core_file_limit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 186 {
 187     struct rlimit cores;
 188     int rc = getrlimit(RLIMIT_CORE, &cores);
 189 
 190     if (rc < 0) {
 191         crm_warn("Cannot determine current maximum core file size: %s",
 192                  strerror(errno));
 193         return;
 194     }
 195 
 196     if ((cores.rlim_max == 0) && (geteuid() == 0)) {
 197         cores.rlim_max = RLIM_INFINITY;
 198     } else {
 199         crm_info("Maximum core file size is %llu bytes",
 200                  (unsigned long long) cores.rlim_max);
 201     }
 202     cores.rlim_cur = cores.rlim_max;
 203 
 204     rc = setrlimit(RLIMIT_CORE, &cores);
 205     if (rc < 0) {
 206         crm_warn("Cannot raise system limit on core file size "
 207                  "(consider doing so manually)");
 208     }
 209 }
 210 
 211 static void
 212 pacemakerd_event_cb(pcmk_ipc_api_t *pacemakerd_api,
     /* [previous][next][first][last][top][bottom][index][help] */
 213                     enum pcmk_ipc_event event_type, crm_exit_t status,
 214                     void *event_data, void *user_data)
 215 {
 216     pcmk_pacemakerd_api_reply_t *reply = event_data;
 217 
 218     switch (event_type) {
 219         case pcmk_ipc_event_reply:
 220             break;
 221 
 222         default:
 223             return;
 224     }
 225 
 226     if (status != CRM_EX_OK) {
 227         out->err(out, "Bad reply from pacemakerd: %s", crm_exit_str(status));
 228         return;
 229     }
 230 
 231     if (reply->reply_type != pcmk_pacemakerd_reply_shutdown) {
 232         out->err(out, "Unknown reply type %d from pacemakerd",
 233                  reply->reply_type);
 234     }
 235 }
 236 
 237 static GOptionContext *
 238 build_arg_context(pcmk__common_args_t *args, GOptionGroup **group) {
     /* [previous][next][first][last][top][bottom][index][help] */
 239     GOptionContext *context = NULL;
 240 
 241     context = pcmk__build_arg_context(args, "text (default), xml", group, NULL);
 242     pcmk__add_main_args(context, entries);
 243     return context;
 244 }
 245 
 246 int
 247 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249     int rc = pcmk_rc_ok;
 250     crm_exit_t exit_code = CRM_EX_OK;
 251 
 252     GError *error = NULL;
 253 
 254     GOptionGroup *output_group = NULL;
 255     pcmk__common_args_t *args = pcmk__new_common_args(SUMMARY);
 256     gchar **processed_args = pcmk__cmdline_preproc(argv, "p");
 257     GOptionContext *context = build_arg_context(args, &output_group);
 258 
 259     bool old_instance_connected = false;
 260 
 261     pcmk_ipc_api_t *old_instance = NULL;
 262     qb_ipcs_service_t *ipcs = NULL;
 263 
 264     subdaemon_check_progress = time(NULL);
 265 
 266     setenv("LC_ALL", "C", 1); // Ensure logs are in a common language
 267 
 268     crm_log_preinit(NULL, argc, argv);
 269     mainloop_add_signal(SIGHUP, pcmk_ignore);
 270     mainloop_add_signal(SIGQUIT, pcmk_sigquit);
 271 
 272     pcmk__register_formats(output_group, formats);
 273     if (!g_option_context_parse_strv(context, &processed_args, &error)) {
 274         exit_code = CRM_EX_USAGE;
 275         goto done;
 276     }
 277 
 278     rc = pcmk__output_new(&out, args->output_ty, args->output_dest, argv);
 279     if ((rc != pcmk_rc_ok) || (out == NULL)) {
 280         exit_code = CRM_EX_ERROR;
 281         g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "Error creating output format %s: %s",
 282                     args->output_ty, pcmk_rc_str(rc));
 283         goto done;
 284     }
 285 
 286     pcmk__force_args(context, &error, "%s --xml-simple-list", g_get_prgname());
 287 
 288     pcmk__register_messages(out, fmt_functions);
 289 
 290     if (options.features) {
 291         out->message(out, "features");
 292         exit_code = CRM_EX_OK;
 293         goto done;
 294     }
 295 
 296     if (args->version) {
 297         out->version(out, false);
 298         goto done;
 299     }
 300 
 301     pcmk__set_env_option("mcp", "true");
 302 
 303     if (options.shutdown) {
 304         pcmk__cli_init_logging("pacemakerd", args->verbosity);
 305     } else {
 306         crm_log_init(NULL, LOG_INFO, TRUE, FALSE, argc, argv, FALSE);
 307     }
 308 
 309     crm_debug("Checking for existing Pacemaker instance");
 310 
 311     rc = pcmk_new_ipc_api(&old_instance, pcmk_ipc_pacemakerd);
 312     if (old_instance == NULL) {
 313         out->err(out, "Could not check for existing pacemakerd: %s", pcmk_rc_str(rc));
 314         exit_code = pcmk_rc2exitc(rc);
 315         goto done;
 316     }
 317 
 318     pcmk_register_ipc_callback(old_instance, pacemakerd_event_cb, NULL);
 319     rc = pcmk_connect_ipc(old_instance, pcmk_ipc_dispatch_sync);
 320     old_instance_connected = pcmk_ipc_is_connected(old_instance);
 321 
 322     if (options.shutdown) {
 323         if (old_instance_connected) {
 324             rc = pcmk_pacemakerd_api_shutdown(old_instance, crm_system_name);
 325             pcmk_dispatch_ipc(old_instance);
 326             pcmk_free_ipc_api(old_instance);
 327             exit_code = pcmk_rc2exitc(rc);
 328             goto done;
 329         } else {
 330             out->err(out, "Could not request shutdown "
 331                      "of existing Pacemaker instance: %s", pcmk_rc_str(rc));
 332             pcmk_free_ipc_api(old_instance);
 333             exit_code = CRM_EX_DISCONNECT;
 334             goto done;
 335         }
 336 
 337     } else if (old_instance_connected) {
 338         pcmk_free_ipc_api(old_instance);
 339         crm_err("Aborting start-up because active Pacemaker instance found");
 340         exit_code = CRM_EX_FATAL;
 341         goto done;
 342     }
 343 
 344     pcmk_free_ipc_api(old_instance);
 345 
 346     /* Don't allow any accidental output after this point. */
 347     if (out != NULL) {
 348         out->finish(out, exit_code, true, NULL);
 349         pcmk__output_free(out);
 350         out = NULL;
 351     }
 352 
 353 #ifdef SUPPORT_COROSYNC
 354     if (mcp_read_config() == FALSE) {
 355         crm_exit(CRM_EX_UNAVAILABLE);
 356     }
 357 #endif
 358 
 359     // OCF shell functions and cluster-glue need facility under different name
 360     {
 361         const char *facility = pcmk__env_option(PCMK__ENV_LOGFACILITY);
 362 
 363         if (!pcmk__str_eq(facility, PCMK__VALUE_NONE,
 364                           pcmk__str_casei|pcmk__str_null_matches)) {
 365             setenv("HA_LOGFACILITY", facility, 1);
 366         }
 367     }
 368 
 369     crm_notice("Starting Pacemaker %s "CRM_XS" build=%s features:%s",
 370                PACEMAKER_VERSION, BUILD_VERSION, CRM_FEATURES);
 371     mainloop = g_main_loop_new(NULL, FALSE);
 372 
 373     remove_core_file_limit();
 374     create_pcmk_dirs();
 375     pcmk__serve_pacemakerd_ipc(&ipcs, &mcp_ipc_callbacks);
 376 
 377 #ifdef SUPPORT_COROSYNC
 378     /* Allows us to block shutdown */
 379     if (!cluster_connect_cfg()) {
 380         exit_code = CRM_EX_PROTOCOL;
 381         goto done;
 382     }
 383 #endif
 384 
 385     if (pcmk__locate_sbd() > 0) {
 386         setenv("PCMK_watchdog", "true", 1);
 387         running_with_sbd = TRUE;
 388     } else {
 389         setenv("PCMK_watchdog", "false", 1);
 390     }
 391 
 392     switch (find_and_track_existing_processes()) {
 393         case pcmk_rc_ok:
 394             break;
 395         case pcmk_rc_ipc_unauthorized:
 396             exit_code = CRM_EX_CANTCREAT;
 397             goto done;
 398         default:
 399             exit_code = CRM_EX_FATAL;
 400             goto done;
 401     };
 402 
 403     mainloop_add_signal(SIGTERM, pcmk_shutdown);
 404     mainloop_add_signal(SIGINT, pcmk_shutdown);
 405 
 406     if ((running_with_sbd) && pcmk__get_sbd_sync_resource_startup()) {
 407         crm_notice("Waiting for startup-trigger from SBD.");
 408         pacemakerd_state = XML_PING_ATTR_PACEMAKERDSTATE_WAITPING;
 409         startup_trigger = mainloop_add_trigger(G_PRIORITY_HIGH, init_children_processes, NULL);
 410     } else {
 411         if (running_with_sbd) {
 412             crm_warn("Enabling SBD_SYNC_RESOURCE_STARTUP would (if supported "
 413                      "by your SBD version) improve reliability of "
 414                      "interworking between SBD & pacemaker.");
 415         }
 416         pacemakerd_state = XML_PING_ATTR_PACEMAKERDSTATE_STARTINGDAEMONS;
 417         init_children_processes(NULL);
 418     }
 419 
 420     crm_notice("Pacemaker daemon successfully started and accepting connections");
 421     g_main_loop_run(mainloop);
 422 
 423     if (ipcs) {
 424         crm_trace("Closing IPC server");
 425         mainloop_del_ipc_server(ipcs);
 426         ipcs = NULL;
 427     }
 428 
 429     g_main_loop_unref(mainloop);
 430 #ifdef SUPPORT_COROSYNC
 431     cluster_disconnect_cfg();
 432 #endif
 433 
 434 done:
 435     g_strfreev(processed_args);
 436     pcmk__free_arg_context(context);
 437 
 438     pcmk__output_and_clear_error(error, out);
 439 
 440     if (out != NULL) {
 441         out->finish(out, exit_code, true, NULL);
 442         pcmk__output_free(out);
 443     }
 444     crm_exit(exit_code);
 445 }

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