1#!/bin/sh
2#
3# aprsc		Start or stop the aprsc server
4#
5# chkconfig: 2345 55 25
6# description: Provide an APRS-IS server for the amateur radio APRS network
7#
8# processname: aprsc
9# config: /opt/aprsc/etc/aprsc.conf
10# pidfile: /opt/aprsc/logs/aprsc.pid
11
12### BEGIN INIT INFO
13# Provides:       aprsc
14# Required-Start: $syslog $network $local_fs
15# Required-Stop:  $syslog $local_fs
16# Default-Start:  2 3 4 5
17# Default-Stop:   0 1 6
18# Short-Description: start and stop aprsc
19# Description: aprsc provides an APRS-IS server for the amateur radio APRS network
20### END INIT INFO
21
22
23# source function library
24. /etc/rc.d/init.d/functions
25
26# pull in sysconfig settings
27[ -f /etc/sysconfig/aprsc ] && . /etc/sysconfig/aprsc
28
29RETVAL=0
30NAME="aprsc"
31prog="aprsc"
32lockfile=/var/lock/subsys/$prog
33
34DIRNAME=aprsc
35BASEDIR=/opt/$DIRNAME
36APRSC=$BASEDIR/sbin/aprsc
37PID_FILE=$BASEDIR/logs/aprsc.pid
38
39if [ "$STARTAPRSC" != "yes" ];then
40	echo "Starting of $NAME not enabled in /etc/sysconfig/$NAME."
41	exit 0
42fi
43
44# copy files required for chrooted operation
45prepare_chroot () {
46	echo "Preparing chroot for aprsc..."
47	/bin/cp -p /etc/resolv.conf /etc/nsswitch.conf /etc/hosts /etc/gai.conf $BASEDIR/etc/
48	# live upgrade requires libraries to be visible within chroot, so
49	# set up a read-only bind mount of /lib
50	grep -q "$DIRNAME/lib " /proc/mounts || \
51		( mount --bind /lib $BASEDIR/lib \
52		&& mount -o remount,ro,bind $BASEDIR/lib )
53	if [ -e /lib64 ]; then
54		grep -q "$DIRNAME/lib64 " /proc/mounts || \
55			( mount --bind /lib64 $BASEDIR/lib64 \
56			&& mount -o remount,ro,bind $BASEDIR/lib64 )
57	fi
58	grep -q "$DIRNAME/usr/lib " /proc/mounts || \
59		( mount --bind /usr/lib $BASEDIR/usr/lib \
60		&& mount -o remount,ro,bind $BASEDIR/usr/lib )
61	if [ -e /usr/lib64 ]; then
62		grep -q "$DIRNAME/usr/lib64 " /proc/mounts || \
63			( mount --bind /usr/lib64 $BASEDIR/usr/lib64 \
64			&& mount -o remount,ro,bind $BASEDIR/usr/lib64 )
65	fi
66}
67
68check_configuration() {
69  	echo "Testing aprsc configuration..."
70	if ! $APRSC $DAEMON_OPTS -y > /dev/null 2>&1; then
71		$APRSC $DAEMON_OPTS -y || true
72		exit 1
73	fi
74}
75
76start()
77{
78	[ -x $APRSC ] || exit 5
79	[ -f /opt/aprsc/etc/aprsc.conf ] || exit 6
80	prepare_chroot
81	ulimit -c unlimited
82
83	echo -n $"Starting $prog: "
84	$APRSC $DAEMON_OPTS && success || failure
85	RETVAL=$?
86	[ $RETVAL -eq 0 ] && touch $lockfile
87	echo
88	return $RETVAL
89}
90
91stop()
92{
93	echo -n $"Stopping $prog: "
94	if [ -f $PID_FILE ] ; then
95	    killproc -p $PID_FILE $APRSC
96	else
97	    failure $"Stopping $prog"
98	fi
99	RETVAL=$?
100	[ $RETVAL -eq 0 ] && rm -f $lockfile
101	echo
102}
103
104reload()
105{
106	check_configuration
107	echo -n $"Reloading $prog: "
108	if [ -f $PID_FILE ] ; then
109	    killproc -p $PID_FILE $APRSC -USR1
110	else
111	    failure $"Reloading $prog"
112	fi
113	RETVAL=$?
114	echo
115}
116
117liveupgrade()
118{
119	check_configuration
120	prepare_chroot
121	echo -n $"Performing live upgrade of $prog: "
122	if [ -f $PID_FILE ] ; then
123	    killproc -p $PID_FILE $APRSC -USR2
124	else
125	    failure $"Performing live upgrade of $prog"
126	fi
127	RETVAL=$?
128	echo
129}
130
131restart() {
132	check_configuration
133	stop
134	start
135}
136
137force_reload() {
138	restart
139}
140
141rh_status() {
142	status -p $PID_FILE aprsc
143}
144
145rh_status_q() {
146	rh_status >/dev/null 2>&1
147}
148
149case "$1" in
150	start)
151		rh_status_q && exit 0
152		start
153		;;
154	stop)
155		if ! rh_status_q; then
156			rm -f $lockfile
157			exit 0
158		fi
159		stop
160		;;
161	restart)
162		restart
163		;;
164	reload)
165		rh_status_q || exit 7
166		reload
167		;;
168	liveupgrade)
169		rh_status_q || exit 7
170		liveupgrade
171		;;
172	force-reload)
173		force_reload
174		;;
175	condrestart|try-restart)
176		rh_status_q || exit 0
177		if [ -f $lockfile ] ; then
178			if [ $RETVAL -eq 0 ] ; then
179				stop
180				# avoid race
181				sleep 3
182				start
183			else
184				RETVAL=6
185			fi
186		fi
187		;;
188	status)
189		rh_status
190		RETVAL=$?
191		if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
192			RETVAL=2
193		fi
194		;;
195	*)
196		echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|liveupgrade|status}"
197		RETVAL=2
198esac
199exit $RETVAL
200