summaryrefslogtreecommitdiff
blob: f78a517a7bfd7f22147b79c70c5fa19144082973 (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
#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-apps/ifplugd/files/ifplugd.init,v 1.3 2005/10/10 07:39:01 uberlord Exp $

#NB: Config is in /etc/conf.d/ifplugd

depend()
{
	use pcmcia
}

opts="stop start status"

get_start_interfaces() {
	if [[ -n ${INTERFACES} ]]; then
		echo "${INTERFACES}"
		return
	fi

	INTERFACES=" $(sed -ne 's/^[ \t]*\(.*\):.*/\1/p' /proc/net/dev | xargs) "
	local exclude iface
	if [[ -f /proc/net/wireless && ${WIRELESS_INTERFACES} == "no" ]]; then
		exclude=$(sed -ne 's/^[ \t]*\(.*\):.*/\1/p' /proc/net/wireless | xargs)
	fi
	
	# Exclude ifplugd started by other scripts - like net.xxx
	if [[ -d "${svcdir}/daemons" ]]; then
		exclude="${exclude} $(
			grep "\"/var/run/ifplugd.*.pid\"" "${svcdir}"/daemons/* 2>/dev/null \
			| grep -v "${svcdir}/daemons/ifplugd:" \
			| sed -n -e 's/.*ifplugd\.\(.*\)\.pid.*/\1/p'
		)"
	fi
	
	exclude=" lo ${exclude} "
	for iface in ${exclude}; do
		INTERFACES="${INTERFACES// ${iface} / }"
	done
	echo "${INTERFACES}"
}

get_running_interfaces() {
	local exclude="" INTERFACES=$(
		cd /var/run
		ls ifplugd.*.pid 2>/dev/null | sed -n -e 's/^ifplugd.\(.*\).pid$/\1/p' | xargs
	)
	if [[ -d "${svcdir}/daemons" ]]; then
		local exclude=$(
			grep "\"/var/run/ifplugd.*.pid\"" "${svcdir}"/daemons/* 2>/dev/null \
			| grep -v "${svcdir}/daemons/ifplugd:" \
			| sed -n -e 's/.*ifplugd\.\(.*\)\.pid.*/\1/p'
		)
	fi

	INTERFACES=" ${INTERFACES} "
	for iface in ${exclude}; do
		INTERFACES=${INTERFACES// ${iface} / }
	done
	echo "${INTERFACES}"
}

# Check if an option is set for a given interface.
# $1 is interface, $2 is option name, $3 is preset
is_set() {
	[[ $(get_opt "$@") == "yes" ]]
}

# Expand an option value for a given interface.
# $1 is interface, $2 is option name, $3 is preset
get_opt() {
	local iface="$1" option="$2" preset="$3"
	eval preset=\"\${${option}:=${preset}}\"
	eval echo \"\${${option}_${iface}:=${preset}}\"
}

# Handle starting for all interfaces
start() {
	local iface oneworked=false

	einfo "Starting ifplugd: "
	eindent
	
	for iface in $(get_start_interfaces); do
		ebegin "${iface}"
		local args="" pidfile="/var/run/ifplugd.${iface}.pid"

		if [[ -e ${pidfile} ]] ; then
			if is_daemon_running /usr/sbin/ifplugd "${pidfile}" ; then
				eindent
				einfo "ifplugd is already running on ${iface}"
				eend 0
				eoutdent
				oneworked=true
				continue
			fi
		fi
		
		is_set "${iface}" AUTO yes || args="${args}a"
		is_set "${iface}" BEEP yes || args="${args}b"
		is_set "${iface}" IGNORE_FAIL yes && args="${args}f"
		is_set "${iface}" IGNORE_FAIL_POSITIVE no && args="${args}F"
		is_set "${iface}" IGNORE_RETVAL yes && args="${args}I"
		is_set "${iface}" WAIT_ON_FORK yes && args="${args}w"
		is_set "${iface}" MONITOR no && args="${args}M"

		[[ -n ${args} ]] && args="-${args}"

		args="${args} -t$(get_opt ${iface} POLL_TIME 1)"
		args="${args} -u$(get_opt ${iface} DELAY_UP 0)"
		args="${args} -d$(get_opt ${iface} DELAY_DOWN 5)"
		args="${args} -m$(get_opt ${iface} API_MODE auto)"
		args="${args} $(get_opt ${iface} ARGS '')"

		start-stop-daemon --start --exec /usr/sbin/ifplugd \
			--pidfile "/var/run/ifplugd.${iface}.pid" \
			-- --iface="${iface}" ${args}
		local r="$?"
		if is_set "${iface}" WAIT_ON_FORK yes ; then
			[[ ${r} -le 2 ]]
		else
			[[ ${r} == "0" ]]
		fi
		eend $? && oneworked=true
	done

	${oneworked}
}

stop() {
	local iface allstopped=true
	
	einfo "Stopping ifplugd: "
	eindent

	for iface in $(get_running_interfaces); do
		ebegin "${iface}"
		start-stop-daemon --stop --exec /usr/sbin/ifplugd \
			--pidfile "/var/run/ifplugd.${iface}.pid" --signal 3 
		eend $? || allstopped=false
	done

	${allstopped}
}

status() {
	local iface

	service_started "${myservice}" || return 0

	for iface in $(get_running_interfaces); do
		einfo "$(/usr/sbin/ifplugstatus ${iface})"
	done
}

# vim:ts=4