service: drop a service's group when the configuration drops it

Changing a service from @alice:wheel to @alice 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>
(cherry picked from commit bf42811a13)
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-17 08:03:50 +01:00
1 parent 5049c32c35
commit 8a5102e714
3 files changed
+33 -6

No files matched your search

+3
View File
@@ -39,6 +39,9 @@ All relevant changes are documented in this file.
characters. Groups dropped from a service's configuration are now characters. Groups dropped from a service's configuration are now
also dropped the next time it starts, before they lingered until also dropped the next time it starts, before they lingered until
reboot reboot
- Fix a service's group lingering after it was dropped from the
configuration, e.g. `@alice:wheel` changed to `@alice` kept running
as `wheel` until reboot
- Remove stale daemon-owned (`pid:!`) pidfiles after unclean exits. - Remove stale daemon-owned (`pid:!`) pidfiles after unclean exits.
When a daemon dies via SIGKILL/OOM/segfault, or exits early during When a daemon dies via SIGKILL/OOM/segfault, or exits early during
startup, its pidfile lingers and can prevent the next instance from startup, its pidfile lingers and can prevent the next instance from
+1
View File
@@ -2142,6 +2142,7 @@ int service_register(int type, char *cfg, struct rlimit rlimit[], char *file)
svc_unblock(svc); svc_unblock(svc);
} }
svc->group[0] = 0;
svc->supgroups[0] = 0; svc->supgroups[0] = 0;
if (username) { if (username) {
char *ptr = strchr(username, ':'); char *ptr = strchr(username, ':');
+29 -6
View File
@@ -1,14 +1,19 @@
#!/bin/sh #!/bin/sh
# Verify supplementary groups, @user:group,sup1,sup2,..., are all set on # Verify group and supplementary groups, @user:group,sup1,sup2,..., are
# the service, and that dropping them drops the groups. # set on the service, and that dropping either drops the group(s) too.
set -eu set -eu
TEST_DIR=$(dirname "$0") TEST_DIR=$(dirname "$0")
# Optional argument is the supplementary group list, ",sup1,sup2,..." # Optional arguments are the group and the supplementary group list,
# ",sup1,sup2,...", the latter requires a group
write_conf() write_conf()
{ {
run "echo 'service @root:root${1:-} name:grp serv -np -i grp -- Groups' > $FINIT_CONF" user="@root"
if [ -n "${1:-}" ]; then
user="@root:$1${2:-}"
fi
run "echo 'service $user name:grp serv -np -i grp -- Groups' > $FINIT_CONF"
} }
# Kernel sorts the list, so it compares as a plain string. # Kernel sorts the list, so it compares as a plain string.
@@ -19,6 +24,13 @@ assert_groups()
"$(texec cat "/proc/$pid/status" | sed -n 's/^Groups:[[:space:]]*//p' | xargs)" = "$1" "$(texec cat "/proc/$pid/status" | sed -n 's/^Groups:[[:space:]]*//p' | xargs)" = "$1"
} }
assert_gid()
{
pid=$(texec initctl -j status grp | jq .pid)
assert "Service gid == $1" \
"$(texec cat "/proc/$pid/status" | awk '/^Gid:/ {print $2}')" = "$1"
}
test_teardown() test_teardown()
{ {
say "Running test teardown." say "Running test teardown."
@@ -29,7 +41,7 @@ test_teardown()
. "$TEST_DIR/lib/setup.sh" . "$TEST_DIR/lib/setup.sh"
say 'Seven supplementary groups' say 'Seven supplementary groups'
write_conf ',daemon,bin,sys,adm,tty,disk,dialout' write_conf root ',daemon,bin,sys,adm,tty,disk,dialout'
run "initctl reload" run "initctl reload"
retry 'assert_num_children 1 serv' retry 'assert_num_children 1 serv'
@@ -37,8 +49,19 @@ retry 'assert_groups "0 1 2 3 4 5 6 20"'
# The running process keeps its groups, the next start drops them # The running process keeps its groups, the next start drops them
say 'Dropping supplementary groups drops the groups' say 'Dropping supplementary groups drops the groups'
write_conf write_conf root
run "initctl reload" run "initctl reload"
run "initctl restart grp" run "initctl restart grp"
retry 'assert_groups "0"' retry 'assert_groups "0"'
say 'Dropping group drops the group'
write_conf adm
run "initctl reload"
run "initctl restart grp"
retry 'assert_gid 4'
write_conf
run "initctl reload"
run "initctl restart grp"
retry 'assert_gid 0'