Commit Graph
554 Commits
Author SHA1 Message Date
Joachim Nilsson 484aa0ba97 Fix issue with VINTR, VEOF et al not being restored after boot
On Alpine Linux 3.6 the tcsetattr() calls failed.  This turned out to be
a deeply rooted issue not limited to just musl libc.  The workaround is
to call tcsetattr() in several rounds.  In particular the modem signals,
c_cflag bits, always fail, so we take a bet on Linux having set up the
terminals (hope as a strategy) and ignore that problem, for now.

Also, use TCSAFLUSH with tcsetattr(), that seems to work better and is
what BusyBox stty use.

The local CTL() macro is replaced with CTL() from ttydefaults.h, which
exists also in musl libc.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 22:06:59 +02:00
Joachim Nilsson 83078d1947 Ignore ENOENT from realpath(), origin probably moved
The svc_find_by_pidfile() function is called async from the pidfile
plugin, so by the time the queue is processed the origin may have
moved or been removed even.  Safe to ignore.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 21:46:24 +02:00
Joachim Nilsson e02f8cf353 Check result of fsck before attempting remounting / read-write
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 21:45:12 +02:00
Joachim Nilsson 317fe7aceb Follow-up to e16a6ad: cgroups must be initialized after /sys
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 21:41:53 +02:00
Joachim Nilsson 54fd38adcc Default to set TZ=UTC0 for RTC plugin
This patch sets the TZ variable to a sane default, UTC.  Fixing, at
least, the time restore problems of the RTC plugin.

Also, let the C library figure out DST, as the docs say -1 does ...

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 21:38:07 +02:00
Joachim Nilsson 5fd5e722a8 Only mount /proc if it isn't already mounted
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 15:05:17 +02:00
Joachim Nilsson e16a6ad4f0 Check if /sys exists and only mount if /sys isn't already mounted
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 15:04:56 +02:00
Joachim Nilsson 2f87c10cf1 Remount / as read-write as soon as possible (after fsck)
We may need to create directories early, e.g., /sys and directories in
/var that plugins rely on.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 14:56:45 +02:00
Joachim Nilsson 389328c99f Allow fsck for read-only mounted filesystems
When starting up a system booted from initramfs / may already be
mounted (read-only), so we can safely fsck it before proceeding
to remount it as read-write later (possibly).

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 14:55:38 +02:00
Joachim Nilsson e3884597a5 Delay mount of /proc & /sys to *after* emergency_shell()
In case we run into problems mounting /sys or /proc we should have
signals and the optional emergency shell available for recovery.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 14:55:38 +02:00
Joachim Nilsson dc99d8637b Let create() macro return result of operation
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 14:55:38 +02:00
Joachim Nilsson 3f5ff0d339 Export ismnt() and fismnt() helper functions, for modprobe plugin
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 14:55:38 +02:00
Joachim Nilsson 37d92e87bc sig.c: Skip unknown signals, e.g. musl libc does not define SIGCLD
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-26 11:29:19 +02:00
Joachim Nilsson c0b74c5e53 Fix minor build warning for --disable-inetd
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 18:06:25 +02:00
Joachim Nilsson 88a6864fd4 Relocate docs/ --> doc/, like most other projects have
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 17:53:36 +02:00
Joachim Nilsson 8ed37f5fa0 Always append /ID to cond if run/task/service is declared with :ID
This patch is a follow-up to the idea introduced in 1d6737e, but this
time we make sure to always append the svc ID to conditions if their
.conf has been declared with :ID

Meaning, the following .conf:

    service :0 /sbin/teamd --config-file /etc/teamd-lag0.conf
    service :1 /sbin/teamd --config-file /etc/teamd-lag0.conf

results in the following two conditions:

    svc/sbin/teamd/0
    svc/sbin/teamd/1

With a custom 'name:foo':

    service name:lag :0 /sbin/teamd
    service name:lag :1 /sbin/teamd

the resulting conditions look even prettier:

    svc/lag/0
    svc/lag/1

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 15:22:29 +02:00
Joachim Nilsson 8fe4aad53e Make run/task/service :ID optional, default to zero/NULL
This patch makes the ':ID' field optional when registering non-inetd
run/taks/service etc.  It has always been optional in the .conf files,
but internally we defaulted to ':1'.

To open up for more useful conditions we now default to zero/NULL :ID,
meaning the user now *must* keep track of the :ID field if they have
multiple services with the same name.  This should already have been
an issue since such services would likely have conflicted with their
PID files (same name).  So it's unlikely to affect any user severely.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 15:21:28 +02:00
Joachim Nilsson 54e5fd2a78 initctl: Show :ID in process listing(s) only if non-zero
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 15:14:09 +02:00
Joachim Nilsson 1d6737eac1 Custom conditions include :ID; 'name:foo :bar' => svc/foo/bar
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 09:56:30 +02:00
Joachim Nilsson fa0c2f7050 Custom name:foo will now create condition svc/foo w/o path
When registering a service there is the option to set a custom PID file,
and even tell Finit to maintain that PID file in case the daemon doesn't

Until now there hasn't been a way to create custom conditions though ...

This patch leverages the new 'name:foo' argument to tas/run/services to
trigger condition 'svc/foo' (notice lack of path!) when a custom name is
set and either the default, or the custom, PID file is created.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-23 08:57:12 +02:00
Joachim Nilsson 70c0a38307 Provide description to built-in watchdog daemon
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 23:00:21 +02:00
Joachim Nilsson 217df432d6 Add support for 'console' as compat alias to '@console'
This patch makes it possible to declare tty's using the magic keyword 'console',
an alias for the already existing '@console' keyword, which automatically detect
valid console(s) from the console=foo kernel command line.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 22:35:32 +02:00
Joachim Nilsson cfa97253be Fix missing signal.h, required when building for uClibc
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 22:34:19 +02:00
Joachim Nilsson 6462bac249 finit: /run: Follow-up to 7c8b0df7, use MS_ mount flags instead
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:28:48 +02:00
Joachim Nilsson b0a1d6bd61 Fix warning message when service doesn't exist: !. -> .
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:15:48 +02:00
Joachim Nilsson b7c1a2a198 Change tty default baud rate: 38400 -> 0, i.e. keep kernel default
This is a more sane default, and also what systems expect, and what
other getty implementations support.  I.e., unless specified, or set
to 0, the tty baud rate is zero -- which means do not change speed
at all.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:15:48 +02:00
Joachim Nilsson af45166814 src: Add critical debug messages for debugging condition handling
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:15:48 +02:00
Joachim Nilsson 6306c757a2 src: svc: Use realpath() to check if untangled file exists
The svc_find_by_pidfile() function may be called with "/run/./foo.pid",
or similar, which must be sanitized before being compared to the PID
file of each svc_t.

This patch introduce realpath() to untangle the paths and check if the
file actually exists.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:15:48 +02:00
Joachim Nilsson 3865ce765e src: svc: Handle case where multiple services have same PID file
Services may exist in multiple runlevels, i.e. one behavior at bootstrap
and another at runtime.  If the user does not set a specific PID file to
be used as condition finit can match the wrong svc.

This patch extends svc_find_by_pidfile() to also compare the contents of
the PID file with the PID of the registered svc_t.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 21:15:48 +02:00
Joachim Nilsson aa08221666 configure: Remove --enable-rw-rootfs detect using /etc/fstab instead
This patch removes the --enable-rw-rootfs build flag.  Like OpenRC we
now check /etc/fstab instad; if `/` is listed *and* doesn't have the
`ro` flag set, we remount / read-write.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-22 20:08:52 +02:00
Joachim Nilsson 7c8b0df720 finit: /run: Mount with nosuid,nodev,noexec for added security
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-15 19:28:23 +02:00
Joachim Nilsson cb802cd54e finit: devpts: Mount with recommended ptmxmode=0666
For details https://www.kernel.org/doc/Documentation/filesystems/devpts.txt

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-15 19:28:13 +02:00
Joachim Nilsson 8379b4927b Fix GCC truncation warnings, too small buffers
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-12 00:33:52 +02:00
Joachim Nilsson 6a7f48970c Add cgreaper.sh to dist
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-12 00:29:39 +02:00
Joachim Nilsson 770a79fa89 Fix #120: Redirect stdin to /dev/null for services by default
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-11 22:24:33 +02:00
Joachim Nilsson ff24ee3553 Fix #124: reap all children in same process group of dying parent
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-11 21:55:40 +02:00
Joachim Nilsson 25c194ef93 Create unique service cgroup based on name:id
We want to have unique cgroups per instance.  I.e., a DHCP client for
eth0 should have its own cgroup separate from a DHCP client for eth1.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-11 21:50:38 +02:00
Joachim Nilsson 91a9c83928 Send signal to every process in the same process group
When stopping a run/task/service we must send SIGTERM/KILL to all every
process in the same process group.  Otherwise we may end up with stray
processes like 'logit' when log redirection is enabled.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-04-08 18:00:48 +02:00
Joachim Nilsson bf6e599584 Handle log redirect to syslog when logit is disabled
`--disable-logit` at configure time means redirecting stdout from a
service to a file is completely disabled.  Fallback to `logger` can
not be guaranteed to provide log file rotation, so all logs will be
sent to syslog instead.  In case logger is also missing, a simple
redirect using fork() + syslog() is used as fallback.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-30 11:53:43 +02:00
Joachim Nilsson 89c7171223 Refactor run/task/service stdout redirection after setsid() addition
This patch refactors redirect_null() and adds support for redirecting
stdout to console when 'log:console' is enabled.  This was previously
sort of the default, but with the addition of the long-sought-after
setsid() call we needed to change things.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-30 10:11:29 +02:00
Joachim Nilsson c41f3f4a06 Call setsid() by default to detach from controlling terminal
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-29 17:39:35 +02:00
Yaroslav Syrytsia 0a54a9624a Fix #123: Regression in sysv start/stop scripts
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-26 05:16:52 +01:00
Joachim Nilsson 1e238eec61 Increase MAX number of arguments to a service 32 -> 64
Usually a wrapper script is recommended, but for some use-cases it
is more efficient to just be able to use more command line args.

Example application that needs this; udhcpc

Note, we should refactor this to use a dynamically allocated array
instead of this static one.  We relied heavily on static arrays in
Finit 1, when everything had to fit i shm.  This is just legacy.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-19 07:50:36 +01:00
Joachim Nilsson 4163527006 Fix run-parts regression introduced in 9a98bff2
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-09 14:29:04 +01:00
Joachim Nilsson f06e17ccc6 initctl: Widen status column a bit to fit 'stopping' state
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-03-05 17:51:52 +01:00
Joachim Nilsson 0ad99ce054 Fix use-after-close in service.c:redirect(), found by Coverity Scan
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-02-29 16:42:53 +01:00
Joachim Nilsson 54cfa74042 Fix #109: Support PID files in subdirectories to /var/run
Services, like dbus and teamd for instance, may create their PID files
in a subdirectory of /var/run (today often /run). E.g.,

   - /var/run/teamd/a1.pid    -- For aggregate A1
   - /var/run/dbus/pid
   - /var/run/lxc/foo.pid     -- For container foo

This patch adds support for dynamically adding inotify watchers to any
new subdirectory created in /var/run (discarding too deep directories).

To match services in this directory the run/task/service/sysv stanza
must contain the pid:!/path/to/pidfile.pid syntax.  This pid file name
is also used to create the condition this service asserts using the
following formula:

   svc/ + <dirname of service> + <subdir and file without .pid>

E.g., the case of teamd (above) gives condition 'svc/usr/bin/teamd/a1'

The special case of dbus is interesting, since it may not be a special
case, but rather the norm for services using a subdirectory.  It is
handled as follows; when a new subdirectory is detected, the directory
is scanned for files matching *.pid.  Matching files follow the teamd
case.  The directory is also scanned for 'pid', which then gives us the
condition 'svc/usr/bin/dbus'

One last example, illustrated by lxc-start, where we want to track the
condition for the LXC container foo.  The service stanza:

   service pid:!/run/lxc/foo.pid lxc-start -n foo -F -p /run/lxc/foo.pid -- Container foo

This command has no leading path so the condition is composed entirely
from the PID file location:

   svc/  + '' + lxc/foo => svc/lxc/foo

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-02-28 16:39:53 +01:00
Joachim Nilsson 97ce0aa519 Rename local variable shadowing variable in outer scope
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-02-28 14:36:14 +01:00
Joachim Nilsson b66069278f Support for a custom kill:DELAY, default 3 sec
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-02-26 18:49:47 +01:00
Joachim Nilsson 1192506d43 Support for custom halt:signal, default SIGTERM
Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
2020-02-26 18:28:02 +01:00