Debian Codegraph @

script/service

raw symbols SCIP
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 <[email protected]>
14 # Copyright (C) 2013 Michael Stapelberg <[email protected]>
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="`basename $0` ver. __VERSION__"
45 USAGE="Usage: `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 cd ${SERVICEDIR}
77 for SERVICE in * ; do
78 case "${SERVICE}" in
79 functions | halt | killall | single| linuxconf| kudzu)
80 ;;
81 *)
82 if ! is_ignored_file "${SERVICE}" \
83 && [ -x "${SERVICEDIR}/${SERVICE}" ]; then
84 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)
85 retval=$?
86 if echo "$out" | grep -Fiq "usage:"; then
87 #printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
88 echo " [ ? ] $SERVICE" 1>&2
89 continue
90 else
91 if [ "$retval" = "0" -a -n "$out" ]; then
92 #printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
93 echo " [ + ] $SERVICE"
94 continue
95 else
96 #printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
97 echo " [ - ] $SERVICE"
98 continue
99 fi
100 fi
101 #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
102 fi
103 ;;
104 esac
105 done
106 exit 0
107 elif [ $# -eq 2 -a "${2}" = "--full-restart" ]; then
108 SERVICE="${1}"
109 # On systems using systemd, we just perform a normal restart:
110 # A restart with systemd is already a full restart.
111 if [ -n "$is_systemd" ]; then
112 ACTION="restart"
113 else
114 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
115 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
116 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
117 exit $?
118 fi
119 fi
120 elif [ -z "${SERVICE}" ]; then
121 SERVICE="${1}"
122 elif [ -z "${ACTION}" ]; then
123 ACTION="${1}"
124 else
125 OPTIONS="${OPTIONS} ${1}"
126 fi
127 shift
128 ;;
129 esac
130 done
131
132 run_via_sysvinit() {
133 # Otherwise, use the traditional sysvinit
134 if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
135 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}
136 else
137 echo "${SERVICE}: unrecognized service" >&2
138 exit 1
139 fi
140 }
141
142 update_openrc_started_symlinks() {
143 # Skip native openrc-run files
144 grep -q '^#! */\(usr/\)\?sbin/openrc-run' $SERVICEDIR/$SERVICE && return
145 # maintain the symlinks of /run/openrc/started so that
146 # rc-status works with the service command as well
147 if [ -d /run/openrc/started ] ; then
148 case "${ACTION}" in
149 start)
150 if [ ! -h /run/openrc/started/$SERVICE ] ; then
151 ln -s $SERVICEDIR/$SERVICE /run/openrc/started/$SERVICE || true
152 fi
153 ;;
154 stop)
155 rm /run/openrc/started/$SERVICE || true
156 ;;
157 esac
158 fi
159 }
160
161 # When this machine is running systemd, standard service calls are turned into
162 # systemctl calls.
163 if [ -n "$is_systemd" ]
164 then
165 UNIT="${SERVICE%.sh}.service"
166
167 case "${ACTION}" in
168 restart|status|try-restart)
169 exec systemctl $sctl_args ${ACTION} ${UNIT}
170 ;;
171 start|stop)
172 # Follow the principle of least surprise for SysV people:
173 # When running "service foo stop" and foo happens to be a service that
174 # has one or more .socket files, we also stop the .socket units.
175 # Users who need more control will use systemctl directly.
176 for unit in $(systemctl list-unit-files --full --type=socket 2>/dev/null | sed -ne 's/\.socket\s*[a-z]*\s*$/.socket/p'); do
177 if [ "$(systemctl -p Triggers show $unit)" = "Triggers=${UNIT}" ]; then
178 systemctl $sctl_args ${ACTION} $unit
179 fi
180 done
181 exec systemctl $sctl_args ${ACTION} ${UNIT}
182 ;;
183 reload)
184 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
185 # Don't block on reload requests during bootup and shutdown
186 # from units/hooks and simply schedule the task.
187 if ! systemctl --quiet is-system-running; then
188 sctl_args="--no-block"
189 fi
190 if [ "$_canreload" = "CanReload=no" ]; then
191 # The reload action falls back to the sysv init script just in case
192 # the systemd service file does not (yet) support reload for a
193 # specific service.
194 run_via_sysvinit
195 else
196 exec systemctl $sctl_args reload "${UNIT}"
197 fi
198 ;;
199 force-stop)
200 exec systemctl --signal=KILL kill "${UNIT}"
201 ;;
202 force-reload)
203 _canreload="$(systemctl -p CanReload show ${UNIT} 2>/dev/null)"
204 if [ "$_canreload" = "CanReload=no" ]; then
205 exec systemctl $sctl_args restart "${UNIT}"
206 else
207 exec systemctl $sctl_args reload "${UNIT}"
208 fi
209 ;;
210 *)
211 # We try to run non-standard actions by running
212 # the init script directly.
213 run_via_sysvinit
214 ;;
215 esac
216 fi
217
218 update_openrc_started_symlinks
219 run_via_sysvinit