nps: poci/nws_poci.c - add call_iproute2 function to eliminate the...

baggins baggins at pld-linux.org
Tue Sep 13 18:06:02 CEST 2005


Author: baggins                      Date: Tue Sep 13 16:06:02 2005 GMT
Module: nps                           Tag: HEAD
---- Log message:
- add call_iproute2 function to eliminate the use of system(3)

---- Files affected:
nps/poci:
   nws_poci.c (1.10 -> 1.11) 

---- Diffs:

================================================================
Index: nps/poci/nws_poci.c
diff -u nps/poci/nws_poci.c:1.10 nps/poci/nws_poci.c:1.11
--- nps/poci/nws_poci.c:1.10	Tue Sep 13 13:32:47 2005
+++ nps/poci/nws_poci.c	Tue Sep 13 18:05:57 2005
@@ -398,38 +398,121 @@
     return;
 }
 
-int call_iproute2(int command, char *iface, char *pl_ip, char *sp_ip, int prefix, char *logfile)
+int call_iproute2(int command, char *iface, char *pl_ip, char *sp_ip, int prefix, char **outbuf)
 {
-	char *const *argv;
+	char *tmpbuf;
+	char *argv[16];
+	char ip_pref[32];
 	pid_t pid;
-	int status;
-
+	int status, i, size, ippipe[2];
+	static char program[] = "/sbin/ip";
+	static char comm_addr[] = "address";
+	static char comm_show[] = "show";
+	static char comm_flush[] = "flush";
+	static char comm_add[] = "add";
+	static char comm_peer[] = "peer";
+	static char comm_dev[] = "dev";
+
+	for (i=0; i<16; i++)
+		argv[i] = NULL;
+
+	if (outbuf != NULL)
+		*outbuf = NULL;
+	argv[0] = program;
+	argv[1] = comm_addr;
 	switch (command) {
 		case IP_A_SHOW:
 		// /sbin/ip address show %s 2>%s
+			argv[2] = comm_show;
+			argv[3] = comm_dev;
+			argv[4] = iface;
 			break;
 		case IP_A_FLUSH:
 		// /sbin/ip address flush jnet0
+			argv[2] = comm_flush;
+			argv[3] = comm_dev;
+			argv[4] = iface;
 			break;
 		case IP_A_ADD:
-		// /sbin/ip address add %s peer %s dev jnet0
-		// /sbin/ip address add %s peer %s dev jnet0
-		// /sbin/ip address add %s dev %s 2>%s
-		// /sbin/ip address add %s/%d dev %s 2>%s
-		// /sbin/ip address add %s/%d dev %s 2>%s
+			argv[2] = comm_add;
+			if (prefix >= 0) {
+				// /sbin/ip address add %s/%d dev %s 2>%s
+				// /sbin/ip address add %s/%d dev %s 2>%s
+				snprintf(ip_pref, 32, "%s/%d", pl_ip, prefix);
+				argv[3] = ip_pref;
+				argv[4] = comm_dev;
+				argv[5] = iface;
+			} else if (sp_ip != NULL) {
+				// /sbin/ip address add %s peer %s dev jnet0
+				// /sbin/ip address add %s peer %s dev jnet0
+				argv[3] = pl_ip;
+				argv[4] = comm_peer;
+				argv[5] = sp_ip;
+				argv[6] = comm_dev;
+				argv[7] = iface;
+			} else {
+				// /sbin/ip address add %s dev %s 2>%s
+				argv[3] = pl_ip;
+				argv[4] = comm_dev;
+				argv[5] = iface;
+			}
 			break;
 		default:
 			return -1;
 	}
-
+	if (outbuf != NULL) {
+		*outbuf = malloc(1024);
+		if (*outbuf == NULL)
+			return -1;
+	}
+	if (pipe (ippipe) == -1) {
+		fprintf(stderr, "pipe sie nie udal\n");
+		if (outbuf != NULL)
+			free(*outbuf);
+		return -1;
+	}
 	pid = fork();
-	if (pid < 0)
+	if (pid < 0) {
+		if (outbuf != NULL)
+			free(*outbuf);
+		close(ippipe[0]);
+		close(ippipe[1]);
 		return -1;
-
+	}
 	if (pid == 0) {
-//		execl("/sbin/ip", "ip", ...);
+		close(0);
+		close(1);
+		close(2);
+		open("/dev/null", O_RDONLY);	// redirect stdin to /dev/null
+		dup(0);				// stdout too
+		dup(ippipe[1]);			// and stderr to a pipe
+		close(ippipe[0]);
+		close(ippipe[1]);
+		execv(program, argv);
 	} else {
+		if (outbuf != NULL) {
+			size = 1024;
+			tmpbuf = *outbuf;
+			while ((i = read(ippipe[0], tmpbuf, size/2)) > 0) {
+				tmpbuf += i;
+				if ((tmpbuf - *outbuf) == size) {
+					// unlikely
+					*outbuf = realloc(*outbuf, size + 1024);
+					if (*outbuf == NULL)
+						break;
+					tmpbuf = *outbuf + size;
+					size += 1024;
+				}
+			}
+			*tmpbuf = '\0';
+		}
 		waitpid(pid, &status, 0);
+		close(ippipe[0]);
+		close(ippipe[1]);
+		if (WIFEXITED(status))
+			return WEXITSTATUS(status);
+		else
+			return -1;
 	}
 	return 0;
 }
@@ -982,8 +1065,10 @@
     strncpy(sp_addr, inet_ntoa(sp_ip), 16);
     snprintf(command,BUFSIZE,"/sbin/ip address flush jnet0 ; /sbin/ip address add %s peer %s dev jnet0", pl_addr, sp_addr);
 fprintf(stderr, "%s\n", command);
+    // we can ignore exit status of 'ip address flush jnet0'
+    call_iproute2(IP_A_FLUSH, "jnet0", NULL, NULL, -1, NULL);
+    rc = call_iproute2(IP_A_ADD, "jnet0", pl_addr, sp_addr, -1, NULL);
 
-    rc = system(command);
     if(rc != 0) {
         syslog(LOG_CRIT, "initialization of jnet0 device failed\n");
     }
================================================================

---- CVS-web:
    http://cvs.pld-linux.org/nps/poci/nws_poci.c?r1=1.10&r2=1.11&f=u




More information about the pld-cvs-commit mailing list