Skip to content

Commit 62a1420

Browse files
committed
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.
1 parent c6ef326 commit 62a1420

File tree

4 files changed

+25
-27
lines changed

4 files changed

+25
-27
lines changed

src/backend/main/main.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/main/main.c,v 1.99 2006/01/05 03:01:34 momjian Exp $
16+
* $PostgreSQL: pgsql/src/backend/main/main.c,v 1.100 2006/02/01 00:31:59 momjian Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -45,7 +45,7 @@
4545
#include "libpq/pqsignal.h"
4646
#endif
4747

48-
48+
const char *progname;
4949

5050
int
5151
main(int argc, char *argv[])
@@ -77,6 +77,8 @@ main(int argc, char *argv[])
7777
char *env_locale;
7878
#endif
7979

80+
progname = get_progname(argv[0]);
81+
8082
/*
8183
* On some platforms, unaligned memory accesses result in a kernel trap;
8284
* the default kernel behavior is to emulate the memory access, but this
@@ -246,7 +248,7 @@ main(int argc, char *argv[])
246248
* possibly first argument) we were called with. The lack of consistency
247249
* here is historical.
248250
*/
249-
if (strcmp(get_progname(argv[0]), "postmaster") == 0)
251+
if (strcmp(progname, "postmaster") == 0)
250252
{
251253
/* Called as "postmaster" */
252254
exit(PostmasterMain(argc, argv));

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.479 2006/01/06 02:58:25 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.480 2006/02/01 00:31:59 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -171,9 +171,6 @@ char *ListenAddresses;
171171
*/
172172
int ReservedBackends;
173173

174-
175-
static const char *progname = NULL;
176-
177174
/* The socket(s) we're listening to. */
178175
#define MAXLISTEN 64
179176
static int ListenSocket[MAXLISTEN];
@@ -383,9 +380,6 @@ PostmasterMain(int argc, char *argv[])
383380
char *userDoption = NULL;
384381
int i;
385382

386-
/* This will call exit() if strdup() fails. */
387-
progname = get_progname(argv[0]);
388-
389383
MyProcPid = PostmasterPid = getpid();
390384

391385
IsPostmasterEnvironment = true;

src/include/postmaster/postmaster.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.11 2005/08/20 23:26:33 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/postmaster/postmaster.h,v 1.12 2006/02/01 00:31:59 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -34,6 +34,7 @@ extern char *bonjour_name;
3434
extern HANDLE PostmasterHandle;
3535
#endif
3636

37+
extern const char *progname;
3738

3839
extern int PostmasterMain(int argc, char *argv[]);
3940
extern void ClosePostmasterPorts(bool am_syslogger);

src/port/path.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/port/path.c,v 1.63 2005/12/23 22:34:22 tgl Exp $
11+
* $PostgreSQL: pgsql/src/port/path.c,v 1.64 2006/02/01 00:31:59 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -388,33 +388,34 @@ path_is_prefix_of_path(const char *path1, const char *path2)
388388
const char *
389389
get_progname(const char *argv0)
390390
{
391-
const char *nodir_name;
391+
const char *nodir_name;
392+
const char *progname;
392393

393394
nodir_name = last_dir_separator(argv0);
394395
if (nodir_name)
395396
nodir_name++;
396397
else
397398
nodir_name = skip_drive(argv0);
398399

399-
#if defined(__CYGWIN__) || defined(WIN32)
400-
/* strip .exe suffix, regardless of case */
401-
if (strlen(nodir_name) > sizeof(EXE) - 1 &&
402-
pg_strcasecmp(nodir_name + strlen(nodir_name) - (sizeof(EXE) - 1), EXE) == 0)
400+
/*
401+
* Make a copy in case argv[0] is modified by ps_status.
402+
* Leaks memory, but called only once.
403+
*/
404+
progname = strdup(nodir_name);
405+
if (progname == NULL)
403406
{
404-
char *progname;
407+
fprintf(stderr, "%s: out of memory\n", nodir_name);
408+
exit(1); /* This could exit the postmaster */
409+
}
405410

406-
progname = strdup(nodir_name); /* leaks memory, but called only once */
407-
if (progname == NULL)
408-
{
409-
fprintf(stderr, "%s: out of memory\n", nodir_name);
410-
exit(1); /* This could exit the postmaster */
411-
}
411+
#if defined(__CYGWIN__) || defined(WIN32)
412+
/* strip ".exe" suffix, regardless of case */
413+
if (strlen(progname) > sizeof(EXE) - 1 &&
414+
pg_strcasecmp(progname + strlen(progname) - (sizeof(EXE) - 1), EXE) == 0)
412415
progname[strlen(progname) - (sizeof(EXE) - 1)] = '\0';
413-
nodir_name = progname;
414-
}
415416
#endif
416417

417-
return nodir_name;
418+
return progname;
418419
}
419420

420421

0 commit comments

Comments
 (0)