Skip to content

Commit

Permalink
Attempt to open log file immediately when starting
Browse files Browse the repository at this point in the history
See #243
  • Loading branch information
davmac314 committed Sep 20, 2023
1 parent 2d9f907 commit 3562556
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
8 changes: 5 additions & 3 deletions doc/manpages/dinit.8.m4
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ socket is written on the file descriptor.
\fB\-l\fR \fIpath\fP, \fB\-\-log\-file\fR \fIpath\fP
Species \fIpath\fP as the path to the log file, to which Dinit will log status
and error messages.
Note that when running as the system service manager, Dinit
does not begin logging until the log service has started.
Using this option inhibits logging via the syslog facility, however, all logging messages are
duplicated as usual to the console (so long as no service owns the console).
duplicated as usual to the console (as long as \fB\-\-quiet\fR has not also been specified).
Note that when running as the system init, Dinit will continue if it cannot open the specified
file, and will attempt to open it again once the root file system is writable.
If not running as the system init and the file cannot be opened, Dinit will immediately exit
with an error.
.TP
\fB\-s\fR, \fB\-\-system\fR
Run as the system service manager.
Expand Down
20 changes: 13 additions & 7 deletions src/dinit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,14 @@ int dinit_main(int argc, char **argv)
log(loglevel_t::NOTICE, false, "Starting system");
}

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

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

return 0;
return EXIT_SUCCESS;
}

// Get user confirmation before proceeding with restarting boot sequence.
Expand Down Expand Up @@ -817,7 +822,8 @@ void rootfs_is_rw() noexcept
{
open_control_socket(true);
control_socket_ready();
if (!log_is_syslog) {
if (!log_is_syslog && !external_log_open) {
// Try (again) to open log file if we couldn't do so earlier.
setup_external_log();
}
if (!did_log_boot) {
Expand Down Expand Up @@ -944,7 +950,7 @@ static void close_control_socket() noexcept

void setup_external_log() noexcept
{
if (! external_log_open) {
if (!external_log_open) {
if (log_is_syslog) {
const char * saddrname = log_path;
size_t saddrname_len = strlen(saddrname);
Expand Down Expand Up @@ -1003,7 +1009,7 @@ void setup_external_log() noexcept
else {
// log failure to log? It makes more sense than first appears, because we also log
// to console:
log(loglevel_t::ERROR, "Setting up log failed: ", strerror(errno));
log(loglevel_t::ERROR, "Opening log file failed: ", strerror(errno));
}
}
}
Expand Down

0 comments on commit 3562556

Please sign in to comment.