From: Peter Eisentraut Date: Wed, 4 Mar 2009 09:12:49 +0000 (+0000) Subject: Don't actively violate the system limit of maximum open files (RLIMIT_NOFILE). X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=e0dd110ed2531ff2e69546804a03de4df9f5e37a;p=users%2Fsimon%2Fpostgres.git Don't actively violate the system limit of maximum open files (RLIMIT_NOFILE). This avoids irritating kernel logs (if system overstep violations are enabled) and also the grsecurity alert when starting PostgreSQL. original patch by Jacek Drobiecki References: http://archives.postgresql.org/pgsql-bugs/2004-05/msg00103.php http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=248967 --- diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 5eb8f5422f..1a81f1f1ca 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -45,6 +45,9 @@ #include #include #include +#ifdef HAVE_SYS_RESOURCE_H +#include /* for getrlimit */ +#endif #include "miscadmin.h" #include "access/xact.h" @@ -361,15 +364,35 @@ count_usable_fds(int max_to_probe, int *usable_fds, int *already_open) 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; causes irritating kernel logs on some platforms */ + if (getrlimit_status == 0 && highestfd >= rlim.rlim_cur - 1) + break; +#endif + thisfd = dup(0); if (thisfd < 0) {