diff --git a/fencing/commands.c b/fencing/commands.c index 6d86f0ba79..9cf8204d63 100644 --- a/fencing/commands.c +++ b/fencing/commands.c @@ -1,275 +1,457 @@ /* * 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.1 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include #define FE_AGENT_FORK -1 #define FE_AGENT_ERROR -2 -#define FE_AGENT_SUCCESS -3 GHashTable *device_list = NULL; +int invoke_device(stonith_device_t *device, const char *action, const char *port, char **output); static void append_arg( gpointer key, gpointer value, gpointer user_data) { int len = 3; /* =, \n, \0 */ int last = 0; char **args = user_data; + + if(strstr(key, "pcmk-")) { + return; + } + len += strlen(key); len += strlen(value); if(*args != NULL) { last = strlen(*args); } crm_realloc(*args, last+len); - if(*args == NULL) { - sprintf((*args)+last, "%s=%s\n", (char *)key, (char *)value); - } + sprintf((*args)+last, "%s=%s\n", (char *)key, (char *)value); } static char *make_args(GHashTable *args) { char *arg_list = NULL; g_hash_table_foreach(args, append_arg, &arg_list); + crm_debug_3("Calculated: %s", arg_list); return arg_list; } /* Borrowed from libfence */ -static int run_agent(char *agent, GHashTable *arg_hash, int *agent_result) +static int run_agent(char *agent, GHashTable *arg_hash, int *agent_result, char **output) { char *args = make_args(arg_hash); int pid, status, len; int pr_fd, pw_fd; /* parent read/write file descriptors */ int cr_fd, cw_fd; /* child read/write file descriptors */ int fd1[2]; int fd2[2]; cr_fd = cw_fd = pr_fd = pw_fd = -1; if (args == NULL || agent == NULL) goto fail; len = strlen(args); if (pipe(fd1)) goto fail; pr_fd = fd1[0]; cw_fd = fd1[1]; if (pipe(fd2)) goto fail; cr_fd = fd2[0]; pw_fd = fd2[1]; pid = fork(); if (pid < 0) { *agent_result = FE_AGENT_FORK; goto fail; } if (pid) { /* parent */ int ret; fcntl(pr_fd, F_SETFL, fcntl(pr_fd, F_GETFL, 0) | O_NONBLOCK); do { ret = write(pw_fd, args, len); } while (ret < 0 && errno == EINTR); if (ret != len) goto fail; close(pw_fd); waitpid(pid, &status, 0); + if(output != NULL) { + len = 0; + do { + char buf[500]; + ret = read(pr_fd, buf, 500); + if(ret > 0) { + buf[ret] = 0; + crm_realloc(*output, len + ret + 1); + sprintf((*output)+len, "%s", buf); + len += ret; + } + } while (ret < 0 && errno == EINTR); + } + if (!WIFEXITED(status) || WEXITSTATUS(status)) { *agent_result = FE_AGENT_ERROR; goto fail; } else { - *agent_result = FE_AGENT_SUCCESS; + *agent_result = stonith_ok; } } else { /* child */ close(1); if (dup(cw_fd) < 0) goto fail; close(2); if (dup(cw_fd) < 0) goto fail; close(0); if (dup(cr_fd) < 0) goto fail; /* keep cw_fd open so parent can report all errors. */ close(pr_fd); close(cr_fd); close(pw_fd); execlp(agent, agent, NULL); exit(EXIT_FAILURE); } crm_free(args); close(pr_fd); close(cw_fd); close(cr_fd); close(pw_fd); return 0; fail: crm_free(args); close(pr_fd); close(cw_fd); close(cr_fd); close(pw_fd); return -1; } static void free_device(gpointer data) { stonith_device_t *device = data; g_hash_table_destroy(device->params); crm_free(device->namespace); crm_free(device->agent); crm_free(device->id); crm_free(device); } +static void build_port_aliases(stonith_device_t *device) +{ + char *name = NULL; + char *value = NULL; + int last = 0, lpc = 0, max = 0; + + const char *portmap = g_hash_table_lookup(device->params, "pcmk-portmap"); + if(portmap == NULL) { + return; + } + + max = strlen(portmap); + for(; lpc < max; lpc++) { + if(portmap[lpc] == 0) { + break; + + } else if(isalpha(portmap[lpc])) { + /* keep going */ + + } else if(portmap[lpc] == '=') { + crm_malloc0(name, 1 + lpc - last); + strncpy(name, portmap + last, lpc - last); + last = lpc + 1; + + } else if(name && isspace(portmap[lpc])) { + crm_malloc0(value, 1 + lpc - last); + strncpy(value, portmap + last, lpc - last); + last = lpc + 1; + + crm_info("Adding alias '%s'='%s' for %s", name, value, device->id); + g_hash_table_replace(device->aliases, name, value); + value=NULL; + name=NULL; + + } else if(isspace(portmap[lpc])) { + last = lpc; + } + } +} static int stonith_device_register(xmlNode *msg) { xmlNode *dev = get_xpath_object("//"F_STONITH_DEVICE, msg, LOG_ERR); stonith_device_t *device = NULL; crm_malloc0(device, 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->aliases = g_hash_table_new_full(g_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str); + build_port_aliases(device); + g_hash_table_insert(device_list, device->id, device); crm_info("Added '%s' to the device list (%d active devices)", device->id, g_hash_table_size(device_list)); return stonith_ok; } static int stonith_device_remove(xmlNode *msg) { 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)); } else { crm_info("Device '%s' not found (%d active devices)", id, g_hash_table_size(device_list)); } return stonith_ok; } +static const char *get_device_port(stonith_device_t *dev, const char *host) +{ + time_t now; + char *alias = NULL; + + if(host == NULL) { + return NULL; + } + + now = time(NULL); + alias = g_hash_table_lookup(dev->aliases, host); + + if(dev->targets == NULL || dev->targets_age + 300 < now) { + int rc = stonith_ok; + char *output = NULL; + crm_free(dev->targets); + dev->targets = NULL; + + rc = invoke_device(dev, "list", NULL, &output); + crm_info("Port list for %s: %d", dev->id, rc); + if(rc == stonith_ok) { + crm_info("Refreshing port list for %s", dev->id); + dev->targets = output; + dev->targets_age = now; + } else { + crm_info("Disabling port list queries for %s", dev->id); + dev->targets_age = -1; + } + } + + /* See if portmap is defined and look up the translated name */ + if(alias) { + if(dev->targets && strstr(dev->targets, alias)) { + return alias; + } else if(dev->targets == NULL) { + return alias; + } + } + + if(dev->targets && strstr(dev->targets, host)) { + return host; + } + + return NULL; +} + +int invoke_device(stonith_device_t *device, const char *action, const char *port, char **output) +{ + int rc = 0; + const char *device_port = get_device_port(device, port); + if(port && device_port) { + g_hash_table_replace(device->params, crm_strdup("port"), crm_strdup(device_port)); + + } else if(port) { + crm_err("Unknown or unhandled port '%s' for device '%s'", port, device->id); + return -123; + } + + crm_info("Calling '%s' with action '%s'%s%s", device->id, action, port?" on port ":"", port?port:""); + g_hash_table_replace(device->params, crm_strdup("option"), crm_strdup(action)); + if(run_agent(device->agent, device->params, &rc, output) < 0) { + crm_err("Operation %s on %s failed (%d): %s", action, device->id, rc, *output); + + } else { + crm_info("Operation %s on %s passed: %s", action, device->id, *output); + } + g_hash_table_remove(device->params, "port"); + return rc; +} + static int stonith_device_action(xmlNode *msg) { + int rc = stonith_ok; xmlNode *dev = get_xpath_object("//@"F_STONITH_DEVICE, msg, LOG_ERR); const char *id = crm_element_value(dev, F_STONITH_DEVICE); const char *action = crm_element_value(dev, F_STONITH_ACTION); + const char *port = crm_element_value(dev, F_STONITH_PORT); stonith_device_t *device = NULL; if(id) { crm_info("Looking for '%s'", id); device = g_hash_table_lookup(device_list, id); } if(device) { - int rc = 0; - g_hash_table_replace(device->params, crm_strdup("option"), crm_strdup(action)); - crm_info("Calling '%s' with action '%s'", id, action); - if(run_agent(device->agent, device->params, &rc) < 0) { - crm_err("Operation %s on %s failed: %d", action, id, rc); - } else { - crm_err("Operation %s on %s passed", action, id); - } - + char *output = NULL; + rc = invoke_device(device, action, port, &output); } else { crm_err("Device %s not found", id); } - return stonith_ok; + return rc; +} + +struct device_search_s +{ + 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(get_device_port(value, search->host)) { + crm_debug_4("Device '%s' can fence '%s'", dev->id, search->host); + search->capable = g_list_append(search->capable, value); + + } else { + crm_debug_3("Device '%s' cannot fence '%s'", dev->id, search->host); + } +} + +static int stonith_fence(xmlNode *msg) +{ + xmlNode *dev = get_xpath_object("//@target", msg, LOG_ERR); + const char *host = crm_element_value(dev, "target"); + struct device_search_s search; + + search.host = host; + search.capable = NULL; + + g_hash_table_foreach(device_list, search_devices, &search); + crm_info("Found %d matching devices for '%s'", g_list_length(search.capable), host); + + slist_iter(dev, stonith_device_t, search.capable, lpc, + int rc = 0; + char *output = NULL; + const char *port = get_device_port(dev, host); + CRM_CHECK(port != NULL, continue); + + g_hash_table_replace(dev->params, crm_strdup("option"), crm_strdup("off")); + g_hash_table_replace(dev->params, crm_strdup("port"), crm_strdup(port)); + + if(run_agent(dev->agent, dev->params, &rc, &output) == 0) { + crm_info("Terminated host '%s' with device '%s'", host, dev->id); + crm_free(output); + return stonith_ok; + + } else { + crm_err("Termination of host '%s' with device '%s' failed: %s", host, dev->id, output); + } + crm_free(output); + ); + + return -666; } void stonith_command(stonith_client_t *client, xmlNode *op_request) { int rc = stonith_ok; const char *op = crm_element_value(op_request, F_STONITH_OPERATION); if(device_list == NULL) { device_list = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_device); } crm_info("Processing %s from %s", op, client->name); if(crm_str_eq(op, CRM_OP_REGISTER, TRUE)) { return; } else if(crm_str_eq(op, T_STONITH_NOTIFY, TRUE)) { /* Update the notify filters for this client */ int on_off = 0; crm_element_value_int(op_request, F_STONITH_NOTIFY_ACTIVATE, &on_off); crm_debug("Setting callbacks for %s (%s): %s", client->name, client->id, on_off?"on":"off"); client->flags = on_off; return; } else if(crm_str_eq(op, STONITH_OP_DEVICE_ADD, TRUE)) { rc = stonith_device_register(op_request); } else if(crm_str_eq(op, STONITH_OP_DEVICE_DEL, TRUE)) { rc = stonith_device_remove(op_request); } else if(crm_str_eq(op, STONITH_OP_EXEC, TRUE)) { rc = stonith_device_action(op_request); } else if(crm_str_eq(op, STONITH_OP_FENCE, TRUE)) { + rc = stonith_fence(op_request); } } diff --git a/fencing/internal.h b/fencing/internal.h index 39c829b5b8..4c1d755def 100644 --- a/fencing/internal.h +++ b/fencing/internal.h @@ -1,25 +1,30 @@ typedef struct stonith_device_s { - char *id; - char *agent; - char *namespace; - GHashTable *params; - + char *id; + char *agent; + char *namespace; + char *targets; + + time_t targets_age; + + GHashTable *params; + GHashTable *aliases; + } stonith_device_t; typedef struct stonith_client_s { char *id; char *name; char *callback_id; const char *channel_name; IPC_Channel *channel; GCHSource *source; long long flags; } stonith_client_t; extern void stonith_command(stonith_client_t *client, xmlNode *op_request); diff --git a/fencing/main.c b/fencing/main.c index fe86ef86e9..08370fe989 100644 --- a/fencing/main.c +++ b/fencing/main.c @@ -1,437 +1,441 @@ /* * 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.1 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include char *channel1 = NULL; char *channel2 = NULL; char *stonith_our_uname = NULL; GMainLoop *mainloop = NULL; GHashTable *client_list = NULL; gboolean stonith_shutdown_flag = FALSE; +#if SUPPORT_HEARTBEAT +ll_cluster_t *hb_conn = NULL; +#endif + static gboolean stonith_client_disconnect( IPC_Channel *channel, stonith_client_t *stonith_client) { if (channel == NULL) { CRM_DEV_ASSERT(stonith_client == NULL); } else if (stonith_client == NULL) { crm_err("No client"); } else { CRM_DEV_ASSERT(channel->ch_status != IPC_CONNECT); crm_debug_2("Cleaning up after client disconnect: %s/%s/%s", crm_str(stonith_client->name), stonith_client->channel_name, stonith_client->id); if(stonith_client->id != NULL) { if(!g_hash_table_remove(client_list, stonith_client->id)) { crm_err("Client %s not found in the hashtable", stonith_client->name); } } } return FALSE; } static gboolean stonith_client_callback(IPC_Channel *channel, gpointer user_data) { int lpc = 0; const char *value = NULL; xmlNode *op_request = NULL; gboolean keep_channel = TRUE; stonith_client_t *stonith_client = user_data; CRM_CHECK(stonith_client != NULL, crm_err("Invalid client"); return FALSE); CRM_CHECK(stonith_client->id != NULL, crm_err("Invalid client: %p", stonith_client); return FALSE); if(IPC_ISRCONN(channel) && channel->ops->is_message_pending(channel)) { lpc++; op_request = xmlfromIPC(channel, MAX_IPC_DELAY); if (op_request == NULL) { goto bail; } if(stonith_client->name == NULL) { value = crm_element_value(op_request, F_STONITH_CLIENTNAME); if(value == NULL) { stonith_client->name = crm_itoa(channel->farside_pid); } else { stonith_client->name = crm_strdup(value); } } crm_xml_add(op_request, F_STONITH_CLIENTID, stonith_client->id); crm_xml_add(op_request, F_STONITH_CLIENTNAME, stonith_client->name); if(stonith_client->callback_id == NULL) { value = crm_element_value(op_request, F_STONITH_CALLBACK_TOKEN); if(value != NULL) { stonith_client->callback_id = crm_strdup(value); } else { stonith_client->callback_id = crm_strdup(stonith_client->id); } } crm_log_xml(LOG_MSG, "Client[inbound]", op_request); stonith_command(stonith_client, op_request); free_xml(op_request); } bail: if(channel->ch_status != IPC_CONNECT) { crm_debug_2("Client disconnected"); keep_channel = stonith_client_disconnect(channel, stonith_client); } return keep_channel; } static void stonith_client_destroy(gpointer user_data) { stonith_client_t *stonith_client = user_data; if(stonith_client == NULL) { crm_debug_4("Destroying %p", user_data); return; } if(stonith_client->source != NULL) { crm_debug_4("Deleting %s (%p) from mainloop", stonith_client->name, stonith_client->source); G_main_del_IPC_Channel(stonith_client->source); stonith_client->source = NULL; } crm_debug_3("Destroying %s (%p)", stonith_client->name, user_data); crm_free(stonith_client->name); crm_free(stonith_client->callback_id); crm_free(stonith_client->id); crm_free(stonith_client); crm_debug_4("Freed the cib client"); return; } static gboolean stonith_client_connect(IPC_Channel *channel, gpointer user_data) { cl_uuid_t client_id; xmlNode *reg_msg = NULL; stonith_client_t *new_client = NULL; char uuid_str[UU_UNPARSE_SIZEOF]; const char *channel_name = user_data; crm_debug_3("Connecting channel"); CRM_CHECK(channel_name != NULL, return FALSE); if (channel == NULL) { crm_err("Channel was NULL"); return FALSE; } else if (channel->ch_status != IPC_CONNECT) { crm_err("Channel was disconnected"); return FALSE; } else if(stonith_shutdown_flag) { crm_info("Ignoring new client [%d] during shutdown", channel->farside_pid); return FALSE; } crm_malloc0(new_client, sizeof(stonith_client_t)); new_client->channel = channel; new_client->channel_name = channel_name; crm_debug_3("Created channel %p for channel %s", new_client, new_client->channel_name); channel->ops->set_recv_qlen(channel, 1024); channel->ops->set_send_qlen(channel, 1024); new_client->source = G_main_add_IPC_Channel( G_PRIORITY_DEFAULT, channel, FALSE, stonith_client_callback, new_client, stonith_client_destroy); crm_debug_3("Channel %s connected for client %s", new_client->channel_name, new_client->id); cl_uuid_generate(&client_id); cl_uuid_unparse(&client_id, uuid_str); CRM_CHECK(new_client->id == NULL, crm_free(new_client->id)); new_client->id = crm_strdup(uuid_str); /* make sure we can find ourselves later for sync calls * redirected to the master instance */ g_hash_table_insert(client_list, new_client->id, new_client); reg_msg = create_xml_node(NULL, "callback"); crm_xml_add(reg_msg, F_STONITH_OPERATION, CRM_OP_REGISTER); crm_xml_add(reg_msg, F_STONITH_CLIENTID, new_client->id); send_ipc_message(channel, reg_msg); free_xml(reg_msg); return TRUE; } static void stonith_peer_callback(xmlNode * msg, void* private_data) { crm_log_xml(LOG_MSG, "Peer[inbound]", msg); } static void stonith_peer_hb_callback(HA_Message * msg, void* private_data) { xmlNode *xml = convert_ha_message(NULL, msg, __FUNCTION__); stonith_peer_callback(xml, private_data); free_xml(xml); } #if SUPPORT_AIS static gboolean stonith_peer_ais_callback( AIS_Message *wrapper, char *data, int sender) { xmlNode *xml = NULL; if(wrapper->header.id == crm_class_cluster) { xml = string2xml(data); if(xml == NULL) { goto bail; } crm_xml_add(xml, F_ORIG, wrapper->sender.uname); crm_xml_add_int(xml, F_SEQ, wrapper->id); stonith_peer_callback(xml, NULL); } free_xml(xml); return TRUE; bail: crm_err("Invalid XML: '%.120s'", data); return TRUE; } static void stonith_peer_ais_destroy(gpointer user_data) { crm_err("AIS connection terminated"); ais_fd_sync = -1; exit(1); } #endif static void stonith_peer_hb_destroy(gpointer user_data) { if(stonith_shutdown_flag) { crm_info("Heartbeat disconnection complete... exiting"); } else { crm_err("Heartbeat connection lost! Exiting."); } crm_info("Exiting..."); if (mainloop != NULL && g_main_is_running(mainloop)) { g_main_quit(mainloop); } else { exit(LSB_EXIT_OK); } } static void stonith_shutdown(int nsig) { stonith_shutdown_flag = TRUE; crm_info("Terminating with %d clients", g_hash_table_size(client_list)); stonith_client_disconnect(NULL, NULL); } static void stonith_cleanup(void) { crm_peer_destroy(); g_hash_table_destroy(client_list); crm_free(stonith_our_uname); #if HAVE_LIBXML2 xmlCleanupParser(); #endif crm_free(channel1); } static struct crm_option long_options[] = { {"stand-alone", 0, 0, 's'}, {"verbose", 0, 0, 'V'}, {"version", 0, 0, '$'}, {"help", 0, 0, '?'}, {0, 0, 0, 0} }; int main(int argc, char ** argv) { int flag; int rc = 0; int argerr = 0; int option_index = 0; gboolean stand_alone = FALSE; crm_log_init("stonith-ng", LOG_INFO, TRUE, TRUE, argc, argv); crm_set_options("V?s$", "mode [options]", long_options, "Provides a summary of cluster's current state." "\n\nOutputs varying levels of detail in a number of different formats.\n"); mainloop_add_signal(SIGTERM, stonith_shutdown); /* EnableProcLogging(); */ set_sigchld_proctrack(G_PRIORITY_HIGH,DEFAULT_MAXDISPATCHTIME); crm_peer_init(); client_list = g_hash_table_new(g_str_hash, g_str_equal); while (1) { flag = crm_get_option(argc, argv, &option_index); if (flag == -1) break; switch(flag) { case 'V': alter_debug(DEBUG_INC); cl_log_enable_stderr(1); break; case 's': stand_alone = TRUE; cl_log_enable_stderr(1); break; case '$': case '?': crm_help(flag, LSB_EXIT_OK); break; default: ++argerr; break; } } if (optind > argc) { ++argerr; } if (argerr) { crm_help('?', LSB_EXIT_GENERIC); } if(stand_alone == FALSE) { void *dispatch = stonith_peer_hb_callback; void *destroy = stonith_peer_hb_destroy; if(is_openais_cluster()) { #if SUPPORT_AIS destroy = stonith_peer_ais_destroy; dispatch = stonith_peer_ais_callback; #endif } if(crm_cluster_connect(&stonith_our_uname, NULL, dispatch, destroy, #if SUPPORT_HEARTBEAT &hb_conn #else NULL #endif ) == FALSE){ crm_crit("Cannot sign in to the cluster... terminating"); exit(100); } } else { stonith_our_uname = crm_strdup("localhost"); } channel1 = crm_strdup(stonith_channel); rc = init_server_ipc_comms( channel1, stonith_client_connect, default_ipc_connection_destroy); channel2 = crm_strdup(stonith_channel_callback); rc = init_server_ipc_comms( channel2, stonith_client_connect, default_ipc_connection_destroy); if(rc == 0) { /* Create the mainloop and run it... */ mainloop = g_main_new(FALSE); crm_info("Starting %s mainloop", crm_system_name); g_main_run(mainloop); } else { crm_err("Couldnt start all communication channels, exiting."); } stonith_cleanup(); #if SUPPORT_HEARTBEAT if(hb_conn) { hb_conn->llc_ops->delete(hb_conn); } #endif crm_info("Done"); return rc; }