nmh-workers
[Top] [All Lists]

Re: [nmh-workers] closefds() _before_ fork?

2019-04-24 05:48:49
Hi Ken,

they called getdtablesize() on Linux, which it seems returns a
smaller number than getrlimit().

That's surprising.  I thought getdtablesize() was effectively

    return getrlimit(RLIMIT_NOFILE, &ru) < 0 ? OPEN_MAX : ru.rlim_cur;

Hey, I don't make the news, I just report it.  If you look at the
bug fix referenced in that thread, the "fix" was to make sure that Linux
wasn't detected as SVR4.  That makes it so it calls getdtablesize() instead
of getrlimit().  My understanding of getdtablesize() matches yours, but
I can't see how that "fix" could make this problem better otherwise.

screen-4.6.2 has

    void
    closeallfiles(except)
    int except;
    {
      int f;
    #ifdef SVR4
      struct rlimit rl;

      if ((getrlimit(RLIMIT_NOFILE, &rl) == 0) && rl.rlim_max != RLIM_INFINITY)
        f = rl.rlim_max;
      else
    #endif /* SVR4 */
    #if defined(SYSV) && defined(NOFILE) && !defined(ISC)
      f = NOFILE;
    #else /* SYSV && !ISC */
      f = getdtablesize();
    #endif /* SYSV && !ISC */
      while (--f > 2)
        if (f != except)
          close(f);
    }

When wrongly identifying Linux as SVR4, it uses rl.rlim_max whereas
getdtablesize() uses ru.rlim_cur.  Here, there's a ×512 difference.

    $ cat nfd.c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/resource.h>
    #include <sys/param.h>

    int main(void)
    {
        struct rlimit rl;

        printf("get()\t%d\n", getrlimit(RLIMIT_NOFILE, &rl));
        printf("rl.cur\t%lu\n", rl.rlim_cur);
        printf("rl.max\t%lu\n", rl.rlim_max);
        printf("INF\t%#lx\n", RLIM_INFINITY);
        printf("NOFILE\t%d\n", NOFILE);
        printf("table()\t%d\n", getdtablesize());

        return 0;
    }
    $
    $ ./a.out
    get()   0
    rl.cur  1024
    rl.max  524288
    INF     0xffffffffffffffff
    NOFILE  256
    table() 1024
    $

-- 
Cheers, Ralph.

-- 
nmh-workers
https://lists.nongnu.org/mailman/listinfo/nmh-workers

<Prev in Thread] Current Thread [Next in Thread>