Subject: bug#24325: 25.1; Crash - fd larger than FD_SETSIZE

From: Paul Eggert <[email-masked]>
Date: Thu, 1 Sep 2016 21:23:25 -0700

Thanks for reporting the problem. I see a bug in Emacs in this area, which I 
fixed by installing the attached patch into master on Savannah. Can you please 
give this patch a try in your environment (doesn't have to be emacs -Q)? Thanks.
From 49c304d09659cd273b4d0bfd6e825c89febfc27f Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 1 Sep 2016 21:15:35 -0700
Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20create=20fd=20>=3D=20FD=5FSETSI?=
 =?UTF-8?q?ZE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This avoids a potential crash if too many subprocesses (Bug#24325).
* src/process.c [HAVE_SETRLIMIT]: Include <sys/resource.h>.
(init_process_emacs): If ulimit -n is greater than FD_SETSIZE,
set it to FD_SETSIZE.
---
 src/process.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/process.c b/src/process.c
index 69d1b2a..344a886 100644
--- a/src/process.c
+++ b/src/process.c
@@ -39,6 +39,10 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
+#ifdef HAVE_SETRLIMIT
+# include <sys/resource.h>
+#endif
+
 /* Are local (unix) sockets supported?  */
 #if defined (HAVE_SYS_UN_H)
 #if !defined (AF_LOCAL) && defined (AF_UNIX)
@@ -7784,6 +7788,16 @@ init_process_emacs (int sockfd)
       catch_child_signal ();
     }
 
+#ifdef HAVE_SETRLIMIT
+  /* Don't allocate more than FD_SETSIZE file descriptors.  */
+  struct rlimit rlim;
+  if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && FD_SETSIZE < rlim.rlim_cur)
+    {
+      rlim.rlim_cur = FD_SETSIZE;
+      setrlimit (RLIMIT_NOFILE, &rlim);
+    }
+#endif
+
   FD_ZERO (&input_wait_mask);
   FD_ZERO (&non_keyboard_wait_mask);
   FD_ZERO (&non_process_wait_mask);
-- 
2.7.4