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.
This commit is contained in:
Tom Lane 2001-10-21 03:25:36 +00:00
parent 2b7206a993
commit b2e92a712e
10 changed files with 147 additions and 113 deletions

View File

@ -13,7 +13,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.45 2001/06/03 14:53:56 petere Exp $
* $Header: /cvsroot/pgsql/src/backend/main/main.c,v 1.46 2001/10/21 03:25:35 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -39,12 +39,15 @@
#include "miscadmin.h"
#include "bootstrap/bootstrap.h"
#include "tcop/tcopprot.h"
#include "utils/ps_status.h"
int
main(int argc, char *argv[])
{
char **new_argv;
int i;
int len;
struct passwd *pw;
char *pw_name_persist;
@ -91,6 +94,12 @@ main(int argc, char *argv[])
beos_startup(argc, argv);
#endif
/*
* Not-quite-so-platform-specific startup environment checks. Still
* best to minimize these.
*/
/* Initialize NLS settings so we can give localized error messages */
#ifdef ENABLE_NLS
#ifdef LC_MESSAGES
setlocale(LC_MESSAGES, "");
@ -99,11 +108,6 @@ main(int argc, char *argv[])
textdomain("postgres");
#endif
/*
* Not-quite-so-platform-specific startup environment checks. Still
* best to minimize these.
*/
/*
* Skip permission checks if we're just trying to do --help or --version;
* otherwise root will get unhelpful failure messages from initdb.
@ -164,26 +168,47 @@ main(int argc, char *argv[])
setlocale(LC_MONETARY, "");
#endif
/*
* Remember the physical location of the initially given argv[] array,
* since on some platforms that storage must be overwritten in order
* to set the process title for ps. Then make a copy of the argv[]
* array for subsequent use, so that argument parsing doesn't get
* affected if init_ps_display overwrites the original argv[].
*
* (NB: do NOT think to remove this copying, even though postmaster.c
* finishes looking at argv[] long before we ever consider changing
* the ps display. On some platforms, getopt(3) keeps pointers into
* the argv array, and will get horribly confused when it is re-called
* to analyze a subprocess' argument string if the argv storage has
* been clobbered meanwhile.)
*/
save_ps_display_args(argc, argv);
new_argv = (char **) malloc((argc + 1) * sizeof(char *));
for (i = 0; i < argc; i++)
new_argv[i] = strdup(argv[i]);
new_argv[argc] = NULL;
/*
* Now dispatch to one of PostmasterMain, PostgresMain, or
* BootstrapMain depending on the program name (and possibly first
* argument) we were called with. The lack of consistency here is
* historical.
*/
len = strlen(argv[0]);
len = strlen(new_argv[0]);
if (len >= 10 && strcmp(argv[0] + len - 10, "postmaster") == 0)
if (len >= 10 && strcmp(new_argv[0] + len - 10, "postmaster") == 0)
{
/* Called as "postmaster" */
exit(PostmasterMain(argc, argv));
exit(PostmasterMain(argc, new_argv));
}
/*
* If the first argument is "-boot", then invoke bootstrap mode. Note
* we remove "-boot" from the arguments passed on to BootstrapMain.
*/
if (argc > 1 && strcmp(argv[1], "-boot") == 0)
exit(BootstrapMain(argc - 1, argv + 1));
if (argc > 1 && strcmp(new_argv[1], "-boot") == 0)
exit(BootstrapMain(argc - 1, new_argv + 1));
/*
* Otherwise we're a standalone backend. Invoke PostgresMain,
@ -194,11 +219,11 @@ main(int argc, char *argv[])
if (pw == NULL)
{
fprintf(stderr, gettext("%s: invalid current euid %d\n"),
argv[0], (int) geteuid());
new_argv[0], (int) geteuid());
exit(1);
}
/* Allocate new memory because later getpwuid() calls can overwrite it */
pw_name_persist = strdup(pw->pw_name);
exit(PostgresMain(argc, argv, argc, argv, pw_name_persist));
exit(PostgresMain(argc, new_argv, pw_name_persist));
}

View File

@ -16,7 +16,7 @@
*
* Copyright (c) 2001, PostgreSQL Global Development Group
*
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.11 2001/10/16 22:35:27 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/pgstat.c,v 1.12 2001/10/21 03:25:35 tgl Exp $
* ----------
*/
#include "postgres.h"
@ -95,8 +95,8 @@ static char pgStat_fname[MAXPGPATH];
* Local function forward declarations
* ----------
*/
static void pgstat_main(int real_argc, char *real_argv[]);
static void pgstat_recvbuffer(int real_argc, char *real_argv[]);
static void pgstat_main(void);
static void pgstat_recvbuffer(void);
static void pgstat_die(SIGNAL_ARGS);
static int pgstat_add_backend(PgStat_MsgHdr *msg);
@ -246,13 +246,10 @@ pgstat_init(void)
*
* Called from postmaster at startup or after an existing collector
* died. Fire up a fresh statistics collector.
*
* The process' original argc and argv are passed, because they are
* needed by init_ps_display() on some platforms.
* ----------
*/
int
pgstat_start(int real_argc, char *real_argv[])
pgstat_start(void)
{
/*
* Do nothing if no collector needed
@ -316,7 +313,7 @@ pgstat_start(int real_argc, char *real_argv[])
/* Close the postmaster's sockets, except for pgstat link */
ClosePostmasterPorts(false);
pgstat_main(real_argc, real_argv);
pgstat_main();
exit(0);
}
@ -1104,7 +1101,7 @@ pgstat_send(void *msg, int len)
* ----------
*/
static void
pgstat_main(int real_argc, char *real_argv[])
pgstat_main(void)
{
PgStat_Msg msg;
fd_set rfds;
@ -1176,7 +1173,7 @@ pgstat_main(int real_argc, char *real_argv[])
default:
/* parent becomes buffer process */
close(pgStatPipe[0]);
pgstat_recvbuffer(real_argc, real_argv);
pgstat_recvbuffer();
exit(0);
}
@ -1192,7 +1189,7 @@ pgstat_main(int real_argc, char *real_argv[])
* WARNING: On some platforms the environment will be moved around to
* make room for the ps display string.
*/
init_ps_display(real_argc, real_argv, "stats collector process", "", "");
init_ps_display("stats collector process", "", "");
set_ps_display("");
/*
@ -1451,7 +1448,7 @@ pgstat_main(int real_argc, char *real_argv[])
* ----------
*/
static void
pgstat_recvbuffer(int real_argc, char *real_argv[])
pgstat_recvbuffer(void)
{
fd_set rfds;
fd_set wfds;
@ -1477,7 +1474,7 @@ pgstat_recvbuffer(int real_argc, char *real_argv[])
* WARNING: On some platforms the environment will be moved around to
* make room for the ps display string.
*/
init_ps_display(real_argc, real_argv, "stats buffer process", "", "");
init_ps_display("stats buffer process", "", "");
set_ps_display("");
/*

View File

@ -37,7 +37,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.249 2001/10/19 20:47:09 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.250 2001/10/21 03:25:35 tgl Exp $
*
* NOTES
*
@ -153,8 +153,6 @@ int MaxBackends = DEF_MAXBACKENDS;
static char *progname = (char *) NULL;
static char **real_argv;
static int real_argc;
/* flag to indicate that SIGHUP arrived during server loop */
static volatile bool got_SIGHUP = false;
@ -228,9 +226,6 @@ extern int optind,
#ifdef HAVE_INT_OPTRESET
extern int optreset;
#endif
#ifdef HAVE_INT___GETOPT_INITIALIZED
extern int __getopt_initialized;
#endif
/*
* postmaster.c - function prototypes
@ -337,8 +332,6 @@ PostmasterMain(int argc, char *argv[])
*original_extraoptions = '\0';
progname = argv[0];
real_argv = argv;
real_argc = argc;
/*
* Catch standard options before doing much else. This even works on
@ -444,10 +437,7 @@ PostmasterMain(int argc, char *argv[])
/* reset getopt(3) to rescan arguments */
optind = 1;
#ifdef HAVE_INT_OPTRESET
optreset = 1; /* some systems need this */
#endif
#ifdef HAVE_INT___GETOPT_INITIALIZED
__getopt_initialized = 0; /* glibc needs this */
optreset = 1; /* some systems need this too */
#endif
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[])
*/
optind = 1;
#ifdef HAVE_INT_OPTRESET
optreset = 1; /* some systems need this */
#endif
#ifdef HAVE_INT___GETOPT_INITIALIZED
__getopt_initialized = 0; /* glibc needs this */
optreset = 1; /* some systems need this too */
#endif
/* For debugging: display postmaster environment */
@ -619,6 +606,13 @@ PostmasterMain(int argc, char *argv[])
fprintf(stderr, "-----------------------------------------\n");
}
/*
* On some systems our dynloader code needs the executable's pathname.
*/
if (FindExec(pg_pathname, progname, "postgres") < 0)
elog(FATAL, "%s: could not locate executable, bailing out...",
progname);
/*
* Initialize SSL library, if specified.
*/
@ -742,7 +736,7 @@ PostmasterMain(int argc, char *argv[])
*/
if (pgstat_init() < 0)
ExitPostmaster(1);
if (pgstat_start(real_argc, real_argv) < 0)
if (pgstat_start() < 0)
ExitPostmaster(1);
/*
@ -1570,7 +1564,7 @@ reaper(SIGNAL_ARGS)
else if (WIFSIGNALED(exitstatus))
elog(DEBUG, "statistics collector was terminated by signal %d",
WTERMSIG(exitstatus));
pgstat_start(real_argc, real_argv);
pgstat_start();
continue;
}
@ -1848,6 +1842,17 @@ BackendStartup(Port *port)
*/
MyCancelKey = PostmasterRandom();
/*
* Make room for backend data structure. Better before the fork()
* so we can handle failure cleanly.
*/
bn = (Backend *) malloc(sizeof(Backend));
if (!bn)
{
elog(DEBUG, "out of memory; connection startup aborted");
return STATUS_ERROR;
}
/*
* Flush stdio channels just before fork, to avoid double-output
* problems. Ideally we'd use fflush(NULL) here, but there are still a
@ -1864,17 +1869,6 @@ BackendStartup(Port *port)
beos_before_backend_startup();
#endif
/*
* Make room for backend data structure. Better before the fork()
* so we can handle failure cleanly.
*/
bn = (Backend *) malloc(sizeof(Backend));
if (!bn)
{
elog(DEBUG, "out of memory; connection startup aborted");
return STATUS_ERROR;
}
pid = fork();
if (pid == 0) /* child */
@ -1912,8 +1906,8 @@ BackendStartup(Port *port)
/* in parent, normal */
if (DebugLvl >= 1)
elog(DEBUG, "BackendStartup: pid=%d user=%s db=%s socket=%d\n",
pid, port->user, port->database, port->sock);
elog(DEBUG, "BackendStartup: forked pid=%d socket=%d",
pid, port->sock);
/*
* Everything's been successful, it's safe to add this backend to our
@ -2103,8 +2097,7 @@ DoBackend(Port *port)
* optarg or getenv() from above will be invalid after this call.
* Better use strdup or something similar.
*/
init_ps_display(real_argc, real_argv, port->user, port->database,
remote_host);
init_ps_display(port->user, port->database, remote_host);
set_ps_display("authentication");
/*
@ -2223,7 +2216,7 @@ DoBackend(Port *port)
fprintf(stderr, ")\n");
}
return (PostgresMain(ac, av, real_argc, real_argv, port->user));
return (PostgresMain(ac, av, port->user));
}
/*
@ -2469,7 +2462,7 @@ SSDataBase(int xlop)
statmsg = "??? subprocess";
break;
}
init_ps_display(real_argc, real_argv, statmsg, "", "");
init_ps_display(statmsg, "", "");
set_ps_display("");
/* Set up command-line arguments for subprocess */
@ -2568,7 +2561,7 @@ CreateOptsFile(int argc, char *argv[])
FILE *fp;
unsigned i;
if (FindExec(fullprogname, argv[0], "postmaster") == -1)
if (FindExec(fullprogname, argv[0], "postmaster") < 0)
return false;
filename = palloc(strlen(DataDir) + 20);

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.237 2001/10/19 18:19:41 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.238 2001/10/21 03:25:35 tgl Exp $
*
* NOTES
* this is the "main" module of the postgres backend and
@ -86,7 +86,6 @@ bool Warn_restart_ready = false;
bool InError = false;
static bool EchoQuery = false; /* default don't echo */
char pg_pathname[MAXPGPATH];
FILE *StatFp = NULL;
/* ----------------
@ -1097,17 +1096,14 @@ usage(char *progname)
* PostgresMain
* postgres main loop -- all backends, interactive or otherwise start here
*
* argc/argv are the command line arguments to be used. When being forked
* by the postmaster, these are not the original argv array of the process.
* real_argc/real_argv point to the original argv array, which is needed by
* `ps' display on some platforms. username is the (possibly authenticated)
* PostgreSQL user name to be used for the session.
* argc/argv are the command line arguments to be used. (When being forked
* by the postmaster, these are not the original argv array of the process.)
* username is the (possibly authenticated) PostgreSQL user name to be used
* for the session.
* ----------------------------------------------------------------
*/
int
PostgresMain(int argc, char *argv[],
int real_argc, char *real_argv[],
const char *username)
PostgresMain(int argc, char *argv[], const char *username)
{
int flag;
@ -1581,6 +1577,14 @@ PostgresMain(int argc, char *argv[],
proc_exit(1);
}
/*
* On some systems our dynloader code needs the executable's
* pathname. (If under postmaster, this was done already.)
*/
if (FindExec(pg_pathname, argv[0], "postgres") < 0)
elog(FATAL, "%s: could not locate executable, bailing out...",
argv[0]);
/*
* Validate we have been given a reasonable-looking DataDir
* (if under postmaster, assume postmaster did this already).
@ -1612,11 +1616,6 @@ PostgresMain(int argc, char *argv[],
SetCharSet();
#endif
/* On some systems our dynloader code needs the executable's pathname */
if (FindExec(pg_pathname, real_argv[0], "postgres") < 0)
elog(FATAL, "%s: could not locate executable, bailing out...",
real_argv[0]);
/*
* General initialization.
*
@ -1649,7 +1648,7 @@ PostgresMain(int argc, char *argv[],
if (!IsUnderPostmaster)
{
puts("\nPOSTGRES backend interactive interface ");
puts("$Revision: 1.237 $ $Date: 2001/10/19 18:19:41 $\n");
puts("$Revision: 1.238 $ $Date: 2001/10/21 03:25:35 $\n");
}
/*

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.60 2001/09/21 03:32:35 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/init/globals.c,v 1.61 2001/10/21 03:25:35 tgl Exp $
*
* NOTES
* Globals used all over the place should be declared here and not
@ -55,7 +55,9 @@ char *DataDir = NULL;
Relation reldesc; /* current relation descriptor */
char OutputFileName[MAXPGPATH] = "";
char OutputFileName[MAXPGPATH];
char pg_pathname[MAXPGPATH]; /* full path to postgres executable */
BackendId MyBackendId;

View File

@ -2,10 +2,10 @@
* ps_status.c
*
* Routines to support changing the ps display of PostgreSQL backends
* to contain some useful information. Differs wildly across
* to contain some useful information. Mechanism differs wildly across
* platforms.
*
* $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.5 2001/10/05 15:47:48 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/misc/ps_status.c,v 1.6 2001/10/21 03:25:35 tgl Exp $
*
* Copyright 2000 by PostgreSQL Global Development Group
* various details abducted from various places
@ -15,8 +15,6 @@
#include "postgres.h"
#include <unistd.h>
#ifdef HAVE_SYS_PSTAT_H
#include <sys/pstat.h> /* for HP-UX */
#endif
@ -26,7 +24,6 @@
#endif
#include "miscadmin.h"
#include "utils/ps_status.h"
extern char **environ;
@ -91,14 +88,29 @@ static size_t ps_buffer_size; /* space determined at run time */
static size_t ps_buffer_fixed_size; /* size of the constant prefix */
/* save the original argv[] location here */
static int save_argc;
static char **save_argv;
/*
* Call this once at backend start.
* Call this early in startup to save the original argc/argv values.
* argv[] will not be overwritten by this routine, but may be overwritten
* during init_ps_display.
*/
void
init_ps_display(int argc, char *argv[],
const char *username, const char *dbname,
save_ps_display_args(int argc, char *argv[])
{
save_argc = argc;
save_argv = argv;
}
/*
* Call this once during subprocess startup to set the identification
* values. At this point, the original argv[] array may be overwritten.
*/
void
init_ps_display(const char *username, const char *dbname,
const char *host_info)
{
#ifndef PS_USE_NONE
@ -109,9 +121,13 @@ init_ps_display(int argc, char *argv[],
if (!IsUnderPostmaster)
return;
/* no ps display if you didn't call save_ps_display_args() */
if (!save_argv)
return;
#ifdef PS_USE_CHANGE_ARGV
argv[0] = ps_buffer;
argv[1] = NULL;
save_argv[0] = ps_buffer;
save_argv[1] = NULL;
#endif /* PS_USE_CHANGE_ARGV */
#ifdef PS_USE_CLOBBER_ARGV
@ -127,9 +143,9 @@ init_ps_display(int argc, char *argv[],
/*
* check for contiguous argv strings
*/
for (i = 0; i < argc; i++)
if (i == 0 || end_of_area + 1 == argv[i])
end_of_area = argv[i] + strlen(argv[i]);
for (i = 0; i < save_argc; i++)
if (i == 0 || end_of_area + 1 == save_argv[i])
end_of_area = save_argv[i] + strlen(save_argv[i]);
/*
* check for contiguous environ strings following argv
@ -142,13 +158,14 @@ init_ps_display(int argc, char *argv[],
{
ps_buffer = NULL;
ps_buffer_size = 0;
return;
}
else
{
ps_buffer = argv[0];
ps_buffer_size = end_of_area - argv[0] - 1;
ps_buffer = save_argv[0];
ps_buffer_size = end_of_area - save_argv[0] - 1;
}
argv[1] = NULL;
save_argv[1] = NULL;
/*
* move the environment out of the way
@ -192,7 +209,7 @@ init_ps_display(int argc, char *argv[],
* indication of what you're currently doing passed in the argument.
*/
void
set_ps_display(const char *value)
set_ps_display(const char *activity)
{
#ifndef PS_USE_NONE
/* no ps display for stand-alone backend */
@ -205,8 +222,8 @@ set_ps_display(const char *value)
return;
#endif
/* Update ps_buffer to contain both fixed part and value */
StrNCpy(ps_buffer + ps_buffer_fixed_size, value,
/* Update ps_buffer to contain both fixed part and activity */
StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
ps_buffer_size - ps_buffer_fixed_size);
/* Transmit new setting to kernel, if necessary */
@ -247,7 +264,7 @@ set_ps_display(const char *value)
/*
* Returns what's currently in the ps display, in case someone needs
* it. Note that only the variable part is returned.
* it. Note that only the activity part is returned.
*/
const char *
get_ps_display(void)

View File

@ -12,7 +12,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: miscadmin.h,v 1.93 2001/09/29 04:02:26 tgl Exp $
* $Id: miscadmin.h,v 1.94 2001/10/21 03:25:35 tgl Exp $
*
* NOTES
* some of the information in this file should be moved to
@ -121,6 +121,7 @@ extern struct Port *MyProcPort;
extern long MyCancelKey;
extern char OutputFileName[];
extern char pg_pathname[];
/*
* done in storage/backendid.h for now.

View File

@ -5,7 +5,7 @@
*
* Copyright (c) 2001, PostgreSQL Global Development Group
*
* $Id: pgstat.h,v 1.7 2001/09/03 12:00:00 petere Exp $
* $Id: pgstat.h,v 1.8 2001/10/21 03:25:36 tgl Exp $
* ----------
*/
#ifndef PGSTAT_H
@ -333,7 +333,7 @@ extern bool pgstat_collect_blocklevel;
* ----------
*/
extern int pgstat_init(void);
extern int pgstat_start(int real_argc, char *real_argv[]);
extern int pgstat_start(void);
extern int pgstat_ispgstat(int pid);
extern void pgstat_close_sockets(void);
extern void pgstat_beterm(int pid);

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: tcopprot.h,v 1.42 2001/09/07 16:12:49 wieck Exp $
* $Id: tcopprot.h,v 1.43 2001/10/21 03:25:36 tgl Exp $
*
* OLD COMMENTS
* This file was created so that other c files could get the two
@ -45,8 +45,7 @@ extern void pg_exec_query_string(char *query_string,
extern void die(SIGNAL_ARGS);
extern void quickdie(SIGNAL_ARGS);
extern void authdie(SIGNAL_ARGS);
extern int PostgresMain(int argc, char *argv[],
int real_argc, char *real_argv[], const char *username);
extern int PostgresMain(int argc, char *argv[], const char *username);
extern void ResetUsage(void);
extern void ShowUsage(void);
extern FILE *StatFp;

View File

@ -4,20 +4,21 @@
*
* Declarations for backend/utils/misc/ps_status.c
*
* $Id: ps_status.h,v 1.20 2001/10/21 03:25:36 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#ifndef PS_STATUS_H
#define PS_STATUS_H
void init_ps_display(int argc, char *argv[],
const char *username, const char *dbname,
const char *host_info);
extern void save_ps_display_args(int argc, char *argv[]);
void
set_ps_display(const char *value);
extern void init_ps_display(const char *username, const char *dbname,
const char *host_info);
const char *
get_ps_display(void);
extern void set_ps_display(const char *activity);
extern const char *get_ps_display(void);
#endif /* PS_STATUS_H */