···1+#!/bin/sh
2+3+###########################################################################
4+# /usr/bin/service
5+#
6+# A convenient wrapper for the /etc/init.d init scripts.
7+#
8+# This script is a modified version of the /sbin/service utility found on
9+# Red Hat/Fedora systems (licensed GPLv2+).
10+#
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>
15+#
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.
20+#
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.
25+#
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
29+#
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+###########################################################################
33+34+35+is_ignored_file() {
36+ case "$1" in
37+ skeleton | README | *.dpkg-dist | *.dpkg-old | rc | rcS | single | reboot | bootclean.sh)
38+ return 0
39+ ;;
40+ esac
41+ return 1
42+}
43+44+VERSION=$(@coreutils@/bin/basename $0)" ver. 0.91-ubuntu1"
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=
52+53+54+if [ $# -eq 0 ]; then
55+ echo "${USAGE}" >&2
56+ exit 1
57+fi
58+59+if [ -d /run/systemd/system ]; then
60+ is_systemd=1
61+fi
62+63+cd /
64+while [ $# -gt 0 ]; do
65+ case "${1}" in
66+ --help | -h | --h* )
67+ echo "${USAGE}" >&2
68+ exit 0
69+ ;;
70+ --version | -V )
71+ echo "${VERSION}" >&2
72+ exit 0
73+ ;;
74+ *)
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)
81+ ;;
82+ *)
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
100+ fi
101+ fi
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
103+ fi
104+ ;;
105+ esac
106+ done
107+ else
108+ systemctl $sctl_args list-units
109+ fi
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 $?
122+ fi
123+ fi
124+ elif [ -z "${SERVICE}" ]; then
125+ SERVICE="${1}"
126+ elif [ -z "${ACTION}" ]; then
127+ ACTION="${1}"
128+ else
129+ OPTIONS="${OPTIONS} ${1}"
130+ fi
131+ shift
132+ ;;
133+ esac
134+done
135+136+# Operate against system upstart, not session
137+unset UPSTART_SESSION
138+if [ -r "/etc/init/${SERVICE}.conf" ] && which initctl >/dev/null \
139+ && initctl version 2>/dev/null | grep -q upstart \
140+ && initctl status ${SERVICE} 2>/dev/null 1>/dev/null
141+then
142+ # Upstart configuration exists for this job and we're running on upstart
143+ case "${ACTION}" in
144+ start|stop|status|reload)
145+ # Action is a valid upstart action
146+ exec ${ACTION} ${SERVICE} ${OPTIONS}
147+ ;;
148+ restart|force-reload)
149+ # Map restart to the usual sysvinit behavior.
150+ # Map force-reload to restart as per Debian policy 9.3.2,
151+ # since there is no way to know if "reload" is supported
152+ stop ${SERVICE} ${OPTIONS} || :
153+ exec start ${SERVICE} ${OPTIONS}
154+ ;;
155+ esac
156+fi
157+158+159+run_via_sysvinit() {
160+ # Otherwise, use the traditional sysvinit
161+ if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
162+ 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}
163+ else
164+ echo "${SERVICE}: unrecognized service" >&2
165+ exit 1
166+ fi
167+}
168+169+update_openrc_started_symlinks() {
170+ # maintain the symlinks of /run/openrc/started so that
171+ # rc-status works with the service command as well
172+ if [ -d /run/openrc/started ] ; then
173+ case "${ACTION}" in
174+ start)
175+ if [ ! -h /run/openrc/started/$SERVICE ] ; then
176+ ln -s $SERVICEDIR/$SERVICE /run/openrc/started/$SERVICE || true
177+ fi
178+ ;;
179+ stop)
180+ rm /run/openrc/started/$SERVICE || true
181+ ;;
182+ esac
183+ fi
184+}
185+186+# When this machine is running systemd, standard service calls are turned into
187+# systemctl calls.
188+if [ -n "$is_systemd" ]
189+then
190+ UNIT="${SERVICE%.sh}.service"
191+ # avoid deadlocks during bootup and shutdown from units/hooks
192+ # which call "invoke-rc.d service reload" and similar, since
193+ # the synchronous wait plus systemd's normal behaviour of
194+ # transactionally processing all dependencies first easily
195+ # causes dependency loops
196+ if ! systemctl --quiet is-active multi-user.target; then
197+ sctl_args="--job-mode=ignore-dependencies"
198+ fi
199+200+ case "${ACTION}" in
201+ restart|status)
202+ exec systemctl $sctl_args ${ACTION} ${UNIT}
203+ ;;
204+ start|stop)
205+ # Follow the principle of least surprise for SysV people:
206+ # When running "service foo stop" and foo happens to be a service that
207+ # has one or more .socket files, we also stop the .socket units.
208+ # Users who need more control will use systemctl directly.
209+ for unit in $(systemctl list-unit-files --full --type=socket 2>/dev/null | sed -ne 's/\.socket\s*[a-z]*\s*$/.socket/p'); do
210+ if [ "$(systemctl -p Triggers show $unit)" = "Triggers=${UNIT}" ]; then
211+ systemctl $sctl_args ${ACTION} $unit
212+ fi
213+ done
214+ exec systemctl $sctl_args ${ACTION} ${UNIT}
215+ ;;
216+ reload)
217+ _canreload="$(SYSTEMCTL -p CanReload show ${UNIT} 2>/dev/null)"
218+ if [ "$_canreload" = "CanReload=no" ]; then
219+ # The reload action falls back to the sysv init script just in case
220+ # the systemd service file does not (yet) support reload for a
221+ # specific service.
222+ run_via_sysvinit
223+ else
224+ exec systemctl $sctl_args reload "${UNIT}"
225+ fi
226+ ;;
227+ force-stop)
228+ exec systemctl --signal=KILL kill "${UNIT}"
229+ ;;
230+ force-reload)
231+ _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
232+ if [ "$_canreload" = "CanReload=no" ]; then
233+ exec systemctl $sctl_args restart "${UNIT}"
234+ else
235+ exec systemctl $sctl_args reload "${UNIT}"
236+ fi
237+ ;;
238+ *)
239+ # We try to run non-standard actions by running
240+ # the init script directly.
241+ run_via_sysvinit
242+ ;;
243+ esac
244+fi
245+246+update_openrc_started_symlinks
247+run_via_sysvinit