aboutsummaryrefslogtreecommitdiffstats
path: root/mageia/etc/rc.d/init.d/network-up
blob: c5c6c0efc6811ad6c57cebdf55383df177129d34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#! /bin/bash
#
### BEGIN INIT INFO
# Provides: $network $named
# Should-Start: portreserve NetworkManager
# Default-Start: 2 3 4 5
# Short-Description: Wait for the hotplugged network to be up
# Description: Wait for all network interfaces started asynchronously
#              at boot time.
### END INIT INFO

# Source function library.
. /etc/init.d/functions

NETWORKDELAY=20
DEFAULT_LINK_DETECTION_DELAY=2
MIN_LINK_DETECTION_DELAY=0
MAX_LINK_DETECTION_DELAY=$MIN_LINK_DETECTION_DELAY
ELAPSED_TIME=0
RESOLVCONF_FLAGFILE=/run/resolvconf/enable-updates
RESOLVCONF_DIR=/run/resolvconf/interface

# source network configuration
. /etc/sysconfig/network

# Check that networking is up.
is_false "${NETWORKING}" && exit 0
cd /etc/sysconfig/network-scripts

. network-functions

# find all the interfaces besides loopback.
# ignore aliases, alternative configurations, and editor backup files
interfaces=$(/bin/ls ifcfg* | \
	    LC_ALL=C sed -e "$__sed_discard_ignored_files" \
		       -e '/\(ifcfg-lo$\|:\|ifcfg-.*-range\)/d' \
		       -e '/ifcfg-[ A-Za-z0-9#\._-]\+$/ { s/^ifcfg-//g;s/[0-9]/ &/}' | \
	    LC_ALL=C grep -v '^ifcfg-' | \
	    LC_ALL=C sort -k 1,1 -k 2n | \
	    LC_ALL=C sed -e 's/ \([0-9]\)/\1/')

function may_have_link() {
    local DEVICE=$1
    local LINKDELAY=0
    ! check_link_down "${DEVICE}" || is_associating "${DEVICE}"
}

function is_associating() {
    local DEVICE=$1
    is_wireless_device "${DEVICE}" || return 1
    local AP=$(iwgetid -a -r "${DEVICE}" 2>/dev/null)
    [ -n "$AP" ] && [ "$AP" != "00:00:00:00:00:00" ] && [ "$AP" != "44:44:44:44:44:44" ] && [ "$AP" != "FF:FF:FF:FF:FF:FF" ]
}

function should_wait_network() {
    for i in $interfaces; do
        unset DEVICE TYPE BOOTPROTO MII_NOT_SUPPORTED PEERDNS DNS1 DNS2
	unset REALDEVICE PARENTDEVICE NM_CONTROLLED
        LINK_DETECTION_DELAY=$DEFAULT_LINK_DETECTION_DELAY
        eval $(LANG=C grep -F "DEVICE=" "ifcfg-$i")
        eval $(LANG=C grep -F "REALDEVICE=" "ifcfg-$i")
        eval $(LANG=C grep -F "PARENTDEVICE=" "ifcfg-$i")
        eval $(LANG=C grep -F "TYPE=" "ifcfg-$i")
        eval $(LANG=C grep -F "BOOTPROTO=" "ifcfg-$i")
        eval $(LANG=C grep -F "MII_NOT_SUPPORTED=" "ifcfg-$i")
        eval $(LANG=C grep -F "LINK_DETECTION_DELAY=" "ifcfg-$i")
        eval $(LANG=C grep -F "PEERDNS=" "ifcfg-$i")
        eval $(LANG=C grep -F "DNS1=" "ifcfg-$i")
        eval $(LANG=C grep -F "DNS2=" "ifcfg-$i")
        eval $(LANG=C grep -F "NM_CONTROLLED=" "ifcfg-$i")
	[ -z "$REALDEVICE" ] && [ -n "$PARENTDEVICE" ] && REALDEVICE=$PARENTDEVICE
	[ -z "$REALDEVICE" ] && REALDEVICE=${DEVICE%%:*}
        if [ $LINK_DETECTION_DELAY -lt $MIN_LINK_DETECTION_DELAY ]; then
            LINK_DETECTION_DELAY=$MIN_LINK_DETECTION_DELAY
        fi

        if [ -z "$DEVICE" ] ; then DEVICE="$i"; fi
        if [ "$BOOTPROTO" != "static" ] \
            && [ "$BOOTPROTO" != "dhcp" ] \
            && [ "$BOOTPROTO" != "bootp" ]; then
            continue
        fi

        # Ignore Wifi network files created by NetworkManager
        if [ "$TYPE" = "Wireless" ]; then
          continue
        fi

        # only check interfaces using ifplug, other interfaces are
        # started synchronously from the network service
        if is_true "$MII_NOT_SUPPORTED"; then
            continue
        fi

        # only check interfaces automatically launched
        if LANG=C grep -q -E "^ONBOOT=['\"]?[Nn][Oo]['\"]?" "ifcfg-$i"; then
            continue
        fi

        # ignore devices that are not present
        ip -o link show "${DEVICE}" &>/dev/null || continue

        ! is_false "$NM_CONTROLLED" && is_nm_running && _use_nm=true
        # for NM controlled just ask NetworkManager
        if [ "$_use_nm" = "true" ]; then
            # Ignore disabled wifi h/w
            if is_nm_device_unavailable "${DEVICE}"; then
                # Question: Is NM cleverer than us here? Does it do this delay
                # internally and mark it as disconnected until it knows better?

                # before configured delay, consider a lack of link beat
                # as not ready, and unplugged thereafter
                if [ $ELAPSED_TIME -lt $LINK_DETECTION_DELAY ]; then
                    return 0
                fi
                # no need to wait for unplugged devices to come up
                continue
            fi

            is_nm_active "${DEVICE}" || return 0

            # The resolvconf check below uses a single generic "NetworkManager"
            # DNS file, rather than device specific ones, so fudge the device.
            DEVICE=NetworkManager
        else
            # check link beat
            if ! may_have_link ${DEVICE}; then
                # before configured delay, consider a lack of link beat
                # as not ready, and unplugged thereafter
                if [ $ELAPSED_TIME -lt $LINK_DETECTION_DELAY ]; then
                    return 0
                fi
                # no need to wait for unplugged devices to come up
                continue
            fi

            # check address is set
	    ADDR=$(ip addr show scope global "${DEVICE}" | awk '/inet/ {print $2;}')
            if [ -z "$ADDR" ]; then
                return 0
            fi
        fi

        # wait for changes to be propagated by resolvconf if needed
        if [ -e $RESOLVCONF_FLAGFILE ]; then
            if [ "$BOOTPROTO" = "dhcp" ] && ! is_false "$PEERDNS" \
                || [ -n "$DNS1" ] || [ -n "$DNS2" ]; then
                if [ ! -e $RESOLVCONF_DIR/$DEVICE ]; then
                    return 0
                fi
                if [ $RESOLVCONF_DIR/$DEVICE -nt /etc/resolv.conf ]; then
                    return 0
                fi
            fi
        fi
    done
    # all interfaces are ready
    return 1
}

case "$1" in
  start)
    gprintf "Waiting for network to be up"

    for i in $interfaces; do
        LINK_DETECTION_DELAY=$DEFAULT_LINK_DETECTION_DELAY
        eval $(LANG=C grep -F "LINK_DETECTION_DELAY=" "ifcfg-$i")
        if [ "$LINK_DETECTION_DELAY" -gt $MAX_LINK_DETECTION_DELAY ]; then
            MAX_LINK_DETECTION_DELAY=$LINK_DETECTION_DELAY
        fi
    done
    NETWORKDELAY=$(( NETWORKDELAY + MAX_LINK_DETECTION_DELAY ))

    while should_wait_network && [ $ELAPSED_TIME -lt $NETWORKDELAY ]; do
        sleep 1
        let ELAPSED_TIME=$ELAPSED_TIME+1
    done
    [ $ELAPSED_TIME -ge $NETWORKDELAY ] && failure || success
    echo
    ;;
  stop)
      ;;
  *)
        gprintf "Usage: %s\n" "$(basename "$0") {start|stop}"
        exit 1
esac

exit 0