root/daemons/controld/pacemaker-controld.c

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

DEFINITIONS

This source file includes following definitions.
  1. main
  2. log_deprecation_warnings
  3. crmd_init

   1 /*
   2  * Copyright 2004-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 
  12 #include <sys/param.h>
  13 #include <stdio.h>
  14 #include <sys/types.h>
  15 #include <sys/stat.h>
  16 #include <unistd.h>
  17 
  18 #include <stdlib.h>
  19 #include <errno.h>
  20 #include <fcntl.h>
  21 
  22 #include <crm/crm.h>
  23 #include <crm/common/ipc.h>
  24 #include <crm/common/xml.h>
  25 
  26 #include <pacemaker-controld.h>
  27 
  28 #define OPTARGS "hV"
  29 
  30 void usage(const char *cmd, int exit_status);
  31 _Noreturn void crmd_init(void);
  32 void crmd_hamsg_callback(const xmlNode * msg, void *private_data);
  33 extern void init_dotfile(void);
  34 
  35 GMainLoop *crmd_mainloop = NULL;
  36 
  37 pcmk__output_t *logger_out = NULL;
  38 
  39 static pcmk__cli_option_t long_options[] = {
  40     // long option, argument type, storage, short option, description, flags
  41     {
  42         "help", no_argument, NULL, '?',
  43         "\tThis text", pcmk__option_default
  44     },
  45     {
  46         "verbose", no_argument, NULL, 'V',
  47         "\tIncrease debug output", pcmk__option_default
  48     },
  49     { 0, 0, 0, 0 }
  50 };
  51 
  52 int
  53 main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
  54 {
  55     int flag;
  56     int index = 0;
  57     int argerr = 0;
  58     crm_ipc_t *old_instance = NULL;
  59 
  60     crmd_mainloop = g_main_loop_new(NULL, FALSE);
  61     crm_log_preinit(NULL, argc, argv);
  62     pcmk__set_cli_options(NULL, "[options]", long_options,
  63                           "daemon for coordinating a Pacemaker cluster's "
  64                           "response to events");
  65 
  66     while (1) {
  67         flag = pcmk__next_cli_option(argc, argv, &index, NULL);
  68         if (flag == -1)
  69             break;
  70 
  71         switch (flag) {
  72             case 'V':
  73                 crm_bump_log_level(argc, argv);
  74                 break;
  75             case 'h':          /* Help message */
  76                 pcmk__cli_help(flag, CRM_EX_OK);
  77                 break;
  78             default:
  79                 ++argerr;
  80                 break;
  81         }
  82     }
  83 
  84     if (argc - optind == 1 && pcmk__str_eq("metadata", argv[optind], pcmk__str_casei)) {
  85         crmd_metadata();
  86         return CRM_EX_OK;
  87     } else if (argc - optind == 1 && pcmk__str_eq("version", argv[optind], pcmk__str_casei)) {
  88         fprintf(stdout, "CRM Version: %s (%s)\n", PACEMAKER_VERSION, BUILD_VERSION);
  89         return CRM_EX_OK;
  90     }
  91 
  92     crm_log_init(NULL, LOG_INFO, TRUE, FALSE, argc, argv, FALSE);
  93 
  94     if (optind > argc) {
  95         ++argerr;
  96     }
  97 
  98     if (argerr) {
  99         pcmk__cli_help('?', CRM_EX_USAGE);
 100     }
 101 
 102     crm_notice("Starting Pacemaker controller");
 103 
 104     old_instance = crm_ipc_new(CRM_SYSTEM_CRMD, 0);
 105     if (crm_ipc_connect(old_instance)) {
 106         /* IPC end-point already up */
 107         crm_ipc_close(old_instance);
 108         crm_ipc_destroy(old_instance);
 109         crm_err("pacemaker-controld is already active, aborting startup");
 110         crm_exit(CRM_EX_OK);
 111     } else {
 112         /* not up or not authentic, we'll proceed either way */
 113         crm_ipc_destroy(old_instance);
 114         old_instance = NULL;
 115     }
 116 
 117     if (pcmk__daemon_can_write(PE_STATE_DIR, NULL) == FALSE) {
 118         crm_err("Terminating due to bad permissions on " PE_STATE_DIR);
 119         fprintf(stderr,
 120                 "ERROR: Bad permissions on " PE_STATE_DIR " (see logs for details)\n");
 121         fflush(stderr);
 122         return CRM_EX_FATAL;
 123 
 124     } else if (pcmk__daemon_can_write(CRM_CONFIG_DIR, NULL) == FALSE) {
 125         crm_err("Terminating due to bad permissions on " CRM_CONFIG_DIR);
 126         fprintf(stderr,
 127                 "ERROR: Bad permissions on " CRM_CONFIG_DIR " (see logs for details)\n");
 128         fflush(stderr);
 129         return CRM_EX_FATAL;
 130     }
 131 
 132     if (pcmk__log_output_new(&logger_out) != pcmk_rc_ok) {
 133         return CRM_EX_FATAL;
 134     }
 135 
 136     pcmk__output_set_log_level(logger_out, LOG_TRACE);
 137 
 138     crmd_init();
 139     return 0; // not reachable
 140 }
 141 
 142 static void
 143 log_deprecation_warnings(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 144 {
 145     // Add deprecations here as needed
 146 }
 147 
 148 void
 149 crmd_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 150 {
 151     crm_exit_t exit_code = CRM_EX_OK;
 152     enum crmd_fsa_state state;
 153 
 154     log_deprecation_warnings();
 155 
 156     fsa_state = S_STARTING;
 157     fsa_input_register = 0;     /* zero out the regester */
 158 
 159     init_dotfile();
 160     register_fsa_input(C_STARTUP, I_STARTUP, NULL);
 161 
 162     crm_peer_init();
 163     state = s_crmd_fsa(C_STARTUP);
 164 
 165     if (state == S_PENDING || state == S_STARTING) {
 166         /* Create the mainloop and run it... */
 167         crm_trace("Starting %s's mainloop", crm_system_name);
 168         g_main_loop_run(crmd_mainloop);
 169         if (pcmk_is_set(fsa_input_register, R_STAYDOWN)) {
 170             crm_info("Inhibiting automated respawn");
 171             exit_code = CRM_EX_FATAL;
 172         }
 173 
 174     } else {
 175         crm_err("Startup of %s failed.  Current state: %s",
 176                 crm_system_name, fsa_state2string(state));
 177         exit_code = CRM_EX_ERROR;
 178     }
 179 
 180     crm_info("%s[%lu] exiting with status %d (%s)",
 181              crm_system_name, (unsigned long) getpid(), exit_code,
 182              crm_exit_str(exit_code));
 183 
 184     crmd_fast_exit(exit_code);
 185 }

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