nps: poci/nws_poci.c - switched all calls to iproute2 from system(...

baggins baggins at pld-linux.org
Tue Sep 13 18:45:40 CEST 2005


Author: baggins                      Date: Tue Sep 13 16:45:39 2005 GMT
Module: nps                           Tag: HEAD
---- Log message:
- switched all calls to iproute2 from system(3) to fork/exec
- fixed memory allocation logic in call_iproute2

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

---- Diffs:

================================================================
Index: nps/poci/nws_poci.c
diff -u nps/poci/nws_poci.c:1.11 nps/poci/nws_poci.c:1.12
--- nps/poci/nws_poci.c:1.11	Tue Sep 13 18:05:57 2005
+++ nps/poci/nws_poci.c	Tue Sep 13 18:45:33 2005
@@ -373,26 +373,11 @@
 Purpose:    To output the contents of the error log to the user through
 the socket.
 **********************************************/
-void printLog(char *outBuf, char *logFileName) {
-    FILE *outFile;
-    char buf[BUFSIZE];
-
+void printLog(char *outBuf, char *errlog)
+{
     strncat(outBuf, "<error>", AVAIL(outBuf));
-
-    if((outFile=fopen(logFileName,"r"))!=NULL) {
-        while(!feof(outFile)) {
-            if( fscanf(outFile,"%255s",buf) != 1) {
-                break;
-            }
-            strncat(outBuf, buf, AVAIL(outBuf));
-            strncat(outBuf, " ", AVAIL(outBuf));
-
-        }
-        fclose(outFile);
-        if( remove(logFileName) != 0) {
-            syslog(LOG_INFO, "unable to remove log file\n");
-        }
-    }
+    if (errlog != NULL)
+	strncat(outBuf, errlog, AVAIL(outBuf));
     strncat(outBuf, "</error> ", AVAIL(outBuf));
 
     return;
@@ -404,7 +389,7 @@
 	char *argv[16];
 	char ip_pref[32];
 	pid_t pid;
-	int status, i, size, ippipe[2];
+	int status, i, size, freesp, ippipe[2];
 	static char program[] = "/sbin/ip";
 	static char comm_addr[] = "address";
 	static char comm_show[] = "show";
@@ -463,7 +448,7 @@
 	if (outbuf != NULL) {
 		*outbuf = malloc(1024);
 		if (*outbuf == NULL)
-			return -1;
+			outbuf = NULL;
 	}
 	if (pipe (ippipe) == -1) {
 		fprintf(stderr, "pipe sie nie udal\n");
@@ -492,15 +477,18 @@
 	} else {
 		if (outbuf != NULL) {
 			size = 1024;
+			freesp = 1024;
 			tmpbuf = *outbuf;
-			while ((i = read(ippipe[0], tmpbuf, size/2)) > 0) {
+			while ((i = read(ippipe[0], tmpbuf, freesp)) > 0) {
+				freesp -= i;
 				tmpbuf += i;
-				if ((tmpbuf - *outbuf) == size) {
+				if (freesp == 0) {
 					// unlikely
-					*outbuf = realloc(*outbuf, size + 1024);
-					if (*outbuf == NULL)
+					tmpbuf = realloc(*outbuf, size + 1024);
+					if (tmpbuf == NULL)
 						break;
-					tmpbuf = *outbuf + size;
+					*outbuf = tmpbuf;
+					tmpbuf += size;
 					size += 1024;
 				}
 			}
@@ -1033,7 +1021,6 @@
     char pl_addr[16], sp_addr[16];
     unsigned long platIp, spIp;
     char *drivername = JNET_DRIVERNAME;
-    char command[BUFSIZE]; 
     char errMsg[BUFSIZE];
 
     //now call jnet, and tell it.
@@ -1063,10 +1050,10 @@
     sp_ip.s_addr = ntohl(spIp);
     strncpy(pl_addr, inet_ntoa(pl_ip), 16);
     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'
+    //	/sbin/ip address flush jnet0
     call_iproute2(IP_A_FLUSH, "jnet0", NULL, NULL, -1, NULL);
+    //	/sbin/ip address add 'pl_addr' peer 'sp_addr' dev jnet0
     rc = call_iproute2(IP_A_ADD, "jnet0", pl_addr, sp_addr, -1, NULL);
 
     if(rc != 0) {
@@ -1413,6 +1400,7 @@
     char tokensep[] =" \t,";
     char command[BUFSIZE];
     char logFileName[BUFSIZE] = ERROR_FILE_TEMPLATE;
+    char *errbuf;
 
     printStart(outBuf);  //print the XML header return message
     //initialize params
@@ -1461,39 +1449,38 @@
     //the appropriate fields
     switch(i) {
     case 2:
-	snprintf(command,BUFSIZE,
-			"/sbin/ip address add %s dev %s 2>%s",
-			param[2],param[1], logFileName);
-fprintf(stderr, "2: %s\n", command);
-        break;
+	    cleanse(param[1]);
+	    cleanse(param[2]);
+	    // /sbin/ip address add 'param[2]' dev 'param[1]' 2>%s
+	    val = call_iproute2(IP_A_ADD, param[1], param[2], NULL, -1, &errbuf);
+	    break;
     case 3:
-	pref = netmask_to_prefix(param[3]);
-	snprintf(command,BUFSIZE,
-			"/sbin/ip address add %s/%d dev %s 2>%s",
-			param[2],pref,param[1], logFileName);
-fprintf(stderr, "3: %s\n", command);
+	    cleanse(param[1]);
+	    cleanse(param[2]);
+	    cleanse(param[3]);
+	    pref = netmask_to_prefix(param[3]);
+	    // /sbin/ip address add 'param[2]/pref' dev 'param[1]' 2>%s",
+	    val = call_iproute2(IP_A_ADD, param[1], param[2], NULL, pref, &errbuf);
         break;
     case 4:
-	pref = netmask_to_prefix(param[3]);
-	snprintf(command,BUFSIZE,
-			"/sbin/ip address add %s/%d dev %s 2>%s",
-			param[2],pref,param[1], logFileName);
-fprintf(stderr, "4: %s\n", command);
+	    cleanse(param[1]);
+	    cleanse(param[2]);
+	    cleanse(param[3]);
+	    pref = netmask_to_prefix(param[3]);
+	    // /sbin/ip address add 'param[2],pref' dev 'param[1]' 2>%s",
+	    val = call_iproute2(IP_A_ADD, param[1], param[2], NULL, pref, &errbuf);
         break;
     default:
-	snprintf(command,BUFSIZE,
-			"/sbin/ip address show %s 2>%s",
-			param[0], logFileName);
-fprintf(stderr, "def: %s\n", command);
+	    cleanse(param[0]);
+	    // /sbin/ip address show 'param[0]' 2>%s
+	    val = call_iproute2(IP_A_SHOW, param[0], NULL, NULL, -1, &errbuf);
         break;
     }
-    cleanse(command);   //remove non alpha-numeric text
-    val=system(command);  //now execute the command
     snprintf(command,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>\n",val);
     strncat(outBuf, command, AVAIL(outBuf));
 
-    if(val != 0) {
-        printLog(outBuf, logFileName); //print the XML error log for return msg
+    if (val != 0) {
+        printLog(outBuf, errbuf); //print the XML error log for return msg
     }
     printEnd(outBuf);   //print the XML trailer for return message
     return;
@@ -1787,7 +1774,7 @@
     unsigned char *platIp, *spIp;
     char command[BUFSIZE], errMsg[BUFSIZE];
     int jnetChanged = FALSE;
-    int fd, fd1;
+    int fd, fd1, rc;
     uint32_t pip;
     struct in_addr inp;
 
@@ -1848,11 +1835,11 @@
 	close(fd1);
 
         //now bring restart the network at this new address.
-	snprintf(command,BUFSIZE,"/sbin/ip address flush jnet0 ;"
-				 "/sbin/ip address add %s peer %s dev jnet0",
-				 platIp, spIp);
-fprintf(stderr, "set: %s\n", command);
-        if( system(command) != 0) {
+	//	/sbin/ip address flush jnet0
+	call_iproute2(IP_A_FLUSH, "jnet0", NULL, NULL, -1, NULL);
+	//	/sbin/ip address add 'platIp' peer 'spIp' dev jnet0
+	rc = call_iproute2(IP_A_ADD, "jnet0", platIp, spIp, -1, NULL);
+        if(rc != 0) {
             syslog(LOG_CRIT, "Jnet set ip failed\n");
             //Even though the change may have failed, we will return
             //that it succeeded so the parent loop will re-establish
================================================================

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




More information about the pld-cvs-commit mailing list