File: init.d.functions

package info (click to toggle)
isdnutils 1%3A3.1pre4-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,096 kB
  • ctags: 11,039
  • sloc: ansic: 95,142; sh: 15,612; perl: 4,429; makefile: 3,064; cpp: 2,708; sql: 54
file content (446 lines) | stat: -rw-r--r-- 15,920 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

# don't call this file directly.


# Try not to change this file!  It should not be necessary.
#
#
# Please read /usr/share/doc/isdnutils/README.debian
# and         /usr/share/doc/isdnutils/HOWTO.isdnutils.gz .
#
# This file is for use from the /etc/init.d/isdn* scripts.
# If you want to do things differently, I advise you to put the
# parts you want to change into the init.d script directly after the
# line where this file is sourced, and change it there. Any changes
# here may be overwritten when isdnutils is updated!
#
# If you have an idea that something here could/should be done
# differently, let me (isdnutils@packages.debian.org) know and
# we can work something out.

do_what="$1"

if [ ! -d /etc/isdn ]; then
       echo "The /etc/isdn directory does not exist; this can't happen!"
       exit 1
fi

if [ "${VERBOSE:=yes}" = yes ]
then    isdn_verbose=true
else    isdn_verbose=false
fi


#################
# misc. functions
#################

NOT_CONFIGED='not configured yet'
not_configured=''

listdevices() {
	DEVS=`find /etc/isdn -regex '/etc/isdn/device\.\(ippp\|isdn\)[0-9]+' -printf '%f\n'`
}

# devices for which there is no config file (anymore)
listextradevices() {
    # can't count on "isdnctrl status all" working yet, unfortunately...
	DEVS=`/usr/sbin/isdnctrl list all | grep 'Current setup' | cut -f2 -d"'" | sort`
}

listdaemons() {
	DAEMONS=`find /etc/isdn -regex '/etc/isdn/ipppd\.ippp[0-9]+' -printf '%f\n'`
}

list_unconfigured() {
    if [ -n "$not_configured" ]; then
        echo "Note: the following configuration files are not configured yet:"
        for i in $not_configured; do echo "	$i"; done
    fi
}

findslaves() {
    fs_device=$1
    fs_base=${fs_device%%[0-9]*}			# ippp or isdn part
    # magic to get it sorted numerically;
    #  if there's a clean way of doing it easier, let me know!
    fs_list=`echo device.${fs_device}+*`
    if [ "$fs_list" = "device.${fs_device}+*" ]; then
	return
    fi
    for fs_slavefile in $fs_list; do
	echo ${fs_slavefile#*+}
    done | sort -n | while read fs_slave; do echo $fs_base$fs_slave; done
}

# needs to be called at least once per init.d/isdnutils invocation...
isdncards_unchecked=true

check_isdncards() {
    if $isdncards_unchecked; then
	isdncards_unchecked=false
        if [ -e /dev/isdninfo ]; then ISDNINFO=/dev/isdninfo;
        elif [ -e /dev/isdn/isdninfo ]; then ISDNINFO=/dev/isdn/isdninfo;
        else echo " error:"
             echo "    Neither /dev/isdninfo nor /dev/isdn/isdninfo exist!"
            # apparently a devfs system...
             echo "    Before you can use any ISDN facilities, ensure you have the proper kernel"
             echo "    modules loaded. These will probably be 'isdn' and 'hisax'."
             echo "    Read /usr/share/doc/isdnutils/README.HiSax.gz for more information."
             exit 0
        fi
	set -- `perl -ne 's/^idmap:\s+//; s/ -//g; s/(\S+) \1/\1/g; print; exit;' $ISDNINFO 2>/dev/null`
	if [ $# -eq 0 -a "$do_what" != stop ]; then
            echo " "
            echo " no ISDN cards configured! Please configure 'hisax' module with modconf"
            echo " (read /usr/share/doc/isdnutils/HOWTO.isdnutils.gz for more info)."
	    exit 0
	fi
	isdncards="$@"
    fi
}


##########################
# start and stop functions
##########################

start_isdnlog() {
    check_isdncards # side-effect: sets $isdncards
    what=' isdnlog'
    cardnum=0   # counts in channels, just like /dev/isdnctrlX
    for optionfile in /etc/isdn/isdnlog.isdnctrl[02468]; do
        test -e $optionfile || continue;
        if grep -s "$NOT_CONFIGED" $optionfile > /dev/null; then
            not_configured="$not_configured$optionfile "
            continue    # next patient please...
        fi
        device=${optionfile#*.}
        device=${device%.*}
        if   [ -e /dev/$device ];      then DEVISDNCTRL=/dev/$device;
        elif [ -e /dev/isdn/$device ]; then DEVISDNCTRL=/dev/isdn/$device;
        else
            echo " error:"
            echo "    Neither /dev/$device nor /dev/isdn/$device exist!"
            continue
        fi
        devicenum=${device#isdnctrl}
        devicenum=$(($devicenum / 2))
        set -- $isdncards
        if [ $devicenum -gt 0 ]; then
            shift $devicenum
        fi
	cardid=$1; shift
        if [ -z "$cardid" ]; then
            echo " error:"
            echo "    There doesn't appear to be a $device device!"
            continue
        fi
        if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        # Enable debugging for this card (isdnlog requires "1 4",
        # for D-Channel Q.931, call control messages) unless it's
        # clearly not a hisax card...
        case "$cardid" in
            capidrv-*)  ;;
            ?*)	        hisaxctrl $cardid 1 4
                        # the ?* doesn't match an empty string
                        ;;
        esac
        # test for isdnctrl dualmode. With dualmode, one isdnlog listens to
        # two cards, and both cards must have the D-channel tracing enabled.
        if grep '^[ 	]*dual=' $optionfile >/dev/null 2>&1; then
            cardid=$1; shift
            case "$cardid" in
                capidrv-*)  ;;
                ?*)         hisaxctrl $cardid 1 4
                            ;;
            esac
        fi
        start-stop-daemon --start --pidfile=/var/run/isdnlog.$device.pid \
            --quiet --exec /usr/sbin/isdnlog -- -D -f$optionfile $DEVISDNCTRL &
    done
}

stop_isdnlog() {
    check_isdncards # side-effect: sets $isdncards
    what=' isdnlog'
    for optionfile in /etc/isdn/isdnlog.isdnctrl?; do
        test -e $optionfile || continue;
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        device=${optionfile#*.}
        start-stop-daemon --stop --quiet --oknodo \
                --pidfile /var/run/isdnlog.$device.pid
                # --exec /usr/sbin/isdnlog
    done
}

reload_isdnlog() {
    check_isdncards # side-effect: sets $isdncards
    what=' isdnlog'
    for optionfile in /etc/isdn/isdnlog.isdnctrl?; do
        test -e $optionfile || continue;
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        device=${optionfile#*.}
        start-stop-daemon --stop --quiet --signal 1 \
                --pidfile /var/run/isdnlog.$device.pid || return 1
                # --exec /usr/sbin/isdnlog
    done
}

start_devices() {
    # the next part doesn't belong here, but there isn't really any choice...
    if [ "x$quiet_start" != 'x' ]; then	# user didn't replace old init.d script
	/bin/echo -e "\n\tplease update your copy of /etc/init.d/isdnutils to\n\tinclude the 'isdn_verbose' setting instead of 'quiet_start'\n\t(see /etc/init.d/isdnutils.dpkg-new)."
	case "$quiet_start" in
		true)	isdn_verbose=false;;
		*)	isdn_verbose=true;;
	esac
    fi
    check_isdncards # side-effect: sets $isdncards
    what=' interfaces'
    olddir=`pwd`
    cd /etc/isdn
    # check whether formware needs to be downloaded (done elsewhere)
    if [ -s firmware.conf ]; then
        /bin/echo -e 'firmware \c'
        . ./firmware.conf       # I hate ash
    fi
    listdevices
    if [ -z "$DEVS" -a ! -f /etc/isdn/noconfig ]; then
        echo "  There is apparently no configuration created yet."
        echo "  You can create configuration files with the 'isdnconfig' tool."
        echo " "
        echo "  If you don't want this warning in future, please create the file"
        echo "     /etc/isdn/noconfig"
        echo "  For more information, see /usr/share/doc/isdnutils/HOWTO.isdnutils.gz"
    fi
    # start devices in order
    # Device files of the form "device.ippp2+3" means that this (ippp3) is a
    # slave device for ippp2. So when ippp2 is being started, look for any
    # "device.ippp2+*" files and also start those (as slaves)
    tempfile=`tempfile -p isdn.`
    for devicefile in `echo $DEVS | sort`; do
        test -f $devicefile || continue;
        if grep -s "^[^#].*$NOT_CONFIGED" $devicefile > /dev/null; then
	    not_configured="$not_configured$devicefile "
	    continue
	fi
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
	master=${devicefile#*.}			# part after .
	slaves=`findslaves $master`
	if [ -z "$slaves" ]; then
	    extraopts=''
	else
	    extraopts='master'
	fi
	for device in $master $slaves; do
            # if the device doesn't exist in /dev nor in /dev/isdn,
            # then this is (obviously?) not a devfs system, thus
            # the device needs to be created.
            if [ ! -c /dev/$device -a ! -c /dev/isdn/$device ]; then
		cd /dev
		/bin/echo -e " (creating /dev/$device)\c"
		/sbin/MAKEDEV $device || true
		cd /etc/isdn
	    fi
	    if [ `expr "$extraopts" : 'slave'` -eq 5 ]; then
		device=${master}+${device##????}
		# slaves can't be added if the master interface
		# is already up; so "down" it now.
                # Problem is that any routes over this interface disappear when
                # you do "down"... Hope this hack works...
                netstat -rn | grep " $master$" | fgrep -v 255.255.255.255 |
                    while read net bla mask bla; do
                        echo "/sbin/route add -net $net netmask $mask dev $master"
                    done > $tempfile
		ifconfig $master down
	    fi

            # stop / remove device if it happens to exist
            if grep -w $device /proc/net/dev >/dev/null 2>&1; then
                sh /etc/isdn/device.$device stop >/dev/null 2>&1 ||
                    /usr/sbin/isdnctrl delif $device >/dev/null 2>&1 || true
            fi
	    if sh /etc/isdn/device.$device start $extraopts > /dev/null; then
                : # OK
            else
                echo " $device failed."
            fi
	    extraopts="slave $master"
	done
	if [ ! -z "$slaves" ]; then
	    ifconfig $master up
	fi
	if [ -s $tempfile ]; then
	    . $tempfile
	fi
	rm -f $tempfile
    done
    cd "$olddir"
}

stop_devices() {
    check_isdncards # side-effect: sets $isdncards
    what=' interfaces'
    olddir=`pwd`
    cd /etc/isdn
    listdevices
    # stop the devices in reverse order to starting them
    for devicefile in `echo $DEVS | sort -r` ; do
        test -f $devicefile || continue;
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        master=${devicefile#*.}
	# not needed, removing master will automagically remove all slaves.
	#  slaves=`findslaves $master`
	#  slave=''
	#  for device in $master $slaves; do
	#      sh ${devicefile} stop $slave > /dev/null || true
	#      slave="slave $master"
	#  done
	sh ${devicefile} stop > /dev/null || true
    done
    listextradevices
    if [ ! -z "$DEVS" ]; then
        if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        /bin/echo -e "\nThese ISDN devices were not stopped by the config files"
	echo    "(possible error in a file?): $DEVS"
        /bin/echo -e "These are now also being stopped... "
        # There's no graceful way of doing this...
        for device in $DEVS; do
            /bin/echo -e "$device \c"
            /sbin/ifconfig $device down 2>/dev/null || true
            /usr/sbin/isdnctrl delif $device >/dev/null 2>&1 || true
        done
        echo " done."
    fi
    cd "$olddir"
}

start_ipppd() {
    check_isdncards # side-effect: sets $isdncards
    what=' ipppd'
    olddir=`pwd`
    cd /etc/isdn
    listdaemons
    for optionfile in $DAEMONS; do
        test -e $optionfile || continue;
        if grep -s "$NOT_CONFIGED" $optionfile > /dev/null; then
	    not_configured="$not_configured$optionfile "
	    continue
	fi
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
	master=${optionfile#*.}
	slaves=`findslaves $master |while read slave; do if [ -c /dev/isdn/$slave ]; then slave=isdn/$slave; fi; echo /dev/$slave; done`
	extraops=''
	if [ ! -z "$slaves" ]; then
	    if ! grep -qw '^+mp' $optionfile; then
		echo " warning: adding +mp option for $master!"
		/bin/echo -e '	\c'
		extraops='+mp'
	    fi
	fi
        if [ -c /dev/isdn/$master ]; then master=isdn/$master; fi
	start-stop-daemon --start --quiet \
	    --pidfile /var/run/ipppd.$master.pid \
	    --exec /usr/sbin/ipppd -- file $optionfile $extraops /dev/$master $slaves &
    done
    cd "$olddir"
    # next part copied from ipppd.postinst
    ipppd_have_up=false
    ipppd_have_down=false
    if [ -s /etc/ppp/ip-up.d/00-isdnutils ]; then ipppd_have_up=true; fi
    if [ -s /etc/ppp/ip-down.d/99-isdnutils ]; then ipppd_have_down=true; fi
    if $ipppd_have_up || $ipppd_have_down; then
	echo ""
        if ! $ipppd_have_up; then # have only down
            echo "The /etc/ppp/ip-down.d/99-isdnutils file from the old isdnutils package still"
            echo "exists. If you changed that file at some point, you may need to redo those"
            echo "changes in the 99-ipppd file (which is the new name). After that, please"
            echo "delete the old 99-isdnutils file."
        elif ! $ipppd_have_down; then # have only up
            echo "The /etc/ppp/ip-up.d/00-isdnutils file from the old isdnutils package still"
            echo "exists. If you changed that file at some point, you may need to redo those"
            echo "changes in the 00-ipppd file (which is the new name). After that, please"
            echo "delete the old 00-isdnutils file."
        else # have both
            echo "The /etc/ppp/ip-up.d/00-isdnutils and /etc/ppp/ip-down.d/99-isdnutils files"
            echo "from the old isdnutils package still exist. If you changed those files at some"
            echo "point, you may need to redo those changes in the 00-ipppd and 99-ipppd files"
            echo "(which are the new names). After that, please delete the old 00-isdnutils"
            echo "and 99-isdnutils files."
        fi
        sleep 5
    fi
}

reload_ipppd() {
    check_isdncards # side-effect: sets $isdncards
    what=' ipppd'
    olddir=`pwd`
    cd /etc/isdn
    listdaemons
    for optionfile in $DAEMONS; do
        test -e $optionfile || continue;
        device=${optionfile#*.}
        if grep -s "$NOT_CONFIGED" $optionfile > /dev/null; then
	    not_configured="$not_configured$optionfile "
	else
	    if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
	    start-stop-daemon --stop --quiet --signal 1 \
                --pidfile /var/run/ipppd.$device.pid || return 1
                # --exec /usr/sbin/ipppd
        fi
    done
    cd "$olddir"
}

stop_ipppd() {
    check_isdncards # side-effect: sets $isdncards
    what=' ipppd'
    olddir=`pwd`
    cd /etc/isdn
    listdaemons
    for optionfile in $DAEMONS; do
        test -e $optionfile || continue;
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
        device=${optionfile#*.}
        start-stop-daemon --stop --quiet --oknodo \
		--pidfile /var/run/ipppd.$device.pid
		# --exec /usr/sbin/ipppd
    done
    # in case of removed config files, kill off any remaining ipppds...
    if [ -x /usr/bin/killall ]; then
        killall ipppd 2>/dev/null && /bin/echo -e "\n\tunknown ipppd processes killed... \c" || true
    else
        killedit=false
        for i in /var/run/ipppd.*.pid; do
            killedit=true
            kill `cat $i 2>/dev/null || true` 2>/dev/null || true
        done
        if $killedit; then
            /bin/echo -e "\n\tunknown ipppd processes killed... \c"
        fi
    fi
    cd "$olddir"
}

start_iprofd() {
    check_isdncards # side-effect: sets $isdncards
    what=' iprofd'
    if test -e /etc/isdn/iprofd.data ; then
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
	start-stop-daemon --start --quiet \
	    --exec /usr/sbin/iprofd -- /etc/isdn/iprofd.data &
    fi
}

stop_iprofd() {
    check_isdncards # side-effect: sets $isdncards
    what=' iprofd'
    if test -n "`pidof iprofd`"; then
	if $isdn_verbose; then /bin/echo -e "$what\c"; what=''; fi
	start-stop-daemon --stop --quiet --exec /usr/sbin/iprofd || true
    fi
}

# vim:set sw=4 si: