aboutsummaryrefslogtreecommitdiff
path: root/net.Linux/netplugd.sh
blob: 24539b14c9e4b8a8663b6a33d086d1d1d27d1518 (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
# Copyright 2007 Roy Marples
# All rights reserved

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

_config_vars="$_config_vars plug_timeout"

netplugd_depend() {
	program start /sbin/netplugd
	after macnet rename
	before interface
	provide plug

	# Prefer us to ifplugd
	after ifplugd
}

netplugd_pre_start() {
	local pidfile="/var/run/netplugd-${IFACE}.pid" timeout=

	# We don't start netplug if we're being called from the background
	yesno ${IN_BACKGROUND} && return 0

	_exists || return 0

	# We need a valid MAC address
	# It's a basic test to ensure it's not a virtual interface
	if ! _get_mac_address >/dev/null 2>&1; then
		vewarn "netplug only works on interfaces with a valid MAC address"
		return 0
	fi

	# We don't work on bonded, bridges, tun/tap, vlan or wireless
	for f in bond bridge tuntap vlan wireless; do
		if type "_is_${f}" >/dev/null 2>&1; then
			if _is_${f}; then
				veinfo "netplug does not work with" "${f}"
				return 0
			fi
		fi
	done

	ebegin "Starting netplug on" "${IFACE}"

	# Mark the us as inactive so netplug can restart us
	mark_service_inactive

	# Start netplug
	start-stop-daemon --start --exec /sbin/netplugd \
		--pidfile "${pidfile}" \
		-- -i "${IFACE}" -P -p "${pidfile}" -c /dev/null
	eend "$?" || return 1

	eindent

	eval timeout=\$plug_timeout_${IFVAR}
	[ -z "${timeout}" ] && timeout=-1
	if [ ${timeout} -eq 0 ]; then
		ewarn "WARNING: infinite timeout set for ${IFACE} to come up"
	elif [ ${timeout} -lt 0 ]; then
		einfo "Backgrounding ..."
		exit 1 
	fi

	veinfo "Waiting for ${IFACE} to be marked as started"

	local i=0
	while true; do
		if service_started; then
			_show_address
			exit 0
		fi
		sleep 1
		[ ${timeout} -eq 0 ] && continue
		i=$((${i} + 1))
		[ ${i} -ge ${timeout} ] && break
	done

	eend 1 "Failed to configure ${IFACE} in the background"
	exit 1
}

netplugd_stop() {
	yesno ${IN_BACKGROUND} && return 0

	local pidfile="/var/run/netplugd-${IFACE}.pid"
	[ ! -e "${pidfile}" ] && return 0
	
	ebegin "Stopping netplug on" "${IFACE}"
	start-stop-daemon --stop --quiet --exec /sbin/netplugd \
		--pidfile "${pidfile}"
	eend $?
}

# vim: set ts=4 :