📄 clientlib.c
字号:
} if (HA_OK != ha_msg_add_int(msg, F_LRM_CALLID, call_id)) { client_log(LOG_ERR, "rsc_cancel_op: can not add call_id"); ha_msg_del(msg); return HA_FAIL; } /* send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, "rsc_cancel_op: can not send msg to lrmd"); return HA_FAIL; } ha_msg_del(msg); rc = get_rc_from_ch(ch_cmd); client_log(LOG_INFO, "rsc_cancel_op: end."); return rc;}static intrsc_flush_ops (lrm_rsc_t* rsc){ int rc; struct ha_msg* msg = NULL; client_log(LOG_INFO, "rsc_flush_ops: start."); /* check whether the channel to lrmd is available */ if (NULL == ch_cmd) { client_log(LOG_ERR, "rsc_flush_ops: ch_mod is null."); return HA_FAIL; } /* check parameter */ if (NULL == rsc) { client_log(LOG_ERR, "rsc_flush_ops: rsc is null."); return HA_FAIL; } /* create the msg of flush ops */ msg = create_lrm_rsc_msg(rsc->id,FLUSHOPS); if ( NULL == msg) { client_log(LOG_ERR, "rsc_flush_ops: can not create msg"); return HA_FAIL; } /* send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, "rsc_flush_ops: can not send msg to lrmd"); return HA_FAIL; } ha_msg_del(msg); rc = get_rc_from_ch(ch_cmd); client_log(LOG_INFO, "rsc_flush_ops: end."); return rc>0?HA_OK:HA_FAIL;}static GList*rsc_get_cur_state (lrm_rsc_t* rsc, state_flag_t* cur_state){ GList* pending_op_list = NULL; struct ha_msg* msg = NULL; struct ha_msg* ret = NULL; struct ha_msg* op_msg = NULL; lrm_op_t* op = NULL; GList* node = NULL; int state; int op_count, i; client_log(LOG_INFO, "rsc_get_cur_state: start."); /* check whether the channel to lrmd is available */ if (NULL == ch_cmd) { client_log(LOG_ERR, "rsc_get_cur_state: ch_mod is null."); return NULL; } /* check paramter */ if (NULL == rsc) { client_log(LOG_ERR, "rsc_get_cur_state: rsc is null."); return NULL; } /* create the msg of get current state of resource */ msg = create_lrm_rsc_msg(rsc->id,GETRSCSTATE); if ( NULL == msg) { client_log(LOG_ERR, "rsc_get_cur_state: can not create msg"); return NULL; } /* send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, "rsc_get_cur_state: can not send msg to lrmd"); return NULL; } ha_msg_del(msg); /* get the return msg */ ret = msgfromIPC_noauth(ch_cmd); if (NULL == ret) { client_log(LOG_ERR, "rsc_get_cur_state: can not receive ret msg"); return NULL; } /* get the state of the resource from the message */ if (HA_OK != ha_msg_value_int(ret, F_LRM_STATE, &state)) { ha_msg_del(ret); client_log(LOG_ERR, "rsc_get_cur_state: can not get state from msg"); return NULL; } *cur_state = (state_flag_t)state; if (LRM_RSC_IDLE == *cur_state) { /* if the state is idle, the last finsihed op returned. */ /* the op is stored in the same msg, just get it out */ op = msg_to_op(ret); if (NULL != op) { op->rsc = lrm_get_rsc( NULL, op->rsc_id ); pending_op_list = g_list_append(pending_op_list, op); } client_log(LOG_INFO, "rsc_get_cur_state: end."); ha_msg_del(ret); return pending_op_list; } if (LRM_RSC_BUSY == *cur_state) { /* if the state is busy, the whole pending op list would be return */ /* the first msg includes the count of pending ops. */ if (HA_OK != ha_msg_value_int(ret, F_LRM_OPCNT, &op_count)) { client_log(LOG_ERR, "rsc_get_cur_state: can not get op count"); ha_msg_del(ret); return NULL; } ha_msg_del(ret); for (i = 0; i < op_count; i++) { /* one msg for one pending op */ op_msg = msgfromIPC_noauth(ch_cmd); if (NULL == op_msg) { client_log(LOG_ERR, "rsc_get_cur_state: can not recieve ret msg"); continue; } op = msg_to_op(op_msg); /* add msg to the return list */ if (NULL != op) { pending_op_list = g_list_append(pending_op_list, op); } ha_msg_del(op_msg); } for (node = g_list_first(pending_op_list); NULL!=node; node = g_list_next(node)) { lrm_op_t* op = (lrm_op_t*) node->data; op->rsc = lrm_get_rsc( NULL, op->rsc_id ); } client_log(LOG_INFO, "rsc_get_cur_state: end."); return pending_op_list; } client_log(LOG_ERR, "rsc_get_cur_state: unkown state from msg"); ha_msg_del(ret); return NULL;}/* * following are the implements of the utility functions */static lrm_op_t*msg_to_op(struct ha_msg* msg){ lrm_op_t* op; const char* op_type; const char* app_name; const char* rsc_id; const char* output; const void* user_data; size_t userdata_len=0; size_t output_len = 0; client_log(LOG_INFO, "msg_to_op: start."); op = g_new0(lrm_op_t, 1); /* op->timeout, op->interval, op->target_rc, op->call_id*/ if (HA_OK != ha_msg_value_int(msg,F_LRM_TIMEOUT, &op->timeout) || HA_OK != ha_msg_value_int(msg,F_LRM_INTERVAL, &op->interval) || HA_OK != ha_msg_value_int(msg,F_LRM_TARGETRC, &op->target_rc) || HA_OK != ha_msg_value_int(msg,F_LRM_CALLID, &op->call_id)) { client_log(LOG_ERR, "msg_to_op: can not get fields."); free_op(op); return NULL; } /* op->op_status */ if (HA_OK != ha_msg_value_int(msg, F_LRM_OPSTATUS, (int*)&op->op_status)) { client_log(LOG_INFO, "msg_to_op: can not get op status from msg."); op->op_status = -1; } /* if it finished successfully */ if (LRM_OP_DONE == op->op_status ) { /* op->rc */ if (HA_OK != ha_msg_value_int(msg, F_LRM_RC, &op->rc)) { free_op(op); client_log(LOG_ERR, "msg_to_op: can not get op rc from msg."); return NULL; } /* op->output */ output = cl_get_binary(msg, F_LRM_DATA,&output_len); if (NULL != output){ int len = strnlen(output, output_len); if (len != output_len) { free_op(op); client_log(LOG_ERR, "msg_to_op: can not get data from msg."); return NULL; } op->output = ha_malloc(len+1); if (NULL == op->output) { free_op(op); client_log(LOG_ERR, "msg_to_op: can't alloc memory for output"); return NULL; } memcpy(op->output, output,len); op->output[len]=0; } else { op->output = NULL; } } /* op->app_name */ app_name = ha_msg_value(msg, F_LRM_APP); if (NULL == app_name) { client_log(LOG_ERR, "msg_to_op: can not get app_name."); free_op(op); return NULL; } op->app_name = g_strdup(app_name); /* op->op_type */ op_type = ha_msg_value(msg, F_LRM_OP); if (NULL == op_type) { client_log(LOG_ERR, "msg_to_op: can not get op_type."); free_op(op); return NULL; } op->op_type = g_strdup(op_type); /* op->rsc_id */ rsc_id = ha_msg_value(msg, F_LRM_RID); if (NULL == rsc_id) { client_log(LOG_ERR, "msg_to_op: can not get rsc_id."); free_op(op); return NULL; } /* op->user_data */ user_data = cl_get_binary(msg, F_LRM_USERDATA,&userdata_len); if (NULL != user_data && 0 < userdata_len ) { op->user_data = ha_malloc(userdata_len); if ( NULL != op->user_data ) { memcpy(op->user_data,user_data,userdata_len); op->user_data_len = userdata_len; } } op->rsc_id = g_strdup(rsc_id); /* op->params */ op->params = ha_msg_value_str_table(msg, F_LRM_PARAM); client_log(LOG_INFO, "msg_to_op: end."); return op;}static struct ha_msg*op_to_msg (lrm_op_t* op){ struct ha_msg* msg = ha_msg_new(5); if (NULL == msg) { client_log(LOG_ERR, "op_to_msg: can not create msg."); return NULL; } if (HA_OK != ha_msg_add(msg, F_LRM_TYPE, PERFORMOP) || HA_OK != ha_msg_add(msg, F_LRM_RID, op->rsc_id) || HA_OK != ha_msg_add(msg, F_LRM_OP, op->op_type) || HA_OK != ha_msg_add_int(msg, F_LRM_TIMEOUT, op->timeout) || HA_OK != ha_msg_add_int(msg, F_LRM_INTERVAL, op->interval) || HA_OK != ha_msg_add_int(msg, F_LRM_TARGETRC, op->target_rc)) { ha_msg_del(msg); client_log(LOG_ERR, "op_to_msg: can not add field."); return NULL; } if (NULL != op->user_data && 0 != op->user_data_len) { if (HA_OK != ha_msg_addbin(msg,F_LRM_USERDATA, op->user_data, op->user_data_len)){ ha_msg_del(msg); client_log(LOG_ERR, "op_to_msg: can not add field."); return NULL; } } if (NULL != op->params) { if (HA_OK != ha_msg_add_str_table(msg,F_LRM_PARAM,op->params)){ ha_msg_del(msg); client_log(LOG_ERR, "op_to_msg: can not add field."); return NULL; } } return msg;}static intget_rc_from_ch(IPC_Channel* ch){ int rc; struct ha_msg* msg; client_log(LOG_INFO, "get_rc_from_ch: start."); msg = msgfromIPC_noauth(ch); if (NULL == msg) { client_log(LOG_ERR, "get_rc_from_ch: can not recieve msg"); return HA_FAIL; } if (HA_OK != ha_msg_value_int(msg, F_LRM_RC, &rc)) { ha_msg_del(msg); client_log(LOG_ERR, "get_rc_from_ch: can not get rc from msg"); return HA_FAIL; } ha_msg_del(msg); client_log(LOG_INFO, "get_rc_from_ch: end."); return rc;}static intget_rc_from_msg(struct ha_msg* msg){ int rc; client_log(LOG_INFO, "get_rc_from_msg: start."); if (NULL == msg) { client_log(LOG_ERR, "get_rc_from_msg: msg is null"); return HA_FAIL; } if (HA_OK != ha_msg_value_int(msg, F_LRM_RC, &rc)) { client_log(LOG_ERR, "get_rc_from_msg: can not get rc from msg"); return HA_FAIL; } client_log(LOG_INFO, "get_rc_from_msg: end."); return rc;}static voidfree_op (lrm_op_t* op){ if (NULL == op) { return; } if (NULL != op->op_type) { g_free(op->op_type); } if (NULL != op->output) { g_free(op->output); } if (NULL != op->rsc_id) { g_free(op->rsc_id); } if (NULL != op->app_name) { g_free(op->app_name); } if (NULL != op->params) { free_str_table(op->params); }} static int debug_level = 0;void set_debug_level(int level){ client_log(LOG_INFO, "set_debug_level: start."); if(0!=level && LOG_INFO!=level && LOG_ERR!=level) { client_log(LOG_ERR, "set_debug_level: wrong parameter"); return; } debug_level = level; client_log(LOG_INFO, "set_debug_level: end.");}static voidclient_log (int priority, const char* fmt){ if (0 == debug_level) { return; } if (LOG_ERR == priority) { printf("client_log:ERR:%s\n",fmt); } else if (LOG_INFO == debug_level) { printf("client_log:INFO:%s\n",fmt); }}const char *execra_code2string(uniform_ret_execra_t code){ switch(code) { case EXECRA_EXEC_UNKNOWN_ERROR: return "unknown exec error"; case EXECRA_NO_RA: return "no RA"; case EXECRA_OK: return "ok"; case EXECRA_UNKNOWN_ERROR: return "unknown error"; case EXECRA_INVALID_PARAM: return "invalid parameter"; case EXECRA_UNIMPLEMENT_FEATURE: return "unimplemented feature"; case EXECRA_INSUFFICIENT_PRIV: return "insufficient privileges"; case EXECRA_NOT_INSTALLED: return "not installed"; case EXECRA_NOT_CONFIGURED: return "not configured"; case EXECRA_NOT_RUNNING: return "not running"; /* For status command only */ case EXECRA_RA_DEAMON_DEAD1: return "status: deamon dead"; case EXECRA_RA_DEAMON_DEAD2: return "status: deamon dead"; case EXECRA_RA_DEAMON_STOPPED: return "status: deamon stopped"; case EXECRA_STATUS_UNKNOWN: return "status: unknown"; default: break; } return "<unknown>";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -