]>
Commit | Line | Data |
---|---|---|
ff7a0685 WG |
1 | #compdef systemctl |
2 | ||
3 | (( $+functions[_systemctl_command] )) || _systemctl_command() | |
4 | { | |
5 | local -a _systemctl_cmds | |
6 | _systemctl_cmds=( | |
d808ca64 | 7 | "list-sockets:List sockets" |
d1811159 | 8 | "list-timers:List timers" |
ff7a0685 WG |
9 | "list-units:List units" |
10 | "start:Start (activate) one or more units" | |
11 | "stop:Stop (deactivate) one or more units" | |
12 | "reload:Reload one or more units" | |
13 | "restart:Start or restart one or more units" | |
14 | "condrestart:Restart one or more units if active" | |
15 | "try-restart:Restart one or more units if active" | |
16 | "reload-or-restart:Reload one or more units if possible, otherwise start or restart" | |
17 | "force-reload:Reload one or more units if possible, otherwise restart if active" | |
18 | "hibernate:Hibernate the system" | |
19 | "hybrid-sleep:Hibernate and suspend the system" | |
aabf5d42 | 20 | "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active" |
ff7a0685 WG |
21 | "isolate:Start one unit and stop all others" |
22 | "kill:Send signal to processes of a unit" | |
23 | "is-active:Check whether units are active" | |
24 | "is-failed:Check whether units are failed" | |
25 | "status:Show runtime status of one or more units" | |
26 | "show:Show properties of one or more units/jobs or the manager" | |
6da49b8b | 27 | "cat:Show the source unit files and drop-ins" |
ff7a0685 | 28 | "reset-failed:Reset failed state for all, one, or more units" |
ff7a0685 WG |
29 | "list-unit-files:List installed unit files" |
30 | "enable:Enable one or more unit files" | |
31 | "disable:Disable one or more unit files" | |
32 | "reenable:Reenable one or more unit files" | |
33 | "preset:Enable/disable one or more unit files based on preset configuration" | |
1cf3c30c ZJS |
34 | "set-default:Set the default target" |
35 | "get-default:Query the default target" | |
409886c4 | 36 | "edit:Edit one or more unit files" |
1cf3c30c | 37 | "is-system-running:Query overall status of the system" |
ff7a0685 WG |
38 | "help:Show documentation for specified units" |
39 | "list-dependencies:Show unit dependency tree" | |
40 | "mask:Mask one or more units" | |
41 | "unmask:Unmask one or more units" | |
42 | "link:Link one or more units files into the search path" | |
43 | "is-enabled:Check whether unit files are enabled" | |
44 | "list-jobs:List jobs" | |
45 | "cancel:Cancel all, one, or more jobs" | |
ff7a0685 WG |
46 | "show-environment:Dump environment" |
47 | "set-environment:Set one or more environment variables" | |
48 | "unset-environment:Unset one or more environment variables" | |
49 | "daemon-reload:Reload systemd manager configuration" | |
50 | "daemon-reexec:Reexecute systemd manager" | |
51 | "default:Enter system default mode" | |
52 | "rescue:Enter system rescue mode" | |
53 | "emergency:Enter system emergency mode" | |
54 | "halt:Shut down and halt the system" | |
55 | "suspend:Suspend the system" | |
56 | "poweroff:Shut down and power-off the system" | |
57 | "reboot:Shut down and reboot the system" | |
58 | "kexec:Shut down and reboot the system with kexec" | |
59 | "exit:Ask for user instance termination" | |
7b742b31 | 60 | "switch-root:Change root directory" |
ff7a0685 WG |
61 | ) |
62 | ||
63 | if (( CURRENT == 1 )); then | |
64 | _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@" | |
65 | else | |
d34b7c11 | 66 | local curcontext="$curcontext" expl |
ff7a0685 WG |
67 | |
68 | cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}" | |
69 | # Deal with any aliases | |
70 | case $cmd in | |
71 | condrestart) cmd="try-restart";; | |
aabf5d42 | 72 | force-reload) cmd="try-reload-or-restart";; |
ff7a0685 WG |
73 | esac |
74 | ||
75 | if (( $#cmd )); then | |
76 | curcontext="${curcontext%:*:*}:systemctl-${cmd}:" | |
77 | ||
78 | local update_policy | |
79 | zstyle -s ":completion:${curcontext}:" cache-policy update_policy | |
80 | if [[ -z "$update_policy" ]]; then | |
81 | zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy | |
82 | fi | |
83 | ||
84 | _call_function ret _systemctl_$cmd || _message 'no more arguments' | |
85 | else | |
86 | _message "unknown systemctl command: $words[1]" | |
87 | fi | |
88 | return ret | |
89 | fi | |
90 | } | |
91 | ||
92 | __systemctl() | |
93 | { | |
99171d2f | 94 | systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null |
ff7a0685 WG |
95 | } |
96 | ||
97 | ||
98 | # Fills the unit list | |
99 | _systemctl_all_units() | |
100 | { | |
101 | if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) && | |
102 | ! _retrieve_cache SYS_ALL_UNITS; | |
103 | then | |
2103d29d | 104 | _sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} ) |
ff7a0685 WG |
105 | _store_cache SYS_ALL_UNITS _sys_all_units |
106 | fi | |
107 | } | |
108 | ||
109 | # Fills the unit list including all file units | |
110 | _systemctl_really_all_units() | |
111 | { | |
112 | local -a all_unit_files; | |
113 | local -a really_all_units; | |
114 | if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) && | |
115 | ! _retrieve_cache SYS_REALLY_ALL_UNITS; | |
116 | then | |
2103d29d | 117 | all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} ) |
ff7a0685 WG |
118 | _systemctl_all_units |
119 | really_all_units=($_sys_all_units $all_unit_files) | |
120 | _sys_really_all_units=(${(u)really_all_units}) | |
121 | _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units | |
122 | fi | |
123 | } | |
124 | ||
125 | _filter_units_by_property() { | |
6c9414a7 EC |
126 | local property=$1 value=$2; shift 2 |
127 | local -a units; units=("${(q-)@}") | |
128 | local -A props | |
129 | props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"}) | |
130 | echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}" | |
ff7a0685 WG |
131 | } |
132 | ||
e4e868f3 | 133 | _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ } |
e9a19bd8 | 134 | |
f29c77bc | 135 | |
2103d29d | 136 | _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )} |
81333ecf ZJS |
137 | |
138 | _systemctl_startable_units(){ | |
67afa931 | 139 | _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $( |
81333ecf ZJS |
140 | _filter_units_by_property CanStart yes $( |
141 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ | |
142 | { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } | |
143 | __systemctl $mode list-units --state inactive,failed | \ | |
67afa931 | 144 | { while read -r a b; do echo -E - " $a"; done; } )) ) ) |
81333ecf ZJS |
145 | } |
146 | ||
147 | _systemctl_restartable_units(){ | |
67afa931 | 148 | _sys_restartable_units=( $(_filter_units_by_property CanStart yes $( |
81333ecf ZJS |
149 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ |
150 | { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } | |
151 | __systemctl $mode list-units | \ | |
67afa931 | 152 | { while read -r a b; do echo -E - " $a"; done; } )) ) |
81333ecf ZJS |
153 | } |
154 | ||
ed119049 | 155 | _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) } |
fb869ca1 | 156 | _systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) } |
ff7a0685 | 157 | |
67afa931 | 158 | local fun |
ff7a0685 | 159 | # Completion functions for ALL_UNITS |
409886c4 | 160 | for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit ; do |
ff7a0685 WG |
161 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() |
162 | { | |
163 | _systemctl_really_all_units | |
d34b7c11 EC |
164 | _wanted systemd-units expl unit \ |
165 | compadd "$@" -a - _sys_really_all_units | |
ff7a0685 WG |
166 | } |
167 | done | |
168 | ||
169 | # Completion functions for ENABLED_UNITS | |
e9a19bd8 ZJS |
170 | (( $+functions[_systemctl_disable] )) || _systemctl_disable() |
171 | { | |
fb869ca1 | 172 | local _sys_unit_state; _systemctl_unit_state |
d34b7c11 EC |
173 | _wanted systemd-units expl 'enabled unit' \ |
174 | compadd "$@" - ${(k)_sys_unit_state[(R)enabled]} | |
e9a19bd8 ZJS |
175 | } |
176 | ||
177 | (( $+functions[_systemctl_reenable] )) || _systemctl_reenable() | |
178 | { | |
fb869ca1 | 179 | local _sys_unit_state; _systemctl_unit_state |
d34b7c11 EC |
180 | _wanted systemd-units expl 'enabled/disabled unit' \ |
181 | compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names) | |
e9a19bd8 | 182 | } |
ff7a0685 WG |
183 | |
184 | # Completion functions for DISABLED_UNITS | |
185 | (( $+functions[_systemctl_enable] )) || _systemctl_enable() | |
186 | { | |
fb869ca1 | 187 | local _sys_unit_state; _systemctl_unit_state |
d34b7c11 EC |
188 | _wanted systemd-units expl 'disabled unit' \ |
189 | compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names) | |
ff7a0685 WG |
190 | } |
191 | ||
192 | # Completion functions for FAILED_UNITS | |
193 | (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed() | |
194 | { | |
463985a9 | 195 | local _sys_failed_units; _systemctl_failed_units |
d34b7c11 EC |
196 | _wanted systemd-units expl 'failed unit' \ |
197 | compadd "$@" -a - _sys_failed_units || _message "no failed unit found" | |
ff7a0685 WG |
198 | } |
199 | ||
200 | # Completion functions for STARTABLE_UNITS | |
201 | (( $+functions[_systemctl_start] )) || _systemctl_start() | |
202 | { | |
bf8864c2 | 203 | local _sys_startable_units; _systemctl_startable_units |
d34b7c11 EC |
204 | _wanted systemd-units expl 'startable unit' \ |
205 | compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names) | |
ff7a0685 WG |
206 | } |
207 | ||
208 | # Completion functions for STOPPABLE_UNITS | |
209 | for fun in stop kill try-restart condrestart ; do | |
210 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
211 | { | |
bf8864c2 | 212 | local _sys_active_units; _systemctl_active_units |
d34b7c11 EC |
213 | _wanted systemd-units expl 'stoppable unit' \ |
214 | compadd "$@" - $( _filter_units_by_property CanStop yes \ | |
215 | ${_sys_active_units[*]} ) | |
ff7a0685 WG |
216 | } |
217 | done | |
218 | ||
219 | # Completion functions for ISOLATABLE_UNITS | |
220 | (( $+functions[_systemctl_isolate] )) || _systemctl_isolate() | |
221 | { | |
222 | _systemctl_all_units | |
d34b7c11 EC |
223 | _wanted systemd-units expl 'isolatable unit' \ |
224 | compadd "$@" - $( _filter_units_by_property AllowIsolate yes \ | |
225 | ${_sys_all_units[*]} ) | |
ff7a0685 WG |
226 | } |
227 | ||
228 | # Completion functions for RELOADABLE_UNITS | |
aabf5d42 | 229 | for fun in reload try-reload-or-restart force-reload ; do |
ff7a0685 WG |
230 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() |
231 | { | |
bf8864c2 | 232 | local _sys_active_units; _systemctl_active_units |
d34b7c11 EC |
233 | _wanted systemd-units expl 'reloadable unit' \ |
234 | compadd "$@" - $( _filter_units_by_property CanReload yes \ | |
235 | ${_sys_active_units[*]} ) | |
ff7a0685 WG |
236 | } |
237 | done | |
238 | ||
239 | # Completion functions for RESTARTABLE_UNITS | |
240 | for fun in restart reload-or-restart ; do | |
241 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
242 | { | |
bf8864c2 | 243 | local _sys_restartable_units; _systemctl_restartable_units |
d34b7c11 EC |
244 | _wanted systemd-units expl 'restartable unit' \ |
245 | compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names) | |
ff7a0685 WG |
246 | } |
247 | done | |
248 | ||
249 | # Completion functions for MASKED_UNITS | |
250 | (( $+functions[_systemctl_unmask] )) || _systemctl_unmask() | |
251 | { | |
fb869ca1 | 252 | local _sys_unit_state; _systemctl_unit_state |
d34b7c11 EC |
253 | _wanted systemd-units expl 'masked unit' \ |
254 | compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found" | |
ff7a0685 WG |
255 | } |
256 | ||
257 | # Completion functions for JOBS | |
258 | (( $+functions[_systemctl_cancel] )) || _systemctl_cancel() | |
259 | { | |
d34b7c11 EC |
260 | _wanted systemd-jobs expl job \ |
261 | compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} || | |
262 | _message "no jobs found" | |
ff7a0685 WG |
263 | } |
264 | ||
1cf3c30c ZJS |
265 | # Completion functions for TARGETS |
266 | (( $+functions[_systemctl_set-default] )) || _systemctl_set-default() | |
267 | { | |
d34b7c11 EC |
268 | _wanted systemd-targets expl target \ |
269 | compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} || | |
270 | _message "no targets found" | |
ff7a0685 WG |
271 | } |
272 | ||
273 | # Completion functions for ENVS | |
274 | for fun in set-environment unset-environment ; do | |
275 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
276 | { | |
277 | local fun=$0 ; fun=${fun##_systemctl_} | |
278 | local suf | |
279 | if [[ "${fun}" = "set-environment" ]]; then | |
280 | suf='-S=' | |
281 | fi | |
d34b7c11 EC |
282 | _wanted systemd-environment expl 'environment variable' \ |
283 | compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*} | |
ff7a0685 WG |
284 | } |
285 | done | |
286 | ||
2c12a402 ZJS |
287 | (( $+functions[_systemctl_link] )) || _systemctl_link() { |
288 | _sd_unit_files | |
289 | } | |
ff7a0685 | 290 | |
7b742b31 ZJS |
291 | (( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() { |
292 | _files | |
293 | } | |
294 | ||
ff7a0685 | 295 | # no systemctl completion for: |
299c397c | 296 | # [STANDALONE]='daemon-reexec daemon-reload default |
ff7a0685 WG |
297 | # emergency exit halt kexec list-jobs list-units |
298 | # list-unit-files poweroff reboot rescue show-environment' | |
ff7a0685 WG |
299 | |
300 | _systemctl_caching_policy() | |
301 | { | |
302 | local _sysunits | |
303 | local -a oldcache | |
304 | ||
305 | # rebuild if cache is more than a day old | |
306 | oldcache=( "$1"(mh+1) ) | |
307 | (( $#oldcache )) && return 0 | |
308 | ||
2103d29d | 309 | _sysunits=(${${(f)"$(__systemctl --all)"}%% *}) |
ff7a0685 WG |
310 | |
311 | if (( $#_sysunits )); then | |
312 | for unit in $_sysunits; do | |
313 | [[ "$unit" -nt "$1" ]] && return 0 | |
314 | done | |
315 | fi | |
316 | ||
317 | return 1 | |
318 | } | |
319 | ||
298b9e23 WG |
320 | _unit_states() { |
321 | local -a _states | |
840b2c0e | 322 | _states=("${(fo)$(__systemctl --state=help)}") |
298b9e23 WG |
323 | _values -s , "${_states[@]}" |
324 | } | |
325 | ||
326 | _unit_types() { | |
327 | local -a _types | |
840b2c0e | 328 | _types=("${(fo)$(__systemctl -t help)}") |
298b9e23 WG |
329 | _values -s , "${_types[@]}" |
330 | } | |
331 | ||
c0a67aef ZJS |
332 | _unit_properties() { |
333 | if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) && | |
334 | ! _retrieve_cache SYS_ALL_PROPERTIES; | |
335 | then | |
2103d29d EC |
336 | _sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all; |
337 | @rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*} | |
338 | ) | |
c0a67aef ZJS |
339 | _store_cache SYS_ALL_PROPRTIES _sys_all_properties |
340 | fi | |
341 | _values -s , "${_sys_all_properties[@]}" | |
342 | } | |
343 | ||
903e7c37 ZJS |
344 | _job_modes() { |
345 | local -a _modes | |
346 | _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush) | |
347 | _values -s , "${_modes[@]}" | |
348 | } | |
349 | ||
e09d0d46 | 350 | # Build arguments for "systemctl" to be used in completion. |
68c4f6d4 | 351 | local -a _modes; _modes=("--user" "--system") |
e09d0d46 DH |
352 | # Use the last mode (they are exclusive and the last one is used). |
353 | local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]} | |
ff7a0685 WG |
354 | _arguments -s \ |
355 | {-h,--help}'[Show help]' \ | |
356 | '--version[Show package version]' \ | |
862f4963 | 357 | {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \ |
903e7c37 ZJS |
358 | '--state=[Display units in the specified state]:unit state:_unit_states' \ |
359 | '--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \ | |
c0a67aef | 360 | {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \ |
ff7a0685 WG |
361 | {-a,--all}'[Show all units/properties, including dead/empty ones]' \ |
362 | '--reverse[Show reverse dependencies]' \ | |
363 | '--after[Show units ordered after]' \ | |
364 | '--before[Show units ordered before]' \ | |
ff7a0685 | 365 | {-l,--full}"[Don't ellipsize unit names on output]" \ |
ff7a0685 | 366 | '--show-types[When showing sockets, show socket type]' \ |
ff7a0685 WG |
367 | {-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \ |
368 | {-q,--quiet}'[Suppress output]' \ | |
369 | '--no-block[Do not wait until operation finished]' \ | |
370 | '--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \ | |
371 | '--no-pager[Do not pipe output into a pager]' \ | |
372 | '--system[Connect to system manager]' \ | |
373 | '--user[Connect to user service manager]' \ | |
374 | "--no-wall[Don't send wall message before halt/power-off/reboot]" \ | |
375 | '--global[Enable/disable unit files globally]' \ | |
376 | "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \ | |
377 | '--no-ask-password[Do not ask for system passwords]' \ | |
378 | '--kill-who=[Who to send signal to]:killwho:(main control all)' \ | |
862f4963 | 379 | {-s+,--signal=}'[Which signal to send]:signal:_signals' \ |
ff7a0685 WG |
380 | {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \ |
381 | '--root=[Enable unit files in the specified root directory]:directory:_directories' \ | |
382 | '--runtime[Enable unit files only temporarily until next reboot]' \ | |
6da49b8b | 383 | {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \ |
ff7a0685 | 384 | {-P,--privileged}'[Acquire privileges before execution]' \ |
862f4963 | 385 | {-n+,--lines=}'[Journal entries to show]:number of entries' \ |
a02c5fe7 | 386 | {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \ |
5bdf2243 | 387 | '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \ |
ff7a0685 WG |
388 | '--plain[When used with list-dependencies, print output as a list]' \ |
389 | '*::systemctl command:_systemctl_command' |