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