diff --git a/code/sys/sys_local.h b/code/sys/sys_local.h index 1c27faac..168d7e6d 100644 --- a/code/sys/sys_local.h +++ b/code/sys/sys_local.h @@ -70,6 +70,8 @@ qboolean Sys_PIDIsRunning( int pid ); qboolean Sys_OpenFolderInPlatformFileManager( const char *path ); +qboolean Sys_SetMaxFileLimit( void ); + #ifdef PROTOCOL_HANDLER char *Sys_ParseProtocolUri( const char *uri ); #endif diff --git a/code/sys/sys_main.c b/code/sys/sys_main.c index dd64fead..e3b63cb5 100644 --- a/code/sys/sys_main.c +++ b/code/sys/sys_main.c @@ -823,6 +823,7 @@ int main( int argc, char **argv ) #endif Sys_PlatformInit( ); + Sys_SetMaxFileLimit( ); // Set the initial time base Sys_Milliseconds( ); diff --git a/code/sys/sys_unix.c b/code/sys/sys_unix.c index 5ec7d583..2ed5f1b0 100644 --- a/code/sys/sys_unix.c +++ b/code/sys/sys_unix.c @@ -40,6 +40,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include #include +#include qboolean stdinIsATTY; @@ -1326,3 +1327,41 @@ qboolean Sys_OpenFolderInPlatformFileManager( const char *path ) return Sys_Exec( ) == 0; } + +/* +================= +Sys_SetMaxFileLimit +================= +*/ +qboolean Sys_SetMaxFileLimit( void ) +{ +#ifdef RLIMIT_NOFILE + struct rlimit limit; + + // Get the current open file limit + if( getrlimit( RLIMIT_NOFILE, &limit ) == 0 ) + { + // Set the file limit to the maximum + limit.rlim_cur = limit.rlim_max; + if( setrlimit( RLIMIT_NOFILE, &limit ) == 0 ) + return qtrue; + else + Com_DPrintf( S_COLOR_YELLOW "WARNING: setrlimit (rlim_max) failed\n" ); + +#ifdef 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. + limit.rlim_cur = OPEN_MAX; + if( setrlimit( RLIMIT_NOFILE, &limit ) == 0 ) + return qtrue; + else + Com_DPrintf( S_COLOR_YELLOW "WARNING: setrlimit (OPEN_MAX) failed\n" ); +#endif + } + else + Com_DPrintf( S_COLOR_YELLOW "WARNING: getrlimit failed\n" ); + +#endif // RLIMIT_NOFILE + + return qfalse; +} diff --git a/code/sys/sys_win32.c b/code/sys/sys_win32.c index 20ab29a2..5985ec69 100644 --- a/code/sys/sys_win32.c +++ b/code/sys/sys_win32.c @@ -929,3 +929,13 @@ qboolean Sys_OpenFolderInPlatformFileManager( const char *path ) { return ShellExecute( NULL, "explore", path, NULL, NULL, SW_SHOWDEFAULT ) > (HINSTANCE)32; } + +/* +================= +Sys_SetMaxFileLimit +================= +*/ +qboolean Sys_SetMaxFileLimit( void ) +{ + return qtrue; +}