new "dahdi_waitfor_span_assignments" tool
authorOron Peled <oron.peled@xorcom.com>
Thu, 16 Jan 2014 13:03:22 +0000 (08:03 -0500)
committerTzafrir Cohen <tzafrir.cohen@xorcom.com>
Thu, 23 Jan 2014 10:35:59 +0000 (12:35 +0200)
* Allows waiting until all spans are "assigned" or "unassigned"
* Current implementation use a polling loop with sleep
* Future implementation may block on sysfs attribute
  (like waitfor_xpds is blocking on sysfs Astribanks attribute)

Signed-off-by: Tzafrir Cohen <tzafrir.cohen@xorcom.com>
Acked-by: Russ Meyerriecks <rmeyerriecks@digium.com>

Makefile
dahdi_waitfor_span_assignments [new file with mode: 0755]
doc/dahdi_waitfor_span_assignments.8 [new file with mode: 0644]

index 45fc7b7..09f42bc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -112,7 +112,8 @@ ASSIGNED_DATA_SCRIPTS:=\
        handle_device.d/10-span-types   \
        handle_device.d/20-span-assignments
 
-ASSIGNED_UTILS:=dahdi_span_assignments dahdi_span_types
+ASSIGNED_UTILS:=dahdi_span_assignments dahdi_span_types \
+       dahdi_waitfor_span_assignments
 ASSIGNED_CONF:=assigned-spans.conf.sample span-types.conf.sample
 
 MAN_PAGES:= \
diff --git a/dahdi_waitfor_span_assignments b/dahdi_waitfor_span_assignments
new file mode 100755 (executable)
index 0000000..1080d84
--- /dev/null
@@ -0,0 +1,73 @@
+#! /bin/sh
+
+usage() {
+       echo >&2 "Usage: $0 {assigned|unassigned}"
+       echo >&2 "# wait until all spans known are assigned/unassigned"
+       exit 1
+}
+
+TIMEOUT=5      # How much time to wait for spans
+
+if [ "$#" -lt 1 ]; then
+       usage
+fi
+wanted_event="$1"
+shift
+
+case "$wanted_event" in
+assigned)
+       ;;
+unassigned)
+       ;;
+*)
+       usage
+       ;;
+esac
+
+devbase='/sys/bus/dahdi_devices/devices'
+
+spans_of() {
+       dev="$1"
+       wc -l < "$dev/spantype"
+}
+
+assigned_spans_of() {
+       dev="$1"
+       ls -d "$dev/span-"* 2>/dev/null | wc -l
+}
+
+
+waitfor_span_assignments() {
+       wanted_state="$1"
+       device_list=`ls -d "$devbase/"* 2> /dev/null`
+       finished=''
+       count="$TIMEOUT"
+       echo -n "Waiting for spans to become $wanted_state: "
+       while [ "$count" -gt 0 ]; do
+               finished='yes'
+               for dev in $device_list
+               do
+                       spans=`spans_of "$dev"`
+                       assigned_spans=`assigned_spans_of "$dev"`
+                       if [ "$wanted_state" = 'assigned' -a "$assigned_spans" -ne "$spans" ]; then
+                               finished='no'
+                       elif [ "$wanted_state" = 'unassigned' -a "$assigned_spans" -ne 0 ]; then
+                               finished='no'
+                       fi
+               done
+               if [ "$finished" = 'yes' ]; then
+                       break
+               else
+                       sleep 1
+                       echo -n "."
+               fi
+               count=`expr "$count" - 1`
+       done
+       if [ "$finished" = 'yes' ]; then
+               echo "done"
+       else
+               echo "timeout"
+       fi
+}
+
+waitfor_span_assignments "$wanted_event"
diff --git a/doc/dahdi_waitfor_span_assignments.8 b/doc/dahdi_waitfor_span_assignments.8
new file mode 100644 (file)
index 0000000..6aaa0b9
--- /dev/null
@@ -0,0 +1,49 @@
+.TH "DAHDI_WAITFOR_SPAN_ASSIGNMENTS" "8" "22 Jan 2014" "" ""
+
+.SH NAME
+dahdi_waitfor_span_assignments \- wait for DAHDI spans to get (un)assigned
+.SH SYNOPSIS
+
+.B dahdi_span_assignments assigned
+
+.B dahdi_span_assignments unassigned
+
+.SH DESCRIPTION
+DAHDI spans get assigned / unassigned asynchronously.
+
+.B dahdi_span_assignments
+is a helper script that allows running commands after all the spans have
+been assigned or unassigned.
+
+It takes a single command: \fBassigned\fR or \fBunassigned\fR and waits
+(up until a timeout of 5 seconds) for all the DAHDI spans in the system
+to do so.
+
+Note that if the system has a span that will not get assigned
+automatically (e.g.: it's not in assigned\-spans.conf), this program
+does not know and will wait until a timeout.
+
+.SH EXAMPLES
+
+ modprobe wctdm24xxp
+ dahdi_waitfor_span_assignments assigned
+ do_something
+
+ dahdi_span_assignments add
+ dahdi_waitfor_span_assignments assigned
+ do_something_else
+
+ dahdi_span_assignments remove
+ dahdi_span_assignments unassigned
+ do_something_completely_different
+
+.SH SEE ALSO
+dahdi_span_assignments(8)
+
+.SH AUTHOR
+dahdi_waitfor_span_assignments was written by Oron Peled.  This manual
+page was written by Tzafrir Cohen. Permission is granted to copy,
+distribute and/or modify this document under the terms of the GNU
+General Public License, Version 2 any  later version published by the
+Free Software Foundation.
+