Skip to content

Commit 3562556

Browse files
committed
Attempt to open log file immediately when starting
See #243
1 parent 2d9f907 commit 3562556

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Diff for: doc/manpages/dinit.8.m4

+5-3
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ socket is written on the file descriptor.
6767
\fB\-l\fR \fIpath\fP, \fB\-\-log\-file\fR \fIpath\fP
6868
Species \fIpath\fP as the path to the log file, to which Dinit will log status
6969
and error messages.
70-
Note that when running as the system service manager, Dinit
71-
does not begin logging until the log service has started.
7270
Using this option inhibits logging via the syslog facility, however, all logging messages are
73-
duplicated as usual to the console (so long as no service owns the console).
71+
duplicated as usual to the console (as long as \fB\-\-quiet\fR has not also been specified).
72+
Note that when running as the system init, Dinit will continue if it cannot open the specified
73+
file, and will attempt to open it again once the root file system is writable.
74+
If not running as the system init and the file cannot be opened, Dinit will immediately exit
75+
with an error.
7476
.TP
7577
\fB\-s\fR, \fB\-\-system\fR
7678
Run as the system service manager.

Diff for: src/dinit.cc

+13-7
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,14 @@ int dinit_main(int argc, char **argv)
586586
log(loglevel_t::NOTICE, false, "Starting system");
587587
}
588588

589-
// Only try to set up the external log (via syslog) now if we aren't the system init. (If we
590-
// are the system init, wait until the log service starts, unless a logfile has been specified).
591-
if (!am_system_init || log_specified) setup_external_log();
589+
// If a log file was specified, open it now.
590+
if (log_specified) {
591+
setup_external_log();
592+
if (!am_system_init && !external_log_open) {
593+
flush_log(); // flush console messages
594+
return EXIT_FAILURE;
595+
}
596+
}
592597

593598
if (env_file != nullptr) {
594599
read_env_file(env_file, true, main_env, false, nullptr);
@@ -705,7 +710,7 @@ int dinit_main(int argc, char **argv)
705710
sigprocmask(SIG_UNBLOCK, &sigwait_set_int, NULL);
706711
}
707712

708-
return 0;
713+
return EXIT_SUCCESS;
709714
}
710715

711716
// Get user confirmation before proceeding with restarting boot sequence.
@@ -817,7 +822,8 @@ void rootfs_is_rw() noexcept
817822
{
818823
open_control_socket(true);
819824
control_socket_ready();
820-
if (!log_is_syslog) {
825+
if (!log_is_syslog && !external_log_open) {
826+
// Try (again) to open log file if we couldn't do so earlier.
821827
setup_external_log();
822828
}
823829
if (!did_log_boot) {
@@ -944,7 +950,7 @@ static void close_control_socket() noexcept
944950

945951
void setup_external_log() noexcept
946952
{
947-
if (! external_log_open) {
953+
if (!external_log_open) {
948954
if (log_is_syslog) {
949955
const char * saddrname = log_path;
950956
size_t saddrname_len = strlen(saddrname);
@@ -1003,7 +1009,7 @@ void setup_external_log() noexcept
10031009
else {
10041010
// log failure to log? It makes more sense than first appears, because we also log
10051011
// to console:
1006-
log(loglevel_t::ERROR, "Setting up log failed: ", strerror(errno));
1012+
log(loglevel_t::ERROR, "Opening log file failed: ", strerror(errno));
10071013
}
10081014
}
10091015
}

0 commit comments

Comments
 (0)