mirror of
https://github.com/troglobit/finit.git
synced 2026-11-22 20:01:46 +00:00
Merge branch 'master' into wip-tests
This commit is contained in:
commit
765e1eb640
17 files changed
+303
-157
No files matched your search
@@ -12,6 +12,11 @@ to service conditions. There are other significant changes as well, so
|
||||
make sure to read the whole changelog when upgrading.
|
||||
|
||||
### 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 𝓜𝓸𝓭𝓮𝓻𝓷
|
||||
* Removed built-in inetd super server. If you need this functionality,
|
||||
use an external inetd, like xinetd, instead. A pull request for a
|
||||
|
||||
+14
-1
@@ -4,9 +4,22 @@ Tips & Tricks with the kernel cmdline
|
||||
This document summarizes the different boot parameters that can be
|
||||
passed on the Linux kernel command line. Not limited to Finit.
|
||||
|
||||
The `bool` setting is one of `on, off, true false, 1, 0`.
|
||||
|
||||
|
||||
* `debug`__
|
||||
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`
|
||||
Bypass system default init and tell kernel to start a shell. Note,
|
||||
|
||||
+28
-29
@@ -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.
|
||||
For example:
|
||||
|
||||
- *"do not start this service until another process has started"*, or
|
||||
- *"do not starty until basic network access is available"*
|
||||
- *"wait for service A to start before starting service B"*, or
|
||||
- *"wait for basic network access to be available"*
|
||||
|
||||
Conditions are similar in syntax to declaring runlevels per service.
|
||||
They are specified within angle brackets `<>` and can be applied to any
|
||||
@@ -30,14 +30,12 @@ service to run.
|
||||
|
||||
### 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
|
||||
both the `pid/sbin/setupd` *and* `pid/sbin/zebra` conditions are
|
||||
satisfied. An `svc` condition is satisfied by the corresponding
|
||||
service's PID file being created.
|
||||
both the `pid/setupd` *and* `pid/zebra` conditions are satisfied. A
|
||||
`pid/` condition is satisfied by the corresponding service's PID file
|
||||
being created.
|
||||
|
||||
**NOTE:** Conditions also stop services when a condition is no longer
|
||||
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
|
||||
unchanged) state of it.
|
||||
|
||||
> **Note:** For `pid/` conditions it is expected that services "touch"
|
||||
> or recreate their PID file on `SIGHUP`.
|
||||
|
||||
|
||||
Built-in Conditions
|
||||
-------------------
|
||||
|
||||
Finit is distributed with a `pidfile` and `netlink` plugin. If enabled,
|
||||
the `pidfile` plugin watches `/var/run/` for PID files created by the
|
||||
monitored services, and sets a corresponding condition in the `pid/`
|
||||
namespace. Similarily, the `netlink` plugin provides basic conditions
|
||||
for when an interface is brought up/down and when a default route
|
||||
(gateway) is set, in the `net/` namespace.
|
||||
Finit comes with a `pidfile` and `netlink` plugin. When enabled, the
|
||||
`pidfile` plugin watches `/var/run/` (recursively) for PID files created
|
||||
by the monitored services, and sets a corresponding condition in the
|
||||
`pid/` namespace. Similarily, the `netlink` plugin provides basic
|
||||
conditions for when an interface is brought up/down and when a default
|
||||
route (gateway) is set, in the `net/` namespace.
|
||||
|
||||
With the example listed above, finit does not start the `/sbin/netd`
|
||||
daemon until `setupd` and `zebra` has started *and* created their PID
|
||||
files. Which they do when they have completed their initial set up and
|
||||
are ready to receive signals.
|
||||
|
||||
The full path to the dependency is needed by finit to match the PID file
|
||||
to a monitored process. Finit expects monitored services to touch their
|
||||
PID files, i.e. update the mtime, when they reload their configuration
|
||||
files after a `SIGHUP`. Some services do not support `SIGHUP` and are
|
||||
instead restarted, which is a crude but effective way to have the PID
|
||||
file touched (re-created).
|
||||
Finit expects monitored services to touch their PID files, i.e. update
|
||||
the mtime, when they reload their configuration files after a `SIGHUP`.
|
||||
Some services do not support `SIGHUP` and are instead restarted, which
|
||||
is a crude but effective way to have the PID file touched (re-created).
|
||||
|
||||
Built-in conditions:
|
||||
|
||||
- `pid/<PATH>`
|
||||
- `pid/<SERVICE>`
|
||||
- `net/route/default`
|
||||
- `net/<IFNAME>/exist`
|
||||
- `net/<IFNAME>/up`
|
||||
@@ -107,9 +106,9 @@ the daemon and the empty string.
|
||||
| /usr/bin/dbus-daemon | pid/dbus-daemon |
|
||||
| :222 dropbear -p 222 | pid/dropbear:222 |
|
||||
|
||||
The condition is asserted when `pidfile.so` receives an inotify event for a file
|
||||
matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`, which contains the
|
||||
PID of the service Finit has started.
|
||||
The condition is asserted when `pidfile.so` receives an inotify event
|
||||
for a file matching `/run/*.pid`, `/run/**/*.pid`, or `/run/**/pid`,
|
||||
which contains the PID of the service Finit has started.
|
||||
|
||||
When Finit configuration files are changed and the `initctl reload`
|
||||
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
|
||||
~ # initctl cond show
|
||||
PID Service Status Condition (+ on, ~ flux, - off)
|
||||
===============================================================================
|
||||
1419 /sbin/netd on <+pid/sbin/setupd,+pid/sbin/zebra>
|
||||
0 /sbin/udhcpc off <-net/vlan1/exist>
|
||||
PID IDENT STATUS CONDITION (+ ON, ~ FLUX, - OFF)
|
||||
=======================================================================
|
||||
1419 /sbin/netd on <+pid/setupd,+pid/zebra>
|
||||
0 /sbin/udhcpc off <-net/vlan1/exist>
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
@@ -182,6 +182,8 @@ optional arguments and description.
|
||||
`run` commands are guaranteed to be completed before running the next
|
||||
command. Highly useful if true serialization is needed.
|
||||
|
||||
> `<COND>` is described in the [Services](#services) section.
|
||||
|
||||
|
||||
### One-shot Commands (parallel)
|
||||
|
||||
@@ -194,6 +196,8 @@ redirects can be freely used:
|
||||
|
||||
task [s] echo "foo" | cat >/tmp/bar
|
||||
|
||||
> `<COND>` is described in the [Services](#services) section.
|
||||
|
||||
|
||||
### 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
|
||||
by declaring the PID file to look for: `pid:!/path/to/pidfile.pid`.
|
||||
|
||||
> `<COND>` is described in the [Services](#services) section.
|
||||
|
||||
|
||||
### Services
|
||||
|
||||
|
||||
+44
-25
@@ -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
|
||||
have been adopted:
|
||||
|
||||
* `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.
|
||||
`SIGHUP`
|
||||
--------
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
`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.
|
||||
* `SIGTERM`
|
||||
Like `SIGUSR1`, but tell kernel to reboot the system when done.
|
||||
SysV init N/A, systemd dumps its internal state to log.
|
||||
|
||||
`SIGTERM`
|
||||
---------
|
||||
|
||||
Like `SIGUSR1`, but tell kernel to reboot the system when done.
|
||||
|
||||
SysV init N/A, systemd rexecutes itself.
|
||||
* `SIGINT`
|
||||
Sent from kernel when the CTRL-ALT-DEL key combo is pressed.
|
||||
SysV init and systemd default to reboot with `shutdown -r`.
|
||||
Finit currently forwards this to `SIGTERM`.
|
||||
* `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`.
|
||||
SysV init N/A, systemd rexecutes itself.
|
||||
|
||||
`SIGINT`
|
||||
--------
|
||||
|
||||
Sent from kernel when the CTRL-ALT-DEL key combo is pressed. SysV init
|
||||
and systemd default to reboot with `shutdown -r`.
|
||||
|
||||
Finit currently forwards this to `SIGTERM`.
|
||||
|
||||
`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
@@ -43,19 +43,19 @@ static void setup(void *arg)
|
||||
path = which("/lib/systemd/systemd-udevd");
|
||||
if (path) {
|
||||
/* 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);
|
||||
if (service_register(SVC_TYPE_SERVICE, cmd, global_rlimit, NULL)) {
|
||||
_pe("Failed registering %s", path);
|
||||
} else {
|
||||
snprintf(cmd, sizeof(cmd), ":1 [S] <svc%s> "
|
||||
snprintf(cmd, sizeof(cmd), ":1 [S] log <pid/udevd> "
|
||||
"udevadm trigger -c add -t devices "
|
||||
"-- Requesting device events", path);
|
||||
"-- Requesting device events");
|
||||
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 "
|
||||
"-- Requesting subsystem events", path);
|
||||
"-- Requesting subsystem events");
|
||||
service_register(SVC_TYPE_RUN, cmd, global_rlimit, NULL);
|
||||
}
|
||||
|
||||
|
||||
+37
-49
@@ -8,16 +8,16 @@ else
|
||||
AM_LDFLAGS = -export-dynamic
|
||||
endif
|
||||
|
||||
sbin_PROGRAMS = finit initctl reboot
|
||||
sbin_PROGRAMS = finit initctl
|
||||
pkglibexec_SCRIPTS = cgreaper.sh
|
||||
if WATCHDOGD
|
||||
pkglibexec_PROGRAMS = watchdogd
|
||||
endif
|
||||
|
||||
if LOGIT
|
||||
bin_PROGRAMS = logit
|
||||
logit_SOURCES = logit.c
|
||||
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
||||
pkglibexec_PROGRAMS = logit
|
||||
logit_SOURCES = logit.c
|
||||
logit_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
||||
endif
|
||||
|
||||
finit_SOURCES = api.c cgroup.c cgroup.h \
|
||||
@@ -49,19 +49,15 @@ else
|
||||
finit_LDADD += -ldl
|
||||
endif
|
||||
|
||||
initctl_SOURCES = initctl.c client.c client.h \
|
||||
serv.c serv.h svc.h \
|
||||
initctl_SOURCES = initctl.c client.c client.h \
|
||||
reboot.c serv.c serv.h svc.h \
|
||||
cond.c cond.h util.c util.h
|
||||
initctl_CFLAGS = -W -Wall -Wextra -Wno-unused-parameter -std=gnu99
|
||||
initctl_CFLAGS += $(lite_CFLAGS)
|
||||
initctl_LDADD = $(lite_LIBS)
|
||||
|
||||
reboot_SOURCES = reboot.c util.c util.h
|
||||
reboot_CFLAGS = $(lite_CFLAGS)
|
||||
reboot_LDADD = $(lite_LIBS)
|
||||
|
||||
INIT_LNKS = init
|
||||
REBOOT_LNKS = shutdown halt poweroff suspend
|
||||
REBOOT_LNKS = reboot shutdown halt poweroff suspend
|
||||
|
||||
if WATCHDOGD
|
||||
watchdogd_SOURCES = watchdog.c watchdog.h
|
||||
@@ -72,46 +68,38 @@ endif
|
||||
# Hook in install to add finit and reboot symlink(s)
|
||||
install-exec-hook:
|
||||
@$(INSTALL_DATA) $(srcdir)/rescue.conf $(DESTDIR)$(pkglibdir)
|
||||
@for file in $(INIT_LNKS); do \
|
||||
lnk=$(DESTDIR)$(sbindir)/$$file; \
|
||||
if [ -e $$lnk ]; then \
|
||||
echo "Skipping $$lnk exists."; \
|
||||
else \
|
||||
$(LN_S) finit $$lnk; \
|
||||
fi; \
|
||||
@for file in $(INIT_LNKS); do \
|
||||
lnk=$(DESTDIR)$(sbindir)/$$file; \
|
||||
if [ -e $$lnk ]; then \
|
||||
echo "Skipping $$lnk exists."; \
|
||||
else \
|
||||
$(LN_S) finit $$lnk; \
|
||||
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
|
||||
@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:
|
||||
@$(RM) $(DESTDIR)$(pkglibdir)/rescue.conf
|
||||
@for file in $(INIT_LNKS); do \
|
||||
lnk=`readlink $(DESTDIR)$(sbindir)/$$file`; \
|
||||
if [ "$$lnk" != "finit" ]; then \
|
||||
echo "Skipping non-finit $$file."; \
|
||||
continue; \
|
||||
fi; \
|
||||
$(RM) $(DESTDIR)$(sbindir)/$$file; \
|
||||
@for file in $(INIT_LNKS); do \
|
||||
lnk=$(DESTDIR)$(sbindir)/$$file; \
|
||||
dst=`readlink $$lnk`; \
|
||||
if [ "$$dst" != "finit" ]; then \
|
||||
continue; \
|
||||
fi; \
|
||||
$(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
|
||||
@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
|
||||
@@ -182,6 +182,49 @@ static svc_t *do_find_byc(char *buf, size_t len)
|
||||
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 {
|
||||
char *event;
|
||||
void (*cb)(void);
|
||||
@@ -302,6 +345,13 @@ static void api_cb(uev_t *w, void *arg, int events)
|
||||
rq.sleeptime = prevlevel;
|
||||
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:
|
||||
_d("Client failed reading ACK");
|
||||
goto leave;
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ BASE=/sys/fs/cgroup/finit
|
||||
PROC=`basename $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
|
||||
|
||||
exit 0
|
||||
+4
-1
@@ -80,7 +80,10 @@ int client_send(struct init_request *rq, ssize_t len)
|
||||
if (read(sd, rq, len) != len)
|
||||
goto error;
|
||||
|
||||
result = 0;
|
||||
if (rq->cmd == INIT_CMD_NACK)
|
||||
result = 1;
|
||||
else
|
||||
result = 0;
|
||||
goto exit;
|
||||
error:
|
||||
perror("Failed communicating with finit");
|
||||
|
||||
+4
-1
@@ -77,11 +77,14 @@ static void hide_args(int argc, char *argv[])
|
||||
|
||||
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"))
|
||||
return 1;
|
||||
if (string_compare(ptr, "false") || string_compare(ptr, "off") || string_compare(ptr, "0"))
|
||||
return 0;
|
||||
|
||||
fallback:
|
||||
return default_value;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@
|
||||
#define _PATH_VARRUN "/var/run/"
|
||||
#endif
|
||||
|
||||
#define LOGIT_PATH FINIT_LIBPATH_ "/logit"
|
||||
|
||||
#define CMD_SIZE 256
|
||||
#define LINE_SIZE 1024
|
||||
#define BUF_SIZE 4096
|
||||
@@ -72,6 +74,10 @@
|
||||
#define INIT_CMD_UNUSED2 14 /* Unused, was INIT_CMD_QUERY_INETD */
|
||||
#define INIT_CMD_UNUSED1 15 /* Unused, was INIT_CMD_EMIT */
|
||||
#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_SVC_ITER 129
|
||||
#define INIT_CMD_SVC_QUERY 130
|
||||
|
||||
+44
-8
@@ -54,6 +54,9 @@ int verbose = 0;
|
||||
int runlevel = 0;
|
||||
int iw, pw;
|
||||
|
||||
extern int reboot_main(int argc, char *argv[]);
|
||||
|
||||
|
||||
/* figure ut width of IDENT and PID columns */
|
||||
static void col_widths(void)
|
||||
{
|
||||
@@ -349,17 +352,30 @@ static int do_cond(char *cmd)
|
||||
return do_cond_show(NULL);
|
||||
}
|
||||
|
||||
static int do_signal(int signo, const char *msg)
|
||||
static int do_cmd(int cmd)
|
||||
{
|
||||
if (kill(1, signo))
|
||||
err(1, "Failed signalling init to %s", msg);
|
||||
struct init_request rq = {
|
||||
.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;
|
||||
}
|
||||
|
||||
static int do_halt (char *arg) { return do_signal(SIGUSR1, "halt"); }
|
||||
static int do_poweroff(char *arg) { return do_signal(SIGUSR2, "power off"); }
|
||||
static int do_reboot (char *arg) { return do_signal(SIGTERM, "reboot"); }
|
||||
int do_reboot (char *arg) { return do_cmd(INIT_CMD_REBOOT); }
|
||||
int do_halt (char *arg) { return do_cmd(INIT_CMD_HALT); }
|
||||
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)
|
||||
{
|
||||
@@ -605,6 +621,22 @@ static int show_cgroup(char *arg)
|
||||
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)
|
||||
{
|
||||
fprintf(stderr,
|
||||
@@ -644,7 +676,8 @@ static int usage(int rc)
|
||||
" runlevel [0-9] Show or set runlevel: 0 halt, 6 reboot\n"
|
||||
" reboot Reboot 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())
|
||||
fprintf(stderr,
|
||||
@@ -693,6 +726,7 @@ int main(int argc, char *argv[])
|
||||
{ "reboot", do_reboot },
|
||||
{ "halt", do_halt },
|
||||
{ "poweroff", do_poweroff },
|
||||
{ "suspend", do_suspend },
|
||||
|
||||
{ "utmp", do_utmp },
|
||||
{ NULL, NULL }
|
||||
@@ -706,7 +740,9 @@ int main(int argc, char *argv[])
|
||||
{ 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) {
|
||||
switch(c) {
|
||||
case 'b':
|
||||
|
||||
+34
-17
@@ -22,18 +22,14 @@
|
||||
*/
|
||||
|
||||
#include <err.h>
|
||||
#include <errno.h>
|
||||
#include <getopt.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/reboot.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 {
|
||||
CMD_UNKNOWN = -1,
|
||||
CMD_REBOOT = 0,
|
||||
@@ -45,6 +41,13 @@ typedef enum {
|
||||
static cmd_t cmd = CMD_UNKNOWN;
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
(void)signo; /* NOP */
|
||||
@@ -105,7 +118,7 @@ static int usage(int rc)
|
||||
return rc;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int reboot_main(int argc, char *argv[])
|
||||
{
|
||||
int c, force = 0;
|
||||
struct option long_options[] = {
|
||||
@@ -118,7 +131,7 @@ int main(int argc, char *argv[])
|
||||
};
|
||||
|
||||
/* Initial command taken from program name */
|
||||
transform(progname(argv[0]));
|
||||
transform(prognm);
|
||||
|
||||
while ((c = getopt_long(argc, argv, "h?fHPpr", long_options, NULL)) != EOF) {
|
||||
switch(c) {
|
||||
@@ -170,6 +183,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
case CMD_SUSPEND:
|
||||
c = reboot(RB_SW_SUSPEND);
|
||||
if (c && errno == EINVAL)
|
||||
errx(1, "Kernel does not support suspend.");
|
||||
break;
|
||||
|
||||
case CMD_UNKNOWN:
|
||||
@@ -178,22 +193,25 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
|
||||
if (c)
|
||||
warn("Failed forced %s", msg);
|
||||
else
|
||||
return 0;
|
||||
err(1, "Failed forced %s", msg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
switch (cmd) {
|
||||
case CMD_REBOOT:
|
||||
c = kill(1, SIGTERM);
|
||||
if (do_reboot(NULL))
|
||||
do_kill(SIGTERM, msg);
|
||||
break;
|
||||
|
||||
case CMD_HALT:
|
||||
c = kill(1, SIGUSR1);
|
||||
if (do_halt(NULL))
|
||||
do_kill(SIGUSR1, msg);
|
||||
break;
|
||||
|
||||
case CMD_POWEROFF:
|
||||
c = kill(1, SIGUSR2);
|
||||
if (do_poweroff(NULL))
|
||||
do_kill(SIGUSR2, msg);
|
||||
break;
|
||||
|
||||
case CMD_SUSPEND:
|
||||
@@ -201,16 +219,15 @@ int main(int argc, char *argv[])
|
||||
* Only initctl supports suspend, we avoid adding
|
||||
* another signal to finit for compat reasons.
|
||||
*/
|
||||
do_suspend(NULL);
|
||||
break;
|
||||
|
||||
|
||||
case CMD_UNKNOWN:
|
||||
errx(1, "Invalid command");
|
||||
break;
|
||||
}
|
||||
|
||||
if (c)
|
||||
err(1, "Failed signalling init to %s", msg);
|
||||
do_sleep(5);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+14
-20
@@ -27,6 +27,7 @@
|
||||
#include <ctype.h> /* isblank() */
|
||||
#include <sched.h> /* sched_yield() */
|
||||
#include <string.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/wait.h>
|
||||
#include <net/if.h>
|
||||
@@ -149,7 +150,6 @@ static int fredirect(const char *file)
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifndef LOGIT_ENABLED
|
||||
/*
|
||||
* Fallback in case we don't even have logger on the system.
|
||||
* 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;
|
||||
char buf[256];
|
||||
|
||||
prctl(PR_SET_NAME, "finitlog", 0, 0, 0);
|
||||
openlog(ident, LOG_NOWAIT | LOG_PID, facility);
|
||||
while ((fgets(buf, sizeof(buf), stdin)))
|
||||
syslog(level, "%s", buf);
|
||||
|
||||
closelog();
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Redirect output to syslog using the command line logit tool
|
||||
@@ -206,19 +206,20 @@ static int lredirect(svc_t *svc)
|
||||
/* Reset signals */
|
||||
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] == '/') {
|
||||
#ifdef LOGIT_ENABLED
|
||||
char sz[20], num[3];
|
||||
|
||||
snprintf(sz, sizeof(sz), "%d", logfile_size_max);
|
||||
snprintf(num, sizeof(num), "%d", logfile_count_max);
|
||||
|
||||
execlp("logit", "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL);
|
||||
#else
|
||||
logit(LOG_INFO, "logit disabled, logging %s to syslog instead", svc->name);
|
||||
fallback_logger(tag, prio);
|
||||
_exit(0);
|
||||
#endif
|
||||
execlp(LOGIT_PATH, "logit", "-f", svc->log.file, "-n", sz, "-r", num, NULL);
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
if (svc->log.ident[0])
|
||||
@@ -226,15 +227,8 @@ static int lredirect(svc_t *svc)
|
||||
if (svc->log.prio[0])
|
||||
prio = svc->log.prio;
|
||||
|
||||
#ifdef LOGIT_ENABLED
|
||||
execlp("logit", "logit", "-t", tag, "-p", prio, NULL);
|
||||
#else
|
||||
if (whichp("logger"))
|
||||
execlp("logger", "logger", "-t", tag, "-p", prio, NULL);
|
||||
|
||||
fallback_logger(tag, prio);
|
||||
#endif
|
||||
_exit(0);
|
||||
execlp(LOGIT_PATH, "logit", "-t", tag, "-p", prio, NULL);
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
@@ -364,7 +358,7 @@ static int service_start(svc_t *svc)
|
||||
if (!svc_is_sysv(svc)) {
|
||||
for (i = 0; i < MAX_NUM_SVC_ARGS; i++) {
|
||||
if (strlen(svc->args[i]) == 0)
|
||||
continue;
|
||||
break;
|
||||
args[i] = svc->args[i];
|
||||
}
|
||||
} else {
|
||||
@@ -400,7 +394,7 @@ static int service_start(svc_t *svc)
|
||||
} else if (debug) {
|
||||
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)
|
||||
continue;
|
||||
if (buf[0])
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <signal.h>
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <lite/lite.h>
|
||||
@@ -474,6 +475,7 @@ int tty_fallback(pid_t lost)
|
||||
setsid();
|
||||
ioctl(STDIN_FILENO, TIOCSCTTY, 1);
|
||||
|
||||
prctl(PR_SET_NAME, "finitsh", 0, 0, 0);
|
||||
_exit(execl(_PATH_BSHELL, _PATH_BSHELL, NULL));
|
||||
#else
|
||||
(void)lost;
|
||||
|
||||
@@ -28,9 +28,14 @@
|
||||
#ifdef HAVE_TERMIOS_H /* for screen_width() */
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/reboot.h>
|
||||
#include <termios.h>
|
||||
#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_cols;
|
||||
extern char *prognm;
|
||||
|
||||
Reference in new issue
Block a user