Re: O_CLOEXEC Lassi Kortela 26 Jul 2020 19:16 UTC

> Manually calling close() in a loop is slow and it's not clear how many
> fd's to try.

An emulation would go something like this:

static int
closefrom(int n)
{
     int max;

     max = sysconf(_SC_OPEN_MAX);
     while ((n >= 0) && (n <= max)) {
         for (;;) {
             if (close(n) == 0) {
                 break;
             }
             if (errno == EBADF) {
                 break;
             }
             if (errno != EINTR) {
                 return -1;
             }
         }
         n++;
     }
     return 0;
}