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

Commit 8bed238

Browse files
committed
Try to make silent_mode behave somewhat reasonably.
Instead of sending stdout/stderr to /dev/null after forking away from the terminal, send them to postmaster.log within the data directory. Since this opens the door to indefinite logfile bloat, recommend even more strongly that log output be redirected when using silent_mode. Move the postmaster's initial calls of load_hba() and load_ident() down to after we have started the log collector, if we are going to. This is so that errors reported by them will appear in the "usual" place. Reclassify silent_mode as a LOGGING_WHERE, not LOGGING_WHEN, parameter, since it's got absolutely nothing to do with the latter category. In passing, fix some obsolete references to -S ... this option hasn't had that switch letter for a long time. Back-patch to 8.4, since as of 8.4 load_hba() and load_ident() are more picky (and thus more likely to fail) than they used to be. This entire change was driven by a complaint about those errors disappearing into the bit bucket.
1 parent 5a4f763 commit 8bed238

File tree

4 files changed

+111
-56
lines changed

4 files changed

+111
-56
lines changed

doc/src/sgml/config.sgml

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.223 2009/08/04 16:08:35 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.224 2009/08/24 20:08:31 tgl Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -2588,7 +2588,37 @@ local0.* /var/log/postgresql
25882588
</para>
25892589
</listitem>
25902590
</varlistentry>
2591-
2591+
2592+
<varlistentry id="guc-silent-mode" xreflabel="silent_mode">
2593+
<term><varname>silent_mode</varname> (<type>boolean</type>)</term>
2594+
<indexterm>
2595+
<primary><varname>silent_mode</> configuration parameter</primary>
2596+
</indexterm>
2597+
<listitem>
2598+
<para>
2599+
Runs the server silently. If this parameter is set, the server
2600+
will automatically run in background and disassociate from the
2601+
controlling terminal.
2602+
This parameter can only be set at server start.
2603+
</para>
2604+
2605+
<caution>
2606+
<para>
2607+
When this parameter is set,
2608+
the server's standard output and standard error are redirected
2609+
to the file <filename>postmaster.log</> within the data directory.
2610+
There is no provision for rotating this file, so it will grow
2611+
indefinitely unless server log output is redirected elsewhere
2612+
by other settings. It is recommended that <varname>log_destination</>
2613+
be set to <literal>syslog</> or that <varname>logging_collector</> be
2614+
enabled when using this option. Even with those measures, errors
2615+
reported early during startup may appear in
2616+
<filename>postmaster.log</> rather than the normal log destination.
2617+
</para>
2618+
</caution>
2619+
</listitem>
2620+
</varlistentry>
2621+
25922622
</variablelist>
25932623
</sect2>
25942624
<sect2 id="runtime-config-logging-when">
@@ -2722,26 +2752,6 @@ local0.* /var/log/postgresql
27222752
</listitem>
27232753
</varlistentry>
27242754

2725-
<varlistentry id="guc-silent-mode" xreflabel="silent_mode">
2726-
<term><varname>silent_mode</varname> (<type>boolean</type>)</term>
2727-
<indexterm>
2728-
<primary><varname>silent_mode</> configuration parameter</primary>
2729-
</indexterm>
2730-
<listitem>
2731-
<para>
2732-
Runs the server silently. If this parameter is set, the server
2733-
will automatically run in background and any controlling
2734-
terminals are disassociated.
2735-
The server's standard output and standard error are redirected
2736-
to <literal>/dev/null</>, so any messages sent to them will be lost.
2737-
Unless <application>syslog</> logging is selected or
2738-
<varname>logging_collector</> is enabled, using this parameter
2739-
is discouraged because it makes it impossible to see error messages.
2740-
This parameter can only be set at server start.
2741-
</para>
2742-
</listitem>
2743-
</varlistentry>
2744-
27452755
</variablelist>
27462756

27472757
<para>

src/backend/postmaster/postmaster.c

+72-29
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.589 2009/08/24 18:09:37 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.590 2009/08/24 20:08:32 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -191,7 +191,7 @@ static int SendStop = false;
191191

192192
/* still more option variables */
193193
bool EnableSSL = false;
194-
bool SilentMode = false; /* silent mode (-S) */
194+
bool SilentMode = false; /* silent_mode */
195195

196196
int PreAuthDelay = 0;
197197
int AuthenticationTimeout = 60;
@@ -744,7 +744,7 @@ PostmasterMain(int argc, char *argv[])
744744
}
745745

746746
/*
747-
* Fork away from controlling terminal, if -S specified.
747+
* Fork away from controlling terminal, if silent_mode specified.
748748
*
749749
* Must do this before we grab any interlock files, else the interlocks
750750
* will show the wrong PID.
@@ -894,20 +894,6 @@ PostmasterMain(int argc, char *argv[])
894894
*/
895895
set_max_safe_fds();
896896

897-
/*
898-
* Load configuration files for client authentication.
899-
*/
900-
if (!load_hba())
901-
{
902-
/*
903-
* It makes no sense continue if we fail to load the HBA file, since
904-
* there is no way to connect to the database in this case.
905-
*/
906-
ereport(FATAL,
907-
(errmsg("could not load pg_hba.conf")));
908-
}
909-
load_ident();
910-
911897
/*
912898
* Initialize the list of active backends.
913899
*/
@@ -1023,6 +1009,20 @@ PostmasterMain(int argc, char *argv[])
10231009
*/
10241010
autovac_init();
10251011

1012+
/*
1013+
* Load configuration files for client authentication.
1014+
*/
1015+
if (!load_hba())
1016+
{
1017+
/*
1018+
* It makes no sense to continue if we fail to load the HBA file,
1019+
* since there is no way to connect to the database in this case.
1020+
*/
1021+
ereport(FATAL,
1022+
(errmsg("could not load pg_hba.conf")));
1023+
}
1024+
load_ident();
1025+
10261026
/*
10271027
* Remember postmaster startup time
10281028
*/
@@ -1204,15 +1204,46 @@ reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
12041204

12051205

12061206
/*
1207-
* Fork away from the controlling terminal (-S option)
1207+
* Fork away from the controlling terminal (silent_mode option)
1208+
*
1209+
* Since this requires disconnecting from stdin/stdout/stderr (in case they're
1210+
* linked to the terminal), we re-point stdin to /dev/null and stdout/stderr
1211+
* to "postmaster.log" in the data directory, where we're already chdir'd.
12081212
*/
12091213
static void
12101214
pmdaemonize(void)
12111215
{
12121216
#ifndef WIN32
1213-
int i;
1217+
const char *pmlogname = "postmaster.log";
1218+
int dvnull;
1219+
int pmlog;
12141220
pid_t pid;
1221+
int res;
1222+
1223+
/*
1224+
* Make sure we can open the files we're going to redirect to. If this
1225+
* fails, we want to complain before disconnecting. Mention the full path
1226+
* of the logfile in the error message, even though we address it by
1227+
* relative path.
1228+
*/
1229+
dvnull = open(DEVNULL, O_RDONLY, 0);
1230+
if (dvnull < 0)
1231+
{
1232+
write_stderr("%s: could not open file \"%s\": %s\n",
1233+
progname, DEVNULL, strerror(errno));
1234+
ExitPostmaster(1);
1235+
}
1236+
pmlog = open(pmlogname, O_CREAT | O_WRONLY | O_APPEND, 0600);
1237+
if (pmlog < 0)
1238+
{
1239+
write_stderr("%s: could not open log file \"%s/%s\": %s\n",
1240+
progname, DataDir, pmlogname, strerror(errno));
1241+
ExitPostmaster(1);
1242+
}
12151243

1244+
/*
1245+
* Okay to fork.
1246+
*/
12161247
pid = fork_process();
12171248
if (pid == (pid_t) -1)
12181249
{
@@ -1231,8 +1262,8 @@ pmdaemonize(void)
12311262
MyStartTime = time(NULL);
12321263

12331264
/*
1234-
* GH: If there's no setsid(), we hopefully don't need silent mode. Until
1235-
* there's a better solution.
1265+
* Some systems use setsid() to dissociate from the TTY's process group,
1266+
* while on others it depends on stdin/stdout/stderr. Do both if possible.
12361267
*/
12371268
#ifdef HAVE_SETSID
12381269
if (setsid() < 0)
@@ -1242,14 +1273,26 @@ pmdaemonize(void)
12421273
ExitPostmaster(1);
12431274
}
12441275
#endif
1245-
i = open(DEVNULL, O_RDWR, 0);
1246-
dup2(i, 0);
1247-
dup2(i, 1);
1248-
dup2(i, 2);
1249-
close(i);
1276+
1277+
/*
1278+
* Reassociate stdin/stdout/stderr. fork_process() cleared any pending
1279+
* output, so this should be safe. The only plausible error is EINTR,
1280+
* which just means we should retry.
1281+
*/
1282+
do {
1283+
res = dup2(dvnull, 0);
1284+
} while (res < 0 && errno == EINTR);
1285+
close(dvnull);
1286+
do {
1287+
res = dup2(pmlog, 1);
1288+
} while (res < 0 && errno == EINTR);
1289+
do {
1290+
res = dup2(pmlog, 2);
1291+
} while (res < 0 && errno == EINTR);
1292+
close(pmlog);
12501293
#else /* WIN32 */
12511294
/* not supported */
1252-
elog(FATAL, "SilentMode not supported under WIN32");
1295+
elog(FATAL, "silent_mode is not supported under Windows");
12531296
#endif /* WIN32 */
12541297
}
12551298

@@ -3241,8 +3284,8 @@ BackendInitialize(Port *port)
32413284
if (!load_hba())
32423285
{
32433286
/*
3244-
* It makes no sense continue if we fail to load the HBA file, since
3245-
* there is no way to connect to the database in this case.
3287+
* It makes no sense to continue if we fail to load the HBA file,
3288+
* since there is no way to connect to the database in this case.
32463289
*/
32473290
ereport(FATAL,
32483291
(errmsg("could not load pg_hba.conf")));

src/backend/utils/misc/guc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.510 2009/08/04 16:08:36 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.511 2009/08/24 20:08:32 tgl Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -734,7 +734,7 @@ static struct config_bool ConfigureNamesBool[] =
734734
true, NULL, NULL
735735
},
736736
{
737-
{"silent_mode", PGC_POSTMASTER, LOGGING_WHEN,
737+
{"silent_mode", PGC_POSTMASTER, LOGGING_WHERE,
738738
gettext_noop("Runs the server silently."),
739739
gettext_noop("If this parameter is set, the server will automatically run in the "
740740
"background and any controlling terminals are dissociated.")

src/backend/utils/misc/postgresql.conf.sample

+5-3
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@
265265
#syslog_facility = 'LOCAL0'
266266
#syslog_ident = 'postgres'
267267

268+
#silent_mode = off # Run server silently.
269+
# DO NOT USE without syslog or
270+
# logging_collector
271+
# (change requires restart)
272+
268273

269274
# - When to Log -
270275

@@ -314,9 +319,6 @@
314319
# statements running at least this number
315320
# of milliseconds
316321

317-
#silent_mode = off # DO NOT USE without syslog or
318-
# logging_collector
319-
# (change requires restart)
320322

321323
# - What to Log -
322324

0 commit comments

Comments
 (0)