Commit Graph
1110 Commits
Author SHA1 Message Date
Joachim Wiberg 54dd726d16 Follow-up to bd9bb92, missing API change
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-17 00:01:10 +02:00
Joachim Wiberg bd9bb92ca0 Replace reboot(RB_SW_SUSPEND) w/ internal /sys/power/state API
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-16 23:59:32 +02:00
Joachim Wiberg 01cb088e51 Fix initctl debug toggle regression introduced in ae09272b
Calling `initctl debug` is supposed to toggle Finit debug messages on
the boot console.  This broke in ae09272b when improving support for
running Finit in containers.

Part of this change is a slight refactor of who calles log_init() when
starting up, and when to call ttinit().  We now call ttinit() every time
we toggle debug.

Also, toggling back to normal logging had a bug.  The new default log
level for Finit is LOG_INFO, but toggling back set it to LOG_NOTICE.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 11:55:17 +02:00
Joachim Wiberg 3d91112e0d Rename finit command line option finit.show_status -> finit.status
Usability.

Remains backwards compatible for now, but finit.show_status is as of 4.3
deprecated.  Likely to be removed int 5.0

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 10:55:48 +02:00
Joachim Wiberg 16aef80682 Silence Coverity Scan NULL ptr deref warning
Explicitly set arg to non-NULL value.  The call to conf() later will
steer up the value to point to the full path of the user's finit.conf

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 10:02:11 +02:00
Joachim Wiberg fd34914936 Fix unchecked return value, found by Coverity Scan
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 09:57:04 +02:00
Joachim Wiberg 21a890a04e Fix NULL ptr deref, introduced in 8e2d0060 for issue #189
Found by Coverity Scan.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 09:53:58 +02:00
Joachim Wiberg 7f5f7e8ef6 Fix resource leak in shutdown, found by Coverity Scan
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 09:48:42 +02:00
Joachim Wiberg ff937576ea Fix possible NULL deref in run() error handling
When we added support for using popen() to run(), to log the output, we
forgot to update the error handling path.  Found by Coverity Scan.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 09:45:41 +02:00
Joachim Wiberg 6a21ac55ad Enable more printf format attributes
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 08:32:18 +02:00
Joachim Wiberg a0820c7f20 Fix printf format specifier and formatting issues in use of log fns
Almost all related to and (less likely) possible causes of issue #236.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 00:26:14 +02:00
Joachim Wiberg 6d58c9ed0b Enable printf format attributes to logit() and flog()
We don't want repeats of issue #236, so let's start by tracking down any
bugs hidden in logging functions.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 00:17:15 +02:00
Joachim Wiberg bc2ffbc767 Fix printf style function missing argument to %s
This is very likely the root cause of issue #236, let's see.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-14 00:10:30 +02:00
Joachim Wiberg 4c1fc2410e Show progress output also for unmount
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 22:41:12 +02:00
Joachim Wiberg 70e1aa889a Ignore errors from the first two remount,ro they are just decoys
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 22:28:10 +02:00
Joachim Wiberg c8c5e4e88a Revert "Simplify shutdown, no need for workarounds to remount / read-only"
This reverts commit 7c42986800 since we
seem to still need this werid thing on some systems.  The key seems
to use 'any' device at remount[1].  Anyway, the key issue before was
the noisy output, but like Debian we'll just redirect to /dev/null.

[1]: https://bugs.launchpad.net/ubuntu/+source/util-linux/+bug/29187

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 22:22:18 +02:00
Joachim Wiberg 86c1398778 Run swapoff interactively, let user know what's happening.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 22:21:52 +02:00
Joachim Wiberg b37fa2b417 Silence process killer at shutdown, too noisy on regular systems
On regular desktop systems, like Debian, the dbus-daemon forks off a
dbus-launch process to start up things like your desktop for you.  This
process is not known to Finit and lingers in the background since it's
been reparented to init, and to top things off it seems to ignore any
SIGTERMs send to it.

Obviously this makes a shutdown on such systems very noisy, so this
patch changes the print() to a _d() so anyone debugging a system can see
it with `-- finit.debug` on the kernel command line, or `initctl debug`.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 22:16:57 +02:00
Joachim Wiberg a91e197553 Avoid looping forever trying to unmount busy tmpfs
This split unmount is a bit crude, unfortunately.  A user may have set
up a bind or overlayfs mount on top of a tmpfs -- so unmounting tmpfs
first will then always result in EBUSY.

Avoid logging busy errors and fake OK.  We'll catch it later in the
second stage unmount.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 21:57:14 +02:00
Joachim Wiberg 5adb92ca1b Catch and print any errors when unmounting filesystems at reboot
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 21:45:37 +02:00
Joachim Wiberg cfc99c4e8b Follow-up to ef9e9ec, run ifdown also in background, with --force
Turns out that on Debian `ifdown -a` can block for quite a while at
shutdown/reboot.  So we'd like to use the --force option.  However,
the BusyBox ifdown tool doesn't support --force, only -f, which in
turn the regular ifdown tool doesn't support.

Regardless, we can allow network shutdown to run in the background
like bring-up, at reboot we want to reboot quickly and don't care
so much, and on runlevel change to single-user mode we can allow
for some lagging behind in the background.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 21:45:37 +02:00
Joachim Wiberg 3e51ed1c55 Run swapon interactively, let user know what's happening.
Let's try this, should be OK for all types of systems.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 21:45:37 +02:00
Joachim Wiberg b5df0a6274 Fix swapoff at shutdown, does not support -e flag
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-12 21:33:54 +02:00
Joachim Wiberg c24855d4d5 keventd: add support for toggling debug messages w/ SIGUSR1
Useful to debug system events after the system has started up and you
don't want to rebuild Finit to add the `-d` command line flag.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-11 22:38:35 +02:00
Joachim Wiberg 30c189b116 initctl: add helpful hint when showing/editing built-in services
Built-in services, like keventd, do not have a .conf file to edit.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-11 20:51:46 +02:00
Joachim Wiberg 1502838a0d Fix regression in _pe() log macro, introduced in 685f332
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-10 12:39:20 +02:00
Joachim Wiberg a39d958d25 Follow-up to 7dc7f9a, protect against stalling on non-existing pids
A forking service, e.g. a SysV init script, that returns is no longer
running, even though the monitored service may still be starting.  We
must mark the svc->pid as terminated until we know more -- otherwise
we may stall on a shutdown at that exact point.

Also, when shutting down, and stopping all services, ensure we do not
start the carousel for non-existing PIDs.  This might also cause our
stalling at shutdown.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-08 07:27:42 +02:00
Joachim Wiberg c926160853 Issue #223: add type:forking service option, with updated docs
- Add `type:forking` service option to trigger guessing pidfile to
   watch for, instead of `pid:!foo` option, which is not intuitive.
   This option may likely also survive into the new file format :)
 - Update docs and add examples
 - Update start-stop-serv.sh test case with this new variant

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-04 19:13:28 +02:00
Joachim Wiberg 462d5b22b4 Fix parse bug, always set service's respawn flag
Before this fix, a service declared with respawn could not be changed at
runtime to remove the respawn flag.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-04 18:06:35 +02:00
Joachim Wiberg b1bfab08f3 Coding style, reverse Christmas tree ordering
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-04 08:26:13 +02:00
Joachim Wiberg 7c42986800 Simplify shutdown, no need for workarounds to remount / read-only
Calling sync is not needed, remount does this for us.

Remount with 'dummydev' causes warnings and is not needed.  It appears
sysvinit used this to try and fix a sparc related bug.  Instead, use the
rootfs keyword to ensure we don't accidentally remount a bind mounted /.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-02 11:09:29 +02:00
Joachim Wiberg 20290a4166 Clear any outstanding restart timer when svc reaches running state
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-27 16:32:42 +02:00
Joachim Wiberg ba77e4f217 Ensure we clear the 'starting' flag when cleaning up an svc_t
Prevent crashing/restarting tasks from winding up in the wrong state.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-27 16:31:19 +02:00
Joachim Wiberg e7b18feca5 Don't block PID 1 waiting for start/stop scripts, fork and forget
No point waiting for SysV start/stop scripts to finish, PID 1 should not
risk getting blocked forever by broken scripts. Instead we fork them off
and forget about them until they terminate. If they are buggy and don't
finish, it's the problem of the admin.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-27 16:27:46 +02:00
Joachim Wiberg 10577f3e48 Allow matching of svc_t and pid file, prefer PID over file lookup
When a SysV init script starts a daemon, Finit knows nothing of the PID
it should monitor.  The PID is written, by start-stop-daemon or the
daemon itself, to the PID file.  Finit monitors for new PID files and
can match the PID in such files with an svc_t.

For the regular use-case, we prefer first looking up the matching svc_t
based on the PID -- assuming we start and monitor the service.  As a
fallback we resort to mathching the svc_t's declared PID file with the
new file we just discovered.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-25 06:53:59 +01:00
Joachim Wiberg 07f7caef0d Allow setting custom pid: file for SysV init scripts
We want to find the PID of the daemon the init script starts, so we need
a way to declare this to Finit.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-25 06:52:03 +01:00
Joachim Wiberg 093b3ba467 Fix problem with restarting stopped SysV init scripts
SysV init scripts should not go to "done" state but "halted" so we can
do: `initctl stop foo; initctl start foo`, like we do for our regular
monitored services.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-25 06:50:40 +01:00
Joachim Wiberg c63279630b Fix starting and stopping of SysV init scripts
Due to lack of tests the SysV start/stop script stopped working.
Unclear when.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-25 06:45:55 +01:00
Joachim Wiberg 2002157032 Simplify shutdown in container, call _exit()
Calling _exit(2) lets libc do most of the cleanup we want when shutting
down while in a container.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-24 21:56:47 +01:00
Joachim Wiberg e01836c506 Clarify debug messages for respawning daemons/ttys
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-23 06:36:46 +01:00
Joachim Wiberg 685f332c6e logit(): avoid extra \n when logging to stderr
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-23 06:33:21 +01:00
Joachim Wiberg ae09272bcc logit(): detect running in container to use stderr for logging
When running in a container we still want to use any syslog daemon
available for our logging needs.  However, the time between the first
logit() in Finit and any such daemon having started can be long.  In a
normal (non-containerized) setup we log to the kernel ring buffer, but
that's not available in a container scenario.  At least not for
unprivileged containers.  So we need to detect all these cases and be
prepared to fall back to log to the console, either using these LOG_CONS
flag to openlog(), or by simply calling vfprintf() to stderr.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-23 06:29:40 +01:00
Joachim Wiberg d16f7e0c88 test: help finit know its under test
This trick can also be used by others who want to run Finit in an
unshare.  Set the container environment variable to 'unshare',
like lxc and docker do for their products.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-23 06:27:33 +01:00
Joachim Wiberg 063d645d95 in_container(): cache positive results
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-23 06:25:43 +01:00
Joachim Wiberg f87e9d8880 Follow-up to 6fa3aa4, log cmd not args[0] which may be NULL
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-22 22:30:59 +01:00
Joachim Wiberg e8099f1bfa The kill(2) syscall returns ENOENT, not ESRCH on missing PIDs
Turns out the kill(2) syscall returns ENOENT, not ESRCH, in our test
suite.  Don't know why, the man page never mentions ENOENT, only the
ESRCH code.  Let's check for both, either way ithe PID is not there.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-22 07:22:15 +01:00
Joachim Wiberg 7dc7f9a968 Fix #227: set svc state late when stopping services, nay be dead
A service may have unexpectedly died, and we never got the signal, so
when stopping services we must set the new state after we've tried to
stop the service.  Otherwise the svc_set_state() function starts a
background timer for the SIGKILL job, which may block a reboot.

The kill() syscall tells us if the service was there or not, if not we
must clean up and go to HALTED state.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-22 07:18:37 +01:00
Joachim Wiberg 996ab8c6c6 Minor, fix local var shadowing outer var
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-21 22:04:09 +01:00
Joachim Wiberg bda3cdd3ad Increase inotify socket receive buffer size
At startup (and reconf) of systems with lots of services there is a risk
of losing inotify events, e.g., PID file creation/delete events.  This
patch increase the receive buffer (doubles it).

On Linux the getsockopt() for SO_RCVBUF returns double the set size, due
to housekeeping in the kernel.  So we don't have to do any adjustments
when setting it.

Issue #226

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-20 15:33:56 +01:00
Joachim Wiberg 57329832d0 initctl: skip cgroup in status output pid_cgruop() fails
In some (error) cases the PID known to Finit may no longer exist, or may
not have been added to a cgroup (yet).  Handle this case by skipping the
output of cgroup info in such conditions.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-07 15:48:15 +01:00