From de79ee4c5866e2c9676db7c1e3ca11bc5b3039e6 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 1 Feb 2006 00:47:03 +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 | 4 +++- src/backend/postmaster/postmaster.c | 6 ------ src/include/postmaster/postmaster.h | 1 + src/port/path.c | 33 +++++++++++++++-------------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/backend/main/main.c b/src/backend/main/main.c index 778b263c4f..bbe89bf7b7 100644 --- a/src/backend/main/main.c +++ b/src/backend/main/main.c @@ -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; diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c index afd861b5ca..4a9c1e98f3 100644 --- a/src/backend/postmaster/postmaster.c +++ b/src/backend/postmaster/postmaster.c @@ -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; diff --git a/src/include/postmaster/postmaster.h b/src/include/postmaster/postmaster.h index 083968f967..9c0d9841a0 100644 --- a/src/include/postmaster/postmaster.h +++ b/src/include/postmaster/postmaster.h @@ -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); diff --git a/src/port/path.c b/src/port/path.c index e707455c60..715f4e5956 100644 --- a/src/port/path.c +++ b/src/port/path.c @@ -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 -- 2.39.5