2 # SPDX-License-Identifier: LGPL-2.1+
4 (( $+functions[_systemctl_command] )) || _systemctl_command()
6 local -a _systemctl_cmds
8 "list-sockets:List sockets"
9 "list-timers:List timers"
10 "list-units:List units"
11 "start:Start (activate) one or more units"
12 "stop:Stop (deactivate) one or more units"
13 "reload:Reload one or more units"
14 "restart:Start or restart one or more units"
15 "condrestart:Restart one or more units if active"
16 "try-restart:Restart one or more units if active"
17 "reload-or-restart:Reload one or more units if possible, otherwise start or restart"
18 "force-reload:Reload one or more units if possible, otherwise restart if active"
19 "hibernate:Hibernate the system"
20 "hybrid-sleep:Hibernate and suspend the system"
21 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
22 "isolate:Start one unit and stop all others"
23 "kill:Send signal to processes of a unit"
24 "is-active:Check whether units are active"
25 "is-failed:Check whether units are failed"
26 "status:Show runtime status of one or more units"
27 "show:Show properties of one or more units/jobs or the manager"
28 "cat:Show the source unit files and drop-ins"
29 "reset-failed:Reset failed state for all, one, or more units"
30 "list-unit-files:List installed unit files"
31 "enable:Enable one or more unit files"
32 "disable:Disable one or more unit files"
33 "add-wants:Add Wants= dependencies to a unit"
34 "add-requires:Add Requires= dependencies to a unit"
35 "reenable:Reenable one or more unit files"
36 "preset:Enable/disable one or more unit files based on preset configuration"
37 "set-default:Set the default target"
38 "get-default:Query the default target"
39 "edit:Edit one or more unit files"
40 "is-system-running:Query overall status of the system"
41 "help:Show documentation for specified units"
42 "list-dependencies:Show unit dependency tree"
43 "mask:Mask one or more units"
44 "unmask:Unmask one or more units"
45 "link:Link one or more units files into the search path"
46 "is-enabled:Check whether unit files are enabled"
48 "cancel:Cancel all, one, or more jobs"
49 "show-environment:Dump environment"
50 "set-environment:Set one or more environment variables"
51 "unset-environment:Unset one or more environment variables"
52 "daemon-reload:Reload systemd manager configuration"
53 "daemon-reexec:Reexecute systemd manager"
54 "default:Enter system default mode"
55 "rescue:Enter system rescue mode"
56 "emergency:Enter system emergency mode"
57 "halt:Shut down and halt the system"
58 "suspend:Suspend the system"
59 "poweroff:Shut down and power-off the system"
60 "reboot:Shut down and reboot the system"
61 "kexec:Shut down and reboot the system with kexec"
62 "exit:Ask for user instance termination"
63 "switch-root:Change root directory"
64 "revert:Revert unit files to their vendor versions"
67 if (( CURRENT == 1 )); then
68 _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
70 local curcontext="$curcontext" expl
72 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
73 # Deal with any aliases
75 condrestart) cmd="try-restart";;
76 force-reload) cmd="try-reload-or-restart";;
80 curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
83 zstyle -s ":completion:${curcontext}:" cache-policy update_policy
84 if [[ -z "$update_policy" ]]; then
85 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
88 _call_function ret _systemctl_$cmd || _message 'no more arguments'
90 _message "unknown systemctl command: $words[1]"
98 systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null
102 # Fills the unit list
103 _systemctl_all_units()
105 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
106 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
108 _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
109 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
113 # Fills the unit list including all file units
114 _systemctl_really_all_units()
116 local -a all_unit_files;
117 local -a really_all_units;
118 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
119 ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
121 all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
123 really_all_units=($_sys_all_units $all_unit_files)
124 _sys_really_all_units=(${(u)really_all_units})
125 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
129 _filter_units_by_property() {
130 local property=$1 value=$2; shift 2
131 local -a units; units=("${(q-)@}")
133 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
134 echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}"
137 _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "*$PREFIX*$SUFFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ }
140 _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "*$PREFIX*$SUFFIX*" )"}%% *} )}
142 _systemctl_startable_units(){
143 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
144 _filter_units_by_property CanStart yes ${${${(f)"$(
145 __systemctl $mode list-unit-files --state enabled,disabled,static "*$PREFIX*$SUFFIX*"
146 __systemctl $mode list-units --state inactive,failed "*$PREFIX*$SUFFIX*"
147 )"}:#*@.*}%%[[:space:]]*}
151 _systemctl_restartable_units(){
152 _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
153 __systemctl $mode list-unit-files --state enabled,disabled,static "*$PREFIX*$SUFFIX*"
154 __systemctl $mode list-units "*$PREFIX*$SUFFIX*"
155 )"}:#*@.*}%%[[:space:]]*} ) )
158 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "*$PREFIX*$SUFFIX*" )"}%% *} ) }
159 _systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files "*$PREFIX*$SUFFIX*" ) ) }
162 # Completion functions for ALL_UNITS
163 for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit revert add-wants add-requires ; do
164 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
166 _systemctl_really_all_units
167 _wanted systemd-units expl unit \
168 compadd "$@" -a - _sys_really_all_units
172 # Completion functions for ENABLED_UNITS
173 (( $+functions[_systemctl_disable] )) || _systemctl_disable()
175 local _sys_unit_state; _systemctl_unit_state
176 _wanted systemd-units expl 'enabled unit' \
177 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
180 (( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
182 local _sys_unit_state; _systemctl_unit_state
183 _wanted systemd-units expl 'enabled/disabled unit' \
184 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
187 # Completion functions for DISABLED_UNITS
188 (( $+functions[_systemctl_enable] )) || _systemctl_enable()
190 local _sys_unit_state; _systemctl_unit_state
191 _wanted systemd-units expl 'disabled unit' \
192 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
195 # Completion functions for FAILED_UNITS
196 (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
198 local _sys_failed_units; _systemctl_failed_units
199 _wanted systemd-units expl 'failed unit' \
200 compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
203 # Completion functions for STARTABLE_UNITS
204 (( $+functions[_systemctl_start] )) || _systemctl_start()
206 local _sys_startable_units; _systemctl_startable_units
207 _wanted systemd-units expl 'startable unit' \
208 compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
211 # Completion functions for STOPPABLE_UNITS
212 for fun in stop kill try-restart condrestart ; do
213 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
215 local _sys_active_units; _systemctl_active_units
216 _wanted systemd-units expl 'stoppable unit' \
217 compadd "$@" - $( _filter_units_by_property CanStop yes \
218 ${_sys_active_units[*]} )
222 # Completion functions for ISOLATABLE_UNITS
223 (( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
226 _wanted systemd-units expl 'isolatable unit' \
227 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
228 ${_sys_all_units[*]} )
231 # Completion functions for RELOADABLE_UNITS
232 for fun in reload try-reload-or-restart force-reload ; do
233 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
235 local _sys_active_units; _systemctl_active_units
236 _wanted systemd-units expl 'reloadable unit' \
237 compadd "$@" - $( _filter_units_by_property CanReload yes \
238 ${_sys_active_units[*]} )
242 # Completion functions for RESTARTABLE_UNITS
243 for fun in restart reload-or-restart ; do
244 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
246 local _sys_restartable_units; _systemctl_restartable_units
247 _wanted systemd-units expl 'restartable unit' \
248 compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
252 # Completion functions for MASKED_UNITS
253 (( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
255 local _sys_unit_state; _systemctl_unit_state
256 _wanted systemd-units expl 'masked unit' \
257 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
260 # Completion functions for JOBS
261 (( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
263 _wanted systemd-jobs expl job \
264 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
265 _message "no jobs found"
268 # Completion functions for TARGETS
269 (( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
271 _wanted systemd-targets expl target \
272 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
273 _message "no targets found"
276 # Completion functions for ENVS
277 for fun in set-environment unset-environment ; do
278 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
280 local fun=$0 ; fun=${fun##_systemctl_}
282 if [[ "${fun}" = "set-environment" ]]; then
285 _wanted systemd-environment expl 'environment variable' \
286 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
290 (( $+functions[_systemctl_link] )) || _systemctl_link() {
294 (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
298 # no systemctl completion for:
299 # [STANDALONE]='daemon-reexec daemon-reload default
300 # emergency exit halt kexec list-jobs list-units
301 # list-unit-files poweroff reboot rescue show-environment'
303 _systemctl_caching_policy()
308 # rebuild if cache is more than a day old
309 oldcache=( "$1"(mh+1) )
310 (( $#oldcache )) && return 0
312 _sysunits=(${${(f)"$(__systemctl --all)"}%% *})
314 if (( $#_sysunits )); then
315 for unit in $_sysunits; do
316 [[ "$unit" -nt "$1" ]] && return 0
325 _states=("${(fo)$(__systemctl --state=help)}")
326 _values -s , "${_states[@]}"
331 _types=("${(fo)$(__systemctl -t help)}")
332 _values -s , "${_types[@]}"
336 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
337 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
339 _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all;
340 @rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*}
342 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
344 _values -s , "${_sys_all_properties[@]}"
349 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
350 _values -s , "${_modes[@]}"
353 # Build arguments for "systemctl" to be used in completion.
354 local -a _modes; _modes=("--user" "--system")
355 # Use the last mode (they are exclusive and the last one is used).
356 local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
358 {-h,--help}'[Show help]' \
359 '--version[Show package version]' \
360 {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
361 '--state=[Display units in the specified state]:unit state:_unit_states' \
362 '--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \
363 {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \
364 {-a,--all}'[Show all units/properties, including dead/empty ones]' \
365 '--reverse[Show reverse dependencies]' \
366 '--after[Show units ordered after]' \
367 '--before[Show units ordered before]' \
368 {-l,--full}"[Don't ellipsize unit names on output]" \
369 '--show-types[When showing sockets, show socket type]' \
370 {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
371 {-q,--quiet}'[Suppress output]' \
372 '--no-block[Do not wait until operation finished]' \
373 '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
374 '--no-pager[Do not pipe output into a pager]' \
375 '--system[Connect to system manager]' \
376 '--user[Connect to user service manager]' \
377 "--no-wall[Don't send wall message before halt/power-off/reboot]" \
378 '--global[Enable/disable/mask unit files globally]' \
379 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
380 '--no-ask-password[Do not ask for system passwords]' \
381 '--kill-who=[Who to send signal to]:killwho:(main control all)' \
382 {-s+,--signal=}'[Which signal to send]:signal:_signals' \
383 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
384 '--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \
385 '--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \
386 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
387 {-P,--privileged}'[Acquire privileges before execution]' \
388 {-n+,--lines=}'[Journal entries to show]:number of entries' \
389 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
390 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
391 '--plain[When used with list-dependencies, print output as a list]' \
392 '--failed[Show failed units]' \
393 '*::systemctl command:_systemctl_command'