diff options
Diffstat (limited to 'src/framework/common/base/liblinuxapi')
3 files changed, 71 insertions, 598 deletions
diff --git a/src/framework/common/base/liblinuxapi/base_linux_api_declare.h b/src/framework/common/base/liblinuxapi/base_linux_api_declare.h index 943ae04..3f96e6c 100644 --- a/src/framework/common/base/liblinuxapi/base_linux_api_declare.h +++ b/src/framework/common/base/liblinuxapi/base_linux_api_declare.h @@ -14,38 +14,42 @@ * limitations under the License. */ -BASE_MK_DECL (int, socket, (int, int, int)); -BASE_MK_DECL (int, bind, (int, const struct sockaddr *, socklen_t)); -BASE_MK_DECL (int, listen, (int, int)); -BASE_MK_DECL (int, shutdown, (int, int)); -BASE_MK_DECL (int, getsockname, (int, struct sockaddr *, socklen_t *)); -BASE_MK_DECL (int, getpeername, (int, struct sockaddr *, socklen_t *)); -BASE_MK_DECL (int, getsockopt, (int, int, int, void *, socklen_t *)); -BASE_MK_DECL (int, setsockopt, (int, int, int, const void *, socklen_t)); -BASE_MK_DECL (int, accept, (int, struct sockaddr *, socklen_t *)); -BASE_MK_DECL (int, accept4, (int, struct sockaddr *, socklen_t *, int flags)); -BASE_MK_DECL (int, connect, (int, const struct sockaddr *, socklen_t)); -BASE_MK_DECL (ssize_t, recv, (int, void *, size_t, int)); -BASE_MK_DECL (ssize_t, send, (int, const void *, size_t, int)); -BASE_MK_DECL (ssize_t, read, (int, void *, size_t)); -BASE_MK_DECL (ssize_t, write, (int, const void *, size_t)); -BASE_MK_DECL (ssize_t, writev, (int, const struct iovec *, int)); -BASE_MK_DECL (ssize_t, readv, (int, const struct iovec *, int)); -BASE_MK_DECL (ssize_t, sendto, - (int, const void *, size_t, int, const struct sockaddr *, - socklen_t)); -BASE_MK_DECL (ssize_t, recvfrom, - (int, void *, size_t, int, struct sockaddr *, socklen_t *)); -BASE_MK_DECL (ssize_t, sendmsg, (int, const struct msghdr *, int flags)); -BASE_MK_DECL (ssize_t, recvmsg, (int, struct msghdr *, int flags)); -BASE_MK_DECL (int, close, (int)); -BASE_MK_DECL (int, select, - (int, fd_set *, fd_set *, fd_set *, struct timeval *)); -BASE_MK_DECL (int, ioctl, (int, unsigned long, unsigned long)); -BASE_MK_DECL (int, fcntl, (int, int, unsigned long)); -BASE_MK_DECL (int, epoll_create, (int)); -BASE_MK_DECL (int, epoll_create1, (int)); -BASE_MK_DECL (int, epoll_ctl, (int, int, int, struct epoll_event *)); -BASE_MK_DECL (int, epoll_wait, (int, struct epoll_event *, int, int)); -BASE_MK_DECL (pid_t, fork, (void)); +BASE_MK_DECL(int, socket, (int, int, int)); +BASE_MK_DECL(int, bind, (int, const struct sockaddr *, socklen_t)); +BASE_MK_DECL(int, listen, (int, int)); +BASE_MK_DECL(int, shutdown, (int, int)); +BASE_MK_DECL(int, getaddrinfo, + (const char *, const char *, const struct addrinfo *, + struct addrinfo **)); +BASE_MK_DECL(int, getsockname, (int, struct sockaddr *, socklen_t *)); +BASE_MK_DECL(int, getpeername, (int, struct sockaddr *, socklen_t *)); +BASE_MK_DECL(int, getsockopt, (int, int, int, void *, socklen_t *)); +BASE_MK_DECL(int, setsockopt, (int, int, int, const void *, socklen_t)); +BASE_MK_DECL(int, accept, (int, struct sockaddr *, socklen_t *)); +BASE_MK_DECL(int, accept4, (int, struct sockaddr *, socklen_t *, int flags)); +BASE_MK_DECL(int, connect, (int, const struct sockaddr *, socklen_t)); +BASE_MK_DECL(ssize_t, recv, (int, void *, size_t, int)); +BASE_MK_DECL(ssize_t, send, (int, const void *, size_t, int)); +BASE_MK_DECL(ssize_t, read, (int, void *, size_t)); +BASE_MK_DECL(ssize_t, write, (int, const void *, size_t)); +BASE_MK_DECL(ssize_t, writev, (int, const struct iovec *, int)); +BASE_MK_DECL(ssize_t, readv, (int, const struct iovec *, int)); +BASE_MK_DECL(ssize_t, sendto, + (int, const void *, size_t, int, const struct sockaddr *, + socklen_t)); +BASE_MK_DECL(ssize_t, recvfrom, + (int, void *, size_t, int, struct sockaddr *, socklen_t *)); +BASE_MK_DECL(ssize_t, sendmsg, (int, const struct msghdr *, int flags)); +BASE_MK_DECL(ssize_t, recvmsg, (int, struct msghdr *, int flags)); +BASE_MK_DECL(int, close, (int)); +BASE_MK_DECL(int, select, + (int, fd_set *, fd_set *, fd_set *, struct timeval *)); +BASE_MK_DECL(int, ioctl, (int, unsigned long, unsigned long)); +BASE_MK_DECL(int, fcntl, (int, int, unsigned long)); +BASE_MK_DECL(int, epoll_create, (int)); +BASE_MK_DECL(int, epoll_create1, (int)); +//BASE_MK_DECL(int , poll , (struct pollfd *, nfds_t, int)); +BASE_MK_DECL(int, epoll_ctl, (int, int, int, struct epoll_event *)); +BASE_MK_DECL(int, epoll_wait, (int, struct epoll_event *, int, int)); +BASE_MK_DECL(pid_t, fork, (void)); #undef BASE_MK_DECL diff --git a/src/framework/common/base/liblinuxapi/nsfw_base_linux_api.c b/src/framework/common/base/liblinuxapi/nsfw_base_linux_api.c index 4e59d68..734a09f 100644 --- a/src/framework/common/base/liblinuxapi/nsfw_base_linux_api.c +++ b/src/framework/common/base/liblinuxapi/nsfw_base_linux_api.c @@ -15,7 +15,6 @@ */ #include "nsfw_base_linux_api.h" -#include "nstack_log.h" #include <pthread.h> #include <stdio.h> #include <errno.h> @@ -39,9 +38,9 @@ typedef enum { - BASE_STATE_INIT, - BASE_STATE_SUCCESS, - BASE_STATE_FAIL + BASE_STATE_INIT, + BASE_STATE_SUCCESS, + BASE_STATE_FAIL } nsfw_base_state; typedef struct __base_linux_api @@ -50,27 +49,24 @@ typedef struct __base_linux_api #include "base_linux_api_declare.h" } base_linux_api; -nsfw_base_state g_nsfw_module_state = BASE_STATE_INIT; +nsfw_base_state g_nsfw_mudule_state = BASE_STATE_INIT; pthread_mutex_t g_nsfw_init_mutex = PTHREAD_MUTEX_INITIALIZER; base_linux_api g_nsfw_posix_api = { 0 }; void *g_linux_lib_handle = (void *) 0; -int -nsfw_posix_symbol_load () +int nsfw_posix_symbol_load() { - g_linux_lib_handle = dlopen ("libc.so.6", RTLD_NOW | RTLD_GLOBAL); - if ((void *) 0 == g_linux_lib_handle) + g_linux_lib_handle = dlopen("libc.so.6", RTLD_NOW | RTLD_GLOBAL); + if ((void *) 0 == g_linux_lib_handle) { - /* optimize dlopen err print */ - NSSOC_LOGERR ("cannot dlopen libc.so.6] err_string=%s", dlerror ()); - return NSFW_BASE_FAIL; + return NSFW_BASE_FAIL; } #define BASE_MK_DECL(ret, fn, args) \ g_nsfw_posix_api.pf##fn = (typeof(g_nsfw_posix_api.pf##fn))dlsym(g_linux_lib_handle, #fn); #include <base_linux_api_declare.h> - return NSFW_BASE_OK; + return NSFW_BASE_OK; } /***************************************************************** @@ -78,549 +74,191 @@ Parameters : void Return : Description : linux posix api init with threadonce *****************************************************************/ -static inline int -nsfw_posix_api_init () +static inline int nsfw_posix_api_init() { - int iret = NSFW_BASE_OK; + int iret = NSFW_BASE_OK; - /*if init already, just return success, if init fail before, just return err */ - if (BASE_STATE_INIT != g_nsfw_module_state) + /*if init already, just return success, if init fail before, just return err */ + if (BASE_STATE_INIT != g_nsfw_mudule_state) { - return (BASE_STATE_SUCCESS == - g_nsfw_module_state ? NSFW_BASE_OK : NSFW_BASE_FAIL); + return (BASE_STATE_SUCCESS == + g_nsfw_mudule_state ? NSFW_BASE_OK : NSFW_BASE_FAIL); } - (void) pthread_mutex_lock (&g_nsfw_init_mutex); + (void) pthread_mutex_lock(&g_nsfw_init_mutex); - /*if init already, just return success, if init fail before, just return err */ - if (BASE_STATE_INIT != g_nsfw_module_state) + /*if init already, just return success, if init fail before, just return err */ + if (BASE_STATE_INIT != g_nsfw_mudule_state) { - (void) pthread_mutex_unlock (&g_nsfw_init_mutex); - return (BASE_STATE_SUCCESS == - g_nsfw_module_state ? NSFW_BASE_OK : NSFW_BASE_FAIL); + (void) pthread_mutex_unlock(&g_nsfw_init_mutex); + return (BASE_STATE_SUCCESS == + g_nsfw_mudule_state ? NSFW_BASE_OK : NSFW_BASE_FAIL); } - iret = nsfw_posix_symbol_load (); - if (NSFW_BASE_OK == iret) + iret = nsfw_posix_symbol_load(); + if (NSFW_BASE_OK == iret) { - g_nsfw_module_state = BASE_STATE_SUCCESS; + g_nsfw_mudule_state = BASE_STATE_SUCCESS; } - else + else { - g_nsfw_module_state = BASE_STATE_FAIL; + g_nsfw_mudule_state = BASE_STATE_FAIL; } - (void) pthread_mutex_unlock (&g_nsfw_init_mutex); - return iret; + (void) pthread_mutex_unlock(&g_nsfw_init_mutex); + return iret; } /* *INDENT-OFF* */ -/***************************************************************************** -* Prototype : nsfw_base_socket -* Description : linux socket api -* Input : int a -* int b -* int c -* Output : None -* Return Value : int -* Calls : -* Called By : - -* -*****************************************************************************/ int nsfw_base_socket(int a, int b, int c) { nsfw_call_ret(socket, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_bind -* Description : linux fd bind api -* Input : int a -* const struct sockaddr* b -* socklen_t c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_bind(int a, const struct sockaddr* b, socklen_t c) { nsfw_call_ret(bind, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_listen -* Description : linux fd listen api -* Input : int a -* int b -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_listen(int a, int b) { nsfw_call_ret(listen, (a, b)) } -/***************************************************************************** -* Prototype : nsfw_base_shutdown -* Description : linux shutdown api -* Input : int a -* int b -* Output : None -* Return Value : int -* Calls : -* Called By : -* -* -*****************************************************************************/ int nsfw_base_shutdown(int a, int b) { nsfw_call_ret(shutdown, (a, b)) } -/***************************************************************************** -* Prototype : nsfw_base_getsockname -* Description : linux getsockname api -* Input : int a -* struct sockaddr* b -* socklen_t* c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ +int nsfw_base_getaddrinfo(const char *a, const char *b, const struct addrinfo *c, struct addrinfo **d) +{ + nsfw_call_ret(getaddrinfo, (a, b, c, d)) +} + int nsfw_base_getsockname(int a, struct sockaddr* b, socklen_t* c) { nsfw_call_ret(getsockname, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_getpeername -* Description : linux getpername api -* Input : int a -* struct sockaddr* b -* socklen_t* c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_getpeername(int a, struct sockaddr* b, socklen_t* c) { nsfw_call_ret(getpeername, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_getsockopt -* Description : linux getsockopt api -* Input : int a -* int b -* int c -* void* d -* socklen_t* e -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_getsockopt(int a, int b, int c, void* d, socklen_t* e) { nsfw_call_ret(getsockopt, (a, b, c, d, e)) } -/***************************************************************************** -* Prototype : nsfw_base_setsockopt -* Description : linux setsockopt api -* Input : int a -* int b -* int c -* const void* d -* socklen_t e -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ int nsfw_base_setsockopt(int a, int b, int c, const void* d, socklen_t e) { nsfw_call_ret(setsockopt, (a, b, c, d, e)) } -/***************************************************************************** -* Prototype : nsfw_base_accept -* Description : linux accept api -* Input : int a -* struct sockaddr* b -* socklen_t* c -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ int nsfw_base_accept(int a, struct sockaddr* b, socklen_t* c) { nsfw_call_ret(accept, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_accept4 -* Description : linux accept4 api -* Input : int a -* struct sockaddr* b -* socklen_t* c -* int flags -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ int nsfw_base_accept4(int a, struct sockaddr* b, socklen_t* c, int flags) { nsfw_call_ret(accept4, (a, b, c, flags)) } -/***************************************************************************** -* Prototype : nsfw_base_connect -* Description : linux connect api -* Input : int a -* const struct sockaddr* b -* socklen_t c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_connect(int a, const struct sockaddr* b, socklen_t c) { nsfw_call_ret(connect, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_recv -* Description : linux recv api -* Input : int a -* void* b -* size_t c -* int d -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_recv(int a, void* b, size_t c, int d) { nsfw_call_ret(recv, (a, b, c, d)) } -/***************************************************************************** -* Prototype : nsfw_base_send -* Description : linux send api -* Input : int a -* const void* b -* size_t c -* int d -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_send(int a, const void* b, size_t c, int d) { nsfw_call_ret(send, (a, b, c, d)) } -/***************************************************************************** -* Prototype : nsfw_base_read -* Description : linux read api -* Input : int a -* void* b -* size_t c -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_read(int a, void* b, size_t c) { nsfw_call_ret(read, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_write -* Description : linux write api -* Input : int a -* const void* b -* size_t c -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_write(int a, const void* b, size_t c) { nsfw_call_ret(write, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_writev -* Description : linux writev api -* Input : int a -* const struct iovec * b -* int c -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -*****************************************************************************/ ssize_t nsfw_base_writev(int a, const struct iovec * b, int c) { nsfw_call_ret(writev, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_readv -* Description : linux readv api -* Input : int a -* const struct iovec * b -* int c -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_readv(int a, const struct iovec * b, int c) { nsfw_call_ret(readv, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_sendto -* Description : linux sendto api -* Input : int a -* const void * b -* size_t c -* int d -* const struct sockaddr *e -* socklen_t f -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -*****************************************************************************/ ssize_t nsfw_base_sendto(int a, const void * b, size_t c, int d, const struct sockaddr *e, socklen_t f) { nsfw_call_ret(sendto, (a, b, c, d, e, f)) } -/***************************************************************************** -* Prototype : nsfw_base_recvfrom -* Description : linux recvfrom api -* Input : int a -* void *b -* size_t c -* int d -* struct sockaddr *e -* socklen_t *f -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -*****************************************************************************/ ssize_t nsfw_base_recvfrom(int a, void *b, size_t c, int d,struct sockaddr *e, socklen_t *f) { nsfw_call_ret(recvfrom, (a, b, c, d, e, f)) } -/***************************************************************************** -* Prototype : nsfw_base_sendmsg -* Description : linux sendmsg api -* Input : int a -* const struct msghdr *b -* int flags -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_sendmsg(int a, const struct msghdr *b, int flags) { nsfw_call_ret(sendmsg, (a, b, flags)) } -/***************************************************************************** -* Prototype : nsfw_base_recvmsg -* Description : linux recvmsg api -* Input : int a -* struct msghdr *b -* int flags -* Output : None -* Return Value : ssize_t -* Calls : -* Called By : -* -*****************************************************************************/ ssize_t nsfw_base_recvmsg(int a, struct msghdr *b, int flags) { nsfw_call_ret(recvmsg, (a, b, flags)) } -/***************************************************************************** -* Prototype : nsfw_base_close -* Description : linux close api -* Input : int a -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_close(int a) { nsfw_call_ret(close, (a)) } -/***************************************************************************** -* Prototype : nsfw_base_select -* Description : linux select api -* Input : int a -* fd_set *b -* fd_set *c -* fd_set *d -* struct timeval *e -* Output : None -* Return Value : int -* Calls : -* Called By : -*****************************************************************************/ int nsfw_base_select(int a, fd_set *b, fd_set *c, fd_set *d, struct timeval *e) { nsfw_call_ret(select, (a, b, c, d, e)) } -/***************************************************************************** -* Prototype : nsfw_base_ioctl -* Description : linux ioctl api -* Input : int a -* unsigned long b -* unsigned long c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_ioctl(int a, unsigned long b, unsigned long c) { nsfw_call_ret(ioctl, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_fcntl -* Description : linux fcntl api -* Input : int a -* int b -* unsigned long c -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_fcntl(int a, int b, unsigned long c) { nsfw_call_ret(fcntl, (a, b, c)) } -/***************************************************************************** -* Prototype : nsfw_base_epoll_create -* Description : linux epoll_create api -* Input : int a -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_epoll_create(int a) { nsfw_call_ret(epoll_create, (a)) } -/***************************************************************************** -* Prototype : nsfw_base_epoll_create1 -* Description : linux epoll_create1 api -* Input : int a -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_epoll_create1(int a) { nsfw_call_ret(epoll_create1, (a)) } -/***************************************************************************** -* Prototype : nsfw_base_epoll_ctl -* Description : linux epoll_ctl api -* Input : int a -* int b -* int c -* struct epoll_event *d -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_epoll_ctl(int a, int b, int c, struct epoll_event *d) { nsfw_call_ret(epoll_ctl, (a, b, c, d)) } -/***************************************************************************** -* Prototype : nsfw_base_epoll_wait -* Description : linux epoll_wait api -* Input : int a -* struct epoll_event *b -* int c -* int d -* Output : None -* Return Value : int -* Calls : -* Called By : -* -*****************************************************************************/ int nsfw_base_epoll_wait(int a, struct epoll_event *b, int c, int d) { nsfw_call_ret(epoll_wait, (a, b, c, d)) } -/***************************************************************************** -* Prototype : nsfw_base_fork -* Description : linux fork api -* Input : void -* Output : None -* Return Value : pid_t -* Calls : -* Called By : -* -*****************************************************************************/ pid_t nsfw_base_fork(void) { nsfw_call_ret(fork, ()) diff --git a/src/framework/common/base/liblinuxapi/nsfw_lock_file.c b/src/framework/common/base/liblinuxapi/nsfw_lock_file.c deleted file mode 100644 index cef8d95..0000000 --- a/src/framework/common/base/liblinuxapi/nsfw_lock_file.c +++ /dev/null @@ -1,169 +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 <stdio.h> -#include <stdlib.h> -#include <errno.h> -#include <fcntl.h> -#include <unistd.h> - -#include "types.h" -#include "nstack_securec.h" -#include "nsfw_init.h" -#include "common_mem_api.h" - -#include "nstack_log.h" -#include "nsfw_maintain_api.h" -#include "nsfw_mgr_com_api.h" - -#include "nsfw_base_linux_api.h" -#ifdef __cplusplus -/* *INDENT-OFF* */ -extern "C"{ -/* *INDENT-ON* */ -#endif /* __cplusplus */ - -#define NSFW_FILE_PATH_LEN 128 -#define LOCK_FOLDER "/ip_module/" -#define LOCK_SUFFIX ".pid" - -#define read_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len)) -#define readw_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLKW, F_RDLCK, (offset), (whence), (len)) -#define write_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len)) -#define writew_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLKW, F_WRLCK, (offset), (whence), (len)) -#define un_lock(fd, offset, whence, len) nsfw_lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len)) - -i32 -nsfw_lock_reg (i32 fd, i32 cmd, i32 type, off_t offset, i32 whence, off_t len) -{ - struct flock lock_file; - lock_file.l_type = type; - lock_file.l_start = offset; - lock_file.l_whence = whence; - lock_file.l_len = len; - return (fcntl (fd, cmd, &lock_file)); -} - -/***************************************************************************** -* Prototype : nsfw_proc_start_with_lock -* Description : lock file start -* Input : u8 proc_type -* Output : None -* Return Value : i32 -* Calls : -* Called By : -*****************************************************************************/ -i32 -nsfw_proc_start_with_lock (u8 proc_type) -{ - NSFW_LOGINF ("lock_file init]type=%u", proc_type); - char *module_name = nsfw_get_proc_name (proc_type); - if (NULL == module_name) - { - NSFW_LOGERR ("proc type error]proc_type=%u", proc_type); - return 0; - } - - const char *directory = NSFW_DOMAIN_DIR; - const char *home_dir = getenv ("HOME"); - - if (getuid () != 0 && home_dir != NULL) - { - directory = home_dir; - } - - int ret; - char lock_fpath[NSFW_FILE_PATH_LEN] = { 0 }; - ret = STRCPY_S (lock_fpath, NSFW_FILE_PATH_LEN, directory); - if (EOK != ret) - { - NSFW_LOGERR ("lock init STRCPY_S failed]ret=%d", ret); - return -1; - } - - nsfw_mgr_com_mkdir_domainpath (lock_fpath); - - ret = STRCAT_S (lock_fpath, NSFW_FILE_PATH_LEN, module_name); - if (EOK != ret) - { - NSFW_LOGERR ("lock init STRCAT_S failed]ret=%d", ret); - return -1; - } - - ret = STRCAT_S (lock_fpath, NSFW_FILE_PATH_LEN, LOCK_SUFFIX); - if (EOK != ret) - { - NSFW_LOGERR ("lock init STRCAT_S failed]ret=%d", ret); - return -1; - } - - i32 fd; - if ((fd = open (lock_fpath, O_RDWR | O_CREAT, 0640)) == -1) - { /* file permission no large than 0640 */ - NSFW_LOGERR ("open lock file error!]path=%s,error = %d", lock_fpath, - errno); - return -1; - } - - 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; - } - - if (write_lock (fd, 0, SEEK_SET, 0) < 0) - { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("get lock file error!]path=%s,error = %d", lock_fpath, - errno); - return -1; - } - - char buf[32] = { 0 }; - if (ftruncate (fd, 0) < 0) - { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("ftruncate file error!]path=%s,error = %d", lock_fpath, - errno); - return -1; - } - - ret = - SNPRINTF_S (buf, sizeof (buf), sizeof (buf) - 1, "%ld", (long) getpid ()); - if (-1 == ret) - { - NSTCP_LOGERR ("SNPRINTF_S failed]ret=%d", ret); - (void) nsfw_base_close (fd); - return -1; - } - - if (write (fd, buf, strlen (buf) + 1) < 0) - { - (void) nsfw_base_close (fd); - NSFW_LOGERR ("write file error!]path=%s,error = %d", lock_fpath, errno); - return -1; - } - - return 0; -} - -#ifdef __cplusplus -/* *INDENT-OFF* */ -} -/* *INDENT-ON* */ -#endif /* __cplusplus */ |