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

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