📄 stonithd_lib.c
字号:
} tmpstr = cl_get_string(reply, F_STONITHD_RAOPTYPE); if (tmpstr != NULL) { ra_op->op_type = g_strdup(tmpstr); stdlib_log(LOG_DEBUG, "ra_op->op_type=%s.", ra_op->op_type); } else { stdlib_log(LOG_ERR, "stonithd_receive_ops_result: the " "reply message contains no op_type field."); rc = ST_FAIL; } tmpstr = cl_get_string(reply, F_STONITHD_RANAME); if (tmpstr != NULL) { ra_op->ra_name = g_strdup(tmpstr); stdlib_log(LOG_DEBUG, "ra_op->ra_name=%s.", ra_op->ra_name); } else { stdlib_log(LOG_ERR, "stonithd_receive_ops_result: the " "reply message contains no ra_name field."); rc = ST_FAIL; } ra_op->params = cl_get_hashtable(reply, F_STONITHD_PARAMS); if (ra_op->params != NULL) { stdlib_log(LOG_DEBUG, "ra_op->params address:=%p.", ra_op->params); } else { stdlib_log(LOG_ERR, "stonithd_receive_ops_result: the " "reply contains no parameter field."); rc = ST_FAIL; } if ( ha_msg_value_int(reply, F_STONITHD_CALLID, &tmpint) == HA_OK ) { ra_op->call_id = tmpint; stdlib_log(LOG_DEBUG, "receive_ops_result: " "ra_op->call_id=%d.", ra_op->call_id); } else { stdlib_log(LOG_ERR, "stonithd_receive_ops_result: " "no call_id field in reply"); rc = ST_FAIL; } if ( ha_msg_value_int(reply, F_STONITHD_FRC, &tmpint) == HA_OK ) { ra_op->op_result = tmpint; stdlib_log(LOG_DEBUG, "stonithd_receive_ops_result: " "ra_op->op_result=%d.", ra_op->op_result); } else { stdlib_log(LOG_ERR, "no op_result field in reply"); rc = ST_FAIL; } /* if ( rc == ST_OK && stonithRA_ops_cb != NULL) */ if ( stonithRA_ops_cb != NULL) { stdlib_log(LOG_DEBUG, "trigger stonithRA op callback."); stonithRA_ops_cb(ra_op, stonithRA_ops_cb_private_data); } else { stdlib_log(LOG_DEBUG, "No stonithRA op callback."); } free_stonithRA_ops_t(ra_op); ZAPMSG(reply); return rc; } ZAPMSG(reply); stdlib_log(LOG_DEBUG, "stonithd_receive_ops_result: " "Got an unexpected message."); return ST_FAIL;}intstonithd_set_stonith_ops_callback(stonith_ops_callback_t callback, void * private_data){ if ( SIGNONED_TO_STONITHD == TRUE ) { stonith_ops_cb = callback; stonith_ops_cb_private_data = private_data; stdlib_log(LOG_DEBUG, "setted stonith ops callback."); } else { stdlib_log(LOG_ERR, "stonithd_set_stonith_ops_callback: "\ "failed due to not on signon status."); return ST_FAIL; } return ST_OK;}intstonithd_virtual_stonithRA_ops( stonithRA_ops_t * op, int * call_id){ int rc = ST_FAIL; struct ha_msg * request, * reply; const char * tmpstr; stdlib_log(LOG_DEBUG, "stonithd_virtual_stonithRA_ops: begin"); if (op == NULL) { stdlib_log(LOG_ERR, "stonithd_virtual_stonithRA_ops: op==NULL"); return ST_FAIL; } if (call_id == NULL) { stdlib_log(LOG_ERR, "stonithd_stonithd_stonithRA_ops: " "call_id==NULL"); return ST_FAIL; } if (SIGNONED_TO_STONITHD == FALSE) { stdlib_log(LOG_ERR, "Not in signon status."); return ST_FAIL; } if ( (request = create_basic_reqmsg_fields(ST_RAOP)) == NULL) { return ST_FAIL; } if ( (ha_msg_add(request, F_STONITHD_RSCID, op->rsc_id) != HA_OK) ||(ha_msg_add(request, F_STONITHD_RAOPTYPE, op->op_type) != HA_OK) ||(ha_msg_add(request, F_STONITHD_RANAME, op->ra_name) != HA_OK) ||(ha_msg_addhash(request, F_STONITHD_PARAMS, op->params) != HA_OK) ) { stdlib_log(LOG_ERR, "stonithd_virtual_stonithRA_ops: " "cannot add field to ha_msg."); ZAPMSG(request); return ST_FAIL; } /* Send the request message */ if (msg2ipcchan(request, chan) != HA_OK) { ZAPMSG(request); stdlib_log(LOG_ERR, "can't send stonithRA message to IPC"); return ST_FAIL; } /* waiting for the output to finish */ chan_waitout_timeout(chan, DEFAULT_TIMEOUT); ZAPMSG(request); /* Read the reply... */ stdlib_log(LOG_DEBUG, "waiting for the stonithRA reply msg."); if ( IPC_OK != chan_waitin_timeout(chan, DEFAULT_TIMEOUT) ) { stdlib_log(LOG_ERR, "stonith:waitin failed."); /* how to deal. important */ return ST_FAIL; } if ( (reply = msgfromIPC_noauth(chan)) == NULL ) { stdlib_log(LOG_ERR, "stonithd_virtual_stonithRA_ops: " "to fetch reply msg failed."); return ST_FAIL; } if ( FALSE == is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, F_STONITHD_APIRPL, ST_RRAOP) ) { ZAPMSG(reply); /* avoid to zap the msg ? */ stdlib_log(LOG_DEBUG, "stonithd_virtual_stonithRA_ops: " "Got an unexpected message."); return ST_FAIL; } if ( ((tmpstr = cl_get_string(reply, F_STONITHD_APIRET)) != NULL) && (strncmp(tmpstr, ST_APIOK, strlen(ST_APIOK)) == 0) ) { int tmpint; if ( ha_msg_value_int(reply, F_STONITHD_CALLID, &tmpint) == HA_OK ) { *call_id = tmpint; rc = ST_OK; stdlib_log(LOG_DEBUG, "a stonith RA operation queue " \ "to run, call_id=%d.", *call_id); } else { stdlib_log(LOG_ERR, "no return call_id in reply"); rc = ST_FAIL; } } else { stdlib_log(LOG_DEBUG, "failed to do the RA op."); rc = ST_FAIL; * call_id = -1; } ZAPMSG(reply); return rc;}intstonithd_set_stonithRA_ops_callback(stonithRA_ops_callback_t callback, void * private_data){ if ( SIGNONED_TO_STONITHD == TRUE ) { stonithRA_ops_cb = callback; stonithRA_ops_cb_private_data = private_data; stdlib_log(LOG_DEBUG, "setted stonith ops callback."); } else { stdlib_log(LOG_ERR, "stonithd_set_stonithRA_ops_callback: " "failed due to not on signon status."); return ST_FAIL; } return ST_OK;}int stonithd_list_stonith_types(GList ** types){ int rc = ST_FAIL; struct ha_msg * request, * reply; const char * tmpstr; if (SIGNONED_TO_STONITHD == FALSE) { stdlib_log(LOG_ERR, "Not in signon status."); return ST_FAIL; } if (*types != NULL) { stdlib_log(LOG_ERR, "stonithd_list_stonith_types: *types!=NULL," " Will casue memory leak."); *types = NULL; } if ( (request = create_basic_reqmsg_fields(ST_LTYPES)) == NULL) { return ST_FAIL; } /* Send the request message */ if (msg2ipcchan(request, chan) != HA_OK) { ZAPMSG(request); stdlib_log(LOG_ERR, "can't send stonithRA message to IPC"); return ST_FAIL; } /* waiting for the output to finish */ chan_waitout_timeout(chan, DEFAULT_TIMEOUT); ZAPMSG(request); /* Read the reply... */ stdlib_log(LOG_DEBUG, "waiting for the reply to list stonith types."); if ( IPC_OK != chan_waitin_timeout(chan, DEFAULT_TIMEOUT) ) { stdlib_log(LOG_ERR, "stonithd_list_stonith_types: " "chan_waitin failed."); /* how to deal. important */ return ST_FAIL; } if ( (reply = msgfromIPC_noauth(chan)) == NULL ) { stdlib_log(LOG_ERR, "stonithd_list_stonith_types: " "failed to fetch reply."); return ST_FAIL; } if ( TRUE == is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, F_STONITHD_APIRPL, ST_RLTYPES) ) { if ( ((tmpstr = cl_get_string(reply, F_STONITHD_APIRET)) != NULL) && (strncmp(tmpstr, ST_APIOK, strlen(ST_APIOK)) == 0) ) { int i, len; if ((len=cl_msg_list_length(reply, F_STONITHD_STTYPES)) < 0) { stdlib_log(LOG_ERR, "Not field to list stonith " "types."); } else { for (i = 0; i < len; i++) { tmpstr = cl_msg_list_nth_data(reply, F_STONITHD_STTYPES, i); *types = g_list_append(*types, g_strdup(tmpstr)); } stdlib_log(LOG_DEBUG, "got stonith types."); rc = ST_OK; } } else { stdlib_log(LOG_DEBUG, "failed to get stonith types."); } } else { stdlib_log(LOG_DEBUG, "stonithd_list_stonith_types: " "Got an unexpected message."); } ZAPMSG(reply); return rc;}static struct ha_msg * create_basic_reqmsg_fields(const char * apitype){ struct ha_msg * msg = NULL; if ((msg = ha_msg_new(4)) == NULL) { stdlib_log(LOG_ERR, "create_basic_msg_fields:out of memory."); return NULL; } /* important error check client name length */ if ( (ha_msg_add(msg, F_STONITHD_TYPE, ST_APIREQ ) != HA_OK ) ||( ha_msg_add(msg, F_STONITHD_APIREQ, apitype) != HA_OK) ||( ha_msg_add(msg, F_STONITHD_CNAME, CLIENT_NAME) != HA_OK) ||( ha_msg_add_int(msg, F_STONITHD_CPID, CLIENT_PID) != HA_OK) ) { stdlib_log(LOG_ERR, "create basic msg fields: " "cannot add field to ha_msg."); ZAPMSG(msg); } return msg;}static gboolean is_expected_msg(const struct ha_msg * msg, const char * field_name1, const char * field_content1, const char * field_name2, const char * field_content2 ){ const char * tmpstr; gboolean rc = FALSE; if ( msg == NULL ) { stdlib_log(LOG_ERR, "is_expected _msg: msg==NULL"); } if ( ( (tmpstr = cl_get_string(msg, field_name1)) != NULL ) && (strncmp(tmpstr, field_content1, strlen(field_content1))==0) ) { stdlib_log(LOG_DEBUG, "%s = %s", field_name1, tmpstr); if ( ( (tmpstr = cl_get_string(msg, field_name2)) != NULL ) && strncmp(tmpstr, field_content2, strlen(field_content2)) == 0) { stdlib_log(LOG_DEBUG, "%s = %s.", field_name2, tmpstr); rc= TRUE; } else { stdlib_log(LOG_DEBUG, "no field %s.", field_name2); } } else { stdlib_log(LOG_DEBUG, "No field %s", field_name1); } return rc;}static voidsigalarm_handler(int signum){ if ( signum == SIGALRM ) { INT_BY_ALARM = TRUE; }}static intchan_waitin_timeout(IPC_Channel * chan, unsigned int timeout){ int ret; unsigned int other_remaining; sighandler_t old_handler; other_remaining = alarm(0); if ( other_remaining > 0 ) { alarm(other_remaining); stdlib_log(LOG_NOTICE, "chan_waitin_timeout: There are others " "using timer:%d. I donnot use alarm.", other_remaining); alarm(other_remaining); ret = chan->ops->waitin(chan); } else { old_handler = signal(SIGALRM, sigalarm_handler); INT_BY_ALARM = FALSE; alarm(timeout); ret = chan->ops->waitin(chan); if ( ret == IPC_INTR && INT_BY_ALARM ) { stdlib_log(LOG_ERR, "chan_waitin_timeout: waitin was " "interrupted by alarm signal."); } else { alarm(0); } signal(SIGALRM, old_handler); stdlib_log(LOG_DEBUG, "chan_waitin_timeout: ret=%d.", ret); } return ret;}static intchan_waitout_timeout(IPC_Channel * chan, unsigned int timeout){ int ret; unsigned int other_remaining = 0; sighandler_t old_handler; other_remaining = alarm(0); if ( other_remaining > 0 ) { alarm(other_remaining); stdlib_log(LOG_NOTICE, "chan_waitout_timeout: There are others " "using timer, I donnot use alarm."); ret = chan->ops->waitout(chan); } else { old_handler = signal(SIGALRM, sigalarm_handler); INT_BY_ALARM = FALSE; alarm(timeout); ret = chan->ops->waitout(chan); if ( ret == IPC_INTR && INT_BY_ALARM ) { stdlib_log(LOG_ERR, "chan_waitout_timeout: waitout was " "interrupted by alarm setted by myself."); } else { alarm(0); } signal(SIGALRM, old_handler); stdlib_log(LOG_DEBUG, "chan_waitout_timeout: ret=%d.", ret); } return ret;}void stdlib_enable_debug_mode(void){ DEBUG_MODE = TRUE;}/* copied from cl_log.c, need to be the same */#ifndef MAXLINE# define MAXLINE 512#endifstatic voidstdlib_log(int priority, const char * fmt, ...){ va_list ap; char buf[MAXLINE]; if ( DEBUG_MODE == FALSE && priority == LOG_DEBUG ) { return; } va_start(ap, fmt); vsnprintf(buf, sizeof(buf)-1, fmt, ap); va_end(ap); cl_log(priority, "%s", buf);}static voidfree_stonithRA_ops_t(stonithRA_ops_t * ra_op){ if (ra_op == NULL) { stdlib_log(LOG_DEBUG, "free_stonithRA_ops_t: ra_op==NULL"); return; } ZAPGDOBJ(ra_op->rsc_id); ZAPGDOBJ(ra_op->ra_name); ZAPGDOBJ(ra_op->op_type); /* Has used g_hash_table_new_full to create params */ g_hash_table_destroy(ra_op->params); ZAPGDOBJ(ra_op);}static voidfree_stonith_ops_t(stonith_ops_t * st_op){ if (st_op == NULL) { stdlib_log(LOG_DEBUG, "free_stonith_ops_t: st_op==NULL"); return; } ZAPGDOBJ(st_op->node_name); ZAPGDOBJ(st_op->node_list); ZAPGDOBJ(st_op);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -