[Please CC any replies]

As per discussion on -hackers.

Here is a patch that adds a -C option to the postgres process which
will cause the server to emit one of the following:

NOTICE:  Core dumps hard disabled by admin
NOTICE:  Core dumps already enabled by admin (size)
NOTICE:  Core limit successfully changed to (size)

There are probably multiple takes on the security of this option, but
if you have a client that can cause the server to dump core, you've got
bigger issues. Patch just emits a warning on a Win32 server.

Intended usage:

$ PGOPTIONS=-C ./psql test 
NOTICE:  Core limit successfully changed to (unlimited)
Welcome to psql 8.1beta2, the PostgreSQL interactive terminal.
<snip>
test=# 

Although it would work with any libpq client.

I think it would be helpful in situations where the user can't
immediately find out how to change the ulimit of the backend process.

Have a nice day,
-- 
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/tcop/postgres.c,v
retrieving revision 1.466
diff -c -r1.466 postgres.c
*** src/backend/tcop/postgres.c 15 Oct 2005 02:49:27 -0000      1.466
--- src/backend/tcop/postgres.c 28 Oct 2005 07:45:13 -0000
***************
*** 29,34 ****
--- 29,37 ----
  #ifdef HAVE_GETOPT_H
  #include <getopt.h>
  #endif
+ #ifndef WIN32
+ #include <sys/resource.h>
+ #endif
  
  #include "access/printtup.h"
  #include "access/xlog.h"
***************
*** 2273,2278 ****
--- 2276,2342 ----
        }
  }
  
+ #ifndef WIN32
+ /*
+  * EnableCoreDump: Try to enable core dumps for this backend
+  *
+  * Sometimes you're trying to have users extract a backtrace from a core
+  * dump but the system has disabled them by default. This function attempts
+  * to enable them and displays a warning if it isn't possible.
+  *
+  * Intended use: -C on the postgres command line, thus to clients via 
PGOPTIONS
+  */
+  
+ static void
+ EnableCoreDump()
+ {
+       struct rlimit limits;
+       
+       if( getrlimit( RLIMIT_CORE, &limits ) == -1 )
+       {
+               ereport(NOTICE,
+                               (errmsg("Unable to retrieve core limit: %m")));
+               return;
+       }
+       
+       if( limits.rlim_max == 0 )
+       {
+               ereport(NOTICE,
+                               (errmsg("Core dumps hard disabled by admin")));
+               return;
+       }
+       
+       if( limits.rlim_cur == limits.rlim_max )
+       {
+               if( limits.rlim_cur == RLIM_INFINITY )
+                       ereport(NOTICE,
+                                       (errmsg("Core dumps already enabled by 
admin (unlimited)")));
+               else
+                       ereport(NOTICE,
+                                       (errmsg("Core dumps already enabled by 
admin (%u KB)", ((unsigned int)limits.rlim_cur)/1024 )));
+               return;
+       }
+ 
+       limits.rlim_cur = limits.rlim_max;
+ 
+       if( setrlimit( RLIMIT_CORE, &limits ) == -1 )
+       {
+               ereport(NOTICE,
+                               (errmsg("Unable to change core limit: %m")));
+               return;
+       }
+       
+       if( limits.rlim_cur == RLIM_INFINITY )
+               ereport(NOTICE,
+                               (errmsg("Core limit successfully changed to 
(unlimited)")));
+       else
+               ereport(NOTICE,
+                               (errmsg("Core limit successfully changed to (%u 
KB)", ((unsigned int)limits.rlim_cur)/1024 )));
+       return;
+ }
+ 
+ #endif  /* WIN32 */
+ 
  /* GUC assign hook to update max_stack_depth_bytes from max_stack_depth */
  bool
  assign_max_stack_depth(int newval, bool doit, GucSource source)
***************
*** 2469,2475 ****
        ctx = PGC_POSTMASTER;
        gucsource = PGC_S_ARGV;         /* initial switches came from command 
line */
  
!       while ((flag = getopt(argc, argv, 
"A:B:c:D:d:Eef:FiNOPo:p:S:st:v:W:-:")) != -1)
        {
                switch (flag)
                {
--- 2533,2539 ----
        ctx = PGC_POSTMASTER;
        gucsource = PGC_S_ARGV;         /* initial switches came from command 
line */
  
!       while ((flag = getopt(argc, argv, 
"A:B:Cc:D:d:Eef:FiNOPo:p:S:st:v:W:-:")) != -1)
        {
                switch (flag)
                {
***************
*** 2490,2495 ****
--- 2554,2570 ----
                                 */
                                SetConfigOption("shared_buffers", optarg, ctx, 
gucsource);
                                break;
+                               
+                       case 'C':
+                               /* Enable coredumps if possible */
+ #ifdef WIN32
+                               ereport(WARNING,
+                                               
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                                errmsg("core dumping not 
available on Win32")));
+ #else
+                               EnableCoreDump();
+ #endif
+                               break;                                          
                
  
                        case 'D':                       /* PGDATA or config 
directory */
                                if (secure)

Attachment: pgpuJtfKmGpDJ.pgp
Description: PGP signature

Reply via email to