From 62a142036ba59c8167ab4539a414e56ba4c39faa Mon Sep 17 00:00:00 2001
From: Bruce Momjian <bruce@momjian.us>
Date: Wed, 1 Feb 2006 00:31:59 +0000
Subject: [PATCH] Set progname early in the postmaster/postgres binary, rather
 than doing 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             |  8 ++++---
 src/backend/postmaster/postmaster.c |  8 +------
 src/include/postmaster/postmaster.h |  3 ++-
 src/port/path.c                     | 33 +++++++++++++++--------------
 4 files changed, 25 insertions(+), 27 deletions(-)

diff --git a/src/backend/main/main.c b/src/backend/main/main.c
index c2ea5fc8578..64d77451c0b 100644
--- a/src/backend/main/main.c
+++ b/src/backend/main/main.c
@@ -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));
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 43deb57aa2c..78d0471f85d 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -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;
diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h
index 0ad21cf59c0..ed3847e8765 100644
--- a/src/include/postmaster/postmaster.h
+++ b/src/include/postmaster/postmaster.h
@@ -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);
diff --git a/src/port/path.c b/src/port/path.c
index 463c50f7287..ba7fd9d712d 100644
--- a/src/port/path.c
+++ b/src/port/path.c
@@ -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;
 }
 
 
-- 
GitLab