Set progname early in the postmaster/postgres binary, rather than doing
authorBruce Momjian
Wed, 1 Feb 2006 00:31:59 +0000 (00:31 +0000)
committerBruce Momjian
Wed, 1 Feb 2006 00:31:59 +0000 (00:31 +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 c2ea5fc85784967cd3d8a777f723684753876be8..64d77451c0b527f25dae6fbec70e99e78881da9e 100644 (file)
@@ -13,7 +13,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/main/main.c,v 1.99 2006/01/05 03:01:34 momjian Exp $
+ *   $PostgreSQL: pgsql/src/backend/main/main.c,v 1.100 2006/02/01 00:31:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -45,7 +45,7 @@
 #include "libpq/pqsignal.h"
 #endif
 
-
+const char *progname;
 
 int
 main(int argc, char *argv[])
@@ -77,6 +77,8 @@ main(int argc, char *argv[])
    char       *env_locale;
 #endif
 
+   progname = get_progname(argv[0]);
+
    /*
     * On some platforms, unaligned memory accesses result in a kernel trap;
     * the default kernel behavior is to emulate the memory access, but this
@@ -246,7 +248,7 @@ main(int argc, char *argv[])
     * possibly first argument) we were called with. The lack of consistency
     * here is historical.
     */
-   if (strcmp(get_progname(argv[0]), "postmaster") == 0)
+   if (strcmp(progname, "postmaster") == 0)
    {
        /* Called as "postmaster" */
        exit(PostmasterMain(argc, argv));
index 43deb57aa2c2ca1e744e7f73e2d47ea6415156eb..78d0471f85d2a84999a28958615abc230c294012 100644 (file)
@@ -37,7 +37,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.479 2006/01/06 02:58:25 tgl Exp $
+ *   $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.480 2006/02/01 00:31:59 momjian Exp $
  *
  * NOTES
  *
@@ -171,9 +171,6 @@ char       *ListenAddresses;
  */
 int            ReservedBackends;
 
-
-static const char *progname = NULL;
-
 /* The socket(s) we're listening to. */
 #define MAXLISTEN  64
 static int ListenSocket[MAXLISTEN];
@@ -383,9 +380,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 0ad21cf59c0f8e79f7c78d1102502c12b903f579..ed3847e8765f36cbc86b94c2d8d66a51a58af4d1 100644 (file)
@@ -6,7 +6,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.11 2005/08/20 23:26:33 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.12 2006/02/01 00:31:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -34,6 +34,7 @@ extern char *bonjour_name;
 extern HANDLE PostmasterHandle;
 #endif
 
+extern const char *progname;
 
 extern int PostmasterMain(int argc, char *argv[]);
 extern void ClosePostmasterPorts(bool am_syslogger);
index 463c50f72879a591165b347a4c2fb1cff32fce13..ba7fd9d712da95e6b3df63103d470c7d5c0a13fa 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *   $PostgreSQL: pgsql/src/port/path.c,v 1.63 2005/12/23 22:34:22 tgl Exp $
+ *   $PostgreSQL: pgsql/src/port/path.c,v 1.64 2006/02/01 00:31:59 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -388,7 +388,8 @@ path_is_prefix_of_path(const char *path1, const char *path2)
 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)
@@ -396,25 +397,25 @@ 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);  /* leaks memory, but called only once */
-       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;
 }