aboutsummaryrefslogtreecommitdiff
path: root/net/clip.sh
blob: 78344d23841b25450fc6dfb505347c7e5d862d25 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2005-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

clip_depend()
{
	program /usr/sbin/atmsigd
    before interface
}

_config_vars="$_config_vars clip"

# This starts a service. Albeit atmsigd, ilmid and atmarpd do allow for back-
# grounding through the -b option, its usage causes them to be sensible to
# SIGHUP, which is sent to all daemons when console detaches right after
# startup. This is probably due to the fact that these programs don't detach
# themself from the controlling terminal when backgrounding... The only way I
# see to overcame this is to use the --background option in start-stop-daemon,
# which is reported as a "last resort" method, but it acts correctly about this.
atmclip_svc_start()
{
    ebegin "Starting $2 Daemon ($1)"
    start-stop-daemon --start \
		--background \
		--make-pidfile --pidfile "/var/run/$1.pid" \
		--exec "/usr/sbin/$1" -- -l syslog
    eend $?
}

atmclip_svcs_start()
{
    einfo "First CLIP instance: starting ATM CLIP daemons"
    eindent

	if yesno ${clip_full:-yes}; then
		atmclip_svc_start atmsigd "Signaling" && \
		atmclip_svc_start ilmid	  "Integrated Local Management Interface" && \
		atmclip_svc_start atmarpd "Address Resolution Protocol"
    else
		atmclip_svc_start atmarpd "Address Resolution Protocol"
    fi

    local r=$?

    eoutdent
    return ${r}
}

atmclip_svc_stop()
{
    ebegin "Stopping $2 Daemon ($1)"
    start-stop-daemon --stop --quiet \
		--pidfile "/var/run/$1.pid" \
		--exec "/usr/sbin/$1"
    eend $?
}

atmclip_svcs_stop()
{
    einfo "Last CLIP instance: stopping ATM CLIP daemons"
    eindent

    # Heartake operation!
    sync

    atmclip_svc_stop atmarpd "Address Resolution Protocol"
	if yesno ${clip_full:-yes}; then
		atmclip_svc_stop ilmid "Integrated Local Management Interface"
		atmclip_svc_stop atmsigd "Signaling"
    fi

    eoutdent
}

are_atmclip_svcs_running()
{

	start-stop-daemon --quiet --test --stop --pidfile /var/run/atmarpd.pid || return 1

	if yesno ${clip_full:-yes}; then
		start-stop-daemon --quiet --test --stop --pidfile /var/run/ilmid.pid || return 1
		start-stop-daemon --quiet --test --stop --pidfile /var/run/atmsigd.pid || return 1
	fi

    return 0
}

clip_pre_start()
{
	local clip=
	eval clip=\$clip_${IFVAR}
	[ -z "${clip}" ] && return 0

    if [ ! -r /proc/net/atm/arp ]; then
		modprobe clip && sleep 2
		if [ ! -r /proc/net/atm/arp ]; then
	    	eerror "You need first to enable kernel support for ATM CLIP"
	    	return 1
		fi
    fi
	
    local started_here=
    if ! are_atmclip_svcs_running; then
		atmclip_svcs_start || return 1
		started_here=1
    fi

    if ! _exists; then
		ebegin "Creating CLIP interface ${IFACE}"
		atmarp -c "${IFACE}"
		if ! eend $?; then
			[ -z "${started_here}" ] && atmclip_svcs_stop
	    	return 1
		fi
    fi

    return 0
}

clip_post_start()
{
	local clip="$(_get_array "clip_${IFVAR}")"
	[ -z "${clip}" ] && return 0

	are_atmclip_svcs_running || return 1

    # The atm tools (atmarpd?) are silly enough that they would not work with
    # iproute2 interface setup as opposed to the ifconfig one.
    # The workaround is to temporarily toggle the interface state from up
    # to down and then up again, without touching its address. This (should)
    # work with both iproute2 and ifconfig.
    _down
    _up

    # Now the real thing: create a PVC with our peer(s).
    # There are cases in which the ATM interface is not yet
    # ready to establish new VCCs. In that cases, atmarp would
    # fail. Here we allow 10 retries to happen every 2 seconds before
    # reporting problems. Also, when no defined VC can be established,
    # we stop the ATM daemons.
    local has_failures= i=
	for i in ${clip}; do
		local IFS=","
		set -- ${i}
		unset IFS
		local peerip="$1"; shift
		local ifvpivci="$1"; shift
		ebegin "Creating PVC ${ifvpivci} for peer ${peerip}"

		local nleftretries=10 emsg= ecode=
		while [ ${nleftretries} -gt 0 ]; do
			nleftretries=$((${nleftretries} - 1))
	    	emsg="$(atmarp -s "${peerip}" "${ifvpivci}" "$@" 2>&1)"
	    	ecode=$? && break
	    	sleep 2
		done

		if ! eend ${ecode}; then
	    	eerror "Creation failed for PVC ${ifvpivci}: ${emsg}"
	    	has_failures=1
		fi
    done

    if [ -n "${has_failures}" ]; then
		clip_pre_stop "${iface}"
		clip_post_stop "${iface}"
		return 1
    else
		return 0
    fi
}

clip_pre_stop()
{
    are_atmclip_svcs_running || return 0

	# We remove all the PVCs which may have been created by
	# clip_post_start for this interface. This shouldn't be
	# needed by the ATM stack, but sometimes I got a panic
	# killing CLIP daemons without previously vacuuming
	# every active CLIP PVCs.
	# The linux 2.6's ATM stack is really a mess...
	local itf= t= encp= idle= ipaddr= left=
	einfo "Removing PVCs on this interface"
	eindent
	{
		read left && \
		while read itf t encp idle ipaddr left; do
			if [ "${itf}" = "${IFACE}" ]; then
				ebegin "Removing PVC to ${ipaddr}"
				atmarp -d "${ipaddr}"
				eend $?
			fi
		done
	} < /proc/net/atm/arp
	eoutdent
}

# Here we should teorically delete the interface previously created in the
# clip_pre_start function, but there is no way to "undo" an interface creation.
# We can just leave the interface down. "ifconfig -a" will still list it...
# Also, here we can stop the ATM CLIP daemons if there is no other CLIP PVC
# outstanding. We check this condition by inspecting the /proc/net/atm/arp file.
clip_post_stop()
{
    are_atmclip_svcs_running || return 0

    local itf= left= hasothers=
    {
		read left && \
		while read itf left; do
	    	if [ "${itf}" != "${IFACE}" ]; then
				hasothers=1
				break
	    	fi
		done
    } < /proc/net/atm/arp

    if [ -z "${hasothers}" ]; then
		atmclip_svcs_stop || return 1
    fi
}