updated on Mon Jan 9 08:00:21 UTC 2012
[aur-mirror.git] / avg-free / avgd
blobc04f450848a27789fbb463b759e80a62b4cbef6c
1 #!/bin/bash
3 . /etc/rc.conf
4 . /etc/rc.d/functions
6 #From avgdinit.conf
7 . /opt/avg/avg8/etc/init.d/avgdinit.conf
10 # From functions.common
12 # wait until avgd pid-file exist or time's out
13 # if time's out, kill avgd and his childern
14 wait_and_kill()
16 # print count-down limit
17 printf "\r"
18 dist_echo_stop_begin
19 i=$AVG_WAIT_FOR_PID_TIME
20 while [ $i -gt 0 ]
22 i=`expr $i - 1`
23 echo -n " "
24 done
25 echo -n "|"
26 printf "\r"
27 dist_echo_stop_begin
29 # wait until pid-file exist or time's out
30 i=$AVG_WAIT_FOR_PID_TIME
31 while [ $i -gt 0 ]
33 if [ -f "$AVG_PID_FILE" ] ; then
34 echo -n "."
35 i=`expr $i - 1`
36 sleep 2s
37 else
38 i=0
40 done
42 # if avgd is still running, kill it and all his childern (ps ppid==pid avgd)
43 if [ -f "$AVG_PID_FILE" ] ; then
44 PID=`cat "${AVG_PID_FILE}"`
45 PIDS=`ps axopid,ppid,ruser | grep -w $PID | grep -w $SUSER | awk '{print $1}' | tr '\n' ' '`
46 if [ -n "$PIDS" ] ; then
47 printf "\r killing $AVG_DAEMON and his components with pids: ($PIDS)"
48 kill -KILL $PIDS
49 RETVAL=$?
50 [ $RETVAL -eq 0 ] && rm -f $AVG_PID_FILE
51 else
52 RETVAL=2
54 sleep 2s
56 return $RETVAL
60 # Return values are LSB-compliant.
61 # Should set the value of RETVAL to return value.
62 dist_status()
64 if [ -f "$AVG_PID_FILE" ] ; then
65 PID=`cat "${AVG_PID_FILE}"`
66 ISRUN=`ps axo pid,comm | grep -w $PID | grep -w ${AVG_DAEMON}`
67 if [ -n "$ISRUN" ] ; then
68 echo -n "(pid $PID) is running"
69 RETVAL=0
70 else
71 echo -n "$AVG_DAEMON dead but pidfile exists"
72 RETVAL=1
73 fi
74 elif [ -n "$AVG_LOCK_FILE" -a -f "$AVG_LOCK_FILE" ] ; then
75 echo -n "${AVG_DAEMON} dead but subsys locked"
76 RETVAL=2
77 else
78 echo -n "$AVG_DAEMON is not running"
79 RETVAL=3
81 return $RETVAL
85 # Should set the value of RETVAL to return value.
86 dist_reload()
88 #TODO: avgctl --reload
89 if [ -f "$AVG_PID_FILE" ] ; then
90 kill -HUP `cat "${AVG_PID_FILE}"`
91 RETVAL=0
92 else
93 RETVAL=7 # program is not running (LSB specification)
94 echo -n "service not running"
96 return $RETVAL
100 # Stops the daemon (and waits until his pidfile is removed).
101 # Should set the value of RETVAL to return value.
103 dist_stop()
105 if [ -f "$AVG_PID_FILE" ] ; then
106 $AVG_CTL --stop >/dev/null 2>&1
107 RETVAL=$?
108 if [ -f "$AVG_PID_FILE" ] ; then
109 PID=`cat $AVG_PID_FILE`
110 ISRUN=`ps axo pid,comm | grep -w $PID | grep -w ${AVG_DAEMON}`
111 [ -n "${ISRUN}" ] && wait_and_kill
112 RETVAL=$?
114 else
115 echo -n " (not running)"
116 RETVAL=0
119 return $RETVAL
123 # Should set the value of RETVAL to return value.
124 # Note: Used for FreeBSD currently.
126 dist_start()
128 # if daemon is not running, nothing should be in RUN_DIR
129 if [ -n "`ls $AVG_RUN_DIR`" ] ; then
130 # See if it's already running.
131 if [ -f "$AVG_PID_FILE" ] ; then
132 PID=`cat $AVG_PID_FILE`
133 ISRUN=`ps axo pid,comm | grep -w $PID | grep -w ${AVG_DAEMON}`
134 if [ -n "${ISRUN}" ] ; then
135 #avgd is running = do nothing
136 echo -n " (already running with pid: $PID)"
137 RETVAL=0
138 return $RETVAL
141 #avgd is dead, clean mess (old pid and pipe files), before run
142 echo -n " (recovering)"
143 rm -f $AVG_RUN_DIR/*
146 # make sure it doesn't core dump anywhere unless requested
147 ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0} >/dev/null 2>&1
149 # if they set NICELEVEL, honor it
150 [ -n "$NICELEVEL" ] && nice="nice -n $NICELEVEL"
152 # And start it up.
153 if [ "$SUSER" = "root" ] ; then
154 $nice "$AVG_CTL" --start >/dev/null 2>&1
155 else
156 $nice su -s /bin/sh -c "$AVG_CTL --start >/dev/null 2>&1" $SUSER
158 RETVAL=$?
159 return $RETVAL
164 ############################################################
165 # Functions for echoing
168 dist_echo_status_begin()
170 stat_busy "Checking for service ${AVG_DAEMON}: "
174 dist_echo_status_end()
176 stat_done
180 dist_echo_start_begin()
182 stat_busy "Starting $AVG_DAEMON"
186 dist_echo_start_end()
188 [ "$RETVAL" -eq 0 ] && stat_done || stat_fail
192 dist_echo_stop_begin()
194 stat_busy "Shutting down $AVG_DAEMON"
198 dist_echo_stop_end()
200 [ "$RETVAL" -eq 0 ] && stat_done || stat_fail
204 dist_echo_reload_begin()
206 stat_busy "Reloading virus database: "
210 dist_echo_reload_end()
212 [ "$RETVAL" -eq 0 ] && stat_done || stat_fail
215 # From avgd.all
217 if [ $(id -u) -ne 0 ] ; then
218 echo "ERROR: Try to run this as root."
219 # LSB specification errorcode
220 exit 4
223 # split deprecated variables in avgdinit.conf into new ones,
224 # used (and possibely reset in sourced functions.*) since v7.5
225 if [ -n "$AVG_LOCK_FILE" ] ; then
226 AVG_LOCK_FILE_NAME=${AVG_LOCK_FILE##*/}
227 AVG_LOCK_FILE_DIR=${AVG_LOCK_FILE%/*}
229 if [ -n "$AVG_CONF_FILE" ] ; then
230 AVG_CONF_FILE_NAME=${AVG_CONF_FILE##*/}
231 AVG_CONF_FILE_DIR=${AVG_CONF_FILE%/*}
234 # Source our distro-specific init functions and variables
235 # which can override common/default init functions.
236 # They can also source some init functions (mainly these
237 # used for echoing) provided by distro.
239 if [ -f ${AVG_SCRIPTS_DIR}/functions.${distro} ] ; then
240 . ${AVG_SCRIPTS_DIR}/functions.${distro}
243 # After sourcing distro-specific variables
244 # we can set some composite path variables
246 if [ -n "$AVG_LOCK_FILE_NAME" ] ; then
247 AVG_LOCK_FILE="${AVG_LOCK_FILE_DIR}/${AVG_LOCK_FILE_NAME}"
250 if [ -n "$AVG_CONF_FILE_NAME" ] ; then
251 AVG_CONF_FILE="${AVG_CONF_FILE_DIR}/${AVG_CONF_FILE_NAME}"
255 # Source configuration
257 if [ -f "/etc/sysconfig/${AVG_DAEMON}" ] ; then
258 . "/etc/sysconfig/${AVG_DAEMON}"
262 # Check that required config file exists.
264 if [ -n "$AVG_CONF_FILE" -a ! -f "$AVG_CONF_FILE" ] ; then
265 echo "ERROR: Could not find ${AVG_CONF_FILE}"
266 # LSB specification errorcode
267 if [ "$1" = "status" ] ; then
268 exit 150
269 else
270 exit 6
274 status()
276 dist_echo_status_begin
277 dist_status
278 dist_echo_status_end
279 return $RETVAL
283 start()
285 dist_echo_start_begin
286 dist_start
287 dist_echo_start_end
288 if [ -n "$AVG_LOCK_FILE" ] ; then
289 [ "$RETVAL" -eq 0 ] && touch "${AVG_LOCK_FILE}" || RETVAL=150
291 return $RETVAL
295 stop()
297 dist_echo_stop_begin
298 dist_stop
299 dist_echo_stop_end
300 if [ -n "$AVG_LOCK_FILE" ] ; then
301 [ "$RETVAL" -eq 0 ] && rm -f "${AVG_LOCK_FILE}" || RETVAL=150
303 return $RETVAL
307 reload()
309 dist_echo_reload_begin
310 dist_reload
311 dist_echo_reload_end
312 return $RETVAL
316 restart()
318 stop
319 start
323 case "$1" in
324 start)
325 start
327 stop)
328 stop
330 status)
331 status
333 restart)
334 restart
336 reload)
337 reload
339 condrestart)
340 # Restarts the servce iff it is already running
341 # (which is detected by presence of lockfile or status function).
342 if [ -n "$AVG_LOCK_FILE" ] ; then
343 [ -f "${AVG_LOCK_FILE}" ] && restart || :
344 else
345 dist_status > /dev/null && restart || :
349 echo "Usage: $0 {start|stop|status|reload|restart|condrestart}"
350 exit 1
351 esac
353 exit $?