int setup_cluster_cfg(void) { cs_error_t err; unsigned int nodeid; int fd; err = corosync_cfg_initialize(&ch, &cfg_callbacks); if (err != CS_OK) { log_error("corosync cfg init error %d", err); return -1; } err = corosync_cfg_fd_get(ch, &fd); if (err != CS_OK) { log_error("corosync cfg fd_get error %d", err); corosync_cfg_finalize(ch); return -1; } err = corosync_cfg_local_get(ch, &nodeid); if (err != CS_OK) { log_error("corosync cfg local_get error %d", err); corosync_cfg_finalize(ch); return -1; } our_nodeid = nodeid; log_debug("our_nodeid %d", our_nodeid); return fd; }
static void _cs_local_node_info_get(char **nodename, uint32_t *nodeid) { cs_error_t rc; corosync_cfg_handle_t cfg_handle; if (local_nodeid == 0) { rc = corosync_cfg_initialize(&cfg_handle, NULL); if (rc != CS_OK) { syslog (LOG_ERR, "Failed to initialize the cfg API. Error %d\n", rc); exit (EXIT_FAILURE); } rc = corosync_cfg_local_get (cfg_handle, &local_nodeid); corosync_cfg_finalize(cfg_handle); if (rc != CS_OK) { local_nodeid = 0; strncpy(local_nodename, "localhost", sizeof (local_nodename)); local_nodename[sizeof (local_nodename) - 1] = '\0'; } else { gethostname(local_nodename, CS_MAX_NAME_LENGTH); } } *nodeid = local_nodeid; *nodename = local_nodename; }
static int reload_config_do (void) { cs_error_t result; corosync_cfg_handle_t handle; int rc; rc = 0; printf ("Reloading corosync.conf...\n"); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %s\n", cs_strerror(result)); exit (1); } result = corosync_cfg_reload_config (handle); if (result != CS_OK) { printf ("Could not reload configuration. Error %s\n", cs_strerror(result)); rc = (int)result; } else { printf ("Done\n"); } (void)corosync_cfg_finalize (handle); return (rc); }
gboolean cluster_disconnect_cfg(void) { if (cfg_handle) { corosync_cfg_finalize(cfg_handle); cfg_handle = 0; } pcmk_shutdown(SIGTERM); return TRUE; }
static int ringstatusget_do (char *interface_name) { cs_error_t result; corosync_cfg_handle_t handle; unsigned int interface_count; char **interface_names; char **interface_status; unsigned int i; unsigned int nodeid; int rc = 0; printf ("Printing ring status.\n"); result = corosync_cfg_initialize (&handle, NULL); printf ("xxx 47500: tools/corosync-cfgtool.c After corosync_cfg_initialize...\n"); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_local_get(handle, &nodeid); if (result != CS_OK) { printf ("Could not get the local node id, the error is: %d\n", result); } else { printf ("Local node ID %d\n", nodeid); } result = corosync_cfg_ring_status_get (handle, &interface_names, &interface_status, &interface_count); if (result != CS_OK) { printf ("Could not get the ring status, the error is: %d\n", result); } else { for (i = 0; i < interface_count; i++) { if ( (interface_name && (interface_name[0]=='\0' || strcasecmp (interface_name, interface_names[i]) == 0)) || !interface_name ) { printf ("RING ID %d\n", i); printf ("\tid\t= %s\n", interface_names[i]); printf ("\tstatus\t= %s\n", interface_status[i]); if (strstr(interface_status[i], "FAULTY")) { rc = 1; } } } } (void)corosync_cfg_finalize (handle); return rc; }
static int cfg_dispatch_wrapper_fn ( int fd, int revents, void *data) { cs_error_t error; if (revents & POLLHUP || revents & POLLERR) { qb_log (LOG_ERR, "got POLLHUP disconnecting from CFG"); corosync_cfg_finalize(cfg_handle); cfg_handle = 0; return -1; } error = corosync_cfg_dispatch (cfg_handle, CS_DISPATCH_ALL); if (error == CS_ERR_LIBRARY) { qb_log (LOG_ERR, "got LIB error disconnecting from CFG."); corosync_cfg_finalize(cfg_handle); cfg_handle = 0; return -1; } return 0; }
static void my_pre_exit(void) { qb_log (LOG_INFO, "%s PRE EXIT", __FILE__); if (cpg_handle > 0) { cpg_finalize (cpg_handle); cpg_handle = 0; } if (cfg_handle > 0) { corosync_cfg_finalize (cfg_handle); cfg_handle = 0; } PK11_DestroyContext(sha1_context, PR_TRUE); }
static void close_all(void) { if (cmap_handle) { cmap_finalize(cmap_handle); } if (q_handle) { quorum_finalize(q_handle); } if (c_handle) { corosync_cfg_finalize(c_handle); } if (v_handle) { votequorum_finalize(v_handle); } }
static void service_unload_do (const char *service, unsigned int version) { cs_error_t result; corosync_cfg_handle_t handle; printf ("Unloading service '%s' version '%d'\n", service, version); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_service_unload (handle, service, version); if (result != CS_OK) { printf ("Could not unload service (error = %d)\n", result); } (void)corosync_cfg_finalize (handle); }
static void killnode_do(unsigned int nodeid) { cs_error_t result; corosync_cfg_handle_t handle; printf ("Killing node %d\n", nodeid); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool"); if (result != CS_OK) { printf ("Could not kill node (error = %d)\n", result); } (void)corosync_cfg_finalize (handle); }
static void showaddrs_do(unsigned int nodeid) { cs_error_t result; corosync_cfg_handle_t handle; corosync_cfg_callbacks_t callbacks; int numaddrs; int i; corosync_cfg_node_address_t addrs[INTERFACE_MAX]; result = corosync_cfg_initialize (&handle, &callbacks); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) { for (i=0; i<numaddrs; i++) { char buf[INET6_ADDRSTRLEN]; struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address; struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address; struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address; void *saddr; if (!ss->ss_family) { continue; } if (ss->ss_family == AF_INET6) { saddr = &sin6->sin6_addr; } else { saddr = &sin->sin_addr; } inet_ntop(ss->ss_family, saddr, buf, sizeof(buf)); if (i != 0) { printf(" "); } printf("%s", buf); } printf("\n"); } (void)corosync_cfg_finalize (handle); }
static void crypto_do(unsigned int type) { cs_error_t result; corosync_cfg_handle_t handle; printf ("Setting crypto to mode %d\n", type); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_crypto_set (handle, type); if (result != CS_OK) { printf ("Could not set crypto mode (error = %d)\n", result); } (void)corosync_cfg_finalize (handle); }
static void ringreenable_do (void) { cs_error_t result; corosync_cfg_handle_t handle; printf ("Re-enabling all failed rings.\n"); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_ring_reenable (handle); if (result != CS_OK) { printf ("Could not reenable ring error %d\n", result); } (void)corosync_cfg_finalize (handle); }
gboolean cluster_connect_cfg(uint32_t * nodeid) { cs_error_t rc; int fd = 0, retries = 0; static struct mainloop_fd_callbacks cfg_fd_callbacks = { .dispatch = pcmk_cfg_dispatch, .destroy = cfg_connection_destroy, }; cs_repeat(retries, 30, rc = corosync_cfg_initialize(&cfg_handle, &cfg_callbacks)); if (rc != CS_OK) { crm_err("corosync cfg init error %d", rc); return FALSE; } rc = corosync_cfg_fd_get(cfg_handle, &fd); if (rc != CS_OK) { crm_err("corosync cfg fd_get error %d", rc); goto bail; } retries = 0; cs_repeat(retries, 30, rc = corosync_cfg_local_get(cfg_handle, nodeid)); if (rc != CS_OK) { crm_err("corosync cfg local_get error %d", rc); goto bail; } crm_debug("Our nodeid: %d", *nodeid); mainloop_add_fd("corosync-cfg", G_PRIORITY_DEFAULT, fd, &cfg_handle, &cfg_fd_callbacks); return TRUE; bail: corosync_cfg_finalize(cfg_handle); return FALSE; }
static void ringstatusget_do (void) { cs_error_t result; corosync_cfg_handle_t handle; unsigned int interface_count; char **interface_names; char **interface_status; unsigned int i; unsigned int nodeid; printf ("Printing ring status.\n"); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_local_get(handle, &nodeid); if (result != CS_OK) { printf ("Could not get the local node id, the error is: %d\n", result); } else { printf ("Local node ID %d\n", nodeid); } result = corosync_cfg_ring_status_get (handle, &interface_names, &interface_status, &interface_count); if (result != CS_OK) { printf ("Could not get the ring status, the error is: %d\n", result); } else { for (i = 0; i < interface_count; i++) { printf ("RING ID %d\n", i); printf ("\tid\t= %s\n", interface_names[i]); printf ("\tstatus\t= %s\n", interface_status[i]); } } (void)corosync_cfg_finalize (handle); }
gboolean cluster_connect_cfg(uint32_t * nodeid) { cs_error_t rc; int fd = 0, retries = 0; cs_repeat(retries, 30, rc = corosync_cfg_initialize(&cfg_handle, &cfg_callbacks)); if (rc != CS_OK) { crm_err("corosync cfg init error %d", rc); return FALSE; } rc = corosync_cfg_fd_get(cfg_handle, &fd); if (rc != CS_OK) { crm_err("corosync cfg fd_get error %d", rc); goto bail; } retries = 0; cs_repeat(retries, 30, rc = corosync_cfg_local_get(cfg_handle, nodeid)); if (rc != CS_OK) { crm_err("corosync cfg local_get error %d", rc); goto bail; } crm_debug("Our nodeid: %d", *nodeid); crm_debug("Adding fd=%d to mainloop", fd); G_main_add_fd(G_PRIORITY_HIGH, fd, FALSE, pcmk_cfg_dispatch, &cfg_handle, cfg_connection_destroy); return TRUE; bail: corosync_cfg_finalize(cfg_handle); return FALSE; }
static void shutdown_do(void) { cs_error_t result; corosync_cfg_handle_t handle; corosync_cfg_callbacks_t callbacks; callbacks.corosync_cfg_shutdown_callback = NULL; result = corosync_cfg_initialize (&handle, &callbacks); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } printf ("Shutting down corosync\n"); result = corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST); if (result != CS_OK) { printf ("Could not shutdown (error = %d)\n", result); } (void)corosync_cfg_finalize (handle); }
void close_cluster_cfg(void) { corosync_cfg_finalize(ch); }
static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format) { quorum_handle_t q_handle = 0; votequorum_handle_t v_handle = 0; corosync_cfg_handle_t c_handle = 0; corosync_cfg_callbacks_t c_callbacks; int i; int using_vq = 0; quorum_callbacks_t q_callbacks; votequorum_callbacks_t v_callbacks; int err; int result = EXIT_FAILURE; q_callbacks.quorum_notify_fn = quorum_notification_fn; err=quorum_initialize(&q_handle, &q_callbacks); if (err != CS_OK) { fprintf(stderr, "Cannot connect to quorum service, is it loaded?\n"); return result; } v_callbacks.votequorum_notify_fn = NULL; v_callbacks.votequorum_expectedvotes_notify_fn = NULL; using_vq = using_votequorum(); if (using_vq) { if ( (err=votequorum_initialize(&v_handle, &v_callbacks)) != CS_OK) { fprintf(stderr, "votequorum_initialize FAILED: %d, this is probably a configuration error\n", err); v_handle = 0; goto err_exit; } } err = quorum_trackstart(q_handle, CS_TRACK_CURRENT); if (err != CS_OK) { fprintf(stderr, "quorum_trackstart FAILED: %d\n", err); goto err_exit; } g_called = 0; while (g_called == 0) quorum_dispatch(q_handle, CS_DISPATCH_ONE); quorum_finalize(q_handle); q_handle = 0; err = corosync_cfg_initialize(&c_handle, &c_callbacks); if (err != CS_OK) { fprintf(stderr, "Cannot initialise CFG service\n"); c_handle = 0; goto err_exit; } if (using_vq) printf("Nodeid Votes Name\n"); else printf("Nodeid Name\n"); for (i=0; i < g_view_list_entries; i++) { if (nodeid_format == NODEID_FORMAT_DECIMAL) { printf("%4u ", g_view_list[i]); } else { printf("0x%04x ", g_view_list[i]); } if (using_vq) { printf("%3d %s\n", get_votes(v_handle, g_view_list[i]), node_name(c_handle, g_view_list[i], name_format)); } else { printf("%s\n", node_name(c_handle, g_view_list[i], name_format)); } } result = EXIT_SUCCESS; err_exit: if (q_handle != 0) { quorum_finalize (q_handle); } if (using_vq && v_handle != 0) { votequorum_finalize (v_handle); } if (c_handle != 0) { corosync_cfg_finalize (c_handle); } return result; }
int main (int argc, char *argv[]) { const char *options = "hd:ne"; int opt; const char *prog_name = basename(argv[0]); char delimiter = 0; int escape = 1; operation_t operation = OPER_FULL_OUTPUT; int result; while ( (opt = getopt(argc, argv, options)) != -1 ) { switch (opt) { case 'd': if (strlen (optarg) > 0) { delimiter = optarg[0]; } break; case 'n': operation = OPER_NAMES_ONLY; break; case 'e': escape = 0; break; case 'h': usage_do (prog_name); return (EXIT_SUCCESS); break; case '?': case ':': return (EXIT_FAILURE); break; } } result = cpg_initialize (&cpg_handle, NULL); if (result != CS_OK) { fprintf (stderr, "Could not initialize corosync cpg API error %d\n", result); return (EXIT_FAILURE); } result = corosync_cfg_initialize (&cfg_handle, NULL); if (result != CS_OK) { fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result); return (EXIT_FAILURE); } switch (operation) { case OPER_NAMES_ONLY: result = display_groups (delimiter, escape); break; case OPER_FULL_OUTPUT: result = display_groups_with_members (delimiter, escape); break; } cpg_finalize (cpg_handle); corosync_cfg_finalize (cfg_handle); return (result ? EXIT_SUCCESS : EXIT_FAILURE); }
static int linkstatusget_do (char *interface_name, int brief) { cs_error_t result; corosync_cfg_handle_t handle; unsigned int interface_count; char **interface_names; char **interface_status; unsigned int i; unsigned int nodeid; int rc = 0; int len, s = 0, t; printf ("Printing link status.\n"); result = corosync_cfg_initialize (&handle, NULL); if (result != CS_OK) { printf ("Could not initialize corosync configuration API error %d\n", result); exit (1); } result = corosync_cfg_local_get(handle, &nodeid); if (result != CS_OK) { printf ("Could not get the local node id, the error is: %d\n", result); } else { printf ("Local node ID %u\n", nodeid); } result = corosync_cfg_ring_status_get (handle, &interface_names, &interface_status, &interface_count); if (result != CS_OK) { printf ("Could not get the link status, the error is: %d\n", result); } else { for (i = 0; i < interface_count; i++) { s = 0; if ( (interface_name && interface_names[i][0] != '\0' && (interface_name[0]=='\0' || strcasecmp (interface_name, interface_names[i]) == 0)) || !interface_name ) { /* * Interface_name is "<linkid> <IP address>" * separate them out */ char *space = strchr(interface_names[i], ' '); if (!space) { continue; } *space = '\0'; printf ("LINK ID %s\n", interface_names[i]); printf ("\taddr\t= %s\n", space+1); if((!brief) && (strcmp(interface_status[i], "OK") != 0) && (!strstr(interface_status[i], "FAULTY"))) { len = strlen(interface_status[i]); printf ("\tstatus:\n"); while(s < len) { t = interface_status[i][s] - '0'; printf("\t\tnode %d:\t", s++); printf("link enabled:%d\t", t&1? 1 : 0); printf("link connected:%d\n", t&2? 1: 0); } } else { printf ("\tstatus\t= %s\n", interface_status[i]); if (strstr(interface_status[i], "FAULTY")) { rc = 1; } } } } for (i = 0; i < interface_count; i++) { free(interface_status[i]); free(interface_names[i]); } free(interface_status); free(interface_names); } (void)corosync_cfg_finalize (handle); return rc; }