A package that ships a tmpfiles.d file wants its own paths created
when it is installed, without running every other entry on the
system:
tmpfiles --create --prefix=/var/lib/app
Add the option, it can be given more than once. It and
--exclude-prefix compare whole path components now, before
--exclude-prefix=/dev also left out /devices.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry with %a, or one of the specifiers that come from os-release,
was skipped as unsupported:
d /var/lib/app/%o-%w 0755 - - -
Add %o, %w, %W, %B, %M, and %A, read from /etc/os-release with
/usr/lib/os-release as fallback, and %a with the names tmpfiles.d(5)
has for the architectures. A field that os-release lacks gives an
empty string, as in tmpfiles.d(5). An entry is still skipped when
the architecture is not one of those known.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An e entry takes a glob, but cleaning looked for a directory with
the pattern as its name:
e /var/cache/* - - - 10d
tmpfiles --clean t.conf
This cleaned nothing.
Expand the glob and clean each directory it matches. Also use the
age of a C entry, tmpfiles.d(5) has it clean what it copied.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Only one number with one unit was read from the age of an entry. An
age like this one was taken as two days:
d /var/tmp 1777 root root 2d12h
What came after the first unit was not looked at. ms and us were not
known, so 500ms gave no cleaning at all.
Read any number of values with units, us, ms, s, m or min, h, d, and
w, and refuse an age that cannot be read or that overflows. Add the ~
prefix, which keeps what is right in the directory, and the prefix
that says which timestamps to go by, e.g. "aAmM:" for access and
modification time of files and directories.
Without that prefix the birth time of a file now counts too, as in
tmpfiles.d(5). It is read with statx(), where the C library has it,
and left out on a file system that has none.
A prefix that names timestamps for files only, or directories only,
leaves the default set for the other. The age is capped at what a 32
bit time_t holds, and nothing is cleaned when it reaches back past
the epoch.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry with a % in the path was skipped, and files from
distributions have them:
d /run/log/journal/%m 2755 root systemd-journal - -
Expand the specifiers that do not need the OS release or the
architecture: %b, %m, %H, %l, %v, the directories %t, %T, %V, %S, %C,
%L, %h, and the user ones %u, %U, %g, %G. tmpfiles runs as root, so
the last ones are fixed. An entry with any other specifier is still
skipped.
The argument of C, f, F, L, and w entries is expanded too, as in
tmpfiles.d(5). A literal % in such an argument must from now on be
written %%.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Files from distributions use v, q, and Q for directories that should
be btrfs subvolumes when that is possible:
v /var/lib/machines 0700 - - -
tmpfiles skipped them as unsupported, so the directory was missing.
On a file system without subvolumes tmpfiles.d(5) has them make a
plain directory. Do that everywhere, read them as d.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry for a device with a 0 in its number was skipped:
c /dev/tty 0666 root tty - 5:0
parse_mm() took 0 to mean atoi() could not read the number.
Use atonum(), which tells the two apart.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Several types read the mode and owner of an entry and did nothing
with them. An f entry was skipped when the file was there:
f /var/log/app.log 0640 app adm
This left a file with another mode or owner as it was. The owner of
p, b, and c entries was never set, and their mode was cut by the
umask. C, z, and Z entries had neither applied.
Set them for all of these, Z for everything below the path. Fifos
and device nodes are opened with O_PATH, so nothing is opened for
real. A path that is not what the entry makes, like a directory
where a p entry wants a fifo, is left alone.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
A mode with a prefix was not read as a number, so the entry was taken
to have no mode, and a user with one was looked up with the colon in
the name, which skipped the entry:
d /var/lib/app :0750 :app :app
With : the mode, user, or group is only set on a path the entry
creates, an existing one keeps what it has. With ~ the mode loses
read, write, or execute when no one has that on the existing path,
and setuid, setgid, and sticky when it is not a directory.
Mode and owner now go through perm_set() in tmpfiles, which knows the
entry created the path or not.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
What an unknown user in an entry did depended on the type. A d entry
was skipped, but this one made a file owned by root:
f /run/frr/state 0640 frr frr
The user was looked up in each type on its own, and only d checked
the result.
Look it up once for the entry, and skip the entry when the user is
not known. An unknown group leaves the group of the path as it is,
for a new path that is the group of root, as before. Both are only
reported with --debug, Finit's own files have entries for services
that may not be installed.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Finit only runs tmpfiles at boot, to create and remove. The age of an
entry has no effect until something calls tmpfiles --clean, and the
documentation said there was no aging at all.
Describe how to call it from cron, with a service as the fallback on a
system without cron, and what cleaning leaves alone. Update the
tmpfiles TODO to match.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry with the age 0 was read as having no age, so nothing in its
directory was ever cleaned:
d /var/spool/work - - - 0
tmpfiles --clean t.conf
In tmpfiles.d(5) age 0 means everything in the directory is removed
on each clean. parse_age() returned 0 for both.
It now returns -1 for no age and for one it cannot read, and with
age 0 the timestamps are not looked at.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
The x and X entries were read and ignored, so a path that a package
had marked to be left alone was cleaned like the rest:
d /var/tmp - - - 30d
x /var/tmp/keep
tmpfiles --clean t.conf
This removed what was old in /var/tmp/keep.
Gather the x and X entries of all files in a pass before the cleaning,
they may come after the entry they apply to, or in another file. x
keeps the path and what is in it, X the path alone. A directory that
is not empty afterwards is no longer warned about.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
Cleaning the directory of an entry with an age went into whatever was
mounted below it:
d /srv - - - 10d
tmpfiles --clean t.conf
With a disk mounted on /srv/data this removed every file on it older
than ten days.
Give nftw() FTW_MOUNT, so it stays on the file system of the directory.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
A ! after the type marks an entry that is only safe to run at boot.
Finit's own files have them, like this one in x11.conf:
D! /tmp/.X11-unix 1777 root root
tmpfiles ignored the ! and ran these entries on every call, so this
emptied the directory under a running X server:
tmpfiles --remove
Only the character right after the type was looked at, and only for
a +.
Read all the modifiers after the type, in any order, and skip an
entry with ! unless the new --boot option is given. Finit gives it
at boot, together with --remove. Before this the r, R, and D entries
were never run at boot, so "r! /forcefsck" in legacy.conf left the
file in place.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
tmpfiles exited at the first entry with a type it does not know, or
with a % specifier in the path:
h /tmp/dir - - - - +i
f /tmp/dir/after
tmpfiles --create t.conf
This never created /tmp/dir/after. At boot it meant that one such
line, in any tmpfiles.d file, stopped every entry after it, in that
file and in all the files sorted after it. The message says skipping,
but errx() exits.
Warn and go on with the next entry. With two passes, for --create
with --remove, only the second one warns.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
With both --create and --remove each entry was removed and then
created before the next one was read. An r entry took what an entry
above it had just created:
f /tmp/file - - - - new
r /tmp/file
tmpfiles --create --remove t.conf
This left no /tmp/file. The same goes for --clean.
Run all the files twice in that case, first to remove and clean, then
to create.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
The tpm2-tss package sets a default ACL on its keystore:
d /var/lib/tpm2-tss/system/keystore 2775 tss tss - -
a+ /var/lib/tpm2-tss/system/keystore - - - - default:group:tss:rwx
tmpfiles skipped the a+ line as unsupported, so what was created in
the keystore did not get the group access it relies on.
Add a and A, the latter recursive, with and without +. With + the
entries are added to the ACL of the path, without it they replace it.
The ACL is written to the system.posix_acl_access and _default
extended attributes in the format the kernel uses, which saves a
dependency on libacl. As with setfacl, the mask is made to cover the
named entries unless the entry sets one.
An ACL can have at most 32 entries. A path with more is left as it
is, with a warning.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry written in aligned columns got the space after the age field
as part of its argument:
L /tmp/null - - - - /dev/null
This made a link to " /dev/null". Only the first separator after
the age was dropped. A lone "-" for no argument was also taken as is,
so an f entry with it made a file holding a dash.
Skip all the space before the argument, and read "-" as no argument.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
An entry without a user or group, or with "-", made root the owner of
an existing directory or file:
mkdir -p /tmp/t && chown daemon:daemon /tmp/t
echo 'd /tmp/t' > t.conf
tmpfiles --create t.conf
stat -c %U:%G /tmp/t
This gave root:root. A missing field was read as "root" when parsing
the entry, the same as if it had been written out. tmpfiles.d(5)
only adjusts the owner when the entry gives one.
A missing user or group is now -1, and fdperm() looks at the two ids
separately, so "d /tmp/t - - sys" changes the group and keeps the
owner. mksubsys() and the service directories used uid -1 to skip
the chown altogether, they now pass -1 for the group as well.
Issue #523
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
A d entry without a mode reset an existing directory to 0755. On
finix a bare "d /tmp" line took the sticky bit and the write
permission for others off the /tmp mount, so nobody but root could
create files there:
mkdir -p /tmp/t && chmod 1777 /tmp/t
echo 'd /tmp/t' > t.conf
tmpfiles --create t.conf
stat -c %a /tmp/t
This gave 755. Commit 9bfbdc6b made the chmod of an existing
directory work, and it used the 0755 default for a new directory as
the mode to set.
The default is now only for creating the directory. With no mode in
the entry, dirperm() leaves the mode alone.
Fixes#523
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
initctl -j status had the state of a service but little of how it is
set up. A program reading it could not tell how the service is
stopped, what scripts it runs, or how it signals readiness.
Add the settings: stop, reload, notify, remain, scripts, directories,
cgroup-config, rlimits, log or tty, provides, conflicts, if, groups,
capabilities, pam, name, id, protected, and nowarn. The restart
object also gets sec, respawn, and oncrash, and the state Finit keeps
for its own use goes in internal:
"stop": {
"signal": "SIGTERM",
"timeout": 3,
"scope": "pgroup"
},
"scripts": {
"exec-stop-post": { "command": "/bin/true", "timeout": 7 }
},
initctl reads the status over D-Bus first, so each setting is also a
property of org.finit.Service1. The new test checks that the JSON is
the same over D-Bus as over the initctl socket.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
Building it by default installs our libsystemd.so.0 in $libdir. Where
the real one is already present, and contrib/debian/build.sh configures
--prefix=/usr --exec-prefix= on a Debian host, ours outranks it in the
loader cache, and every program linking libsystemd loses
sd_journal_stream_fd and the LIBSYSTEMD_209 symbol versions. The test
sysroot ran into exactly that, dbus-daemon exited 127 on every restart.
Restore the default to no. sulogin and watchdogd stay on, they only add
binaries. distcheck asks for the library so it stays covered.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
v5.0 ships keventd and the D-Bus support enabled out of the box, but the
remaining bundled pieces stayed opt-in, and the help text for two of them
already claimed otherwise.
Default all three to yes; --without-sulogin, --without-watchdog, and
--without-libsystemd opt out. The distcheck and CI configure lines drop
the flags they no longer need, so CI exercises the defaults.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>