|
|
|
1 |
#!/bin/sh |
2 |
# Univention Net Installer Daemon |
3 |
# init script |
4 |
# |
5 |
# Copyright 2010 Univention GmbH |
6 |
# |
7 |
# http://www.univention.de/ |
8 |
# |
9 |
# All rights reserved. |
10 |
# |
11 |
# The source code of this program is made available |
12 |
# under the terms of the GNU Affero General Public License version 3 |
13 |
# (GNU AGPL V3) as published by the Free Software Foundation. |
14 |
# |
15 |
# Binary versions of this program provided by Univention to you as |
16 |
# well as other copyrighted, protected or trademarked materials like |
17 |
# Logos, graphics, fonts, specific documentations and configurations, |
18 |
# cryptographic keys etc. are subject to a license agreement between |
19 |
# you and Univention and not subject to the GNU AGPL V3. |
20 |
# |
21 |
# In the case you use this program under the terms of the GNU AGPL V3, |
22 |
# the program is provided in the hope that it will be useful, |
23 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
24 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 |
# GNU Affero General Public License for more details. |
26 |
# |
27 |
# You should have received a copy of the GNU Affero General Public |
28 |
# License with the Debian GNU/Linux or Univention distribution in file |
29 |
# /usr/share/common-licenses/AGPL-3; if not, see |
30 |
# <http://www.gnu.org/licenses/>. |
31 |
# |
32 |
### BEGIN INIT INFO |
33 |
# Provides: univention-net-installer-daemon |
34 |
# Required-Start: $network $local_fs |
35 |
# Required-Stop: |
36 |
# Should-Start: $named |
37 |
# Should-Stop: |
38 |
# Default-Start: 2 3 4 5 |
39 |
# Default-Stop: 0 1 6 |
40 |
# Short-Description: Univention Net Installer Daemon |
41 |
# Description: Service to reset the reinstall flag |
42 |
### END INIT INFO |
43 |
|
44 |
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin |
45 |
|
46 |
DAEMON=/usr/sbin/univention-net-installer-daemon # Introduce the server's location here |
47 |
NAME="univention-net-installer-daemon" # Introduce the short server's name here |
48 |
DESC="Univention Net Installer Daemon" # Introduce a short description here |
49 |
LOGDIR=/var/log/univention # Log directory to use |
50 |
|
51 |
PIDFILE=/var/run/$NAME.pid |
52 |
|
53 |
test -x "$DAEMON" || exit 0 |
54 |
|
55 |
. /lib/lsb/init-functions |
56 |
|
57 |
# Default options, these can be overriden by the information |
58 |
# at /etc/default/$NAME |
59 |
DAEMON_OPTS="-p 49173 -D -d 1" # Additional options given to the server |
60 |
|
61 |
DIETIME=10 # Time to wait for the server to die, in seconds |
62 |
# If this value is set too low you might not |
63 |
# let some servers to die gracefully and |
64 |
# 'restart' will not work |
65 |
|
66 |
STARTTIME=2 # Time to wait for the server to start, in seconds |
67 |
# If this value is set each time the server is |
68 |
# started (on start or restart) the script will |
69 |
# stall to try to determine if it is running |
70 |
# If it is not set and the server takes time |
71 |
# to setup a pid file the log message might |
72 |
# be a false positive (says it did not start |
73 |
# when it actually did) |
74 |
|
75 |
LOGFILE="$LOGDIR/$NAME.log" # Server logfile |
76 |
|
77 |
# Include defaults if available |
78 |
if [ -f "/etc/default/$NAME" ] ; then |
79 |
. "/etc/default/$NAME" |
80 |
fi |
81 |
|
82 |
set -e |
83 |
|
84 |
running_pid() { |
85 |
# Check if a given process pid's cmdline matches a given name |
86 |
pid=$1 |
87 |
name=$2 |
88 |
[ -z "$pid" ] && return 1 |
89 |
[ ! -d /proc/$pid ] && return 1 |
90 |
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|sed -ne 2p |cut -d : -f 1` |
91 |
# Is this the expected server |
92 |
[ "$cmd" != "$name" ] && return 1 |
93 |
return 0 |
94 |
} |
95 |
|
96 |
running() { |
97 |
# Check if the process is running looking at /proc |
98 |
# No pidfile, probably no daemon present |
99 |
[ ! -f "$PIDFILE" ] && return 1 |
100 |
pid=`cat $PIDFILE` |
101 |
running_pid $pid "$DAEMON" || return 1 |
102 |
return 0 |
103 |
} |
104 |
|
105 |
start_server() { |
106 |
start_daemon -p "$PIDFILE" "$DAEMON" $DAEMON_OPTS |
107 |
errcode=$? |
108 |
return $errcode |
109 |
} |
110 |
|
111 |
stop_server() { |
112 |
killproc -p "$PIDFILE" "$DAEMON" |
113 |
errcode=$? |
114 |
return $errcode |
115 |
} |
116 |
|
117 |
reload_server() { |
118 |
[ ! -f "$PIDFILE" ] && return 1 |
119 |
pid=pidofproc $PIDFILE # This is the daemon's pid |
120 |
# Send a SIGHUP |
121 |
kill -1 $pid |
122 |
return $? |
123 |
} |
124 |
|
125 |
force_stop() { |
126 |
# Force the process to die killing it manually |
127 |
[ ! -e "$PIDFILE" ] && return |
128 |
if running ; then |
129 |
kill -15 $pid |
130 |
# Is it really dead? |
131 |
sleep "$DIETIME"s |
132 |
if running ; then |
133 |
kill -9 $pid |
134 |
sleep "$DIETIME"s |
135 |
if running ; then |
136 |
echo "Cannot kill $NAME (pid=$pid)!" |
137 |
exit 1 |
138 |
fi |
139 |
fi |
140 |
fi |
141 |
rm -f "$PIDFILE" |
142 |
} |
143 |
|
144 |
|
145 |
case "$1" in |
146 |
start) |
147 |
log_daemon_msg "Starting $DESC " "$NAME" |
148 |
# Check if it's running first |
149 |
if running ; then |
150 |
log_progress_msg "apparently already running" |
151 |
log_end_msg 0 |
152 |
exit 0 |
153 |
fi |
154 |
if start_server ; then |
155 |
# NOTE: Some servers might die some time after they start, |
156 |
# this code will detect this issue if STARTTIME is set |
157 |
# to a reasonable value |
158 |
[ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time |
159 |
if running ; then |
160 |
# It's ok, the server started and is running |
161 |
log_end_msg 0 |
162 |
else |
163 |
# It is not running after we did start |
164 |
log_end_msg 1 |
165 |
fi |
166 |
else |
167 |
# Either we could not start it |
168 |
log_end_msg 1 |
169 |
fi |
170 |
;; |
171 |
stop) |
172 |
log_daemon_msg "Stopping $DESC" "$NAME" |
173 |
if running ; then |
174 |
# Only stop the server if we see it running |
175 |
errcode=0 |
176 |
stop_server || errcode=$? |
177 |
log_end_msg $errcode |
178 |
else |
179 |
# If it's not running don't do anything |
180 |
log_progress_msg "apparently not running" |
181 |
log_end_msg 0 |
182 |
exit 0 |
183 |
fi |
184 |
;; |
185 |
force-stop) |
186 |
# First try to stop gracefully the program |
187 |
"$0" stop |
188 |
if running; then |
189 |
# If it's still running try to kill it more forcefully |
190 |
log_daemon_msg "Stopping (force) $DESC" "$NAME" |
191 |
errcode=0 |
192 |
force_stop || errcode=$? |
193 |
log_end_msg $errcode |
194 |
fi |
195 |
;; |
196 |
restart|force-reload) |
197 |
log_daemon_msg "Restarting $DESC" "$NAME" |
198 |
errcode=0 |
199 |
stop_server || errcode=$? |
200 |
# Wait some sensible amount, some server need this |
201 |
[ -n "$DIETIME" ] && sleep $DIETIME |
202 |
start_server || errcode=$? |
203 |
[ -n "$STARTTIME" ] && sleep $STARTTIME |
204 |
running || errcode=$? |
205 |
log_end_msg $errcode |
206 |
;; |
207 |
status) |
208 |
|
209 |
log_daemon_msg "Checking status of $DESC" "$NAME" |
210 |
if running ; then |
211 |
log_progress_msg "running" |
212 |
log_end_msg 0 |
213 |
else |
214 |
log_progress_msg "apparently not running" |
215 |
log_end_msg 1 |
216 |
exit 1 |
217 |
fi |
218 |
;; |
219 |
# Use this if the daemon cannot reload |
220 |
reload) |
221 |
log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" |
222 |
log_warning_msg "cannot re-read the config file (use restart)." |
223 |
;; |
224 |
*) |
225 |
N=/etc/init.d/$NAME |
226 |
echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 |
227 |
exit 1 |
228 |
;; |
229 |
esac |
230 |
|
231 |
exit 0 |
232 |
# vim:set ft=sh: |
233 |
|