Commit f69ecb4f authored by Bruce Momjian's avatar Bruce Momjian

Reorganize backend code to more cleanly manage executable names and

backend startup.
parent 261eda55
...@@ -16,7 +16,7 @@ port_id beos_dl_port_out = 0; ...@@ -16,7 +16,7 @@ port_id beos_dl_port_out = 0;
sem_id beos_shm_sem; sem_id beos_shm_sem;
/* Global var containing the postgres path */ /* Global var containing the postgres path */
extern char pg_pathname[]; extern char my_exec_path[];
/* Shared library loading doesn't work after fork in beos. The solution is to use an exact /* Shared library loading doesn't work after fork in beos. The solution is to use an exact
...@@ -50,7 +50,7 @@ beos_dl_open(char *filename) ...@@ -50,7 +50,7 @@ beos_dl_open(char *filename)
char Cmd[4000]; char Cmd[4000];
/* Build arg list */ /* Build arg list */
sprintf(Cmd, "%s -beossupportserver %d %d &", pg_pathname, (int) beos_dl_port_in, (int) beos_dl_port_out); sprintf(Cmd, "%s -beossupportserver %d %d &", my_exec_path, (int) beos_dl_port_in, (int) beos_dl_port_out);
/* Lauch process */ /* Lauch process */
system(Cmd); system(Cmd);
......
...@@ -11,14 +11,14 @@ ...@@ -11,14 +11,14 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/port/dynloader/bsdi.c,v 1.23 2003/11/29 19:51:54 pgsql Exp $ * $PostgreSQL: pgsql/src/backend/port/dynloader/bsdi.c,v 1.24 2004/05/13 22:45:02 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
#include "postgres.h" #include "postgres.h"
#ifndef HAVE_DLOPEN #ifndef HAVE_DLOPEN
extern char pg_pathname[]; extern char my_exec_path[];
void * void *
pg_dlopen(char *filename) pg_dlopen(char *filename)
...@@ -31,7 +31,7 @@ pg_dlopen(char *filename) ...@@ -31,7 +31,7 @@ pg_dlopen(char *filename)
*/ */
if (!dl_initialized) if (!dl_initialized)
{ {
if (dld_init(dld_find_executable(pg_pathname))) if (dld_init(dld_find_executable(my_exec_path)))
return NULL; return NULL;
/* /*
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/port/dynloader/linux.c,v 1.27 2003/11/29 19:51:54 pgsql Exp $ * $PostgreSQL: pgsql/src/backend/port/dynloader/linux.c,v 1.28 2004/05/13 22:45:02 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -43,7 +43,7 @@ pg_dlopen(char *filename) ...@@ -43,7 +43,7 @@ pg_dlopen(char *filename)
*/ */
if (!dl_initialized) if (!dl_initialized)
{ {
if (dld_init(dld_find_executable(pg_pathname))) if (dld_init(dld_find_executable(my_exec_path)))
return NULL; return NULL;
/* /*
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/port/dynloader/ultrix4.c,v 1.19 2003/11/29 19:51:54 pgsql Exp $ * $PostgreSQL: pgsql/src/backend/port/dynloader/ultrix4.c,v 1.20 2004/05/13 22:45:02 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "dl.h" #include "dl.h"
#include "utils/dynamic_loader.h" #include "utils/dynamic_loader.h"
extern char pg_pathname[]; extern char my_exec_path[];
void * void *
pg_dlopen(char *filename) pg_dlopen(char *filename)
...@@ -31,7 +31,7 @@ pg_dlopen(char *filename) ...@@ -31,7 +31,7 @@ pg_dlopen(char *filename)
*/ */
if (!dl_initialized) if (!dl_initialized)
{ {
if (!dl_init(pg_pathname)) if (!dl_init(my_exec_path))
return NULL; return NULL;
/* /*
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* Copyright (c) 2001-2003, PostgreSQL Global Development Group * Copyright (c) 2001-2003, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.68 2004/05/06 19:23:25 momjian Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.69 2004/05/13 22:45:02 momjian Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -487,7 +487,7 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType) ...@@ -487,7 +487,7 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType)
/* + the pstat file names, and postgres pathname */ /* + the pstat file names, and postgres pathname */
snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",pgStat_tmpfname); snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",pgStat_tmpfname);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",pgStat_fname); snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",pgStat_fname);
snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",pg_pathname); snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",my_exec_path); /* used? */
snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",DataDir); snprintf(pgstatBuf[bufc++],MAXPGPATH,"\"%s\"",DataDir);
/* Add to the arg list */ /* Add to the arg list */
...@@ -500,9 +500,9 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType) ...@@ -500,9 +500,9 @@ pgstat_forkexec(STATS_PROCESS_TYPE procType)
/* Fire off execv in child */ /* Fire off execv in child */
#ifdef WIN32 #ifdef WIN32
pid = win32_forkexec(pg_pathname,av); pid = win32_forkexec(my_exec_path, av);
#else #else
if ((pid = fork()) == 0 && (execv(pg_pathname,av) == -1)) if ((pid = fork()) == 0 && (execv(my_exec_path, av) == -1))
/* FIXME: [fork/exec] suggestions for what to do here? Can't call elog... */ /* FIXME: [fork/exec] suggestions for what to do here? Can't call elog... */
abort(); abort();
#endif #endif
...@@ -530,9 +530,9 @@ pgstat_parseArgs(PGSTAT_FORK_ARGS) ...@@ -530,9 +530,9 @@ pgstat_parseArgs(PGSTAT_FORK_ARGS)
pgStatPipe[0] = atoi(argv[argc++]); pgStatPipe[0] = atoi(argv[argc++]);
pgStatPipe[1] = atoi(argv[argc++]); pgStatPipe[1] = atoi(argv[argc++]);
MaxBackends = atoi(argv[argc++]); MaxBackends = atoi(argv[argc++]);
strncpy(pgStat_tmpfname,argv[argc++],MAXPGPATH); StrNCpy(pgStat_tmpfname,argv[argc++],MAXPGPATH);
strncpy(pgStat_fname, argv[argc++],MAXPGPATH); StrNCpy(pgStat_fname, argv[argc++],MAXPGPATH);
strncpy(pg_pathname, argv[argc++],MAXPGPATH); StrNCpy(my_exec_path, argv[argc++],MAXPGPATH);
DataDir = strdup(argv[argc++]); DataDir = strdup(argv[argc++]);
read_nondefault_variables(); read_nondefault_variables();
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.385 2004/05/12 13:38:39 momjian Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.386 2004/05/13 22:45:02 momjian Exp $
* *
* NOTES * NOTES
* *
...@@ -181,7 +181,6 @@ static int ListenSocket[MAXLISTEN]; ...@@ -181,7 +181,6 @@ static int ListenSocket[MAXLISTEN];
/* Used to reduce macros tests */ /* Used to reduce macros tests */
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
const bool ExecBackend = true; const bool ExecBackend = true;
#else #else
const bool ExecBackend = false; const bool ExecBackend = false;
#endif #endif
...@@ -286,7 +285,7 @@ static long PostmasterRandom(void); ...@@ -286,7 +285,7 @@ static long PostmasterRandom(void);
static void RandomSalt(char *cryptSalt, char *md5Salt); static void RandomSalt(char *cryptSalt, char *md5Salt);
static void SignalChildren(int signal); static void SignalChildren(int signal);
static int CountChildren(void); static int CountChildren(void);
static bool CreateOptsFile(int argc, char *argv[]); static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
NON_EXEC_STATIC void SSDataBaseInit(int xlop); NON_EXEC_STATIC void SSDataBaseInit(int xlop);
static pid_t SSDataBase(int xlop); static pid_t SSDataBase(int xlop);
static void static void
...@@ -295,6 +294,9 @@ postmaster_error(const char *fmt,...) ...@@ -295,6 +294,9 @@ postmaster_error(const char *fmt,...)
__attribute__((format(printf, 1, 2))); __attribute__((format(printf, 1, 2)));
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
static char postgres_exec_path[MAXPGPATH];
#ifdef WIN32 #ifdef WIN32
pid_t win32_forkexec(const char* path, char *argv[]); pid_t win32_forkexec(const char* path, char *argv[]);
...@@ -323,7 +325,6 @@ static void ShmemBackendArrayRemove(pid_t pid); ...@@ -323,7 +325,6 @@ static void ShmemBackendArrayRemove(pid_t pid);
#define StartBackgroundWriter() SSDataBase(BS_XLOG_BGWRITER) #define StartBackgroundWriter() SSDataBase(BS_XLOG_BGWRITER)
#define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN) #define ShutdownDataBase() SSDataBase(BS_XLOG_SHUTDOWN)
static void static void
checkDataDir(const char *checkdir) checkDataDir(const char *checkdir)
{ {
...@@ -692,11 +693,18 @@ PostmasterMain(int argc, char *argv[]) ...@@ -692,11 +693,18 @@ PostmasterMain(int argc, char *argv[])
/* /*
* On some systems our dynloader code needs the executable's pathname. * On some systems our dynloader code needs the executable's pathname.
*/ */
if (find_my_exec(pg_pathname, argv[0]) < 0) if (find_my_exec(my_exec_path, argv[0]) < 0)
ereport(FATAL, ereport(FATAL,
(errmsg("%s: could not locate postgres executable", (errmsg("%s: could not locate my own executable path",
progname))); progname)));
#ifdef EXEC_BACKEND
if (find_other_exec(postgres_exec_path, argv[0], "postgres", PG_VERSIONSTR) < 0)
ereport(FATAL,
(errmsg("%s: could not locate postgres executable or non-matching version",
progname)));
#endif
/* /*
* Initialize SSL library, if specified. * Initialize SSL library, if specified.
*/ */
...@@ -852,7 +860,7 @@ PostmasterMain(int argc, char *argv[]) ...@@ -852,7 +860,7 @@ PostmasterMain(int argc, char *argv[])
* recording bogus options (eg, NBuffers too high for available * recording bogus options (eg, NBuffers too high for available
* memory). * memory).
*/ */
if (!CreateOptsFile(argc, argv)) if (!CreateOptsFile(argc, argv, my_exec_path))
ExitPostmaster(1); ExitPostmaster(1);
/* /*
...@@ -2754,10 +2762,10 @@ Backend_forkexec(Port *port) ...@@ -2754,10 +2762,10 @@ Backend_forkexec(Port *port)
Assert(ac <= lengthof(av)); Assert(ac <= lengthof(av));
#ifdef WIN32 #ifdef WIN32
pid = win32_forkexec(pg_pathname,av); /* logs on error */ pid = win32_forkexec(postgres_exec_path, av); /* logs on error */
#else #else
/* Fire off execv in child */ /* Fire off execv in child */
if ((pid = fork()) == 0 && (execv(pg_pathname,av) == -1)) if ((pid = fork()) == 0 && (execv(postgres_exec_path, av) == -1))
/* /*
* FIXME: [fork/exec] suggestions for what to do here? * FIXME: [fork/exec] suggestions for what to do here?
* Probably OK to issue error (unlike pgstat case) * Probably OK to issue error (unlike pgstat case)
...@@ -3116,12 +3124,12 @@ SSDataBase(int xlop) ...@@ -3116,12 +3124,12 @@ SSDataBase(int xlop)
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
/* EXEC_BACKEND case; fork/exec here */ /* EXEC_BACKEND case; fork/exec here */
#ifdef WIN32 #ifdef WIN32
pid = win32_forkexec(pg_pathname,av); /* logs on error */ pid = win32_forkexec(postgres_exec_path, av); /* logs on error */
#else #else
if ((pid = fork()) == 0 && (execv(pg_pathname,av) == -1)) if ((pid = fork()) == 0 && (execv(postgres_exec_path, av) == -1))
{ {
/* in child */ /* in child */
elog(ERROR,"unable to execv in SSDataBase: %m"); elog(ERROR, "unable to execv in SSDataBase: %m");
exit(0); exit(0);
} }
#endif #endif
...@@ -3215,19 +3223,12 @@ SSDataBase(int xlop) ...@@ -3215,19 +3223,12 @@ SSDataBase(int xlop)
* Create the opts file * Create the opts file
*/ */
static bool static bool
CreateOptsFile(int argc, char *argv[]) CreateOptsFile(int argc, char *argv[], char *fullprogname)
{ {
char fullprogname[MAXPGPATH];
char filename[MAXPGPATH]; char filename[MAXPGPATH];
FILE *fp; FILE *fp;
int i; int i;
if (find_my_exec(fullprogname, argv[0]) < 0)
{
elog(LOG, "could not locate postmaster");
return false;
}
snprintf(filename, sizeof(filename), "%s/postmaster.opts", DataDir); snprintf(filename, sizeof(filename), "%s/postmaster.opts", DataDir);
if ((fp = fopen(filename, "w")) == NULL) if ((fp = fopen(filename, "w")) == NULL)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.404 2004/05/12 13:38:40 momjian Exp $ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.405 2004/05/13 22:45:03 momjian Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -2159,7 +2159,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2159,7 +2159,7 @@ PostgresMain(int argc, char *argv[], const char *username)
} }
if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0) if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
{ {
puts("postgres (PostgreSQL) " PG_VERSION); puts(PG_VERSIONSTR);
exit(0); exit(0);
} }
} }
...@@ -2646,14 +2646,12 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2646,14 +2646,12 @@ PostgresMain(int argc, char *argv[], const char *username)
} }
/* /*
* On some systems our dynloader code needs the executable's * On some systems our dynloader code needs the executable's pathname.
* pathname. (If under postmaster, this was done already.)
*/ */
if (find_my_exec(pg_pathname, argv[0]) < 0) if (strlen(my_exec_path) == 0 && find_my_exec(my_exec_path, argv[0]) < 0)
ereport(FATAL, ereport(FATAL,
(errmsg("%s: could not locate postgres executable", (errmsg("%s: could not locate postgres executable",
argv[0]))); argv[0])));
/* /*
* Validate we have been given a reasonable-looking DataDir (if * Validate we have been given a reasonable-looking DataDir (if
* under postmaster, assume postmaster did this already). * under postmaster, assume postmaster did this already).
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.84 2004/02/10 03:42:45 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/init/globals.c,v 1.85 2004/05/13 22:45:03 momjian Exp $
* *
* NOTES * NOTES
* Globals used all over the place should be declared here and not * Globals used all over the place should be declared here and not
...@@ -45,8 +45,7 @@ char *DataDir = NULL; ...@@ -45,8 +45,7 @@ char *DataDir = NULL;
char OutputFileName[MAXPGPATH]; char OutputFileName[MAXPGPATH];
char pg_pathname[MAXPGPATH]; /* full path to postgres char my_exec_path[MAXPGPATH]; /* full path to postgres executable */
* executable */
BackendId MyBackendId; BackendId MyBackendId;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.157 2004/05/11 21:57:15 momjian Exp $ * $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.158 2004/05/13 22:45:04 momjian Exp $
* *
* NOTES * NOTES
* some of the information in this file should be moved to * some of the information in this file should be moved to
...@@ -124,6 +124,8 @@ extern void SubPostmasterMain(int argc, char* argv[]); ...@@ -124,6 +124,8 @@ extern void SubPostmasterMain(int argc, char* argv[]);
#endif #endif
extern void ClosePostmasterPorts(bool pgstat_too); extern void ClosePostmasterPorts(bool pgstat_too);
#define PG_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
/* /*
* from utils/init/globals.c * from utils/init/globals.c
*/ */
...@@ -140,7 +142,7 @@ extern struct Port *MyProcPort; ...@@ -140,7 +142,7 @@ extern struct Port *MyProcPort;
extern long MyCancelKey; extern long MyCancelKey;
extern char OutputFileName[]; extern char OutputFileName[];
extern char pg_pathname[]; extern char my_exec_path[];
/* /*
* done in storage/backendid.h for now. * done in storage/backendid.h for now.
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/port/exec.c,v 1.3 2004/05/13 01:47:12 momjian Exp $ * $PostgreSQL: pgsql/src/port/exec.c,v 1.4 2004/05/13 22:45:04 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -184,7 +184,6 @@ find_my_exec(char *full_path, const char *argv0) ...@@ -184,7 +184,6 @@ find_my_exec(char *full_path, const char *argv0)
char *path, char *path,
*startp, *startp,
*endp; *endp;
const char *binary_name = get_progname(argv0);
/* /*
* First try: use the binary that's located in the * First try: use the binary that's located in the
...@@ -192,24 +191,23 @@ find_my_exec(char *full_path, const char *argv0) ...@@ -192,24 +191,23 @@ find_my_exec(char *full_path, const char *argv0)
* Presumably the user used an explicit path because it * Presumably the user used an explicit path because it
* wasn't in PATH, and we don't want to use incompatible executables. * wasn't in PATH, and we don't want to use incompatible executables.
* *
* This has the neat property that it works for installed binaries, old * For the binary: First try: if we're given some kind of path, use it
* source trees (obj/support/post{master,gres}) and new source
* trees (obj/post{master,gres}) because they all put the two binaries
* in the same place.
*
* for the binary: First try: if we're given some kind of path, use it
* (making sure that a relative path is made absolute before returning * (making sure that a relative path is made absolute before returning
* it). * it).
*/ */
if (argv0 && (p = last_path_separator(argv0)) && *++p) /* Does argv0 have a separator? */
if (argv0 && (p = last_path_separator(argv0)))
{
if (*++p == '\0')
{ {
log_debug("argv[0] ends with a path separator \"%s\"", argv0);
return -1;
}
if (is_absolute_path(argv0) || !getcwd(buf, MAXPGPATH)) if (is_absolute_path(argv0) || !getcwd(buf, MAXPGPATH))
buf[0] = '\0'; buf[0] = '\0';
else else /* path is not absolute and getcwd worked */
strcat(buf, "/"); strcat(buf, "/");
strcat(buf, argv0); strcat(buf, argv0);
p = last_path_separator(buf);
strcpy(++p, binary_name);
if (validate_exec(buf) == 0) if (validate_exec(buf) == 0)
{ {
strncpy(full_path, buf, MAXPGPATH); strncpy(full_path, buf, MAXPGPATH);
...@@ -239,11 +237,11 @@ find_my_exec(char *full_path, const char *argv0) ...@@ -239,11 +237,11 @@ find_my_exec(char *full_path, const char *argv0)
*endp = '\0'; *endp = '\0';
if (is_absolute_path(startp) || !getcwd(buf, MAXPGPATH)) if (is_absolute_path(startp) || !getcwd(buf, MAXPGPATH))
buf[0] = '\0'; buf[0] = '\0';
else else /* path is not absolute and getcwd worked */
strcat(buf, "/"); strcat(buf, "/");
strcat(buf, startp); strcat(buf, startp);
strcat(buf, "/"); strcat(buf, "/");
strcat(buf, binary_name); strcat(buf, argv0);
switch (validate_exec(buf)) switch (validate_exec(buf))
{ {
case 0: /* found ok */ case 0: /* found ok */
...@@ -265,7 +263,7 @@ find_my_exec(char *full_path, const char *argv0) ...@@ -265,7 +263,7 @@ find_my_exec(char *full_path, const char *argv0)
free(path); free(path);
} }
log_debug("could not find a \"%s\" to execute", binary_name); log_debug("could not find a \"%s\" to execute", argv0);
return -1; return -1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment