Skip to content

Commit 491a8de

Browse files
committed
get_max_fd_limit(): fall back to OPEN_MAX upon getrlimit/sysconf failure
On broken systems where RLIMIT_NOFILE is visible by the compliers but underlying getrlimit() system call does not behave, we used to simply die() when we are trying to decide how many file descriptors to allocate for keeping packfiles open. Instead, allow the fallback codepath to take over when we get such a failure from getrlimit(). The same issue exists with _SC_OPEN_MAX and sysconf(); restructure the code in a similar way to prepare for a broken sysconf() as well. Noticed-by: Joey Hess <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5512ac5 commit 491a8de

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

sha1_file.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -807,15 +807,38 @@ void free_pack_by_name(const char *pack_name)
807807
static unsigned int get_max_fd_limit(void)
808808
{
809809
#ifdef RLIMIT_NOFILE
810-
struct rlimit lim;
810+
{
811+
struct rlimit lim;
811812

812-
if (getrlimit(RLIMIT_NOFILE, &lim))
813-
die_errno("cannot get RLIMIT_NOFILE");
813+
if (!getrlimit(RLIMIT_NOFILE, &lim))
814+
return lim.rlim_cur;
815+
}
816+
#endif
817+
818+
#ifdef _SC_OPEN_MAX
819+
{
820+
long open_max = sysconf(_SC_OPEN_MAX);
821+
if (0 < open_max)
822+
return open_max;
823+
/*
824+
* Otherwise, we got -1 for one of the two
825+
* reasons:
826+
*
827+
* (1) sysconf() did not understand _SC_OPEN_MAX
828+
* and signaled an error with -1; or
829+
* (2) sysconf() said there is no limit.
830+
*
831+
* We _could_ clear errno before calling sysconf() to
832+
* tell these two cases apart and return a huge number
833+
* in the latter case to let the caller cap it to a
834+
* value that is not so selfish, but letting the
835+
* fallback OPEN_MAX codepath take care of these cases
836+
* is a lot simpler.
837+
*/
838+
}
839+
#endif
814840

815-
return lim.rlim_cur;
816-
#elif defined(_SC_OPEN_MAX)
817-
return sysconf(_SC_OPEN_MAX);
818-
#elif defined(OPEN_MAX)
841+
#ifdef OPEN_MAX
819842
return OPEN_MAX;
820843
#else
821844
return 1; /* see the caller ;-) */

0 commit comments

Comments
 (0)