Commit 488f2785 authored by Bruce Momjian's avatar Bruce Momjian

Final rearrangement of main postgresql child process (ie.

BackendFork/SSDataBase/pgstat) startup, to allow fork/exec calls to
closely mimic (the soon to be provided) Win32 CreateProcess equivalent
calls.

Claudio Natoli
parent 06288d4e
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.172 2003/12/25 03:52:50 momjian Exp $ * $PostgreSQL: pgsql/src/backend/bootstrap/bootstrap.c,v 1.173 2004/01/06 23:15:22 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -51,7 +51,7 @@ ...@@ -51,7 +51,7 @@
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
typedef struct Port Port; typedef struct Port Port;
extern void SSDataBaseInit(int); extern void SSDataBaseInit(int);
extern void read_backend_variables(pid_t, Port*); extern void read_backend_variables(unsigned long, Port*);
#endif #endif
extern int Int_yyparse(void); extern int Int_yyparse(void);
...@@ -231,6 +231,9 @@ BootstrapMain(int argc, char *argv[]) ...@@ -231,6 +231,9 @@ BootstrapMain(int argc, char *argv[])
int flag; int flag;
int xlogop = BS_XLOG_NOP; int xlogop = BS_XLOG_NOP;
char *potential_DataDir = NULL; char *potential_DataDir = NULL;
#ifdef EXEC_BACKEND
unsigned long backendID = 0;
#endif
/* /*
* initialize globals * initialize globals
...@@ -291,10 +294,15 @@ BootstrapMain(int argc, char *argv[]) ...@@ -291,10 +294,15 @@ BootstrapMain(int argc, char *argv[])
break; break;
case 'p': case 'p':
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
IsUnderPostmaster = true; {
char buf[MAXPGPATH];
IsUnderPostmaster = true;
sscanf(optarg,"%lu,%s",&backendID,buf);
dbname = strdup(buf);
}
#endif #endif
dbname = strdup(optarg); dbname = strdup(optarg);
break; break;
case 'B': case 'B':
SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV); SetConfigOption("shared_buffers", optarg, PGC_POSTMASTER, PGC_S_ARGV);
break; break;
...@@ -363,7 +371,7 @@ BootstrapMain(int argc, char *argv[]) ...@@ -363,7 +371,7 @@ BootstrapMain(int argc, char *argv[])
{ {
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
read_nondefault_variables(); read_nondefault_variables();
read_backend_variables(getpid(),NULL); read_backend_variables(backendID,NULL);
SSDataBaseInit(xlogop); SSDataBaseInit(xlogop);
#endif #endif
...@@ -431,11 +439,11 @@ BootstrapMain(int argc, char *argv[]) ...@@ -431,11 +439,11 @@ BootstrapMain(int argc, char *argv[])
switch (xlogop) switch (xlogop)
{ {
case BS_XLOG_BGWRITER: case BS_XLOG_BGWRITER:
InitDummyProcess(DUMMY_PROC_BGWRITER); InitDummyProcess(DUMMY_PROC_BGWRITER);
break; break;
default: default:
InitDummyProcess(DUMMY_PROC_DEFAULT); InitDummyProcess(DUMMY_PROC_DEFAULT);
break; break;
} }
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/main/main.c,v 1.69 2003/12/25 03:52:50 momjian Exp $ * $PostgreSQL: pgsql/src/backend/main/main.c,v 1.70 2004/01/06 23:15:22 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -203,9 +203,9 @@ main(int argc, char *argv[]) ...@@ -203,9 +203,9 @@ main(int argc, char *argv[])
/* /*
* Now dispatch to one of PostmasterMain, PostgresMain, GucInfoMain, * Now dispatch to one of PostmasterMain, PostgresMain, GucInfoMain,
* pgstat_main, pgstat_mainChild or BootstrapMain depending on the * SubPostmasterMain, pgstat_main, pgstat_mainChild or BootstrapMain
* program name (and possibly first argument) we were called with. * depending on the program name (and possibly first argument) we
* The lack of consistency here is historical. * were called with. The lack of consistency here is historical.
*/ */
len = strlen(new_argv[0]); len = strlen(new_argv[0]);
...@@ -223,6 +223,16 @@ main(int argc, char *argv[]) ...@@ -223,6 +223,16 @@ main(int argc, char *argv[])
exit(BootstrapMain(argc - 1, new_argv + 1)); exit(BootstrapMain(argc - 1, new_argv + 1));
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
/*
* If the first argument is "-forkexec", then invoke SubPostmasterMain. Note
* we remove "-forkexec" from the arguments passed on to SubPostmasterMain.
*/
if (argc > 1 && strcmp(new_argv[1], "-forkexec") == 0)
{
SubPostmasterMain(argc - 2, new_argv + 2);
exit(0);
}
/* /*
* If the first argument is "-statBuf", then invoke pgstat_main. Note * If the first argument is "-statBuf", then invoke pgstat_main. Note
* we remove "-statBuf" from the arguments passed on to pgstat_main. * we remove "-statBuf" from the arguments passed on to pgstat_main.
......
...@@ -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.50 2003/12/25 03:52:51 momjian Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.51 2004/01/06 23:15:22 momjian Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -107,7 +107,7 @@ static char pgStat_fname[MAXPGPATH]; ...@@ -107,7 +107,7 @@ static char pgStat_fname[MAXPGPATH];
* ---------- * ----------
*/ */
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
static void pgstat_exec(STATS_PROCESS_TYPE procType); static pid_t pgstat_forkexec(STATS_PROCESS_TYPE procType);
static void pgstat_parseArgs(PGSTAT_FORK_ARGS); static void pgstat_parseArgs(PGSTAT_FORK_ARGS);
#endif #endif
NON_EXEC_STATIC void pgstat_main(PGSTAT_FORK_ARGS); NON_EXEC_STATIC void pgstat_main(PGSTAT_FORK_ARGS);
...@@ -337,15 +337,16 @@ startup_failed: ...@@ -337,15 +337,16 @@ startup_failed:
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
/* ---------- /* ----------
* pgstat_exec() - * pgstat_forkexec() -
* *
* Used to format up the arglist for, and exec, statistics * Used to format up the arglist for, then fork and exec, statistics
* (buffer and collector) processes * (buffer and collector) processes
* *
*/ */
static void static pid_t
pgstat_exec(STATS_PROCESS_TYPE procType) pgstat_forkexec(STATS_PROCESS_TYPE procType)
{ {
pid_t pid;
char *av[11]; char *av[11];
int ac = 0, bufc = 0, i; int ac = 0, bufc = 0, i;
char pgstatBuf[8][MAXPGPATH]; char pgstatBuf[8][MAXPGPATH];
...@@ -387,9 +388,12 @@ pgstat_exec(STATS_PROCESS_TYPE procType) ...@@ -387,9 +388,12 @@ pgstat_exec(STATS_PROCESS_TYPE procType)
av[ac++] = NULL; av[ac++] = NULL;
Assert(ac <= lengthof(av)); Assert(ac <= lengthof(av));
if (execv(pg_pathname,av) == -1) /* Fire off execv in child */
if ((pid = fork()) == 0 && (execv(pg_pathname,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... */
Assert(false); abort();
return pid; /* Parent returns pid */
} }
...@@ -479,7 +483,11 @@ pgstat_start(void) ...@@ -479,7 +483,11 @@ pgstat_start(void)
beos_before_backend_startup(); beos_before_backend_startup();
#endif #endif
#ifdef EXEC_BACKEND
switch ((pgStatSock = (int) pgstat_forkexec(STAT_PROC_BUFFER)))
#else
switch ((pgStatPid = (int) fork())) switch ((pgStatPid = (int) fork()))
#endif
{ {
case -1: case -1:
#ifdef __BEOS__ #ifdef __BEOS__
...@@ -490,32 +498,27 @@ pgstat_start(void) ...@@ -490,32 +498,27 @@ pgstat_start(void)
(errmsg("could not fork statistics buffer: %m"))); (errmsg("could not fork statistics buffer: %m")));
return; return;
#ifndef EXEC_BACKEND
case 0: case 0:
/* in postmaster child ... */
#ifdef __BEOS__
/* Specific beos actions after backend startup */
beos_backend_startup();
#endif
/* Close the postmaster's sockets, except for pgstat link */
ClosePostmasterPorts(false);
/* Drop our connection to postmaster's shared memory, as well */
PGSharedMemoryDetach();
pgstat_main();
break; break;
#endif
default: default:
pgstat_is_running = true; pgstat_is_running = true;
return; return;
} }
/* in postmaster child ... */
#ifdef __BEOS__
/* Specific beos actions after backend startup */
beos_backend_startup();
#endif
/* Close the postmaster's sockets, except for pgstat link */
ClosePostmasterPorts(false);
/* Drop our connection to postmaster's shared memory, as well */
PGSharedMemoryDetach();
#ifdef EXEC_BACKEND
pgstat_exec(STAT_PROC_BUFFER);
#else
pgstat_main();
#endif
} }
...@@ -1385,28 +1388,31 @@ pgstat_main(PGSTAT_FORK_ARGS) ...@@ -1385,28 +1388,31 @@ pgstat_main(PGSTAT_FORK_ARGS)
exit(1); exit(1);
} }
#ifdef EXEC_BACKEND
/* child becomes collector process */
switch (pgstat_forkexec(STAT_PROC_COLLECTOR))
#else
switch (fork()) switch (fork())
#endif
{ {
case -1: case -1:
ereport(LOG, ereport(LOG,
(errmsg("could not fork statistics collector: %m"))); (errmsg("could not fork statistics collector: %m")));
exit(1); exit(1);
#ifndef EXEC_BACKEND
case 0: case 0:
/* child becomes collector process */ /* child becomes collector process */
#ifdef EXEC_BACKEND
pgstat_exec(STAT_PROC_COLLECTOR);
#else
pgstat_mainChild(); pgstat_mainChild();
break;
#endif #endif
exit(0);
default: default:
/* parent becomes buffer process */ /* parent becomes buffer process */
closesocket(pgStatPipe[0]); closesocket(pgStatPipe[0]);
pgstat_recvbuffer(); pgstat_recvbuffer();
exit(0);
} }
exit(0);
} }
......
This diff is collapsed.
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.382 2004/01/06 17:36:31 neilc Exp $ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.383 2004/01/06 23:15:22 momjian Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -68,11 +68,6 @@ ...@@ -68,11 +68,6 @@
extern int optind; extern int optind;
extern char *optarg; extern char *optarg;
#ifdef EXEC_BACKEND
extern bool BackendInit(Port*);
extern void read_backend_variables(pid_t, Port*);
#endif
/* ---------------- /* ----------------
* global variables * global variables
* ---------------- * ----------------
...@@ -2063,7 +2058,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2063,7 +2058,7 @@ PostgresMain(int argc, char *argv[], const char *username)
* *
* If we are running under the postmaster, this is done already. * If we are running under the postmaster, this is done already.
*/ */
if (!IsUnderPostmaster || ExecBackend) if (!IsUnderPostmaster)
MemoryContextInit(); MemoryContextInit();
set_ps_display("startup"); set_ps_display("startup");
...@@ -2268,11 +2263,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2268,11 +2263,7 @@ PostgresMain(int argc, char *argv[], const char *username)
*/ */
if (secure) if (secure)
{ {
#ifdef EXEC_BACKEND
IsUnderPostmaster = true;
#else
dbname = strdup(optarg); dbname = strdup(optarg);
#endif
secure = false; /* subsequent switches are NOT secure = false; /* subsequent switches are NOT
* secure */ * secure */
...@@ -2478,25 +2469,7 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2478,25 +2469,7 @@ PostgresMain(int argc, char *argv[], const char *username)
if (IsUnderPostmaster) if (IsUnderPostmaster)
{ {
#ifdef EXEC_BACKEND #ifdef EXEC_BACKEND
Port *port =(Port*)malloc(sizeof(Port));
if (port == NULL)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("insufficient memory to allocate port")));
read_nondefault_variables(); read_nondefault_variables();
read_backend_variables(getpid(),port);
/* FIXME: [fork/exec] Ugh */
load_hba();
load_ident();
load_user();
load_group();
if (!BackendInit(port))
return -1;
dbname = port->database_name;
#endif #endif
} else } else
ProcessConfigFile(PGC_POSTMASTER); ProcessConfigFile(PGC_POSTMASTER);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,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.141 2004/01/06 17:36:31 neilc Exp $ * $PostgreSQL: pgsql/src/include/miscadmin.h,v 1.142 2004/01/06 23:15:22 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
...@@ -115,6 +115,9 @@ extern bool ClientAuthInProgress; ...@@ -115,6 +115,9 @@ extern bool ClientAuthInProgress;
extern const bool ExecBackend; extern const bool ExecBackend;
extern int PostmasterMain(int argc, char *argv[]); extern int PostmasterMain(int argc, char *argv[]);
#ifdef EXEC_BACKEND
extern void SubPostmasterMain(int argc, char* argv[]);
#endif
extern void ClosePostmasterPorts(bool pgstat_too); extern void ClosePostmasterPorts(bool pgstat_too);
/* /*
......
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