From 9f80802ad639fe35efaeede485e9cd6e1f002486 Mon Sep 17 00:00:00 2001 From: sewn Date: Thu, 6 Jun 2024 16:57:13 +0300 Subject: [PATCH] set max open file descriptors to available max --- dwl.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/dwl.c b/dwl.c index 6f041a0..a28291e 100644 --- a/dwl.c +++ b/dwl.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -311,6 +312,7 @@ static void pointerfocus(Client *c, struct wlr_surface *surface, double sx, double sy, uint32_t time); static void printstatus(void); static void quit(const Arg *arg); +static void restorerlimit(void); static void rendermon(struct wl_listener *listener, void *data); static void requestdecorationmode(struct wl_listener *listener, void *data); static void requeststartdrag(struct wl_listener *listener, void *data); @@ -353,6 +355,7 @@ static void zoom(const Arg *arg); /* variables */ static const char broken[] = "broken"; +static struct rlimit og_rlimit; static pid_t child_pid = -1; static int locked; static void *exclusive_focus; @@ -2014,6 +2017,15 @@ quit(const Arg *arg) wl_display_terminate(dpy); } +void +restorerlimit(void) +{ + if (og_rlimit.rlim_cur == 0) + return; + if (setrlimit(RLIMIT_NOFILE, &og_rlimit) < 0) + die("setrlimit:"); +} + void rendermon(struct wl_listener *listener, void *data) { @@ -2143,6 +2155,7 @@ run(char *startup_cmd) if ((child_pid = fork()) < 0) die("startup: fork:"); if (child_pid == 0) { + restorerlimit(); dup2(piperw[0], STDIN_FILENO); close(piperw[0]); close(piperw[1]); @@ -2333,10 +2346,18 @@ setsel(struct wl_listener *listener, void *data) void setup(void) { + struct rlimit new_rlimit; int i, sig[] = {SIGCHLD, SIGINT, SIGTERM, SIGPIPE}; struct sigaction sa = {.sa_flags = SA_RESTART, .sa_handler = handlesig}; sigemptyset(&sa.sa_mask); + if (getrlimit(RLIMIT_NOFILE, &og_rlimit) < 0) + die("getrlimit:"); + new_rlimit = og_rlimit; + new_rlimit.rlim_cur = new_rlimit.rlim_max; + if (setrlimit(RLIMIT_NOFILE, &new_rlimit) < 0) + die("setrlimit:"); + for (i = 0; i < (int)LENGTH(sig); i++) sigaction(sig[i], &sa, NULL); @@ -2547,6 +2568,7 @@ void spawn(const Arg *arg) { if (fork() == 0) { + restorerlimit(); dup2(STDERR_FILENO, STDOUT_FILENO); setsid(); execvp(((char **)arg->v)[0], (char **)arg->v); -- 2.45.0