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

Re: [PATCH v2] Prefer sysconf(_SC_OPEN_MAX) over getrlimit(RLIMIT_NOFILE,...)

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 24, 2012, 16:43 UTC
Message-ID
<7v393c1br5.fsf@alter.siamese.dyndns.org>
In-Reply-To
<002f01cd81de$28f43bf0$7adcb3d0$@schmitz-digital.de>
"Joachim Schmitz" <jojo@schmitz-digital.de> writes:
> Signed-off-by: Joachim Schmitz <jojo@schmitz-digital.de>
> ---
> As discussed now as a small helper function rather than #ifdef/#endif in the primary flow of the code.
> And hopefully without having screwed up whitespace and line breaks
The formatting looks fine.

Perhaps I am being overly paranoid, but I would prefer not to change things for people who have been using getrlimit(). For them, if they also have sysconf(_SC_OPEN_MAX), your code _ought to_ work, but if it does not work for whatever reason (perhaps some platforms claim to have both, but getrlimit() works and sysconf(_SC_OPEN_MAX) is broken), it will given them an unnecessary regression.

So how about doing it this way instead?
-- >8 --
Subject: sha1_file.c: introduce get_max_fd_limit() helper

Not all platforms have getrlimit(), but there are other ways to see the maximum number of files that a process can have open. If getrlimit() is unavailable, fall back to sysconf(_SC_OPEN_MAX) if available, and use OPEN_MAX from <limits.h>.

Signed-off-by: Joachim Schmitz <jojo@schmitz-digital.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 sha1_file.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
diff --git c/sha1_file.c w/sha1_file.c
index af5cfbd..9152974 100644
--- c/sha1_file.c
+++ w/sha1_file.c
@@ -731,6 +731,24 @@ void free_pack_by_name(const char *pack_name)
 	}
 }
 
+static unsigned int get_max_fd_limit(void)
+{
+#ifdef RLIMIT_NOFILE
+	struct rlimit lim;
+
+	if (getrlimit(RLIMIT_NOFILE, &lim))
+		die_errno("cannot get RLIMIT_NOFILE");
+
+	return lim.rlim_cur;
+#elif defined(_SC_OPEN_MAX)
+	return sysconf(_SC_OPEN_MAX);
+#elif defined(OPEN_MAX)
+	return OPEN_MAX;
+#else
+	return 1; /* see the caller ;-) */
+#endif
+}
+
 /*
  * Do not call this directly as this leaks p->pack_fd on error return;
  * call open_packed_git() instead.
@@ -747,13 +765,7 @@ static int open_packed_git_1(struct packed_git *p)
 		return error("packfile %s index unavailable", p->pack_name);
 
 	if (!pack_max_fds) {
-		struct rlimit lim;
-		unsigned int max_fds;
-
-		if (getrlimit(RLIMIT_NOFILE, &lim))
-			die_errno("cannot get RLIMIT_NOFILE");
-
-		max_fds = lim.rlim_cur;
+		unsigned int max_fds = get_max_fd_limit();
 
 		/* Save 3 for stdin/stdout/stderr, 22 for work */
 		if (25 < max_fds)
Previous: Joachim SchmitzNext: Joachim Schmitz
Message 2 of 3 in “Prefer sysconf(_SC_OPEN_MAX) over getrlimit(RLIMIT_NOFILE,...)”
  1. Prefer sysconf(_SC_OPEN_MAX) over getrlimit(RLIMIT_NOFILE,...)Joachim Schmitz, Aug 24, 2012
  2. Junio C HamanoAug 24, 2012
  3. Joachim SchmitzAug 24, 2012

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.