]>
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" | |
62 | ) | |
63 | ||
64 | if (( CURRENT == 1 )); then | |
65 | _describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@" | |
66 | else | |
67 | local curcontext="$curcontext" | |
68 | ||
69 | cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}" | |
70 | # Deal with any aliases | |
71 | case $cmd in | |
72 | condrestart) cmd="try-restart";; | |
73 | force-reload) cmd="reload-or-try-restart";; | |
74 | esac | |
75 | ||
76 | if (( $#cmd )); then | |
77 | curcontext="${curcontext%:*:*}:systemctl-${cmd}:" | |
78 | ||
79 | local update_policy | |
80 | zstyle -s ":completion:${curcontext}:" cache-policy update_policy | |
81 | if [[ -z "$update_policy" ]]; then | |
82 | zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy | |
83 | fi | |
84 | ||
85 | _call_function ret _systemctl_$cmd || _message 'no more arguments' | |
86 | else | |
87 | _message "unknown systemctl command: $words[1]" | |
88 | fi | |
89 | return ret | |
90 | fi | |
91 | } | |
92 | ||
93 | __systemctl() | |
94 | { | |
95 | local -a _modes | |
96 | _modes=("--user" "--system") | |
97 | systemctl ${words:*_modes} --full --no-legend --no-pager "$@" | |
98 | } | |
99 | ||
100 | ||
101 | # Fills the unit list | |
102 | _systemctl_all_units() | |
103 | { | |
104 | if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) && | |
105 | ! _retrieve_cache SYS_ALL_UNITS; | |
106 | then | |
6d314eca | 107 | _sys_all_units=( $(__systemctl list-units --all | { while read -r a b; do echo -E - " $a"; done; }) ) |
ff7a0685 WG |
108 | _store_cache SYS_ALL_UNITS _sys_all_units |
109 | fi | |
110 | } | |
111 | ||
112 | # Fills the unit list including all file units | |
113 | _systemctl_really_all_units() | |
114 | { | |
115 | local -a all_unit_files; | |
116 | local -a really_all_units; | |
117 | if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) && | |
118 | ! _retrieve_cache SYS_REALLY_ALL_UNITS; | |
119 | then | |
6d314eca | 120 | all_unit_files=( $(__systemctl list-unit-files | { while read -r a b; do echo -E - " $a"; done; }) ) |
ff7a0685 WG |
121 | _systemctl_all_units |
122 | really_all_units=($_sys_all_units $all_unit_files) | |
123 | _sys_really_all_units=(${(u)really_all_units}) | |
124 | _store_cache SYS_REALLY_ALL_UNITS _sys_really_all_units | |
125 | fi | |
126 | } | |
127 | ||
128 | _filter_units_by_property() { | |
129 | local property=$1 value=$2 ; shift ; shift | |
130 | local -a units ; units=($*) | |
131 | local prop unit | |
132 | for ((i=1; $i <= ${#units[*]}; i++)); do | |
133 | # FIXME: "Failed to issue method call: Unknown unit" errors are ignored for | |
134 | # now (related to DBUS_ERROR_UNKNOWN_OBJECT). in the future, we need to | |
135 | # revert to calling 'systemctl show' once for all units, which is way | |
136 | # faster | |
137 | unit=${units[i]} | |
138 | prop=${(f)"$(_call_program units "$service show --no-pager --property="$property" ${unit} 2>/dev/null")"} | |
139 | if [[ "${prop}" = "$property=$value" ]]; then | |
e37b56c7 | 140 | echo -E - " ${unit}" |
ff7a0685 WG |
141 | fi |
142 | done | |
143 | } | |
144 | ||
e9a19bd8 ZJS |
145 | _systemctl_get_template_names() { __systemctl list-unit-files | { while read -r a b; do [[ $a =~ @\. ]] && echo -E - " ${a%%@.*}@"; done; } } |
146 | ||
f29c77bc | 147 | |
6d314eca | 148 | _systemctl_active_units() {_sys_active_units=( $(__systemctl list-units | { while read -r a b; do echo -E - " $a"; done; }) )} |
81333ecf ZJS |
149 | |
150 | _systemctl_startable_units(){ | |
151 | _sys_startable_units=(_filter_units_by_property ActiveState inactive $( | |
152 | _filter_units_by_property CanStart yes $( | |
153 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ | |
154 | { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } | |
155 | __systemctl $mode list-units --state inactive,failed | \ | |
156 | { while read -r a b; do echo -E - " $a"; done; } ))) | |
157 | } | |
158 | ||
159 | _systemctl_restartable_units(){ | |
160 | _sys_restartable_units=(_filter_units_by_property CanStart yes $( | |
161 | __systemctl $mode list-unit-files --state enabled,disabled,static | \ | |
162 | { while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; } | |
163 | __systemctl $mode list-units | \ | |
164 | { while read -r a b; do echo -E - " $a"; done; } )) | |
165 | } | |
166 | ||
6d314eca EC |
167 | _systemctl_failed_units() {_sys_failed_units=( $(__systemctl list-units --failed | { while read -r a b; do echo -E - " $a"; done; }) )} |
168 | _systemctl_enabled_units() {_sys_enabled_units=( $(__systemctl list-unit-files | { while read -r a b; do [[ $b == "enabled" ]] && echo -E - " $a"; done; }) )} | |
169 | _systemctl_disabled_units(){_sys_disabled_units=($(__systemctl list-unit-files | { while read -r a b; do [[ $b == "disabled" ]] && echo -E - " $a"; done; }) )} | |
170 | _systemctl_masked_units() {_sys_masked_units=( $(__systemctl list-unit-files | { while read -r a b; do [[ $b == "masked" ]] && echo -E - " $a"; done; }) )} | |
ff7a0685 WG |
171 | |
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 | { | |
184 | _systemctl_enabled_units | |
185 | compadd "$@" -a - _sys_enabled_units | |
186 | } | |
187 | ||
188 | (( $+functions[_systemctl_reenable] )) || _systemctl_reenable() | |
189 | { | |
ff7a0685 WG |
190 | _systemctl_enabled_units |
191 | _systemctl_disabled_units | |
e9a19bd8 ZJS |
192 | compadd "$@" -a - _sys_enabled_units _sys_disabled_units $(_systemctl_get_template_names) |
193 | } | |
ff7a0685 WG |
194 | |
195 | # Completion functions for DISABLED_UNITS | |
196 | (( $+functions[_systemctl_enable] )) || _systemctl_enable() | |
197 | { | |
198 | _systemctl_disabled_units | |
e9a19bd8 | 199 | compadd "$@" -a - _sys_disabled_units $(_systemctl_get_template_names) |
ff7a0685 WG |
200 | } |
201 | ||
202 | # Completion functions for FAILED_UNITS | |
203 | (( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed() | |
204 | { | |
205 | _systemctl_failed_units | |
206 | compadd "$@" -a - _sys_failed_units || _message "no failed unit found" | |
207 | } | |
208 | ||
209 | # Completion functions for STARTABLE_UNITS | |
210 | (( $+functions[_systemctl_start] )) || _systemctl_start() | |
211 | { | |
f29c77bc | 212 | _systemctl_startable_units |
81333ecf | 213 | compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names) |
ff7a0685 WG |
214 | } |
215 | ||
216 | # Completion functions for STOPPABLE_UNITS | |
217 | for fun in stop kill try-restart condrestart ; do | |
218 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
219 | { | |
220 | _systemctl_active_units | |
221 | compadd "$@" - $( _filter_units_by_property CanStop yes \ | |
222 | ${_sys_active_units[*]} ) | |
223 | } | |
224 | done | |
225 | ||
226 | # Completion functions for ISOLATABLE_UNITS | |
227 | (( $+functions[_systemctl_isolate] )) || _systemctl_isolate() | |
228 | { | |
229 | _systemctl_all_units | |
230 | compadd "$@" - $( _filter_units_by_property AllowIsolate yes \ | |
231 | ${_sys_all_units[*]} ) | |
232 | } | |
233 | ||
234 | # Completion functions for RELOADABLE_UNITS | |
235 | for fun in reload reload-or-try-restart force-reload ; do | |
236 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
237 | { | |
238 | _systemctl_active_units | |
239 | compadd "$@" - $( _filter_units_by_property CanReload yes \ | |
240 | ${_sys_active_units[*]} ) | |
241 | } | |
242 | done | |
243 | ||
244 | # Completion functions for RESTARTABLE_UNITS | |
245 | for fun in restart reload-or-restart ; do | |
246 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
247 | { | |
f29c77bc | 248 | _systemctl_restartable_units |
81333ecf | 249 | compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names) |
ff7a0685 WG |
250 | } |
251 | done | |
252 | ||
253 | # Completion functions for MASKED_UNITS | |
254 | (( $+functions[_systemctl_unmask] )) || _systemctl_unmask() | |
255 | { | |
256 | _systemctl_masked_units | |
1cf3c30c | 257 | compadd "$@" -a - _sys_masked_units || _message "no masked units found" |
ff7a0685 WG |
258 | } |
259 | ||
260 | # Completion functions for JOBS | |
261 | (( $+functions[_systemctl_cancel] )) || _systemctl_cancel() | |
262 | { | |
263 | compadd "$@" - $(__systemctl list-jobs \ | |
1cf3c30c | 264 | | cut -d' ' -f1 2>/dev/null ) || _message "no jobs found" |
ff7a0685 WG |
265 | } |
266 | ||
267 | # Completion functions for SNAPSHOTS | |
268 | (( $+functions[_systemctl_delete] )) || _systemctl_delete() | |
269 | { | |
270 | compadd "$@" - $(__systemctl list-units --type snapshot --all \ | |
1cf3c30c ZJS |
271 | | cut -d' ' -f1 2>/dev/null ) || _message "no snapshots found" |
272 | } | |
273 | ||
274 | # Completion functions for TARGETS | |
275 | (( $+functions[_systemctl_set-default] )) || _systemctl_set-default() | |
276 | { | |
277 | compadd "$@" - $(__systemctl list-unit-files --type target --all \ | |
278 | | cut -d' ' -f1 2>/dev/null ) || _message "no targets found" | |
ff7a0685 WG |
279 | } |
280 | ||
281 | # Completion functions for ENVS | |
282 | for fun in set-environment unset-environment ; do | |
283 | (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() | |
284 | { | |
285 | local fun=$0 ; fun=${fun##_systemctl_} | |
286 | local suf | |
287 | if [[ "${fun}" = "set-environment" ]]; then | |
288 | suf='-S=' | |
289 | fi | |
290 | ||
291 | compadd "$@" ${suf} - $(systemctl show-environment \ | |
292 | | while read line; do echo " ${line%%\=}";done ) | |
293 | } | |
294 | done | |
295 | ||
2c12a402 ZJS |
296 | (( $+functions[_systemctl_link] )) || _systemctl_link() { |
297 | _sd_unit_files | |
298 | } | |
ff7a0685 WG |
299 | |
300 | # no systemctl completion for: | |
299c397c | 301 | # [STANDALONE]='daemon-reexec daemon-reload default |
ff7a0685 WG |
302 | # emergency exit halt kexec list-jobs list-units |
303 | # list-unit-files poweroff reboot rescue show-environment' | |
f89a4474 | 304 | # [NAME]='snapshot' |
ff7a0685 WG |
305 | |
306 | _systemctl_caching_policy() | |
307 | { | |
308 | local _sysunits | |
309 | local -a oldcache | |
310 | ||
311 | # rebuild if cache is more than a day old | |
312 | oldcache=( "$1"(mh+1) ) | |
313 | (( $#oldcache )) && return 0 | |
314 | ||
315 | _sysunits=($(__systemctl --all | cut -d' ' -f1)) | |
316 | ||
317 | if (( $#_sysunits )); then | |
318 | for unit in $_sysunits; do | |
319 | [[ "$unit" -nt "$1" ]] && return 0 | |
320 | done | |
321 | fi | |
322 | ||
323 | return 1 | |
324 | } | |
325 | ||
298b9e23 WG |
326 | _unit_states() { |
327 | local -a _states | |
328 | _states=(loaded failed active inactive not-found listening running waiting plugged mounted exited dead masked) | |
329 | _values -s , "${_states[@]}" | |
330 | } | |
331 | ||
332 | _unit_types() { | |
333 | local -a _types | |
2c12a402 | 334 | _types=(automount busname device mount path service snapshot socket swap target timer) |
298b9e23 WG |
335 | _values -s , "${_types[@]}" |
336 | } | |
337 | ||
c0a67aef ZJS |
338 | _unit_properties() { |
339 | if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) && | |
340 | ! _retrieve_cache SYS_ALL_PROPERTIES; | |
341 | then | |
342 | _sys_all_properties=( $( {__systemctl show --all; | |
343 | @rootlibexecdir@/systemd --dump-configuration-items; } | { | |
344 | while IFS='=' read -r a b; do [ -n "$b" ] && echo "$a"; done | |
345 | }) ) | |
346 | _store_cache SYS_ALL_PROPRTIES _sys_all_properties | |
347 | fi | |
348 | _values -s , "${_sys_all_properties[@]}" | |
349 | } | |
350 | ||
ff7a0685 WG |
351 | _arguments -s \ |
352 | {-h,--help}'[Show help]' \ | |
353 | '--version[Show package version]' \ | |
862f4963 | 354 | {-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \ |
298b9e23 | 355 | '--state=[Display units in the specifyied state]:unit state:_unit_states' \ |
c0a67aef | 356 | {-p+,--property=}'[Show only properties by specific name]:unit property:_unit_properties' \ |
ff7a0685 WG |
357 | {-a,--all}'[Show all units/properties, including dead/empty ones]' \ |
358 | '--reverse[Show reverse dependencies]' \ | |
359 | '--after[Show units ordered after]' \ | |
360 | '--before[Show units ordered before]' \ | |
361 | '--failed[Show only failed units]' \ | |
362 | {-l,--full}"[Don't ellipsize unit names on output]" \ | |
363 | '--fail[When queueing a new job, fail if conflicting jobs are pending]' \ | |
364 | '--show-types[When showing sockets, show socket type]' \ | |
365 | '--irreversible[Mark transactions as irreversible]' \ | |
366 | '--ignore-dependencies[When queueing a new job, ignore all its dependencies]' \ | |
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' |