From a2776a26724b21442371cfecccc9159b398a7b11 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Wed, 13 Sep 2023 19:14:54 +0900 Subject: [PATCH] Use sysconf() instead of global variable Some process changes the maximum open file descriptor number itself. In this case, the MAX_FDS must be changed. Change-Id: I70c3ffe91c78a9068572c9c4f6471701fea0045d Signed-off-by: Hwankyu Jhun --- src/aul_sock.cc | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/aul_sock.cc b/src/aul_sock.cc index eea2667..c1d6e10 100644 --- a/src/aul_sock.cc +++ b/src/aul_sock.cc @@ -48,7 +48,6 @@ constexpr const int MAX_NR_OF_DESCRIPTORS = 2; constexpr const int MAX_PAYLOAD_SIZE = 1024 * 1024 * 1; constexpr const char PATH_AUL_SOCKET_TIMEOUT[] = "/run/aul/.socket_timeout"; constexpr const char PATH_AMD_SOCK[] = "/run/aul/daemons/.amd-sock"; -int MAX_FDS = sysconf(_SC_OPEN_MAX); constexpr unsigned int SOCKET_TIMEOUT_MIN = 5000U; constexpr unsigned int SOCKET_TIMEOUT_MAX = 30000U; @@ -415,7 +414,7 @@ extern "C" API struct timeval aul_sock_get_rcv_timeout(void) { } extern "C" API int aul_sock_set_sock_option(int fd, int cli) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } @@ -476,7 +475,7 @@ extern "C" API int aul_sock_create_server(int pid, uid_t uid) { extern "C" API int aul_sock_send_raw_with_fd(int fd, int cmd, unsigned char* kb_data, int datalen, int opt) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } @@ -486,7 +485,7 @@ extern "C" API int aul_sock_send_raw_with_fd(int fd, int cmd, extern "C" API int aul_sock_send_bundle_with_fd(int fd, int cmd, bundle* kb, int opt) { - if (fd < 0 || fd >= MAX_FDS || kb == nullptr) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX) || kb == nullptr) { _E("Invalid parameter"); return -EINVAL; } @@ -532,7 +531,7 @@ extern "C" API int aul_sock_send_bundle(int pid, uid_t uid, int cmd, extern "C" API app_pkt_t* aul_sock_recv_pkt(int fd, int* clifd, struct ucred* cred) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return nullptr; } @@ -568,7 +567,7 @@ extern "C" API int aul_sock_recv_reply_pkt(int fd, app_pkt_t** ret_pkt) { extern "C" API int aul_sock_recv_reply_sock_fd(int fd, int (*ret_fd)[2], int fd_size) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } @@ -623,7 +622,7 @@ extern "C" API int aul_sock_create_launchpad_client(const char* pad_type, extern "C" API int aul_sock_recv_pkt_with_cb(int fd, void (*callback)(app_pkt_t* pkt, void* user_data), void* user_data) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -1; } @@ -674,7 +673,7 @@ extern "C" API int aul_sock_recv_pkt_with_cb(int fd, } extern "C" API int aul_sock_recv_result_with_fd(int fd) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } @@ -692,7 +691,7 @@ extern "C" API int aul_sock_recv_result_with_fd(int fd) { } extern "C" API int aul_sock_destroy_server(int fd) { - if (fd > -1 && fd < MAX_FDS) + if (fd > -1 && fd < sysconf(_SC_OPEN_MAX)) close(fd); if (getuid() < REGULAR_UID_MIN) { @@ -714,7 +713,7 @@ extern "C" API int aul_sock_send_result(int fd, int res) { } extern "C" API int aul_sock_send_result_v2(int fd, int res, bool do_close) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } @@ -731,7 +730,7 @@ extern "C" API int aul_sock_send_result_v2(int fd, int res, bool do_close) { extern "C" API int aul_sock_recv_reply_pkt_v2(int fd, app_pkt_t** pkt, bool do_close) { - if (fd < 0 || fd >= MAX_FDS) { + if (fd < 0 || fd >= sysconf(_SC_OPEN_MAX)) { _E("Invalid parameter"); return -EINVAL; } -- 2.7.4