[dahdi-commits] dahdi/tools.git branch "master" updated.

SVN commits to the DAHDI project dahdi-commits at lists.digium.com
Wed Jan 22 12:59:49 CST 2014


branch "master" has been updated
       via  69ce6f07e0842e1dcb8b18f19fd3d61f577d3022 (commit)
       via  9989b8779cef3c243ee58e273c0b2709fb11e31b (commit)
      from  a4f79134c959830bfcb869068cd1cf4891f00527 (commit)

Summary of changes:
 Makefile    |    2 +-
 dahdi_cfg.c |  114 +++++++++++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 88 insertions(+), 28 deletions(-)


- Log -----------------------------------------------------------------
commit 69ce6f07e0842e1dcb8b18f19fd3d61f577d3022
Author: Shaun Ruffell <sruffell at digium.com>
Date:   Wed Jan 8 14:11:03 2014 -0600

    dahdi_cfg: Allow dynamic spans to handle udev based span assignment.
    
    Prior to this commit, if auto_assign_spans was set to 0, it was possible to
    get in an endless loop creating and destroying spans. The primary reason was
    that all dynamic spans are destroyed and recreated each time dahdi_cfg runs, BUT
    dahdi_cfg was run each time a new span showed up in udev when auto_assign_spans
    was set to 0.
    
    Now dahdi_cfg will only destroy and recreate dynamic spans if dahdi_cfg is run
    without a specifc span number. Also, while creating dynamic spans, dahdi_cfg
    will pause for up to one second for the span to be assigned in order to ensure
    that the spans are numbered consistently when auto span assignment is
    configured.
    
    Signed-off-by: Shaun Ruffell <sruffell at digium.com>
    Signed-off-by: Russ Meyerriecks <rmeyerriecks at digium.com>

diff --git a/dahdi_cfg.c b/dahdi_cfg.c
index c86eaac..4e9cc9e 100644
--- a/dahdi_cfg.c
+++ b/dahdi_cfg.c
@@ -219,6 +219,19 @@ static bool are_all_spans_assigned(void)
 	return res;
 }
 
+static bool wait_for_all_spans_assigned(unsigned long timeout_sec)
+{
+	bool all_assigned = are_all_spans_assigned();
+	unsigned int timeout = 10*timeout_sec;
+
+	while (!all_assigned && --timeout) {
+		usleep(100000);
+		all_assigned = are_all_spans_assigned();
+	}
+
+	return all_assigned;
+}
+
 static const char *sigtype_to_str(const int sig)
 {
 	switch (sig) {
@@ -1582,15 +1595,9 @@ int main(int argc, char *argv[])
 	}
 
 	if (!restrict_channels && !only_span) {
-		bool all_assigned = are_all_spans_assigned();
-		unsigned int timeout = 4*5; /* We'll wait 5 seconds */
-
-		while (!all_assigned && --timeout) {
-			usleep(250000);
-			all_assigned = are_all_spans_assigned();
-		}
+		bool all_assigned = wait_for_all_spans_assigned(5);
 
-		if (0 == timeout) {
+		if (!all_assigned) {
 			fprintf(stderr,
 				"Timeout waiting for all spans to be assigned.\n");
 		}
@@ -1673,13 +1680,16 @@ finish:
 	if (-1 == sem_wait(lock)) {
 		error("Failed to wait for dahdi_cfg mutex.\n");
 		exit_code = 1;
-		goto release_sem;
+		goto unlink_sem;
 	}
 
-	for (x=0;x<numdynamic;x++) {
-		/* destroy them all */
-		ioctl(fd, DAHDI_DYNAMIC_DESTROY, &zds[x]);
+	if (!restrict_channels && !only_span) {
+		for (x=0;x<numdynamic;x++) {
+			/* destroy them all */
+			ioctl(fd, DAHDI_DYNAMIC_DESTROY, &zds[x]);
+		}
 	}
+
 	if (stopmode) {
 		for (x=0;x<spans;x++) {
 			if (only_span && lc[x].span != only_span)
@@ -1704,14 +1714,28 @@ finish:
 			goto release_sem;
 		}
 	}
-	for (x=0;x<numdynamic;x++) {
-		if (ioctl(fd, DAHDI_DYNAMIC_CREATE, &zds[x])) {
-			fprintf(stderr, "DAHDI dynamic span creation failed: %s\n", strerror(errno));
-			close(fd);
+
+	if (!restrict_channels && !only_span) {
+
+		sem_post(lock);
+
+		for (x=0;x<numdynamic;x++) {
+			if (ioctl(fd, DAHDI_DYNAMIC_CREATE, &zds[x])) {
+				fprintf(stderr, "DAHDI dynamic span creation failed: %s\n", strerror(errno));
+				close(fd);
+				exit_code = 1;
+				goto release_sem;
+			}
+			wait_for_all_spans_assigned(1);
+		}
+
+		if (-1 == sem_wait(lock)) {
+			error("Failed to wait for dahdi_cfg mutex after creating dynamic spans.\n");
 			exit_code = 1;
-			goto release_sem;
+			goto unlink_sem;
 		}
 	}
+
 	for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
 		struct dahdi_params current_state;
 		int master;
@@ -1891,9 +1915,11 @@ finish:
 	exit_code = apply_fiftysix();
 
 release_sem:
-	if (SEM_FAILED != lock) {
+	if (SEM_FAILED != lock)
 		sem_post(lock);
+
+unlink_sem:
+	if (SEM_FAILED != lock)
 		sem_unlink(SEM_NAME);
-	}
 	exit(exit_code);
 }

commit 9989b8779cef3c243ee58e273c0b2709fb11e31b
Author: Shaun Ruffell <sruffell at digium.com>
Date:   Thu Jan 2 17:31:19 2014 -0600

    dahdi_cfg: Add semaphore to prevent parallel execution.
    
    When dahdi is configured for fully dynamic configuration on a device and
    span basis via sysfs and udev it is possible for multiple instances of
    dahdi_cfg to be run in parallel on different spans. If this happens it
    is possible to see errors on the console that tone zones are already
    registered since the check for the existence of a tone zone and the
    re-registering needs to be atomic.
    
    dahdi_cfg will now prevent itself from running in parallel.
    
    Signed-off-by: Shaun Ruffell <sruffell at digium.com>
    Signed-off-by: Russ Meyerriecks <rmeyerriecks at digium.com>

diff --git a/Makefile b/Makefile
index eea390e..144b292 100644
--- a/Makefile
+++ b/Makefile
@@ -168,7 +168,7 @@ $(LTZ_SO): $(LTZ_SO_OBJS)
 	$(CC) $(CFLAGS) -shared -Wl,-soname,$(LTZ_SO).$(LTZ_SO_MAJOR_VER).$(LTZ_SO_MINOR_VER) -o $@ $^ -lm
 
 dahdi_cfg: $(LTZ_A)
-dahdi_cfg: LIBS+=-lm
+dahdi_cfg: LIBS+=-lm -lpthread
 dahdi_pcap:
 	$(CC) $(CFLAGS) dahdi_pcap.c -lpcap -o $@ $<
 	
diff --git a/dahdi_cfg.c b/dahdi_cfg.c
index 0de1091..c86eaac 100644
--- a/dahdi_cfg.c
+++ b/dahdi_cfg.c
@@ -35,6 +35,8 @@
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <fcntl.h>
+#include <sys/stat.h>
+#include <semaphore.h>
 #include <errno.h>
 #include <dirent.h>
 #include <stdbool.h>
@@ -794,7 +796,7 @@ static int setfiftysixkhdlc(char *keyword, char *args)
 	return 0;
 }
 
-static void apply_fiftysix(void)
+static int apply_fiftysix(void)
 {
 	int x;
 	int rate;
@@ -808,7 +810,7 @@ static void apply_fiftysix(void)
 			fprintf(stderr, 
 			    "Couldn't open /dev/dahdi/channel: %s\n", 
 			    strerror(errno));
-			exit(-1);
+			return -1;	
 		}
 
 		if (ioctl(chanfd, DAHDI_SPECIFY, &x)) {
@@ -829,6 +831,7 @@ static void apply_fiftysix(void)
 		}
 		close(chanfd);
 	}
+	return 0;
 }
 
 static int setechocan(char *keyword, char *args)
@@ -1530,6 +1533,9 @@ int main(int argc, char *argv[])
 	char *buf;
 	char *key, *value;
 	int x,found;
+	sem_t *lock = SEM_FAILED;
+	const char *SEM_NAME = "dahdi_cfg";
+	int exit_code = 0;
 
 	while((c = getopt(argc, argv, "fthc:vsd::C:S:")) != -1) {
 		switch(c) {
@@ -1656,6 +1662,20 @@ finish:
 		printf("About to open Master device\n");
 		fflush(stdout);
 	}
+
+	lock = sem_open(SEM_NAME, O_CREAT, O_RDWR, 1);
+	if (SEM_FAILED == lock) {
+		error("Unable to create 'dahdi_cfg' mutex.\n");
+		exit_code = 1;
+		goto release_sem;
+	}
+
+	if (-1 == sem_wait(lock)) {
+		error("Failed to wait for dahdi_cfg mutex.\n");
+		exit_code = 1;
+		goto release_sem;
+	}
+
 	for (x=0;x<numdynamic;x++) {
 		/* destroy them all */
 		ioctl(fd, DAHDI_DYNAMIC_DESTROY, &zds[x]);
@@ -1667,10 +1687,12 @@ finish:
 			if (ioctl(fd, DAHDI_SHUTDOWN, &lc[x].span)) {
 				fprintf(stderr, "DAHDI shutdown failed: %s\n", strerror(errno));
 				close(fd);
-				exit(1);
+				exit_code = 1;
+				goto release_sem;
 			}
 		}
-		exit(1);
+		exit_code = 1;
+		goto release_sem;
 	}
 	for (x=0;x<spans;x++) {
 		if (only_span && lc[x].span != only_span)
@@ -1678,14 +1700,16 @@ finish:
 		if (ioctl(fd, DAHDI_SPANCONFIG, lc + x)) {
 			fprintf(stderr, "DAHDI_SPANCONFIG failed on span %d: %s (%d)\n", lc[x].span, strerror(errno), errno);
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 	}
 	for (x=0;x<numdynamic;x++) {
 		if (ioctl(fd, DAHDI_DYNAMIC_CREATE, &zds[x])) {
 			fprintf(stderr, "DAHDI dynamic span creation failed: %s\n", strerror(errno));
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 	}
 	for (x=1;x<DAHDI_MAX_CHANNELS;x++) {
@@ -1810,7 +1834,8 @@ finish:
 					" to channel 16 of an E1 CAS span\n");
 			}
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 
 		ae[x].chan = x;
@@ -1821,7 +1846,8 @@ finish:
 		if (ioctl(fd, DAHDI_ATTACH_ECHOCAN, &ae[x])) {
 			fprintf(stderr, "DAHDI_ATTACH_ECHOCAN failed on channel %d: %s (%d)\n", x, strerror(errno), errno);
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 	}
 	if (0 == numzones) {
@@ -1848,7 +1874,8 @@ finish:
 		if (ioctl(fd, DAHDI_DEFAULTZONE, &deftonezone)) {
 			fprintf(stderr, "DAHDI_DEFAULTZONE failed: %s (%d)\n", strerror(errno), errno);
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 	}
 	for (x=0;x<spans;x++) {
@@ -1857,9 +1884,16 @@ finish:
 		if (ioctl(fd, DAHDI_STARTUP, &lc[x].span)) {
 			fprintf(stderr, "DAHDI startup failed: %s\n", strerror(errno));
 			close(fd);
-			exit(1);
+			exit_code = 1;
+			goto release_sem;
 		}
 	}
-	apply_fiftysix();
-	exit(0);
+	exit_code = apply_fiftysix();
+
+release_sem:
+	if (SEM_FAILED != lock) {
+		sem_post(lock);
+		sem_unlink(SEM_NAME);
+	}
+	exit(exit_code);
 }

-----------------------------------------------------------------------


-- 
dahdi/tools.git



More information about the dahdi-commits mailing list