Skip to content

Commit 0d1f4fd

Browse files
acroncetimangus
authored andcommitted
Set the maximum file limit for POSIX systems
Resolves an issue where the macOS application's dylibs could not be loaded because there were too many maps; on macOS the default maximum number of open files is 256.
1 parent 3ef30e7 commit 0d1f4fd

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

code/sys/sys_local.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ qboolean Sys_PIDIsRunning( int pid );
7070

7171
qboolean Sys_OpenFolderInPlatformFileManager( const char *path );
7272

73+
qboolean Sys_SetMaxFileLimit( void );
74+
7375
#ifdef PROTOCOL_HANDLER
7476
char *Sys_ParseProtocolUri( const char *uri );
7577
#endif

code/sys/sys_main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ int main( int argc, char **argv )
823823
#endif
824824

825825
Sys_PlatformInit( );
826+
Sys_SetMaxFileLimit( );
826827

827828
// Set the initial time base
828829
Sys_Milliseconds( );

code/sys/sys_unix.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
4040
#include <fenv.h>
4141
#include <sys/wait.h>
4242
#include <time.h>
43+
#include <sys/resource.h>
4344

4445
qboolean stdinIsATTY;
4546

@@ -1326,3 +1327,41 @@ qboolean Sys_OpenFolderInPlatformFileManager( const char *path )
13261327

13271328
return Sys_Exec( ) == 0;
13281329
}
1330+
1331+
/*
1332+
=================
1333+
Sys_SetMaxFileLimit
1334+
=================
1335+
*/
1336+
qboolean Sys_SetMaxFileLimit( void )
1337+
{
1338+
#ifdef RLIMIT_NOFILE
1339+
struct rlimit limit;
1340+
1341+
// Get the current open file limit
1342+
if( getrlimit( RLIMIT_NOFILE, &limit ) == 0 )
1343+
{
1344+
// Set the file limit to the maximum
1345+
limit.rlim_cur = limit.rlim_max;
1346+
if( setrlimit( RLIMIT_NOFILE, &limit ) == 0 )
1347+
return qtrue;
1348+
else
1349+
Com_DPrintf( S_COLOR_YELLOW "WARNING: setrlimit (rlim_max) failed\n" );
1350+
1351+
#ifdef OPEN_MAX
1352+
// On older macOS versions an error can happen trying to set a file limit above
1353+
// OPEN_MAX. If we see an error, then try again with OPEN_MAX as the limit.
1354+
limit.rlim_cur = OPEN_MAX;
1355+
if( setrlimit( RLIMIT_NOFILE, &limit ) == 0 )
1356+
return qtrue;
1357+
else
1358+
Com_DPrintf( S_COLOR_YELLOW "WARNING: setrlimit (OPEN_MAX) failed\n" );
1359+
#endif
1360+
}
1361+
else
1362+
Com_DPrintf( S_COLOR_YELLOW "WARNING: getrlimit failed\n" );
1363+
1364+
#endif // RLIMIT_NOFILE
1365+
1366+
return qfalse;
1367+
}

code/sys/sys_win32.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,3 +929,13 @@ qboolean Sys_OpenFolderInPlatformFileManager( const char *path )
929929
{
930930
return ShellExecute( NULL, "explore", path, NULL, NULL, SW_SHOWDEFAULT ) > (HINSTANCE)32;
931931
}
932+
933+
/*
934+
=================
935+
Sys_SetMaxFileLimit
936+
=================
937+
*/
938+
qboolean Sys_SetMaxFileLimit( void )
939+
{
940+
return qtrue;
941+
}

0 commit comments

Comments
 (0)