[gnome-terminal] server: Increase RLIMIT_NOFILE to the maximum allowed



commit 7ebf350cfb68ab30f26e68fcb773fe644c42a5df
Author: Christian Persch <chpe gnome org>
Date:   Sun Oct 19 09:30:39 2014 +0200

    server: Increase RLIMIT_NOFILE to the maximum allowed
    
    VTE uses up to 8 FDs per terminal, so we'll exhaust the default (1024)
    quite soon (~120 terminals). Apparently people have actually hit this
    limit; so let's increase it.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=646098
    https://bugzilla.redhat.com/show_bug.cgi?id=667539

 src/server.c |   38 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 38 insertions(+), 0 deletions(-)
---
diff --git a/src/server.c b/src/server.c
index 94d0a89..e871c8f 100644
--- a/src/server.c
+++ b/src/server.c
@@ -23,9 +23,11 @@
 
 #include <errno.h>
 #include <locale.h>
+#include <pthread.h>
 #include <stdlib.h>
 #include <time.h>
 #include <unistd.h>
+#include <sys/resource.h>
 #include <sys/types.h>
 
 #include <glib.h>
@@ -64,6 +66,37 @@ static const GOptionEntry options[] = {
   { NULL }
 };
 
+/* We use up to 8 FDs per terminal, so let's bump the limit way up.
+ * However we need to restore the original limit for the child processes.
+ */
+
+static struct rlimit sv_rlimit_nofile;
+
+static void
+atfork_child_restore_rlimit_nofile (void)
+{
+  if (setrlimit (RLIMIT_NOFILE, &sv_rlimit_nofile) < 0)
+    _exit (127);
+}
+
+static gboolean
+increase_rlimit_nofile (void)
+{
+  struct rlimit l;
+
+  if (getrlimit (RLIMIT_NOFILE, &sv_rlimit_nofile) < 0)
+    return FALSE;
+
+  l.rlim_cur = l.rlim_max = sv_rlimit_nofile.rlim_max;
+  if (setrlimit (RLIMIT_NOFILE, &l) < 0)
+    return FALSE;
+
+  if (pthread_atfork (NULL, NULL, atfork_child_restore_rlimit_nofile) != 0)
+    return FALSE;
+
+  return TRUE;
+}
+
 enum {
   _EXIT_FAILURE_WRONG_ID = 7,
   _EXIT_FAILURE_NO_UTF8 = 8,
@@ -126,6 +159,11 @@ main (int argc, char **argv)
     exit (EXIT_FAILURE);
   }
 
+  if (!increase_rlimit_nofile ()) {
+    g_printerr ("Failed to increase RLIMIT_NOFILE: %m\n");
+  }
+
+  /* Now we can create the app */
   app = terminal_app_new (app_id);
   g_free (app_id);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]