char * corosync_cluster_name(void) { cmap_handle_t handle; char *cluster_name = NULL; int rc = CS_OK; rc = cmap_initialize(&handle); if (rc != CS_OK) { crm_info("Failed to initialize the cmap API: %s (%d)", ais_error2text(rc), rc); return NULL; } rc = cmap_get_string(handle, "totem.cluster_name", &cluster_name); if (rc != CS_OK) { crm_info("Cannot get totem.cluster_name: %s (%d)", ais_error2text(rc), rc); } else { crm_debug("cmap totem.cluster_name = '%s'", cluster_name); } cmap_finalize(handle); return cluster_name; }
enum cluster_type_e find_corosync_variant(void) { int rc = CS_OK; cmap_handle_t handle; rc = cmap_initialize(&handle); switch(rc) { case CS_OK: break; case CS_ERR_SECURITY: crm_debug("Failed to initialize the cmap API: Permission denied (%d)", rc); /* It's there, we just can't talk to it. * Good enough for us to identify as 'corosync' */ return pcmk_cluster_corosync; default: crm_info("Failed to initialize the cmap API: %s (%d)", ais_error2text(rc), rc); return pcmk_cluster_unknown; } cmap_finalize(handle); return pcmk_cluster_corosync; }
char * corosync_cluster_name(void) { cmap_handle_t handle; char *cluster_name = NULL; cs_error_t rc = CS_OK; int fd = -1; uid_t found_uid = 0; gid_t found_gid = 0; pid_t found_pid = 0; int rv; rc = cmap_initialize(&handle); if (rc != CS_OK) { crm_info("Failed to initialize the cmap API: %s (%d)", cs_strerror(rc), rc); return NULL; } rc = cmap_fd_get(handle, &fd); if (rc != CS_OK) { crm_err("Could not obtain the CMAP API connection: %s (%d)", cs_strerror(rc), rc); goto bail; } /* CMAP provider run as root (in given user namespace, anyway)? */ if (!(rv = crm_ipc_is_authentic_process(fd, (uid_t) 0,(gid_t) 0, &found_pid, &found_uid, &found_gid))) { crm_err("CMAP provider is not authentic:" " process %lld (uid: %lld, gid: %lld)", (long long) PCMK__SPECIAL_PID_AS_0(found_pid), (long long) found_uid, (long long) found_gid); goto bail; } else if (rv < 0) { crm_err("Could not verify authenticity of CMAP provider: %s (%d)", strerror(-rv), -rv); goto bail; } rc = cmap_get_string(handle, "totem.cluster_name", &cluster_name); if (rc != CS_OK) { crm_info("Cannot get totem.cluster_name: %s (%d)", cs_strerror(rc), rc); } else { crm_debug("cmap totem.cluster_name = '%s'", cluster_name); } bail: cmap_finalize(handle); return cluster_name; }
int corosync_cmap_has_config(const char *prefix) { int rc = CS_OK; int retries = 0; static int found = -1; cmap_handle_t cmap_handle; cmap_iter_handle_t iter_handle; char key_name[CMAP_KEYNAME_MAXLEN + 1]; if(found != -1) { return found; } do { rc = cmap_initialize(&cmap_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API: %s (rc=%d)", cs_strerror(rc), rc); return -1; } rc = cmap_iter_init(cmap_handle, prefix, &iter_handle); if (rc != CS_OK) { crm_warn("Failed to initialize iteration for corosync cmap '%s': %s (rc=%d)", prefix, cs_strerror(rc), rc); goto bail; } found = 0; while ((rc = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) { crm_trace("'%s' is configured in corosync cmap: %s", prefix, key_name); found++; break; } cmap_iter_finalize(cmap_handle, iter_handle); bail: cmap_finalize(cmap_handle); return found; }
static void _cs_cmap_init(void) { cs_error_t rc; int cmap_fd = 0; cmap_track_handle_t track_handle; rc = cmap_initialize (&cmap_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to initialize the cmap API. Error %d", rc); exit (EXIT_FAILURE); } cmap_fd_get(cmap_handle, &cmap_fd); qb_loop_poll_add(main_loop, QB_LOOP_MED, cmap_fd, POLLIN|POLLNVAL, NULL, _cs_cmap_dispatch); rc = cmap_track_add(cmap_handle, "runtime.connections.", CMAP_TRACK_ADD | CMAP_TRACK_DELETE | CMAP_TRACK_PREFIX, _cs_cmap_connections_key_changed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the connections key. Error %d", rc); exit (EXIT_FAILURE); } rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.srp.members.", CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX, _cs_cmap_members_key_changed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the members key. Error %d", rc); exit (EXIT_FAILURE); } rc = cmap_track_add(cmap_handle, "runtime.totem.pg.mrp.rrp.", CMAP_TRACK_ADD | CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX, _cs_cmap_rrp_faulty_key_changed, NULL, &track_handle); if (rc != CS_OK) { qb_log(LOG_ERR, "Failed to track the rrp key. Error %d", rc); exit (EXIT_FAILURE); } }
static gboolean sbd_get_two_node(void) { uint8_t two_node_u8 = 0; int cmap_fd; if (!track_handle) { if (cmap_initialize(&cmap_handle) != CS_OK) { cl_log(LOG_WARNING, "Cannot initialize CMAP service\n"); goto out; } if (cmap_track_add(cmap_handle, "quorum.two_node", CMAP_TRACK_DELETE|CMAP_TRACK_MODIFY|CMAP_TRACK_ADD, sbd_cmap_notify_fn, NULL, &track_handle) != CS_OK) { cl_log(LOG_WARNING, "Failed adding CMAP tracker for 2Node-mode\n"); goto out; } /* add the tracker to mainloop */ if (cmap_fd_get(cmap_handle, &cmap_fd) != CS_OK) { cl_log(LOG_WARNING, "Failed to get a file handle for cmap\n"); goto out; } if (!(cmap_source = g_unix_fd_source_new (cmap_fd, G_IO_IN))) { cl_log(LOG_WARNING, "Couldn't create source for cmap\n"); goto out; } g_source_set_callback(cmap_source, cmap_dispatch_callback, NULL, NULL); g_source_attach(cmap_source, NULL); } if (cmap_get_uint8(cmap_handle, "quorum.two_node", &two_node_u8) == CS_OK) { cl_log(two_node_u8? LOG_NOTICE : LOG_INFO, "Corosync is%s in 2Node-mode", two_node_u8?"":" not"); two_node = two_node_u8; } else { cl_log(LOG_INFO, "quorum.two_node not present in cmap\n"); } return TRUE; out: cmap_destroy(); return FALSE; }
static int init_all(void) { cmap_handle = 0; q_handle = 0; v_handle = 0; c_handle = 0; if (cmap_initialize(&cmap_handle) != CS_OK) { fprintf(stderr, "Cannot initialize CMAP service\n"); cmap_handle = 0; goto out; } if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) { fprintf(stderr, "Cannot initialize QUORUM service\n"); q_handle = 0; goto out; } if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) { fprintf(stderr, "Cannot initialise CFG service\n"); c_handle = 0; goto out; } if (using_votequorum() <= 0) { return 0; } if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) { fprintf(stderr, "Cannot initialise VOTEQUORUM service\n"); v_handle = 0; goto out; } if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) { fprintf(stderr, "Unable to retrieve this_node_id\n"); goto out; } return 0; out: return -1; }
void qdevice_cmap_init(struct qdevice_instance *instance) { cs_error_t res; int no_retries; no_retries = 0; while ((res = cmap_initialize(&instance->cmap_handle)) == CS_ERR_TRY_AGAIN && no_retries++ < instance->advanced_settings->max_cs_try_again) { (void)poll(NULL, 0, 1000); } if (res != CS_OK) { errx(1, "Failed to initialize the cmap API. Error %s", cs_strerror(res)); } if ((res = cmap_context_set(instance->cmap_handle, (void *)instance)) != CS_OK) { errx(1, "Can't set cmap context. Error %s", cs_strerror(res)); } cmap_fd_get(instance->cmap_handle, &instance->cmap_poll_fd); }
gboolean read_config(void) { int rc = CS_OK; int retries = 0; gboolean have_log = FALSE; char *logging_debug = NULL; char *logging_logfile = NULL; char *logging_to_logfile = NULL; char *logging_to_syslog = NULL; char *logging_syslog_facility = NULL; enum cluster_type_e stack = pcmk_cluster_unknown; #if HAVE_CONFDB char *value = NULL; confdb_handle_t config; confdb_handle_t top_handle = 0; hdb_handle_t local_handle; static confdb_callbacks_t callbacks = { }; do { rc = confdb_initialize(&config, &callbacks); if(rc < 0) { retries++; printf("Connection setup failed: %d. Retrying in %ds\n", rc, retries); sleep(retries); } else { break; } } while(retries < 5); #elif HAVE_CMAP cmap_handle_t local_handle; /* There can be only one (possibility if confdb isn't around) */ do { rc = cmap_initialize(&local_handle); if(rc < 0) { retries++; printf("Connection setup failed: %s. Retrying in %ds\n", cs_strerror(rc), retries); sleep(retries); } else { break; } } while(retries < 5); #endif if (rc != CS_OK) { printf("Could not initialize Cluster Configuration Database API instance, error %d\n", rc); return FALSE; } stack = get_cluster_type(); crm_info("Reading configure for stack: %s", name_for_cluster_type(stack)); /* =::=::= Should we be here =::=::= */ if (stack == pcmk_cluster_corosync) { setenv("HA_cluster_type", "corosync", 1); setenv("HA_quorum_type", "corosync", 1); #if HAVE_CONFDB } else if (stack == pcmk_cluster_cman) { setenv("HA_cluster_type", "cman", 1); setenv("HA_quorum_type", "cman", 1); enable_crmd_as_root(TRUE); use_cman = TRUE; } else if (stack == pcmk_cluster_classic_ais) { setenv("HA_cluster_type", "openais", 1); setenv("HA_quorum_type", "pcmk", 1); /* Look for a service block to indicate our plugin is loaded */ top_handle = config_find_init(config); local_handle = config_find_next(config, "service", top_handle); while (local_handle) { crm_free(value); get_config_opt(config, local_handle, "name", &value, NULL); if (safe_str_eq("pacemaker", value)) { crm_free(value); get_config_opt(config, local_handle, "ver", &value, "0"); if (safe_str_eq(value, "1")) { crm_free(value); get_config_opt(config, local_handle, "use_logd", &value, "no"); setenv("HA_use_logd", value, 1); setenv("HA_LOGD", value, 1); crm_free(value); get_config_opt(config, local_handle, "use_mgmtd", &value, "no"); enable_mgmtd(crm_is_true(value)); } else { crm_err("We can only start Pacemaker from init if using version 1" " of the Pacemaker plugin for Corosync. Terminating."); exit(100); } break; } local_handle = config_find_next(config, "service", top_handle); } crm_free(value); #endif } else { crm_err("Unsupported stack type: %s", name_for_cluster_type(stack)); return FALSE; } #if HAVE_CONFDB top_handle = config_find_init(config); local_handle = config_find_next(config, "logging", top_handle); get_config_opt(config, local_handle, "debug", &logging_debug, "off"); get_config_opt(config, local_handle, "logfile", &logging_logfile, "/var/log/pacemaker"); get_config_opt(config, local_handle, "to_logfile", &logging_to_logfile, "off"); get_config_opt(config, local_handle, "to_syslog", &logging_to_syslog, "on"); get_config_opt(config, local_handle, "syslog_facility", &logging_syslog_facility, "daemon"); confdb_finalize(config); #elif HAVE_CMAP /* =::=::= Logging =::=::= */ get_config_opt(local_handle, "logging.debug", &logging_debug, "off"); get_config_opt(local_handle, "logging.logfile", &logging_logfile, "/var/log/pacemaker"); get_config_opt(local_handle, "logging.to_logfile", &logging_to_logfile, "off"); get_config_opt(local_handle, "logging.to_syslog", &logging_to_syslog, "on"); get_config_opt(local_handle, "logging.syslog_facility", &logging_syslog_facility, "daemon"); cmap_finalize(local_handle); #endif if (crm_is_true(logging_debug)) { setenv("HA_debug", "1", 1); if(get_crm_log_level() < LOG_DEBUG) { set_crm_log_level(LOG_DEBUG); } } else { setenv("HA_debug", "0", 1); } if (crm_is_true(logging_to_logfile)) { struct stat parent; uid_t pcmk_uid = 0; uid_t pcmk_gid = getegid(); FILE *logfile = NULL; char *parent_dir = NULL; struct passwd *pcmk_user = getpwnam(CRM_DAEMON_USER); if(pcmk_user) { pcmk_uid = pcmk_user->pw_uid; } else { crm_err("User %s does not exist. Terminating", CRM_DAEMON_USER); exit(100); } parent_dir = dirname(strdup(logging_logfile)); rc = stat(parent_dir, &parent); if (rc != 0) { crm_err("Directory '%s' does not exist for logfile '%s'", parent_dir, logging_logfile); } else if (parent.st_uid == pcmk_uid && (parent.st_mode & (S_IRUSR | S_IWUSR))) { /* all good - user */ logfile = fopen(logging_logfile, "a"); } else if (parent.st_gid == pcmk_gid && (parent.st_mode & S_IXGRP)) { /* all good - group */ logfile = fopen(logging_logfile, "a"); } else { crm_err ("Daemons running as %s do not have permission to access '%s'. Logging to '%s' is disabled", CRM_DAEMON_USER, parent_dir, logging_logfile); crm_err ("Either %s must be owned by %s with rwx permissions, or have rwx permissions for group %d", parent_dir, CRM_DAEMON_USER, pcmk_gid); } if (logfile) { int logfd = fileno(logfile); setenv("HA_debugfile", logging_logfile, 1); setenv("HA_DEBUGLOG", logging_logfile, 1); setenv("HA_LOGFILE", logging_logfile, 1); /* Ensure the file has the correct permissions */ rc = fchown(logfd, pcmk_uid, pcmk_gid); if(rc < 0) { crm_warn("Cannot change the ownership of %s to user %s and gid %d", logging_logfile, CRM_DAEMON_USER, pcmk_gid); } rc = fchmod(logfd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if(rc < 0) { crm_warn("Cannot change the mode of %s to rw-rw----", logging_logfile); } have_log = TRUE; fprintf(logfile, "Set r/w permissions for uid=%d, gid=%d on %s\n", pcmk_uid, pcmk_gid, logging_logfile); if(fflush(logfile) < 0 || fsync(logfd) < 0) { crm_err("Couldn't write out logfile: %s", logging_logfile); have_log = FALSE; } fclose(logfile); } else { crm_err("Couldn't create logfile: %s", logging_logfile); } crm_free(parent_dir); } if (have_log && crm_is_true(logging_to_syslog) == FALSE) { crm_info("User configured file based logging and explicitly disabled syslog."); free(logging_syslog_facility); logging_syslog_facility = NULL; } else { if (crm_is_true(logging_to_syslog) == FALSE) { crm_err ("Please enable some sort of logging, either 'to_logfile: on' or 'to_syslog: on'."); crm_err("If you use file logging, be sure to also define a value for 'logfile'"); } } if(logging_syslog_facility) { setenv("HA_logfacility", logging_syslog_facility, 1); setenv("HA_LOGFACILITY", logging_syslog_facility, 1); } else { unsetenv("HA_logfacility"); unsetenv("HA_LOGFACILITY"); } free(logging_debug); free(logging_logfile); free(logging_to_logfile); free(logging_to_syslog); free(logging_syslog_facility); return TRUE; }
int main(int argc, char *argv[]) { enum user_action action; int c; cs_error_t err; cmap_handle_t handle; int i; size_t value_len; cmap_value_types_t type; int track_prefix; int no_retries; char * settings_file = NULL; action = ACTION_PRINT_PREFIX; track_prefix = 1; while ((c = getopt(argc, argv, "hgsdDtTbp:")) != -1) { switch (c) { case 'h': return print_help(); break; case 'b': show_binary++; break; case 'g': action = ACTION_GET; break; case 's': action = ACTION_SET; break; case 'd': action = ACTION_DELETE; break; case 'D': action = ACTION_DELETE_PREFIX; break; case 'p': settings_file = optarg; action = ACTION_LOAD; break; case 't': action = ACTION_TRACK; track_prefix = 0; break; case 'T': action = ACTION_TRACK; break; case '?': return (EXIT_FAILURE); break; default: action = ACTION_PRINT_PREFIX; break; } } if (argc == 1 || (argc == 2 && show_binary)) { action = ACTION_PRINT_ALL; } argc -= optind; argv += optind; if (argc == 0 && action != ACTION_LOAD && action != ACTION_PRINT_ALL) { fprintf(stderr, "Expected key after options\n"); return (EXIT_FAILURE); } no_retries = 0; while ((err = cmap_initialize(&handle)) == CS_ERR_TRY_AGAIN && no_retries++ < MAX_TRY_AGAIN) { sleep(1); } if (err != CS_OK) { fprintf (stderr, "Failed to initialize the cmap API. Error %s\n", cs_strerror(err)); exit (EXIT_FAILURE); } switch (action) { case ACTION_PRINT_ALL: print_iter(handle, NULL); break; case ACTION_PRINT_PREFIX: for (i = 0; i < argc; i++) { print_iter(handle, argv[i]); } break; case ACTION_GET: for (i = 0; i < argc; i++) { err = cmap_get(handle, argv[i], NULL, &value_len, &type); if (err == CS_OK) { print_key(handle, argv[i], value_len, NULL, type); } else { fprintf(stderr, "Can't get key %s. Error %s\n", argv[i], cs_strerror(err)); } } break; case ACTION_DELETE: for (i = 0; i < argc; i++) { err = cmap_delete(handle, argv[i]); if (err != CS_OK) { fprintf(stderr, "Can't delete key %s. Error %s\n", argv[i], cs_strerror(err)); } } break; case ACTION_DELETE_PREFIX: for (i = 0; i < argc; i++) { delete_with_prefix(handle, argv[i]); } break; case ACTION_LOAD: read_in_config_file(handle, settings_file); break; case ACTION_TRACK: for (i = 0; i < argc; i++) { add_track(handle, argv[i], track_prefix); } track_changes(handle); break; case ACTION_SET: if (argc < 3) { fprintf(stderr, "At least 3 parameters are expected for set\n"); return (EXIT_FAILURE); } set_key(handle, argv[0], argv[1], argv[2]); break; } err = cmap_finalize(handle); if (err != CS_OK) { fprintf (stderr, "Failed to finalize the cmap API. Error %s\n", cs_strerror(err)); exit (EXIT_FAILURE); } return (0); }
gboolean read_config(void) { int rc = CS_OK; int retries = 0; gboolean have_log = FALSE; char *logging_debug = NULL; char *logging_logfile = NULL; char *logging_to_logfile = NULL; char *logging_to_syslog = NULL; char *logging_syslog_facility = NULL; enum cluster_type_e stack = pcmk_cluster_unknown; #if HAVE_CONFDB char *value = NULL; confdb_handle_t config; confdb_handle_t top_handle = 0; hdb_handle_t local_handle; static confdb_callbacks_t callbacks = { }; do { rc = confdb_initialize(&config, &callbacks); if(rc != CS_OK) { retries++; printf("Connection setup failed: %d. Retrying in %ds\n", rc, retries); sleep(retries); } else { break; } } while(retries < 5); #elif HAVE_CMAP cmap_handle_t local_handle; /* There can be only one (possibility if confdb isn't around) */ do { rc = cmap_initialize(&local_handle); if(rc != CS_OK) { retries++; printf("API connection setup failed: %s. Retrying in %ds\n", cs_strerror(rc), retries); crm_info("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } else { break; } } while(retries < 5); #endif if (rc != CS_OK) { printf("Could not connect to Cluster Configuration Database API, error %d\n", rc); crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } stack = get_cluster_type(); crm_info("Reading configure for stack: %s", name_for_cluster_type(stack)); /* =::=::= Should we be here =::=::= */ if (stack == pcmk_cluster_corosync) { setenv("HA_cluster_type", "corosync", 1); setenv("HA_quorum_type", "corosync", 1); #if HAVE_CONFDB } else if (stack == pcmk_cluster_cman) { setenv("HA_cluster_type", "cman", 1); setenv("HA_quorum_type", "cman", 1); enable_crmd_as_root(TRUE); use_cman = TRUE; } else if (stack == pcmk_cluster_classic_ais) { setenv("HA_cluster_type", "openais", 1); setenv("HA_quorum_type", "pcmk", 1); /* Look for a service block to indicate our plugin is loaded */ top_handle = config_find_init(config); local_handle = config_find_next(config, "service", top_handle); while (local_handle) { get_config_opt(config, local_handle, "name", &value, NULL); if (safe_str_eq("pacemaker", value)) { get_config_opt(config, local_handle, "ver", &value, "0"); if (safe_str_eq(value, "1")) { get_config_opt(config, local_handle, "use_logd", &value, "no"); setenv("HA_use_logd", value, 1); setenv("HA_LOGD", value, 1); get_config_opt(config, local_handle, "use_mgmtd", &value, "no"); enable_mgmtd(crm_is_true(value)); } else { crm_err("We can only start Pacemaker from init if using version 1" " of the Pacemaker plugin for Corosync. Terminating."); exit(100); } break; } local_handle = config_find_next(config, "service", top_handle); } free(value); #endif } else { crm_err("Unsupported stack type: %s", name_for_cluster_type(stack)); return FALSE; } #if HAVE_CONFDB top_handle = config_find_init(config); local_handle = config_find_next(config, "logging", top_handle); get_config_opt(config, local_handle, "debug", &logging_debug, "off"); get_config_opt(config, local_handle, "logfile", &logging_logfile, "/var/log/pacemaker"); get_config_opt(config, local_handle, "to_logfile", &logging_to_logfile, "off"); get_config_opt(config, local_handle, "to_syslog", &logging_to_syslog, "on"); get_config_opt(config, local_handle, "syslog_facility", &logging_syslog_facility, "daemon"); confdb_finalize(config); #elif HAVE_CMAP /* =::=::= Logging =::=::= */ get_config_opt(local_handle, "logging.debug", &logging_debug, "off"); get_config_opt(local_handle, "logging.logfile", &logging_logfile, "/var/log/pacemaker"); get_config_opt(local_handle, "logging.to_logfile", &logging_to_logfile, "off"); get_config_opt(local_handle, "logging.to_syslog", &logging_to_syslog, "on"); get_config_opt(local_handle, "logging.syslog_facility", &logging_syslog_facility, "daemon"); cmap_finalize(local_handle); #endif if (crm_is_true(logging_debug)) { setenv("HA_debug", "1", 1); if(get_crm_log_level() < LOG_DEBUG) { set_crm_log_level(LOG_DEBUG); } } else { setenv("HA_debug", "0", 1); } if (crm_is_true(logging_to_logfile)) { if(crm_add_logfile(logging_logfile)) { setenv("HA_debugfile", logging_logfile, 1); setenv("HA_DEBUGLOG", logging_logfile, 1); setenv("HA_LOGFILE", logging_logfile, 1); have_log = TRUE; } else { crm_err("Couldn't create logfile: %s", logging_logfile); } } if (have_log && crm_is_true(logging_to_syslog) == FALSE) { crm_info("User configured file based logging and explicitly disabled syslog."); free(logging_syslog_facility); logging_syslog_facility = NULL; } else { if (crm_is_true(logging_to_syslog) == FALSE) { crm_err ("Please enable some sort of logging, either 'to_logfile: on' or 'to_syslog: on'."); crm_err("If you use file logging, be sure to also define a value for 'logfile'"); } } if(logging_syslog_facility) { setenv("HA_logfacility", logging_syslog_facility, 1); setenv("HA_LOGFACILITY", logging_syslog_facility, 1); } else { unsetenv("HA_logfacility"); unsetenv("HA_LOGFACILITY"); } free(logging_debug); free(logging_logfile); free(logging_to_logfile); free(logging_to_syslog); free(logging_syslog_facility); return TRUE; }
gboolean corosync_initialize_nodelist(void *cluster, gboolean force_member, xmlNode * xml_parent) { int lpc = 0; int rc = CS_OK; int retries = 0; gboolean any = FALSE; cmap_handle_t cmap_handle; do { rc = cmap_initialize(&cmap_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } crm_peer_init(); crm_trace("Initializing corosync nodelist"); for (lpc = 0;; lpc++) { uint32_t nodeid = 0; char *name = NULL; char *key = NULL; key = g_strdup_printf("nodelist.node.%d.nodeid", lpc); rc = cmap_get_uint32(cmap_handle, key, &nodeid); g_free(key); if (rc != CS_OK) { break; } name = corosync_node_name(cmap_handle, nodeid); if (name != NULL) { GHashTableIter iter; crm_node_t *node = NULL; g_hash_table_iter_init(&iter, crm_peer_cache); while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &node)) { if(node && node->uname && strcasecmp(node->uname, name) == 0) { if (node->id && node->id != nodeid) { crm_crit("Nodes %u and %u share the same name '%s': shutting down", node->id, nodeid, name); crm_exit(DAEMON_RESPAWN_STOP); } } } } if (nodeid > 0 || name != NULL) { crm_trace("Initializing node[%d] %u = %s", lpc, nodeid, name); crm_get_peer(nodeid, name); } if (nodeid > 0 && name != NULL) { any = TRUE; if (xml_parent) { xmlNode *node = create_xml_node(xml_parent, XML_CIB_TAG_NODE); crm_xml_add_int(node, XML_ATTR_ID, nodeid); crm_xml_add(node, XML_ATTR_UNAME, name); if (force_member) { crm_xml_add(node, XML_ATTR_TYPE, CRM_NODE_MEMBER); } } } free(name); } cmap_finalize(cmap_handle); return any; }
/* * CFG functionality stolen from node_name() in corosync-quorumtool.c * This resolves the first address assigned to a node and returns the name or IP address. */ char * corosync_node_name(uint64_t /*cmap_handle_t */ cmap_handle, uint32_t nodeid) { int lpc = 0; int rc = CS_OK; int retries = 0; char *name = NULL; cmap_handle_t local_handle = 0; /* nodeid == 0 == CMAN_NODEID_US */ if (nodeid == 0) { nodeid = get_local_nodeid(0); } if (cmap_handle == 0 && local_handle == 0) { retries = 0; crm_trace("Initializing CMAP connection"); do { rc = cmap_initialize(&local_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API, error %s", cs_strerror(rc)); local_handle = 0; } } if (cmap_handle == 0) { cmap_handle = local_handle; } while (name == NULL && cmap_handle != 0) { uint32_t id = 0; char *key = NULL; key = g_strdup_printf("nodelist.node.%d.nodeid", lpc); rc = cmap_get_uint32(cmap_handle, key, &id); crm_trace("Checking %u vs %u from %s", nodeid, id, key); g_free(key); if (rc != CS_OK) { break; } if (nodeid == id) { crm_trace("Searching for node name for %u in nodelist.node.%d %s", nodeid, lpc, name); if (name == NULL) { key = g_strdup_printf("nodelist.node.%d.ring0_addr", lpc); rc = cmap_get_string(cmap_handle, key, &name); crm_trace("%s = %s", key, name); if (node_name_is_valid(key, name) == FALSE) { free(name); name = NULL; } g_free(key); } if (name == NULL) { key = g_strdup_printf("nodelist.node.%d.name", lpc); rc = cmap_get_string(cmap_handle, key, &name); crm_trace("%s = %s %d", key, name, rc); g_free(key); } break; } lpc++; } if(local_handle) { cmap_finalize(local_handle); } if (name == NULL) { crm_notice("Unable to get node name for nodeid %u", nodeid); } return name; }
gboolean mcp_read_config(void) { int rc = CS_OK; int retries = 0; cmap_handle_t local_handle; uint64_t config = 0; // There can be only one possibility do { rc = cmap_initialize(&local_handle); if (rc != CS_OK) { retries++; printf("cmap connection setup failed: %s. Retrying in %ds\n", cs_strerror(rc), retries); crm_info("cmap connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } else { break; } } while (retries < 5); if (rc != CS_OK) { printf("Could not connect to Cluster Configuration Database API, error %d\n", rc); crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } stack = get_cluster_type(); crm_info("Reading configure for stack: %s", name_for_cluster_type(stack)); /* =::=::= Should we be here =::=::= */ if (stack == pcmk_cluster_corosync) { set_daemon_option("cluster_type", "corosync"); set_daemon_option("quorum_type", "corosync"); } else { crm_err("Unsupported stack type: %s", name_for_cluster_type(stack)); return FALSE; } /* =::=::= Logging =::=::= */ if (daemon_option("debug")) { /* Syslog logging is already setup by crm_log_init() */ } else { /* Check corosync */ char *debug_enabled = NULL; get_config_opt(config, local_handle, "logging.debug", &debug_enabled, "off"); if (crm_is_true(debug_enabled)) { set_daemon_option("debug", "1"); if (get_crm_log_level() < LOG_DEBUG) { set_crm_log_level(LOG_DEBUG); } } else { set_daemon_option("debug", "0"); } free(debug_enabled); } if(local_handle){ gid_t gid = 0; if (crm_user_lookup(CRM_DAEMON_USER, NULL, &gid) < 0) { crm_warn("Could not authorize group with corosync " CRM_XS " No group found for user %s", CRM_DAEMON_USER); } else { char key[PATH_MAX]; snprintf(key, PATH_MAX, "uidgid.gid.%u", gid); rc = cmap_set_uint8(local_handle, key, 1); if (rc != CS_OK) { crm_warn("Could not authorize group with corosync "CRM_XS " group=%u rc=%d (%s)", gid, rc, ais_error2text(rc)); } } } cmap_finalize(local_handle); return TRUE; }
gboolean read_config(void) { int rc = CS_OK; int retries = 0; gboolean have_log = FALSE; const char *const_value = NULL; char *logging_debug = NULL; char *logging_logfile = NULL; char *logging_to_logfile = NULL; char *logging_to_syslog = NULL; char *logging_syslog_facility = NULL; #if HAVE_CONFDB char *value = NULL; confdb_handle_t config; confdb_handle_t top_handle = 0; hdb_handle_t local_handle; static confdb_callbacks_t callbacks = { }; do { rc = confdb_initialize(&config, &callbacks); if (rc != CS_OK) { retries++; printf("Connection setup failed: %d. Retrying in %ds\n", rc, retries); sleep(retries); } else { break; } } while (retries < 5); #elif HAVE_CMAP cmap_handle_t local_handle; uint64_t config = 0; /* There can be only one (possibility if confdb isn't around) */ do { rc = cmap_initialize(&local_handle); if (rc != CS_OK) { retries++; printf("API connection setup failed: %s. Retrying in %ds\n", cs_strerror(rc), retries); crm_info("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } else { break; } } while (retries < 5); #endif if (rc != CS_OK) { printf("Could not connect to Cluster Configuration Database API, error %d\n", rc); crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } stack = get_cluster_type(); crm_info("Reading configure for stack: %s", name_for_cluster_type(stack)); /* =::=::= Should we be here =::=::= */ if (stack == pcmk_cluster_corosync) { set_daemon_option("cluster_type", "corosync"); set_daemon_option("quorum_type", "corosync"); #if HAVE_CONFDB } else if (stack == pcmk_cluster_cman) { set_daemon_option("cluster_type", "cman"); set_daemon_option("quorum_type", "cman"); enable_crmd_as_root(TRUE); } else if (stack == pcmk_cluster_classic_ais) { set_daemon_option("cluster_type", "openais"); set_daemon_option("quorum_type", "pcmk"); /* Look for a service block to indicate our plugin is loaded */ top_handle = config_find_init(config); local_handle = config_find_next(config, "service", top_handle); while (local_handle) { get_config_opt(config, local_handle, "name", &value, NULL); if (safe_str_eq("pacemaker", value)) { get_config_opt(config, local_handle, "ver", &value, "0"); if (safe_str_eq(value, "1")) { get_config_opt(config, local_handle, "use_logd", &value, "no"); set_daemon_option("use_logd", value); set_daemon_option("LOGD", value); get_config_opt(config, local_handle, "use_mgmtd", &value, "no"); enable_mgmtd(crm_is_true(value)); } else { crm_err("We can only start Pacemaker from init if using version 1" " of the Pacemaker plugin for Corosync. Terminating."); crm_exit(100); } break; } local_handle = config_find_next(config, "service", top_handle); } free(value); #endif } else { crm_err("Unsupported stack type: %s", name_for_cluster_type(stack)); return FALSE; } #if HAVE_CONFDB top_handle = config_find_init(config); local_handle = config_find_next(config, "logging", top_handle); #endif /* =::=::= Logging =::=::= */ get_config_opt(config, local_handle, KEY_PREFIX "debug", &logging_debug, "off"); const_value = daemon_option("debugfile"); if (const_value) { logging_to_logfile = strdup("on"); logging_logfile = strdup(const_value); crm_trace("Using debugfile setting from the environment: %s", logging_logfile); } else { get_config_opt(config, local_handle, KEY_PREFIX "to_logfile", &logging_to_logfile, "off"); get_config_opt(config, local_handle, KEY_PREFIX "logfile", &logging_logfile, "/var/log/pacemaker"); } const_value = daemon_option("logfacility"); if (const_value) { logging_syslog_facility = strdup(const_value); crm_trace("Using logfacility setting from the environment: %s", logging_syslog_facility); if (safe_str_eq(logging_syslog_facility, "none")) { logging_to_syslog = strdup("off"); } else { logging_to_syslog = strdup("on"); } } else { get_config_opt(config, local_handle, KEY_PREFIX "to_syslog", &logging_to_syslog, "on"); get_config_opt(config, local_handle, KEY_PREFIX "syslog_facility", &logging_syslog_facility, "daemon"); } #if HAVE_CONFDB confdb_finalize(config); #elif HAVE_CMAP cmap_finalize(local_handle); #endif if (daemon_option("debug")) { crm_trace("Using debug setting from the environment: %s", daemon_option("debug")); if (get_crm_log_level() < LOG_DEBUG && daemon_option_enabled("pacemakerd", "debug")) { set_crm_log_level(LOG_DEBUG); } } else if (crm_is_true(logging_debug)) { set_daemon_option("debug", "1"); if (get_crm_log_level() < LOG_DEBUG) { set_crm_log_level(LOG_DEBUG); } } else { set_daemon_option("debug", "0"); } if (crm_is_true(logging_to_logfile)) { if (crm_add_logfile(logging_logfile)) { /* What a cluster fsck, eventually we need to mandate /one/ */ set_daemon_option("debugfile", logging_logfile); set_daemon_option("DEBUGLOG", logging_logfile); have_log = TRUE; } else { crm_err("Couldn't create logfile: %s", logging_logfile); } } if (have_log && crm_is_true(logging_to_syslog) == FALSE) { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); free(logging_syslog_facility); logging_syslog_facility = strdup("none"); crm_info("User configured file based logging and explicitly disabled syslog."); } else if (crm_is_true(logging_to_syslog) == FALSE) { crm_err("Please enable some sort of logging, either 'to_logfile: on' or 'to_syslog: on'."); crm_err("If you use file logging, be sure to also define a value for 'logfile'"); } set_daemon_option("logfacility", logging_syslog_facility); free(logging_debug); free(logging_logfile); free(logging_to_logfile); free(logging_to_syslog); free(logging_syslog_facility); return TRUE; }
gboolean mcp_read_config(void) { int rc = CS_OK; int retries = 0; const char *const_value = NULL; #if HAVE_CONFDB char *value = NULL; confdb_handle_t config = 0; confdb_handle_t top_handle = 0; hdb_handle_t local_handle; static confdb_callbacks_t callbacks = { }; do { rc = confdb_initialize(&config, &callbacks); if (rc != CS_OK) { retries++; printf("confdb connection setup failed: %s. Retrying in %ds\n", ais_error2text(rc), retries); crm_info("confdb connection setup failed: %s. Retrying in %ds", ais_error2text(rc), retries); sleep(retries); } else { break; } } while (retries < 5); #elif HAVE_CMAP cmap_handle_t local_handle; uint64_t config = 0; /* There can be only one (possibility if confdb isn't around) */ do { rc = cmap_initialize(&local_handle); if (rc != CS_OK) { retries++; printf("cmap connection setup failed: %s. Retrying in %ds\n", cs_strerror(rc), retries); crm_info("cmap connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } else { break; } } while (retries < 5); #endif if (rc != CS_OK) { printf("Could not connect to Cluster Configuration Database API, error %d\n", rc); crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } stack = get_cluster_type(); crm_info("Reading configure for stack: %s", name_for_cluster_type(stack)); /* =::=::= Should we be here =::=::= */ if (stack == pcmk_cluster_corosync) { set_daemon_option("cluster_type", "corosync"); set_daemon_option("quorum_type", "corosync"); #if HAVE_CONFDB } else if (stack == pcmk_cluster_cman) { set_daemon_option("cluster_type", "cman"); set_daemon_option("quorum_type", "cman"); enable_crmd_as_root(TRUE); } else if (stack == pcmk_cluster_classic_ais) { set_daemon_option("cluster_type", "openais"); set_daemon_option("quorum_type", "pcmk"); /* Look for a service block to indicate our plugin is loaded */ top_handle = config_find_init(config); local_handle = config_find_next(config, "service", top_handle); while (local_handle) { get_config_opt(config, local_handle, "name", &value, NULL); if (safe_str_eq("pacemaker", value)) { get_config_opt(config, local_handle, "ver", &value, "0"); if (safe_str_eq(value, "1")) { get_config_opt(config, local_handle, "use_logd", &value, "no"); set_daemon_option("use_logd", value); set_daemon_option("LOGD", value); get_config_opt(config, local_handle, "use_mgmtd", &value, "no"); enable_mgmtd(crm_is_true(value)); } else { crm_err("We can only start Pacemaker from init if using version 1" " of the Pacemaker plugin for Corosync. Terminating."); crm_exit(DAEMON_RESPAWN_STOP); } break; } local_handle = config_find_next(config, "service", top_handle); } free(value); #endif } else { crm_err("Unsupported stack type: %s", name_for_cluster_type(stack)); return FALSE; } #if HAVE_CONFDB top_handle = config_find_init(config); local_handle = config_find_next(config, "logging", top_handle); #endif /* =::=::= Logging =::=::= */ if (daemon_option("debug")) { /* Syslog logging is already setup by crm_log_init() */ } else { /* Check corosync */ char *debug_enabled = NULL; get_config_opt(config, local_handle, KEY_PREFIX "debug", &debug_enabled, "off"); if (crm_is_true(debug_enabled)) { set_daemon_option("debug", "1"); if (get_crm_log_level() < LOG_DEBUG) { set_crm_log_level(LOG_DEBUG); } } else { set_daemon_option("debug", "0"); } free(debug_enabled); } const_value = daemon_option("debugfile"); if (daemon_option("logfile")) { /* File logging is already setup by crm_log_init() */ } else if(const_value) { /* From when we cared what options heartbeat used */ set_daemon_option("logfile", const_value); crm_add_logfile(const_value); } else { /* Check corosync */ char *logfile = NULL; char *logfile_enabled = NULL; get_config_opt(config, local_handle, KEY_PREFIX "to_logfile", &logfile_enabled, "on"); get_config_opt(config, local_handle, KEY_PREFIX "logfile", &logfile, "/var/log/pacemaker.log"); if (crm_is_true(logfile_enabled) == FALSE) { crm_trace("File logging disabled in corosync"); } else if (crm_add_logfile(logfile)) { set_daemon_option("logfile", logfile); } else { crm_err("Couldn't create logfile: %s", logfile); set_daemon_option("logfile", "none"); } free(logfile); free(logfile_enabled); } if (daemon_option("logfacility")) { /* Syslog logging is already setup by crm_log_init() */ } else { /* Check corosync */ char *syslog_enabled = NULL; char *syslog_facility = NULL; get_config_opt(config, local_handle, KEY_PREFIX "to_syslog", &syslog_enabled, "on"); get_config_opt(config, local_handle, KEY_PREFIX "syslog_facility", &syslog_facility, "daemon"); if (crm_is_true(syslog_enabled) == FALSE) { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); set_daemon_option("logfacility", "none"); } else { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, qb_log_facility2int(syslog_facility)); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE); set_daemon_option("logfacility", syslog_facility); } free(syslog_enabled); free(syslog_facility); } const_value = daemon_option("logfacility"); if (const_value) { /* cluster-glue module needs HA_LOGFACILITY */ setenv("HA_LOGFACILITY", const_value, 1); } #if HAVE_CONFDB confdb_finalize(config); #elif HAVE_CMAP if(local_handle){ gid_t gid = 0; if (crm_user_lookup(CRM_DAEMON_USER, NULL, &gid) < 0) { crm_warn("Could not authorize group with corosync " CRM_XS " No group found for user %s", CRM_DAEMON_USER); } else { char key[PATH_MAX]; snprintf(key, PATH_MAX, "uidgid.gid.%u", gid); rc = cmap_set_uint8(local_handle, key, 1); if (rc != CS_OK) { crm_warn("Could not authorize group with corosync "CRM_XS " group=%u rc=%d (%s)", gid, rc, ais_error2text(rc)); } } } cmap_finalize(local_handle); #endif return TRUE; }
/* * Test cmap integration + quit policy */ static int test8 (pid_t pid, pid_t old_pid, int test_n) { cmap_handle_t cmap_handle; cs_error_t err; uint64_t tstamp1, tstamp2; int32_t msec_diff; unsigned int instance_id; char key_name[CMAP_KEYNAME_MAXLEN]; char *str; err = cmap_initialize (&cmap_handle); if (err != CS_OK) { qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err); return (1); } qb_log (LOG_INFO, "test %d", test_n); if (test_n == 2) { /* * Object should not exist */ qb_log (LOG_INFO, "Testing if object exists (it shouldn't)"); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err == CS_OK) { qb_log (LOG_INFO, "Could find key \"%s\": %d.", key_name, err); free(str); return (2); } } if (test_n == 1 || test_n == 2) { qb_log (LOG_INFO, " initialize"); err = sam_initialize (2000, SAM_RECOVERY_POLICY_QUIT | SAM_RECOVERY_POLICY_CMAP); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 2; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err); return (2); } if (strcmp(str, "quit") != 0) { qb_log (LOG_INFO, "Recovery key \"%s\" is not \"quit\".", key_name); return (2); } free(str); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: stop", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: sleeping 5", instance_id); sleep (5); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start 2", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); if (test_n == 2) { qb_log (LOG_INFO, "iid %d: sleeping 5. Should be killed", instance_id); sleep (5); return (2); } else { qb_log (LOG_INFO, "iid %d: Test HC", instance_id); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.last_updated", pid); err = cmap_get_uint64(cmap_handle, key_name, &tstamp1); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } qb_log (LOG_INFO, "iid %d: Sleep 1", instance_id); sleep (1); err = sam_hc_send (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't send hc. Error %d", err); return 2; } sleep (1); err = cmap_get_uint64(cmap_handle, key_name, &tstamp2); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"last_updated\" key: %d.", err); return (2); } msec_diff = (tstamp2 - tstamp1)/CS_TIME_NS_IN_MSEC; if (msec_diff < 500 || msec_diff > 2000) { qb_log (LOG_INFO, "Difference %d is not within <500, 2000> interval.", msec_diff); return (2); } qb_log (LOG_INFO, "iid %d: stop 2", instance_id); err = sam_stop (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't stop hc. Error %d", err); return 2; } snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: exiting", instance_id); return (0); } } if (test_n == 3) { qb_log (LOG_INFO, "Testing if status is failed"); /* * Previous should be FAILED */ snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "failed") != 0) { qb_log (LOG_INFO, "State key is not \"failed\"."); return (2); } return (0); } return (2); }
gboolean corosync_initialize_nodelist(void *cluster, gboolean force_member, xmlNode * xml_parent) { int lpc = 0; cs_error_t rc = CS_OK; int retries = 0; gboolean any = FALSE; cmap_handle_t cmap_handle; int fd = -1; uid_t found_uid = 0; gid_t found_gid = 0; pid_t found_pid = 0; int rv; do { rc = cmap_initialize(&cmap_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API, error %d", rc); return FALSE; } rc = cmap_fd_get(cmap_handle, &fd); if (rc != CS_OK) { crm_err("Could not obtain the CMAP API connection: %s (%d)", cs_strerror(rc), rc); goto bail; } /* CMAP provider run as root (in given user namespace, anyway)? */ if (!(rv = crm_ipc_is_authentic_process(fd, (uid_t) 0,(gid_t) 0, &found_pid, &found_uid, &found_gid))) { crm_err("CMAP provider is not authentic:" " process %lld (uid: %lld, gid: %lld)", (long long) PCMK__SPECIAL_PID_AS_0(found_pid), (long long) found_uid, (long long) found_gid); goto bail; } else if (rv < 0) { crm_err("Could not verify authenticity of CMAP provider: %s (%d)", strerror(-rv), -rv); goto bail; } crm_peer_init(); crm_trace("Initializing corosync nodelist"); for (lpc = 0; TRUE; lpc++) { uint32_t nodeid = 0; char *name = NULL; char *key = NULL; key = crm_strdup_printf("nodelist.node.%d.nodeid", lpc); rc = cmap_get_uint32(cmap_handle, key, &nodeid); free(key); if (rc != CS_OK) { break; } name = corosync_node_name(cmap_handle, nodeid); if (name != NULL) { GHashTableIter iter; crm_node_t *node = NULL; g_hash_table_iter_init(&iter, crm_peer_cache); while (g_hash_table_iter_next(&iter, NULL, (gpointer *) &node)) { if(node && node->uname && strcasecmp(node->uname, name) == 0) { if (node->id && node->id != nodeid) { crm_crit("Nodes %u and %u share the same name '%s': shutting down", node->id, nodeid, name); crm_exit(CRM_EX_FATAL); } } } } if (nodeid > 0 || name != NULL) { crm_trace("Initializing node[%d] %u = %s", lpc, nodeid, name); crm_get_peer(nodeid, name); } if (nodeid > 0 && name != NULL) { any = TRUE; if (xml_parent) { xmlNode *node = create_xml_node(xml_parent, XML_CIB_TAG_NODE); crm_xml_set_id(node, "%u", nodeid); crm_xml_add(node, XML_ATTR_UNAME, name); if (force_member) { crm_xml_add(node, XML_ATTR_TYPE, CRM_NODE_MEMBER); } } } free(name); } bail: cmap_finalize(cmap_handle); return any; }
/* * CFG functionality stolen from node_name() in corosync-quorumtool.c * This resolves the first address assigned to a node and returns the name or IP address. */ char * corosync_node_name(uint64_t /*cmap_handle_t */ cmap_handle, uint32_t nodeid) { int lpc = 0; cs_error_t rc = CS_OK; int retries = 0; char *name = NULL; cmap_handle_t local_handle = 0; int fd = -1; uid_t found_uid = 0; gid_t found_gid = 0; pid_t found_pid = 0; int rv; if (nodeid == 0) { nodeid = get_local_nodeid(0); } if (cmap_handle == 0 && local_handle == 0) { retries = 0; crm_trace("Initializing CMAP connection"); do { rc = cmap_initialize(&local_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API, error %s", cs_strerror(rc)); local_handle = 0; } } if (cmap_handle == 0) { cmap_handle = local_handle; rc = cmap_fd_get(cmap_handle, &fd); if (rc != CS_OK) { crm_err("Could not obtain the CMAP API connection: %s (%d)", cs_strerror(rc), rc); goto bail; } /* CMAP provider run as root (in given user namespace, anyway)? */ if (!(rv = crm_ipc_is_authentic_process(fd, (uid_t) 0,(gid_t) 0, &found_pid, &found_uid, &found_gid))) { crm_err("CMAP provider is not authentic:" " process %lld (uid: %lld, gid: %lld)", (long long) PCMK__SPECIAL_PID_AS_0(found_pid), (long long) found_uid, (long long) found_gid); goto bail; } else if (rv < 0) { crm_err("Could not verify authenticity of CMAP provider: %s (%d)", strerror(-rv), -rv); goto bail; } } while (name == NULL && cmap_handle != 0) { uint32_t id = 0; char *key = NULL; key = crm_strdup_printf("nodelist.node.%d.nodeid", lpc); rc = cmap_get_uint32(cmap_handle, key, &id); crm_trace("Checking %u vs %u from %s", nodeid, id, key); free(key); if (rc != CS_OK) { break; } if (nodeid == id) { crm_trace("Searching for node name for %u in nodelist.node.%d %s", nodeid, lpc, name); if (name == NULL) { key = crm_strdup_printf("nodelist.node.%d.name", lpc); cmap_get_string(cmap_handle, key, &name); crm_trace("%s = %s", key, name); free(key); } if (name == NULL) { key = crm_strdup_printf("nodelist.node.%d.ring0_addr", lpc); cmap_get_string(cmap_handle, key, &name); crm_trace("%s = %s", key, name); if (node_name_is_valid(key, name) == FALSE) { free(name); name = NULL; } free(key); } break; } lpc++; } bail: if(local_handle) { cmap_finalize(local_handle); } if (name == NULL) { crm_info("Unable to get node name for nodeid %u", nodeid); } return name; }
int corosync_cmap_has_config(const char *prefix) { cs_error_t rc = CS_OK; int retries = 0; static int found = -1; cmap_handle_t cmap_handle; cmap_iter_handle_t iter_handle; char key_name[CMAP_KEYNAME_MAXLEN + 1]; int fd = -1; uid_t found_uid = 0; gid_t found_gid = 0; pid_t found_pid = 0; int rv; if(found != -1) { return found; } do { rc = cmap_initialize(&cmap_handle); if (rc != CS_OK) { retries++; crm_debug("API connection setup failed: %s. Retrying in %ds", cs_strerror(rc), retries); sleep(retries); } } while (retries < 5 && rc != CS_OK); if (rc != CS_OK) { crm_warn("Could not connect to Cluster Configuration Database API: %s (rc=%d)", cs_strerror(rc), rc); return -1; } rc = cmap_fd_get(cmap_handle, &fd); if (rc != CS_OK) { crm_err("Could not obtain the CMAP API connection: %s (%d)", cs_strerror(rc), rc); goto bail; } /* CMAP provider run as root (in given user namespace, anyway)? */ if (!(rv = crm_ipc_is_authentic_process(fd, (uid_t) 0,(gid_t) 0, &found_pid, &found_uid, &found_gid))) { crm_err("CMAP provider is not authentic:" " process %lld (uid: %lld, gid: %lld)", (long long) PCMK__SPECIAL_PID_AS_0(found_pid), (long long) found_uid, (long long) found_gid); goto bail; } else if (rv < 0) { crm_err("Could not verify authenticity of CMAP provider: %s (%d)", strerror(-rv), -rv); goto bail; } rc = cmap_iter_init(cmap_handle, prefix, &iter_handle); if (rc != CS_OK) { crm_warn("Failed to initialize iteration for corosync cmap '%s': %s (rc=%d)", prefix, cs_strerror(rc), rc); goto bail; } found = 0; while ((rc = cmap_iter_next(cmap_handle, iter_handle, key_name, NULL, NULL)) == CS_OK) { crm_trace("'%s' is configured in corosync cmap: %s", prefix, key_name); found++; break; } cmap_iter_finalize(cmap_handle, iter_handle); bail: cmap_finalize(cmap_handle); return found; }
/* * Test cmap integration + restart policy */ static int test9 (pid_t pid, pid_t old_pid, int test_n) { cs_error_t err; cmap_handle_t cmap_handle; unsigned int instance_id; char *str; char key_name[CMAP_KEYNAME_MAXLEN]; err = cmap_initialize (&cmap_handle); if (err != CS_OK) { qb_log (LOG_INFO, "Could not initialize Cluster Map API instance error %d. Test skipped", err); return (1); } qb_log (LOG_INFO, "test %d", test_n); if (test_n == 1) { qb_log (LOG_INFO, " initialize"); err = sam_initialize (2000, SAM_RECOVERY_POLICY_RESTART | SAM_RECOVERY_POLICY_CMAP); if (err != CS_OK) { qb_log (LOG_ERR, "Can't initialize SAM API. Error %d", err); return 2; } qb_log (LOG_INFO, " register"); err = sam_register (&instance_id); if (err != CS_OK) { qb_log (LOG_ERR, "Can't register. Error %d", err); return 2; } qb_log (LOG_INFO, " iid %d", instance_id); if (instance_id < 3) { snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.recovery", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"recovery\" key: %d.", err); return (2); } if (strcmp(str, "restart") != 0) { qb_log (LOG_INFO, "Recovery key \"%s\" is not \"restart\".", str); return (2); } free(str); snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "stopped") != 0) { qb_log (LOG_INFO, "State key is not \"stopped\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: start", instance_id); err = sam_start (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't start hc. Error %d", err); return 2; } err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "running") != 0) { qb_log (LOG_INFO, "State key is not \"running\"."); return (2); } free(str); qb_log (LOG_INFO, "iid %d: waiting for kill", instance_id); sleep (10); return (2); } if (instance_id == 3) { qb_log (LOG_INFO, "iid %d: mark failed", instance_id); err = sam_mark_failed (); if (err != CS_OK) { qb_log (LOG_ERR, "Can't mark failed. Error %d", err); return 2; } sleep (10); return (2); } return (2); } if (test_n == 2) { qb_log (LOG_INFO, "Testing if status is failed"); /* * Previous should be FAILED */ snprintf(key_name, CMAP_KEYNAME_MAXLEN, "resources.process.%d.state", pid); err = cmap_get_string(cmap_handle, key_name, &str); if (err != CS_OK) { qb_log (LOG_INFO, "Could not get \"state\" key: %d.", err); return (2); } if (strcmp(str, "failed") != 0) { qb_log (LOG_INFO, "State key is not \"failed\"."); return (2); } free(str); return (0); } return (2); }
int main(int argc, char *argv[]) { #ifdef USE_CONFDB confdb_handle_t handle; confdb_callbacks_t callbacks; hdb_handle_t object_handle; #endif #ifdef USE_CMAP cmap_handle_t handle; cmap_track_handle_t track_handle; int retries; cs_error_t result; #endif uint32_t u32; int status; int i; cs_error_t res; char str_val[255]; int ch; char *ep; change_uint32 = 0; change_str_len = 0; no_childs = 16; burst_count = 64; while ((ch = getopt(argc, argv, "hus:c:n:")) != -1) { switch (ch) { case 'u': change_uint32 = 1; break; case 's': change_str_len = strtol(optarg, &ep, 10); if (change_str_len <= 0 || *ep != '\0') { warnx("illegal number, -s argument -- %s", optarg); usage(); } break; case 'c': no_childs = strtol(optarg, &ep, 10); if (no_childs <= 0 || *ep != '\0') { warnx("illegal number, -c argument -- %s", optarg); usage(); } break; case 'n': burst_count = strtol(optarg, &ep, 10); if (burst_count <= 0 || *ep != '\0') { warnx("illegal number, -n argument -- %s", optarg); usage(); } break; case 'h': case '?': default: usage(); /* NOTREACHED */ } } signal(SIGPIPE, SIG_IGN); setlinebuf(stdout); #ifdef USE_CONFDB memset(&callbacks, 0, sizeof(callbacks)); assert(confdb_initialize(&handle, &callbacks) == CS_OK); assert(confdb_object_create(handle, OBJECT_PARENT_HANDLE, "testconfdb", strlen("testconfdb"), &object_handle) == CS_OK); assert(confdb_finalize(handle) == CS_OK); #endif my_id = create_childs(); u32 = my_id; #ifdef USE_CONFDB snprintf(my_key_uint, sizeof(my_key_uint), "testkeyu32id%u", my_id); snprintf(my_key_str, sizeof(my_key_str), "testkeystrid%u", my_id); #endif #ifdef USE_CMAP snprintf(my_key_uint, sizeof(my_key_uint), "testconfdb.testkeyu32id%u", my_id); snprintf(my_key_str, sizeof(my_key_str), "testconfdb.testkeystrid%u", my_id); #endif for (i = 0; i < change_str_len; i++) { str_val[i] = ((my_id + i) % ('Z' - 'A' + 1)) + 'A'; } str_val[i] = '\0'; if (my_id > 0) { #ifdef USE_CONFDB memset(&callbacks, 0, sizeof(callbacks)); callbacks.confdb_key_change_notify_fn = confdb_key_change_notify; assert(confdb_initialize(&handle, &callbacks) == CS_OK); #endif #ifdef USE_CMAP retries = 0; cs_repeat(retries, 30, result = cmap_initialize(&handle)); assert(result == CS_OK); #endif if (change_uint32) { #ifdef USE_CONFDB assert(confdb_key_create_typed(handle, object_handle, my_key_uint, &u32, sizeof(u32), CONFDB_VALUETYPE_UINT32) == CS_OK); #endif #ifdef USE_CMAP assert(cmap_set_uint32(handle, my_key_uint, u32) == CS_OK); #endif } if (change_str_len > 0) { #ifdef USE_CONFDB assert(confdb_key_create_typed(handle, object_handle, my_key_str, str_val, strlen(str_val), CONFDB_VALUETYPE_STRING) == CS_OK); #endif #ifdef USE_CMAP assert(cmap_set_string(handle, my_key_str, str_val) == CS_OK); #endif } } else { /* * "Wait" for other processes to initialize */ poll(NULL, 0, 1000); printf("Confdb-track-and-change initialization finished\n"); } if (my_id > 0) { signal(SIGINT, sigint_handler_child); #ifdef USE_CONFDB assert(confdb_track_changes(handle, object_handle, OBJECT_KEY_REPLACED) == CS_OK); #endif #ifdef USE_CMAP assert(cmap_track_add(handle, "testconfdb.", CMAP_TRACK_MODIFY | CMAP_TRACK_PREFIX, cmap_notify, NULL, &track_handle) == CS_OK); #endif if (change_uint32) { #ifdef USE_CONFDB assert(confdb_key_increment(handle, object_handle, my_key_uint, strlen(my_key_uint), &u32) == CS_OK); #endif #ifdef USE_CMAP assert(cmap_inc(handle, my_key_uint) == CS_OK); #endif expected_msgs_uint = 1; } if (change_str_len > 0) { inc_str(str_val, change_str_len); #ifdef USE_CONFDB assert(confdb_key_replace(handle, object_handle, my_key_str, strlen(my_key_str), NULL, 0, str_val, strlen(str_val)) == CS_OK); #endif #ifdef USE_CMAP assert(cmap_set_string(handle, my_key_str, str_val) == CS_OK); #endif expected_msgs_str = 1; } /* * Give other processes a little time to initialize */ poll(NULL, 0, 250); do { #ifdef USE_CONFDB res = confdb_dispatch(handle, CS_DISPATCH_BLOCKING); #endif #ifdef USE_CMAP res = cmap_dispatch(handle, CS_DISPATCH_BLOCKING); #endif } while (res == CS_OK || res == CS_ERR_TRY_AGAIN); } else { signal(SIGINT, sigint_handler_parent); for (i = 0; i < no_childs; i++) { wait(&status); } #ifdef USE_CONFDB confdb_object_destroy(handle, object_handle); #endif printf("Confdb-track-and-change finalization finished\n"); } return (0); }