1#compdef systemctl 2# SPDX-License-Identifier: LGPL-2.1-or-later 3 4(( $+functions[_systemctl_commands] )) || _systemctl_commands() 5{ 6 local expl i 7 8 local -a unit_commands=( 9 # Unit Commands 10 "list-sockets:List sockets" 11 "list-timers:List timers" 12 "list-units:List units" 13 "start:Start (activate) one or more units" 14 "stop:Stop (deactivate) one or more units" 15 "reload:Reload one or more units" 16 "restart:Start or restart one or more units" 17 "condrestart:Restart one or more units if active" 18 "try-restart:Restart one or more units if active" 19 "reload-or-restart:Reload one or more units if possible, otherwise start or restart" 20 "force-reload:Reload one or more units if possible, otherwise restart if active" 21 "try-reload-or-restart:Reload one or more units if possible, otherwise restart if active" 22 "isolate:Start one unit and stop all others" 23 "kill:Send signal to processes of a unit" 24 "is-active:Check whether units are active" 25 "is-failed:Check whether units are failed" 26 "status:Show runtime status of one or more units" 27 "show:Show properties of one or more units/jobs or the manager" 28 "cat:Show the source unit files and drop-ins" 29 "set-property:Sets one or more properties of a unit" 30 "help:Show documentation for specified units" 31 "reset-failed:Reset failed state for all, one, or more units" 32 "list-dependencies:Show unit dependency tree" 33 "clean:Remove configuration, state, cache, logs or runtime data of units" 34 "bind:Bind mount a path from the host into a unit's namespace" 35 "mount-image:Mount an image from the host into a unit's namespace" 36 ) 37 38 local -a machine_commands=( 39 # Machine Commands 40 "list-machines:List the host and all running local containers" 41 ) 42 43 local -a unit_file_commands=( 44 # Unit File Commands 45 "list-unit-files:List installed unit files" 46 "enable:Enable one or more unit files" 47 "disable:Disable one or more unit files" 48 "reenable:Reenable one or more unit files" 49 "preset:Enable/disable one or more unit files based on preset configuration" 50 "preset-all:Enable/disable all unit files based on preset configuration" 51 "is-enabled:Check whether unit files are enabled" 52 "mask:Mask one or more units" 53 "unmask:Unmask one or more units" 54 "link:Link one or more units files into the search path" 55 "revert:Revert unit files to their vendor versions" 56 "add-wants:Add Wants= dependencies to a unit" 57 "add-requires:Add Requires= dependencies to a unit" 58 "set-default:Set the default target" 59 "get-default:Query the default target" 60 "edit:Edit one or more unit files" 61 ) 62 63 local -a job_commands=( 64 # Job Commands 65 "list-jobs:List jobs" 66 "cancel:Cancel all, one, or more jobs" 67 ) 68 69 local -a environment_commands=( 70 # Environment Commands 71 "show-environment:Dump environment" 72 "set-environment:Set one or more environment variables" 73 "unset-environment:Unset one or more environment variables" 74 "import-environment:Import environment variables set on the client" 75 ) 76 77 local -a manager_state_commands=( 78 # Manager State Commands 79 "daemon-reload:Reload systemd manager configuration" 80 "daemon-reexec:Reexecute systemd manager" 81 "log-level:Get or set the log level" 82 "log-target:Get or set the log target" 83 "service-watchdogs:Get or set the state of software watchdogs" 84 ) 85 86 local -a system_commands=( 87 # System Commands 88 "is-system-running:Query overall status of the system" 89 "default:Enter system default mode" 90 "rescue:Enter system rescue mode" 91 "emergency:Enter system emergency mode" 92 "halt:Shut down and halt the system" 93 "suspend:Suspend the system" 94 "poweroff:Shut down and power-off the system" 95 "reboot:Shut down and reboot the system" 96 "kexec:Shut down and reboot the system with kexec" 97 "exit:Ask for user instance termination" 98 "switch-root:Change root directory" 99 "hibernate:Hibernate the system" 100 "hybrid-sleep:Hibernate and suspend the system" 101 "suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it" 102 ) 103 104 local -a groups=( unit machine unit_file job environment manager_state system ) 105 local -a _systemctl_cmds 106 for i in $groups; do 107 _systemctl_cmds+=( "${(@P)${:-"${i}_commands"}}" ) 108 done 109 110 if (( CURRENT == 1 )); then 111 _tags ${^groups//_/-}-commands 112 while _tags; do 113 for i in $groups; do 114 if _requested ${i//_/-}-commands; then 115 _describe -t ${i//_/-}-commands "${i//_/ } command" ${i}_commands \ 116 && ret=0 117 fi 118 done 119 done 120 else 121 local curcontext="$curcontext" 122 123 cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}" 124 # Deal with any aliases 125 case $cmd in 126 condrestart) cmd="try-restart";; 127 force-reload) cmd="try-reload-or-restart";; 128 esac 129 130 if (( $#cmd )); then 131 curcontext="${curcontext%:*:*}:systemctl-${cmd}:" 132 133 local update_policy 134 zstyle -s ":completion:${curcontext}:" cache-policy update_policy 135 if [[ -z "$update_policy" ]]; then 136 zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy 137 fi 138 139 _call_function ret _systemctl_$cmd || _message 'no more arguments' 140 else 141 _message "unknown systemctl command: $words[1]" 142 fi 143 return ret 144 fi 145} 146 147# @todo _systemd-run has a helper with the same name, so we must redefine 148__systemctl() 149{ 150 systemctl $_sys_service_mgr --full --legend=no --no-pager --plain "$@" 2>/dev/null 151} 152 153 154# Fills the unit list 155(( $+functions[_systemctl_all_units] )) || 156 _systemctl_all_units() 157{ 158 if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) || 159 ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr; 160 then 161 _sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} ) 162 _store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units 163 fi 164} 165 166# Fills the unit list including all file units 167(( $+functions[_systemctl_really_all_units] )) || 168 _systemctl_really_all_units() 169{ 170 local -a all_unit_files; 171 local -a really_all_units; 172 if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) || 173 ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr; 174 then 175 all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} ) 176 _systemctl_all_units 177 really_all_units=($_sys_all_units $all_unit_files) 178 _sys_really_all_units=(${(u)really_all_units}) 179 _store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units 180 fi 181} 182 183(( $+functions[_filter_units_by_property] )) || 184 _filter_units_by_property() { 185 local property=$1 value=$2; shift 2 186 local -a units; units=("${(q-)@}") 187 local -A props 188 props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"}) 189 echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}" 190 } 191 192(( $+functions[_systemctl_get_non_template_names] )) || 193 _systemctl_get_non_template_names() { echo -E - ${^${(R)${(f)"$( 194 __systemctl $mode list-unit-files "$PREFIX*" 195 __systemctl $mode list-units --all "$PREFIX*" 196 )"}:#*@.*}%%[[:space:]]*} } 197 198(( $+functions[_systemctl_get_template_names] )) || 199 _systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ } 200 201(( $+functions[_systemctl_active_units] )) || 202 _systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )} 203 204(( $+functions[_systemctl_startable_units] )) || 205 _systemctl_startable_units(){ 206 _sys_startable_units=( $( _filter_units_by_property ActiveState inactive $( 207 _filter_units_by_property CanStart yes ${${${(f)"$( 208 __systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*" 209 __systemctl $mode list-units --state inactive,failed "$PREFIX*" 210 )"}:#*@.*}%%[[:space:]]*} 211 )) ) 212 } 213 214(( $+functions[_systemctl_restartable_units] )) || 215 _systemctl_restartable_units(){ 216 _sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$( 217 __systemctl $mode list-unit-files --state enabled,disabled,static "$PREFIX*" 218 __systemctl $mode list-units "$PREFIX*" 219 )"}:#*@.*}%%[[:space:]]*} ) ) 220 } 221 222(( $+functions[_systemctl_failed_units] )) || 223 _systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) } 224 225(( $+functions[_systemctl_unit_state] )) || 226 _systemctl_unit_state() { 227 setopt localoptions extendedglob 228 typeset -gA _sys_unit_state 229 _sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%%[[:space:]]#}% *} ) 230 } 231 232local fun 233# Completion functions for ALL_UNITS 234for fun in cat mask ; do 235 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 236 { 237 _systemctl_really_all_units 238 _wanted systemd-units expl unit \ 239 compadd "$@" -a - _sys_really_all_units 240 } 241done 242 243# Completion functions for NONTEMPLATE_UNITS 244for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires set-property; do 245 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 246 { 247 _wanted systemd-units expl unit \ 248 compadd "$@" - $(_systemctl_get_non_template_names) 249 } 250done 251 252# Completion functions for ENABLED_UNITS 253(( $+functions[_systemctl_disable] )) || _systemctl_disable() 254{ 255 local _sys_unit_state; _systemctl_unit_state 256 _wanted systemd-units expl 'enabled unit' \ 257 compadd "$@" - ${(k)_sys_unit_state[(R)enabled]} 258} 259 260(( $+functions[_systemctl_reenable] )) || _systemctl_reenable() 261{ 262 local _sys_unit_state; _systemctl_unit_state 263 _wanted systemd-units expl 'enabled/disabled unit' \ 264 compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names) 265} 266 267# Completion functions for DISABLED_UNITS 268(( $+functions[_systemctl_enable] )) || _systemctl_enable() 269{ 270 local _sys_unit_state; _systemctl_unit_state 271 _wanted systemd-units expl 'disabled unit' \ 272 compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names) 273} 274 275# Completion functions for FAILED_UNITS 276(( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed() 277{ 278 local _sys_failed_units; _systemctl_failed_units 279 _wanted systemd-units expl 'failed unit' \ 280 compadd "$@" -a - _sys_failed_units || _message "no failed unit found" 281} 282 283# Completion functions for STARTABLE_UNITS 284(( $+functions[_systemctl_start] )) || _systemctl_start() 285{ 286 local _sys_startable_units; _systemctl_startable_units 287 _wanted systemd-units expl 'startable unit' \ 288 compadd "$@" - ${_sys_startable_units[*]} 289} 290 291# Completion functions for STOPPABLE_UNITS 292for fun in stop kill try-restart condrestart ; do 293 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 294 { 295 local _sys_active_units; _systemctl_active_units 296 _wanted systemd-units expl 'stoppable unit' \ 297 compadd "$@" - $( _filter_units_by_property CanStop yes \ 298 ${_sys_active_units[*]} ) 299 } 300done 301 302# Completion functions for ISOLATABLE_UNITS 303(( $+functions[_systemctl_isolate] )) || _systemctl_isolate() 304{ 305 _systemctl_all_units 306 _wanted systemd-units expl 'isolatable unit' \ 307 compadd "$@" - $( _filter_units_by_property AllowIsolate yes \ 308 ${_sys_all_units[*]} ) 309} 310 311# Completion functions for RELOADABLE_UNITS 312for fun in reload try-reload-or-restart force-reload ; do 313 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 314 { 315 local _sys_active_units; _systemctl_active_units 316 _wanted systemd-units expl 'reloadable unit' \ 317 compadd "$@" - $( _filter_units_by_property CanReload yes \ 318 ${_sys_active_units[*]} ) 319 } 320done 321 322# Completion functions for RESTARTABLE_UNITS 323for fun in restart reload-or-restart ; do 324 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 325 { 326 local _sys_restartable_units; _systemctl_restartable_units 327 _wanted systemd-units expl 'restartable unit' \ 328 compadd "$@" - ${_sys_restartable_units[*]} 329 } 330done 331 332# Completion functions for MASKED_UNITS 333(( $+functions[_systemctl_unmask] )) || _systemctl_unmask() 334{ 335 local _sys_unit_state; _systemctl_unit_state 336 _wanted systemd-units expl 'masked unit' \ 337 compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found" 338} 339 340# Completion functions for JOBS 341(( $+functions[_systemctl_cancel] )) || _systemctl_cancel() 342{ 343 _wanted systemd-jobs expl job \ 344 compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} || 345 _message "no jobs found" 346} 347 348# Completion functions for TARGETS 349(( $+functions[_systemctl_set-default] )) || _systemctl_set-default() 350{ 351 _wanted systemd-targets expl target \ 352 compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} || 353 _message "no targets found" 354} 355 356# Completion functions for ENVS 357for fun in set-environment unset-environment ; do 358 (( $+functions[_systemctl_$fun] )) || _systemctl_$fun() 359 { 360 local fun=$0 ; fun=${fun##_systemctl_} 361 local suf 362 if [[ "${fun}" = "set-environment" ]]; then 363 suf='-S=' 364 fi 365 _wanted systemd-environment expl 'environment variable' \ 366 compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*} 367 } 368done 369 370(( $+functions[_systemctl_import-environment] )) || _systemctl_import-environment() 371{ 372 _parameters 373} 374 375(( $+functions[_systemctl_link] )) || _systemctl_link() { 376 _sd_unit_files 377 } 378 379(( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() { 380 _files 381 } 382 383(( $+functions[_systemctl_bind] )) || _systemctl_bind() { 384 _files 385 } 386 387(( $+functions[_systemctl_mount-image] )) || _systemctl_mount-image() { 388 _files 389 } 390 391# no systemctl completion for: 392# [STANDALONE]='daemon-reexec daemon-reload default 393# emergency exit halt kexec list-jobs list-units 394# list-unit-files poweroff reboot rescue show-environment' 395 396(( $+functions[_systemctl_caching_policy] )) || 397 _systemctl_caching_policy() 398{ 399 local _sysunits 400 local -a oldcache 401 402 # rebuild if cache is more than a day old 403 oldcache=( "$1"(mh+1) ) 404 (( $#oldcache )) && return 0 405 406 _sysunits=(${${(f)"$(__systemctl --all)"}%% *}) 407 408 if (( $#_sysunits )); then 409 for unit in $_sysunits; do 410 [[ "$unit" -nt "$1" ]] && return 0 411 done 412 fi 413 414 return 1 415} 416 417(( $+functions[_systemctl_unit_states] )) || 418 _systemctl_unit_states() { 419 local -a _states 420 _states=("${(fo)$(__systemctl --state=help)}") 421 _values -s , "${_states[@]}" 422 } 423 424(( $+functions[_systemctl_unit_types] )) || 425 _systemctl_unit_types() { 426 local -a _types 427 _types=("${(fo)$(__systemctl -t help)}") 428 _values -s , "${_types[@]}" 429 } 430 431(( $+functions[_systemctl_unit_properties] )) || 432 _systemctl_unit_properties() { 433 if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) || 434 ! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr; 435 then 436 _sys_all_properties=( ${${(M)${(f)"$({{ROOTLIBEXECDIR}}/systemd --dump-bus-properties)"}}} ) 437 _store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties 438 fi 439 _values -s , "${_sys_all_properties[@]}" 440 } 441 442(( $+functions[_systemctl_job_modes] )) || 443 _systemctl_job_modes() { 444 local -a _modes 445 _modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush) 446 _values -s , "${_modes[@]}" 447 } 448 449(( $+functions[_systemctl_timestamp] )) || 450 _systemctl_timestamp() { 451 local -a _styles 452 _styles=(help pretty us µs utc us+utc µs+utc) 453 _values -s , "${_styles[@]}" 454 } 455 456(( $+functions[_systemctl_check_inhibitors] )) || 457 _systemctl_check_inhibitors() { 458 local -a _modes 459 _modes=(auto yes no) 460 _values -s , "${_modes[@]}" 461 } 462 463# Build arguments for "systemctl" to be used in completion. 464local -a _modes; _modes=("--user" "--system") 465# Use the last mode (they are exclusive and the last one is used). 466local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]} 467_arguments -s \ 468 {-h,--help}'[Show help]' \ 469 '--version[Show package version]' \ 470 {-t+,--type=}'[List only units of a particular type]:unit type:_systemctl_unit_types' \ 471 '--state=[Display units in the specified state]:unit state:_systemctl_unit_states' \ 472 '--job-mode=[Specify how to deal with other jobs]:mode:_systemctl_job_modes' \ 473 {-p+,--property=}'[Show only properties by specific name]:unit property:_systemctl_unit_properties' \ 474 {-a,--all}'[Show all units/properties, including dead/empty ones]' \ 475 '--reverse[Show reverse dependencies]' \ 476 '--after[Show units ordered after]' \ 477 '--before[Show units ordered before]' \ 478 {-l,--full}"[Don't ellipsize unit names on output]" \ 479 '--show-types[When showing sockets, show socket type]' \ 480 '--check-inhibitors[Specify if inhibitors should be checked]:mode:_systemctl_check_inhibitors' \ 481 {-q,--quiet}'[Suppress output]' \ 482 '--no-block[Do not wait until operation finished]' \ 483 '--legend=no[Do not print a legend, i.e. the column headers and the footer with hints]' \ 484 '--no-pager[Do not pipe output into a pager]' \ 485 '--system[Connect to system manager]' \ 486 '--user[Connect to user service manager]' \ 487 "--no-wall[Don't send wall message before halt/power-off/reboot]" \ 488 '--global[Enable/disable/mask default user unit files globally]' \ 489 "--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \ 490 '--no-ask-password[Do not ask for system passwords]' \ 491 '--kill-who=[Who to send signal to]:killwho:(main control all)' \ 492 {-s+,--signal=}'[Which signal to send]:signal:_signals' \ 493 {-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \ 494 '--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \ 495 '--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \ 496 {-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \ 497 {-P,--privileged}'[Acquire privileges before execution]' \ 498 {-n+,--lines=}'[Journal entries to show]:number of entries' \ 499 {-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \ 500 '--firmware-setup[Tell the firmware to show the setup menu on next boot]' \ 501 '--plain[When used with list-dependencies, print output as a list]' \ 502 '--failed[Show failed units]' \ 503 '--timestamp=[Change format of printed timestamps]:style:_systemctl_timestamp' \ 504 '*::systemctl command:_systemctl_commands' 505