blob: 9860c65aa20393ba015ef403ed48e69a269c66d1 [file] [log] [blame]
Andrew Geissler1fe918a2020-05-15 14:16:47 -05001#!/bin/sh
2
3PATH=/sbin:/bin:/usr/sbin:/usr/bin
4NAME=arpwatch
5DAEMON=/usr/sbin/$NAME
6DESC="Ethernet/FDDI station monitor daemon"
7DATADIR=/var/lib/$NAME
8RETVAL=0
9
10. /etc/init.d/functions
11
12### You shouldn't touch anything below unless you know what you are doing.
13
14[ -f /etc/default/arpwatch ] && . /etc/default/arpwatch
15
16# Decide whether we have to deal with multiple interfaces.
17CONF=/etc/arpwatch.conf
18MULTIPLE=0
19if [ -r $CONF ]; then
20 grep -c '^[a-z]' $CONF 2>&1 >/dev/null && MULTIPLE=1
21fi
22
23# Check whether we have to drop privileges.
24if [ -n "$RUNAS" ]; then
25 if getent passwd "$RUNAS" >/dev/null; then
26 ARGS="-u ${RUNAS} $ARGS"
27 else
28 RUNAS=""
29 fi
30fi
31
32start_instance () {
33 IFACE=$1
34 INSTANCE=${NAME}-${IFACE}
35 DATAFILE=$DATADIR/${IFACE}.dat
36 IFACE_OPTS="-P /var/run/${INSTANCE}.pid -i ${IFACE} -f ${DATAFILE} $2"
37
38 echo -n "Starting $DESC: "
39 if [ ! -f $DATAFILE ]; then
40 echo -n "(creating $DATAFILE) " :> $DATAFILE
41 fi
42 if [ -n "$RUNAS" ]; then
43 echo -n "(chown $RUNAS $DATAFILE) "
44 chown $RUNAS $DATAFILE
45 fi
46 start-stop-daemon --start --quiet \
47 --pidfile /var/run/${INSTANCE}.pid \
48 --exec $DAEMON -- $IFACE_OPTS $ARGS
49 echo "${INSTANCE}."
50 ps h -C $NAME -o pid,args | \
51 awk "/$IFACE/ { print \$1 }" > /var/run/${INSTANCE}.pid
52}
53
54stop_instance () {
55 IFACE=$1
56 INSTANCE=${NAME}-${IFACE}
57 [ -f /var/run/${INSTANCE}.pid ] || return 0
58 echo -n "Stopping $DESC: "
59 start-stop-daemon --stop --quiet --oknodo \
60 --pidfile /var/run/${INSTANCE}.pid
61 echo "${INSTANCE}."
62 rm -f /var/run/${INSTANCE}.pid
63}
64
65process_loop_break_line () {
66 __IFACE=$1
67 shift
68 __IOPTS="$@"
69}
70
71process_loop () {
72 OPERATION=$1
73 grep '^[a-z]' $CONF 2>/dev/null | \
74 while read LINE
75 do
76 process_loop_break_line $LINE
77 I=$__IFACE
78 I_OPTS="$__IOPTS"
79 $OPERATION $I "$I_OPTS"
80 done
81}
82
83startup () {
84 process_loop start_instance
85}
86
87shutdown () {
88 process_loop stop_instance
89}
90
91case "$1" in
92 start)
93 startup
94 ;;
95 stop)
96 shutdown
97 ;;
98 reload)
99 echo "Reload operation not supported -- use restart."
100 RETVAL=2
101 ;;
102 restart|force-reload)
103 #
104 # If the "reload" option is implemented, move the "force-reload"
105 # option to the "reload" entry above. If not, "force-reload" is
106 # just the same as "restart".
107 #
108 shutdown
109 sleep 1
110 startup
111 ;;
112 status)
113 status_of_proc $DAEMON $NAME
114 ;;
115 *)
116 N=/etc/init.d/$NAME
117 # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
118 echo "Usage: $N {start|stop|restart|force-reload}" >&2
119 RETVAL=2
120 ;;
121esac
122
123exit $RETVAL