gspawn: safe_fdwalk for Solaris 11.4

Use F_NEXTFD/F_PREVFD for fdwalk when they are available.

Closes #2429
This commit is contained in:
Casper Dik 2021-08-01 12:45:13 -07:00 committed by Alan Coopersmith
parent 790571a2cd
commit a75ffc5112

View File

@ -1444,6 +1444,27 @@ safe_fdwalk (int (*cb)(void *data, int fd), void *data)
#endif
#if defined(__sun__) && defined(F_PREVFD) && defined(F_NEXTFD)
/*
* Solaris 11.4 has a signal-safe way which allows
* us to find all file descriptors in a process.
*
* fcntl(fd, F_NEXTFD, maxfd)
* - returns the first allocated file descriptor <= maxfd > fd.
*
* fcntl(fd, F_PREVFD)
* - return highest allocated file descriptor < fd.
*/
open_max = fcntl (INT_MAX, F_PREVFD); /* find the maximum fd */
if (open_max < 0) /* No open files */
return 0;
for (fd = -1; (fd = fcntl (fd, F_NEXTFD, open_max)) != -1; )
if ((res = cb (data, fd)) != 0 || fd == open_max)
break;
#else
#if 0 && defined(HAVE_SYS_RESOURCE_H)
/* Use getrlimit() function provided by the system if it is known to be
* async-signal safe.
@ -1477,6 +1498,7 @@ safe_fdwalk (int (*cb)(void *data, int fd), void *data)
for (fd = 0; fd < open_max; fd++)
if ((res = cb (data, fd)) != 0)
break;
#endif
return res;
#endif