diff --git a/native/jni/daemon/socket.c b/native/jni/daemon/socket.c index fa7b67b4a..0019861e1 100644 --- a/native/jni/daemon/socket.c +++ b/native/jni/daemon/socket.c @@ -40,23 +40,11 @@ int create_rand_socket(struct sockaddr_un *sun) { } int socket_accept(int sockfd, int timeout) { - struct timeval tv; - fd_set fds; - int rc; - - tv.tv_sec = timeout; - tv.tv_usec = 0; - FD_ZERO(&fds); - FD_SET(sockfd, &fds); - do { - rc = select(sockfd + 1, &fds, NULL, NULL, &tv); - } while (rc < 0 && errno == EINTR); - if (rc < 1) { - PLOGE("select"); - return -1; - } - - return xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC); + struct pollfd pfd = { + .fd = sockfd, + .events = POLL_IN + }; + return xpoll(&pfd, 1, timeout * 1000) <= 0 ? -1 : xaccept4(sockfd, NULL, NULL, SOCK_CLOEXEC); } /* diff --git a/native/jni/su/su_daemon.c b/native/jni/su/su_daemon.c index 4bf1a2c45..40bb5d401 100644 --- a/native/jni/su/su_daemon.c +++ b/native/jni/su/su_daemon.c @@ -168,12 +168,14 @@ static struct su_info *get_su_info(unsigned uid) { // Connect manager app_connect(addr.sun_path + 1, info); int fd = socket_accept(sockfd, 60); - - socket_send_request(fd, info); - int ret = read_int_be(fd); - info->access.policy = ret < 0 ? DENY : ret; - - close(fd); + if (fd < 0) { + info->access.policy = DENY; + } else { + socket_send_request(fd, info); + int ret = read_int_be(fd); + info->access.policy = ret < 0 ? DENY : ret; + close(fd); + } close(sockfd); } diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 6f4efc488..9387663f4 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -78,6 +79,7 @@ void *xmmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset); ssize_t xsendfile(int out_fd, int in_fd, off_t *offset, size_t count); pid_t xfork(); +int xpoll(struct pollfd *fds, nfds_t nfds, int timeout); // misc.c diff --git a/native/jni/utils/xwrap.c b/native/jni/utils/xwrap.c index 4d26bf36a..f50caa0cb 100644 --- a/native/jni/utils/xwrap.c +++ b/native/jni/utils/xwrap.c @@ -394,3 +394,11 @@ pid_t xfork() { } return ret; } + +int xpoll(struct pollfd *fds, nfds_t nfds, int timeout) { + int ret = poll(fds, nfds, timeout); + if (ret == -1) { + PLOGE("poll"); + } + return ret; +}