diff --git a/crm/admin/adminmain.c b/crm/admin/adminmain.c index dc7eaf8424..99c00c2efb 100644 --- a/crm/admin/adminmain.c +++ b/crm/admin/adminmain.c @@ -1,636 +1,624 @@ -/* $Id: adminmain.c,v 1.26 2004/06/01 16:12:49 andrew Exp $ */ +/* $Id: adminmain.c,v 1.27 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include #include -#include - -#include -#include -#include -#include -#include #include -#include -#include -#include - -#include -#include -#include -#include + #include +#include +#include +#include #include #define OPTARGS "V?i:o:D:C:S:HA:U:M:I:EWRFt:m:a:d:w:c:r:p:s:" #include #include GMainLoop *mainloop = NULL; const char *crm_system_name = "crmadmin"; IPC_Channel *crmd_channel = NULL; char *admin_uuid = NULL; void usage(const char *cmd, int exit_status); ll_cluster_t *do_init(void); int do_work(ll_cluster_t * hb_cluster); -gboolean decodeNVpair(const char *srcstring, char separator, - char **name, char **value); + gboolean admin_msg_callback(IPC_Channel * source_data, void *private_data); char *pluralSection(const char *a_section); xmlNodePtr handleCibMod(void); gboolean DO_DAEMON = FALSE; gboolean BE_VERBOSE = FALSE; int expected_responses = 1; gboolean DO_HEALTH = FALSE; gboolean DO_ELECT_DC = FALSE; gboolean DO_WHOIS_DC = FALSE; gboolean DO_RECALC_TREE = FALSE; gboolean DO_FLUSH_RECALC = FALSE; const char *cib_action = NULL; xmlNodePtr msg_options = NULL; typedef struct str_list_s { int num_items; char *value; struct str_list_s *next; } str_list_t; const char *verbose = XML_BOOLEAN_FALSE; char *id = NULL; char *this_msg_reference = NULL; char *obj_type = NULL; char *clear = NULL; char *status = NULL; char *disconnect = NULL; char *unload_ha = NULL; char *migrate_from = NULL; char *migrate_res = NULL; char *subtype = NULL; char *reset = NULL; int operation_status = 0; const char *sys_to = NULL;; int main(int argc, char **argv) { int option_index = 0; int argerr = 0; int flag; ll_cluster_t *hb_cluster = NULL; static struct option long_options[] = { // Top-level Options {"daemon", 0, 0, 0}, {CRM_OP_ERASE, 0, 0, 0}, {CRM_OP_QUERY, 0, 0, 0}, {CRM_OP_CREATE, 0, 0, 0}, {CRM_OP_REPLACE, 0, 0, 0}, {CRM_OP_STORE, 0, 0, 0}, {CRM_OP_UPDATE, 0, 0, 0}, {CRM_OP_DELETE, 0, 0, 0}, {"verbose", 0, 0, 'V'}, {"help", 0, 0, '?'}, {"reference", 1, 0, 0}, // common options {XML_ATTR_ID, 1, 0, 'i'}, {"obj_type", 1, 0, 'o'}, // daemon options {"reset", 1, 0, 'C'}, {"status", 1, 0, 'S'}, {"health", 0, 0, 'H'}, {"disconnect", 1, 0, 'A'}, {"unload_ha", 1, 0, 'U'}, {"migrate_from", 1, 0, 'M'}, {"migrate_res", 1, 0, 'I'}, {"elect_dc", 0, 0, 'E'}, {"whois_dc", 0, 0, 'W'}, {"recalc_tree", 0, 0, 'R'}, {"flush_recalc_tree", 0, 0, 'F'}, {0, 0, 0, 0} }; cl_log_set_entity(crm_system_name); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); while (1) { flag = getopt_long(argc, argv, OPTARGS, long_options, &option_index); if (flag == -1) break; switch(flag) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); if (strcmp("daemon", long_options[option_index].name) == 0) DO_DAEMON = TRUE; else if (strcmp(CRM_OP_ERASE, long_options[option_index].name) == 0 || strcmp(CRM_OP_CREATE, long_options[option_index].name) == 0 || strcmp(CRM_OP_UPDATE, long_options[option_index].name) == 0 || strcmp(CRM_OP_DELETE, long_options[option_index].name) == 0 || strcmp(CRM_OP_REPLACE, long_options[option_index].name) == 0 || strcmp(CRM_OP_STORE, long_options[option_index].name) == 0 || strcmp(CRM_OP_QUERY, long_options[option_index].name) == 0){ cib_action = crm_strdup(long_options[option_index].name); } else if (strcmp("reference", long_options[option_index].name) == 0) { this_msg_reference = crm_strdup(optarg); } else { printf( "?? Long option (--%s) is not yet properly supported ??\n", long_options[option_index].name); ++argerr; } break; /* a sample test for multiple instance if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); */ case 'V': BE_VERBOSE = TRUE; verbose = XML_BOOLEAN_TRUE; break; case '?': usage(crm_system_name, LSB_EXIT_OK); break; case 'i': CRM_DEBUG("Option %c => %s", flag, optarg); id = crm_strdup(optarg); break; case 'o': CRM_DEBUG("Option %c => %s", flag, optarg); obj_type = crm_strdup(optarg); break; case 'C': printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'S': DO_HEALTH = TRUE; status = crm_strdup(optarg); break; case 'H': DO_HEALTH = TRUE; break; case 'A': printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'U': printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'M': printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'I': printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'E': DO_ELECT_DC = TRUE; printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'W': DO_WHOIS_DC = TRUE; printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'R': DO_RECALC_TREE = TRUE; printf("Option %c is not yet supported\n", flag); ++argerr; break; case 'F': DO_FLUSH_RECALC = TRUE; printf("Option %c is not yet supported\n", flag); ++argerr; break; default: printf("?? getopt returned character code 0%o ??\n", flag); ++argerr; break; } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (optind > argc) { ++argerr; } if (argerr) { usage(crm_system_name, LSB_EXIT_GENERIC); } hb_cluster = do_init(); if (hb_cluster != NULL) { if (do_work(hb_cluster) > 0) { /* wait for the reply by creating a mainloop and running it until * the callbacks are invoked... */ mainloop = g_main_new(FALSE); cl_log(LOG_INFO, "%s waiting for reply from the local CRM", crm_system_name); g_main_run(mainloop); return_to_orig_privs(); } else { cl_log(LOG_ERR, "No message to send"); operation_status = -1; } } else { cl_log(LOG_ERR, "Init failed, could not perform requested operations"); operation_status = -2; } cl_log(LOG_DEBUG, "%s exiting normally", crm_system_name); return operation_status; } xmlNodePtr handleCibMod(void) { const char *attr_name = NULL; const char *attr_value = NULL; xmlNodePtr fragment = NULL; xmlNodePtr cib_object = file2xml(stdin); if(cib_object == NULL) { return NULL; } if(strcmp(cib_object->name, obj_type) != 0) { cl_log(LOG_ERR, "Mismatching xml." " Expected root element <%s>, got <%s>", obj_type, cib_object->name); return NULL; } attr_name = XML_ATTR_ID; attr_value = xmlGetProp(cib_object, attr_name); if(attr_name == NULL || strlen(attr_name) == 0) { cl_log(LOG_ERR, "No value for %s specified.", attr_name); return NULL; } CRM_NOTE("Object creation complete"); // create the cib request fragment = create_cib_fragment(cib_object, NULL); set_xml_property_copy(msg_options, XML_ATTR_OP, cib_action); return fragment; } int do_work(ll_cluster_t * hb_cluster) { /* construct the request */ xmlNodePtr msg_data = NULL; const char *dest_node = NULL; gboolean all_is_good = TRUE; char *obj_type_parent = NULL; const char *ping_type = NULL; msg_options = create_xml_node(NULL, XML_TAG_OPTIONS); set_xml_property_copy(msg_options, XML_ATTR_VERBOSE, verbose); set_xml_property_copy(msg_options, XML_ATTR_TIMEOUT, "0"); if (DO_DAEMON == TRUE && cib_action != NULL) { if(strcmp(CRM_OP_QUERY, cib_action) == 0) { cl_log(LOG_DEBUG, "Querying the CIB"); obj_type_parent = pluralSection(obj_type); CRM_DEBUG("Querying the CIB for section: %s", obj_type_parent); set_xml_property_copy(msg_options, XML_ATTR_OP, CRM_OP_QUERY); set_xml_property_copy(msg_options, XML_ATTR_FILTER_ID, obj_type_parent); dest_node = status; CRM_DEBUG("CIB query creation %s", msg_data == NULL ? "failed." : "passed."); sys_to = CRM_SYSTEM_DCIB; } else if (strcmp(CRM_OP_ERASE, cib_action) == 0) { set_xml_property_copy(msg_options, XML_ATTR_OP, CRM_OP_ERASE); dest_node = status; CRM_NOTE("CIB Erase op in progress"); sys_to = CRM_SYSTEM_DCIB; } else { cl_log(LOG_ERR, "Unknown daemon options"); all_is_good = FALSE; } } else if(cib_action != NULL) { msg_data = handleCibMod(); sys_to = CRM_SYSTEM_DCIB; if(msg_data == NULL) all_is_good = FALSE; } else if (DO_DAEMON == TRUE && DO_HEALTH == TRUE) { CRM_NOTE("Querying the system"); sys_to = CRM_SYSTEM_DC; if (status != NULL) { sys_to = CRM_SYSTEM_CRMD; ping_type = CRM_OP_PING; if (BE_VERBOSE) { ping_type = "ping_deep"; if (status != NULL) expected_responses = 2; // 5; // CRM/DC, LRMD, CIB, PENGINE, TENGINE else expected_responses = -1;// wait until timeout instead } set_xml_property_copy(msg_options, XML_ATTR_OP, ping_type); set_xml_property_copy(msg_options, XML_ATTR_TIMEOUT, "0"); dest_node = status; } else { cl_log(LOG_INFO, "Cluster-wide health not available yet"); all_is_good = FALSE; } } else { cl_log(LOG_ERR, "Unknown options"); all_is_good = FALSE; } if(all_is_good == FALSE) { cl_log(LOG_ERR, "Creation of request failed. No message to send"); return -1; } /* send it */ if (crmd_channel == NULL) { cl_log(LOG_ERR, "The IPC connection is not valid, cannot send anything"); return -1; } if(sys_to == NULL) { if (dest_node != NULL) sys_to = CRM_SYSTEM_CRMD; else sys_to = CRM_SYSTEM_DC; } send_ipc_request(crmd_channel, msg_options, msg_data, dest_node, sys_to, crm_system_name, admin_uuid, this_msg_reference); return 1; } ll_cluster_t * do_init(void) { int facility; ll_cluster_t *hb_cluster = NULL; /* docs say only do this once, but in their code they do it every time! */ xmlInitParser (); /* change the logging facility to the one used by heartbeat daemon */ hb_cluster = ll_cluster_new("heartbeat"); cl_log(LOG_INFO, "Switching to Heartbeat logger"); if (( facility = hb_cluster->llc_ops->get_logfacility(hb_cluster)) > 0) { cl_log_set_facility(facility); } admin_uuid = crm_malloc(sizeof(char) * 11); snprintf(admin_uuid, 10, "%d", getpid()); admin_uuid[10] = '\0'; crmd_channel = init_client_ipc_comms(CRM_SYSTEM_CRMD,admin_msg_callback,NULL); if(crmd_channel != NULL) { send_hello_message(crmd_channel, admin_uuid, crm_system_name, "0", "1"); return hb_cluster; } return NULL; } void usage(const char *cmd, int exit_status) { FILE *stream; stream = exit_status ? stderr : stdout; fprintf(stream, "usage: %s [-srkh]" "[-c configure file]\n", cmd); /* fprintf(stream, "\t-d\tsets debug level\n"); */ /* fprintf(stream, "\t-s\tgets daemon status\n"); */ /* fprintf(stream, "\t-r\trestarts daemon\n"); */ /* fprintf(stream, "\t-k\tstops daemon\n"); */ /* fprintf(stream, "\t-h\thelp message\n"); */ fflush(stream); exit(exit_status); } const char *ournode; gboolean admin_msg_callback(IPC_Channel * server, void *private_data) { int lpc = 0; IPC_Message *msg = NULL; gboolean hack_return_good = TRUE; static int received_responses = 0; char *filename; int filename_len = 0; const char *result = NULL; xmlNodePtr options = NULL; xmlNodePtr xml_root_node = NULL; char *buffer = NULL; FNIN(); while (server->ch_status != IPC_DISCONNECT && server->ops->is_message_pending(server) == TRUE) { if (server->ops->recv(server, &msg) != IPC_OK) { perror("Receive failure:"); FNRET(!hack_return_good); } if (msg == NULL) { CRM_NOTE("No message this time"); continue; } lpc++; buffer =(char *) msg->msg_body; CRM_DEBUG("Got xml [text=%s]", buffer); xml_root_node = find_xml_in_ipcmessage(msg, TRUE); if (xml_root_node == NULL) { cl_log(LOG_INFO, "XML in IPC message was not valid... " "discarding."); continue; } else if (validate_crm_message(xml_root_node, crm_system_name, admin_uuid, "response") == FALSE) { cl_log(LOG_INFO, "Message was not a CRM response. Discarding."); continue; } options = find_xml_node(xml_root_node, XML_TAG_OPTIONS); result = xmlGetProp(options, XML_ATTR_RESULT); if(result == NULL || strcmp(result, "ok") == 0) { result = "pass"; } else { result = "fail"; } received_responses++; // do stuff if (this_msg_reference != NULL) { // in testing mode... /* 31 = "test-_.xml" + an_int_as_string + '\0' */ filename_len = 31 + strlen(this_msg_reference); filename = crm_malloc(sizeof(char) * filename_len); sprintf(filename, "%s-%s_%d.xml", result, this_msg_reference, received_responses); filename[filename_len - 1] = '\0'; if (xmlSaveFormatFile(filename, xml_root_node->doc, 1) < 0) { cl_log(LOG_CRIT, "Could not save response %s_%s_%d.xml", this_msg_reference, result, received_responses); } } } if (server->ch_status == IPC_DISCONNECT) { cl_log(LOG_INFO, "admin_msg_callback: received HUP"); FNRET(!hack_return_good); } if (received_responses >= expected_responses) { cl_log(LOG_INFO, "Recieved expected number (%d) of messages from Heartbeat." " Exiting normally.", expected_responses); g_main_quit(mainloop); return !hack_return_good; } FNRET(hack_return_good); } diff --git a/crm/cib/cib.c b/crm/cib/cib.c index 65d2cc9f5d..2aef6981b0 100644 --- a/crm/cib/cib.c +++ b/crm/cib/cib.c @@ -1,315 +1,313 @@ -/* $Id: cib.c,v 1.38 2004/06/01 16:12:49 andrew Exp $ */ +/* $Id: cib.c,v 1.39 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include -#include - #include -#include +#include +#include #include +#include +#include -#include -#include -#include #include #include #include gboolean startCib(const char *filename) { xmlNodePtr cib = readCibXmlFile(filename); if (initializeCib(cib)) { cl_log(LOG_INFO, "CIB Initialization completed successfully"); } else { // free_xml(cib); cl_log(LOG_WARNING, "CIB Initialization failed, " "starting with an empty default."); activateCibXml(createEmptyCib(), filename); } return TRUE; } xmlNodePtr get_cib_copy() { return copy_xml_node_recursive(get_the_CIB()); } /* * The caller should never free the return value */ xmlNodePtr get_object_root(const char *object_type, xmlNodePtr the_root) { const char *node_stack[2]; xmlNodePtr tmp_node = NULL; FNIN(); node_stack[0] = XML_CIB_TAG_CONFIGURATION; node_stack[1] = object_type; if(object_type == NULL || strlen(object_type) == 0) { FNRET(the_root); /* get the whole cib */ } else if(strcmp(object_type, XML_CIB_TAG_STATUS) == 0) { node_stack[0] = XML_CIB_TAG_STATUS; node_stack[1] = NULL; /* these live in a different place */ } tmp_node = find_xml_node_nested(the_root, node_stack, 2); if (tmp_node == NULL) { cl_log(LOG_ERR, "[cib] Section cib[%s[%s]] not present", node_stack[0], node_stack[1]); } FNRET(tmp_node); } xmlNodePtr process_cib_message(xmlNodePtr message, gboolean auto_reply) { xmlNodePtr data; xmlNodePtr reply; enum cib_result result = CIBRES_OK; xmlNodePtr fragment = find_xml_node(message, XML_TAG_FRAGMENT); xmlNodePtr options = find_xml_node(message, XML_TAG_OPTIONS); const char *op = get_xml_attr (message, XML_TAG_OPTIONS, XML_ATTR_OP, TRUE); data = cib_process_request(op, options, fragment, &result); CRM_DEBUG("[cib] operation returned result %d", result); if(auto_reply) { reply = create_reply(message, data); free_xml(data); // TODO: put real result in here set_xml_attr(reply, XML_TAG_OPTIONS, XML_ATTR_RESULT, "ok", TRUE); return reply; } return data; } xmlNodePtr process_cib_request(const char *op, const xmlNodePtr options, const xmlNodePtr fragment) { enum cib_result result = CIBRES_OK; return cib_process_request(op, options, fragment, &result); } xmlNodePtr create_cib_fragment(xmlNodePtr update, const char *section) { gboolean whole_cib = FALSE; xmlNodePtr fragment = create_xml_node(NULL, XML_TAG_FRAGMENT); xmlNodePtr cib = NULL; xmlNodePtr object_root = NULL; char *auto_section = pluralSection(update?update->name:NULL); if(update == NULL) { cl_log(LOG_ERR, "No update to create a fragment for"); crm_free(auto_section); return NULL; } else if(section == NULL) { section = auto_section; } else if(strcmp(auto_section, section) != 0) { cl_log(LOG_ERR, "Values for update (tag=%s) and section (%s)" " were not consistent", update->name, section); crm_free(auto_section); return NULL; } if(strcmp(section, "all")==0 && strcmp(update->name, XML_TAG_CIB)==0) { whole_cib = TRUE; } set_xml_property_copy(fragment, XML_ATTR_SECTION, section); if(whole_cib == FALSE) { cib = createEmptyCib(); object_root = get_object_root(section, cib); xmlAddChildList(object_root, xmlCopyNodeList(update)); } else { cib = xmlCopyNodeList(update); } xmlAddChild(fragment, cib); crm_free(auto_section); return fragment; } char * pluralSection(const char *a_section) { char *a_section_parent = NULL; if (a_section == NULL) { a_section_parent = crm_strdup("all"); } else if(strcmp(a_section, XML_TAG_CIB) == 0) { a_section_parent = crm_strdup("all"); } else if(strcmp(a_section, XML_CIB_TAG_NODE) == 0) { a_section_parent = crm_strdup(XML_CIB_TAG_NODES); } else if(strcmp(a_section, XML_CIB_TAG_STATE) == 0) { a_section_parent = crm_strdup(XML_CIB_TAG_STATUS); } else if(strcmp(a_section, XML_CIB_TAG_CONSTRAINT) == 0) { a_section_parent = crm_strdup(XML_CIB_TAG_CONSTRAINTS); } else if(strcmp(a_section, XML_CIB_TAG_RESOURCE) == 0) { a_section_parent = crm_strdup(XML_CIB_TAG_RESOURCES); } else { cl_log(LOG_ERR, "Unknown section %s", a_section); a_section_parent = crm_strdup("all"); } CRM_DEBUG("Plural is %s", a_section_parent); return a_section_parent; } const char * cib_error2string(enum cib_result return_code) { const char *error_msg = NULL; switch(return_code) { case CIBRES_MISSING_ID: error_msg = "The id field is missing"; break; case CIBRES_MISSING_TYPE: error_msg = "The type field is missing"; break; case CIBRES_MISSING_FIELD: error_msg = "A required field is missing"; break; case CIBRES_OBJTYPE_MISMATCH: error_msg = "CIBRES_OBJTYPE_MISMATCH"; break; case CIBRES_FAILED_EXISTS: error_msg = "The object already exists"; break; case CIBRES_FAILED_NOTEXISTS: error_msg = "The object does not exist"; break; case CIBRES_CORRUPT: error_msg = "The CIB is corrupt"; break; case CIBRES_FAILED_NOOBJECT: error_msg = "The update was empty"; break; case CIBRES_FAILED_NOPARENT: error_msg = "The parent object does not exist"; break; case CIBRES_FAILED_NODECOPY: error_msg = "Failed while copying update"; break; case CIBRES_OTHER: error_msg = "CIBRES_OTHER"; break; case CIBRES_OK: error_msg = "ok"; break; case CIBRES_FAILED: error_msg = "Failed"; break; case CIBRES_FAILED_STALE: error_msg = "Discarded old update"; break; case CIBRES_FAILED_ACTIVATION: error_msg = "Activation Failed"; break; case CIBRES_FAILED_NOSECTION: error_msg = "Required section was missing"; break; case CIBRES_FAILED_NOTSUPPORTED: error_msg = "Supplied information is not supported"; break; } if(error_msg == NULL) { cl_log(LOG_ERR, "Unknown CIB Error %d", return_code); error_msg = ""; } return error_msg; } const char * cib_op2string(enum cib_op operation) { const char *operation_msg = NULL; switch(operation) { case 0: operation_msg = "none"; break; case 1: operation_msg = "add"; break; case 2: operation_msg = "modify"; break; case 3: operation_msg = "delete"; break; case CIB_OP_MAX: operation_msg = "invalid operation"; break; } if(operation_msg == NULL) { cl_log(LOG_ERR, "Unknown CIB operation %d", operation); operation_msg = ""; } return operation_msg; } diff --git a/crm/cib/cibio.c b/crm/cib/cibio.c index cd777508f3..7993954516 100644 --- a/crm/cib/cibio.c +++ b/crm/cib/cibio.c @@ -1,428 +1,427 @@ -/* $Id: cibio.c,v 1.24 2004/06/01 16:12:49 andrew Exp $ */ +/* $Id: cibio.c,v 1.25 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include #include #include -#include -#include #include -#include // for getNow() +#include #include -#include +#include +#include #include const char * local_resource_path[] = { XML_CIB_TAG_STATUS, }; const char * resource_path[] = { XML_CIB_TAG_RESOURCES, }; const char * node_path[] = { XML_CIB_TAG_NODES, }; const char * constraint_path[] = { XML_CIB_TAG_CONSTRAINTS, }; gboolean initialized = FALSE; xmlNodePtr the_cib = NULL; xmlNodePtr node_search = NULL; xmlNodePtr resource_search = NULL; xmlNodePtr constraint_search = NULL; xmlNodePtr status_search = NULL; /* * It is the callers responsibility to free both the new CIB (output) * and the new CIB (input) */ xmlNodePtr createEmptyCib(void) { xmlNodePtr cib_root = NULL, config = NULL, status = NULL; cib_root = create_xml_node(NULL, XML_TAG_CIB); config = create_xml_node(cib_root, XML_CIB_TAG_CONFIGURATION); status = create_xml_node(cib_root, XML_CIB_TAG_STATUS); set_node_tstamp(cib_root); set_node_tstamp(config); set_node_tstamp(status); set_xml_property_copy(cib_root, "version", "1"); set_xml_property_copy(cib_root, "generated", XML_BOOLEAN_TRUE); create_xml_node(config, XML_CIB_TAG_NODES); create_xml_node(config, XML_CIB_TAG_RESOURCES); create_xml_node(config, XML_CIB_TAG_CONSTRAINTS); if (verifyCibXml(cib_root)) { FNRET(cib_root); } cl_log(LOG_CRIT, "The generated CIB did not pass integrity testing!!" " All hope is lost."); FNRET(NULL); } gboolean verifyCibXml(xmlNodePtr cib) { gboolean is_valid = TRUE; xmlNodePtr tmp_node = NULL; FNIN(); if (cib == NULL) { cl_log(LOG_ERR, "XML Buffer was empty."); FNRET(FALSE); } tmp_node = get_object_root(XML_CIB_TAG_NODES, cib); if (tmp_node == NULL) is_valid = FALSE; tmp_node = get_object_root(XML_CIB_TAG_RESOURCES, cib); if (tmp_node == NULL) is_valid = FALSE; tmp_node = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); if (tmp_node == NULL) is_valid = FALSE; tmp_node = get_object_root(XML_CIB_TAG_STATUS, cib); if (tmp_node == NULL) is_valid = FALSE; // more integrity tests FNRET(is_valid); } /* * It is the callers responsibility to free the output of this function */ xmlNodePtr readCibXml(char *buffer) { xmlNodePtr root = string2xml(buffer); if (verifyCibXml(root) == FALSE) { free_xml(root); FNRET(createEmptyCib()); } FNRET(root); } /* * It is the callers responsibility to free the output of this function */ xmlNodePtr readCibXmlFile(const char *filename) { int s_res = -1; struct stat buf; xmlNodePtr root = NULL; FNIN(); if(filename != NULL) { s_res = stat(filename, &buf); } if (s_res == 0) { FILE *cib_file = fopen(filename, "r"); root = file2xml(cib_file); set_xml_property_copy(root, "generated", XML_BOOLEAN_FALSE); fclose(cib_file); } else { cl_log(LOG_WARNING, "Stat of (%s) failed, file does not exist.", CIB_FILENAME); } if (verifyCibXml(root) == FALSE) { free_xml(root); // FNRET(createEmptyCib()); root = NULL; } FNRET(root); } /* * The caller should never free the return value */ xmlNodePtr get_the_CIB(void) { FNIN(); FNRET(the_cib); } gboolean uninitializeCib(void) { xmlNodePtr tmp_cib = the_cib; FNIN(); if(tmp_cib == NULL) { cl_log(LOG_ERR, "The CIB has already been deallocated."); FNRET(FALSE); } initialized = FALSE; the_cib = NULL; node_search = NULL; resource_search = NULL; constraint_search = NULL; status_search = NULL; cl_log(LOG_WARNING, "Deallocating the CIB."); free_xml(tmp_cib); cl_log(LOG_WARNING, "The CIB has been deallocated."); FNRET(TRUE); } /* * This method will not free the old CIB pointer or the new one. * We rely on the caller to have saved a pointer to the old CIB * and to free the old/bad one depending on what is appropriate. */ gboolean initializeCib(xmlNodePtr new_cib) { if (verifyCibXml(new_cib)) { initialized = FALSE; the_cib = new_cib; // update search paths /* not used yet... node_search = get_object_root(XML_CIB_TAG_NODES, new_cib); resource_search = get_object_root(XML_CIB_TAG_RESOURCES, new_cib); constraint_search = get_object_root(XML_CIB_TAG_CONSTRAINTS, new_cib); status_search = get_object_root(XML_CIB_TAG_STATUS, new_cib); */ initialized = TRUE; CRM_NOTE("CIB initialized"); FNRET(TRUE); } else { cl_log(LOG_ERR, "CIB Verification failed"); } FNRET(FALSE); } int moveFile(const char *oldname, const char *newname, gboolean backup, char *ext) { /* move 'oldname' to 'newname' by creating a hard link to it * and then removing the original hard link */ int res = 0; struct stat tmp; int s_res = stat(newname, &tmp); FNIN(); if (s_res >= 0) { if (backup == TRUE) { char backname[1024]; static const char *back_ext = "bak"; if (ext != NULL) back_ext = (char*)ext; snprintf(backname, sizeof(backname)-1, "%s.%s", newname, back_ext); moveFile(newname, backname, FALSE, NULL); } else { res = unlink(newname); if (res < 0) { perror("Could not remove the current backup of Cib"); FNRET(-1); } } } s_res = stat(oldname, &tmp); if (s_res >= 0) { res = link(oldname, newname); if (res < 0) { perror("Could not create backup of current Cib"); FNRET(-2); } res = unlink(oldname); if (res < 0) { perror("Could not unlink the current Cib"); FNRET(-3); } } FNRET(0); } int activateCibBuffer(char *buffer, const char *filename) { int result = -1; xmlNodePtr local_cib = NULL; FNIN(); local_cib = readCibXml(buffer); result = activateCibXml(local_cib, filename); FNRET(result); } /* * This method will free the old CIB pointer on success and the new one * on failure. */ int activateCibXml(xmlNodePtr new_cib, const char *filename) { int error_code = 0; xmlNodePtr saved_cib = get_the_CIB(); const char *filename_bak = CIB_BACKUP; // calculate xmlDocPtr foo; FNIN(); if (initializeCib(new_cib) == TRUE) { int res = moveFile(filename, filename_bak, FALSE, NULL); if (res < 0) { cl_log(LOG_INFO, "Could not make backup of the current Cib " "(code: %d)... aborting update.", res); error_code = -1; } else { cl_log(LOG_INFO, "Writing CIB out to %s", CIB_FILENAME); if (new_cib->doc == NULL) { foo = xmlNewDoc("1.0"); xmlDocSetRootElement(foo, new_cib); xmlSetTreeDoc(new_cib,foo); } time_t now = time(NULL); char *now_str = asctime(localtime(&now)); set_xml_property_copy(new_cib, "last_written",now_str); free(now_str); /* save it. * set arg 3 to 0 to disable line breaks,1 to enable * res == num bytes saved */ res = xmlSaveFormatFile(filename, new_cib->doc, 1); /* for some reason, reading back after saving with * line-breaks doesnt go real well */ cl_log(LOG_INFO, "Saved %d bytes to the Cib as XML", res); if (res < 0) { // assume 0 is good if (moveFile(filename_bak, filename, FALSE, NULL) < -1) { cl_log(LOG_CRIT, "Could not restore the " "backup of the current Cib " "(code: %d)... panic!", res); error_code = -2; // should probably exit here } else if (initializeCib(saved_cib) == FALSE) { // oh we are so dead cl_log(LOG_CRIT, "Could not re-initialize " "with the old CIB. " "Everything is about to go " "pear shaped"); error_code = -3; } else { cl_log(LOG_CRIT, "Update of Cib failed " "(code: %d)... reverted to " "last known valid version", res); error_code = -4; } } } } else { cl_log(LOG_INFO, "Ignoring invalid or NULL Cib"); error_code = -5; } // Make sure memory is cleaned up appropriately if (error_code != 0) { // CRM_DEBUG("Freeing new CIB %p", new_cib); free_xml(new_cib); } else { // CRM_DEBUG("Freeing saved CIB %p", saved_cib); free_xml(saved_cib); } FNRET(error_code); } diff --git a/crm/cib/cibmain.c b/crm/cib/cibmain.c index 182de07377..5896ff8c0a 100644 --- a/crm/cib/cibmain.c +++ b/crm/cib/cibmain.c @@ -1,275 +1,264 @@ -/* $Id: cibmain.c,v 1.20 2004/06/01 16:05:44 andrew Exp $ */ +/* $Id: cibmain.c,v 1.21 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include -#include - -#include - -#include -#include -#include -#include -#include #include -#include -#include -#include -#include +//#include -#include -#include -#include +#include +#include #include +#include +#include +#include +#include #include -#include -#include #include /* #define REALTIME_SUPPORT 0 */ #define PID_FILE WORKING_DIR"/cib.pid" #define DAEMON_LOG LOG_DIR"/cib.log" #define DAEMON_DEBUG LOG_DIR"/cib.debug" GMainLoop* mainloop = NULL; const char* crm_system_name = CRM_SYSTEM_CIB; void usage(const char* cmd, int exit_status); int init_start(void); void cib_shutdown(int nsig); gboolean cib_msg_callback(IPC_Channel *client, gpointer user_data); gboolean process_maincib_message(xmlNodePtr msg, IPC_Channel *sender); #define OPTARGS "skrh" int main(int argc, char ** argv) { int req_comms_restart = FALSE; int req_restart = FALSE; int req_status = FALSE; int req_stop = FALSE; int argerr = 0; int flag; cl_log_set_entity(crm_system_name); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); while ((flag = getopt(argc, argv, OPTARGS)) != EOF) { switch(flag) { case 's': /* Status */ req_status = TRUE; break; case 'k': /* Stop (kill) */ req_stop = TRUE; break; case 'r': /* Restart */ req_restart = TRUE; break; case 'c': /* Restart */ req_comms_restart = TRUE; break; case 'h': /* Help message */ usage(crm_system_name, LSB_EXIT_OK); break; default: ++argerr; break; } } if (optind > argc) { ++argerr; } if (argerr) { usage(crm_system_name,LSB_EXIT_GENERIC); } // read local config file if (req_status){ FNRET(init_status(PID_FILE, crm_system_name)); } if (req_stop){ FNRET(init_stop(PID_FILE)); } if (req_restart) { init_stop(PID_FILE); } FNRET(init_start()); } int init_start(void) { long pid; ll_cluster_t *hb_fd; int facility; IPC_Channel *crm_ch = NULL; #ifdef REALTIME_SUPPORT static int crm_realtime = 1; #endif if ((pid = get_running_pid(PID_FILE, NULL)) > 0) { cl_log(LOG_CRIT, "already running: [pid %ld].", pid); exit(LSB_EXIT_OK); } cl_log(LOG_INFO, "Register PID"); register_pid(PID_FILE, FALSE, cib_shutdown); cl_log_set_logfile(DAEMON_LOG); // if (crm_debug()) { cl_log_set_debugfile(DAEMON_DEBUG); // } hb_fd = ll_cluster_new("heartbeat"); cl_log(LOG_INFO, "Switching to Heartbeat logger"); if ((facility = hb_fd->llc_ops->get_logfacility(hb_fd))>0) { cl_log_set_facility(facility); } if(startCib(CIB_FILENAME) == FALSE){ cl_log(LOG_CRIT, "Cannot start CIB... terminating"); exit(1); } crm_ch = init_client_ipc_comms(CRM_SYSTEM_CRMD, subsystem_input_dispatch, (void*)process_maincib_message); if(crm_ch != NULL) { send_hello_message(crm_ch, "-", CRM_SYSTEM_CIB, "0", "1"); /* Create the mainloop and run it... */ mainloop = g_main_new(FALSE); cl_log(LOG_INFO, "Starting %s", crm_system_name); #ifdef REALTIME_SUPPORT if (crm_realtime == 1) { cl_enable_realtime(); } else if (crm_realtime == 0) { cl_disable_realtime(); } cl_make_realtime(SCHED_RR, 5, 64, 64); #endif g_main_run(mainloop); return_to_orig_privs(); } else { cl_log(LOG_ERR, "Connection to CRM not valid, exiting."); } if (unlink(PID_FILE) == 0) { cl_log(LOG_INFO, "[%s] stopped", crm_system_name); } FNRET(0); } gboolean process_maincib_message(xmlNodePtr msg, IPC_Channel *sender) { const char *op = get_xml_attr (msg, XML_TAG_OPTIONS, XML_ATTR_OP, FALSE); const char *sys_to = xmlGetProp(msg, XML_ATTR_SYSTO); cl_log(LOG_DEBUG, "Processing %s message", op); if(safe_str_eq(xmlGetProp(msg, XML_ATTR_MSGTYPE), XML_ATTR_REQUEST)) { cl_log(LOG_INFO, "Message was a response not a request." " Discarding"); } else if (strcmp(sys_to, CRM_SYSTEM_CIB) == 0 || strcmp(sys_to, CRM_SYSTEM_DCIB) == 0) { xmlNodePtr answer = process_cib_message(msg, TRUE); if (send_xmlipc_message(sender, answer)==FALSE) cl_log(LOG_WARNING, "Cib answer could not be sent"); free_xml(answer); } else { cl_log(LOG_WARNING, "Received a message destined for %s by mistake", sys_to); FNRET(FALSE); } FNRET(TRUE); } void usage(const char* cmd, int exit_status) { FILE* stream; stream = exit_status ? stderr : stdout; fprintf(stream, "usage: %s [-srkh]" "[-c configure file]\n", cmd); /* fprintf(stream, "\t-d\tsets debug level\n"); */ /* fprintf(stream, "\t-s\tgets daemon status\n"); */ /* fprintf(stream, "\t-r\trestarts daemon\n"); */ /* fprintf(stream, "\t-k\tstops daemon\n"); */ /* fprintf(stream, "\t-h\thelp message\n"); */ fflush(stream); exit(exit_status); } void cib_shutdown(int nsig) { static int shuttingdown = 0; CL_SIGNAL(nsig, cib_shutdown); if (!shuttingdown) { shuttingdown = 1; } if (mainloop != NULL && g_main_is_running(mainloop)) { g_main_quit(mainloop); } else { exit(LSB_EXIT_OK); } } diff --git a/crm/cib/cibmessages.c b/crm/cib/cibmessages.c index 25afee803b..0c86a0c96d 100644 --- a/crm/cib/cibmessages.c +++ b/crm/cib/cibmessages.c @@ -1,477 +1,474 @@ -/* $Id: cibmessages.c,v 1.37 2004/06/01 16:12:49 andrew Exp $ */ +/* $Id: cibmessages.c,v 1.38 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include -#include - #include -#include #include -#include -#include +#include #include -#include #include +#include +#include -#include -#include +#include #include +#include #include -FILE *msg_cib_strm = NULL; +FILE *msg_cib_strm = NULL; enum cib_result updateList(xmlNodePtr local_cib, xmlNodePtr update_command, xmlNodePtr failed, int operation, const char *section); xmlNodePtr createCibFragmentAnswer(const char *section, xmlNodePtr failed); gboolean replace_section(const char *section, xmlNodePtr tmpCib, xmlNodePtr command); gboolean check_generation(xmlNodePtr newCib, xmlNodePtr oldCib); gboolean update_results(xmlNodePtr failed, xmlNodePtr target, int operation, int return_code); xmlNodePtr cib_process_request(const char *op, const xmlNodePtr options, const xmlNodePtr fragment, enum cib_result *result) { const char *verbose = NULL; const char *section = NULL; const char *output_section = NULL; xmlNodePtr failed = NULL; xmlNodePtr cib_answer = NULL; gboolean update_the_cib = FALSE; int cib_update_op = CIB_OP_NONE; xmlNodePtr tmpCib; char *new_value = NULL; char *old_value = NULL; int int_value = -1; FNIN(); *result = CIBRES_OK; verbose = xmlGetProp(options, XML_ATTR_VERBOSE); section = xmlGetProp(options, XML_ATTR_FILTER_TYPE); failed = create_xml_node(NULL, XML_TAG_FAILED); #ifdef MSG_LOG if(msg_cib_strm == NULL) { msg_cib_strm = fopen("/tmp/cib.log", "w"); } fprintf(msg_cib_strm, "[Input %s]\t%s\n", op, dump_xml_node(fragment, FALSE)); fflush(msg_cib_strm); #endif cl_log(LOG_DEBUG, "[cib] Processing \"%s\" event", op); if(op == NULL) { *result = CIBRES_FAILED; cl_log(LOG_ERR, "No operation specified\n"); } else if(strcmp("noop", op) == 0) { ; } else if(strcmp(CRM_OP_QUIT, op) == 0) { cl_log(LOG_WARNING, "The CRMd has asked us to exit... complying"); exit(0); } else if (strcmp(CRM_OP_PING, op) == 0) { cib_answer = createPingAnswerFragment(CRM_SYSTEM_CIB, "ok"); } else if (strcmp(CRM_OP_BUMP, op) == 0) { tmpCib = get_cib_copy(); CRM_DEBUG("Handling a %s for section=%s of the cib", CRM_OP_BUMP, section); // modify the timestamp set_node_tstamp(tmpCib); old_value = xmlGetProp(get_the_CIB(), XML_ATTR_GENERATION); if(old_value != NULL) { new_value = (char*)crm_malloc(128*(sizeof(char))); int_value = atoi(old_value); sprintf(new_value, "%d", ++int_value); } else { new_value = crm_strdup("0"); } cl_log(LOG_DEBUG, "Generation %d(%s)->%s", int_value, old_value, new_value); set_xml_property_copy(tmpCib, XML_ATTR_GENERATION, new_value); crm_free(new_value); if(activateCibXml(tmpCib, CIB_FILENAME) >= 0) { verbose = XML_BOOLEAN_TRUE; } else { *result = CIBRES_FAILED; } } else if (strcmp("query", op) == 0) { CRM_DEBUG("Handling a query for section=%s of the cib", section); /* force a pick-up of the relevant section before * returning */ verbose = XML_BOOLEAN_TRUE; } else if (strcmp(CRM_OP_ERASE, op) == 0) { xmlNodePtr new_cib = createEmptyCib(); // Preserve generation counters etc copy_in_properties(new_cib, get_the_CIB()); if (activateCibXml(new_cib, CIB_FILENAME) < 0) { *result = CIBRES_FAILED; } } else if (strcmp(CRM_OP_CREATE, op) == 0) { update_the_cib = TRUE; cib_update_op = CIB_OP_ADD; } else if (strcmp(CRM_OP_UPDATE, op) == 0 || strcmp(CRM_OP_WELCOME, op) == 0 || strcmp(CRM_OP_SHUTDOWN_REQ, op) == 0) { update_the_cib = TRUE; cib_update_op = CIB_OP_MODIFY; } else if (strcmp(CRM_OP_DELETE, op) == 0) { update_the_cib = TRUE; cib_update_op = CIB_OP_DELETE; } else if (strcmp(CRM_OP_REPLACE, op) == 0) { CRM_DEBUG("Replacing section=%s of the cib", section); section = xmlGetProp(fragment, XML_ATTR_SECTION); if (section == NULL || strlen(section) == 0 || strcmp("all", section) == 0) { tmpCib = copy_xml_node_recursive( find_xml_node(fragment, XML_TAG_CIB)); } else { tmpCib = copy_xml_node_recursive(get_the_CIB()); replace_section(section, tmpCib, fragment); } /*if(check_generation(cib_updates, tmpCib) == FALSE) *result = "discarded old update"; else */ if (activateCibXml(tmpCib, CIB_FILENAME) < 0) *result = CIBRES_FAILED; } else { *result = CIBRES_FAILED_NOTSUPPORTED; cl_log(LOG_ERR, "Action [%s] is not supported by the CIB", op); } if (update_the_cib) { tmpCib = copy_xml_node_recursive(get_the_CIB()); section = xmlGetProp(fragment, XML_ATTR_SECTION); CRM_DEBUG("Updating section=%s of the cib (op=%s)", section, op); // should we be doing this? // do logging // make changes to a temp copy then activate if(section == NULL) { cl_log(LOG_ERR, "No section specified in %s", XML_ATTR_FILTER_TYPE); *result = CIBRES_FAILED_NOSECTION; } else if(strcmp("all", section) == 0 && cib_update_op == CIB_OP_DELETE) { // delete /* order is no longer important here */ updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_STATUS); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_CONSTRAINTS); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_RESOURCES); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_NODES); } else if(strcmp("all", section) == 0) { /* order is no longer important here */ updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_NODES); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_RESOURCES); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_CONSTRAINTS); updateList(tmpCib, fragment, failed, cib_update_op, XML_CIB_TAG_STATUS); } else { *result = updateList(tmpCib, fragment, failed, cib_update_op, section); } CRM_NOTE("Activating temporary CIB"); /* if(check_generation(cib_updates, tmpCib) == FALSE) */ /* status = "discarded old update"; */ /* else */ if (activateCibXml(tmpCib, CIB_FILENAME) < 0) { *result = CIBRES_FAILED_ACTIVATION; } else if (failed->children != NULL) { *result = CIBRES_FAILED; } CRM_DEBUG("CIB update status: %d", *result); } output_section = section; if (failed->children != NULL || *result != CIBRES_OK) { cib_answer = createCibFragmentAnswer(NULL /*"all"*/, failed); } else if (verbose != NULL && strcmp(XML_BOOLEAN_TRUE, verbose) == 0) { cib_answer = createCibFragmentAnswer(output_section, failed); } free_xml(failed); #ifdef MSG_LOG fprintf(msg_cib_strm, "[Reply (%s)]\t%s\n", op, dump_xml_node(cib_answer, FALSE)); fflush(msg_cib_strm); #endif FNRET(cib_answer); } gboolean replace_section(const char *section, xmlNodePtr tmpCib, xmlNodePtr fragment) { xmlNodePtr parent = NULL, cib_updates = NULL, new_section = NULL, old_section = NULL; FNIN(); cib_updates = find_xml_node(fragment, XML_TAG_CIB); /* find the old and new versions of the section */ new_section = get_object_root(section, cib_updates); old_section = get_object_root(section, tmpCib); if(old_section == NULL) { cl_log(LOG_ERR, "The CIB is corrupt, cannot replace missing section %s", section); FNRET(FALSE); } else if(new_section == NULL) { cl_log(LOG_ERR, "The CIB is corrupt, cannot set section %s to nothing", section); FNRET(FALSE); } parent = old_section->parent; /* unlink and free the old one */ unlink_xml_node(old_section); free_xml(old_section); /* add the new copy */ add_node_copy(parent, new_section); FNRET(TRUE); } enum cib_result updateList(xmlNodePtr local_cib, xmlNodePtr update_fragment, xmlNodePtr failed, int operation, const char *section) { xmlNodePtr child = NULL; xmlNodePtr this_section = get_object_root(section, local_cib); xmlNodePtr cib_updates = find_xml_node(update_fragment, XML_TAG_CIB); xmlNodePtr xml_section = get_object_root(section, cib_updates); if (section == NULL || xml_section == NULL) { cl_log(LOG_ERR, "Section %s not found in message." " CIB update is corrupt, ignoring.", section); return CIBRES_FAILED_NOSECTION; } if(CIB_OP_NONE > operation > CIB_OP_MAX) { cl_log(LOG_ERR, "Invalid operation on section %s", section); return CIBRES_FAILED; } set_node_tstamp(this_section); child = xml_section->children; while(child != NULL) { if(operation == CIB_OP_DELETE) { update_results(failed, child, operation, delete_cib_object(this_section, child)); } else if(operation == CIB_OP_MODIFY) { update_results(failed, child, operation, update_cib_object(this_section, child, FALSE)); } else { update_results(failed, child, operation, add_cib_object(this_section, child)); } child = child->next; } if (failed->children != NULL) return CIBRES_FAILED; else return CIBRES_OK; } xmlNodePtr createCibFragmentAnswer(const char *section, xmlNodePtr failed) { xmlNodePtr fragment = create_xml_node(NULL, XML_TAG_FRAGMENT); FNIN(); set_xml_property_copy(fragment, XML_ATTR_SECTION, section); if (section == NULL || strlen(section) == 0 || strcmp("all", section) == 0) { add_node_copy(fragment, get_the_CIB()); } else { xmlNodePtr cib = create_xml_node(fragment, XML_TAG_CIB); add_node_copy(cib, get_object_root(section, get_the_CIB())); copy_in_properties(cib, get_the_CIB()); } if (failed != NULL && failed->children != NULL) { add_node_copy(fragment, failed); } FNRET(fragment); } gboolean check_generation(xmlNodePtr newCib, xmlNodePtr oldCib) { char *new_value = xmlGetProp(newCib, XML_ATTR_GENERATION); char *old_value = xmlGetProp(oldCib, XML_ATTR_GENERATION); int int_new_value = -1; int int_old_value = -1; if(old_value != NULL) int_old_value = atoi(old_value); if(new_value != NULL) int_new_value = atoi(new_value); if(int_new_value >= int_old_value) { return TRUE; } else { cl_log(LOG_ERR, "Generation from update (%d) is older than %d", int_new_value, int_old_value); } return FALSE; } gboolean update_results(xmlNodePtr failed, xmlNodePtr target, int operation, int return_code) { gboolean was_error = FALSE; const char *error_msg = NULL; const char *operation_msg = NULL; xmlNodePtr xml_node; FNIN(); if (return_code != CIBRES_OK) { error_msg = cib_error2string(return_code); operation_msg = cib_op2string(operation); xml_node = create_xml_node(failed, XML_FAIL_TAG_CIB); was_error = TRUE; set_xml_property_copy(xml_node, XML_FAILCIB_ATTR_ID, ID(target)); set_xml_property_copy(xml_node, XML_FAILCIB_ATTR_OBJTYPE, TYPE(target)); set_xml_property_copy(xml_node, XML_FAILCIB_ATTR_OP, operation_msg); set_xml_property_copy(xml_node, XML_FAILCIB_ATTR_REASON, error_msg); cl_log(LOG_DEBUG, "Action %s failed: %s (cde=%d)", operation_msg, error_msg, return_code); } FNRET(was_error); } diff --git a/crm/cib/cibprimatives.c b/crm/cib/cibprimatives.c index e6a2478b66..b9b0811b99 100644 --- a/crm/cib/cibprimatives.c +++ b/crm/cib/cibprimatives.c @@ -1,578 +1,574 @@ -/* $Id: cibprimatives.c,v 1.32 2004/06/01 16:04:57 andrew Exp $ */ +/* $Id: cibprimatives.c,v 1.33 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include -#include #include -#include - +#include +#include #include #include - -#include -#include -#include +#include #include /* * In case of confusion, this is the memory management policy for * all functions in this file. * * All add/modify functions use copies of supplied data. * It is therefore appropriate that the callers free the supplied data * at some point after the function has finished. * * All delete functions will handle the freeing of deleted data * but not the function arguments. */ void update_node_state(xmlNodePtr existing_node, xmlNodePtr update); //--- Resource int addResource(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Adding " XML_CIB_TAG_RESOURCE " (%s)...", id); root = get_object_root(XML_CIB_TAG_RESOURCES, cib); return add_cib_object(root, anXmlNode); } xmlNodePtr findResource(xmlNodePtr cib, const char *id) { xmlNodePtr root = NULL, ret = NULL; FNIN(); root = get_object_root(XML_CIB_TAG_RESOURCES, cib); ret = find_entity(root, XML_CIB_TAG_RESOURCE, id, FALSE); FNRET(ret); } int updateResource(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Updating " XML_CIB_TAG_RESOURCE " (%s)...", id); root = get_object_root(XML_CIB_TAG_RESOURCES, cib); return update_cib_object(root, anXmlNode, FALSE); } int delResource(xmlNodePtr cib, xmlNodePtr delete_spec) { const char *id = ID(delete_spec); xmlNodePtr root; if(id == NULL || strlen(id) == 0) { return CIBRES_MISSING_ID; } CRM_DEBUG("Deleting " XML_CIB_TAG_RESOURCE " (%s)...", id); root = get_object_root(XML_CIB_TAG_RESOURCES, cib); return delete_cib_object(root, delete_spec); } //--- Constraint int addConstraint(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Adding " XML_CIB_TAG_CONSTRAINT " (%s)...", id); root = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); return add_cib_object(root, anXmlNode); } xmlNodePtr findConstraint(xmlNodePtr cib, const char *id) { xmlNodePtr root = NULL, ret = NULL; FNIN(); root = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); ret = find_entity(root, XML_CIB_TAG_CONSTRAINT, id, FALSE); FNRET(ret); } int updateConstraint(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Updating " XML_CIB_TAG_CONSTRAINT " (%s)...", id); root = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); return update_cib_object(root, anXmlNode, FALSE); } int delConstraint(xmlNodePtr cib, xmlNodePtr delete_spec) { const char *id = ID(delete_spec); xmlNodePtr root; if(id == NULL || strlen(id) == 0) { return CIBRES_MISSING_ID; } CRM_DEBUG("Deleting " XML_CIB_TAG_CONSTRAINT " (%s)...", id); root = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); return delete_cib_object(root, delete_spec); } //--- HaNode int addHaNode(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Adding " XML_CIB_TAG_NODE " (%s)...", id); root = get_object_root(XML_CIB_TAG_NODES, cib); return add_cib_object(root, anXmlNode); } xmlNodePtr findHaNode(xmlNodePtr cib, const char *id) { xmlNodePtr root = NULL, ret = NULL; FNIN(); root = get_object_root(XML_CIB_TAG_NODES, cib); ret = find_entity(root, XML_CIB_TAG_NODE, id, FALSE); FNRET(ret); } int updateHaNode(xmlNodePtr cib, cibHaNode *anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Updating " XML_CIB_TAG_NODE " (%s)...", id); root = get_object_root(XML_CIB_TAG_NODES, cib); return update_cib_object(root, anXmlNode, FALSE); } int delHaNode(xmlNodePtr cib, xmlNodePtr delete_spec) { const char *id = ID(delete_spec); xmlNodePtr root; if(id == NULL || strlen(id) == 0) { return CIBRES_MISSING_ID; } CRM_DEBUG("Deleting " XML_CIB_TAG_NODE " (%s)...", id); root = get_object_root(XML_CIB_TAG_CONSTRAINTS, cib); return delete_cib_object(root, delete_spec); } //--- Status int addStatus(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Adding " XML_CIB_TAG_NODE " (%s)...", id); root = get_object_root(XML_CIB_TAG_STATUS, cib); return add_cib_object(root, anXmlNode); } xmlNodePtr findStatus(xmlNodePtr cib, const char *id) { xmlNodePtr root = NULL, ret = NULL; root = get_object_root(XML_CIB_TAG_STATUS, cib); ret = find_entity(root, XML_CIB_TAG_STATE, id, FALSE); FNRET(ret); } int updateStatus(xmlNodePtr cib, xmlNodePtr anXmlNode) { const char *id = ID(anXmlNode); xmlNodePtr root; if (id == NULL || strlen(id) < 1) { return CIBRES_MISSING_ID; } CRM_DEBUG("Updating " XML_CIB_TAG_NODE " (%s)...", id); root = get_object_root(XML_CIB_TAG_STATUS, cib); return update_cib_object(root, anXmlNode, FALSE); } int delStatus(xmlNodePtr cib, xmlNodePtr delete_spec) { const char *id = ID(delete_spec); xmlNodePtr root; if(id == NULL || strlen(id) == 0) { return CIBRES_MISSING_ID; } CRM_DEBUG("Deleting " XML_CIB_TAG_STATE " (%s)...", id); root = get_object_root(XML_CIB_TAG_STATUS, cib); return delete_cib_object(root, delete_spec); } int delete_cib_object(xmlNodePtr parent, xmlNodePtr delete_spec) { const char *object_name = NULL; const char *object_id = NULL; xmlNodePtr equiv_node = NULL; xmlNodePtr children = NULL; int result = CIBRES_OK; if(delete_spec == NULL) { return CIBRES_FAILED_NOOBJECT; } else if(parent == NULL) { return CIBRES_FAILED_NOPARENT; } object_name = delete_spec->name; object_id = xmlGetProp(delete_spec, XML_ATTR_ID); children = delete_spec->children; if(object_id == NULL) { // placeholder object equiv_node = find_xml_node(parent, object_name); } else { equiv_node = find_entity(parent, object_name, object_id, FALSE); } if(equiv_node == NULL) { return CIBRES_FAILED_NOTEXISTS; } else if(children == NULL) { // only leaves are deleted unlink_xml_node(equiv_node); free_xml(equiv_node); } else { while(children != NULL) { int tmp_result = delete_cib_object(equiv_node, children); // only the first error is likely to be interesting if(tmp_result != CIBRES_OK && result == CIBRES_OK) { result = tmp_result; } children = children->next; } } return result; } int add_cib_object(xmlNodePtr parent, xmlNodePtr new_obj) { const char *object_name = NULL; const char *object_id = NULL; xmlNodePtr equiv_node = NULL; xmlNodePtr children = NULL; if(new_obj == NULL) { return CIBRES_FAILED_NOOBJECT; } else if(parent == NULL) { return CIBRES_FAILED_NOPARENT; } object_name = new_obj->name; object_id = xmlGetProp(new_obj, XML_ATTR_ID); children = new_obj->children; if(object_id == NULL) { // placeholder object equiv_node = find_xml_node(parent, object_name); } else { equiv_node = find_entity(parent, object_name, object_id, FALSE); } if(equiv_node != NULL) { return CIBRES_FAILED_EXISTS; } else if(add_node_copy(parent, new_obj) == NULL) { return CIBRES_FAILED_NODECOPY; } return CIBRES_OK; } int update_cib_object(xmlNodePtr parent, xmlNodePtr new_obj, gboolean force) { const char *object_name = NULL; const char *object_id = NULL; xmlNodePtr equiv_node = NULL; xmlNodePtr children = NULL; int result = CIBRES_OK; if(new_obj == NULL) { return CIBRES_FAILED_NOOBJECT; } else if(parent == NULL) { return CIBRES_FAILED_NOPARENT; } object_name = new_obj->name; object_id = xmlGetProp(new_obj, XML_ATTR_ID); children = new_obj->children; if(object_id == NULL) { // placeholder object equiv_node = find_xml_node(parent, object_name); } else { equiv_node = find_entity(parent, object_name, object_id, FALSE); } if(equiv_node != NULL) { if(force == FALSE) { const char *ts_existing = NULL; const char *ts_new = NULL; /* default to false? * * that would mean every node would have to * carry a timestamp */ gboolean is_update = TRUE; ts_existing = TSTAMP(equiv_node); ts_new = TSTAMP(new_obj); if(ts_new != NULL && ts_existing != NULL) { is_update = (strcmp(ts_new, ts_existing) > 0); } if(is_update == FALSE) { cl_log(LOG_ERR, "Ignoring old update to <%s id=\"%s\">" "(%s vs. %s)", object_name, object_id, ts_new, ts_existing); return CIBRES_FAILED_STALE; } } if(safe_str_eq(XML_CIB_TAG_STATE, object_name)){ update_node_state(equiv_node, new_obj); } else { copy_in_properties(equiv_node, new_obj); } while(children != NULL) { int tmp_result = update_cib_object(equiv_node, children,force); // only the first error is likely to be interesting if(tmp_result != CIBRES_OK && result == CIBRES_OK) { result = tmp_result; } children = children->next; } } else if(add_node_copy(parent, new_obj) == NULL) { return CIBRES_FAILED_NODECOPY; } return result; } void update_node_state(xmlNodePtr target, xmlNodePtr update) { const char *source = NULL; const char *replace = NULL; xmlAttrPtr prop_iter = NULL; gboolean any_updates = FALSE; gboolean clear_stonith = FALSE; gboolean clear_shutdown = FALSE; FNIN(); prop_iter = update->properties; while(prop_iter != NULL) { const char *local_prop_name = prop_iter->name; const char *local_prop_value = xmlGetProp(update, local_prop_name); if(local_prop_name == NULL) { // error } else if(strcmp(local_prop_name, XML_ATTR_ID) == 0) { } else if(strcmp(local_prop_name, XML_ATTR_TSTAMP) == 0) { } else if(strcmp(local_prop_name, XML_CIB_ATTR_CLEAR_SHUTDOWN) == 0) { clear_shutdown = TRUE; } else if(strcmp(local_prop_name, XML_CIB_ATTR_CLEAR_STONITH) == 0) { clear_stonith = TRUE; clear_shutdown = TRUE; } else if(strcmp(local_prop_name, "replace") == 0) { replace = local_prop_value; any_updates = TRUE; } else if(strcmp(local_prop_name, "source") == 0) { source = local_prop_value; } else { any_updates = TRUE; set_xml_property_copy(target, local_prop_name, local_prop_value); } prop_iter = prop_iter->next; } if(clear_shutdown) { // unset XML_CIB_ATTR_SHUTDOWN CRM_DEBUG("Clearing %s", XML_CIB_ATTR_SHUTDOWN); xmlUnsetProp(target, XML_CIB_ATTR_SHUTDOWN); any_updates = TRUE; } if(clear_stonith) { // unset XML_CIB_ATTR_STONITH CRM_DEBUG("Clearing %s", XML_CIB_ATTR_STONITH); xmlUnsetProp(target, XML_CIB_ATTR_STONITH); any_updates = TRUE; } if(replace != NULL) { xmlNodePtr lrm = find_xml_node(target, replace); xmlUnlinkNode(lrm); lrm->doc = NULL; free_xml(lrm); } if(any_updates) { set_node_tstamp(target); set_xml_property_copy(target, "source", source); } } diff --git a/crm/pengine/pengine.c b/crm/pengine/pengine.c index 9d946dd5d6..45ea8a406b 100755 --- a/crm/pengine/pengine.c +++ b/crm/pengine/pengine.c @@ -1,2122 +1,2140 @@ +/* $Id: pengine.c,v 1.30 2004/06/02 11:48:10 andrew Exp $ */ +/* + * 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 + */ #include -#include -#include -#include -#include #include +#include +#include +#include + #include #include #include #include xmlNodePtr do_calculations(xmlNodePtr cib_object); gboolean process_pe_message(xmlNodePtr msg, IPC_Channel *sender); void color_resource(resource_t *lh_resource, GSListPtr *colors, GSListPtr resources); gboolean create_rsc_to_rsc(const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh); gboolean create_ordering(const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh, GSListPtr *action_constraints); gboolean unpack_constraints(xmlNodePtr xml_constraints, GSListPtr nodes, GSListPtr resources, GSListPtr *node_constraints, GSListPtr *action_constraints); gboolean unpack_resources(xmlNodePtr xml_resources, GSListPtr *resources, GSListPtr *actions, GSListPtr *action_cons, GSListPtr all_nodes); gboolean unpack_nodes(xmlNodePtr xml_nodes, GSListPtr *nodes); gboolean unpack_status(xmlNodePtr status, GSListPtr nodes, GSListPtr rsc_list, GSListPtr *node_constraints); gboolean apply_node_constraints(GSListPtr constraints, GSListPtr resources, GSListPtr nodes); gboolean is_active(rsc_to_node_t *cons); gboolean choose_node_from_list(GSListPtr colors, color_t *color, GSListPtr nodes); gboolean unpack_rsc_to_attr(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr node_list, GSListPtr *node_constraints); gboolean unpack_rsc_to_node(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr node_list, GSListPtr *node_constraints); gboolean unpack_rsc_to_rsc(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr *action_constraints); gboolean choose_color(resource_t *lh_resource); gboolean strict_postproc(rsc_to_rsc_t *constraint, color_t *local_color, color_t *other_color, GSListPtr *colors, GSListPtr resources); gboolean strict_preproc(rsc_to_rsc_t *constraint, color_t *local_color, color_t *other_color, GSListPtr *colors, GSListPtr resources); gboolean update_node_weight(rsc_to_node_t *cons, char *id, GSListPtr nodes); gboolean process_node_lrm_state(node_t *node, xmlNodePtr lrm_state, GSListPtr rsc_list, GSListPtr nodes, GSListPtr *node_constraints); GSListPtr match_attrs(xmlNodePtr attr_exp, GSListPtr node_list); gboolean update_runnable(GSListPtr actions); GSListPtr create_action_set(action_t *action); color_t *no_color = NULL; int max_valid_nodes = 0; int order_id = 1; int action_id = 1; gboolean pe_debug = FALSE; gboolean pe_debug_saved = FALSE; /* * Unpack everything * At the end you'll have: * - A list of nodes * - A list of resources (each with any dependancies on other resources) * - A list of constraints between resources and nodes * - A list of constraints between start/stop actions * - A list of nodes that need to be stonith'd * - A list of nodes that need to be shutdown * - A list of the possible stop/start actions (without dependancies) */ gboolean stage0(xmlNodePtr cib, GSListPtr *resources, GSListPtr *nodes, GSListPtr *node_constraints, GSListPtr *actions, GSListPtr *action_constraints, GSListPtr *stonith_list, GSListPtr *shutdown_list) { int lpc; xmlNodePtr cib_nodes = get_object_root( XML_CIB_TAG_NODES, cib); xmlNodePtr cib_status = get_object_root( XML_CIB_TAG_STATUS, cib); xmlNodePtr cib_resources = get_object_root( XML_CIB_TAG_RESOURCES, cib); xmlNodePtr cib_constraints = get_object_root( XML_CIB_TAG_CONSTRAINTS, cib); /* reset remaining global variables */ max_valid_nodes = 0; order_id = 1; action_id = 1; unpack_nodes(safe_val(NULL, cib_nodes, children), nodes); unpack_resources(safe_val(NULL, cib_resources, children), resources, actions, action_constraints, *nodes); unpack_status(safe_val(NULL, cib_status, children), *nodes, *resources, node_constraints); unpack_constraints(safe_val(NULL, cib_constraints, children), *nodes, *resources, node_constraints, action_constraints); slist_iter( node, node_t, *nodes, lpc, if(node->details->shutdown) { *shutdown_list = g_slist_append(*shutdown_list, node); pdebug("Scheduling Node %s for shutdown", node->details->id); } else if(node->details->unclean) { *stonith_list = g_slist_append(*stonith_list, node); pdebug("Scheduling Node %s for STONITH", node->details->id); } ); return TRUE; } /* * Count how many valid nodes we have (so we know the maximum number of * colors we can resolve). * * Apply node constraints (ie. filter the "allowed_nodes" part of resources */ gboolean stage1(GSListPtr node_constraints, GSListPtr nodes, GSListPtr resources) { int lpc = 0; slist_iter( node, node_t, nodes, lpc, if(node == NULL) { // error } else if(node->weight >= 0.0 && node->details->online && node->details->type == node_member) { max_valid_nodes++; } ); apply_node_constraints(node_constraints, nodes, resources); return TRUE; } /* * Choose a color for all resources from highest priority and XML_STRENGTH_VAL_MUST * dependancies to lowest, creating new colors as necessary (returned * as "colors"). * * Some nodes may be colored as a "no_color" meaning that it was unresolvable * given the current node stati and constraints. */ gboolean stage2(GSListPtr sorted_rscs, GSListPtr sorted_nodes, GSListPtr *colors) { int lpc; color_t *current_color = NULL; // Set initial color // Set color.candidate_nodes = all active nodes if(no_color != NULL) { crm_free(no_color->details); crm_free(no_color); } no_color = create_color(NULL, NULL, sorted_rscs); current_color = create_color(colors, sorted_nodes, sorted_rscs); // Set resource.color = color (all resources) // Set resource.provisional = TRUE (all resources) slist_iter( this_resource, resource_t, sorted_rscs, lpc, this_resource->color = current_color; this_resource->provisional = TRUE; ); pdebug("initialized resources to default color"); // Take (next) highest resource slist_iter( lh_resource, resource_t, sorted_rscs, lpc, // if resource.provisional == FALSE, repeat if(lh_resource->provisional == FALSE) { // already processed this resource continue; } color_resource(lh_resource, colors, sorted_rscs); // next resource ); return TRUE; } /* * not sure if this is a good idea or not, but eventually we might like * to utilize as many nodes as possible... and this might be a convienient * hook */ gboolean stage3(GSListPtr colors) { // not sure if this is a good idea or not if(g_slist_length(colors) > max_valid_nodes) { // we need to consolidate some } else if(g_slist_length(colors) < max_valid_nodes) { // we can create a few more } return TRUE; } #define color_n_nodes color_n->details->candidate_nodes #define color_n_plus_1_nodes color_n_plus_1->details->candidate_nodes /* * Choose a node for each (if possible) color */ gboolean stage4(GSListPtr colors) { int lpc = 0; color_t *color_n = NULL; color_t *color_n_plus_1 = NULL; for(lpc = 0; lpc < g_slist_length(colors); lpc++) { color_n = color_n_plus_1; color_n_plus_1 = (color_t*)g_slist_nth_data(colors, lpc); pdebug_action( print_color("Choose node for...", color_n, FALSE)); if(color_n == NULL) { continue; } GSListPtr xor = node_list_xor(color_n_nodes, color_n_plus_1_nodes); GSListPtr minus = node_list_minus(color_n_nodes, color_n_plus_1_nodes); if(g_slist_length(xor) == 0 || g_slist_length(minus) == 0) { pdebug("Choose any node from our list"); choose_node_from_list(colors, color_n, color_n_nodes); } else { pdebug("Choose a node not in n+1"); choose_node_from_list(colors, color_n, minus); } pe_free_shallow(xor); pe_free_shallow(minus); } // choose last color if(color_n_plus_1 != NULL) { pdebug_action(print_color("Choose node for last color...", color_n_plus_1, FALSE)); choose_node_from_list(colors, color_n_plus_1, color_n_plus_1_nodes); } pdebug("done %s", __FUNCTION__); return TRUE; } /* * Attach nodes to the actions that need to be taken * * Mark actions XML_LRM_ATTR_OPTIONAL if possible (Ie. if the start and stop are * for the same node) * * Mark unrunnable actions */ gboolean stage5(GSListPtr resources) { pdebug("filling in the nodes to perform the actions on"); int lpc = 0; slist_iter( rsc, resource_t, resources, lpc, print_resource("Processing", rsc, FALSE); if(safe_val(NULL, rsc, stop) == NULL || safe_val(NULL, rsc, start) == NULL) { // error cl_log(LOG_ERR, "Either start action (%p) or" " stop action (%p) were not defined", safe_val(NULL, rsc, stop), safe_val(NULL, rsc, start)); continue; } if(safe_val4(NULL, rsc, color, details, chosen_node) == NULL){ rsc->stop->node = safe_val(NULL, rsc, cur_node); rsc->start->node = NULL; cl_log(LOG_DEBUG, "Stop resource %s (%s)", safe_val(NULL, rsc, id), safe_val5(NULL, rsc, stop, node, details, id)); pdebug_action( print_action( CRMD_STATE_ACTIVE, rsc->stop, FALSE)); } else if(safe_str_eq(safe_val4(NULL, rsc, cur_node, details, id), safe_val6(NULL, rsc, color ,details, chosen_node, details, id))){ cl_log(LOG_DEBUG, "No change for Resource %s (%s)", safe_val(NULL, rsc, id), safe_val4(NULL, rsc, cur_node, details, id)); rsc->stop->optional = TRUE; rsc->start->optional = TRUE; rsc->stop->node = safe_val(NULL, rsc, cur_node); rsc->start->node = safe_val4(NULL, rsc, color, details, chosen_node); } else if(safe_val4(NULL, rsc, cur_node, details, id) == NULL) { rsc->stop->optional = TRUE; rsc->start->node = safe_val4(NULL, rsc, color, details, chosen_node); cl_log(LOG_DEBUG, "Start resource %s (%s)", safe_val(NULL, rsc, id), safe_val5(NULL, rsc, start, node, details, id)); } else { rsc->stop->node = safe_val(NULL, rsc, cur_node); rsc->start->node = safe_val4(NULL, rsc, color, details, chosen_node); cl_log(LOG_DEBUG, "Move resource %s (%s -> %s)", safe_val(NULL, rsc, id), safe_val5(NULL, rsc, stop, node, details, id), safe_val5(NULL, rsc, start, node, details, id)); } if(rsc->stop->node != NULL) { rsc->stop->runnable = TRUE; } if(rsc->start->node != NULL) { rsc->start->runnable = TRUE; } ); return TRUE; } /* * Create dependacies for stonith and shutdown operations */ gboolean stage6(GSListPtr *actions, GSListPtr *action_constraints, GSListPtr stonith_nodes, GSListPtr shutdown_nodes) { int lpc = 0; int llpc = 0; slist_iter( node, node_t, shutdown_nodes, lpc, action_t *down_node = action_new(action_id++, NULL, shutdown_crm); down_node->node = node; down_node->runnable = TRUE; *actions = g_slist_append(*actions, down_node); slist_iter( rsc, resource_t, node->details->running_rsc, llpc, order_constraint_t *order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); /* stop resources before shutdown */ order->id = order_id++; order->lh_action = rsc->stop; order->rh_action = down_node; order->strength = must; pdebug_action( print_action("LH (Shutdown)", order->lh_action, FALSE)); pdebug_action( print_action("RH (Shutdown)", order->rh_action, FALSE)); *action_constraints = g_slist_append(*action_constraints, order); ); ); slist_iter( node, node_t, stonith_nodes, lpc, action_t *stonith_node = action_new(action_id++, NULL, stonith_op); stonith_node->node = node; stonith_node->runnable = TRUE; *actions = g_slist_append(*actions, stonith_node); slist_iter( rsc, resource_t, node->details->running_rsc, llpc, order_constraint_t *order = NULL; #if 1 /* * Mark the stop as irrelevant * * Possibly one day failed actions wont terminate * the transition, but not yet */ rsc->stop->discard = TRUE; #else rsc->stop->optional = TRUE; #endif /* try stopping the resource before stonithing the node * * if the stop succeeds, the transitioner can then * decided if stonith is needed */ order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); order->lh_action = rsc->stop; order->rh_action = stonith_node; order->id = order_id++; order->strength = must; *action_constraints = g_slist_append(*action_constraints, order); /* stonith before start */ order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); order->id = order_id++; order->lh_action = stonith_node; order->rh_action = rsc->start; order->strength = must; *action_constraints = g_slist_append(*action_constraints, order); ); ); return TRUE; } /* * Determin the sets of independant actions and the correct order for the * actions in each set. * * Mark dependancies of un-runnable actions un-runnable * */ gboolean stage7(GSListPtr resources, GSListPtr actions, GSListPtr action_constraints, GSListPtr *action_sets) { int lpc; /* for(lpc = 0; lpc < g_slist_length(action_constraints); lpc++) { order_constraint_t *order = (order_constraint_t*) g_slist_nth_data(action_constraints, lpc); */ slist_iter( order, order_constraint_t, action_constraints, lpc, pdebug("Processing %d -> %d", order->lh_action->id, order->rh_action->id); pdebug_action( print_action("LH (stage7)", order->lh_action, FALSE)); pdebug_action( print_action("RH (stage7)", order->rh_action, FALSE)); action_wrapper_t *wrapper = (action_wrapper_t*) crm_malloc(sizeof(action_wrapper_t)); wrapper->action = order->rh_action; wrapper->strength = order->strength; GSListPtr list = order->lh_action->actions_after; list = g_slist_append(list, wrapper); order->lh_action->actions_after = list; wrapper = (action_wrapper_t*) crm_malloc(sizeof(action_wrapper_t)); wrapper->action = order->lh_action; wrapper->strength = order->strength; list = order->rh_action->actions_before; list = g_slist_append(list, wrapper); order->rh_action->actions_before = list; ); // } update_runnable(actions); slist_iter( rsc, resource_t, resources, lpc, GSListPtr action_set = NULL; /* any non-essential stop actions will be marked redundant by * during stage6 */ action_set = create_action_set(rsc->start); if(action_set != NULL) { pdebug("Created action set for %s->start", rsc->id); *action_sets = g_slist_append(*action_sets, action_set); } else { pdebug("No actions resulting from %s->start", rsc->id); } ); return TRUE; } /* * Create a dependancy graph to send to the transitioner (via the CRMd) */ gboolean stage8(GSListPtr action_sets, xmlNodePtr *graph) { int lpc = 0; xmlNodePtr xml_action_set = NULL; *graph = create_xml_node(NULL, "transition_graph"); /* errors... slist_iter(action, action_t, action_list, lpc, if(action->optional == FALSE && action->runnable == FALSE) { print_action("Ignoring", action, TRUE); } ); */ int lpc2; slist_iter(action_set, GSList, action_sets, lpc, pdebug("Processing Action Set %d", lpc); xml_action_set = create_xml_node(NULL, "actions"); set_xml_property_copy( xml_action_set, XML_ATTR_ID, crm_itoa(lpc)); slist_iter(action, action_t, action_set, lpc2, xmlNodePtr xml_action = action2xml(action); xmlAddChild(xml_action_set, xml_action); ) xmlAddChild(*graph, xml_action_set); ); xml_message_debug(*graph, "created action list"); return TRUE; } /* * Print a nice human readable high-level summary of what we're going to do */ gboolean summary(GSListPtr resources) { int lpc = 0; slist_iter( rsc, resource_t, resources, lpc, char *rsc_id = safe_val(NULL, rsc, id); char *node_id = safe_val4(NULL, rsc, cur_node, details, id); char *new_node_id = safe_val6(NULL, rsc, color, details, chosen_node, details, id); if(rsc->runnable == FALSE) { cl_log(LOG_ERR, "Resource %s was not runnable", rsc_id); if(node_id != NULL) { cl_log(LOG_WARNING, "Stopping Resource (%s) on node %s", rsc_id, node_id); } } else if(safe_val4(NULL, rsc, color, details, chosen_node) == NULL) { cl_log(LOG_ERR, "Could not allocate Resource %s", rsc_id); pdebug_action(print_resource("Could not allocate", rsc, TRUE)); if(node_id != NULL) { cl_log(LOG_WARNING, "Stopping Resource (%s) on node %s", rsc_id, node_id); } } else if(safe_str_eq(node_id, new_node_id)){ cl_log(LOG_DEBUG, "No change for Resource %s (%s)", rsc_id, safe_val4(NULL, rsc, cur_node, details, id)); } else if(node_id == NULL) { cl_log(LOG_INFO, "Starting Resource %s on %s", rsc_id, new_node_id); } else { cl_log(LOG_INFO, "Moving Resource %s from %s to %s", rsc_id, node_id, new_node_id); } ); return TRUE; } gboolean choose_node_from_list(GSListPtr colors, color_t *color, GSListPtr nodes) { int lpc; /* 1. Sort by weight 2. color.chosen_node = highest wieghted node 3. remove color.chosen_node from all other colors */ nodes = g_slist_sort(nodes, sort_node_weight); color->details->chosen_node = node_copy((node_t*)g_slist_nth_data(nodes, 0)); if(color->details->chosen_node == NULL) { cl_log(LOG_ERR, "Could not allocate a node for color %d", color->id); return FALSE; } slist_iter( color_n, color_t, colors, lpc, node_t *other_node = pe_find_node(color_n->details->candidate_nodes, color->details->chosen_node->details->id); if(color_n != color) { color_n->details->candidate_nodes = g_slist_remove(color_n->details->candidate_nodes, other_node); // crm_free(other_node); } ); return TRUE; } gboolean unpack_nodes(xmlNodePtr xml_nodes, GSListPtr *nodes) { pdebug("Begining unpack... %s", __FUNCTION__); while(xml_nodes != NULL) { xmlNodePtr xml_obj = xml_nodes; xmlNodePtr attrs = xml_obj->children; const char *id = xmlGetProp(xml_obj, XML_ATTR_ID); const char *type = xmlGetProp(xml_obj, XML_ATTR_TYPE); pdebug("Processing node %s", id); if(attrs != NULL) { attrs = attrs->children; } xml_nodes = xml_nodes->next; if(id == NULL) { cl_log(LOG_ERR, "Must specify id tag in "); continue; } if(type == NULL) { cl_log(LOG_ERR, "Must specify type tag in "); continue; } node_t *new_node = crm_malloc(sizeof(node_t)); new_node->weight = 1.0; new_node->fixed = FALSE; new_node->details = (struct node_shared_s*) crm_malloc(sizeof(struct node_shared_s)); new_node->details->online = FALSE; new_node->details->unclean = FALSE; new_node->details->shutdown = FALSE; new_node->details->running_rsc = NULL; new_node->details->id = crm_strdup(id); new_node->details->attrs = g_hash_table_new(g_str_hash, g_str_equal); new_node->details->type = node_ping; if(safe_str_eq(type, "node")) { new_node->details->type = node_member; } while(attrs != NULL){ const char *name = xmlGetProp( attrs, XML_NVPAIR_ATTR_NAME); const char *value = xmlGetProp( attrs, XML_NVPAIR_ATTR_VALUE); if(name != NULL && value != NULL) { g_hash_table_insert(new_node->details->attrs, crm_strdup(name), crm_strdup(value)); } attrs = attrs->next; } pdebug("Done with node %s", xmlGetProp(xml_obj, "uname")); pdebug_action(print_node("Added", new_node, FALSE)); *nodes = g_slist_append(*nodes, new_node); } *nodes = g_slist_sort(*nodes, sort_node_weight); return TRUE; } gboolean unpack_resources(xmlNodePtr xml_resources, GSListPtr *resources, GSListPtr *actions, GSListPtr *action_cons, GSListPtr all_nodes) { pdebug("Begining unpack... %s", __FUNCTION__); while(xml_resources != NULL) { xmlNodePtr xml_obj = xml_resources; const char *id = xmlGetProp(xml_obj, XML_ATTR_ID); const char *priority = xmlGetProp(xml_obj, XML_CIB_ATTR_PRIORITY); float priority_f = atof(priority); xml_resources = xml_resources->next; pdebug("Processing resource..."); if(id == NULL) { cl_log(LOG_ERR, "Must specify id tag in "); continue; } resource_t *new_rsc = crm_malloc(sizeof(resource_t)); new_rsc->xml = xml_obj; new_rsc->priority = priority_f; new_rsc->candidate_colors = NULL; new_rsc->color = NULL; new_rsc->runnable = TRUE; new_rsc->provisional = TRUE; new_rsc->allowed_nodes = node_list_dup(all_nodes); new_rsc->rsc_cons = NULL; new_rsc->node_cons = NULL; new_rsc->id = crm_strdup(id); new_rsc->cur_node = NULL; action_t *action_stop = action_new(action_id++, new_rsc, stop_rsc); action_t *action_start = action_new(action_id++, new_rsc, start_rsc); new_rsc->stop = action_stop; *actions = g_slist_append(*actions, action_stop); new_rsc->start = action_start; *actions = g_slist_append(*actions, action_start); order_constraint_t *order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); order->id = order_id++; order->lh_action = action_stop; order->rh_action = action_start; order->strength = startstop; *action_cons = g_slist_append(*action_cons, order); pdebug_action(print_resource("Added", new_rsc, FALSE)); *resources = g_slist_append(*resources, new_rsc); } *resources = g_slist_sort(*resources, sort_rsc_priority); return TRUE; } gboolean unpack_constraints(xmlNodePtr xml_constraints, GSListPtr nodes, GSListPtr resources, GSListPtr *node_constraints, GSListPtr *action_constraints) { pdebug("Begining unpack... %s", __FUNCTION__); while(xml_constraints != NULL) { const char *id = xmlGetProp(xml_constraints, XML_ATTR_ID); xmlNodePtr xml_obj = xml_constraints; xml_constraints = xml_constraints->next; if(id == NULL) { cl_log(LOG_ERR, "Constraint must have an id"); continue; } pdebug("Processing constraint %s %s", xml_obj->name,id); if(safe_str_eq("rsc_to_rsc", xml_obj->name)) { unpack_rsc_to_rsc(xml_obj, resources, action_constraints); } else if(safe_str_eq("rsc_to_node", xml_obj->name)) { unpack_rsc_to_node(xml_obj, resources, nodes, node_constraints); } else if(safe_str_eq("rsc_to_attr", xml_obj->name)) { unpack_rsc_to_attr(xml_obj, resources, nodes, node_constraints); } else { cl_log(LOG_ERR, "Unsupported constraint type: %s", xml_obj->name); } } return TRUE; } gboolean apply_node_constraints(GSListPtr constraints, GSListPtr resources, GSListPtr nodes) { pdebug("Applying constraints... %s", __FUNCTION__); int lpc = 0; slist_iter( cons, rsc_to_node_t, constraints, lpc, pdebug_action(print_rsc_to_node("Applying", cons, FALSE)); // take "lifetime" into account if(cons == NULL) { cl_log(LOG_ERR, "Constraint (%d) is NULL", lpc); continue; } else if(is_active(cons) == FALSE) { cl_log(LOG_INFO, "Constraint (%d) is not active", lpc); // warning continue; } resource_t *rsc_lh = cons->rsc_lh; if(rsc_lh == NULL) { cl_log(LOG_ERR, "LHS of rsc_to_node (%s) is NULL", cons->id); continue; } cons->rsc_lh->node_cons = g_slist_append(cons->rsc_lh->node_cons, cons); if(cons->node_list_rh == NULL) { cl_log(LOG_ERR, "RHS of rsc_to_node (%s) is NULL", cons->id); continue; } else { int llpc = 0; slist_iter(node_rh, node_t, cons->node_list_rh, llpc, update_node_weight(cons, node_rh->details->id, nodes)); } /* dont add it to the resource, * the information is in the resouce's node list */ ); return TRUE; } // remove nodes that are down, stopping // create +ve rsc_to_node constraints between resources and the nodes they are running on // anything else? gboolean unpack_status(xmlNodePtr status, GSListPtr nodes, GSListPtr rsc_list, GSListPtr *node_constraints) { pdebug("Begining unpack %s", __FUNCTION__); while(status != NULL) { const char *id = xmlGetProp( status, XML_ATTR_ID); const char *state = xmlGetProp( status, XML_LRM_ATTR_STATE); const char *exp_state = xmlGetProp( status, XML_CIB_ATTR_EXPSTATE); const char *join_state = xmlGetProp( status, XML_CIB_ATTR_JOINSTATE); // const char *crm_state = xmlGetProp( // status, XML_CIB_ATTR_CRMDSTATE); const char *ccm_state = xmlGetProp( status, XML_CIB_ATTR_INCCM); const char *shutdown = xmlGetProp( status, XML_CIB_ATTR_SHUTDOWN); const char *unclean = xmlGetProp( status, XML_CIB_ATTR_STONITH); xmlNodePtr lrm_state = find_xml_node(status, XML_CIB_TAG_LRM); xmlNodePtr attrs = find_xml_node(status, "attributes"); lrm_state = find_xml_node(lrm_state, XML_LRM_TAG_RESOURCES); lrm_state = find_xml_node(lrm_state, "lrm_resource"); status = status->next; pdebug("Processing node %s", id); if(id == NULL){ // error continue; } pdebug("Processing node attrs"); node_t *this_node = pe_find_node(nodes, id); if(this_node == NULL) { cl_log(LOG_ERR, "Node %s in status section no longer exists", id); continue; } while(attrs != NULL){ const char *name = xmlGetProp( attrs, XML_NVPAIR_ATTR_NAME); const char *value = xmlGetProp( attrs, XML_NVPAIR_ATTR_VALUE); if(name != NULL && value != NULL && safe_val(NULL, this_node, details) != NULL) { pdebug("Adding %s => %s", name, value); g_hash_table_insert(this_node->details->attrs, crm_strdup(name), crm_strdup(value)); } attrs = attrs->next; } pdebug("determining node state"); if(safe_str_eq(join_state, CRMD_JOINSTATE_MEMBER) && safe_str_eq(ccm_state, XML_BOOLEAN_YES) && shutdown == NULL) { // process resource, make +ve preference this_node->details->online = TRUE; } else { pdebug("remove %s", __FUNCTION__); // remove node from contention this_node->weight = -1; this_node->fixed = TRUE; pdebug("state %s, expected %s, shutdown %s", state, exp_state, shutdown); if(unclean != NULL) { this_node->details->unclean = TRUE; } else if(shutdown != NULL) { this_node->details->shutdown = TRUE; } else if(safe_str_eq(exp_state, CRMD_STATE_ACTIVE) && safe_str_eq( join_state, CRMD_JOINSTATE_DOWN) ){ // mark unclean in the xml this_node->details->unclean = TRUE; } if(this_node->details->unclean) { pdebug("Node %s is due for STONITH", id); } if(this_node->details->shutdown) { pdebug("Node %s is due for shutdown", id); } } pdebug("Processing node lrm state"); process_node_lrm_state(this_node, lrm_state, rsc_list, nodes, node_constraints); } return TRUE; } gboolean is_active(rsc_to_node_t *cons) { return TRUE; } gboolean strict_preproc(rsc_to_rsc_t *constraint, color_t *local_color, color_t *other_color, GSListPtr *colors, GSListPtr resources) { resource_t * lh_resource = constraint->rsc_lh; switch(constraint->strength) { case must: if(constraint->rsc_rh->runnable == FALSE) { cl_log(LOG_WARNING, "Resource %s must run on the same node" " as %s (cons %s), but %s is not" " runnable.", constraint->rsc_lh->id, constraint->rsc_rh->id, constraint->id, constraint->rsc_rh->id); constraint->rsc_lh->runnable = FALSE; } break; // x * should * should_not = x case should: if(constraint->rsc_rh->provisional == FALSE) { local_color->local_weight = local_color->local_weight * 2.0; } break; case should_not: if(constraint->rsc_rh->provisional == FALSE) { local_color->local_weight = local_color->local_weight * 0.5; } pdebug("# Colors %d, Nodes %d", g_slist_length(*colors), max_valid_nodes); if(g_slist_length(*colors) < max_valid_nodes // && g_slist_length(lh_resource->candidate_colors)==1 ) { create_color(colors, lh_resource->allowed_nodes, resources); } break; case must_not: if(constraint->rsc_rh->provisional == FALSE && local_color->id != no_color->id) { lh_resource->candidate_colors = g_slist_remove( lh_resource->candidate_colors, local_color); pdebug_action( print_color("Removed", local_color, FALSE)); // surely this is required... but mtrace says no... // crm_free(local_color); } break; default: // error break; } return TRUE; } gboolean strict_postproc(rsc_to_rsc_t *constraint, color_t *local_color, color_t *other_color, GSListPtr *colors, GSListPtr resources) { print_rsc_to_rsc("Post processing", constraint, FALSE); switch(constraint->strength) { case must: if(constraint->rsc_rh->provisional == TRUE) { constraint->rsc_rh->color = other_color; constraint->rsc_rh->provisional = FALSE; color_resource(constraint->rsc_rh, colors, resources); } // else check for error if(constraint->rsc_lh->runnable == FALSE) { cl_log(LOG_WARNING, "Resource %s must run on the same node" " as %s (cons %s), but %s is not" " runnable.", constraint->rsc_rh->id, constraint->rsc_lh->id, constraint->id, constraint->rsc_lh->id); constraint->rsc_rh->runnable = FALSE; } break; case should: break; case should_not: break; case must_not: if(constraint->rsc_rh->provisional == TRUE) { // check for error } break; default: // error break; } return TRUE; } gboolean choose_color(resource_t *lh_resource) { int lpc = 0; if(lh_resource->runnable == FALSE) { lh_resource->color = find_color(lh_resource->candidate_colors, no_color); lh_resource->provisional = FALSE; } if(lh_resource->provisional) { GSListPtr sorted_colors = g_slist_sort(lh_resource->candidate_colors, sort_color_weight); lh_resource->candidate_colors = sorted_colors; pdebug("Choose a color from %d possibilities", g_slist_length(sorted_colors)); slist_iter( this_color, color_t,lh_resource->candidate_colors, lpc, GSListPtr intersection = node_list_and( this_color->details->candidate_nodes, lh_resource->allowed_nodes); if(g_slist_length(intersection) != 0) { // TODO: merge node weights GSListPtr old_list = this_color->details->candidate_nodes; pe_free_shallow(old_list); this_color->details->candidate_nodes = intersection; lh_resource->color = this_color; lh_resource->provisional = FALSE; break; } else { pe_free_shallow(intersection); } ); } return !lh_resource->provisional; } gboolean unpack_rsc_to_node(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr node_list, GSListPtr *node_constraints) { xmlNodePtr node_ref = xml_obj->children; rsc_to_node_t *new_con = crm_malloc(sizeof(rsc_to_node_t)); const char *id_lh = xmlGetProp(xml_obj, "from"); const char *id = xmlGetProp(xml_obj, XML_ATTR_ID); const char *mod = xmlGetProp(xml_obj, "modifier"); const char *weight = xmlGetProp(xml_obj, "weight"); float weight_f = atof(weight); resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); if(rsc_lh == NULL) { cl_log(LOG_ERR, "No resource (con=%s, rsc=%s)", id, id_lh); } new_con->id = crm_strdup(id); new_con->rsc_lh = rsc_lh; new_con->weight = weight_f; if(safe_str_eq(mod, "set")){ new_con->modifier = set; } else if(safe_str_eq(mod, "inc")){ new_con->modifier = inc; } else if(safe_str_eq(mod, "dec")){ new_con->modifier = dec; } else { // error } /* */ // while(node_ref != NULL) { const char *xml_name = node_ref->name; const char *id_rh = xmlGetProp(node_ref, XML_NVPAIR_ATTR_NAME); node_t *node_rh = pe_find_node(node_list, id_rh); node_ref = node_ref->next; if(node_rh == NULL) { // error cl_log(LOG_ERR, "node %s (from %s) not found", id_rh, xml_name); continue; } new_con->node_list_rh = g_slist_append(new_con->node_list_rh, node_rh); /* dont add it to the resource, * the information is in the resouce's node list */ } *node_constraints = g_slist_append(*node_constraints, new_con); return TRUE; } gboolean unpack_rsc_to_attr(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr node_list, GSListPtr *node_constraints) { /* Translation: give any node a +ve weight of 20.0 to run rsc2 if: attr "cpu" is set _and_ "kernel"="2.6", _or_ attr "hdd" is set _and_ "kernel"="2.4" Further translation: 2 constraints that give any node a +ve weight of 20.0 to run rsc2 cons1: attr "cpu" is set and "kernel"="2.6" cons2: attr "hdd" is set and "kernel"="2.4" */ xmlNodePtr attr_exp = xml_obj->children; const char *id_lh = xmlGetProp(xml_obj, "from"); const char *mod = xmlGetProp(xml_obj, "modifier"); const char *weight = xmlGetProp(xml_obj, "weight"); const char *id = xmlGetProp(attr_exp, XML_ATTR_ID); float weight_f = atof(weight); enum con_modifier a_modifier = modifier_none; resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); if(rsc_lh == NULL) { cl_log(LOG_ERR, "No resource (con=%s, rsc=%s)", id, id_lh); return FALSE; } if(safe_str_eq(mod, "set")){ a_modifier = set; } else if(safe_str_eq(mod, "inc")){ a_modifier = inc; } else if(safe_str_eq(mod, "dec")){ a_modifier = dec; } else { // error } if(attr_exp == NULL) { cl_log(LOG_WARNING, "no attrs for constraint %s", id); } while(attr_exp != NULL) { const char *id_rh = xmlGetProp(attr_exp, XML_NVPAIR_ATTR_NAME); const char *id = xmlGetProp(attr_exp, XML_ATTR_ID); rsc_to_node_t *new_con = crm_malloc(sizeof(rsc_to_node_t)); new_con->id = crm_strdup(id); new_con->rsc_lh = rsc_lh; new_con->weight = weight_f; new_con->modifier = a_modifier; new_con->node_list_rh = match_attrs(attr_exp, node_list); if(new_con->node_list_rh == NULL) { // error cl_log(LOG_ERR, "node %s (from %s) not found", id_rh, attr_exp->name); } pdebug_action(print_rsc_to_node("Added", new_con, FALSE)); *node_constraints = g_slist_append(*node_constraints, new_con); /* dont add it to the resource, * the information is in the resouce's node list */ attr_exp = attr_exp->next; } return TRUE; } gboolean update_node_weight(rsc_to_node_t *cons, char *id, GSListPtr nodes) { node_t *node_rh = pe_find_node(cons->rsc_lh->allowed_nodes, id); if(node_rh == NULL) { node_t *node_tmp = pe_find_node(nodes, id); node_rh = node_copy(node_tmp); cons->rsc_lh->allowed_nodes = g_slist_append(cons->rsc_lh->allowed_nodes, node_rh); } if(node_rh == NULL) { // error return FALSE; } if(node_rh->fixed) { // warning cl_log(LOG_WARNING, "Constraint %s is irrelevant as the" " weight of node %s is fixed as %f.", cons->id, node_rh->details->id, node_rh->weight); return TRUE; } pdebug("Constraint %s: node %s weight %s %f.", cons->id, node_rh->details->id, modifier2text(cons->modifier), node_rh->weight); switch(cons->modifier) { case set: node_rh->weight = cons->weight; node_rh->fixed = TRUE; break; case inc: node_rh->weight += cons->weight; break; case dec: node_rh->weight -= cons->weight; break; case modifier_none: // warning break; } return TRUE; } gboolean process_node_lrm_state(node_t *node, xmlNodePtr lrm_state, GSListPtr rsc_list, GSListPtr nodes, GSListPtr *node_constraints) { while(lrm_state != NULL) { const char *rsc_id = xmlGetProp( lrm_state, XML_ATTR_ID); const char *node_id = xmlGetProp( lrm_state, XML_LRM_ATTR_TARGET); const char *rsc_state = xmlGetProp( lrm_state, XML_LRM_ATTR_STATE); resource_t *rsc_lh = pe_find_resource(rsc_list, rsc_id); pdebug("[%s] Processing %s on %s (%s)", lrm_state->name, rsc_id, node_id, rsc_state); lrm_state = lrm_state->next; if(rsc_lh == NULL) { cl_log(LOG_ERR, "Could not find a match for resource" " %s in %s's status section", rsc_id, node_id); continue; } pdebug("Setting cur_node = %s for rsc = %s", node->details->id, rsc_lh->id); if(rsc_lh->cur_node != NULL) { cl_log(LOG_ERR, "Resource %s running on multiple nodes %s & %s", rsc_lh->id, rsc_lh->cur_node->details->id, node->details->id); // TODO: some recovery action!! // like force a stop on the first node? continue; } rsc_lh->cur_node = node; node->details->running_rsc = g_slist_append(node->details->running_rsc, rsc_lh); if((safe_str_eq(rsc_state, "starting")) || (safe_str_eq(rsc_state, "started"))) { node_t *node_rh; rsc_to_node_t *new_cons = crm_malloc(sizeof(rsc_to_node_t)); new_cons->id = crm_strdup("create_me"); // genereate one new_cons->weight = 100.0; new_cons->modifier = inc; new_cons->rsc_lh = rsc_lh; node_rh = pe_find_node(nodes, node_id); new_cons->node_list_rh = g_slist_append(NULL, node_rh); *node_constraints = g_slist_append(*node_constraints, new_cons); pdebug_action(print_rsc_to_node( "Added", new_cons, FALSE)); } else if(safe_str_eq(rsc_state, "stop_fail")) { // do soemthing } // else no preference } return TRUE; } GSListPtr match_attrs(xmlNodePtr attr_exp, GSListPtr node_list) { int lpc = 0; GSListPtr result = NULL; slist_iter( node, node_t, node_list, lpc, xmlNodePtr node_match = attr_exp->children; gboolean accept = TRUE; while(accept && node_match != NULL) { const char *type = xmlGetProp( node_match, XML_ATTR_TYPE); const char *value= xmlGetProp( node_match, XML_NVPAIR_ATTR_VALUE); const char *name = xmlGetProp(node_match, "target"); node_match = node_match->next; if(name == NULL || type == NULL) { // error continue; } const char *h_val = (const char*) g_hash_table_lookup(node->details->attrs, name); if(h_val != NULL && safe_str_eq(type, "has_attr")){ accept = TRUE; } else if(h_val == NULL && safe_str_eq(type, "not_attr")) { accept = TRUE; } else if(h_val != NULL && safe_str_eq(type, "attr_value") && safe_str_eq(h_val, value)) { accept = TRUE; } else { accept = FALSE; } } if(accept) { result = g_slist_append(result, node); } ); return result; } gboolean create_rsc_to_rsc(const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh) { if(rsc_lh == NULL || rsc_rh == NULL){ // error return FALSE; } rsc_to_rsc_t *new_con = crm_malloc(sizeof(rsc_to_node_t)); rsc_to_rsc_t *inverted_con = NULL; new_con->id = crm_strdup(id); new_con->rsc_lh = rsc_lh; new_con->rsc_rh = rsc_rh; new_con->strength = strength; inverted_con = invert_constraint(new_con); rsc_lh->rsc_cons = g_slist_insert_sorted(rsc_lh->rsc_cons, new_con, sort_cons_strength); rsc_rh->rsc_cons = g_slist_insert_sorted(rsc_rh->rsc_cons, inverted_con, sort_cons_strength); return TRUE; } gboolean create_ordering(const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh, GSListPtr *action_constraints) { if(rsc_lh == NULL || rsc_rh == NULL){ // error return FALSE; } action_t *lh_stop = rsc_lh->stop; action_t *lh_start = rsc_lh->start; action_t *rh_stop = rsc_rh->stop; action_t *rh_start = rsc_rh->start; order_constraint_t *order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); order->id = order_id++; order->lh_action = lh_stop; order->rh_action = rh_stop; order->strength = strength; *action_constraints = g_slist_append(*action_constraints, order); order = (order_constraint_t*) crm_malloc(sizeof(order_constraint_t)); order->id = order_id++; order->lh_action = rh_start; order->rh_action = lh_start; order->strength = strength; *action_constraints = g_slist_append(*action_constraints, order); return TRUE; } gboolean unpack_rsc_to_rsc(xmlNodePtr xml_obj, GSListPtr rsc_list, GSListPtr *action_constraints) { const char *id_lh = xmlGetProp(xml_obj, "from"); const char *id = xmlGetProp(xml_obj, XML_ATTR_ID); resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); const char *id_rh = xmlGetProp(xml_obj, "to"); resource_t *rsc_rh = pe_find_resource(rsc_list, id_rh); const char *strength = xmlGetProp(xml_obj, "strength"); const char *type = xmlGetProp(xml_obj, XML_ATTR_TYPE); enum con_strength strength_e = ignore; if(rsc_lh == NULL) { cl_log(LOG_ERR, "No resource (con=%s, rsc=%s)", id, id_lh); return FALSE; } if(safe_str_eq(strength, XML_STRENGTH_VAL_MUST)) { strength_e = must; } else if(safe_str_eq(strength, XML_STRENGTH_VAL_SHOULD)) { strength_e = should; } else if(safe_str_eq(strength, XML_STRENGTH_VAL_SHOULDNOT)) { strength_e = should_not; } else if(safe_str_eq(strength, XML_STRENGTH_VAL_MUSTNOT)) { strength_e = must_not; } else { // error } if(safe_str_eq(type, "ordering")) { // make an action_cons instead return create_ordering(id, strength_e, rsc_lh, rsc_rh, action_constraints); } return create_rsc_to_rsc(id, strength_e, rsc_lh, rsc_rh); } GSListPtr create_action_set(action_t *action) { int lpc; GSListPtr tmp = NULL; GSListPtr result = NULL; gboolean preceeding_complete = FALSE; if(action->processed) { return NULL; } pdebug_action(print_action("Create action set for", action, FALSE)); // process actions_before if(action->seen_count == 0) { pdebug("Processing \"before\" for action %d", action->id); slist_iter( other, action_wrapper_t, action->actions_before, lpc, tmp = create_action_set(other->action); pdebug("%d (%d total) \"before\" actions for %d)", g_slist_length(tmp), g_slist_length(result), action->id); result = g_slist_concat(result, tmp); preceeding_complete = TRUE; ); } else { pdebug("Already seen action %d", action->id); pdebug("Processing \"before\" for action %d", action->id); slist_iter( other, action_wrapper_t, action->actions_before, lpc, if(other->action->seen_count > action->seen_count && other->strength == must) { tmp = create_action_set(other->action); pdebug("%d (%d total) \"before\" actions for %d)", g_slist_length(tmp), g_slist_length(result), action->id); result = g_slist_concat(result, tmp); } ); } // add ourselves if(action->runnable) { if(action->processed == FALSE) { pdebug("Adding self %d", action->id); result = g_slist_append(result, action); } else { pdebug("Already added self %d", action->id); } } else { pdebug("Skipping ourselves, we're not runnable"); } action->processed = TRUE; if(preceeding_complete == FALSE) { // add strength == !MUST slist_iter( other, action_wrapper_t, action->actions_before, lpc, tmp = create_action_set(other->action); pdebug("%d (%d total) post-self \"before\" actions for %d)", g_slist_length(tmp), g_slist_length(result),action->id); result = g_slist_concat(result, tmp); ); } action->seen_count = action->seen_count + 1; /* process actions_after * * do this regardless of whether we are runnable. Any direct or * indirect hard/XML_STRENGTH_VAL_MUST dependancies on us will have been picked * up earlier on in stage 7 */ pdebug("Processing \"after\" for action %d", action->id); slist_iter( other, action_wrapper_t, action->actions_after, lpc, tmp = create_action_set(other->action); pdebug("%d (%d total) \"after\" actions for %d)", g_slist_length(tmp), g_slist_length(result),action->id); result = g_slist_concat(result, tmp); ); return result; } gboolean update_runnable(GSListPtr actions) { int lpc = 0, lpc2 = 0; gboolean change = TRUE; while(change) { change = FALSE; slist_iter( action, action_t, actions, lpc, if(action->runnable) { continue; } else if(action->optional) { continue; } slist_iter( other, action_wrapper_t, action->actions_after, lpc2, if(other->action->runnable) { change = TRUE; pdebug_action( print_action( "Marking unrunnable", other->action, FALSE)); } other->action->runnable = FALSE; ); ); } return TRUE; } void color_resource(resource_t *lh_resource, GSListPtr *colors, GSListPtr resources) { int lpc = 0; pdebug_action(print_resource("Coloring", lh_resource, FALSE)); if(lh_resource->provisional == FALSE) { // already processed this resource return; } lh_resource->rsc_cons = g_slist_sort(lh_resource->rsc_cons, sort_cons_strength); pdebug("=== Pre-processing"); //------ Pre-processing slist_iter( constraint, rsc_to_rsc_t, lh_resource->rsc_cons, lpc, color_t *other_color = NULL; color_t *local_color = NULL; if(lh_resource->runnable == FALSE) { break; } pdebug_action(print_rsc_to_rsc( "Processing constraint", constraint, FALSE)); if(constraint->rsc_rh == NULL) { cl_log(LOG_ERR, "rsc_rh was NULL for %s", constraint->id); continue; } other_color = constraint->rsc_rh->color; local_color = find_color(lh_resource->candidate_colors, other_color); strict_preproc(constraint, local_color, other_color, colors, resources); ); // filter out nodes with a negative weight filter_nodes(lh_resource); /* Choose a color from the candidates or, * create a new one if no color is suitable * (this may need modification pending further napkin drawings) */ choose_color(lh_resource); pdebug("* Colors %d, Nodes %d", g_slist_length(*colors), max_valid_nodes); if(lh_resource->provisional && g_slist_length(*colors) < max_valid_nodes) { // Create new color pdebug("Create a new color"); lh_resource->color = create_color(colors, lh_resource->allowed_nodes, resources); } else if(lh_resource->provisional) { cl_log(LOG_ERR, "Could not color resource %s", lh_resource->id); print_resource("ERROR: No color", lh_resource, FALSE); lh_resource->color = find_color(lh_resource->candidate_colors, no_color); } lh_resource->provisional = FALSE; pdebug_action(print_resource("Post-processing", lh_resource, FALSE)); //------ Post-processing color_t *local_color = lh_resource->color; slist_iter( constraint, rsc_to_rsc_t, lh_resource->rsc_cons, lpc, color_t *other_color = find_color(constraint->rsc_rh->candidate_colors, local_color); strict_postproc(constraint, local_color, other_color, colors, resources); ); pdebug_action(print_resource("Colored", lh_resource, FALSE)); } FILE *pemsg_strm = NULL; gboolean process_pe_message(xmlNodePtr msg, IPC_Channel *sender) { const char *op = get_xml_attr (msg, XML_TAG_OPTIONS, XML_ATTR_OP, TRUE); const char *ref = xmlGetProp(msg, XML_ATTR_REFERENCE); if(safe_str_eq(xmlGetProp(msg, XML_ATTR_MSGTYPE), XML_ATTR_REQUEST)) { cl_log(LOG_INFO, "Message was a response not a request." " Discarding"); } CRM_DEBUG("Processing %s op (ref=%s)...", op, ref); if(pemsg_strm == NULL) { pemsg_strm = fopen("/tmp/pemsg.log", "w"); } char *msg_buffer = dump_xml_node(msg, FALSE); fprintf(pemsg_strm, "%s: %s\n", "[in ]", msg_buffer); fflush(pemsg_strm); crm_free(msg_buffer); const char *sys_to = xmlGetProp(msg, XML_ATTR_SYSTO); if(op == NULL){ // error } else if(strcmp(op, CRM_OP_HELLO) == 0) { // ignore } else if(sys_to == NULL || strcmp(sys_to, CRM_SYSTEM_PENGINE) != 0) { CRM_DEBUG("Bad sys-to %s", sys_to); return FALSE; } else if(strcmp(op, CRM_OP_PECALC) == 0) { xmlNodePtr input_cib = find_xml_node(msg, XML_TAG_CIB); xmlNodePtr output = do_calculations(input_cib); if (send_ipc_reply(sender, msg, output) ==FALSE) { cl_log(LOG_WARNING, "Answer could not be sent"); } free_xml(output); } else if(strcmp(op, CRM_OP_QUIT) == 0) { cl_log(LOG_WARNING, "Received quit message, terminating"); exit(0); } return TRUE; } xmlNodePtr do_calculations(xmlNodePtr cib_object) { int lpc, lpc2; GSListPtr resources = NULL; GSListPtr nodes = NULL; GSListPtr node_constraints = NULL; GSListPtr actions = NULL; GSListPtr action_constraints = NULL; GSListPtr stonith_list = NULL; GSListPtr shutdown_list = NULL; GSListPtr colors = NULL; GSListPtr action_sets = NULL; xmlNodePtr graph = NULL; // pe_debug_on(); pdebug("=#=#=#=#= Stage 0 =#=#=#=#="); stage0(cib_object, &resources, &nodes, &node_constraints, &actions, &action_constraints, &stonith_list, &shutdown_list); pdebug("=#=#=#=#= Stage 1 =#=#=#=#="); stage1(node_constraints, nodes, resources); pdebug("=#=#=#=#= Stage 2 =#=#=#=#="); stage2(resources, nodes, &colors); pdebug("========= Nodes ========="); pdebug_action( slist_iter(node, node_t, nodes, lpc, print_node(NULL, node, TRUE) ) ); pdebug("========= Resources ========="); pdebug_action( slist_iter(resource, resource_t, resources, lpc, print_resource(NULL, resource, TRUE) ) ); pdebug("=#=#=#=#= Stage 3 =#=#=#=#="); stage3(colors); pdebug("=#=#=#=#= Stage 4 =#=#=#=#="); stage4(colors); pdebug("========= Colors ========="); pdebug_action( slist_iter(color, color_t, colors, lpc, print_color(NULL, color, FALSE) ) ); pdebug("=#=#=#=#= Stage 5 =#=#=#=#="); stage5(resources); pdebug("=#=#=#=#= Stage 6 =#=#=#=#="); stage6(&actions, &action_constraints, stonith_list, shutdown_list); pdebug("========= Action List ========="); pdebug_action( slist_iter(action, action_t, actions, lpc, print_action(NULL, action, TRUE) ) ); pdebug("=#=#=#=#= Stage 7 =#=#=#=#="); stage7(resources, actions, action_constraints, &action_sets); pdebug("=#=#=#=#= Summary =#=#=#=#="); summary(resources); pdebug("========= Action Sets ========="); pdebug("\t========= Set %d (Un-runnable) =========", -1); pdebug_action( slist_iter(action, action_t, actions, lpc, if(action->optional == FALSE && action->runnable == FALSE) { print_action("\t", action, TRUE); } ) ); pdebug_action( slist_iter(action_set, GSList, action_sets, lpc, pdebug("\t========= Set %d =========", lpc); slist_iter(action, action_t, action_set, lpc2, print_action("\t", action, TRUE); ) ) ); pdebug("========= Stonith List ========="); pdebug_action( slist_iter(node, node_t, stonith_list, lpc, print_node(NULL, node, FALSE); ) ); pdebug("========= Shutdown List ========="); pdebug_action( slist_iter(node, node_t, shutdown_list, lpc, print_node(NULL, node, FALSE); ) ); pdebug("=#=#=#=#= Stage 8 =#=#=#=#="); stage8(action_sets, &graph); pdebug("=#=#=#=#= Cleanup =#=#=#=#="); pdebug("deleting node cons"); while(node_constraints) { pe_free_rsc_to_node((rsc_to_node_t*)node_constraints->data); node_constraints = node_constraints->next; } g_slist_free(node_constraints); pdebug("deleting order cons"); pe_free_shallow(action_constraints); pdebug("deleting action sets"); slist_iter(action_set, GSList, action_sets, lpc, pe_free_shallow_adv(action_set, FALSE); ); pe_free_shallow_adv(action_sets, FALSE); pdebug("deleting actions"); pe_free_actions(actions); pdebug("deleting resources"); pe_free_resources(resources); pdebug("deleting colors"); pe_free_colors(colors); pdebug("deleting nodes"); pe_free_nodes(nodes); g_slist_free(shutdown_list); g_slist_free(stonith_list); return graph; } diff --git a/crm/pengine/pengine.h b/crm/pengine/pengine.h index 7fe08dc69c..98e959f67b 100644 --- a/crm/pengine/pengine.h +++ b/crm/pengine/pengine.h @@ -1,227 +1,229 @@ #ifndef PENGINE__H #define PENGINE__H +#include + typedef struct node_s node_t; typedef struct color_s color_t; typedef struct rsc_to_node_s rsc_to_node_t; typedef struct rsc_to_rsc_s rsc_to_rsc_t; typedef struct resource_s resource_t; typedef struct order_constraint_s order_constraint_t; typedef struct action_s action_t; typedef struct action_wrapper_s action_wrapper_t; enum con_type { type_none, rsc_to_rsc, rsc_to_node, rsc_to_attr, base_weight }; enum node_type { node_ping, node_member }; enum con_strength { ignore, must, should, should_not, must_not, startstop }; enum con_modifier { modifier_none, set, inc, dec }; enum action_tasks { no_action, stop_rsc, start_rsc, shutdown_crm, stonith_op }; enum action_order { dontcare, before, after }; struct node_shared_s { char *id; gboolean online; gboolean unclean; gboolean shutdown; GSListPtr running_rsc; // resource_t* GHashTable *attrs; // char* => char* enum node_type type; }; struct node_s { float weight; gboolean fixed; struct node_shared_s *details; }; struct color_shared_s { int id; GSListPtr candidate_nodes; // node_t* node_t *chosen_node; }; struct color_s { int id; struct color_shared_s *details; float local_weight; }; struct rsc_to_rsc_s { char *id; resource_t *rsc_lh; // gboolean is_placement; resource_t *rsc_rh; enum con_strength strength; }; struct rsc_to_node_s { char *id; resource_t *rsc_lh; float weight; GSListPtr node_list_rh; // node_t* enum con_modifier modifier; }; struct resource_s { char *id; xmlNodePtr xml; int priority; node_t *cur_node; gboolean runnable; gboolean provisional; action_t *stop; action_t *start; GSListPtr candidate_colors; // color_t* GSListPtr allowed_nodes; // node_t* GSListPtr node_cons; // rsc_to_node_t* GSListPtr rsc_cons; // resource_t* color_t *color; }; struct action_wrapper_s { enum con_strength strength; action_t *action; }; struct action_s { int id; resource_t *rsc; node_t *node; enum action_tasks task; gboolean runnable; gboolean processed; gboolean optional; gboolean discard; gboolean failure_is_fatal; int seen_count; GSListPtr actions_before; // action_warpper_t* GSListPtr actions_after; // action_warpper_t* }; struct order_constraint_s { int id; action_t *lh_action; action_t *rh_action; enum con_strength strength; // enum action_order order; }; extern gboolean stage0(xmlNodePtr cib, GSListPtr *nodes, GSListPtr *rscs, GSListPtr *cons, GSListPtr *actions, GSListPtr *action_constraints, GSListPtr *stonith_list, GSListPtr *shutdown_list); extern gboolean stage1(GSListPtr node_constraints, GSListPtr nodes, GSListPtr resources); extern gboolean stage2(GSListPtr sorted_rscs, GSListPtr sorted_nodes, GSListPtr *colors); extern gboolean stage3(GSListPtr colors); extern gboolean stage4(GSListPtr colors); extern gboolean stage5(GSListPtr resources); extern gboolean stage6(GSListPtr *actions, GSListPtr *action_constraints, GSListPtr stonith, GSListPtr shutdown); extern gboolean stage7(GSListPtr resources, GSListPtr actions, GSListPtr action_constraints, GSListPtr *action_sets); extern gboolean stage8(GSListPtr action_sets, xmlNodePtr *graph); extern gboolean summary(GSListPtr resources); extern gboolean pe_input_dispatch(IPC_Channel *sender, void *user_data); extern void pe_free_nodes(GSListPtr nodes); extern void pe_free_colors(GSListPtr colors); extern void pe_free_rsc_to_rsc(rsc_to_rsc_t *cons); extern void pe_free_rsc_to_node(rsc_to_node_t *cons); extern void pe_free_shallow(GSListPtr alist); extern void pe_free_shallow_adv(GSListPtr alist, gboolean with_data); extern void pe_free_resources(GSListPtr resources); extern void pe_free_actions(GSListPtr actions); extern gboolean pe_debug; extern gboolean pe_debug_saved; extern color_t *no_color; #define pdebug_action(x) if(pe_debug) { \ x; \ } #define pdebug(x...) if(pe_debug) { \ cl_log(LOG_DEBUG, x); \ } #define pe_debug_on() pe_debug_saved = pe_debug; pe_debug = TRUE; #define pe_debug_off() pe_debug_saved = pe_debug; pe_debug = FALSE; #define pe_debug_restore() pe_debug = pe_debug_saved; #define safe_val(def, x,y) (x?x->y:def) #define safe_val3(def, t,u,v) (t?t->u?t->u->v:def:def) #define safe_val4(def, t,u,v,w) (t?t->u?t->u->v?t->u->v->w:def:def:def) #define safe_val5(def, t,u,v,w,x) (t?t->u?t->u->v?t->u->v->w?t->u->v->w->x:def:def:def:def) #define safe_val6(def, t,u,v,w,x,y) (t?t->u?t->u->v?t->u->v->w?t->u->v->w->x?t->u->v->w->x->y:def:def:def:def:def) #define safe_val7(def, t,u,v,w,x,y,z) (t?t->u?t->u->v?t->u->v->w?t->u->v->w->x?t->u->v->w->x->y?t->u->v->w->x->y->z:def:def:def:def:def:def) #endif diff --git a/crm/pengine/penginemain.c b/crm/pengine/penginemain.c index b81042be35..94260db814 100644 --- a/crm/pengine/penginemain.c +++ b/crm/pengine/penginemain.c @@ -1,238 +1,226 @@ -/* $Id: penginemain.c,v 1.14 2004/06/01 16:12:50 andrew Exp $ */ +/* $Id: penginemain.c,v 1.15 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include #include -#include - -#include -#include -#include -#include -#include #include -#include -#include -#include - -#include -#include -#include +#include +#include #include #define SYS_NAME CRM_SYSTEM_PENGINE #define OPTARGS "skrh" #define PID_FILE WORKING_DIR "/"SYS_NAME".pid" #define DAEMON_LOG "/var/log/"SYS_NAME".log" #define DAEMON_DEBUG "/var/log/"SYS_NAME".debug" GMainLoop* mainloop = NULL; const char* crm_system_name = SYS_NAME; void usage(const char* cmd, int exit_status); int init_start(void); void pengine_shutdown(int nsig); extern gboolean process_pe_message(xmlNodePtr msg, IPC_Channel *sender); int main(int argc, char ** argv) { int req_restart = FALSE; int req_status = FALSE; int req_stop = FALSE; int argerr = 0; int flag; cl_log_set_entity(crm_system_name); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); if (0) { send_ipc_message(NULL, NULL); } while ((flag = getopt(argc, argv, OPTARGS)) != EOF) { switch(flag) { case 's': /* Status */ req_status = TRUE; break; case 'k': /* Stop (kill) */ req_stop = TRUE; break; case 'r': /* Restart */ req_restart = TRUE; break; case 'h': /* Help message */ usage(crm_system_name, LSB_EXIT_OK); break; default: ++argerr; break; } } if (optind > argc) { ++argerr; } if (argerr) { usage(crm_system_name,LSB_EXIT_GENERIC); } // read local config file if (req_status){ return init_status(PID_FILE, crm_system_name); } if (req_stop){ return init_stop(PID_FILE); } if (req_restart) { init_stop(PID_FILE); } return init_start(); } int init_start(void) { long pid; ll_cluster_t* hb_fd = NULL; int facility; IPC_Channel *crm_ch = NULL; #ifdef REALTIME_SUPPORT static int crm_realtime = 1; #endif if ((pid = get_running_pid(PID_FILE, NULL)) > 0) { cl_log(LOG_CRIT, "already running: [pid %ld].", pid); exit(LSB_EXIT_OK); } cl_log_set_logfile(DAEMON_LOG); // if (crm_debug()) { cl_log_set_debugfile(DAEMON_DEBUG); // } /* change the logging facility to the one used by heartbeat daemon */ hb_fd = ll_cluster_new("heartbeat"); cl_log(LOG_INFO, "Switching to Heartbeat logger"); if ((facility = hb_fd->llc_ops->get_logfacility(hb_fd))>0) { cl_log_set_facility(facility); } cl_log(LOG_INFO, "Register PID"); register_pid(PID_FILE, FALSE, pengine_shutdown); crm_ch = init_client_ipc_comms(CRM_SYSTEM_CRMD, subsystem_input_dispatch, (void*)process_pe_message); if(crm_ch != NULL) { send_hello_message(crm_ch, "1234", CRM_SYSTEM_PENGINE, "0", "1"); /* Create the mainloop and run it... */ mainloop = g_main_new(FALSE); cl_log(LOG_INFO, "Starting %s", crm_system_name); #ifdef REALTIME_SUPPORT if (crm_realtime == 1){ cl_enable_realtime(); }else if (crm_realtime == 0){ cl_disable_realtime(); } cl_make_realtime(SCHED_RR, 5, 64, 64); #endif g_main_run(mainloop); } else { cl_log(LOG_ERR, "Could not connect to the CRMd"); } return_to_orig_privs(); if (unlink(PID_FILE) == 0) { cl_log(LOG_INFO, "[%s] stopped", crm_system_name); } if(crm_ch != NULL) return 0; return 1; } void usage(const char* cmd, int exit_status) { FILE* stream; stream = exit_status ? stderr : stdout; fprintf(stream, "usage: %s [-srkh]" "[-c configure file]\n", cmd); /* fprintf(stream, "\t-d\tsets debug level\n"); */ /* fprintf(stream, "\t-s\tgets daemon status\n"); */ /* fprintf(stream, "\t-r\trestarts daemon\n"); */ /* fprintf(stream, "\t-k\tstops daemon\n"); */ /* fprintf(stream, "\t-h\thelp message\n"); */ fflush(stream); exit(exit_status); } void pengine_shutdown(int nsig) { static int shuttingdown = 0; CL_SIGNAL(nsig, pengine_shutdown); if (!shuttingdown) { shuttingdown = 1; } if (mainloop != NULL && g_main_is_running(mainloop)) { g_main_quit(mainloop); }else{ exit(LSB_EXIT_OK); } } diff --git a/crm/pengine/ptest.c b/crm/pengine/ptest.c index e7d50c4982..05e20ba216 100644 --- a/crm/pengine/ptest.c +++ b/crm/pengine/ptest.c @@ -1,308 +1,295 @@ -/* $Id: ptest.c,v 1.16 2004/05/23 20:13:46 andrew Exp $ */ +/* $Id: ptest.c,v 1.17 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include +#include #include #include #define OPTARGS "V?i:o:D:C:S:HA:U:M:I:EWRFt:m:a:d:w:c:r:p:s:" #include #include #include #include int main(int argc, char **argv) { xmlNodePtr cib_object = NULL; int lpc = 0; int argerr = 0; int flag; cl_log_set_entity("ptest"); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); while (1) { int option_index = 0; static struct option long_options[] = { // Top-level Options {"daemon", 0, 0, 0}, {0, 0, 0, 0} }; flag = getopt_long(argc, argv, OPTARGS, long_options, &option_index); if (flag == -1) break; switch(flag) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; /* a sample test for multiple instance if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); */ case 'V': printf("option %d", flag); break; default: printf("?? getopt returned character code 0%o ??\n", flag); ++argerr; break; } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (optind > argc) { ++argerr; } if (argerr) { cl_log(LOG_ERR, "%d errors in option parsing", argerr); } cl_log(LOG_INFO, "=#=#=#=#= Getting XML =#=#=#=#="); cib_object = file2xml(stdin); cl_log(LOG_INFO, "=#=#=#=#= Stage 0 =#=#=#=#="); GSListPtr resources = NULL; GSListPtr nodes = NULL; GSListPtr node_constraints = NULL; GSListPtr actions = NULL; GSListPtr action_constraints = NULL; GSListPtr stonith_list = NULL; GSListPtr shutdown_list = NULL; GSListPtr colors = NULL; GSListPtr action_sets = NULL; xmlNodePtr graph = NULL; mtrace(); pe_debug_on(); stage0(cib_object, &resources, &nodes, &node_constraints, &actions, &action_constraints, &stonith_list, &shutdown_list); cl_log(LOG_INFO, "========= Nodes ========="); slist_iter(node, node_t, nodes, lpc, print_node(NULL, node, TRUE)); cl_log(LOG_INFO, "========= Resources ========="); slist_iter(resource, resource_t, resources, lpc, print_resource(NULL, resource, TRUE)); cl_log(LOG_INFO, "========= Constraints ========="); slist_iter(constraint, rsc_to_node_t, node_constraints, lpc, print_rsc_to_node(NULL, constraint, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 1 =#=#=#=#="); stage1(node_constraints, nodes, resources); cl_log(LOG_INFO, "========= Nodes ========="); slist_iter(node, node_t, nodes, lpc, print_node(NULL, node, TRUE)); cl_log(LOG_INFO, "========= Resources ========="); slist_iter(resource, resource_t, resources, lpc, print_resource(NULL, resource, TRUE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 2 =#=#=#=#="); // pe_debug_on(); stage2(resources, nodes, &colors); // pe_debug_off(); cl_log(LOG_INFO, "========= Nodes ========="); slist_iter(node, node_t, nodes, lpc, print_node(NULL, node, TRUE)); cl_log(LOG_INFO, "========= Resources ========="); slist_iter(resource, resource_t, resources, lpc, print_resource(NULL, resource, TRUE)); cl_log(LOG_INFO, "========= Colors ========="); slist_iter(color, color_t, colors, lpc, print_color(NULL, color, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 3 =#=#=#=#="); stage3(colors); cl_log(LOG_INFO, "========= Colors ========="); slist_iter(color, color_t, colors, lpc, print_color(NULL, color, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 4 =#=#=#=#="); stage4(colors); cl_log(LOG_INFO, "========= Colors ========="); slist_iter(color, color_t, colors, lpc, print_color(NULL, color, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Summary =#=#=#=#="); summary(resources); cl_log(LOG_INFO, "========= Action List ========="); slist_iter(action, action_t, actions, lpc, print_action(NULL, action, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 5 =#=#=#=#="); stage5(resources); cl_log(LOG_INFO, "=#=#=#=#= Stage 6 =#=#=#=#="); stage6(&actions, &action_constraints, stonith_list, shutdown_list); cl_log(LOG_INFO, "========= Action List ========="); slist_iter(action, action_t, actions, lpc, print_action(NULL, action, TRUE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 7 =#=#=#=#="); stage7(resources, actions, action_constraints, &action_sets); cl_log(LOG_INFO, "=#=#=#=#= Summary =#=#=#=#="); summary(resources); cl_log(LOG_INFO, "========= All Actions ========="); slist_iter(action, action_t, actions, lpc, print_action("\t", action, TRUE); ); cl_log(LOG_INFO, "========= Action Sets ========="); cl_log(LOG_INFO, "\t========= Set %d (Un-runnable) =========", -1); slist_iter(action, action_t, actions, lpc, if(action->optional == FALSE && action->runnable == FALSE) { print_action("\t", action, TRUE); } ); int lpc2; slist_iter(action_set, GSList, action_sets, lpc, cl_log(LOG_INFO, "\t========= Set %d =========", lpc); slist_iter(action, action_t, action_set, lpc2, print_action("\t", action, TRUE))); cl_log(LOG_INFO, "========= Stonith List ========="); slist_iter(node, node_t, stonith_list, lpc, print_node(NULL, node, FALSE)); cl_log(LOG_INFO, "========= Shutdown List ========="); slist_iter(node, node_t, shutdown_list, lpc, print_node(NULL, node, FALSE)); cl_log(LOG_INFO, "=#=#=#=#= Stage 8 =#=#=#=#="); stage8(action_sets, &graph); // GSListPtr action_sets = NULL; pdebug("deleting node cons"); while(node_constraints) { pe_free_rsc_to_node((rsc_to_node_t*)node_constraints->data); node_constraints = node_constraints->next; } g_slist_free(node_constraints); pdebug("deleting order cons"); pe_free_shallow(action_constraints); pdebug("deleting action sets"); slist_iter(action_set, GSList, action_sets, lpc, pe_free_shallow_adv(action_set, FALSE); ); pe_free_shallow_adv(action_sets, FALSE); pdebug("deleting actions"); pe_free_actions(actions); pdebug("deleting resources"); pe_free_resources(resources); pdebug("deleting colors"); pe_free_colors(colors); crm_free(no_color->details); crm_free(no_color); pdebug("deleting nodes"); pe_free_nodes(nodes); g_slist_free(shutdown_list); g_slist_free(stonith_list); pe_debug_off(); muntrace(); char *msg_buffer = dump_xml_node(graph, FALSE); fprintf(stdout, "%s\n", msg_buffer); fflush(stdout); crm_free(msg_buffer); return 0; } diff --git a/crm/pengine/utils.c b/crm/pengine/utils.c index b9a943b8bc..a901787e2a 100644 --- a/crm/pengine/utils.c +++ b/crm/pengine/utils.c @@ -1,1009 +1,1009 @@ #include -#include -#include -#include #include +#include +#include +#include + #include -#include #include #include void print_str_str(gpointer key, gpointer value, gpointer user_data); /* only for rsc_to_rsc constraints */ rsc_to_rsc_t * invert_constraint(rsc_to_rsc_t *constraint) { pdebug("Inverting constraint"); rsc_to_rsc_t *inverted_con = crm_malloc(sizeof(rsc_to_node_t)); inverted_con->id = crm_strdup(constraint->id); inverted_con->strength = constraint->strength; // swap the direction inverted_con->rsc_lh = constraint->rsc_rh; inverted_con->rsc_rh = constraint->rsc_lh; pdebug_action( print_rsc_to_rsc("Inverted constraint", inverted_con, FALSE) ); return inverted_con; } rsc_to_node_t * copy_constraint(rsc_to_node_t *constraint) { rsc_to_node_t *copied_con = crm_malloc(sizeof(rsc_to_node_t)); copied_con->id = crm_strdup(constraint->id); copied_con->rsc_lh = constraint->rsc_lh; copied_con->node_list_rh = constraint->node_list_rh; copied_con->modifier = constraint->modifier; copied_con->weight = constraint->weight; return copied_con; } /* are the contents of list1 and list2 equal */ /* nodes with weight < 0 are ignored */ gboolean node_list_eq(GSListPtr list1, GSListPtr list2) { GSListPtr result = NULL; if(g_slist_length(list1) != g_slist_length(list2)) { return FALSE; } // do stuff cl_log(LOG_ERR, "Not yet implemented"); return g_slist_length(result) != 0; } /* the intersection of list1 and list2 * when merging weights, nodes set to < 0 in either list will always * have their weight set to -1 in the result */ GSListPtr node_list_and(GSListPtr list1, GSListPtr list2) { GSListPtr result = NULL; int lpc = 0; for(lpc = 0; lpc < g_slist_length(list1); lpc++) { node_t *node = (node_t*)g_slist_nth_data(list1, lpc); node_t *new_node = NULL; node_t *other_node = find_list_node(list2, node->details->id); if(node == NULL || other_node == NULL) { continue; // merge node weights } else if(node->weight < 0 || other_node->weight < 0) { new_node = node_copy(node); new_node->weight = -1; } else { new_node = node_copy(node); new_node->weight = node->weight + other_node->weight; if(new_node->weight != 0) { new_node->weight = new_node->weight /2.0; } } result = g_slist_append(result, new_node); } return result; } node_t * find_list_node(GSListPtr list, const char *id) { int lpc = 0; slist_iter( thing, node_t, list, lpc, if(safe_str_eq(thing->details->id, id)) { return thing; } ); return NULL; } /* list1 - list2 */ GSListPtr node_list_minus(GSListPtr list1, GSListPtr list2) { GSListPtr result = NULL; int lpc = 0; slist_iter( node, node_t, list1, lpc, node_t *other_node = find_list_node(list2, node->details->id); if(node == NULL || other_node != NULL) { continue; } node_t *new_node = node_copy(node); result = g_slist_append(result, new_node); ); pdebug("Minus result len: %d", g_slist_length(result)); return result; } /* list1 + list2 - (intersection of list1 and list2) */ GSListPtr node_list_xor(GSListPtr list1, GSListPtr list2) { GSListPtr result = NULL; int lpc = 0; slist_iter( node, node_t, list1, lpc, node_t *other_node = (node_t*)find_list_node(list2, node->details->id); if(node == NULL || other_node != NULL) { continue; } node_t *new_node = node_copy(node); result = g_slist_append(result, new_node); ); slist_iter( node, node_t, list1, lpc, node_t *other_node = (node_t*)find_list_node(list1, node->details->id); if(node == NULL || other_node != NULL) { continue; } node_t *new_node = node_copy(node); result = g_slist_append(result, new_node); ); pdebug("Xor result len: %d", g_slist_length(result)); return result; } GSListPtr node_list_dup(GSListPtr list1) { GSListPtr result = NULL; int lpc = 0; slist_iter( this_node, node_t, list1, lpc, node_t *new_node = node_copy(this_node); if(new_node != NULL) { result = g_slist_append(result, new_node); } ); return result; } node_t * node_copy(node_t *this_node) { if(this_node == NULL) { print_node("Failed copy of", this_node, TRUE); return NULL; } node_t *new_node = crm_malloc(sizeof(node_t)); new_node->weight = this_node->weight; new_node->fixed = this_node->fixed; new_node->details = this_node->details; return new_node; } static int color_id = 0; /* * Create a new color with the contents of "nodes" as the list of * possible nodes that resources with this color can be run on. * * Typically, when creating a color you will provide the node list from * the resource you will first assign the color to. * * If "colors" != NULL, it will be added to that list * If "resources" != NULL, it will be added to every provisional resource * in that list */ color_t * create_color(GSListPtr *colors, GSListPtr nodes, GSListPtr resources) { color_t *new_color = crm_malloc(sizeof(color_t)); new_color->id = color_id++; new_color->local_weight = 1.0; new_color->details = crm_malloc(sizeof(struct color_shared_s)); new_color->details->id = new_color->id; new_color->details->chosen_node = NULL; new_color->details->candidate_nodes = node_list_dup(nodes); pdebug_action(print_color("Created color", new_color, TRUE)); if(colors != NULL) { *colors = g_slist_append(*colors, new_color); } if(resources != NULL) { /* Add any new color to the list of candidate_colors for * resources that havent been decided yet */ int lpc; slist_iter( rsc, resource_t, resources, lpc, if(rsc->provisional && rsc->runnable) { color_t *color_copy = (color_t *) cl_malloc(sizeof(color_t)); color_copy->id = new_color->id; color_copy->details = new_color->details; color_copy->local_weight = 1.0; rsc->candidate_colors = g_slist_append(rsc->candidate_colors, color_copy); } ); } return new_color; } /* * Remove any nodes with a -ve weight */ gboolean filter_nodes(resource_t *rsc) { int lpc2 = 0; pdebug_action(print_resource("Filtering nodes for", rsc, FALSE)); slist_iter( node, node_t, rsc->allowed_nodes, lpc2, if(node == NULL) { cl_log(LOG_ERR, "Invalid NULL node"); } else if(node->weight < 0.0 || node->details->online == FALSE || node->details->type == node_ping) { pdebug_action(print_node("Removing", node, FALSE)); rsc->allowed_nodes = g_slist_remove(rsc->allowed_nodes,node); crm_free(node); lpc2--; } ); return TRUE; } resource_t * pe_find_resource(GSListPtr rsc_list, const char *id_rh) { int lpc = 0; for(lpc = 0; lpc < g_slist_length(rsc_list); lpc++) { resource_t *rsc = g_slist_nth_data(rsc_list, lpc); if(rsc != NULL && safe_str_eq(rsc->id, id_rh)){ return rsc; } } // error return NULL; } node_t * pe_find_node(GSListPtr nodes, const char *id) { int lpc = 0; for(lpc = 0; lpc < g_slist_length(nodes); lpc++) { node_t *node = g_slist_nth_data(nodes, lpc); if(safe_str_eq(node->details->id, id)) { return node; } } // error return NULL; } gint gslist_color_compare(gconstpointer a, gconstpointer b); color_t * find_color(GSListPtr candidate_colors, color_t *other_color) { GSListPtr tmp = g_slist_find_custom(candidate_colors, other_color, gslist_color_compare); if(tmp != NULL) { return (color_t *)tmp->data; } return NULL; } gint gslist_color_compare(gconstpointer a, gconstpointer b) { const color_t *color_a = (const color_t*)a; const color_t *color_b = (const color_t*)b; if(a == b) { return 0; } else if(a == NULL || b == NULL) { return 1; } else if(color_a->id == color_b->id) { return 0; } return 1; } gint sort_rsc_priority(gconstpointer a, gconstpointer b) { const resource_t *resource1 = (const resource_t*)a; const resource_t *resource2 = (const resource_t*)b; if(a == NULL) return 1; if(b == NULL) return -1; if(resource1->priority > resource2->priority) return -1; if(resource1->priority < resource2->priority) return 1; return 0; } gint sort_cons_strength(gconstpointer a, gconstpointer b) { const rsc_to_rsc_t *rsc_constraint1 = (const rsc_to_rsc_t*)a; const rsc_to_rsc_t *rsc_constraint2 = (const rsc_to_rsc_t*)b; if(a == NULL) return 1; if(b == NULL) return -1; if(rsc_constraint1->strength > rsc_constraint2->strength) return 1; if(rsc_constraint1->strength < rsc_constraint2->strength) return -1; return 0; } gint sort_color_weight(gconstpointer a, gconstpointer b) { const color_t *color1 = (const color_t*)a; const color_t *color2 = (const color_t*)b; if(a == NULL) return 1; if(b == NULL) return -1; if(color1->local_weight > color2->local_weight) return -1; if(color1->local_weight < color2->local_weight) return 1; return 0; } gint sort_node_weight(gconstpointer a, gconstpointer b) { const node_t *node1 = (const node_t*)a; const node_t *node2 = (const node_t*)b; if(a == NULL) return 1; if(b == NULL) return -1; if(node1->weight > node2->weight) return -1; if(node1->weight < node2->weight) return 1; return 0; } action_t * action_new(int id, resource_t *rsc, enum action_tasks task) { action_t *action = (action_t*)crm_malloc(sizeof(action_t)); action->id = id; action->rsc = rsc; action->task = task; action->node = NULL; // fill node in later action->actions_before = NULL; action->actions_after = NULL; action->failure_is_fatal = TRUE; action->discard = FALSE; action->runnable = FALSE; action->processed = FALSE; action->optional = FALSE; action->seen_count = 0; return action; } const char * contype2text(enum con_type type) { const char *result = ""; switch(type) { case type_none: result = "none"; break; case rsc_to_rsc: result = "rsc_to_rsc"; break; case rsc_to_node: result = "rsc_to_node"; break; case rsc_to_attr: result = "rsc_to_attr"; break; case base_weight: result = "base_weight"; break; } return result; }; const char * strength2text(enum con_strength strength) { const char *result = ""; switch(strength) { case ignore: result = "ignore"; break; case must: result = XML_STRENGTH_VAL_MUST; break; case should: result = XML_STRENGTH_VAL_SHOULD; break; case should_not: result = XML_STRENGTH_VAL_SHOULDNOT; break; case must_not: result = XML_STRENGTH_VAL_MUSTNOT; break; case startstop: result = "start/stop"; break; } return result; }; const char * modifier2text(enum con_modifier modifier) { const char *result = ""; switch(modifier) { case modifier_none: result = "modifier_none"; break; case set: result = "set"; break; case inc: result = "inc"; break; case dec: result = "dec"; break; } return result; }; const char * task2text(enum action_tasks task) { const char *result = ""; switch(task) { case no_action: result = "no_action"; break; case stop_rsc: result = "stop"; break; case start_rsc: result = "start"; break; case shutdown_crm: result = "shutdown_crm"; break; case stonith_op: result = "stonith"; break; } return result; }; void print_node(const char *pre_text, node_t *node, gboolean details) { if(node == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%s%sNode %s: (weight=%f, fixed=%s)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", node->details==NULL?"error ":node->details->online?"":"Unavailable/Unclean ", node->details->id, node->weight, node->fixed?"True":"False"); if(details && node->details != NULL) { char *mutable = crm_strdup("\t\t"); cl_log(LOG_DEBUG, "\t\t===Node Attributes"); g_hash_table_foreach(node->details->attrs, print_str_str, mutable); crm_free(mutable); } if(details) { int lpc = 0; cl_log(LOG_DEBUG, "\t\t===Node Attributes"); slist_iter( rsc, resource_t, node->details->running_rsc, lpc, print_resource("\t\t", rsc, FALSE); ); } }; /* * Used by the HashTable for-loop */ void print_str_str(gpointer key, gpointer value, gpointer user_data) { cl_log(LOG_DEBUG, "%s%s %s ==> %s", user_data==NULL?"":(char*)user_data, user_data==NULL?"":": ", (char*)key, (char*)value); } void print_color_details(const char *pre_text, struct color_shared_s *color, gboolean details) { if(color == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%sColor %d: node=%s (from %d candidates)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", color->id, color->chosen_node==NULL?"":color->chosen_node->details->id, g_slist_length(color->candidate_nodes)); if(details) { int lpc = 0; slist_iter(node, node_t, color->candidate_nodes, lpc, print_node("\t", node, FALSE)); } } void print_color(const char *pre_text, color_t *color, gboolean details) { if(color == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%sColor %d: (weight=%f, node=%s, possible=%d)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", color->id, color->local_weight, color->details->chosen_node==NULL?"":color->details->chosen_node->details->id, g_slist_length(color->details->candidate_nodes)); if(details) { print_color_details("\t", color->details, details); } } void print_rsc_to_node(const char *pre_text, rsc_to_node_t *cons, gboolean details) { if(cons == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%s%s Constraint %s (%p):", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", "rsc_to_node", cons->id, cons); if(details == FALSE) { cl_log(LOG_DEBUG, "\t%s --> %s, %f (node placement rule)", cons->rsc_lh->id, modifier2text(cons->modifier), cons->weight); int lpc = 0; slist_iter( node, node_t, cons->node_list_rh, lpc, print_node("\t\t-->", node, FALSE) ); } } void print_rsc_to_rsc(const char *pre_text, rsc_to_rsc_t *cons, gboolean details) { if(cons == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%s%s Constraint %s (%p):", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", "rsc_to_rsc", cons->id, cons); if(details == FALSE) { cl_log(LOG_DEBUG, "\t%s --> %s, %s", cons->rsc_lh==NULL?"null":cons->rsc_lh->id, cons->rsc_rh==NULL?"null":cons->rsc_rh->id, strength2text(cons->strength)); } } void print_resource(const char *pre_text, resource_t *rsc, gboolean details) { if(rsc == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } cl_log(LOG_DEBUG, "%s%s%s%sResource %s: (priority=%f, color=%d, now=%s)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", rsc->provisional?"Provisional ":"", rsc->runnable?"":"(Non-Startable) ", rsc->id, (double)rsc->priority, safe_val3(-1, rsc, color, id), safe_val4(NULL, rsc, cur_node, details, id)); cl_log(LOG_DEBUG, "\t%d candidate colors, %d allowed nodes, %d rsc_cons and %d node_cons", g_slist_length(rsc->candidate_colors), g_slist_length(rsc->allowed_nodes), g_slist_length(rsc->rsc_cons), g_slist_length(rsc->node_cons)); if(details) { int lpc = 0; cl_log(LOG_DEBUG, "\t=== Actions"); print_action("\tStop: ", rsc->stop, FALSE); print_action("\tStart: ", rsc->start, FALSE); cl_log(LOG_DEBUG, "\t=== Colors"); slist_iter( color, color_t, rsc->candidate_colors, lpc, print_color("\t", color, FALSE) ); cl_log(LOG_DEBUG, "\t=== Allowed Nodes"); slist_iter( node, node_t, rsc->allowed_nodes, lpc, print_node("\t", node, FALSE); ); } } void print_action(const char *pre_text, action_t *action, gboolean details) { if(action == NULL) { cl_log(LOG_DEBUG, "%s%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", __FUNCTION__); return; } switch(action->task) { case stonith_op: case shutdown_crm: cl_log(LOG_DEBUG, "%s%s%sAction %d: %s @ %s", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", action->discard?"Discarded ":action->optional?"Optional ":action->runnable?action->processed?"":"(Provisional) ":"!!Non-Startable!! ", action->id, task2text(action->task), safe_val4(NULL, action, node, details, id)); break; default: cl_log(LOG_DEBUG, "%s%s%sAction %d: %s %s @ %s", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", action->optional?"Optional ":action->runnable?action->processed?"":"(Provisional) ":"!!Non-Startable!! ", action->id, task2text(action->task), safe_val3(NULL, action, rsc, id), safe_val4(NULL, action, node, details, id)); break; } if(details) { int lpc = 0; #if 1 cl_log(LOG_DEBUG, "\t\t====== Preceeding Actions"); slist_iter( other, action_wrapper_t, action->actions_before, lpc, print_action("\t\t", other->action, FALSE); ); cl_log(LOG_DEBUG, "\t\t====== Subsequent Actions"); slist_iter( other, action_wrapper_t, action->actions_after, lpc, print_action("\t\t", other->action, FALSE); ); #else cl_log(LOG_DEBUG, "\t\t====== Subsequent Actions"); slist_iter( other, action_wrapper_t, action->actions_after, lpc, print_action("\t\t", other->action, FALSE); ); #endif cl_log(LOG_DEBUG, "\t\t====== End"); } else { cl_log(LOG_DEBUG, "\t\t(seen=%d, before=%d, after=%d)", action->seen_count, g_slist_length(action->actions_before), g_slist_length(action->actions_after)); } } xmlNodePtr action2xml(action_t *action) { xmlNodePtr action_xml = NULL; if(action == NULL) { return NULL; } switch(action->task) { case stonith_op: action_xml = create_xml_node(NULL, "pseduo_event"); break; case shutdown_crm: action_xml = create_xml_node(NULL, "crm_event"); break; default: action_xml = create_xml_node(NULL, "rsc_op"); add_node_copy(action_xml, action->rsc->xml); break; } set_xml_property_copy(action_xml, XML_LRM_ATTR_TARGET, safe_val4(NULL, action, node, details, id)); set_xml_property_copy(action_xml, XML_ATTR_ID, crm_itoa(action->id)); set_xml_property_copy(action_xml, XML_LRM_ATTR_RUNNABLE, action->runnable?XML_BOOLEAN_TRUE:XML_BOOLEAN_FALSE); set_xml_property_copy(action_xml, XML_LRM_ATTR_OPTIONAL, action->optional?XML_BOOLEAN_TRUE:XML_BOOLEAN_FALSE); set_xml_property_copy(action_xml, XML_LRM_ATTR_TASK, task2text(action->task)); set_xml_property_copy(action_xml, XML_LRM_ATTR_DISCARD, action->discard?XML_BOOLEAN_TRUE:XML_BOOLEAN_FALSE); set_xml_property_copy(action_xml, "allow_fail", action->failure_is_fatal?XML_BOOLEAN_FALSE:XML_BOOLEAN_TRUE); return action_xml; } gboolean ghash_free_str_str(gpointer key, gpointer value, gpointer user_data); void pe_free_nodes(GSListPtr nodes) { while(nodes != NULL){ GSListPtr list_item = nodes; node_t *node = (node_t*)list_item->data; struct node_shared_s *details = node->details; nodes = nodes->next; if(details != NULL) { crm_free(details->id); g_hash_table_foreach_remove(details->attrs, ghash_free_str_str, NULL); crm_free(details); } crm_free(node); } g_slist_free(nodes); } gboolean ghash_free_str_str(gpointer key, gpointer value, gpointer user_data) { crm_free(key); crm_free(value); return TRUE; } void pe_free_colors(GSListPtr colors) { while(colors != NULL) { GSListPtr list_item = colors; color_t *color = (color_t *)list_item->data; struct color_shared_s *details = color->details; colors = colors->next; if(details != NULL) { pe_free_shallow(details->candidate_nodes); crm_free(details->chosen_node); crm_free(details); } crm_free(color); } g_slist_free(colors); } void pe_free_shallow(GSListPtr alist) { pe_free_shallow_adv(alist, TRUE); } void pe_free_shallow_adv(GSListPtr alist, gboolean with_data) { GSListPtr item; GSListPtr item_next = alist; while(item_next != NULL) { item = item_next; item_next = item_next->next; if(with_data) { crm_free(item->data); } item->data = NULL; item->next = NULL; g_slist_free(item); } } void pe_free_resources(GSListPtr resources) { volatile GSListPtr list_item = NULL; resource_t *rsc = NULL; while(resources != NULL) { list_item = resources; rsc = (resource_t *)list_item->data; resources = resources->next; crm_free(rsc->id); // pdebug("color"); // crm_free(rsc->color); int lpc; slist_iter(clr, color_t, rsc->candidate_colors, lpc, print_color("deleting", clr, FALSE)); // pe_free_shallow(rsc->candidate_colors); pe_free_shallow(rsc->allowed_nodes); while(rsc->rsc_cons) { pe_free_rsc_to_rsc((rsc_to_rsc_t*)rsc->rsc_cons->data); rsc->rsc_cons = rsc->rsc_cons->next; } g_slist_free(rsc->rsc_cons); crm_free(rsc); } g_slist_free(resources); } void pe_free_actions(GSListPtr actions) { while(actions != NULL) { GSListPtr list_item = actions; action_t *action = (action_t *)list_item->data; actions = actions->next; pe_free_shallow(action->actions_before); // action_warpper_t* pe_free_shallow(action->actions_after); // action_warpper_t* action->actions_before = NULL; action->actions_after = NULL; crm_free(action); } g_slist_free(actions); } void pe_free_rsc_to_rsc(rsc_to_rsc_t *cons) { if(cons != NULL) { crm_free(cons->id); crm_free(cons); } } void pe_free_rsc_to_node(rsc_to_node_t *cons) { if(cons != NULL) { crm_free(cons->id); pe_free_shallow(cons->node_list_rh); // node_t* crm_free(cons); } } diff --git a/crm/tengine/tengine.c b/crm/tengine/tengine.c index 0dcf734520..a606bf3cf2 100644 --- a/crm/tengine/tengine.c +++ b/crm/tengine/tengine.c @@ -1,685 +1,703 @@ +/* $Id: tengine.c,v 1.14 2004/06/02 11:48:10 andrew Exp $ */ +/* + * 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 + */ #include -#include -#include -#include #include +#include +#include +#include #include GSListPtr graph = NULL; IPC_Channel *crm_ch = NULL; typedef struct action_list_s { gboolean force; int index; int index_max; GSListPtr actions; } action_list_t; void print_state(void); gboolean initialize_graph(void); gboolean unpack_graph(xmlNodePtr xml_graph); gboolean extract_event(xmlNodePtr msg); gboolean initiate_transition(void); gboolean initiate_action(action_list_t *list); gboolean process_graph_event(const char *event_node, const char *event_rsc, const char *event_action, const char *event_status, const char *event_rc); void send_success(void); void send_abort(xmlNodePtr msg); gboolean initialize_graph(void) { while(g_slist_length(graph) > 0) { action_list_t *action_list = g_slist_nth_data(graph, 0); while(g_slist_length(action_list->actions) > 0) { xmlNodePtr action = g_slist_nth_data(action_list->actions, 0); action_list->actions = g_slist_remove(action_list->actions, action); free_xml(action); } graph = g_slist_remove(graph, action_list); crm_free(action_list); } graph = NULL; return TRUE; } gboolean unpack_graph(xmlNodePtr xml_graph) { /* */ xmlNodePtr xml_action_list = xml_graph?xml_graph->children:NULL; if(xml_action_list == NULL) { // nothing to do return FALSE; } while(xml_action_list != NULL) { xmlNodePtr xml_obj = xml_action_list; xmlNodePtr xml_action = xml_obj->children; action_list_t *action_list = (action_list_t*) crm_malloc(sizeof(action_list_t)); xml_action_list = xml_action_list->next; action_list->force = FALSE; action_list->index = -1; action_list->index_max = 0; action_list->actions = NULL; while(xml_action != NULL) { xmlNodePtr action = copy_xml_node_recursive(xml_action); action_list->actions = g_slist_append(action_list->actions, action); action_list->index_max++; xml_action = xml_action->next; } graph = g_slist_append(graph, action_list); } return TRUE; } gboolean extract_event(xmlNodePtr msg) { gboolean abort = FALSE; xmlNodePtr iter = NULL; const char *section = NULL; const char *event_action = NULL; const char *event_node = NULL; const char *event_rsc = NULL; const char *event_status = NULL; const char *event_rc = NULL; /* [cib fragment] ... */ iter = find_xml_node(msg, XML_TAG_FRAGMENT); section = xmlGetProp(iter, XML_ATTR_SECTION); if(safe_str_neq(section, XML_CIB_TAG_STATUS)) { // these too are never expected return FALSE; } iter = find_xml_node(iter, XML_TAG_CIB); iter = get_object_root(XML_CIB_TAG_STATUS, iter); iter = iter->children; while(abort == FALSE && iter != NULL) { xmlNodePtr node_state = iter; xmlNodePtr child = iter->children; const char *state = xmlGetProp( node_state, XML_CIB_ATTR_JOINSTATE); iter = iter->next; if(xmlGetProp(node_state, XML_CIB_ATTR_SHUTDOWN) != NULL || xmlGetProp(node_state, XML_CIB_ATTR_STONITH) != NULL) { abort = TRUE; } else if(state != NULL && child == NULL) { /* node state update, * possibly from a shutdown we requested */ event_status = state; event_node = xmlGetProp(node_state, XML_ATTR_ID); if(safe_str_eq(event_status, CRMD_JOINSTATE_DOWN)) { event_action = XML_CIB_ATTR_SHUTDOWN; } else { // never expected... yet. STONITH? event_action = "startup"; } abort = !process_graph_event(event_node, event_rsc, event_action, event_status, event_rc); } else if(state == NULL && child != NULL) { child = find_xml_node(node_state, XML_CIB_TAG_LRM); child = find_xml_node(child, XML_LRM_TAG_RESOURCES); if(child != NULL) { child = child->children; } else { abort = TRUE; } while(abort == FALSE && child != NULL) { event_action = xmlGetProp( child, XML_LRM_ATTR_LASTOP); event_node = xmlGetProp( child, XML_LRM_ATTR_TARGET); event_rsc = xmlGetProp( child, XML_ATTR_ID); event_status = xmlGetProp( child, XML_LRM_ATTR_OPSTATE); event_rc = xmlGetProp( child, XML_LRM_ATTR_OPCODE); abort = !process_graph_event(event_node, event_rsc, event_action, event_status, event_rc); child = child->next; } } else if(state != NULL && child != NULL) { /* this is a complex event and could not be completely * due to any request we made */ abort = TRUE; } else { /* ignore */ } } return !abort; } gboolean process_graph_event(const char *event_node, const char *event_rsc, const char *event_action, const char *event_status, const char *event_rc) { int lpc; xmlNodePtr action = NULL; // or xmlNodePtr next_action = NULL; action_list_t *matched_action_list = NULL; // Find the action corresponding to this event slist_iter( action_list, action_list_t, graph, lpc, action = g_slist_nth_data(action_list->actions, action_list->index); if(action == NULL) { continue; } /* */ const char *this_action = xmlGetProp( action, XML_LRM_ATTR_TASK); const char *this_node = xmlGetProp( action, XML_LRM_ATTR_TARGET); const char *this_rsc = xmlGetProp( action->children, XML_ATTR_ID); if(safe_str_neq(this_node, event_node)) { continue; } else if(safe_str_neq(this_action, event_action)) { continue; } else if(safe_str_eq(action->name, "rsc_op") && safe_str_eq(this_rsc, event_rsc)) { matched_action_list = action_list; } else if(safe_str_eq(action->name, "crm_event")) { matched_action_list = action_list; } ); if(matched_action_list == NULL) { // unexpected event, trigger a pe-recompute // possibly do this only for certain types of actions cl_log(LOG_ERR, "Unexpected event... matched action list was NULL"); return FALSE; } // how do we distinguish action failure? if(safe_str_neq(event_rc, "0")){ if(safe_str_neq((const char*)xmlGetProp(action, "allow_fail"), XML_BOOLEAN_TRUE)) { cl_log(LOG_ERR, "Action %s to %s on %s resulted in failure..." " aborting transition.", event_action, event_rsc, event_node); return FALSE; } } while(matched_action_list->index <= matched_action_list->index_max) { gboolean passed = FALSE; next_action = g_slist_nth_data(matched_action_list->actions, matched_action_list->index); passed = initiate_action(matched_action_list); if(passed == FALSE) { cl_log(LOG_ERR, "Initiation of next event failed"); return FALSE; } else if(matched_action_list->index > matched_action_list->index_max) { /* last action in that list, check if there are * anymore actions at all */ slist_iter( action_list, action_list_t, graph, lpc, if(action_list->index <= action_list->index_max){ return TRUE; } ); } else { return TRUE; } } cl_log(LOG_INFO, "Transition complete..."); send_success(); return TRUE; } gboolean initiate_transition(void) { int lpc; gboolean anything = FALSE; FNIN(); slist_iter( action_list, action_list_t, graph, lpc, if(initiate_action(action_list) && action_list->index <= action_list->index_max) { anything = TRUE; } ); FNRET(anything); } gboolean initiate_action(action_list_t *list) { gboolean is_optional = TRUE; xmlNodePtr xml_action = NULL; const char *on_node = NULL; const char *id = NULL; const char *runnable = NULL; const char *optional = NULL; const char *task = NULL; const char *discard = NULL; while(TRUE) { list->index++; xml_action = g_slist_nth_data(list->actions, list->index); if(xml_action == NULL) { cl_log(LOG_INFO, "No tasks left on this list"); list->index = list->index_max + 1; return TRUE; } discard = xmlGetProp(xml_action, XML_LRM_ATTR_DISCARD); on_node = xmlGetProp(xml_action, XML_LRM_ATTR_TARGET); id = xmlGetProp(xml_action, XML_ATTR_ID); runnable = xmlGetProp(xml_action, XML_LRM_ATTR_RUNNABLE); optional = xmlGetProp(xml_action, XML_LRM_ATTR_OPTIONAL); task = xmlGetProp(xml_action, XML_LRM_ATTR_TASK); if(safe_str_eq(discard, XML_BOOLEAN_TRUE)) { cl_log(LOG_INFO, "Skipping discarded rsc-op (%s): %s %s on %s", id, task, xmlGetProp(xml_action->children, XML_ATTR_ID), on_node); continue; } if(safe_str_neq(optional, XML_BOOLEAN_TRUE)) { is_optional = FALSE; } list->force = list->force || !is_optional; /* cl_log(LOG_DEBUG, "Processing action %s (id=%s) on %s", task, id, on_node); */ if(list->force && is_optional) { cl_log(LOG_INFO, "Forcing execution of otherwise optional task " "due to a dependancy on a previous action"); } if(list->force == FALSE && is_optional) { if(safe_str_eq(xml_action->name, "rsc_op")){ cl_log(LOG_INFO, "Skipping optional rsc-op (%s):" " %s %s on %s", id, task, xmlGetProp(xml_action->children, XML_ATTR_ID), on_node); } else { cl_log(LOG_INFO, "Skipping optional command" " %s (id=%s) on %s", task, id, on_node); } } else if(safe_str_eq(runnable, XML_BOOLEAN_FALSE)) { cl_log(LOG_ERR, "Terminated transition on un-runnable command:" " %s (id=%s) on %s", task, id, on_node); return FALSE; } else if(id == NULL || strlen(id) == 0 || on_node == NULL || strlen(on_node) == 0 || task == NULL || strlen(task) == 0) { // error cl_log(LOG_ERR, "Failed on corrupted command: %s (id=%s) on %s", task, id, on_node); return FALSE; } else if(safe_str_eq(xml_action->name, "pseduo_event")){ if(safe_str_eq(task, "stonith")){ cl_log(LOG_INFO, "Executing %s (%s) of node %s", task, id, on_node); /* translate this into a stonith op by deisgnated node may need the CIB to determine who is running the stonith resource for this node more liekly, have the pengine find and supply that info */ } else { cl_log(LOG_ERR, "Failed on unsupported %s: " "%s (id=%s) on %s", xml_action->name, task, id, on_node); return FALSE; } } else if(safe_str_eq(xml_action->name, "crm_event")){ /* */ cl_log(LOG_INFO, "Executing crm-event (%s): %s on %s", id, task, on_node); #ifndef TESTING xmlNodePtr options = create_xml_node( NULL, XML_TAG_OPTIONS); set_xml_property_copy(options, XML_ATTR_OP, task); send_ipc_request(crm_ch, options, NULL, on_node, CRM_SYSTEM_CRMD, CRM_SYSTEM_TENGINE, NULL, NULL); free_xml(options); return TRUE; #endif } else if(safe_str_eq(xml_action->name, "rsc_op")){ cl_log(LOG_INFO, "Executing rsc-op (%s): %s %s on %s", id, task, xmlGetProp(xml_action->children, XML_ATTR_ID), on_node); #ifndef TESTING /* ... */ xmlNodePtr options = create_xml_node( NULL, XML_TAG_OPTIONS); xmlNodePtr data = create_xml_node(NULL, "msg_data"); xmlNodePtr rsc_op = create_xml_node(data, "rsc_op"); set_xml_property_copy(options, XML_ATTR_OP, "rsc_op"); set_xml_property_copy(rsc_op, XML_ATTR_ID, id); set_xml_property_copy( rsc_op, XML_LRM_ATTR_TASK, task); set_xml_property_copy( rsc_op, XML_LRM_ATTR_TARGET, on_node); add_node_copy(rsc_op, xml_action->children); send_ipc_request(crm_ch, options, data, on_node, "lrmd", CRM_SYSTEM_TENGINE, NULL, NULL); free_xml(options); free_xml(data); return TRUE; #endif } else { // error cl_log(LOG_ERR, "Failed on unsupported command type: " "%s, %s (id=%s) on %s", xml_action->name, task, id, on_node); return FALSE; } } return FALSE; } FILE *msg_te_strm = NULL; gboolean process_te_message(xmlNodePtr msg, IPC_Channel *sender) { const char *op = get_xml_attr (msg, XML_TAG_OPTIONS, XML_ATTR_OP, FALSE); const char *sys_to = xmlGetProp(msg, XML_ATTR_SYSTO); const char *ref = xmlGetProp(msg, XML_ATTR_REFERENCE); cl_log(LOG_DEBUG, "Processing %s (%s) message", op, ref); #ifdef MSG_LOG if(msg_te_strm == NULL) { msg_te_strm = fopen("/tmp/te.log", "w"); } fprintf(msg_te_strm, "[Input %s]\t%s\n", op, dump_xml_node(msg, FALSE)); fflush(msg_te_strm); #endif if(safe_str_eq(xmlGetProp(msg, XML_ATTR_MSGTYPE), XML_ATTR_RESPONSE) && safe_str_neq(op, CRM_OP_EVENTCC)) { #ifdef MSG_LOG fprintf(msg_te_strm, "[Result ]\tDiscarded\n"); fflush(msg_te_strm); #endif cl_log(LOG_INFO, "Message was a response not a request. Discarding"); return TRUE; } if(op == NULL){ // error } else if(strcmp(op, CRM_OP_HELLO) == 0) { // ignore } else if(sys_to == NULL || strcmp(sys_to, CRM_SYSTEM_TENGINE) != 0) { CRM_DEBUG("Bad sys-to %s", sys_to); return FALSE; } else if(strcmp(op, CRM_OP_TRANSITION) == 0) { CRM_NOTE("Initializing graph..."); initialize_graph(); xmlNodePtr graph = find_xml_node(msg, "transition_graph"); CRM_NOTE("Unpacking graph..."); unpack_graph(graph); CRM_NOTE("Initiating transition..."); if(initiate_transition() == FALSE) { // nothing to be done.. means we're done. cl_log(LOG_INFO, "No actions to be taken..." " transition compelte."); send_success(); } CRM_NOTE("Processing complete..."); } else if(strcmp(op, CRM_OP_EVENTCC) == 0) { const char *true_op = get_xml_attr (msg, XML_TAG_OPTIONS, XML_ATTR_TRUEOP, TRUE); if(true_op == NULL) { cl_log(LOG_ERR, "Illegal update," " the original operation must be specified"); send_abort(msg); } else if(strcmp(true_op, CRM_OP_CREATE) == 0 || strcmp(true_op, CRM_OP_DELETE) == 0 || strcmp(true_op, CRM_OP_REPLACE) == 0 || strcmp(true_op, CRM_OP_WELCOME) == 0 || strcmp(true_op, CRM_OP_SHUTDOWN_REQ) == 0 || strcmp(true_op, CRM_OP_ERASE) == 0) { // these are always unexpected, trigger the PE send_abort(msg); } else if(strcmp(true_op, CRM_OP_UPDATE) == 0) { // this may not be un-expected if(extract_event(msg) == FALSE){ send_abort(msg); } } else { cl_log(LOG_ERR, "Did not expect copy of action %s", op); } } else if(strcmp(op, CRM_OP_ABORT) == 0) { initialize_graph(); } else if(strcmp(op, CRM_OP_QUIT) == 0) { cl_log(LOG_WARNING, "Received quit message, terminating"); exit(0); } return TRUE; } void send_abort(xmlNodePtr msg) { xmlNodePtr options = create_xml_node(NULL, XML_TAG_OPTIONS); print_state(); CRM_NOTE("Sending \"abort\" message"); #ifdef MSG_LOG fprintf(msg_te_strm, "[Result ]\tTransition aborted\n"); fflush(msg_te_strm); #endif set_xml_property_copy(options, XML_ATTR_OP, CRM_OP_TEABORT); send_ipc_request(crm_ch, options, NULL, NULL, CRM_SYSTEM_DC, CRM_SYSTEM_TENGINE, NULL, NULL); free_xml(options); } void send_success(void) { xmlNodePtr options = create_xml_node(NULL, XML_TAG_OPTIONS); print_state(); CRM_NOTE("Sending \"complete\" message"); #ifdef MSG_LOG fprintf(msg_te_strm, "[Result ]\tTransition complete\n"); fflush(msg_te_strm); #endif set_xml_property_copy(options, XML_ATTR_OP, CRM_OP_TECOMPLETE); send_ipc_request(crm_ch, options, NULL, NULL, CRM_SYSTEM_DC, CRM_SYSTEM_TENGINE, NULL, NULL); free_xml(options); } void print_state(void) { int lpc = 0; cl_log(LOG_DEBUG, "#!!#!!# Start Transitioner state"); if(graph == NULL) { cl_log(LOG_DEBUG, "\tEmpty transition graph"); } else { slist_iter( action_list, action_list_t, graph, lpc, cl_log(LOG_DEBUG, "\tAction set %d: %d of %d actions invoked", lpc, action_list->index, action_list->index_max); ); } cl_log(LOG_DEBUG, "#!!#!!# End Transitioner state"); } diff --git a/crm/tengine/tengine.h b/crm/tengine/tengine.h index 7c96d043ff..9775d3c12b 100644 --- a/crm/tengine/tengine.h +++ b/crm/tengine/tengine.h @@ -1,10 +1,12 @@ #ifndef TENGINE__H #define TENGINE__H +#include + extern gboolean process_te_message(xmlNodePtr msg, IPC_Channel *sender); extern IPC_Channel *crm_ch; #endif diff --git a/crm/tengine/tenginemain.c b/crm/tengine/tenginemain.c index 61dd5ea2b4..c72b832f4a 100644 --- a/crm/tengine/tenginemain.c +++ b/crm/tengine/tenginemain.c @@ -1,237 +1,225 @@ -/* $Id: tenginemain.c,v 1.14 2004/06/01 16:12:50 andrew Exp $ */ +/* $Id: tenginemain.c,v 1.15 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include #include -#include - -#include -#include -#include -#include -#include #include -#include -#include -#include - -#include -#include -#include +#include +#include #include #include #define OPTARGS "skrh" #define PID_FILE WORKING_DIR "/transitioner.pid" #define DAEMON_LOG "/var/log/transitioner.log" #define DAEMON_DEBUG "/var/log/transitioner.debug" GMainLoop* mainloop = NULL; const char* crm_system_name = "transitioner"; void usage(const char* cmd, int exit_status); int init_start(void); void tengine_shutdown(int nsig); int main(int argc, char ** argv) { int req_restart = FALSE; int req_status = FALSE; int req_stop = FALSE; int argerr = 0; int flag; cl_log_set_entity(crm_system_name); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); if (0) { send_ipc_message(NULL, NULL); } while ((flag = getopt(argc, argv, OPTARGS)) != EOF) { switch(flag) { case 's': /* Status */ req_status = TRUE; break; case 'k': /* Stop (kill) */ req_stop = TRUE; break; case 'r': /* Restart */ req_restart = TRUE; break; case 'h': /* Help message */ usage(crm_system_name, LSB_EXIT_OK); break; default: ++argerr; break; } } if (optind > argc) { ++argerr; } if (argerr) { usage(crm_system_name,LSB_EXIT_GENERIC); } // read local config file if (req_status){ return init_status(PID_FILE, crm_system_name); } if (req_stop){ return init_stop(PID_FILE); } if (req_restart) { init_stop(PID_FILE); } return init_start(); } int init_start(void) { long pid; ll_cluster_t* hb_fd = NULL; int facility; #ifdef REALTIME_SUPPORT static int crm_realtime = 1; #endif if ((pid = get_running_pid(PID_FILE, NULL)) > 0) { cl_log(LOG_CRIT, "already running: [pid %ld].", pid); exit(LSB_EXIT_OK); } cl_log_set_logfile(DAEMON_LOG); // if (crm_debug()) { cl_log_set_debugfile(DAEMON_DEBUG); // } /* change the logging facility to the one used by heartbeat daemon */ hb_fd = ll_cluster_new("heartbeat"); cl_log(LOG_INFO, "Switching to Heartbeat logger"); if ((facility = hb_fd->llc_ops->get_logfacility(hb_fd))>0) { cl_log_set_facility(facility); } cl_log(LOG_INFO, "Register PID"); register_pid(PID_FILE, FALSE, tengine_shutdown); crm_ch = init_client_ipc_comms(CRM_SYSTEM_CRMD, subsystem_input_dispatch, (void*)process_te_message); if(crm_ch != NULL) { send_hello_message(crm_ch, "1234", CRM_SYSTEM_TENGINE, "0", "1"); /* Create the mainloop and run it... */ mainloop = g_main_new(FALSE); cl_log(LOG_INFO, "Starting %s", crm_system_name); #ifdef REALTIME_SUPPORT if (crm_realtime == 1){ cl_enable_realtime(); }else if (crm_realtime == 0){ cl_disable_realtime(); } cl_make_realtime(SCHED_RR, 5, 64, 64); #endif g_main_run(mainloop); } else { cl_log(LOG_ERR, "Could not connect to the CRMd"); } return_to_orig_privs(); if (unlink(PID_FILE) == 0) { cl_log(LOG_INFO, "[%s] stopped", crm_system_name); } if(crm_ch != NULL) return 0; return 1; } void usage(const char* cmd, int exit_status) { FILE* stream; stream = exit_status ? stderr : stdout; fprintf(stream, "usage: %s [-srkh]" "[-c configure file]\n", cmd); /* fprintf(stream, "\t-d\tsets debug level\n"); */ /* fprintf(stream, "\t-s\tgets daemon status\n"); */ /* fprintf(stream, "\t-r\trestarts daemon\n"); */ /* fprintf(stream, "\t-k\tstops daemon\n"); */ /* fprintf(stream, "\t-h\thelp message\n"); */ fflush(stream); exit(exit_status); } void tengine_shutdown(int nsig) { static int shuttingdown = 0; CL_SIGNAL(nsig, tengine_shutdown); if (!shuttingdown) { shuttingdown = 1; } if (mainloop != NULL && g_main_is_running(mainloop)) { g_main_quit(mainloop); }else{ exit(LSB_EXIT_OK); } } diff --git a/crm/tengine/ttest.c b/crm/tengine/ttest.c index 665a5c46ce..d27c7f126c 100644 --- a/crm/tengine/ttest.c +++ b/crm/tengine/ttest.c @@ -1,152 +1,139 @@ -/* $Id: ttest.c,v 1.3 2004/06/01 16:12:50 andrew Exp $ */ +/* $Id: ttest.c,v 1.4 2004/06/02 11:48:10 andrew Exp $ */ /* * 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 */ #include #include #include #include #include #include #include #include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include +#include #include #include #define OPTARGS "V?i:o:D:C:S:HA:U:M:I:EWRFt:m:a:d:w:c:r:p:s:" #include #include #include extern gboolean unpack_graph(xmlNodePtr xml_graph); extern gboolean initiate_transition(void); extern gboolean initialize_graph(void); int main(int argc, char **argv) { int argerr = 0; int flag; cl_log_set_entity("ttest"); cl_log_enable_stderr(TRUE); cl_log_set_facility(LOG_USER); while (1) { int option_index = 0; static struct option long_options[] = { // Top-level Options {"daemon", 0, 0, 0}, {0, 0, 0, 0} }; flag = getopt_long(argc, argv, OPTARGS, long_options, &option_index); if (flag == -1) break; switch(flag) { case 0: printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; /* a sample test for multiple instance if (digit_optind != 0 && digit_optind != this_option_optind) printf ("digits occur in two different argv-elements.\n"); digit_optind = this_option_optind; printf ("option %c\n", c); */ case 'V': printf("option %d", flag); break; default: printf("?? getopt returned character code 0%o ??\n", flag); ++argerr; break; } } if (optind < argc) { printf("non-option ARGV-elements: "); while (optind < argc) printf("%s ", argv[optind++]); printf("\n"); } if (optind > argc) { ++argerr; } if (argerr) { cl_log(LOG_ERR, "%d errors in option parsing", argerr); } cl_log(LOG_INFO, "=#=#=#=#= Getting XML =#=#=#=#="); mtrace(); CRM_NOTE("Initializing graph..."); initialize_graph(); xmlNodePtr xml_graph = file2xml(stdin); CRM_NOTE("Unpacking graph..."); unpack_graph(xml_graph); CRM_NOTE("Initiating transition..."); if(initiate_transition() == FALSE) { // nothing to be done.. means we're done. cl_log(LOG_INFO, "No actions to be taken..." " transition compelte."); } initialize_graph(); free_xml(xml_graph); muntrace(); CRM_NOTE("Transition complete..."); return 0; }