]> git.mdlowis.com Git - proto/labwc.git/commitdiff
common: Add helpers for raising/lowering FD limit
authorJoshua Ashton <joshua@froggi.es>
Sun, 17 Jul 2022 00:47:20 +0000 (00:47 +0000)
committerJoshua Ashton <joshua@froggi.es>
Sun, 17 Jul 2022 00:59:23 +0000 (00:59 +0000)
Signed-off-by: Joshua Ashton <joshua@froggi.es>
include/common/fd_util.h [new file with mode: 0644]
src/common/fd_util.c [new file with mode: 0644]
src/common/meson.build

diff --git a/include/common/fd_util.h b/include/common/fd_util.h
new file mode 100644 (file)
index 0000000..9819f25
--- /dev/null
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LABWC_FD_UTIL_H
+#define __LABWC_FD_UTIL_H
+
+void increase_nofile_limit(void);
+void restore_nofile_limit(void);
+
+#endif /* __LABWC_FD_UTIL_H */
diff --git a/src/common/fd_util.c b/src/common/fd_util.c
new file mode 100644 (file)
index 0000000..f45cad8
--- /dev/null
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#define _POSIX_C_SOURCE 200809L
+
+#include <sys/resource.h>
+#include <wlr/util/log.h>
+
+#include "common/fd_util.h"
+
+static struct rlimit original_nofile_rlimit = {0};
+
+void
+increase_nofile_limit(void)
+{
+       if (getrlimit(RLIMIT_NOFILE, &original_nofile_rlimit) != 0) {
+               wlr_log_errno(WLR_ERROR, "Failed to bump max open files limit: getrlimit(NOFILE) failed");
+               return;
+       }
+
+       struct rlimit new_rlimit = original_nofile_rlimit;
+       new_rlimit.rlim_cur = new_rlimit.rlim_max;
+       if (setrlimit(RLIMIT_NOFILE, &new_rlimit) != 0) {
+               wlr_log_errno(WLR_ERROR, "Failed to bump max open files limit: setrlimit(NOFILE) failed");
+
+               wlr_log(WLR_INFO, "Running with %d max open files",
+            (int)original_nofile_rlimit.rlim_cur);
+       }
+}
+
+void
+restore_nofile_limit(void)
+{
+       if (original_nofile_rlimit.rlim_cur == 0) {
+               return;
+       }
+
+       if (setrlimit(RLIMIT_NOFILE, &original_nofile_rlimit) != 0) {
+               wlr_log_errno(WLR_ERROR, "Failed to restore max open files limit: setrlimit(NOFILE) failed");
+       }
+}
index d04e9f63ca4cfb4317bbae600d57c79f82da42b3..3b099537ccc993d2de18b6d8f6f7d0a3f2680884 100644 (file)
@@ -1,6 +1,7 @@
 labwc_sources += files(
   'buf.c',
   'dir.c',
+  'fd_util.c',
   'font.c',
   'grab-file.c',
   'nodename.c',