| 1 |
# |
| 2 |
# Copyright (C) 2026 Matthias Klumpp <[email protected]> |
| 3 |
# |
| 4 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
# |
| 6 |
|
| 7 |
import os |
| 8 |
import time |
| 9 |
import shutil |
| 10 |
import tempfile |
| 11 |
import subprocess |
| 12 |
|
| 13 |
|
| 14 |
def system_bus_reachable(): |
| 15 |
"""Whether a system D-Bus daemon looks reachable.""" |
| 16 |
if os.environ.get('DBUS_SYSTEM_BUS_ADDRESS'): |
| 17 |
return True |
| 18 |
return os.path.exists('/run/dbus/system_bus_socket') or os.path.exists( |
| 19 |
'/var/run/dbus/system_bus_socket' |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def _name_has_owner(name): |
| 24 |
"""Whether a bus name currently has an owner on the system bus.""" |
| 25 |
res = subprocess.run( |
| 26 |
[ |
| 27 |
'gdbus', |
| 28 |
'call', |
| 29 |
'--system', |
| 30 |
'--dest', |
| 31 |
'org.freedesktop.DBus', |
| 32 |
'--object-path', |
| 33 |
'/org/freedesktop/DBus', |
| 34 |
'--method', |
| 35 |
'org.freedesktop.DBus.NameHasOwner', |
| 36 |
name, |
| 37 |
], |
| 38 |
capture_output=True, |
| 39 |
text=True, |
| 40 |
) |
| 41 |
return res.returncode == 0 and 'true' in res.stdout |
| 42 |
|
| 43 |
|
| 44 |
def start_system_bus_if_needed(out): |
| 45 |
"""Start a private system bus if none is reachable. |
| 46 |
|
| 47 |
Returns (proc, tmpdir): proc is the dbus-daemon we launched (or None if an |
| 48 |
existing bus is reused), tmpdir is the temporary runtime dir to clean up. |
| 49 |
""" |
| 50 |
if system_bus_reachable(): |
| 51 |
print('Reusing the already-running system bus.', flush=True) |
| 52 |
return None, None |
| 53 |
tmpdir = tempfile.mkdtemp(prefix='pk-test-bus-') |
| 54 |
# polkitd drops privileges to the unprivileged 'polkitd' system user, so the |
| 55 |
# directory holding the bus socket must be traversable by it to connect (and |
| 56 |
# claim org.freedesktop.PolicyKit1); mkdtemp() creates it as 0700. |
| 57 |
os.chmod(tmpdir, 0o755) |
| 58 |
socket_path = os.path.join(tmpdir, 'system_bus_socket') |
| 59 |
address = 'unix:path=' + socket_path |
| 60 |
print('No system bus found; starting a private one at {}.'.format(address), flush=True) |
| 61 |
proc = subprocess.Popen( |
| 62 |
['dbus-daemon', '--system', '--nofork', '--nopidfile', '--address=' + address], |
| 63 |
stdout=out, |
| 64 |
stderr=subprocess.STDOUT, |
| 65 |
) |
| 66 |
os.environ['DBUS_SYSTEM_BUS_ADDRESS'] = address |
| 67 |
for _ in range(40): |
| 68 |
if os.path.exists(socket_path): |
| 69 |
break |
| 70 |
time.sleep(0.25) |
| 71 |
return proc, tmpdir |
| 72 |
|
| 73 |
|
| 74 |
def start_polkitd_if_needed(out): |
| 75 |
"""Start polkitd only if no polkit authority is on the bus. Returns the proc |
| 76 |
we launched, or None if an existing authority is reused or none can be started. |
| 77 |
""" |
| 78 |
if _name_has_owner('org.freedesktop.PolicyKit1'): |
| 79 |
print('Reusing the already-running polkit authority.', flush=True) |
| 80 |
return None |
| 81 |
polkitd = shutil.which('polkitd') |
| 82 |
for candidate in ('/usr/lib/polkit-1/polkitd', '/usr/libexec/polkit-1/polkitd'): |
| 83 |
if polkitd: |
| 84 |
break |
| 85 |
if os.path.exists(candidate): |
| 86 |
polkitd = candidate |
| 87 |
if not polkitd: |
| 88 |
print('Warning: polkitd not found; the daemon will fail to reach an authority.', flush=True) |
| 89 |
return None |
| 90 |
print('No polkit authority found; starting {}.'.format(polkitd), flush=True) |
| 91 |
proc = subprocess.Popen([polkitd, '--no-debug'], stdout=out, stderr=subprocess.STDOUT) |
| 92 |
for _ in range(40): |
| 93 |
if _name_has_owner('org.freedesktop.PolicyKit1'): |
| 94 |
break |
| 95 |
time.sleep(0.25) |
| 96 |
return proc |
| 97 |
|
| 98 |
|
| 99 |
def terminate(proc): |
| 100 |
"""Best-effort terminate-then-kill of a child process we started.""" |
| 101 |
if proc is None or proc.poll() is not None: |
| 102 |
return |
| 103 |
proc.terminate() |
| 104 |
try: |
| 105 |
proc.wait(timeout=10) |
| 106 |
except subprocess.TimeoutExpired: |
| 107 |
proc.kill() |
| 108 |
proc.wait() |