Debian Codegraph @

dahdi_span_types

raw ◀ ▶
1 Debian patch touch this file
1 #! /bin/sh
2 #
3 # /usr/sbin/dahdi_span_types
4 #
5 # This script can be used both from udev and
6 # from the command line to manage PRI spans
7 # type (E1/T1/J1).
8 #
9 # Span types can be set only *BEFORE* span are assigned.
10 #
11 # It uses a configuration file: $DAHDICONFDIR/span-types.conf
12 # (default DAHDICONFDIR=/etc/dahdi)
13 # (the format is documented inside that file)
14 #
15 # The first argument is an action:
16 # "set" - actually write the setting to the driver
17 # "list" - human-readable list of E1/T1/J1 types
18 # "dumpconfig" - dump current assignments in a /etc/dahdi/span-types.conf
19 # compatible format
20 #
21 # Without further arguments, it operates on all existing spans
22 # With one or more sysfs dahdi_devices it is limited to those.
23 #
24 # We may use alternative "keys" for device matching:
25 # * Available keys:
26 # - "hwid" - Hardware id attribute from sysfs
27 # - "@location" - Location attribute from sysfs (embeded inside '<>')
28 # - "/devpath" - The sysfs absolute devpath
29 #
30 # * Wildcard are allowed in the configuration file:
31 # - In the device specifiers (keys)
32 # - In the span numbers
33 # - Example for "match-all": * *:T1
34 #
35 # * During "set":
36 # - If there are multiple matches, for a span, all are applied
37 # - They are always applied in their order in the configuration file
38 # - This means the last match wins
39 # - Example:
40 # * *:T1 # All span on all devices are T1
41 # usb:X1234567 [34]:E1 # Except spans 3,4 on specific device
42 #
43 # * During "dumpconfig", for each device we take the first available key:
44 # - The preference is: "hwid" or else "@location" or else "/devpath"
45 # - This can be overriden via the SPAN_ASSIGNMENTS_KEY environment variable
46 # or the '{-k|--key} key' command line option.
47 #
48 # Command line options:
49 # - The '-h|--help' show a usage message.
50 # - The '-v|--verbose' show debugging messages (on stderr)
51 # - The '-n|--dry-run' During "set", only show what would be done
52 # - The '-k <key>|--key <key>' overrides the SPAN_ASSIGNMENTS_KEY environment
53 # variable.
54 #
55 # Examples:
56 # dahdi_span_types list
57 # dahdi_span_types set # all devices
58 # dahdi_span_types set /sys/bus/dahdi_devices/devices/astribanks:xbus-00
59 # dahdi_span_types -k location dumpconfig
60 #
61
62
63 devbase='/sys/bus/dahdi_devices/devices'
64 DAHDICONFDIR="${DAHDICONFDIR:-/etc/dahdi}"
65 DAHDISPANTYPESCONF="${DAHDISPANTYPESCONF:-"${DAHDICONFDIR}/span-types.conf"}"
66 SPAN_ASSIGNMENTS_KEY=${SPAN_ASSIGNMENTS_KEY:-hwid}
67
68 usage() {
69 echo >&2 "Usage: $0 [options] action [devpath ...]"
70 echo >&2 " action:"
71 echo >&2 " set - set spans to E1/T1 according to /etc/dahdi/span-types.conf"
72 echo >&2 " compare - show config values that differ from system"
73 echo >&2 " list - human-readable list of all spans"
74 echo >&2 " dumpconfig - dump current state in /etc/dahdi/span-types.conf format"
75 echo >&2 ""
76 echo >&2 " options:"
77 echo >&2 " -h|--help - Show this help"
78 echo >&2 " -v|--verbose' - Show debugging messages (on stderr)"
79 echo >&2 " -n|--dry-run' - During 'set', only show what would be done"
80 echo >&2 " -k|--key <k> - Override prefered key during dumpconfig action"
81 echo >&2 " --line-mode <m> - Set default line mode to <m> (E1/T1/J1)"
82 exit 1
83 }
84
85 # Parse command line options
86 TEMP=`getopt -o hnvk: --long help,dry-run,verbose,key:,line-mode: -n "$0" -- "$@"`
87 if [ $? != 0 ]; then
88 echo >&2 "Bad options"
89 usage
90 fi
91
92 compare=false
93
94 # Note the quotes around `$TEMP': they are essential!
95 eval set -- "$TEMP"
96
97 while true ; do
98 case "$1" in
99 -h|--help)
100 usage
101 ;;
102 -n|--dry-run)
103 shift
104 dry_run=true
105 ;;
106 -v|--verbose)
107 shift
108 verbose=true
109 ;;
110 -k|--key)
111 SPAN_ASSIGNMENTS_KEY="$2"
112 shift
113 shift
114 ;;
115 --line-mode)
116 DEFAULT_LINE_MODE="$2"
117 shift
118 shift
119 ;;
120 --)
121 shift
122 break
123 ;;
124 *)
125 echo "Internal error!"
126 exit 1
127 ;;
128 esac
129 done
130
131 if [ "$#" -eq 0 ]; then
132 echo >&2 "Missing action argument"
133 usage
134 fi
135 action="$1"
136 shift
137
138 # Validate SPAN_ASSIGNMENTS_KEY
139 case "$SPAN_ASSIGNMENTS_KEY" in
140 hwid|location|devpath)
141 ;;
142 *)
143 echo >&2 "Bad --key='$SPAN_ASSIGNMENTS_KEY' (should be: hwid|location|devpath)"
144 usage
145 ;;
146 esac
147
148 # Validate DEFAULT_LINE_MODE
149 case "$DEFAULT_LINE_MODE" in
150 E1|T1|J1|'')
151 ;;
152 *)
153 echo >&2 "Bad --line-mode='$DEFAULT_LINE_MODE' (should be: E1|T1|J1)"
154 usage
155 ;;
156 esac
157
158 if [ ! -d "$devbase" ]; then
159 echo >&2 "$0: Missing '$devbase' (DAHDI driver unloaded?)"
160 exit 1
161 fi
162
163 # Use given devices or otherwise, all existing devices
164 if [ "$#" -gt 0 ]; then
165 DEVICES="$@"
166 else
167 DEVICES=`ls -d $devbase/* 2>/dev/null`
168 fi
169
170 # Beware of special characters in attributes
171 attr_clean() {
172 cat "$1" 2>/dev/null | tr -d '\n' | tr '!' '/' | tr -c 'a-zA-Z0-9/:.-' '_'
173 }
174
•175 get_device_spantype_file() {
176 device=$1
177 sp_file="dahdi_spantype"
178 if [ ! -f "$device/$sp_file" ]; then
179 sp_file="spantype"
180 fi
181 echo "$device/$sp_file"
182 }
183
184 show_spantypes() {
185 echo "# PRI span types (E1/T1/J1)"
186 for device in $DEVICES
187 do
188 devpath=`cd "$device" && pwd -P`
189 location='@'`attr_clean "$device/location"`
190 hardware_id=`attr_clean "$device/hardware_id"`
•191 sp_file=`get_device_spantype_file "$device"`
192 cat "$sp_file" | while read st; do
193 case "$st" in
194 *:[ETJ]1)
195 printf "%-10s %-20s %-30s %s\n" \
196 "$st" "[$hardware_id]" "$location" \
197 "$devpath"
198 ;;
199 esac
200 done
201 done
202 }
203
204 list_pri_spantypes() {
•205 find $DEVICES -follow -maxdepth 1 -name spantype -or -name dahdi_spantype | \
206 xargs cat | \
207 sed -n '/:[ETJ]1$/s/^.*://p' | \
208 sort -u | \
209 tr '\n' ' ' | \
210 sed -e 's/^ *//' -e 's/ *$//'
211 }
212
213 dump_config() {
214 pri_spantypes=`list_pri_spantypes`
215 num_spantypes=`echo "$pri_spantypes" | wc -w`
216 gen_default=''
217 echo '#'
218 echo "# Autogenerated by $0 on `date`"
219 echo "# Map PRI DAHDI devices to span types for E1/T1/J1"
220 echo "#"
221
222 echo "# Summary:"
223 if [ "$DEFAULT_LINE_MODE" != '' ]; then
224 gen_default="$DEFAULT_LINE_MODE"
225 echo "# * Generating wildcard match of $gen_default."
226 echo "# - Was run with '--line-mode=$DEFAULT_LINE_MODE'"
227 elif [ "$num_spantypes" -eq 1 ]; then
228 gen_default="$pri_spantypes"
229 echo "# * Generating wildcard match of $gen_default."
230 echo "# - Spans were $pri_spantypes"
231 else
232 echo "# * Not generating wildcard match."
233 echo "# - Was run without '--line-mode' option and span were of mixed types [$pri_spantypes]"
234 fi
235 echo "#"
236 if [ "$num_spantypes" -eq 1 ]; then
237 echo "# * Generating a list of commented out configurations for spans."
238 echo "# - Spans were $pri_spantypes"
239 echo "# - Uncomment for specific overrides"
240 else
241 echo "# * Generating a list of specific span configurations."
242 echo "# - Spans were of mixed types: $pri_spantypes"
243 fi
244 echo "#"
245 echo ''
246
247 fmt="%-65s %s"
248 printf "$fmt\n" '# @location/hardware_id' 'span_type'
249
250 if [ "$gen_default" != '' ]; then
251 printf "$fmt\t\t# Wildcard line-mode" "*" "*:$gen_default"
252 echo ""
253 fi
254 echo ""
255 for device in $DEVICES
256 do
257 devpath=`cd "$device" && pwd -P`
258 location=`attr_clean "$device/location"`
259 hardware_id=`attr_clean "$device/hardware_id"`
260 if [ "$SPAN_ASSIGNMENTS_KEY" = 'hwid' -a "$hardware_id" != '' ]; then
261 id="$hardware_id"
262 elif [ "$SPAN_ASSIGNMENTS_KEY" = 'location' -a "$location" != '' ]; then
263 id="@$location"
264 else
265 id="$devpath"
266 fi
267 echo "# Device: [$hardware_id] @$location $devpath"
•268 sp_file=`get_device_spantype_file "$device"`
269 cat "$sp_file" | while read st; do
270 case "$st" in
271 *:[ETJ]1)
272 if [ "$num_spantypes" -eq 1 ]; then
273 printf "#$fmt\n" "$id" "$st"
274 else
275 printf "$fmt\n" "$id" "$st"
276 fi
277 ;;
278 *)
279 #echo "# Skipped local span `echo $st | sed 's/:/ -- /'`"
280 ;;
281 esac
282 done | sort -n
283 echo ''
284 done
285 }
286
287 # Allow comments and empty lines in config file
288 filter_conf() {
289 sed -e 's/#.*//' -e '/^[ \t]*$/d' "$DAHDISPANTYPESCONF"
290 }
291
292 handle_span() {
293 device="$1"
294 spantype="$2"
•295 sp_file=`get_device_spantype_file "$device"`
296 attr_file="$sp_file"
297 devpath=`cd "$device" && pwd -P`
298 devname=`echo "$device" | sed "s,$devbase/,,"`
299 location='@'`attr_clean "$device/location"`
300 hardware_id=`attr_clean "$device/hardware_id"`
301 spanno=`echo "$spantype" | cut -d: -f1`
302 #echo >&2 "DEBUG: $device $spanno ($spantype)"
303 filter_conf | while read id span_spec; do
304 sn=`echo "$span_spec" | cut -d: -f1`
305 val=`echo "$span_spec" | cut -d: -f2`
306 case "$spanno" in
307 $sn)
308 ;;
309 *)
310 #echo >&2 "no-match($device $spanno): $sn"
311 continue
312 ;;
313 esac
314 found=no
315 # GLOBBING
316 case "$location" in
317 $id)
318 #echo >&2 "match($id): $span_spec"
319 found=yes
320 ;;
321 esac
322 case "$hardware_id" in
323 $id)
324 #echo >&2 "match([$id]): $span_spec"
325 found=yes
326 ;;
327 esac
328 case "$devpath" in
329 $id)
330 #echo >&2 "match([$id]): $span_spec"
331 found=yes
332 ;;
333 esac
334 if [ "$found" = 'yes' ]; then
335 if [ "$dry_run" = 'true' -o "$verbose" = 'true' ]; then
336 echo >&2 "Set $devname span $spanno = $val"
337 fi
338 if [ "$dry_run" != 'true' ]; then
339 if [ "$compare" = 'true' ]; then
340 config="$spanno:$val"
341 system=`grep "$spanno:" "$attr_file"`
342 if [ "$config" != "$system" ]; then
343
344 active_val=`echo $system | cut -d: -f2`
345 echo "$devname $spanno $val $active_val" >>"$compare_results_file"
346 fi
347 else
348 echo "$spanno:$val" > "$attr_file"
349 fi
350 fi
351 fi
352 done
353 }
354
355 set_all_devices() {
356 span_differs='false'
357 SPANS_DIFFER='false'
358
359 if [ ! -f "$DAHDISPANTYPESCONF" ]; then
360 echo >&2 "$0: Missing configuration '$DAHDISPANTYPESCONF'"
361 exit 1
362 fi
363 for device in $DEVICES
364 do
365 devname=`echo "$device" | sed "s,$devbase/,,"`
•366 sp_file=`get_device_spantype_file "$device"`
367 cat "$sp_file" | while read spantype; do
368 case "$spantype" in
369 *:[ETJ]1)
370 handle_span "$device" "$spantype"
371 ;;
372 *)
373 if [ "$dry_run" = 'true' -o "$verbose" = 'true' ]; then
374 echo >&2 "Skipping non-E1/T1/J1 span ($devname -- $spantype)"
375 fi
376 ;;
377 esac
378 done
379 done
380 if [ "$compare" = 'true' ]; then
381 if [ -s "$compare_results_file" ]; then
382 echo "# Device Unit Config Active"
383 cat "$compare_results_file"
384 rm -f "$compare_results_file"
385 exit 5
386 fi
387 rm -f "$compare_results_file"
388 exit 0
389 fi
390 }
391
392 case "$action" in
393 list)
394 show_spantypes
395 ;;
396 dumpconfig)
397 dump_config
398 ;;
399 set)
400 set_all_devices
401 ;;
402 compare)
403 compare=true
404 compare_results_file=`mktemp`
405 set_all_devices
406 ;;
407 *)
408 usage
409 ;;
410 esac