diff options
Diffstat (limited to 'src/framework/ipc')
-rw-r--r-- | src/framework/ipc/mgr_com/mgr_com.c | 2220 | ||||
-rw-r--r-- | src/framework/ipc/mgr_com/mgr_com.h | 98 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_fd_timer.c | 374 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_ps_mem_module.c | 1170 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_ps_mem_module.h | 37 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_ps_module.c | 2320 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_ps_module.h | 42 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_recycle_module.c | 613 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_recycle_module.h | 39 | ||||
-rw-r--r-- | src/framework/ipc/ps/nsfw_soft_param.c | 297 |
10 files changed, 3765 insertions, 3445 deletions
diff --git a/src/framework/ipc/mgr_com/mgr_com.c b/src/framework/ipc/mgr_com/mgr_com.c index a2998fa..d09f1d5 100644 --- a/src/framework/ipc/mgr_com/mgr_com.c +++ b/src/framework/ipc/mgr_com/mgr_com.c @@ -20,8 +20,7 @@ #include "types.h" #include "nstack_securec.h" -#include "nsfw_init.h" -#include "common_mem_api.h" +#include "nsfw_init_api.h" #include "nsfw_mgr_com_api.h" #include "mgr_com.h" @@ -32,13 +31,12 @@ #include <stddef.h> #include <sys/epoll.h> #include <fcntl.h> -#include <sys/stat.h> #include "nsfw_maintain_api.h" #include "nsfw_ps_api.h" #include "nsfw_fd_timer_api.h" -#include "common_func.h" +#include "nsfw_maintain_api.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -46,49 +44,50 @@ extern "C"{ /* *INDENT-ON* */ #endif /* __cplusplus */ +nsfw_mgr_msg_fun g_mgr_fun[MGR_MSG_MAX][NSFW_MGRCOM_MAX_PROC_FUN]; /* *INDENT-OFF* */ +/* nstackMaster can't start daemon-stack successfully, + because daemon-stack can't successfully recv MGR_MSG_INIT_NTY_RSP */ +nsfw_mgr_init_cfg g_mgr_com_cfg = +{ + .msg_size = MGR_COM_MSG_COUNT_DEF, + .max_recv_timeout = MGR_COM_RECV_TIMEOUT_DEF, + .max_recv_drop_msg = MGR_COM_MAX_DROP_MSG_DEF, +}; -/* *INDENT-OFF* */ -nsfw_mgr_msg_fun g_mgr_fun[MGR_MSG_MAX][NSFW_MGRCOM_MAX_PROC_FUN]; -nsfw_mgr_init_cfg g_mgr_com_cfg; -nsfw_mgr_sock_map g_mgr_socket_map = {{0}, NULL}; +nsfw_mgr_sock_map g_mgr_sockt_map = {{0}, NULL}; nsfw_mgrcom_stat g_mgr_stat; -nsfw_mgrcom_proc g_ep_proc = { 0 }; -/* *INDENT-ON* */ - +nsfw_mgrcom_proc g_ep_proc = {0}; u32 g_mgr_sockfdmax = NSFW_MGRCOM_MAX_SOCKET; + char g_proc_info[NSFW_PROC_MAX][NSTACK_MAX_PROC_NAME_LEN] = { - "", "nStackMain", "nStackMaster", "nStackLib", "nStackTools", "nStackCtrl", - "", "", "", "", "", "", "", "", "", "" + "" + ,"nStackMain" + ,"nStackMaster" + ,"nStackLib" + ,"nStackCtrl" + ,"nStackTools" + ,"","","","","" + ,"","","","","" }; - /* *INDENT-ON* */ -int g_thread_policy = 0; -int g_thread_pri = 0; +char *g_home_direct = NULL; -void -nsfw_com_attr_set (int policy, int pri) +char *nsfw_get_proc_name(u8 proc_type) { - g_thread_policy = policy; - g_thread_pri = pri; -} - -char * -nsfw_get_proc_name (u8 proc_type) -{ - if (proc_type >= NSFW_PROC_MAX || NSFW_PROC_NULL == proc_type) + if (proc_type >= NSFW_PROC_MAX || NSFW_PROC_NULL == proc_type) { - return NULL; + return NULL; } - return g_proc_info[proc_type]; + return g_proc_info[proc_type]; } /***************************************************************************** * Prototype : nsfw_mgr_reg_msg_fun -* Description : reg the callback function when receive new message +* Description : reg the callback funciton when receive new message * Input : u16 msg_type * nsfw_mgr_msg_fun fun * Output : None @@ -96,29 +95,29 @@ nsfw_get_proc_name (u8 proc_type) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_reg_msg_fun (u16 msg_type, nsfw_mgr_msg_fun fun) +u8 nsfw_mgr_reg_msg_fun(u16 msg_type, nsfw_mgr_msg_fun fun) { - u32 i; - if (MGR_MSG_MAX <= msg_type) + u32 i; + if (MGR_MSG_MAX <= msg_type) { - NSFW_LOGERR ("reg mgr_msg]msg_type=%u,fun=%p", msg_type, fun); - return FALSE; + NSFW_LOGERR("reg mgr_msg]msg_type=%u,fun=%p", msg_type, fun); + return FALSE; } - for (i = 0; i < NSFW_MGRCOM_MAX_PROC_FUN; i++) + for (i = 0; i < NSFW_MGRCOM_MAX_PROC_FUN; i++) { - if (NULL == g_mgr_fun[msg_type][i]) + if (NULL == g_mgr_fun[msg_type][i]) { - g_mgr_fun[msg_type][i] = fun; - NSFW_LOGINF ("reg mgr_msg fun suc]msg_type=%u,fun=%p", msg_type, - fun); - return TRUE; + /*TODO should use cas */ + g_mgr_fun[msg_type][i] = fun; + NSFW_LOGINF("reg mgr_msg fun suc]msg_type=%u,fun=%p", msg_type, + fun); + return TRUE; } } - NSFW_LOGERR ("reg mgr_msg type full]msg_type=%u,fun=%p", msg_type, fun); - return FALSE; + NSFW_LOGERR("reg mgr_msg type full]msg_type=%u,fun=%p", msg_type, fun); + return FALSE; } /***************************************************************************** @@ -130,10 +129,9 @@ nsfw_mgr_reg_msg_fun (u16 msg_type, nsfw_mgr_msg_fun fun) * Calls : * Called By : *****************************************************************************/ -nsfw_mgr_msg * -nsfw_mgr_null_rspmsg_alloc () +nsfw_mgr_msg *nsfw_mgr_null_rspmsg_alloc() { - return nsfw_mgr_msg_alloc (MGR_MSG_NULL, NSFW_PROC_NULL); + return nsfw_mgr_msg_alloc(MGR_MSG_NULL, NSFW_PROC_NULL); } /***************************************************************************** @@ -146,86 +144,75 @@ nsfw_mgr_null_rspmsg_alloc () * Calls : * Called By : *****************************************************************************/ -nsfw_mgr_msg * -nsfw_mgr_msg_alloc (u16 msg_type, u8 dst_proc_type) +nsfw_mgr_msg *nsfw_mgr_msg_alloc(u16 msg_type, u8 dst_proc_type) { - nsfw_mgr_msg *p_msg = NULL; - u8 from_mem_flag = FALSE; - u32 alloc_len = sizeof (nsfw_mgr_msg); + nsfw_mgr_msg *p_msg = NULL; + u8 from_mem_flag = FALSE; + u32 alloc_len = sizeof(nsfw_mgr_msg); - if (MGR_MSG_LAG_QRY_RSP_BEGIN <= msg_type) + if (MGR_MSG_LAG_QRY_RSP_BEGIN <= msg_type + || MGR_MSG_SPLNET_REQ == msg_type) { - from_mem_flag = TRUE; - alloc_len = NSFW_MGR_LARGE_MSG_LEN; + from_mem_flag = TRUE; + alloc_len = NSFW_MGR_LARGE_MSG_LEN; } - if ((NULL == g_mgr_com_cfg.msg_pool) - && (MGR_MSG_INIT_NTY_REQ == msg_type - || MGR_MSG_INIT_NTY_RSP == msg_type)) + if ((NULL == g_mgr_com_cfg.msg_pool) + && (MGR_MSG_INIT_NTY_REQ == msg_type + || MGR_MSG_INIT_NTY_RSP == msg_type)) { - from_mem_flag = TRUE; + from_mem_flag = TRUE; } - if (FALSE == from_mem_flag) + if (FALSE == from_mem_flag) { - if (0 == - nsfw_mem_ring_dequeue (g_mgr_com_cfg.msg_pool, (void *) &p_msg)) + if (0 == + nsfw_mem_ring_dequeue(g_mgr_com_cfg.msg_pool, (void *) &p_msg)) { - NSFW_LOGERR ("alloc msg full]type=%u,dst=%u", msg_type, - dst_proc_type); - return NULL; + NSFW_LOGERR("alloc msg full]type=%u,dst=%u", msg_type, + dst_proc_type); + return NULL; } - alloc_len = sizeof (nsfw_mgr_msg); + alloc_len = sizeof(nsfw_mgr_msg); } - else + else { - p_msg = (nsfw_mgr_msg *) malloc (alloc_len); + p_msg = (nsfw_mgr_msg *) malloc(alloc_len); /*malloc() can be used */ } - if (NULL == p_msg) + if (NULL == p_msg) { - NSFW_LOGERR ("alloc msg nul]type=%u,dst=%u", msg_type, dst_proc_type); - return NULL; + NSFW_LOGERR("alloc msg nul]type=%u,dst=%u", msg_type, dst_proc_type); + return NULL; } - if (EOK != MEMSET_S (p_msg, alloc_len, 0, alloc_len)) + if (EOK != memset_s(p_msg, alloc_len, 0, alloc_len)) { - p_msg->from_mem = from_mem_flag; - nsfw_mgr_msg_free (p_msg); - NSFW_LOGERR ("alloc msg MEMSET_S failed]type=%u,dst=%u", msg_type, - dst_proc_type); - return NULL; + p_msg->from_mem = from_mem_flag; + + nsfw_mgr_msg_free(p_msg); + NSFW_LOGERR("alloc msg memset_s failed]type=%u,dst=%u", msg_type, + dst_proc_type); + return NULL; } - p_msg->from_mem = from_mem_flag; + p_msg->from_mem = from_mem_flag; - if (msg_type < MGR_MSG_RSP_BASE && msg_type > 0) + if (msg_type < MGR_MSG_RSP_BASE && msg_type > 0) { - p_msg->seq = common_mem_atomic32_add_return (&g_mgr_com_cfg.cur_idx, 1); + p_msg->seq = dmm_atomic_add_return(&g_mgr_com_cfg.cur_idx, 1); } - p_msg->from_mem = from_mem_flag; - p_msg->msg_type = msg_type; - p_msg->src_pid = get_sys_pid (); - p_msg->src_proc_type = g_mgr_com_cfg.proc_type; - p_msg->msg_len = alloc_len; - p_msg->dst_proc_type = dst_proc_type; - p_msg->alloc_flag = TRUE; - p_msg->more_msg_flag = 0; + p_msg->from_mem = from_mem_flag; + p_msg->msg_type = msg_type; + p_msg->src_pid = get_sys_pid(); + p_msg->src_proc_type = g_mgr_com_cfg.proc_type; + p_msg->msg_len = alloc_len; + p_msg->dst_proc_type = dst_proc_type; + p_msg->alloc_flag = TRUE; + p_msg->more_msg_flag = 0; - g_mgr_stat.msg_alloc++; - return p_msg; -} - -static inline void -lint_lock_1 () -{ - return; -} - -static inline void -lint_unlock_1 () -{ - return; + g_mgr_stat.msg_alloc++; + return p_msg; } /***************************************************************************** @@ -237,27 +224,26 @@ lint_unlock_1 () * Calls : * Called By : *****************************************************************************/ -nsfw_mgr_msg * -nsfw_mgr_rsp_msg_alloc (nsfw_mgr_msg * req_msg) +nsfw_mgr_msg *nsfw_mgr_rsp_msg_alloc(nsfw_mgr_msg * req_msg) { - nsfw_mgr_msg *p_msg = NULL; - if (NULL == req_msg) + nsfw_mgr_msg *p_msg = NULL; + if (NULL == req_msg) { - NSFW_LOGERR ("req msg nul!"); - return NULL; + NSFW_LOGERR("req msg nul!"); + return NULL; } - p_msg = - nsfw_mgr_msg_alloc (req_msg->msg_type + MGR_MSG_RSP_BASE, - req_msg->src_proc_type); - if (NULL == p_msg) + p_msg = + nsfw_mgr_msg_alloc(req_msg->msg_type + MGR_MSG_RSP_BASE, + req_msg->src_proc_type); + if (NULL == p_msg) { - return NULL; + return NULL; } - p_msg->dst_pid = req_msg->src_pid; - p_msg->seq = req_msg->seq; - return p_msg; + p_msg->dst_pid = req_msg->src_pid; + p_msg->seq = req_msg->seq; + return p_msg; } /***************************************************************************** @@ -269,44 +255,87 @@ nsfw_mgr_rsp_msg_alloc (nsfw_mgr_msg * req_msg) * Calls : * Called By : *****************************************************************************/ -void -nsfw_mgr_msg_free (nsfw_mgr_msg * msg) +void nsfw_mgr_msg_free(nsfw_mgr_msg * msg) { - if (NULL == msg) + + if (NULL == msg) { - return; + return; } - if (FALSE == msg->alloc_flag) + if (FALSE == msg->alloc_flag) + { + NSFW_LOGERR("msg refree]msg=%p, type=%u", msg, msg->msg_type); + return; + } + + msg->alloc_flag = FALSE; + + if (TRUE == msg->from_mem) + { + /*remove msg->msg_type judgement */ + free(msg); /*free() can be used */ + g_mgr_stat.msg_free++; + return; + } + else { - NSFW_LOGERR ("msg refree]msg=%p, type=%u", msg, msg->msg_type); - return; + if (0 == nsfw_mem_ring_enqueue(g_mgr_com_cfg.msg_pool, msg)) + { + NSFW_LOGERR("msg free failed pool full]msg=%p, type=%u", msg, + msg->msg_type); + return; + } + g_mgr_stat.msg_free++; } - msg->alloc_flag = FALSE; + return; +} - if (TRUE == msg->from_mem) +/***************************************************************************** +* Prototype : get_home_path +* Description : get the env "HOME" path info +* Input : +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +const char *get_home_path() +{ + //a extern global var, if not null ,just return it. + if (g_home_direct) { - if ((MGR_MSG_INIT_NTY_REQ == msg->msg_type - || MGR_MSG_INIT_NTY_RSP == msg->msg_type) - || (MGR_MSG_LAG_QRY_RSP_BEGIN <= msg->msg_type)) + return g_home_direct; + } + + //just need do one time. + const char *home_dir = getenv("HOME"); /*getenv() can be used */ + if (getuid() != 0 && home_dir != NULL) + { + g_home_direct = realpath(home_dir, NULL); + if (!g_home_direct) { - free (msg); - g_mgr_stat.msg_free++; - return; + NSFW_LOGWAR("realpath fail]home_dir=%s,errno=%d", home_dir, + errno); + return NULL; } - NSFW_LOGERR ("msg err free]type=%u", msg->msg_type); } - if (0 == nsfw_mem_ring_enqueue (g_mgr_com_cfg.msg_pool, msg)) + //check if the path is valid + if (check_log_dir_valid(g_home_direct) < 0) { - NSFW_LOGERR ("msg free failed pool full]msg=%p, type=%u", msg, - msg->msg_type); - return; + NSFW_LOGWAR("path check valid fail, free it]home_direct=%s", + g_home_direct); + if (g_home_direct) + { + free(g_home_direct); + g_home_direct = NULL; + } + return NULL; } - g_mgr_stat.msg_free++; - return; + return g_home_direct; } /***************************************************************************** @@ -318,82 +347,99 @@ nsfw_mgr_msg_free (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_mgr_get_listen_socket () +i32 nsfw_mgr_get_listen_socket() { - i32 fd, len, retVal; - struct sockaddr_un un; - char name[NSFW_MGRCOM_PATH_LEN] = { 0 }; + i32 fd, len; + struct sockaddr_un un; - if ((fd = nsfw_base_socket (AF_UNIX, SOCK_STREAM, 0)) < 0) + if ((fd = nsfw_base_socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - NSFW_LOGERR ("create sock failed!"); - return -1; + NSFW_LOGERR("create sock failed!"); + return -1; } - retVal = STRCPY_S ((char *) name, sizeof (name), - (char *) g_mgr_com_cfg.domain_path); - if (EOK != retVal) + if (-1 == unlink((char *) g_mgr_com_cfg.domain_path)) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("module mgr get listen STRCPY_S failed! ret=%d", retVal); - return -1; + NSFW_LOGWAR("unlink failed]error=%d", errno); } - if (EOK != STRCAT_S (name, NSFW_MGRCOM_PATH_LEN, NSFW_MAIN_FILE)) + if (EOK != memset_s(&un, sizeof(un), 0, sizeof(un))) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("module mgr get listen STRCAT_S failed!"); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("create sock memset_s failed!] error=%d", errno); + return -1; } - if (-1 == unlink ((char *) name)) + un.sun_family = AF_UNIX; + int retVal = strcpy_s((char *) un.sun_path, sizeof(un.sun_path), + (char *) g_mgr_com_cfg.domain_path); + if (EOK != retVal) { - NSFW_LOGWAR ("unlink failed]error=%d", errno); + (void) nsfw_base_close(fd); + NSFW_LOGERR("create sock strcpy_s failed!] error=%d", errno); + return -1; } - if (EOK != MEMSET_S (&un, sizeof (un), 0, sizeof (un))) + + /* close on exec */ + int rc = nsfw_set_close_on_exec(fd); + if (rc == -1) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("create sock MEMSET_S failed!] error=%d", errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("set exec err]fd=%d, errno=%d", fd, errno); + return -1; } - un.sun_family = AF_UNIX; - retVal = STRCPY_S ((char *) un.sun_path, sizeof (un.sun_path), - (char *) name); - if (EOK != retVal) + len = offsetof(struct sockaddr_un, sun_path) +strlen((char *) g_mgr_com_cfg.domain_path); /*strlen() can be used */ + + if (nsfw_base_bind(fd, (struct sockaddr *) &un, len) < 0) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("create sock STRCPY_S failed!] error=%d", errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("bind failed!]mgr_fd=%d,error=%d", fd, errno); + return -1; } - int rc = nsfw_set_close_on_exec (fd); - if (rc == -1) + if (nsfw_base_listen(fd, 10) < 0) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("set exec err]fd=%d, errno=%d", fd, errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("listen failed!]mgr_fd=%d,error=%d", fd, errno); + return -1; } - len = offsetof (struct sockaddr_un, sun_path) +strlen ((char *) name); + NSFW_LOGINF("mgr com start with]mgr_fd=%d", fd); + return fd; +} - if (nsfw_base_bind (fd, (struct sockaddr *) &un, len) < 0) +/***************************************************************************** +* Prototype : nsfw_mgr_connect_with_retry +* Description : connect dst_socket, retry some times every 100ms +* Input : nsfw_mgr_msg* req_msg +* nsfw_mgr_msg* rsp_msg +* Output : None +* Return Value : u8 +* Calls : +* Called By : +*****************************************************************************/ +i32 nsfw_mgr_connect_with_retry(i32 fd, struct sockaddr * un, i32 len, + i32 interval_ms, i32 retry_times) +{ + if (nsfw_base_connect(fd, un, len) == 0) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("bind failed!]mgr_fd=%d,error=%d", fd, errno); - return -1; + return 0; } - - if (nsfw_base_listen (fd, 10) < 0) + while (retry_times > 0) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("listen failed!]mgr_fd=%d,error=%d", fd, errno); - return -1; + sys_sleep_ns(0, interval_ms * 1000 * 1000); + --retry_times; + if (nsfw_base_connect(fd, un, len) == 0) + { + return 0; + } + else if (errno != ECONNRESET && errno != ECONNREFUSED) /* Retry is for Master upgrade only */ + { + return -1; + } } - - NSFW_LOGINF ("mgr com start with]mgr_fd=%d", fd); - return fd; + return -1; } /***************************************************************************** @@ -406,115 +452,138 @@ nsfw_mgr_get_listen_socket () * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_mgr_get_connect_socket (u8 proc_type, u32 host_pid) +i32 nsfw_mgr_get_connect_socket(u8 proc_type, u32 host_pid) { - i32 fd, len; - char *name; - struct sockaddr_un un; - const char *directory = NSFW_DOMAIN_DIR; - const char *home_dir = getenv ("HOME"); + i32 fd = -1; + i32 len = 0; + char *name; + struct sockaddr_un un; + i32 retry_times = 0; + i32 interval_ms = 50; + const char *directory = NSFW_DOMAIN_DIR; + const char *home_dir = get_home_path(); + if (home_dir) + { + directory = home_dir; + } - if (getuid () != 0 && home_dir != NULL) - directory = home_dir; + switch (proc_type) + { + case NSFW_PROC_MAIN: + name = NSFW_MAIN_FILE; + break; + case NSFW_PROC_MASTER: + name = NSFW_MASTER_FILE; + retry_times = 10; + break; + case NSFW_PROC_ALARM: /* alarm */ + directory = "/tmp"; + name = NSFW_ALARM_FILE; + break; + default: + NSFW_LOGERR("get dst socket err]type=%u,pid=%u", proc_type, + host_pid); + return -1; + } - switch (proc_type) + if ((fd = nsfw_base_socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - case NSFW_PROC_MAIN: - name = NSFW_MAIN_FILE; - break; - case NSFW_PROC_ALARM: - directory = "/tmp"; - name = NSFW_ALARM_FILE; - break; - default: - NSFW_LOGERR ("get dst socket err]type=%u,pid=%u", proc_type, host_pid); - return -1; + NSFW_LOGERR("create socket err]type=%u,pid=%u,errno=%d", proc_type, + host_pid, errno); + return -1; } - if ((fd = nsfw_base_socket (AF_UNIX, SOCK_STREAM, 0)) < 0) + if (EOK != memset_s(&un, sizeof(un), 0, sizeof(un))) { - NSFW_LOGERR ("create socket err]type=%u,pid=%u,errno=%d", proc_type, - host_pid, errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("get dst socket err]mgr_fd=%d,type=%u,pid=%u", fd, + proc_type, host_pid); + return -1; } - if (EOK != MEMSET_S (&un, sizeof (un), 0, sizeof (un))) + struct timeval tv; + tv.tv_sec = MGR_COM_RECV_TIMEOUT; + tv.tv_usec = 0; + if (nsfw_base_setsockopt + (fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv)) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("get dst socket err]mgr_fd=%d,type=%u,pid=%u", fd, - proc_type, host_pid); - return -1; + NSFW_LOGERR + ("setsockopt socket err]mgr_fd=%d,type=%u,pid=%u,errno=%d", fd, + proc_type, host_pid, errno); + (void) nsfw_base_close(fd); + return -1; } - struct timeval tv; - tv.tv_sec = MGR_COM_RECV_TIMEOUT; - tv.tv_usec = 0; - if (nsfw_base_setsockopt - (fd, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv)) + tv.tv_sec = MGR_COM_SEND_TIMEOUT_DEF; + tv.tv_usec = 0; + if (nsfw_base_setsockopt + (fd, SOL_SOCKET, SO_SNDTIMEO, (char *) &tv, sizeof tv)) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("setsockopt socket err]mgr_fd=%d,type=%u,pid=%u", fd, - proc_type, host_pid); - return -1; + NSFW_LOGERR + ("setsockopt socket err]mgr_fd=%d,type=%u,pid=%u,errno=%d", fd, + proc_type, host_pid, errno); + (void) nsfw_base_close(fd); + return -1; } - int rc = nsfw_set_close_on_exec (fd); - if (rc == -1) + /* close on exec Add Begin */ + int rc = nsfw_set_close_on_exec(fd); + if (rc == -1) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("set exec err]fd=%d, errno=%d", fd, errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("set exec err]fd=%d, errno=%d", fd, errno); + return -1; } - int size, size_len; - size = MAX_RECV_BUF_DEF; - size_len = sizeof (size); - if (0 > - nsfw_base_setsockopt (fd, SOL_SOCKET, SO_RCVBUF, (void *) &size, - (socklen_t) size_len)) + int size, size_len; + size = MAX_RECV_BUF_DEF; + size_len = sizeof(size); + if (0 > + nsfw_base_setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *) &size, + (socklen_t) size_len)) { - NSFW_LOGERR ("set socket opt err!]error=%d", errno); + NSFW_LOGERR("set socket opt err]error=%d", errno); } - if (0 > - nsfw_base_setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (void *) &size, - (socklen_t) size_len)) + if (0 > + nsfw_base_setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *) &size, + (socklen_t) size_len)) { - NSFW_LOGERR ("set socket opt err!]error=%d", errno); + NSFW_LOGERR("set socket opt err]error=%d", errno); } - un.sun_family = AF_UNIX;; - int retVal = STRCPY_S ((char *) un.sun_path, sizeof (un.sun_path), - (char *) directory); - if (EOK != retVal) + un.sun_family = AF_UNIX;; + int retVal = strcpy_s((char *) un.sun_path, sizeof(un.sun_path), + (char *) directory); + if (EOK != retVal) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("create sock STRCPY_S failed!] error=%d", errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("create sock strcpy_s fail]error=%d", errno); + return -1; } - retVal = STRCAT_S (un.sun_path, sizeof (un.sun_path), name); - if (EOK != retVal) + retVal = strcat_s(un.sun_path, sizeof(un.sun_path), name); + if (EOK != retVal) { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("create sock STRCAT_S failed!] error=%d", errno); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR("create sock strcat_s fail]error=%d", errno); + return -1; } - len = offsetof (struct sockaddr_un, sun_path) +strlen (un.sun_path); - if (nsfw_base_connect (fd, (struct sockaddr *) &un, len) < 0) + len = offsetof(struct sockaddr_un, sun_path) +strlen(un.sun_path); /*strlen() can be used */ + if (nsfw_mgr_connect_with_retry + (fd, (struct sockaddr *) &un, len, interval_ms, retry_times) < 0) { - (void) nsfw_base_close (fd); - NSFW_LOGERR - ("create socket err]mgr_fd=%d,type=%u,pid=%u,errno=%d,path=%s", fd, - proc_type, host_pid, errno, un.sun_path); - return -1; + (void) nsfw_base_close(fd); + NSFW_LOGERR + ("connect socket failed]mgr_fd=%d,type=%u,pid=%u,errno=%d,path=%s", + fd, proc_type, host_pid, errno, un.sun_path); + return -1; } - NSFW_LOGINF ("get dst socket]mgr_fd=%d,type=%u,pid=%u", fd, proc_type, - host_pid); - return (fd); + NSFW_LOGINF("get dst socket]mgr_fd=%d,type=%u,pid=%u", fd, proc_type, + host_pid); + return fd; } /***************************************************************************** @@ -528,33 +597,42 @@ nsfw_mgr_get_connect_socket (u8 proc_type, u32 host_pid) * Calls : * Called By : *****************************************************************************/ -NSTACK_STATIC inline u8 -nsfw_mgr_new_socket (i32 fd, u8 proc_type, u32 host_pid) +NSTACK_STATIC inline u8 nsfw_mgr_new_socket(i32 fd, u8 proc_type, + u32 host_pid) { - nsfw_mgr_sock_info *sock_info = NULL; - if (((i32) NSFW_MGR_FD_MAX <= fd) || (fd < 0) || (!g_mgr_socket_map.sock)) + nsfw_mgr_sock_info *sock_info = NULL; + if (((i32) NSFW_MGR_FD_MAX <= fd) || (fd < 0) || (!g_mgr_sockt_map.sock)) { - NSFW_LOGERR ("fd err]mgr_fd=%d, sock=%p", fd, g_mgr_socket_map.sock); - return FALSE; + NSFW_LOGERR("fd err]mgr_fd=%d, sock=%p", fd, g_mgr_sockt_map.sock); + return FALSE; } - sock_info = &g_mgr_socket_map.sock[fd]; - if (host_pid != sock_info->host_pid) + sock_info = &g_mgr_sockt_map.sock[fd]; + if (host_pid != sock_info->host_pid) { - NSFW_LOGDBG - ("update sock info]mgr_fd=%d,old_pid=%u,new_pid=%u,type=%u", fd, - sock_info->host_pid, host_pid, proc_type); + /* Fix mgr fd leak */ + if (g_mgr_com_cfg.proc_type == NSFW_PROC_MAIN + && (proc_type == NSFW_PROC_MAIN || proc_type == NSFW_PROC_MASTER)) + { + NSFW_LOGINF("update sock]fd=%d,opid=%u,npid=%u,type=%u", fd, + sock_info->host_pid, host_pid, proc_type); + } + else + { + NSFW_LOGDBG("update sock]fd=%d,opid=%u,npid=%u,type=%u", fd, + sock_info->host_pid, host_pid, proc_type); + } } - sock_info->host_pid = host_pid; - sock_info->proc_type = proc_type; + sock_info->host_pid = host_pid; + sock_info->proc_type = proc_type; - if (proc_type < NSFW_PROC_MAX) + if (proc_type < NSFW_PROC_MAX) { - g_mgr_socket_map.proc_cache[proc_type] = fd; + g_mgr_sockt_map.proc_cache[proc_type] = fd; } - return TRUE; + return TRUE; } /***************************************************************************** @@ -566,29 +644,28 @@ nsfw_mgr_new_socket (i32 fd, u8 proc_type, u32 host_pid) * Calls : * Called By : *****************************************************************************/ -NSTACK_STATIC inline u8 -nsfw_mgr_del_socket (u32 fd) +NSTACK_STATIC inline u8 nsfw_mgr_del_socket(u32 fd) { - nsfw_mgr_sock_info *sock_info = NULL; - if ((NSFW_MGR_FD_MAX <= fd) || (!g_mgr_socket_map.sock)) + nsfw_mgr_sock_info *sock_info = NULL; + if ((NSFW_MGR_FD_MAX <= fd) || (!g_mgr_sockt_map.sock)) { - NSFW_LOGERR ("fd err]mgr_fd=%u, sock=%p", fd, g_mgr_socket_map.sock); - return FALSE; + NSFW_LOGERR("fd err]mgr_fd=%u,sock=%p", fd, g_mgr_sockt_map.sock); + return FALSE; } - sock_info = &g_mgr_socket_map.sock[fd]; + sock_info = &g_mgr_sockt_map.sock[fd]; - if (sock_info->proc_type < NSFW_PROC_MAX - && fd == g_mgr_socket_map.proc_cache[sock_info->proc_type]) + if (sock_info->proc_type < NSFW_PROC_MAX + && fd == g_mgr_sockt_map.proc_cache[sock_info->proc_type]) { - g_mgr_socket_map.proc_cache[sock_info->proc_type] = 0; + g_mgr_sockt_map.proc_cache[sock_info->proc_type] = 0; } - NSFW_LOGDBG ("del sock]mgr_fd=%u,type=%u,pid=%u", fd, - sock_info->proc_type, sock_info->host_pid); - sock_info->host_pid = 0; - sock_info->proc_type = 0; - return TRUE; + NSFW_LOGDBG("del sock]mgr_fd=%u,type=%u,pid=%u", fd, + sock_info->proc_type, sock_info->host_pid); + sock_info->host_pid = 0; + sock_info->proc_type = 0; + return TRUE; } /***************************************************************************** @@ -601,53 +678,52 @@ nsfw_mgr_del_socket (u32 fd) * Calls : * Called By : *****************************************************************************/ -NSTACK_STATIC inline i32 -nsfw_mgr_get_dst_socket (u8 proc_type, u32 dst_pid) +NSTACK_STATIC inline i32 nsfw_mgr_get_dst_socket(u8 proc_type, u32 dst_pid) { - i32 fd = -1; + i32 fd = -1; - nsfw_mgr_sock_info *sock_info = NULL; + nsfw_mgr_sock_info *sock_info = NULL; - if (proc_type < NSFW_PROC_MAX) + if (proc_type < NSFW_PROC_MAX) { - fd = g_mgr_socket_map.proc_cache[proc_type]; + fd = g_mgr_sockt_map.proc_cache[proc_type]; } - if (!g_mgr_socket_map.sock) + if (!g_mgr_sockt_map.sock) { - return -1; + return -1; } - if (fd > 0 && fd < (i32) NSFW_MGR_FD_MAX) + if (fd > 0 && fd < (i32) NSFW_MGR_FD_MAX) { - sock_info = &g_mgr_socket_map.sock[fd]; - if (sock_info->host_pid != 0) + sock_info = &g_mgr_sockt_map.sock[fd]; + if (sock_info->host_pid != 0) { - if (0 == dst_pid || dst_pid == sock_info->host_pid) + if (0 == dst_pid || dst_pid == sock_info->host_pid) { - return fd; + return fd; } } - else if (proc_type == sock_info->proc_type) + else if (proc_type == sock_info->proc_type) { - return fd; + return fd; } } - i32 i; - for (i = 0; i < (i32) NSFW_MGR_FD_MAX; i++) + i32 i; + for (i = 0; i < (i32) NSFW_MGR_FD_MAX; i++) { - sock_info = &g_mgr_socket_map.sock[i]; - if (sock_info->host_pid != 0 && proc_type == sock_info->proc_type) + sock_info = &g_mgr_sockt_map.sock[i]; + if (sock_info->host_pid != 0 && proc_type == sock_info->proc_type) { - if (0 == dst_pid || dst_pid == sock_info->host_pid) + if (0 == dst_pid || dst_pid == sock_info->host_pid) { - return i; + return i; } } } - return -1; + return -1; } /***************************************************************************** @@ -660,17 +736,16 @@ nsfw_mgr_get_dst_socket (u8 proc_type, u32 dst_pid) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_mgr_get_new_socket (u8 proc_type, u32 dst_pid) +i32 nsfw_mgr_get_new_socket(u8 proc_type, u32 dst_pid) { - i32 fd = 0; - fd = nsfw_mgr_get_connect_socket (proc_type, dst_pid); - if (fd > 0) + i32 fd = 0; + fd = nsfw_mgr_get_connect_socket(proc_type, dst_pid); + if (fd > 0) { - (void) nsfw_mgr_new_socket (fd, proc_type, dst_pid); - (void) nsfw_mgr_reg_sock_fun (fd, nsfw_mgr_new_msg); + (void) nsfw_mgr_new_socket(fd, proc_type, dst_pid); + (void) nsfw_mgr_reg_sock_fun(fd, nsfw_mgr_new_msg); } - return fd; + return fd; } /***************************************************************************** @@ -682,20 +757,19 @@ nsfw_mgr_get_new_socket (u8 proc_type, u32 dst_pid) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_clr_fd_lock () +u8 nsfw_mgr_clr_fd_lock() { - i32 i; - if (!g_mgr_socket_map.sock) + i32 i; + if (!g_mgr_sockt_map.sock) { - NSFW_LOGERR ("clr fd lock fail, sock is null"); - return FALSE; + NSFW_LOGERR("clr fd lock fail, sock is null"); + return FALSE; } - for (i = 0; i < (i32) NSFW_MGR_FD_MAX; i++) + for (i = 0; i < (i32) NSFW_MGR_FD_MAX; i++) { - common_mem_spinlock_init (&(g_mgr_socket_map.sock[i].opr_lock)); + dmm_spin_init(&(g_mgr_sockt_map.sock[i].opr_lock)); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -708,15 +782,14 @@ nsfw_mgr_clr_fd_lock () * Calls : * Called By : *****************************************************************************/ -void -nsfw_mgr_close_dst_proc (u8 proc_type, u32 dst_pid) +void nsfw_mgr_close_dst_proc(u8 proc_type, u32 dst_pid) { - i32 fd = nsfw_mgr_get_dst_socket (proc_type, dst_pid); - if (fd > 0) + i32 fd = nsfw_mgr_get_dst_socket(proc_type, dst_pid); + if (fd > 0) { - (void) nsfw_mgr_del_socket (fd); - (void) nsfw_mgr_unreg_sock_fun (fd); - (void) nsfw_base_close (fd); + (void) nsfw_mgr_del_socket(fd); + (void) nsfw_mgr_unreg_sock_fun(fd); + (void) nsfw_base_close(fd); } } @@ -730,49 +803,49 @@ nsfw_mgr_close_dst_proc (u8 proc_type, u32 dst_pid) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_send_msg_socket (u32 fd, nsfw_mgr_msg * msg) +u8 nsfw_mgr_send_msg_socket(u32 fd, nsfw_mgr_msg * msg) { - i32 send_len = 0; - i32 off_set = 0; + i32 send_len = 0; + i32 off_set = 0; - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("msg nul]mgr_fd=%u", fd); - return FALSE; + NSFW_LOGERR("msg nul]mgr_fd=%u", fd); + return FALSE; } - if (msg->msg_len < sizeof (nsfw_mgr_msg)) + if (msg->msg_len < sizeof(nsfw_mgr_msg)) { - msg->msg_len = sizeof (nsfw_mgr_msg); + msg->msg_len = sizeof(nsfw_mgr_msg); } - if (msg->msg_type == MGR_MSG_LARGE_ALARM_RSP) + /* only need send msg->msg_body */ + if (msg->msg_type == MGR_MSG_LARGE_ALARM_RSP) { - off_set = NSFW_MGR_MSG_HDR_LEN; + off_set = NSFW_MGR_MSG_HDR_LEN; } - do + /*TODO if closed by peer, may send failed, should close this fd */ + do { - off_set += send_len; - send_len = - nsfw_base_send (fd, (char *) msg + off_set, msg->msg_len - off_set, - MSG_NOSIGNAL); - if (send_len <= 0) + off_set += send_len; + send_len = + nsfw_base_send(fd, (char *) msg + off_set, + msg->msg_len - off_set, MSG_NOSIGNAL); + if (send_len <= 0) { - NSFW_LOGERR - ("send error]mgr_fd=%u,send_len=%d,off_set=%d,errno=%d" MSGINFO, - fd, send_len, off_set, errno, PRTMSG (msg)); - return FALSE; + NSFW_LOGERR("send err]mgr_fd=%u,send_len=%d,off_set=%d,errno=%d" + MSGINFO, fd, send_len, off_set, errno, PRTMSG(msg)); + return FALSE; } } - while ((send_len + off_set) < (i32) msg->msg_len); - NSFW_LOGDBG ("send mgr_msg suc]mgr_fd=%u," MSGINFO, fd, PRTMSG (msg)); - g_mgr_stat.msg_send[msg->msg_type]++; - return TRUE; + while ((send_len + off_set) < (i32) msg->msg_len); + NSFW_LOGDBG("send mgr_msg suc]mgr_fd=%u," MSGINFO, fd, PRTMSG(msg)); + g_mgr_stat.msg_send[msg->msg_type]++; + return TRUE; } -#define MAX_RECV_COUNT 100 +#define MAX_RECV_COUNT 2 /***************************************************************************** * Prototype : nsfw_mgr_recv_msg_socket @@ -784,212 +857,289 @@ nsfw_mgr_send_msg_socket (u32 fd, nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_recv_msg_socket (u32 fd, nsfw_mgr_msg * msg, - nsfw_mgr_msg ** large_msg) +u8 nsfw_mgr_recv_msg_socket(u32 fd, nsfw_mgr_msg * msg, + nsfw_mgr_msg ** large_msg) { - i32 recv_len = 0; - i32 off_set = 0; - u32 msg_len = 0; - i32 max_count = 0; - if (NULL == msg) + i32 recv_len = 0; + i32 off_set = 0; + u32 msg_len = 0; + i32 max_count = 0; + if (NULL == msg) { - return FALSE; + return FALSE; } - u8 from_flag = msg->from_mem; - msg_len = msg->msg_len; - do + u8 from_flag = msg->from_mem; + msg_len = msg->msg_len; + do { - off_set += recv_len; - recv_len = - nsfw_base_recv (fd, (char *) msg + off_set, msg_len - off_set, 0); - if (recv_len <= 0) + off_set += recv_len; + recv_len = + nsfw_base_recv(fd, (char *) msg + off_set, msg_len - off_set, 0); + if (recv_len <= 0) { - if ((EINTR == errno || EAGAIN == errno) - && (max_count < MAX_RECV_COUNT)) + if ((EINTR == errno || EAGAIN == errno) + && (max_count < MAX_RECV_COUNT)) { - recv_len = 0; - max_count++; - continue; + recv_len = 0; + max_count++; + continue; } - NSFW_LOGERR - ("recv error]mgr_fd=%u,recv_len=%d,off_set=%d,errno=%d," - MSGINFO, fd, recv_len, off_set, errno, PRTMSG (msg)); - msg->from_mem = from_flag; - return FALSE; + NSFW_LOGERR("recv err]mgr_fd=%u,recv_len=%d,off_set=%d,errno=%d," + MSGINFO, fd, recv_len, off_set, errno, PRTMSG(msg)); + msg->from_mem = from_flag; + return FALSE; } } - while (recv_len + off_set < (i32) msg_len); + while (recv_len + off_set < (i32) msg_len); - msg->from_mem = from_flag; + msg->from_mem = from_flag; - g_mgr_stat.msg_recv[msg->msg_type]++; + g_mgr_stat.msg_recv[msg->msg_type]++; - if (msg->msg_len <= msg_len) + if (msg->msg_len <= msg_len) { - NSFW_LOGDBG ("recv mgr_msg suc]mgr_fd=%u," MSGINFO, fd, PRTMSG (msg)); - return TRUE; + NSFW_LOGDBG("recv mgr_msg suc]mgr_fd=%u," MSGINFO, fd, PRTMSG(msg)); + return TRUE; } - if (large_msg == NULL) + if (large_msg == NULL) { - return TRUE; + return TRUE; } - nsfw_mgr_msg *l_msg = - nsfw_mgr_msg_alloc (msg->msg_type, msg->dst_proc_type); - if (NULL == l_msg) + nsfw_mgr_msg *l_msg = + nsfw_mgr_msg_alloc(msg->msg_type, msg->dst_proc_type); + if (NULL == l_msg) { - return TRUE; + return TRUE; } - if (l_msg->msg_len <= msg_len) + if (l_msg->msg_len <= msg_len) { - NSFW_LOGWAR ("alloc new msg error!]len=%u,org_len=%u,type=%u", - l_msg->msg_len, msg->msg_len, msg->msg_type); - nsfw_mgr_msg_free (l_msg); - return TRUE; + NSFW_LOGWAR("alloc new msg err]len=%u,org_len=%u,type=%u", + l_msg->msg_len, msg->msg_len, msg->msg_type); + nsfw_mgr_msg_free(l_msg); + return TRUE; } - max_count = 0; - (void) nsfw_set_sock_block (fd, FALSE); - from_flag = l_msg->from_mem; - u32 l_msg_len = l_msg->msg_len; - do + max_count = 0; + (void) nsfw_set_sock_block(fd, FALSE); + from_flag = l_msg->from_mem; + u32 l_msg_len = l_msg->msg_len; + do { - off_set += recv_len; - recv_len = - nsfw_base_recv (fd, (char *) l_msg + off_set, l_msg_len - off_set, 0); - if (recv_len <= 0) + off_set += recv_len; + recv_len = + nsfw_base_recv(fd, (char *) l_msg + off_set, l_msg_len - off_set, + 0); + if (recv_len <= 0) { - if ((EINTR == errno || EAGAIN == errno) - && (max_count < MAX_RECV_COUNT)) + if ((EINTR == errno || EAGAIN == errno) + && (max_count < MAX_RECV_COUNT)) { - recv_len = 0; - max_count++; - continue; + recv_len = 0; + max_count++; + continue; } - NSFW_LOGERR - ("recv error]mgr_fd=%u,recv_len=%d,off_set=%d,errno=%d," - MSGINFO, fd, recv_len, off_set, errno, PRTMSG (msg)); - l_msg->from_mem = from_flag; - nsfw_mgr_msg_free (l_msg); - return FALSE; + NSFW_LOGERR("recv err]mgr_fd=%u,recv_len=%d,off_set=%d,errno=%d," + MSGINFO, fd, recv_len, off_set, errno, PRTMSG(msg)); + l_msg->from_mem = from_flag; + nsfw_mgr_msg_free(l_msg); + return FALSE; } } - while (recv_len + off_set < (i32) l_msg_len); - (void) nsfw_set_sock_block (fd, TRUE); - int retVal = MEMCPY_S (l_msg, msg_len, msg, msg_len); - if (EOK != retVal) + while (recv_len + off_set < (i32) l_msg_len); + (void) nsfw_set_sock_block(fd, TRUE); + int retVal = memcpy_s(l_msg, msg_len, msg, msg_len); + if (EOK != retVal) { - NSFW_LOGERR ("MEMCPY_S failed] ret=%d", retVal); - l_msg->from_mem = from_flag; - nsfw_mgr_msg_free (l_msg); - return TRUE; + NSFW_LOGERR("memcpy_s fail]ret=%d", retVal); + l_msg->from_mem = from_flag; + nsfw_mgr_msg_free(l_msg); + return TRUE; } - l_msg->from_mem = from_flag; - l_msg->msg_len = l_msg_len; + l_msg->from_mem = from_flag; + l_msg->msg_len = l_msg_len; - *large_msg = l_msg; - NSFW_LOGDBG ("recv large mgr_msg suc]mgr_fd=%u," MSGINFO, fd, - PRTMSG (l_msg)); - return TRUE; + *large_msg = l_msg; + NSFW_LOGDBG("recv large mgr_msg suc]mgr_fd=%u," MSGINFO, fd, + PRTMSG(l_msg)); + return TRUE; } /***************************************************************************** -* Prototype : nsfw_mgr_send_req_wait_rsp -* Description : send request message and block waiting for it's response - within MGR_COM_RECV_TIMEOUT +* Prototype : nsfw_mgr_get_socket +* Description : try best to return an available dst_socket * Input : nsfw_mgr_msg* req_msg * nsfw_mgr_msg* rsp_msg + i32 dst_socket (origin fd, <=0 to lookup or create, >0 to renew) * Output : None -* Return Value : u8 +* Return Value : i32 * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_send_req_wait_rsp (nsfw_mgr_msg * req_msg, nsfw_mgr_msg * rsp_msg) +i32 nsfw_mgr_get_socket(nsfw_mgr_msg * req_msg, nsfw_mgr_msg * rsp_msg, + i32 dst_socket) { - if (NULL == req_msg) + if (dst_socket <= 0) /* origin fd <= 0, first lookup the eastablished, if none, create a new one */ { - NSFW_LOGERR ("req msg nul!"); - return FALSE; + dst_socket = + nsfw_mgr_get_dst_socket(req_msg->dst_proc_type, req_msg->dst_pid); + if (dst_socket <= 0) + { + dst_socket = + nsfw_mgr_get_new_socket(req_msg->dst_proc_type, + req_msg->dst_pid); + if (dst_socket <= 0) + { + NSFW_LOGERR("send msg get dst_socket_err]" MSGINFO, + PRTMSG(req_msg)); + return -1; + } + } } - - i32 dst_socket = - nsfw_mgr_get_dst_socket (req_msg->dst_proc_type, req_msg->dst_pid); - if (dst_socket <= 0) + else /* origin fd is broken probably because the other end just upgraded, delete the old one and re-create */ { - dst_socket = - nsfw_mgr_get_new_socket (req_msg->dst_proc_type, req_msg->dst_pid); - if (dst_socket <= 0) + (void) nsfw_mgr_del_socket(dst_socket); + (void) nsfw_mgr_unreg_sock_fun(dst_socket); + (void) nsfw_base_close(dst_socket); + UNLOCK_MGR_FD(dst_socket) + dst_socket = + nsfw_mgr_get_new_socket(req_msg->dst_proc_type, req_msg->dst_pid); + if (dst_socket <= 0) { - NSFW_LOGERR ("send msg get dst_socket_error]" MSGINFO, - PRTMSG (req_msg)); - return FALSE; + NSFW_LOGERR("send msg get dst_socket_err]" MSGINFO, + PRTMSG(req_msg)); + return -1; } } + return dst_socket; +} - if ((NULL == rsp_msg) && (req_msg->msg_len == sizeof (nsfw_mgr_msg))) +/***************************************************************************** +* Prototype : nsfw_mgr_do_send_and_recv +* Description : do send and recv work +* Input : nsfw_mgr_msg* req_msg +* nsfw_mgr_msg* rsp_msg +* Output : None +* Return Value : u8 +* Calls : +* Called By : +*****************************************************************************/ +u8 nsfw_mgr_do_send_and_recv(nsfw_mgr_msg * req_msg, nsfw_mgr_msg * rsp_msg, + i32 dst_socket, i32 retry_times_left) +{ + LOCK_MGR_FD(dst_socket) + if ((NULL == rsp_msg) && (req_msg->msg_len == sizeof(nsfw_mgr_msg))) { - LOCK_MGR_FD (dst_socket) - if (FALSE == nsfw_mgr_send_msg_socket (dst_socket, req_msg)) + if (FALSE == nsfw_mgr_send_msg_socket(dst_socket, req_msg)) { - NSFW_LOGERR ("send msg error]" MSGINFO, PRTMSG (req_msg)); - g_mgr_stat.msg_send_failed++; - UNLOCK_MGR_FD (dst_socket) return FALSE; + g_mgr_stat.msg_send_failed++; + goto ERROR_RETURN; } - UNLOCK_MGR_FD (dst_socket) return TRUE; + UNLOCK_MGR_FD(dst_socket) return TRUE; } - LOCK_MGR_FD (dst_socket); - (void) nsfw_mgr_unreg_sock_fun (dst_socket); - if (FALSE == nsfw_mgr_send_msg_socket (dst_socket, req_msg)) + (void) nsfw_mgr_unreg_sock_fun(dst_socket); + if (FALSE == nsfw_mgr_send_msg_socket(dst_socket, req_msg)) { - NSFW_LOGERR ("send msg error]" MSGINFO, PRTMSG (req_msg)); - g_mgr_stat.msg_send_failed++; - (void) nsfw_mgr_reg_sock_fun (dst_socket, nsfw_mgr_new_msg); - UNLOCK_MGR_FD (dst_socket); - return FALSE; + g_mgr_stat.msg_send_failed++; + goto ERROR_RETURN; } - if (NULL == rsp_msg) + if (NULL == rsp_msg) { - (void) nsfw_mgr_reg_sock_fun (dst_socket, nsfw_mgr_new_msg); - UNLOCK_MGR_FD (dst_socket) return TRUE; + (void) nsfw_mgr_reg_sock_fun(dst_socket, nsfw_mgr_new_msg); + UNLOCK_MGR_FD(dst_socket) return TRUE; } - u16 i; - for (i = 0; i < MGR_COM_MAX_DROP_MSG; i++) + u16 i; + for (i = 0; i < MGR_COM_MAX_DROP_MSG; i++) { - if (FALSE == nsfw_mgr_recv_msg_socket (dst_socket, rsp_msg, NULL)) + if (FALSE == nsfw_mgr_recv_msg_socket(dst_socket, rsp_msg, NULL)) { - NSFW_LOGERR ("recv msg error]" MSGINFO, PRTMSG (req_msg)); - (void) nsfw_mgr_reg_sock_fun (dst_socket, nsfw_mgr_new_msg); - UNLOCK_MGR_FD (dst_socket) return FALSE; + goto ERROR_RETURN; } - if ((rsp_msg->seq == req_msg->seq) - && (rsp_msg->msg_type == req_msg->msg_type + MGR_MSG_RSP_BASE)) + if ((rsp_msg->seq == req_msg->seq) + && (rsp_msg->msg_type == req_msg->msg_type + MGR_MSG_RSP_BASE)) { - break; + break; } - NSFW_LOGINF ("recv msg forward]" MSGINFO, PRTMSG (rsp_msg)); - rsp_msg->fw_flag = TRUE; - (void) nsfw_mgr_send_msg (rsp_msg); + NSFW_LOGDBG("recv msg forward]" MSGINFO, PRTMSG(rsp_msg)); + rsp_msg->fw_flag = TRUE; + (void) nsfw_mgr_send_msg(rsp_msg); + } + + (void) nsfw_mgr_reg_sock_fun(dst_socket, nsfw_mgr_new_msg); + if (0 == req_msg->dst_pid) + { + (void) nsfw_mgr_new_socket(dst_socket, rsp_msg->src_proc_type, + rsp_msg->src_pid); + } + UNLOCK_MGR_FD(dst_socket) return TRUE; + + ERROR_RETURN: + if (0 >= retry_times_left) + { + NSFW_LOGERR("send msg err]" MSGINFO, PRTMSG(req_msg)); + (void) nsfw_mgr_reg_sock_fun(dst_socket, nsfw_mgr_new_msg); + UNLOCK_MGR_FD(dst_socket); + } + else + { + NSFW_LOGWAR + ("origin mgr socket=%d is broken, try re-create, %d times left", + dst_socket, retry_times_left); + /* NOTE: no need to re-register sock_fun, will UNLOCK in next call of nsfw_mgr_get_socket */ + } + return FALSE; + +} + +/***************************************************************************** +* Prototype : nsfw_mgr_send_req_wait_rsp +* Description : send request message and block waiting for it's response + within MGR_COM_RECV_TIMEOUT +* Input : nsfw_mgr_msg* req_msg +* nsfw_mgr_msg* rsp_msg +* Output : None +* Return Value : u8 +* Calls : +* Called By : +*****************************************************************************/ +u8 nsfw_mgr_send_req_wait_rsp(nsfw_mgr_msg * req_msg, nsfw_mgr_msg * rsp_msg) +{ + if (NULL == req_msg) + { + NSFW_LOGERR("req msg nul"); + return FALSE; } - (void) nsfw_mgr_reg_sock_fun (dst_socket, nsfw_mgr_new_msg); - if (0 == req_msg->dst_pid) + i32 dst_socket = 0; + i32 retry_times_left = 1; + while (retry_times_left >= 0) { - (void) nsfw_mgr_new_socket (dst_socket, rsp_msg->src_proc_type, - rsp_msg->src_pid); + dst_socket = nsfw_mgr_get_socket(req_msg, rsp_msg, dst_socket); + if (dst_socket <= 0) + { + NSFW_LOGERR("send msg get dst_socket_err]" MSGINFO, + PRTMSG(req_msg)); + return FALSE; + } + u8 ret = nsfw_mgr_do_send_and_recv(req_msg, rsp_msg, dst_socket, + retry_times_left); + if (ret == TRUE) + { + return TRUE; + } + --retry_times_left; } - UNLOCK_MGR_FD (dst_socket) return TRUE; + return FALSE; } /***************************************************************************** @@ -1001,10 +1151,9 @@ nsfw_mgr_send_req_wait_rsp (nsfw_mgr_msg * req_msg, nsfw_mgr_msg * rsp_msg) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_send_msg (nsfw_mgr_msg * msg) +u8 nsfw_mgr_send_msg(nsfw_mgr_msg * msg) { - return nsfw_mgr_send_req_wait_rsp (msg, NULL); + return nsfw_mgr_send_req_wait_rsp(msg, NULL); } /***************************************************************************** @@ -1016,79 +1165,77 @@ nsfw_mgr_send_msg (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_msg_in (i32 fd) +u8 nsfw_mgr_msg_in(i32 fd) { - u32 i = 0; - u8 ret = FALSE; - u8 msg_match = FALSE; - nsfw_mgr_msg *msg = nsfw_mgr_null_rspmsg_alloc (); - nsfw_mgr_msg *large_msg = NULL; + u32 i = 0; + u8 ret = FALSE; + u8 msg_match = FALSE; + nsfw_mgr_msg *msg = nsfw_mgr_null_rspmsg_alloc(); + nsfw_mgr_msg *large_msg = NULL; - LOCK_MGR_FD (fd) ret = nsfw_mgr_recv_msg_socket (fd, msg, &large_msg); - UNLOCK_MGR_FD (fd) if (large_msg != NULL) + LOCK_MGR_FD(fd) ret = nsfw_mgr_recv_msg_socket(fd, msg, &large_msg); + UNLOCK_MGR_FD(fd) if (large_msg != NULL) { - nsfw_mgr_msg_free (msg); - msg = large_msg; + nsfw_mgr_msg_free(msg); + msg = large_msg; } - if (FALSE == ret) + if (FALSE == ret) { - nsfw_mgr_msg_free (msg); - return FALSE; + nsfw_mgr_msg_free(msg); + return FALSE; } - - if (msg->fw_flag != TRUE) + if (msg->fw_flag != TRUE) { - (void) nsfw_mgr_new_socket (fd, msg->src_proc_type, msg->src_pid); + (void) nsfw_mgr_new_socket(fd, msg->src_proc_type, msg->src_pid); } - if (msg->msg_type < MGR_MSG_MAX) + if (msg->msg_type < MGR_MSG_MAX) { - for (i = 0; i < NSFW_MGRCOM_MAX_PROC_FUN; i++) + for (i = 0; i < NSFW_MGRCOM_MAX_PROC_FUN; i++) { - if (NULL == g_mgr_fun[msg->msg_type][i]) + if (NULL == g_mgr_fun[msg->msg_type][i]) { - break; + break; } - (void) g_mgr_fun[msg->msg_type][i] (msg); - msg_match = TRUE; + (void) g_mgr_fun[msg->msg_type][i] (msg); + msg_match = TRUE; } } - if (FALSE != msg_match) + if (FALSE != msg_match) { - nsfw_mgr_msg_free (msg); - return TRUE; + nsfw_mgr_msg_free(msg); + return TRUE; } - if (msg->msg_type < MGR_MSG_RSP_BASE) + if (msg->msg_type < MGR_MSG_RSP_BASE) { - NSFW_LOGERR ("msg match failed! auto rsp]" MSGINFO, PRTMSG (msg)); - nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg); - if (NULL != rsp_msg) + NSFW_LOGERR("msg match failed! auto rsp]" MSGINFO, PRTMSG(msg)); + nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc(msg); + if (NULL != rsp_msg) { - rsp_msg->resp_code = NSFW_MGR_MSG_TYPE_ERROR; - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); + rsp_msg->resp_code = NSFW_MGR_MSG_TYPE_ERROR; + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); } } - NSFW_LOGERR ("drop msg]" MSGINFO, PRTMSG (msg)); - /* fix "Out-of-bounds write" type codex issue */ - if (msg->msg_type < MGR_MSG_MAX) + NSFW_LOGERR("drop msg]" MSGINFO, PRTMSG(msg)); + /* fix "Out-of-bounds write" */ + if (msg->msg_type < MGR_MSG_MAX) { - g_mgr_stat.recv_drop[msg->msg_type]++; + g_mgr_stat.recv_drop[msg->msg_type]++; } - nsfw_mgr_msg_free (msg); - return FALSE; + nsfw_mgr_msg_free(msg); + return FALSE; } /***************************************************************************** * Prototype : nsfw_mgr_new_msg -* Description : when new mgr message receive from socket, this function +* Description : when new mgr message recive from socket, this funciton will call back * Input : i32 epfd * i32 fd @@ -1098,22 +1245,19 @@ nsfw_mgr_msg_in (i32 fd) * Calls : * Called By : *****************************************************************************/ -int -nsfw_mgr_new_msg (i32 epfd, i32 fd, u32 events) +/*try to get event form all modules */ +int nsfw_mgr_new_msg(i32 epfd, i32 fd, u32 events) { - lint_lock_1 (); - if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) + if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) { - (void) nsfw_mgr_del_socket (fd); - (void) nsfw_mgr_unreg_sock_fun (fd); - (void) nsfw_base_close (fd); - lint_unlock_1 (); - return TRUE; + (void) nsfw_mgr_del_socket(fd); + (void) nsfw_mgr_unreg_sock_fun(fd); + (void) nsfw_base_close(fd); + return TRUE; } - (void) nsfw_mgr_msg_in (fd); - lint_unlock_1 (); - return TRUE; + (void) nsfw_mgr_msg_in(fd); + return TRUE; } /***************************************************************************** @@ -1126,11 +1270,11 @@ nsfw_mgr_new_msg (i32 epfd, i32 fd, u32 events) * Calls : * Called By : *****************************************************************************/ -int -nsfw_mgr_com_rereg_fun (u32 timer_type, void *data) +int nsfw_mgr_com_rereg_fun(u32 timer_type, void *data) { - (void) nsfw_mgr_reg_sock_fun (timer_type, (nsfw_mgr_sock_fun) data); - return TRUE; + + (void) nsfw_mgr_reg_sock_fun(timer_type, (nsfw_mgr_sock_fun) data); + return TRUE; } /***************************************************************************** @@ -1144,18 +1288,18 @@ nsfw_mgr_com_rereg_fun (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -int -nsfw_mgr_com_socket_error (i32 fd, nsfw_mgr_sock_fun fun, i32 timer) +int nsfw_mgr_com_socket_error(i32 fd, nsfw_mgr_sock_fun fun, i32 timer) { - struct timespec time_left = { timer, 0 }; - nsfw_mgr_unreg_sock_fun (fd); - nsfw_timer_reg_timer (fd, (void *) fun, nsfw_mgr_com_rereg_fun, time_left); - return TRUE; + + struct timespec time_left = { timer, 0 }; + nsfw_mgr_unreg_sock_fun(fd); + nsfw_timer_reg_timer(fd, (void *) fun, nsfw_mgr_com_rereg_fun, time_left); + return TRUE; } /***************************************************************************** * Prototype : nsfw_mgr_new_connection -* Description : when new mgr connection in, this function will call back +* Description : when new mgr connection in, this funciton will call back * Input : i32 epfd * i32 fd * u32 events @@ -1164,81 +1308,79 @@ nsfw_mgr_com_socket_error (i32 fd, nsfw_mgr_sock_fun fun, i32 timer) * Calls : * Called By : *****************************************************************************/ -int -nsfw_mgr_new_connection (i32 epfd, i32 fd, u32 events) +int nsfw_mgr_new_connection(i32 epfd, i32 fd, u32 events) { - if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) - { - (void) nsfw_base_close (fd); - NSFW_LOGWAR ("listen disconnect!]epfd=%d,listen=%d,event=0x%x", epfd, - fd, events); - (void) nsfw_mgr_unreg_sock_fun (fd); - i32 listen_fd = nsfw_mgr_get_listen_socket (); - if (listen_fd < 0) + if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) + { + (void) nsfw_base_close(fd); + NSFW_LOGWAR("listen disconnect]epfd=%d,listen=%d,event=0x%x", epfd, + fd, events); + (void) nsfw_mgr_unreg_sock_fun(fd); + i32 listen_fd = nsfw_mgr_get_listen_socket(); + if (listen_fd < 0) { - NSFW_LOGERR - ("get listen_fd failed!]epfd=%d,listen_fd=%d,event=0x%x", epfd, - fd, events); - return FALSE; + NSFW_LOGERR("get listen_fd fail]epfd=%d,listen_fd=%d,event=0x%x", + epfd, fd, events); + return FALSE; } - (void) nsfw_mgr_reg_sock_fun (listen_fd, nsfw_mgr_new_connection); - return TRUE; + (void) nsfw_mgr_reg_sock_fun(listen_fd, nsfw_mgr_new_connection); + return TRUE; } - struct sockaddr in_addr; - socklen_t in_len; - int infd; - in_len = sizeof in_addr; + int infd; + struct sockaddr addr; + socklen_t len = sizeof(addr); + int size, size_len; + u8 accept_flag = FALSE; - int size, size_len; - u8 accept_flag = FALSE; - while (1) + while (1) { - infd = nsfw_base_accept (fd, &in_addr, &in_len); - if (infd == -1) + infd = nsfw_base_accept(fd, &addr, &len); + if (infd == -1) { - if (FALSE == accept_flag) + if (FALSE == accept_flag) { - nsfw_mgr_com_socket_error (fd, nsfw_mgr_new_connection, 1); + nsfw_mgr_com_socket_error(fd, nsfw_mgr_new_connection, 1); } - break; + break; } - if (-1 == nsfw_set_close_on_exec (infd)) + /* close on exec */ + if (-1 == nsfw_set_close_on_exec(infd)) { - (void) nsfw_base_close (infd); - NSFW_LOGERR ("set exec err]fd=%d, errno=%d", infd, errno); - break; + (void) nsfw_base_close(infd); + NSFW_LOGERR("set exec err]fd=%d, errno=%d", infd, errno); + break; } - size = MAX_RECV_BUF_DEF; - size_len = sizeof (size); - if (0 > - nsfw_base_setsockopt (infd, SOL_SOCKET, SO_RCVBUF, (void *) &size, - (socklen_t) size_len)) + size = MAX_RECV_BUF_DEF; + size_len = sizeof(size); + if (0 > + nsfw_base_setsockopt(infd, SOL_SOCKET, SO_RCVBUF, (void *) &size, + (socklen_t) size_len)) { - NSFW_LOGERR ("set socket opt err!]error=%d", errno); + NSFW_LOGERR("set socket opt err]error=%d", errno); } - if (0 > - nsfw_base_setsockopt (infd, SOL_SOCKET, SO_SNDBUF, (void *) &size, - (socklen_t) size_len)) + if (0 > + nsfw_base_setsockopt(infd, SOL_SOCKET, SO_SNDBUF, (void *) &size, + (socklen_t) size_len)) { - NSFW_LOGERR ("set socket opt err!]error=%d", errno); + NSFW_LOGERR("set socket opt err]error=%d", errno); } - (void) nsfw_mgr_reg_sock_fun (infd, nsfw_mgr_new_msg); - NSFW_LOGDBG ("accept_flag new fd]new_mgr_fd=%d", infd); - accept_flag = TRUE; + (void) nsfw_mgr_reg_sock_fun(infd, nsfw_mgr_new_msg); + NSFW_LOGDBG("accept_flag new fd]new_mgr_fd=%d", infd); + accept_flag = TRUE; } - return TRUE; + return TRUE; } /***************************************************************************** * Prototype : nsfw_set_sock_block -* Description : set fd block or not for epoll thread +* Description : set fd blok or not for epoll thread * Input : i32 sock * u8 flag * Output : None @@ -1246,45 +1388,45 @@ nsfw_mgr_new_connection (i32 epfd, i32 fd, u32 events) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_set_sock_block (i32 sock, u8 flag) +i32 nsfw_set_sock_block(i32 sock, u8 flag) { - i32 flags; - flags = nsfw_base_fcntl (sock, F_GETFL, 0); - if (flags < 0) + i32 flags; + flags = nsfw_base_fcntl(sock, F_GETFL, 0); + if (flags < 0) { - NSFW_LOGERR ("fcntl err]new_mgr_fd=%d,errno=%d", sock, errno); - return -1; + NSFW_LOGERR("fcntl err]new_mgr_fd=%d,errno=%d", sock, errno); + return -1; } - if (TRUE == flag) + if (TRUE == flag) { - flags = flags | O_NONBLOCK; + flags = flags | O_NONBLOCK; } - else + else { - flags = flags & (~O_NONBLOCK); - struct timeval tv; - tv.tv_sec = MGR_COM_RECV_TIMEOUT; - tv.tv_usec = 0; - if (nsfw_base_setsockopt - (sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv)) + flags = flags & (~O_NONBLOCK); + struct timeval tv; + tv.tv_sec = MGR_COM_RECV_TIMEOUT; + tv.tv_usec = 0; + if (nsfw_base_setsockopt + (sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &tv, sizeof tv)) { - NSFW_LOGERR ("setsockopt socket err]mgr_fd=%d", sock); - return -1; + NSFW_LOGERR("setsockopt socket err]mgr_fd=%d", sock); + return -1; } } - if (nsfw_base_fcntl (sock, F_SETFL, flags) < 0) + if (nsfw_base_fcntl(sock, F_SETFL, flags) < 0) { - NSFW_LOGERR ("fcntl err]new_mgr_fd=%d,errno=%d,flags=%d", sock, errno, - flags); - return -1; + NSFW_LOGERR("fcntl err]new_mgr_fd=%d,errno=%d,flags=%d", sock, errno, + flags); + return -1; } - return 0; + return 0; } +/* close on exec */ /***************************************************************************** * Prototype : nsfw_set_close_on_exec * Description : close on exec set @@ -1294,26 +1436,25 @@ nsfw_set_sock_block (i32 sock, u8 flag) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_set_close_on_exec (i32 sock) +i32 nsfw_set_close_on_exec(i32 sock) { - i32 flags; - flags = nsfw_base_fcntl (sock, F_GETFD, 0); - if (flags < 0) + i32 flags; + flags = nsfw_base_fcntl(sock, F_GETFD, 0); + if (flags < 0) { - NSFW_LOGERR ("fcntl err]fd=%d,errno=%d", sock, errno); - return -1; + NSFW_LOGERR("fcntl err]fd=%d,errno=%d", sock, errno); + return -1; } - flags |= FD_CLOEXEC; + flags |= FD_CLOEXEC; - if (nsfw_base_fcntl (sock, F_SETFD, flags) < 0) + if (nsfw_base_fcntl(sock, F_SETFD, flags) < 0) { - NSFW_LOGERR ("fcntl err]fd=%d,errno=%d,flags=%d", sock, errno, flags); - return -1; + NSFW_LOGERR("fcntl err]fd=%d,errno=%d,flags=%d", sock, errno, flags); + return -1; } - return 0; + return 0; } /***************************************************************************** @@ -1325,29 +1466,35 @@ nsfw_set_close_on_exec (i32 sock) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_add_sock_to_ep (i32 fd) +u8 nsfw_add_sock_to_ep(i32 fd) { - struct epoll_event event; - event.data.fd = fd; - event.events = EPOLLIN; - if (g_ep_proc.epfd == 0) + struct epoll_event event; + event.data.fd = fd; + event.events = EPOLLIN; + if (g_ep_proc.epfd == 0) { - return TRUE; + return TRUE; } - (void) nsfw_set_sock_block (fd, TRUE); + (void) nsfw_set_sock_block(fd, TRUE); - if (0 > nsfw_base_epoll_ctl (g_ep_proc.epfd, EPOLL_CTL_ADD, fd, &event)) + if (0 > nsfw_base_epoll_ctl(g_ep_proc.epfd, EPOLL_CTL_ADD, fd, &event)) { - NSFW_LOGINF - ("add sock to ep thread failed]mgr_fd=%d,errno=%d,epfd=%d", fd, - errno, g_ep_proc.epfd); - return FALSE; + /*It is possible that multi-threads operate EPOLL_ADD at the same time, + *we allow this case, and return TRUE for it when errno==EEXIST*/ + if (EEXIST == errno) + { + NSFW_LOGDBG + ("add sock to ep thread but exist already, return TRUE directly]mgr_fd=%d,epfd=%d", + fd, g_ep_proc.epfd) return TRUE; + } + NSFW_LOGINF("add sock to ep thread fail]mgr_fd=%d,errno=%d,epfd=%d", + fd, errno, g_ep_proc.epfd); + return FALSE; } - NSFW_LOGDBG ("add sock to ep thread]mgr_fd=%d,epfd=%d", fd, - g_ep_proc.epfd) return TRUE; + NSFW_LOGDBG("add sock to ep thread]mgr_fd=%d,epfd=%d", fd, + g_ep_proc.epfd) return TRUE; } /***************************************************************************** @@ -1359,29 +1506,28 @@ nsfw_add_sock_to_ep (i32 fd) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_rmv_sock_from_ep (i32 fd) +u8 nsfw_rmv_sock_from_ep(i32 fd) { - struct epoll_event event; - event.data.fd = fd; - event.events = EPOLLIN; - if (g_ep_proc.epfd == 0) + struct epoll_event event; + event.data.fd = fd; + event.events = EPOLLIN; + if (g_ep_proc.epfd == 0) { - return TRUE; + //NSFW_LOGINF("rmv sock to ep thread before start]mgr_fd=%d",fd); + return TRUE; } - (void) nsfw_set_sock_block (fd, FALSE); + (void) nsfw_set_sock_block(fd, FALSE); - if (0 > nsfw_base_epoll_ctl (g_ep_proc.epfd, EPOLL_CTL_DEL, fd, &event)) + if (0 > nsfw_base_epoll_ctl(g_ep_proc.epfd, EPOLL_CTL_DEL, fd, &event)) { - NSFW_LOGINF - ("rmv sock to ep thread failed] mgr_fd=%d,errno=%d,epfd=%d", fd, - errno, g_ep_proc.epfd); - return FALSE; + NSFW_LOGINF("rmv sock to ep thread fail]mgr_fd=%d,errno=%d,epfd=%d", + fd, errno, g_ep_proc.epfd); + return FALSE; } - NSFW_LOGDBG ("rmv sock to ep thread] mgr_fd=%d,epfd=%d", fd, - g_ep_proc.epfd) return TRUE; + NSFW_LOGDBG("rmv sock to ep thread]mgr_fd=%d,epfd=%d", fd, + g_ep_proc.epfd) return TRUE; } /***************************************************************************** @@ -1395,33 +1541,28 @@ nsfw_rmv_sock_from_ep (i32 fd) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_reg_sock_fun (i32 fd, nsfw_mgr_sock_fun fun) +/*try to get event form all modules */ +u8 nsfw_mgr_reg_sock_fun(i32 fd, nsfw_mgr_sock_fun fun) { - lint_lock_1 (); - if ((fd >= (i32) NSFW_MGR_FD_MAX) || (fd < 0) || NULL == fun) + if ((fd >= (i32) NSFW_MGR_FD_MAX) || (fd < 0) || NULL == fun) { - NSFW_LOGINF ("reg sock fun error!] mgr_fd=%d,fun=%p", fd, fun); - lint_unlock_1 (); - return FALSE; + NSFW_LOGINF("reg sock fun error]mgr_fd=%d,fun=%p", fd, fun); + return FALSE; } - if ((g_ep_proc.ep_fun) && (NULL == g_ep_proc.ep_fun[fd])) + if ((g_ep_proc.ep_fun) && (NULL == g_ep_proc.ep_fun[fd])) { - g_ep_proc.ep_fun[fd] = fun; - if (FALSE == nsfw_add_sock_to_ep (fd)) + g_ep_proc.ep_fun[fd] = fun; + if (FALSE == nsfw_add_sock_to_ep(fd)) { - g_ep_proc.ep_fun[fd] = NULL; - lint_unlock_1 (); - return FALSE; + g_ep_proc.ep_fun[fd] = NULL; + return FALSE; } - NSFW_LOGDBG ("reg sock fun] mgr_fd=%d,fun=%p", fd, fun); - lint_unlock_1 (); - return TRUE; + NSFW_LOGDBG("reg sock fun]mgr_fd=%d,fun=%p", fd, fun); + return TRUE; } - lint_unlock_1 (); - return FALSE; + return FALSE; } /***************************************************************************** @@ -1433,28 +1574,24 @@ nsfw_mgr_reg_sock_fun (i32 fd, nsfw_mgr_sock_fun fun) * Calls : * Called By : *****************************************************************************/ -void -nsfw_mgr_unreg_sock_fun (i32 fd) +/*try to get event form all modules */ +void nsfw_mgr_unreg_sock_fun(i32 fd) { - lint_lock_1 (); - if (fd >= (i32) NSFW_MGR_FD_MAX) + if (fd >= (i32) NSFW_MGR_FD_MAX) { - NSFW_LOGINF ("unreg sock fun failed!] mgr_fd=%d", fd); - lint_unlock_1 (); - return; + NSFW_LOGINF("unreg sock fun fail]mgr_fd=%d", fd); + return; } - if ((g_ep_proc.ep_fun) && (NULL != g_ep_proc.ep_fun[fd])) + if ((g_ep_proc.ep_fun) && (NULL != g_ep_proc.ep_fun[fd])) { - g_ep_proc.ep_fun[fd] = NULL; - (void) nsfw_rmv_sock_from_ep (fd); - NSFW_LOGDBG ("unreg sock fun] mgr_fd=%d", fd); - lint_unlock_1 (); - return; + g_ep_proc.ep_fun[fd] = NULL; + (void) nsfw_rmv_sock_from_ep(fd); + NSFW_LOGDBG("unreg sock fun]mgr_fd=%d", fd); + return; } - lint_unlock_1 (); - return; + return; } /***************************************************************************** @@ -1468,22 +1605,21 @@ nsfw_mgr_unreg_sock_fun (i32 fd) * Calls : * Called By : *****************************************************************************/ -NSTACK_STATIC inline u8 -nsfw_sock_fun_callback (i32 epfd, i32 fd, u32 events) +NSTACK_STATIC inline u8 nsfw_sock_fun_callback(i32 epfd, i32 fd, u32 events) { - if ((fd < (i32) NSFW_MGR_FD_MAX) - && (g_ep_proc.ep_fun) && (NULL != g_ep_proc.ep_fun[fd])) + if ((fd < (i32) NSFW_MGR_FD_MAX) + && (g_ep_proc.ep_fun) && (NULL != g_ep_proc.ep_fun[fd])) { - (void) g_ep_proc.ep_fun[fd] (epfd, fd, events); - return TRUE; + (void) g_ep_proc.ep_fun[fd] (epfd, fd, events); + return TRUE; } - return FALSE; + return FALSE; } /***************************************************************************** -* Prototype : nsfw_sock_add_to_ep -* Description : add all event process function has ben reg to the epoll +* Prototype : nsfw_add_prestored_sock_to_ep +* Description : add all the socks (whose process function has ben reg) to the epoll thread when thread start * Input : i32 epfd * Output : None @@ -1491,21 +1627,37 @@ nsfw_sock_fun_callback (i32 epfd, i32 fd, u32 events) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_sock_add_to_ep (i32 epfd) +u8 nsfw_add_prestored_sock_to_ep() { - u32 i; + u32 i; + u8 ret; + + if (NULL == g_ep_proc.ep_fun) + { + NSFW_LOGERR("g_ep_proc.ep_fun should not be NULL at this time"); + return FALSE; + } - for (i = 0; i < NSFW_MGR_FD_MAX; i++) + for (i = 0; i < NSFW_MGR_FD_MAX; i++) { - if ((g_ep_proc.ep_fun) && (NULL == g_ep_proc.ep_fun[i])) + if (NULL == g_ep_proc.ep_fun[i]) { - continue; + continue; + } + + /*add return value check */ + ret = nsfw_add_sock_to_ep(i); + if (FALSE == ret) + { + /*here, we add some fd(which belongs to other module and prestore to g_ep_proc) to ep . + must add success. if add fail, stack init should end up */ + NSFW_LOGERR("nsfw_add_sock_to_ep fail]epfd=%d,i=%u", + g_ep_proc.epfd, i); + return FALSE; } - (void) nsfw_add_sock_to_ep (i); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -1517,19 +1669,18 @@ nsfw_sock_add_to_ep (i32 epfd) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_com_start () +u8 nsfw_mgr_com_start() { - i32 listen_fd = nsfw_mgr_get_listen_socket (); - if (listen_fd < 0) + i32 listern_fd = nsfw_mgr_get_listen_socket(); + if (listern_fd < 0) { - NSFW_LOGERR ("get listen_fd failed!"); - return FALSE; + NSFW_LOGERR("get listern_fd fail"); + return FALSE; } - NSFW_LOGINF ("start mgr_com module!] listen_fd=%d", listen_fd); - (void) nsfw_mgr_reg_sock_fun (listen_fd, nsfw_mgr_new_connection); - return TRUE; + NSFW_LOGINF("start mgr_com module]listern_fd=%d", listern_fd); + (void) nsfw_mgr_reg_sock_fun(listern_fd, nsfw_mgr_new_connection); + return TRUE; } /***************************************************************************** @@ -1541,24 +1692,28 @@ nsfw_mgr_com_start () * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_com_start_local (u8 proc_type) +u8 nsfw_mgr_com_start_local(u8 proc_type) { - int fd[2]; - if ((socketpair (AF_UNIX, SOCK_STREAM, 0, fd)) < 0) + int fd[2]; + if ((socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) < 0) { - NSFW_LOGERR ("create socket err] type=%u,errno=%d", proc_type, errno); - return FALSE; + NSFW_LOGERR("create socket err]type=%u,errno=%d", proc_type, errno); + return FALSE; } - (void) nsfw_mgr_new_socket (fd[0], proc_type, get_sys_pid ()); - (void) nsfw_mgr_new_socket (fd[1], proc_type, get_sys_pid ()); - (void) nsfw_mgr_reg_sock_fun (fd[0], nsfw_mgr_new_msg); - (void) nsfw_mgr_reg_sock_fun (fd[1], nsfw_mgr_new_msg); - NSFW_LOGINF ("create local socket] fd0=%d,fd1=%d", fd[0], fd[1]); - return TRUE; + (void) nsfw_mgr_new_socket(fd[0], proc_type, get_sys_pid()); + (void) nsfw_mgr_new_socket(fd[1], proc_type, get_sys_pid()); + (void) nsfw_mgr_reg_sock_fun(fd[0], nsfw_mgr_new_msg); + (void) nsfw_mgr_reg_sock_fun(fd[1], nsfw_mgr_new_msg); + if (proc_type != NSFW_PROC_CTRL) + { + NSFW_LOGINF("create local socket]fd0=%d,fd1=%d", fd[0], fd[1]); + } + return TRUE; } +/*try to get event form all modules */ + /***************************************************************************** * Prototype : nsfw_mgr_listen_thread * Description : epoll thread function @@ -1568,115 +1723,40 @@ nsfw_mgr_com_start_local (u8 proc_type) * Calls : * Called By : *****************************************************************************/ -void * -nsfw_mgr_listen_thread (void *arg) +void *nsfw_mgr_listen_thread(void *arg) { - i32 epfd = 0; - //i32 listen_socket = 0; - - lint_lock_1 (); #define MAXEVENTS 10 - epfd = nsfw_base_epoll_create (10); + struct epoll_event events[MAXEVENTS]; + + i32 epfd = g_ep_proc.epfd; - struct epoll_event events[MAXEVENTS]; - if (EOK != MEMSET_S (events, sizeof (events), 0, sizeof (events))) + /* Init log suppression for this thread */ + if (NULL == init_sup_table(LOG_SUP_TABLE_SIZE_FOR_MGR_COM_THREAD)) { - NSFW_LOGERR ("MEMSET_S failed!]epfd=%d", epfd); - lint_unlock_1 (); - return NULL; + NSFW_LOGWAR("log suppression init failed in thread: %s", + NSFW_MGRCOM_THREAD); } - g_ep_proc.epfd = epfd; - g_ep_proc.hbt_count = 0; - (void) nsfw_sock_add_to_ep (epfd); - lint_unlock_1 (); - while (1) + while (1) { - lint_lock_1 (); - int n, i; - n = nsfw_base_epoll_wait (epfd, events, MAXEVENTS, -1); - for (i = 0; i < n; i++) + int n, i; + n = nsfw_base_epoll_wait(epfd, events, MAXEVENTS, -1); + for (i = 0; i < n; i++) { - if (TRUE == - nsfw_sock_fun_callback (epfd, events[i].data.fd, - events[i].events)) + if (TRUE == + nsfw_sock_fun_callback(epfd, events[i].data.fd, + events[i].events)) { - g_ep_proc.hbt_count = 0; - continue; + g_ep_proc.hbt_count = 0; /*, we know here has multi-thread case, but we allow it */ + continue; } - NSFW_LOGERR ("error event recv] fd=%d,event=%d", - events[i].data.fd, events[i].events); + NSFW_LOGERR("error event recv]fd=%d,event=%u", events[i].data.fd, + events[i].events); } - lint_unlock_1 (); - } - -} - -NSTACK_STATIC inline void -get_thread_policy (pthread_attr_t * attr) -{ - int policy; - int rs = pthread_attr_getschedpolicy (attr, &policy); - if (rs != 0) - { - NSFW_LOGERR ("pthread_attr_getschedpolicy failed"); - return; - } - switch (policy) - { - case SCHED_FIFO: - NSFW_LOGINF ("policy= SCHED_FIFO"); - break; - case SCHED_RR: - NSFW_LOGINF ("policy= SCHED_RR"); - break; - case SCHED_OTHER: - NSFW_LOGINF ("policy=SCHED_OTHER"); - break; - default: - NSFW_LOGINF ("policy=UNKNOWN"); - break; - } - - return; -} -NSTACK_STATIC inline void -get_thread_priority (pthread_attr_t * attr) -{ - struct sched_param param; - int rs = pthread_attr_getschedparam (attr, ¶m); - if (rs != 0) - { - NSFW_LOGERR ("pthread_attr_getschedparam failed"); - return; } - NSFW_LOGINF ("get thread priority] pri=%d", param.sched_priority); -} - -/* support thread priority configuration */ -void -set_thread_attr (pthread_attr_t * pattr, int stacksize, int pri, int policy) -{ - struct sched_param param; - (void) pthread_attr_init (pattr); - - if (stacksize > 0) - { - (void) pthread_attr_setstacksize (pattr, stacksize); - } - - param.sched_priority = pri; - if (SCHED_OTHER != policy) - { - (void) pthread_attr_setschedpolicy (pattr, policy); - (void) pthread_attr_setschedparam (pattr, ¶m); - (void) pthread_attr_setinheritsched (pattr, PTHREAD_EXPLICIT_SCHED); - } - get_thread_policy (pattr); - get_thread_priority (pattr); } /***************************************************************************** @@ -1688,66 +1768,71 @@ set_thread_attr (pthread_attr_t * pattr, int stacksize, int pri, int policy) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_mgr_ep_start () +u8 nsfw_mgr_ep_start() { - /* heart beat thread should have the same priority with the tcpip thread */ - pthread_attr_t attr; - pthread_attr_t *pattr = NULL; + pthread_attr_t *pattr = NULL; - if (g_thread_policy != SCHED_OTHER) + /* move this code snippet from nsfw_mgr_listen_thread to here, + * to avoid multi-thread problem when calling nsfw_add_prestored_sock_to_ep in nsfw_mgr_listen_thread*/ + g_ep_proc.epfd = nsfw_base_epoll_create(10); + if (g_ep_proc.epfd <= 0) { - set_thread_attr (&attr, 0, g_thread_pri, g_thread_policy); - pattr = &attr; + NSFW_LOGERR("epoll_create fail]epfd=%d,errno=%d", g_ep_proc.epfd, + errno); + return FALSE; } + g_ep_proc.hbt_count = 0; - if (pthread_create - (&g_ep_proc.ep_thread, pattr, nsfw_mgr_listen_thread, NULL)) + if (FALSE == nsfw_add_prestored_sock_to_ep()) { - return FALSE; + return FALSE; } - NSFW_LOGINF ("start thread] id=%d", g_ep_proc.ep_thread); + if (pthread_create + (&g_ep_proc.ep_thread, pattr, nsfw_mgr_listen_thread, NULL)) + { + return FALSE; + } - if (pthread_setname_np (g_ep_proc.ep_thread, NSFW_MGRCOM_THREAD)) + NSFW_LOGINF("start thread]id=%d", g_ep_proc.ep_thread); + + if (pthread_setname_np(g_ep_proc.ep_thread, NSFW_MGRCOM_THREAD)) { - return TRUE; + return TRUE; } - (void) nsfw_reg_trace_thread (g_ep_proc.ep_thread); - return TRUE; + (void) nsfw_reg_trace_thread(g_ep_proc.ep_thread); + return TRUE; } -int -nsfw_mgr_com_chk_hbt (int v_add) +int nsfw_mgr_com_chk_hbt(int v_add) { - int ret = g_ep_proc.hbt_count; - g_ep_proc.hbt_count += v_add; - return ret; + int ret = g_ep_proc.hbt_count; + g_ep_proc.hbt_count += v_add; + return ret; } /***************************************************************************** * Prototype : nsfw_mgr_comm_fd_destroy -* Description : free the memory +* Description : free the memeory * Input : * Output : None * Return Value : int * Calls : * Called By : *****************************************************************************/ -void -nsfw_mgr_comm_fd_destroy () +void nsfw_mgr_comm_fd_destroy() { - if (g_ep_proc.ep_fun) + if (g_ep_proc.ep_fun) { - free (g_ep_proc.ep_fun); - g_ep_proc.ep_fun = NULL; + free(g_ep_proc.ep_fun); + g_ep_proc.ep_fun = NULL; } - if (g_mgr_socket_map.sock) + if (g_mgr_sockt_map.sock) { - free (g_mgr_socket_map.sock); - g_mgr_socket_map.sock = NULL; + free(g_mgr_sockt_map.sock); + g_mgr_sockt_map.sock = NULL; } - return; + return; } /***************************************************************************** @@ -1759,115 +1844,91 @@ nsfw_mgr_comm_fd_destroy () * Calls : * Called By : *****************************************************************************/ -int -nsfw_mgr_comm_fd_init (u32 proc_type) +int nsfw_mgr_comm_fd_init(u32 proc_type) { - /*only app need to do this */ - if ((g_mgr_socket_map.sock) && (g_ep_proc.ep_fun)) + /*distributed log collect, RANCU multi-nodes senario, nstack fd is limited to 1024, + this leads to DLOG SERVER container can't accept new connection, log can't be record normally. + maxfd limit to 60k begin */ + /*only app need to do this */ + errno_t err_ret = -1; + + if ((g_mgr_sockt_map.sock) && (g_ep_proc.ep_fun)) { - return 0; + return 0; } - if (NSFW_PROC_APP == proc_type) + if (NSFW_PROC_APP == proc_type) { - long sysfdmax = 0; - sysfdmax = sysconf (_SC_OPEN_MAX); - NSFW_LOGINF ("] sys max open files=%d", sysfdmax); - if (sysfdmax > 0) + long sysfdmax = 0; + sysfdmax = sysconf(_SC_OPEN_MAX); + NSFW_LOGINF("sys max open files]fds=%ld", sysfdmax); + if (sysfdmax > 0) { - NSFW_MGR_FD_MAX = - (int) ((sysfdmax <= - NSFW_MGRCOM_MAX_SOCKET * - 60) ? sysfdmax : NSFW_MGRCOM_MAX_SOCKET * 60); + NSFW_MGR_FD_MAX = + (int) ((sysfdmax <= + NSFW_MGRCOM_MAX_SOCKET * + 60) ? sysfdmax : NSFW_MGRCOM_MAX_SOCKET * 60); } - else + else { - NSFW_LOGERR ("get sys max open file fail"); - NSFW_MGR_FD_MAX = NSFW_MGRCOM_MAX_SOCKET; + NSFW_LOGERR("get sys max open file fail"); + NSFW_MGR_FD_MAX = NSFW_MGRCOM_MAX_SOCKET; } } - NSFW_LOGINF ("] final max fd=%d", NSFW_MGR_FD_MAX); - if (!g_mgr_socket_map.sock) + /* distributed log collect, RANCU multi-nodes senario, nstack fd is limited to 1024, + this leads to DLOG SERVER container can't accept new connection, log can't be record normally. + maxfd limit to 60k end */ + if (proc_type != NSFW_PROC_CTRL) { - g_mgr_socket_map.sock = - (nsfw_mgr_sock_info *) malloc (sizeof (nsfw_mgr_sock_info) * - NSFW_MGR_FD_MAX); - if (NULL == g_mgr_socket_map.sock) - { - NSFW_LOGERR ("malloc fail] length=%d", - sizeof (nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX); - return -1; - } - (void) MEMSET_S (g_mgr_socket_map.sock, - sizeof (nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX, 0, - sizeof (nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX); + NSFW_LOGINF("final]max fd=%d", NSFW_MGR_FD_MAX); } - if (!g_ep_proc.ep_fun) + + if (!g_mgr_sockt_map.sock) { - g_ep_proc.ep_fun = - (nsfw_mgr_sock_fun *) malloc (sizeof (nsfw_mgr_sock_fun) * - NSFW_MGR_FD_MAX); - if (NULL == g_ep_proc.ep_fun) + g_mgr_sockt_map.sock = + (nsfw_mgr_sock_info *) malloc(sizeof(nsfw_mgr_sock_info) * + NSFW_MGR_FD_MAX); + if (NULL == g_mgr_sockt_map.sock) { - NSFW_LOGERR ("malloc fail] length=%d ", - sizeof (nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX); - return -1; + NSFW_LOGERR("malloc fail]length=%d", + sizeof(nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX); + return -1; + } + err_ret = + memset_s(g_mgr_sockt_map.sock, + sizeof(nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX, 0, + sizeof(nsfw_mgr_sock_info) * NSFW_MGR_FD_MAX); + if (EOK != err_ret) + { + NSFW_LOGERR("memset_s fail]err_ret=%d", err_ret); + goto error; } - (void) MEMSET_S (g_ep_proc.ep_fun, - sizeof (nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX, 0, - sizeof (nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX); } - return 0; -} - -/***************************************************************************** -* Prototype : nsfw_mgr_com_mkdir_domainpath -* Description : check whether the domain path exist.if not exist, create it. -* Input : char *pathname -* Output : None -* Return Value : void -* Calls : -* Called By : -*****************************************************************************/ -void -nsfw_mgr_com_mkdir_domainpath (char *pathname) -{ - char dirname[NSFW_MGRCOM_PATH_LEN] = { 0 }; - int i, len; - if (NULL == pathname) + if (!g_ep_proc.ep_fun) { - NSFW_LOGERR ("the pathname is null."); - return; - } - - strncpy (dirname, pathname, NSFW_MGRCOM_PATH_LEN - 1); - len = strlen (dirname); - if (dirname[len - 1] != '/') - strncat (dirname, "/", 2); - - if (access (dirname, F_OK) == 0) - return; - - len = strlen (dirname); - - for (i = 1; i < len; i++) - { - if (dirname[i] == '/') + g_ep_proc.ep_fun = + (nsfw_mgr_sock_fun *) malloc(sizeof(nsfw_mgr_sock_fun) * + NSFW_MGR_FD_MAX); + if (NULL == g_ep_proc.ep_fun) { - dirname[i] = 0; - if (access (dirname, F_OK) != 0) - { - if (mkdir (dirname, 0755) == -1) - { - NSFW_LOGERR ("mkdir:%s error", dirname); - return; - } - } - dirname[i] = '/'; + NSFW_LOGERR("malloc fail] length=%d ", + sizeof(nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX); + goto error; + } + err_ret = + memset_s(g_ep_proc.ep_fun, + sizeof(nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX, 0, + sizeof(nsfw_mgr_sock_fun) * NSFW_MGR_FD_MAX); + if (EOK != err_ret) + { + NSFW_LOGERR("memset_s fail]err_ret=%d", err_ret); + goto error; } } - - return; + return 0; + error: + nsfw_mgr_comm_fd_destroy(); + return -1; } /***************************************************************************** @@ -1879,129 +1940,155 @@ nsfw_mgr_com_mkdir_domainpath (char *pathname) * Calls : * Called By : *****************************************************************************/ -int nsfw_mgr_com_module_init (void *param); -int -nsfw_mgr_com_module_init (void *param) +/*try to get event form all modules */ +int nsfw_mgr_com_module_init(void *param); +int nsfw_mgr_com_module_init(void *param) { - lint_lock_1 (); - u32 proc_type = (u32) ((long long) param); - nsfw_mgr_init_cfg *mgr_cfg = &g_mgr_com_cfg; - const char *directory = NSFW_DOMAIN_DIR; - const char *home_dir = getenv ("HOME"); + u32 proc_type = (u32) ((long long) param); + nsfw_mgr_init_cfg *mgr_cfg = &g_mgr_com_cfg; + const char *directory = NSFW_DOMAIN_DIR; + const char *home_dir = get_home_path(); - NSFW_LOGINF ("module mgr init] type=%u", proc_type); + NSFW_LOGINF("module mgr init]type=%u", proc_type); - if (getuid () != 0 && home_dir != NULL) - directory = home_dir; + if (home_dir) + { + directory = home_dir; + } - if (0 != nsfw_mgr_comm_fd_init (proc_type)) + if (0 != nsfw_mgr_comm_fd_init(proc_type)) { - NSFW_LOGERR ("fd init fail] proc_type=%u", proc_type); - lint_unlock_1 (); - return -1; + NSFW_LOGERR("fd init fail]proc_type=%u", proc_type); + return -1; } - switch (proc_type) + switch (proc_type) { - case NSFW_PROC_MAIN: - /* modify destMax, remove "-1" */ - if (EOK != - STRCPY_S (mgr_cfg->domain_path, NSFW_MGRCOM_PATH_LEN, directory)) - { - NSFW_LOGERR ("module mgr init STRCPY_S failed!"); - lint_unlock_1 (); - return -1; - } + case NSFW_PROC_MAIN: + /*modify destMax, remove "-1" */ + if (EOK != + strcpy_s(mgr_cfg->domain_path, NSFW_MGRCOM_PATH_LEN, + directory)) + { + NSFW_LOGERR("module mgr init strcpy_s fail"); + return -1; + } - nsfw_mgr_com_mkdir_domainpath (mgr_cfg->domain_path); + /*modify destMax, remove "-1" */ + if (EOK != + strcat_s(mgr_cfg->domain_path, NSFW_MGRCOM_PATH_LEN, + NSFW_MAIN_FILE)) + { + NSFW_LOGERR("module mgr init strcat_s fail"); + return -1; + } - NSFW_LOGINF ("module mgr init]NSFW_PROC_MAIN domain_path=%s", - mgr_cfg->domain_path); + NSFW_LOGINF("module mgr init]NSFW_PROC_MAIN domain_path=%s", + mgr_cfg->domain_path); - if (TRUE != nsfw_mgr_com_start ()) - { - NSFW_LOGERR ("module mgr nsfw_mgr_com_start failed!"); - lint_unlock_1 (); - return -1; - } + if (TRUE != nsfw_mgr_com_start()) + { + NSFW_LOGERR("module mgr nsfw_mgr_com_start fail"); + return -1; + } - break; + break; + case NSFW_PROC_MASTER: + /* modify destMax, remove "-1" */ + if (EOK != + strcpy_s(mgr_cfg->domain_path, NSFW_MGRCOM_PATH_LEN, + directory)) + { + NSFW_LOGERR("module mgr init strcpy_s fail"); + return -1; + } - case NSFW_PROC_TOOLS: - break; - case NSFW_PROC_CTRL: - if (TRUE != nsfw_mgr_com_start_local (proc_type)) - { - NSFW_LOGERR ("module mgr nsfw_mgr_com_start_local failed!"); - lint_unlock_1 (); - return -1; - } - break; - default: - if (proc_type < NSFW_PROC_MAX) - { - break; - } - lint_unlock_1 (); - return -1; + /* modify destMax, remove "-1" */ + if (EOK != + strcat_s(mgr_cfg->domain_path, NSFW_MGRCOM_PATH_LEN, + NSFW_MASTER_FILE)) + { + NSFW_LOGERR("module mgr init strcat_s fail"); + return -1; + } + + NSFW_LOGINF("module mgr init]NSFW_PROC_MASTER domain_path=%s", + mgr_cfg->domain_path); + + if (TRUE != nsfw_mgr_com_start()) + { + NSFW_LOGERR("module mgr nsfw_mgr_com_start fail"); + return -1; + } + + break; + case NSFW_PROC_TOOLS: + case NSFW_PROC_CTRL: + if (TRUE != nsfw_mgr_com_start_local(proc_type)) + { + NSFW_LOGERR("module mgr nsfw_mgr_com_start_local fail"); + return -1; + } + break; + default: + if (proc_type < NSFW_PROC_MAX) + { + break; + } + return -1; } - mgr_cfg->msg_size = MGR_COM_MSG_COUNT_DEF; - mgr_cfg->max_recv_timeout = MGR_COM_RECV_TIMEOUT_DEF; - mgr_cfg->max_recv_drop_msg = MGR_COM_MAX_DROP_MSG_DEF; + mgr_cfg->msg_size = MGR_COM_MSG_COUNT_DEF; + mgr_cfg->max_recv_timeout = MGR_COM_RECV_TIMEOUT_DEF; + mgr_cfg->max_recv_drop_msg = MGR_COM_MAX_DROP_MSG_DEF; - mgr_cfg->proc_type = proc_type; + mgr_cfg->proc_type = proc_type; - nsfw_mem_sppool pmpinfo; - if (EOK != MEMSET_S (&pmpinfo, sizeof (pmpinfo), 0, sizeof (pmpinfo))) + nsfw_mem_sppool pmpinfo; + if (EOK != memset_s(&pmpinfo, sizeof(pmpinfo), 0, sizeof(pmpinfo))) { - NSFW_LOGERR ("Error to memset!!!"); - nsfw_mgr_comm_fd_destroy (); - lint_unlock_1 (); - return -1; + NSFW_LOGERR("memset fail"); + nsfw_mgr_comm_fd_destroy(); + return -1; } - pmpinfo.enmptype = NSFW_MRING_MPMC; - pmpinfo.usnum = mgr_cfg->msg_size; - pmpinfo.useltsize = sizeof (nsfw_mgr_msg); - pmpinfo.isocket_id = NSFW_SOCKET_ANY; - pmpinfo.stname.entype = NSFW_NSHMEM; - if (-1 == - SPRINTF_S (pmpinfo.stname.aname, sizeof (pmpinfo.stname.aname), "%s", - "MS_MGR_MSGPOOL")) + pmpinfo.enmptype = NSFW_MRING_MPMC; + pmpinfo.usnum = mgr_cfg->msg_size; + pmpinfo.useltsize = sizeof(nsfw_mgr_msg); + pmpinfo.isocket_id = NSFW_SOCKET_ANY; + pmpinfo.stname.entype = NSFW_NSHMEM; + if (-1 == + sprintf_s(pmpinfo.stname.aname, sizeof(pmpinfo.stname.aname), "%s", + "MS_MGR_MSGPOOL")) { - NSFW_LOGERR ("Error to SPRINTF_S!!!"); - nsfw_mgr_comm_fd_destroy (); - lint_unlock_1 (); - return -1; + NSFW_LOGERR("sprintf_s fail"); + nsfw_mgr_comm_fd_destroy(); + return -1; } - mgr_cfg->msg_pool = nsfw_mem_sp_create (&pmpinfo); + mgr_cfg->msg_pool = nsfw_mem_sp_create(&pmpinfo); - if (!mgr_cfg->msg_pool) + if (!mgr_cfg->msg_pool) { - NSFW_LOGERR ("module mgr init msg_pool alloc failed!"); - nsfw_mgr_comm_fd_destroy (); - lint_unlock_1 (); - return -1; + NSFW_LOGERR("module mgr init msg_pool alloc fail"); + nsfw_mgr_comm_fd_destroy(); + return -1; } - (void) MEM_STAT (NSFW_MGR_COM_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, - nsfw_mem_get_len (mgr_cfg->msg_pool, NSFW_MEM_SPOOL)); + (void) MEM_STAT(NSFW_MGR_COM_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, + nsfw_mem_get_len(mgr_cfg->msg_pool, NSFW_MEM_SPOOL)); - if ((NSFW_PROC_TOOLS == proc_type) - || (NSFW_PROC_CTRL == proc_type) || (NSFW_PROC_MAIN == proc_type)) + if ((NSFW_PROC_TOOLS == proc_type) + || (NSFW_PROC_CTRL == proc_type) || (NSFW_PROC_MAIN == proc_type)) { - if (TRUE != nsfw_mgr_ep_start ()) + if (TRUE != nsfw_mgr_ep_start()) { - NSFW_LOGERR ("module mgr nsfw_mgr_ep_start failed!"); - nsfw_mgr_comm_fd_destroy (); - lint_unlock_1 (); - return -1; + NSFW_LOGERR("module mgr nsfw_mgr_ep_start fail"); + nsfw_mgr_comm_fd_destroy(); + return -1; } } - lint_unlock_1 (); - return 0; + return 0; } /***************************************************************************** @@ -2012,37 +2099,36 @@ nsfw_mgr_com_module_init (void *param) * Return Value : int * Calls : * Called By : - *****************************************************************************/ -int -nsfw_mgr_run_script (const char *cmd, char *result, int result_buf_len) +*****************************************************************************/ +int nsfw_mgr_run_script(const char *cmd, char *result, int result_buf_len) { - if (!cmd || !result || result_buf_len <= 1) + if (!cmd || !result || result_buf_len <= 1) { - return -1; + return -1; } - FILE *fp = popen (cmd, "r"); - if (fp != NULL) + FILE *fp = popen(cmd, "r"); + if (fp != NULL) { - size_t n = fread (result, sizeof (char), result_buf_len - 1, fp); - if (n == 0) + size_t n = fread(result, sizeof(char), result_buf_len - 1, fp); + if (n == 0) { - result[0] = '\0'; + result[0] = '\0'; } - else if ('\n' == result[n - 1]) + else if ('\n' == result[n - 1]) { - result[n - 1] = '\0'; + result[n - 1] = '\0'; } - else + else { - result[n] = '\0'; + result[n] = '\0'; } - pclose (fp); - return n; + pclose(fp); + return n; } - return -1; + return -1; } /* *INDENT-OFF* */ diff --git a/src/framework/ipc/mgr_com/mgr_com.h b/src/framework/ipc/mgr_com/mgr_com.h index 03f58c8..0011716 100644 --- a/src/framework/ipc/mgr_com/mgr_com.h +++ b/src/framework/ipc/mgr_com/mgr_com.h @@ -14,23 +14,13 @@ * limitations under the License. */ -/***************************************************************************** -* Prototype : ifndef _NSFW_MGRCOM_MODULE_H -* Description : mgr com module definition -* Input : None -* Output : None -* Return Value : # -* Calls : -* Called By : - *****************************************************************************/ #ifndef _NSFW_MGRCOM_MODULE_H #define _NSFW_MGRCOM_MODULE_H #include "pthread.h" #include "nsfw_mem_api.h" -#include "common_mem_api.h" -#include "common_mem_memzone.h" -#include "common_func.h" +#include "dmm_spinlock.h" +#include "dmm_atomic.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -47,100 +37,96 @@ extern "C"{ #define MGR_COM_MSG_COUNT_DEF 1023 /*g_mgr_com_cfg */ #define MGR_COM_RECV_TIMEOUT_DEF 5 #define MGR_COM_MAX_DROP_MSG_DEF 1024 +#define MGR_COM_SEND_TIMEOUT_DEF 5 #define MGR_COM_MSG_COUNT (g_mgr_com_cfg.msg_size) #define MGR_COM_RECV_TIMEOUT (g_mgr_com_cfg.max_recv_timeout) #define MGR_COM_MAX_DROP_MSG (g_mgr_com_cfg.max_recv_drop_msg) -#define NSFW_MAIN_FILE "nStackMainMgr" -#define NSFW_MASTER_FILE "nStackMasterMgr" +#define NSFW_MAIN_FILE "/ip_module/nStackMainMgr" +#define NSFW_MASTER_FILE "/ip_module/nStackMasterMgr" #define NSFW_ALARM_FILE "/HuskyAlarm.domain" -#define NSFW_MGRCOM_THREAD "nStackMgrCom" +#define NSFW_MGRCOM_THREAD "nstk_mgr_com" typedef struct _nsfw_mgr_init_cfg { - u8 proc_type; /*fw_poc_type */ - u8 max_recv_timeout; - u16 max_recv_drop_msg; - u32 msg_size; - common_mem_atomic32_t cur_idx; - u64 u64reserve; - mring_handle msg_pool; - char domain_path[NSFW_MGRCOM_PATH_LEN]; + u8 proc_type; /*fw_poc_type */ + u8 max_recv_timeout; + u16 max_recv_drop_msg; + u32 msg_size; + dmm_atomic_t cur_idx; + u64 u64reserve; + mring_handle msg_pool; + char domain_path[NSFW_MGRCOM_PATH_LEN]; } nsfw_mgr_init_cfg; typedef struct _nsfw_mgrcom_stat { - u64 msg_send[MGR_MSG_MAX]; - u64 msg_recv[MGR_MSG_MAX]; - u64 recv_drop[MGR_MSG_MAX]; - u64 msg_alloc; - u64 msg_free; - u64 msg_send_failed; - u64 reconnect_count; + u64 msg_send[MGR_MSG_MAX]; + u64 msg_recv[MGR_MSG_MAX]; + u64 recv_drop[MGR_MSG_MAX]; + u64 msg_alloc; + u64 msg_free; + u64 msg_send_failed; + u64 reconnect_count; } nsfw_mgrcom_stat; typedef struct _nsfw_mgr_sock_info { - u8 proc_type; /*_ns_poc_type*/ - u32 host_pid; - common_mem_spinlock_t opr_lock; + u8 proc_type; /*_ns_poc_type*/ + u32 host_pid; + dmm_spinlock_t opr_lock; } nsfw_mgr_sock_info; typedef struct _nsfw_mgr_sock_map { - i32 proc_cache[NSFW_PROC_MAX]; - nsfw_mgr_sock_info *sock; + i32 proc_cache[NSFW_PROC_MAX]; + nsfw_mgr_sock_info *sock; } nsfw_mgr_sock_map; #define NSFW_SOCK_MAX_PROC_FUN 4 typedef struct _nsfw_mgrcom_proc_fun { - i32 fd; - nsfw_mgr_sock_fun fun; + i32 fd; + nsfw_mgr_sock_fun fun; } nsfw_mgrcom_proc_fun; typedef struct _nsfw_mgrcom_proc { - i32 epfd; - u32 hbt_count; - pthread_t ep_thread; - nsfw_mgr_sock_fun *ep_fun; + i32 epfd; + u32 hbt_count; + pthread_t ep_thread; + nsfw_mgr_sock_fun *ep_fun; } nsfw_mgrcom_proc; -i32 nsfw_set_sock_block (i32 sock, u8 flag); +i32 nsfw_set_sock_block(i32 sock, u8 flag); -u8 nsfw_rmv_sock_from_ep (i32 fd); -u8 nsfw_add_sock_to_ep (i32 fd); +u8 nsfw_rmv_sock_from_ep(i32 fd); +u8 nsfw_add_sock_to_ep(i32 fd); -int nsfw_mgr_new_msg (i32 epfd, i32 socket, u32 events); +int nsfw_mgr_new_msg(i32 epfd, i32 socket, u32 events); -u8 nsfw_mgr_ep_start (); -u8 nsfw_mgr_stop (); +u8 nsfw_mgr_ep_start(); +u8 nsfw_mgr_stop(); #define LOCK_MGR_FD(_fd){\ - if ((i32)NSFW_MGR_FD_MAX > _fd)\ + if ((i32)NSFW_MGR_FD_MAX > _fd && g_mgr_sockt_map.sock)\ {\ - common_mem_spinlock_lock(&g_mgr_socket_map.sock[_fd].opr_lock);\ + dmm_spin_lock(&g_mgr_sockt_map.sock[_fd].opr_lock);\ }\ } #define UNLOCK_MGR_FD(_fd){\ - if ((i32)NSFW_MGR_FD_MAX > _fd)\ + if ((i32)NSFW_MGR_FD_MAX > _fd && g_mgr_sockt_map.sock)\ {\ - common_mem_spinlock_unlock(&g_mgr_socket_map.sock[_fd].opr_lock);\ + dmm_spin_unlock(&g_mgr_sockt_map.sock[_fd].opr_lock);\ }\ } #define NSFW_MGR_FD_MAX g_mgr_sockfdmax -extern void set_thread_attr (pthread_attr_t * pattr, int stacksize, int pri, - int policy); - -extern void nsfw_com_attr_set (int policy, int pri); - #ifdef __cplusplus /* *INDENT-OFF* */ } diff --git a/src/framework/ipc/ps/nsfw_fd_timer.c b/src/framework/ipc/ps/nsfw_fd_timer.c index cea2486..fc71748 100644 --- a/src/framework/ipc/ps/nsfw_fd_timer.c +++ b/src/framework/ipc/ps/nsfw_fd_timer.c @@ -22,10 +22,8 @@ #include "types.h" #include "list.h" -#include "common_mem_common.h" - #include "nstack_securec.h" -#include "nsfw_init.h" +#include "nsfw_init_api.h" #include "nsfw_mgr_com_api.h" #include "nsfw_mem_api.h" #include "nstack_log.h" @@ -42,11 +40,10 @@ extern "C"{ #define NSFW_TIMER_CYCLE 1 #define NSFW_TIMER_INFO_MAX_COUNT_DEF 8191 #define NSFW_TIMER_INFO_MAX_COUNT (g_timer_cfg.timer_info_size) -/* *INDENT-OFF* */ + nsfw_timer_init_cfg g_timer_cfg; u8 g_hbt_switch = FALSE; -/* *INDENT-ON* */ /***************************************************************************** * Prototype : nsfw_timer_reg_timer @@ -59,38 +56,39 @@ u8 g_hbt_switch = FALSE; * Return Value : nsfw_timer_info* * Calls : * Called By : -* *****************************************************************************/ -nsfw_timer_info * -nsfw_timer_reg_timer (u32 timer_type, void *data, - nsfw_timer_proc_fun fun, struct timespec time_left) +nsfw_timer_info *nsfw_timer_reg_timer(u32 timer_type, void *data, + nsfw_timer_proc_fun fun, + struct timespec time_left) { - nsfw_timer_info *tm_info = NULL; - if (0 == - nsfw_mem_ring_dequeue (g_timer_cfg.timer_info_pool, (void *) &tm_info)) + nsfw_timer_info *tm_info = NULL; + if (0 == + nsfw_mem_ring_dequeue(g_timer_cfg.timer_info_pool, (void *) &tm_info)) { - NSFW_LOGERR ("dequeue error]data=%p,fun=%p", data, fun); - return NULL; + NSFW_LOGERR("dequeue error]data=%p,fun=%p", data, fun); + return NULL; } - if (EOK != MEMSET_S (tm_info, sizeof (*tm_info), 0, sizeof (*tm_info))) + if (EOK != memset_s(tm_info, sizeof(*tm_info), 0, sizeof(*tm_info))) { - if (0 == nsfw_mem_ring_enqueue (g_timer_cfg.timer_info_pool, tm_info)) + if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info)) { - NSFW_LOGERR ("enqueue error]data=%p,fun=%p", data, fun); + NSFW_LOGERR("enqueue error]data=%p,fun=%p", data, fun); } - NSFW_LOGERR ("mem set error]data=%p,fun=%p", data, fun); - return NULL; + NSFW_LOGERR("mem set error]data=%p,fun=%p", data, fun); + return NULL; } - tm_info->fun = fun; - tm_info->argv = data; - tm_info->time_left = time_left; - //tm_info->time_left.tv_sec += NSFW_TIMER_CYCLE; - tm_info->timer_type = timer_type; - list_add_tail (&tm_info->node, &g_timer_cfg.timer_head); - tm_info->alloc_flag = TRUE; - return tm_info; + tm_info->fun = fun; + tm_info->argv = data; + tm_info->time_left = time_left; + tm_info->timer_type = timer_type; + tm_info->alloc_flag = TRUE; + /* it can start timer after only finish all timer reg, or else it have multi-thread issue */ + dmm_spin_lock(&g_timer_cfg.timer_lock); + list_add_tail(&tm_info->node, &g_timer_cfg.timer_head); + dmm_spin_unlock(&g_timer_cfg.timer_lock); + return tm_info; } /***************************************************************************** @@ -101,32 +99,34 @@ nsfw_timer_reg_timer (u32 timer_type, void *data, * Return Value : void * Calls : * Called By : -* *****************************************************************************/ -void -nsfw_timer_rmv_timer (nsfw_timer_info * tm_info) +void nsfw_timer_rmv_timer(nsfw_timer_info * tm_info) { - if (NULL == tm_info) + if (NULL == tm_info) { - NSFW_LOGWAR ("tm_info nul"); - return; + NSFW_LOGWAR("tm_info nul"); + return; } + /* it can start timer after only finish all timer reg, or else it have multi-thread issue */ + dmm_spin_lock(&g_timer_cfg.timer_lock); - if (FALSE == tm_info->alloc_flag) + if (FALSE == tm_info->alloc_flag) { - NSFW_LOGERR ("tm_info refree]tm_info=%p,argv=%p,fun=%p", tm_info, - tm_info->argv, tm_info->fun); - return; + dmm_spin_unlock(&g_timer_cfg.timer_lock); + NSFW_LOGERR("tm_info refree]tm_info=%p,argv=%p,fun=%p", tm_info, + tm_info->argv, tm_info->fun); + return; } - tm_info->alloc_flag = FALSE; - list_del (&tm_info->node); - if (0 == nsfw_mem_ring_enqueue (g_timer_cfg.timer_info_pool, tm_info)) + tm_info->alloc_flag = FALSE; + list_del(&tm_info->node); + if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info)) { - NSFW_LOGERR ("tm_info free failed]tm_info=%p,argv=%p,fun=%p", tm_info, - tm_info->argv, tm_info->fun); + NSFW_LOGERR("tm_info free failed]tm_info=%p,argv=%p,fun=%p", tm_info, + tm_info->argv, tm_info->fun); } - return; + dmm_spin_unlock(&g_timer_cfg.timer_lock); + return; } /***************************************************************************** @@ -137,41 +137,59 @@ nsfw_timer_rmv_timer (nsfw_timer_info * tm_info) * Return Value : u8 * Calls : * Called By : -* *****************************************************************************/ -void -nsfw_timer_exp (u64 count) +void nsfw_timer_exp(u64 count) { - nsfw_timer_info *tm_info = NULL; - struct list_head *tNode; - struct list_head *tPretNode; - - LINT_LIST ()list_for_each_entry (tm_info, tNode, (&g_timer_cfg.timer_head), - node) - { - tPretNode = tm_info->node.prev; - if (tm_info->time_left.tv_sec > (long) count * NSFW_TIMER_CYCLE) - { - tm_info->time_left.tv_sec -= count * NSFW_TIMER_CYCLE; - continue; - } - - list_del (&tm_info->node); - list_add_tail (&tm_info->node, &g_timer_cfg.exp_timer_head); - tNode = tPretNode; - } - - u32 i = 0; - while (!list_empty (&g_timer_cfg.exp_timer_head) - && i++ < NSFW_TIMER_INFO_MAX_COUNT) + nsfw_timer_info *tm_info = NULL; + struct list_head *tNode; + struct list_head *tPretNode; + + /* it can start timer after only finish all timer reg, or else it have multi-thread issue */ + dmm_spin_lock(&g_timer_cfg.timer_lock); + + list_for_each_entry(tm_info, tNode, (&g_timer_cfg.timer_head), node) + { + + tPretNode = tm_info->node.prev; + if (tm_info->time_left.tv_sec > (long) count * NSFW_TIMER_CYCLE) + { + tm_info->time_left.tv_sec -= count * NSFW_TIMER_CYCLE; + continue; + } + + list_del(&tm_info->node); + list_add_tail(&tm_info->node, &g_timer_cfg.exp_timer_head); + tNode = tPretNode; + } + dmm_spin_unlock(&g_timer_cfg.timer_lock); + + u32 i = 0; + while (!list_empty(&g_timer_cfg.exp_timer_head) + && i++ < NSFW_TIMER_INFO_MAX_COUNT) { - tm_info = - (nsfw_timer_info *) list_get_first (&g_timer_cfg.exp_timer_head); - if (NULL != tm_info->fun) + dmm_spin_lock(&g_timer_cfg.timer_lock); + tm_info = + (nsfw_timer_info *) list_get_first(&g_timer_cfg.exp_timer_head); + if (NULL == tm_info) + { + dmm_spin_unlock(&g_timer_cfg.timer_lock); + continue; + } + tm_info->alloc_flag = FALSE; + list_del(&tm_info->node); + dmm_spin_unlock(&g_timer_cfg.timer_lock); + /* here can't lock that it is possible that the tm_info->fun call reg timer or remove timer, which will cause deadlock */ + if (NULL != tm_info->fun) + { + (void) tm_info->fun(tm_info->timer_type, tm_info->argv); + } + /* only after finish handle tm_info->fun, it can put it into timer_info_pool, avoid that it is applied by other timer */ + if (0 == nsfw_mem_ring_enqueue(g_timer_cfg.timer_info_pool, tm_info)) { - (void) tm_info->fun (tm_info->timer_type, tm_info->argv); + NSFW_LOGERR("tm_info free failed]tm_info=%p,argv=%p,fun=%p", + tm_info, tm_info->argv, tm_info->fun); } - nsfw_timer_rmv_timer (tm_info); + } } @@ -185,38 +203,32 @@ nsfw_timer_exp (u64 count) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_get_timer_socket () +i32 nsfw_get_timer_socket() { - i32 tfd = timerfd_create (CLOCK_MONOTONIC, TFD_NONBLOCK); - if (tfd == -1) + + i32 tfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK); + if (tfd == -1) { - NSFW_LOGERR ("timerfd_create failed!]errno=%d\n", errno); - return -1; + NSFW_LOGERR("timerfd_create failed!]errno=%d\n", errno); + return -1; } - /* close on exe */ - if (-1 == nsfw_set_close_on_exec (tfd)) + /* close on exec */ + if (-1 == nsfw_set_close_on_exec(tfd)) { - (void) nsfw_base_close (tfd); - NSFW_LOGERR ("set exec err]fd=%d, errno=%d", tfd, errno); - return -1; + (void) nsfw_base_close(tfd); + NSFW_LOGERR("set exec err]fd=%d, errno=%d", tfd, errno); + return -1; } - struct itimerspec ts; - ts.it_interval.tv_sec = NSFW_TIMER_CYCLE; - ts.it_interval.tv_nsec = 0; - ts.it_value.tv_sec = 0; - ts.it_value.tv_nsec = NSFW_TIMER_CYCLE; - - if (timerfd_settime (tfd, 0, &ts, NULL) < 0) + if (timerfd_settime(tfd, 0, &g_timer_cfg.ts, NULL) < 0) { - NSFW_LOGERR ("timerfd_settime failed] errno=%d", errno); - close (tfd); - return -1; + NSFW_LOGERR("timerfd_settime failed] errno=%d", errno); + close(tfd); + return -1; } - return tfd; + return tfd; } /***************************************************************************** @@ -230,53 +242,52 @@ nsfw_get_timer_socket () * Calls : * Called By : *****************************************************************************/ -int -nsfw_timer_notify_fun (i32 epfd, i32 fd, u32 events) +int nsfw_timer_notify_fun(i32 epfd, i32 fd, u32 events) { - i32 rc; + i32 rc; - if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) + if ((events & EPOLLERR) || (events & EPOLLHUP) || (!(events & EPOLLIN))) { - (void) nsfw_base_close (fd); - NSFW_LOGWAR ("timer disconnect!]epfd=%d,timer=%d,event=0x%x", epfd, - fd, events); + (void) nsfw_base_close(fd); + NSFW_LOGWAR("timer disconnect!]epfd=%d,timer=%d,event=0x%x", epfd, + fd, events); - (void) nsfw_mgr_unreg_sock_fun (fd); - i32 timer_fd = nsfw_get_timer_socket (); - if (timer_fd < 0) + (void) nsfw_mgr_unreg_sock_fun(fd); + i32 timer_fd = nsfw_get_timer_socket(); + if (timer_fd < 0) { - NSFW_LOGERR ("get timer_fd failed!]epfd=%d,timer_fd=%d,event=0x%x", - epfd, fd, events); - return FALSE; + NSFW_LOGERR("get timer_fd faied!]epfd=%d,timer_fd=%d,event=0x%x", + epfd, fd, events); + return FALSE; } - (void) nsfw_mgr_reg_sock_fun (timer_fd, nsfw_timer_notify_fun); - return TRUE; + (void) nsfw_mgr_reg_sock_fun(timer_fd, nsfw_timer_notify_fun); + return TRUE; } - u64 data; - while (1) + u64 data; + while (1) { - rc = nsfw_base_read (fd, &data, sizeof (data)); - if (rc == 0) + rc = nsfw_base_read(fd, &data, sizeof(data)); + if (rc == 0) { - NSFW_LOGERR ("timer_fd recv 0]timer_fd=%d,errno=%d", fd, errno); - break; + NSFW_LOGERR("timer_fd recv 0]timer_fd=%d,errno=%d", fd, errno); + break; } - else if (rc == -1) + else if (rc == -1) { - if (errno == EINTR || errno == EAGAIN) + if (errno == EINTR || errno == EAGAIN) { - break; + break; } - NSMON_LOGERR ("timer_fd recv]timer_fd=%d,errno=%d", fd, errno); - break; + NSMON_LOGERR("timer_fd recv]timer_fd=%d,errno=%d", fd, errno); + break; } - nsfw_timer_exp (data); + nsfw_timer_exp(data); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -287,21 +298,19 @@ nsfw_timer_notify_fun (i32 epfd, i32 fd, u32 events) * Return Value : u8 * Calls : * Called By : -* *****************************************************************************/ -u8 -nsfw_timer_start () +u8 nsfw_timer_start() { - i32 timer_fd = nsfw_get_timer_socket (); - if (timer_fd < 0) + i32 timer_fd = nsfw_get_timer_socket(); + if (timer_fd < 0) { - NSFW_LOGERR ("get timer_fd failed!"); - return FALSE; + NSFW_LOGERR("get timer_fd fail"); + return FALSE; } - NSFW_LOGINF ("start timer_fd module!]timer_fd=%d", timer_fd); - (void) nsfw_mgr_reg_sock_fun (timer_fd, nsfw_timer_notify_fun); - return TRUE; + NSFW_LOGINF("start timer_fd module]timer_fd=%d", timer_fd); + (void) nsfw_mgr_reg_sock_fun(timer_fd, nsfw_timer_notify_fun); + return TRUE; } /***************************************************************************** @@ -312,64 +321,79 @@ nsfw_timer_start () * Return Value : int * Calls : * Called By : -* *****************************************************************************/ -int -nsfw_timer_module_init (void *param) +int nsfw_timer_module_init(void *param) { - u32 proc_type = (u32) ((long long) param); - nsfw_timer_init_cfg *timer_cfg = &g_timer_cfg; - NSFW_LOGINF ("ps module init]type=%u", proc_type); - switch (proc_type) + u32 proc_type = (u32) ((long long) param); + nsfw_timer_init_cfg *timer_cfg = &g_timer_cfg; + if (proc_type != NSFW_PROC_CTRL) { - case NSFW_PROC_MAIN: - (void) NSFW_REG_SOFT_INT (NSFW_DBG_MODE_PARAM, g_hbt_switch, 0, 1); - break; - case NSFW_PROC_TOOLS: - case NSFW_PROC_CTRL: - break; - default: - return 0; + NSFW_LOGINF("ps module init]type=%u", proc_type); } - timer_cfg->timer_info_size = NSFW_TIMER_INFO_MAX_COUNT_DEF; - - nsfw_mem_sppool pmpinfo; - pmpinfo.enmptype = NSFW_MRING_MPMC; - pmpinfo.usnum = timer_cfg->timer_info_size; - pmpinfo.useltsize = sizeof (nsfw_timer_info); - pmpinfo.isocket_id = NSFW_SOCKET_ANY; - pmpinfo.stname.entype = NSFW_NSHMEM; - if (-1 == - SPRINTF_S (pmpinfo.stname.aname, NSFW_MEM_NAME_LENGTH, "%s", - "MS_TM_INFOPOOL")) + /* The first expiration should be the same as the cyclic interval. This fixs the bug of skipping one beat */ + timer_cfg->ts.it_interval.tv_sec = NSFW_TIMER_CYCLE; + timer_cfg->ts.it_interval.tv_nsec = 0; + timer_cfg->ts.it_value.tv_sec = timer_cfg->ts.it_interval.tv_sec; + timer_cfg->ts.it_value.tv_nsec = timer_cfg->ts.it_interval.tv_nsec; + + switch (proc_type) { - NSFW_LOGERR ("SPRINTF_S failed"); - return -1; + case NSFW_PROC_MASTER: + /** For nMaster, we call nsfw_mgr_ep_start() after every other module is inited, + ** so the first expiration is set to 1 nanosecond for nMaster to bring up nMain ASAP */ + timer_cfg->ts.it_value.tv_sec = 0; + timer_cfg->ts.it_value.tv_nsec = 1; + case NSFW_PROC_MAIN: + (void) NSFW_REG_SOFT_INT(NSFW_DBG_MODE_PARAM, g_hbt_switch, 0, 1); + break; + case NSFW_PROC_TOOLS: + case NSFW_PROC_CTRL: + break; + default: + return 0; } - timer_cfg->timer_info_pool = nsfw_mem_sp_create (&pmpinfo); - if (!timer_cfg->timer_info_pool) + timer_cfg->timer_info_size = NSFW_TIMER_INFO_MAX_COUNT_DEF; + + nsfw_mem_sppool pmpinfo; + pmpinfo.enmptype = NSFW_MRING_MPMC; + pmpinfo.usnum = timer_cfg->timer_info_size; + pmpinfo.useltsize = sizeof(nsfw_timer_info); + pmpinfo.isocket_id = NSFW_SOCKET_ANY; + pmpinfo.stname.entype = NSFW_NSHMEM; + if (-1 == + sprintf_s(pmpinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s", + "MS_TM_INFOPOOL")) { - NSFW_LOGERR ("alloc timer info pool_err"); - return -1; + NSFW_LOGERR("sprintf_s fail"); + return -1; } + timer_cfg->timer_info_pool = nsfw_mem_sp_create(&pmpinfo); + + if (!timer_cfg->timer_info_pool) + { + NSFW_LOGERR("alloc timer info pool_err"); + return -1; + } + + MEM_STAT(NSFW_TIMER_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, + nsfw_mem_get_len(timer_cfg->timer_info_pool, NSFW_MEM_SPOOL)); - MEM_STAT (NSFW_TIMER_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, - nsfw_mem_get_len (timer_cfg->timer_info_pool, NSFW_MEM_SPOOL)); + INIT_LIST_HEAD(&(timer_cfg->timer_head)); + INIT_LIST_HEAD(&(timer_cfg->exp_timer_head)); - INIT_LIST_HEAD (&(timer_cfg->timer_head)); - INIT_LIST_HEAD (&(timer_cfg->exp_timer_head)); + dmm_spin_init(&g_timer_cfg.timer_lock); + (void) nsfw_timer_start(); - (void) nsfw_timer_start (); - return 0; + return 0; } /* *INDENT-OFF* */ -NSFW_MODULE_NAME (NSFW_TIMER_MODULE) -NSFW_MODULE_PRIORITY (10) -NSFW_MODULE_DEPENDS (NSFW_MGR_COM_MODULE) -NSFW_MODULE_INIT (nsfw_timer_module_init) +NSFW_MODULE_NAME(NSFW_TIMER_MODULE) +NSFW_MODULE_PRIORITY(10) +NSFW_MODULE_DEPENDS(NSFW_MGR_COM_MODULE) +NSFW_MODULE_INIT(nsfw_timer_module_init) /* *INDENT-ON* */ #ifdef __cplusplus diff --git a/src/framework/ipc/ps/nsfw_ps_mem_module.c b/src/framework/ipc/ps/nsfw_ps_mem_module.c index 55af158..57662f9 100644 --- a/src/framework/ipc/ps/nsfw_ps_mem_module.c +++ b/src/framework/ipc/ps/nsfw_ps_mem_module.c @@ -17,7 +17,7 @@ #include <stdlib.h> #include "types.h" #include "nstack_securec.h" -#include "nsfw_init.h" +#include "nsfw_init_api.h" #include "nsfw_ps_module.h" #include "nsfw_mgr_com_api.h" @@ -37,31 +37,30 @@ extern "C"{ ns_mem_mng_init_cfg g_mem_cfg; -int mem_ps_exiting (void *pps_info, void *argv); +int mem_ps_exiting(void *pps_info, void *argv); -int -nsfw_mem_ps_exit_resend_timeout (u32 timer_type, void *data) +int nsfw_mem_ps_exit_resend_timeout(u32 timer_type, void *data) { - nsfw_ps_info *ps_info = data; - if (NULL == ps_info) + nsfw_ps_info *ps_info = data; + if (NULL == ps_info) { - NSFW_LOGERR ("ps_info nul!"); - return FALSE; + NSFW_LOGERR("ps_info nul!"); + return FALSE; } - if (NSFW_PROC_APP != ps_info->proc_type) + if (NSFW_PROC_APP != ps_info->proc_type) { - return FALSE; + return FALSE; } - if (NSFW_PS_EXITING != ps_info->state) + if (NSFW_PS_EXITING != ps_info->state) { - return FALSE; + return FALSE; } - ps_info->resend_timer_ptr = NULL; - (void) mem_ps_exiting (ps_info, NULL); - return TRUE; + ps_info->resend_timer_ptr = NULL; + (void) mem_ps_exiting(ps_info, NULL); + return TRUE; } /***************************************************************************** @@ -74,143 +73,128 @@ nsfw_mem_ps_exit_resend_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -int -mem_ps_exiting (void *pps_info, void *argv) +int mem_ps_exiting(void *pps_info, void *argv) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("ps_info nul!"); - return FALSE; + NSFW_LOGERR("ps_info nul!"); + return FALSE; } - if (TRUE == NSFW_SRV_STATE_SUSPEND) + if (TRUE == NSFW_SRV_STATE_SUSPEND) { - NSFW_LOGERR ("main suspend]ps_info=%d,pid=%u", pps_info, - ((nsfw_ps_info *) pps_info)->host_pid); - return FALSE; + NSFW_LOGERR("main suspend]ps_info=%p,pid=%u", pps_info, + ((nsfw_ps_info *) pps_info)->host_pid); + return FALSE; } - nsfw_mgr_msg *msg = - nsfw_mgr_msg_alloc (MGR_MSG_APP_EXIT_REQ, NSFW_PROC_MAIN); - if (NULL == msg) + nsfw_mgr_msg *msg = + nsfw_mgr_msg_alloc(MGR_MSG_APP_EXIT_REQ, NSFW_PROC_MAIN); + if (NULL == msg) { - NSFW_LOGERR ("ps_exit alloc msg failed]ps_info=%p,pid=%u", pps_info, - ((nsfw_ps_info *) pps_info)->host_pid); - return FALSE; + NSFW_LOGERR("ps_exit alloc msg failed]ps_info=%p,pid=%u", pps_info, + ((nsfw_ps_info *) pps_info)->host_pid); + return FALSE; } - nsfw_ps_info_msg *ps_msg = GET_USER_MSG (nsfw_ps_info_msg, msg); - ps_msg->host_pid = ((nsfw_ps_info *) pps_info)->host_pid; + nsfw_ps_info_msg *ps_msg = GET_USER_MSG(nsfw_ps_info_msg, msg); + ps_msg->host_pid = ((nsfw_ps_info *) pps_info)->host_pid; - (void) nsfw_mgr_send_msg (msg); - NSFW_LOGINF ("ps_exiting send msg]ps_info=%p,pid=%u", pps_info, - ps_msg->host_pid); - nsfw_mgr_msg_free (msg); - return TRUE; + (void) nsfw_mgr_send_msg(msg); + NSFW_LOGINF("ps_exiting send msg]ps_info=%p,pid=%u", pps_info, + ps_msg->host_pid); + nsfw_mgr_msg_free(msg); + + return TRUE; } -/***************************************************************************** -* Prototype : mem_alloc_ps_info -* Description : alloc ps_info -* Input : u32 pid -* u8 proc_type -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -mem_alloc_ps_info (u32 pid, u8 proc_type) +u8 mem_alloc_ps_info(u32 pid, u8 proc_type) { - nsfw_ps_info *pps_info = NULL; - pps_info = nsfw_ps_info_get (pid); - if (NULL != pps_info) + nsfw_ps_info *pps_info = NULL; + pps_info = nsfw_ps_info_get(pid); + if (NULL != pps_info) { - return TRUE; + return TRUE; } - pps_info = nsfw_ps_info_alloc (pid, proc_type); - if (NULL == pps_info) + pps_info = nsfw_ps_info_alloc(pid, proc_type, 0); + if (NULL == pps_info) { - NSFW_LOGERR ("alloc ps_info failed!]pid=%u,proc_type=%u", pid, - proc_type); - return FALSE; + NSFW_LOGERR("alloc ps_info falied!]pid=%u,proc_type=%u", pid, + proc_type); + return FALSE; } - NSFW_LOGINF ("alloc new ps_info]pps_info=%p,pid=%u", pps_info, pid); - return TRUE; + NSFW_LOGINF("alloc new ps_info]pps_info=%p,pid=%u", pps_info, pid); + return TRUE; } /*mem alloc by msg begin*/ -void * -mem_item_zone_create (void *mem_info) +void *mem_item_zone_create(void *mem_info) { - return (void *) nsfw_mem_zone_create ((nsfw_mem_zone *) mem_info); + return (void *) nsfw_mem_zone_create((nsfw_mem_zone *) mem_info); } -void * -mem_item_mbfmp_create (void *mem_info) +void *mem_item_mbfmp_create(void *mem_info) { - return (void *) nsfw_mem_mbfmp_create ((nsfw_mem_mbfpool *) mem_info); + return (void *) nsfw_mem_mbfmp_create((nsfw_mem_mbfpool *) mem_info); } -void * -mem_item_sp_create (void *mem_info) +void *mem_item_sp_create(void *mem_info) { - return (void *) nsfw_mem_sp_create ((nsfw_mem_sppool *) mem_info); + return (void *) nsfw_mem_sp_create((nsfw_mem_sppool *) mem_info); } -void * -mem_item_ring_create (void *mem_info) +void *mem_item_ring_create(void *mem_info) { - return (void *) nsfw_mem_ring_create ((nsfw_mem_mring *) mem_info); + return (void *) nsfw_mem_ring_create((nsfw_mem_mring *) mem_info); } nsfw_ps_mem_item_cfg g_ps_mem_map[] = { - { - NSFW_RESERV_REQ_MSG, - sizeof (nsfw_shmem_reserv_req), - NSFW_MEM_MZONE, - mem_item_zone_create, - mem_item_get_callargv} - , - - { - NSFW_MBUF_REQ_MSG, - sizeof (nsfw_shmem_mbuf_req), - NSFW_MEM_MBUF, - mem_item_mbfmp_create, - mem_item_get_callargv} - , - { - NSFW_SPPOOL_REQ_MSG, - sizeof (nsfw_shmem_sppool_req), - NSFW_MEM_SPOOL, - mem_item_sp_create, - mem_item_get_callargv} - , - { - NSFW_RING_REQ_MSG, - sizeof (nsfw_shmem_ring_req), - NSFW_MEM_RING, - mem_item_ring_create, - mem_item_get_callargv} - , - { - NSFW_RELEASE_REQ_MSG, - sizeof (nsfw_shmem_free_req), - 0xFFFF, - mem_item_free, - mem_item_get_callargv, - } - , - { - NSFW_MEM_LOOKUP_REQ_MSG, - sizeof (nsfw_shmem_lookup_req), - 0xFFFF, - mem_item_lookup, - mem_item_get_callargv, - } + { + NSFW_RESERV_REQ_MSG, + sizeof(nsfw_shmem_reserv_req), + NSFW_MEM_MZONE, + mem_item_zone_create, + mem_item_get_callargv} + , + + { + NSFW_MBUF_REQ_MSG, + sizeof(nsfw_shmem_mbuf_req), + NSFW_MEM_MBUF, + mem_item_mbfmp_create, + mem_item_get_callargv} + , + { + NSFW_SPPOOL_REQ_MSG, + sizeof(nsfw_shmem_sppool_req), + NSFW_MEM_SPOOL, + mem_item_sp_create, + mem_item_get_callargv} + , + { + NSFW_RING_REQ_MSG, + sizeof(nsfw_shmem_ring_req), + NSFW_MEM_RING, + mem_item_ring_create, + mem_item_get_callargv} + , + { + NSFW_RELEASE_REQ_MSG, + sizeof(nsfw_shmem_free_req), + 0xFFFF, + mem_item_free, + mem_item_get_callargv, + } + , + { + NSFW_MEM_LOOKUP_REQ_MSG, + sizeof(nsfw_shmem_lookup_req), + 0xFFFF, + mem_item_lookup, + mem_item_get_callargv, + } }; /***************************************************************************** @@ -222,25 +206,25 @@ nsfw_ps_mem_item_cfg g_ps_mem_map[] = { * Calls : * Called By : *****************************************************************************/ -nsfw_ps_mem_item_cfg * -mem_item_get_cfg_from_msg (u16 msg_type) +nsfw_ps_mem_item_cfg *mem_item_get_cfg_from_msg(u16 msg_type) { - int idx; - int map_count = sizeof (g_ps_mem_map) / sizeof (nsfw_ps_mem_item_cfg); - for (idx = 0; idx < map_count; idx++) + int idx; + int map_count = sizeof(g_ps_mem_map) / sizeof(nsfw_ps_mem_item_cfg); + for (idx = 0; idx < map_count; idx++) { - if (g_ps_mem_map[idx].usmsg_type == msg_type) + if (g_ps_mem_map[idx].usmsg_type == msg_type) { - return &g_ps_mem_map[idx]; + + return &g_ps_mem_map[idx]; } } - return NULL; + return NULL; } /***************************************************************************** * Prototype : mem_item_get_callargv -* Description : change the message value to structure value +* Description : change the message value to structur value * Input : u16 msg_type * char* msg_body * char *memstr_buf @@ -250,163 +234,143 @@ mem_item_get_cfg_from_msg (u16 msg_type) * Calls : * Called By : *****************************************************************************/ -u8 -mem_item_get_callargv (u16 msg_type, char *msg_body, char *memstr_buf, - i32 buf_len) +u8 mem_item_get_callargv(u16 msg_type, char *msg_body, char *memstr_buf, + i32 buf_len) { - switch (msg_type) - { - case NSFW_RESERV_REQ_MSG: - MEM_GET_CALLARGV (length, length, nsfw_mem_zone, nsfw_shmem_reserv_req, - memstr_buf, msg_body); - MEM_GET_CALLARGV (isocket_id, isocket_id, nsfw_mem_zone, - nsfw_shmem_reserv_req, memstr_buf, msg_body); - break; - case NSFW_MBUF_REQ_MSG: - MEM_GET_CALLARGV (usnum, usnum, nsfw_mem_mbfpool, nsfw_shmem_mbuf_req, - memstr_buf, msg_body); - MEM_GET_CALLARGV (uscash_size, uscash_size, nsfw_mem_mbfpool, - nsfw_shmem_mbuf_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (uspriv_size, uspriv_size, nsfw_mem_mbfpool, - nsfw_shmem_mbuf_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (usdata_room, usdata_room, nsfw_mem_mbfpool, - nsfw_shmem_mbuf_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (enmptype, enmptype, nsfw_mem_mbfpool, - nsfw_shmem_mbuf_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (isocket_id, isocket_id, nsfw_mem_mbfpool, - nsfw_shmem_mbuf_req, memstr_buf, msg_body); - break; - case NSFW_SPPOOL_REQ_MSG: - MEM_GET_CALLARGV (usnum, usnum, nsfw_mem_sppool, - nsfw_shmem_sppool_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (useltsize, useltsize, nsfw_mem_sppool, - nsfw_shmem_sppool_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (enmptype, enmptype, nsfw_mem_sppool, - nsfw_shmem_sppool_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (isocket_id, isocket_id, nsfw_mem_sppool, - nsfw_shmem_sppool_req, memstr_buf, msg_body); - break; - case NSFW_RING_REQ_MSG: - MEM_GET_CALLARGV (usnum, usnum, nsfw_mem_mring, nsfw_shmem_ring_req, - memstr_buf, msg_body); - MEM_GET_CALLARGV (enmptype, enmptype, nsfw_mem_mring, - nsfw_shmem_ring_req, memstr_buf, msg_body); - MEM_GET_CALLARGV (isocket_id, isocket_id, nsfw_mem_mring, - nsfw_shmem_ring_req, memstr_buf, msg_body); - break; - case NSFW_RELEASE_REQ_MSG: - MEM_GET_CALLARGV (ustype, ustype, nsfw_mem_type_info, - nsfw_shmem_free_req, memstr_buf, msg_body); - break; - case NSFW_MEM_LOOKUP_REQ_MSG: - MEM_GET_CALLARGV (ustype, ustype, nsfw_mem_type_info, - nsfw_shmem_lookup_req, memstr_buf, msg_body); - break; - default: - NSFW_LOGERR ("error msg]type=%u", msg_type); - return FALSE; - } - if (EOK != - STRCPY_S (((nsfw_mem_zone *) memstr_buf)->stname.aname, - NSFW_MEM_NAME_LENGTH, - ((nsfw_shmem_reserv_req *) msg_body)->aname)) - { - NSFW_LOGERR ("STRCPY_S failed]msg_type=%u", msg_type); - return FALSE; - } - - ((nsfw_mem_zone *) memstr_buf)->stname.entype = NSFW_SHMEM; - - return TRUE; + switch (msg_type) + { + case NSFW_RESERV_REQ_MSG: + MEM_GET_CALLARGV(lenth, lenth, nsfw_mem_zone, + nsfw_shmem_reserv_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(isocket_id, isocket_id, nsfw_mem_zone, + nsfw_shmem_reserv_req, memstr_buf, msg_body); + break; + case NSFW_MBUF_REQ_MSG: + MEM_GET_CALLARGV(usnum, usnum, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(uscash_size, uscash_size, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(uspriv_size, uspriv_size, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(usdata_room, usdata_room, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(enmptype, enmptype, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(isocket_id, isocket_id, nsfw_mem_mbfpool, + nsfw_shmem_mbuf_req, memstr_buf, msg_body); + break; + case NSFW_SPPOOL_REQ_MSG: + MEM_GET_CALLARGV(usnum, usnum, nsfw_mem_sppool, + nsfw_shmem_sppool_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(useltsize, useltsize, nsfw_mem_sppool, + nsfw_shmem_sppool_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(enmptype, enmptype, nsfw_mem_sppool, + nsfw_shmem_sppool_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(isocket_id, isocket_id, nsfw_mem_sppool, + nsfw_shmem_sppool_req, memstr_buf, msg_body); + break; + case NSFW_RING_REQ_MSG: + MEM_GET_CALLARGV(usnum, usnum, nsfw_mem_mring, + nsfw_shmem_ring_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(enmptype, enmptype, nsfw_mem_mring, + nsfw_shmem_ring_req, memstr_buf, msg_body); + MEM_GET_CALLARGV(isocket_id, isocket_id, nsfw_mem_mring, + nsfw_shmem_ring_req, memstr_buf, msg_body); + break; + case NSFW_RELEASE_REQ_MSG: + MEM_GET_CALLARGV(ustype, ustype, nsfw_mem_type_info, + nsfw_shmem_free_req, memstr_buf, msg_body); + break; + case NSFW_MEM_LOOKUP_REQ_MSG: + MEM_GET_CALLARGV(ustype, ustype, nsfw_mem_type_info, + nsfw_shmem_lookup_req, memstr_buf, msg_body); + break; + default: + NSFW_LOGERR("error msg]type=%u", msg_type); + return FALSE; + } + /*copy string should use strcpy_s */ + if (EOK != + strcpy_s(((nsfw_mem_zone *) memstr_buf)->stname.aname, + NSFW_MEM_NAME_LENTH, + ((nsfw_shmem_reserv_req *) msg_body)->aname)) + { + NSFW_LOGERR("strcpy_s failed]msg_type=%u", msg_type); + return FALSE; + } + + ((nsfw_mem_zone *) memstr_buf)->stname.entype = NSFW_SHMEM; + + return TRUE; } -/***************************************************************************** -* Prototype : mem_item_free -* Description : free memory item -* Input : void *pdata -* Output : None -* Return Value : void* -* Calls : -* Called By : -*****************************************************************************/ -void * -mem_item_free (void *pdata) +void *mem_item_free(void *pdata) { - nsfw_mem_type_info *mem_free = (nsfw_mem_type_info *) pdata; - i32 ret; - NSFW_LOGINF ("free mem]type=%u,name=%s", mem_free->ustype, - mem_free->stname.aname); - switch (mem_free->ustype) - { - case NSFW_MEM_MZONE: - ret = nsfw_mem_zone_release (&mem_free->stname); - break; - case NSFW_MEM_MBUF: - ret = nsfw_mem_mbfmp_release (&mem_free->stname); - break; - case NSFW_MEM_SPOOL: - ret = nsfw_mem_sp_release (&mem_free->stname); - break; - case NSFW_MEM_RING: - ret = nsfw_mem_ring_release (&mem_free->stname); - break; - default: - NSFW_LOGERR ("free mem err type]type=%u", mem_free->ustype); - return NULL; - } - - if (NSFW_MEM_OK != ret) - { - NSFW_LOGERR ("mem free failed!]ret=%d", ret); - return NULL; - } - - return pdata; + nsfw_mem_type_info *mem_free = (nsfw_mem_type_info *) pdata; + i32 ret; + NSFW_LOGINF("free mem]type=%u,name=%s", mem_free->ustype, + mem_free->stname.aname); + switch (mem_free->ustype) + { + case NSFW_MEM_MZONE: + ret = nsfw_mem_zone_release(&mem_free->stname); + break; + case NSFW_MEM_MBUF: + ret = nsfw_mem_mbfmp_release(&mem_free->stname); + break; + case NSFW_MEM_SPOOL: + ret = nsfw_mem_sp_release(&mem_free->stname); + break; + case NSFW_MEM_RING: + ret = nsfw_mem_ring_release(&mem_free->stname); + break; + default: + NSFW_LOGERR("free mem err type]type=%u", mem_free->ustype); + return NULL; + } + + if (NSFW_MEM_OK != ret) + { + NSFW_LOGERR("mem free failed!]ret=%d", ret); + return NULL; + } + + return pdata; } -/***************************************************************************** -* Prototype : mem_item_lookup -* Description : lookup memory item -* Input : void *pdata -* Output : None -* Return Value : void* -* Calls : -* Called By : -*****************************************************************************/ -void * -mem_item_lookup (void *pdata) +void *mem_item_lookup(void *pdata) { - nsfw_mem_type_info *mem_lookup = (nsfw_mem_type_info *) pdata; - void *ret; - NSFW_LOGDBG ("lookup mem]type=%u,name=%s", mem_lookup->ustype, - mem_lookup->stname.aname); - switch (mem_lookup->ustype) - { - case NSFW_MEM_MZONE: - ret = nsfw_mem_zone_lookup (&mem_lookup->stname); - break; - case NSFW_MEM_MBUF: - ret = nsfw_mem_mbfmp_lookup (&mem_lookup->stname); - break; - case NSFW_MEM_SPOOL: - ret = nsfw_mem_sp_lookup (&mem_lookup->stname); - break; - case NSFW_MEM_RING: - ret = nsfw_mem_ring_lookup (&mem_lookup->stname); - break; - default: - NSFW_LOGERR ("lookup mem err type]type=%d", mem_lookup->ustype); - return NULL; - } - - if (NULL == ret) - { - NSFW_LOGERR ("mem lookup failed!]ret=%p,name=%s", ret, - mem_lookup->stname.aname); - return NULL; - } - - return ret; + nsfw_mem_type_info *mem_lookup = (nsfw_mem_type_info *) pdata; + void *ret; + NSFW_LOGDBG("lookup mem]type=%u,name=%s", mem_lookup->ustype, + mem_lookup->stname.aname); + switch (mem_lookup->ustype) + { + case NSFW_MEM_MZONE: + ret = nsfw_mem_zone_lookup(&mem_lookup->stname); + break; + case NSFW_MEM_MBUF: + ret = nsfw_mem_mbfmp_lookup(&mem_lookup->stname); + break; + case NSFW_MEM_SPOOL: + ret = nsfw_mem_sp_lookup(&mem_lookup->stname); + break; + case NSFW_MEM_RING: + ret = nsfw_mem_ring_lookup(&mem_lookup->stname); + break; + default: + NSFW_LOGERR("lookup mem err type]type=%d", mem_lookup->ustype); + return NULL; + } + + if (NULL == ret) + { + NSFW_LOGERR("mem lookup failed!]ret=%p,name=%s", ret, + mem_lookup->stname.aname); + return NULL; + } + + return ret; } /***************************************************************************** @@ -419,234 +383,191 @@ mem_item_lookup (void *pdata) * Calls : * Called By : *****************************************************************************/ -void * -mem_item_proc_by_msg (void *pdata, nsfw_ps_mem_item_cfg * item_cfg) +void *mem_item_proc_by_msg(void *pdata, nsfw_ps_mem_item_cfg * item_cfg) { - char argv_buf[NSFW_MEM_CALL_ARG_BUF] = { 0 }; + char argv_buf[NSFW_MEM_CALL_ARG_BUF] = { 0 }; - if ((NULL == item_cfg->change_fun) || (NULL == item_cfg->create_fun)) + if ((NULL == item_cfg->change_fun) || (NULL == item_cfg->create_fun)) { - NSFW_LOGERR ("item error]change_fun=%p,create_fun=%p", - item_cfg->change_fun, item_cfg->create_fun); - return NULL; + NSFW_LOGERR("item error]change_fun=%p,create_fun=%p", + item_cfg->change_fun, item_cfg->create_fun); + return NULL; } - if (FALSE == - item_cfg->change_fun (item_cfg->usmsg_type, pdata, argv_buf, - NSFW_MEM_CALL_ARG_BUF)) + if (FALSE == + item_cfg->change_fun(item_cfg->usmsg_type, pdata, argv_buf, + NSFW_MEM_CALL_ARG_BUF)) { - NSFW_LOGERR ("call change_fun failed!]type=%u", item_cfg->usmsg_type); - return NULL; + NSFW_LOGERR("call change_fun failed!]type=%u", item_cfg->usmsg_type); + return NULL; } - void *pdataret = NULL; - pdataret = (item_cfg->create_fun) ((void *) argv_buf); - return pdataret; + void *pdataret = NULL; + pdataret = (item_cfg->create_fun) ((void *) argv_buf); + return pdataret; } -/***************************************************************************** -* Prototype : mem_init_rsp_msg -* Description : init the rsp message -* Input : nsfw_shmem_msg_head* msg -* nsfw_shmem_msg_head *rsp -* Output : None -* Return Value : u8 -* Calls : -* Called By : -* -*****************************************************************************/ -u8 -mem_init_rsp_msg (nsfw_shmem_msg_head * msg, nsfw_shmem_msg_head * rsp) +u8 mem_init_rsp_msg(nsfw_shmem_msg_head * msg, nsfw_shmem_msg_head * rsp) { - nsfw_ps_mem_item_cfg *item_cfg = - mem_item_get_cfg_from_msg (msg->usmsg_type); - if (NULL == item_cfg) + nsfw_ps_mem_item_cfg *item_cfg = + mem_item_get_cfg_from_msg(msg->usmsg_type); + if (NULL == item_cfg) { - NSFW_LOGERR ("get item cfg failed!]msg_type=%u", msg->usmsg_type); - return FALSE; + NSFW_LOGERR("get item cfg failed!]msg_type=%u", msg->usmsg_type); + return FALSE; } - int idx; - int mem_count = msg->uslength / item_cfg->item_size; + int idx; + int mem_count = msg->uslenth / item_cfg->item_size; - rsp->usmsg_type = msg->usmsg_type + 1; - rsp->uslength = mem_count * sizeof (nsfw_shmem_ack); - nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); - char *pdata = NULL; - for (idx = 0; idx < mem_count; idx++) + rsp->usmsg_type = msg->usmsg_type + 1; + rsp->uslenth = mem_count * sizeof(nsfw_shmem_ack); + nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); + char *pdata = NULL; + for (idx = 0; idx < mem_count; idx++) { - pdata = (char *) msg->aidata + idx * item_cfg->item_size; - pack->pbase_addr = NULL; - pack->usseq = ((nsfw_shmem_reserv_req *) pdata)->usseq; - pack->cstate = NSFW_MEM_ALLOC_FAIL; - pack++; + + pdata = (char *) msg->aidata + idx * item_cfg->item_size; + pack->pbase_addr = NULL; + pack->usseq = ((nsfw_shmem_reserv_req *) pdata)->usseq; + pack->cstate = NSFW_MEM_ALLOC_FAIL; + pack++; } - NSFW_LOGDBG ("init all rsp ack]mem_count=%d,msg_type=%u", mem_count, - msg->usmsg_type); - return TRUE; + NSFW_LOGDBG("init all rsp ack]mem_count=%d,msg_type=%u", mem_count, + msg->usmsg_type); + return TRUE; } -/***************************************************************************** -* Prototype : mem_rel_mem_by_msg -* Description : release memory by message -* Input : nsfw_shmem_msg_head* req_msg -* nsfw_shmem_msg_head *rsp -* u32 pid -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -mem_rel_mem_by_msg (nsfw_shmem_msg_head * req_msg, - nsfw_shmem_msg_head * rsp, u32 pid) +u8 mem_rel_mem_by_msg(nsfw_shmem_msg_head * req_msg, + nsfw_shmem_msg_head * rsp, u32 pid) { - u32 i; - nsfw_ps_mem_item_cfg *item_cfg = - mem_item_get_cfg_from_msg (req_msg->usmsg_type); - if (NULL == item_cfg) + u32 i; + nsfw_ps_mem_item_cfg *item_cfg = + mem_item_get_cfg_from_msg(req_msg->usmsg_type); + if (NULL == item_cfg) { - NSFW_LOGERR ("get item cfg failed!]msg_type=%u", req_msg->usmsg_type); - return FALSE; + NSFW_LOGERR("get item cfg failed!]msg_type=%u", req_msg->usmsg_type); + return FALSE; } - unsigned int mem_count = req_msg->uslength / item_cfg->item_size; - char *pdata = NULL; - nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); - for (i = 0; i < mem_count; i++) + unsigned int mem_count = req_msg->uslenth / item_cfg->item_size; + char *pdata = NULL; + nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); + for (i = 0; i < mem_count; i++) { - pdata = (char *) req_msg->aidata + i * item_cfg->item_size; - if (NULL != mem_item_proc_by_msg ((void *) pdata, item_cfg)) + + pdata = (char *) req_msg->aidata + i * item_cfg->item_size; + if (NULL != mem_item_proc_by_msg((void *) pdata, item_cfg)) { - pack->cstate = NSFW_MEM_ALLOC_SUCC; - pack->pbase_addr = NULL; + pack->cstate = NSFW_MEM_ALLOC_SUCC; + pack->pbase_addr = NULL; } - pack++; + pack++; } - return TRUE; + return TRUE; } -/***************************************************************************** -* Prototype : mem_lookup_mem_by_msg -* Description : lookup memory by message -* Input : nsfw_shmem_msg_head* mgr_msg -* nsfw_shmem_msg_head *rsp -* u32 pid -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -mem_lookup_mem_by_msg (nsfw_shmem_msg_head * mgr_msg, - nsfw_shmem_msg_head * rsp, u32 pid) +u8 mem_lookup_mem_by_msg(nsfw_shmem_msg_head * mgr_msg, + nsfw_shmem_msg_head * rsp, u32 pid) { - i32 idx; - nsfw_ps_mem_item_cfg *item_cfg = - mem_item_get_cfg_from_msg (mgr_msg->usmsg_type); - if (NULL == item_cfg) + i32 idx; + nsfw_ps_mem_item_cfg *item_cfg = + mem_item_get_cfg_from_msg(mgr_msg->usmsg_type); + if (NULL == item_cfg) { - NSFW_LOGERR ("get item cfg failed!]msg_type=%u", mgr_msg->usmsg_type); - return FALSE; + NSFW_LOGERR("get item cfg failed!]msg_type=%u", mgr_msg->usmsg_type); + return FALSE; } - int mem_count = mgr_msg->uslength / item_cfg->item_size; - char *pdata = NULL; - void *paddr = NULL; - nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); + int mem_count = mgr_msg->uslenth / item_cfg->item_size; + char *pdata = NULL; + void *paddr = NULL; + nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); - for (idx = 0; idx < mem_count; idx++) + for (idx = 0; idx < mem_count; idx++) { - pdata = (char *) mgr_msg->aidata + idx * item_cfg->item_size; - paddr = mem_item_proc_by_msg ((void *) pdata, item_cfg); - if (NULL != paddr) + + pdata = (char *) mgr_msg->aidata + idx * item_cfg->item_size; + paddr = mem_item_proc_by_msg((void *) pdata, item_cfg); + if (NULL != paddr) { - pack->cstate = NSFW_MEM_ALLOC_SUCC; - pack->pbase_addr = paddr; + pack->cstate = NSFW_MEM_ALLOC_SUCC; + pack->pbase_addr = paddr; } - pack++; + pack++; } - return TRUE; + return TRUE; } -/***************************************************************************** -* Prototype : mem_alloc_mem_by_msg -* Description : alloc memory by message -* Input : nsfw_shmem_msg_head* mem_msg -* nsfw_shmem_msg_head *rsp -* Output : None -* Return Value : ns_mem_info* -* Calls : -* Called By : -*****************************************************************************/ -void * -mem_alloc_mem_by_msg (nsfw_shmem_msg_head * mem_msg, - nsfw_shmem_msg_head * rsp) +void *mem_alloc_mem_by_msg(nsfw_shmem_msg_head * mem_msg, + nsfw_shmem_msg_head * rsp) { - nsfw_ps_mem_item_cfg *item_cfg = - mem_item_get_cfg_from_msg (mem_msg->usmsg_type); - if (NULL == item_cfg) + nsfw_ps_mem_item_cfg *item_cfg = + mem_item_get_cfg_from_msg(mem_msg->usmsg_type); + if (NULL == item_cfg) { - NSFW_LOGERR ("get item cfg failed!]msg_type=%u", mem_msg->usmsg_type); - return NULL; + NSFW_LOGERR("get item cfg fail]msg_type=%u", mem_msg->usmsg_type); + return NULL; } - int i; - int j; - nsfw_mem_type_info mem_free; - char *pdata = NULL; - void *p_addr = NULL; + int i; + int j; + nsfw_mem_type_info mem_free; + char *pdata = NULL; + void *p_addr = NULL; - int mem_count = mem_msg->uslength / item_cfg->item_size; - nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); - for (i = 0; i < mem_count; i++) + int mem_count = mem_msg->uslenth / item_cfg->item_size; + nsfw_shmem_ack *pack = (nsfw_shmem_ack *) & (rsp->aidata[0]); + for (i = 0; i < mem_count; i++) { - pdata = (char *) mem_msg->aidata + i * item_cfg->item_size; - p_addr = mem_item_proc_by_msg ((void *) pdata, item_cfg); - if (NULL == p_addr) + + pdata = (char *) mem_msg->aidata + i * item_cfg->item_size; + p_addr = mem_item_proc_by_msg((void *) pdata, item_cfg); + if (NULL == p_addr) { - NSFW_LOGERR - ("alloc mem failed!]type=%u,mem_count=%d,item=%d,name=%s", - mem_msg->usmsg_type, mem_count, i, - ((nsfw_shmem_reserv_req *) pdata)->aname); - goto fail_free_mem; + NSFW_LOGERR + ("alloc mem fail]type=%u,mem_count=%d,item=%d,name=%s", + mem_msg->usmsg_type, mem_count, i, + ((nsfw_shmem_reserv_req *) pdata)->aname); + goto fail_free_mem; } - pack->cstate = NSFW_MEM_ALLOC_SUCC; - pack->pbase_addr = p_addr; - NSFW_LOGINF - ("alloc mem suc!]addr=%p,type=%u,mem_count=%d,item=%d,name=%s", - p_addr, mem_msg->usmsg_type, mem_count, i, - ((nsfw_shmem_reserv_req *) pdata)->aname); - pack++; + pack->cstate = NSFW_MEM_ALLOC_SUCC; + pack->pbase_addr = p_addr; + NSFW_LOGINF("alloc suc]addr=%p,type=%u,mem_count=%d,item=%d,name=%s", + p_addr, mem_msg->usmsg_type, mem_count, i, + ((nsfw_shmem_reserv_req *) pdata)->aname); + pack++; } - return p_addr; + return p_addr; -fail_free_mem: - for (j = 0; j < i; j++) + fail_free_mem: + for (j = 0; j < i; j++) { - pdata = (char *) mem_msg->aidata + j * item_cfg->item_size; - if (EOK != - STRCPY_S (mem_free.stname.aname, NSFW_MEM_NAME_LENGTH, - ((nsfw_shmem_reserv_req *) pdata)->aname)) + + pdata = (char *) mem_msg->aidata + j * item_cfg->item_size; + /*copy string should use strcpy_s */ + if (EOK != + strcpy_s(mem_free.stname.aname, NSFW_MEM_NAME_LENTH, + ((nsfw_shmem_reserv_req *) pdata)->aname)) { - NSFW_LOGERR ("STRCPY_S failed]j=%d", j); - continue; + NSFW_LOGERR("strcpy_s fail]j=%d", j); + continue; } - mem_free.ustype = item_cfg->mem_type; - mem_free.stname.entype = NSFW_SHMEM; - (void) mem_item_free (&mem_free); - NSFW_LOGINF ("free mem]addr=%p,type=%u,mem_count=%d,item=%d,name=%s", - p_addr, mem_msg->usmsg_type, mem_count, j, - ((nsfw_shmem_reserv_req *) pdata)->aname); - pack++; + mem_free.ustype = item_cfg->mem_type; + mem_free.stname.entype = NSFW_SHMEM; + (void) mem_item_free(&mem_free); + NSFW_LOGINF("free mem]addr=%p,type=%u,mem_count=%d,item=%d,name=%s", + p_addr, mem_msg->usmsg_type, mem_count, j, + ((nsfw_shmem_reserv_req *) pdata)->aname); + pack++; } - return NULL; + return NULL; } @@ -659,75 +580,75 @@ fail_free_mem: * Calls : * Called By : *****************************************************************************/ -int -mem_alloc_req_proc (nsfw_mgr_msg * msg) +int mem_alloc_req_proc(nsfw_mgr_msg * msg) { - void *mem_addr = NULL; - - if (NULL == msg) - { - NSFW_LOGERR ("msg nul"); - return FALSE; - } - - nsfw_shmem_msg_head *mem_msg = GET_USER_MSG (nsfw_shmem_msg_head, msg); - nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg); - if (NULL == rsp_msg) - { - NSFW_LOGERR ("alloc rsp msg failed]msg_type=%u", mem_msg->usmsg_type); - return FALSE; - } - - nsfw_shmem_msg_head *mem_rsp_msg = - GET_USER_MSG (nsfw_shmem_msg_head, rsp_msg); - - if (!mem_init_rsp_msg (mem_msg, mem_rsp_msg)) - { - NSFW_LOGERR ("init rsp msg failed]msg_type=%u", mem_msg->usmsg_type); - nsfw_mgr_msg_free (rsp_msg); - return FALSE; - } - - switch (mem_msg->usmsg_type) - { - case NSFW_MEM_LOOKUP_REQ_MSG: - if (!mem_lookup_mem_by_msg (mem_msg, mem_rsp_msg, msg->src_pid)) - { - NSFW_LOGERR ("lookup mem msg failed]msg_type=%u", - mem_msg->usmsg_type); - goto sendrspmsg; - } - (void) mem_alloc_ps_info (msg->src_pid, msg->src_proc_type); - break; - case NSFW_RESERV_REQ_MSG: - case NSFW_MBUF_REQ_MSG: - case NSFW_SPPOOL_REQ_MSG: - case NSFW_RING_REQ_MSG: - mem_addr = mem_alloc_mem_by_msg (mem_msg, mem_rsp_msg); - if (NULL == mem_addr) - { - NSFW_LOGERR ("alloc mem msg failed]msg_type=%u", - mem_msg->usmsg_type); - (void) mem_init_rsp_msg (mem_msg, mem_rsp_msg); - goto sendrspmsg; - } - (void) mem_alloc_ps_info (msg->src_pid, msg->src_proc_type); - break; - case NSFW_RELEASE_REQ_MSG: - if (!mem_rel_mem_by_msg (mem_msg, mem_rsp_msg, msg->src_pid)) - { - NSFW_LOGERR ("rel mem msg failed]msg_type=%u", mem_msg->usmsg_type); - goto sendrspmsg; - } - break; - default: - break; - } - -sendrspmsg: - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); - return TRUE; + void *mem_addr = NULL; + + if (NULL == msg) + { + NSFW_LOGERR("msg nul"); + return FALSE; + } + + nsfw_shmem_msg_head *mem_msg = GET_USER_MSG(nsfw_shmem_msg_head, msg); + nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc(msg); + if (NULL == rsp_msg) + { + NSFW_LOGERR("alloc rsp msg failed]msg_type=%u", mem_msg->usmsg_type); + return FALSE; + } + + nsfw_shmem_msg_head *mem_rsp_msg = + GET_USER_MSG(nsfw_shmem_msg_head, rsp_msg); + + if (!mem_init_rsp_msg(mem_msg, mem_rsp_msg)) + { + NSFW_LOGERR("init rsp msg failed]msg_type=%u", mem_msg->usmsg_type); + nsfw_mgr_msg_free(rsp_msg); + return FALSE; + } + + switch (mem_msg->usmsg_type) + { + case NSFW_MEM_LOOKUP_REQ_MSG: + if (!mem_lookup_mem_by_msg(mem_msg, mem_rsp_msg, msg->src_pid)) + { + NSFW_LOGERR("lookup mem msg failed]msg_type=%u", + mem_msg->usmsg_type); + goto sendrspmsg; + } + (void) mem_alloc_ps_info(msg->src_pid, msg->src_proc_type); + break; + case NSFW_RESERV_REQ_MSG: + case NSFW_MBUF_REQ_MSG: + case NSFW_SPPOOL_REQ_MSG: + case NSFW_RING_REQ_MSG: + mem_addr = mem_alloc_mem_by_msg(mem_msg, mem_rsp_msg); + if (NULL == mem_addr) + { + NSFW_LOGERR("alloc mem msg failed]msg_type=%u", + mem_msg->usmsg_type); + (void) mem_init_rsp_msg(mem_msg, mem_rsp_msg); + goto sendrspmsg; + } + (void) mem_alloc_ps_info(msg->src_pid, msg->src_proc_type); + break; + case NSFW_RELEASE_REQ_MSG: + if (!mem_rel_mem_by_msg(mem_msg, mem_rsp_msg, msg->src_pid)) + { + NSFW_LOGERR("rel mem msg failed]msg_type=%u", + mem_msg->usmsg_type); + goto sendrspmsg; + } + break; + default: + break; + } + + sendrspmsg: + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); + return TRUE; } /***************************************************************************** @@ -740,47 +661,64 @@ sendrspmsg: * Calls : * Called By : *****************************************************************************/ -int -mem_ps_exiting_resend (void *pps_info, void *argv) +int mem_ps_exiting_resend(void *pps_info, void *argv) { - u32 *count = argv; - nsfw_ps_info *ps_info = pps_info; - if (NULL == ps_info) + u32 *count = argv; + nsfw_ps_info *ps_info = pps_info; + if (NULL == ps_info) { - NSFW_LOGERR ("ps_info nul!"); - return FALSE; + NSFW_LOGERR("ps_info nul!"); + return FALSE; } - if (NSFW_PROC_APP != ps_info->proc_type) + if (NSFW_PROC_APP != ps_info->proc_type) { - return FALSE; + return FALSE; } - if (NSFW_PS_EXITING != ps_info->state) + if (NSFW_PS_EXITING != ps_info->state) { - return FALSE; + return FALSE; } - if (NULL != count) + if (NULL != count) { - NSFW_LOGINF ("send count]count=%u,pid=%u", *count, ps_info->host_pid); - if (NSFW_PS_SEND_PER_TIME < (*count)++) + NSFW_LOGINF("send count]count=%u,pid=%u", *count, ps_info->host_pid); + if (NSFW_PS_SEND_PER_TIME < (*count)++) { - struct timespec time_left = - { NSFW_PS_MEM_RESEND_TVLAUE + + struct timespec time_left = + { NSFW_PS_MEM_RESEND_TVLAUE + ((*count) / NSFW_PS_SEND_PER_TIME), 0 - }; - ps_info->resend_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_PS_MEM_RESEND_TIMER, - pps_info, - nsfw_mem_ps_exit_resend_timeout, - time_left); - return TRUE; + }; + ps_info->resend_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_PS_MEM_RESEND_TIMER, + pps_info, + nsfw_mem_ps_exit_resend_timeout, + time_left); + return TRUE; } } - (void) mem_ps_exiting (ps_info, NULL); - return TRUE; + (void) mem_ps_exiting(ps_info, NULL); + return TRUE; +} + +/***************************************************************************** +* Prototype : mem_ps_exiting +* Description : Send exiting message when a single ps_info is exiting. +* If there have been more than NSFW_PS_SEND_PER_TIME apps +* in EXITING state, register a resend timer. +* Input : void *pps_info +* void* argv +* Output : None +* Return Value : int +* Calls : +* Called By : See NSFW_PS_EXITING msg +*****************************************************************************/ +int mem_ps_exiting_send(void *pps_info, void *argv) +{ + u32 count = get_ps_exiting_cnt(); + return mem_ps_exiting_resend(pps_info, (void *) &count); } /***************************************************************************** @@ -793,18 +731,17 @@ mem_ps_exiting_resend (void *pps_info, void *argv) * Calls : * Called By : *****************************************************************************/ -int -nsfw_mem_srv_restore_timeout (u32 timer_type, void *data) +int nsfw_mem_srv_restore_timeout(u32 timer_type, void *data) { - u32 max_count = 0; + u32 max_count = 0; - g_mem_cfg.p_restore_timer = NULL; - if (TRUE == NSFW_SRV_STATE_SUSPEND) + g_mem_cfg.p_restore_timer = NULL; + if (TRUE == NSFW_SRV_STATE_SUSPEND) { - NSFW_SRV_STATE_SUSPEND = FALSE; - (void) nsfw_ps_iterator (mem_ps_exiting_resend, &max_count); + NSFW_SRV_STATE_SUSPEND = FALSE; + (void) nsfw_ps_iterator(mem_ps_exiting_resend, &max_count); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -816,105 +753,100 @@ nsfw_mem_srv_restore_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -int -mem_srv_ctrl_proc (nsfw_mgr_msg * msg) +int mem_srv_ctrl_proc(nsfw_mgr_msg * msg) { - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("msg nul"); - return FALSE; + NSFW_LOGERR("msg nul"); + return FALSE; } - nsfw_srv_ctrl_msg *ctrl_msg = GET_USER_MSG (nsfw_srv_ctrl_msg, msg); - nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg); - if (NULL == rsp_msg) + nsfw_srv_ctrl_msg *ctrl_msg = GET_USER_MSG(nsfw_srv_ctrl_msg, msg); + nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc(msg); + if (NULL == rsp_msg) { - NSFW_LOGERR ("alloc rsp msg failed]msg=%p", msg); - return FALSE; + NSFW_LOGERR("alloc rsp msg failed]msg=%p", msg); + return FALSE; } - nsfw_srv_ctrl_msg *ctrl_rsp_msg = GET_USER_MSG (nsfw_srv_ctrl_msg, rsp_msg); - NSFW_LOGINF ("get srv ctrl state] state=%d", ctrl_msg->srv_state); + nsfw_srv_ctrl_msg *ctrl_rsp_msg = + GET_USER_MSG(nsfw_srv_ctrl_msg, rsp_msg); + NSFW_LOGINF("get srv ctrl state] state=%d", ctrl_msg->srv_state); - ctrl_rsp_msg->rsp_code = NSFW_MGR_SUCCESS; + ctrl_rsp_msg->rsp_code = NSFW_MGR_SUCESS; - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); - if (NSFW_SRV_CTRL_RESUME == ctrl_msg->srv_state) + if (NSFW_SRV_CTRL_RESUME == ctrl_msg->srv_state) { - if (TRUE == NSFW_SRV_STATE_SUSPEND) + if (TRUE == NSFW_SRV_STATE_SUSPEND) { - NSFW_SRV_STATE_SUSPEND = FALSE; + NSFW_SRV_STATE_SUSPEND = FALSE; } - u32 max_count = 0; - (void) nsfw_ps_iterator (mem_ps_exiting_resend, &max_count); - if (NULL != g_mem_cfg.p_restore_timer) + u32 max_count = 0; + (void) nsfw_ps_iterator(mem_ps_exiting_resend, &max_count); + if (NULL != g_mem_cfg.p_restore_timer) { - nsfw_timer_rmv_timer ((nsfw_timer_info *) - g_mem_cfg.p_restore_timer); - g_mem_cfg.p_restore_timer = NULL; + nsfw_timer_rmv_timer((nsfw_timer_info *) + g_mem_cfg.p_restore_timer); + g_mem_cfg.p_restore_timer = NULL; } } - else + else { - NSFW_SRV_STATE_SUSPEND = TRUE; - struct timespec time_left = { NSFW_SRV_RESTORE_TVALUE, 0 }; - g_mem_cfg.p_restore_timer = - (void *) nsfw_timer_reg_timer (0, NULL, - nsfw_mem_srv_restore_timeout, - time_left); + NSFW_SRV_STATE_SUSPEND = TRUE; + struct timespec time_left = { NSFW_SRV_RESTORE_TVALUE, 0 }; + g_mem_cfg.p_restore_timer = + (void *) nsfw_timer_reg_timer(0, NULL, + nsfw_mem_srv_restore_timeout, + time_left); } - return TRUE; + return TRUE; } -/***************************************************************************** -* Prototype : nsfw_ps_mem_module_init -* Description : module init -* Input : void* param -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ -int -nsfw_ps_mem_module_init (void *param) +int nsfw_ps_mem_module_init(void *param) { - u32 proc_type = (u32) ((long long) param); - NSFW_LOGINF ("ps_mem module init]type=%u", proc_type); - switch (proc_type) - { - case NSFW_PROC_MAIN: - (void) nsfw_ps_reg_global_fun (NSFW_PROC_APP, NSFW_PS_EXITING, - mem_ps_exiting, NULL); - (void) nsfw_mgr_reg_msg_fun (MGR_MSG_MEM_ALLOC_REQ, mem_alloc_req_proc); - (void) NSFW_REG_SOFT_INT (NSFW_SRV_RESTORE_TIMER, - NSFW_SRV_RESTORE_TVALUE, 1, 0xFFFF); - (void) NSFW_REG_SOFT_INT (NSFW_APP_RESEND_TIMER, - NSFW_PS_MEM_RESEND_TVLAUE, 1, 0xFFFF); - (void) NSFW_REG_SOFT_INT (NSFW_APP_SEND_PER_TIME, - NSFW_PS_SEND_PER_TIME, 1, 0xFFFF); - break; - default: - if (proc_type < NSFW_PROC_MAX) - { - return 0; - } - return -1; - } - - g_mem_cfg.srv_restore_tvalue = NSFW_SRV_RESTORE_TVALUE_DEF; - g_mem_cfg.ps_exit_resend_tvalue = NSFW_PS_MEM_RESEND_TVLAUE_DEF; - g_mem_cfg.ps_send_per_time = NSFW_PS_SEND_PER_TIME_DEF; - - return 0; + u32 proc_type = (u32) ((long long) param); + NSFW_LOGINF("ps_mem module init]type=%u", proc_type); + switch (proc_type) + { + case NSFW_PROC_MASTER: + (void) nsfw_ps_reg_global_fun(NSFW_PROC_APP, NSFW_PS_EXITING, + mem_ps_exiting_send, NULL); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_MEM_ALLOC_REQ, + mem_alloc_req_proc); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_SRV_CTL_REQ, + mem_srv_ctrl_proc); + (void) NSFW_REG_SOFT_INT(NSFW_SRV_RESTORE_TIMER, + NSFW_SRV_RESTORE_TVALUE, 1, 0xFFFF); + (void) NSFW_REG_SOFT_INT(NSFW_APP_RESEND_TIMER, + NSFW_PS_MEM_RESEND_TVLAUE, 1, 0xFFFF); + (void) NSFW_REG_SOFT_INT(NSFW_APP_SEND_PER_TIME, + NSFW_PS_SEND_PER_TIME, 1, 0xFFFF); + break; + default: + if (proc_type < NSFW_PROC_MAX) + { + return 0; + } + return -1; + } + + g_mem_cfg.srv_restore_tvalue = NSFW_SRV_RESTORE_TVALUE_DEF; + g_mem_cfg.ps_exit_resend_tvalue = NSFW_PS_MEM_RESEND_TVLAUE_DEF; + g_mem_cfg.ps_send_per_time = NSFW_PS_SEND_PER_TIME_DEF; + g_mem_cfg.srv_suspend = 0; + g_mem_cfg.p_restore_timer = NULL; + + return 0; } /* *INDENT-OFF* */ -NSFW_MODULE_NAME (NSFW_PS_MEM_MODULE) -NSFW_MODULE_PRIORITY (10) -NSFW_MODULE_DEPENDS (NSFW_PS_MODULE) -NSFW_MODULE_INIT (nsfw_ps_mem_module_init) +NSFW_MODULE_NAME(NSFW_PS_MEM_MODULE) +NSFW_MODULE_PRIORITY(10) +NSFW_MODULE_DEPENDS(NSFW_PS_MODULE) +NSFW_MODULE_INIT(nsfw_ps_mem_module_init) /* *INDENT-ON* */ #ifdef __cplusplus diff --git a/src/framework/ipc/ps/nsfw_ps_mem_module.h b/src/framework/ipc/ps/nsfw_ps_mem_module.h index 6b2b3c9..337b578 100644 --- a/src/framework/ipc/ps/nsfw_ps_mem_module.h +++ b/src/framework/ipc/ps/nsfw_ps_mem_module.h @@ -19,6 +19,7 @@ #include "list.h" #include "pidinfo.h" +#include "nsfw_mem_api.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -42,18 +43,18 @@ extern "C"{ typedef struct _ns_mem_mng_init_cfg { - u16 srv_restore_tvalue; - u16 ps_exit_resend_tvalue; - u16 ps_send_per_time; - u16 srv_suspend; - void *p_restore_timer; + u16 srv_restore_tvalue; + u16 ps_exit_resend_tvalue; + u16 ps_send_per_time; + u16 srv_suspend; + void *p_restore_timer; } ns_mem_mng_init_cfg; /*mem alloc by msg begin*/ typedef struct { - nsfw_mem_name stname; - u16 ustype; + nsfw_mem_name stname; + u16 ustype; } nsfw_mem_type_info; #define NSFW_MEM_CALL_ARG_BUF 256 @@ -61,22 +62,22 @@ typedef struct ((_dst_type*)(void*)_dst_buf)->_dst_member = ((_srctype*)(void*)_src_buf)->_src_member typedef void *(*nsfw_ps_mem_create_fun) (void *memstr); -typedef u8 (*nsfw_ps_mem_msg_to_memstr) (u16 msg_type, char *msg_body, - char *memstr_buf, i32 buf_len); +typedef u8(*nsfw_ps_mem_msg_to_memstr) (u16 msg_type, char *msg_body, + char *memstr_buf, i32 buf_len); typedef struct __nsfw_ps_mem_item_cfg { - u16 usmsg_type; - u16 item_size; - u16 mem_type; - nsfw_ps_mem_create_fun create_fun; - nsfw_ps_mem_msg_to_memstr change_fun; + u16 usmsg_type; + u16 item_size; + u16 mem_type; + nsfw_ps_mem_create_fun create_fun; + nsfw_ps_mem_msg_to_memstr change_fun; } nsfw_ps_mem_item_cfg; -void *mem_item_free (void *pdata); -void *mem_item_lookup (void *pdata); -u8 mem_item_get_callargv (u16 msg_type, char *msg_body, char *memstr_buf, - i32 buf_len); +void *mem_item_free(void *pdata); +void *mem_item_lookup(void *pdata); +u8 mem_item_get_callargv(u16 msg_type, char *msg_body, char *memstr_buf, + i32 buf_len); #ifdef __cplusplus /* *INDENT-OFF* */ diff --git a/src/framework/ipc/ps/nsfw_ps_module.c b/src/framework/ipc/ps/nsfw_ps_module.c index 6e03e11..6d11c5f 100644 --- a/src/framework/ipc/ps/nsfw_ps_module.c +++ b/src/framework/ipc/ps/nsfw_ps_module.c @@ -17,9 +17,11 @@ #include <stdlib.h> #include "types.h" #include "nstack_securec.h" -#include "nsfw_init.h" +#include "nsfw_init_api.h" #include "nsfw_mgr_com_api.h" +#include "mgr_com.h" +#include "nsfw_alarm_api.h" #include "nsfw_ps_api.h" #include "nsfw_ps_module.h" #include "nsfw_mem_api.h" @@ -39,7 +41,6 @@ #include <linux/netlink.h> #include <dirent.h> #include <fnmatch.h> -#include "common_mem_common.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -57,8 +58,454 @@ struct list_head g_ps_runing_list; nsfw_ps_callback g_ps_init_callback[NSFW_PROC_MAX][NSFW_PS_MAX_CALLBACK]; nsfw_ps_info g_main_ps_info; +extern nsfw_mgr_init_cfg g_mgr_com_cfg; + void *g_ps_chk_timer = NULL; +/* keep count of processes being in EXITING state */ +u32 g_ps_exiting_cnt = 0; +NSTACK_STATIC void inc_ps_exiting_cnt(); +NSTACK_STATIC void dec_ps_exiting_cnt(); + +typedef struct _omc_alarm_status +{ + char send_succ_flag; + alarm_flag current_alarm_state; + nsfw_timer_info *resend_timer; +} omc_alarm_stat; + +#define SCRIPT_NS_CTL_SND_NSMAIN_NOR_ALM "./nStackCtrl --module alm -n nsMain -t normal" +#define SCRIPT_NS_CTL_SND_NSMAIN_ABNOR_ALM "./nStackCtrl --module alm -n nsMain -t abnormal" +#define SCRIPT_NS_CTL_SND_NSMAIN_START "./nStackCtrl --module alm -n nsMain -t normalStart" +#define SCRIPT_NS_CTL_SND_NSMAIN_EXIT "./nStackCtrl --module alm -n nsMain -t abnormalExit" + +#define MASTER_RESEND_NSMAIN_TIMER 1 + +omc_alarm_stat g_omc_alm_state[ALARM_EVENT_MAX]; + +typedef struct _omc_hf_alarm_status +{ + nsfw_timer_info *resend_timer; + hotfix_res res; +} omc_hf_alarm_stat; +omc_hf_alarm_stat g_omc_hf_alm_state; +#define MASTER_RESEND_HOTFIX_TIMER 1 + +const char *omc_get_nstackctrl_alarm_string(enum_alarm_id alarm_id, + alarm_flag alarm_state) +{ + const char *alarm_script = NULL; + + switch (alarm_id) + { + case ALARM_EVENT_NSTACK_MAIN_ABNORMAL_RESTART_FAIL: + + if (ALARM_PRODUCT == alarm_state) + { + alarm_script = SCRIPT_NS_CTL_SND_NSMAIN_ABNOR_ALM; + } + else + { + alarm_script = SCRIPT_NS_CTL_SND_NSMAIN_NOR_ALM; + } + + break; + case ALARM_EVENT_NSTACK_MAIN_EXIT_CAUSE_FD_FAIL: + + if (ALARM_PRODUCT == alarm_state) + { + alarm_script = SCRIPT_NS_CTL_SND_NSMAIN_EXIT; + } + else + { + alarm_script = SCRIPT_NS_CTL_SND_NSMAIN_START; + } + + break; + default: + NSAM_LOGERR("alarm_id is invalid"); + break; + } + + return alarm_script; + +} + +/* should send restore alarm when daemon-stack restart successfully*/ +int omc_resend_nsmain_alarm(u32 timer_type, void *argv) +{ +#define TMP_RESULT_BUF_LEN 128 + + int retval; + char result[TMP_RESULT_BUF_LEN] = { 0 }; + int rt_len; + const char *alarm_script = NULL; + enum_alarm_id tmp_alarm_id = (enum_alarm_id) argv; + omc_alarm_stat *tmp_alarm_stat = NULL; + + if (ALARM_ID_NOT_VALID(tmp_alarm_id)) + { + NSAM_LOGERR("alarm_id is invalid"); + return 0; + } + + tmp_alarm_stat = &g_omc_alm_state[tmp_alarm_id]; + + /* when timeout, need clear */ + tmp_alarm_stat->resend_timer = NULL; + + alarm_script = + omc_get_nstackctrl_alarm_string(tmp_alarm_id, + tmp_alarm_stat->current_alarm_state); + + if (NULL == alarm_script) + { + NSAM_LOGERR("get alarm_script fail"); + return 0; + } + + if (0 != tmp_alarm_stat->send_succ_flag) + { + rt_len = + nsfw_mgr_run_script(alarm_script, result, TMP_RESULT_BUF_LEN); + if (0 == rt_len || -1 == rt_len || (!nsfw_isdigitstr(result))) //also check return -1 + { + retval = -1; + } + else + { + retval = atoi(result); + } + tmp_alarm_stat->send_succ_flag = retval; + + if (0 == tmp_alarm_stat->send_succ_flag) + { + NSAM_LOGINF("send alarm success]alarm_id=%u,alarm_status=%u", + tmp_alarm_id + ALARM_ID_BASE_VALUE, + tmp_alarm_stat->current_alarm_state); + } + else + { + NSAM_LOGINF("send alarm fail]alarm_id=%u,alarm_status=%u", + tmp_alarm_id + ALARM_ID_BASE_VALUE, + tmp_alarm_stat->current_alarm_state); + /* only when send fail, start resend timer */ + struct timespec alarm_resend_time = + { ALARM_RESEND_TIMER_LENGTH, 0 }; + tmp_alarm_stat->resend_timer = + nsfw_timer_reg_timer(MASTER_RESEND_NSMAIN_TIMER, + (void *) tmp_alarm_id, + omc_resend_nsmain_alarm, + alarm_resend_time); + if (NULL == tmp_alarm_stat->resend_timer) + { + NSFW_LOGERR("start alarm resend timer fail"); + } + } + + } + + return 0; + +} + +/***************************************************************************** +* Prototype : omc_remove_resend_timer_inner +* Description : remove alarm resend timer if exist +* Input : None +* Output : None +* Return Value : None +* Calls : +* Called By : omc_send_nsmain_alarm_inner +*****************************************************************************/ +NSTACK_STATIC void omc_remove_resend_timer_inner(enum_alarm_id alarm_id) +{ + omc_alarm_stat *tmp_alarm_stat = NULL; + if (ALARM_ID_NOT_VALID(alarm_id)) + { + NSAM_LOGERR("alarm_id is invalid"); + return; + } + + tmp_alarm_stat = &g_omc_alm_state[alarm_id]; + + if (NULL == tmp_alarm_stat->resend_timer) + { + return; + } + + nsfw_timer_rmv_timer(tmp_alarm_stat->resend_timer); + tmp_alarm_stat->resend_timer = NULL; + +} + +/***************************************************************************** +* Prototype : omc_send_nsmain_alarm_inner +* Description : omc_send_nsmain_alarm_inner process function +* Input : init_status_flag ; alarm_flag alm_flag +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +void omc_send_nsmain_alarm_inner(char init_status_flag, + enum_alarm_id alarm_id, alarm_flag alm_flag) +{ +#define TMP_RESULT_BUF_LEN 128 + int retval; + char result[TMP_RESULT_BUF_LEN] = { 0 }; + int rt_len; + omc_alarm_stat *tmp_alarm_stat = NULL; + + const char *alarm_script = NULL; + + if (ALARM_ID_NOT_VALID(alarm_id)) + { + NSAM_LOGDBG("alarm_id is invalid"); + return; + } + + tmp_alarm_stat = &g_omc_alm_state[alarm_id]; + + if (1 != init_status_flag) + { + /* only for alarm status change, can send alarm */ + if (alm_flag == tmp_alarm_stat->current_alarm_state) + { + NSAM_LOGDBG("same status, can't send alarm"); + return; + } + } + else + { + /* init alarm, use current alarm state stored */ + alm_flag = tmp_alarm_stat->current_alarm_state; + } + + alarm_script = omc_get_nstackctrl_alarm_string(alarm_id, alm_flag); + + if (NULL == alarm_script) + { + NSAM_LOGERR("get alarm_script fail"); + return; + } + + rt_len = nsfw_mgr_run_script(alarm_script, result, TMP_RESULT_BUF_LEN); + + if (0 == rt_len || -1 == rt_len || (!nsfw_isdigitstr(result))) + { + retval = -1; + } + else + { + retval = atoi(result); + } + tmp_alarm_stat->current_alarm_state = alm_flag; + tmp_alarm_stat->send_succ_flag = retval; + + if (0 == tmp_alarm_stat->send_succ_flag) + { + NSAM_LOGINF("send alarm success]alarm_id=%d,alarm_status=%d", + alarm_id + ALARM_ID_BASE_VALUE, + tmp_alarm_stat->current_alarm_state); + + /* when send success, then stop resend timer */ + omc_remove_resend_timer_inner(alarm_id); + } + else + { + NSAM_LOGINF("send alarm fail]alarm_id=%d,alarm_status=%d", + alarm_id + ALARM_ID_BASE_VALUE, + tmp_alarm_stat->current_alarm_state); + struct timespec alarm_resend_time = { ALARM_RESEND_TIMER_LENGTH, 0 }; + + /* should clear the old timer before register a new one */ + omc_remove_resend_timer_inner(alarm_id); + + /* only start resend time when send failed */ + tmp_alarm_stat->resend_timer = + nsfw_timer_reg_timer(MASTER_RESEND_NSMAIN_TIMER, + (void *) (alarm_id), + omc_resend_nsmain_alarm, alarm_resend_time); + if (NULL == tmp_alarm_stat->resend_timer) + { + NSFW_LOGERR("start alarm resend timer fail"); + } + } + +} + +void omc_send_init_nsmain_alarm(enum_alarm_id alarm_id) +{ + omc_send_nsmain_alarm_inner(1, alarm_id, ALARM_CLEAN); +} + +void omc_send_nsmain_alarm(enum_alarm_id alarm_id, alarm_flag alm_flag) +{ + omc_send_nsmain_alarm_inner(0, alarm_id, alm_flag); +} + +/***************************************************************************** +* Prototype : omc_remove_hf_resend_timer_inner +* Description : remove hotfix alarm resend timer if exist +* Input : None +* Output : None +* Return Value : None +* Calls : +* Called By : omc_send_nsmain_alarm_inner +*****************************************************************************/ +NSTACK_STATIC void omc_remove_hf_resend_timer_inner() +{ + if (NULL != g_omc_hf_alm_state.resend_timer) + { + nsfw_timer_rmv_timer(g_omc_hf_alm_state.resend_timer); + g_omc_hf_alm_state.resend_timer = NULL; + } +} + +/***************************************************************************** +* Prototype : omc_send_hotfix_alarm_inner +* Description : +* Input : void +* Output : None +* Return Value : int +* Calls : +* Called By : +*****************************************************************************/ +int omc_send_hotfix_alarm_inner() +{ + int retval; + int rt_len; + char alarm_script[512] = { 0 }; + char res_buffer[128] = { 0 }; + + retval = sprintf_s(alarm_script, sizeof(alarm_script), + "./nStackCtrl --module alm -n hotfix -a %d -r %d -v\"%s\" -p %d", + g_omc_hf_alm_state.res.action, + g_omc_hf_alm_state.res.result, + g_omc_hf_alm_state.res.patch_version, + g_omc_hf_alm_state.res.proc_type); + if (-1 == retval) + { + NSAM_LOGERR("sprintf_s fail]ret=%d", retval); + return -1; + } + + rt_len = + nsfw_mgr_run_script(alarm_script, res_buffer, sizeof(res_buffer)); + if (0 == rt_len || -1 == rt_len || (!nsfw_isdigitstr(res_buffer))) + { + retval = -1; + } + else + { + retval = atoi(res_buffer); + } + + if (0 == retval) + { + NSAM_LOGINF("send hotfix alarm success]alarm_id=4"); + /* when send success, then stop resend timer */ + omc_remove_hf_resend_timer_inner(); + } + else + { + NSAM_LOGINF("send hotfix alarm fail]alarm_id=4"); + /* should clear the old timer before register a new one */ + omc_remove_hf_resend_timer_inner(); + + struct timespec alarm_resend_time = { ALARM_RESEND_TIMER_LENGTH, 0 }; + /* only start resend time when send failed */ + g_omc_hf_alm_state.resend_timer = + nsfw_timer_reg_timer(MASTER_RESEND_HOTFIX_TIMER, NULL, + omc_send_hotfix_alarm_inner, + alarm_resend_time); + if (NULL == g_omc_hf_alm_state.resend_timer) + { + NSFW_LOGERR("start hotfix alarm resend timer fail"); + } + } + + return 0; + +} + +/***************************************************************************** +* Prototype : omc_send_hotfix_alarm +* Description : called by hotfix module +* Input : void +* Output : None +* Return Value : void +* Calls : +* Called By : +*****************************************************************************/ +void omc_send_hotfix_alarm(int proc_type, int action, int result, + const char *version) +{ + g_omc_hf_alm_state.res.proc_type = (fw_poc_type) proc_type; + g_omc_hf_alm_state.res.action = (hotfix_optype) action; + g_omc_hf_alm_state.res.result = result; + int ret = strcpy_s(g_omc_hf_alm_state.res.patch_version, + sizeof(g_omc_hf_alm_state.res.patch_version), + version); + if (EOK != ret) + { + NSFW_LOGERR("strcpy_s failed]ret=%d", ret); + } + + (void) omc_send_hotfix_alarm_inner(); +} + +/* API for another module to get the global */ +u32 get_ps_exiting_cnt() +{ + return g_ps_exiting_cnt; +} + +/***************************************************************************** +* Prototype : inc_ps_exiting_cnt +* Description : g_ps_exiting_cnt++ +* Input : None +* Output : None +* Return Value : None +* Calls : +* Called By : nsfw_sw_ps_state( ,NSFW_PS_EXITING) ONLY! +*****************************************************************************/ +NSTACK_STATIC void inc_ps_exiting_cnt() +{ + if (g_ps_exiting_cnt < NSFW_MAX_PID) + { + g_ps_exiting_cnt++; + } + else + { + NSFW_LOGERR("g_ps_exiting_cnt would exceed NSFW_MAX_PID! now is %u", + g_ps_exiting_cnt); + } +} + +/***************************************************************************** +* Prototype : dec_ps_exiting_cnt +* Description : g_ps_exiting_cnt-- +* Input : None +* Output : None +* Return Value : None +* Calls : +* Called By : nsfw_sw_ps_state( ,NSFW_PS_EXIT) ONLY! +*****************************************************************************/ +NSTACK_STATIC void dec_ps_exiting_cnt() +{ + if (g_ps_exiting_cnt > 0) + { + g_ps_exiting_cnt--; + } + else + { + NSFW_LOGERR("g_ps_exiting_cnt would drop below zero! now is %u", + g_ps_exiting_cnt); + } +} + /***************************************************************************** * Prototype : nsfw_ps_reg_fun * Description : reg the callback fun when process state change @@ -71,34 +518,33 @@ void *g_ps_chk_timer = NULL; * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_reg_fun (nsfw_ps_info * pps_info, u8 ps_state, - nsfw_ps_proc_fun fun, void *argv) +u8 nsfw_ps_reg_fun(nsfw_ps_info * pps_info, u8 ps_state, + nsfw_ps_proc_fun fun, void *argv) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("reg pps_info nul]state=%d,fun=%p", ps_state, fun); - return FALSE; + NSFW_LOGERR("reg pps_info nul]state=%d,fun=%p", ps_state, fun); + return FALSE; } - u32 i; - for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) + u32 i; + for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) { - if (NULL == pps_info->callback[i].fun) + if (NULL == pps_info->callback[i].fun) { - pps_info->callback[i].fun = fun; - pps_info->callback[i].argv = argv; - pps_info->callback[i].state = ps_state; - NSFW_LOGDBG - ("reg fun suc]ps_info=%p,state=%d,fun=%p,argv=%p,i=%d", - pps_info, ps_state, fun, argv, i); - return TRUE; + pps_info->callback[i].fun = fun; + pps_info->callback[i].argv = argv; + pps_info->callback[i].state = ps_state; + NSFW_LOGDBG + ("reg fun suc]ps_info=%p,state=%d,fun=%p,argv=%p,i=%u", + pps_info, ps_state, fun, argv, i); + return TRUE; } } - NSFW_LOGERR ("reg fun failed]ps_info=%p,state=%d,fun=%p,argv=%p,i=%d", - pps_info, ps_state, fun, argv, i); - return FALSE; + NSFW_LOGERR("reg fun failed]ps_info=%p,state=%d,fun=%p,argv=%p,i=%u", + pps_info, ps_state, fun, argv, i); + return FALSE; } /***************************************************************************** @@ -114,35 +560,34 @@ nsfw_ps_reg_fun (nsfw_ps_info * pps_info, u8 ps_state, * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_reg_global_fun (u8 proc_type, u8 ps_state, nsfw_ps_proc_fun fun, - void *argv) +u8 nsfw_ps_reg_global_fun(u8 proc_type, u8 ps_state, nsfw_ps_proc_fun fun, + void *argv) { - if (NSFW_PROC_MAX <= proc_type) + if (NSFW_PROC_MAX <= proc_type) { - NSFW_LOGERR ("proc_type err]state=%u,fun=%p,type=%u", ps_state, fun, - proc_type); - return FALSE; + NSFW_LOGERR("proc_type err]state=%u,fun=%p,type=%u", ps_state, fun, + proc_type); + return FALSE; } - nsfw_ps_callback *cb_fun = g_ps_init_callback[proc_type]; - u32 i; - for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) + nsfw_ps_callback *cb_fun = g_ps_init_callback[proc_type]; + u32 i; + for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) { - if (NULL == cb_fun[i].fun) + if (NULL == cb_fun[i].fun) { - cb_fun[i].fun = fun; - cb_fun[i].argv = argv; - cb_fun[i].state = ps_state; - NSFW_LOGINF ("reg fun suc]type=%u,state=%u,fun=%p,argv=%p,i=%u", - proc_type, ps_state, fun, argv, i); - return TRUE; + cb_fun[i].fun = fun; + cb_fun[i].argv = argv; + cb_fun[i].state = ps_state; + NSFW_LOGINF("reg fun suc]type=%u,state=%u,fun=%p,argv=%p,i=%u", + proc_type, ps_state, fun, argv, i); + return TRUE; } } - NSFW_LOGERR ("reg fun ful failed]type=%u,state=%u,fun=%p,argv=%p,i=%u", - proc_type, ps_state, fun, argv, i); - return FALSE; + NSFW_LOGERR("reg fun ful failed]type=%u,state=%u,fun=%p,argv=%p,i=%u", + proc_type, ps_state, fun, argv, i); + return FALSE; } /***************************************************************************** @@ -155,28 +600,27 @@ nsfw_ps_reg_global_fun (u8 proc_type, u8 ps_state, nsfw_ps_proc_fun fun, * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_exit (nsfw_mgr_msg * msg) +int nsfw_ps_exit(nsfw_mgr_msg * msg) { - nsfw_ps_info *pps_info; - if (NULL == msg) + nsfw_ps_info *pps_info; + if (NULL == msg) { - NSFW_LOGERR ("msg nul!"); - return FALSE; + NSFW_LOGERR("msg nul!"); + return FALSE; } - nsfw_ps_info_msg *ps_msg = GET_USER_MSG (nsfw_ps_info_msg, msg); - pps_info = nsfw_ps_info_get (ps_msg->host_pid); - NSFW_LOGINF ("recv ps exit]host_pid=%d,ps_info=%p", ps_msg->host_pid, - pps_info); + nsfw_ps_info_msg *ps_msg = GET_USER_MSG(nsfw_ps_info_msg, msg); + pps_info = nsfw_ps_info_get(ps_msg->host_pid); + NSFW_LOGINF("recv ps exit]host_pid=%u,ps_info=%p", ps_msg->host_pid, + pps_info); - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("error msg pps_info nul]host_pid=%d", ps_msg->host_pid); - return true; + NSFW_LOGERR("error msg pps_info nul]host_pid=%u", ps_msg->host_pid); + return true; } - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_EXIT); - return TRUE; + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_EXIT); + return TRUE; } /***************************************************************************** @@ -189,60 +633,60 @@ nsfw_ps_exit (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -nsfw_ps_info * -nsfw_ps_info_fork_alloc (u32 parent_pid, u32 child_pid) +nsfw_ps_info *nsfw_ps_info_fork_alloc(u32 parent_pid, u32 child_pid) { - nsfw_ps_info *pps_info = NULL; - nsfw_ps_info *pps_info_parent = NULL; - pps_info_parent = nsfw_ps_info_get (parent_pid); - if (NULL == pps_info_parent) + nsfw_ps_info *pps_info = NULL; + nsfw_ps_info *pps_info_parent = NULL; + pps_info_parent = nsfw_ps_info_get(parent_pid); + if (NULL == pps_info_parent) { - NSFW_LOGERR ("pps_info_parent nul"); - return NULL; + NSFW_LOGERR("pps_info_parent nul"); + return NULL; } - pps_info = nsfw_ps_info_get (child_pid); - if (NULL == pps_info) + pps_info = nsfw_ps_info_get(child_pid); + if (NULL == pps_info) { - pps_info = nsfw_ps_info_alloc (child_pid, pps_info_parent->proc_type); - if (NULL == pps_info) + pps_info = + nsfw_ps_info_alloc(child_pid, pps_info_parent->proc_type, 0); + if (NULL == pps_info) { - NSFW_LOGERR - ("alloc ps_info failed!]ps_info=%p,host_pid=%u,child_pid=%u", - pps_info_parent, pps_info_parent->host_pid, child_pid); - return NULL; + NSFW_LOGERR + ("alloc ps_info failed!]ps_info=%p,host_pid=%u,child_pid=%u", + pps_info_parent, pps_info_parent->host_pid, child_pid); + return NULL; } } - else + else { - NSFW_LOGWAR - ("fork alloc mem before!]ps_info=%p,host_pid=%u,child_pid=%u", - pps_info_parent, pps_info_parent->host_pid, child_pid); + NSFW_LOGWAR + ("fork alloc mem before!]ps_info=%p,host_pid=%u,child_pid=%u", + pps_info_parent, pps_info_parent->host_pid, child_pid); } - NSFW_LOGWAR ("ps_info fork]ps_info=%p,host_pid=%u,child_pid=%u", - pps_info_parent, pps_info_parent->host_pid, child_pid); - pps_info->parent_pid = parent_pid; - if (EOK != - MEMCPY_S (pps_info->callback, sizeof (pps_info->callback), - pps_info_parent->callback, - sizeof (pps_info_parent->callback))) + NSFW_LOGWAR("ps_info fork]ps_info=%p,host_pid=%u,child_pid=%u", + pps_info_parent, pps_info_parent->host_pid, child_pid); + pps_info->parent_pid = parent_pid; + if (EOK != + memcpy_s(pps_info->callback, sizeof(pps_info->callback), + pps_info_parent->callback, + sizeof(pps_info_parent->callback))) { - nsfw_ps_info_free (pps_info); - NSFW_LOGERR ("ps_info set_failed"); - return NULL; + nsfw_ps_info_free(pps_info); + NSFW_LOGERR("ps_info set_failed"); + return NULL; } - if (EOK != - MEMCPY_S (pps_info->value, sizeof (pps_info->value), - pps_info_parent->value, sizeof (pps_info_parent->value))) + if (EOK != + memcpy_s(pps_info->value, sizeof(pps_info->value), + pps_info_parent->value, sizeof(pps_info_parent->value))) { - nsfw_ps_info_free (pps_info); - NSFW_LOGERR ("ps_info cpy_failed"); - return NULL; + nsfw_ps_info_free(pps_info); + NSFW_LOGERR("ps_info cpy_failed"); + return NULL; } - return pps_info; + return pps_info; } /***************************************************************************** @@ -255,64 +699,71 @@ nsfw_ps_info_fork_alloc (u32 parent_pid, u32 child_pid) * Calls : * Called By : *****************************************************************************/ -nsfw_ps_info * -nsfw_ps_info_alloc (u32 pid, u8 proc_type) +nsfw_ps_info *nsfw_ps_info_alloc(u32 pid, u8 proc_type, u8 realloc_flg) { - nsfw_ps_info *pps_info = NULL; - if (0 == nsfw_mem_ring_dequeue (g_ps_cfg.ps_info_pool, (void *) &pps_info)) + nsfw_ps_info *pps_info = NULL; + if (0 == nsfw_mem_ring_dequeue(g_ps_cfg.ps_info_pool, (void *) &pps_info)) { - NSFW_LOGERR ("alloc ps_info failed]pid=%u,type=%u", pid, proc_type); - return NULL; + NSFW_LOGERR("alloc ps_info falied]pid=%u,type=%u", pid, proc_type); + return NULL; } - if (NULL == pps_info) + if (NULL == pps_info) { - if (NSFW_PROC_MAIN != proc_type || TRUE == g_main_ps_info.alloc_flag) + if (NSFW_PROC_MAIN != proc_type || TRUE == g_main_ps_info.alloc_flag) { - NSFW_LOGERR ("alloc ps_info nul]pid=%u,type=%u", pid, proc_type); - return NULL; + NSFW_LOGERR("alloc ps_info nul]pid=%u,type=%u", pid, proc_type); + return NULL; } - pps_info = &g_main_ps_info; + pps_info = &g_main_ps_info; } - if (EOK != - MEMSET_S (pps_info, sizeof (nsfw_ps_info), 0, sizeof (nsfw_ps_info))) + if (EOK != + memset_s(pps_info, sizeof(nsfw_ps_info), 0, sizeof(nsfw_ps_info))) { - nsfw_ps_info_free (pps_info); - NSFW_LOGERR ("set failed"); - return NULL; + NSFW_LOGERR("memset_s failed"); + goto error; } - pps_info->proc_type = proc_type; - pps_info->host_pid = pid; - - if (proc_type < NSFW_PROC_MAX) + if (proc_type < NSFW_PROC_MAX) { - int retval; - retval = - MEMCPY_S (pps_info->callback, sizeof (pps_info->callback), - g_ps_init_callback[proc_type], sizeof (pps_info->callback)); - if (EOK != retval) + int retval = memcpy_s(pps_info->callback, sizeof(pps_info->callback), + g_ps_init_callback[proc_type], + sizeof(pps_info->callback)); + if (EOK != retval) { - NSFW_LOGERR ("Failed to MEMCPY_S]retval=%d", retval); - nsfw_ps_info_free (pps_info); - return NULL; + NSFW_LOGERR("memcpy failed]retval=%d", retval); + goto error; } } - list_add_tail (&pps_info->node, &g_ps_runing_list); - pps_info->alloc_flag = TRUE; + pps_info->proc_type = proc_type; + pps_info->host_pid = pid; + + list_add_tail(&pps_info->node, &g_ps_runing_list); - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_RUNNING); - if (pid < NSFW_MAX_PID) + pps_info->alloc_flag = TRUE; + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_RUNNING); + + if (pid < NSFW_MAX_PID) { - g_ps_info[pid].ps_info = pps_info; - g_ps_info[pid].proc_type = proc_type; + g_ps_info[pid].ps_info = pps_info; + if (!realloc_flg) /* If re-alloced, should keep the same as stored */ + { + g_ps_info[pid].proc_type = proc_type; + g_ps_info[pid].starttime = + (u32) (nsfw_ps_get_starttime_from_file(pid) & 0xFFFFFFFF); + } } - NSFW_LOGINF ("ps_info alloc]ps_info=%p,pid=%u,type=%u", pps_info, pid, - proc_type); - return pps_info; - + NSFW_LOGINF("ps_info alloc]ps_info=%p,pid=%u,type=%u", pps_info, pid, + proc_type); + return pps_info; + error: + if (1 != nsfw_mem_ring_enqueue(g_ps_cfg.ps_info_pool, pps_info)) + { + NSFW_LOGERR("nsfw_mem_ring_enqueue failed]ps_info=%p", pps_info); + } + return NULL; } /***************************************************************************** @@ -324,94 +775,83 @@ nsfw_ps_info_alloc (u32 pid, u8 proc_type) * Calls : * Called By : *****************************************************************************/ -inline nsfw_ps_info * -nsfw_ps_info_get (u32 pid) +inline nsfw_ps_info *nsfw_ps_info_get(u32 pid) { - if (pid < NSFW_MAX_PID) + if (pid < NSFW_MAX_PID) { - return g_ps_info[pid].ps_info; + return g_ps_info[pid].ps_info; } - return NULL; + return NULL; } -nsfw_ps_info * -nsfw_share_ps_info_get (u32 pid) +nsfw_ps_info *nsfw_share_ps_info_get(u32 pid) { - if (pid < NSFW_MAX_PID) + if (pid < NSFW_MAX_PID) { - return g_master_ps_info[pid].ps_info; + return g_master_ps_info[pid].ps_info; } - return NULL; + return NULL; } -/***************************************************************************** -* Prototype : nsfw_ps_info_free -* Description : free ps_info -* Input : nsfw_ps_info *ps_info -* Output : None -* Return Value : void -* Calls : -* Called By : -*****************************************************************************/ -void -nsfw_ps_info_free (nsfw_ps_info * ps_info) +void nsfw_ps_info_free(nsfw_ps_info * ps_info) { - if (NULL == ps_info) + if (NULL == ps_info) { - NSFW_LOGERR ("ps_info nul"); - return; + NSFW_LOGERR("ps_info nul"); + return; } - if (FALSE == ps_info->alloc_flag) + if (FALSE == ps_info->alloc_flag) { - NSFW_LOGERR ("ps_info refree]ps_info=%p,pid=%u,state=%u", ps_info, - ps_info->host_pid, ps_info->state); - return; + NSFW_LOGERR("ps_info refree]ps_info=%p,pid=%u,state=%u", ps_info, + ps_info->host_pid, ps_info->state); + return; } - if (NULL != ps_info->exit_timer_ptr) + if (NULL != ps_info->exit_timer_ptr) { - nsfw_timer_rmv_timer ((nsfw_timer_info *) ps_info->exit_timer_ptr); - ps_info->exit_timer_ptr = NULL; + nsfw_timer_rmv_timer((nsfw_timer_info *) ps_info->exit_timer_ptr); + ps_info->exit_timer_ptr = NULL; } - if (NULL != ps_info->resend_timer_ptr) + if (NULL != ps_info->resend_timer_ptr) { - nsfw_timer_rmv_timer ((nsfw_timer_info *) ps_info->resend_timer_ptr); - ps_info->resend_timer_ptr = NULL; + nsfw_timer_rmv_timer((nsfw_timer_info *) ps_info->resend_timer_ptr); + ps_info->resend_timer_ptr = NULL; } - if (NULL != ps_info->hbt_timer_ptr) + if (NULL != ps_info->hbt_timer_ptr) { - nsfw_timer_rmv_timer ((nsfw_timer_info *) ps_info->hbt_timer_ptr); - ps_info->hbt_timer_ptr = NULL; + nsfw_timer_rmv_timer((nsfw_timer_info *) ps_info->hbt_timer_ptr); + ps_info->hbt_timer_ptr = NULL; } - list_del (&ps_info->node); + list_del(&ps_info->node); - ps_info->alloc_flag = FALSE; + ps_info->alloc_flag = FALSE; - NSFW_LOGINF ("ps_info free]ps_info=%p,pid=%u,state=%u", ps_info, - ps_info->host_pid, ps_info->state); - if (ps_info != &g_main_ps_info) + NSFW_LOGINF("ps_info free]ps_info=%p,pid=%u,state=%u", ps_info, + ps_info->host_pid, ps_info->state); + if (ps_info != &g_main_ps_info) { - if (0 == nsfw_mem_ring_enqueue (g_ps_cfg.ps_info_pool, ps_info)) + if (0 == nsfw_mem_ring_enqueue(g_ps_cfg.ps_info_pool, ps_info)) { - NSFW_LOGERR ("ps_info free failed]ps_info=%p,pid=%u,state=%u", - ps_info, ps_info->host_pid, ps_info->state); - return; + NSFW_LOGERR("ps_info free failed]ps_info=%p,pid=%u,state=%u", + ps_info, ps_info->host_pid, ps_info->state); + return; } } - if (ps_info->host_pid < NSFW_MAX_PID) + if (ps_info->host_pid < NSFW_MAX_PID) { - g_ps_info[ps_info->host_pid].proc_type = 0; - g_ps_info[ps_info->host_pid].ps_info = NULL; + g_ps_info[ps_info->host_pid].proc_type = 0; + g_ps_info[ps_info->host_pid].ps_info = NULL; + g_ps_info[ps_info->host_pid].starttime = 0; } - return; + return; } /***************************************************************************** @@ -424,35 +864,35 @@ nsfw_ps_info_free (nsfw_ps_info * ps_info) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_exiting_timeout (u32 timer_type, void *data) +int nsfw_ps_exiting_timeout(u32 timer_type, void *data) { - nsfw_ps_info *pps_info = (nsfw_ps_info *) data; - NSFW_LOGINF ("ps_info timerout]pps_info=%p,pid=%u", pps_info, - pps_info->host_pid); - if (NULL == pps_info) + nsfw_ps_info *pps_info = (nsfw_ps_info *) data; + + if (NULL == pps_info) { - return TRUE; + return TRUE; } + NSFW_LOGINF("ps_info timerout]pps_info=%p,pid=%u", pps_info, + pps_info->host_pid); - pps_info->exit_timer_ptr = NULL; + pps_info->exit_timer_ptr = NULL; - if (TRUE == g_hbt_switch) + if (TRUE == g_hbt_switch) { - NSFW_LOGINF ("hbt off"); - struct timespec time_left = { NSFW_PS_WEXIT_TVLAUE, 0 }; - pps_info->exit_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_PS_WEXIT_TIMER, pps_info, - nsfw_ps_exiting_timeout, time_left); - return TRUE; + NSFW_LOGINF("hbt off"); + struct timespec time_left = { NSFW_PS_WEXIT_TVLAUE, 0 }; + pps_info->exit_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_PS_WEXIT_TIMER, pps_info, + nsfw_ps_exiting_timeout, time_left); + return TRUE; } - if (NSFW_PS_EXITING == pps_info->state) + if (NSFW_PS_EXITING == pps_info->state) { - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_EXIT); + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_EXIT); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -464,23 +904,22 @@ nsfw_ps_exiting_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_exit_end_notify (u32 pid) +u8 nsfw_ps_exit_end_notify(u32 pid) { - nsfw_mgr_msg *rsp_msg = - nsfw_mgr_msg_alloc (MGR_MSG_APP_EXIT_RSP, NSFW_PROC_MAIN); - if (NULL == rsp_msg) + nsfw_mgr_msg *rsp_msg = + nsfw_mgr_msg_alloc(MGR_MSG_APP_EXIT_RSP, g_mgr_com_cfg.proc_type); + if (NULL == rsp_msg) { - NSFW_LOGERR ("alloc rsp msg failed]pid=%u", pid); - return FALSE; + NSFW_LOGERR("alloc rsp msg failed]pid=%u", pid); + return FALSE; } - nsfw_ps_info_msg *ps_msg = GET_USER_MSG (nsfw_ps_info_msg, rsp_msg); - ps_msg->host_pid = pid; - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); - NSFW_LOGINF ("send exit rsp msg]pid=%u", pid); - return TRUE; + nsfw_ps_info_msg *ps_msg = GET_USER_MSG(nsfw_ps_info_msg, rsp_msg); + ps_msg->host_pid = pid; + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); + NSFW_LOGINF("send exit rsp msg]pid=%u", pid); + return TRUE; } /***************************************************************************** @@ -493,57 +932,63 @@ nsfw_ps_exit_end_notify (u32 pid) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_sw_ps_state (nsfw_ps_info * pps_info, u8 new_state) +u8 nsfw_sw_ps_state(nsfw_ps_info * pps_info, u8 new_state) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("pps_info nul!"); - return FALSE; + NSFW_LOGERR("pps_info nul!"); + return FALSE; } - NSFW_LOGINF ("sw]ps_info=%p,pid=%u,type=%u,old_state=%u,newstate=%u", - pps_info, pps_info->host_pid, pps_info->proc_type, - pps_info->state, new_state); + u8 old_state = pps_info->state; + NSFW_LOGINF("sw]ps_info=%p,pid=%u,type=%u,old_state=%u,newstate=%u", + pps_info, pps_info->host_pid, pps_info->proc_type, old_state, + new_state); + + if (NSFW_PS_HBT_FAILED != new_state) + { + pps_info->state = new_state; + } - i32 i, ret; - for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) + i32 i, ret; + for (i = 0; i < NSFW_PS_MAX_CALLBACK; i++) { - if (NULL == pps_info->callback[i].fun) + if (NULL == pps_info->callback[i].fun) { - /* NULL should be the last fun */ - break; + /* NULL should be the last fun */ + break; } - if (new_state == pps_info->callback[i].state) + if (new_state == pps_info->callback[i].state) { - ret = - pps_info->callback[i].fun (pps_info, pps_info->callback[i].argv); - NSFW_LOGINF ("callback fun]ps_info=%p,i=%d,fun=%p,argv=%p,ret=%d", - pps_info, i, pps_info->callback[i].fun, - pps_info->callback[i].argv, ret); + ret = + pps_info->callback[i].fun(pps_info, + pps_info->callback[i].argv); + NSFW_LOGINF("callback fun]ps_info=%p,i=%d,fun=%p,argv=%p,ret=%d", + pps_info, i, pps_info->callback[i].fun, + pps_info->callback[i].argv, ret); } } - if (NSFW_PS_HBT_FAILED != new_state) + if (NSFW_PS_EXIT == new_state) { - pps_info->state = new_state; - } - - if (NSFW_PS_EXIT == new_state) - { - nsfw_ps_info_free (pps_info); + nsfw_ps_info_free(pps_info); + if (NSFW_PS_EXITING == old_state) + { + dec_ps_exiting_cnt(); + } } - if (NSFW_PS_EXITING == new_state) + if (NSFW_PS_EXITING == new_state) { - struct timespec time_left = { NSFW_PS_WEXIT_TVLAUE, 0 }; - pps_info->exit_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_PS_WEXIT_TIMER, pps_info, - nsfw_ps_exiting_timeout, time_left); + struct timespec time_left = { NSFW_PS_WEXIT_TVLAUE, 0 }; + pps_info->exit_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_PS_WEXIT_TIMER, pps_info, + nsfw_ps_exiting_timeout, time_left); + inc_ps_exiting_cnt(); } - return TRUE; + return TRUE; } /***************************************************************************** @@ -555,108 +1000,112 @@ nsfw_sw_ps_state (nsfw_ps_info * pps_info, u8 new_state) * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_ps_get_netlink_socket () +i32 nsfw_ps_get_netlink_socket() { - int rc; - int nl_sock; - int size, size_len; - struct sockaddr_nl sa_nl; + int rc; + int nl_sock; + int size, size_len; + struct sockaddr_nl sockad_nl; - nl_sock = nsfw_base_socket (PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); - if (nl_sock == -1) + nl_sock = nsfw_base_socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR); + if (nl_sock == -1) { - NSFW_LOGERR ("get netlink socket err]errno=%d", errno); - return -1; + NSFW_LOGERR("get netlink socket err]errno=%d", errno); + return -1; } - rc = nsfw_set_close_on_exec (nl_sock); - if (rc == -1) + /* close on exec */ + rc = nsfw_set_close_on_exec(nl_sock); + if (rc == -1) { - (void) nsfw_base_close (nl_sock); - NSFW_LOGERR ("set exec err]fd=%d, errno=%d", nl_sock, errno); - return -1; + (void) nsfw_base_close(nl_sock); + NSFW_LOGERR("set exec err]fd=%d, errno=%d", nl_sock, errno); + return -1; } - sa_nl.nl_family = AF_NETLINK; - sa_nl.nl_groups = CN_IDX_PROC; - sa_nl.nl_pid = getpid (); + sockad_nl.nl_family = AF_NETLINK; + sockad_nl.nl_groups = CN_IDX_PROC; + sockad_nl.nl_pid = getpid(); - rc = nsfw_base_bind (nl_sock, (struct sockaddr *) &sa_nl, sizeof (sa_nl)); - if (rc == -1) + rc = nsfw_base_bind(nl_sock, (struct sockaddr *) &sockad_nl, + sizeof(sockad_nl)); + if (rc == -1) { - (void) nsfw_base_close (nl_sock); - NSFW_LOGERR ("netlink bind err]netlink_fd=%d, errno=%d", nl_sock, - errno); - return -1; + (void) nsfw_base_close(nl_sock); + NSFW_LOGERR("netlink bind err]netlink_fd=%d, errno=%d", nl_sock, + errno); + return -1; } - struct __attribute__ ((aligned (NLMSG_ALIGNTO))) - { - struct nlmsghdr nl_hdr; - struct __attribute__ ((__packed__)) + /* this struct will be use */ + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { - struct cn_msg cn_msg; - enum proc_cn_mcast_op cn_mcast; - }; - } nlcn_msg; - if (EOK != MEMSET_S (&nlcn_msg, sizeof (nlcn_msg), 0, sizeof (nlcn_msg))) + struct nlmsghdr nl_msg_hdr; + struct __attribute__ ((__packed__)) + { + struct cn_msg c_msg; + enum proc_cn_mcast_op c_mcast; + }; + } nlc_msg; + + if (EOK != memset_s(&nlc_msg, sizeof(nlc_msg), 0, sizeof(nlc_msg))) { - (void) nsfw_base_close (nl_sock); - NSFW_LOGERR ("netlink set failed]netlink_fd=%d", nl_sock); - return -1; + (void) nsfw_base_close(nl_sock); + NSFW_LOGERR("netlink set failed]netlink_fd=%d", nl_sock); + return -1; } - nlcn_msg.nl_hdr.nlmsg_len = sizeof (nlcn_msg); - nlcn_msg.nl_hdr.nlmsg_pid = getpid (); - nlcn_msg.nl_hdr.nlmsg_type = NLMSG_DONE; + nlc_msg.nl_msg_hdr.nlmsg_len = sizeof(nlc_msg); + nlc_msg.nl_msg_hdr.nlmsg_pid = getpid(); + nlc_msg.nl_msg_hdr.nlmsg_type = NLMSG_DONE; - nlcn_msg.cn_msg.id.idx = CN_IDX_PROC; - nlcn_msg.cn_msg.id.val = CN_VAL_PROC; - nlcn_msg.cn_msg.len = sizeof (enum proc_cn_mcast_op); + nlc_msg.c_msg.id.idx = CN_IDX_PROC; + nlc_msg.c_msg.id.val = CN_VAL_PROC; + nlc_msg.c_msg.len = sizeof(enum proc_cn_mcast_op); - nlcn_msg.cn_mcast = PROC_CN_MCAST_LISTEN; - rc = nsfw_base_send (nl_sock, &nlcn_msg, sizeof (nlcn_msg), 0); - if (rc == -1) + nlc_msg.c_mcast = PROC_CN_MCAST_LISTEN; + rc = nsfw_base_send(nl_sock, &nlc_msg, sizeof(nlc_msg), 0); + if (rc == -1) { - (void) nsfw_base_close (nl_sock); - NSFW_LOGERR ("netlink send err]netlink_fd=%d, errno=%d", nl_sock, - errno); - return -1; + (void) nsfw_base_close(nl_sock); + NSFW_LOGERR("netlink send err]netlink_fd=%d, errno=%d", nl_sock, + errno); + return -1; } - NSFW_LOGINF ("netlink connect]netlink_fd=%d", nl_sock); - int val, len; - len = sizeof (val); - if (0 > - nsfw_base_getsockopt (nl_sock, SOL_SOCKET, SO_RCVBUF, &val, - (socklen_t *) & len)) + NSFW_LOGINF("netlink connect]netlink_fd=%d", nl_sock); + + int val = 0; + int len = sizeof(val); + if (0 > + nsfw_base_getsockopt(nl_sock, SOL_SOCKET, SO_RCVBUF, &val, + (socklen_t *) & len)) { - NSFW_LOGERR ("get socket opt err!]error=%d", errno); + NSFW_LOGERR("get socket opt err]error=%d", errno); } - else + else { - NSFW_LOGINF ("] SO_RCVBUF=0x%x", val); + NSFW_LOGINF("]SO_RCVBUF=0x%x", val); } - size = MAX_NET_LINK_BUF; - size_len = sizeof (size); - if (0 > - nsfw_base_setsockopt (nl_sock, SOL_SOCKET, SO_RCVBUFFORCE, - (void *) &size, (socklen_t) size_len)) + size = MAX_NET_LINK_BUF; + size_len = sizeof(size); + if (0 > + nsfw_base_setsockopt(nl_sock, SOL_SOCKET, SO_RCVBUFFORCE, + (void *) &size, (socklen_t) size_len)) { - NSFW_LOGERR ("set socket opt err!]error=%d", errno); + NSFW_LOGERR("set socket opt err]error=%d", errno); } - size_len = sizeof (size); - if (0 > - nsfw_base_getsockopt (nl_sock, SOL_SOCKET, SO_RCVBUF, (void *) &size, - (socklen_t *) & size_len)) + size_len = sizeof(size); + if (0 > + nsfw_base_getsockopt(nl_sock, SOL_SOCKET, SO_RCVBUF, (void *) &size, + (socklen_t *) & size_len)) { - NSFW_LOGERR ("get socket opt err!]error=%d", errno); + NSFW_LOGERR("get socket opt err]error=%d", errno); } - NSFW_LOGINF ("] SO_RCVBUF=0x%x", size); - return nl_sock; + NSFW_LOGINF("]SO_RCVBUF=0x%x", size); + return nl_sock; } /***************************************************************************** @@ -670,81 +1119,81 @@ nsfw_ps_get_netlink_socket () * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_change_fun (i32 epfd, i32 fd, u32 events) +int nsfw_ps_change_fun(i32 epfd, i32 fd, u32 events) { - i32 rc; - u32 host_pid; - nsfw_ps_info *pps_info = NULL; + i32 rc; + u32 host_pid; + nsfw_ps_info *pps_info = NULL; - struct __attribute__ ((aligned (NLMSG_ALIGNTO))) - { - struct nlmsghdr nl_hdr; - struct __attribute__ ((__packed__)) + /*this struct will be used */ + struct __attribute__ ((aligned(NLMSG_ALIGNTO))) { - struct cn_msg cn_msg; - struct proc_event proc_ev; - }; - } nlcn_msg; + struct nlmsghdr nl_msg_hdr; + struct __attribute__ ((__packed__)) + { + struct cn_msg c_msg; + struct proc_event p_ev; + }; + } nlc_msg; - if (!(events & EPOLLIN)) + if (!(events & EPOLLIN)) { - return TRUE; + return TRUE; } - while (1) + while (1) { - rc = nsfw_base_recv (fd, &nlcn_msg, sizeof (nlcn_msg), 0); - if (rc == 0) + rc = nsfw_base_recv(fd, &nlc_msg, sizeof(nlc_msg), 0); + if (rc == 0) { - NSFW_LOGWAR ("netlink recv 0]netlink_fd=%d,errno=%d", fd, errno); - break; + NSFW_LOGWAR("netlink recv 0]netlink_fd=%d,errno=%d", fd, errno); + break; } - else if (rc == -1) + else if (rc == -1) { - if (errno == EINTR || errno == EAGAIN) + if (errno == EINTR || errno == EAGAIN) { - break; + break; } - NSMON_LOGERR ("netlink recv]netlink_fd=%d,errno=%d", fd, errno); - if (errno == ENOBUFS) + NSMON_LOGERR("netlink recv]netlink_fd=%d,errno=%d", fd, errno); + if (errno == ENOBUFS) { - struct timespec time_left = { NSFW_PS_FIRST_CHK_TVLAUE, 0 }; - g_ps_chk_timer = - (void *) nsfw_timer_reg_timer (NSFW_PS_CHK_TIMER, - (void *) FALSE, - nsfw_ps_chk_timeout, - time_left); + struct timespec time_left = { NSFW_PS_FIRST_CHK_TVLAUE, 0 }; + g_ps_chk_timer = + (void *) nsfw_timer_reg_timer(NSFW_PS_CHK_TIMER, + (void *) FALSE, + nsfw_ps_chk_timeout, + time_left); } - break; + break; } - switch (nlcn_msg.proc_ev.what) + switch (nlc_msg.p_ev.what) { - case PROC_EVENT_EXIT: - host_pid = nlcn_msg.proc_ev.event_data.exit.process_pid; - pps_info = nsfw_ps_info_get (host_pid); - if (NULL == pps_info) - { - NSFW_LOGDBG ("pps info is null]host pid=%d", host_pid); - break; - } - - if (NSFW_PS_EXITING == pps_info->state) - { - NSFW_LOGERR ("double pid info]ps_info=%p,pid=%d", pps_info, - host_pid); - break; - } - - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_EXITING); - break; - default: - break; + case PROC_EVENT_EXIT: /*enum or int no effect here */ + host_pid = nlc_msg.p_ev.event_data.exit.process_pid; + pps_info = nsfw_ps_info_get(host_pid); + if (NULL == pps_info) + { + /*too many useless debug log, del NSFW_LOGDBG("pps info is null]host pid=%u", host_pid); */ + break; + } + + if (NSFW_PS_EXITING == pps_info->state) + { + NSFW_LOGERR("double pid info]ps_info=%p,pid=%u", + pps_info, host_pid); + break; + } + + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_EXITING); + break; + default: + break; } } - return TRUE; + return TRUE; } /***************************************************************************** @@ -755,22 +1204,21 @@ nsfw_ps_change_fun (i32 epfd, i32 fd, u32 events) * Return Value : u8 * Calls : * Called By : -* *****************************************************************************/ -u8 -nsfw_ps_start_netlink () +u8 nsfw_ps_start_netlink() { - i32 netlink_fd = nsfw_ps_get_netlink_socket (); - if (netlink_fd < 0) + + i32 netlink_fd = nsfw_ps_get_netlink_socket(); + if (netlink_fd < 0) { - NSFW_LOGERR ("get netlink failed!"); - return FALSE; + NSFW_LOGERR("get netlink failed!"); + return FALSE; } - NSFW_LOGINF ("start ps_info module!]netlink_fd=%d", netlink_fd); - (void) nsfw_mgr_reg_sock_fun (netlink_fd, nsfw_ps_change_fun); - (void) nsfw_mgr_reg_msg_fun (MGR_MSG_APP_EXIT_RSP, nsfw_ps_exit); - return TRUE; + NSFW_LOGINF("start ps_info module!]netlink_fd=%d", netlink_fd); + (void) nsfw_mgr_reg_sock_fun(netlink_fd, nsfw_ps_change_fun); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_APP_EXIT_RSP, nsfw_ps_exit); + return TRUE; } /* for heartbeat check*/ @@ -786,26 +1234,32 @@ nsfw_thread_dogs g_thread_dogs[NSFW_MAX_THREAD_DOGS_COUNT]; * Calls : * Called By : *****************************************************************************/ -i32 -nsfw_all_thread_chk () +i32 nsfw_all_thread_chk() { - i32 count = -1; - u32 i; - for (i = 0; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) + i32 count = -1; + u32 i; + for (i = 0; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) { - if (FALSE == g_thread_dogs[i].alloc_flag) + if (FALSE == g_thread_dogs[i].alloc_flag) { - continue; + continue; } - if (count < g_thread_dogs[i].count) + if (count < g_thread_dogs[i].count) { - count = g_thread_dogs[i].count; + count = g_thread_dogs[i].count; } - g_thread_dogs[i].count++; + /* only if the thread abnormal, then output log */ + if (g_thread_dogs[i].count > 0) + { /* print thread_id, know which thread is abnormal */ + NSFW_LOGINF("hb_timeout]count=%d,thread_id=%u", + g_thread_dogs[i].count, g_thread_dogs[i].thread_id); + } + + g_thread_dogs[i].count++; } - return count; + return count; } /***************************************************************************** @@ -817,17 +1271,16 @@ nsfw_all_thread_chk () * Calls : * Called By : *****************************************************************************/ -inline u8 -nsfw_thread_chk_unreg () +inline u8 nsfw_thread_chk_unreg() { - if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) + if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) { - g_thread_dogs[t_val_idx].alloc_flag = FALSE; - g_thread_dogs[t_val_idx].count = 0; - g_thread_dogs[t_val_idx].thread_id = 0; - t_val_idx = 0; + g_thread_dogs[t_val_idx].alloc_flag = FALSE; + g_thread_dogs[t_val_idx].count = 0; + g_thread_dogs[t_val_idx].thread_id = 0; + t_val_idx = 0; } - return TRUE; + return TRUE; } /***************************************************************************** @@ -840,86 +1293,86 @@ nsfw_thread_chk_unreg () * Calls : * Called By : *****************************************************************************/ -inline u8 -nsfw_thread_chk () +inline u8 nsfw_thread_chk() { - u32 i; - if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) + u32 i; + if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) { - g_thread_dogs[t_val_idx].count = 0; - return TRUE; + g_thread_dogs[t_val_idx].count = 0; + return TRUE; } - for (i = 1; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) + for (i = 1; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) { - if ((FALSE == g_thread_dogs[i].alloc_flag) - && __sync_bool_compare_and_swap (&g_thread_dogs[i].alloc_flag, - FALSE, TRUE)) + /* + (1) use some fixed value but no effect (e506) + (2) it don't contain any extra commas (e505) + */ + if ((FALSE == g_thread_dogs[i].alloc_flag) + && __sync_bool_compare_and_swap(&g_thread_dogs[i].alloc_flag, + FALSE, TRUE)) { - t_val_idx = i; - break; + t_val_idx = i; + break; } } - if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) + if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) { - g_thread_dogs[t_val_idx].count = 0; - g_thread_dogs[t_val_idx].thread_id = syscall (SYS_gettid); + g_thread_dogs[t_val_idx].count = 0; + g_thread_dogs[t_val_idx].thread_id = syscall(SYS_gettid); } - return TRUE; + return TRUE; } -/***************************************************************** -Parameters : None -Return : -Description : -*****************************************************************/ -nsfw_thread_dogs * -nsfw_thread_getDog () +nsfw_thread_dogs *nsfw_thread_get_dog() { - u32 i; - nsfw_thread_dogs *retPtr = NULL; - if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) + u32 i; + nsfw_thread_dogs *retPtr = NULL; + if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) { - return &g_thread_dogs[t_val_idx]; + return &g_thread_dogs[t_val_idx]; } - for (i = 1; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) + for (i = 1; i < NSFW_MAX_THREAD_DOGS_COUNT; i++) { - if ((FALSE == g_thread_dogs[i].alloc_flag) - && __sync_bool_compare_and_swap (&g_thread_dogs[i].alloc_flag, - FALSE, TRUE)) + /* + (1) use some fixed value but no effect (e506) + (2) it don't contain any extra commas (e505) + */ + if ((FALSE == g_thread_dogs[i].alloc_flag) + && __sync_bool_compare_and_swap(&g_thread_dogs[i].alloc_flag, + FALSE, TRUE)) { - t_val_idx = i; - break; + t_val_idx = i; + break; } } - if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) + if (t_val_idx > 0 && t_val_idx < NSFW_MAX_THREAD_DOGS_COUNT) { - g_thread_dogs[t_val_idx].count = 0; - g_thread_dogs[t_val_idx].thread_id = syscall (SYS_gettid); - retPtr = &g_thread_dogs[t_val_idx]; + g_thread_dogs[t_val_idx].count = 0; + g_thread_dogs[t_val_idx].thread_id = syscall(SYS_gettid); + retPtr = &g_thread_dogs[t_val_idx]; } - return retPtr; + return retPtr; } pthread_t g_all_thread[MAX_THREAD] = { 0 }; -u8 -nsfw_reg_trace_thread (pthread_t tid) +u8 nsfw_reg_trace_thread(pthread_t tid) { - int i; - for (i = 0; i < MAX_THREAD; i++) + int i; + for (i = 0; i < MAX_THREAD; i++) { - if ((0 == g_all_thread[i]) - && __sync_bool_compare_and_swap (&g_all_thread[i], 0, tid)) + if ((0 == g_all_thread[i]) + && __sync_bool_compare_and_swap(&g_all_thread[i], 0, tid)) { - return TRUE; + return TRUE; } } - return FALSE; + return FALSE; } /***************************************************************************** @@ -931,75 +1384,73 @@ nsfw_reg_trace_thread (pthread_t tid) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_check_dst_init (u8 dst_proc_type) +u8 nsfw_ps_check_dst_init(u8 dst_proc_type) { - u8 ps_state = FALSE; - nsfw_mgr_msg *msg = - nsfw_mgr_msg_alloc (MGR_MSG_CHK_INIT_REQ, dst_proc_type); - if (NULL == msg) + u8 ps_state = FALSE; + nsfw_mgr_msg *msg = + nsfw_mgr_msg_alloc(MGR_MSG_CHK_INIT_REQ, dst_proc_type); + if (NULL == msg) { - NSFW_LOGERR ("alloc msg failed]dst_typ=%d", dst_proc_type); - return FALSE; + NSFW_LOGERR("alloc msg failed]dst_typ=%d", dst_proc_type); + return FALSE; } - nsfw_ps_chk_msg *ps_msg = GET_USER_MSG (nsfw_ps_chk_msg, msg); - ps_msg->ps_state = TRUE; + nsfw_ps_chk_msg *ps_msg = GET_USER_MSG(nsfw_ps_chk_msg, msg); + ps_msg->ps_state = TRUE; - nsfw_mgr_msg *rsp_msg = nsfw_mgr_null_rspmsg_alloc (); - if (NULL == rsp_msg) + nsfw_mgr_msg *rsp_msg = nsfw_mgr_null_rspmsg_alloc(); + if (NULL == rsp_msg) { - nsfw_mgr_msg_free (msg); - NSFW_LOGERR ("alloc rsp msg failed]dst_typ=%d", dst_proc_type); - return FALSE; + nsfw_mgr_msg_free(msg); + NSFW_LOGERR("alloc rsp msg failed]dst_typ=%d", dst_proc_type); + return FALSE; } - (void) nsfw_mgr_send_req_wait_rsp (msg, rsp_msg); + (void) nsfw_mgr_send_req_wait_rsp(msg, rsp_msg); - ps_msg = GET_USER_MSG (nsfw_ps_chk_msg, rsp_msg); - ps_state = ps_msg->ps_state; + ps_msg = GET_USER_MSG(nsfw_ps_chk_msg, rsp_msg); + ps_state = ps_msg->ps_state; - nsfw_mgr_msg_free (msg); - nsfw_mgr_msg_free (rsp_msg); - NSFW_LOGINF ("get peer state]dst_type=%d,state=%d", dst_proc_type, - ps_state); - return ps_state; + nsfw_mgr_msg_free(msg); + nsfw_mgr_msg_free(rsp_msg); + NSFW_LOGINF("get peer state]dst_type=%d,state=%d", dst_proc_type, + ps_state); + return ps_state; } /***************************************************************************** * Prototype : nsfw_ps_send_hbt -* Description : send heart beat message to peer +* Description : seng heart beat message to peer * Input : nsfw_ps_info* pps_info * Output : None * Return Value : u8 * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_send_hbt (nsfw_ps_info * pps_info) +u8 nsfw_ps_send_hbt(nsfw_ps_info * pps_info) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("null ps_info!"); - return FALSE; + NSFW_LOGERR("null ps_info!"); + return FALSE; } - nsfw_mgr_msg *req_msg = - nsfw_mgr_msg_alloc (MGR_MSG_CHK_HBT_REQ, pps_info->proc_type); - if (NULL == req_msg) + nsfw_mgr_msg *req_msg = + nsfw_mgr_msg_alloc(MGR_MSG_CHK_HBT_REQ, pps_info->proc_type); + if (NULL == req_msg) { - NSFW_LOGERR ("alloc req msg failed]pps_info=%p", pps_info); - return FALSE; + NSFW_LOGERR("alloc req msg failed]pps_info=%p", pps_info); + return FALSE; } - req_msg->dst_pid = pps_info->host_pid; - nsfw_ps_chk_msg *ps_msg = GET_USER_MSG (nsfw_ps_chk_msg, req_msg); - ps_msg->ps_state = TRUE; - u8 ret = nsfw_mgr_send_msg (req_msg); - nsfw_mgr_msg_free (req_msg); - NSFW_LOGDBG ("send hbt msg]ret=%d,pps_info=%p,pid=%d,type=%d", ret, - pps_info, pps_info->host_pid, pps_info->proc_type); - return ret; + req_msg->dst_pid = pps_info->host_pid; + nsfw_ps_chk_msg *ps_msg = GET_USER_MSG(nsfw_ps_chk_msg, req_msg); + ps_msg->ps_state = TRUE; + u8 ret = nsfw_mgr_send_msg(req_msg); + nsfw_mgr_msg_free(req_msg); + NSFW_LOGDBG("send hbt msg]ret=%d,pps_info=%p,pid=%u,type=%d", ret, + pps_info, pps_info->host_pid, pps_info->proc_type); + return ret; } /***************************************************************************** @@ -1011,29 +1462,51 @@ nsfw_ps_send_hbt (nsfw_ps_info * pps_info) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_recv_hbt (nsfw_mgr_msg * msg) +int nsfw_ps_recv_hbt(nsfw_mgr_msg * msg) { - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("error msg nul!"); - return FALSE; + NSFW_LOGERR("error msg nul!"); + return FALSE; } - nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg); - if (NULL == rsp_msg) + nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc(msg); + if (NULL == rsp_msg) { - NSFW_LOGERR ("alloc rsp failed,drop msg!" MSGINFO, PRTMSG (msg)); - return FALSE; + NSFW_LOGERR("alloc rsp failed,drop msg!" MSGINFO, PRTMSG(msg)); + return FALSE; } - nsfw_ps_chk_msg *ps_msg = GET_USER_MSG (nsfw_ps_chk_msg, rsp_msg); - ps_msg->ps_state = TRUE; - ps_msg->thread_chk_count = nsfw_all_thread_chk (); - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); + nsfw_ps_chk_msg *ps_msg = GET_USER_MSG(nsfw_ps_chk_msg, rsp_msg); + ps_msg->ps_state = TRUE; + ps_msg->thread_chk_count = nsfw_all_thread_chk(); + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); - return TRUE; + return TRUE; +} + +void log_sys_process_info(void) +{ + /* get thread info which is running in 0 core cpu */ +#define GET_SYS_PROCESS_STATUS_INFO "ps c -w -T -e -O user,s,spid,%cpu,%mem,psr,start --sort -pcpu |grep -E 'PSR| R ' |grep -E 'PSR| 0 | 1 '" + /* check cpu usage of all thread for daemon-stack process, this cmd running need spend 180ms */ +#define GET_NSTACK_MAIN_PROCESS_STATUS_INFO "top -d 1 -H -p `pidof nStackMain` -n 1 -b" + char result[SPL_MAX_BUF_LEN] = { 0 }; + int retVal = + nsfw_mgr_run_script(GET_NSTACK_MAIN_PROCESS_STATUS_INFO, result, + SPL_MAX_BUF_LEN); + if (-1 != retVal) + { + NSFW_LOGWAR("NSTACK_MAIN=%d\n%s", retVal, result); + } + retVal = + nsfw_mgr_run_script(GET_SYS_PROCESS_STATUS_INFO, result, + SPL_MAX_BUF_LEN); + if (-1 != retVal) + { + NSFW_LOGWAR("SYS=%d\n%s", retVal, result); + } } /***************************************************************************** @@ -1045,66 +1518,75 @@ nsfw_ps_recv_hbt (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_recv_hbt_rsp (nsfw_mgr_msg * msg) +int nsfw_ps_recv_hbt_rsp(nsfw_mgr_msg * msg) { - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("error msg nul!"); - return FALSE; + NSFW_LOGERR("error msg nul!"); + return FALSE; } - nsfw_ps_chk_msg *ps_msg = GET_USER_MSG (nsfw_ps_chk_msg, msg); - if (TRUE != ps_msg->ps_state) + nsfw_ps_chk_msg *ps_msg = GET_USER_MSG(nsfw_ps_chk_msg, msg); + if (TRUE != ps_msg->ps_state) { - NSFW_LOGERR ("Heartbeat failed]pid=%u,type=%u", msg->src_pid, - msg->src_proc_type); - return FALSE; + NSFW_LOGERR("Heartbeat failed]pid=%u,type=%u", msg->src_pid, + msg->src_proc_type); + return FALSE; } - nsfw_ps_info *pps_info = nsfw_ps_info_get (msg->src_pid); - if (NULL == pps_info) + nsfw_ps_info *pps_info = nsfw_ps_info_get(msg->src_pid); + if (NULL == pps_info) { - NSFW_LOGERR ("get ps_info failed]pid=%u,type=%u,count=%d", - msg->src_pid, msg->src_proc_type, - ps_msg->thread_chk_count); - return FALSE; + NSFW_LOGERR("get ps_info failed]pid=%u,type=%u,count=%d", + msg->src_pid, msg->src_proc_type, + ps_msg->thread_chk_count); + return FALSE; } - if (0 == ps_msg->thread_chk_count) + if (0 == ps_msg->thread_chk_count) { - pps_info->hbt_failed_count = 0; - return TRUE; + pps_info->hbt_failed_count = 0; + + /* send daemon-stack normal alarm, when hb normal one time */ + omc_send_nsmain_alarm(ALARM_EVENT_NSTACK_MAIN_ABNORMAL_RESTART_FAIL, + ALARM_CLEAN); + return TRUE; } - if (pps_info->hbt_failed_count > (u32) ps_msg->thread_chk_count) + u32 tmp_hbt_failed_count = pps_info->hbt_failed_count; + + if (pps_info->hbt_failed_count > (u32) ps_msg->thread_chk_count) { - pps_info->hbt_failed_count = (u32) ps_msg->thread_chk_count; + pps_info->hbt_failed_count = (u32) ps_msg->thread_chk_count; } - NSFW_LOGERR ("Heartbeat failed]pid=%u,type=%u,count=%d,ps_count=%u", - msg->src_pid, msg->src_proc_type, ps_msg->thread_chk_count, - pps_info->hbt_failed_count); - return FALSE; + NSFW_LOGERR("Heartbeat failed]pid=%u,type=%u,count=%d,ps_count=%u", + msg->src_pid, msg->src_proc_type, ps_msg->thread_chk_count, + pps_info->hbt_failed_count); + /* for fast get sys process status info */ + if (1 == tmp_hbt_failed_count) + { + log_sys_process_info(); + } + return FALSE; } -int -nsfw_ps_reset_hbt (void *pps_info, void *argv) +int nsfw_ps_reset_hbt(void *pps_info, void *argv) { - nsfw_ps_info *ps_info = pps_info; - if (NULL == ps_info) + nsfw_ps_info *ps_info = pps_info; + if (NULL == ps_info) { - NSFW_LOGERR ("ps_info nul!"); - return FALSE; + NSFW_LOGERR("ps_info nul!"); + return FALSE; } - if (NSFW_PROC_MAIN != ps_info->proc_type) + if (NSFW_PROC_MAIN != ps_info->proc_type) { - return FALSE; + return FALSE; } - ps_info->hbt_failed_count = *(u32 *) argv; - return TRUE; + ps_info->hbt_failed_count = *(u32 *) argv; + return TRUE; } /***************************************************************************** @@ -1117,69 +1599,70 @@ nsfw_ps_reset_hbt (void *pps_info, void *argv) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_hbt_timeout (u32 timer_type, void *data) +int nsfw_ps_hbt_timeout(u32 timer_type, void *data) { - nsfw_ps_info *pps_info = (nsfw_ps_info *) data; + nsfw_ps_info *pps_info = (nsfw_ps_info *) data; - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("null ps_info!"); - return FALSE; + NSFW_LOGERR("null ps_info!"); + return FALSE; } - if (NULL == pps_info->hbt_timer_ptr) + if (NULL == pps_info->hbt_timer_ptr) { - NSFW_LOGERR ("hbt has stop]pps_info=%p", pps_info); - pps_info->hbt_failed_count = 0; - return TRUE; + NSFW_LOGERR("hbt has stop]pps_info=%p", pps_info); + pps_info->hbt_failed_count = 0; + return TRUE; } - if (TRUE == g_hbt_switch) + if (TRUE == g_hbt_switch) { - struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; - pps_info->hbt_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_CHK_HBT_TIMER, data, - nsfw_ps_hbt_timeout, time_left); - return TRUE; + struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; + pps_info->hbt_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_CHK_HBT_TIMER, data, + nsfw_ps_hbt_timeout, time_left); + return TRUE; } - /* nic init may cost a few seconds, master will restart main if heartbeat timeout */ - if (NSFW_SOFT_HBT_CHK_COUNT != NSFW_MAX_HBT_CHK_COUNT) + /* nic init may cost a few seconds, master will restart main if heartbeat timeout- Begin */ + if (NSFW_SOFT_HBT_CHK_COUNT != NSFW_MAX_HBT_CHK_COUNT) { - if (NSFW_SOFT_HBT_CHK_COUNT < NSFW_MAX_HBT_CHK_COUNT) + if (NSFW_SOFT_HBT_CHK_COUNT < NSFW_MAX_HBT_CHK_COUNT) { - u32 new_hbt_count = 0; - (void) nsfw_ps_iterator (nsfw_ps_reset_hbt, &new_hbt_count); + u32 new_hbt_count = 0; + (void) nsfw_ps_iterator(nsfw_ps_reset_hbt, &new_hbt_count); } - NSFW_MAX_HBT_CHK_COUNT = NSFW_SOFT_HBT_CHK_COUNT; + NSFW_MAX_HBT_CHK_COUNT = NSFW_SOFT_HBT_CHK_COUNT; } - if (NSFW_MAX_HBT_CHK_COUNT <= pps_info->hbt_failed_count) + if (NSFW_MAX_HBT_CHK_COUNT <= pps_info->hbt_failed_count) { - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_HBT_FAILED); - /*reset counter */ - pps_info->hbt_failed_count = 0; + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_HBT_FAILED); + /*reset counter */ + pps_info->hbt_failed_count = 0; } - if (TRUE != nsfw_ps_send_hbt (pps_info)) + if (TRUE != nsfw_ps_send_hbt(pps_info)) { } - if (pps_info->hbt_failed_count > 0) + struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; + pps_info->hbt_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_CHK_HBT_TIMER, data, + nsfw_ps_hbt_timeout, time_left); + + if (pps_info->hbt_failed_count > 0) { - NSFW_LOGWAR ("Heartbeat failed]pid=%u,ps_count=%u, max_count=%d", - pps_info->host_pid, pps_info->hbt_failed_count, - NSFW_MAX_HBT_CHK_COUNT); + NSFW_LOGWAR("Heartbeat failed]pid=%u,ps_count=%u, max_count=%d", + pps_info->host_pid, pps_info->hbt_failed_count, + NSFW_MAX_HBT_CHK_COUNT); + log_sys_process_info(); } - pps_info->hbt_failed_count++; - struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; - pps_info->hbt_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_CHK_HBT_TIMER, data, - nsfw_ps_hbt_timeout, time_left); - return TRUE; + pps_info->hbt_failed_count++; + return TRUE; } /***************************************************************************** @@ -1191,29 +1674,28 @@ nsfw_ps_hbt_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_hbt_start (nsfw_ps_info * pps_info) +u8 nsfw_ps_hbt_start(nsfw_ps_info * pps_info) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("null ps_info!"); - return FALSE; + NSFW_LOGERR("null ps_info"); + return FALSE; } - if (NULL != pps_info->hbt_timer_ptr) + if (NULL != pps_info->hbt_timer_ptr) { - NSFW_LOGERR ("hbt start before!]ps_info=%p,pid=%u,type=%u", pps_info, - pps_info->host_pid, pps_info->proc_type); - return FALSE; + NSFW_LOGERR("hbt start before]ps_info=%p,pid=%u,type=%u", pps_info, + pps_info->host_pid, pps_info->proc_type); + return FALSE; } - struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; - pps_info->hbt_timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_CHK_HBT_TIMER, (void *) pps_info, - nsfw_ps_hbt_timeout, time_left); - NSFW_LOGINF ("hbt start!]ps_info=%p,pid=%u,type=%u", pps_info, - pps_info->host_pid, pps_info->proc_type); - return TRUE; + struct timespec time_left = { NSFW_CHK_HBT_TVLAUE, 0 }; + pps_info->hbt_timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_CHK_HBT_TIMER, (void *) pps_info, + nsfw_ps_hbt_timeout, time_left); + NSFW_LOGINF("hbt start]ps_info=%p,pid=%u,type=%u", pps_info, + pps_info->host_pid, pps_info->proc_type); + return TRUE; } /***************************************************************************** @@ -1225,24 +1707,23 @@ nsfw_ps_hbt_start (nsfw_ps_info * pps_info) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_ps_hbt_stop (nsfw_ps_info * pps_info) +u8 nsfw_ps_hbt_stop(nsfw_ps_info * pps_info) { - if (NULL == pps_info) + if (NULL == pps_info) { - NSFW_LOGERR ("null ps_info!"); - return FALSE; + NSFW_LOGERR("null ps_info!"); + return FALSE; } - if (NULL != pps_info->hbt_timer_ptr) + if (NULL != pps_info->hbt_timer_ptr) { - nsfw_timer_rmv_timer ((nsfw_timer_info *) pps_info->hbt_timer_ptr); - pps_info->hbt_timer_ptr = NULL; + nsfw_timer_rmv_timer((nsfw_timer_info *) pps_info->hbt_timer_ptr); + pps_info->hbt_timer_ptr = NULL; } - NSFW_LOGINF ("hbt stop!]ps_info=%p,pid=%d,type=%d", pps_info, - pps_info->host_pid, pps_info->proc_type); - return TRUE; + NSFW_LOGINF("hbt stop!]ps_info=%p,pid=%u,type=%d", pps_info, + pps_info->host_pid, pps_info->proc_type); + return TRUE; } /***************************************************************************** @@ -1255,33 +1736,31 @@ nsfw_ps_hbt_stop (nsfw_ps_info * pps_info) * Calls : * Called By : *****************************************************************************/ -u32 -nsfw_ps_iterator (nsfw_ps_proc_fun fun, void *argv) +u32 nsfw_ps_iterator(nsfw_ps_proc_fun fun, void *argv) { - u32 count = 0; - nsfw_ps_info *pps_info = NULL; - struct list_head *tNode; + u32 count = 0; + nsfw_ps_info *pps_info = NULL; + struct list_head *tNode; - if (NULL == fun) + if (NULL == fun) { - NSFW_LOGERR ("fun null!"); - return count; + NSFW_LOGERR("fun null!"); + return count; } - LINT_LIST ()list_for_each_entry (pps_info, tNode, (&g_ps_runing_list), node) - { - (void) fun (pps_info, argv); - count++; - } + list_for_each_entry(pps_info, tNode, (&g_ps_runing_list), node) + { + (void) fun(pps_info, argv); + count++; + } - NSFW_LOGINF ("proc pid]count=%u", count); - return count; + NSFW_LOGINF("proc pid]count=%u", count); + return count; } -int -filter (const struct dirent *dir) +int filter(const struct dirent *dir) { - return !fnmatch ("[1-9]*", dir->d_name, 0); + return !fnmatch("[1-9]*", dir->d_name, 0); } /***************************************************************************** @@ -1293,37 +1772,36 @@ filter (const struct dirent *dir) * Calls : * Called By : *****************************************************************************/ -inline nsfw_ps_info * -nsfw_ps_realloc_pid (u32 pid, u8 realloc_flg) +inline nsfw_ps_info *nsfw_ps_realloc_pid(u32 pid, u8 realloc_flg) { - nsfw_ps_info *pps_info = NULL; - if (pid >= NSFW_MAX_PID) + nsfw_ps_info *pps_info = NULL; + if (pid >= NSFW_MAX_PID) { - return NULL; + return NULL; } - if (g_ps_info[pid].ps_info == NULL) + if (g_ps_info[pid].ps_info == NULL) { - return NULL; + return NULL; } - if (TRUE == realloc_flg) + if (TRUE == realloc_flg) { - pps_info = nsfw_ps_info_alloc (pid, g_ps_info[pid].proc_type); - if (NULL == pps_info) + pps_info = nsfw_ps_info_alloc(pid, g_ps_info[pid].proc_type, 1); + if (NULL == pps_info) { - NSFW_LOGERR ("alloc ps_info failed!]pid=%u,type=%u", pid, - g_ps_info[pid].proc_type); - return NULL; + NSFW_LOGERR("alloc ps_info failed!]pid=%u,type=%u", pid, + g_ps_info[pid].proc_type); + return NULL; } } - else + else { - pps_info = g_ps_info[pid].ps_info; + pps_info = g_ps_info[pid].ps_info; } - pps_info->rechk_flg = TRUE; - return pps_info; + pps_info->rechk_flg = TRUE; + return pps_info; } /***************************************************************************** @@ -1335,34 +1813,37 @@ nsfw_ps_realloc_pid (u32 pid, u8 realloc_flg) * Calls : * Called By : *****************************************************************************/ -void -nsfw_ps_reload_pid () +void nsfw_ps_reload_pid() { - struct dirent **namelist; - i32 n; - u32 host_pid; - nsfw_ps_info *pps_info = NULL; + struct dirent **namelist; + i32 n; + u32 host_pid; + nsfw_ps_info *pps_info = NULL; - n = scandir ("/proc", &namelist, filter, 0); - if (n < 0) + n = scandir("/proc", &namelist, filter, 0); + if (n < 0) { - NSFW_LOGERR ("buf null"); + NSFW_LOGERR("buf null"); } - else + else { - while (n--) + + while (n--) { - host_pid = strtol (namelist[n]->d_name, NULL, 10); - pps_info = nsfw_ps_info_get (host_pid); - if (NULL != pps_info) + host_pid = strtol(namelist[n]->d_name, NULL, 10); + pps_info = nsfw_ps_info_get(host_pid); + if ((NULL != pps_info) && /* There is a nStack app with this pid, and */ + (nsfw_ps_get_starttime_from_file(host_pid) == nsfw_ps_get_starttime_from_info(host_pid) /* starttime is the same */ + || 0 == nsfw_ps_get_starttime_from_info(host_pid)) /* or NA in earlier version of Master */ + ) { - pps_info->rechk_flg = FALSE; + pps_info->rechk_flg = FALSE; } - free (namelist[n]); + free(namelist[n]); } - free (namelist); + free(namelist); } - return; + return; } /***************************************************************************** @@ -1374,19 +1855,18 @@ nsfw_ps_reload_pid () * Calls : * Called By : *****************************************************************************/ -void -nsfw_ps_realloc_all_pid (u32 * main_pid, u8 realloc_flg) +void nsfw_ps_realloc_all_pid(u32 * main_pid, u8 realloc_flg) { - u32 i; - nsfw_ps_info *pps_info = NULL; - for (i = 0; i < NSFW_MAX_PID; i++) + u32 i; + nsfw_ps_info *pps_info = NULL; + for (i = 0; i < NSFW_MAX_PID; i++) { - pps_info = nsfw_ps_realloc_pid (i, realloc_flg); - if (NULL != main_pid) + pps_info = nsfw_ps_realloc_pid(i, realloc_flg); + if (NULL != main_pid) { - if (NULL != pps_info && NSFW_PROC_MAIN == pps_info->proc_type) + if (NULL != pps_info && NSFW_PROC_MAIN == pps_info->proc_type) { - (*main_pid) = i; + (*main_pid) = i; } } } @@ -1402,49 +1882,48 @@ nsfw_ps_realloc_all_pid (u32 * main_pid, u8 realloc_flg) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_chk_exit_timeout (u32 timer_type, void *data) +int nsfw_ps_chk_exit_timeout(u32 timer_type, void *data) { - u32 i; - nsfw_ps_info *pps_info = NULL; - u32 pid = (u64) data; + u32 i; + nsfw_ps_info *pps_info = NULL; + u32 pid = (u64) data; - /*main pid exit first */ - if (NULL != data) + /*main pid exit first */ + if (NULL != data) { - pps_info = nsfw_ps_info_get (pid); - if (NULL != pps_info && TRUE == pps_info->rechk_flg) + pps_info = nsfw_ps_info_get(pid); + if (NULL != pps_info && TRUE == pps_info->rechk_flg) { - if (NSFW_PS_EXITING != pps_info->state) + if (NSFW_PS_EXITING != pps_info->state) { - NSFW_LOGWAR ("rechk pid exit]ps_info=%p,pid=%u", pps_info, - pps_info->host_pid); - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_EXITING); - pps_info->rechk_flg = FALSE; + NSFW_LOGWAR("rechk pid exit]ps_info=%p,pid=%u", pps_info, + pps_info->host_pid); + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_EXITING); + pps_info->rechk_flg = FALSE; } } } - for (i = 0; i < NSFW_MAX_PID; i++) + for (i = 0; i < NSFW_MAX_PID; i++) { - pps_info = nsfw_ps_info_get (i); - if (NULL != pps_info && TRUE == pps_info->rechk_flg) + pps_info = nsfw_ps_info_get(i); + if (NULL != pps_info && TRUE == pps_info->rechk_flg) { - if (NSFW_PS_EXITING == pps_info->state) + if (NSFW_PS_EXITING == pps_info->state) { - NSFW_LOGWAR ("double pid info]ps_info=%p,pid=%u", pps_info, - pps_info->host_pid); - continue; + NSFW_LOGWAR("double pid info]ps_info=%p,pid=%u", pps_info, + pps_info->host_pid); + continue; } - NSFW_LOGWAR ("rechk pid exit]ps_info=%p,pid=%u", pps_info, - pps_info->host_pid); - (void) nsfw_sw_ps_state (pps_info, NSFW_PS_EXITING); - pps_info->rechk_flg = FALSE; + NSFW_LOGWAR("rechk pid exit]ps_info=%p,pid=%u", pps_info, + pps_info->host_pid); + (void) nsfw_sw_ps_state(pps_info, NSFW_PS_EXITING); + pps_info->rechk_flg = FALSE; } } - return TRUE; + return TRUE; } /***************************************************************************** @@ -1456,27 +1935,44 @@ nsfw_ps_chk_exit_timeout (u32 timer_type, void *data) * Return Value : int * Calls : * Called By : -****************************************************************************/ -int -nsfw_ps_chk_timeout (u32 timer_type, void *data) +*****************************************************************************/ +int nsfw_ps_chk_timeout(u32 timer_type, void *data) { - u32 main_pid = 0; - u8 realloc_flg = (u8) (u64) data; - - nsfw_ps_realloc_all_pid (&main_pid, realloc_flg); - nsfw_ps_reload_pid (); - - struct timespec time_left = { NSFW_PS_CHK_EXIT_TVLAUE, 0 }; - g_ps_chk_timer = - (void *) nsfw_timer_reg_timer (NSFW_PS_CHK_EXIT_TVLAUE, - (void *) (u64) main_pid, - nsfw_ps_chk_exit_timeout, time_left); - return TRUE; + u32 main_pid = 0; + u8 realloc_flg = (u8) (u64) data; + + nsfw_ps_realloc_all_pid(&main_pid, realloc_flg); + nsfw_ps_reload_pid(); + + struct timespec time_left = { NSFW_PS_CHK_EXIT_TVLAUE, 0 }; + g_ps_chk_timer = + (void *) nsfw_timer_reg_timer(NSFW_PS_CHK_EXIT_TVLAUE, + (void *) (u64) main_pid, + nsfw_ps_chk_exit_timeout, time_left); + return TRUE; +} + +int nsfw_ps_check_pid_nstack_status(int pid) +{ + + if (pid < 0 || pid >= NSFW_MAX_PID) + { + NSFW_LOGWAR("get wrong pid :%d", pid); + return -1; + } + + if (NULL == g_master_ps_info[pid].ps_info) + { + return -1; + } + + /* pid is using nstack */ + return 0; } /***************************************************************************** * Prototype : nsfw_ps_rechk_pid_exit -* Description : recheck pid exit +* Description : rechck pid exit * Input : nsfw_ps_proc_fun fun * void* argv * Output : None @@ -1484,203 +1980,329 @@ nsfw_ps_chk_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -int -nsfw_ps_rechk_pid_exit (nsfw_ps_pid_fun fun, void *argv) +int nsfw_ps_rechk_pid_exit(nsfw_ps_pid_fun fun, void *argv) { - u32 ulI = 0; - if (NULL == fun) + + u32 ulI = 0; + if (NULL == fun) { - NSFW_LOGERR ("input err! fun null"); - return -1; + NSFW_LOGERR("input err! fun null"); + return -1; } - u8 *ps_pid = malloc (NSFW_MAX_PID); - if (NULL == ps_pid) + u8 *ps_pid = malloc(NSFW_MAX_PID); + if (NULL == ps_pid) { - NSFW_LOGERR ("malloc failed"); - return -1; + NSFW_LOGERR("malloc failed"); + return -1; } - int retval = MEMSET_S (ps_pid, NSFW_MAX_PID, 0, NSFW_MAX_PID); - if (EOK != retval) + int retval = memset_s(ps_pid, NSFW_MAX_PID, 0, NSFW_MAX_PID); + if (EOK != retval) { - NSFW_LOGERR ("MEMSET_S failed]retval=%d", retval); - free (ps_pid); - return -1; + NSFW_LOGERR("memset_s failed]retval=%d", retval); + free(ps_pid); + return -1; } - struct dirent **namelist; - i32 n; - u32 host_pid; - n = scandir ("/proc", &namelist, filter, 0); - if (n < 0) + struct dirent **namelist; + i32 n; + u32 host_pid; + n = scandir("/proc", &namelist, filter, 0); + if (n < 0) { - NSFW_LOGERR ("buf null"); - free (ps_pid); - return -1; + NSFW_LOGERR("buf null"); + free(ps_pid); + return -1; } - while (n--) + while (n--) { - host_pid = strtol (namelist[n]->d_name, NULL, 10); - if (host_pid < NSFW_MAX_PID) + host_pid = strtol(namelist[n]->d_name, NULL, 10); + /* here should use host_pid as ps_pid's index */ + if (host_pid < NSFW_MAX_PID) { - ps_pid[ulI] = TRUE; + ps_pid[host_pid] = TRUE; } - free (namelist[n]); + free(namelist[n]); + namelist[n] = NULL; } - free (namelist); + free(namelist); - int count = 0; - for (ulI = 0; ulI < NSFW_MAX_PID; ulI++) + int count = 0; + for (ulI = 0; ulI < NSFW_MAX_PID; ulI++) { - if ((NULL != g_master_ps_info[ulI].ps_info) && (FALSE == ps_pid[ulI])) + if ((NULL != g_master_ps_info[ulI].ps_info) && (FALSE == ps_pid[ulI])) { - (void) fun (ulI, g_master_ps_info[ulI].proc_type, argv); - NSFW_LOGWAR ("rechk pid exit]pid=%d,type=%d", ulI, - g_master_ps_info[ulI].proc_type); - count++; - continue; + (void) fun(ulI, g_master_ps_info[ulI].proc_type, argv); + NSFW_LOGWAR("rechk pid exit]ulI=%u,type=%d", ulI, + g_master_ps_info[ulI].proc_type); + count++; + continue; } } - free (ps_pid); - return count; + free(ps_pid); + return count; } -void -nsfw_ps_cfg_set_chk_count (u16 count) +void nsfw_ps_cfg_set_chk_count(u16 count) { - g_ps_cfg.ps_chk_hbt_count = count; + g_ps_cfg.ps_chk_hbt_count = count; } -/***************************************************************************** -* Prototype : nsfw_ps_module_init -* Description : ps_module init -* Input : void* param -* Output : None -* Return Value : static int -* Calls : -* Called By : -*****************************************************************************/ -int -nsfw_ps_module_init (void *param) +u64 nsfw_ps_get_starttime_from_file(u32 pid) { - u32 proc_type = (u32) ((long long) param); - nsfw_ps_init_cfg *ps_cfg = &g_ps_cfg; - int retval; - nsfw_pid_item *pid_info = NULL; - NSFW_LOGINF ("ps module init]type=%u", proc_type); - - ps_cfg->ps_chk_hbt_count = NSFW_MAX_HBT_CHK_COUNT_DEF; - ps_cfg->ps_chk_hbt_tvalue = NSFW_CHK_HBT_TVLAUE_DEF; - ps_cfg->ps_chk_hbt_soft_count = ps_cfg->ps_chk_hbt_count; - - nsfw_mem_zone pzoneinfo; - pzoneinfo.isocket_id = NSFW_SOCKET_ANY; - pzoneinfo.stname.entype = NSFW_SHMEM; - pzoneinfo.length = sizeof (nsfw_pid_item) * NSFW_MAX_PID; - if (-1 == - SPRINTF_S (pzoneinfo.stname.aname, NSFW_MEM_NAME_LENGTH, "%s", - "MAS_PS_INFO")) - { - NSFW_LOGERR ("SPRINTF_S failed]"); - return -1; - } - - switch (proc_type) - { - case NSFW_PROC_MAIN: - { - pid_info = malloc (sizeof (nsfw_pid_item) * NSFW_MAX_PID); - if (NULL == pid_info) - { - NSFW_LOGERR ("malloc mem failed!"); - return -1; - } - - retval = - MEMSET_S (pid_info, (sizeof (nsfw_pid_item) * NSFW_MAX_PID), 0, - (sizeof (nsfw_pid_item) * NSFW_MAX_PID)); - if (EOK != retval) - { - NSFW_LOGERR ("MEMSET_S failed]retval=%d.\n", retval); - free (pid_info); - return -1; - } + if (pid > NSFW_MAX_PID) + { + NSFW_LOGERR("Invalid pid=%u", pid); + return 0; + } + + char str[20]; + if (sprintf_s(str, sizeof(str), "/proc/%u/stat", pid) < 0) + { + NSFW_LOGERR("sprintf_s failed, pid=%u", pid); + return 0; + } + + FILE *psfile = fopen(str, "r"); + if (NULL == psfile) + { + NSFW_LOGERR("fopen /proc/%u/stat failed", pid); + return 0; + } + + u64 starttime = 0; + const char PROC_PID_STAT_FORMAT_GET_STARTTIME_ONLY[] = "%*d " /* 1. pid */ + "%*s " /* 2. filename of exe */ + "%*c " /* 3. state */ + "%*d " /* 4. ppid */ + "%*d " /* 5. process group */ + "%*d " /* 6. session ID */ + "%*d " /* 7. tty_nr */ + "%*d " /* 8. tpgid */ + "%*u " /* 9. flags */ + "%*u " /* 10. minflt */ + "%*u " /* 11. cminflt */ + "%*u " /* 12. majflt */ + "%*u " /* 13. cmajflt */ + "%*u " /* 14. utime */ + "%*u " /* 15. stime */ + "%*d " /* 16. cuttime */ + "%*d " /* 17. cstime */ + "%*d " /* 18. priority */ + "%*d " /* 19. nice */ + "%*d " /* 20. num_threads */ + "%*d " /* 21. NA since Linux 2.6 */ + "%llu"; /* 22. starttime, we need this field */ + + int ret; + if ((ret = + fscanf_s(psfile, PROC_PID_STAT_FORMAT_GET_STARTTIME_ONLY, + &starttime)) != 1) + { + NSFW_LOGERR("fscanf_s /proc/%u/stat failed, ret=%d, errno=%d", pid, + ret, errno); + fclose(psfile); /*no need to check return */ + return 0; + } + + NSFW_LOGDBG("pid:%u, starttime:%llu", pid, starttime); + fclose(psfile); /*no need to check return */ + return starttime; +} - g_ps_info = pid_info; +u64 nsfw_ps_get_starttime_from_info(u32 pid) +{ + return (u64) (g_ps_info[pid].starttime); +} - pzoneinfo.stname.enowner = NSFW_PROC_MAIN; - pid_info = nsfw_mem_zone_create (&pzoneinfo); - if (NULL == pid_info) - { - NSFW_LOGERR ("create pid_info failed!"); - return -1; - } +int nsfw_ps_module_init(void *param) +{ + u32 proc_type = (u32) ((long long) param); + nsfw_ps_init_cfg *ps_cfg = &g_ps_cfg; + int retval; + nsfw_pid_item *pid_info = NULL; + enum_alarm_id loop_alarm_id = ALARM_EVENT_BASE; - g_master_ps_info = pid_info; - break; - } - default: - return 0; - } + NSFW_LOGINF("ps module init]type=%u", proc_type); - ps_cfg->ps_info_size = NSFW_PS_INFO_MAX_COUNT; - ps_cfg->ps_waite_exit_tvalue = NSFW_PS_WEXIT_TVLAUE_DEF; - ps_cfg->net_link_buf = MAX_NET_LINK_BUF_DEF; + ps_cfg->ps_chk_hbt_count = NSFW_MAX_HBT_CHK_COUNT_DEF; + ps_cfg->ps_chk_hbt_tvalue = NSFW_CHK_HBT_TVLAUE_DEF; + ps_cfg->ps_chk_hbt_soft_count = ps_cfg->ps_chk_hbt_count; - INIT_LIST_HEAD (&(g_ps_runing_list)); + nsfw_mem_zone pzoneinfo; + pzoneinfo.isocket_id = NSFW_SOCKET_ANY; + pzoneinfo.stname.entype = NSFW_SHMEM; + pzoneinfo.lenth = sizeof(nsfw_pid_item) * NSFW_MAX_PID; + if (-1 == + sprintf_s(pzoneinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s", + "MAS_PS_INFO")) + { + NSFW_LOGERR("sprintf_s failed]"); + return -1; + } - nsfw_mem_sppool pmpinfo; - pmpinfo.enmptype = NSFW_MRING_MPMC; - pmpinfo.usnum = ps_cfg->ps_info_size; - pmpinfo.useltsize = sizeof (nsfw_ps_info); - pmpinfo.isocket_id = NSFW_SOCKET_ANY; - pmpinfo.stname.entype = NSFW_NSHMEM; - if (-1 == - SPRINTF_S (pmpinfo.stname.aname, NSFW_MEM_NAME_LENGTH, "%s", - "MAS_PS_INFOPOOL")) + switch (proc_type) { - NSFW_LOGERR ("SPRINTF_S failed]"); - return -1; + case NSFW_PROC_MASTER: + { + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_CHK_HBT_RSP, + nsfw_ps_recv_hbt_rsp); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_CHK_INIT_REQ, + nsfw_ps_recv_hbt); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_CHK_HBT_REQ, + nsfw_ps_recv_hbt); + (void) NSFW_REG_SOFT_INT(NSFW_HBT_TIMER, NSFW_CHK_HBT_TVLAUE, + 1, 0xFFFF); + (void) NSFW_REG_SOFT_INT(NSFW_HBT_COUNT_PARAM, + NSFW_SOFT_HBT_CHK_COUNT, 1, 0xFFFF); + (void) NSFW_REG_SOFT_INT(NSFW_APP_EXIT_TIMER, + NSFW_PS_WEXIT_TVLAUE, 1, 0xFFFF); + + pid_info = nsfw_mem_zone_lookup(&pzoneinfo.stname); + if (NULL == pid_info) + { + pid_info = nsfw_mem_zone_create(&pzoneinfo); + if (NULL == pid_info) + { + NSFW_LOGERR("alloc rec nul!"); + return -1; + } + + retval = + memset_s(pid_info, + (sizeof(nsfw_pid_item) * NSFW_MAX_PID), 0, + (sizeof(nsfw_pid_item) * NSFW_MAX_PID)); + if (EOK != retval) + { + NSFW_LOGERR("memset_s failed]retval=%d.\n", retval); + return -1; + } + } + g_master_ps_info = pid_info; + + MEM_STAT(NSFW_PS_MODULE, pzoneinfo.stname.aname, NSFW_SHMEM, + pzoneinfo.lenth); + g_ps_info = pid_info; + + for (loop_alarm_id = ALARM_EVENT_BASE; + loop_alarm_id < ALARM_EVENT_MAX; loop_alarm_id++) + { + g_omc_alm_state[loop_alarm_id].send_succ_flag = 1; + g_omc_alm_state[loop_alarm_id].current_alarm_state = + ALARM_CLEAN; + g_omc_alm_state[loop_alarm_id].resend_timer = NULL; + } + + break; + } + case NSFW_PROC_MAIN: + { + + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_CHK_INIT_REQ, + nsfw_ps_recv_hbt); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_CHK_HBT_REQ, + nsfw_ps_recv_hbt); + pid_info = malloc(sizeof(nsfw_pid_item) * NSFW_MAX_PID); + if (NULL == pid_info) + { + NSFW_LOGERR("malloc mem failed!"); + return -1; + } + + retval = + memset_s(pid_info, + (sizeof(nsfw_pid_item) * NSFW_MAX_PID), 0, + (sizeof(nsfw_pid_item) * NSFW_MAX_PID)); + if (EOK != retval) + { + NSFW_LOGERR("memset_s failed]retval=%d.\n", retval); + free(pid_info); + return -1; + } + + g_ps_info = pid_info; + + pzoneinfo.stname.enowner = NSFW_PROC_MASTER; + pid_info = nsfw_mem_zone_lookup(&pzoneinfo.stname); + if (NULL == pid_info) + { + NSFW_LOGERR("lookup pid_info failed!"); + return -1; + } + + g_master_ps_info = pid_info; + break; + } + default: + return 0; } - ps_cfg->ps_info_pool = nsfw_mem_sp_create (&pmpinfo); + ps_cfg->ps_info_size = NSFW_PS_INFO_MAX_COUNT; + ps_cfg->ps_waite_exit_tvalue = NSFW_PS_WEXIT_TVLAUE_DEF; + ps_cfg->net_link_buf = MAX_NET_LINK_BUF_DEF; - if (!ps_cfg->ps_info_pool) + INIT_LIST_HEAD(&(g_ps_runing_list)); + + nsfw_mem_sppool pmpinfo; + pmpinfo.enmptype = NSFW_MRING_MPMC; + pmpinfo.usnum = ps_cfg->ps_info_size; + pmpinfo.useltsize = sizeof(nsfw_ps_info); + pmpinfo.isocket_id = NSFW_SOCKET_ANY; + pmpinfo.stname.entype = NSFW_NSHMEM; + if (-1 == + sprintf_s(pmpinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s", + "MAS_PS_INFOPOOL")) { - NSFW_LOGERR ("alloc ps info pool_err"); - return -1; + NSFW_LOGERR("sprintf_s failed]"); + return -1; } + ps_cfg->ps_info_pool = nsfw_mem_sp_create(&pmpinfo); - MEM_STAT (NSFW_PS_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, - nsfw_mem_get_len (ps_cfg->ps_info_pool, NSFW_MEM_SPOOL)); + if (!ps_cfg->ps_info_pool) + { + NSFW_LOGERR("alloc ps info pool_err"); + return -1; + } + + MEM_STAT(NSFW_PS_MODULE, pmpinfo.stname.aname, NSFW_NSHMEM, + nsfw_mem_get_len(ps_cfg->ps_info_pool, NSFW_MEM_SPOOL)); - if (NSFW_PROC_MAIN != proc_type) + if (NSFW_PROC_MASTER != proc_type) { - return 0; + return 0; } - struct timespec time_left = { NSFW_PS_FIRST_CHK_TVLAUE, 0 }; - g_ps_chk_timer = - (void *) nsfw_timer_reg_timer (NSFW_PS_CHK_TIMER, (void *) TRUE, - nsfw_ps_chk_timeout, time_left); + struct timespec time_left = { NSFW_PS_FIRST_CHK_TVLAUE, 0 }; + g_ps_chk_timer = (void *) nsfw_timer_reg_timer(NSFW_PS_CHK_TIMER, (void *) TRUE, /* the first call will re-alloc all pid */ + nsfw_ps_chk_timeout, + time_left); + /* There may be latency from process exiting to /proc being updated, Master needs to check multiple times */ + int itv; + for (itv = 1; itv < 5; itv++) + { + time_left.tv_sec += itv; /* increasing interval */ + g_ps_chk_timer = (void *) nsfw_timer_reg_timer(NSFW_PS_CHK_TIMER, (void *) FALSE, /* the latter calls should not re-alloc */ + nsfw_ps_chk_timeout, + time_left); + } - if (TRUE != nsfw_ps_start_netlink ()) + if (TRUE != nsfw_ps_start_netlink()) { - return -1; + return -1; } - return 0; + return 0; } /* *INDENT-OFF* */ -NSFW_MODULE_NAME (NSFW_PS_MODULE) -NSFW_MODULE_PRIORITY (10) -NSFW_MODULE_DEPENDS (NSFW_MGR_COM_MODULE) -NSFW_MODULE_DEPENDS (NSFW_TIMER_MODULE) -NSFW_MODULE_INIT (nsfw_ps_module_init) +NSFW_MODULE_NAME(NSFW_PS_MODULE) +NSFW_MODULE_PRIORITY(10) +NSFW_MODULE_DEPENDS(NSFW_MGR_COM_MODULE) +NSFW_MODULE_DEPENDS(NSFW_TIMER_MODULE) +NSFW_MODULE_INIT(nsfw_ps_module_init) /* *INDENT-ON* */ #ifdef __cplusplus diff --git a/src/framework/ipc/ps/nsfw_ps_module.h b/src/framework/ipc/ps/nsfw_ps_module.h index b754cea..ce2db13 100644 --- a/src/framework/ipc/ps/nsfw_ps_module.h +++ b/src/framework/ipc/ps/nsfw_ps_module.h @@ -29,10 +29,6 @@ extern "C"{ /*==============================================* * constants or macros define * *----------------------------------------------*/ - -#define NSFW_MAX_PID 65535 -COMPAT_PROTECT (NSFW_MAX_PID, 65535); - #define NSFW_PS_INFO_MAX_COUNT 4095 #define MAX_NET_LINK_BUF_DEF 0x34000*32 @@ -44,15 +40,15 @@ COMPAT_PROTECT (NSFW_MAX_PID, 65535); typedef struct _nsfw_ps_init_cfg { - u32 ps_info_size; - u32 net_link_buf; + u32 ps_info_size; + u32 net_link_buf; - u16 ps_waite_exit_tvalue; - u16 ps_chk_hbt_count; - u16 ps_chk_hbt_soft_count; - u16 ps_chk_hbt_tvalue; + u16 ps_waite_exit_tvalue; + u16 ps_chk_hbt_count; + u16 ps_chk_hbt_soft_count; + u16 ps_chk_hbt_tvalue; - mring_handle ps_info_pool; + mring_handle ps_info_pool; } nsfw_ps_init_cfg; #define NSFW_PS_CHK_TIMER 1 @@ -61,22 +57,24 @@ typedef struct _nsfw_ps_init_cfg typedef struct _nsfw_pid_item { - u8 proc_type; - u8 u8_reserve; - u16 u16_reserve; - u32 u32_reserve; - nsfw_ps_info *ps_info; + u8 proc_type; + u8 u8_reserve; + u16 u16_reserve; + u32 starttime; + nsfw_ps_info *ps_info; } nsfw_pid_item; -int nsfw_ps_change_fun (i32 epfd, i32 socket, u32 events); -u8 nsfw_sw_ps_state (nsfw_ps_info * pps_info, u8 new_state); +u32 get_ps_exiting_cnt(); + +int nsfw_ps_change_fun(i32 epfd, i32 socket, u32 events); +u8 nsfw_sw_ps_state(nsfw_ps_info * pps_info, u8 new_state); /* for heartbeat checking*/ #define NSFW_MAX_THREAD_DOGS_COUNT 8 #define NSFW_CHK_HBT_TIMER 1 #define NSFW_MAX_HBT_PROC_FUN 4 -#define NSFW_CHK_HBT_TVLAUE_DEF 1 +#define NSFW_CHK_HBT_TVLAUE_DEF 3 #define NSFW_MAX_HBT_CHK_COUNT (g_ps_cfg.ps_chk_hbt_count) #define NSFW_SOFT_HBT_CHK_COUNT (g_ps_cfg.ps_chk_hbt_soft_count) @@ -84,11 +82,11 @@ u8 nsfw_sw_ps_state (nsfw_ps_info * pps_info, u8 new_state); typedef struct _nsfw_ps_chk_msg { - u32 ps_state; - i32 thread_chk_count; + u32 ps_state; + i32 thread_chk_count; } nsfw_ps_chk_msg; -int nsfw_ps_chk_timeout (u32 timer_type, void *data); +int nsfw_ps_chk_timeout(u32 timer_type, void *data); #ifdef __cplusplus /* *INDENT-OFF* */ diff --git a/src/framework/ipc/ps/nsfw_recycle_module.c b/src/framework/ipc/ps/nsfw_recycle_module.c index 01e16fb..06d7dd5 100644 --- a/src/framework/ipc/ps/nsfw_recycle_module.c +++ b/src/framework/ipc/ps/nsfw_recycle_module.c @@ -17,7 +17,7 @@ #include <stdlib.h> #include "types.h" #include "nstack_securec.h" -#include "nsfw_init.h" +#include "nsfw_init_api.h" #include "nsfw_mgr_com_api.h" #include "nsfw_mem_api.h" @@ -27,7 +27,6 @@ #include "nsfw_recycle_module.h" #include "nsfw_maintain_api.h" #include "nstack_log.h" -#include "common_mem_api.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -35,7 +34,7 @@ extern "C"{ /* *INDENT-ON* */ #endif /* __cplusplus */ -/* only work on nStackMain*/ +/* only work on daemon-stack*/ nsfw_recycle_cfg g_rec_cfg; nsfw_recycle_fun g_rec_fun[NSFW_REC_TYPE_MAX] = { 0 }; @@ -44,7 +43,7 @@ nsfw_rec_fun_info g_rec_lock_fun[NSFW_REC_LOCK_REL_MAX_FUN]; /***************************************************************************** * Prototype : nsfw_recycle_reg_fun -* Description : reg one recycle type recycle function +* Description : reg one recycle type recycle funciton * Input : u16 rec_type * nsfw_recycle_fun fun * Output : None @@ -52,18 +51,17 @@ nsfw_rec_fun_info g_rec_lock_fun[NSFW_REC_LOCK_REL_MAX_FUN]; * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_recycle_reg_fun (u16 rec_type, nsfw_recycle_fun fun) +u8 nsfw_recycle_reg_fun(u16 rec_type, nsfw_recycle_fun fun) { - if (NULL == fun || rec_type >= NSFW_REC_TYPE_MAX) + if (NULL == fun || rec_type >= NSFW_REC_TYPE_MAX) { - NSFW_LOGERR ("argv err]fun=%p,type=%u", fun, rec_type); - return FALSE; + NSFW_LOGERR("argv err]fun=%p,type=%u", fun, rec_type); + return FALSE; } - g_rec_fun[rec_type] = fun; - NSFW_LOGINF ("reg]fun=%d,type=%u", fun, rec_type); - return TRUE; + g_rec_fun[rec_type] = fun; + NSFW_LOGINF("reg]fun=%p,type=%u", fun, rec_type); + return TRUE; } /***************************************************************************** @@ -77,31 +75,30 @@ nsfw_recycle_reg_fun (u16 rec_type, nsfw_recycle_fun fun) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_recycle_lock_rel_fun (nsfw_recycle_fun fun, void *data, u8 proc_type) +u8 nsfw_recycle_lock_rel_fun(nsfw_recycle_fun fun, void *data, u8 proc_type) { - if (NULL == fun) + if (NULL == fun) { - NSFW_LOGERR ("argv err]fun=%p,data=%p", fun, data); - return FALSE; + NSFW_LOGERR("argv err]fun=%p,data=%p", fun, data); + return FALSE; } - u32 i; + u32 i; - for (i = 0; i < NSFW_REC_LOCK_REL_MAX_FUN; i++) + for (i = 0; i < NSFW_REC_LOCK_REL_MAX_FUN; i++) { - if (NULL == g_rec_lock_fun[i].rec_fun) + if (NULL == g_rec_lock_fun[i].rec_fun) { - g_rec_lock_fun[i].rec_fun = fun; - g_rec_lock_fun[i].data = data; - g_rec_lock_fun[i].proc_type = proc_type; - NSFW_LOGINF ("reg mgr_msg fun suc]fun=%p,data=%p", fun, data); - return TRUE; + g_rec_lock_fun[i].rec_fun = fun; + g_rec_lock_fun[i].data = data; + g_rec_lock_fun[i].proc_type = proc_type; + NSFW_LOGINF("reg mgr_msg fun suc]fun=%p,data=%p", fun, data); + return TRUE; } } - NSFW_LOGINF ("reg]fun=%p,data=%p", fun, data); - return TRUE; + NSFW_LOGINF("reg]fun=%p,data=%p", fun, data); + return TRUE; } /***************************************************************************** @@ -113,26 +110,25 @@ nsfw_recycle_lock_rel_fun (nsfw_recycle_fun fun, void *data, u8 proc_type) * Calls : * Called By : *****************************************************************************/ -int -nsfw_recycle_exit_pid_lock (u32 pid, u8 proc_type, void *argv) +int nsfw_recycle_exit_pid_lock(u32 pid, u8 proc_type, void *argv) { - u32 i; - NSFW_LOGINF ("release lock]pid=%d,type=%d", pid, proc_type); - for (i = 0; i < NSFW_REC_LOCK_REL_MAX_FUN; i++) + u32 i; + NSFW_LOGINF("release lock]pid=%u,type=%d", pid, proc_type); + for (i = 0; i < NSFW_REC_LOCK_REL_MAX_FUN; i++) { - if (NULL == g_rec_lock_fun[i].rec_fun) + if (NULL == g_rec_lock_fun[i].rec_fun) { - break; + break; } - if ((NSFW_PROC_NULL == g_rec_lock_fun[i].proc_type) - || (proc_type == g_rec_lock_fun[i].proc_type)) + if ((NSFW_PROC_NULL == g_rec_lock_fun[i].proc_type) + || (proc_type == g_rec_lock_fun[i].proc_type)) { - (void) g_rec_lock_fun[i].rec_fun (pid, g_rec_lock_fun[i].data, 0); + (void) g_rec_lock_fun[i].rec_fun(pid, g_rec_lock_fun[i].data, 0); } } - return 0; + return 0; } /***************************************************************************** @@ -144,23 +140,22 @@ nsfw_recycle_exit_pid_lock (u32 pid, u8 proc_type, void *argv) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_recycle_obj_end (u32 pid) +u8 nsfw_recycle_obj_end(u32 pid) { - nsfw_mgr_msg *rsp_msg = - nsfw_mgr_msg_alloc (MGR_MSG_RCC_END_REQ, NSFW_PROC_MAIN); - if (NULL == rsp_msg) + nsfw_mgr_msg *rsp_msg = + nsfw_mgr_msg_alloc(MGR_MSG_RCC_END_REQ, NSFW_PROC_MAIN); + if (NULL == rsp_msg) { - NSFW_LOGERR ("alloc rsp msg failed]pid=%u", pid); - return FALSE; + NSFW_LOGERR("alloc rsp msg failed]pid=%u", pid); + return FALSE; } - nsfw_ps_info_msg *ps_msg = GET_USER_MSG (nsfw_ps_info_msg, rsp_msg); - ps_msg->host_pid = pid; - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); - NSFW_LOGINF ("send obj end msg]pid=%d", pid); - return TRUE; + nsfw_ps_info_msg *ps_msg = GET_USER_MSG(nsfw_ps_info_msg, rsp_msg); + ps_msg->host_pid = pid; + (void) nsfw_mgr_send_msg(rsp_msg); + nsfw_mgr_msg_free(rsp_msg); + NSFW_LOGINF("send obj end msg]pid=%u", pid); + return TRUE; } /***************************************************************************** @@ -173,74 +168,74 @@ nsfw_recycle_obj_end (u32 pid) * Calls : * Called By : *****************************************************************************/ -nsfw_rcc_stat -nsfw_recycle_callback_all_obj (u32 pid, nsfw_recycle_pool * rec_pool) +nsfw_rcc_stat nsfw_recycle_callback_all_obj(u32 pid, + nsfw_recycle_pool * rec_pool) { - u32 match = 0; - nsfw_recycle_obj *obj = NULL; - if (NULL == rec_pool) + u32 match = 0; + nsfw_recycle_obj *obj = NULL; + if (NULL == rec_pool) { - return NSFW_RCC_CONTINUE; + return NSFW_RCC_CONTINUE; } - nsfw_recycle_obj *p_start = rec_pool->obj; - u32 i; - u32 size = rec_pool->pool_size; + nsfw_recycle_obj *p_start = rec_pool->obj; + u32 i; + u32 size = rec_pool->pool_size; - nsfw_ps_info *pps_info; - pps_info = nsfw_ps_info_get (pid); - if (NULL == pps_info) + nsfw_ps_info *pps_info; + pps_info = nsfw_ps_info_get(pid); + if (NULL == pps_info) { - NSFW_LOGERR ("get ps_info failed!]pid=%d", pid); - return NSFW_RCC_CONTINUE; + NSFW_LOGERR("get ps_info falied!]pid=%d", pid); + return NSFW_RCC_CONTINUE; } - i32 cur_idx = (i32) (u64) nsfw_ps_get_uv (pps_info, NSFW_REC_IDX); + i32 cur_idx = (i32) (u64) nsfw_ps_get_uv(pps_info, NSFW_REC_IDX); - if (-1 == cur_idx) + if (-1 == cur_idx) { - cur_idx = 0; - nsfw_ps_set_uv (pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); - (void) nsfw_recycle_exit_pid_lock (pid, NSFW_PROC_APP, NULL); + cur_idx = 0; + nsfw_ps_set_uv(pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); + (void) nsfw_recycle_exit_pid_lock(pid, NSFW_PROC_APP, NULL); } - else + else { - cur_idx++; - nsfw_ps_set_uv (pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); + cur_idx++; + nsfw_ps_set_uv(pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); } - for (i = cur_idx; i < size; i++) + for (i = cur_idx; i < size; i++) { - obj = &p_start[i]; - cur_idx = i; - nsfw_ps_set_uv (pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); - if (FALSE == obj->alloc_flag) + obj = &p_start[i]; + cur_idx = i; + nsfw_ps_set_uv(pps_info, NSFW_REC_IDX, (void *) (u64) cur_idx); + if (FALSE == obj->alloc_flag) { - continue; + continue; } - if ((obj->rec_type < NSFW_REC_TYPE_MAX) - && (NULL != g_rec_fun[obj->rec_type])) + if ((obj->rec_type < NSFW_REC_TYPE_MAX) + && (NULL != g_rec_fun[obj->rec_type])) { - match++; - if (NSFW_RCC_SUSPEND == - g_rec_fun[obj->rec_type] (pid, obj->data, obj->rec_type)) + match++; + if (NSFW_RCC_SUSPEND == + g_rec_fun[obj->rec_type] (pid, obj->data, obj->rec_type)) { - NSFW_LOGINF - ("call suspend]type=%d,obj_pid=%d,pid=%d,count=%d", - obj->rec_type, obj->host_pid, pid, match); - return NSFW_RCC_SUSPEND; + NSFW_LOGINF + ("call suspend]type=%d,obj_pid=%d,pid=%d,count=%d", + obj->rec_type, obj->host_pid, pid, match); + return NSFW_RCC_SUSPEND; } } - else + else { - NSFW_LOGERR ("obj_error!drop]type=%d,obj_pid=%d,pid=%d", - obj->rec_type, obj->host_pid, pid); + NSFW_LOGERR("obj_error!drop]type=%d,obj_pid=%u,pid=%u", + obj->rec_type, obj->host_pid, pid); } } - NSFW_LOGWAR ("rec obj]pid=%d,count=%d,cur_idx=%d", pid, match, cur_idx); - return NSFW_RCC_CONTINUE; + NSFW_LOGWAR("rec obj]pid=%u,match=%u,cur_idx=%d", pid, match, cur_idx); + return NSFW_RCC_CONTINUE; } /***************************************************************************** @@ -252,75 +247,67 @@ nsfw_recycle_callback_all_obj (u32 pid, nsfw_recycle_pool * rec_pool) * Calls : * Called By : *****************************************************************************/ -u8 -nsfw_recycle_pid_obj (u32 pid) +u8 nsfw_recycle_pid_obj(u32 pid) { - nsfw_ps_info *pps_info; - pps_info = nsfw_ps_info_get (pid); - if (NULL == pps_info) + nsfw_ps_info *pps_info; + pps_info = nsfw_ps_info_get(pid); + if (NULL == pps_info) { - NSFW_LOGERR ("get ps_info failed!]pid=%d", pid); - return FALSE; + NSFW_LOGERR("get ps_info falied]pid=%u", pid); + return FALSE; } - nsfw_recycle_pool *rec_pool = g_rec_cfg.mem_rec_obj_pool; - void *timer_ptr = nsfw_ps_get_uv (pps_info, NSFW_REC_TIMER); - if (NSFW_RCC_SUSPEND == nsfw_recycle_callback_all_obj (pid, rec_pool)) + nsfw_recycle_pool *rec_pool = g_rec_cfg.mem_rec_obj_pool; + void *timer_ptr = nsfw_ps_get_uv(pps_info, NSFW_REC_TIMER); + if (NSFW_RCC_SUSPEND == nsfw_recycle_callback_all_obj(pid, rec_pool)) { - if (NULL != timer_ptr) + if (NULL != timer_ptr) { - nsfw_timer_rmv_timer (timer_ptr); - timer_ptr = NULL; + nsfw_timer_rmv_timer(timer_ptr); + timer_ptr = NULL; } - struct timespec time_left = { NSFW_REC_WEND_TVLAUE, 0 }; - timer_ptr = - (void *) nsfw_timer_reg_timer (NSFW_REC_WEND_TIMER, - (void *) (u64) pid, - nsfw_recycle_obj_timeout, time_left); - nsfw_ps_set_uv (pps_info, NSFW_REC_TIMER, timer_ptr); - return TRUE; + struct timespec time_left = { NSFW_REC_WEND_TVLAUE, 0 }; + timer_ptr = + (void *) nsfw_timer_reg_timer(NSFW_REC_WEND_TIMER, + (void *) (u64) pid, + nsfw_recycle_obj_timeout, + time_left); + nsfw_ps_set_uv(pps_info, NSFW_REC_TIMER, timer_ptr); + return TRUE; } - if (NULL != timer_ptr) + if (NULL != timer_ptr) { - nsfw_timer_rmv_timer (timer_ptr); - nsfw_ps_set_uv (pps_info, NSFW_REC_TIMER, NULL); + nsfw_timer_rmv_timer(timer_ptr); + nsfw_ps_set_uv(pps_info, NSFW_REC_TIMER, NULL); } - (void) nsfw_ps_exit_end_notify (pid); - nsfw_ps_info_free (pps_info); - return TRUE; + (void) nsfw_ps_exit_end_notify(pid); + if (pps_info->proc_type == NSFW_PROC_MASTER) + { + nsfw_ps_info_free(pps_info); + } + return TRUE; } -/***************************************************************************** -* Prototype : nsfw_recycle_all_obj -* Description : -* Input : u32 pid -* nsfw_mem_addr_msg *addr_msg -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -nsfw_recycle_all_obj (u32 pid) +u8 nsfw_recycle_all_obj(u32 pid) { - nsfw_ps_info *pps_info; - pps_info = nsfw_ps_info_get (pid); - if (NULL == pps_info) + nsfw_ps_info *pps_info; + pps_info = nsfw_ps_info_get(pid); + if (NULL == pps_info) { - pps_info = nsfw_ps_info_alloc (pid, NSFW_PROC_APP); - if (NULL == pps_info) + pps_info = nsfw_ps_info_alloc(pid, NSFW_PROC_APP, 0); + if (NULL == pps_info) { - NSFW_LOGERR ("alloc ps_info failed!]pid=%u", pid); - return FALSE; + NSFW_LOGERR("alloc ps_info falied!]pid=%u", pid); + return FALSE; } } - nsfw_ps_set_uv (pps_info, NSFW_REC_IDX, (void *) (-1)); - nsfw_ps_set_uv (pps_info, NSFW_REC_TIMER, NULL); - return nsfw_recycle_pid_obj (pid); + nsfw_ps_set_uv(pps_info, NSFW_REC_IDX, (void *) (-1)); + nsfw_ps_set_uv(pps_info, NSFW_REC_TIMER, NULL); + return nsfw_recycle_pid_obj(pid); } /***************************************************************************** @@ -332,27 +319,26 @@ nsfw_recycle_all_obj (u32 pid) * Calls : * Called By : *****************************************************************************/ -int -mem_app_exit_proc (nsfw_mgr_msg * msg) +int mem_app_exit_proc(nsfw_mgr_msg * msg) { - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("msg nul"); - return FALSE; + NSFW_LOGERR("msg nul"); + return FALSE; } - nsfw_ps_info_msg *ps_info_msg = GET_USER_MSG (nsfw_ps_info_msg, msg); + nsfw_ps_info_msg *ps_info_msg = GET_USER_MSG(nsfw_ps_info_msg, msg); - /* look up the app rec memzone and release all resource */ - /* no need to send rsp for it will be send after stack process over */ - if (TRUE == nsfw_recycle_all_obj (ps_info_msg->host_pid)) + /* look up the app rec memzone and release all resource */ + /* no need to send rsp for it will be send after stack process over */ + if (TRUE == nsfw_recycle_all_obj(ps_info_msg->host_pid)) { - NSFW_LOGINF ("obj found!]pid=%d", ps_info_msg->host_pid); - return TRUE; + NSFW_LOGINF("obj found!]pid=%u", ps_info_msg->host_pid); + return TRUE; } - (void) nsfw_ps_exit_end_notify (ps_info_msg->host_pid); - return FALSE; + (void) nsfw_ps_exit_end_notify(ps_info_msg->host_pid); + return FALSE; } /***************************************************************************** @@ -364,18 +350,17 @@ mem_app_exit_proc (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -int -nsfw_recycle_obj_end_proc (nsfw_mgr_msg * msg) +int nsfw_recycle_obj_end_proc(nsfw_mgr_msg * msg) { - if (NULL == msg) + if (NULL == msg) { - NSFW_LOGERR ("msg nul"); - return FALSE; + NSFW_LOGERR("msg nul"); + return FALSE; } - nsfw_ps_info_msg *ps_info_msg = GET_USER_MSG (nsfw_ps_info_msg, msg); + nsfw_ps_info_msg *ps_info_msg = GET_USER_MSG(nsfw_ps_info_msg, msg); - return nsfw_recycle_pid_obj (ps_info_msg->host_pid); + return nsfw_recycle_pid_obj(ps_info_msg->host_pid); } /***************************************************************************** @@ -388,36 +373,35 @@ nsfw_recycle_obj_end_proc (nsfw_mgr_msg * msg) * Calls : * Called By : *****************************************************************************/ -int -nsfw_recycle_obj_timeout (u32 timer_type, void *data) +int nsfw_recycle_obj_timeout(u32 timer_type, void *data) { - u32 pid = (u64) data; - void *timer_ptr = NULL; - NSFW_LOGINF ("ps_info timerout]pid=%u", pid); + u32 pid = (u64) data; + void *timer_ptr = NULL; + NSFW_LOGINF("ps_info timerout]pid=%u", pid); - nsfw_ps_info *pps_info; - pps_info = nsfw_ps_info_get (pid); - if (NULL != pps_info) + nsfw_ps_info *pps_info; + pps_info = nsfw_ps_info_get(pid); + if (NULL != pps_info) { - nsfw_recycle_pool *rec_pool = g_rec_cfg.mem_rec_obj_pool; - if (NULL != rec_pool) + nsfw_recycle_pool *rec_pool = g_rec_cfg.mem_rec_obj_pool; + if (NULL != rec_pool) { - if (TRUE == g_hbt_switch) + if (TRUE == g_hbt_switch) { - struct timespec time_left = { NSFW_REC_WEND_TVLAUE, 0 }; - timer_ptr = - (void *) nsfw_timer_reg_timer (timer_type, data, - nsfw_recycle_obj_timeout, - time_left); - nsfw_ps_set_uv (pps_info, NSFW_REC_TIMER, timer_ptr); - return TRUE; + struct timespec time_left = { NSFW_REC_WEND_TVLAUE, 0 }; + timer_ptr = + (void *) nsfw_timer_reg_timer(timer_type, data, + nsfw_recycle_obj_timeout, + time_left); + nsfw_ps_set_uv(pps_info, NSFW_REC_TIMER, timer_ptr); + return TRUE; } } - nsfw_ps_set_uv (pps_info, NSFW_REC_TIMER, timer_ptr); + nsfw_ps_set_uv(pps_info, NSFW_REC_TIMER, timer_ptr); } - (void) nsfw_recycle_pid_obj (pid); - return TRUE; + (void) nsfw_recycle_pid_obj(pid); + return TRUE; } /***************************************************************************** @@ -429,96 +413,95 @@ nsfw_recycle_obj_timeout (u32 timer_type, void *data) * Calls : * Called By : *****************************************************************************/ -u8 -mem_rec_zone_init () +u8 mem_rec_zone_init() { - nsfw_mem_mring pringinfo; - pringinfo.enmptype = NSFW_MRING_MPMC; - pringinfo.isocket_id = NSFW_SOCKET_ANY; - pringinfo.stname.entype = NSFW_NSHMEM; - pringinfo.usnum = MEM_RECYCLE_PER_PRO_QUE; - u32 i; - for (i = 0; i < NSFW_REC_PRO_MAX; i++) + nsfw_mem_mring pringinfo; + pringinfo.enmptype = NSFW_MRING_MPMC; + pringinfo.isocket_id = NSFW_SOCKET_ANY; + pringinfo.stname.entype = NSFW_NSHMEM; + pringinfo.usnum = MEM_RECYCLE_PER_PRO_QUE; + u32 i; + for (i = 0; i < NSFW_REC_PRO_MAX; i++) { - if (-1 == - SPRINTF_S (pringinfo.stname.aname, NSFW_MEM_NAME_LENGTH, "%s%d", - MEM_REC_QUEUE_NAME, i)) + if (-1 == + sprintf_s(pringinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s%u", + MEM_REC_QUEUE_NAME, i)) { - NSFW_LOGERR ("SPRINTF_S failed]"); - return FALSE; + NSFW_LOGERR("sprintf_s fail"); + return FALSE; } - g_rec_cfg.mem_rec_pro[i] = nsfw_mem_ring_create (&pringinfo); - if (NULL == g_rec_cfg.mem_rec_pro[i]) + g_rec_cfg.mem_rec_pro[i] = nsfw_mem_ring_create(&pringinfo); + if (NULL == g_rec_cfg.mem_rec_pro[i]) { - NSFW_LOGERR ("alloc rec ring nul!"); - return FALSE; + NSFW_LOGERR("alloc rec ring nul"); + return FALSE; } } - MEM_STAT (NSFW_RECYCLE_MODULE, MEM_REC_QUEUE_NAME, NSFW_NSHMEM, - NSFW_REC_PRO_MAX * nsfw_mem_get_len (g_rec_cfg.mem_rec_pro[0], + MEM_STAT(NSFW_RECYCLE_MODULE, MEM_REC_QUEUE_NAME, NSFW_NSHMEM, + NSFW_REC_PRO_MAX * nsfw_mem_get_len(g_rec_cfg.mem_rec_pro[0], NSFW_MEM_RING)); - nsfw_mem_zone pzoneinfo; - pzoneinfo.isocket_id = NSFW_SOCKET_ANY; - pzoneinfo.stname.entype = NSFW_NSHMEM; - pzoneinfo.length = - MEM_RECYCLE_OBJ_MAX_NUM * sizeof (nsfw_recycle_obj) + - sizeof (nsfw_recycle_pool); - if (-1 == - SPRINTF_S (pzoneinfo.stname.aname, NSFW_MEM_NAME_LENGTH, "%s", - MEM_REC_POOL_NAME)) + nsfw_mem_zone pzoneinfo; + pzoneinfo.isocket_id = NSFW_SOCKET_ANY; + pzoneinfo.stname.entype = NSFW_NSHMEM; + pzoneinfo.lenth = + MEM_RECYCLE_OBJ_MAX_NUM * sizeof(nsfw_recycle_obj) + + sizeof(nsfw_recycle_pool); + if (-1 == + sprintf_s(pzoneinfo.stname.aname, NSFW_MEM_NAME_LENTH, "%s", + MEM_REC_POOL_NAME)) { - NSFW_LOGERR ("SPRINTF_S failed]"); - return FALSE; + NSFW_LOGERR("sprintf_s fail"); + return FALSE; } - g_rec_cfg.mem_rec_obj_pool = nsfw_mem_zone_create (&pzoneinfo); - if (NULL == g_rec_cfg.mem_rec_obj_pool) + g_rec_cfg.mem_rec_obj_pool = nsfw_mem_zone_create(&pzoneinfo); + if (NULL == g_rec_cfg.mem_rec_obj_pool) { - NSFW_LOGERR ("alloc rec pool nul!"); - return FALSE; + NSFW_LOGERR("alloc rec pool nul"); + return FALSE; } - MEM_STAT (NSFW_RECYCLE_MODULE, MEM_REC_POOL_NAME, NSFW_NSHMEM, - pzoneinfo.length); + MEM_STAT(NSFW_RECYCLE_MODULE, MEM_REC_POOL_NAME, NSFW_NSHMEM, + pzoneinfo.lenth); - int retval; - retval = - MEMSET_S (g_rec_cfg.mem_rec_obj_pool, pzoneinfo.length, 0, - pzoneinfo.length); - if (EOK != retval) + int retval; + retval = + memset_s(g_rec_cfg.mem_rec_obj_pool, pzoneinfo.lenth, 0, + pzoneinfo.lenth); + if (EOK != retval) { - NSFW_LOGERR ("mem set init failed!"); - return FALSE; + NSFW_LOGERR("mem set init fail"); + return FALSE; } - i32 j; - nsfw_recycle_pool *rec_pool = - (nsfw_recycle_pool *) g_rec_cfg.mem_rec_obj_pool; - rec_pool->pool_size = MEM_RECYCLE_OBJ_MAX_NUM; + i32 j; + nsfw_recycle_pool *rec_pool = + (nsfw_recycle_pool *) g_rec_cfg.mem_rec_obj_pool; + rec_pool->pool_size = MEM_RECYCLE_OBJ_MAX_NUM; - nsfw_recycle_obj *p_start = rec_pool->obj; - for (i = 0; i < NSFW_REC_PRO_MAX; i++) + nsfw_recycle_obj *p_start = rec_pool->obj; + for (i = 0; i < NSFW_REC_PRO_MAX; i++) { - for (j = 0; j < MEM_RECYCLE_PER_PRO_QUE; j++) + for (j = 0; j < MEM_RECYCLE_PER_PRO_QUE; j++) { - if (FALSE == p_start[j].alloc_flag) + if (FALSE == p_start[j].alloc_flag) { - if (0 == - nsfw_mem_ring_enqueue (g_rec_cfg.mem_rec_pro[i], - &p_start[j])) + if (0 == + nsfw_mem_ring_enqueue(g_rec_cfg.mem_rec_pro[i], + &p_start[j])) { - NSFW_LOGERR ("enqueue failed"); - break; + NSFW_LOGERR("enqueue fail"); + break; } } } - p_start = p_start + MEM_RECYCLE_PER_PRO_QUE; + p_start = p_start + MEM_RECYCLE_PER_PRO_QUE; } - NSFW_LOGINF ("init rec pool and ring suc!"); - return TRUE; + NSFW_LOGINF("init rec pool and ring suc"); + return TRUE; } /***************************************************************************** @@ -532,46 +515,45 @@ mem_rec_zone_init () * Calls : * Called By : *****************************************************************************/ -void * -nsfw_recycle_reg_obj (u8 priority, u16 rec_type, void *data) +void *nsfw_recycle_reg_obj(u8 priority, u16 rec_type, void *data) { - if (NSFW_REC_PRO_MAX <= priority) + if (NSFW_REC_PRO_MAX <= priority) { - NSFW_LOGERR ("pro error]priority=%d,rec_type=%d,data=%p", priority, - rec_type, data); - return NULL; + NSFW_LOGERR("pro error]priority=%d,rec_type=%d,data=%p", priority, + rec_type, data); + return NULL; } - nsfw_recycle_obj *obj = NULL; - if (0 == - nsfw_mem_ring_dequeue (g_rec_cfg.mem_rec_pro[priority], (void *) &obj)) + nsfw_recycle_obj *obj = NULL; + if (0 == + nsfw_mem_ring_dequeue(g_rec_cfg.mem_rec_pro[priority], (void *) &obj)) { - NSFW_LOGERR ("dequeue error]priority=%d,rec_type=%d,data=%p", - priority, rec_type, data); - return NULL; + NSFW_LOGERR("dequeue error]priority=%d,rec_type=%d,data=%p", + priority, rec_type, data); + return NULL; } - if (EOK != MEMSET_S (obj, sizeof (*obj), 0, sizeof (*obj))) + if (EOK != memset_s(obj, sizeof(*obj), 0, sizeof(*obj))) { - if (0 == nsfw_mem_ring_enqueue (g_rec_cfg.mem_rec_pro[priority], obj)) + if (0 == nsfw_mem_ring_enqueue(g_rec_cfg.mem_rec_pro[priority], obj)) { - NSFW_LOGERR ("enqueue error]priority=%d,rec_type=%d,data=%p", - priority, rec_type, data); + NSFW_LOGERR("enqueue error]priority=%d,rec_type=%d,data=%p", + priority, rec_type, data); } - NSFW_LOGERR ("mem set error]priority=%d,rec_type=%d,data=%p", - priority, rec_type, data); - return NULL; + NSFW_LOGERR("mem set error]priority=%d,rec_type=%d,data=%p", + priority, rec_type, data); + return NULL; } - obj->alloc_flag = TRUE; - obj->rec_type = rec_type; - obj->data = data; - obj->host_pid = get_sys_pid (); - obj->alloc_flag = TRUE; - NSFW_LOGINF ("en queue obj]priority=%d,rec_type=%d,data=%p", priority, - rec_type, data); - return obj; + obj->alloc_flag = TRUE; + obj->rec_type = rec_type; + obj->data = data; + obj->host_pid = get_sys_pid(); + obj->alloc_flag = TRUE; + NSFW_LOGINF("en queue obj]priority=%d,rec_type=%d,data=%p", priority, + rec_type, data); + return obj; } /***************************************************************************** @@ -583,77 +565,60 @@ nsfw_recycle_reg_obj (u8 priority, u16 rec_type, void *data) * Calls : * Called By : *****************************************************************************/ -int -nsfw_recycle_rechk_lock () +int nsfw_recycle_rechk_lock() { - return nsfw_ps_rechk_pid_exit (nsfw_recycle_exit_pid_lock, NULL); + return nsfw_ps_rechk_pid_exit(nsfw_recycle_exit_pid_lock, NULL); } -/***************************************************************************** -* Prototype : nsfw_recycle_module_init -* Description : module init -* Input : void* param -* Output : None -* Return Value : static int -* Calls : -* Called By : -*****************************************************************************/ -NSTACK_STATIC int nsfw_recycle_module_init (void *param); -NSTACK_STATIC int -nsfw_recycle_module_init (void *param) +NSTACK_STATIC int nsfw_recycle_module_init(void *param); +NSTACK_STATIC int nsfw_recycle_module_init(void *param) { - u32 proc_type = (u32) ((long long) param); - NSFW_LOGINF ("recycle module init]type=%d", proc_type); - g_rec_cfg.rec_waite_end_tvalue = NSFW_REC_WEND_TVLAUE_DEF; - switch (proc_type) + u32 proc_type = (u32) ((long long) param); + NSFW_LOGINF("recycle module init]type=%u", proc_type); + g_rec_cfg.rec_waite_end_tvalue = NSFW_REC_WEND_TVLAUE_DEF; + switch (proc_type) { - case NSFW_PROC_MAIN: - (void) nsfw_mgr_reg_msg_fun (MGR_MSG_APP_EXIT_REQ, mem_app_exit_proc); - (void) nsfw_mgr_reg_msg_fun (MGR_MSG_RCC_END_REQ, - nsfw_recycle_obj_end_proc); - if (TRUE == mem_rec_zone_init ()) - { - return 0; - } + case NSFW_PROC_MASTER: + return 0; + case NSFW_PROC_MAIN: + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_APP_EXIT_REQ, + mem_app_exit_proc); + (void) nsfw_mgr_reg_msg_fun(MGR_MSG_RCC_END_REQ, + nsfw_recycle_obj_end_proc); + if (TRUE == mem_rec_zone_init()) + { + return 0; + } - return 0; - default: - if (proc_type < NSFW_PROC_MAX) - { - break; - } - return -1; + return 0; + default: + if (proc_type < NSFW_PROC_MAX) + { + break; + } + return -1; } - return 0; + return 0; } -/***************************************************************************** -* Prototype : nsfw_recycle_fork_init -* Description : fork init -* Input : None -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -nsfw_recycle_fork_init () +u8 nsfw_recycle_fork_init() { - /* reconnect to master after fork in child proc */ - nsfw_mgr_close_dst_proc (NSFW_PROC_MAIN, 0); - if (0 == nsfw_recycle_module_init ((void *) ((long long) NSFW_PROC_APP))) + /* reconnect to master after fork in child proc */ + nsfw_mgr_close_dst_proc(NSFW_PROC_MAIN, 0); + nsfw_mgr_close_dst_proc(NSFW_PROC_MASTER, 0); + if (0 == nsfw_recycle_module_init((void *) ((long long) NSFW_PROC_APP))) { - return TRUE; + return TRUE; } - return FALSE; + return FALSE; } /* *INDENT-OFF* */ -NSFW_MODULE_NAME (NSFW_RECYCLE_MODULE) -NSFW_MODULE_PRIORITY (10) -NSFW_MODULE_DEPENDS (NSFW_PS_MEM_MODULE) -NSFW_MODULE_INIT (nsfw_recycle_module_init) +NSFW_MODULE_NAME(NSFW_RECYCLE_MODULE) +NSFW_MODULE_PRIORITY(10) +NSFW_MODULE_DEPENDS(NSFW_PS_MEM_MODULE) +NSFW_MODULE_INIT(nsfw_recycle_module_init) /* *INDENT-ON* */ #ifdef __cplusplus diff --git a/src/framework/ipc/ps/nsfw_recycle_module.h b/src/framework/ipc/ps/nsfw_recycle_module.h index 694c1d2..ca9ce7e 100644 --- a/src/framework/ipc/ps/nsfw_recycle_module.h +++ b/src/framework/ipc/ps/nsfw_recycle_module.h @@ -18,6 +18,8 @@ #define _NSFW_RECYCLE_MODULE_H #include "nsfw_recycle_api.h" +#include "nsfw_mem_api.h" +#include "types.h" #ifdef __cplusplus /* *INDENT-OFF* */ @@ -40,40 +42,41 @@ extern "C"{ typedef struct _nsfw_recycle_obj { - u8 alloc_flag; - u8 u8reserve; - u16 rec_type; - u32 host_pid; - void *data; - u64 u64reserve; + u8 alloc_flag; + u8 u8reserve; + u16 rec_type; + u32 host_pid; + void *data; + u64 u64reserve; } nsfw_recycle_obj; #define NSFW_REC_LOCK_REL_MAX_FUN 32 typedef struct _nsfw_rec_fun_info { - nsfw_recycle_fun rec_fun; - void *data; - u8 proc_type; + nsfw_recycle_fun rec_fun; + void *data; + u8 proc_type; } nsfw_rec_fun_info; typedef struct _nsfw_recycle_pool { - u32 pool_size; - nsfw_recycle_obj obj[0]; + u32 pool_size; + + nsfw_recycle_obj obj[0]; } nsfw_recycle_pool; typedef struct _nsfw_recycle_cfg { - u16 rec_waite_end_tvalue; - mring_handle mem_rec_obj_pool; - mzone_handle mem_rec_pro[NSFW_REC_PRO_MAX]; + u16 rec_waite_end_tvalue; + mring_handle mem_rec_obj_pool; + mzone_handle mem_rec_pro[NSFW_REC_PRO_MAX]; } nsfw_recycle_cfg; -extern nsfw_rcc_stat nsfw_recycle_callback_all_obj (u32 pid, - nsfw_recycle_pool * - rec_pool); -extern int nsfw_recycle_obj_timeout (u32 timer_type, void *data); +extern nsfw_rcc_stat nsfw_recycle_callback_all_obj(u32 pid, + nsfw_recycle_pool * + rec_pool); +extern int nsfw_recycle_obj_timeout(u32 timer_type, void *data); #ifdef __cplusplus /* *INDENT-OFF* */ diff --git a/src/framework/ipc/ps/nsfw_soft_param.c b/src/framework/ipc/ps/nsfw_soft_param.c deleted file mode 100644 index 743276f..0000000 --- a/src/framework/ipc/ps/nsfw_soft_param.c +++ /dev/null @@ -1,297 +0,0 @@ -/* -* -* Copyright (c) 2018 Huawei Technologies Co.,Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at: -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -/*==============================================* - * include header files * - *----------------------------------------------*/ - -#include <stdlib.h> -#include "types.h" -#include "nstack_securec.h" -#include "nsfw_init.h" - -#include "nstack_log.h" -#include "nsfw_maintain_api.h" - -#include "nsfw_mem_api.h" - -#ifdef __cplusplus -/* *INDENT-OFF* */ -extern "C"{ -/* *INDENT-ON* */ -#endif /* __cplusplus */ - -typedef struct _nsfw_set_soft_item -{ - void *data; - u32 size; - u64 max; - u64 min; -} nsfw_set_soft_item; - -/* *INDENT-OFF* */ -nsfw_set_soft_fun g_soft_fun[NSFW_MAX_SOFT_PARAM] = { 0 }; -nsfw_set_soft_item g_soft_int_cfg[NSFW_MAX_SOFT_PARAM]; -/* *INDENT-ON* */ - -int -nsfw_isdigitstr (const char *str) -{ - if (NULL == str || '\0' == str[0]) - return 0; - - while (*str) - { - if (*str < '0' || *str > '9') - return 0; - str++; - } - return 1; -} - -u8 -nsfw_soft_param_reg_fun (u32 param_name, nsfw_set_soft_fun fun) -{ - if (NULL == fun || param_name >= NSFW_MAX_SOFT_PARAM) - { - NSFW_LOGERR ("argv err]fun=%p,type=%u", fun, param_name); - return FALSE; - } - - g_soft_fun[param_name] = fun; - NSFW_LOGINF ("reg]fun=%d,type=%u", fun, param_name); - return TRUE; -} - -int -nsfw_soft_set_int (u32 param, char *buf, u32 buf_len) -{ - u64 buf_value = 0; - if (NULL == buf || param >= NSFW_MAX_SOFT_PARAM) - { - NSFW_LOGERR ("argv err]buf=%p,param=%u", buf, param); - return FALSE; - } - - if (!nsfw_isdigitstr (buf)) - { - NSFW_LOGERR ("argv err]buf=%s,param=%u", buf, param); - return FALSE; - } - - char *parsing_end; - buf_value = (u64) strtol (buf, &parsing_end, 10); - nsfw_set_soft_item *int_item = &g_soft_int_cfg[param]; - if (NULL == int_item->data) - { - NSFW_LOGERR ("data err]buf=%s,param=%u,min=%llu,max=%llu", buf, param, - int_item->min, int_item->max); - return FALSE; - } - - if (buf_value < int_item->min || buf_value > int_item->max) - { - NSFW_LOGERR ("argv err]buf=%s,param=%u,min=%llu,max=%llu", buf, param, - int_item->min, int_item->max); - return FALSE; - } - - u32 size = int_item->size; - if (size >= sizeof (u64)) - { - size = sizeof (u64); - } - - if (EOK != MEMCPY_S (int_item->data, size, &buf_value, size)) - { - NSFW_LOGERR ("MEMCPY_S failed"); - return FALSE; - } - - NSFW_LOGINF ("set soft param ok]param=%u,value=%llu,size=%u", param, - buf_value, size); - return TRUE; -} - -/***************************************************************************** -* Prototype : nsfw_soft_param_reg_int -* Description : reg int param set -* Input : u32 param_name -* u32 size -* u32 min -* u32 max -* u64 *data -* Output : None -* Return Value : u8 -* Calls : -* Called By : -*****************************************************************************/ -u8 -nsfw_soft_param_reg_int (u32 param_name, u32 size, u32 min, u32 max, - u64 * data) -{ - if (NULL == data || param_name >= NSFW_MAX_SOFT_PARAM) - { - NSFW_LOGERR ("argv err]data=%p,type=%u", data, param_name); - return FALSE; - } - - g_soft_int_cfg[param_name].data = data; - g_soft_int_cfg[param_name].size = size; - g_soft_int_cfg[param_name].max = max; - g_soft_int_cfg[param_name].min = min; - return nsfw_soft_param_reg_fun (param_name, nsfw_soft_set_int); -} - -void -nsfw_set_soft_para (fw_poc_type proc_type, u32 para_name, void *value, - u32 size) -{ - nsfw_mgr_msg *msg = - (nsfw_mgr_msg *) nsfw_mgr_msg_alloc (MGR_MSG_SOF_PAR_REQ, proc_type); - if (NULL == msg) - { - NSFW_LOGERR - ("nsfw_mgr_msg_alloc failed] msg type=%d, proc type=%d, para_name=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name); - return; - } - - nsfw_soft_param_msg *soft_msg = GET_USER_MSG (nsfw_soft_param_msg, msg); - - soft_msg->param_name = para_name; - soft_msg->rsp_code = 0; - - if (EOK != - MEMCPY_S (soft_msg->param_value, sizeof (soft_msg->param_value), - value, size)) - { - NSFW_LOGERR - ("MEMCPY_S failed] msg type=%d, proc type=%d, para_name=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name); - nsfw_mgr_msg_free (msg); - return; - } - - nsfw_mgr_msg *rsp = nsfw_mgr_null_rspmsg_alloc (); - if (NULL == rsp) - { - NSFW_LOGERR - ("nsfw_mgr_null_rspmsg_alloc failed] msg type=%d, proc type=%d, para_name=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name); - nsfw_mgr_msg_free (msg); - return; - } - - if (!nsfw_mgr_send_req_wait_rsp (msg, rsp)) - { - NSFW_LOGERR - ("can not get response] msg type=%d, proc type=%d, para_name=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name); - nsfw_mgr_msg_free (msg); - nsfw_mgr_msg_free (rsp); - return; - } - - nsfw_soft_param_msg *soft_rsp_msg = GET_USER_MSG (nsfw_soft_param_msg, rsp); - if (soft_rsp_msg->rsp_code != NSFW_EXIT_SUCCESS) - { - NSFW_LOGERR - ("set soft param failed] msg type=%d, proc type=%d, para_name=%u, rsp code=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name, soft_rsp_msg->rsp_code); - nsfw_mgr_msg_free (msg); - nsfw_mgr_msg_free (rsp); - return; - } - - NSFW_LOGINF - ("set soft param success] msg type=%d, proc type=%d, para_name=%u", - MGR_MSG_SOF_PAR_REQ, proc_type, para_name); - - nsfw_mgr_msg_free (msg); - nsfw_mgr_msg_free (rsp); - return; -} - -int -nsfw_softparam_msg_proc (nsfw_mgr_msg * msg) -{ - nsfw_mgr_msg *rsp_msg = nsfw_mgr_rsp_msg_alloc (msg); - if (NULL == rsp_msg) - { - NSFW_LOGERR ("alloc rsp failed,drop msg!" MSGINFO, PRTMSG (msg)); - return FALSE; - } - - nsfw_soft_param_msg *soft_msg = GET_USER_MSG (nsfw_soft_param_msg, msg); - nsfw_soft_param_msg *soft_rsp_msg = - GET_USER_MSG (nsfw_soft_param_msg, rsp_msg); - if ((soft_msg->param_name < NSFW_MAX_SOFT_PARAM) - && (NULL != g_soft_fun[soft_msg->param_name])) - { - soft_msg->param_value[sizeof (soft_msg->param_value) - 1] = 0; - (void) g_soft_fun[soft_msg->param_name] (soft_msg->param_name, - (char *) - soft_msg->param_value, - sizeof - (soft_msg->param_value)); - soft_rsp_msg->rsp_code = NSFW_EXIT_SUCCESS; - } - else - { - NSFW_LOGERR ("set soft failed!]soft=%u", soft_msg->param_name); - soft_rsp_msg->rsp_code = NSFW_EXIT_FAILED; - } - - (void) nsfw_mgr_send_msg (rsp_msg); - nsfw_mgr_msg_free (rsp_msg); - return TRUE; -} - -int nsfw_softparam_module_init (void *param); -int -nsfw_softparam_module_init (void *param) -{ - u8 proc_type = (u8) ((long long) param); - NSFW_LOGINF ("softparam module init]type=%u", proc_type); - switch (proc_type) - { - case NSFW_PROC_MAIN: - (void) nsfw_mgr_reg_msg_fun (MGR_MSG_SOF_PAR_REQ, - nsfw_softparam_msg_proc); - return 0; - default: - if (proc_type < NSFW_PROC_MAX) - { - break; - } - return -1; - } - - return 0; -} - -/* *INDENT-OFF* */ -NSFW_MODULE_NAME (NSFW_SOFT_PARAM_MODULE) -NSFW_MODULE_PRIORITY (99) -NSFW_MODULE_INIT (nsfw_softparam_module_init) -/* *INDENT-ON* */ - -#ifdef __cplusplus -/* *INDENT-OFF* */ -} -/* *INDENT-ON* */ -#endif /* __cplusplus */ |