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     logger_out = pcmk__new_logger();
 133     if (logger_out == NULL) {
 134         return CRM_EX_FATAL;
 135     }
 136 
 137     pcmk__output_set_log_level(logger_out, LOG_TRACE);
 138 
 139     crmd_init();
 140     return 0; // not reachable
 141 }
 142 
 143 static void
 144 log_deprecation_warnings(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 145 {
 146     // Add deprecations here as needed
 147 }
 148 
 149 void
 150 crmd_init(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 151 {
 152     crm_exit_t exit_code = CRM_EX_OK;
 153     enum crmd_fsa_state state;
 154 
 155     log_deprecation_warnings();
 156 
 157     fsa_state = S_STARTING;
 158     fsa_input_register = 0;     /* zero out the regester */
 159 
 160     init_dotfile();
 161     register_fsa_input(C_STARTUP, I_STARTUP, NULL);
 162 
 163     crm_peer_init();
 164     state = s_crmd_fsa(C_STARTUP);
 165 
 166     if (state == S_PENDING || state == S_STARTING) {
 167         /* Create the mainloop and run it... */
 168         crm_trace("Starting %s's mainloop", crm_system_name);
 169         g_main_loop_run(crmd_mainloop);
 170         if (pcmk_is_set(fsa_input_register, R_STAYDOWN)) {
 171             crm_info("Inhibiting automated respawn");
 172             exit_code = CRM_EX_FATAL;
 173         }
 174 
 175     } else {
 176         crm_err("Startup of %s failed.  Current state: %s",
 177                 crm_system_name, fsa_state2string(state));
 178         exit_code = CRM_EX_ERROR;
 179     }
 180 
 181     crm_info("%s[%lu] exiting with status %d (%s)",
 182              crm_system_name, (unsigned long) getpid(), exit_code,
 183              crm_exit_str(exit_code));
 184 
 185     crmd_fast_exit(exit_code);
 186 }

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