diff --git a/fencing/commands.c b/fencing/commands.c index e9f47eca86..b0ada51887 100644 --- a/fencing/commands.c +++ b/fencing/commands.c @@ -1,505 +1,540 @@ /* * 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_FORK -2 +#define FE_AGENT_ERROR -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); 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, char **output) { char *args = make_args(arg_hash); - int pid, status, len; + int pid, status, len, rc = -1; 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 = stonith_ok; + crm_info("%d %d", WIFEXITED(status), WEXITSTATUS(status)); + + *agent_result = FE_AGENT_ERROR; + if (WIFEXITED(status)) { + *agent_result = -WEXITSTATUS(status); + rc = 0; } + } 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; + return rc; } 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) { + if(rc == 0) { 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; + return -1; } 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, char **output) { 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) { rc = invoke_device(device, action, port, output); } else { crm_err("Device %s not found", id); } 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, const char *action) +static int stonith_query(xmlNode *msg, xmlNode **list) { + struct device_search_s search; xmlNode *dev = get_xpath_object("//@target", msg, LOG_ERR); - const char *host = crm_element_value(dev, "target"); + + search.capable = NULL; + search.host = crm_element_value(dev, "target"); + + g_hash_table_foreach(device_list, search_devices, &search); + crm_info("Found %d matching devices for '%s'", g_list_length(search.capable), search.host); + + /* Pack the results into data */ + if(list) { + *list = create_xml_node(NULL, __FUNCTION__); + crm_xml_add_int(*list, "st-available-devices", g_list_length(search.capable)); + slist_iter(device, stonith_device_t, search.capable, lpc, + 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); + ); + } + + return g_list_length(search.capable); +} + +static int stonith_fence(xmlNode *msg, const char *action) +{ struct device_search_s search; + xmlNode *dev = get_xpath_object("//@target", msg, LOG_ERR); - search.host = host; search.capable = NULL; + search.host = crm_element_value(dev, "target"); g_hash_table_foreach(device_list, search_devices, &search); - crm_info("Found %d matching devices for '%s'", g_list_length(search.capable), host); + crm_info("Found %d matching devices for '%s'", g_list_length(search.capable), search.host); slist_iter(dev, stonith_device_t, search.capable, lpc, int rc = 0; char *output = NULL; - const char *port = get_device_port(dev, host); + const char *port = get_device_port(dev, search.host); CRM_CHECK(port != NULL, continue); g_hash_table_replace(dev->params, crm_strdup("option"), crm_strdup(action)); 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_info("Terminated host '%s' with device '%s'", search.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_err("Termination of host '%s' with device '%s' failed: %s", search.host, dev->id, output); } crm_free(output); ); - return -666; + return -1; } static 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_CALLOPTS }; crm_debug_4("Creating a basic reply"); reply = create_xml_node(NULL, "stonith-reply"); crm_xml_add(reply, F_TYPE, T_STONITH); for(lpc = 0; lpc < DIMOF(names); lpc++) { name = names[lpc]; value = crm_element_value(request, name); crm_xml_add(reply, name, value); } crm_xml_add_int(reply, F_STONITH_RC, rc); crm_xml_add(reply, "st_output", output); if(data != NULL) { crm_debug_4("Attaching reply output"); add_message_xml(reply, F_STONITH_CALLDATA, data); } return reply; } void -stonith_command(stonith_client_t *client, xmlNode *request) +stonith_command(stonith_client_t *client, xmlNode *request, gboolean remote) { int rc = stonith_ok; - char *output = NULL; - xmlNode *reply = NULL; int call_options = 0; + + gboolean done = TRUE; + + 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(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(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(request); } else if(crm_str_eq(op, STONITH_OP_DEVICE_DEL, TRUE)) { rc = stonith_device_remove(request); } else if(crm_str_eq(op, STONITH_OP_EXEC, TRUE)) { rc = stonith_device_action(request, &output); } else if(crm_str_eq(op, STONITH_OP_FENCE, TRUE)) { rc = stonith_fence(request, "off"); + if(rc < 0) { + stonith_query(request, &data); + } } else if(crm_str_eq(op, STONITH_OP_UNFENCE, TRUE)) { rc = stonith_fence(request, "on"); + + } else if(crm_str_eq(op, STONITH_OP_QUERY, TRUE)) { + rc = stonith_query(request, &data); } - reply = stonith_construct_reply(request, output, NULL, rc); - do_local_notify(reply, client_id, call_options & stonith_sync_call, FALSE); - free_xml(reply); - + if(done) { + reply = stonith_construct_reply(request, output, data, rc); + do_local_notify(reply, client_id, call_options & stonith_sync_call, remote); + free_xml(reply); + } + + crm_free(output); + free_xml(data); } diff --git a/fencing/internal.h b/fencing/internal.h index 9074067850..dba50eeab6 100644 --- a/fencing/internal.h +++ b/fencing/internal.h @@ -1,32 +1,32 @@ typedef struct stonith_device_s { 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); +extern void stonith_command(stonith_client_t *client, xmlNode *op_request, gboolean remote); extern void do_local_notify(xmlNode *notify_src, const char *client_id, gboolean sync_reply, gboolean from_peer); diff --git a/fencing/main.c b/fencing/main.c index e6e4842d92..412aebad8c 100644 --- a/fencing/main.c +++ b/fencing/main.c @@ -1,530 +1,530 @@ /* * 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 *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++; request = xmlfromIPC(channel, MAX_IPC_DELAY); if (request == NULL) { goto bail; } if(stonith_client->name == NULL) { value = crm_element_value(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(request, F_STONITH_CLIENTID, stonith_client->id); crm_xml_add(request, F_STONITH_CLIENTNAME, stonith_client->name); if(stonith_client->callback_id == NULL) { value = crm_element_value(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]", request); - stonith_command(stonith_client, request); + stonith_command(stonith_client, request, FALSE); free_xml(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 int send_via_callback_channel(xmlNode *msg, const char *token) { stonith_client_t *hash_client = NULL; enum stonith_errors rc = stonith_ok; crm_debug_3("Delivering msg %p to client %s", msg, token); if(token == NULL) { crm_err("No client id token, cant send message"); if(rc == stonith_ok) { rc = -1; } } else if(msg == NULL) { crm_err("No message to send"); rc = -1; } else { /* A client that left before we could reply is not really * _our_ error. Warn instead. */ hash_client = g_hash_table_lookup(client_list, token); if(hash_client == NULL) { crm_warn("Cannot find client for token %s", token); rc = -1; } else if (crm_str_eq(hash_client->channel_name, "remote", FALSE)) { /* just hope it's alive */ } else if(hash_client->channel == NULL) { crm_err("Cannot find channel for client %s", token); rc = -1; } } if(rc == stonith_ok) { crm_debug_3("Delivering reply to client %s (%s)", token, hash_client->channel_name); if(send_ipc_message(hash_client->channel, msg) == FALSE) { crm_warn("Delivery of reply to client %s/%s failed", hash_client->name, token); rc = -1; } } return rc; } void do_local_notify(xmlNode *notify_src, const char *client_id, gboolean sync_reply, gboolean from_peer) { /* send callback to originating child */ stonith_client_t *client_obj = NULL; enum stonith_errors local_rc = stonith_ok; crm_debug_2("Performing notification"); if(client_id != NULL) { client_obj = g_hash_table_lookup(client_list, client_id); } else { crm_debug_2("No client to sent the response to." " F_STONITH_CLIENTID not set."); } crm_debug_3("Sending callback to request originator"); if(client_obj == NULL) { local_rc = -123; } else { const char *client_id = client_obj->callback_id; crm_debug_2("Sending %ssync response to %s %s", sync_reply?"":"an a-", client_obj->name, from_peer?"(originator of delegated request)":""); if(sync_reply) { client_id = client_obj->id; } local_rc = send_via_callback_channel(notify_src, client_id); } if(local_rc != stonith_ok && client_obj != NULL) { crm_warn("%sSync reply to %s failed: %s", sync_reply?"":"A-", client_obj?client_obj->name:"", stonith_error2string(local_rc)); } } 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; } diff --git a/include/crm/stonith-ng.h b/include/crm/stonith-ng.h index f7fdf895cb..59067e3c22 100644 --- a/include/crm/stonith-ng.h +++ b/include/crm/stonith-ng.h @@ -1,143 +1,146 @@ /* * Copyright (C) 2004 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 */ #ifndef STONITH_NG__H #define STONITH_NG__H #include #include enum stonith_state { stonith_connected_command, stonith_connected_query, stonith_disconnected }; enum stonith_call_options { stonith_none = 0x00000000, stonith_verbose = 0x00000001, stonith_discard_reply = 0x00000010, stonith_scope_local = 0x00000100, stonith_sync_call = 0x00001000, }; #define stonith_default_options = stonith_none enum stonith_errors { stonith_ok = 0, stonith_not_supported = -1, stonith_connection = -2, stonith_authentication = -3, stonith_callback_register = -4, stonith_missing = -5, stonith_exists = -6, stonith_timeout = -7, stonith_ipc = -8, stonith_peer = -9, }; #define F_STONITH_CLIENTID "st_clientid" #define F_STONITH_CALLOPTS "st_callopt" #define F_STONITH_CALLID "st_callid" #define F_STONITH_CALLDATA "st_calldata" #define F_STONITH_OPERATION "st_op" #define F_STONITH_HOST "st_host" #define F_STONITH_RC "st_rc" #define F_STONITH_DELEGATED "st_delegated_from" #define F_STONITH_TIMEOUT "st_timeout" #define F_STONITH_CALLBACK_TOKEN "st_async_id" #define F_STONITH_CLIENTNAME "st_clientname" #define F_STONITH_NOTIFY_TYPE "st_notify_type" #define F_STONITH_NOTIFY_ACTIVATE "st_notify_activate" #define T_STONITH_NG "stonith-ng" #define F_STONITH_DEVICE "st_device_id" #define F_STONITH_PORT "st_device_port" #define F_STONITH_ACTION "st_device_action" #define T_STONITH_NOTIFY "st_notify" #define STONITH_OP_EXEC "st_execute" +#define STONITH_OP_QUERY "st_query" #define STONITH_OP_FENCE "st_fence" #define STONITH_OP_UNFENCE "st_unfence" #define STONITH_OP_DEVICE_ADD "st_device_register" #define STONITH_OP_DEVICE_DEL "st_device_remove" #define stonith_channel "st_command" #define stonith_channel_callback "st_callback" typedef struct stonith_s stonith_t; typedef struct stonith_api_operations_s { int (*free) (stonith_t *st); int (*connect) (stonith_t *st, const char *name, int *async_fd, int *sync_fd); int (*disconnect)(stonith_t *st); int (*remove_device)( stonith_t *st, int options, const char *name); int (*register_device)( stonith_t *st, int options, const char *id, const char *namespace, const char *agent, GHashTable *parameters); int (*call)(stonith_t *st, int options, const char *id, const char *action, const char *port, int timeout); + + int (*query)(stonith_t *st, int options, const char *node, int timeout); int (*fence)(stonith_t *st, int options, const char *node, int timeout); int (*unfence)(stonith_t *st, int options, const char *node, int timeout); int (*register_notification)( stonith_t *st, const char *event, void (*callback)( const char *event, xmlNode *msg)); int (*remove_notification)( stonith_t *st, const char *event, void (*callback)( const char *event, xmlNode *msg)); int (*remove_callback)(int call_id, gboolean all_callbacks); int (*register_callback)( stonith_t *st, int call_id, int timeout, gboolean only_success, void *user_data, const char *callback_name, void (*callback)(const xmlNode*, int, int, xmlNode*,void*)); } stonith_api_operations_t; struct stonith_s { enum stonith_state state; int call_id; int call_timeout; void *private; GList *notify_list; stonith_api_operations_t *cmds; }; /* Core functions */ extern stonith_t *stonith_api_new(void); extern void stonith_api_delete(stonith_t *st); extern const char *stonith_error2string(enum stonith_errors return_code); extern void stonith_dump_pending_callbacks(void); extern int num_stonith_op_callbacks(void); extern void remove_stonith_op_callback(int call_id, gboolean all_callbacks); #define add_stonith_op_callback(cib, id, flag, data, fn) cib->cmds->register_callback(cib, id, 120, flag, data, #fn, fn) #endif