2013-07-29 23:02:01 +04:00
#compdef systemctl
(( $+functions[_systemctl_command] )) || _systemctl_command()
{
local -a _systemctl_cmds
_systemctl_cmds=(
2013-09-20 02:00:36 +04:00
"list-sockets:List sockets"
2013-11-11 19:13:36 +04:00
"list-timers:List timers"
2013-07-29 23:02:01 +04:00
"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"
"reload-or-try-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"
2013-12-01 07:31:31 +04:00
"cat:Show the source unit files and drop-ins"
2013-07-29 23:02:01 +04:00
"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"
"reenable:Reenable one or more unit files"
"preset:Enable/disable one or more unit files based on preset configuration"
2014-10-28 05:34:28 +03:00
"set-default:Set the default target"
"get-default:Query the default target"
2014-12-10 03:10:01 +03:00
"edit:Edit one or more unit files"
2014-10-28 05:34:28 +03:00
"is-system-running:Query overall status of the system"
2013-07-29 23:02:01 +04:00
"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"
2015-04-03 05:54:35 +03:00
"switch-root:Change root directory"
2013-07-29 23:02:01 +04:00
)
if (( CURRENT == 1 )); then
_describe -t commands 'systemctl command' _systemctl_cmds || compadd "$@"
else
2015-05-29 17:40:11 +03:00
local curcontext="$curcontext" expl
2013-07-29 23:02:01 +04:00
cmd="${${_systemctl_cmds[(r)$words[1]:*]%%:*}}"
# Deal with any aliases
case $cmd in
condrestart) cmd="try-restart";;
force-reload) cmd="reload-or-try-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
fi
}
__systemctl()
{
2015-05-19 01:45:31 +03:00
systemctl $_sys_service_mgr --full --no-legend --no-pager "$@"
2013-07-29 23:02:01 +04:00
}
# Fills the unit list
_systemctl_all_units()
{
if ( [[ ${+_sys_all_units} -eq 0 ]] || _cache_invalid SYS_ALL_UNITS ) &&
! _retrieve_cache SYS_ALL_UNITS;
then
2015-05-18 10:19:59 +03:00
_sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
2013-07-29 23:02:01 +04:00
_store_cache SYS_ALL_UNITS _sys_all_units
fi
}
# Fills the unit list including all file units
_systemctl_really_all_units()
{
local -a all_unit_files;
local -a really_all_units;
if ( [[ ${+_sys_really_all_units} -eq 0 ]] || _cache_invalid SYS_REALLY_ALL_UNITS ) &&
! _retrieve_cache SYS_REALLY_ALL_UNITS;
then
2015-05-18 10:19:59 +03:00
all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
2013-07-29 23:02:01 +04:00
_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_really_all_units
fi
}
_filter_units_by_property() {
local property=$1 value=$2 ; shift ; shift
local -a units ; units=($*)
2015-05-27 22:26:26 +03:00
local props
for props in ${(ps:\n\n:)"$(_call_program units "$service show --no-pager --property="Id,$property" -- ${units} 2>/dev/null")"}; do
props=(${(f)props})
if [[ "${props[2]}" = "$property=$value" ]]; then
echo -E - " ${props[1]#Id=}"
2013-07-29 23:02:01 +04:00
fi
done
}
2015-05-18 08:02:40 +03:00
_systemctl_get_template_names() { echo -E - ${^${(M)${(f)"$(__systemctl list-unit-files)"}##*@.[^[:space:]]##}%%@.*}\@ }
2014-10-15 05:10:02 +04:00
2014-10-15 04:20:07 +04:00
2015-05-18 10:19:59 +03:00
_systemctl_active_units() {_sys_active_units=( ${${(f)"$(__systemctl list-units)"}%% *} )}
2014-10-30 06:47:55 +03:00
_systemctl_startable_units(){
2015-05-18 08:02:38 +03:00
_sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
2014-10-30 06:47:55 +03:00
_filter_units_by_property CanStart yes $(
__systemctl $mode list-unit-files --state enabled,disabled,static | \
{ while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
__systemctl $mode list-units --state inactive,failed | \
2015-05-18 08:02:38 +03:00
{ while read -r a b; do echo -E - " $a"; done; } )) ) )
2014-10-30 06:47:55 +03:00
}
_systemctl_restartable_units(){
2015-05-18 08:02:38 +03:00
_sys_restartable_units=( $(_filter_units_by_property CanStart yes $(
2014-10-30 06:47:55 +03:00
__systemctl $mode list-unit-files --state enabled,disabled,static | \
{ while read -r a b; do [[ $a =~ @\. ]] || echo -E - " $a"; done; }
__systemctl $mode list-units | \
2015-05-18 08:02:38 +03:00
{ while read -r a b; do echo -E - " $a"; done; } )) )
2014-10-30 06:47:55 +03:00
}
2015-05-18 08:02:41 +03:00
_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --failed)"}%% *} ) }
2015-05-18 08:02:39 +03:00
_systemctl_unit_state() { typeset -gA _sys_unit_state; _sys_unit_state=( $(__systemctl list-unit-files) ) }
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
2014-12-10 03:10:01 +03:00
for fun in is-active is-failed is-enabled status show cat mask preset help list-dependencies edit ; do
2013-07-29 23:02:01 +04:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
_systemctl_really_all_units
2015-05-29 17:40:11 +03:00
_wanted systemd-units expl unit \
compadd "$@" -a - _sys_really_all_units
2013-07-29 23:02:01 +04:00
}
done
# 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' \
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' \
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()
{
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 '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()
{
2015-05-18 08:02:41 +03:00
local _sys_failed_units; _systemctl_failed_units
2015-05-29 17:40:11 +03:00
_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()
{
2015-05-18 08:02:43 +03:00
local _sys_startable_units; _systemctl_startable_units
2015-05-29 17:40:11 +03:00
_wanted systemd-units expl 'startable unit' \
compadd "$@" - ${_sys_startable_units[*]} $(_systemctl_get_template_names)
2013-07-29 23:02:01 +04:00
}
# Completion functions for STOPPABLE_UNITS
for fun in stop kill try-restart condrestart ; do
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
2015-05-18 08:02:43 +03:00
local _sys_active_units; _systemctl_active_units
2015-05-29 17:40:11 +03:00
_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()
{
_systemctl_all_units
2015-05-29 17:40:11 +03:00
_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
for fun in reload reload-or-try-restart force-reload ; do
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
2015-05-18 08:02:43 +03:00
local _sys_active_units; _systemctl_active_units
2015-05-29 17:40:11 +03:00
_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
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
2015-05-18 08:02:43 +03:00
local _sys_restartable_units; _systemctl_restartable_units
2015-05-29 17:40:11 +03:00
_wanted systemd-units expl 'restartable unit' \
compadd "$@" - ${_sys_restartable_units[*]} $(_systemctl_get_template_names)
2013-07-29 23:02:01 +04:00
}
done
# Completion functions for MASKED_UNITS
(( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
{
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 '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()
{
2015-05-29 17:40:11 +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()
{
2015-05-29 17:40:11 +03:00
_wanted systemd-targets expl target \
compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
_message "no targets found"
2013-07-29 23:02:01 +04:00
}
# Completion functions for ENVS
for fun in set-environment unset-environment ; do
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
local fun=$0 ; fun=${fun##_systemctl_}
local suf
if [[ "${fun}" = "set-environment" ]]; then
suf='-S='
fi
2015-05-29 17:40:11 +03:00
_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() {
_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() {
_files
}
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'
_systemctl_caching_policy()
{
local _sysunits
local -a oldcache
# rebuild if cache is more than a day old
oldcache=( "$1"(mh+1) )
(( $#oldcache )) && return 0
2015-05-18 10:19:59 +03:00
_sysunits=(${${(f)"$(__systemctl --all)"}%% *})
2013-07-29 23:02:01 +04:00
if (( $#_sysunits )); then
for unit in $_sysunits; do
[[ "$unit" -nt "$1" ]] && return 0
done
fi
return 1
}
2013-08-15 07:09:14 +04:00
_unit_states() {
local -a _states
2015-09-28 21:43:26 +03:00
_states=("${(fo)$(__systemctl --state=help)}")
2013-08-15 07:09:14 +04:00
_values -s , "${_states[@]}"
}
_unit_types() {
local -a _types
2015-09-28 21:43:26 +03:00
_types=("${(fo)$(__systemctl -t help)}")
2013-08-15 07:09:14 +04:00
_values -s , "${_types[@]}"
}
2014-07-15 03:24:46 +04:00
_unit_properties() {
if ( [[ ${+_sys_all_properties} -eq 0 ]] || _cache_invalid SYS_ALL_PROPERTIES ) &&
! _retrieve_cache SYS_ALL_PROPERTIES;
then
2015-05-18 10:19:59 +03:00
_sys_all_properties=( ${${(M)${(f)"$(__systemctl show --all;
@rootlibexecdir@/systemd --dump-configuration-items)"}##[[:alnum:]]##=*}%%=*}
)
2014-07-15 03:24:46 +04:00
_store_cache SYS_ALL_PROPRTIES _sys_all_properties
fi
_values -s , "${_sys_all_properties[@]}"
}
2015-05-18 07:58:24 +03:00
_job_modes() {
local -a _modes
_modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
_values -s , "${_modes[@]}"
}
2015-05-19 01:45:31 +03:00
local -a _modes; _modes=("--user" "--system")
local _sys_service_mgr=${${words:*_modes}[(R)(${(j.|.)_modes})]:---system}
2013-07-29 23:02:01 +04:00
_arguments -s \
{-h,--help}'[Show help]' \
'--version[Show package version]' \
2013-08-21 07:06:54 +04:00
{-t+,--type=}'[List only units of a particular type]:unit type:_unit_types' \
2015-05-18 07:58:24 +03:00
'--state=[Display units in the specified state]:unit state:_unit_states' \
'--job-mode=[Specify how to deal with other jobs]:mode:_job_modes' \
2014-07-15 03:24:46 +04:00
{-p+,--property=}'[Show only properties by specific name]:unit property:_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]' \
'--failed[Show only failed units]' \
{-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]" \
'--global[Enable/disable unit files globally]' \
"--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]' \
'--root=[Enable unit files in the specified root directory]:directory:_directories' \
'--runtime[Enable 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]' \
'*::systemctl command:_systemctl_command'