diff --git a/src/assign-cgroups.py b/src/assign-cgroups.py index 0a49669..55f90a6 100755 --- a/src/assign-cgroups.py +++ b/src/assign-cgroups.py @@ -15,6 +15,7 @@ import argparse import asyncio import logging +import re import socket import struct from typing import Optional @@ -46,10 +47,23 @@ # Launcher should only be listed here if it creates cgroup of its own. LAUNCHER_APPS = ["nwgbar", "nwgdmenu", "nwggrid"] +SD_UNIT_ESCAPE_RE = re.compile(r"[^\w:.\\]", re.ASCII) + def escape_app_id(app_id: str) -> str: - """Escape app_id for systemd APIs""" - return app_id.replace("-", "\\x2d") + """Escape app_id for systemd APIs. + + The "unit prefix" must consist of one or more valid characters (ASCII letters, + digits, ":", "-", "_", ".", and "\"). The total length of the unit name including + the suffix must not exceed 256 characters. [systemd.unit(5)] + + We also want to escape "-" to avoid creating extra slices. + """ + + def repl(match): + return "".join([f"\\x{x:02x}" for x in match.group().encode()]) + + return SD_UNIT_ESCAPE_RE.sub(repl, app_id) LAUNCHER_APP_CGROUPS = [