ripasso-cursive: cosmetic changes (#361736)
[NixPkgs.git] / pkgs / by-name / se / service-wrapper / service-wrapper.sh
blob2889adc186860bffe7235f6e03806fc446322674
1 #!@shell@
3 ###########################################################################
4 # /usr/bin/service
6 # A convenient wrapper for the /etc/init.d init scripts.
8 # This script is a modified version of the /sbin/service utility found on
9 # Red Hat/Fedora systems (licensed GPLv2+).
11 # Copyright (C) 2006 Red Hat, Inc. All rights reserved.
12 # Copyright (C) 2008 Canonical Ltd.
13 # * August 2008 - Dustin Kirkland <kirkland@canonical.com>
14 # Copyright (C) 2013 Michael Stapelberg <stapelberg@debian.org>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 # On Debian GNU/Linux systems, the complete text of the GNU General
31 # Public License can be found in `/usr/share/common-licenses/GPL-2'.
32 ###########################################################################
35 is_ignored_file() {
36 case "$1" in
37 skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
38 return 0
40 esac
41 return 1
44 VERSION=$(@coreutils@/bin/basename $0)" ver. 19-04"
45 USAGE="Usage: "$(@coreutils@/bin/basename $0)" < option > | --status-all | \
46 [ service_name [ command | --full-restart ] ]"
47 SERVICE=
48 ACTION=
49 SERVICEDIR="/etc/init.d"
50 OPTIONS=
51 is_systemd=
54 if [ $# -eq 0 ]; then
55 echo "${USAGE}" >&2
56 exit 1
59 if [ -d /run/systemd/system ]; then
60 is_systemd=1
63 cd /
64 while [ $# -gt 0 ]; do
65 case "${1}" in
66 --help | -h | --h* )
67 echo "${USAGE}" >&2
68 exit 0
70 --version | -V )
71 echo "${VERSION}" >&2
72 exit 0
75 if [ -z "${SERVICE}" -a $# -eq 1 -a "${1}" = "--status-all" ]; then
76 if [ -d "${SERVICEDIR}" ]; then
77 cd ${SERVICEDIR}
78 for SERVICE in * ; do
79 case "${SERVICE}" in
80 functions | halt | killall | single| linuxconf| kudzu)
83 if ! is_ignored_file "${SERVICE}" \
84 && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
85 out=$(env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status 2>&1)
86 retval=$?
87 if echo "$out" | egrep -iq "usage:"; then
88 #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
89 echo " [ ? ] $SERVICE" 1>&2
90 continue
91 else
92 if [ "$retval" = "0" -a -n "$out" ]; then
93 #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
94 echo " [ + ] $SERVICE"
95 continue
96 else
97 #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
98 echo " [ - ] $SERVICE"
99 continue
102 #env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" status
105 esac
106 done
107 else
108 systemctl $sctl_args list-units
110 exit 0
111 elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
112 SERVICE="${1}"
113 # On systems using systemd, we just perform a normal restart:
114 # A restart with systemd is already a full restart.
115 if [ -n "$is_systemd" ]; then
116 ACTION="restart"
117 else
118 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
119 env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" stop
120 env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" start
121 exit $?
124 elif [ -z "${SERVICE}" ]; then
125 SERVICE="${1}"
126 elif [ -z "${ACTION}" ]; then
127 ACTION="${1}"
128 else
129 OPTIONS="${OPTIONS} ${1}"
131 shift
133 esac
134 done
136 run_via_sysvinit() {
137 # Otherwise, use the traditional sysvinit
138 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
139 exec env -i LANG="$LANG" LANGUAGE="$LANGUAGE" LC_CTYPE="$LC_CTYPE" LC_NUMERIC="$LC_NUMERIC" LC_TIME="$LC_TIME" LC_COLLATE="$LC_COLLATE" LC_MONETARY="$LC_MONETARY" LC_MESSAGES="$LC_MESSAGES" LC_PAPER="$LC_PAPER" LC_NAME="$LC_NAME" LC_ADDRESS="$LC_ADDRESS" LC_TELEPHONE="$LC_TELEPHONE" LC_MEASUREMENT="$LC_MEASUREMENT" LC_IDENTIFICATION="$LC_IDENTIFICATION" LC_ALL="$LC_ALL" PATH="$PATH" TERM="$TERM" "$SERVICEDIR/$SERVICE" ${ACTION} ${OPTIONS}
140 else
141 echo "${SERVICE}: unrecognized service" >&2
142 exit 1
146 update_openrc_started_symlinks() {
147 # maintain the symlinks of /run/openrc/started so that
148 # rc-status works with the service command as well
149 if [ -d /run/openrc/started ] ; then
150 case "${ACTION}" in
151 start)
152 if [ ! -h /run/openrc/started/$SERVICE ] ; then
153 ln -s $SERVICEDIR/$SERVICE /run/openrc/started/$SERVICE || true
156 stop)
157 rm /run/openrc/started/$SERVICE || true
159 esac
163 # When this machine is running systemd, standard service calls are turned into
164 # systemctl calls.
165 if [ -n "$is_systemd" ]
166 then
167 UNIT="${SERVICE%.sh}.service"
168 # avoid deadlocks during bootup and shutdown from units/hooks
169 # which call "invoke-rc.d service reload" and similar, since
170 # the synchronous wait plus systemd's normal behaviour of
171 # transactionally processing all dependencies first easily
172 # causes dependency loops
173 if ! systemctl --quiet is-active multi-user.target; then
174 sctl_args="--job-mode=ignore-dependencies"
177 case "${ACTION}" in
178 restart|status|try-restart)
179 exec systemctl $sctl_args ${ACTION} ${UNIT}
181 start|stop)
182 # Follow the principle of least surprise for SysV people:
183 # When running "service foo stop" and foo happens to be a service that
184 # has one or more .socket files, we also stop the .socket units.
185 # Users who need more control will use systemctl directly.
186 for unit in $(systemctl list-unit-files --full --type=socket 2>/dev/null | sed -ne 's/\.socket\s*[a-z]*\s*$/.socket/p'); do
187 if [ "$(systemctl -p Triggers show $unit)" = "Triggers=${UNIT}" ]; then
188 systemctl $sctl_args ${ACTION} $unit
190 done
191 exec systemctl $sctl_args ${ACTION} ${UNIT}
193 reload)
194 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
195 if [ "$_canreload" = "CanReload=no" ]; then
196 # The reload action falls back to the sysv init script just in case
197 # the systemd service file does not (yet) support reload for a
198 # specific service.
199 run_via_sysvinit
200 else
201 exec systemctl $sctl_args reload "${UNIT}"
204 force-stop)
205 exec systemctl --signal=KILL kill "${UNIT}"
207 force-reload)
208 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
209 if [ "$_canreload" = "CanReload=no" ]; then
210 exec systemctl $sctl_args restart "${UNIT}"
211 else
212 exec systemctl $sctl_args reload "${UNIT}"
216 # We try to run non-standard actions by running
217 # the init script directly.
218 run_via_sysvinit
220 esac
223 update_openrc_started_symlinks
224 run_via_sysvinit