changeset 6314:c6d6ce742a82 HEAD

Added restrict_fd_limit() and restrict_raise_fd_limit()
author Timo Sirainen <tss@iki.fi>
date Fri, 24 Aug 2007 20:50:44 +0300
parents 0e08960275f8
children 5ebf96e37a39
files src/lib/restrict-process-size.c src/lib/restrict-process-size.h
diffstat 2 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib/restrict-process-size.c	Fri Aug 24 20:49:33 2007 +0300
+++ b/src/lib/restrict-process-size.c	Fri Aug 24 20:50:44 2007 +0300
@@ -42,3 +42,36 @@
 	}
 #endif
 }
+
+void restrict_fd_limit(unsigned int count)
+{
+#ifdef HAVE_SETRLIMIT
+	struct rlimit rlim;
+
+	rlim.rlim_cur = rlim.rlim_max = count;
+	if (setrlimit(RLIMIT_NOFILE, &rlim) < 0)
+		i_fatal("setrlimit(RLIMIT_NOFILE, %u): %m", count);
+#endif
+}
+
+bool restrict_raise_fd_limit(unsigned int count)
+{
+#ifdef HAVE_SETRLIMIT
+	struct rlimit rlim, new_rlim;
+
+	if (getrlimit(RLIMIT_NOFILE, &rlim) < 0)
+		return FALSE;
+
+	if (rlim.rlim_cur < count)
+		new_rlim.rlim_cur = new_rlim.rlim_max = count;
+	if (setrlimit(RLIMIT_NOFILE, &new_rlim) == 0)
+		return TRUE;
+
+	/* raise as high as we can */
+	if (rlim.rlim_cur < rlim.rlim_max) {
+		rlim.rlim_cur = rlim.rlim_max;
+		(void)setrlimit(RLIMIT_NOFILE, &new_rlim);
+	}
+#endif
+	return FALSE;
+}
--- a/src/lib/restrict-process-size.h	Fri Aug 24 20:49:33 2007 +0300
+++ b/src/lib/restrict-process-size.h	Fri Aug 24 20:50:44 2007 +0300
@@ -4,5 +4,10 @@
 /* Restrict max. process size. The size is in megabytes, setting it to
    (unsigned int)-1 sets it unlimited. */
 void restrict_process_size(unsigned int size, unsigned int max_processes);
+/* Set fd limit to count. */
+void restrict_fd_limit(unsigned int count);
+/* If fd limit is less than count, try to raise it. Probably fails (silently)
+   if we're not running as root. Returns TRUE if succeeded. */
+bool restrict_raise_fd_limit(unsigned int count);
 
 #endif