2 # SPDX-License-Identifier: LGPL-2.1-or-later
4 (( $+functions[_systemctl_commands] )) || _systemctl_commands()
8 local -a unit_commands=(
10 "list-automounts:List automounts"
11 "list-paths:List paths"
12 "list-sockets:List sockets"
13 "list-timers:List timers"
14 "list-units:List units"
15 "start:Start (activate) one or more units"
16 "stop:Stop (deactivate) one or more units"
17 "reload:Reload one or more units"
18 "restart:Start or restart one or more units"
19 "condrestart:Restart one or more units if active"
20 "try-restart:Restart one or more units if active"
21 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
22 "force-reload:Reload one or more units if possible, otherwise restart if active"
23 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
24 "isolate:Start one unit and stop all others"
25 "kill:Send signal to processes of a unit"
26 "is-active:Check whether units are active"
27 "is-failed:Check whether units are failed"
28 "status:Show runtime status of one or more units"
29 "show:Show properties of one or more units/jobs or the manager"
30 "cat:Show the source unit files and drop-ins"
31 "set-property:Sets one or more properties of a unit"
32 "service-log-level:Get or set the logging threshold for service"
33 "service-log-target:Get or set the logging target for service"
34 "help:Show documentation for specified units"
35 "reset-failed:Reset failed state for all, one, or more units"
36 "list-dependencies:Show unit dependency tree"
37 "clean:Remove configuration, state, cache, logs or runtime data of units"
38 "bind:Bind mount a path from the host into a unit's namespace"
39 "mount-image:Mount an image from the host into a unit's namespace"
40 "whoami:Determines as part of which unit the command is being invoked"
43 local -a machine_commands=(
45 "list-machines:List the host and all running local containers"
48 local -a unit_file_commands=(
50 "list-unit-files:List installed unit files"
51 "enable:Enable one or more unit files"
52 "disable:Disable one or more unit files"
53 "reenable:Reenable one or more unit files"
54 "preset:Enable/disable one or more unit files based on preset configuration"
55 "preset-all:Enable/disable all unit files based on preset configuration"
56 "is-enabled:Check whether unit files are enabled"
57 "mask:Mask one or more units"
58 "unmask:Unmask one or more units"
59 "link:Link one or more units files into the search path"
60 "revert:Revert unit files to their vendor versions"
61 "add-wants:Add Wants= dependencies to a unit"
62 "add-requires:Add Requires= dependencies to a unit"
63 "set-default:Set the default target"
64 "get-default:Query the default target"
65 "edit:Edit one or more unit files"
68 local -a job_commands=(
71 "cancel:Cancel all, one, or more jobs"
74 local -a environment_commands=(
75 # Environment Commands
76 "show-environment:Dump environment"
77 "set-environment:Set one or more environment variables"
78 "unset-environment:Unset one or more environment variables"
79 "import-environment:Import environment variables set on the client"
82 local -a manager_state_commands=(
83 # Manager State Commands
84 "daemon-reload:Reload systemd manager configuration"
85 "daemon-reexec:Reexecute systemd manager"
86 "log-level:Get or set the log level"
87 "log-target:Get or set the log target"
88 "service-watchdogs:Get or set the state of software watchdogs"
91 local -a system_commands=(
93 "is-system-running:Query overall status of the system"
94 "default:Enter system default mode"
95 "rescue:Enter system rescue mode"
96 "emergency:Enter system emergency mode"
97 "halt:Shut down and halt the system"
98 "suspend:Suspend the system"
99 "poweroff:Shut down and power-off the system"
100 "reboot:Shut down and reboot the system"
101 "soft-reboot:Shut down and reboot the userspace"
102 "kexec:Shut down and reboot the system with kexec"
103 "exit:Ask for user instance termination"
104 "switch-root:Change root directory"
105 "hibernate:Hibernate the system"
106 "hybrid-sleep:Hibernate and suspend the system"
107 "suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it"
108 "sleep:Put the system to sleep"
111 local -a groups=( unit machine unit_file job environment manager_state system )
112 local -a _systemctl_cmds
114 _systemctl_cmds+=( "${(@P)${:-"${i}_commands"}}" )
117 if (( CURRENT == 1 )); then
118 _tags ${^groups//_/-}-commands
121 if _requested ${i//_/-}-commands; then
122 _describe -t ${i//_/-}-commands "${i//_/ } command" ${i}_commands &&
128 local curcontext="$curcontext"
130 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
131 # Deal with any aliases
133 condrestart) cmd="try-restart";;
134 force-reload) cmd="try-reload-or-restart";;
138 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
141 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
142 if [[ -z "$update_policy" ]]; then
143 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
146 _call_function ret _systemctl_$cmd || _message 'no more arguments'
148 _message "unknown systemctl command: $words[1]"
154 # @todo _systemd-run has a helper with the same name, so we must redefine
157 command systemctl $_sys_service_mgr --full --legend=no --no-pager --plain "$@" 2>/dev/null
161 # Fills the unit list
162 (( $+functions[_systemctl_all_units] )) ||
163 _systemctl_all_units()
165 if _cache_invalid SYS_ALL_UNITS$_sys_service_mgr || ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr
167 _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
168 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
172 # Fills the unit list including all file units
173 (( $+functions[_systemctl_really_all_units] )) ||
174 _systemctl_really_all_units()
176 local -a all_unit_files
177 local -a really_all_units
178 if _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr || ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr
180 all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
182 really_all_units=($_sys_all_units $all_unit_files)
183 _sys_really_all_units=(${(u)really_all_units})
184 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
188 (( $+functions[_filter_units_by_property] )) ||
189 _filter_units_by_property() {
190 local property=$1 value=$2; shift 2
191 local -a units; units=("${(q-)@}")
193 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
194 echo -E - "${(@)${(k@)props[(Re)$property=$value]}#Id=}"
197 (( $+functions[_systemctl_get_non_template_names] )) ||
198 _systemctl_get_non_template_names() {
199 _systemctl_really_all_units
200 print -r - ${_sys_really_all_units:#*@.*}
203 (( $+functions[_systemctl_get_template_names] )) ||
204 _systemctl_get_template_names() {
205 local pathkind=systemd-search-${_sys_service_mgr##*--}-unit
206 print -r - ${(s-:-)^$(_call_program $pathkind systemd-path $pathkind)}/*@.(${(~j.|.)$(__systemctl --type=help)})(N:t:r)
209 (( $+functions[_systemctl_active_units] )) ||
210 _systemctl_active_units() {
212 if zstyle -T ":completion:$curcontext" use-pattern; then
213 pattern="$PREFIX*$SUFFIX"
215 _sys_active_units=( ${${(f)"$(__systemctl list-units $pattern)"}%% *} )
218 (( $+functions[_systemctl_startable_units] )) ||
219 _systemctl_startable_units(){
221 if zstyle -T ":completion:$curcontext" use-pattern; then
222 pattern="$PREFIX*$SUFFIX"
224 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
225 _filter_units_by_property CanStart yes ${${${(f)"$(
226 __systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient $pattern
227 __systemctl list-units --state inactive,failed $pattern
228 )"}:#*@.*}%%[[:space:]]*}
232 (( $+functions[_systemctl_restartable_units] )) ||
233 _systemctl_restartable_units(){
235 if zstyle -T ":completion:$curcontext" use-pattern; then
236 pattern="$PREFIX*$SUFFIX"
238 _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
239 __systemctl list-unit-files --state enabled,disabled,static $pattern
240 __systemctl list-units $pattern
241 )"}:#*@.*}%%[[:space:]]*} ) )
244 (( $+functions[_systemctl_failed_units] )) ||
245 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) }
247 (( $+functions[_systemctl_unit_state] )) ||
248 _systemctl_unit_state() {
249 setopt localoptions extendedglob
251 if zstyle -T ":completion:$curcontext" use-pattern; then
252 pattern="$PREFIX*$SUFFIX"
254 typeset -gA _sys_unit_state
255 _sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files $pattern)"}%%[[:space:]]#}% *} )
259 # Completion functions for ALL_UNITS
260 for fun in cat mask ; do
261 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
263 _systemctl_really_all_units
264 _wanted systemd-units expl unit \
265 compadd "$@" -a - _sys_really_all_units
269 # Completion functions for NONTEMPLATE_UNITS
270 for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires set-property; do
271 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
273 _wanted systemd-units expl unit \
274 compadd "$@" - $(_systemctl_get_non_template_names)
278 # Completion functions for ENABLED_UNITS
279 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
281 local _sys_unit_state; _systemctl_unit_state
282 _wanted systemd-units expl 'enabled unit' \
283 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
286 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
288 local _sys_unit_state; _systemctl_unit_state
289 _wanted systemd-units expl 'enabled/disabled unit' \
290 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
293 # Completion functions for DISABLED_UNITS
294 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
296 local _sys_unit_state; _systemctl_unit_state
297 _wanted systemd-units expl 'disabled unit' \
298 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
301 # Completion functions for FAILED_UNITS
302 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
304 local _sys_failed_units; _systemctl_failed_units
305 _wanted systemd-units expl 'failed unit' \
306 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
309 # Completion functions for STARTABLE_UNITS
310 (( $+functions[_systemctl_start] )) || _systemctl_start()
312 local _sys_startable_units; _systemctl_startable_units
313 _wanted systemd-units expl 'startable unit' \
314 compadd "$@" - ${_sys_startable_units[*]}
317 # Completion functions for STOPPABLE_UNITS
318 for fun in stop kill try-restart condrestart ; do
319 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
321 local _sys_active_units; _systemctl_active_units
322 _wanted systemd-units expl 'stoppable unit' \
323 compadd "$@" - $( _filter_units_by_property CanStop yes \
324 ${_sys_active_units[*]} )
328 (( $+functions[_systemctl_service-log-level] )) ||
329 _systemctl_service-log-level() {
330 local -a log_levels=( emerg alert crit err warning notice info debug )
331 local _sys_active_units; _systemctl_active_units
332 if (( CURRENT == 2 )); then
333 _wanted systemd-units expl 'active unit' \
334 compadd "$@" -a - _sys_active_units || _message "no units found"
336 compadd "$@" -a - log_levels
340 (( $+functions[_systemctl_service-log-target] )) ||
341 _systemctl_service-log-target() {
342 local -a log_targets=( console kmsg journal syslog null auto )
343 local _sys_active_units; _systemctl_active_units
344 if (( CURRENT == 2 )); then
345 _wanted systemd-units expl 'active unit' \
346 compadd "$@" -a - _sys_active_units || _message "no units found"
348 compadd "$@" -a - log_targets
352 # Completion functions for ISOLATABLE_UNITS
353 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
356 _wanted systemd-units expl 'isolatable unit' \
357 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
358 ${_sys_all_units[*]} )
361 # Completion functions for RELOADABLE_UNITS
362 for fun in reload try-reload-or-restart force-reload ; do
363 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
365 local _sys_active_units; _systemctl_active_units
366 _wanted systemd-units expl 'reloadable unit' \
367 compadd "$@" - $( _filter_units_by_property CanReload yes \
368 ${_sys_active_units[*]} )
372 # Completion functions for RESTARTABLE_UNITS
373 for fun in restart reload-or-restart ; do
374 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
376 local _sys_restartable_units; _systemctl_restartable_units
377 _wanted systemd-units expl 'restartable unit' \
378 compadd "$@" - ${_sys_restartable_units[*]}
382 # Completion functions for MASKED_UNITS
383 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
385 local _sys_unit_state; _systemctl_unit_state
386 _wanted systemd-units expl 'masked unit' \
387 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
390 # Completion functions for JOBS
391 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
393 _wanted systemd-jobs expl job \
394 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
395 _message "no jobs found"
398 # Completion functions for TARGETS
399 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
401 _wanted systemd-targets expl target \
402 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
403 _message "no targets found"
406 # Completion functions for ENVS
407 for fun in set-environment unset-environment ; do
408 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
410 local fun=$0 ; fun=${fun##_systemctl_}
412 if [[ "${fun}" = "set-environment" ]]; then
415 _wanted systemd-environment expl 'environment variable' \
416 compadd "$@" ${suf} - ${${(f)"$(systemctl "$_sys_service_mgr" show-environment)"}%%=*}
420 (( $+functions[_systemctl_import-environment] )) || _systemctl_import-environment()
425 (( $+functions[_systemctl_link] )) || _systemctl_link() {
429 (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
433 (( $+functions[_systemctl_bind] )) || _systemctl_bind() {
437 (( $+functions[_systemctl_mount-image] )) || _systemctl_mount-image() {
441 # no systemctl completion for:
442 # [STANDALONE]='daemon-reexec daemon-reload default
443 # emergency exit halt kexec list-jobs list-units
444 # list-unit-files poweroff reboot rescue show-environment'
446 (( $+functions[_systemctl_caching_policy] )) ||
447 _systemctl_caching_policy()
449 # rebuild if cache is more than a day old
450 [[ -n $1(#qNmd+1) ]] && return 0
452 local pathkind=systemd-search-${1##*--}-unit
453 for dir in ${(s-:-)^$(_call_program $pathkind systemd-path $pathkind)}; do
454 [[ $dir -nt $1 ]] && return 0
460 (( $+functions[_systemctl_unit_states] )) ||
461 _systemctl_unit_states() {
463 _states=("${(fo)$(__systemctl --state=help)}")
464 _values -s , "${_states[@]}"
467 (( $+functions[_systemctl_unit_types] )) ||
468 _systemctl_unit_types() {
470 _types=("${(fo)$(__systemctl -t help)}")
471 _values -s , "${_types[@]}"
474 (( $+functions[_systemctl_unit_properties] )) ||
475 _systemctl_unit_properties() {
476 local -a _sys_all_properties=( ${(f)"$({{LIBEXECDIR}}/systemd --no-pager --dump-bus-properties 2>/dev/null)"} )
477 _wanted systemd-unit-properties expl 'unit property' \
478 _values -s , "${_sys_all_properties[@]}"
481 (( $+functions[_systemctl_job_modes] )) ||
482 _systemctl_job_modes() {
484 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
485 _values -s , "${_modes[@]}"
488 (( $+functions[_systemctl_timestamp] )) ||
489 _systemctl_timestamp() {
491 _styles=(help pretty us µs utc us+utc µs+utc)
492 _values -s , "${_styles[@]}"
495 (( $+functions[_systemctl_check_inhibitors] )) ||
496 _systemctl_check_inhibitors() {
499 _values -s , "${_modes[@]}"
502 # Build arguments for "systemctl" to be used in completion.
503 # Use the last mode, or --system (they are exclusive and the last one is used).
504 local _sys_service_mgr=${words[(R)(--user|--system)]:---system}
506 '(- *)'{-h,--help}'[Show help]' \
507 '(- *)--version[Show package version]' \
508 '(-t --type)'{-t+,--type=}'[List only units of a particular type]:unit type:_systemctl_unit_types' \
509 '--state=[Display units in the specified state]:unit state:_systemctl_unit_states' \
510 '--job-mode=[Specify how to deal with other jobs]:mode:_systemctl_job_modes' \
511 '(-p --property)'{-p+,--property=}'[Show only properties by specific name]:unit property:_systemctl_unit_properties' \
512 '(-a --all)'{-a,--all}'[Show all units/properties, including dead/empty ones]' \
513 '--reverse[Show reverse dependencies]' \
514 '--after[Show units ordered after]' \
515 '--before[Show units ordered before]' \
516 '(-l --full)'{-l,--full}"[Don't ellipsize unit names on output]" \
517 '--show-types[When showing sockets, show socket type]' \
518 '--check-inhibitors[Specify if inhibitors should be checked]:mode:_systemctl_check_inhibitors' \
519 '(-q --quiet)'{-q,--quiet}'[Suppress output]' \
520 '--no-warn[Suppress several warnings shown by default]' \
521 '--no-block[Do not wait until operation finished]' \
522 '--legend=no[Do not print a legend, i.e. the column headers and the footer with hints]' \
523 '--no-pager[Do not pipe output into a pager]' \
524 '--system[Connect to system manager]' \
525 '--user[Connect to user service manager]' \
526 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
527 '--global[Enable/disable/mask default user unit files globally]' \
528 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
529 '--no-ask-password[Do not ask for system passwords]' \
530 '--kill-whom=[Whom to send signal to]:killwhom:(main control all)' \
531 '(-s --signal)'{-s+,--signal=}'[Which signal to send]:signal:_signals' \
532 '(-f --force)'{-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
533 '--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \
534 '--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \
535 '(-H --host)'{-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
536 '(-P --privileged)'{-P,--privileged}'[Acquire privileges before execution]' \
537 '(-n --lines)'{-n+,--lines=}'[Journal entries to show]:number of entries' \
538 '(-o --output)'{-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
539 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
540 '--plain[When used with list-dependencies, print output as a list]' \
541 '--failed[Show failed units]' \
542 '--timestamp=[Change format of printed timestamps]:style:_systemctl_timestamp' \
543 '*::systemctl command:_systemctl_commands'