diff --git a/fencing/admin.c b/fencing/admin.c index c8671f3e78..c580e60aa1 100644 --- a/fencing/admin.c +++ b/fencing/admin.c @@ -1,384 +1,379 @@ /* * Copyright (C) 2009 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* *INDENT-OFF* */ static struct crm_option long_options[] = { {"help", 0, 0, '?', "\tThis text"}, {"version", 0, 0, '$', "\tVersion information" }, {"verbose", 0, 0, 'V', "\tIncrease debug output"}, {"quiet", 0, 0, 'q', "\tPrint only essential output"}, {"-spacer-", 0, 0, '-', "Commands:"}, {"list", 1, 0, 'l', "List devices that can terminate the specified host"}, {"list-registered", 0, 0, 'L', "List all registered devices"}, {"list-installed", 0, 0, 'I', "List all installed devices"}, {"-spacer-", 0, 0, '-', ""}, {"metadata", 0, 0, 'M', "Check the device's metadata"}, {"query", 1, 0, 'Q', "Check the device's status"}, {"fence", 1, 0, 'F', "Fence the named host"}, {"unfence", 1, 0, 'U', "Unfence the named host"}, {"reboot", 1, 0, 'B', "Reboot the named host"}, {"confirm", 1, 0, 'C', "Confirm the named host is now safely down"}, {"history", 1, 0, 'H', "Retrieve last fencing operation"}, {"-spacer-", 0, 0, '-', ""}, {"register", 1, 0, 'R', "Register the named stonith device. Requires: --agent, optional: --option"}, {"deregister", 1, 0, 'D', "De-register the named stonith device"}, {"register-level", 1, 0, 'r', "Register a stonith level for the named host. Requires: --index, one or more --device entries"}, {"deregister-level", 1, 0, 'd', "De-register a stonith level for the named host. Requires: --index"}, - + {"-spacer-", 0, 0, '-', ""}, {"-spacer-", 0, 0, '-', "Options and modifiers:"}, {"agent", 1, 0, 'a', "The agent (eg. fence_xvm) to instantiate when calling with --register"}, {"env-option", 1, 0, 'e'}, {"option", 1, 0, 'o'}, {"device", 1, 0, 'v', "A device to associate with a given host and stonith level"}, {"index", 1, 0, 'i', "The stonith level (1-9)"}, {"timeout", 1, 0, 't', "Operation timeout in seconds"}, {"list-all", 0, 0, 'L', "legacy alias for --list-registered"}, {0, 0, 0, 0} }; /* *INDENT-ON* */ int st_opts = st_opt_sync_call; int main(int argc, char ** argv) { int flag; int rc = 0; int quiet = 0; int verbose = 0; int argerr = 0; int timeout = 120; int option_index = 0; int fence_level = 0; char name[512]; char value[512]; const char *agent = NULL; const char *device = NULL; const char *target = NULL; - + char action = 0; stonith_t *st = NULL; stonith_key_value_t *params = NULL; stonith_key_value_t *devices = NULL; stonith_key_value_t *dIter = NULL; - + crm_log_init(NULL, LOG_NOTICE, FALSE, FALSE, argc, argv, FALSE); crm_set_options(NULL, "mode [options]", long_options, - "Provides access to the stonith-ng API.\n" - "\nAllows the administrator to add/remove/list devices, check device and host status and fence hosts\n"); + "Provides access to the stonith-ng API.\n" + "\nAllows the administrator to add/remove/list devices, check device and host status and fence hosts\n"); while (1) { - flag = crm_get_option(argc, argv, &option_index); - if (flag == -1) - break; - - switch(flag) { - case 'V': - verbose = 1; - crm_bump_log_level(); - break; - case '$': - case '?': - crm_help(flag, EX_OK); - break; - case 'L': - case 'I': - action = flag; - break; - case 'q': - quiet = 1; - break; - case 'Q': - case 'R': - case 'D': - action = flag; - device = optarg; - break; - case 'a': - agent = optarg; - break; - case 'l': - target = optarg; - action = 'L'; - break; - case 'M': - action = flag; - break; - case 't': - timeout = crm_atoi(optarg, NULL); - break; - case 'B': - case 'F': - case 'U': - case 'C': - case 'H': - case 'r': - case 'd': - target = optarg; - action = flag; - break; - case 'i': - fence_level = crm_atoi(optarg, NULL); - break; - case 'v': - devices = stonith_key_value_add(devices, NULL, optarg); - break; - case 'o': - crm_info("Scanning: -o %s", optarg); - rc = sscanf(optarg, "%[^=]=%[^=]", name, value); - if(rc != 2) { - crm_err("Invalid option: -o %s", optarg); - ++argerr; - } else { - crm_info("Got: '%s'='%s'", name, value); - stonith_key_value_add(params, name, value); - } - break; - case 'e': - { - char *key = crm_concat("OCF_RESKEY", optarg, '_'); - const char *env = getenv(key); - - if(env == NULL) { - crm_err("Invalid option: -e %s", optarg); - ++argerr; - } else { - crm_info("Got: '%s'='%s'", optarg, env); - stonith_key_value_add( params, optarg, env); - } - } - break; - default: - ++argerr; - break; - } + flag = crm_get_option(argc, argv, &option_index); + if (flag == -1) + break; + + switch(flag) { + case 'V': + verbose = 1; + crm_bump_log_level(); + break; + case '$': + case '?': + crm_help(flag, EX_OK); + break; + case 'L': + case 'I': + action = flag; + break; + case 'q': + quiet = 1; + break; + case 'Q': + case 'R': + case 'D': + action = flag; + device = optarg; + break; + case 'a': + agent = optarg; + break; + case 'l': + target = optarg; + action = 'L'; + break; + case 'M': + action = flag; + break; + case 't': + timeout = crm_atoi(optarg, NULL); + break; + case 'B': + case 'F': + case 'U': + case 'C': + case 'H': + case 'r': + case 'd': + target = optarg; + action = flag; + break; + case 'i': + fence_level = crm_atoi(optarg, NULL); + break; + case 'v': + devices = stonith_key_value_add(devices, NULL, optarg); + break; + case 'o': + crm_info("Scanning: -o %s", optarg); + rc = sscanf(optarg, "%[^=]=%[^=]", name, value); + if(rc != 2) { + crm_err("Invalid option: -o %s", optarg); + ++argerr; + } else { + crm_info("Got: '%s'='%s'", name, value); + params = stonith_key_value_add(params, name, value); + } + break; + case 'e': + { + char *key = crm_concat("OCF_RESKEY", optarg, '_'); + const char *env = getenv(key); + + if(env == NULL) { + crm_err("Invalid option: -e %s", optarg); + ++argerr; + } else { + crm_info("Got: '%s'='%s'", optarg, env); + params = stonith_key_value_add( params, optarg, env); + } + } + break; + default: + ++argerr; + break; + } } if (optind > argc) { - ++argerr; + ++argerr; } - + if (argerr) { - crm_help('?', EX_USAGE); + crm_help('?', EX_USAGE); } crm_debug("Create"); st = stonith_api_new(); if(action != 'M' && action != 'I') { - rc = st->cmds->connect(st, crm_system_name, NULL); - crm_debug("Connect: %d", rc); + rc = st->cmds->connect(st, crm_system_name, NULL); + crm_debug("Connect: %d", rc); - if(rc < 0) { - goto done; - } + if(rc < 0) { + goto done; + } } - - switch(action) - { - case 'I': - rc = st->cmds->list(st, st_opt_sync_call, NULL, &devices, timeout); - for(dIter = devices; dIter; dIter = dIter->next ) { - fprintf( stdout, " %s\n", dIter->value ); - } - if(rc == 0) { - fprintf(stderr, "No devices found\n"); - - } else if(rc > 0) { - fprintf(stderr, "%d devices found\n", rc); - rc = 0; - } - stonith_key_value_freeall(devices, 1, 1); - break; - - case 'L': - rc = st->cmds->query(st, st_opts, target, &devices, timeout); - for(dIter = devices; dIter; dIter = dIter->next ) { - fprintf( stdout, " %s\n", dIter->value ); - } - if(rc == 0) { - fprintf(stderr, "No devices found\n"); - - } else if(rc > 0) { - fprintf(stderr, "%d devices found\n", rc); - rc = 0; - } - stonith_key_value_freeall(devices, 1, 1); - break; - case 'Q': - rc = st->cmds->call(st, st_opts, device, "monitor", NULL, timeout); - if(rc < 0) { - rc = st->cmds->call(st, st_opts, device, "list", NULL, timeout); - } - break; - case 'R': - rc = st->cmds->register_device(st, st_opts, device, "stonith-ng", + + switch(action) { + case 'I': + rc = st->cmds->list(st, st_opt_sync_call, NULL, &devices, timeout); + for(dIter = devices; dIter; dIter = dIter->next ) { + fprintf( stdout, " %s\n", dIter->value ); + } + if(rc == 0) { + fprintf(stderr, "No devices found\n"); + + } else if(rc > 0) { + fprintf(stderr, "%d devices found\n", rc); + rc = 0; + } + stonith_key_value_freeall(devices, 1, 1); + break; + case 'L': + rc = st->cmds->query(st, st_opts, target, &devices, timeout); + for(dIter = devices; dIter; dIter = dIter->next ) { + fprintf( stdout, " %s\n", dIter->value ); + } + if(rc == 0) { + fprintf(stderr, "No devices found\n"); + } else if(rc > 0) { + fprintf(stderr, "%d devices found\n", rc); + rc = 0; + } + stonith_key_value_freeall(devices, 1, 1); + break; + case 'Q': + rc = st->cmds->call(st, st_opts, device, "monitor", NULL, timeout); + if(rc < 0) { + rc = st->cmds->call(st, st_opts, device, "list", NULL, timeout); + } + break; + case 'R': + rc = st->cmds->register_device(st, st_opts, device, "stonith-ng", agent, params); - break; - case 'D': - rc = st->cmds->remove_device(st, st_opts, device); - break; - case 'r': - rc = st->cmds->register_level(st, st_opts, target, fence_level, devices); - break; - case 'd': - rc = st->cmds->remove_level(st, st_opts, target, fence_level); - break; - case 'M': - if (agent == NULL) { - printf("Please specify an agent to query using -a,--agent [value]\n"); - return -1; - - } else { - char *buffer = NULL; - rc = st->cmds->metadata(st, st_opt_sync_call, agent, NULL, &buffer, timeout); - if(rc == pcmk_ok) { - printf("%s\n", buffer); + break; + case 'D': + rc = st->cmds->remove_device(st, st_opts, device); + break; + case 'r': + rc = st->cmds->register_level(st, st_opts, target, fence_level, devices); + break; + case 'd': + rc = st->cmds->remove_level(st, st_opts, target, fence_level); + break; + case 'M': + if (agent == NULL) { + printf("Please specify an agent to query using -a,--agent [value]\n"); + return -1; + } else { + char *buffer = NULL; + rc = st->cmds->metadata(st, st_opt_sync_call, agent, NULL, &buffer, timeout); + if(rc == pcmk_ok) { + printf("%s\n", buffer); + } + free(buffer); + } + break; + case 'C': + rc = st->cmds->confirm(st, st_opts, target); + break; + case 'B': + rc = st->cmds->fence(st, st_opts, target, "reboot", timeout); + break; + case 'F': + rc = st->cmds->fence(st, st_opts, target, "off", timeout); + break; + case 'U': + rc = st->cmds->fence(st, st_opts, target, "on", timeout); + break; + case 'H': + { + stonith_history_t *history, *hp, *latest = NULL; + rc = st->cmds->history(st, st_opts, target, &history, timeout); + for(hp = history; hp; hp = hp->next) { + char *action_s = NULL; + time_t complete = hp->completed; + + if(hp->state == st_done) { + latest = hp; + } + + if(quiet || !verbose) { + continue; + } else if(hp->action == NULL) { + action_s = strdup("unknown"); + } else if(hp->action[0] != 'r') { + action_s = crm_concat("turn", hp->action, ' '); + } else { + action_s = strdup(hp->action); + } + + if(hp->state == st_failed) { + printf("%s failed to %s node %s on behalf of %s at %s\n", + hp->delegate?hp->delegate:"We", action_s, hp->target, hp->origin, + ctime(&complete)); + + } else if(hp->state == st_done && hp->delegate) { + printf("%s was able to %s node %s on behalf of %s at %s\n", + hp->delegate, action_s, hp->target, hp->origin, ctime(&complete)); + + } else if(hp->state == st_done) { + printf("We were able to %s node %s on behalf of %s at %s\n", + action_s, hp->target, hp->origin, ctime(&complete)); + } else { + printf("%s wishes to %s node %s - %d %d\n", + hp->origin, action_s, hp->target, hp->state, hp->completed); + } + + free(action_s); + } + + if(latest) { + if(quiet) { + printf("%d\n", latest->completed); + } else { + char *action_s = NULL; + time_t complete = latest->completed; + if(latest->action == NULL) { + action_s = strdup("unknown"); + } else if(latest->action[0] != 'r') { + action_s = crm_concat("turn", latest->action, ' '); + } else { + action_s = strdup(latest->action); } - free(buffer); - } - break; - - case 'C': - rc = st->cmds->confirm(st, st_opts, target); - break; - case 'B': - rc = st->cmds->fence(st, st_opts, target, "reboot", timeout); - break; - case 'F': - rc = st->cmds->fence(st, st_opts, target, "off", timeout); - break; - case 'U': - rc = st->cmds->fence(st, st_opts, target, "on", timeout); - break; - case 'H': - { - stonith_history_t *history, *hp, *latest = NULL; - rc = st->cmds->history(st, st_opts, target, &history, timeout); - for(hp = history; hp; hp = hp->next) { - char *action_s = NULL; - time_t complete = hp->completed; - - if(hp->state == st_done) { - latest = hp; - } - - if(quiet || !verbose) { - continue; - } else if(hp->action == NULL) { - action_s = strdup("unknown"); - } else if(hp->action[0] != 'r') { - action_s = crm_concat("turn", hp->action, ' '); - } else { - action_s = strdup(hp->action); - } - - if(hp->state == st_failed) { - printf("%s failed to %s node %s on behalf of %s at %s\n", - hp->delegate?hp->delegate:"We", action_s, hp->target, hp->origin, - ctime(&complete)); - - } else if(hp->state == st_done && hp->delegate) { - printf("%s was able to %s node %s on behalf of %s at %s\n", - hp->delegate, action_s, hp->target, hp->origin, ctime(&complete)); - - } else if(hp->state == st_done) { - printf("We were able to %s node %s on behalf of %s at %s\n", - action_s, hp->target, hp->origin, ctime(&complete)); - } else { - printf("%s wishes to %s node %s - %d %d\n", - hp->origin, action_s, hp->target, hp->state, hp->completed); - } - - free(action_s); - } - - if(latest) { - if(quiet) { - printf("%d\n", latest->completed); - } else { - char *action_s = NULL; - time_t complete = latest->completed; - if(latest->action == NULL) { - action_s = strdup("unknown"); - } else if(latest->action[0] != 'r') { - action_s = crm_concat("turn", latest->action, ' '); - } else { - action_s = strdup(latest->action); - } - - printf("%s was able to %s node %s on behalf of %s at %s\n", - latest->delegate?latest->delegate:"We", action_s, latest->target, - latest->origin, ctime(&complete)); - - free(action_s); - } - } - } - break; - } - - done: + + printf("%s was able to %s node %s on behalf of %s at %s\n", + latest->delegate?latest->delegate:"We", action_s, latest->target, + latest->origin, ctime(&complete)); + + free(action_s); + } + } + break; + } /* closing bracket for -H case */ + } /* closing bracket for switch case */ + +done: if(rc < 0) { - printf("Command failed: %s\n", pcmk_strerror(rc)); + printf("Command failed: %s\n", pcmk_strerror(rc)); } - + stonith_key_value_freeall(params, 1, 1); st->cmds->disconnect(st); crm_debug("Disconnect: %d", rc); crm_debug("Destroy"); stonith_api_delete(st); - + return rc; } diff --git a/fencing/commands.c b/fencing/commands.c index f8672cc6c3..1a7d56e342 100644 --- a/fencing/commands.c +++ b/fencing/commands.c @@ -1,1312 +1,1310 @@ /* * Copyright (C) 2009 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include GHashTable *device_list = NULL; GHashTable *topology = NULL; static int active_children = 0; static gboolean stonith_device_dispatch(gpointer user_data); static void st_child_done(GPid pid, gint status, gpointer user_data); - -static void free_async_command(async_command_t *cmd) +static void free_async_command(async_command_t *cmd) { g_list_free(cmd->device_list); free(cmd->device); free(cmd->action); free(cmd->victim); free(cmd->remote); free(cmd->client); free(cmd->origin); free(cmd->op); - free(cmd); + free(cmd); } -static async_command_t *create_async_command(xmlNode *msg) +static async_command_t *create_async_command(xmlNode *msg) { async_command_t *cmd = NULL; xmlNode *op = get_xpath_object("//@"F_STONITH_ACTION, msg, LOG_ERR); const char *action = crm_element_value(op, F_STONITH_ACTION); CRM_CHECK(action != NULL, crm_log_xml_warn(msg, "NoAction"); return NULL); crm_log_xml_trace(msg, "Command"); cmd = calloc(1, sizeof(async_command_t)); crm_element_value_int(msg, F_STONITH_CALLID, &(cmd->id)); crm_element_value_int(msg, F_STONITH_CALLOPTS, &(cmd->options)); crm_element_value_int(msg, F_STONITH_TIMEOUT, &(cmd->timeout)); cmd->origin = crm_element_value_copy(msg, F_ORIG); cmd->remote = crm_element_value_copy(msg, F_STONITH_REMOTE); cmd->client = crm_element_value_copy(msg, F_STONITH_CLIENTID); cmd->op = crm_element_value_copy(msg, F_STONITH_OPERATION); cmd->action = strdup(action); cmd->victim = crm_element_value_copy(op, F_STONITH_TARGET); cmd->mode = crm_element_value_copy(op, F_STONITH_MODE); cmd->device = crm_element_value_copy(op, F_STONITH_DEVICE); cmd->done = st_child_done; CRM_CHECK(cmd->op != NULL, crm_log_xml_warn(msg, "NoOp"); free_async_command(cmd); return NULL); CRM_CHECK(cmd->client != NULL, crm_log_xml_warn(msg, "NoClient")); return cmd; } -static int stonith_manual_ack(xmlNode *msg, remote_fencing_op_t *op) +static int stonith_manual_ack(xmlNode *msg, remote_fencing_op_t *op) { async_command_t *cmd = create_async_command(msg); xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, msg, LOG_ERR); - + if(cmd == NULL) { - return -EINVAL; + return -EINVAL; } cmd->device = strdup("manual_ack"); cmd->remote = strdup(op->id); - + crm_notice("Injecting manual confirmation that %s is safely off/down", crm_element_value(dev, F_STONITH_TARGET)); st_child_done(0, 0, cmd); return pcmk_ok; } static gboolean stonith_device_execute(stonith_device_t *device) { int rc = 0; int exec_rc = 0; async_command_t *cmd = NULL; CRM_CHECK(device != NULL, return FALSE); - if(device->active_pid) { - crm_trace("%s is still active with pid %u", device->id, device->active_pid); - return TRUE; + if(device->active_pid) { + crm_trace("%s is still active with pid %u", device->id, device->active_pid); + return TRUE; } - + if(device->pending_ops) { - GList *first = device->pending_ops; - device->pending_ops = g_list_remove_link(device->pending_ops, first); - cmd = first->data; - g_list_free_1(first); + GList *first = device->pending_ops; + device->pending_ops = g_list_remove_link(device->pending_ops, first); + cmd = first->data; + g_list_free_1(first); } if(cmd == NULL) { - crm_trace("Nothing further to do for %s", device->id); - return TRUE; + crm_trace("Nothing further to do for %s", device->id); + return TRUE; } - + cmd->device = strdup(device->id); exec_rc = run_stonith_agent(device->agent, cmd->action, cmd->victim, - device->params, device->aliases, &rc, NULL, cmd); + device->params, device->aliases, &rc, NULL, cmd); if(exec_rc > 0) { - crm_debug("Operation %s%s%s on %s now running with pid=%d, timeout=%dms", - cmd->action, cmd->victim?" for node ":"", cmd->victim?cmd->victim:"", - device->id, exec_rc, cmd->timeout); - device->active_pid = exec_rc; - + crm_debug("Operation %s%s%s on %s now running with pid=%d, timeout=%dms", + cmd->action, cmd->victim?" for node ":"", cmd->victim?cmd->victim:"", + device->id, exec_rc, cmd->timeout); + device->active_pid = exec_rc; + } else { - crm_warn("Operation %s%s%s on %s failed (%d/%d)", - cmd->action, cmd->victim?" for node ":"", cmd->victim?cmd->victim:"", - device->id, exec_rc, rc); - st_child_done(0, rc<0?rc:exec_rc, cmd); + crm_warn("Operation %s%s%s on %s failed (%d/%d)", + cmd->action, cmd->victim?" for node ":"", cmd->victim?cmd->victim:"", + device->id, exec_rc, rc); + st_child_done(0, rc<0?rc:exec_rc, cmd); } return TRUE; } static gboolean stonith_device_dispatch(gpointer user_data) { return stonith_device_execute(user_data); } static void schedule_stonith_command(async_command_t *cmd, stonith_device_t *device) { CRM_CHECK(cmd != NULL, return); CRM_CHECK(device != NULL, return); crm_debug("Scheduling %s on %s for %s (timeout=%dms)", cmd->action, device->id, cmd->remote?cmd->remote:cmd->client, cmd->timeout); device->pending_ops = g_list_append(device->pending_ops, cmd); mainloop_set_trigger(device->work); } void free_device(gpointer data) { GListPtr gIter = NULL; stonith_device_t *device = data; g_hash_table_destroy(device->params); g_hash_table_destroy(device->aliases); - + for(gIter = device->pending_ops; gIter != NULL; gIter = gIter->next) { - async_command_t *cmd = gIter->data; - - crm_warn("Removal of device '%s' purged operation %s", device->id, cmd->action); - st_child_done(0, -ENODEV, cmd); - free_async_command(cmd); + async_command_t *cmd = gIter->data; + + crm_warn("Removal of device '%s' purged operation %s", device->id, cmd->action); + st_child_done(0, -ENODEV, cmd); + free_async_command(cmd); } g_list_free(device->pending_ops); g_list_free_full(device->targets, free); free(device->namespace); free(device->agent); free(device->id); free(device); } static GHashTable *build_port_aliases(const char *hostmap, GListPtr *targets) { char *name = NULL; int last = 0, lpc = 0, max = 0, added = 0; GHashTable *aliases = g_hash_table_new_full(crm_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str); - + if(hostmap == NULL) { - return aliases; + return aliases; } - + max = strlen(hostmap); for(; lpc <= max; lpc++) { - switch(hostmap[lpc]) { - /* Assignment chars */ - case '=': - case ':': - if(lpc > last) { - free(name); - name = calloc(1, 1 + lpc - last); - memcpy(name, hostmap + last, lpc - last); - } - last = lpc + 1; - break; - - /* Delimeter chars */ - /* case ',': Potentially used to specify multiple ports */ - case 0: - case ';': - case ' ': - case '\t': - if(name) { - char *value = NULL; - value = calloc(1, 1 + lpc - last); - memcpy(value, hostmap + last, lpc - last); - - crm_debug("Adding alias '%s'='%s'", name, value); - g_hash_table_replace(aliases, name, value); - if(targets) { - *targets = g_list_append(*targets, strdup(value)); - } - value=NULL; - name=NULL; - added++; - - } else if(lpc > last) { - crm_debug("Parse error at offset %d near '%s'", lpc-last, hostmap+last); - } - - last = lpc + 1; - break; - } - - if(hostmap[lpc] == 0) { - break; - } + switch(hostmap[lpc]) { + /* Assignment chars */ + case '=': + case ':': + if(lpc > last) { + free(name); + name = calloc(1, 1 + lpc - last); + memcpy(name, hostmap + last, lpc - last); + } + last = lpc + 1; + break; + + /* Delimeter chars */ + /* case ',': Potentially used to specify multiple ports */ + case 0: + case ';': + case ' ': + case '\t': + if(name) { + char *value = NULL; + value = calloc(1, 1 + lpc - last); + memcpy(value, hostmap + last, lpc - last); + + crm_debug("Adding alias '%s'='%s'", name, value); + g_hash_table_replace(aliases, name, value); + if(targets) { + *targets = g_list_append(*targets, strdup(value)); + } + value=NULL; + name=NULL; + added++; + + } else if(lpc > last) { + crm_debug("Parse error at offset %d near '%s'", lpc-last, hostmap+last); + } + + last = lpc + 1; + break; + } + + if(hostmap[lpc] == 0) { + break; + } } if(added == 0) { - crm_info("No host mappings detected in '%s'", hostmap); + crm_info("No host mappings detected in '%s'", hostmap); } - + free(name); return aliases; } static void parse_host_line(const char *line, GListPtr *output) { int lpc = 0; int max = 0; int last = 0; if(line) { - max = strlen(line); + max = strlen(line); } else { - return; + return; } /* Check for any complaints about additional parameters that the device doesn't understand */ if(strstr(line, "invalid") || strstr(line, "variable")) { - crm_debug("Skipping: %s", line); - return; + crm_debug("Skipping: %s", line); + return; } - + crm_trace("Processing: %s", line); /* Skip initial whitespace */ for(lpc = 0; lpc <= max && isspace(line[lpc]); lpc++) { - last = lpc+1; + last = lpc+1; } /* Now the actual content */ for(lpc = 0; lpc <= max; lpc++) { - gboolean a_space = isspace(line[lpc]); - if(a_space && lpc < max && isspace(line[lpc+1])) { - /* fast-forward to the end of the spaces */ - - } else if(a_space || line[lpc] == ',' || line[lpc] == 0) { - int rc = 1; - char *entry = NULL; - + gboolean a_space = isspace(line[lpc]); + if(a_space && lpc < max && isspace(line[lpc+1])) { + /* fast-forward to the end of the spaces */ + + } else if(a_space || line[lpc] == ',' || line[lpc] == 0) { + int rc = 1; + char *entry = NULL; + if(lpc != last) { entry = calloc(1, 1 + lpc - last); rc = sscanf(line+last, "%[a-zA-Z0-9_-.]", entry); - } + } if(entry == NULL) { /* Skip */ } else if(rc != 1) { crm_warn("Could not parse (%d %d): %s", last, lpc, line+last); - - } else if(safe_str_neq(entry, "on") && safe_str_neq(entry, "off")) { - crm_trace("Adding '%s'", entry); - *output = g_list_append(*output, entry); - entry = NULL; - } - - free(entry); - last = lpc + 1; - } + } else if(safe_str_neq(entry, "on") && safe_str_neq(entry, "off")) { + crm_trace("Adding '%s'", entry); + *output = g_list_append(*output, entry); + entry = NULL; + } + + free(entry); + last = lpc + 1; + } } } static GListPtr parse_host_list(const char *hosts) { int lpc = 0; int max = 0; int last = 0; GListPtr output = NULL; if(hosts == NULL) { - return output; + return output; } - + max = strlen(hosts); for(lpc = 0; lpc <= max; lpc++) { - if(hosts[lpc] == '\n' || hosts[lpc] == 0) { - char *line = NULL; + if(hosts[lpc] == '\n' || hosts[lpc] == 0) { + char *line = NULL; - line = calloc(1, 2 + lpc - last); - snprintf(line, 1 + lpc - last, "%s", hosts+last); - parse_host_line(line, &output); - free(line); + line = calloc(1, 2 + lpc - last); + snprintf(line, 1 + lpc - last, "%s", hosts+last); + parse_host_line(line, &output); + free(line); - last = lpc + 1; - } + last = lpc + 1; + } } - + return output; } static stonith_device_t *build_device_from_xml(xmlNode *msg) { xmlNode *dev = get_xpath_object("//"F_STONITH_DEVICE, msg, LOG_ERR); stonith_device_t *device = NULL; device = calloc(1, sizeof(stonith_device_t)); device->id = crm_element_value_copy(dev, XML_ATTR_ID); device->agent = crm_element_value_copy(dev, "agent"); device->namespace = crm_element_value_copy(dev, "namespace"); device->params = xml2list(dev); device->work = mainloop_add_trigger(G_PRIORITY_HIGH, stonith_device_dispatch, device); /* TODO: Hook up priority */ - + return device; } int stonith_device_register(xmlNode *msg, const char **desc) { const char *value = NULL; stonith_device_t *device = build_device_from_xml(msg); value = g_hash_table_lookup(device->params, STONITH_ATTR_HOSTLIST); if(value) { - device->targets = parse_host_list(value); + device->targets = parse_host_list(value); } - + value = g_hash_table_lookup(device->params, STONITH_ATTR_HOSTMAP); device->aliases = build_port_aliases(value, &(device->targets)); g_hash_table_replace(device_list, device->id, device); crm_notice("Added '%s' to the device list (%d active devices)", device->id, g_hash_table_size(device_list)); if(desc) { *desc = device->id; } return pcmk_ok; } static int stonith_device_remove(xmlNode *msg, const char **desc) { xmlNode *dev = get_xpath_object("//"F_STONITH_DEVICE, msg, LOG_ERR); const char *id = crm_element_value(dev, XML_ATTR_ID); if(g_hash_table_remove(device_list, id)) { - crm_info("Removed '%s' from the device list (%d active devices)", - id, g_hash_table_size(device_list)); + crm_info("Removed '%s' from the device list (%d active devices)", + id, g_hash_table_size(device_list)); } else { - crm_info("Device '%s' not found (%d active devices)", - id, g_hash_table_size(device_list)); + crm_info("Device '%s' not found (%d active devices)", + id, g_hash_table_size(device_list)); } if(desc) { *desc = id; } return pcmk_ok; } static int count_active_levels(stonith_topology_t *tp) { int lpc = 0; int count = 0; for(lpc = 0; lpc < ST_LEVEL_MAX; lpc++) { if(tp->levels[lpc] != NULL) { count++; } } return count; } void free_topology_entry(gpointer data) { stonith_topology_t *tp = data; int lpc = 0; for(lpc = 0; lpc < ST_LEVEL_MAX; lpc++) { if(tp->levels[lpc] != NULL) { g_list_free_full(tp->levels[lpc], free); } } free(tp->node); free(tp); } int stonith_level_register(xmlNode *msg, char **desc) { int id = 0; int rc = pcmk_ok; xmlNode *child = NULL; - + xmlNode *level = get_xpath_object("//"F_STONITH_LEVEL, msg, LOG_ERR); const char *node = crm_element_value(level, F_STONITH_TARGET); stonith_topology_t *tp = g_hash_table_lookup(topology, node); - + crm_element_value_int(level, XML_ATTR_ID, &id); if(desc) { *desc = g_strdup_printf("%s[%d]", node, id); } if(id <= 0 || id >= ST_LEVEL_MAX) { return -EINVAL; } - + if(tp == NULL) { tp = calloc(1, sizeof(stonith_topology_t)); tp->node = strdup(node); g_hash_table_replace(topology, tp->node, tp); crm_trace("Added %s to the topology (%d active entries)", node, g_hash_table_size(topology)); } if(tp->levels[id] != NULL) { crm_info("Adding to the existing %s[%d] topology entry (%d active entries)", node, id, count_active_levels(tp)); } for (child = __xml_first_child(level); child != NULL; child = __xml_next(child)) { const char *device = ID(child); crm_trace("Adding device '%s' for %s (%d)", device, node, id); tp->levels[id] = g_list_append(tp->levels[id], strdup(device)); } - + crm_info("Node %s has %d active fencing levels", node, count_active_levels(tp)); return rc; } int stonith_level_remove(xmlNode *msg, char **desc) { int id = 0; xmlNode *level = get_xpath_object("//"F_STONITH_LEVEL, msg, LOG_ERR); const char *node = crm_element_value(level, F_STONITH_TARGET); stonith_topology_t *tp = g_hash_table_lookup(topology, node); if(desc) { *desc = g_strdup_printf("%s[%d]", node, id); } crm_element_value_int(level, XML_ATTR_ID, &id); if(tp == NULL) { - crm_info("Node %s not found (%d active entries)", - node, g_hash_table_size(topology)); + crm_info("Node %s not found (%d active entries)", + node, g_hash_table_size(topology)); return pcmk_ok; } else if(id < 0 || id >= ST_LEVEL_MAX) { return -EINVAL; } if(id == 0 && g_hash_table_remove(topology, node)) { - crm_info("Removed all %s related entries from the topology (%d active entries)", - node, g_hash_table_size(topology)); + crm_info("Removed all %s related entries from the topology (%d active entries)", + node, g_hash_table_size(topology)); } else if(id > 0 && tp->levels[id] != NULL) { g_list_free_full(tp->levels[id], free); tp->levels[id] = NULL; - crm_info("Removed entry '%d' from %s's topology (%d active entries remaining)", - id, node, count_active_levels(tp)); - } + crm_info("Removed entry '%d' from %s's topology (%d active entries remaining)", + id, node, count_active_levels(tp)); + } return pcmk_ok; } static gboolean string_in_list(GListPtr list, const char *item) { int lpc = 0; int max = g_list_length(list); for(lpc = 0; lpc < max; lpc ++) { - const char *value = g_list_nth_data(list, lpc); - if(safe_str_eq(item, value)) { - return TRUE; - } + const char *value = g_list_nth_data(list, lpc); + if(safe_str_eq(item, value)) { + return TRUE; + } } return FALSE; } static int stonith_device_action(xmlNode *msg, char **output) { int rc = pcmk_ok; xmlNode *dev = get_xpath_object("//"F_STONITH_DEVICE, msg, LOG_ERR); const char *id = crm_element_value(dev, F_STONITH_DEVICE); async_command_t *cmd = NULL; stonith_device_t *device = NULL; if(id) { - crm_trace("Looking for '%s'", id); - device = g_hash_table_lookup(device_list, id); + crm_trace("Looking for '%s'", id); + device = g_hash_table_lookup(device_list, id); } if(device) { - cmd = create_async_command(msg); - if(cmd == NULL) { - free_device(device); - return -EPROTO; - } - - schedule_stonith_command(cmd, device); - rc = -EINPROGRESS; - + cmd = create_async_command(msg); + if(cmd == NULL) { + free_device(device); + return -EPROTO; + } + + schedule_stonith_command(cmd, device); + rc = -EINPROGRESS; + } else { - crm_info("Device %s not found", id?id:""); - rc = -ENODEV; + crm_info("Device %s not found", id?id:""); + rc = -ENODEV; } return rc; } static gboolean can_fence_host_with_device(stonith_device_t *dev, const char *host) { gboolean can = FALSE; const char *alias = host; const char *check_type = NULL; if(dev == NULL) { - return FALSE; + return FALSE; } else if(host == NULL) { - return TRUE; + return TRUE; } if(g_hash_table_lookup(dev->aliases, host)) { - alias = g_hash_table_lookup(dev->aliases, host); + alias = g_hash_table_lookup(dev->aliases, host); } check_type = g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTCHECK); - + if(check_type == NULL) { - if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTLIST)) { - check_type = "static-list"; - } else if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP)) { - check_type = "static-list"; - } else { - check_type = "dynamic-list"; - } + if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTLIST)) { + check_type = "static-list"; + } else if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP)) { + check_type = "static-list"; + } else { + check_type = "dynamic-list"; + } } - + if(safe_str_eq(check_type, "none")) { - can = TRUE; + can = TRUE; } else if(safe_str_eq(check_type, "static-list")) { - /* Presence in the hostmap is sufficient - * Only use if all hosts on which the device can be active can always fence all listed hosts - */ + /* Presence in the hostmap is sufficient + * Only use if all hosts on which the device can be active can always fence all listed hosts + */ - if(string_in_list(dev->targets, host)) { - can = TRUE; - } else if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP) + if(string_in_list(dev->targets, host)) { + can = TRUE; + } else if(g_hash_table_lookup(dev->params, STONITH_ATTR_HOSTMAP) && g_hash_table_lookup(dev->aliases, host)) { - can = TRUE; - } + can = TRUE; + } } else if(safe_str_eq(check_type, "dynamic-list")) { - time_t now = time(NULL); - - /* Host/alias must be in the list output to be eligable to be fenced - * - * Will cause problems if down'd nodes aren't listed or (for virtual nodes) - * if the guest is still listed despite being moved to another machine - */ - - if(dev->targets_age < 0) { - crm_trace("Port list queries disabled for %s", dev->id); - - } else if(dev->targets == NULL || dev->targets_age + 60 < now) { - char *output = NULL; - int rc = pcmk_ok; - int exec_rc = pcmk_ok; - - /* Check for the target's presence in the output of the 'list' command */ - g_list_free_full(dev->targets, free); - dev->targets = NULL; + time_t now = time(NULL); + + /* Host/alias must be in the list output to be eligable to be fenced + * + * Will cause problems if down'd nodes aren't listed or (for virtual nodes) + * if the guest is still listed despite being moved to another machine + */ + + if(dev->targets_age < 0) { + crm_trace("Port list queries disabled for %s", dev->id); + + } else if(dev->targets == NULL || dev->targets_age + 60 < now) { + char *output = NULL; + int rc = pcmk_ok; + int exec_rc = pcmk_ok; + + /* Check for the target's presence in the output of the 'list' command */ + g_list_free_full(dev->targets, free); + dev->targets = NULL; while(dev->active_pid != 0 && kill(dev->active_pid, 0) == 0) { /* This is a hack * The proper approach would be to do asynchronous replies */ crm_trace("Waiting for %u to exit for %s", dev->active_pid, dev->id); sleep(1); } - exec_rc = run_stonith_agent(dev->agent, "list", NULL, dev->params, NULL, &rc, &output, NULL); + exec_rc = run_stonith_agent(dev->agent, "list", NULL, dev->params, NULL, &rc, &output, NULL); if(rc != 0 && dev->active_pid == 0) { /* This device probably only supports a single * connection, which appears to already be in use, * likely involved in a montior or (less likely) * metadata operation. * * Avoid disabling port list queries in the hope that * the op would succeed next time */ crm_info("Couldn't query ports for %s. Call failed with rc=%d and active_pid=%d: %s", dev->agent, rc, dev->active_pid, output); - } else if(exec_rc < 0 || rc != 0) { - crm_notice("Disabling port list queries for %s (%d/%d): %s", - dev->id, exec_rc, rc, output); - dev->targets_age = -1; + } else if(exec_rc < 0 || rc != 0) { + crm_notice("Disabling port list queries for %s (%d/%d): %s", + dev->id, exec_rc, rc, output); + dev->targets_age = -1; /* Fall back to status */ g_hash_table_replace(dev->params, strdup(STONITH_ATTR_HOSTCHECK), strdup("status")); - - } else { - crm_info("Refreshing port list for %s", dev->id); - dev->targets = parse_host_list(output); - dev->targets_age = now; - } - - free(output); - } - - if(string_in_list(dev->targets, alias)) { - can = TRUE; - } + + } else { + crm_info("Refreshing port list for %s", dev->id); + dev->targets = parse_host_list(output); + dev->targets_age = now; + } + + free(output); + } + + if(string_in_list(dev->targets, alias)) { + can = TRUE; + } } else if(safe_str_eq(check_type, "status")) { - int rc = 0; - int exec_rc = 0; + int rc = 0; + int exec_rc = 0; - /* Run the status operation for the device/target combination - * Will cause problems if the device doesn't return 2 for down'd nodes or - * (for virtual nodes) if the device doesn't return 1 for guests that - * have been moved to another host - */ + /* Run the status operation for the device/target combination + * Will cause problems if the device doesn't return 2 for down'd nodes or + * (for virtual nodes) if the device doesn't return 1 for guests that + * have been moved to another host + */ - exec_rc = run_stonith_agent( - dev->agent, "status", host, dev->params, dev->aliases, &rc, NULL, NULL); + exec_rc = run_stonith_agent( + dev->agent, "status", host, dev->params, dev->aliases, &rc, NULL, NULL); - if(exec_rc != 0) { - crm_err("Could not invoke %s: rc=%d", dev->id, exec_rc); + if(exec_rc != 0) { + crm_err("Could not invoke %s: rc=%d", dev->id, exec_rc); - } else if(rc == 1 /* unkown */) { - crm_trace("Host %s is not known by %s", host, dev->id); - - } else if(rc == 0 /* active */ || rc == 2 /* inactive */) { - can = TRUE; + } else if(rc == 1 /* unkown */) { + crm_trace("Host %s is not known by %s", host, dev->id); - } else { - crm_notice("Unkown result when testing if %s can fence %s: rc=%d", dev->id, host, rc); - } + } else if(rc == 0 /* active */ || rc == 2 /* inactive */) { + can = TRUE; + + } else { + crm_notice("Unkown result when testing if %s can fence %s: rc=%d", dev->id, host, rc); + } } else { - crm_err("Unknown check type: %s", check_type); + crm_err("Unknown check type: %s", check_type); } if(safe_str_eq(host, alias)) { - crm_info("%s can%s fence %s: %s", dev->id, can?"":" not", host, check_type); + crm_info("%s can%s fence %s: %s", dev->id, can?"":" not", host, check_type); } else { - crm_info("%s can%s fence %s (aka. '%s'): %s", dev->id, can?"":" not", host, alias, check_type); + crm_info("%s can%s fence %s (aka. '%s'): %s", dev->id, can?"":" not", host, alias, check_type); } return can; } struct device_search_s { - const char *host; - GListPtr capable; + const char *host; + GListPtr capable; }; static void search_devices( gpointer key, gpointer value, gpointer user_data) { stonith_device_t *dev = value; struct device_search_s *search = user_data; if(can_fence_host_with_device(dev, search->host)) { - search->capable = g_list_append(search->capable, value); + search->capable = g_list_append(search->capable, value); } } static int stonith_query(xmlNode *msg, xmlNode **list) { struct device_search_s search; int available_devices = 0; xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, msg, LOG_DEBUG_3); - + search.host = NULL; search.capable = NULL; if(dev) { const char *device = crm_element_value(dev, F_STONITH_DEVICE); - search.host = crm_element_value(dev, F_STONITH_TARGET); + search.host = crm_element_value(dev, F_STONITH_TARGET); if(device && safe_str_eq(device, "manual_ack")) { /* No query necessary */ if(list) { *list = NULL; } return pcmk_ok; } } - + crm_log_xml_debug(msg, "Query"); - + g_hash_table_foreach(device_list, search_devices, &search); available_devices = g_list_length(search.capable); if(search.host) { - crm_debug("Found %d matching devices for '%s'", - available_devices, search.host); + crm_debug("Found %d matching devices for '%s'", + available_devices, search.host); } else { - crm_debug("%d devices installed", available_devices); + crm_debug("%d devices installed", available_devices); } - + /* Pack the results into data */ if(list) { - GListPtr lpc = NULL; - *list = create_xml_node(NULL, __FUNCTION__); - crm_xml_add(*list, F_STONITH_TARGET, search.host); - crm_xml_add_int(*list, "st-available-devices", available_devices); - for(lpc = search.capable; lpc != NULL; lpc = lpc->next) { - stonith_device_t *device = (stonith_device_t*)lpc->data; - dev = create_xml_node(*list, F_STONITH_DEVICE); - crm_xml_add(dev, XML_ATTR_ID, device->id); - crm_xml_add(dev, "namespace", device->namespace); - crm_xml_add(dev, "agent", device->agent); - if(search.host == NULL) { - xmlNode *attrs = create_xml_node(dev, XML_TAG_ATTRS); - g_hash_table_foreach(device->params, hash2field, attrs); - } - } - } - + GListPtr lpc = NULL; + *list = create_xml_node(NULL, __FUNCTION__); + crm_xml_add(*list, F_STONITH_TARGET, search.host); + crm_xml_add_int(*list, "st-available-devices", available_devices); + for(lpc = search.capable; lpc != NULL; lpc = lpc->next) { + stonith_device_t *device = (stonith_device_t*)lpc->data; + dev = create_xml_node(*list, F_STONITH_DEVICE); + crm_xml_add(dev, XML_ATTR_ID, device->id); + crm_xml_add(dev, "namespace", device->namespace); + crm_xml_add(dev, "agent", device->agent); + if(search.host == NULL) { + xmlNode *attrs = create_xml_node(dev, XML_TAG_ATTRS); + g_hash_table_foreach(device->params, hash2field, attrs); + } + } + } + g_list_free(search.capable); return available_devices; } static void log_operation(async_command_t *cmd, int rc, int pid, const char *next, const char *output) { if(rc == 0) { - next = NULL; + next = NULL; } - + if(cmd->victim != NULL) { - do_crm_log(rc==0?LOG_NOTICE:LOG_ERR, - "Operation '%s' [%d] (call %d from %s) for host '%s' with device '%s' returned: %d%s%s", - cmd->action, pid, cmd->id, cmd->client, cmd->victim, cmd->device, rc, - next?". Trying: ":"", next?next:""); + do_crm_log(rc==0?LOG_NOTICE:LOG_ERR, + "Operation '%s' [%d] (call %d from %s) for host '%s' with device '%s' returned: %d%s%s", + cmd->action, pid, cmd->id, cmd->client, cmd->victim, cmd->device, rc, + next?". Trying: ":"", next?next:""); } else { - do_crm_log_unlikely(rc==0?LOG_DEBUG:LOG_NOTICE, - "Operation '%s' [%d] for device '%s' returned: %d%s%s", - cmd->action, pid, cmd->device, rc, next?". Trying: ":"", next?next:""); + do_crm_log_unlikely(rc==0?LOG_DEBUG:LOG_NOTICE, + "Operation '%s' [%d] for device '%s' returned: %d%s%s", + cmd->action, pid, cmd->device, rc, next?". Trying: ":"", next?next:""); } if(output) { - /* Logging the whole string confuses syslog when the string is xml */ - char *local_copy = strdup(output); - int lpc = 0, last = 0, more = strlen(local_copy); - for(lpc = 0; lpc < more; lpc++) { - if(local_copy[lpc] == '\n' || local_copy[lpc] == 0) { - local_copy[lpc] = 0; - do_crm_log(rc==0?LOG_INFO:LOG_WARNING, "%s: %s", - cmd->device, local_copy+last); - last = lpc+1; - } - } - crm_debug("%s: %s (total %d bytes)", cmd->device, local_copy+last, more); - free(local_copy); + /* Logging the whole string confuses syslog when the string is xml */ + char *local_copy = strdup(output); + int lpc = 0, last = 0, more = strlen(local_copy); + for(lpc = 0; lpc < more; lpc++) { + if(local_copy[lpc] == '\n' || local_copy[lpc] == 0) { + local_copy[lpc] = 0; + do_crm_log(rc==0?LOG_INFO:LOG_WARNING, "%s: %s", + cmd->device, local_copy+last); + last = lpc+1; + } + } + crm_debug("%s: %s (total %d bytes)", cmd->device, local_copy+last, more); + free(local_copy); } } #define READ_MAX 500 static void st_child_done(GPid pid, gint status, gpointer user_data) { int rc = -pcmk_err_generic; int len = 0; int more = 0; gboolean bcast = TRUE; - - char *output = NULL; + + char *output = NULL; xmlNode *data = NULL; xmlNode *reply = NULL; stonith_device_t *device = NULL; async_command_t *cmd = user_data; - + CRM_CHECK(cmd != NULL, return); if(cmd->timer_sigterm > 0) { g_source_remove(cmd->timer_sigterm); } if(cmd->timer_sigkill > 0) { g_source_remove(cmd->timer_sigkill); } - + if(WIFSIGNALED(status)) { int signo = WTERMSIG(status); if(signo == SIGTERM || signo == SIGKILL) { rc = -ETIME; } else { rc = -ECONNABORTED; } crm_notice("Child process %d performing action '%s' with '%s' terminated with signal %d", pid, cmd->action, cmd->device, signo); } else if(WIFEXITED(status)) { rc = WEXITSTATUS(status); crm_debug("Child process %d performing action '%s' with '%s' exited with rc %d", pid, cmd->action, cmd->device, rc); } - + active_children--; /* The device is ready to do something else now */ device = g_hash_table_lookup(device_list, cmd->device); if(device) { - device->active_pid = 0; - mainloop_set_trigger(device->work); + device->active_pid = 0; + mainloop_set_trigger(device->work); } do { - char buffer[READ_MAX]; - - errno = 0; - if(cmd->stdout > 0) { - memset(&buffer, 0, READ_MAX); - more = read(cmd->stdout, buffer, READ_MAX-1); - crm_trace("Got %d more bytes: %s", more, buffer); - } - - if(more > 0) { - output = realloc(output, len + more + 1); - sprintf(output+len, "%s", buffer); - len += more; - } - + char buffer[READ_MAX]; + + errno = 0; + if(cmd->stdout > 0) { + memset(&buffer, 0, READ_MAX); + more = read(cmd->stdout, buffer, READ_MAX-1); + crm_trace("Got %d more bytes: %s", more, buffer); + } + + if(more > 0) { + output = realloc(output, len + more + 1); + sprintf(output+len, "%s", buffer); + len += more; + } + } while (more == (READ_MAX-1) || (more < 0 && errno == EINTR)); if(cmd->stdout) { - close(cmd->stdout); - cmd->stdout = 0; + close(cmd->stdout); + cmd->stdout = 0; } crm_trace("Operation on %s completed with rc=%d (%d remaining)", cmd->device, rc, g_list_length(cmd->device_next)); if(rc != 0 && cmd->device_next) { - stonith_device_t *dev = cmd->device_next->data; + stonith_device_t *dev = cmd->device_next->data; - log_operation(cmd, rc, pid, dev->id, output); - - cmd->device_next = cmd->device_next->next; - schedule_stonith_command(cmd, dev); - goto done; + log_operation(cmd, rc, pid, dev->id, output); + + cmd->device_next = cmd->device_next->next; + schedule_stonith_command(cmd, dev); + goto done; } if(rc > 0) { - rc = -pcmk_err_generic; + rc = -pcmk_err_generic; } - + reply = stonith_construct_async_reply(cmd, output, data, rc); if(safe_str_eq(cmd->action, "metadata")) { - /* Too verbose to log */ - bcast = FALSE; - free(output); output = NULL; + /* Too verbose to log */ + bcast = FALSE; + free(output); output = NULL; crm_trace("Directed reply: %s op", cmd->action); - + } else if(crm_str_eq(cmd->action, "monitor", TRUE)) { crm_trace("Directed reply: %s op", cmd->action); - bcast = FALSE; + bcast = FALSE; } else if(safe_str_eq(cmd->mode, "slave")) { crm_trace("Directed reply: Complex op with %s", cmd->device); - bcast = FALSE; + bcast = FALSE; } log_operation(cmd, rc, pid, NULL, output); crm_log_xml_trace(reply, "Reply"); - + if(bcast && !stand_alone) { - /* Send reply as T_STONITH_NOTIFY so everyone does notifications - * Potentially limit to unsucessful operations to the originator? - */ + /* Send reply as T_STONITH_NOTIFY so everyone does notifications + * Potentially limit to unsucessful operations to the originator? + */ crm_trace("Broadcast reply"); - crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY); - send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE); + crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY); + send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE); } else if(cmd->origin) { crm_trace("Directed reply to %s", cmd->origin); - send_cluster_message(cmd->origin, crm_msg_stonith_ng, reply, FALSE); + send_cluster_message(cmd->origin, crm_msg_stonith_ng, reply, FALSE); } else { crm_trace("Directed local %ssync reply to %s", (cmd->options & st_opt_sync_call)?"":"a-", cmd->client); - do_local_reply(reply, cmd->client, cmd->options & st_opt_sync_call, FALSE); + do_local_reply(reply, cmd->client, cmd->options & st_opt_sync_call, FALSE); } if(stand_alone) { /* Do notification with a clean data object */ xmlNode *notify_data = create_xml_node(NULL, T_STONITH_NOTIFY_FENCE); crm_xml_add_int(notify_data, F_STONITH_RC, rc); crm_xml_add(notify_data, F_STONITH_TARGET, cmd->victim); - crm_xml_add(notify_data, F_STONITH_OPERATION, cmd->op); + crm_xml_add(notify_data, F_STONITH_OPERATION, cmd->op); crm_xml_add(notify_data, F_STONITH_DELEGATE, cmd->device); crm_xml_add(notify_data, F_STONITH_REMOTE, cmd->remote); crm_xml_add(notify_data, F_STONITH_ORIGIN, cmd->client); - + do_stonith_notify(0, T_STONITH_NOTIFY_FENCE, rc, notify_data, NULL); } free_async_command(cmd); done: free(output); free_xml(reply); free_xml(data); } static gint sort_device_priority(gconstpointer a, gconstpointer b) { const stonith_device_t *dev_a = a; const stonith_device_t *dev_b = a; if(dev_a->priority > dev_b->priority) { - return -1; + return -1; } else if(dev_a->priority < dev_b->priority) { - return 1; + return 1; } return 0; } -static int stonith_fence(xmlNode *msg) +static int stonith_fence(xmlNode *msg) { int options = 0; const char *device_id = NULL; stonith_device_t *device = NULL; async_command_t *cmd = create_async_command(msg); xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, msg, LOG_ERR); if(cmd == NULL) { - return -EPROTO; + return -EPROTO; } device_id = crm_element_value(dev, F_STONITH_DEVICE); if(device_id) { device = g_hash_table_lookup(device_list, device_id); if(device == NULL) { crm_err("Requested device '%s' is not available", device_id); } - + } else { struct device_search_s search; search.capable = NULL; search.host = crm_element_value(dev, F_STONITH_TARGET); - + crm_element_value_int(msg, F_STONITH_CALLOPTS, &options); if(options & st_opt_cs_nodeid) { int nodeid = crm_atoi(search.host, NULL); crm_node_t *node = crm_get_peer(nodeid, NULL); if(node) { search.host = node->uname; } } - + g_hash_table_foreach(device_list, search_devices, &search); crm_info("Found %d matching devices for '%s'", g_list_length(search.capable), search.host); - + if(g_list_length(search.capable) > 0) { /* Order based on priority */ search.capable = g_list_sort(search.capable, sort_device_priority); - + device = search.capable->data; if(g_list_length(search.capable) > 1) { cmd->device_list = search.capable; cmd->device_next = cmd->device_list->next; } else { g_list_free(search.capable); } } } if(device) { cmd->device = device->id; schedule_stonith_command(cmd, device); return -EINPROGRESS; } - free_async_command(cmd); + free_async_command(cmd); return -EHOSTUNREACH; } xmlNode *stonith_construct_reply(xmlNode *request, char *output, xmlNode *data, int rc) { int lpc = 0; xmlNode *reply = NULL; - + const char *name = NULL; const char *value = NULL; const char *names[] = { - F_STONITH_OPERATION, - F_STONITH_CALLID, - F_STONITH_CLIENTID, - F_STONITH_REMOTE, - F_STONITH_CALLOPTS + F_STONITH_OPERATION, + F_STONITH_CALLID, + F_STONITH_CLIENTID, + F_STONITH_REMOTE, + F_STONITH_CALLOPTS }; crm_trace("Creating a basic reply"); reply = create_xml_node(NULL, T_STONITH_REPLY); crm_xml_add(reply, "st_origin", __FUNCTION__); crm_xml_add(reply, F_TYPE, T_STONITH_NG); crm_xml_add(reply, "st_output", output); crm_xml_add_int(reply, F_STONITH_RC, rc); CRM_CHECK(request != NULL, crm_warn("Can't create a sane reply"); return reply); for(lpc = 0; lpc < DIMOF(names); lpc++) { - name = names[lpc]; - value = crm_element_value(request, name); - crm_xml_add(reply, name, value); + name = names[lpc]; + value = crm_element_value(request, name); + crm_xml_add(reply, name, value); } if(data != NULL) { - crm_trace("Attaching reply output"); - add_message_xml(reply, F_STONITH_CALLDATA, data); + crm_trace("Attaching reply output"); + add_message_xml(reply, F_STONITH_CALLDATA, data); } return reply; } xmlNode *stonith_construct_async_reply(async_command_t *cmd, char *output, xmlNode *data, int rc) { xmlNode *reply = NULL; crm_trace("Creating a basic reply"); reply = create_xml_node(NULL, T_STONITH_REPLY); crm_xml_add(reply, "st_origin", __FUNCTION__); crm_xml_add(reply, F_TYPE, T_STONITH_NG); crm_xml_add(reply, F_STONITH_OPERATION, cmd->op); crm_xml_add(reply, F_STONITH_DEVICE, cmd->device); crm_xml_add(reply, F_STONITH_REMOTE, cmd->remote); crm_xml_add(reply, F_STONITH_CLIENTID, cmd->client); crm_xml_add(reply, F_STONITH_TARGET, cmd->victim); crm_xml_add(reply, F_STONITH_ACTION, cmd->op); crm_xml_add_int(reply, F_STONITH_CALLID, cmd->id); crm_xml_add_int(reply, F_STONITH_CALLOPTS, cmd->options); - + crm_xml_add_int(reply, F_STONITH_RC, rc); crm_xml_add(reply, "st_output", output); if(data != NULL) { - crm_info("Attaching reply output"); - add_message_xml(reply, F_STONITH_CALLDATA, data); + crm_info("Attaching reply output"); + add_message_xml(reply, F_STONITH_CALLDATA, data); } return reply; } void stonith_command(stonith_client_t *client, xmlNode *request, const char *remote) { int call_options = 0; int rc = -EOPNOTSUPP; gboolean is_reply = FALSE; gboolean always_reply = FALSE; xmlNode *reply = NULL; xmlNode *data = NULL; char *output = NULL; const char *op = crm_element_value(request, F_STONITH_OPERATION); const char *client_id = crm_element_value(request, F_STONITH_CLIENTID); - + crm_element_value_int(request, F_STONITH_CALLOPTS, &call_options); if(get_xpath_object("//"T_STONITH_REPLY, request, LOG_DEBUG_3)) { - is_reply = TRUE; + is_reply = TRUE; } - + crm_debug("Processing %s%s from %s (%16x)", op, is_reply?" reply":"", - client?client->name:remote, call_options); + client?client->name:remote, call_options); if(crm_str_eq(op, CRM_OP_REGISTER, TRUE)) { xmlNode *reply = create_xml_node(NULL, "reply"); crm_xml_add(reply, F_STONITH_OPERATION, CRM_OP_REGISTER); crm_xml_add(reply, F_STONITH_CLIENTID, client->id); - crm_ipcs_send(client->channel, reply, FALSE); + crm_ipcs_send(client->channel, reply, FALSE); free_xml(reply); return; } else if(crm_str_eq(op, STONITH_OP_EXEC, TRUE)) { - rc = stonith_device_action(request, &output); + rc = stonith_device_action(request, &output); } else if(is_reply && crm_str_eq(op, STONITH_OP_QUERY, TRUE)) { - process_remote_stonith_query(request); - return; - + process_remote_stonith_query(request); + return; + } else if(crm_str_eq(op, STONITH_OP_QUERY, TRUE)) { - create_remote_stonith_op(client_id, request, TRUE); /* Record it for the future notification */ - rc = stonith_query(request, &data); + create_remote_stonith_op(client_id, request, TRUE); /* Record it for the future notification */ + rc = stonith_query(request, &data); always_reply = TRUE; if(!data) { return; } - + } else if(is_reply && crm_str_eq(op, T_STONITH_NOTIFY, TRUE)) { - process_remote_stonith_exec(request); - return; - + process_remote_stonith_exec(request); + return; + } else if(is_reply && crm_str_eq(op, STONITH_OP_FENCE, TRUE)) { /* Reply to a complex fencing op */ - process_remote_stonith_exec(request); - return; + process_remote_stonith_exec(request); + return; } else if(crm_str_eq(op, T_STONITH_NOTIFY, TRUE)) { - const char *flag_name = NULL; - - flag_name = crm_element_value(request, F_STONITH_NOTIFY_ACTIVATE); - if(flag_name) { - crm_debug("Setting %s callbacks for %s (%s): ON", - flag_name, client->name, client->id); - client->flags |= get_stonith_flag(flag_name); - } - - flag_name = crm_element_value(request, F_STONITH_NOTIFY_DEACTIVATE); - if(flag_name) { - crm_debug("Setting %s callbacks for %s (%s): off", - flag_name, client->name, client->id); - client->flags |= get_stonith_flag(flag_name); - } + const char *flag_name = NULL; + + flag_name = crm_element_value(request, F_STONITH_NOTIFY_ACTIVATE); + if(flag_name) { + crm_debug("Setting %s callbacks for %s (%s): ON", + flag_name, client->name, client->id); + client->flags |= get_stonith_flag(flag_name); + } + + flag_name = crm_element_value(request, F_STONITH_NOTIFY_DEACTIVATE); + if(flag_name) { + crm_debug("Setting %s callbacks for %s (%s): off", + flag_name, client->name, client->id); + client->flags |= get_stonith_flag(flag_name); + } crm_ipcs_send_ack(client->channel, "ack", __FUNCTION__, __LINE__); - return; + return; /* } else if(is_reply && crm_str_eq(op, STONITH_OP_FENCE, TRUE)) { */ - /* process_remote_stonith_exec(request); */ - /* return; */ + /* process_remote_stonith_exec(request); */ + /* return; */ } else if(is_reply == FALSE && crm_str_eq(op, STONITH_OP_RELAY, TRUE)) { if(initiate_remote_stonith_op(NULL, request, FALSE) != NULL) { rc = -EINPROGRESS; } } else if(is_reply == FALSE && crm_str_eq(op, STONITH_OP_FENCE, TRUE)) { if(remote || stand_alone) { rc = stonith_fence(request); - + } else if(call_options & st_opt_manual_ack) { - remote_fencing_op_t *rop = initiate_remote_stonith_op(client, request, TRUE); + remote_fencing_op_t *rop = initiate_remote_stonith_op(client, request, TRUE); rc = stonith_manual_ack(request, rop); } else { const char *alternate_host = NULL; xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, request, LOG_TRACE); const char *target = crm_element_value_copy(dev, F_STONITH_TARGET); if((call_options & st_opt_sync_call) == 0) { crm_ipcs_send_ack(client->channel, "ack", __FUNCTION__, __LINE__); } if(g_hash_table_lookup(topology, target) && safe_str_eq(target, stonith_our_uname)) { GHashTableIter gIter; crm_node_t *entry = NULL; int membership = crm_proc_plugin | crm_proc_heartbeat | crm_proc_cpg; g_hash_table_iter_init(&gIter, crm_peer_cache); while (g_hash_table_iter_next(&gIter, NULL, (void **)&entry)) { crm_trace("Checking for %s.%d != %s", entry->uname, entry->id, target); if(entry->uname && (entry->processes & membership) && safe_str_neq(entry->uname, target)) { alternate_host = entry->uname; break; } } if(alternate_host == NULL) { crm_err("No alternate host available to handle complex self fencing request"); } } if(alternate_host) { crm_notice("Forwarding complex self fencing request to peer %s", alternate_host); crm_xml_add(request, F_STONITH_OPERATION, STONITH_OP_RELAY); crm_xml_add(request, F_STONITH_CLIENTID, client->id); send_cluster_message(alternate_host, crm_msg_stonith_ng, request, FALSE); rc = -EINPROGRESS; } else if(initiate_remote_stonith_op(client, request, FALSE) != NULL) { rc = -EINPROGRESS; } } } else if (crm_str_eq(op, STONITH_OP_FENCE_HISTORY, TRUE)) { - rc = stonith_fence_history(request, &data); - always_reply = TRUE; + rc = stonith_fence_history(request, &data); + always_reply = TRUE; } else if(crm_str_eq(op, CRM_OP_REGISTER, TRUE)) { - return; - + return; + } else if(crm_str_eq(op, STONITH_OP_DEVICE_ADD, TRUE)) { const char *id = NULL; xmlNode *notify_data = create_xml_node(NULL, op); rc = stonith_device_register(request, &id); crm_xml_add(notify_data, F_STONITH_DEVICE, id); crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(device_list)); - do_stonith_notify(call_options, op, rc, notify_data, NULL); + do_stonith_notify(call_options, op, rc, notify_data, NULL); free_xml(notify_data); } else if(crm_str_eq(op, STONITH_OP_DEVICE_DEL, TRUE)) { const char *id = NULL; xmlNode *notify_data = create_xml_node(NULL, op); rc = stonith_device_remove(request, &id); crm_xml_add(notify_data, F_STONITH_DEVICE, id); crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(device_list)); - do_stonith_notify(call_options, op, rc, notify_data, NULL); + do_stonith_notify(call_options, op, rc, notify_data, NULL); free_xml(notify_data); } else if(crm_str_eq(op, STONITH_OP_LEVEL_ADD, TRUE)) { char *id = NULL; xmlNode *notify_data = create_xml_node(NULL, op); rc = stonith_level_register(request, &id); crm_xml_add(notify_data, F_STONITH_DEVICE, id); crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(topology)); - do_stonith_notify(call_options, op, rc, notify_data, NULL); + do_stonith_notify(call_options, op, rc, notify_data, NULL); free_xml(notify_data); } else if(crm_str_eq(op, STONITH_OP_LEVEL_DEL, TRUE)) { char *id = NULL; xmlNode *notify_data = create_xml_node(NULL, op); rc = stonith_level_remove(request, &id); crm_xml_add(notify_data, F_STONITH_DEVICE, id); crm_xml_add_int(notify_data, F_STONITH_ACTIVE, g_hash_table_size(topology)); - do_stonith_notify(call_options, op, rc, notify_data, NULL); + do_stonith_notify(call_options, op, rc, notify_data, NULL); free_xml(notify_data); } else if(crm_str_eq(op, STONITH_OP_CONFIRM, TRUE)) { - async_command_t *cmd = create_async_command(request); - xmlNode *reply = stonith_construct_async_reply(cmd, NULL, NULL, 0); + async_command_t *cmd = create_async_command(request); + xmlNode *reply = stonith_construct_async_reply(cmd, NULL, NULL, 0); - crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY); - crm_notice("Broadcasting manual fencing confirmation for node %s", cmd->victim); - send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE); + crm_xml_add(reply, F_STONITH_OPERATION, T_STONITH_NOTIFY); + crm_notice("Broadcasting manual fencing confirmation for node %s", cmd->victim); + send_cluster_message(NULL, crm_msg_stonith_ng, reply, FALSE); - free_async_command(cmd); - free_xml(reply); + free_async_command(cmd); + free_xml(reply); } else { - crm_err("Unknown %s%s from %s", op, is_reply?" reply":"", - client?client->name:remote); - crm_log_xml_warn(request, "UnknownOp"); + crm_err("Unknown %s%s from %s", op, is_reply?" reply":"", + client?client->name:remote); + crm_log_xml_warn(request, "UnknownOp"); } do_crm_log_unlikely(rc>0?LOG_DEBUG:LOG_INFO,"Processed %s%s from %s: %s (%d)", op, is_reply?" reply":"", - client?client->name:remote, rc>0?"":pcmk_strerror(rc), rc); - + client?client->name:remote, rc>0?"":pcmk_strerror(rc), rc); + if(is_reply || rc == -EINPROGRESS) { - /* Nothing (yet) */ - + /* Nothing (yet) */ + } else if(remote) { - reply = stonith_construct_reply(request, output, data, rc); - send_cluster_message(remote, crm_msg_stonith_ng, reply, FALSE); - free_xml(reply); + reply = stonith_construct_reply(request, output, data, rc); + send_cluster_message(remote, crm_msg_stonith_ng, reply, FALSE); + free_xml(reply); } else if(rc <= pcmk_ok || always_reply) { - reply = stonith_construct_reply(request, output, data, rc); - do_local_reply(reply, client_id, call_options & st_opt_sync_call, remote!=NULL); - free_xml(reply); + reply = stonith_construct_reply(request, output, data, rc); + do_local_reply(reply, client_id, call_options & st_opt_sync_call, remote!=NULL); + free_xml(reply); } free(output); free_xml(data); } diff --git a/fencing/remote.c b/fencing/remote.c index 55467c907b..1b421f4819 100644 --- a/fencing/remote.c +++ b/fencing/remote.c @@ -1,676 +1,665 @@ /* * Copyright (C) 2009 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include typedef struct st_query_result_s { - char *host; - int devices; - GListPtr device_list; + char *host; + int devices; + GListPtr device_list; } st_query_result_t; GHashTable *remote_op_list = NULL; void call_remote_stonith(remote_fencing_op_t *op, st_query_result_t *peer); extern xmlNode *stonith_create_op( int call_id, const char *token, const char *op, xmlNode *data, int call_options); static void free_remote_query(gpointer data) { if(data) { - st_query_result_t *query = data; + st_query_result_t *query = data; crm_trace("Free'ing query result from %s", query->host); - free(query->host); - free(query); + free(query->host); + free(query); } } static void free_remote_op(gpointer data) { remote_fencing_op_t *op = data; crm_trace("Free'ing op %s for %s", op->id, op->target); crm_log_xml_debug(op->request, "Destroying"); free(op->id); free(op->action); free(op->target); free(op->client_id); free(op->originator); if(op->query_timer) { - g_source_remove(op->query_timer); + g_source_remove(op->query_timer); } if(op->op_timer) { - g_source_remove(op->op_timer); + g_source_remove(op->op_timer); } if(op->query_results) { - g_list_free_full(op->query_results, free_remote_query); + g_list_free_full(op->query_results, free_remote_query); } if(op->request) { - free_xml(op->request); - op->request = NULL; + free_xml(op->request); + op->request = NULL; } free(op); } static void remote_op_done(remote_fencing_op_t *op, xmlNode *data, int rc) { xmlNode *reply = NULL; xmlNode *local_data = NULL; xmlNode *notify_data = NULL; op->completed = time(NULL); - + if(op->query_timer) { - g_source_remove(op->query_timer); - op->query_timer = 0; + g_source_remove(op->query_timer); + op->query_timer = 0; } if(op->op_timer) { - g_source_remove(op->op_timer); - op->op_timer = 0; + g_source_remove(op->op_timer); + op->op_timer = 0; } - - if(data == NULL) { - data = create_xml_node(NULL, "remote-op"); - local_data = data; + if(data == NULL) { + data = create_xml_node(NULL, "remote-op"); + local_data = data; } else { - op->delegate = crm_element_value_copy(data, F_ORIG); + op->delegate = crm_element_value_copy(data, F_ORIG); } - + crm_xml_add_int(data, "state", op->state); crm_xml_add(data, F_STONITH_TARGET, op->target); crm_xml_add(data, F_STONITH_OPERATION, op->action); if(op->request != NULL) { - reply = stonith_construct_reply(op->request, NULL, data, rc); - crm_xml_add(reply, F_STONITH_DELEGATE, op->delegate); - + reply = stonith_construct_reply(op->request, NULL, data, rc); + crm_xml_add(reply, F_STONITH_DELEGATE, op->delegate); do_crm_log(rc==pcmk_ok?LOG_NOTICE:LOG_ERR, - "Operation %s of %s by %s for %s[%s]: %s", - op->action, op->target, op->delegate?op->delegate:"", - op->originator, op->client_id, pcmk_strerror(rc)); + "Operation %s of %s by %s for %s[%s]: %s", + op->action, op->target, op->delegate?op->delegate:"", + op->originator, op->client_id, pcmk_strerror(rc)); } else { crm_err("Already sent notifications for '%s of %s by %s' (op=%s, for=%s, state=%d): %s", op->action, op->target, op->delegate, op->id, op->client_id, op->state, pcmk_strerror(rc)); - return; + return; } - + if(reply) { - do_local_reply(reply, op->client_id, op->call_options & st_opt_sync_call, FALSE); + do_local_reply(reply, op->client_id, op->call_options & st_opt_sync_call, FALSE); } /* Do notification with a clean data object */ notify_data = create_xml_node(NULL, T_STONITH_NOTIFY_FENCE); - crm_xml_add_int(notify_data, "state", op->state); + crm_xml_add_int(notify_data, "state", op->state); crm_xml_add_int(notify_data, F_STONITH_RC, rc); crm_xml_add(notify_data, F_STONITH_TARGET, op->target); crm_xml_add(notify_data, F_STONITH_OPERATION, op->action); crm_xml_add(notify_data, F_STONITH_DELEGATE, op->delegate); crm_xml_add(notify_data, F_STONITH_REMOTE, op->id); crm_xml_add(notify_data, F_STONITH_ORIGIN, op->originator); do_stonith_notify(0, T_STONITH_NOTIFY_FENCE, rc, notify_data, NULL); - + free_xml(notify_data); free_xml(local_data); free_xml(reply); /* Free non-essential parts of the record * Keep the record around so we can query the history */ if(op->query_results) { - g_list_free_full(op->query_results, free_remote_query); - op->query_results = NULL; + g_list_free_full(op->query_results, free_remote_query); + op->query_results = NULL; } if(op->request) { - free_xml(op->request); - op->request = NULL; + free_xml(op->request); + op->request = NULL; } } static gboolean remote_op_timeout(gpointer userdata) { remote_fencing_op_t *op = userdata; op->query_timer = 0; if(op->state == st_done) { - crm_debug("Action %s (%s) for %s already completed", op->action, op->id, op->target); - return FALSE; + crm_debug("Action %s (%s) for %s already completed", op->action, op->id, op->target); + return FALSE; } - + crm_debug("Action %s (%s) for %s timed out", op->action, op->id, op->target); remote_op_done(op, NULL, -ETIME); op->state = st_failed; return FALSE; } static gboolean remote_op_query_timeout(gpointer data) { remote_fencing_op_t *op = data; op->query_timer = 0; if(op->state == st_done) { - crm_debug("Operation %s for %s already completed", op->id, op->target); - + crm_debug("Operation %s for %s already completed", op->id, op->target); } else if(op->state == st_exec) { - crm_debug("Operation %s for %s already in progress", op->id, op->target); - + crm_debug("Operation %s for %s already in progress", op->id, op->target); } else if(op->query_results) { - crm_debug("Query %s for %s complete: %d", op->id, op->target, op->state); - call_remote_stonith(op, NULL); - + crm_debug("Query %s for %s complete: %d", op->id, op->target, op->state); + call_remote_stonith(op, NULL); } else { - if(op->op_timer) { - g_source_remove(op->op_timer); - op->op_timer = 0; - } - remote_op_timeout(op); - } - - + if(op->op_timer) { + g_source_remove(op->op_timer); + op->op_timer = 0; + } + remote_op_timeout(op); + } + return FALSE; } static int stonith_topology_next(remote_fencing_op_t *op) { stonith_topology_t *tp = NULL; if(op->target) { /* Queries don't have a target set */ tp = g_hash_table_lookup(topology, op->target); } if(tp == NULL) { return pcmk_ok; } set_bit(op->call_options, st_opt_topology); do { op->level++; } while(op->level < ST_LEVEL_MAX && tp->levels[op->level] == NULL); if(op->level < ST_LEVEL_MAX) { crm_trace("Attempting fencing level %d for %s (%d devices)", op->level, op->target, g_list_length(tp->levels[op->level])); op->devices = tp->levels[op->level]; return pcmk_ok; } crm_notice("All fencing options for %s failed", op->target); return -EINVAL; } void *create_remote_stonith_op(const char *client, xmlNode *request, gboolean peer) { remote_fencing_op_t *op = NULL; xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, request, LOG_TRACE); - + if(remote_op_list == NULL) { - remote_op_list = g_hash_table_new_full( - crm_str_hash, g_str_equal, NULL, free_remote_op); + remote_op_list = g_hash_table_new_full( + crm_str_hash, g_str_equal, NULL, free_remote_op); } - + if(peer && dev) { - const char *peer_id = crm_element_value(dev, F_STONITH_REMOTE); - CRM_CHECK(peer_id != NULL, return NULL); - - op = g_hash_table_lookup(remote_op_list, peer_id); - if(op) { - crm_debug("%s already exists", peer_id); - return op; - } - } - + const char *peer_id = crm_element_value(dev, F_STONITH_REMOTE); + CRM_CHECK(peer_id != NULL, return NULL); + + op = g_hash_table_lookup(remote_op_list, peer_id); + if(op) { + crm_debug("%s already exists", peer_id); + return op; + } + } + op = calloc(1, sizeof(remote_fencing_op_t)); crm_element_value_int(request, F_STONITH_TIMEOUT, (int*)&(op->base_timeout)); if(peer && dev) { - op->id = crm_element_value_copy(dev, F_STONITH_REMOTE); + op->id = crm_element_value_copy(dev, F_STONITH_REMOTE); crm_trace("Recorded new stonith op: %s", op->id); - } else { - op->id = crm_generate_uuid(); + op->id = crm_generate_uuid(); crm_trace("Generated new stonith op: %s", op->id); } g_hash_table_replace(remote_op_list, op->id, op); CRM_LOG_ASSERT(g_hash_table_lookup(remote_op_list, op->id) != NULL); op->state = st_query; op->action = crm_element_value_copy(dev, F_STONITH_ACTION); op->originator = crm_element_value_copy(dev, F_STONITH_OWNER); if(op->originator == NULL) { - /* Local request */ - op->originator = strdup(stonith_our_uname); + /* Local request */ + op->originator = strdup(stonith_our_uname); } if(client) { op->client_id = strdup(client); } op->target = crm_element_value_copy(dev, F_STONITH_TARGET); op->request = copy_xml(request); /* TODO: Figure out how to avoid this */ crm_element_value_int(request, F_STONITH_CALLOPTS, (int*)&(op->call_options)); if(op->call_options & st_opt_cs_nodeid) { int nodeid = crm_atoi(op->target, NULL); crm_node_t *node = crm_get_peer(nodeid, NULL); /* Ensure the conversion only happens once */ op->call_options &= ~st_opt_cs_nodeid; if(node) { free(op->target); op->target = strdup(node->uname); } } if(stonith_topology_next(op) != pcmk_ok) { op->state = st_failed; } return op; } - -remote_fencing_op_t *initiate_remote_stonith_op(stonith_client_t *client, xmlNode *request, gboolean manual_ack) +remote_fencing_op_t *initiate_remote_stonith_op(stonith_client_t *client, xmlNode *request, gboolean manual_ack) { xmlNode *query = NULL; const char *client_id = NULL; remote_fencing_op_t *op = NULL; if(client) { client_id = client->id; } else { client_id = crm_element_value(request, F_STONITH_CLIENTID); } CRM_LOG_ASSERT(client_id != NULL); op = create_remote_stonith_op(client_id, request, FALSE); query = stonith_create_op(0, op->id, STONITH_OP_QUERY, NULL, 0); if(!manual_ack) { op->op_timer = g_timeout_add(1200*op->base_timeout, remote_op_timeout, op); op->query_timer = g_timeout_add(100*op->base_timeout, remote_op_query_timeout, op); } else { - crm_xml_add(query, F_STONITH_DEVICE, "manual_ack"); + crm_xml_add(query, F_STONITH_DEVICE, "manual_ack"); } - + crm_xml_add(query, F_STONITH_REMOTE, op->id); crm_xml_add(query, F_STONITH_TARGET, op->target); - crm_xml_add(query, F_STONITH_ACTION, op->action); + crm_xml_add(query, F_STONITH_ACTION, op->action); crm_xml_add(query, F_STONITH_OWNER, op->originator); - crm_xml_add(query, F_STONITH_CLIENTID, op->client_id); + crm_xml_add(query, F_STONITH_CLIENTID, op->client_id); crm_xml_add_int(query, F_STONITH_TIMEOUT, op->base_timeout); - + crm_info("Initiating remote operation %s for %s: %s", op->action, op->target, op->id); CRM_CHECK(op->action, return NULL); - + send_cluster_message(NULL, crm_msg_stonith_ng, query, FALSE); free_xml(query); return op; } static gint sort_strings(gconstpointer a, gconstpointer b) { return strcmp(a, b); } static st_query_result_t *stonith_choose_peer(remote_fencing_op_t *op) { GListPtr iter = NULL; do { if(op->devices) { crm_trace("Checking for someone to fence %s with %s", op->target, (char*)op->devices->data); } else { crm_trace("Checking for someone to fence %s", op->target); } for(iter = op->query_results; iter != NULL; iter = iter->next) { st_query_result_t *peer = iter->data; if(is_set(op->call_options, st_opt_topology)) { /* Do they have the next device of the current fencing level? */ GListPtr match = NULL; if(op->devices) { match = g_list_find_custom(peer->device_list, op->devices->data, sort_strings); } if(match) { crm_trace("Removing %s from %s (%d remaining)", (char*)match->data, peer->host, g_list_length(peer->device_list)); peer->device_list = g_list_remove(peer->device_list, match->data); return peer; } } else if(peer && peer->devices > 0) { /* No topology: Use the current best peer */ crm_trace("Simple fencing"); return peer; } } /* Try the next fencing level if there is one */ } while(is_set(op->call_options, st_opt_topology) && stonith_topology_next(op) == pcmk_ok); if(op->devices) { crm_trace("Couldn't find anyone to fence %s with %s", op->target, (char*)op->devices->data); } else { crm_trace("Couldn't find anyone to fence %s", op->target); } return NULL; } void call_remote_stonith(remote_fencing_op_t *op, st_query_result_t *peer) { const char *device = NULL; int timeout = op->base_timeout; if(is_set(op->call_options, st_opt_topology)) { int num_devices = g_list_length(op->devices); /* Not accurate when there are multiple levels */ if(num_devices) { timeout /= num_devices; crm_trace("Dividing the timeout (%ds) equally between %d devices: %ds", op->base_timeout, num_devices, timeout); } /* Ignore any preference, they might not have the device we need */ peer = stonith_choose_peer(op); device = op->devices->data; } else if(peer == NULL) { peer = stonith_choose_peer(op); } - + if(peer) { xmlNode *query = stonith_create_op(0, op->id, STONITH_OP_FENCE, NULL, 0); crm_xml_add(query, F_STONITH_REMOTE, op->id); - crm_xml_add(query, F_STONITH_TARGET, op->target); - crm_xml_add(query, F_STONITH_ACTION, op->action); + crm_xml_add(query, F_STONITH_TARGET, op->target); + crm_xml_add(query, F_STONITH_ACTION, op->action); crm_xml_add(query, F_STONITH_OWNER, op->originator); crm_xml_add(query, F_STONITH_CLIENTID, op->client_id); crm_xml_add_int(query, F_STONITH_TIMEOUT, timeout); if(device) { crm_info("Requesting that %s perform op %s %s with %s", peer->host, op->action, op->target, device); crm_xml_add(query, F_STONITH_DEVICE, device); crm_xml_add(query, F_STONITH_MODE, "slave"); } else { crm_info("Requesting that %s perform op %s %s", peer->host, op->action, op->target); crm_xml_add(query, F_STONITH_MODE, "smart"); } op->state = st_exec; - send_cluster_message(peer->host, crm_msg_stonith_ng, query, FALSE); + send_cluster_message(peer->host, crm_msg_stonith_ng, query, FALSE); free_xml(query); return; } else if(op->query_timer == 0) { - /* We've exhausted all available peers */ - crm_info("No remaining peers capable of terminating %s", op->target); - remote_op_timeout(op); - + /* We've exhausted all available peers */ + crm_info("No remaining peers capable of terminating %s", op->target); + remote_op_timeout(op); } else if(device) { - crm_info("Waiting for additional peers capable of terminating %s with %s", op->target, device); - + crm_info("Waiting for additional peers capable of terminating %s with %s", op->target, device); } else { - crm_info("Waiting for additional peers capable of terminating %s", op->target); + crm_info("Waiting for additional peers capable of terminating %s", op->target); } - + free_remote_query(peer); } static gint sort_peers(gconstpointer a, gconstpointer b) { const st_query_result_t *peer_a = a; const st_query_result_t *peer_b = a; if(peer_a->devices > peer_b->devices) { - return -1; - + return -1; } else if(peer_a->devices > peer_b->devices) { - return 1; + return 1; } return 0; } int process_remote_stonith_query(xmlNode *msg) { int devices = 0; const char *id = NULL; const char *host = NULL; remote_fencing_op_t *op = NULL; st_query_result_t *result = NULL; xmlNode *dev = get_xpath_object("//@"F_STONITH_REMOTE, msg, LOG_ERR); xmlNode *child = NULL; - + CRM_CHECK(dev != NULL, return -EPROTO); id = crm_element_value(dev, F_STONITH_REMOTE); CRM_CHECK(id != NULL, return -EPROTO); - + dev = get_xpath_object("//@st-available-devices", msg, LOG_ERR); CRM_CHECK(dev != NULL, return -EPROTO); crm_element_value_int(dev, "st-available-devices", &devices); op = g_hash_table_lookup(remote_op_list, id); if(op == NULL) { - crm_debug("Unknown or expired remote op: %s", id); - return -EOPNOTSUPP; + crm_debug("Unknown or expired remote op: %s", id); + return -EOPNOTSUPP; } op->replies++; host = crm_element_value(msg, F_ORIG); if(devices <= 0) { /* If we're doing 'known' then we might need to fire anyway */ crm_trace("Query result from %s (%d devices)", host, devices); return pcmk_ok; } else if(op->call_options & st_opt_allow_suicide) { crm_trace("Allowing %s to potentialy fence itself", op->target); } else if(safe_str_eq(host, op->target)) { crm_info("Ignoring reply from %s, hosts are not permitted to commit suicide", op->target); return pcmk_ok; } crm_debug("Query result from %s (%d devices)", host, devices); result = calloc(1, sizeof(st_query_result_t)); result->host = strdup(host); result->devices = devices; for (child = __xml_first_child(dev); child != NULL; child = __xml_next(child)) { const char *device = ID(child); if(device) { result->device_list = g_list_prepend(result->device_list, strdup(device)); } } CRM_CHECK(devices == g_list_length(result->device_list), crm_err("Mis-match: Query claimed to have %d devices but %d found", devices, g_list_length(result->device_list))); op->query_results = g_list_insert_sorted(op->query_results, result, sort_peers); if(op->state == st_query && is_set(op->call_options, st_opt_all_replies) == FALSE) { call_remote_stonith(op, result); } else if(op->state == st_done) { crm_info("Discarding query result from %s (%d devices): Operation is in state %d", result->host, result->devices, op->state); } return pcmk_ok; } int process_remote_stonith_exec(xmlNode *msg) { int rc = 0; const char *id = NULL; remote_fencing_op_t *op = NULL; xmlNode *dev = get_xpath_object("//@"F_STONITH_REMOTE, msg, LOG_ERR); CRM_CHECK(dev != NULL, return -EPROTO); id = crm_element_value(dev, F_STONITH_REMOTE); CRM_CHECK(id != NULL, return -EPROTO); - + dev = get_xpath_object("//@"F_STONITH_RC, msg, LOG_ERR); CRM_CHECK(dev != NULL, return -EPROTO); crm_element_value_int(dev, F_STONITH_RC, &rc); if(remote_op_list) { - op = g_hash_table_lookup(remote_op_list, id); + op = g_hash_table_lookup(remote_op_list, id); } if(op == NULL && rc == pcmk_ok) { /* Record successful fencing operations */ const char *client_id = crm_element_value(msg, F_STONITH_CLIENTID); op = create_remote_stonith_op(client_id, msg, TRUE); } - + if(op == NULL) { - /* Could be for an event that began before we started */ - /* TODO: Record the op for later querying */ - crm_info("Unknown or expired remote op: %s", id); - return -EOPNOTSUPP; + /* Could be for an event that began before we started */ + /* TODO: Record the op for later querying */ + crm_info("Unknown or expired remote op: %s", id); + return -EOPNOTSUPP; } if(is_set(op->call_options, st_opt_topology)) { const char *device = crm_element_value(msg, F_STONITH_DEVICE); crm_notice("Call to %s for %s on behalf of %s: %s (%d)", device, op->target, op->originator, rc == pcmk_ok?"passed":"failed", rc); if(safe_str_eq(op->originator, stonith_our_uname)) { if(op->state == st_done) { remote_op_done(op, msg, rc); return rc; } else if(rc == pcmk_ok && op->devices) { /* Success, are there any more? */ op->devices = op->devices->next; } if(op->devices == NULL) { crm_trace("Broadcasting completion of complex fencing op for %s", op->target); send_cluster_message(NULL, crm_msg_stonith_ng, msg, FALSE); op->state = st_done; return rc; } } else { op->state = st_done; remote_op_done(op, msg, rc); } } else if(rc == pcmk_ok && op->devices == NULL) { crm_trace("All done for %s", op->target); op->state = st_done; remote_op_done(op, msg, rc); return rc; } /* Retry on failure or execute the rest of the topology */ crm_trace("Next for %s (rc was %d)", op->target, rc); call_remote_stonith(op, NULL); return rc; } int stonith_fence_history(xmlNode *msg, xmlNode **output) { int rc = 0; const char *target = NULL; xmlNode *dev = get_xpath_object("//@"F_STONITH_TARGET, msg, LOG_TRACE); if(dev) { int options = 0; - target = crm_element_value(dev, F_STONITH_TARGET); + target = crm_element_value(dev, F_STONITH_TARGET); crm_element_value_int(msg, F_STONITH_CALLOPTS, &options); if(target && (options & st_opt_cs_nodeid)) { int nodeid = crm_atoi(target, NULL); crm_node_t *node = crm_get_peer(nodeid, NULL); if(node) { target = node->uname; } } } *output = create_xml_node(NULL, F_STONITH_HISTORY_LIST); if (remote_op_list) { GHashTableIter iter; - remote_fencing_op_t *op = NULL; - - g_hash_table_iter_init(&iter, remote_op_list); - while(g_hash_table_iter_next(&iter, NULL, (void**)&op)) { - xmlNode *entry = NULL; - if (target && strcmp(op->target, target) != 0) { - continue; - } - - rc = 0; - entry = create_xml_node(*output, STONITH_OP_EXEC); - crm_xml_add(entry, F_STONITH_TARGET, op->target); - crm_xml_add(entry, F_STONITH_ACTION, op->action); - crm_xml_add(entry, F_STONITH_ORIGIN, op->originator); - crm_xml_add(entry, F_STONITH_DELEGATE, op->delegate); - crm_xml_add_int(entry, F_STONITH_DATE, op->completed); - crm_xml_add_int(entry, F_STONITH_STATE, op->state); - } + remote_fencing_op_t *op = NULL; + + g_hash_table_iter_init(&iter, remote_op_list); + while(g_hash_table_iter_next(&iter, NULL, (void**)&op)) { + xmlNode *entry = NULL; + if (target && strcmp(op->target, target) != 0) { + continue; + } + + rc = 0; + entry = create_xml_node(*output, STONITH_OP_EXEC); + crm_xml_add(entry, F_STONITH_TARGET, op->target); + crm_xml_add(entry, F_STONITH_ACTION, op->action); + crm_xml_add(entry, F_STONITH_ORIGIN, op->originator); + crm_xml_add(entry, F_STONITH_DELEGATE, op->delegate); + crm_xml_add_int(entry, F_STONITH_DATE, op->completed); + crm_xml_add_int(entry, F_STONITH_STATE, op->state); + } } return rc; }