From fd9219c855b9f9286c2412deba3dbedd7c3e96e3 Mon Sep 17 00:00:00 2001 From: Lemmy Huang Date: Mon, 22 May 2023 14:37:14 +0800 Subject: [PATCH] cleancode: refactor gazelle_posix_api.h Signed-off-by: Lemmy Huang --- src/api/gazelle_posix_api.c | 106 +++++++++++++++----------------- src/include/gazelle_posix_api.h | 63 ++++++++++--------- 2 files changed, 81 insertions(+), 88 deletions(-) diff --git a/src/api/gazelle_posix_api.c b/src/api/gazelle_posix_api.c index 219769a..1bff6d9 100644 --- a/src/api/gazelle_posix_api.c +++ b/src/api/gazelle_posix_api.c @@ -31,38 +31,24 @@ */ #include -#include -#include +// #include +// #include +// #include #include -#include -#include - -#include - -#include "lwip/err.h" -#include "lwipsock.h" #include "gazelle_posix_api.h" -posix_api_t *posix_api; -posix_api_t posix_api_val; - -static int chld_is_epfd(int fd) -{ - return 0; -} +posix_api_t *posix_api = NULL; +static posix_api_t posix_api_val; void posix_api_fork(void) { /* lstack helper api */ - posix_api->ues_posix = 1; - posix_api->is_epfd = chld_is_epfd; + posix_api->use_kernel = 1; } - int posix_api_init(void) { -/* the symbol we use here won't be NULL, so we don't need dlerror() - to test error */ +/* the symbol we use here won't be NULL, so we don't need dlerror() to test error */ #define CHECK_DLSYM_RET_RETURN(ret) do { \ if ((ret) == NULL) \ goto err_out; \ @@ -73,46 +59,50 @@ int posix_api_init(void) void *__restrict handle = RTLD_NEXT; /* glibc standard api */ - CHECK_DLSYM_RET_RETURN(posix_api->socket_fn = dlsym(handle, "socket")); - CHECK_DLSYM_RET_RETURN(posix_api->accept_fn = dlsym(handle, "accept")); - CHECK_DLSYM_RET_RETURN(posix_api->accept4_fn = dlsym(handle, "accept4")); - CHECK_DLSYM_RET_RETURN(posix_api->bind_fn = dlsym(handle, "bind")); - CHECK_DLSYM_RET_RETURN(posix_api->listen_fn = dlsym(handle, "listen")); - CHECK_DLSYM_RET_RETURN(posix_api->connect_fn = dlsym(handle, "connect")); - CHECK_DLSYM_RET_RETURN(posix_api->setsockopt_fn = dlsym(handle, "setsockopt")); - CHECK_DLSYM_RET_RETURN(posix_api->getsockopt_fn = dlsym(handle, "getsockopt")); - CHECK_DLSYM_RET_RETURN(posix_api->getpeername_fn = dlsym(handle, "getpeername")); - CHECK_DLSYM_RET_RETURN(posix_api->getsockname_fn = dlsym(handle, "getsockname")); - CHECK_DLSYM_RET_RETURN(posix_api->shutdown_fn = dlsym(handle, "shutdown")); - CHECK_DLSYM_RET_RETURN(posix_api->close_fn = dlsym(handle, "close")); - CHECK_DLSYM_RET_RETURN(posix_api->read_fn = dlsym(handle, "read")); - CHECK_DLSYM_RET_RETURN(posix_api->readv_fn = dlsym(handle, "readv")); - CHECK_DLSYM_RET_RETURN(posix_api->write_fn = dlsym(handle, "write")); - CHECK_DLSYM_RET_RETURN(posix_api->writev_fn = dlsym(handle, "writev")); - CHECK_DLSYM_RET_RETURN(posix_api->recv_fn = dlsym(handle, "recv")); - CHECK_DLSYM_RET_RETURN(posix_api->send_fn = dlsym(handle, "send")); - CHECK_DLSYM_RET_RETURN(posix_api->recv_msg = dlsym(handle, "recvmsg")); - CHECK_DLSYM_RET_RETURN(posix_api->send_msg = dlsym(handle, "sendmsg")); - CHECK_DLSYM_RET_RETURN(posix_api->recv_from = dlsym(handle, "recvfrom")); - CHECK_DLSYM_RET_RETURN(posix_api->send_to = dlsym(handle, "sendto")); - CHECK_DLSYM_RET_RETURN(posix_api->fcntl_fn = dlsym(handle, "fcntl")); - CHECK_DLSYM_RET_RETURN(posix_api->fcntl64_fn = dlsym(handle, "fcntl64")); - CHECK_DLSYM_RET_RETURN(posix_api->pipe_fn = dlsym(handle, "pipe")); - CHECK_DLSYM_RET_RETURN(posix_api->epoll_create_fn = dlsym(handle, "epoll_create")); - CHECK_DLSYM_RET_RETURN(posix_api->epoll_create1_fn = dlsym(handle, "epoll_create1")); - CHECK_DLSYM_RET_RETURN(posix_api->epoll_ctl_fn = dlsym(handle, "epoll_ctl")); - CHECK_DLSYM_RET_RETURN(posix_api->epoll_wait_fn = dlsym(handle, "epoll_wait")); - CHECK_DLSYM_RET_RETURN(posix_api->fork_fn = dlsym(handle, "fork")); - CHECK_DLSYM_RET_RETURN(posix_api->eventfd_fn = dlsym(handle, "eventfd")); - CHECK_DLSYM_RET_RETURN(posix_api->sigaction_fn = dlsym(handle, "sigaction")); - CHECK_DLSYM_RET_RETURN(posix_api->poll_fn = dlsym(handle, "poll")); - CHECK_DLSYM_RET_RETURN(posix_api->ioctl_fn = dlsym(handle, "ioctl")); + CHECK_DLSYM_RET_RETURN(posix_api->shutdown_fn = dlsym(handle, "shutdown")); + CHECK_DLSYM_RET_RETURN(posix_api->close_fn = dlsym(handle, "close")); + CHECK_DLSYM_RET_RETURN(posix_api->socket_fn = dlsym(handle, "socket")); + CHECK_DLSYM_RET_RETURN(posix_api->connect_fn = dlsym(handle, "connect")); + CHECK_DLSYM_RET_RETURN(posix_api->bind_fn = dlsym(handle, "bind")); + CHECK_DLSYM_RET_RETURN(posix_api->listen_fn = dlsym(handle, "listen")); + CHECK_DLSYM_RET_RETURN(posix_api->accept_fn = dlsym(handle, "accept")); + CHECK_DLSYM_RET_RETURN(posix_api->accept4_fn = dlsym(handle, "accept4")); + + CHECK_DLSYM_RET_RETURN(posix_api->getpeername_fn = dlsym(handle, "getpeername")); + CHECK_DLSYM_RET_RETURN(posix_api->getsockname_fn = dlsym(handle, "getsockname")); + CHECK_DLSYM_RET_RETURN(posix_api->getsockopt_fn = dlsym(handle, "getsockopt")); + CHECK_DLSYM_RET_RETURN(posix_api->setsockopt_fn = dlsym(handle, "setsockopt")); + + CHECK_DLSYM_RET_RETURN(posix_api->read_fn = dlsym(handle, "read")); + CHECK_DLSYM_RET_RETURN(posix_api->write_fn = dlsym(handle, "write")); + CHECK_DLSYM_RET_RETURN(posix_api->readv_fn = dlsym(handle, "readv")); + CHECK_DLSYM_RET_RETURN(posix_api->writev_fn = dlsym(handle, "writev")); + CHECK_DLSYM_RET_RETURN(posix_api->recv_fn = dlsym(handle, "recv")); + CHECK_DLSYM_RET_RETURN(posix_api->send_fn = dlsym(handle, "send")); + CHECK_DLSYM_RET_RETURN(posix_api->recvmsg_fn = dlsym(handle, "recvmsg")); + CHECK_DLSYM_RET_RETURN(posix_api->sendmsg_fn = dlsym(handle, "sendmsg")); + CHECK_DLSYM_RET_RETURN(posix_api->recvfrom_fn = dlsym(handle, "recvfrom")); + CHECK_DLSYM_RET_RETURN(posix_api->sendto_fn = dlsym(handle, "sendto")); + + CHECK_DLSYM_RET_RETURN(posix_api->poll_fn = dlsym(handle, "poll")); + CHECK_DLSYM_RET_RETURN(posix_api->epoll_create_fn = dlsym(handle, "epoll_create")); + CHECK_DLSYM_RET_RETURN(posix_api->epoll_create1_fn = dlsym(handle, "epoll_create1")); + CHECK_DLSYM_RET_RETURN(posix_api->epoll_ctl_fn = dlsym(handle, "epoll_ctl")); + CHECK_DLSYM_RET_RETURN(posix_api->epoll_wait_fn = dlsym(handle, "epoll_wait")); + CHECK_DLSYM_RET_RETURN(posix_api->eventfd_fn = dlsym(handle, "eventfd")); + + CHECK_DLSYM_RET_RETURN(posix_api->ioctl_fn = dlsym(handle, "ioctl")); + CHECK_DLSYM_RET_RETURN(posix_api->fcntl_fn = dlsym(handle, "fcntl")); + CHECK_DLSYM_RET_RETURN(posix_api->fcntl64_fn = dlsym(handle, "fcntl64")); + + CHECK_DLSYM_RET_RETURN(posix_api->sigaction_fn = dlsym(handle, "sigaction")); + CHECK_DLSYM_RET_RETURN(posix_api->fork_fn = dlsym(handle, "fork")); /* support fork */ - posix_api->ues_posix = 1; - return ERR_OK; + posix_api->use_kernel = 1; + return 0; err_out: - return ERR_MEM; + return -1; #undef CHECK_DLSYM_RET_RETURN } diff --git a/src/include/gazelle_posix_api.h b/src/include/gazelle_posix_api.h index 6df414b..e9a7633 100644 --- a/src/include/gazelle_posix_api.h +++ b/src/include/gazelle_posix_api.h @@ -33,57 +33,60 @@ #ifndef _GAZELLE_POSIX_API_H_ #define _GAZELLE_POSIX_API_H_ +#include #include #include #include #include +// #include typedef struct { - void *handle; - int (*socket_fn)(int domain, int type, int protocol); - int (*accept_fn)(int s, struct sockaddr*, socklen_t*); - int (*accept4_fn)(int s, struct sockaddr *addr, socklen_t *addrlen, int flags); - int (*bind_fn)(int s, const struct sockaddr*, socklen_t); - int (*listen_fn)(int s, int backlog); - int (*connect_fn)(int s, const struct sockaddr *name, socklen_t namelen); - int (*getpeername_fn)(int s, struct sockaddr *name, socklen_t *namelen); - int (*getsockname_fn)(int s, struct sockaddr *name, socklen_t *namelen); - int (*setsockopt_fn)(int s, int level, int optname, const void *optval, socklen_t optlen); - int (*getsockopt_fn)(int s, int level, int optname, void *optval, socklen_t *optlen); - int (*shutdown_fn)(int s, int how); + int (*shutdown_fn)(int fd, int how); int (*close_fn)(int fd); - pid_t (*fork_fn)(void); + int (*socket_fn)(int domain, int type, int protocol); + int (*connect_fn)(int fd, const struct sockaddr *name, socklen_t namelen); + int (*bind_fn)(int fd, const struct sockaddr*, socklen_t); + int (*listen_fn)(int fd, int backlog); + int (*accept_fn)(int fd, struct sockaddr*, socklen_t*); + int (*accept4_fn)(int fd, struct sockaddr *addr, socklen_t *addrlen, int flags); + + int (*getpeername_fn)(int fd, struct sockaddr *name, socklen_t *namelen); + int (*getsockname_fn)(int fd, struct sockaddr *name, socklen_t *namelen); + int (*getsockopt_fn)(int fd, int level, int optname, void *optval, socklen_t *optlen); + int (*setsockopt_fn)(int fd, int level, int optname, const void *optval, socklen_t optlen); + ssize_t (*read_fn)(int fd, void *mem, size_t len); - ssize_t (*readv_fn)(int s, const struct iovec *iov, int iovcnt); ssize_t (*write_fn)(int fd, const void *data, size_t len); - ssize_t (*writev_fn)(int s, const struct iovec *iov, int iovcnt); - ssize_t (*recv_fn)(int sockfd, void *buf, size_t len, int flags); - ssize_t (*send_fn)(int sockfd, const void *buf, size_t len, int flags); - ssize_t (*recv_msg)(int sockfd, const struct msghdr *msg, int flags); - ssize_t (*send_msg)(int sockfd, const struct msghdr *msg, int flags); - ssize_t (*recv_from)(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); - ssize_t (*send_to)(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, - socklen_t addrlen); - int (*fcntl_fn)(int fd, int cmd, ...); - int (*fcntl64_fn)(int fd, int cmd, ...); - int (*pipe_fn)(int pipefd[2]); + ssize_t (*readv_fn)(int fd, const struct iovec *iov, int iovcnt); + ssize_t (*writev_fn)(int fd, const struct iovec *iov, int iovcnt); + ssize_t (*recv_fn)(int fd, void *buf, size_t len, int flags); + ssize_t (*send_fn)(int fd, const void *buf, size_t len, int flags); + ssize_t (*recvmsg_fn)(int fd, const struct msghdr *msg, int flags); + ssize_t (*sendmsg_fn)(int fd, const struct msghdr *msg, int flags); + ssize_t (*recvfrom_fn)(int fd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen); + ssize_t (*sendto_fn)(int fd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); + + int (*poll_fn)(struct pollfd *fds, nfds_t nfds, int timeout); int (*epoll_create_fn)(int size); int (*epoll_create1_fn)(int size); int (*epoll_ctl_fn)(int epfd, int op, int fd, struct epoll_event *event); int (*epoll_wait_fn)(int epfd, struct epoll_event *events, int maxevents, int timeout); + int (*epoll_close_fn)(int epfd); int (*eventfd_fn)(unsigned int initval, int flags); - int (*is_epfd)(int fd); - int (*sigaction_fn)(int signum, const struct sigaction *act, struct sigaction *oldact); - int (*poll_fn)(struct pollfd *fds, nfds_t nfds, int timeout); + int (*ioctl_fn)(int fd, int cmd, ...); + int (*fcntl_fn)(int fd, int cmd, ...); + int (*fcntl64_fn)(int fd, int cmd, ...); + + int (*sigaction_fn)(int signum, const struct sigaction *act, struct sigaction *oldact); + pid_t (*fork_fn)(void); - int ues_posix; + int use_kernel; } posix_api_t; extern posix_api_t *posix_api; int posix_api_init(void); -void posix_api_free(void); void posix_api_fork(void); #endif /* _GAZELLE_POSIX_API_H_ */ -- 2.22.0.windows.1