aboutsummaryrefslogtreecommitdiff
path: root/utils/smstools3/files/smstools3.init
blob: 7c60ceaf8c2dd1d27cf935c04609965fae0ffd68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/sh /etc/rc.common
# Copyright (C) 2014 OpenWrt.org
# smsd initscript openwrt mod

START=99
STOP=99

EXTRA_COMMANDS="status"
EXTRA_HELP="        status  View pid and service status "

# Set USER and GROUP, if necessary:
USER=""
GROUP=""

# If an unpriviledged user is selected, make sure that next two
# files are writable by that user:
PIDFILE="/var/run/smsd.pid"
INFOFILE="/var/run/smsd.working"
# Logfile can also be defined in here:
LOGFILE="/var/log/smsd.log"

DAEMON=/usr/bin/smsd
# A program which turns power off for couple of seconds:
RESETMODEMS=/usr/bin/smsd_resetmodems
NAME=smsd
PSOPT=""

# Set/edit this before starting service !!!!!
WRT_SPOOL=/var/spool

# Maximum time to stop smsd, after that it gets killed hardly:
MAXWAIT=45

boot() {
	start
}

start() {
	test -x $DAEMON || exit 0

	echo "Creating minimum spool directories"
	mkdir -p $WRT_SPOOL
	mkdir -p $WRT_SPOOL/sms
	mkdir -p $WRT_SPOOL/sms/incoming
	mkdir -p $WRT_SPOOL/sms/outgoing
	mkdir -p $WRT_SPOOL/sms/checked
	mkdir -p $WRT_SPOOL/sms/failed
	mkdir -p $WRT_SPOOL/sms/sent

	echo -n "Starting SMS Daemon: "
	MSG="."
	ARGS="-n MAINPROCESS -p$PIDFILE -i$INFOFILE"
	[ "x$USER" != x ] && ARGS="$ARGS -u$USER"
	[ "x$GROUP" != x ] && ARGS="$ARGS -g$GROUP"
	[ "x$LOGFILE" != x ] && ARGS="$ARGS -l$LOGFILE"
	PID=`cat $PIDFILE 2>/dev/null`
	if [ "x$PID" != x ]; then
	  if kill -0 $PID 2>/dev/null; then
	    MSG=" already running ($PID)."
	  else
	    PID=""
	  fi
	fi
	if [ "x$PID" = x ]; then
	  if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
	    MSG=" already running."
	  else
	    $DAEMON $ARGS
	    sleep 1
	    PIDS=`ps $PSOPT | grep $NAME | grep -v grep`
	    [ "x$PIDS" = x ] && MSG=" failed."
	  fi
	fi
	echo "$NAME$MSG"
}

stop() {
	if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
	  PID=`cat $PIDFILE 2>/dev/null`
	  if [ "x$PID" != x ]; then
	    P=`kill -0 $PID 2>/dev/null`
	    [ "x$P" != x ] && PID=""
	  fi
	  if [ "x$PID" != x ]; then
	    kill $PID
	  else
	    kill `ps $PSOPT | grep $NAME | grep -v grep | awk '{print $1}'` >/dev/null 2>&1
	  fi
	  sleep 1
	  if ps $PSOPT | grep $NAME | grep -v grep >/dev/null; then
	    echo "Allowing $NAME to terminate gracefully within $MAXWAIT seconds"
	    infofound=0
	    dots=0
	    seconds=0
	    while ps $PSOPT | grep $NAME | grep -v grep >/dev/null; do
	      if [ $infofound -lt 1 ]; then
	        if [ -f $INFOFILE ]; then
	          infofound=1
	          if [ $dots -gt 0 ]; then
	            echo ""
	            dots=0
	          fi
	          $ECHO -n "$NAME is currently "
	          cat $INFOFILE
	          echo "Time counting is now disabled and we will wait until this job is complete."
	        fi
	      fi
	      [ $infofound -lt 1 ] && seconds=`expr $seconds + 1`
	      echo -n "."
	      dots=`expr $dots + 1`
	      if [ "$seconds" -ge $MAXWAIT ]; then
	        if [ $dots -gt 0 ]; then
	          echo ""
	          dots=0
	        fi
	        echo "Timeout occurred, killing $NAME hardly."
	        kill -9 `ps $PSOPT | grep $NAME | grep -v grep | awk '{print $1}'` >/dev/null 2>&1
	        [ -f $PIDFILE ] && rm $PIDFILE
	        seconds=0
	      fi
	      sleep 1
	    done
	    [ $dots -gt 0 ] && echo ""
	    #echo "$NAME is stopped."
	  fi
	fi
}

restart() {
	stop
	start
}


status() {
	PID=$(cat $PIDFILE)

	test -e $PIDFILE
	if [ $? == 0 ]; then
		echo $NAME " running! pid $PID"
	else
		echo $NAME " not running !!!"
	fi
}

reset() {
	$0 stop
	[ -f "$RESETMODEMS" ] && "$RESETMODEMS"
	sleep 30
	$0 start
}