nps: poci/nws_poci.c - added call_command function to eliminate th...

baggins baggins at pld-linux.org
Wed Sep 14 00:51:53 CEST 2005


Author: baggins                      Date: Tue Sep 13 22:51:53 2005 GMT
Module: nps                           Tag: HEAD
---- Log message:
- added call_command function to eliminate the use of popen
- cleane up call_iproute function

---- Files affected:
nps/poci:
   nws_poci.c (1.13 -> 1.14) 

---- Diffs:

================================================================
Index: nps/poci/nws_poci.c
diff -u nps/poci/nws_poci.c:1.13 nps/poci/nws_poci.c:1.14
--- nps/poci/nws_poci.c:1.13	Tue Sep 13 18:50:07 2005
+++ nps/poci/nws_poci.c	Wed Sep 14 00:51:48 2005
@@ -444,7 +444,7 @@
 			}
 			break;
 		default:
-			return -1;
+			return -EINVAL;
 	}
 	if (outbuf != NULL) {
 		*outbuf = malloc(1024);
@@ -452,10 +452,9 @@
 			outbuf = NULL;
 	}
 	if (pipe (ippipe) == -1) {
-		fprintf(stderr, "pipe sie nie udal\n");
 		if (outbuf != NULL)
 			free(*outbuf);
-		return -1;
+		return -EPIPE;
 	}
 	pid = fork();
 	if (pid < 0) {
@@ -463,7 +462,7 @@
 			free(*outbuf);
 		close(ippipe[0]);
 		close(ippipe[1]);
-		return -1;
+		return -errno;
 	}
 	if (pid == 0) {
 		close(0);
@@ -471,39 +470,115 @@
 		close(2);
 		open("/dev/null", O_RDONLY);	// redirect stdin to /dev/null
 		dup(0);				// stdout too
-		dup(ippipe[1]);			// and stderr to a pipe
+		if (outbuf != NULL)
+			dup(ippipe[1]);		// and stderr to a pipe
+		else
+			dup(0);
 		close(ippipe[0]);
 		close(ippipe[1]);
 		execv(program, argv);
-	} else {
-		if (outbuf != NULL) {
-			size = 1024;
-			freesp = 1024;
-			tmpbuf = *outbuf;
-			while ((i = read(ippipe[0], tmpbuf, freesp)) > 0) {
-				freesp -= i;
-				tmpbuf += i;
-				if (freesp == 0) {
-					// unlikely
-					tmpbuf = realloc(*outbuf, size + 1024);
-					if (tmpbuf == NULL)
-						break;
-					*outbuf = tmpbuf;
-					tmpbuf += size;
-					size += 1024;
-				}
+	}
+	if (outbuf != NULL) {
+		size = 1024;
+		freesp = 1024;
+		tmpbuf = *outbuf;
+		while ((i = read(ippipe[0], tmpbuf, freesp)) > 0) {
+			freesp -= i;
+			tmpbuf += i;
+			if (freesp == 0) {
+				// unlikely
+				tmpbuf = realloc(*outbuf, size + 1024);
+				if (tmpbuf == NULL)
+					break;
+				*outbuf = tmpbuf;
+				tmpbuf += size;
+				size += 1024;
 			}
-			*tmpbuf = '\0';
 		}
-		waitpid(pid, &status, 0);
-		close(ippipe[0]);
-		close(ippipe[1]);
-		if (WIFEXITED(status))
-			return WEXITSTATUS(status);
-		else
-			return -1;
+		*tmpbuf = '\0';
+	}
+	waitpid(pid, &status, 0);
+	close(ippipe[0]);
+	close(ippipe[1]);
+	if (WIFEXITED(status))
+		return WEXITSTATUS(status);
+	else if (WIFSIGNALED(status))
+		return WTERMSIG(status);
+	else
+		return -ECHILD;
+}
+
+int call_command(char *program, char *args, char **outbuf)
+{
+	char *tmpbuf;
+	char *argv[16];
+	pid_t pid;
+	int status, i, size, freesp, cmdpipe[2];
+
+	i = 1;
+	argv[0] = program;
+	if (args == NULL)
+		tmpbuf = NULL;
+	else
+		tmpbuf = strtok(args, "\t ");
+	while ((tmpbuf != NULL) && (i < 16)) {
+		argv[i++] = tmpbuf;
+		tmpbuf = strtok(NULL, "\t ");
+	}
+	argv[i] = NULL;
+
+	*outbuf = malloc(1024);
+	if (*outbuf == NULL)
+		return -ENOMEM;
+
+	if (pipe(cmdpipe) == -1) {
+		free(*outbuf);
+		return -EPIPE;
+	}
+	pid = fork();
+	if (pid < 0) {
+		free(*outbuf);
+		close(cmdpipe[0]);
+		close(cmdpipe[1]);
+		return -errno;
+	}
+	if (pid == 0) {
+		close(0);
+		close(1);
+		close(2);
+		open("/dev/null", O_RDONLY);	// redirect stdin to /dev/null
+		dup(cmdpipe[1]);		// stdout
+		dup(cmdpipe[1]);		// and stderr to a pipe
+		close(cmdpipe[0]);
+		close(cmdpipe[1]);
+		execv(program, argv);
+	}
+	size = 1024;
+	freesp = 1024;
+	tmpbuf = *outbuf;
+	while ((i = read(cmdpipe[0], tmpbuf, freesp)) > 0) {
+		freesp -= i;
+		tmpbuf += i;
+		if (freesp == 0) {
+			// unlikely
+			tmpbuf = realloc(*outbuf, size + 1024);
+			if (tmpbuf == NULL)
+				break;
+			*outbuf = tmpbuf;
+			tmpbuf += size;
+			size += 1024;
+		}
 	}
-	return 0;
+	*tmpbuf = '\0';
+	waitpid(pid, &status, 0);
+	close(cmdpipe[0]);
+	close(cmdpipe[1]);
+	if (WIFEXITED(status))
+		return WEXITSTATUS(status);
+	else if (WIFSIGNALED(status))
+		return WTERMSIG(status);
+	else
+		return -ECHILD;
 }
 
 /*
@@ -1253,25 +1328,23 @@
 **********************************************************************/
 void orderly_shutdown(char *outBuf)
 {
-    FILE *fpin;
     int val;
-    char buf[BUFSIZE], errMsg[BUFSIZE];
+    char buf[BUFSIZE];
+    char *errbuf;
 
     printStart(outBuf);  //print the XML header return message
-    if( (fpin = popen("/sbin/shutdown -h now 2>&1","r")) == NULL) {
+
+    val = call_command("/sbin/shutdown", "-h now", &errbuf);
+    if (val < 0) {
         snprintf(buf,BUFSIZE,
             "<error>Unable to open pipe to shutdown command</error>");
         syslog(LOG_CRIT, buf);
         strncat(outBuf, buf, AVAIL(outBuf));
-        val = -EPIPE;
-    } else {
-        fgets(errMsg, BUFSIZE, fpin);
-        val = pclose(fpin);
-        if(val != 0) {  // an error in shuting down
-            snprintf(buf, BUFSIZE, "<error>%s</error>",errMsg);
-            strncat(outBuf, errMsg, AVAIL(outBuf));
-        }
+    } else if (val > 0) { // an error in shuting down
+        snprintf(buf, BUFSIZE, "<error>%s</error>",errbuf);
+        strncat(outBuf, buf, AVAIL(outBuf));
     }
+
     snprintf(buf,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>",val);
     strncat(outBuf, buf, AVAIL(outBuf));
     printEnd(outBuf);
@@ -1288,24 +1361,20 @@
 **********************************************************************/
 void forced_shutdown(char *outBuf)
 {
-    FILE *fpin;
     int val;
-    char buf[BUFSIZE], errMsg[BUFSIZE];
+    char buf[BUFSIZE];
+    char *errbuf;
 
     printStart(outBuf);  //print the XML header return message
-    if( (fpin = popen("/sbin/init 0 2>&1","r")) == NULL) {
-        snprintf(buf,BUFSIZE, 
+    val = call_command("/sbin/init", "0", &errbuf);
+    if (val < 0) {
+        snprintf(buf,BUFSIZE,
             "<error>Unable to open pipe to shutdown command</error>");
         syslog(LOG_CRIT, buf);
         strncat(outBuf, buf, AVAIL(outBuf));
-        val = -EPIPE;
-    } else {
-        fgets(errMsg, BUFSIZE, fpin);
-        val = pclose(fpin);
-        if(val != 0) {  // an error in shuting down
-            snprintf(buf, BUFSIZE, "<error>%s</error>",errMsg);
-            strncat(outBuf, errMsg, AVAIL(outBuf));
-        }
+    } else if (val > 0) { // an error in shuting down
+        snprintf(buf, BUFSIZE, "<error>%s</error>",errbuf);
+        strncat(outBuf, buf, AVAIL(outBuf));
     }
     snprintf(buf,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>",val);
     strncat(outBuf, buf, AVAIL(outBuf));
@@ -1323,24 +1392,20 @@
 **********************************************************************/
 void reboot(char *outBuf)
 {
-    FILE *fpin;
     int val;
-    char buf[BUFSIZE], errMsg[BUFSIZE];
+    char buf[BUFSIZE];
+    char *errbuf;
 
     printStart(outBuf);  //print the XML header return message
-    if( (fpin = popen("/sbin/reboot 2>&1","r")) == NULL) {
-        snprintf(buf,BUFSIZE, 
+    val = call_command("/sbin/reboot", NULL, &errbuf);
+    if (val < 0) {
+        snprintf(buf,BUFSIZE,
             "<error>Unable to open pipe to shutdown command</error>");
         syslog(LOG_CRIT, buf);
-        strncat(outBuf, buf, AVAIL(buf));
-        val = -EPIPE;
-    } else {
-        fgets(errMsg, BUFSIZE, fpin);
-        val = pclose(fpin);
-        if(val != 0) {  // an error in shuting down
-            snprintf(buf, BUFSIZE, "<error>%s</error>",errMsg);
-            strncat(outBuf, errMsg, AVAIL(outBuf));
-        }
+        strncat(outBuf, buf, AVAIL(outBuf));
+    } else if (val > 0) { // an error in shuting down
+        snprintf(buf, BUFSIZE, "<error>%s</error>",errbuf);
+        strncat(outBuf, buf, AVAIL(outBuf));
     }
     snprintf(buf,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>",val);
     strncat(outBuf, buf, AVAIL(outBuf));
@@ -1358,24 +1423,20 @@
 **********************************************************************/
 void forced_reboot(char *outBuf)
 {
-    FILE *fpin;
     int val;
-    char buf[BUFSIZE], errMsg[BUFSIZE];
+    char buf[BUFSIZE];
+    char *errbuf;
 
     printStart(outBuf);  //print the XML header return message
-    if( (fpin = popen("/sbin/reboot -f 2>&1","r")) == NULL) {
+    val = call_command("/sbin/reboot", "-f", &errbuf);
+    if (val < 0) {
         snprintf(buf,BUFSIZE,
             "<error>Unable to open pipe to shutdown command</error>");
         syslog(LOG_CRIT, buf);
         strncat(outBuf, buf, AVAIL(outBuf));
-        val = -EPIPE;
-    } else {
-        fgets(buf, BUFSIZE, fpin);
-        val = pclose(fpin);
-        if(val != 0) {  // an error in shuting down
-            snprintf(buf, BUFSIZE, "<error>%s</error>",errMsg);
-            strncat(outBuf, errMsg, AVAIL(outBuf));
-        }
+    } else if (val > 0) { // an error in shuting down
+        snprintf(buf, BUFSIZE, "<error>%s</error>",errbuf);
+        strncat(outBuf, buf, AVAIL(outBuf));
     }
     snprintf(buf,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>",val);
     strncat(outBuf, buf, AVAIL(outBuf));
@@ -1480,8 +1541,11 @@
     snprintf(command,BUFSIZE,"<cmdRetVal>%d</cmdRetVal>\n",val);
     strncat(outBuf, command, AVAIL(outBuf));
 
-    if (val != 0) {
-        printLog(outBuf, errbuf); //print the XML error log for return msg
+    if (errbuf != NULL) {
+	if (val != 0) {
+	    printLog(outBuf, errbuf); //print the XML error log for return msg
+	}
+	free(errbuf);
     }
     printEnd(outBuf);   //print the XML trailer for return message
     return;
================================================================

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




More information about the pld-cvs-commit mailing list