Commit Graph
1140 Commits
Author SHA1 Message Date
Joachim Wiberg 38db52aa4e Add support for setting environment variables from finit.conf
Finit can now track and set environment variables from finit.conf.  The
syntax is pretty much what you'd expect:

    foo=bar
    baz="qux"

On reload of .conf files, all tracked environment variables are cleared
so if `foo=bar` is removed from finit.conf, or any finit.d/*.conf file,
it will no longer be used by Finit or any new (!) started run/tasks or
services.

The only variables reset to sane defaults on .conf reload are:

    PATH=_PATH_STDPATH
    SHELL=_PATH_BSHELL
    LOGNAME=root
    USER=root

It is entirely possible to override these as well from the .conf files,
but be careful.  Changing SHELL changes the behavior of system() and a
lot of other commands as well.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-05-02 00:02:44 +02:00
Joachim Wiberg d6c55282ef Fix #253: use reentrant getmntent_r() API
When iterating over the system fstab file to call fsck, Finit calls the
helper function ismnt(), which opens /proc/mounts to make sure mounted
file systems are not fsck'ed.  Both the main function and ismnt() used
the same non-reentrant getmntent() API which caused ismnt() to set the
fstab pointer for the first out of whack.

This change replaces getmntent() in the two critical functions with the
getmntent_r() API instead.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-05-01 21:43:13 +02:00
Joachim Wiberg 1015124530 Use sys/sysmacros.h, if available, for minor() and major()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-05-01 20:50:17 +02:00
Joachim Wiberg 38885aaa67 Issue #253: handle fsck of magic /dev/root device
Some systems have /dev/root declared in their /etc/fastb as /.  This is
as special device node created by the kernel based on the root=, and a
few other variables.  This may be a symlink, and if so, and on a mdev
system, the symlink may point to the wrong device node ... so we must
try to figure out the major:minor from / and then traverse /sys/block
to find our root device.

Note: this commit also does a bit of refactoring for readability and
      also adds a few debug lines that may be dropped before release.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-05-01 19:31:51 +02:00
Joachim Wiberg a685b698c2 Follow-up to 5593372f, regression in remounting read-write at boot
A subtle regression was introduced in 5593372f, when moving to get
getmntent() family of APIs.  Finding the 'ro' flag (or not) in the
system fstab broke and Finit never tried to remount / read-write.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-05-01 19:28:50 +02:00
Joachim Wiberg 9bce8f9584 initctl: add cond get foo, supports -v option
Fix issue #255

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-30 10:28:47 +02:00
Joachim Wiberg d04b04d73a Follow-up to 7212543, fix args to svc_ident()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-30 10:20:53 +02:00
Joachim Wiberg 72125430e6 Fix #252: run/task success+failure conditions do not use :ID
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-30 08:50:14 +02:00
Joachim Wiberg e2d1930e6c Fix #248: source env:file also in pre:post: scripts
This change basically add this for pre/post scripts:

    sh -ac '. /path/to/env; exec ./pre-post.sh'

Meaning the env file can be a simple foo=bar file, which Finit reads and
call setenv() on all variables, and the user do not need to call export
on all variables -- additionally, any functions defined in an env file
can be used by the pre-post scripts, but are ignored by Finit.  I.e.,
the service only gets the variables set, no functions.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-30 08:43:44 +02:00
Joachim Wiberg 605c489937 Follow-up to 57c97d8, issue #224: add swapon/swapoff fstab wrappers
The swapon and swapoff commands do not support a custom fstab, they
assume /etc/fstab is the only true source of swap devices and files.

This change adds rudimentary (!) support for figuring out any swap
device or file to use from any finit.fstab.  Please note, options are
not supported at the moment.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 22:57:22 +02:00
Joachim Wiberg 26409bc860 Follow-up to 57c97d8, issue #224: fsck needs FSTAB_FILE env. set
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 21:59:11 +02:00
Joachim Wiberg b01f9f38f2 Improve logging of forking services having started and stopped
Since a forking service, e.g. a sysv init script, will exit very early
we cannot log the same as for regular services "Starting foo[123]", so
instead we log "Started bar[321]" when we get the pidfile update and
the same when stopping a forking service, log "Stopped bar[321]" when
we collect the PID.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 20:58:57 +02:00
Joachim Wiberg 2ddbb805ca Improve logging of sysv start/stop scripts, include full command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 20:58:57 +02:00
Joachim Wiberg ed999d9a80 Fix log regression introduced in 82cc10be
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 20:58:57 +02:00
Joachim Wiberg 82cc10be88 Fix #246: conditions for running/halted services
This change adds the following service conditions, which can be used to
synchronize other stanzas:

  - service/foo/running
  - service/foo/halted
  - service/foo/missing
  - service/foo/crashed
  - service/foo/stopped
  - service/foo/busy
  - service/foo/restart

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 00:07:44 +02:00
Joachim Wiberg 30fb9780d7 initctl: slight improvement to status output for sysv services
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 00:07:44 +02:00
Joachim Wiberg 09f46f62b4 Support args to sysv-like scripts, e.g. bridge-stp br0 start
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-26 00:07:44 +02:00
Joachim Wiberg bc4e7a8d8d Revert "Fix starting and stopping of SysV init scripts"
This reverts commit c63279630b which
clearly was not needed to fix the problem.  After this revert the
test(s) still pass, but now we can also start bridge-stp, e.g.

     bridge-stp br0 start

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 23:38:52 +02:00
Joachim Wiberg addb8c4019 Fix #247: fall back to reboot -f in sulogin mode
When logged in from sulogin we cannot send IPC to Finit, not even
signals, to delegate reboot.  For usability, attempt to tell reboot that
it's in sulogin mode -- provided the bundled sulogin is used -- to let
reboot trigger over to forced reboot.

We cannot do much about initctl, just document this as a limitation.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 21:44:33 +02:00
Joachim Wiberg 0c4a9b62c2 initctl: allow client_connect() to return error
This lets any callers of client_connect() decide what to do wrt. error
recovery and fallback mechanisms.  E.g., reboot may want to check if
it's in sulogin mode, or fall back to sending signals to PID 1.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 21:44:33 +02:00
Joachim Wiberg 57c97d8190 Fix #224: support for cmdline -- finit.fstab=/etc/fstab.secondary
This adds support for bringing up the system with an alternate fstab at
boot.  E.g., when using a primary/secondary setup for boot partitions.

By default /etc/fstab is read, like before, this can now be changed
using configure --with-fstab=/path/to/fstab.primary, which sets the
default that can be overridden using finit.fstab=/etc/fstab.secondary

If mounting, or fsck, fails in any way, Finit calls its own bundled
sulogin, or the system sulogin(8), to let the user handle the issue.
If there is no sulogin available, Finit will try to start up in its
rescue.conf boot mode.

Please note, in either of these rescue modes, use `reboot -f` to get the
system to reboot.  Finit is on pause in the background in rescue mode
and cannot be relied on (since there may not be any writable filesystems
available.).

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 21:44:33 +02:00
Joachim Wiberg b0d0ca76cf Minor, coding style
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 21:37:59 +02:00
Joachim Wiberg 5240c0cf19 Follow-up to a91e197: allow umount fail on EBUSY, skip error message
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 18:52:16 +02:00
Joachim Wiberg a02f369a89 Minor, rename local variable
The iterator works on any fstab like file, but to keep confusion to a
minimum, rename local variable.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-25 18:51:17 +02:00
Joachim Wiberg 5593372ffb Refactor, replace deprecated setfsent() & C:o with setmntent()
The setfsent() family of APIs was never standardized and on Linux only
GLIBC implement them.  We've tried to overcome this limitation, and to
support uClibc/uClibc-ng and musl libc, by providing replacements APIs
in helpers.c.

However, and since we want to support alternative /etc/fstab files, the
setmntent() family of APIs is more widespread and supports reading from
any fstab or mtab file.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-20 07:20:33 +02:00
Joachim Wiberg 05be281301 Rely on (now working) fstab.h check in configure
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-19 20:04:03 +02:00
Joachim Wiberg 108bbf56dd Update copyright years
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-19 19:35:49 +02:00
Joachim Wiberg f39626f132 Minor, fix format string to systemf()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-17 14:25:32 +02:00
Joachim Wiberg e4830c5614 Don't try to restart forking daemons
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-17 11:43:33 +02:00
Joachim Wiberg f33261af6b Fix fnread() string fmt, no format args.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-04-17 11:30:43 +02:00
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