File: init.d

package info (click to toggle)
smstools 3.1.21-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,500 kB
  • sloc: ansic: 18,624; sh: 1,196; php: 115; makefile: 42; awk: 17
file content (236 lines) | stat: -rw-r--r-- 4,282 bytes parent folder | download | duplicates (3)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#! /bin/sh
#
# smstools	Startup script for the SMS Server Tools
#
# Based on upstream sms3 script
# Modified for Debian by Patrick Schoenfeld <schoenfeld@debian.org>

### BEGIN INIT INFO
# Provides:          smstools
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts smstools
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/smsd
DEFAULT=/etc/default/smstools
NAME=smsd
PACKAGE=smstools
DESC='SMS Daemon'

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

if [ ! -f /etc/default/$PACKAGE ]
then
	exit 1
else
	. /etc/default/smstools
fi

start () {
	# Recreate /var/run/smstools if it went missing
	rundir="/var/run/smstools"
	if [ ! -d $rundir ]; then
	    # Create directory
	    mkdir -p $rundir

	    # Set permissions
	    if ! dpkg-statoverride --list $rundir >/dev/null 2>&1 ; then
		dpkg-statoverride --update --add smsd smsd 2755 $rundir
	    else
		# Get user/group/perms from dpkg-statoverride
		# We go through this torture, because the user should be able
		# to override the permissions for /v/r/smstools via
		# dpkg-statoverride
		D_USER="`dpkg-statoverride --list $rundir|cut -d' ' -f1`"
		D_GROUP="`dpkg-statoverride --list $rundir|cut -d' ' -f2`"
		PERMS="`dpkg-statoverride --list $rundir|cut -d' ' -f3`"

		chown ${D_USER}:${D_GROUP} $rundir
		chmod ${PERMS} $rundir
	    fi
	fi

	if ! ps -C smsd > /dev/null 2>&1 ; then
		# Delete infofile if it exists
		if [ -f $INFOFILE ]; then
			rm $INFOFILE
		fi

		if [ -f $PIDFILE ]; then
			rm $PIDFILE
		fi

		# Delete lock files if they exist
		find /var/spool/sms -name '*.LOCK' -exec rm \{\} \;
	fi

	# Start the daemon
	ARGS="-p$PIDFILE -i$INFOFILE -u$USER -g$GROUP"
	if start-stop-daemon -q --start --background -p $PIDFILE --exec $DAEMON -- $ARGS ; then
		echo "$NAME."
	else
		echo "$NAME already running."
	fi

	sleep 1
}

forcestop ()
{
	if [ -f $PIDFILE ]; then
                PID=`cat $PIDFILE 2>/dev/null`
        fi

        if ! kill -0 $PID 2>/dev/null 2>/dev/null; then
                echo "$NAME not running."
        else
		kill -9 $PID
		if [ -f $PIDFILE ]; then
			rm $PIDFILE
		fi

		if kill -0 $PID 2>/dev/null 2>/dev/null; then
			echo "Failed."
		else
			echo "$NAME."
		fi
	fi
}

status()
{
	if [ ! -f $PIDFILE ]; then
		return 1;
	fi

    start-stop-daemon --start --quiet -p $PIDFILE --exec $DAEMON --test > /dev/null
    if [ "$?" = '0' ]; then
		return 1    # Daemon is not running
    else
        return 0    # Daemon is running
    fi
}

stop () {

	restartmode="0"

	if [ "$1" = 'restart' ]; then
		restartmode=1
	fi

	if [ -f $PIDFILE ]; then
		PID=`cat $PIDFILE 2>/dev/null`
	fi

	if ! kill -0 $PID 2>/dev/null 2>/dev/null; then
		echo "$NAME not running."

		if [ "$restartmode" -lt 1 ]
		then
			return 0
		fi
	fi

	infofound=0
	maxwait=15

	start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON

	#
	#	Now we have to wait until smsd has _really_ stopped
	#
	sleep 1

	if test -n "$PID" && kill -0 $PID 2>/dev/null
	then
		echo -n "(waiting..."

		seconds=0
		while kill -0 $PID 2>/dev/null
		do
			if [ $infofound -lt 1 ]; then
				if [ -f $INFOFILE ]; then
					infofound=1
				fi
			fi

			if [ $infofound -lt 1 ]; then
				seconds=`expr $seconds + 1`
			fi

			if [ $seconds -ge $maxwait ]; then
				echo -n "failed)"
				echo -n "Timeout occured, killing smsd hardly."

				kill -9 $PID
				if [ -f $PIDFILE ]; then
					rm $PIDFILE
				fi

				echo ""
				exit 0
			fi

			sleep 1
		done

		echo -n "done)"
	fi

	if [ "$restartmode" -lt 1 ]; then
		echo "$NAME."
	fi
}

case "$1" in
	start)
		echo -n "Starting $DESC: "
		start
	;;

	stop)
		echo -n "Stopping $DESC: "
		stop
	;;

	status)
		echo -n "Status of $DESC: "
		status
		case "$?" in
        	0)
                echo "$NAME is running."
                ;;
            1)
                echo "$NAME is not running."
				;;
		esac
	;;
	force-stop)
		echo -n "Forcing stop of $DESC: "
		force-stop
		echo "$NAME."

	;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		stop restart
		start
	;;

	*)
		echo "Usage: /etc/init.d/$NAME {start|stop|force-stop|force-reload|restart|status}"
		exit 3
	;;
esac

exit 0