2019-04-05 12:41:35 +03:00
#compdef systemctl -*- shell-script -*-
2017-11-18 20:21:19 +03:00
# SPDX-License-Identifier: LGPL-2.1+
2013-07-29 23:02:01 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_commands] )) || _systemctl_commands()
2013-07-29 23:02:01 +04:00
{
2019-04-05 12:39:14 +03:00
local -a _systemctl_cmds
_systemctl_cmds=(
"list-sockets:List sockets"
"list-timers:List timers"
"list-units:List units"
"start:Start (activate) one or more units"
"stop:Stop (deactivate) one or more units"
"reload:Reload one or more units"
"restart:Start or restart one or more units"
"condrestart:Restart one or more units if active"
"try-restart:Restart one or more units if active"
"reload-or-restart:Reload one or more units if possible, otherwise start or restart"
"force-reload:Reload one or more units if possible, otherwise restart if active"
"hibernate:Hibernate the system"
"hybrid-sleep:Hibernate and suspend the system"
"suspend-then-hibernate:Suspend the system for a period of time, and then hibernate it"
"try-reload-or-restart:Reload one or more units if possible, otherwise restart if active"
"isolate:Start one unit and stop all others"
"kill:Send signal to processes of a unit"
"is-active:Check whether units are active"
"is-failed:Check whether units are failed"
"status:Show runtime status of one or more units"
"show:Show properties of one or more units/jobs or the manager"
"cat:Show the source unit files and drop-ins"
"reset-failed:Reset failed state for all, one, or more units"
"list-unit-files:List installed unit files"
"enable:Enable one or more unit files"
"disable:Disable one or more unit files"
"add-wants:Add Wants= dependencies to a unit"
"add-requires:Add Requires= dependencies to a unit"
"reenable:Reenable one or more unit files"
"preset:Enable/disable one or more unit files based on preset configuration"
"set-default:Set the default target"
"get-default:Query the default target"
"edit:Edit one or more unit files"
"is-system-running:Query overall status of the system"
"help:Show documentation for specified units"
"list-dependencies:Show unit dependency tree"
"mask:Mask one or more units"
"unmask:Unmask one or more units"
"link:Link one or more units files into the search path"
"is-enabled:Check whether unit files are enabled"
"list-jobs:List jobs"
"cancel:Cancel all, one, or more jobs"
"show-environment:Dump environment"
"set-environment:Set one or more environment variables"
"unset-environment:Unset one or more environment variables"
"daemon-reload:Reload systemd manager configuration"
"daemon-reexec:Reexecute systemd manager"
"default:Enter system default mode"
"rescue:Enter system rescue mode"
"emergency:Enter system emergency mode"
"halt:Shut down and halt the system"
"suspend:Suspend the system"
"poweroff:Shut down and power-off the system"
"reboot:Shut down and reboot the system"
"kexec:Shut down and reboot the system with kexec"
"exit:Ask for user instance termination"
"switch-root:Change root directory"
"revert:Revert unit files to their vendor versions"
"set-property:Sets one or more properties of a unit"
)
if (( CURRENT == 1 )); then
_describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
2013-07-29 23:02:01 +04:00
else
2019-04-05 12:39:14 +03:00
local curcontext="$curcontext" expl
cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
# Deal with any aliases
case $cmd in
condrestart) cmd="try-restart";;
force-reload) cmd="try-reload-or-restart";;
esac
if (( $#cmd )); then
curcontext="${curcontext%:*:*}:systemctl-${cmd}:"
local update_policy
zstyle -s ":completion:${curcontext}:" cache-policy update_policy
if [[ -z "$update_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy _systemctl_caching_policy
fi
_call_function ret _systemctl_$cmd || _message 'no more arguments'
else
_message "unknown systemctl command: $words[1]"
fi
return ret
2013-07-29 23:02:01 +04:00
fi
}
2018-12-24 14:15:38 +03:00
# @todo _systemd-run has a helper with the same name, so we must redefine
2013-07-29 23:02:01 +04:00
__systemctl()
{
2019-04-05 12:39:14 +03:00
systemctl $_sys_service_mgr --full --no-legend --no-pager "$@" 2>/dev/null
2013-07-29 23:02:01 +04:00
}
# Fills the unit list
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_all_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_all_units()
2013-07-29 23:02:01 +04:00
{
2019-04-05 12:39:14 +03:00
if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS$_sys_service_mgr ) ||
! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
then
_sys_all_units=( ${${(f)"$(__systemctl list-units --all "$PREFIX*" )"}%% *} )
_store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
fi
2013-07-29 23:02:01 +04:00
}
# Fills the unit list including all file units
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_really_all_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_really_all_units()
2013-07-29 23:02:01 +04:00
{
2019-04-05 12:39:14 +03:00
local -a all_unit_files;
local -a really_all_units;
if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr ) ||
! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
then
all_unit_files=( ${${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}%% *} )
_systemctl_all_units
really_all_units=($_sys_all_units $all_unit_files)
_sys_really_all_units=(${(u)really_all_units})
_store_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr _sys_really_all_units
fi
2013-07-29 23:02:01 +04:00
}
2018-12-24 14:15:38 +03:00
(( $+functions[_filter_units_by_property] )) ||
2019-04-05 12:39:14 +03:00
_filter_units_by_property() {
local property=$1 value=$2; shift 2
local -a units; units=("${(q-)@}")
local -A props
props=(${(f)"$(_call_program units "$service $_sys_service_mgr show --no-pager --property=\"Id,$property\" -- ${units} 2>/dev/null")"})
echo -E - "${(@g:o:)${(k@)props[(Re)$property=$value]}#Id=}"
}
2013-07-29 23:02:01 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_get_non_template_names] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_get_non_template_names() { echo -E - ${^${(R)${(f)"$(
2018-06-11 13:53:08 +03:00
__systemctl $mode list-unit-files "$PREFIX*"
__systemctl $mode list-units --all "$PREFIX*"
)"}:#*@.*}%%[[:space:]]*} }
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_get_template_names] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files "$PREFIX*" )"}##*@.[^[:space:]]##}%%@.*}\@ }
2014-10-15 05:10:02 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_active_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units "$PREFIX*" )"}%% *} )}
2014-10-30 06:47:55 +03:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_startable_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_startable_units(){
_sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
_filter_units_by_property CanStart yes ${${${(f)"$(
2018-04-30 16:45:25 +03:00
__systemctl $mode list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$PREFIX*"
2018-04-30 15:37:51 +03:00
__systemctl $mode list-units --state inactive,failed "$PREFIX*"
2018-01-28 15:29:58 +03:00
)"}:#*@.*}%%[[:space:]]*}
2019-04-05 12:39:14 +03:00
)) )
}
2014-10-30 06:47:55 +03:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_restartable_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_restartable_units(){
_sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
2018-04-30 15:37:51 +03:00
__systemctl $mode list-unit-files --state enabled,disabled,static "$PREFIX*"
__systemctl $mode list-units "$PREFIX*"
2018-01-28 15:29:58 +03:00
)"}:#*@.*}%%[[:space:]]*} ) )
2019-04-05 12:39:14 +03:00
}
2014-10-30 06:47:55 +03:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_failed_units] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed "$PREFIX*" )"}%% *} ) }
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_unit_state] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files "$PREFIX*" ) ) }
2013-07-29 23:02:01 +04:00
2015-05-18 08:02:38 +03:00
local fun
2013-07-29 23:02:01 +04:00
# Completion functions for ALL_UNITS
2018-06-11 13:53:08 +03:00
for fun in cat mask ; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
_systemctl_really_all_units
_wanted systemd-units expl unit \
compadd "$@" -a - _sys_really_all_units
}
2013-07-29 23:02:01 +04:00
done
2018-06-11 13:53:08 +03:00
# Completion functions for NONTEMPLATE_UNITS
2018-11-20 07:17:11 +03:00
for fun in is-active is-failed is-enabled status show preset help list-dependencies edit revert add-wants add-requires set-property; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
_wanted systemd-units expl unit \
compadd "$@" - $(_systemctl_get_non_template_names)
}
2018-06-11 13:53:08 +03:00
done
2013-07-29 23:02:01 +04:00
# Completion functions for ENABLED_UNITS
2014-10-15 05:10:02 +04:00
(( $+functions[_systemctl_disable] )) || _systemctl_disable()
{
2015-05-18 08:02:39 +03:00
local _sys_unit_state; _systemctl_unit_state
2015-05-29 17:40:11 +03:00
_wanted systemd-units expl 'enabled unit' \
2019-04-05 12:39:14 +03:00
compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
2014-10-15 05:10:02 +04:00
}
(( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
{
2015-05-18 08:02:39 +03:00
local _sys_unit_state; _systemctl_unit_state
2015-05-29 17:40:11 +03:00
_wanted systemd-units expl 'enabled/disabled unit' \
2019-04-05 12:39:14 +03:00
compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
2014-10-15 05:10:02 +04:00
}
2013-07-29 23:02:01 +04:00
# Completion functions for DISABLED_UNITS
(( $+functions[_systemctl_enable] )) || _systemctl_enable()
{
2019-04-05 12:39:14 +03:00
local _sys_unit_state; _systemctl_unit_state
_wanted systemd-units expl 'disabled unit' \
compadd "$@" - ${(k)_sys_unit_state[(R)disabled]} $(_systemctl_get_template_names)
2013-07-29 23:02:01 +04:00
}
# Completion functions for FAILED_UNITS
(( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
{
2019-04-05 12:39:14 +03:00
local _sys_failed_units; _systemctl_failed_units
_wanted systemd-units expl 'failed unit' \
compadd "$@" -a - _sys_failed_units || _message "no failed unit found"
2013-07-29 23:02:01 +04:00
}
# Completion functions for STARTABLE_UNITS
(( $+functions[_systemctl_start] )) || _systemctl_start()
{
2019-04-05 12:39:14 +03:00
local _sys_startable_units; _systemctl_startable_units
_wanted systemd-units expl 'startable unit' \
compadd "$@" - ${_sys_startable_units[*]}
2013-07-29 23:02:01 +04:00
}
# Completion functions for STOPPABLE_UNITS
for fun in stop kill try-restart condrestart ; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
local _sys_active_units; _systemctl_active_units
_wanted systemd-units expl 'stoppable unit' \
compadd "$@" - $( _filter_units_by_property CanStop yes \
${_sys_active_units[*]} )
}
2013-07-29 23:02:01 +04:00
done
# Completion functions for ISOLATABLE_UNITS
(( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
{
2019-04-05 12:39:14 +03:00
_systemctl_all_units
_wanted systemd-units expl 'isolatable unit' \
compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
${_sys_all_units[*]} )
2013-07-29 23:02:01 +04:00
}
# Completion functions for RELOADABLE_UNITS
2016-01-28 20:57:04 +03:00
for fun in reload try-reload-or-restart force-reload ; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
local _sys_active_units; _systemctl_active_units
_wanted systemd-units expl 'reloadable unit' \
compadd "$@" - $( _filter_units_by_property CanReload yes \
${_sys_active_units[*]} )
}
2013-07-29 23:02:01 +04:00
done
# Completion functions for RESTARTABLE_UNITS
for fun in restart reload-or-restart ; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
local _sys_restartable_units; _systemctl_restartable_units
_wanted systemd-units expl 'restartable unit' \
compadd "$@" - ${_sys_restartable_units[*]}
}
2013-07-29 23:02:01 +04:00
done
# Completion functions for MASKED_UNITS
(( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
{
2019-04-05 12:39:14 +03:00
local _sys_unit_state; _systemctl_unit_state
_wanted systemd-units expl 'masked unit' \
compadd "$@" - ${(k)_sys_unit_state[(R)masked]} || _message "no masked units found"
2013-07-29 23:02:01 +04:00
}
# Completion functions for JOBS
(( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
{
2019-04-05 12:39:14 +03:00
_wanted systemd-jobs expl job \
compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
_message "no jobs found"
2013-07-29 23:02:01 +04:00
}
2014-10-28 05:34:28 +03:00
# Completion functions for TARGETS
(( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
{
2019-04-05 12:39:14 +03:00
_wanted systemd-targets expl target \
compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all "$PREFIX*" )"}%% *} ||
_message "no targets found"
2013-07-29 23:02:01 +04:00
}
# Completion functions for ENVS
for fun in set-environment unset-environment ; do
2019-04-05 12:39:14 +03:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
local fun=$0 ; fun=${fun##_systemctl_}
local suf
if [[ "${fun}" = "set-environment" ]]; then
suf='-S='
fi
_wanted systemd-environment expl 'environment variable' \
compadd "$@" ${suf} - ${${(f)"$(systemctl show-environment)"}%%=*}
}
2013-07-29 23:02:01 +04:00
done
2014-07-22 05:11:56 +04:00
(( $+functions[_systemctl_link] )) || _systemctl_link() {
2019-04-05 12:39:14 +03:00
_sd_unit_files
}
2013-07-29 23:02:01 +04:00
2015-04-03 05:54:35 +03:00
(( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
2019-04-05 12:39:14 +03:00
_files
}
2015-04-03 05:54:35 +03:00
2013-07-29 23:02:01 +04:00
# no systemctl completion for:
2013-10-22 02:53:57 +04:00
# [STANDALONE]='daemon-reexec daemon-reload default
2013-07-29 23:02:01 +04:00
# emergency exit halt kexec list-jobs list-units
# list-unit-files poweroff reboot rescue show-environment'
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_caching_policy] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_caching_policy()
2013-07-29 23:02:01 +04:00
{
2019-04-05 12:39:14 +03:00
local _sysunits
local -a oldcache
2013-07-29 23:02:01 +04:00
2019-04-05 12:39:14 +03:00
# rebuild if cache is more than a day old
oldcache=( "$1"(mh+1) )
(( $#oldcache )) && return 0
2013-07-29 23:02:01 +04:00
2019-04-05 12:39:14 +03:00
_sysunits=(${${(f)"$(__systemctl --all)"}%% *})
2013-07-29 23:02:01 +04:00
2019-04-05 12:39:14 +03:00
if (( $#_sysunits )); then
for unit in $_sysunits; do
[[ "$unit" -nt "$1" ]] && return 0
done
fi
2013-07-29 23:02:01 +04:00
2019-04-05 12:39:14 +03:00
return 1
2013-07-29 23:02:01 +04:00
}
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_unit_states] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_unit_states() {
local -a _states
_states=("${(fo)$(__systemctl --state=help)}")
_values -s , "${_states[@]}"
}
2013-08-15 07:09:14 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_unit_types] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_unit_types() {
local -a _types
_types=("${(fo)$(__systemctl -t help)}")
_values -s , "${_types[@]}"
}
2013-08-15 07:09:14 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_unit_properties] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_unit_properties() {
if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES$_sys_service_mgr ) ||
! _retrieve_cache SYS_ALL_PROPERTIES$_sys_service_mgr;
then
_sys_all_properties=( ${${(M)${(f)"$(@rootlibexecdir@/systemd --dump-bus-properties)"}}} )
_store_cache SYS_ALL_PROPERTIES$_sys_service_mgr _sys_all_properties
fi
_values -s , "${_sys_all_properties[@]}"
}
2014-07-15 03:24:46 +04:00
2018-12-24 14:15:38 +03:00
(( $+functions[_systemctl_job_modes] )) ||
2019-04-05 12:39:14 +03:00
_systemctl_job_modes() {
local -a _modes
_modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
_values -s , "${_modes[@]}"
}
2015-05-18 07:58:24 +03:00
2016-08-13 17:42:55 +03:00
# Build arguments for "systemctl" to be used in completion.
2015-05-19 01:45:31 +03:00
local -a _modes; _modes=("--user" "--system")
2016-08-13 17:42:55 +03:00
# Use the last mode (they are exclusive and the last one is used).
local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]}
2013-07-29 23:02:01 +04:00
_arguments -s \
{-h,--help}'[Show help]' \
'--version[Show package version]' \
2018-12-24 14:15:38 +03:00
{-t+,--type=}'[List only units of a particular type]:unit type:_systemctl_unit_types' \
'--state=[Display units in the specified state]:unit state:_systemctl_unit_states' \
'--job-mode=[Specify how to deal with other jobs]:mode:_systemctl_job_modes' \
{-p+,--property=}'[Show only properties by specific name]:unit property:_systemctl_unit_properties' \
2013-07-29 23:02:01 +04:00
{-a,--all}'[Show all units/properties, including dead/empty ones]' \
'--reverse[Show reverse dependencies]' \
'--after[Show units ordered after]' \
'--before[Show units ordered before]' \
{-l,--full}"[Don't ellipsize unit names on output]" \
'--show-types[When showing sockets, show socket type]' \
{-i,--ignore-inhibitors}'[When executing a job, ignore jobs dependencies]' \
{-q,--quiet}'[Suppress output]' \
'--no-block[Do not wait until operation finished]' \
'--no-legend[Do not print a legend, i.e. the column headers and the footer with hints]' \
'--no-pager[Do not pipe output into a pager]' \
'--system[Connect to system manager]' \
'--user[Connect to user service manager]' \
"--no-wall[Don't send wall message before halt/power-off/reboot]" \
2017-10-12 09:14:25 +03:00
'--global[Enable/disable/mask unit files globally]' \
2013-07-29 23:02:01 +04:00
"--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
'--no-ask-password[Do not ask for system passwords]' \
'--kill-who=[Who to send signal to]:killwho:(main control all)' \
2013-08-21 07:06:54 +04:00
{-s+,--signal=}'[Which signal to send]:signal:_signals' \
2013-07-29 23:02:01 +04:00
{-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
2017-10-12 09:14:25 +03:00
'--root=[Enable/disable/mask unit files in the specified root directory]:directory:_directories' \
'--runtime[Enable/disable/mask unit files only temporarily until next reboot]' \
2013-12-01 07:31:31 +04:00
{-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
2013-07-29 23:02:01 +04:00
{-P,--privileged}'[Acquire privileges before execution]' \
2013-08-21 07:06:54 +04:00
{-n+,--lines=}'[Journal entries to show]:number of entries' \
2013-11-08 02:00:52 +04:00
{-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
2015-04-03 19:03:06 +03:00
'--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
2013-07-29 23:02:01 +04:00
'--plain[When used with list-dependencies, print output as a list]' \
2017-02-02 02:23:46 +03:00
'--failed[Show failed units]' \
2018-12-24 14:15:38 +03:00
'*::systemctl command:_systemctl_commands'