Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the maximum file limit for POSIX systems #580

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions code/sys/sys_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,48 @@ void Sys_SigHandler( int signal )
Sys_Exit( 2 );
}

/*
=================
Sys_SetFileLimit
=================
*/
#ifdef _WIN32
static int Sys_SetMaxFileLimit(void)
{
// A Windows implementation of this function probably is not needed.
return 0;
}
#else
#include <sys/resource.h>
static int Sys_SetMaxFileLimit(void)
{
int result = 0;

#if defined(RLIMIT_NOFILE)
// Get the current open file limit.
struct rlimit limit;
result = getrlimit(RLIMIT_NOFILE, &limit);
if (0 == result)
{
// Set the file limit to the maximum
limit.rlim_cur = limit.rlim_max;
result = setrlimit(RLIMIT_NOFILE, &limit);
# if defined(__APPLE__) && defined(OPEN_MAX)
// On older macOS versions an error can happen trying to set a file limit above
// OPEN_MAX. If we see an error, then try again with OPEN_MAX as the limit.
if (result)
{
limit.rlim_cur = OPEN_MAX;
result = setrlimit(RLIMIT_NOFILE, &limit);
} // Error on first attempt to set the limit
# endif // Apple
} // No error getting the current file limit
#endif // defined(RLIMIT_NOFILE)

return result;
}
#endif // !_WIN32

/*
=================
main
Expand All @@ -690,6 +732,16 @@ int main( int argc, char **argv )
{
int i;
char commandLine[ MAX_STRING_CHARS ] = { 0 };

// Set the maximum number of files. Unlimited maps! Must be called early on before any
// system calls latch the default limit in place.
{
int result = Sys_SetMaxFileLimit();
if (result)
{
Com_Printf("Error trying to set the maxumim file limit: %d\n", result);
}
}

extern void Sys_LaunchAutoupdater(int argc, char **argv);
Sys_LaunchAutoupdater(argc, argv);
Expand Down