📄 clientlib.c
字号:
/* * Client Library for Local Resource Manager API. * * Author: Huang Zhen <zhenh@cn.ibm.com> * Copyright (c) 2004 International Business Machines * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser 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 library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser 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 <portability.h>#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <stdarg.h>#include <string.h>#include <glib.h>#include <heartbeat.h>#include <clplumbing/ipc.h>#include <ha_msg.h>#include <lrm/lrm_api.h>#include <lrm/lrm_msg.h>/*Notice: this define should be replaced when merge to the whole pkg*/#define LRM_MAXPIDLEN 256#define LRM_ID "lrm"/*declare the functions used by the lrm_ops structure*/static int lrm_signon (ll_lrm_t* lrm, const char * app_name);static int lrm_signoff (ll_lrm_t*);static int lrm_delete (ll_lrm_t*);static int lrm_set_lrm_callback (ll_lrm_t* lrm, lrm_op_done_callback_t op_done_callback_func, lrm_monitor_callback_t monitor_callback_func);static GList* lrm_get_ra_supported (ll_lrm_t* lrm);static GList* lrm_get_all_rscs (ll_lrm_t* lrm);static lrm_rsc_t* lrm_get_rsc (ll_lrm_t* lrm, rsc_id_t rsc_id);static int lrm_add_rsc (ll_lrm_t*, rsc_id_t rsc_id, const char* rsc_type ,const char* rsc_name, GHashTable* parameter);static int lrm_delete_rsc (ll_lrm_t*, rsc_id_t rsc_id);static int lrm_inputfd (ll_lrm_t*);static int lrm_msgready (ll_lrm_t*);static int lrm_rcvmsg (ll_lrm_t*, int blocking);struct sys_config * config = NULL;static struct lrm_ops lrm_ops_instance ={ lrm_signon, lrm_signoff, lrm_delete, lrm_set_lrm_callback, lrm_get_ra_supported, lrm_get_all_rscs, lrm_get_rsc, lrm_add_rsc, lrm_delete_rsc, lrm_inputfd, lrm_msgready, lrm_rcvmsg};/*declare the functions used by the lrm_rsc_ops structure*/static int rsc_perform_op (lrm_rsc_t*, lrm_op_t* op);static int rsc_flush_ops (lrm_rsc_t*);static int rsc_set_monitor (lrm_rsc_t*, lrm_mon_t* monitor);static GList* rsc_get_monitors (lrm_rsc_t*);static GList* rsc_get_cur_state (lrm_rsc_t*, state_flag_t* cur_state);static struct rsc_ops rsc_ops_instance ={ rsc_perform_op, rsc_flush_ops, rsc_set_monitor, rsc_get_monitors, rsc_get_cur_state,};/*define the internal data used by the client library*/static int is_signed_on = FALSE;static IPC_Channel* ch_cmd = NULL;static IPC_Channel* ch_cbk = NULL;static lrm_op_done_callback_t op_done_callback = NULL;static lrm_monitor_callback_t monitor_callback = NULL;static GList* op_list = NULL;static GList* mon_list = NULL;/*define some utility functions*/static int on_op_done (int call_id, lrm_op_t* op, struct ha_msg* msg);static int on_monitor (int call_id, lrm_mon_t* mon, struct ha_msg* msg);static lrm_op_t* msg_to_op(struct ha_msg* msg);static lrm_mon_t* lookup_mon(int call_id);static lrm_op_t* lookup_op(int call_id);static int get_rc_from_ch(IPC_Channel* ch);static int get_rc_from_msg(struct ha_msg* msg);static lrm_mon_t* copy_mon(lrm_mon_t* mon_in);static lrm_op_t* copy_op(lrm_op_t* op_in);static void free_op (lrm_op_t* op);static void free_mon (lrm_mon_t* mon);static void client_log (int priority, int level, const char* fmt);void ha_msg_print(struct ha_msg * msg);/*define of the api functions*/ll_lrm_t*ll_lrm_new (const char * llctype){ ll_lrm_t* lrm; client_log(LOG_INFO, 1, "ll_lrm_new: start."); /*check the parameter*/ if (0 != strncmp(LRM_ID, llctype, strlen(LRM_ID))) { client_log(LOG_ERR, -1, "ll_lrm_new: wrong parameter"); return NULL; } /*alloc memory for lrm*/ if (NULL == (lrm = (ll_lrm_t*) g_new(ll_lrm_t,1))) { client_log(LOG_ERR, -1, "ll_lrm_new: can not alloc memory"); return NULL; } /*assign the ops*/ lrm->lrm_ops = &lrm_ops_instance; client_log(LOG_INFO, -1, "ll_lrm_new: end."); return lrm;}intlrm_signon (ll_lrm_t* lrm, const char * app_name){ GHashTable* ch_cmd_attrs; GHashTable* ch_cbk_attrs; struct ha_msg* msg; char path[] = IPC_PATH_ATTR; char cmd_path[] = LRM_CMDPATH; char callback_path[] = LRM_CALLBACKPATH; client_log(LOG_INFO, 1, "lrm_signon: start."); /*check parameters*/ if (NULL == lrm || NULL == app_name) { client_log(LOG_ERR, -1, "lrm_signon: wrong parameter"); return HA_FAIL; } /*if already signed on, sign off first*/ if (is_signed_on) { client_log(LOG_INFO,0, "lrm_signon: the client is alreay signed on,re-sign"); lrm_signoff(lrm); } /*create the command ipc channel to lrmd*/ ch_cmd_attrs = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(ch_cmd_attrs, path, cmd_path); if (NULL == (ch_cmd = ipc_channel_constructor(IPC_ANYTYPE, ch_cmd_attrs))){ lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not connect to lrmd for cmd channel"); return HA_FAIL; } if (IPC_OK != ch_cmd->ops->initiate_connection(ch_cmd)) { lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not initiate connection"); return HA_FAIL; } /*construct the reg msg*/ if (NULL == (msg = create_lrm_reg_msg(app_name))) { lrm_signoff(lrm); client_log(LOG_ERR,-1,"lrm_signon: can not create reg msg"); return HA_FAIL; } /*send the msg*/ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { lrm_signoff(lrm); ha_msg_del(msg); client_log(LOG_ERR,-1,"lrm_signon: can not send msg to lrmd"); return HA_FAIL; } /*parse the return msg*/ if (HA_OK != get_rc_from_ch(ch_cmd)) { lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not recv result from lrmd"); return HA_FAIL; } /*create the callback ipc channel to lrmd*/ ch_cbk_attrs = g_hash_table_new(g_str_hash, g_str_equal); g_hash_table_insert(ch_cbk_attrs, path, callback_path); if (NULL == (ch_cbk = ipc_channel_constructor(IPC_ANYTYPE,ch_cbk_attrs))) { lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not connect to lrmd for callback"); return HA_FAIL; } if (IPC_OK != ch_cbk->ops->initiate_connection(ch_cbk)) { lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not initiate connection"); return HA_FAIL; } /*send the msg*/ if (HA_OK != msg2ipcchan(msg,ch_cbk)) { lrm_signoff(lrm); ha_msg_del(msg); client_log(LOG_ERR,-1, "lrm_signon: can not send msg to lrmd"); return HA_FAIL; } ha_msg_del(msg); /*parse the return msg*/ if (HA_OK != get_rc_from_ch(ch_cbk)) { lrm_signoff(lrm); client_log(LOG_ERR,-1, "lrm_signon: can not recv result from lrmd"); return HA_FAIL; } /*ok, we sign on sucessfully now*/ is_signed_on = TRUE; client_log(LOG_INFO, -1, "lrm_signon: end."); return HA_OK;}intlrm_signoff (ll_lrm_t* lrm){ int ret = HA_OK; struct ha_msg* msg = NULL; client_log(LOG_INFO,1,"lrm_signoff: start."); /*construct the unreg msg*/ if ( NULL == ch_cmd ) { client_log(LOG_ERR,0,"lrm_signoff: ch_cmd is NULL"); ret = HA_FAIL; } else if ( NULL == (msg = create_lrm_msg(UNREGISTER))) { client_log(LOG_ERR,0,"lrm_signoff: can not create unreg msg"); ret = HA_FAIL; } else /*send the msg*/ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { client_log(LOG_ERR,0,"lrm_signoff: can not send msg to lrmd"); ret = HA_FAIL; } else /*parse the return msg*/ if (HA_OK != get_rc_from_ch(ch_cmd)) { client_log(LOG_ERR,0, "lrm_signoff: can not return failed from lrmd"); ret = HA_FAIL; } if( NULL != msg ) { ha_msg_del(msg); } /*close channels */ if (NULL != ch_cmd) { ch_cmd->ops->destroy(ch_cmd); ch_cmd = NULL; } if (NULL != ch_cbk) { ch_cbk->ops->destroy(ch_cbk); ch_cbk = NULL; } is_signed_on = FALSE; client_log(LOG_INFO, -1, "lrm_signoff: end."); return ret;}intlrm_delete (ll_lrm_t* lrm){ client_log(LOG_INFO,1,"lrm_delete: start."); /*check the parameter*/ if (NULL == lrm) { client_log(LOG_ERR,-1,"lrm_delete: lrm is null."); return HA_FAIL; } g_free(lrm); client_log(LOG_INFO,-1,"lrm_delete: end."); return HA_OK;}intlrm_set_lrm_callback (ll_lrm_t* lrm, lrm_op_done_callback_t op_done_callback_func, lrm_monitor_callback_t monitor_callback_func){ client_log(LOG_INFO, 1, "lrm_set_lrm_callback: start."); op_done_callback = op_done_callback_func; monitor_callback = monitor_callback_func; client_log(LOG_INFO, -1, "lrm_set_lrm_callback: end."); return HA_OK;}GList*lrm_get_ra_supported (ll_lrm_t* lrm){ struct ha_msg* msg; struct ha_msg* ret; GList* type_list = NULL; /*check whether the channel to lrmd is available */ client_log(LOG_INFO, 1, "lrm_get_ra_supported: start."); if (NULL == ch_cmd) { client_log(LOG_ERR, -1, "lrm_get_ra_supported: ch_mod is null."); return NULL; } /*create the get ra type message*/ msg = create_lrm_msg(GETRATYPES); if ( NULL == msg) { client_log(LOG_ERR, -1, "lrm_get_ra_supported: can not create types msg"); return NULL; } /*send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, -1, "lrm_get_ra_supported: can not send msg to lrmd"); return NULL; } ha_msg_del(msg); /*get the return message */ ret = msgfromIPC_noauth(ch_cmd); if (NULL == ret) { client_log(LOG_ERR, -1, "lrm_get_ra_supported: can not recieve ret msg"); return NULL; } /*get the rc of the message */ if (HA_FAIL == get_rc_from_msg(ret)) { ha_msg_del(ret); client_log(LOG_ERR, -1, "lrm_get_ra_supported: rc from msg is fail"); return NULL; } /*get the ra type list from message */ type_list = ha_msg_value_list(ret,F_LRM_RTYPE); ha_msg_del(ret); client_log(LOG_INFO, -1, "lrm_get_ra_supported: end."); return type_list;}GList*lrm_get_all_rscs (ll_lrm_t* lrm){ struct ha_msg* msg; struct ha_msg* ret; GList* rid_str_list = NULL; GList* rid_list = NULL; client_log(LOG_INFO, 1, "lrm_get_all_rscs: start."); /*check whether the channel to lrmd is available */ if (NULL == ch_cmd) { client_log(LOG_ERR, -1, "lrm_get_all_rscs: ch_mod is null."); return NULL; } /*create the msg of get all resource */ msg = create_lrm_msg(GETALLRCSES); if ( NULL == msg) { client_log(LOG_ERR, -1, "lrm_get_all_rscs: can not create types msg"); return NULL; } /*send the msg to lrmd */ if (HA_OK != msg2ipcchan(msg,ch_cmd)) { ha_msg_del(msg); client_log(LOG_ERR, -1, "lrm_get_all_rscs: 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, -1, "lrm_get_all_rscs: can not recieve ret msg"); return NULL; } /*get the rc of msg */ if (HA_FAIL == get_rc_from_msg(ret)) { ha_msg_del(ret); client_log(LOG_ERR, -1, "lrm_get_all_rscs: rc from msg is fail"); return NULL; } /*get the rsc_id(in string)list from msg */ rid_str_list = ha_msg_value_list(ret,F_LRM_RID); /*convert the string id to uuid format */ if (NULL != rid_str_list) { GList* element = g_list_first(rid_str_list); while (NULL != element) { rsc_id_t* rid = g_new(rsc_id_t, 1); uuid_parse(element->data, *rid); g_free(element->data); rid_list = g_list_append(rid_list, *rid); element = g_list_next(element); } } g_list_free(rid_str_list); ha_msg_del(ret); client_log(LOG_INFO, -1, "lrm_get_all_rscs: end."); /*return the uuid list */ return rid_list;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -