From e8e5434cc4f5cc3c322659792cc1850a5c5b26b8 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Fri, 20 Jun 2025 09:40:18 +0900 Subject: posix: Clean up get_max_fds implementation. * src/posix-io.c (get_max_fds): Return type is int. Ifdef-out TRACE. Remove call to sysconf. Use long for R and POS. Use aligned buffer for struct linux_dirent64. -- GnuPG-bug-id: 7694 Signed-off-by: NIIBE Yutaka --- src/posix-io.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/posix-io.c b/src/posix-io.c index a422d8f6..cc873626 100644 --- a/src/posix-io.c +++ b/src/posix-io.c @@ -354,7 +354,7 @@ _gpgme_io_set_nonblocking (int fd) } -static long int +static int get_max_fds (void) { const char *source = NULL; @@ -373,9 +373,11 @@ get_max_fds (void) #ifdef USE_LINUX_GETDENTS { int dir_fd; - char dir_buf[DIR_BUF_SIZE]; + struct linux_dirent64 buf[(DIR_BUF_SIZE+sizeof (struct linux_dirent64)-1) + /sizeof (struct linux_dirent64)]; + char *dir_buf = (char *)buf; /* BUF aligned for struct linux_dirent64 */ struct linux_dirent64 *dir_entry; - int r, pos; + long r, pos; const char *s; int x; @@ -384,7 +386,7 @@ get_max_fds (void) { for (;;) { - r = syscall(SYS_getdents64, dir_fd, dir_buf, DIR_BUF_SIZE); + r = syscall(SYS_getdents64, dir_fd, dir_buf, sizeof (buf)); if (r == -1) { /* Fall back to other methods. */ @@ -442,18 +444,6 @@ get_max_fds (void) } } #endif -#ifdef _SC_OPEN_MAX - if (fds == -1) - { - long int scres; - scres = sysconf (_SC_OPEN_MAX); - if (scres >= 0) - { - source = "_SC_OPEN_MAX"; - return scres; - } - } -#endif #ifdef OPEN_MAX if (fds == -1) { @@ -463,7 +453,7 @@ get_max_fds (void) #endif #if !defined(RLIMIT_NOFILE) && !defined(RLIMIT_OFILE) \ - && !defined(_SC_OPEN_MAX) && !defined(OPEN_MAX) + && !defined(OPEN_MAX) #warning "No known way to get the maximum number of file descriptors." #endif if (fds == -1) @@ -483,7 +473,11 @@ get_max_fds (void) } #endif +#if 0 /* This may result hang in multi-thread application. */ TRACE (DEBUG_SYSIO, "gpgme:max_fds", NULL, "max fds=%ld (%s)", fds, source); +#else + (void)source; +#endif return fds; } -- cgit v1.2.3