| #!/bin/sh |
| # |
| # Run dhclient, the system DHCP client, with the appropriate parameters for |
| # SpaceCast. |
| iface=$1 |
| |
| lock=/var/run/dhclient.$iface.lock |
| lockfile-create --use-pid $lock |
| |
| kill_dhclient() { |
| for i in $(seq 1 10); do |
| # Send TERM to all matching processes. |
| pkill -f "$1" |
| # Break if all matching processes are killed. |
| pgrep -f "$1" || break |
| usleep 500000 |
| done |
| |
| # Send KILL to all lingering matching processes. |
| pkill -9 -f "$1" |
| } |
| |
| run_dhclient() { |
| local suffix="$1" xargs="$2" |
| local pidfile="/var/run/dhclient$suffix.$iface.pid" |
| local leasefile="/var/run/dhclient$suffix.$iface.lease" |
| local conffile="/etc/dhclient$suffix.conf" |
| |
| if [ -e "$pidfile" ]; then |
| # kill sends TERM which should make the babysitter exit gracefully. |
| kill $(cat "$pidfile") >/dev/null 2>&1 |
| rm -f "$pidfile" |
| fi |
| babysit 60 dhclient "$iface" $xargs \ |
| -d \ |
| -pf "$pidfile" \ |
| -lf "$leasefile" \ |
| -cf "$conffile" \ |
| 2>&1 | logos "dhclient$suffix.$iface" & |
| } |
| |
| # Kill all dhclient and babysit processes for $iface. |
| kill_dhclient "dhclient $iface -d" |
| |
| run_dhclient "" "" |
| |
| # -N -P means to request a delegated prefix AND get a address. |
| # -S means runs stateless config, requests only the extra information. |
| # On the network box we only run the stateful version, running both |
| # interfere with each other. |
| # TODO(erdi): Disable IPv6 until it is fully supported in App. |
| #run_dhclient "6" "-N -P -6 --never-gonna-give-you-up 900" |
| |
| lockfile-remove $lock |