⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adminmain.c

📁 在LINUX下实现HA的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
{	const char *attr_name = NULL;	const char *attr_value = NULL;	xmlNodePtr cib_object = file2xml(stdin);	xmlNodePtr fragment = NULL;	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_DEBUG("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;}intdo_work(ll_cluster_t * hb_cluster){	/* construct the request */	xmlNodePtr msg_data = NULL;	const char *dest_node = NULL;	gboolean all_is_good = TRUE;		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_OPERATION_QUERY, cib_action) == 0) {			char *obj_type_parent = NULL;			cl_log(LOG_DEBUG, "Querying the CIB");			obj_type_parent = pluralSection(obj_type);						CRM_DEBUG2("Querying the CIB for section: %s",				   obj_type_parent);						set_xml_property_copy(msg_options, XML_ATTR_OP, CRM_OPERATION_QUERY);			set_xml_property_copy(msg_options, XML_ATTR_FILTER_ID,					      obj_type_parent);						dest_node = status;			CRM_DEBUG2("CIB query creation %s",				   msg_data == NULL ? "failed." : "passed.");						sys_to = CRM_SYSTEM_DCIB;					} else if (strcmp(CRM_OPERATION_ERASE, cib_action) == 0) {			set_xml_property_copy(msg_options,					      XML_ATTR_OP,					      CRM_OPERATION_ERASE);						dest_node = status;			CRM_DEBUG("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_DEBUG("Querying the system");		sys_to = CRM_SYSTEM_DC;		if (status != NULL) {			const char *ping_type = NULL;			sys_to = CRM_SYSTEM_CRMD;			ping_type = CRM_OPERATION_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 = cl_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;}voidusage(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;gbooleanadmin_msg_callback(IPC_Channel * server, void *private_data){	int lpc = 0;	IPC_Message *msg = NULL;	xmlNodePtr xml_root_node = NULL;	xmlNodePtr options = NULL;	gboolean hack_return_good = TRUE;	static int received_responses = 0;	char *buffer =NULL;	const char *result = 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_DEBUG("No message this time");			continue;		}		lpc++;		buffer =(char *) msg->msg_body;		CRM_DEBUG2("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... */			char *filename;			/* 31 = "test-_.xml" + an_int_as_string + '\0' */			int filename_len = 31 + strlen(this_msg_reference);			filename = cl_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);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -