2021-06-16 15:35:12 -04:00
#compdef systemctl
2020-11-09 13:23:58 +09:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2013-07-29 14:02:01 -05:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_commands] )) || _systemctl_commands()
2013-07-29 14:02:01 -05:00
{
2020-01-07 20:17:26 +00:00
local expl i
2019-12-23 17:17:31 +00:00
local -a unit_commands=(
2019-12-23 16:44:28 +00:00
# Unit Commands
2022-07-22 17:07:56 +02:00
"list-automounts:List automounts"
2023-02-20 20:11:57 +01:00
"list-paths:List paths"
2019-04-05 11:39:14 +02:00
"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"
"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"
2019-12-23 16:44:28 +00:00
"set-property:Sets one or more properties of a unit"
2023-05-07 14:08:29 -07:00
"service-log-level:Get or set the logging threshold for service"
"service-log-target:Get or set the logging target for service"
2019-12-23 16:44:28 +00:00
"help:Show documentation for specified units"
2019-04-05 11:39:14 +02:00
"reset-failed:Reset failed state for all, one, or more units"
2019-12-23 16:44:28 +00:00
"list-dependencies:Show unit dependency tree"
2019-12-23 16:49:51 +00:00
"clean:Remove configuration, state, cache, logs or runtime data of units"
2020-07-30 19:37:10 +01:00
"bind:Bind mount a path from the host into a unit's namespace"
2021-01-21 18:37:40 +00:00
"mount-image:Mount an image from the host into a unit's namespace"
2023-07-07 14:02:41 +02:00
"whoami:Determines as part of which unit the command is being invoked"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:49:51 +00:00
2019-12-23 17:17:31 +00:00
local -a machine_commands=(
2019-12-23 16:49:51 +00:00
# Machine Commands
"list-machines:List the host and all running local containers"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2019-12-23 17:17:31 +00:00
local -a unit_file_commands=(
2019-12-23 16:44:28 +00:00
# Unit File Commands
2019-04-05 11:39:14 +02:00
"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"
2019-06-11 08:52:42 +02:00
"preset-all:Enable/disable all unit files based on preset configuration"
2019-12-23 16:44:28 +00:00
"is-enabled:Check whether unit files are enabled"
2019-04-05 11:39:14 +02:00
"mask:Mask one or more units"
"unmask:Unmask one or more units"
"link:Link one or more units files into the search path"
2019-12-23 16:44:28 +00:00
"revert:Revert unit files to their vendor versions"
"add-wants:Add Wants= dependencies to a unit"
"add-requires:Add Requires= dependencies to a unit"
"set-default:Set the default target"
"get-default:Query the default target"
"edit:Edit one or more unit files"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2019-12-23 17:17:31 +00:00
local -a job_commands=(
2019-12-23 16:44:28 +00:00
# Job Commands
2019-04-05 11:39:14 +02:00
"list-jobs:List jobs"
"cancel:Cancel all, one, or more jobs"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2019-12-23 17:17:31 +00:00
local -a environment_commands=(
2019-12-23 16:44:28 +00:00
# Environment Commands
2019-04-05 11:39:14 +02:00
"show-environment:Dump environment"
"set-environment:Set one or more environment variables"
"unset-environment:Unset one or more environment variables"
2019-12-23 16:49:51 +00:00
"import-environment:Import environment variables set on the client"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2019-12-23 17:17:31 +00:00
local -a manager_state_commands=(
2019-12-23 16:44:28 +00:00
# Manager State Commands
2019-04-05 11:39:14 +02:00
"daemon-reload:Reload systemd manager configuration"
"daemon-reexec:Reexecute systemd manager"
2019-12-23 16:49:51 +00:00
"log-level:Get or set the log level"
"log-target:Get or set the log target"
"service-watchdogs:Get or set the state of software watchdogs"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2019-12-23 17:17:31 +00:00
local -a system_commands=(
2019-12-23 16:44:28 +00:00
# System Commands
"is-system-running:Query overall status of the system"
2019-04-05 11:39:14 +02:00
"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"
2023-07-07 13:49:11 +02:00
"soft-reboot:Shut down and reboot the userspace"
2019-04-05 11:39:14 +02:00
"kexec:Shut down and reboot the system with kexec"
"exit:Ask for user instance termination"
"switch-root:Change root directory"
2019-12-23 16:44:28 +00:00
"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"
2019-12-23 17:17:31 +00:00
)
2019-12-23 16:44:28 +00:00
2020-01-07 20:17:26 +00:00
local -a groups=( unit machine unit_file job environment manager_state system )
local -a _systemctl_cmds
for i in $groups; do
_systemctl_cmds+=( "${(@P)${:-"${i}_commands"}}" )
done
2019-04-05 11:39:14 +02:00
if (( CURRENT == 1 )); then
2020-01-07 20:17:26 +00:00
_tags ${^groups//_/-}-commands
while _tags; do
for i in $groups; do
if _requested ${i//_/-}-commands; then
_describe -t ${i//_/-}-commands "${i//_/ } command" ${i}_commands \
&& ret=0
fi
done
done
2013-07-29 14:02:01 -05:00
else
2020-01-07 20:17:26 +00:00
local curcontext="$curcontext"
2019-04-05 11:39:14 +02:00
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 14:02:01 -05:00
fi
}
2018-12-24 05:15:38 -06:00
# @todo _systemd-run has a helper with the same name, so we must redefine
2013-07-29 14:02:01 -05:00
__systemctl()
{
2023-07-17 16:17:20 -07:00
command systemctl $_sys_service_mgr --full --legend=no --no-pager --plain "$@" 2>/dev/null
2013-07-29 14:02:01 -05:00
}
# Fills the unit list
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_all_units] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_all_units()
2013-07-29 14:02:01 -05:00
{
2023-07-17 16:06:59 -07:00
if _cache_invalid SYS_ALL_UNITS$_sys_service_mgr || ! _retrieve_cache SYS_ALL_UNITS$_sys_service_mgr;
2019-04-05 11:39:14 +02:00
then
2023-05-05 12:33:29 -07:00
_sys_all_units=( ${${(f)"$(__systemctl list-units --all)"}%% *} )
2023-07-17 14:44:09 -07:00
_store_cache SYS_ALL_UNITS$_sys_service_mgr _sys_all_units
2019-04-05 11:39:14 +02:00
fi
2013-07-29 14:02:01 -05:00
}
# Fills the unit list including all file units
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_really_all_units] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_really_all_units()
2013-07-29 14:02:01 -05:00
{
2019-04-05 11:39:14 +02:00
local -a all_unit_files;
local -a really_all_units;
2023-07-17 16:06:59 -07:00
if _cache_invalid SYS_REALLY_ALL_UNITS$_sys_service_mgr || ! _retrieve_cache SYS_REALLY_ALL_UNITS$_sys_service_mgr;
2019-04-05 11:39:14 +02:00
then
2023-05-05 12:33:29 -07:00
all_unit_files=( ${${(f)"$(__systemctl list-unit-files)"}%% *} )
2019-04-05 11:39:14 +02: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_service_mgr _sys_really_all_units
fi
2013-07-29 14:02:01 -05:00
}
2018-12-24 05:15:38 -06:00
(( $+functions[_filter_units_by_property] )) ||
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_get_non_template_names] )) ||
2023-08-07 12:13:23 -07:00
_systemctl_get_non_template_names() {
_systemctl_really_all_units
print -r - ${_sys_really_all_units:#*@.*}
}
2018-06-11 11:53:08 +01:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_get_template_names] )) ||
2023-07-19 14:26:14 -07:00
_systemctl_get_template_names() {
local pathkind=systemd-search-${_sys_service_mgr##*--}-unit
print -r - ${(s-:-)^$(_call_program $pathkind systemd-path $pathkind)}/*@.(${(~j.|.)$(__systemctl --type=help)})(N:t:r)
}
2014-10-14 21:10:02 -04:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_active_units] )) ||
2023-08-08 01:30:28 -07:00
_systemctl_active_units() {
local pattern
if zstyle -T ":completion:$curcontext" use-pattern; then
pattern="$PREFIX*$SUFFIX"
fi
_sys_active_units=( ${${(f)"$(__systemctl list-units $pattern)"}%% *} )
}
2014-10-29 23:47:55 -04:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_startable_units] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_startable_units(){
2023-08-08 01:30:28 -07:00
local pattern
if zstyle -T ":completion:$curcontext" use-pattern; then
pattern="$PREFIX*$SUFFIX"
fi
2019-04-05 11:39:14 +02:00
_sys_startable_units=( $( _filter_units_by_property ActiveState inactive $(
_filter_units_by_property CanStart yes ${${${(f)"$(
2023-08-08 01:30:28 -07:00
__systemctl list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient $pattern
__systemctl list-units --state inactive,failed $pattern
2018-01-28 06:29:58 -06:00
)"}:#*@.*}%%[[:space:]]*}
2019-04-05 11:39:14 +02:00
)) )
}
2014-10-29 23:47:55 -04:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_restartable_units] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_restartable_units(){
2023-08-08 01:30:28 -07:00
local pattern
if zstyle -T ":completion:$curcontext" use-pattern; then
pattern="$PREFIX*$SUFFIX"
fi
2019-04-05 11:39:14 +02:00
_sys_restartable_units=( $( _filter_units_by_property CanStart yes ${${${(f)"$(
2023-08-08 01:30:28 -07:00
__systemctl list-unit-files --state enabled,disabled,static $pattern
__systemctl list-units $pattern
2018-01-28 06:29:58 -06:00
)"}:#*@.*}%%[[:space:]]*} ) )
2019-04-05 11:39:14 +02:00
}
2014-10-29 23:47:55 -04:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_failed_units] )) ||
2023-05-05 12:33:29 -07:00
_systemctl_failed_units() {_sys_failed_units=( ${${(f)"$(__systemctl list-units --state=failed)"}%% *} ) }
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_unit_state] )) ||
2020-04-27 00:30:49 +02:00
_systemctl_unit_state() {
setopt localoptions extendedglob
2023-08-08 01:30:28 -07:00
local pattern
if zstyle -T ":completion:$curcontext" use-pattern; then
pattern="$PREFIX*$SUFFIX"
fi
2020-04-27 00:30:49 +02:00
typeset -gA _sys_unit_state
2023-08-08 01:30:28 -07:00
_sys_unit_state=( ${=${${(f)"$(__systemctl list-unit-files $pattern)"}%%[[:space:]]#}% *} )
2020-04-27 00:30:49 +02:00
}
2013-07-29 14:02:01 -05:00
2015-05-18 01:02:38 -04:00
local fun
2013-07-29 14:02:01 -05:00
# Completion functions for ALL_UNITS
2018-06-11 11:53:08 +01:00
for fun in cat mask ; do
2019-04-05 11:39:14 +02:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
_systemctl_really_all_units
_wanted systemd-units expl unit \
compadd "$@" -a - _sys_really_all_units
}
2013-07-29 14:02:01 -05:00
done
2018-06-11 11:53:08 +01:00
# Completion functions for NONTEMPLATE_UNITS
2018-11-20 12:17:11 +08: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 11:39:14 +02:00
(( $+functions[_systemctl_$fun] )) || _systemctl_$fun()
{
_wanted systemd-units expl unit \
compadd "$@" - $(_systemctl_get_non_template_names)
}
2018-06-11 11:53:08 +01:00
done
2013-07-29 14:02:01 -05:00
# Completion functions for ENABLED_UNITS
2014-10-14 21:10:02 -04:00
(( $+functions[_systemctl_disable] )) || _systemctl_disable()
{
2015-05-18 01:02:39 -04:00
local _sys_unit_state; _systemctl_unit_state
2015-05-29 10:40:11 -04:00
_wanted systemd-units expl 'enabled unit' \
2019-04-05 11:39:14 +02:00
compadd "$@" - ${(k)_sys_unit_state[(R)enabled]}
2014-10-14 21:10:02 -04:00
}
(( $+functions[_systemctl_reenable] )) || _systemctl_reenable()
{
2015-05-18 01:02:39 -04:00
local _sys_unit_state; _systemctl_unit_state
2015-05-29 10:40:11 -04:00
_wanted systemd-units expl 'enabled/disabled unit' \
2019-04-05 11:39:14 +02:00
compadd "$@" - ${(k)_sys_unit_state[(R)(enabled|disabled)]} $(_systemctl_get_template_names)
2014-10-14 21:10:02 -04:00
}
2013-07-29 14:02:01 -05:00
# Completion functions for DISABLED_UNITS
(( $+functions[_systemctl_enable] )) || _systemctl_enable()
{
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
}
# Completion functions for FAILED_UNITS
(( $+functions[_systemctl_reset-failed] )) || _systemctl_reset-failed()
{
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
}
# Completion functions for STARTABLE_UNITS
(( $+functions[_systemctl_start] )) || _systemctl_start()
{
2019-04-05 11:39:14 +02:00
local _sys_startable_units; _systemctl_startable_units
_wanted systemd-units expl 'startable unit' \
compadd "$@" - ${_sys_startable_units[*]}
2013-07-29 14:02:01 -05:00
}
# Completion functions for STOPPABLE_UNITS
for fun in stop kill try-restart condrestart ; do
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
done
2023-05-07 14:08:29 -07:00
(( $+functions[_systemctl_service-log-level] )) ||
_systemctl_service-log-level() {
local -a log_levels=( emerg alert crit err warning notice info debug )
local _sys_active_units; _systemctl_active_units
if (( CURRENT == 2 )); then
_wanted systemd-units expl 'active unit' \
compadd "$@" -a - _sys_active_units || _message "no units found"
else
compadd "$@" -a - log_levels
fi
}
(( $+functions[_systemctl_service-log-target] )) ||
_systemctl_service-log-target() {
local -a log_targets=( console kmsg journal syslog null auto )
local _sys_active_units; _systemctl_active_units
if (( CURRENT == 2 )); then
_wanted systemd-units expl 'active unit' \
compadd "$@" -a - _sys_active_units || _message "no units found"
else
compadd "$@" -a - log_targets
fi
}
2013-07-29 14:02:01 -05:00
# Completion functions for ISOLATABLE_UNITS
(( $+functions[_systemctl_isolate] )) || _systemctl_isolate()
{
2019-04-05 11:39:14 +02:00
_systemctl_all_units
_wanted systemd-units expl 'isolatable unit' \
compadd "$@" - $( _filter_units_by_property AllowIsolate yes \
${_sys_all_units[*]} )
2013-07-29 14:02:01 -05:00
}
# Completion functions for RELOADABLE_UNITS
2016-01-28 18:57:04 +01:00
for fun in reload try-reload-or-restart force-reload ; do
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
done
# Completion functions for RESTARTABLE_UNITS
for fun in restart reload-or-restart ; do
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
done
# Completion functions for MASKED_UNITS
(( $+functions[_systemctl_unmask] )) || _systemctl_unmask()
{
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
}
# Completion functions for JOBS
(( $+functions[_systemctl_cancel] )) || _systemctl_cancel()
{
2019-04-05 11:39:14 +02:00
_wanted systemd-jobs expl job \
compadd "$@" - ${${(f)"$(__systemctl list-jobs)"}%% *} ||
_message "no jobs found"
2013-07-29 14:02:01 -05:00
}
2014-10-27 21:34:28 -05:00
# Completion functions for TARGETS
(( $+functions[_systemctl_set-default] )) || _systemctl_set-default()
{
2019-04-05 11:39:14 +02:00
_wanted systemd-targets expl target \
2023-05-05 12:33:29 -07:00
compadd "$@" - ${${(f)"$(__systemctl list-unit-files --type target --all)"}%% *} ||
2019-04-05 11:39:14 +02:00
_message "no targets found"
2013-07-29 14:02:01 -05:00
}
# Completion functions for ENVS
for fun in set-environment unset-environment ; do
2019-04-05 11:39:14 +02: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 14:02:01 -05:00
done
2021-01-05 12:34:28 +01:00
(( $+functions[_systemctl_import-environment] )) || _systemctl_import-environment()
{
_parameters
}
2014-07-21 21:11:56 -04:00
(( $+functions[_systemctl_link] )) || _systemctl_link() {
2019-04-05 11:39:14 +02:00
_sd_unit_files
}
2013-07-29 14:02:01 -05:00
2015-04-02 22:54:35 -04:00
(( $+functions[_systemctl_switch-root] )) || _systemctl_switch-root() {
2019-04-05 11:39:14 +02:00
_files
}
2015-04-02 22:54:35 -04:00
2020-07-30 19:37:10 +01:00
(( $+functions[_systemctl_bind] )) || _systemctl_bind() {
_files
}
2021-01-21 18:37:40 +00:00
(( $+functions[_systemctl_mount-image] )) || _systemctl_mount-image() {
_files
}
2013-07-29 14:02:01 -05:00
# no systemctl completion for:
2013-10-21 18:53:57 -04:00
# [STANDALONE]='daemon-reexec daemon-reload default
2013-07-29 14:02:01 -05:00
# emergency exit halt kexec list-jobs list-units
# list-unit-files poweroff reboot rescue show-environment'
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_caching_policy] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_caching_policy()
2013-07-29 14:02:01 -05:00
{
2019-04-05 11:39:14 +02:00
# rebuild if cache is more than a day old
zsh: update default caching policy for units
The existing caching policy was completely bogus.
In the first stanza, despite the comment, the pattern given would
consider the cache invalid if it was more than 1 hour old.
The second stanza was also incorrect, since the output of `systemctl
--all` is not unit file paths, but unit names. When they were being
tested against the cachefile mtime, the test would always fail becuase
of the nonexistant file (hopefully).
In fact it's not very useful to test if the unit files have newer mtime
in this case anyway, since we are only caching their names. Also,
`systemctl --all` is an unfortunately slow operation to be used in
testing for the cache validity — we want this operation to at least be
faster than rebuilding the cache.
I've rewritten this stanza with my best guess at its original intent. It
now checks against the mtime of the parent directories in the search
path, which should be updated and cause the cache to rebuild when we
add, remove, or rename any unit files.
2023-07-17 16:24:36 -07:00
[[ -n $1(#qNmd+1) ]] && return 0
2013-07-29 14:02:01 -05:00
zsh: update default caching policy for units
The existing caching policy was completely bogus.
In the first stanza, despite the comment, the pattern given would
consider the cache invalid if it was more than 1 hour old.
The second stanza was also incorrect, since the output of `systemctl
--all` is not unit file paths, but unit names. When they were being
tested against the cachefile mtime, the test would always fail becuase
of the nonexistant file (hopefully).
In fact it's not very useful to test if the unit files have newer mtime
in this case anyway, since we are only caching their names. Also,
`systemctl --all` is an unfortunately slow operation to be used in
testing for the cache validity — we want this operation to at least be
faster than rebuilding the cache.
I've rewritten this stanza with my best guess at its original intent. It
now checks against the mtime of the parent directories in the search
path, which should be updated and cause the cache to rebuild when we
add, remove, or rename any unit files.
2023-07-17 16:24:36 -07:00
local pathkind=systemd-search-${1##*--}-unit
for dir in ${(s-:-)^$(_call_program $pathkind systemd-path $pathkind)}; do
[[ $dir -nt $1 ]] && return 0
done
2013-07-29 14:02:01 -05:00
2019-04-05 11:39:14 +02:00
return 1
2013-07-29 14:02:01 -05:00
}
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_unit_states] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_unit_states() {
local -a _states
_states=("${(fo)$(__systemctl --state=help)}")
_values -s , "${_states[@]}"
}
2013-08-14 22:09:14 -05:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_unit_types] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_unit_types() {
local -a _types
_types=("${(fo)$(__systemctl -t help)}")
_values -s , "${_types[@]}"
}
2013-08-14 22:09:14 -05:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_unit_properties] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_unit_properties() {
2023-06-12 02:15:19 +01:00
local -a _sys_all_properties=( ${(f)"$({{LIBEXECDIR}}/systemd --no-pager --dump-bus-properties 2>/dev/null)"} )
2023-07-18 16:52:18 -07:00
_wanted systemd-unit-properties expl 'unit property' \
_values -s , "${_sys_all_properties[@]}"
2019-04-05 11:39:14 +02:00
}
2014-07-14 19:24:46 -04:00
2018-12-24 05:15:38 -06:00
(( $+functions[_systemctl_job_modes] )) ||
2019-04-05 11:39:14 +02:00
_systemctl_job_modes() {
local -a _modes
_modes=(fail replace replace-irreversibly isolate ignore-dependencies ignore-requirements flush)
_values -s , "${_modes[@]}"
}
2015-05-18 00:58:24 -04:00
2020-06-19 11:26:22 +01:00
(( $+functions[_systemctl_timestamp] )) ||
_systemctl_timestamp() {
local -a _styles
_styles=(help pretty us µs utc us+utc µs+utc)
_values -s , "${_styles[@]}"
}
2020-10-29 12:48:48 +01:00
(( $+functions[_systemctl_check_inhibitors] )) ||
_systemctl_check_inhibitors() {
local -a _modes
_modes=(auto yes no)
_values -s , "${_modes[@]}"
}
2016-08-13 16:42:55 +02:00
# Build arguments for "systemctl" to be used in completion.
2023-07-17 14:47:23 -07:00
# Use the last mode, or --system (they are exclusive and the last one is used).
local _sys_service_mgr=${words[(R)(--user|--system)]:---system}
2013-07-29 14:02:01 -05:00
_arguments -s \
{-h,--help}'[Show help]' \
'--version[Show package version]' \
2018-12-24 05:15:38 -06: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 14:02:01 -05: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]' \
2020-10-29 12:48:48 +01:00
'--check-inhibitors[Specify if inhibitors should be checked]:mode:_systemctl_check_inhibitors' \
2013-07-29 14:02:01 -05:00
{-q,--quiet}'[Suppress output]' \
2022-12-13 09:05:11 +09:00
'--no-warn[Suppress several warnings shown by default]' \
2013-07-29 14:02:01 -05:00
'--no-block[Do not wait until operation finished]' \
2021-02-15 20:16:33 +01:00
'--legend=no[Do not print a legend, i.e. the column headers and the footer with hints]' \
2013-07-29 14:02:01 -05:00
'--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]" \
2021-06-09 09:00:02 -04:00
'--global[Enable/disable/mask default user unit files globally]' \
2013-07-29 14:02:01 -05:00
"--no-reload[When enabling/disabling unit files, don't reload daemon configuration]" \
'--no-ask-password[Do not ask for system passwords]' \
2022-08-24 10:41:30 +02:00
'--kill-whom=[Whom to send signal to]:killwhom:(main control all)' \
2013-08-20 22:06:54 -05:00
{-s+,--signal=}'[Which signal to send]:signal:_signals' \
2013-07-29 14:02:01 -05:00
{-f,--force}'[When enabling unit files, override existing symlinks. When shutting down, execute action immediately]' \
2017-10-12 08:14:25 +02: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-11-30 22:31:31 -05:00
{-H+,--host=}'[Operate on remote host]:userathost:_sd_hosts_or_user_at_host' \
2013-07-29 14:02:01 -05:00
{-P,--privileged}'[Acquire privileges before execution]' \
2013-08-20 22:06:54 -05:00
{-n+,--lines=}'[Journal entries to show]:number of entries' \
2013-11-07 16:00:52 -06:00
{-o+,--output=}'[Change journal output mode]:modes:_sd_outputmodes' \
2015-04-03 18:03:06 +02:00
'--firmware-setup[Tell the firmware to show the setup menu on next boot]' \
2013-07-29 14:02:01 -05:00
'--plain[When used with list-dependencies, print output as a list]' \
2017-02-01 18:23:46 -05:00
'--failed[Show failed units]' \
2020-06-19 11:26:22 +01:00
'--timestamp=[Change format of printed timestamps]:style:_systemctl_timestamp' \
2018-12-24 05:15:38 -06:00
'*::systemctl command:_systemctl_commands'