fix(services-restart): an LXC container's processes are left out, 4.46.0 (NetOrk/netork#2750) #134

Merged
christianmanivong merged 4 commits from feature/services-need-restart-lxc into main 2026-11-26 18:56:35 +00:00
4 changed files with 44 additions and 7 deletions
Showing only changes of commit 67ffee4e9e - Show all commits

No files matched your search

+3 -1
View File
@@ -18,7 +18,9 @@ recorded here; their history is the git log.
`/proc/<pid>/maps` for executable mappings of deleted files under `/usr`, `/lib*`, `/bin`,
`/sbin` or `/opt`, each process mapped to its service through `/proc/<pid>/cgroup` as the
listening sockets do. A user's own systemd counts as `user@<uid>`, a container's process is
left out, and a process in no service is listed under `processes`. A refused root gives
left out (Docker, Podman, and LXC: `/lxc/<name>/`, Proxmox VE's `/lxc/<vmid>/ns/`, or
`/lxc.payload.<name>/`; a container updates itself, and its own systemd's units are no
services of the host), and a process in no service is listed under `processes`. A refused root gives
no report and raises, never a clean reading. A driver supplies
`_run_services_restart_command(command)`.
- `parse_services_restart`, `SERVICES_RESTART_COMMAND`, `DELETED_CODE_PATTERN`, and the
+2 -1
View File
@@ -214,7 +214,8 @@ finished until they restart. One fixed, read-only command, as root: `needs-resta
where dnf-utils has it, else `needrestart -b -r l` (list only, whatever `needrestart.conf`
says), else the processes whose `/proc/<pid>/maps` maps deleted code
(`DELETED_CODE_PATTERN`), each named by the service its cgroup gives, as for the listening
sockets. A driver supplies `_run_services_restart_command(command)`; the command arrives as
sockets. A container's processes are left out, an LXC container's as well (a Proxmox node's
containers update themselves). A driver supplies `_run_services_restart_command(command)`; the command arrives as
one `sh -c` argument and reads no stdin. Without root there is no reading: one that saw only
the login user's processes would read as clean.
+10 -5
View File
@@ -21,10 +21,11 @@ here once, and a driver only carries the command across.
**Units from cgroups.** For the ``/proc`` scan, ``/proc/<pid>/cgroup`` names the
systemd service a process runs in, as for the listening sockets: a process of a
user's own systemd is that user's manager (``user@1000``), one of a container is
left out (the container runs its image's libraries, which no host update
replaces), and one in no service -- a login session, systemd itself -- is listed
as a process. The host's tools name services only, so ``processes`` stays empty
user's own systemd is that user's manager (``user@1000``), one of a container --
Docker, Podman, or LXC as on a Proxmox node -- is left out (the container runs its
own libraries, which no host update replaces, and its own systemd's units are none
of the host's services), and one in no service -- a login session, systemd itself --
is listed as a process. The host's tools name services only, so ``processes`` stays empty
for them.
**Root, or nothing.** Only root sees every process's memory. The script goes to
@@ -84,6 +85,10 @@ _SERVICE_LINE = re.compile(r"^([\w:.@\\-]+)\.service$")
_NEEDRESTART_SVC = re.compile(r"^NEEDRESTART-SVC:\s*(.+?)\s*$")
#: A process of a user's own systemd: that user's manager is what restarts it.
_USER_MANAGER = re.compile(r"/(user@\d+)\.service(?=/|$)")
#: A process inside an LXC container: ``/lxc/<name>/`` (Proxmox VE, which nests the
#: container's own cgroups under ``ns``; cgroup v1) or ``/lxc.payload.<name>/`` (LXC 4
#: and later). Not ``/lxc.monitor/<name>``: ``lxc-start`` runs the host's binary.
_LXC = re.compile(r"^/lxc(?:/|\.payload\.)[^/]+(?:/|$)")
_SERVICE_SUFFIX = ".service"
@@ -130,7 +135,7 @@ def _maps(sections: dict[str, list[str]]) -> tuple[list[str], list[RestartProces
if not pid.isdigit():
continue
path = paths.get(int(pid))
if _container(path):
if _container(path) or _LXC.match(path or ""):
continue
unit = _process_unit(path)
if unit:
+29
View File
@@ -115,6 +115,35 @@ class TestTheMapsScan:
assert reading == {"source": "maps", "units": [], "processes": []}
@pytest.mark.parametrize(
"cgroup",
[
# Proxmox VE: the container's own systemd, under lxc/<vmid>/ns.
"0::/lxc/101/ns/system.slice/nginx.service",
"0::/lxc/101/ns/init.scope",
# LXC 4 and later by default.
"0::/lxc.payload.web/system.slice/nginx.service",
# cgroup v1, systemd's hierarchy.
"12:name=systemd:/lxc/web/system.slice/nginx.service",
],
)
def test_an_lxc_container_is_left_out(self, cgroup):
"""A Proxmox node's containers update themselves: the host sees their processes'
mappings, and their own systemd's units, which are no services of the host's."""
cgroups = f"2100 {cgroup}\n"
reading = parse_services_restart(_wire(("maps", "2100 nginx\n"), ("cgroups", cgroups)))
assert reading == {"source": "maps", "units": [], "processes": []}
def test_an_lxc_monitor_is_the_hosts(self):
"""``lxc-start`` runs the host's own binary, outside the container."""
reading = parse_services_restart(
_wire(("maps", "2090 lxc-start\n"), ("cgroups", "2090 0::/lxc.monitor/101\n"))
)
assert reading["processes"] == [{"pid": 2090, "name": "lxc-start"}]
def test_a_process_gone_before_its_cgroup_was_read_has_no_unit(self):
reading = parse_services_restart(_wire(("maps", "4242 cron\n"), ("cgroups", "")))