Merge pull request #228 from micmac1/asterisk
[feed/telephony.git] / net / baresip / files / baresip.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2017 OpenWrt.org
3
4 START=92
5
6 USE_PROCD=1
7
8 #PROCD_DEBUG=1
9
10 DAEMON=baresip
11 DEFAULT=/etc/default/$DAEMON
12 LOGGER="/usr/bin/logger -p user.err -s -t $DAEMON"
13 OPTIONS=
14 PROG=/usr/bin/$DAEMON
15 TIMEOUT=30
16
17 [ -f $DEFAULT ] && . $DEFAULT
18
19 start_service() {
20 local dir=
21
22 if [ "$ENABLE_BARESIP" != yes ]; then
23 $LOGGER User configuration incomplete - not starting $DAEMON
24 $LOGGER Check ENABLE_BARESIP in $DEFAULT
25 exit 1
26 fi
27
28 procd_open_instance
29 procd_set_param command $PROG
30 procd_append_param command \
31 -f /etc/$DAEMON \
32 $OPTIONS
33 procd_set_param pidfile /var/run/${DAEMON}.pid
34 # forward stderr to logd
35 procd_set_param stderr 1
36 # forward stdout to logd
37 #procd_set_param stdout 1
38 procd_set_param user $DAEMON
39 procd_close_instance
40 }
41
42 stop_service() {
43 local retval=
44 local mypid=
45 local timeout=$TIMEOUT
46
47 pgrep $DAEMON &> /dev/null
48 [ $? -ne 0 ] && exit 0
49
50 [ -f /var/run/${DAEMON}.pid ]
51 retval=$?
52
53 # init script could find itself in a scenario where baresip was started
54 # very recently, so make it wait a while for a pid file to appear
55 while [ $retval -ne 0 -a $timeout -gt 0 ]; do
56 sleep 1
57 [ -f /var/run/${DAEMON}.pid ]
58 retval=$?
59 timeout=$(($timeout-1))
60 done
61
62 [ $retval -eq 0 ] || {
63 $LOGGER PID file does not exist
64 exit 1
65 }
66
67 mypid=$(cat /var/run/${DAEMON}.pid)
68
69 [ "$mypid" -gt 1 ] 2> /dev/null || {
70 $LOGGER PID file contains garbage
71 exit 1
72 }
73
74 timeout=$TIMEOUT
75 kill $mypid 2>/dev/null
76 pgrep $DAEMON | grep -w $mypid &>/dev/null
77 retval=$?
78
79 while [ $retval -eq 0 -a $timeout -gt 0 ]; do
80 sleep 10
81 pgrep $DAEMON | grep -w $mypid &>/dev/null
82 retval=$?
83 [ $retval -eq 0 ] && kill $mypid 2>/dev/null
84 timeout=$(($timeout-10))
85 done
86
87 [ $retval -ne 1 ] && {
88 $LOGGER Failed to stop $DAEMON
89 exit 1
90 }
91 }