nmh-workers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Ralph Corderoy
Subject: Re: [nmh-workers] closefds() _before_ fork?
Date: Wed, 24 Apr 2019 11:48:36 +0100

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]