f4275bc9579161005bf488829d61d5606dadc0b4
[external/udev.git] / extra / net.agent
1 #!/bin/sh -e
2 #
3 # run /sbin/{ifup,ifdown} with the --allow=hotplug option.
4 #
5
6 . /lib/udev/hotplug.functions
7
8 if [ -z "$INTERFACE" ]; then
9     mesg "Bad net.agent invocation: \$INTERFACE is not set"
10     exit 1
11 fi
12
13 check_program() {
14     [ -x $1 ] && return 0
15
16     mesg "ERROR: $1 not found. You need to install the ifupdown package."
17     mesg "net.agent $ACTION event for $INTERFACE not handled."
18     exit 1
19 }
20
21 wait_for_interface() {
22     local interface=$1
23
24     while :; do
25         local state="$(cat /sys/class/net/$interface/operstate 2>/dev/null || true)"
26         if [ "$state" != down ]; then
27                 return 0
28         fi
29         sleep 1
30     done
31 }
32
33 net_ifup() {
34     check_program /sbin/ifup
35
36     if grep -q '^auto[[:space:]].*\<'"$INTERFACE"'\>' \
37             /etc/network/interfaces; then
38         # this $INTERFACE is marked as "auto"
39         IFUPARG='\('$INTERFACE'\|-a\|--all\)'
40     else
41         IFUPARG=$INTERFACE
42     fi
43
44     if ps -C ifup ho args | grep -q "$IFUPARG"; then
45         debug_mesg "Already ifup-ing interface $INTERFACE"
46         exit 0
47     fi
48
49     wait_for_interface lo
50     if [ -e /bin/systemctl ]; then
51       wait_for_file /dev/log
52     fi
53
54     exec ifup --allow=hotplug $INTERFACE
55 }
56
57 net_ifdown() {
58     check_program /sbin/ifdown
59
60     if ps -C ifdown ho args | grep -q $INTERFACE; then
61         debug_mesg "Already ifdown-ing interface $INTERFACE"
62         exit 0
63     fi
64
65     exec ifdown --allow=hotplug $INTERFACE
66 }
67
68 do_everything() {
69
70 case "$ACTION" in
71     add)
72     # these interfaces generate hotplug events *after* they are brought up
73     case $INTERFACE in
74         ppp*|ippp*|isdn*|plip*|lo|irda*|ipsec*)
75         exit 0 ;;
76     esac
77
78     net_ifup
79     ;;
80
81     remove)
82     # the pppd persist option may have been used, so it should not be killed
83     case $INTERFACE in
84         ppp*)
85         exit 0 ;;
86     esac
87
88     net_ifdown
89     ;;
90
91     *)
92     debug_mesg "NET $ACTION event not supported"
93     exit 1
94     ;;
95 esac
96
97 }
98
99 # When udev_log="debug" stdout and stderr are pipes connected to udevd.
100 # They need to be closed or udevd will wait for this process which will
101 # deadlock with udevsettle until the timeout.
102 do_everything > /dev/null 2> /dev/null &
103
104 exit 0
105