Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qemu: wait for process to be in scope before registering with machined #3297

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions mkosi/qemu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys
import tempfile
import textwrap
import time
import uuid
from collections.abc import Iterator, Sequence
from pathlib import Path
Expand Down Expand Up @@ -917,12 +918,39 @@ def scope_cmd(
] # fmt: skip


def register_machine(config: Config, pid: int, fname: Path) -> None:
def register_machine(config: Config, pid: int, fname: Path, unit: str) -> None:
if os.getuid() != 0 or (
"DBUS_SYSTEM_ADDRESS" not in os.environ and not Path("/run/dbus/system_bus_socket").exists()
):
return

# Wait for the scope unit to be actually active before registering the machine with systemd,
# otherwise the process will still be in the session scope of the caller, and if it fails
# machined will stop the session.
for i in range(300):
if (
str(pid)
in run(
[
"busctl",
"call",
"org.freedesktop.systemd1",
f"/org/freedesktop/systemd1/unit/{unit.replace('-', '_2d').replace('.', '_2e')}_2escope",
"org.freedesktop.systemd1.Scope",
"GetProcesses",
], # fmt: skip
foreground=False,
env=os.environ | config.environment,
sandbox=config.sandbox(relaxed=True),
stdout=subprocess.PIPE,
).stdout.strip()
):
break

time.sleep(0.1)
else:
die(f"systemd-run scope unit {unit}.scope did not become active within 30 seconds")

run(
[
"busctl",
Expand Down Expand Up @@ -1415,7 +1443,7 @@ def add_virtiofs_mount(
for fd in qemu_device_fds.values():
os.close(fd)

register_machine(config, proc.pid, fname)
register_machine(config, proc.pid, fname, name)

if status := int(notifications.get("EXIT_STATUS", 0)):
raise subprocess.CalledProcessError(status, cmdline)
Expand Down
Loading