37
37
*
38
38
*
39
39
* 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 $
41
41
*
42
42
* NOTES
43
43
*
@@ -191,7 +191,7 @@ static int SendStop = false;
191
191
192
192
/* still more option variables */
193
193
bool EnableSSL = false;
194
- bool SilentMode = false; /* silent mode (-S) */
194
+ bool SilentMode = false; /* silent_mode */
195
195
196
196
int PreAuthDelay = 0 ;
197
197
int AuthenticationTimeout = 60 ;
@@ -744,7 +744,7 @@ PostmasterMain(int argc, char *argv[])
744
744
}
745
745
746
746
/*
747
- * Fork away from controlling terminal, if -S specified.
747
+ * Fork away from controlling terminal, if silent_mode specified.
748
748
*
749
749
* Must do this before we grab any interlock files, else the interlocks
750
750
* will show the wrong PID.
@@ -894,20 +894,6 @@ PostmasterMain(int argc, char *argv[])
894
894
*/
895
895
set_max_safe_fds ();
896
896
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
-
911
897
/*
912
898
* Initialize the list of active backends.
913
899
*/
@@ -1023,6 +1009,20 @@ PostmasterMain(int argc, char *argv[])
1023
1009
*/
1024
1010
autovac_init ();
1025
1011
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
+
1026
1026
/*
1027
1027
* Remember postmaster startup time
1028
1028
*/
@@ -1204,15 +1204,46 @@ reg_reply(DNSServiceRegistrationReplyErrorType errorCode, void *context)
1204
1204
1205
1205
1206
1206
/*
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.
1208
1212
*/
1209
1213
static void
1210
1214
pmdaemonize (void )
1211
1215
{
1212
1216
#ifndef WIN32
1213
- int i ;
1217
+ const char * pmlogname = "postmaster.log" ;
1218
+ int dvnull ;
1219
+ int pmlog ;
1214
1220
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
+ }
1215
1243
1244
+ /*
1245
+ * Okay to fork.
1246
+ */
1216
1247
pid = fork_process ();
1217
1248
if (pid == (pid_t ) - 1 )
1218
1249
{
@@ -1231,8 +1262,8 @@ pmdaemonize(void)
1231
1262
MyStartTime = time (NULL );
1232
1263
1233
1264
/*
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 .
1236
1267
*/
1237
1268
#ifdef HAVE_SETSID
1238
1269
if (setsid () < 0 )
@@ -1242,14 +1273,26 @@ pmdaemonize(void)
1242
1273
ExitPostmaster (1 );
1243
1274
}
1244
1275
#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 );
1250
1293
#else /* WIN32 */
1251
1294
/* not supported */
1252
- elog (FATAL , "SilentMode not supported under WIN32 " );
1295
+ elog (FATAL , "silent_mode is not supported under Windows " );
1253
1296
#endif /* WIN32 */
1254
1297
}
1255
1298
@@ -3241,8 +3284,8 @@ BackendInitialize(Port *port)
3241
3284
if (!load_hba ())
3242
3285
{
3243
3286
/*
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.
3246
3289
*/
3247
3290
ereport (FATAL ,
3248
3291
(errmsg ("could not load pg_hba.conf" )));
0 commit comments