mark pod.py as running on python2
[pingofdeath.git] / pod
1 #! /bin/bash
2 #include INTEL_LICENSE.txt
3 #
4 ########################################################################
5 #
6 # PlanetLab Ping Of Death
7 #
8 ########################################################################
9 #
10 # DESCRIPTION
11 #
12 # This shell script takes care of starting and stopping Planetlab
13 # Ping-Of-Death.
14 #
15 # The Planetlab POD is a Linux kernel patch that creates the feature
16 # of resetting and rebooting a computer when a particularily
17 # formatted ICMP "ping" commmand is received.
18 #
19 # A PlanetLab node's kernel gets a host, mask and hash value through
20 # sysctl's that are performed on the node.  The node then looks
21 # for an ICMP packet from a host matching the host/mask and, if the
22 # packet payload is the hash value, the node forces an immediate
23 # reboot.
24 #
25 # HISTORY
26 #
27 # May 17, 2003    -   Paul Brett <paul.brett@intel.com>
28 #                     Initial version based on the work of 
29 #                     Robert Adams <robert.adams@intel.com> and EMULAB
30 #
31 # chkconfig: - 11 99
32 # description: enable Ping of Death
33
34 # Source function library.
35 . /etc/init.d/functions
36
37 # Source networking configuration.
38 . /etc/sysconfig/network
39
40 function enable_pod()
41 {
42     local SYSCTL=/sbin/sysctl
43
44     local IP_SUBNET=$1
45     local IP_MASK=$2
46     local HASH=$3
47
48     # Grotesque sed/awk converts IP addrs into an integer for sysctl
49     local IPODHOST=`echo $IP_SUBNET | \
50               sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1 \2 \3 \4/' | \
51               awk '{ printf "%d\n", $1*16777216+$2*65536+$3*256+$4 }'`
52     local IPODMASK=`echo $IP_MASK | \
53               sed -e 's/\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\)\.\([0-9]*\).*/\1 \2 \3 \4/' | \
54               awk '{ printf "%d\n", $1*16777216+$2*65536+$3*256+$4 }'`
55
56     # figure out the version
57     local version=`$SYSCTL net.ipv4.icmp_ipod_version 2>/dev/null`
58     if [[ "$version" == "" ]]
59     then
60         $SYSCTL net.ipv4.icmp_ipod_enabled >/dev/null 2>&1
61         case $? in
62             0)
63                 version=1
64                 ;;
65             *)
66                 version=0
67                 ;;
68         esac
69     fi
70
71     # enable if possible
72     case $version in
73         0)
74             return 1
75             ;;
76         1)
77             $SYSCTL -w net.ipv4.icmp_ipod_host=$IPODHOST >/dev/null
78             $SYSCTL -w net.ipv4.icmp_ipod_enabled=1 >/dev/null
79             Success=0
80             ;;
81         *)
82             $SYSCTL -w net.ipv4.icmp_ipod_host=$IPODHOST >/dev/null
83             Success=$?
84             $SYSCTL -w net.ipv4.icmp_ipod_mask=$IPODMASK >/dev/null
85             $SYSCTL -w net.ipv4.icmp_ipod_key=$HASH >/dev/null
86             $SYSCTL -w net.ipv4.icmp_ipod_enabled=1 >/dev/null
87         ;;
88     esac
89     return $Success
90 }
91
92 function disable_pod()
93 {
94     local SYSCTL=/sbin/sysctl
95     $SYSCTL -w net.ipv4.icmp_ipod_enabled=0 >/dev/null
96     return 0
97 }
98
99 function status_pod()
100 {
101
102     local SYSCTL="/sbin/sysctl"
103
104     # Check that IPOD is built into this kernel
105     local version
106     version=`$SYSCTL -n net.ipv4.icmp_ipod_version 2> /dev/null`
107     if [[ $? -ne 0 ]]
108     then
109         echo "Not installed"
110         return 255
111     fi
112
113     # Check if it has been enabled
114     local enabled=`$SYSCTL -n net.ipv4.icmp_ipod_enabled`
115     echo -n "version $version "
116     if [[ $enabled -eq 1 ]]
117     then 
118         echo "Enabled"
119     else 
120         echo "Disabled"
121     fi
122     return $enabled
123 }
124
125 echo -n "PlanetLab Ping Of Death "
126
127 CONF=/etc/ipod.conf
128
129 if [[ -r $CONF ]]
130 then
131     # load the POD configuration file
132     . $CONF
133 else
134     # no POD configuration file - failed
135     echo
136     action $"Missing configuration file $CONF" /bin/false
137     exit 1
138 fi
139
140 SESSION=/etc/planetlab/session
141
142 if [[ -r $SESSION ]]
143 then
144     # load the session value file
145     HASH=`cat $SESSION`
146 else
147     # no session value (ie, no hash)
148     echo
149     action $"Missing node session file $SESSION" /bin/false
150     exit 1
151 fi
152
153 # Check that networking is up.
154 if [[ ${NETWORKING} = "no" ]]
155 then
156     action $"" /bin/false
157     exit 1
158 fi
159
160 # Check the POD parameters
161 if [[ "$HASH" == "" ]]
162 then
163     echo 
164     action $"Missing HASH (from $SESSION)" /bin/false
165     exit 1
166 fi
167
168 if [[ "$IP_SUBNET" == "" ]]
169 then
170     echo
171     action $"Missing IP_SUBNET in $CONF" /bin/false
172     exit 1
173 fi
174
175 if [[ "$IP_MASK" == "" ]]
176 then
177     echo
178     action $"Missing IP_MASK in $CONF" /bin/false
179     exit 1
180 fi
181
182 # See how we were called.
183 case "$1" in
184   start)
185         enable_pod $IP_SUBNET $IP_MASK $HASH
186         RETVAL=$?
187         if [[ $RETVAL -eq 0 ]]
188         then
189             action $"" /bin/true
190         else
191             action $"" /bin/false
192         fi
193         ;;
194   stop)
195         disable_pod
196         RETVAL=$?
197         if [[ $RETVAL -eq 0 ]]
198         then
199             action $"" /bin/true
200         else
201             action $"" /bin/false
202         fi
203         ;;
204   status)
205         status_pod
206         RETVAL=$?
207         ;;
208   restart|reload)
209         disable_pod
210         enable_pod
211         RETVAL=$?
212         ;;
213   *)
214         echo $"Usage: $0 {start|stop|restart|status}"
215         exit 1
216 esac