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