diff options
| author | Artur Kowalski <arturkow2000@gmail.com> | 2025-01-20 13:45:59 +0100 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2025-01-21 12:07:56 +0000 |
| commit | 1bb9a3caf390b3bd9977639d8d043350cd1a6d6e (patch) | |
| tree | 5497c0714cf068b52372b6e1d5393009b72ee792 /meta | |
| parent | e330214c2c4c16a0ce6e872db42b08e093a6de1d (diff) | |
| download | poky-1bb9a3caf390b3bd9977639d8d043350cd1a6d6e.tar.gz | |
systemd-systemctl: add support for --global flag
The flag is similar to --user flag as it causes systemctl to operate on
user units, but it performs operations globally for all users. This is
required for user presets support.
(From OE-Core rev: ab6476d28485598ae842472a7b15ca7bf244c776)
Signed-off-by: Artur Kowalski <arturkow2000@gmail.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
| -rwxr-xr-x | meta/recipes-core/systemd/systemd-systemctl/systemctl | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/meta/recipes-core/systemd/systemd-systemctl/systemctl b/meta/recipes-core/systemd/systemd-systemctl/systemctl index 2229bc7b6d..81c246a5b2 100755 --- a/meta/recipes-core/systemd/systemd-systemctl/systemctl +++ b/meta/recipes-core/systemd/systemd-systemctl/systemctl | |||
| @@ -29,15 +29,15 @@ class SystemdFile(): | |||
| 29 | 29 | ||
| 30 | _clearable_keys = ['WantedBy'] | 30 | _clearable_keys = ['WantedBy'] |
| 31 | 31 | ||
| 32 | def __init__(self, root, path, instance_unit_name): | 32 | def __init__(self, root, path, instance_unit_name, unit_type): |
| 33 | self.sections = dict() | 33 | self.sections = dict() |
| 34 | self._parse(root, path) | 34 | self._parse(root, path) |
| 35 | dirname = os.path.basename(path.name) + ".d" | 35 | dirname = os.path.basename(path.name) + ".d" |
| 36 | for location in locations: | 36 | for location in locations: |
| 37 | files = (root / location / "system" / dirname).glob("*.conf") | 37 | files = (root / location / unit_type / dirname).glob("*.conf") |
| 38 | if instance_unit_name: | 38 | if instance_unit_name: |
| 39 | inst_dirname = instance_unit_name + ".d" | 39 | inst_dirname = instance_unit_name + ".d" |
| 40 | files = chain(files, (root / location / "system" / inst_dirname).glob("*.conf")) | 40 | files = chain(files, (root / location / unit_type / inst_dirname).glob("*.conf")) |
| 41 | for path2 in sorted(files): | 41 | for path2 in sorted(files): |
| 42 | self._parse(root, path2) | 42 | self._parse(root, path2) |
| 43 | 43 | ||
| @@ -182,21 +182,22 @@ class SystemdUnitNotFoundError(Exception): | |||
| 182 | 182 | ||
| 183 | 183 | ||
| 184 | class SystemdUnit(): | 184 | class SystemdUnit(): |
| 185 | def __init__(self, root, unit): | 185 | def __init__(self, root, unit, unit_type): |
| 186 | self.root = root | 186 | self.root = root |
| 187 | self.unit = unit | 187 | self.unit = unit |
| 188 | self.unit_type = unit_type | ||
| 188 | self.config = None | 189 | self.config = None |
| 189 | 190 | ||
| 190 | def _path_for_unit(self, unit): | 191 | def _path_for_unit(self, unit): |
| 191 | for location in locations: | 192 | for location in locations: |
| 192 | path = self.root / location / "system" / unit | 193 | path = self.root / location / self.unit_type / unit |
| 193 | if path.exists() or path.is_symlink(): | 194 | if path.exists() or path.is_symlink(): |
| 194 | return path | 195 | return path |
| 195 | 196 | ||
| 196 | raise SystemdUnitNotFoundError(self.root, unit) | 197 | raise SystemdUnitNotFoundError(self.root, unit) |
| 197 | 198 | ||
| 198 | def _process_deps(self, config, service, location, prop, dirstem, instance): | 199 | def _process_deps(self, config, service, location, prop, dirstem, instance): |
| 199 | systemdir = self.root / SYSCONFDIR / "systemd" / "system" | 200 | systemdir = self.root / SYSCONFDIR / "systemd" / self.unit_type |
| 200 | 201 | ||
| 201 | target = ROOT / location.relative_to(self.root) | 202 | target = ROOT / location.relative_to(self.root) |
| 202 | try: | 203 | try: |
| @@ -229,7 +230,7 @@ class SystemdUnit(): | |||
| 229 | # ignore aliases | 230 | # ignore aliases |
| 230 | return | 231 | return |
| 231 | 232 | ||
| 232 | config = SystemdFile(self.root, path, instance_unit_name) | 233 | config = SystemdFile(self.root, path, instance_unit_name, self.unit_type) |
| 233 | if instance == "": | 234 | if instance == "": |
| 234 | try: | 235 | try: |
| 235 | default_instance = config.get('Install', 'DefaultInstance')[0] | 236 | default_instance = config.get('Install', 'DefaultInstance')[0] |
| @@ -250,14 +251,14 @@ class SystemdUnit(): | |||
| 250 | try: | 251 | try: |
| 251 | units_enabled.append(unit) | 252 | units_enabled.append(unit) |
| 252 | if also not in units_enabled: | 253 | if also not in units_enabled: |
| 253 | SystemdUnit(self.root, also).enable(units_enabled) | 254 | SystemdUnit(self.root, also, self.unit_type).enable(units_enabled) |
| 254 | except SystemdUnitNotFoundError as e: | 255 | except SystemdUnitNotFoundError as e: |
| 255 | sys.exit("Error: Systemctl also enable issue with %s (%s)" % (service, e.unit)) | 256 | sys.exit("Error: Systemctl also enable issue with %s (%s)" % (service, e.unit)) |
| 256 | 257 | ||
| 257 | except KeyError: | 258 | except KeyError: |
| 258 | pass | 259 | pass |
| 259 | 260 | ||
| 260 | systemdir = self.root / SYSCONFDIR / "systemd" / "system" | 261 | systemdir = self.root / SYSCONFDIR / "systemd" / self.unit_type |
| 261 | target = ROOT / path.relative_to(self.root) | 262 | target = ROOT / path.relative_to(self.root) |
| 262 | try: | 263 | try: |
| 263 | for dest in config.get('Install', 'Alias'): | 264 | for dest in config.get('Install', 'Alias'): |
| @@ -268,15 +269,15 @@ class SystemdUnit(): | |||
| 268 | pass | 269 | pass |
| 269 | 270 | ||
| 270 | def mask(self): | 271 | def mask(self): |
| 271 | systemdir = self.root / SYSCONFDIR / "systemd" / "system" | 272 | systemdir = self.root / SYSCONFDIR / "systemd" / self.unit_type |
| 272 | add_link(systemdir / self.unit, "/dev/null") | 273 | add_link(systemdir / self.unit, "/dev/null") |
| 273 | 274 | ||
| 274 | 275 | ||
| 275 | def collect_services(root): | 276 | def collect_services(root, unit_type): |
| 276 | """Collect list of service files""" | 277 | """Collect list of service files""" |
| 277 | services = set() | 278 | services = set() |
| 278 | for location in locations: | 279 | for location in locations: |
| 279 | paths = (root / location / "system").glob("*") | 280 | paths = (root / location / unit_type).glob("*") |
| 280 | for path in paths: | 281 | for path in paths: |
| 281 | if path.is_dir(): | 282 | if path.is_dir(): |
| 282 | continue | 283 | continue |
| @@ -285,16 +286,16 @@ def collect_services(root): | |||
| 285 | return services | 286 | return services |
| 286 | 287 | ||
| 287 | 288 | ||
| 288 | def preset_all(root): | 289 | def preset_all(root, unit_type): |
| 289 | presets = Presets('system-preset', root) | 290 | presets = Presets('{}-preset'.format(unit_type), root) |
| 290 | services = collect_services(root) | 291 | services = collect_services(root, unit_type) |
| 291 | 292 | ||
| 292 | for service in services: | 293 | for service in services: |
| 293 | state = presets.state(service) | 294 | state = presets.state(service) |
| 294 | 295 | ||
| 295 | if state == "enable" or state is None: | 296 | if state == "enable" or state is None: |
| 296 | try: | 297 | try: |
| 297 | SystemdUnit(root, service).enable() | 298 | SystemdUnit(root, service, unit_type).enable() |
| 298 | except SystemdUnitNotFoundError: | 299 | except SystemdUnitNotFoundError: |
| 299 | sys.exit("Error: Systemctl preset_all issue in %s" % service) | 300 | sys.exit("Error: Systemctl preset_all issue in %s" % service) |
| 300 | 301 | ||
| @@ -320,6 +321,7 @@ def main(): | |||
| 320 | parser.add_argument('--preset-mode', | 321 | parser.add_argument('--preset-mode', |
| 321 | choices=['full', 'enable-only', 'disable-only'], | 322 | choices=['full', 'enable-only', 'disable-only'], |
| 322 | default='full') | 323 | default='full') |
| 324 | parser.add_argument('--global', dest="opt_global", action="store_true", default=False) | ||
| 323 | 325 | ||
| 324 | args = parser.parse_args() | 326 | args = parser.parse_args() |
| 325 | 327 | ||
| @@ -336,16 +338,18 @@ def main(): | |||
| 336 | parser.print_help() | 338 | parser.print_help() |
| 337 | return 0 | 339 | return 0 |
| 338 | 340 | ||
| 341 | unit_type = "user" if args.opt_global else "system" | ||
| 342 | |||
| 339 | if command == "mask": | 343 | if command == "mask": |
| 340 | for service in args.service: | 344 | for service in args.service: |
| 341 | try: | 345 | try: |
| 342 | SystemdUnit(root, service).mask() | 346 | SystemdUnit(root, service, unit_type).mask() |
| 343 | except SystemdUnitNotFoundError as e: | 347 | except SystemdUnitNotFoundError as e: |
| 344 | sys.exit("Error: Systemctl main mask issue in %s (%s)" % (service, e.unit)) | 348 | sys.exit("Error: Systemctl main mask issue in %s (%s)" % (service, e.unit)) |
| 345 | elif command == "enable": | 349 | elif command == "enable": |
| 346 | for service in args.service: | 350 | for service in args.service: |
| 347 | try: | 351 | try: |
| 348 | SystemdUnit(root, service).enable() | 352 | SystemdUnit(root, service, unit_type).enable() |
| 349 | except SystemdUnitNotFoundError as e: | 353 | except SystemdUnitNotFoundError as e: |
| 350 | sys.exit("Error: Systemctl main enable issue in %s (%s)" % (service, e.unit)) | 354 | sys.exit("Error: Systemctl main enable issue in %s (%s)" % (service, e.unit)) |
| 351 | elif command == "preset-all": | 355 | elif command == "preset-all": |
| @@ -353,7 +357,7 @@ def main(): | |||
| 353 | sys.exit("Too many arguments.") | 357 | sys.exit("Too many arguments.") |
| 354 | if args.preset_mode != "enable-only": | 358 | if args.preset_mode != "enable-only": |
| 355 | sys.exit("Only enable-only is supported as preset-mode.") | 359 | sys.exit("Only enable-only is supported as preset-mode.") |
| 356 | preset_all(root) | 360 | preset_all(root, unit_type) |
| 357 | else: | 361 | else: |
| 358 | raise RuntimeError() | 362 | raise RuntimeError() |
| 359 | 363 | ||
