Merge branch 'master' into wip-tests

This commit is contained in:
Jacques de Laval committed 2021-02-21 12:52:30 +01:00
commit 765e1eb640
17 files changed
+303 -157

No files matched your search

+5
View File
@@ -12,6 +12,11 @@ to service conditions. There are other significant changes as well, so
make sure to read the whole changelog when upgrading. make sure to read the whole changelog when upgrading.
### Changes ### Changes
* The stand-alone `reboot` tool has been replaced with a symlink to
`initctl`, like its siblings: halt, shutdown, poweroff, and suspend.
Calling `reboot` & C:o now defaults to the corresponding `initctl cmd`
with a fallback to sending signals as per traditional SysV init. The
`-f` (force) flag remains, where `reboot(2)` is called directly
* Introducing Finit progress 𝓜𝓸𝓭𝓮𝓻𝓷 * Introducing Finit progress 𝓜𝓸𝓭𝓮𝓻𝓷
* Removed built-in inetd super server. If you need this functionality, * Removed built-in inetd super server. If you need this functionality,
use an external inetd, like xinetd, instead. A pull request for a use an external inetd, like xinetd, instead. A pull request for a
+14 -1
View File
@@ -4,9 +4,22 @@ Tips & Tricks with the kernel cmdline
This document summarizes the different boot parameters that can be This document summarizes the different boot parameters that can be
passed on the Linux kernel command line. Not limited to Finit. passed on the Linux kernel command line. Not limited to Finit.
The `bool` setting is one of `on, off, true false, 1, 0`.
* `debug`__ * `debug`__
Enable kernel debug. Debug messages are printed to the console Enable kernel debug. Debug messages are printed to the console
until Finit starts up, the syslog daemon then empties `/dev/kmsg` until Finit starts up.
* `finit.debug[=bool]`
Enable finit debug. This is operated independently of the kerne
debug setting. New as of Finit v4.
* `finit.show_status[=bool]`
Control finit boot progress, including banner.
* `finit.status_style=<classic,modern>`
Set Finit boot progress style, when enabled.
* `init=/bin/sh` * `init=/bin/sh`
Bypass system default init and tell kernel to start a shell. Note, Bypass system default init and tell kernel to start a shell. Note,
+28 -29
View File
@@ -17,8 +17,8 @@ Conditions are a new addition to Finit, introduced in v3, with the
intention of providing a mechanism for common synchronization problems. intention of providing a mechanism for common synchronization problems.
For example: For example:
- *"do not start this service until another process has started"*, or - *"wait for service A to start before starting service B"*, or
- *"do not starty until basic network access is available"* - *"wait for basic network access to be available"*
Conditions are similar in syntax to declaring runlevels per service. Conditions are similar in syntax to declaring runlevels per service.
They are specified within angle brackets `<>` and can be applied to any They are specified within angle brackets `<>` and can be applied to any
@@ -30,14 +30,12 @@ service to run.
### Example ### Example
```shell service [2345] <pid/setupd,pid/zebra> /sbin/netd -- Network monitor
service [2345] <pid/setupd,pid/zebra> /sbin/netd -- Network monitor
```
In this example the Network monitor daemon `netd` is not started until In this example the Network monitor daemon `netd` is not started until
both the `pid/sbin/setupd` *and* `pid/sbin/zebra` conditions are both the `pid/setupd` *and* `pid/zebra` conditions are satisfied. A
satisfied. An `svc` condition is satisfied by the corresponding `pid/` condition is satisfied by the corresponding service's PID file
service's PID file being created. being created.
**NOTE:** Conditions also stop services when a condition is no longer **NOTE:** Conditions also stop services when a condition is no longer
asserted. I.e., if the Zebra process above stops or restarts, netd asserted. I.e., if the Zebra process above stops or restarts, netd
@@ -56,32 +54,33 @@ on this, see [Internals](#internals).) Thus, after a reconfiguration it
is up to the "owner" of the condition to convey the new (or possibly is up to the "owner" of the condition to convey the new (or possibly
unchanged) state of it. unchanged) state of it.
> **Note:** For `pid/` conditions it is expected that services "touch"
> or recreate their PID file on `SIGHUP`.
Built-in Conditions Built-in Conditions
------------------- -------------------
Finit is distributed with a `pidfile` and `netlink` plugin. If enabled, Finit comes with a `pidfile` and `netlink` plugin. When enabled, the
the `pidfile` plugin watches `/var/run/` for PID files created by the `pidfile` plugin watches `/var/run/` (recursively) for PID files created
monitored services, and sets a corresponding condition in the `pid/` by the monitored services, and sets a corresponding condition in the
namespace. Similarily, the `netlink` plugin provides basic conditions `pid/` namespace. Similarily, the `netlink` plugin provides basic
for when an interface is brought up/down and when a default route conditions for when an interface is brought up/down and when a default
(gateway) is set, in the `net/` namespace. route (gateway) is set, in the `net/` namespace.
With the example listed above, finit does not start the `/sbin/netd` With the example listed above, finit does not start the `/sbin/netd`
daemon until `setupd` and `zebra` has started *and* created their PID daemon until `setupd` and `zebra` has started *and* created their PID
files. Which they do when they have completed their initial set up and files. Which they do when they have completed their initial set up and
are ready to receive signals. are ready to receive signals.
The full path to the dependency is needed by finit to match the PID file Finit expects monitored services to touch their PID files, i.e. update
to a monitored process. Finit expects monitored services to touch their the mtime, when they reload their configuration files after a `SIGHUP`.
PID files, i.e. update the mtime, when they reload their configuration Some services do not support `SIGHUP` and are instead restarted, which
files after a `SIGHUP`. Some services do not support `SIGHUP` and are is a crude but effective way to have the PID file touched (re-created).
instead restarted, which is a crude but effective way to have the PID
file touched (re-created).
Built-in conditions: Built-in conditions:
- `pid/<PATH>` - `pid/<SERVICE>`
- `net/route/default` - `net/route/default`
- `net/<IFNAME>/exist` - `net/<IFNAME>/exist`
- `net/<IFNAME>/up` - `net/<IFNAME>/up`
@@ -107,9 +106,9 @@ the daemon and the empty string.
| /usr/bin/dbus-daemon | pid/dbus-daemon | | /usr/bin/dbus-daemon | pid/dbus-daemon |
| :222 dropbear -p 222 | pid/dropbear:222 | | :222 dropbear -p 222 | pid/dropbear:222 |
The condition is asserted when `pidfile.so` receives an inotify event for a file The condition is asserted when `pidfile.so` receives an inotify event
matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`, which contains the for a file matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`,
PID of the service Finit has started. which contains the PID of the service Finit has started.
When Finit configuration files are changed and the `initctl reload` When Finit configuration files are changed and the `initctl reload`
command is called, it is expected of services to touch their PID files command is called, it is expected of services to touch their PID files
@@ -140,10 +139,10 @@ conditions that are not satisfied. Listed as `off` below.
```shell ```shell
~ # initctl cond show ~ # initctl cond show
PID Service Status Condition (+ on, ~ flux, - off) PID IDENT STATUS CONDITION (+ ON, ~ FLUX, - OFF)
=============================================================================== =======================================================================
1419 /sbin/netd on <+pid/sbin/setupd,+pid/sbin/zebra> 1419 /sbin/netd on <+pid/setupd,+pid/zebra>
0 /sbin/udhcpc off <-net/vlan1/exist> 0 /sbin/udhcpc off <-net/vlan1/exist>
``` ```
Here we can see that `netd` is allowed to run since both its conditions Here we can see that `netd` is allowed to run since both its conditions
@@ -165,7 +164,7 @@ from `initctl cond show` again. The client will likely have failed to
start, but at least the condition is now satisfied. start, but at least the condition is now satisfied.
There is also the `initctl cond dump` command, which dumps all known There is also the `initctl cond dump` command, which dumps all known
conditions and their current status. conditions, their current status, and their origin.
Internals Internals
+6
View File
@@ -182,6 +182,8 @@ optional arguments and description.
`run` commands are guaranteed to be completed before running the next `run` commands are guaranteed to be completed before running the next
command. Highly useful if true serialization is needed. command. Highly useful if true serialization is needed.
> `<COND>` is described in the [Services](#services) section.
### One-shot Commands (parallel) ### One-shot Commands (parallel)
@@ -194,6 +196,8 @@ redirects can be freely used:
task [s] echo "foo" | cat >/tmp/bar task [s] echo "foo" | cat >/tmp/bar
> `<COND>` is described in the [Services](#services) section.
### SysV Init Scripts ### SysV Init Scripts
@@ -212,6 +216,8 @@ calls `init-script restart` on `initctl reload`. Similar to how
Forking services started with `sysv` scripts can be monitored by Finit Forking services started with `sysv` scripts can be monitored by Finit
by declaring the PID file to look for: `pid:!/path/to/pidfile.pid`. by declaring the PID file to look for: `pid:!/path/to/pidfile.pid`.
> `<COND>` is described in the [Services](#services) section.
### Services ### Services
+44 -25
View File
@@ -6,31 +6,50 @@ Though Finit comes with its own tools for (poweroff, halt, reboot), for
compatibility with the existing BusyBox toolset the following signals compatibility with the existing BusyBox toolset the following signals
have been adopted: have been adopted:
* `SIGHUP` `SIGHUP`
Same effect as `finit q`, `init q`, or `initctl reload`, reloads all --------
*.conf files in `/etc/finit.d/`
* `SIGUSR1`
Calls shutdown hooks, including `HOOK_SHUTDOWN`, stopping all running
processes, and unmounts all file systems. Then tells kernel to halt.
Most people these days want `SIGUSR2` though.
SysV init and systemd use this to re-open their FIFO/D-Bus. Same effect as `finit q`, `init q`, or `initctl reload`, reloads all
* `SIGUSR2` *.conf files in `/etc/finit.d/`
Like SIGUSR1, but tell kernel to power-off the system, if ACPI
or similar exists to actually do this. If the kernel fails `SIGUSR1`
power-off, Finit falls back to halt. ---------
Calls shutdown hooks, including `HOOK_SHUTDOWN`, stopping all running
processes, and unmounts all file systems. Then tells kernel to halt.
Most people these days want `SIGUSR2` though.
SysV init and systemd use this to re-open their FIFO/D-Bus.
`SIGUSR2`
---------
Like SIGUSR1, but tell kernel to power-off the system, if ACPI or
similar exists to actually do this. If the kernel fails power-off,
Finit falls back to halt.
SysV init N/A, systemd dumps its internal state to log. SysV init N/A, systemd dumps its internal state to log.
* `SIGTERM`
Like `SIGUSR1`, but tell kernel to reboot the system when done. `SIGTERM`
---------
Like `SIGUSR1`, but tell kernel to reboot the system when done.
SysV init N/A, systemd rexecutes itself. SysV init N/A, systemd rexecutes itself.
* `SIGINT`
Sent from kernel when the CTRL-ALT-DEL key combo is pressed. `SIGINT`
SysV init and systemd default to reboot with `shutdown -r`. --------
Finit currently forwards this to `SIGTERM`.
* `SIGPWR` Sent from kernel when the CTRL-ALT-DEL key combo is pressed. SysV init
Sent from a power daemon, like `powstatd(8)`, on changes to the and systemd default to reboot with `shutdown -r`.
UPS status. Traditionally SysV init read /etc/powerstatus and
acted on "OK", "FAIL", or "LOW" and then removed the file. Finit currently forwards this to `SIGTERM`.
Finit currently forwards this to `SIGUSR2`.
`SIGPWR`
--------
Sent from a power daemon, like `powstatd(8)`, on changes to the
UPS status. Traditionally SysV init read /etc/powerstatus and
acted on "OK", "FAIL", or "LOW" and then removed the file.
Finit currently forwards this to `SIGUSR2`.
+5 -5
View File
@@ -43,19 +43,19 @@ static void setup(void *arg)
path = which("/lib/systemd/systemd-udevd"); path = which("/lib/systemd/systemd-udevd");
if (path) { if (path) {
/* Register udevd as a monitored service */ /* Register udevd as a monitored service */
snprintf(cmd, sizeof(cmd), "[S12345789] pid:udevd %s " snprintf(cmd, sizeof(cmd), "[S12345789] pid:udevd name:udevd %s "
"-- Device event managing daemon", path); "-- Device event managing daemon", path);
if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) { if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) {
_pe("Failed registering %s", path); _pe("Failed registering %s", path);
} else { } else {
snprintf(cmd, sizeof(cmd), ":1 [S] <svc%s> " snprintf(cmd, sizeof(cmd), ":1 [S] log <pid/udevd> "
"udevadm trigger -c add -t devices " "udevadm trigger -c add -t devices "
"-- Requesting device events", path); "-- Requesting device events");
service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL);
snprintf(cmd, sizeof(cmd), ":2 [S] <svc%s> " snprintf(cmd, sizeof(cmd), ":2 [S] log <pid/udevd> "
"udevadm trigger -c add -t subsystems " "udevadm trigger -c add -t subsystems "
"-- Requesting subsystem events", path); "-- Requesting subsystem events");
service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL); service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL);
} }
+37 -49
View File
@@ -8,16 +8,16 @@ else
AM_LDFLAGS = -export-dynamic AM_LDFLAGS = -export-dynamic
endif endif
sbin_PROGRAMS = finit initctl reboot sbin_PROGRAMS = finit initctl
pkglibexec_SCRIPTS = cgreaper.sh pkglibexec_SCRIPTS = cgreaper.sh
if WATCHDOGD if WATCHDOGD
pkglibexec_PROGRAMS = watchdogd pkglibexec_PROGRAMS = watchdogd
endif endif
if LOGIT if LOGIT
bin_PROGRAMS = logit pkglibexec_PROGRAMS = logit
logit_SOURCES = logit.c logit_SOURCES = logit.c
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99 logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
endif endif
finit_SOURCES = api.c cgroup.c cgroup.h \ finit_SOURCES = api.c cgroup.c cgroup.h \
@@ -49,19 +49,15 @@ else
finit_LDADD += -ldl finit_LDADD += -ldl
endif endif
initctl_SOURCES = initctl.c client.c client.h \ initctl_SOURCES = initctl.c client.c client.h \
serv.c serv.h svc.h \ reboot.c serv.c serv.h svc.h \
cond.c cond.h util.c util.h cond.c cond.h util.c util.h
initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99 initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
initctl_CFLAGS += $(lite_CFLAGS) initctl_CFLAGS += $(lite_CFLAGS)
initctl_LDADD = $(lite_LIBS) initctl_LDADD = $(lite_LIBS)
reboot_SOURCES = reboot.c util.c util.h
reboot_CFLAGS = $(lite_CFLAGS)
reboot_LDADD = $(lite_LIBS)
INIT_LNKS = init INIT_LNKS = init
REBOOT_LNKS = shutdown halt poweroff suspend REBOOT_LNKS = reboot shutdown halt poweroff suspend
if WATCHDOGD if WATCHDOGD
watchdogd_SOURCES = watchdog.c watchdog.h watchdogd_SOURCES = watchdog.c watchdog.h
@@ -72,46 +68,38 @@ endif
# Hook in install to add finit and reboot symlink(s) # Hook in install to add finit and reboot symlink(s)
install-exec-hook: install-exec-hook:
@$(INSTALL_DATA) $(srcdir)/rescue.conf $(DESTDIR)$(pkglibdir) @$(INSTALL_DATA) $(srcdir)/rescue.conf $(DESTDIR)$(pkglibdir)
@for file in $(INIT_LNKS); do \ @for file in $(INIT_LNKS); do \
lnk=$(DESTDIR)$(sbindir)/$$file; \ lnk=$(DESTDIR)$(sbindir)/$$file; \
if [ -e $$lnk ]; then \ if [ -e $$lnk ]; then \
echo "Skipping $$lnk exists."; \ echo "Skipping $$lnk exists."; \
else \ else \
$(LN_S) finit $$lnk; \ $(LN_S) finit $$lnk; \
fi; \ fi; \
done
@for file in $(REBOOT_LNKS); do \
lnk=$(DESTDIR)$(sbindir)/$$file; \
if [ -e $$lnk ]; then \
echo "Skipping $$lnk exists."; \
else \
$(LN_S) initctl $$lnk; \
fi; \
done done
@if [ -L $(DESTDIR)$(sbindir)/reboot ]; then \
echo "Non-finit reboot, skipping install."; \
else \
for file in $(REBOOT_LNKS); do \
lnk=$(DESTDIR)$(sbindir)/$$file; \
if [ -e $$lnk ]; then \
echo "Skipping $$lnk exists."; \
else \
$(LN_S) reboot $$lnk; \
fi; \
done; \
fi
uninstall-hook: uninstall-hook:
@$(RM) $(DESTDIR)$(pkglibdir)/rescue.conf @$(RM) $(DESTDIR)$(pkglibdir)/rescue.conf
@for file in $(INIT_LNKS); do \ @for file in $(INIT_LNKS); do \
lnk=`readlink $(DESTDIR)$(sbindir)/$$file`; \ lnk=$(DESTDIR)$(sbindir)/$$file; \
if [ "$$lnk" != "finit" ]; then \ dst=`readlink $$lnk`; \
echo "Skipping non-finit $$file."; \ if [ "$$dst" != "finit" ]; then \
continue; \ continue; \
fi; \ fi; \
$(RM) $(DESTDIR)$(sbindir)/$$file; \ $(RM) $(DESTDIR)$(sbindir)/$$file; \
done
@for file in $(REBOOT_LNKS); do \
lnk=$(DESTDIR)$(sbindir)/$$file; \
dst=`readlink $$lnk`; \
if [ "$$dst" != "initctl" ]; then \
continue; \
fi; \
$(RM) $$lnk; \
done done
@if [ -L $(DESTDIR)$(sbindir)/reboot ]; then \
echo "Non-finit reboot, skipping uninstall."; \
else \
for file in $(REBOOT_LNKS); do \
lnk=$(DESTDIR)$(sbindir)/$$file; \
dst=`readlink $$lnk`; \
if [ "$$dst" != "reboot" ]; then \
continue; \
fi; \
$(RM) $$lnk; \
done; \
fi
+50
View File
@@ -182,6 +182,49 @@ static svc_t *do_find_byc(char *buf, size_t len)
return NULL; return NULL;
} }
static int do_reboot(int cmd, char *buf, size_t len)
{
int rc = 1;
switch (cmd) {
case INIT_CMD_REBOOT:
_d("reboot");
halt = SHUT_REBOOT;
service_runlevel(6);
break;
case INIT_CMD_HALT:
_d("halt");
halt = SHUT_HALT;
service_runlevel(0);
break;
case INIT_CMD_POWEROFF:
_d("poweroff");
halt = SHUT_OFF;
service_runlevel(0);
break;
case INIT_CMD_SUSPEND:
_d("suspend");
sync();
rc = reboot(RB_SW_SUSPEND);
if (rc) {
if (errno == EINVAL)
snprintf(buf, len, "Kernel does not support suspend.");
else
snprintf(buf, len, "Failed: %s", strerror(errno));
}
break;
default:
rc = 255;
break;
}
return rc;
}
typedef struct { typedef struct {
char *event; char *event;
void (*cb)(void); void (*cb)(void);
@@ -302,6 +345,13 @@ static void api_cb(uev_t *w, void *arg, int events)
rq.sleeptime = prevlevel; rq.sleeptime = prevlevel;
break; break;
case INIT_CMD_REBOOT:
case INIT_CMD_HALT:
case INIT_CMD_POWEROFF:
case INIT_CMD_SUSPEND:
result = do_reboot(rq.cmd, rq.data, sizeof(rq.data));
break;
case INIT_CMD_ACK: case INIT_CMD_ACK:
_d("Client failed reading ACK"); _d("Client failed reading ACK");
goto leave; goto leave;
+1 -1
View File
@@ -5,7 +5,7 @@ BASE=/sys/fs/cgroup/finit
PROC=`basename $1` PROC=`basename $1`
DIR="$BASE$1" DIR="$BASE$1"
logit -p daemon.info -t finit "$PROC stopped, cleaning up control group $DIR" /libexec/finit/logit -p daemon.info -t finit "$PROC stopped, cleaning up control group $DIR"
rmdir $DIR rmdir $DIR
exit 0 exit 0
+4 -1
View File
@@ -80,7 +80,10 @@ int client_send(struct init_request *rq, ssize_t len)
if (read(sd, rq, len) != len) if (read(sd, rq, len) != len)
goto error; goto error;
result = 0; if (rq->cmd == INIT_CMD_NACK)
result = 1;
else
result = 0;
goto exit; goto exit;
error: error:
perror("Failed communicating with finit"); perror("Failed communicating with finit");
+4 -1
View File
@@ -77,11 +77,14 @@ static void hide_args(int argc, char *argv[])
int get_bool(char *ptr, int default_value) int get_bool(char *ptr, int default_value)
{ {
if (!ptr)
goto fallback;
if (string_compare(ptr, "true") || string_compare(ptr, "on") || string_compare(ptr, "1")) if (string_compare(ptr, "true") || string_compare(ptr, "on") || string_compare(ptr, "1"))
return 1; return 1;
if (string_compare(ptr, "false") || string_compare(ptr, "off") || string_compare(ptr, "0")) if (string_compare(ptr, "false") || string_compare(ptr, "off") || string_compare(ptr, "0"))
return 0; return 0;
fallback:
return default_value; return default_value;
} }
+6
View File
@@ -45,6 +45,8 @@
#define _PATH_VARRUN "/var/run/" #define _PATH_VARRUN "/var/run/"
#endif #endif
#define LOGIT_PATH FINIT_LIBPATH_ "/logit"
#define CMD_SIZE 256 #define CMD_SIZE 256
#define LINE_SIZE 1024 #define LINE_SIZE 1024
#define BUF_SIZE 4096 #define BUF_SIZE 4096
@@ -72,6 +74,10 @@
#define INIT_CMD_UNUSED2 14 /* Unused, was INIT_CMD_QUERY_INETD */ #define INIT_CMD_UNUSED2 14 /* Unused, was INIT_CMD_QUERY_INETD */
#define INIT_CMD_UNUSED1 15 /* Unused, was INIT_CMD_EMIT */ #define INIT_CMD_UNUSED1 15 /* Unused, was INIT_CMD_EMIT */
#define INIT_CMD_GET_RUNLEVEL 16 #define INIT_CMD_GET_RUNLEVEL 16
#define INIT_CMD_REBOOT 20
#define INIT_CMD_HALT 21
#define INIT_CMD_POWEROFF 22
#define INIT_CMD_SUSPEND 23
#define INIT_CMD_WDOG_HELLO 128 /* Watchdog register and hello */ #define INIT_CMD_WDOG_HELLO 128 /* Watchdog register and hello */
#define INIT_CMD_SVC_ITER 129 #define INIT_CMD_SVC_ITER 129
#define INIT_CMD_SVC_QUERY 130 #define INIT_CMD_SVC_QUERY 130
+44 -8
View File
@@ -54,6 +54,9 @@ int verbose = 0;
int runlevel = 0; int runlevel = 0;
int iw, pw; int iw, pw;
extern int reboot_main(int argc, char *argv[]);
/* figure ut width of IDENT and PID columns */ /* figure ut width of IDENT and PID columns */
static void col_widths(void) static void col_widths(void)
{ {
@@ -349,17 +352,30 @@ static int do_cond(char *cmd)
return do_cond_show(NULL); return do_cond_show(NULL);
} }
static int do_signal(int signo, const char *msg) static int do_cmd(int cmd)
{ {
if (kill(1, signo)) struct init_request rq = {
err(1, "Failed signalling init to %s", msg); .magic = INIT_MAGIC,
.cmd = cmd,
};
if (client_send(&rq, sizeof(rq))) {
if (rq.cmd == INIT_CMD_NACK)
puts(rq.data);
return 1;
}
/* Wait here for systemd to shutdown/reboot */
sleep(5);
return 0; return 0;
} }
static int do_halt (char *arg) { return do_signal(SIGUSR1, "halt"); } int do_reboot (char *arg) { return do_cmd(INIT_CMD_REBOOT); }
static int do_poweroff(char *arg) { return do_signal(SIGUSR2, "power off"); } int do_halt (char *arg) { return do_cmd(INIT_CMD_HALT); }
static int do_reboot (char *arg) { return do_signal(SIGTERM, "reboot"); } int do_poweroff(char *arg) { return do_cmd(INIT_CMD_POWEROFF); }
int do_suspend (char *arg) { return do_cmd(INIT_CMD_SUSPEND); }
int utmp_show(char *file) int utmp_show(char *file)
{ {
@@ -605,6 +621,22 @@ static int show_cgroup(char *arg)
return 0; return 0;
} }
static int transform(char *nm)
{
char *names[] = {
"reboot", "shutdown", "poweroff", "halt", "suspend",
NULL
};
size_t i;
for (i = 0; names[i]; i++) {
if (!strcmp(nm, names[i]))
return 1;
}
return 0;
}
static int usage(int rc) static int usage(int rc)
{ {
fprintf(stderr, fprintf(stderr,
@@ -644,7 +676,8 @@ static int usage(int rc)
" runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n" " runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n"
" reboot Reboot system\n" " reboot Reboot system\n"
" halt Halt system\n" " halt Halt system\n"
" poweroff Halt and power off system\n", prognm); " poweroff Halt and power off system\n"
" suspend Suspend system\n", prognm);
if (has_utmp()) if (has_utmp())
fprintf(stderr, fprintf(stderr,
@@ -693,6 +726,7 @@ int main(int argc, char *argv[])
{ "reboot", do_reboot }, { "reboot", do_reboot },
{ "halt", do_halt }, { "halt", do_halt },
{ "poweroff", do_poweroff }, { "poweroff", do_poweroff },
{ "suspend", do_suspend },
{ "utmp", do_utmp }, { "utmp", do_utmp },
{ NULL, NULL } { NULL, NULL }
@@ -706,7 +740,9 @@ int main(int argc, char *argv[])
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
progname(argv[0]); if (transform(progname(argv[0])))
return reboot_main(argc, argv);
while ((c = getopt_long(argc, argv, "bh?ntv", long_options, NULL)) != EOF) { while ((c = getopt_long(argc, argv, "bh?ntv", long_options, NULL)) != EOF) {
switch(c) { switch(c) {
case 'b': case 'b':
+34 -17
View File
@@ -22,18 +22,14 @@
*/ */
#include <err.h> #include <err.h>
#include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/reboot.h>
#include "util.h" #include "util.h"
#ifndef RB_SW_SUSPEND /* Since Linux 2.5.18, but not in GLIBC until 2.19 */
#define RB_SW_SUSPEND 0xd000fce2
#endif
typedef enum { typedef enum {
CMD_UNKNOWN = -1, CMD_UNKNOWN = -1,
CMD_REBOOT = 0, CMD_REBOOT = 0,
@@ -45,6 +41,13 @@ typedef enum {
static cmd_t cmd = CMD_UNKNOWN; static cmd_t cmd = CMD_UNKNOWN;
static char *msg = NULL; static char *msg = NULL;
/* initctl API */
extern int do_reboot (char *arg);
extern int do_halt (char *arg);
extern int do_poweroff(char *arg);
extern int do_suspend (char *arg);
static void transform(char *nm) static void transform(char *nm)
{ {
if (!nm) if (!nm)
@@ -85,6 +88,16 @@ static void transform(char *nm)
} }
} }
/*
* fallback in case of initctl API failure
*/
static void do_kill(int signo, char *msg)
{
if (kill(1, signo))
err(1, "Failed signalling init to %s", msg);
do_sleep(5);
}
static void sig(int signo) static void sig(int signo)
{ {
(void)signo; /* NOP */ (void)signo; /* NOP */
@@ -105,7 +118,7 @@ static int usage(int rc)
return rc; return rc;
} }
int main(int argc, char *argv[]) int reboot_main(int argc, char *argv[])
{ {
int c, force = 0; int c, force = 0;
struct option long_options[] = { struct option long_options[] = {
@@ -118,7 +131,7 @@ int main(int argc, char *argv[])
}; };
/* Initial command taken from program name */ /* Initial command taken from program name */
transform(progname(argv[0])); transform(prognm);
while ((c = getopt_long(argc, argv, "h?fHPpr", long_options, NULL)) != EOF) { while ((c = getopt_long(argc, argv, "h?fHPpr", long_options, NULL)) != EOF) {
switch(c) { switch(c) {
@@ -170,6 +183,8 @@ int main(int argc, char *argv[])
case CMD_SUSPEND: case CMD_SUSPEND:
c = reboot(RB_SW_SUSPEND); c = reboot(RB_SW_SUSPEND);
if (c && errno == EINVAL)
errx(1, "Kernel does not support suspend.");
break; break;
case CMD_UNKNOWN: case CMD_UNKNOWN:
@@ -178,22 +193,25 @@ int main(int argc, char *argv[])
} }
if (c) if (c)
warn("Failed forced %s", msg); err(1, "Failed forced %s", msg);
else
return 0; return 0;
} }
switch (cmd) { switch (cmd) {
case CMD_REBOOT: case CMD_REBOOT:
c = kill(1, SIGTERM); if (do_reboot(NULL))
do_kill(SIGTERM, msg);
break; break;
case CMD_HALT: case CMD_HALT:
c = kill(1, SIGUSR1); if (do_halt(NULL))
do_kill(SIGUSR1, msg);
break; break;
case CMD_POWEROFF: case CMD_POWEROFF:
c = kill(1, SIGUSR2); if (do_poweroff(NULL))
do_kill(SIGUSR2, msg);
break; break;
case CMD_SUSPEND: case CMD_SUSPEND:
@@ -201,16 +219,15 @@ int main(int argc, char *argv[])
* Only initctl supports suspend, we avoid adding * Only initctl supports suspend, we avoid adding
* another signal to finit for compat reasons. * another signal to finit for compat reasons.
*/ */
do_suspend(NULL);
break;
case CMD_UNKNOWN: case CMD_UNKNOWN:
errx(1, "Invalid command"); errx(1, "Invalid command");
break; break;
} }
if (c)
err(1, "Failed signalling init to %s", msg);
do_sleep(5);
return 0; return 0;
} }
+14 -20
View File
@@ -27,6 +27,7 @@
#include <ctype.h> /* isblank() */ #include <ctype.h> /* isblank() */
#include <sched.h> /* sched_yield() */ #include <sched.h> /* sched_yield() */
#include <string.h> #include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <net/if.h> #include <net/if.h>
@@ -149,7 +150,6 @@ static int fredirect(const char *file)
return -1; return -1;
} }
#ifndef LOGIT_ENABLED
/* /*
* Fallback in case we don't even have logger on the system. * Fallback in case we don't even have logger on the system.
* XXX: we should parse 'prio' here to get facility.level * XXX: we should parse 'prio' here to get facility.level
@@ -160,13 +160,13 @@ static void fallback_logger(char *ident, char *prio)
int level = LOG_NOTICE; int level = LOG_NOTICE;
char buf[256]; char buf[256];
prctl(PR_SET_NAME, "finitlog", 0, 0, 0);
openlog(ident, LOG_NOWAIT | LOG_PID, facility); openlog(ident, LOG_NOWAIT | LOG_PID, facility);
while ((fgets(buf, sizeof(buf), stdin))) while ((fgets(buf, sizeof(buf), stdin)))
syslog(level, "%s", buf); syslog(level, "%s", buf);
closelog(); closelog();
} }
#endif
/* /*
* Redirect output to syslog using the command line logit tool * Redirect output to syslog using the command line logit tool
@@ -206,19 +206,20 @@ static int lredirect(svc_t *svc)
/* Reset signals */ /* Reset signals */
sig_unblock(); sig_unblock();
if (!whichp(LOGIT_PATH)) {
logit(LOG_INFO, LOGIT_PATH " missing, using syslog for %s instead", svc->name);
fallback_logger(tag, prio);
_exit(0);
}
if (svc->log.file[0] == '/') { if (svc->log.file[0] == '/') {
#ifdef LOGIT_ENABLED
char sz[20], num[3]; char sz[20], num[3];
snprintf(sz, sizeof(sz), "%d", logfile_size_max); snprintf(sz, sizeof(sz), "%d", logfile_size_max);
snprintf(num, sizeof(num), "%d", logfile_count_max); snprintf(num, sizeof(num), "%d", logfile_count_max);
execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL); execlp(LOGIT_PATH, "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL);
#else _exit(1);
logit(LOG_INFO, "logit disabled, logging %s to syslog instead", svc->name);
fallback_logger(tag, prio);
_exit(0);
#endif
} }
if (svc->log.ident[0]) if (svc->log.ident[0])
@@ -226,15 +227,8 @@ static int lredirect(svc_t *svc)
if (svc->log.prio[0]) if (svc->log.prio[0])
prio = svc->log.prio; prio = svc->log.prio;
#ifdef LOGIT_ENABLED execlp(LOGIT_PATH, "logit", "-t", tag, "-p", prio, NULL);
execlp("logit", "logit", "-t", tag, "-p", prio, NULL); _exit(1);
#else
if (whichp("logger"))
execlp("logger", "logger", "-t", tag, "-p", prio, NULL);
fallback_logger(tag, prio);
#endif
_exit(0);
} }
dup2(fd, STDOUT_FILENO); dup2(fd, STDOUT_FILENO);
@@ -364,7 +358,7 @@ static int service_start(svc_t *svc)
if (!svc_is_sysv(svc)) { if (!svc_is_sysv(svc)) {
for (i = 0; i < MAX_NUM_SVC_ARGS; i++) { for (i = 0; i < MAX_NUM_SVC_ARGS; i++) {
if (strlen(svc->args[i]) == 0) if (strlen(svc->args[i]) == 0)
continue; break;
args[i] = svc->args[i]; args[i] = svc->args[i];
} }
} else { } else {
@@ -400,7 +394,7 @@ static int service_start(svc_t *svc)
} else if (debug) { } else if (debug) {
char buf[CMD_SIZE] = ""; char buf[CMD_SIZE] = "";
for (i = 1; i < MAX_NUM_SVC_ARGS; i++) { for (i = 0; i < MAX_NUM_SVC_ARGS; i++) {
if (strlen(svc->args[i]) == 0) if (strlen(svc->args[i]) == 0)
continue; continue;
if (buf[0]) if (buf[0])
+2
View File
@@ -26,6 +26,7 @@
#include <signal.h> #include <signal.h>
#include <termios.h> #include <termios.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/prctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <lite/lite.h> #include <lite/lite.h>
@@ -474,6 +475,7 @@ int tty_fallback(pid_t lost)
setsid(); setsid();
ioctl(STDIN_FILENO, TIOCSCTTY, 1); ioctl(STDIN_FILENO, TIOCSCTTY, 1);
prctl(PR_SET_NAME, "finitsh", 0, 0, 0);
_exit(execl(_PATH_BSHELL, _PATH_BSHELL, NULL)); _exit(execl(_PATH_BSHELL, _PATH_BSHELL, NULL));
#else #else
(void)lost; (void)lost;
+5
View File
@@ -28,9 +28,14 @@
#ifdef HAVE_TERMIOS_H /* for screen_width() */ #ifdef HAVE_TERMIOS_H /* for screen_width() */
#include <poll.h> #include <poll.h>
#include <stdio.h> #include <stdio.h>
#include <sys/reboot.h>
#include <termios.h> #include <termios.h>
#endif #endif
#ifndef RB_SW_SUSPEND /* Since Linux 2.5.18, but not in GLIBC until 2.19 */
#define RB_SW_SUSPEND 0xd000fce2
#endif
extern int screen_rows; extern int screen_rows;
extern int screen_cols; extern int screen_cols;
extern char *prognm; extern char *prognm;