Patch for not going beyond NOFILE system limit (updated) - Mailing list pgsql-hackers

From Peter Eisentraut
Subject Patch for not going beyond NOFILE system limit (updated)
Date
Msg-id 49A7E1A9.6050405@gmx.net
Whole thread Raw
List pgsql-hackers
This is an updated and properly autoconf-guarded patch for this
previously reported issue:

"""
Jacek Drobiecki recently sent me a patch which stops postgresql to
actively violate the system limit of maximum open files
(RLIMIT_NOFILE) in src/backend/storage/file/fd.c, function
count_usable_fds().

This avoids irritating kernel logs (if system overstep violations are
enabled) and also the grsecurity alert when starting PostgreSQL.
"""

References:
http://archives.postgresql.org/pgsql-bugs/2004-05/msg00103.php
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=248967

I think we can put this into 8.4 or the first commit fest of 8.5.
Index: src/backend/storage/file/fd.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/storage/file/fd.c,v
retrieving revision 1.147
diff -u -3 -p -r1.147 fd.c
--- src/backend/storage/file/fd.c    12 Jan 2009 05:10:44 -0000    1.147
+++ src/backend/storage/file/fd.c    27 Feb 2009 12:45:25 -0000
@@ -45,6 +45,9 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>        /* for getrlimit */
+#endif

 #include "miscadmin.h"
 #include "access/xact.h"
@@ -361,15 +364,35 @@ count_usable_fds(int max_to_probe, int *
     int            used = 0;
     int            highestfd = 0;
     int            j;
+#ifdef HAVE_GETRLIMIT
+    struct rlimit rlim;
+    int            getrlimit_status;
+#endif

     size = 1024;
     fd = (int *) palloc(size * sizeof(int));

+#ifdef HAVE_GETRLIMIT
+# ifdef RLIMIT_NOFILE            /* most platforms use RLIMIT_NOFILE */
+    getrlimit_status = getrlimit(RLIMIT_NOFILE, &rlim);
+# else                           /* but BSD doesn't ... */
+    getrlimit_status = getrlimit(RLIMIT_OFILE, &rlim);
+# endif /* RLIMIT_NOFILE */
+    if (getrlimit_status != 0)
+        ereport(WARNING, (errmsg("getrlimit failed: %m")));
+#endif /* HAVE_GETRLIMIT */
+
     /* dup until failure or probe limit reached */
     for (;;)
     {
         int            thisfd;

+#ifdef HAVE_GETRLIMIT
+        /* don't go beyond RLIMIT_NOFILE */
+        if (getrlimit_status == 0 && highestfd >= rlim.rlim_cur - 1)
+            break;
+#endif
+
         thisfd = dup(0);
         if (thisfd < 0)
         {

pgsql-hackers by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: Immediate shutdown and system(3)
Next
From: Bruce Momjian
Date:
Subject: Re: Hot Standby - >8.5