Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b2e92a7

Browse files
committed
Fix getopt-vs-init_ps_display problem by copying original argv[] info,
per suggestion from Peter. Simplify several APIs by transmitting the original argv location directly from main.c to ps_status.c, instead of passing it down through several levels of subroutines.
1 parent 2b7206a commit b2e92a7

File tree

10 files changed

+147
-113
lines changed

10 files changed

+147
-113
lines changed

src/backend/main/main.c

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.45 2001/06/03 14:53:56 petere Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.46 2001/10/21 03:25:35 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -39,12 +39,15 @@
3939
#include "miscadmin.h"
4040
#include "bootstrap/bootstrap.h"
4141
#include "tcop/tcopprot.h"
42+
#include "utils/ps_status.h"
4243

4344

4445

4546
int
4647
main(int argc, char *argv[])
4748
{
49+
char **new_argv;
50+
int i;
4851
int len;
4952
struct passwd *pw;
5053
char *pw_name_persist;
@@ -91,6 +94,12 @@ main(int argc, char *argv[])
9194
beos_startup(argc, argv);
9295
#endif
9396

97+
/*
98+
* Not-quite-so-platform-specific startup environment checks. Still
99+
* best to minimize these.
100+
*/
101+
102+
/* Initialize NLS settings so we can give localized error messages */
94103
#ifdef ENABLE_NLS
95104
#ifdef LC_MESSAGES
96105
setlocale(LC_MESSAGES, "");
@@ -99,11 +108,6 @@ main(int argc, char *argv[])
99108
textdomain("postgres");
100109
#endif
101110

102-
/*
103-
* Not-quite-so-platform-specific startup environment checks. Still
104-
* best to minimize these.
105-
*/
106-
107111
/*
108112
* Skip permission checks if we're just trying to do --help or --version;
109113
* otherwise root will get unhelpful failure messages from initdb.
@@ -164,26 +168,47 @@ main(int argc, char *argv[])
164168
setlocale(LC_MONETARY, "");
165169
#endif
166170

171+
/*
172+
* Remember the physical location of the initially given argv[] array,
173+
* since on some platforms that storage must be overwritten in order
174+
* to set the process title for ps. Then make a copy of the argv[]
175+
* array for subsequent use, so that argument parsing doesn't get
176+
* affected if init_ps_display overwrites the original argv[].
177+
*
178+
* (NB: do NOT think to remove this copying, even though postmaster.c
179+
* finishes looking at argv[] long before we ever consider changing
180+
* the ps display. On some platforms, getopt(3) keeps pointers into
181+
* the argv array, and will get horribly confused when it is re-called
182+
* to analyze a subprocess' argument string if the argv storage has
183+
* been clobbered meanwhile.)
184+
*/
185+
save_ps_display_args(argc, argv);
186+
187+
new_argv = (char **) malloc((argc + 1) * sizeof(char *));
188+
for (i = 0; i < argc; i++)
189+
new_argv[i] = strdup(argv[i]);
190+
new_argv[argc] = NULL;
191+
167192
/*
168193
* Now dispatch to one of PostmasterMain, PostgresMain, or
169194
* BootstrapMain depending on the program name (and possibly first
170195
* argument) we were called with. The lack of consistency here is
171196
* historical.
172197
*/
173-
len = strlen(argv[0]);
198+
len = strlen(new_argv[0]);
174199

175-
if (len >= 10 && strcmp(argv[0] + len - 10, "postmaster") == 0)
200+
if (len >= 10 && strcmp(new_argv[0] + len - 10, "postmaster") == 0)
176201
{
177202
/* Called as "postmaster" */
178-
exit(PostmasterMain(argc, argv));
203+
exit(PostmasterMain(argc, new_argv));
179204
}
180205

181206
/*
182207
* If the first argument is "-boot", then invoke bootstrap mode. Note
183208
* we remove "-boot" from the arguments passed on to BootstrapMain.
184209
*/
185-
if (argc > 1 && strcmp(argv[1], "-boot") == 0)
186-
exit(BootstrapMain(argc - 1, argv + 1));
210+
if (argc > 1 && strcmp(new_argv[1], "-boot") == 0)
211+
exit(BootstrapMain(argc - 1, new_argv + 1));
187212

188213
/*
189214
* Otherwise we're a standalone backend. Invoke PostgresMain,
@@ -194,11 +219,11 @@ main(int argc, char *argv[])
194219
if (pw == NULL)
195220
{
196221
fprintf(stderr, gettext("%s: invalid current euid %d\n"),
197-
argv[0], (int) geteuid());
222+
new_argv[0], (int) geteuid());
198223
exit(1);
199224
}
200225
/* Allocate new memory because later getpwuid() calls can overwrite it */
201226
pw_name_persist = strdup(pw->pw_name);
202227

203-
exit(PostgresMain(argc, argv, argc, argv, pw_name_persist));
228+
exit(PostgresMain(argc, new_argv, pw_name_persist));
204229
}

src/backend/postmaster/pgstat.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* Copyright (c) 2001, PostgreSQL Global Development Group
1818
*
19-
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.11 2001/10/16 22:35:27 tgl Exp $
19+
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.12 2001/10/21 03:25:35 tgl Exp $
2020
* ----------
2121
*/
2222
#include "postgres.h"
@@ -95,8 +95,8 @@ static char pgStat_fname[MAXPGPATH];
9595
* Local function forward declarations
9696
* ----------
9797
*/
98-
static void pgstat_main(int real_argc, char *real_argv[]);
99-
static void pgstat_recvbuffer(int real_argc, char *real_argv[]);
98+
static void pgstat_main(void);
99+
static void pgstat_recvbuffer(void);
100100
static void pgstat_die(SIGNAL_ARGS);
101101

102102
static int pgstat_add_backend(PgStat_MsgHdr *msg);
@@ -246,13 +246,10 @@ pgstat_init(void)
246246
*
247247
* Called from postmaster at startup or after an existing collector
248248
* died. Fire up a fresh statistics collector.
249-
*
250-
* The process' original argc and argv are passed, because they are
251-
* needed by init_ps_display() on some platforms.
252249
* ----------
253250
*/
254251
int
255-
pgstat_start(int real_argc, char *real_argv[])
252+
pgstat_start(void)
256253
{
257254
/*
258255
* Do nothing if no collector needed
@@ -316,7 +313,7 @@ pgstat_start(int real_argc, char *real_argv[])
316313
/* Close the postmaster's sockets, except for pgstat link */
317314
ClosePostmasterPorts(false);
318315

319-
pgstat_main(real_argc, real_argv);
316+
pgstat_main();
320317

321318
exit(0);
322319
}
@@ -1104,7 +1101,7 @@ pgstat_send(void *msg, int len)
11041101
* ----------
11051102
*/
11061103
static void
1107-
pgstat_main(int real_argc, char *real_argv[])
1104+
pgstat_main(void)
11081105
{
11091106
PgStat_Msg msg;
11101107
fd_set rfds;
@@ -1176,7 +1173,7 @@ pgstat_main(int real_argc, char *real_argv[])
11761173
default:
11771174
/* parent becomes buffer process */
11781175
close(pgStatPipe[0]);
1179-
pgstat_recvbuffer(real_argc, real_argv);
1176+
pgstat_recvbuffer();
11801177
exit(0);
11811178
}
11821179

@@ -1192,7 +1189,7 @@ pgstat_main(int real_argc, char *real_argv[])
11921189
* WARNING: On some platforms the environment will be moved around to
11931190
* make room for the ps display string.
11941191
*/
1195-
init_ps_display(real_argc, real_argv, "stats collector process", "", "");
1192+
init_ps_display("stats collector process", "", "");
11961193
set_ps_display("");
11971194

11981195
/*
@@ -1451,7 +1448,7 @@ pgstat_main(int real_argc, char *real_argv[])
14511448
* ----------
14521449
*/
14531450
static void
1454-
pgstat_recvbuffer(int real_argc, char *real_argv[])
1451+
pgstat_recvbuffer(void)
14551452
{
14561453
fd_set rfds;
14571454
fd_set wfds;
@@ -1477,7 +1474,7 @@ pgstat_recvbuffer(int real_argc, char *real_argv[])
14771474
* WARNING: On some platforms the environment will be moved around to
14781475
* make room for the ps display string.
14791476
*/
1480-
init_ps_display(real_argc, real_argv, "stats buffer process", "", "");
1477+
init_ps_display("stats buffer process", "", "");
14811478
set_ps_display("");
14821479

14831480
/*

src/backend/postmaster/postmaster.c

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.249 2001/10/19 20:47:09 tgl Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.250 2001/10/21 03:25:35 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -153,8 +153,6 @@ int MaxBackends = DEF_MAXBACKENDS;
153153

154154

155155
static char *progname = (char *) NULL;
156-
static char **real_argv;
157-
static int real_argc;
158156

159157
/* flag to indicate that SIGHUP arrived during server loop */
160158
static volatile bool got_SIGHUP = false;
@@ -228,9 +226,6 @@ extern int optind,
228226
#ifdef HAVE_INT_OPTRESET
229227
extern int optreset;
230228
#endif
231-
#ifdef HAVE_INT___GETOPT_INITIALIZED
232-
extern int __getopt_initialized;
233-
#endif
234229

235230
/*
236231
* postmaster.c - function prototypes
@@ -337,8 +332,6 @@ PostmasterMain(int argc, char *argv[])
337332
*original_extraoptions = '\0';
338333

339334
progname = argv[0];
340-
real_argv = argv;
341-
real_argc = argc;
342335

343336
/*
344337
* Catch standard options before doing much else. This even works on
@@ -444,10 +437,7 @@ PostmasterMain(int argc, char *argv[])
444437
/* reset getopt(3) to rescan arguments */
445438
optind = 1;
446439
#ifdef HAVE_INT_OPTRESET
447-
optreset = 1; /* some systems need this */
448-
#endif
449-
#ifdef HAVE_INT___GETOPT_INITIALIZED
450-
__getopt_initialized = 0; /* glibc needs this */
440+
optreset = 1; /* some systems need this too */
451441
#endif
452442

453443
while ((opt = getopt(argc, argv, "A:a:B:b:c:D:d:Fh:ik:lm:MN:no:p:Ss-:")) != EOF)
@@ -599,10 +589,7 @@ PostmasterMain(int argc, char *argv[])
599589
*/
600590
optind = 1;
601591
#ifdef HAVE_INT_OPTRESET
602-
optreset = 1; /* some systems need this */
603-
#endif
604-
#ifdef HAVE_INT___GETOPT_INITIALIZED
605-
__getopt_initialized = 0; /* glibc needs this */
592+
optreset = 1; /* some systems need this too */
606593
#endif
607594

608595
/* For debugging: display postmaster environment */
@@ -619,6 +606,13 @@ PostmasterMain(int argc, char *argv[])
619606
fprintf(stderr, "-----------------------------------------\n");
620607
}
621608

609+
/*
610+
* On some systems our dynloader code needs the executable's pathname.
611+
*/
612+
if (FindExec(pg_pathname, progname, "postgres") < 0)
613+
elog(FATAL, "%s: could not locate executable, bailing out...",
614+
progname);
615+
622616
/*
623617
* Initialize SSL library, if specified.
624618
*/
@@ -742,7 +736,7 @@ PostmasterMain(int argc, char *argv[])
742736
*/
743737
if (pgstat_init() < 0)
744738
ExitPostmaster(1);
745-
if (pgstat_start(real_argc, real_argv) < 0)
739+
if (pgstat_start() < 0)
746740
ExitPostmaster(1);
747741

748742
/*
@@ -1570,7 +1564,7 @@ reaper(SIGNAL_ARGS)
15701564
else if (WIFSIGNALED(exitstatus))
15711565
elog(DEBUG, "statistics collector was terminated by signal %d",
15721566
WTERMSIG(exitstatus));
1573-
pgstat_start(real_argc, real_argv);
1567+
pgstat_start();
15741568
continue;
15751569
}
15761570

@@ -1848,6 +1842,17 @@ BackendStartup(Port *port)
18481842
*/
18491843
MyCancelKey = PostmasterRandom();
18501844

1845+
/*
1846+
* Make room for backend data structure. Better before the fork()
1847+
* so we can handle failure cleanly.
1848+
*/
1849+
bn = (Backend *) malloc(sizeof(Backend));
1850+
if (!bn)
1851+
{
1852+
elog(DEBUG, "out of memory; connection startup aborted");
1853+
return STATUS_ERROR;
1854+
}
1855+
18511856
/*
18521857
* Flush stdio channels just before fork, to avoid double-output
18531858
* problems. Ideally we'd use fflush(NULL) here, but there are still a
@@ -1864,17 +1869,6 @@ BackendStartup(Port *port)
18641869
beos_before_backend_startup();
18651870
#endif
18661871

1867-
/*
1868-
* Make room for backend data structure. Better before the fork()
1869-
* so we can handle failure cleanly.
1870-
*/
1871-
bn = (Backend *) malloc(sizeof(Backend));
1872-
if (!bn)
1873-
{
1874-
elog(DEBUG, "out of memory; connection startup aborted");
1875-
return STATUS_ERROR;
1876-
}
1877-
18781872
pid = fork();
18791873

18801874
if (pid == 0) /* child */
@@ -1912,8 +1906,8 @@ BackendStartup(Port *port)
19121906

19131907
/* in parent, normal */
19141908
if (DebugLvl >= 1)
1915-
elog(DEBUG, "BackendStartup: pid=%d user=%s db=%s socket=%d\n",
1916-
pid, port->user, port->database, port->sock);
1909+
elog(DEBUG, "BackendStartup: forked pid=%d socket=%d",
1910+
pid, port->sock);
19171911

19181912
/*
19191913
* Everything's been successful, it's safe to add this backend to our
@@ -2103,8 +2097,7 @@ DoBackend(Port *port)
21032097
* optarg or getenv() from above will be invalid after this call.
21042098
* Better use strdup or something similar.
21052099
*/
2106-
init_ps_display(real_argc, real_argv, port->user, port->database,
2107-
remote_host);
2100+
init_ps_display(port->user, port->database, remote_host);
21082101
set_ps_display("authentication");
21092102

21102103
/*
@@ -2223,7 +2216,7 @@ DoBackend(Port *port)
22232216
fprintf(stderr, ")\n");
22242217
}
22252218

2226-
return (PostgresMain(ac, av, real_argc, real_argv, port->user));
2219+
return (PostgresMain(ac, av, port->user));
22272220
}
22282221

22292222
/*
@@ -2469,7 +2462,7 @@ SSDataBase(int xlop)
24692462
statmsg = "??? subprocess";
24702463
break;
24712464
}
2472-
init_ps_display(real_argc, real_argv, statmsg, "", "");
2465+
init_ps_display(statmsg, "", "");
24732466
set_ps_display("");
24742467

24752468
/* Set up command-line arguments for subprocess */
@@ -2568,7 +2561,7 @@ CreateOptsFile(int argc, char *argv[])
25682561
FILE *fp;
25692562
unsigned i;
25702563

2571-
if (FindExec(fullprogname, argv[0], "postmaster") == -1)
2564+
if (FindExec(fullprogname, argv[0], "postmaster") < 0)
25722565
return false;
25732566

25742567
filename = palloc(strlen(DataDir) + 20);

0 commit comments

Comments
 (0)