PERFORCE change 176828 for review

Garrett Cooper gcooper at FreeBSD.org
Mon Apr 12 10:26:48 UTC 2010


http://p4web.freebsd.org/@@176828?ac=10

Change 176828 by gcooper at gcooper-bayonetta on 2010/04/12 10:26:28

	Convert fetch(3) call over to piped read via unpack and fix potential bug in interrupted or HUPed call during unpack where the file wasn't being deleted in the event of a failure. More signals need to be added to the signal handler.

Affected files ...

.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/lib/file.c#10 edit
.. //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/lib/url.c#4 edit

Differences ...

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/lib/file.c#10 (text+ko) ====

@@ -363,7 +363,10 @@
 			    __func__, file_expr);
 	}
 
-	pkg_name_humanized = strcmp(pkg, "-") == 0 ? "(stdin)" : pkg;
+	if (pkg == NULL || strcmp(pkg, "-") == 0)
+		pkg_name_humanized = "(stdin)";
+	else
+		pkg_name_humanized = pkg;
 
 	archive = archive_read_new();
 	archive_read_support_compression_all(archive);

==== //depot/projects/soc2007/gcooper-pkg_install-enhancements-simplified/usr.sbin/pkg_install/lib/url.c#4 (text+ko) ====

@@ -22,11 +22,21 @@
 __FBSDID("$FreeBSD: src/usr.sbin/pkg_install/lib/url.c,v 1.13 2010/04/01 14:27:29 flz Exp $");
 
 #include "lib.h"
+#include <sys/wait.h>
 #include <err.h>
 #include <fetch.h>
 #include <libgen.h>
-#include <sys/wait.h>
 #include <stdio.h>
+#include <unistd.h>
+
+static char pkg[FILENAME_MAX];
+
+static void
+fetch_cleanup(int sig)
+{
+	unlink(pkg);
+	cleanup(sig);
+}
 
 /*
  * Try and fetch a file by URL, returning the directory name for where
@@ -39,7 +49,6 @@
     char *cp, *tmp;
     char fname[FILENAME_MAX];
     char pen[FILENAME_MAX];
-    char pkg[FILENAME_MAX];
     char buf[8192];
     FILE *ftp;
     pid_t tpid;
@@ -128,31 +137,40 @@
 	cleanup(0);
 	exit(2);
     }
-    if ((tpid = fork()) == -1) {
-	warn("pipe()");
+    switch((tpid = fork())) {
+    case -1:
+	warn("fork() failed");
 	cleanup(0);
-	exit(2);
-    }
-    if (!tpid) {
+	exit(EXIT_FAILURE);
+	break;
+    case 0:
+	/* Let the parent deal with the signals. */
+	signal(SIGHUP, SIG_DFL);
+	signal(SIGINT, SIG_DFL);
 	dup2(pfd[0], 0);
 	for (fd = getdtablesize() - 1; fd >= 3; --fd)
-	    close(fd);
-	execl("/usr/bin/tar", "tar",
-	    Verbose ? "-xpjvf" : "-xpjf",
-	    "-", (char *)0);
-	_exit(2);
-    }
-    close(pfd[0]);
-    for (;;) {
-	if ((r = fread(buf, 1, sizeof buf, ftp)) < 1)
-	    break;
-	if ((w = write(pfd[1], buf, r)) != r)
-	    break;
-	if (keep_package) {
-	    if ((w = write(pkgfd, buf, r)) != r)
+	    close(fd);	
+	_exit(unpack(NULL, NULL));
+	break;
+    default:
+	signal(SIGHUP, fetch_cleanup);
+	signal(SIGINT, fetch_cleanup);
+	close(pfd[0]);
+	for (;;) {
+	    if ((r = fread(buf, 1, sizeof(buf), ftp)) < 1)
+		break;
+	    if ((w = write(pfd[1], buf, r)) != r)
 		break;
-	}    
+	    if (keep_package) {
+		if ((w = write(pkgfd, buf, r)) != r)
+		    break;
+	    }    
+
+	}
+	signal(SIGHUP, cleanup);
+	signal(SIGINT, cleanup);
     }
+
     if (ferror(ftp))
 	warn("warning: error reading from server");
     fclose(ftp);
@@ -164,7 +182,7 @@
 	warn("warning: error writing to tar");
     tpid = waitpid(tpid, &pstat, 0);
     if (Verbose)
-	printf("tar command returns %d status\n", WEXITSTATUS(pstat));
+	printf("unpack returned status: %d\n", WEXITSTATUS(pstat));
     if (rp && (isatty(0) || Verbose))
 	printf(" Done.\n");
     return rp;


More information about the p4-projects mailing list