Debian Codegraph @

netscript-hotplug

raw
1 #!/bin/sh -e
2 #
3 # run /usr/sbin/netscript for hotplug operations
4 #
5 # Taken from ifupdown package
6
7 PATH='/sbin:/bin:/usr/sbin:/usr/bin'
8
9 if [ -x /usr/bin/logger ]; then
10 LOGGER=/usr/bin/logger
11 elif [ -x /bin/logger ]; then
12 LOGGER=/bin/logger
13 else
14 unset LOGGER
15 fi
16
17 # for diagnostics
18 if [ -t 1 -a -z "$LOGGER" ] || [ ! -e '/dev/log' ]; then
19 mesg() {
20 echo "$@" >&2
21 }
22 elif [ -t 1 ]; then
23 mesg() {
24 echo "$@"
25 $LOGGER -t "${0##*/}[$$]" "$@"
26 }
27 else
28 mesg() {
29 $LOGGER -t "${0##*/}[$$]" "$@"
30 }
31 fi
32
33 if [ -z "$INTERFACE" ]; then
34 mesg "Bad netscript udev helper invocation: \$INTERFACE is not set"
35 exit 1
36 fi
37
38 check_program() {
39 [ -x $1 ] && return 0
40
41 mesg "ERROR: $1 not found. You need to install the netscript-2.4 package."
42 mesg "netscript udev helper $ACTION event for $INTERFACE not handled."
43 exit 1
44 }
45
46 wait_for_interface() {
47 local interface=$1
48 local state
49
50 while :; do
51 read state /sys/class/net/$interface/operstate 2>/dev/null || true
52 if [ "$state" != down ]; then
53 return 0
54 fi
55 sleep 1
56 done
57 }
58
59 net_ifup() {
60 check_program /usr/sbin/netscript
61
62 # Don't bring up bridge interfaces under hotplug! Can cause issues with openstack
63 local BRG_DEVLIST=`brctl show | sed -e '1d' | grep '^[-a-zA-Z0-9_]' | sed -e 's/^\([a-zA-Z0-9_\-]\+\)[ ].*$/\1/'`
64 if echo "$BRG_DEVLIST" | fgrep "$INTERFACE"; then
65 exit 0
66 fi
67
68 if [ -d /run/systemd/system ]; then
69 exec systemctl --no-block start $(systemd-escape --template [email protected] $INTERFACE)
70 fi
71
72 #local out=$(ps -C ifup ho args)
73 #if [ "${out%$INTERFACE*}" != "$out" ]; then
74 # mesg "Already ifup-ing interface $INTERFACE"
75 # exit 0
76 #fi
77
78 #wait_for_interface lo
79
80 exec netscript ifup $INTERFACE
81 }
82
83 net_ifdown() {
84 check_program /usr/sbin/netscript
85
86 # Don't bring down bridge interfaces under hotplug! Can cause issues with openstack
87 local BRG_DEVLIST=`brctl show | sed -e '1d' | grep '^[-a-zA-Z0-9_]' | sed -e 's/^\([a-zA-Z0-9_\-]\+\)[ ].*$/\1/'`
88 if echo "$BRG_DEVLIST" | fgrep "$INTERFACE"; then
89 exit 0
90 fi
91
92 # systemd will automatically ifdown the interface on device
93 # removal by binding the instanced service to the network device
94 if [ -d /run/systemd/system ]; then
95 exit 0
96 fi
97
98 #local out=$(ps -C ifdown ho args)
99 #if [ "${out%$INTERFACE*}" != "$out" ]; then
100 # mesg "Already ifdown-ing interface $INTERFACE"
101 # exit 0
102 #fi
103
104 exec netscript ifdown $INTERFACE
105 }
106
107 do_everything() {
108
109 case "$ACTION" in
110 add)
111 # these interfaces generate hotplug events *after* they are brought up
112 case $INTERFACE in
113 ppp*|ippp*|isdn*|plip*|lo|irda*|ipsec*)
114 exit 0 ;;
115 esac
116
117 net_ifup
118 ;;
119
120 remove)
121 # the pppd persist option may have been used, so it should not be killed
122 #case $INTERFACE in
123 # ppp*)
124 # exit 0 ;;
125 #esac
126
127 net_ifdown
128 ;;
129
130 *)
131 mesg "NET $ACTION event not supported"
132 exit 1
133 ;;
134 esac
135
136 }
137 # under systemd we don't do synchronous operations, so we can run in the
138 # foreground; we also need to, as forked children get killed right away under
139 # systemd
140 if [ -d /run/systemd/system ]; then
141 do_everything
142 else
143 # under sysvinit/upstart we need to fork as we start the long-running
144 # "ifup". but there, forked processes won't get killed.
145 # When udev_log="debug" stdout and stderr are pipes connected to udevd.
146 # They need to be closed or udevd will wait for this process which will
147 # deadlock with udevsettle until the timeout.
148 exec > /dev/null 2> /dev/null
149 do_everything &
150 fi