git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: RLIMIT_NOFILE fallback

From
Jeff King <peff@peff.net>
Date
Dec 18, 2013, 21:28 UTC
Message-ID
<20131218212847.GA13685@sigill.intra.peff.net>
In-Reply-To
<xmqq61qmhrb3.fsf@gitster.dls.corp.google.com>
On Wed, Dec 18, 2013 at 11:50:24AM -0800, Junio C Hamano wrote:
Show 26 quoted lines
> -------------------------------- 8< ------------------------------
> 
> static unsigned int get_max_fd_limit(void)
> {
> #ifdef RLIMIT_NOFILE
> 	struct rlimit lim;
> 
> 	if (!getrlimit(RLIMIT_NOFILE, &lim))
> 		return lim.rlim_cur;
> #endif
> 
> #if defined(_SC_OPEN_MAX)
> 	{
> 		long sc_open_max = sysconf(_SC_OPEN_MAX);
> 		if (0 < sc_open_max)
> 			return sc_open_max;
> 	}
> 
> #if defined(OPEN_MAX)
> 	return OPEN_MAX;
> #else
> 	return 1; /* see the caller ;-) */
> #endif
> }
> 
> -------------------------------- >8 ------------------------------
Yeah, with the #endif followup you posted, this is what I had in mind.
Show 20 quoted lines
> But the sysconf part makes me wonder; here is what we see in
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html
> 
>     If name is an invalid value, sysconf() shall return -1 and set errno
>     to indicate the error. If the variable corresponding to name is
>     described in <limits.h> as a maximum or minimum value and the
>     variable has no limit, sysconf() shall return -1 without changing
>     the value of errno. Note that indefinite limits do not imply
>     infinite limits; see <limits.h>.
> 
> For a broken system (like RLIMIT_NOFILE defined for the compiler,
> but the actual call returns a bogus error), the compiler may see the
> _SC_OPEN_MAX defined, while sysconf() may say "I've never heard of
> such a name" and return -1, or the system, whether broken or not,
> may want to say "Unlimited" and return -1.  The caller takes
> anything unreasonable as a positive value capped to 25 or something,
> so there isn't a real harm if we returned a bogus value from here,
> but I am not sure what the safe default behaviour of this function
> should be to help such a broken system while not harming systems
> that are functioning correctly.
According to the POSIX quote above, it sounds like we could do:
  #if defined (_SC_OPEN_MAX)
  {
          long max;
          errno = 0;
          max = sysconf(_SC_OPEN_MAX);
          if (0 < max) /* got the limit */
                  return max;
          else if (!errno) /* unlimited, cast to int-max */
                  return max;
          /* otherwise, fall through */
  }
  #endif

Obviously you could collapse the two branches of the conditional, though I think it deserves at least a comment to explain what is going on.

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 7 of 16 in “RLIMIT_NOFILE fallback”
  1. Joey HessDec 18, 2013
  2. Junio C HamanoDec 18, 2013
  3. Joey HessDec 18, 2013
  4. Jeff KingDec 18, 2013
  5. Junio C HamanoDec 18, 2013
  6. Junio C HamanoDec 18, 2013
  7. Jeff KingDec 18, 2013
  8. Junio C HamanoDec 18, 2013
  9. Jeff KingDec 18, 2013
  10. Junio C HamanoDec 18, 2013
  11. Jeff KingDec 19, 2013
  12. Torsten BögershausenDec 19, 2013
  13. Junio C HamanoDec 19, 2013
  14. Jeff KingDec 20, 2013
  15. Torsten BögershausenDec 20, 2013
  16. Joey HessDec 18, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.