Split from https://gitlab.winehq.org/wine/wine/-/merge_requests/8875
-- v2: wineserver: Request RLIMIT_NOFILE maximum allowed value.
From: Rémi Bernon rbernon@codeweavers.com
--- server/main.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
diff --git a/server/main.c b/server/main.c index e014ec535ff..b44abe6e3c2 100644 --- a/server/main.c +++ b/server/main.c @@ -28,6 +28,12 @@ #include <stdlib.h> #include <sys/time.h> #include <unistd.h> +#ifdef HAVE_SYS_RESOURCE_H +# include <sys/resource.h> +#endif +#ifdef HAVE_SYS_SYSCTL_H +# include <sys/sysctl.h> +#endif
#include "object.h" #include "file.h" @@ -212,6 +218,42 @@ static void sigterm_handler( int signum ) exit(1); /* make sure atexit functions get called */ }
+#ifdef RLIMIT_NOFILE +static void set_max_limit( int limit ) +{ + struct rlimit rlimit; + + if (!getrlimit( limit, &rlimit )) + { + rlimit.rlim_cur = rlimit.rlim_max; + if (!setrlimit( limit, &rlimit )) return; + +#if defined(__APPLE__) && defined(OPEN_MAX) + if (limit == RLIMIT_NOFILE) + { + unsigned int nlimit = 0; + size_t size; + + /* On Leopard, setrlimit(RLIMIT_NOFILE, ...) fails on attempts to set + * rlim_cur above OPEN_MAX (even if rlim_max > OPEN_MAX). + * + * In later versions it can be set to kern.maxfilesperproc (from + * sysctl). In Big Sur and later it can be set to rlim_max. */ + size = sizeof(nlimit); + if (sysctlbyname("kern.maxfilesperproc", &nlimit, &size, NULL, 0) != 0 || nlimit < OPEN_MAX) + rlimit.rlim_cur = OPEN_MAX; + else + rlimit.rlim_cur = nlimit; + + if (!setrlimit( limit, &rlimit )) return; + rlimit.rlim_cur = OPEN_MAX; + if (!setrlimit( limit, &rlimit )) return; + } +#endif + } +} +#endif /* RLIMIT_NOFILE */ + int main( int argc, char *argv[] ) { setvbuf( stderr, NULL, _IOLBF, 0 ); @@ -225,6 +267,9 @@ int main( int argc, char *argv[] ) signal( SIGQUIT, sigterm_handler ); signal( SIGTERM, sigterm_handler ); signal( SIGABRT, sigterm_handler ); +#ifdef RLIMIT_NOFILE + set_max_limit( RLIMIT_NOFILE ); +#endif
sock_init(); open_master_socket();
On Wed Sep 17 09:20:51 2025 +0000, Rémi Bernon wrote:
changed this line in [version 2 of the diff](/wine/wine/-/merge_requests/8983/diffs?diff_id=209769&start_sha=84196409fa509b4b0cdf6228af9749cc35c8d0e1#780e0623346e7f4f1e2d5de9f8bd82dc37b9e9bc_231_231)
I removed the extra check, it'll need to be added back if that function is used for more limits.
On Wed Sep 17 09:21:55 2025 +0000, Rémi Bernon wrote:
I removed the extra check, it'll need to be added back if that function is used for more limits.
Actually I found it surprising that we didn't have it already, and we should be needing the RLIMIT_NICE too as it'll change how wineserver can use setpriority.
I think the reason I had to do that is for when wineserver is started separately from a wine processes, rlimit changes are inherited from parent processes right?