mirror of
https://github.com/troglobit/finit.git
synced 2026-10-12 10:12:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
197c5767ea | ||
|
|
22072e477c | ||
|
|
bda2f94e8a | ||
|
|
abb4561516 | ||
|
|
309cc7f741 |
@@ -39,3 +39,25 @@ Log rotation is controlled using the global `log` setting.
|
||||
**Example:**
|
||||
|
||||
service log:prio:user.warn,tag:ntpd /sbin/ntpd pool.ntp.org -- NTP daemon
|
||||
|
||||
Output Buffering
|
||||
----------------
|
||||
|
||||
When using the `log` directive, Finit redirects the service's stdout and
|
||||
stderr to a pipe connected to a logger process. Programs detect this as
|
||||
non-interactive output (i.e., `isatty()` returns false) and typically
|
||||
switch from line-buffered to fully-buffered mode.
|
||||
|
||||
Most well-behaved daemons explicitly flush their output or use syslog
|
||||
directly, so this is rarely an issue. However, if a service's log
|
||||
messages appear delayed or batched, you can force line-buffered output
|
||||
by wrapping the command with `stdbuf`:
|
||||
|
||||
service log /usr/bin/stdbuf -oL /path/to/command -- My service
|
||||
|
||||
The `-oL` option forces line-buffered output, and `-o0` forces unbuffered
|
||||
output. See `stdbuf(1)` for details.
|
||||
|
||||
> [!NOTE]
|
||||
> Using `stdbuf` is rarely necessary. Only use it if you observe actual
|
||||
> buffering issues with a specific service.
|
||||
|
||||
+19
-1
@@ -165,4 +165,22 @@ sync(2) has been called, twice.
|
||||
|
||||
> "On Linux, sync is only guaranteed to schedule the dirty blocks for
|
||||
> writing; it can actually take a short time before all the blocks are
|
||||
> finally written.
|
||||
> finally written.
|
||||
|
||||
**Syntax:** `reboot-watchdog <on|off|true|false|1|0>`
|
||||
|
||||
Controls whether the system should reboot via the watchdog timer (WDT)
|
||||
or directly via the SoC/kernel. When enabled, Finit will:
|
||||
|
||||
1. Send `SIGPWR` to the registered watchdog daemon before shutdown
|
||||
2. Send `SIGTERM` to the watchdog daemon and wait up to 10 seconds
|
||||
for the watchdog to trigger a hardware reset
|
||||
|
||||
When disabled (default), Finit skips the watchdog reboot logic and
|
||||
calls the kernel's `reboot(2)` syscall directly for a clean SoC reboot.
|
||||
|
||||
*Default:* off (reboot via SoC)
|
||||
|
||||
> [!NOTE]
|
||||
> This setting only affects reboots. The watchdog daemon will still
|
||||
> run and monitor the system during normal operation.
|
||||
|
||||
+14
-7
@@ -10,14 +10,21 @@ many embedded systems this is crucial to ensure all circuits on the
|
||||
board are properly reset for the next boot, in effect ensuring the
|
||||
system works the same after both a power-on and reboot event.
|
||||
|
||||
> [!NOTE]
|
||||
> The watchdog reboot delegation can be enabled with the `reboot-watchdog`
|
||||
> configuration option in `/etc/finit.conf`. By default this is disabled
|
||||
> and the system reboots directly via the SoC using the kernel's `reboot(2)`
|
||||
> syscall. See [Runlevels](config/runlevels.md) for details.
|
||||
|
||||
The delegation is performed at the very last steps of system shutdown,
|
||||
if reboot has been selected and an elected watchdog is known, first a
|
||||
`SIGPWR` is sent to advise watchdogd of the pending reboot. Then, when
|
||||
the necessary steps of preparing the system for shutdown (umount etc.)
|
||||
are completed, Finit sends `SIGTERM` to watchdogd and puts itself in a
|
||||
10 sec timeout loop waiting for the WDT to reset the board. If a reset
|
||||
is not done before the timeout, Finit falls back to`reboot(RB_AUTOBOOT)`
|
||||
which tells the kernel to do the reboot.
|
||||
if reboot has been selected, `reboot-watchdog` is enabled, and an elected
|
||||
watchdog is known. First a `SIGPWR` is sent to advise watchdogd of the
|
||||
pending reboot. Then, when the necessary steps of preparing the system
|
||||
for shutdown (umount etc.) are completed, Finit sends `SIGTERM` to
|
||||
watchdogd and puts itself in a 10 sec timeout loop waiting for the WDT
|
||||
to reset the board. If a reset is not done before the timeout, Finit
|
||||
falls back to `reboot(RB_AUTOBOOT)` which tells the kernel to do the
|
||||
reboot.
|
||||
|
||||
An external watchdog service can also be used. The more advanced cousin
|
||||
[watchdogd](https://github.com/troglobit/watchdogd/) is the recommended
|
||||
|
||||
@@ -68,7 +68,8 @@ static int stop(svc_t *svc, void *user_data)
|
||||
service_timeout_cancel(svc);
|
||||
svc_stop(svc);
|
||||
service_step(svc);
|
||||
service_step_all(SVC_TYPE_ANY);
|
||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
||||
service_step_all(SVC_TYPE_ANY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -83,7 +84,8 @@ static int start(svc_t *svc, void *user_data)
|
||||
service_timeout_cancel(svc);
|
||||
svc_start(svc);
|
||||
service_step(svc);
|
||||
service_step_all(SVC_TYPE_ANY);
|
||||
if (!IS_RESERVED_RUNLEVEL(runlevel))
|
||||
service_step_all(SVC_TYPE_ANY);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -391,11 +393,6 @@ static void api_cb(uev_t *w, void *arg, int events)
|
||||
}
|
||||
|
||||
switch (rq.cmd) {
|
||||
case INIT_CMD_RELOAD:
|
||||
case INIT_CMD_START_SVC:
|
||||
case INIT_CMD_RESTART_SVC:
|
||||
case INIT_CMD_STOP_SVC:
|
||||
case INIT_CMD_RELOAD_SVC:
|
||||
case INIT_CMD_REBOOT:
|
||||
case INIT_CMD_HALT:
|
||||
case INIT_CMD_POWEROFF:
|
||||
@@ -453,6 +450,10 @@ static void api_cb(uev_t *w, void *arg, int events)
|
||||
break;
|
||||
|
||||
case INIT_CMD_RELOAD: /* 'init q' and 'initctl reload' */
|
||||
if (IS_RESERVED_RUNLEVEL(runlevel)) {
|
||||
warnx("Ignoring reload in runlevel S and 6/0.");
|
||||
goto done;
|
||||
}
|
||||
dbg("reload");
|
||||
sm_reload();
|
||||
break;
|
||||
@@ -576,7 +577,7 @@ static void api_cb(uev_t *w, void *arg, int events)
|
||||
dbg("Unsupported cmd: %d", rq.cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
done:
|
||||
if (result)
|
||||
rq.cmd = INIT_CMD_NACK;
|
||||
else
|
||||
|
||||
@@ -60,6 +60,7 @@ int single = 0; /* single user mode from kernel cmdline */
|
||||
int bootstrap = 1; /* set while bootstrapping (for TTYs) */
|
||||
int kerndebug = 0; /* set if /proc/sys/kernel/printk > 7 */
|
||||
int syncsec = 0; /* reboot delay */
|
||||
int wdtreboot = 0; /* reboot via watchdog, default: SOC */
|
||||
int readiness = SVC_NOTIFY_PID;
|
||||
char *finit_conf= NULL;
|
||||
char *finit_rcsd= NULL;
|
||||
@@ -1034,6 +1035,11 @@ static int parse_static(char *line, int is_rcsd)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (MATCH_CMD(line, "reboot-watchdog ", x)) {
|
||||
wdtreboot = get_bool(strip_line(x), 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Periodic check and instability index leveler, seconds
|
||||
*/
|
||||
|
||||
@@ -37,6 +37,7 @@ extern int single;
|
||||
extern int bootstrap;
|
||||
extern int kerndebug;
|
||||
extern int syncsec;
|
||||
extern int wdtreboot;
|
||||
extern int readiness;
|
||||
extern char *fstab;
|
||||
extern char *sdown;
|
||||
|
||||
+10
@@ -212,6 +212,11 @@ static int fsck(int pass)
|
||||
if (mnt->mnt_passno == 0 || mnt->mnt_passno != pass)
|
||||
continue;
|
||||
|
||||
if (hasmntopt(mnt, "noauto")) {
|
||||
dbg("Skipping fsck of %s, marked noauto", mnt->mnt_fsname);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Device to maybe fsck, */
|
||||
dev = mnt->mnt_fsname;
|
||||
|
||||
@@ -425,6 +430,11 @@ static void fs_swapon(char *cmd, size_t len)
|
||||
if (strcmp(mnt->mnt_type, MNTTYPE_SWAP))
|
||||
continue;
|
||||
|
||||
if (hasmntopt(mnt, "noauto")) {
|
||||
dbg("Skipping swap %s, marked noauto", mnt->mnt_fsname);
|
||||
continue;
|
||||
}
|
||||
|
||||
snprintf(cmd, len, "swapon %s", mnt->mnt_fsname);
|
||||
run_interactive(cmd, "Enabling swap %s", mnt->mnt_fsname);
|
||||
}
|
||||
|
||||
+51
-22
@@ -271,25 +271,22 @@ static int lredirect(svc_t *svc)
|
||||
{
|
||||
static int have_sysklogd = -1;
|
||||
pid_t svc_pid = getpid();
|
||||
int pipefd[2];
|
||||
pid_t pid;
|
||||
int fd;
|
||||
|
||||
/*
|
||||
* Open PTY to connect to logger. A pty isn't buffered
|
||||
* like a pipe, and it eats newlines so they aren't logged
|
||||
* Use a pipe to connect to logger. This ensures isatty()
|
||||
* returns false for the service, preventing programs from
|
||||
* emitting ANSI escape codes and other TTY-specific output.
|
||||
*/
|
||||
fd = posix_openpt(O_RDWR);
|
||||
if (fd == -1) {
|
||||
dbg("Failed posix_openpt(), errno %d: %s", errno, strerror(errno));
|
||||
svc->log.enabled = 0;
|
||||
return -1;
|
||||
}
|
||||
if (grantpt(fd) == -1 || unlockpt(fd) == -1) {
|
||||
dbg("Failed grantpt()|unlockpt(), errno %d: %s", errno, strerror(errno));
|
||||
close(fd);
|
||||
if (pipe(pipefd) == -1) {
|
||||
dbg("Failed pipe(), errno %d: %s", errno, strerror(errno));
|
||||
svc->log.enabled = 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
fd = pipefd[1]; /* Write end for service */
|
||||
|
||||
/*
|
||||
* First time, check if we have sysklogd logger tool.
|
||||
@@ -319,17 +316,12 @@ static int lredirect(svc_t *svc)
|
||||
char *prio = "daemon.info";
|
||||
char buf[MAX_IDENT_LEN];
|
||||
char *tag;
|
||||
int fds;
|
||||
|
||||
sched_yield();
|
||||
|
||||
fds = open(ptsname(fd), O_RDONLY);
|
||||
close(fd);
|
||||
if (fds == -1) {
|
||||
logit(LOG_WARNING, "failed open() ptsname(%d), errno %d", fd, errno);
|
||||
_exit(0);
|
||||
}
|
||||
dup2(fds, STDIN_FILENO);
|
||||
close(pipefd[1]); /* Close write end in logger */
|
||||
dup2(pipefd[0], STDIN_FILENO);
|
||||
close(pipefd[0]);
|
||||
|
||||
/* Reset signals */
|
||||
sig_unblock();
|
||||
@@ -385,10 +377,11 @@ static int lredirect(svc_t *svc)
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
dup2(fd, STDERR_FILENO);
|
||||
close(pipefd[0]); /* Close read end in service */
|
||||
dup2(pipefd[1], STDOUT_FILENO);
|
||||
dup2(pipefd[1], STDERR_FILENO);
|
||||
|
||||
return close(fd);
|
||||
return close(pipefd[1]);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -3053,6 +3046,42 @@ int service_completed(svc_t **svcp)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* service_log_incomplete - Log all incomplete run/task in current runlevel
|
||||
*
|
||||
* Iterates through all run/task services and logs those that have not
|
||||
* yet completed. Useful for diagnosing bootstrap delays or reporting
|
||||
* what was skipped when user interrupts bootstrap wait.
|
||||
*/
|
||||
void service_log_incomplete(void)
|
||||
{
|
||||
svc_t *svc, *iter = NULL;
|
||||
int count = 0;
|
||||
|
||||
for (svc = svc_iterator(&iter, 1); svc; svc = svc_iterator(&iter, 0)) {
|
||||
if (!svc_is_runtask(svc))
|
||||
continue;
|
||||
|
||||
if (!svc_enabled(svc))
|
||||
continue;
|
||||
|
||||
if (svc_conflicts(svc))
|
||||
continue;
|
||||
|
||||
if (strstr(svc->cond, plugin_hook_str(HOOK_SVC_UP)) ||
|
||||
strstr(svc->cond, plugin_hook_str(HOOK_SYSTEM_UP)))
|
||||
continue;
|
||||
|
||||
if (!svc->once) {
|
||||
logit(LOG_WARNING, "Incomplete: %s", svc_ident(svc, NULL, 0));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0)
|
||||
logit(LOG_WARNING, "No incomplete run/task services found");
|
||||
}
|
||||
|
||||
/*
|
||||
* Called in SM_RELOAD_WAIT_STATE to update unmodified non-native
|
||||
* services' READY condition to the current generation. Similar
|
||||
|
||||
@@ -48,6 +48,7 @@ void service_step_all (int types);
|
||||
void service_worker (void *unused);
|
||||
|
||||
int service_completed (svc_t **svc);
|
||||
void service_log_incomplete (void);
|
||||
void service_notify_reconf (void);
|
||||
|
||||
void service_init (uev_ctx_t *ctx);
|
||||
|
||||
@@ -187,6 +187,11 @@ static void fs_swapoff(void)
|
||||
if (strcmp(mnt->mnt_type, MNTTYPE_SWAP))
|
||||
continue;
|
||||
|
||||
if (hasmntopt(mnt, "noauto")) {
|
||||
dbg("Skipping swap %s, marked noauto", mnt->mnt_fsname);
|
||||
continue;
|
||||
}
|
||||
|
||||
snprintf(cmd, sizeof(cmd), "swapoff %s", mnt->mnt_fsname);
|
||||
run_interactive(cmd, "Disabling swap %s", mnt->mnt_fsname);
|
||||
}
|
||||
@@ -353,7 +358,7 @@ void do_shutdown(shutop_t op)
|
||||
return;
|
||||
}
|
||||
|
||||
if (wdog) {
|
||||
if (wdtreboot && wdog) {
|
||||
print(kill(wdog->pid, SIGPWR) == 1, "Advising watchdog, system going down");
|
||||
do_sleep(2);
|
||||
}
|
||||
@@ -400,17 +405,17 @@ void do_shutdown(shutop_t op)
|
||||
|
||||
/* Reboot via watchdog or kernel, or shutdown? */
|
||||
if (op == SHUT_REBOOT) {
|
||||
print(0, "Rebooting ...");
|
||||
if (wdog && wdog->pid > 1) {
|
||||
if (wdtreboot && wdog && wdog->pid > 1) {
|
||||
int timeout = 10;
|
||||
|
||||
/* Wait here until the WDT reboots, or timeout with fallback */
|
||||
print(0, "Rebooting using WDT, please wait ...");
|
||||
print(kill(wdog->pid, SIGTERM) == 1, "Pending watchdog reboot");
|
||||
while (timeout--)
|
||||
do_sleep(1);
|
||||
}
|
||||
|
||||
dbg("Rebooting ...");
|
||||
print(0, "Rebooting ...");
|
||||
reboot(RB_AUTOBOOT);
|
||||
} else if (op == SHUT_OFF) {
|
||||
print(0, "Powering down ...");
|
||||
|
||||
@@ -23,8 +23,10 @@
|
||||
|
||||
#include "config.h" /* Generated by configure script */
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <paths.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
|
||||
#include "finit.h"
|
||||
#include "cond.h"
|
||||
@@ -56,8 +58,80 @@ static struct {
|
||||
int newlevel; /* Set to new runlevel on change, -1 otherwise */
|
||||
int reload; /* Set on reload event, else 0 */
|
||||
int in_reload; /* Set when waiting for all processes to be halted */
|
||||
int skip_bootstrap; /* Set to skip bootstrap waiting */
|
||||
} sm;
|
||||
|
||||
static uev_t console_watcher;
|
||||
static int console_fd = -1;
|
||||
|
||||
/*
|
||||
* Console input callback - handles Ctrl-C during bootstrap wait
|
||||
*/
|
||||
static void console_input_cb(uev_t *w, void *arg, int events)
|
||||
{
|
||||
ssize_t len;
|
||||
char ch;
|
||||
|
||||
(void)arg;
|
||||
|
||||
if (UEV_ERROR == events) {
|
||||
warnx("Error in console watcher");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read and check for Ctrl-C (ASCII 3) */
|
||||
len = read(w->fd, &ch, 1);
|
||||
if (len == 1 && ch == 3) { /* Ctrl-C */
|
||||
sm.skip_bootstrap = 1;
|
||||
print(2, "Bootstrap wait interrupted by Ctrl-C, resuming ...");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Stop and cleanup the console watcher
|
||||
*/
|
||||
static void console_watcher_stop(void)
|
||||
{
|
||||
if (console_fd >= 0) {
|
||||
uev_io_stop(&console_watcher);
|
||||
close(console_fd);
|
||||
console_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup console watcher for Ctrl-C during bootstrap
|
||||
*/
|
||||
static void console_watcher_start(uev_ctx_t *ctx)
|
||||
{
|
||||
struct termios tio;
|
||||
|
||||
/* Open console in non-blocking mode */
|
||||
console_fd = open(console(), O_RDONLY | O_NONBLOCK);
|
||||
if (console_fd < 0) {
|
||||
dbg("Failed opening console for input monitoring: %s", strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get current terminal settings */
|
||||
if (tcgetattr(console_fd, &tio) == 0) {
|
||||
/* Put terminal in raw mode to catch Ctrl-C */
|
||||
tio.c_lflag &= ~(ICANON | ECHO | ISIG);
|
||||
tio.c_cc[VMIN] = 0;
|
||||
tio.c_cc[VTIME] = 0;
|
||||
tcsetattr(console_fd, TCSANOW, &tio);
|
||||
}
|
||||
|
||||
/* Register I/O watcher */
|
||||
if (uev_io_init(ctx, &console_watcher, console_input_cb, NULL, console_fd, UEV_READ)) {
|
||||
dbg("Failed setting up console watcher");
|
||||
close(console_fd);
|
||||
console_fd = -1;
|
||||
} else {
|
||||
dbg("Console watcher active, press Ctrl-C to skip bootstrap wait");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Wait for system bootstrap to complete, all SVC_TYPE_RUNTASK must be
|
||||
@@ -76,13 +150,19 @@ static void sm_check_bootstrap(void *work)
|
||||
service_step_all(SVC_TYPE_ANY);
|
||||
|
||||
bootstrap_done = service_completed(&svc);
|
||||
if (timeout-- > 0 && !bootstrap_done) {
|
||||
if (timeout-- > 0 && !bootstrap_done && !sm.skip_bootstrap) {
|
||||
dbg("Not all bootstrap run/tasks have completed yet ... %d", timeout);
|
||||
schedule_work(work);
|
||||
return;
|
||||
}
|
||||
|
||||
if (timeout > 0) {
|
||||
/* Stop console watcher before proceeding */
|
||||
console_watcher_stop();
|
||||
|
||||
if (sm.skip_bootstrap) {
|
||||
logit(LOG_WARNING, "Ctrl-C received, skipped waiting for the following:");
|
||||
service_log_incomplete();
|
||||
} else if (timeout > 0) {
|
||||
dbg("All run/task have completed, resuming bootstrap.");
|
||||
} else {
|
||||
dbg("Timeout, resuming bootstrap.");
|
||||
@@ -184,6 +264,7 @@ void sm_init(void)
|
||||
sm.newlevel = -1;
|
||||
sm.reload = 0;
|
||||
sm.in_reload = 0;
|
||||
sm.skip_bootstrap = 0;
|
||||
|
||||
dbg("Starting bootstrap finalize timer ...");
|
||||
schedule_work(&work);
|
||||
@@ -206,6 +287,7 @@ restart:
|
||||
service_step_all(SVC_TYPE_RUNTASK | SVC_TYPE_SERVICE | SVC_TYPE_SYSV);
|
||||
|
||||
sm.state = SM_BOOTSTRAP_WAIT_STATE;
|
||||
console_watcher_start(ctx);
|
||||
break;
|
||||
|
||||
/*
|
||||
@@ -229,6 +311,9 @@ restart:
|
||||
if (sm.newlevel == -1)
|
||||
break;
|
||||
|
||||
/* Ensure console watcher is stopped before proceeding */
|
||||
console_watcher_stop();
|
||||
|
||||
/* Hooks that should run at the very end */
|
||||
dbg("Calling all system up hooks ...");
|
||||
plugin_run_hooks(HOOK_SYSTEM_UP);
|
||||
|
||||
Reference in New Issue
Block a user