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