File: network

package info (click to toggle)
hibernate 2.0%2B15%2Bg88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (211 lines) | stat: -rw-r--r-- 5,163 bytes parent folder | download
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler NetworkConfigOptions
AddConfigHelp "DownInterfaces auto|<ifname> [...]" "The names of network interfaces to bring down before suspending. If the parameter \"auto\" is given, all interfaces which are not lo are brought down."
AddConfigHelp "UpInterfaces auto|<ifname> [...]" "The names of network interfaces to bring up after suspending. If the parameter \"auto\" is given, the interfaces stopped before suspending will be started in reverse order."

NetworkStop() {
    # If the script stops networking altogether, there's not much we can do.
    if [ -n "$NETWORK_CALL_ONCE" ] ; then
	network_ifdown
	[ $? -eq 0 ] && return 0
	return 1
    fi

    local ret
    local int
    local do_auto
    NETWORK_DOWNEDIFS="" # for saving the downed interfaces, for "auto"
    ret=0
    do_auto=
    case $NETWORK_DOWNIFS in *\ auto\ *) do_auto=yes ;; esac
    for int in `sed -rne 's,[[:space:]]*([[:alnum:]]+):.*,\1,p' /proc/net/dev`; do
	if [ -z "$do_auto" ] ; then
	    # Only proceed if we were given "auto" or this interface was meant
	    # to be brought down.
	    case "$NETWORK_DOWNIFS" in *\ $int\ *) ;; *) continue ;; esac
	else
	    # Don't bring down lo in auto mode.
	    [ "$int" = "lo" ] && continue
	fi
	vecho 2 "Bringing down interface $int"
	NETWORK_DOWNEDIFS="$(network_ifdown $int) $NETWORK_DOWNEDIFS" || ret=$?
    done
    return $ret
}

NetworkStart() {
    # If the script starts networking altogether, there's not much we can do.
    if [ -n "$NETWORK_CALL_ONCE" ] ; then
	network_ifup
	[ $? -eq 0 ] && return 0
	return 1
    fi

    local ret
    local int
    ret=0
    for int in $NETWORK_UPIFS ; do
	if [ "$int" = "auto" ] ; then
	    for int in $NETWORK_DOWNEDIFS ; do
		vecho 2 "Bringing up interface $int (from auto)"
		network_ifup $int
		[ $? -ne 0 ] && ret=1
	    done
	    continue
	fi
	vecho 2 "Bringing up interface $int"
	network_ifup $int
	[ $? -ne 0 ] && ret=1
    done
    return 0
}

NetworkConfigOptions() {
    local opt
    opt="$1"
    shift
    case $opt in
	downinterfaces)
	    NETWORK_DOWNIFS="$NETWORK_DOWNIFS $* "
	    ;;
	upinterfaces)
	    NETWORK_UPIFS="$NETWORK_UPIFS $* "
	    ;;
	*)
	    return 1
    esac
    if [ -z "$NETWORK_HOOKED" ] ; then
	AddSuspendHook 60 NetworkStop
	AddResumeHook 60 NetworkStart
	NetworkDetectDistro
	NETWORK_HOOKED=1
    fi
    return 0
}

NetworkDetectDistro() {
    # Use either a given $DISTRIBUTION or autodetect one.
    case "$DISTRIBUTION" in
        arch)
            network_ifup() {
                /etc/rc.d/network ifup $1
            }
            network_ifdown() {
                /etc/rc.d/network ifdown $1
            }
            ;;
	gentoo)
	    network_ifup() {
	    	[ -x "/etc/init.d/net.$1" ] && /etc/init.d/net.$1 start
	    }
	    network_ifdown() {
	    	[ -x "/etc/init.d/net.$1" ] && /etc/init.d/net.$1 stop \
		   || return $?
		echo $1
	    }
	    ;;
	suse)
	    network_ifup() {
		/etc/init.d/network start
	    }
	    network_ifdown() {
		/etc/init.d/network stop || return $?
		echo all
	    }
	    NETWORK_CALL_ONCE=1
	    ;;
	debian|ubuntu)
	    network_ifup() {
		start-stop-daemon --start --background \
		    --startas /sbin/ifup --name "hibernate_ifup_$1" -- $*
	    }
	    network_ifdown() {
                STDERR="$(/sbin/ifdown $1 2>&1 1>/dev/null)" || return $?
                case "$STDERR" in
                    "/sbin/ifdown: interface $1 not configured") :;;
                    *) echo $1;;
                esac
            }
	    ;;
	mandrake|fedora|redhat)
	    network_ifup() {
		/sbin/ifup $*
	    }
	    network_ifdown() {
		/sbin/ifdown $* || return $?
		echo $*
	    }
	    ;;
	slackware)
	    network_ifup() {
		/etc/rc.d/rc.inet1 start
	    }
	    network_ifdown() {
		/etc/rc.d/rc.inet1 stop || return $?
		echo all
	    }
	    NETWORK_CALL_ONCE=1
	    ;;
    	*)
	    # Auto-detect
	    if [ -x "/sbin/ifup" ] ; then
		network_ifup() {
		    /sbin/ifup $*
		}
		network_ifdown() {
		    STDERR="$(/sbin/ifdown $1 2>&1 1>/dev/null)" || return $?
		    case "$STDERR" in
			"/sbin/ifdown: interface $1 not configured") :;;
			*) echo $1;;
		    esac
		}
	    elif [ -x "/etc/init.d/ifup" ] ; then
		network_ifup() {
		    /etc/init.d/ifup $*
		}
		network_ifdown() {
		    /etc/init.d/ifdown $* || return $?
		    echo $*
		}
	    elif [ -x "/etc/sysconfig/network-scripts/ifup" ] ; then
		network_ifup() {
		    /etc/sysconfig/network-scripts/ifup $*
		}
		network_ifdown() {
		    /etc/sysconfig/network-scripts/ifdown $* || return $?
		    echo $*
		}
	    elif [ -x "/etc/init.d/networking" ] ; then
		network_ifup() {
		    /etc/init.d/networking start
		}
		network_ifdown() {
		    /etc/init.d/networking stop || return $?
		    echo all
		}
		NETWORK_CALL_ONCE=1
	    elif [ -x "/etc/init.d/network" ] ; then
		network_ifup() {
		    /etc/init.d/network start
		}
		network_ifdown() {
		    /etc/init.d/network stop || return $?
		    echo all
		}
		NETWORK_CALL_ONCE=1
	    else
	    	network_ifup() {
		    /sbin/ifconfig $1 up
		}
		network_ifdown() {
		    /sbin/ifconfig $1 down || return $?
		    echo $1
		}
	    fi
    esac
    return 0
}

# $Id$