pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter_e@gmx.net>
To: PG Hackers <pgsql-hackers@postgresql.org>
Subject: Patch for not going beyond NOFILE system limit (updated)
Date: Fri, 27 Feb 2009 14:50:49 +0200
Message-ID: <49A7E1A9.6050405@gmx.net> (raw)

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.

Attachments:

  [text/x-diff] check-rlimits-nofile.patch (1.5K, ../49A7E1A9.6050405@gmx.net/2-check-rlimits-nofile.patch)
  download | inline diff:
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)
 		{

Message-ID: <49A7E1A9.6050405@gmx.net>
Permalink:  ../49A7E1A9.6050405@gmx.net/
Also on:    postgresql.org/message-id/49A7E1A9.6050405@gmx.net

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: peter_e@gmx.net
  Subject: Re: Patch for not going beyond NOFILE system limit (updated)
  In-Reply-To: <49A7E1A9.6050405@gmx.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox