Commit Graph
4237 Commits
Author SHA1 Message Date
Joachim Wiberg 9fa50746a7 initctl: add what was missing from the JSON status
initctl -j status is for other programs and should have all there is,
the plain view is the one kept short.  It was the other way around:
the restart counter and restart-max were only in the plain view, and
the restart delay of restart-max-sec was not in the JSON either.

Add a restart object:

    "restarts": 7,
    "restart": {
      "count": 4,
      "max": -1,
      "delay": 160,
      "limit": 300
    },

Also add manual and started, and give starts for all services, not
only those with manual-start.  The restarts total stays where it is.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:43 +01:00
Joachim Wiberg 308287923d initctl: show the restart delay of a service with restart-max-sec
With restart-max-sec the delay before the next restart varies, and
nothing showed what it was or how far it could grow.

Show both in initctl status, for a service that has the setting:

       Restarts : 7 (4/-1)
     Restart in : 160 sec (max 300)

Add the D-Bus properties RestartDelay and RestartDelayMax for it, so
the status is the same when initctl goes over D-Bus.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:43 +01:00
Joachim Wiberg 7f652770e3 test: list what to install to run the test suite
A new machine lacks more than the build dependencies.  Without the
uidmap package every test fails:

    unshare: failed to execute newuidmap: No such file or directory

List the packages for Debian, Ubuntu, and Linux Mint, and what each is
used for.  The build instructions asked for libite and libuEv v2.2.0,
configure wants v2.6.1 and v2.4.1.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:42 +01:00
Joachim Wiberg aae640b8f3 service: spell the restart_sec: option restart-sec:
restart_sec: was the only setting in snake_case.  Every other option
of the line-based format is in kebab-case, and so is the block format,
restart-sec = SEC.

Take restart-sec: and keep restart_sec: as an alias, so a .conf from
an older Finit loads as before:

    service restart-sec:10 foo -- Foo daemon

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:42 +01:00
Joachim Wiberg 62bd5f4892 service: add restart-max-sec, a limit for the restart back-off
The delay between restarts of a crashing service is fixed: two
seconds, then five, or restart-sec if that is longer.  A service that
should be retried quickly at first and then less and less often, e.g.
a modem dialer, cannot be set up.

Add restart-max-sec, in the block format and as the restart-max-sec:
token:

    service dialer {
        restart         = "always"
        restart-sec     = 10
        restart-max-sec = 300
        command         = "dialer"
    }

When set, the delay doubles for each restart, from restart-sec up to
the limit: 10, 20, 40, 80, 160, 300.  Without restart-sec the first
restart is at once, then after 2, 4, 8 seconds.  The delay follows the
restart counter, so it comes down again as the counter ages.  With
restart always the counter stops where the delay peaks.

Fixes #306

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:42 +01:00
Joachim Wiberg bba286036e service: save the configured restart delay when registering
The configured restart delay was saved at the first restart, when the
restart counter went from zero to one, and restored when a restart
succeeded.  The counter ages back to zero while the service runs.  If
it did so right after a restart and the service crashed again, the
back-off delay was saved over the configured one.  From then on the
first restart after a crash waited two or five seconds, until the next
reload.

Save it once, when the service is registered.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:41 +01:00
Joachim Wiberg 644279cd72 service: stop the restart counter of restart always from overflowing
The restart counter is a char, and with restart always nothing stops
it.  A service restarted more often than the counter ages wrapped it
after 127 restarts, 255 on Arm.  initctl status then showed a negative
count and the back-off fell back to two seconds.

Make the counter an int that stops at INT_MAX.  With restart always it
stops at 10, the default restart-max.  The back-off peaks at half of
that, and from 10 the counter ages back within the hour.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:41 +01:00
Joachim Wiberg 4e1b0d4fbe service: start restart always at two seconds between restarts
A crashing service with restart always waited five seconds before
every restart.  It should wait two seconds for the first five, like a
service with the default restart-max:

    service foo {
        restart = "always"
        command = "foo"
    }

The back-off starts after half of restart-max attempts.  For restart
always restart-max is -1, so half of it was zero and the back-off
began with the first restart.  Use the default restart-max instead.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-19 20:39:40 +01:00
Joachim Wiberg 631baab756 api: acknowledge reboot, halt, and poweroff
Finit carried out a reboot, halt, or poweroff requested over the
initctl socket, but replied with a NACK.  The command printed an
empty line and fell back to signalling PID 1.

The result was never set from the action taken.  Return what
shutdown_now() returns.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 11:00:49 +01:00
Joachim Wiberg 20c422af69 shutdown: add time argument, wall message, and cancel
shutdown ignored everything after its options, so a delayed shutdown
went down right away:

    shutdown -r +10 "Upgrading, back in five"

Take a time, now, +MIN, or the next HH:MM, and an optional message.
Finit keeps the countdown, so it survives logging out:

  - warns logged-in users at 60, 30, 15 and 10 minutes left, then
    every minute from five, and on the console
  - writes the message to /etc/nologin from five minutes before,
    leaving one already in place alone
  - shutdown -c cancels, and a new shutdown TIME replaces it

Without a time, or with now and no message, shutdown acts right away
as before.  An immediate reboot, halt, or power-off, also over D-Bus,
cancels a scheduled one.  The warnings are written by a child, reading
utmp and closing a serial tty can block.

Fixes #149

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 11:00:49 +01:00
Joachim Wiberg d51f521bf8 service: add stop-scope, which processes a stop applies to
When the main process of a service exits, Finit sends SIGKILL to its
process group.  That killed the action script of avahi-autoipd, which
it runs on SIGTERM to deconfigure the interface, before it was done.

Add stop-scope, in the block format and as the stop-scope: token:

    service avahi-autoipd {
        stop-scope = "process"
        command    = "avahi-autoipd eth0"
    }

  - pgroup: the stop signal to the main process, SIGKILL to its
    process group when it exits, as before and the default
  - process: only the main process
  - cgroup: the stop signal to all of the cgroup, SIGKILL to the rest
  - mixed: the stop signal to the main process, SIGKILL to the rest of
    the cgroup, like systemd's KillMode=mixed

SIGKILL to a cgroup uses cgroup.kill when the kernel has it.  Without
a cgroup, cgroup and mixed fall back to the process group.  Services
in the same .conf file share a cgroup, Finit warns when one of them
uses a cgroup scope.

Fixes #413

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 11:00:48 +01:00
Joachim Wiberg 35c313c2fa service: keep a stop of a forking service waiting for its PID file
Stopping a forking service after its start script had exited, but
before the daemon had written its PID file, did not hold.  When the
PID file showed up, the service was reported running again and the
daemon was left alone:

    initctl stop ext

service_forked() cleared any block on the service, the user's stop
included.

Keep a user stop, and let the state machine stop the late daemon of
a service no longer enabled.  The service is briefly RUNNING on its
way to STOPPING, so service/NAME/running blinks on and off.

Follow-up to #452

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 07:49:50 +01:00
Joachim Wiberg a1706e8ebd pidfile: don't let a stopped service claim a PID file
A stopped service with a daemon-owned PID file came back as running
when another process wrote that file, e.g., a container started by
hand:

    sudo podman start dash

turned container:dash from stopped to running in initctl, with the
podman PID adopted, moved to the service's cgroup, and its conditions
set.  The same happened to a service waiting for its conditions.

The plugin acted on any new or changed PID file, only skipping
services stopping or running scripts, and service_forked() then
cleared the stop.

Only let a service that is starting or running claim its PID file.

Fixes #452

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 06:44:45 +01:00
Joachim Wiberg a162551896 service: bind the systemd notify socket before fork
A notify:systemd service that calls sd_notify() early could lose its
READY=1 and never become ready.  Seen with udevd in an initramfs.

The socket address contained the PID of the service, so it was bound
in the parent after fork(), racing the service.

Let the kernel pick a unique abstract address instead, bound before
fork(), and pass it in NOTIFY_SOCKET.

Fixes #505

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 06:21:26 +01:00
Joachim Wiberg 14fd315f38 keventd: touch the PID file after SIGHUP
After a SIGHUP, from initctl reload or a changed service, Finit waits
for a pid-notify daemon to recreate or touch its PID file to reassert
readiness.  keventd only reloaded its rules, so with a daemon-owned
PID file it never did.

Touch it with pidfile() after reloading the rules, once keventd has
created it.  With -c that is after the coldplug queue has drained.

Fixes #503

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 06:21:26 +01:00
Joachim Wiberg bf42811a13 service: drop a service's group when the configuration drops it
Changing a service from @alice:wheel to @alice, or removing group from
a block, and reloading kept the service running as group wheel, also
after a restart.

The group is only set when the configuration has one, and on reload
the service is updated in place, so the old one stayed.

Clear it before parsing, like a new service gets it.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 06:21:25 +01:00
Joachim Wiberg de73b06415 service: lift the limit of four supplementary groups
A service listing more than four supplementary groups, in extra-groups
or as @user:group,sup1,sup2,..., got only the first four:

    too many supplementary groups, max 4

The names were stored in a fixed array of four.  It was also only
reset when the new configuration listed groups, so groups dropped from
a service lingered until reboot.

Store the list as one string, split when the service starts, and set
it on every (re)registration.  A list longer than 511 characters drops
the name it cuts off, which may otherwise match another group.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-17 06:21:24 +01:00
Joachim Wiberg 8451c4087a Merge pull request #522 from firasuke/master
doc: list libcap as a dependency
2026-11-14 12:14:04 +01:00
Firas Khana e280569941 doc: list libcap as a dependency 2026-11-14 13:44:58 +03:00
Joachim Wiberg f15e95d1cc service: don't take a command named like an option for one
A run/task/service whose command starts like an option without a ':'
lost its command, e.g. the example in finit.conf(5):

    run [2345] @joe:users logger "Hello world"

was registered as the log option, running "Hello world".  Same for
pidof, pidproxy, cgroupfs-mount, and in block format too.

The option parser matches with MATCH_CMD(), a prefix match, also for
the options with no trailing ':' to end the prefix: log, pid, cgroup,
respawn, nowarn, and the rest.

Match these against the whole token, allowing only the separator that
starts a value: "log:", "pid:", "cgroup." and "cgroup:".

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-13 16:00:25 +01:00
Joachim Wiberg 34a0a4b1ff conf: pass -- in a block command to the program
A command that takes -- as an argument lost it, and everything after
it, to the description:

    service name {
        command     = "cmd -- arg"
        description = "a cmd"
    }

ran `cmd` with description `arg`.

The block translator builds a legacy one-liner, appending the
description as "-- text", and service_register() splits that line at
the first "-- ", here the one in the command.

Split the description off only for legacy one-liners.  Blocks now hand
it to the new service_register_desc() as an argument.

Fixes #512

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-13 15:56:30 +01:00
Paweł c03059a9a0 keventd: don't publish whole-disk by-id links for partitions
disk_serial_id() falls back to <disk>/../serial so a partition got a
by-id link named after its parent disk's serial, clobbering the link
created for the whole disk.

(cherry picked from commit 4b2f8c89c5)
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-11-13 15:06:08 +01:00
Joachim Wiberg 297d2de1da Merge pull request #511 from zhangxiaohui1/fix/remain-task-control
service: preserve explicit stop for remain tasks
2026-11-12 21:06:47 +01:00
Joachim Wiberg 3acf1cb7b1 Merge pull request #517 from vazub/hwdb-parse-fix
keventd: fix hwdb lookups that never match
2026-11-12 19:25:38 +01:00
Joachim Wiberg eb9fb83011 Merge pull request #516 from vazub/symlink-fix
keventd: fix dangling and misnamed /dev/input/by-id links
2026-11-12 19:24:02 +01:00
Joachim Wiberg b6f0d5314b Merge pull request #521 from firasuke/firasuke-patch-1
Add conditional include for sm.h based on AUTO_RELOAD
2026-11-12 19:22:53 +01:00
Joachim Wiberg 59a2f69345 Merge pull request #515 from augustebaum/patch-1
docs: Fix nested bulleted list
2026-11-09 17:14:29 +01:00
Firas Khana 90512b07a0 Add conditional include for sm.h based on AUTO_RELOAD 2026-11-06 23:09:13 +03:00
Firas Khana d313b1fd74 Add conditional include for sm.h based on AUTO_RELOAD 2026-11-06 23:04:57 +03:00
Basil Zubko 439cab0558 keventd: fix hwdb parsing issues 2026-10-31 20:42:07 +01:00
Basil Zubko 98b2effb52 keventd: fix dev symlinks 2026-10-30 12:30:40 +01:00
Auguste Baum cbd5f79ffd docs: Fix nested bulleted list 2026-10-30 12:13:23 +01:00
Joachim Wiberg a220e0fa9b Merge pull request #514 from vazub/static-fix
Fix static build dropping all built-in plugins
2026-10-27 12:24:28 +01:00
Basil Zubko 1efd50191c fix static build dropping all built-in plugins 2026-10-24 15:13:10 +02:00
Joachim Wiberg 096e6337c5 Merge pull request #513 from vazub/keventd-input
keventd: fix ENV{} match on unset properties
2026-10-24 12:33:46 +02:00
Basil Zubko 02cd1bac42 keventd: fix ENV{} match on unset properties 2026-10-24 09:06:23 +00:00
zhangxiaohui 3a18ca4a30 service: preserve explicit stop for remain tasks
A completed remain task unconditionally cleared its user stop block
after running its post script.  Since it was still valid in the current
runlevel, it immediately started again, making initctl stop ineffective.

Keep explicit stops blocked, distinguish automatic and requested
restarts, and route initctl restart through teardown before rerunning the
task.  Cover stop, start, restart, reload, and runlevel transitions with
a regression test.

Signed-off-by: zhangxiaohui <xiaohui.zhang@wbstar.com>
2026-10-21 19:03:46 +08:00
Joachim Wiberg 530d6703f1 Merge pull request #509 from anees-r3hman/umount-rc
mount: report unmount failures
2026-10-12 09:38:08 +02:00
Joachim Wiberg cec76f18a3 doc: ChangeLog entry for the lost reload fix
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-09-27 18:56:03 +02:00
Joachim Wiberg ee6d4a1dae service: keep a pending reload across a second conf reload
A service whose condition goes into flux during a reload is paused
with its reload still pending.  If another reload was requested in
the meantime, re-parsing its unchanged .conf file cleared the pending
mark, so the service was resumed without ever being reloaded.  Seen
with sshd <pid/syslogd> on Infix, where a configuration change that
touched both landed as two reloads in a row and sshd kept its old
listen addresses.

The mark is only ever cleared once the change has been applied, so a
mark that is still set when the file is parsed again means exactly
that: not applied yet.  Leave it alone.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-09-27 18:41:05 +02:00
Joachim Wiberg 0548485be1 doc: update ChangeLog for 5.0
Covers the longer user and group names, the tmpfiles mode and owner
work, the shutdown remount order, and the stop/reload script timeout.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-09-27 18:00:13 +02:00
Joachim Wiberg 1b10c3746e Merge pull request #508 from anees-r3hman/avoid-fstab-on-root-remount
sig: avoid reading fstab during shutdown
2026-09-27 17:58:33 +02:00
Joachim Wiberg 24911ebbc2 Merge pull request #507 from anees-r3hman/master
svc: allow longer user and group names
2026-09-27 17:42:11 +02:00
Joachim Wiberg 88a9e6575b tmpfiles: apply mode and owner of e and f/F entries through an fd
Same treatment as d/D got: e went through fisdir() and a path based
chmod/chown, f/F did the chmod/chown by path after closing the file.
Both now work on the open fd, so the mode and owner end up on the
directory or file that was just checked or written.  f/F use open(2)
directly, which lets a plain f rely on O_EXCL for create-if-missing
instead of the earlier stat().

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
(cherry picked from commit 75739fbeef)
2026-09-27 17:30:16 +02:00
Sam Brkopac 9bfbdc6bac tmpfiles: set mode and owner of d/D directories through an fd
makedir() swallows EEXIST, so the chmod branch for directories that
already existed never ran, while chown() ran every time.  On finix
/var/empty is immutable, which gave a warning on every boot even
though the owner was already correct.

mksubsys() is the original of this code and has the same shape, the
dbus plugin uses it for /tmp/dbus.  Both now go through dirperm(),
which opens the directory and uses fstat/fchmod/fchown on the fd,
changing only what differs.  A failed chown in mksubsys() is a warning
now rather than err(1), PID 1 should not exit over a directory it
cannot adjust.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
(cherry picked from commit 3a72d1263c)
2026-09-27 17:26:08 +02:00
Anees Rehman c016965371 mount: report unmount failures
Return a failure status from unmount_regular() when filesystems remain
mounted, and use it to report the final shutdown unmount result.

Signed-off-by: Anees Rehman <anees.rehman@atlascopco.com>
2026-09-25 21:20:02 +02:00
Anees Rehman 263ea929fd sig: avoid reading fstab during shutdown
Only fall back to the regular mount invocation when both
fstab-independent root remount attempts fail.
This avoids reading an encrypted fstab during a normal shutdown.

Signed-off-by: Anees Rehman <anees.rehman@atlascopco.com>
2026-09-25 19:41:05 +02:00
Anees Rehman cc5e0c5175 svc: allow longer user and group names
Service user and group names are stored in 16-byte buffers, limiting
them to 15 characters. This silently truncates longer names before
getpwnam() and getgrnam(), preventing services configured with valid
longer account names from starting.

Increase the buffers to match the existing maximum configuration
argument length.

Signed-off-by: Anees Rehman <anees.rehman@atlascopco.com>
2026-09-25 16:09:35 +02:00
Joachim Wiberg 417abf9000 Merge pull request #500 from finit-project/pam 2026-09-23 17:52:38 +02:00
Joachim Wiberg 61b0e0f3e6 Fix #420: run services inside a PAM session
Apply a PAM session to run/task/sysv/services Finit starts, pam_limits
above all, so a service running as a given user picks up that user's
limits the way a login does.

Add a new `pam` setting for the new block format (only), like the
per-service directories, naming a file in /etc/pam.d:

    service weston {
        user    = "weston"
        pam     = "weston-autologin"
        command = "/usr/bin/weston --continue-without-input"
    }

pam_close_session() has to be called by a process still holding the
handle, and the handle does not survive exec().  Hence the keeper: it
holds the handle, drops to the service's credentials, and waits for a
parent-death signal before closing the session.  Same shape as
systemd's (sd-pam), for the same reason, and one per fork, so the
script hooks open and close their own.

The keeper closes the descriptors it inherited from Finit and only
those.  Closing everything would also take out what pam_open_session()
opened for itself, a keyring fd or a lock file, and leave the modules
to close a session with those pulled out from under them.  Closing
nothing, as (sd-pam) does, would leave it holding the write end of the
notify pipe for the service's whole lifetime and starve notify = "s6"
services of their ready signal.  So the fds open before pam_start()
are snapshotted and exactly those are closed, while the ones PAM opens
after are marked close-on-exec so the daemon does not inherit them
either.

A refused value, a denied account stack, an uninstalled pam.d file,
and a build without PAM support all keep the service from starting
rather than running it with the stacks skipped: one that quietly loses
pam_limits and its private /tmp, with nothing said.  Capabilities a
module like pam_cap.so granted are merged into the IAB Finit applies
instead of being replaced by it, which only helps a service that also
sets capabilities, the other arm being a plain setuid() with nothing
left to restore once permitted is empty.

The test sysroot gains pam_permit.so, pam_deny.so and pam_limits.so,
which ldd cannot see, libpam dlopen()s them, and the test skips when
the host has none to stage.  The negative cases pin the exit status
rather than only asserting crashed, which serv reports for any early
exit, so a bad command or an unwritable pidfile cannot pass for a
rejected session.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2026-09-23 16:30:14 +02:00