Set progname early in the postmaster/postgres binary, rather than doing
authorBruce Momjian <[email protected]>
Wed, 1 Feb 2006 00:47:03 +0000 (00:47 +0000)
committerBruce Momjian <[email protected]>
Wed, 1 Feb 2006 00:47:03 +0000 (00:47 +0000)
it later.  This fixes a problem where EXEC_BACKEND didn't have progname
set, causing a segfault if log_min_messages was set below debug2 and our
own snprintf.c was being used.

Also alway strdup() progname.

Backpatch to 8.1.X and 8.0.X.

src/backend/main/main.c
src/backend/postmaster/postmaster.c
src/include/postmaster/postmaster.h
src/port/path.c

index 778b263c4fb2141fa0b586c1ff93088b8a94dfb0..bbe89bf7b7fa2060a25f474cfcd8b82430b0a3e5 100644 (file)
@@ -45,7 +45,7 @@
 #include "libpq/pqsignal.h"
 #endif
 
-
+const char *progname;
 
 int
 main(int argc, char *argv[])
@@ -101,6 +101,8 @@ main(int argc, char *argv[])
 #endif
 #endif   /* NOFIXADE */
 
+       progname = get_progname(argv[0]);
+
 #if defined(WIN32)
        {
                WSADATA         wsaData;
index afd861b5ca30163d9d6478df2cc0774e1aa157d6..4a9c1e98f328b71e6a254e83b6be43b01fd7f995 100644 (file)
@@ -168,9 +168,6 @@ char           *ListenAddresses;
  */
 int                    ReservedBackends;
 
-
-static const char *progname = NULL;
-
 /* The socket(s) we're listening to. */
 #define MAXLISTEN      64
 static int     ListenSocket[MAXLISTEN];
@@ -375,9 +372,6 @@ PostmasterMain(int argc, char *argv[])
        char       *userDoption = NULL;
        int                     i;
 
-       /* This will call exit() if strdup() fails. */
-       progname = get_progname(argv[0]);       
-
        MyProcPid = PostmasterPid = getpid();
 
        IsPostmasterEnvironment = true;
index 083968f9671701da93bdf82d8ffeedce8bbd7d8f..9c0d9841a0280b6358f869aa82e8c765ad9294d1 100644 (file)
@@ -34,6 +34,7 @@ extern char *rendezvous_name;
 extern HANDLE PostmasterHandle;
 #endif
 
+extern const char *progname;
 
 extern int     PostmasterMain(int argc, char *argv[]);
 extern void ClosePostmasterPorts(bool am_syslogger);
index e707455c60c297009678c2f0c5e6d9446974b50f..715f4e595613ce149ae85454f1bba8ec2dd4219d 100644 (file)
@@ -302,7 +302,8 @@ canonicalize_path(char *path)
 const char *
 get_progname(const char *argv0)
 {
-       const char *nodir_name;
+       const char  *nodir_name;
+       const char      *progname;
 
        nodir_name = last_dir_separator(argv0);
        if (nodir_name)
@@ -310,27 +311,27 @@ get_progname(const char *argv0)
        else
                nodir_name = skip_drive(argv0);
 
-#if defined(__CYGWIN__) || defined(WIN32)
-       /* strip .exe suffix, regardless of case */
-       if (strlen(nodir_name) > sizeof(EXE) - 1 &&
-               pg_strcasecmp(nodir_name + strlen(nodir_name)-(sizeof(EXE)-1), EXE) == 0)
+       /*
+        *      Make a copy in case argv[0] is modified by ps_status.
+        *      Leaks memory, but called only once.
+        */
+       progname = strdup(nodir_name);
+       if (progname == NULL)
        {
-               char *progname;
+               fprintf(stderr, "%s: out of memory\n", nodir_name);
+               exit(1);                        /* This could exit the postmaster */
+       }
 
-               progname = strdup(nodir_name);
-               if (progname == NULL)
-               {
-                       fprintf(stderr, "%s: out of memory\n", nodir_name);
-                       exit(1);        /* This could exit the postmaster */
-               }
+#if defined(__CYGWIN__) || defined(WIN32)
+       /* strip ".exe" suffix, regardless of case */
+       if (strlen(progname) > sizeof(EXE) - 1 &&
+               pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0)
                progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0';
-               nodir_name = progname; 
-       }
 #endif
 
-       return nodir_name;
+       return progname;
 }
-
+  
 
 /*
  * dir_strcmp: strcmp except any two DIR_SEP characters are considered equal