Commit Graph
803 Commits
Author SHA1 Message Date
Joachim Wiberg 0641b1a73e Split finalize() in two separate tasks
The finalize() function is a bit too long, which means it may block the
API socket initctl (from the final runlevel switch at the beginning) use
to contact os via.

We should perhaps break it up even more, but at least once like this to
give the event loop a chance to interleave with an API callback.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-25 17:24:32 +01:00
Joachim Wiberg 9ba57f0c9d iwatch: ignore EEXIST errors, we're already watching it
In particular this can happen from cgroup.c when multiple services map
to the same cgroup.events (due to being in the same cgroup)

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-25 17:22:42 +01:00
Joachim Wiberg 9c2f0b2dc8 initctl: finit may be busy, wait for it ...
When calling initctl early in the boot process, e.g. just when Finit is
switching to the final runlevel, it may take a few milliseconds for it
to actually be ready to process our request.

This patch adds poll() to both the write(REQ) and the read(RSP) steps
to prevent from getting weird errors when Finit is busy.  The timeout
is currently 2 sec in both directions, which should be enough.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-25 17:18:50 +01:00
Joachim Wiberg 9c4d5341ae No env file, or optional ('-'), are both OK results
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-23 16:24:12 +01:00
Joachim Wiberg b9d498e01c initctl: fall back to .conf in /etc/finit.d if not in available/
For commands like `initctl show foo`, initctl looks in the available/
subdirectory for `foo.conf`.  This patch adds support for falling back
to return `/etc/finit.d/foo.conf` if the service doesn't exist in the
`/etc/finit.d/available/` subdirectory.

Static services, outside of the `enabled/` handling, live in the base
dirctory and this is what you expect from the tool.  "Do what I mean"

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-22 18:57:43 +01:00
Joachim Wiberg a1e38a3e40 cgroup: add missing pointer and result of fopen()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-22 14:28:25 +01:00
Joachim Wiberg c0ae7763a6 initctl: ignore possibly missing cpu.stat in cgroup2 root
Older kernels (4.19) don't have the summary cpu.stat in the cgroup2
root, so let's ignore that problem and return zero.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-22 10:47:52 +01:00
Joachim Wiberg 9f98b1d3d1 Merge pull request #161 from yangfl/upstream
Fix build for some non-x86 archs
2021-03-20 19:45:54 +01:00
yangfl a7e2224f05 Fix build for some non-x86 archs
Ref:
https://buildd.debian.org/status/fetch.php?pkg=finit&arch=mips64el&ver=3.2%7Erc3-2&stamp=1609910244&raw=0
https://buildd.debian.org/status/fetch.php?pkg=finit&arch=sparc64&ver=3.2%7Erc3-2&stamp=1609869348&raw=0
2021-03-20 23:57:08 +08:00
Joachim Wiberg 3bb1e41394 Only check for dangling symlinks, load enabled/*.conf file
In debug mode it's highly confusing to see Finit load available/*.conf
files when you expect it to only load finit.conf and enabled/*.conf,
so let's keep the dangling symlink check to itself and actually load
and parse from enabled/ as it was intended :)

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-20 14:40:11 +01:00
Joachim Wiberg e437379939 Minor, style/clarify debug messages
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-20 14:39:49 +01:00
Joachim Wiberg 3ad4f6ef4b In garbage collect, don't clear conditions provided by new instances
Sneaky bug, when switching between different sets of configurations, a
new instance of a previously removed svc can have its condition removed
by svc_gc() of the old instance.  This patch prevents such accidents.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-20 14:27:01 +01:00
Joachim Wiberg 4170a61d0b Merge pull request #160 from troglobit/release
Forward port dirty vs enabled fixes from release branch
2021-03-19 17:23:36 +01:00
Jacques de Laval b3fad1077e Store removal status separate from dirty
Having removal status stored in the dirty status resulted in removal
status being forgotten when a service was marked as dirty, for instance
when a dependency was updated. This side effect of marke_dirty seems a bit
unexpected and instead of adding exceptions to the logic for when marking
a service dirty - let's separate the two things (dirty and removed) from each
other.

Signed-off-by: Jacques de Laval <Jacques.De.Laval@westermo.com>
2021-03-19 16:53:04 +01:00
Joachim Wiberg ff86f29f41 initctl: add Memory: and CGroup: to process status command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-19 09:33:52 +01:00
Joachim Wiberg 3107027132 cgutil: new functions to query a cgroup from pid and get memory use
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-19 09:33:42 +01:00
Joachim Wiberg 728db73b09 cgutil: export more functions to initctl
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-19 09:32:36 +01:00
Joachim Wiberg fcb8ec9967 Refactor, factor out and export memsz() util function
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-19 09:30:52 +01:00
Joachim Wiberg 5b8fe12502 cgroup: error handling for critical operations
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-19 09:29:27 +01:00
Joachim Wiberg 6e24f2ff13 echo(): refactor and rename helper function -> fnwrite()
More often than not, the file to write to in sysfs changes rather than
the value.  This patch changes echo() into a fnwrite(), flipping what
is vsnprintf()'ed, and adds a stupid str() function that converts any
value (float/int/double/uint64_t) to a static string buffer.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-18 18:25:11 +01:00
Joachim Wiberg 74d715243c sanitize(): avoid strlen() to check string for NUL termination
The strlen() function can easiliy go out of bounds.  Use memchr()
instead, we have the max buffer len as argument anyway.

Also fix call to sanitize() which used wrong length.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-18 18:23:50 +01:00
Joachim Wiberg d9993860cb cgroup: add inotify support for cgroup.events
Replaces previous cgreaper.sh functionality for release notification and
cleanup of service groups.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-18 18:23:38 +01:00
Joachim Wiberg 6a6f3a8291 cgroup: refactor for cgroups v2, much cleaner + unified hieararchy
Since the cgroups support in Finit is not yet officially released, we
decided to change the back-end to use cgroups v2 instead of the aging
and rather clumsy cgroups v1.  Even this initial refactor is a lot
easier to read and understand, more can still be done since there's
a lot of concepts, data values and the ilk that can now be shared
between different controllers.

Still ToDo: inotify for cgroup.events to clean up leaf nodes, which
            in cgroups v1 was handled by cgreaper.sh.  This is fixed
	    in the next commit in the series.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-18 18:23:35 +01:00
Joachim Wiberg 99a8cf3eb7 initctl: add -1,--once flag for top command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-16 23:09:01 +01:00
Joachim Wiberg da95c7d80e initctl: ensure lines don't overflow in top command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-16 23:07:14 +01:00
Joachim Wiberg a9f7f692a9 initctl: add more columns to top command; VmSize, RSS, VmLib, %MEM
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-16 16:49:08 +01:00
Joachim Wiberg ce55f88a00 cgroup: add symlinks for traditional split controller world
Even systemd have collapsed a few of the cgroup controllers into a
semi-unified hierarchy and uses this approach.  We just take it to
the extreme and have collapsed all of them (like cgroup v2).

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-16 16:47:08 +01:00
Joachim Wiberg 74a26f559d initctl: refactor, split into multiple files
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-16 09:23:04 +01:00
Joachim Wiberg 08144df5ea initctl: fix %CPU listing bug and use hash table for faster lookup
Two major bugs: `if (!cg)` and missing `strdup(path)`.

Also convert to use std hash table for lookup of previous cpu load value
for a given cgroup path.  The hash table is sized after the current num.
rows on the screen -- resize currently not supported.  The value given
to hcreate() should be 25% greater than the estimated num of entries,
but we take a wild guess just to make sure we don't run out of space at
runtime.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-15 13:43:37 +01:00
Joachim Wiberg 230c64927b initctl: add very bare-bones cgroup based top command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-15 02:21:43 +01:00
Joachim Wiberg d1e56f300d initctl: show cgroup cpu.shares and memory.usage in ps command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-14 22:16:20 +01:00
Joachim Wiberg 9842786453 cgroups: fix cpu.shares assignment and set memory hierarchy
- Fix obvious refactor mistake in cpu.shares assignment
- For unified memory hierarchy we need to set .use_hierarch=1

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-14 12:27:46 +01:00
Joachim Wiberg 6d0c679e05 initctl: show comm and cmdline in ps output
The ps command currently only lists processes in the three main control
groups: init, system, user.  Kernel threads are not show at all.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-14 11:12:42 +01:00
Joachim Wiberg 5428dea5bf cgroup: refactor, use a unified hierarchy for finit
As of now, the default finit cgroup behavior is to use a unified
hierarchy of controllers under /sys/fs/cgroup/finit.

We mount cpu,cpuacct,cpuset,memory (if available) and gain the
ability to control our three major groups: init, system, user.

The default CPU share setup is ~10% for init and user, and 90% for
system.  These are guaranteed CPU shares to ensure we do not starve
PID 1 or user processes.  Support for configuring these limits will
be added in a later commit.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-14 11:07:13 +01:00
Joachim Wiberg 4e84ecae89 Set umask(022) early, in fs_init(), and no place else
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-14 11:05:12 +01:00
Joachim Wiberg 084b4ce8d3 cgroup: minor refactor/simplify
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-13 09:40:52 +01:00
Joachim Wiberg 46995fe446 Add mksusbys() to helpers.c, a kind-of tmpfiles.d in C
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-13 00:37:47 +01:00
Joachim Wiberg 600b2874e6 initctl: add bug report address to usage text, like finit -h
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 17:08:36 +01:00
Joachim Wiberg b080b970b7 initctl: don't warn on missing env file if it's optional
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 17:08:12 +01:00
Joachim Wiberg 6106574e13 initctl: minor refactor, use systemf() instead of snprintf+system
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 16:49:01 +01:00
Joachim Wiberg 2385a1209b initctl: don't fall back to default command for wrong commands
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 16:48:58 +01:00
Joachim Wiberg e8b942cf95 Fix regression in starting built-in services, introduced in 5b9d990
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 16:28:16 +01:00
Joachim Wiberg ac17d7f5a7 Silence output from gzip in logrotate()
- system may not have gzip (we're opportunistic)
- system may not have the log file (yet)

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 13:26:50 +01:00
Joachim Wiberg 1edc4356a6 initctl: improve error message for unprivileged users
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 13:26:16 +01:00
Joachim Wiberg 9f06a2db99 Silence developer API debug message
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 13:25:50 +01:00
Joachim Wiberg 8aba63a9e3 Track oldpid of services to fix empty pid in restart message
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-12 00:13:15 +01:00
Joachim Wiberg 3d98ad293b Restore telinit symlink to finit, compat only
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-11 23:59:39 +01:00
Joachim Wiberg cbec78dbbc initctl: show pid file and condition for foo in 'status foo' command
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-11 22:01:42 +01:00
Joachim Wiberg 572291a1d9 Don't guess pid file name, either there's one declared or not
The previous patch just added dynamic tracking of non-declared pid
files, so we no longer need to make stuff up.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-11 22:00:29 +01:00
Joachim Wiberg 280608f30e plugins: pidfile: track PID file in svc, if svc has none declared
Services that create their own PID files usually don't declare one with
Finit.  This patch adds support to track those PID files anywayt at
runtime for the purpose of identifying match svc_t when a PID file is
removed, i.e. when a service exits.

 - On IN_CREATE the pidfile.so plugin saves the pid file name in svc_t
 - On ON_DELETE the pidfile.so plugin finds svc_t based on pid file

Quicker tracking and less dead code, win-win.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-03-11 21:56:49 +01:00