Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement auto-recovery #395

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/manpages/dinit.8.m4
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ Run in "container mode", i.e. do not perform system management functions (such
as shutdown/reboot).
The \fBdinit\fR daemon will simply exit rather than executing the \fB$$$SHUTDOWN_PREFIX@@@shutdown\fR program.
.TP
\fB\-r\fR, \fB\-\-auto\-recovery\fR
Automatically run the \fIrecovery\fR service on apparent boot failures (if all
services stop without a shutdown command having been issued) without prompting
the user when Dinit is running as system manager.
.TP
\fB\-q\fR, \fB\-\-quiet\fR
Run with no output to the terminal/console.
This disables service status messages and sets the log level for the console log to \fBnone\fR.
Expand Down
18 changes: 18 additions & 0 deletions src/dinit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static dirload_service_set *services;

static bool am_system_mgr = false; // true if we are PID 1
static bool am_system_init = false; // true if we are the system init process
static bool auto_recovery = false; // automatically run recovery service on boot failure

static bool did_log_boot = false;
static bool control_socket_open = false;
Expand Down Expand Up @@ -292,6 +293,9 @@ static int process_commandline_arg(char **argv, int argc, int &i, options &opts)
am_system_mgr = false;
opts.process_sys_args = false;
}
else if (strcmp(argv[i], "--auto-recovery") == 0 || strcmp(argv[i], "-r") == 0) {
auto_recovery = true;
}
else if (strcmp(argv[i], "--socket-path") == 0 || strcmp(argv[i], "-p") == 0) {
if (++i < argc && argv[i][0] != '\0') {
control_socket_path = argv[i];
Expand Down Expand Up @@ -391,6 +395,7 @@ static int process_commandline_arg(char **argv, int argc, int &i, options &opts)
" --system-mgr, -m run as system manager (perform shutdown etc)\n"
" --user, -u run as a user service manager\n"
" --container, -o run in container mode (do not manage system)\n"
" --auto-recovery, -r auto-run recovery service on system manager boot failure\n"
" --socket-path <path>, -p <path>\n"
" path to control socket\n"
" --ready-fd <fd>, -F <fd>\n"
Expand Down Expand Up @@ -695,6 +700,9 @@ int dinit_main(int argc, char **argv)
else if (shutdown_type == shutdown_type_t::POWEROFF) {
log_msg_end(" Will power down.");
}
else if (shutdown_type == shutdown_type_t::NONE) {
log_msg_end(" Will handle boot failure.");
}
}

flush_log();
Expand All @@ -718,6 +726,16 @@ int dinit_main(int argc, char **argv)
// Services all stopped but there was no shutdown issued. Inform user, wait for ack, and
// re-start boot sequence.
sync(); // Sync to minimise data loss if user elects to power off / hard reset
if (auto_recovery) {
try {
services->start_service("recovery");
}
catch (std::exception &exc) {
log(loglevel_t::ERROR, "Unable to start recovery service: ", exc.what());
// As the following prompt UI could be inaccessible flush the log again already
flush_log();
}
}
confirm_restart_boot();
if (services->count_active_services() != 0) {
// Recovery service started
Expand Down