Convert some fprintf's to elog's.

This commit is contained in:
Peter Eisentraut 2001-08-30 19:02:42 +00:00
parent 11193c8a20
commit 68e5360018
1 changed files with 115 additions and 80 deletions

View File

@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.236 2001/08/17 02:59:19 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.237 2001/08/30 19:02:42 petere Exp $
* *
* NOTES * NOTES
* *
@ -152,8 +152,6 @@ static char *progname = (char *) NULL;
static char **real_argv; static char **real_argv;
static int real_argc; static int real_argc;
static time_t tnow;
/* flag to indicate that SIGHUP arrived during server loop */ /* flag to indicate that SIGHUP arrived during server loop */
static volatile bool got_SIGHUP = false; static volatile bool got_SIGHUP = false;
@ -241,7 +239,8 @@ static int BackendStartup(Port *port);
static int ProcessStartupPacket(Port *port, bool SSLdone); static int ProcessStartupPacket(Port *port, bool SSLdone);
static void processCancelRequest(Port *port, void *pkt); static void processCancelRequest(Port *port, void *pkt);
static int initMasks(fd_set *rmask, fd_set *wmask); static int initMasks(fd_set *rmask, fd_set *wmask);
static char *canAcceptConnections(void); enum CAC_state { CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY, CAC_TOOMANY };
static enum CAC_state canAcceptConnections(void);
static long PostmasterRandom(void); 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);
@ -860,7 +859,7 @@ ServerLoop(void)
PG_SETMASK(&BlockSig); PG_SETMASK(&BlockSig);
if (errno == EINTR || errno == EWOULDBLOCK) if (errno == EINTR || errno == EWOULDBLOCK)
continue; continue;
postmaster_error("ServerLoop: select failed: %s", strerror(errno)); elog(DEBUG, "ServerLoop: select failed: %s", strerror(errno));
return STATUS_ERROR; return STATUS_ERROR;
} }
@ -977,12 +976,15 @@ initMasks(fd_set *rmask, fd_set *wmask)
* *
* Returns STATUS_OK or STATUS_ERROR, or might call elog(FATAL) and * Returns STATUS_OK or STATUS_ERROR, or might call elog(FATAL) and
* not return at all. * not return at all.
*
* (Note that elog(FATAL) stuff is sent to the client, so only use it
* if that's what you want.)
*/ */
static int static int
ProcessStartupPacket(Port *port, bool SSLdone) ProcessStartupPacket(Port *port, bool SSLdone)
{ {
StartupPacket *packet; StartupPacket *packet;
char *rejectMsg; enum CAC_state cac;
int32 len; int32 len;
void *buf; void *buf;
@ -1025,8 +1027,8 @@ ProcessStartupPacket(Port *port, bool SSLdone)
#endif #endif
if (send(port->sock, &SSLok, 1, 0) != 1) if (send(port->sock, &SSLok, 1, 0) != 1)
{ {
postmaster_error("failed to send SSL negotiation response: %s", elog(DEBUG, "failed to send SSL negotiation response: %s",
strerror(errno)); strerror(errno));
return STATUS_ERROR; /* close the connection */ return STATUS_ERROR; /* close the connection */
} }
@ -1037,8 +1039,8 @@ ProcessStartupPacket(Port *port, bool SSLdone)
!SSL_set_fd(port->ssl, port->sock) || !SSL_set_fd(port->ssl, port->sock) ||
SSL_accept(port->ssl) <= 0) SSL_accept(port->ssl) <= 0)
{ {
postmaster_error("failed to initialize SSL connection: %s, errno: %d (%s)", elog(DEBUG, "failed to initialize SSL connection: %s (%s)",
ERR_reason_error_string(ERR_get_error()), errno, strerror(errno)); ERR_reason_error_string(ERR_get_error()), strerror(errno));
return STATUS_ERROR; return STATUS_ERROR;
} }
} }
@ -1092,10 +1094,26 @@ ProcessStartupPacket(Port *port, bool SSLdone)
* so now instead of wasting cycles on an authentication exchange. * so now instead of wasting cycles on an authentication exchange.
* (This also allows a pg_ping utility to be written.) * (This also allows a pg_ping utility to be written.)
*/ */
rejectMsg = canAcceptConnections(); cac = canAcceptConnections();
if (rejectMsg != NULL) switch (cac)
elog(FATAL, "%s", rejectMsg); {
case CAC_STARTUP:
elog(FATAL, "The database system is starting up");
break;
case CAC_SHUTDOWN:
elog(FATAL, "The database system is shutting down");
break;
case CAC_RECOVERY:
elog(FATAL, "The database system is in recovery mode");
break;
case CAC_TOOMANY:
elog(FATAL, "Sorry, too many clients already");
break;
case CAC_OK:
default:
;
}
return STATUS_OK; return STATUS_OK;
} }
@ -1121,7 +1139,7 @@ processCancelRequest(Port *port, void *pkt)
if (backendPID == CheckPointPID) if (backendPID == CheckPointPID)
{ {
if (DebugLvl) if (DebugLvl)
postmaster_error("processCancelRequest: CheckPointPID in cancel request for process %d", backendPID); elog(DEBUG, "processCancelRequest: CheckPointPID in cancel request for process %d", backendPID);
return; return;
} }
@ -1158,20 +1176,17 @@ processCancelRequest(Port *port, void *pkt)
/* /*
* canAcceptConnections --- check to see if database state allows connections. * canAcceptConnections --- check to see if database state allows connections.
*
* If we are open for business, return NULL, otherwise return an error message
* string suitable for rejecting a connection request.
*/ */
static char * static enum CAC_state
canAcceptConnections(void) canAcceptConnections(void)
{ {
/* Can't start backends when in startup/shutdown/recovery state. */ /* Can't start backends when in startup/shutdown/recovery state. */
if (Shutdown > NoShutdown) if (Shutdown > NoShutdown)
return "The Data Base System is shutting down"; return CAC_SHUTDOWN;
if (StartupPID) if (StartupPID)
return "The Data Base System is starting up"; return CAC_STARTUP;
if (FatalError) if (FatalError)
return "The Data Base System is in recovery mode"; return CAC_RECOVERY;
/* /*
* Don't start too many children. * Don't start too many children.
* *
@ -1182,9 +1197,9 @@ canAcceptConnections(void)
* to join the shared-inval backend array. * to join the shared-inval backend array.
*/ */
if (CountChildren() >= 2 * MaxBackends) if (CountChildren() >= 2 * MaxBackends)
return "Sorry, too many clients already"; return CAC_TOOMANY;
return NULL; return CAC_OK;
} }
@ -1198,7 +1213,7 @@ ConnCreate(int serverFd)
if (!(port = (Port *) calloc(1, sizeof(Port)))) if (!(port = (Port *) calloc(1, sizeof(Port))))
{ {
postmaster_error("ConnCreate: malloc failed"); elog(DEBUG, "ConnCreate: malloc failed");
SignalChildren(SIGQUIT); SignalChildren(SIGQUIT);
ExitPostmaster(1); ExitPostmaster(1);
} }
@ -1343,9 +1358,7 @@ pmdie(SIGNAL_ARGS)
return; return;
} }
Shutdown = SmartShutdown; Shutdown = SmartShutdown;
tnow = time(NULL); elog(DEBUG, "smart shutdown request");
fprintf(stderr, gettext("Smart Shutdown request at %s"), ctime(&tnow));
fflush(stderr);
if (DLGetHead(BackendList)) /* let reaper() handle this */ if (DLGetHead(BackendList)) /* let reaper() handle this */
{ {
errno = save_errno; errno = save_errno;
@ -1363,8 +1376,8 @@ pmdie(SIGNAL_ARGS)
} }
if (ShutdownPID > 0) if (ShutdownPID > 0)
{ {
postmaster_error("Shutdown process %d already running", elog(REALLYFATAL, "shutdown process %d already running",
ShutdownPID); ShutdownPID);
abort(); abort();
} }
@ -1385,16 +1398,13 @@ pmdie(SIGNAL_ARGS)
errno = save_errno; errno = save_errno;
return; return;
} }
tnow = time(NULL); elog(DEBUG, "fast shutdown request");
fprintf(stderr, gettext("Fast Shutdown request at %s"), ctime(&tnow));
fflush(stderr);
if (DLGetHead(BackendList)) /* let reaper() handle this */ if (DLGetHead(BackendList)) /* let reaper() handle this */
{ {
Shutdown = FastShutdown; Shutdown = FastShutdown;
if (!FatalError) if (!FatalError)
{ {
fprintf(stderr, gettext("Aborting any active transaction...\n")); elog(DEBUG, "aborting any active transactions");
fflush(stderr);
SignalChildren(SIGTERM); SignalChildren(SIGTERM);
} }
errno = save_errno; errno = save_errno;
@ -1419,8 +1429,8 @@ pmdie(SIGNAL_ARGS)
} }
if (ShutdownPID > 0) if (ShutdownPID > 0)
{ {
postmaster_error("Shutdown process %d already running", elog(REALLYFATAL, "shutdown process %d already running",
ShutdownPID); ShutdownPID);
abort(); abort();
} }
@ -1436,9 +1446,7 @@ pmdie(SIGNAL_ARGS)
* abort all children with SIGQUIT and exit without attempt to * abort all children with SIGQUIT and exit without attempt to
* properly shutdown data base system. * properly shutdown data base system.
*/ */
tnow = time(NULL); elog(DEBUG, "immediate shutdown request");
fprintf(stderr, gettext("Immediate Shutdown request at %s"), ctime(&tnow));
fflush(stderr);
if (ShutdownPID > 0) if (ShutdownPID > 0)
kill(ShutdownPID, SIGQUIT); kill(ShutdownPID, SIGQUIT);
if (StartupPID > 0) if (StartupPID > 0)
@ -1472,7 +1480,7 @@ reaper(SIGNAL_ARGS)
pqsignal(SIGCHLD, reaper); pqsignal(SIGCHLD, reaper);
if (DebugLvl) if (DebugLvl)
postmaster_error("reaping dead processes"); elog(DEBUG, "reaping dead processes");
#ifdef HAVE_WAITPID #ifdef HAVE_WAITPID
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
{ {
@ -1488,37 +1496,56 @@ reaper(SIGNAL_ARGS)
*/ */
if (pgstat_ispgstat(pid)) if (pgstat_ispgstat(pid))
{ {
fprintf(stderr, "%s: Performance collector exited with status %d\n", if (WIFEXITED(exitstatus))
progname, exitstatus); elog(DEBUG, "statistics collector exited with status %d",
WEXITSTATUS(exitstatus));
else if (WIFSIGNALED(exitstatus))
elog(DEBUG, "statistics collector was terminated by signal %d",
WTERMSIG(exitstatus));
pgstat_start(real_argc, real_argv); pgstat_start(real_argc, real_argv);
continue; continue;
} }
if (ShutdownPID > 0 && pid == ShutdownPID) if (ShutdownPID > 0 && pid == ShutdownPID)
{ {
if (exitstatus != 0) if (WIFEXITED(exitstatus) && WEXITSTATUS(exitstatus) != 0)
{ {
postmaster_error("Shutdown proc %d exited with status %d", pid, exitstatus); elog(DEBUG, "shutdown process %d exited with status %d",
pid, WEXITSTATUS(exitstatus));
ExitPostmaster(1);
}
if (WIFSIGNALED(exitstatus))
{
elog(DEBUG, "shutdown process %d was terminated by signal %d",
pid, WTERMSIG(exitstatus));
ExitPostmaster(1); ExitPostmaster(1);
} }
ExitPostmaster(0); ExitPostmaster(0);
} }
if (StartupPID > 0 && pid == StartupPID) if (StartupPID > 0 && pid == StartupPID)
{ {
if (exitstatus != 0) if (WIFEXITED(exitstatus) && WEXITSTATUS(exitstatus) != 0)
{ {
postmaster_error("Startup proc %d exited with status %d - abort", elog(DEBUG, "startup process %d exited with status %d; aborting startup",
pid, exitstatus); pid, WEXITSTATUS(exitstatus));
ExitPostmaster(1); ExitPostmaster(1);
} }
if (WIFSIGNALED(exitstatus))
{
elog(DEBUG, "shutdown process %d was terminated by signal %d; aborting startup",
pid, WTERMSIG(exitstatus));
ExitPostmaster(1);
}
StartupPID = 0; StartupPID = 0;
FatalError = false; /* done with recovery */ FatalError = false; /* done with recovery */
if (Shutdown > NoShutdown) if (Shutdown > NoShutdown)
{ {
if (ShutdownPID > 0) if (ShutdownPID > 0)
{ {
postmaster_error("Shutdown process %d already running", elog(STOP, "startup process %d died while shutdown process %d already running",
ShutdownPID); pid, ShutdownPID);
abort(); abort();
} }
ShutdownPID = ShutdownDataBase(); ShutdownPID = ShutdownDataBase();
@ -1553,11 +1580,7 @@ reaper(SIGNAL_ARGS)
errno = save_errno; errno = save_errno;
return; return;
} }
tnow = time(NULL); elog(DEBUG, "all server processes terminated; reinitializing shared memory and semaphores");
fprintf(stderr, gettext("Server processes were terminated at %s"
"Reinitializing shared memory and semaphores\n"),
ctime(&tnow));
fflush(stderr);
shmem_exit(0); shmem_exit(0);
reset_shared(PostPortNumber); reset_shared(PostPortNumber);
@ -1601,8 +1624,8 @@ CleanupProc(int pid,
Backend *bp; Backend *bp;
if (DebugLvl) if (DebugLvl)
postmaster_error("CleanupProc: pid %d exited with status %d", elog(DEBUG, "CleanupProc: pid %d exited with status %d",
pid, exitstatus); pid, exitstatus);
/* /*
* If a backend dies in an ugly way (i.e. exit status not 0) then we * If a backend dies in an ugly way (i.e. exit status not 0) then we
@ -1642,14 +1665,18 @@ CleanupProc(int pid,
return; return;
} }
/* below here we're dealing with a non-normal exit */
/* Make log entry unless we did so already */
if (!FatalError) if (!FatalError)
{ {
/* Make log entry unless we did so already */ if (WIFEXITED(exitstatus))
tnow = time(NULL); elog(DEBUG, "server process (pid %d) exited with status %d",
fprintf(stderr, gettext("Server process (pid %d) exited with status %d at %s" pid, WEXITSTATUS(exitstatus));
"Terminating any active server processes...\n"), else if (WIFSIGNALED(exitstatus))
pid, exitstatus, ctime(&tnow)); elog(DEBUG, "server process (pid %d) was terminated by signal %d",
fflush(stderr); pid, WTERMSIG(exitstatus));
elog(DEBUG, "terminating any other active server processes");
} }
curr = DLGetHead(BackendList); curr = DLGetHead(BackendList);
@ -1671,9 +1698,9 @@ CleanupProc(int pid,
if (!FatalError) if (!FatalError)
{ {
if (DebugLvl) if (DebugLvl)
postmaster_error("CleanupProc: sending %s to process %d", elog(DEBUG, "CleanupProc: sending %s to process %d",
(SendStop ? "SIGSTOP" : "SIGQUIT"), (SendStop ? "SIGSTOP" : "SIGQUIT"),
bp->pid); bp->pid);
kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT)); kill(bp->pid, (SendStop ? SIGSTOP : SIGQUIT));
} }
} }
@ -1776,8 +1803,7 @@ BackendStartup(Port *port)
bn = (Backend *) malloc(sizeof(Backend)); bn = (Backend *) malloc(sizeof(Backend));
if (!bn) if (!bn)
{ {
fprintf(stderr, gettext("%s: BackendStartup: malloc failed\n"), elog(DEBUG, "out of memory; connection startup aborted");
progname);
return STATUS_ERROR; return STATUS_ERROR;
} }
@ -1796,8 +1822,7 @@ BackendStartup(Port *port)
status = DoBackend(port); status = DoBackend(port);
if (status != 0) if (status != 0)
{ {
fprintf(stderr, gettext("%s child[%d]: BackendStartup: backend startup failed\n"), elog(DEBUG, "connection startup failed");
progname, (int) getpid());
proc_exit(status); proc_exit(status);
} }
else else
@ -1812,16 +1837,15 @@ BackendStartup(Port *port)
/* Specific beos backend startup actions */ /* Specific beos backend startup actions */
beos_backend_startup_failed(); beos_backend_startup_failed();
#endif #endif
fprintf(stderr, gettext("%s: BackendStartup: fork failed: %s\n"), elog(DEBUG, "connection startup failed (fork failure): %s",
progname, strerror(errno)); strerror(errno));
return STATUS_ERROR; return STATUS_ERROR;
} }
/* in parent, normal */ /* in parent, normal */
if (DebugLvl >= 1) if (DebugLvl >= 1)
fprintf(stderr, gettext("%s: BackendStartup: pid=%d user=%s db=%s socket=%d\n"), elog(DEBUG, "BackendStartup: pid=%d user=%s db=%s socket=%d\n",
progname, pid, port->user, port->database, pid, port->user, port->database, port->sock);
port->sock);
/* /*
* Everything's been successful, it's safe to add this backend to our * Everything's been successful, it's safe to add this backend to our
@ -2267,11 +2291,22 @@ SSDataBase(int xlop)
beos_backend_startup_failed(); beos_backend_startup_failed();
#endif #endif
fprintf(stderr, "%s Data Base: fork failed: %s\n", switch(xlop)
((xlop == BS_XLOG_STARTUP) ? "Startup" : {
((xlop == BS_XLOG_CHECKPOINT) ? "CheckPoint" : case BS_XLOG_STARTUP:
"Shutdown")), elog(DEBUG, "could not launch startup process (fork failure): %s",
strerror(errno)); strerror(errno));
break;
case BS_XLOG_CHECKPOINT:
elog(DEBUG, "could not launch checkpoint process (fork failure): %s",
strerror(errno));
break;
case BS_XLOG_SHUTDOWN:
default:
elog(DEBUG, "could not launch shutdown process (fork failure): %s",
strerror(errno));
break;
}
/* /*
* fork failure is fatal during startup/shutdown, but there's no * fork failure is fatal during startup/shutdown, but there's no
@ -2291,7 +2326,7 @@ SSDataBase(int xlop)
{ {
if (!(bn = (Backend *) calloc(1, sizeof(Backend)))) if (!(bn = (Backend *) calloc(1, sizeof(Backend))))
{ {
postmaster_error("CheckPointDataBase: malloc failed"); elog(DEBUG, "CheckPointDataBase: malloc failed");
ExitPostmaster(1); ExitPostmaster(1);
} }