summaryrefslogtreecommitdiff
path: root/linuxnamespaces/systemd/__init__.py
blob: 84cb135e808e854dbed4a47f72f23a12cdd7ac64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2024 Helmut Grohne <helmut@subdivi.de>
# SPDX-License-Identifier: GPL-3

"""Communicate with a systemd instance to create e.g. delegated croups."""

import os
import sys
import typing


_DBUS_INTEGER_BOUNDS = (
    ("q", 0, 1 << 16),
    ("n", -(1 << 15), 1 << 15),
    ("u", 0, 1 << 32),
    ("i", -(1 << 31), 1 << 31),
    ("t", 0, 1 << 64),
    ("x", -(1 << 63), 1 << 63),
)


def _guess_dbus_type(value: typing.Any) -> typing.Iterator[str]:
    """Guess the type of a Python value in dbus. May yield multiple candidates.
    """
    if isinstance(value, bool):
        yield "b"
    elif isinstance(value, str):
        yield "s"
    elif isinstance(value, int):
        found = False
        for guess, low, high in _DBUS_INTEGER_BOUNDS:
            if low <= value < high:
                found = True
                yield guess
        if not found:
            raise ValueError("integer out of bounds for dbus")
    elif isinstance(value, float):
        yield "d"
    elif isinstance(value, list):
        if not value:
            raise ValueError("cannot guess dbus type for empty list")
        types = [list(_guess_dbus_type(v)) for v in value]
        found = False
        for guess in types[0]:
            if all(guess in guesses for guesses in types):
                found = True
                yield "a" + guess
        if not found:
            raise ValueError("could not determine homogeneous type of list")
    else:
        raise ValueError("failed to guess dbus type")


async def start_transient_unit(
    unitname: str,
    pids: list[int] | None = None,
    properties: dict[str, typing.Any] | None = None,
    dbusdriver: typing.Literal["auto", "jeepney", "dbussy"] = "auto",
) -> None:
    """Call the StartTransientUnit dbus method on the user manager."""
    dbus_properties: list[tuple[str, tuple[str, typing.Any]]] = []
    if pids is None:
        pids = [os.getpid()]
    dbus_properties.append(("PIDs", ("au", pids)))
    for key, value in ({} if properties is None else properties).items():
        try:
            guess = next(_guess_dbus_type(value))
        except ValueError as err:
            raise ValueError(
                f"cannot infer dbus type for property {key} value"
            ) from err
        dbus_properties.append((key, (guess, value)))
    if dbusdriver in ("auto", "jeepney"):
        try:
            from .jeepney import start_transient_unit as jeepney_impl
        except ImportError:
            pass
        else:
            return await jeepney_impl(unitname, dbus_properties)
    if dbusdriver in ("auto", "dbussy"):
        try:
            from .dbussy import start_transient_unit as dbussy_impl
        except ImportError:
            pass
        else:
            return await dbussy_impl(unitname, dbus_properties)
    raise NotImplementedError("requested dbusdriver not available")


def reexec_as_transient_unit(
    unitname: str | None = None,
    properties: dict[str, typing.Any] | None = None,
    argv: list[str] | None = None,
) -> typing.NoReturn:
    """Reexecute the current process via systemd-run thus placing it into a new
    .scope unit. If no argv is given, sys.argv is used.
    """
    execargs = ["systemd-run", "--user", "--scope"]
    if unitname is not None:
        execargs.append("--unit=" + unitname)
    if properties:
        for key, value in properties.items():
            if isinstance(value, int):
                value = str(value)
            elif not isinstance(value, str):
                raise ValueError(f"cannot format property {key} value")
            execargs.append(f"--property={key}={value}")
    execargs.append("--")
    execargs.extend(sys.argv if argv is None else argv)
    os.execvp("systemd-run", execargs)