[svn-commits] tzafrir: branch tools/tzafrir/sysfs r8699 - /tools/team/tzafrir/sysfs/

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu May 27 05:50:18 CDT 2010


Author: tzafrir
Date: Thu May 27 05:50:16 2010
New Revision: 8699

URL: http://svnview.digium.com/svn/dahdi?view=rev&rev=8699
Log:
dahdi_cfg can run on single span:

* Add option '-S <spannum>'
* With this option, a configuration of non-existing channels fails silently.
* Move str_replace() upward in source file.
* Calculate and check existance of prefix in entrance to apply_channels()
  so we detect missing links as early as possible. As a result the
  span_string2num() function now receive an absolute path name.

Modified:
    tools/team/tzafrir/sysfs/dahdi_cfg.c

Modified: tools/team/tzafrir/sysfs/dahdi_cfg.c
URL: http://svnview.digium.com/svn/dahdi/tools/team/tzafrir/sysfs/dahdi_cfg.c?view=diff&rev=8699&r1=8698&r2=8699
==============================================================================
--- tools/team/tzafrir/sysfs/dahdi_cfg.c (original)
+++ tools/team/tzafrir/sysfs/dahdi_cfg.c Thu May 27 05:50:16 2010
@@ -111,6 +111,8 @@
 static int showpaths = 0;
 
 static int keep_going = 0;
+
+static int only_span_num = 0;
 
 static int numdynamic = 0;
 
@@ -188,6 +190,11 @@
 	}
 }
 
+static int skip_span(int x)
+{
+	return only_span_num && only_span_num != x + 1;
+}
+
 int ind_ioctl(int channo, int fd, int op, void *data)
 {
 	struct dahdi_indirect_data ind;
@@ -315,14 +322,25 @@
 	return 0;
 }
 
+static void str_replace(char *str, int char1, int char2)
+{
+	char *p;
+
+	for(p = str; *p; p++)
+		if(*p == char1)
+			*p = char2;
+}
+
 int span_string2num(const char *span_string)
 {
 	char		path[PATH_MAX];
 	struct stat	stbuf;
-	char		spanstr[PATH_MAX];
+	char		tmppath[PATH_MAX];
 	int		span;
 
-	snprintf(path, sizeof(path), "/dev/dahdi/%s", span_string);
+	dahdi_copy_string(tmppath, span_string, sizeof(tmppath));
+	str_replace(tmppath, '!', '/');
+	snprintf(path, sizeof(path), "/dev/dahdi/%s", tmppath);
 	if (lstat(path, &stbuf) >= 0) {
 		int	n;
 		char	*tmp;
@@ -331,24 +349,26 @@
 			error("span '%s' (%s) is not a symlink\n", span_string, path);
 			return -1;
 		}
-		n = readlink(path, spanstr, sizeof(spanstr));
+		n = readlink(path, tmppath, sizeof(tmppath));
 		if (n < 0) {
 			error("failed reading symlink '%s': %s\n", path, strerror(errno));
 			return -1;
 		}
-		spanstr[n] = '\0';
-		if((tmp = strrchr(spanstr, '/')) != NULL) {
+		tmppath[n] = '\0';
+		if((tmp = strrchr(tmppath, '/')) != NULL) {
 			tmp++;	/* skip the last slash */
 		} else {
-			tmp = spanstr;
+			tmp = tmppath;
 		}
 		if (debug & DEBUG_APPLY) {
-			printf("%s: map '%s' to '%s'\n", __func__, spanstr, tmp);
+			printf("%s: map '%s' to '%s'\n", __func__, tmppath, tmp);
+			fflush(stdout);
 		}
 		span_string = tmp;
 	} else {
 		if (debug & DEBUG_APPLY) {
 			printf("Failed lstat(%s): %s\n", path, strerror(errno));
+			fflush(stdout);
 		}
 	}
 	if (sscanf(span_string, "%d", &span) != 1) {
@@ -359,9 +379,11 @@
 			return -1;
 		}
 	}
-	if(span_string == spanstr) {
+	/* Compare addresses (modified or not) */
+	if(span_string == tmppath) {
 		span_names[span] = strdup(path);
 		printf("%s: %d -> %s\n", __func__, span, span_names[span]);
+		fflush(stdout);
 	}
 	return span;
 }
@@ -459,16 +481,16 @@
 	return 0;
 }
 
-static int path2channo(const char *relative_path, int relative_index)
+static int path2channo(const char *prefix, int relative_index)
 {
 	char		path[PATH_MAX];
 	struct stat	stbuf;
 	int		channo;
 
-	if(!relative_path)
+	if(!prefix)
 		return relative_index;	/* identify map */
-	//fprintf(stderr, "DEBUG(%s): %s!%d\n", __func__, relative_path, relative_index);
-	snprintf(path, sizeof(path), "/dev/dahdi/%s/%d", relative_path, relative_index);
+	//fprintf(stderr, "DEBUG(%s): %s!%d\n", __func__, prefix, relative_index);
+	snprintf(path, sizeof(path), "%s/%d", prefix, relative_index);
 	if (stat(path, &stbuf) < 0) {
 		perror(path);
 		return (keep_going) ? 0 : -1;
@@ -480,15 +502,6 @@
 	channo = minor(stbuf.st_rdev);
 	path_channels++;
 	return channo;
-}
-
-static void str_replace(char *str, int char1, int char2)
-{
-	char *p;
-
-	for(p = str; *p; p++)
-		if(*p == char1)
-			*p = char2;
 }
 
 int apply_channels(int chans[], char *argstr)
@@ -502,13 +515,19 @@
 	char *pname;
 	char prefix[PATH_MAX];
 
-	dahdi_copy_string(prefix, argstr, sizeof(prefix));
+	snprintf(prefix, sizeof(prefix), "/dev/dahdi/%s", argstr);
 	pname = strrchr(prefix, '!');
 	if (pname) {
 		*pname = '\0';
 		argstr = pname + 1;
 		str_replace(prefix, '!', '/');
 		pname = prefix;
+		if (access(pname, R_OK | X_OK) < 0) {
+			if (only_span_num)
+				return 0;
+			error("Missing '%s'\n", pname);
+			return -1;
+		}
 	}
 	res = parseargs(argstr, args, DAHDI_MAX_CHANNELS, ',');
 	if (res < 0) {
@@ -547,7 +566,7 @@
 			for (y=start;y<=finish;y++) {
 				int realchan = path2channo(pname, y);
 				if (realchan < 0) {
-					error("Could not resolve '%s' to valid channel\n", pname);
+					error("Could not resolve '%s/%d' to valid channel\n", pname, y);
 					return -1;
 				}
 				if (realchan == 0)
@@ -571,7 +590,7 @@
 			}
 			realchan = path2channo(pname, chan);
 			if (realchan < 0) {
-				error("Could not resolve '%s' to valid channel\n", pname);
+				error("Could not resolve '%s/%d' to valid channel\n", pname, chan);
 				return -1;
 			}
 			if (realchan == 0)
@@ -1355,6 +1374,8 @@
 	       "Configuration\n"
 	       "======================\n\n", vi.version, vi.echo_canceller);
 	for (x = 0; x < spans; x++) {
+		if (skip_span(x))
+			continue;
 		printf("SPAN %d: %3s/%4s Build-out: %s\n",
 		       lc[x].span,
 		       (lc[x].lineconfig & DAHDI_CONFIG_D4 ? "D4" :
@@ -1514,7 +1535,7 @@
 	char *key, *value;
 	int x,found;
 
-	while((c = getopt(argc, argv, "fthc:vsd::pk")) != -1) {
+	while((c = getopt(argc, argv, "fthc:vsd::pkS:")) != -1) {
 		switch(c) {
 		case 'c':
 			filename=optarg;
@@ -1549,6 +1570,9 @@
 		case 'k':
 			keep_going = 1;
 			break;
+		case 'S':
+			only_span_num = atoi(optarg);
+			break;
 		}
 	}
 	
@@ -1625,6 +1649,8 @@
 	}
 	if (stopmode) {
 		for (x=0;x<spans;x++) {
+			if (skip_span(x))
+				continue;
 			if (ioctl(fd, DAHDI_SHUTDOWN, &lc[x].span)) {
 				fprintf(stderr, "DAHDI shutdown failed: %s\n", strerror(errno));
 				close(fd);
@@ -1634,6 +1660,8 @@
 		exit(1);
 	}
 	for (x=0;x<spans;x++) {
+		if (skip_span(x))
+			continue;
 		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);
@@ -1756,6 +1784,8 @@
 		}
 	}
 	for (x=0;x<spans;x++) {
+		if (skip_span(x))
+			continue;
 		if (ioctl(fd, DAHDI_STARTUP, &lc[x].span)) {
 			fprintf(stderr, "DAHDI startup failed: %s\n", strerror(errno));
 			close(fd);




More information about the svn-commits mailing list