socsvn commit: r253838 - soc2013/dpl/head/contrib/bzip2

dpl at FreeBSD.org dpl at FreeBSD.org
Tue Jul 2 10:08:17 UTC 2013


Author: dpl
Date: Tue Jul  2 10:08:16 2013
New Revision: 253838
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253838

Log:
  sendfd() and recvfd() are finally working. I also cleant some code inside fork.
  Right now, it blocks somewhere, and the parent process doesn't send the good fds.
  

Modified:
  soc2013/dpl/head/contrib/bzip2/bzip2.c

Modified: soc2013/dpl/head/contrib/bzip2/bzip2.c
==============================================================================
--- soc2013/dpl/head/contrib/bzip2/bzip2.c	Tue Jul  2 09:03:21 2013	(r253837)
+++ soc2013/dpl/head/contrib/bzip2/bzip2.c	Tue Jul  2 10:08:16 2013	(r253838)
@@ -232,7 +232,8 @@
 
 int
 sendfd(int s, int fd)
-{
+{   
+   printf("sendfd sending: %d", fd);
    struct msghdr msg;   
    struct cmsghdr *cmsg;
    struct iovec io[1];
@@ -286,11 +287,9 @@
    cmsg.cmsg_len = DATALEN;
    cmsg.cmsg_level = SOL_SOCKET;
    cmsg.cmsg_type = SCM_RIGHTS;
-   /*Why does sendmsg set errno to 9 EBADF??*/
    if(recvmsg(s, &msg, 0) < 0)
       return -1;
    fd = *(int *)CMSG_DATA(&cmsg);
-   printf("recvfd() fd recieved: %d\n", *((int *)CMSG_DATA(&cmsg)));
    return 0;
 }
 #endif
@@ -1379,8 +1378,15 @@
    }
 
 #  if CAPSICUM
-   infd = fileno(inStr);
-   outfd = fileno(outStr);
+   infd = dup(fileno(inStr));
+   outfd = dup(fileno(outStr));
+   if ( infd == -1 || outfd == -1){
+      fprintf ( stderr, "%s: Couldn't close inStr: %s.\n", 
+                        progName, strerror(errno) );
+     setExit(1);
+     exit(exitValue);
+   }
+
    cap_rights_limit(infd, CAP_READ);
    cap_rights_limit(outfd, CAP_WRITE);
 
@@ -1397,37 +1403,74 @@
      exit(exitValue);
    }
 
-   printf("fork()\n");
-   /* Pass the limited file descriptors with a unix domain socket. */
-   switch( forkpid = fork() ) {
-	  case ( -1 ):
+   /* Pass the limited file descriptors via unix domain socket. */
+   if ( (forkpid = fork()) == -1 ){
        fprintf ( stderr, "%s: Couldn't fork: %s.\n", progName, strerror(errno) );
        setExit(1);
        exit(exitValue);
+   } else if ( forkpid != 0) {
+
+      if ((close(sv[0])) < 0){
+         fprintf ( stderr, "%s: Couldn't close fd: %s.\n", 
+                     progName, strerror(errno) );
+         wait(NULL);
+         setExit(1);
+         exit(exitValue);
+      }
+      if ((closeinfd)) < 0){
+         fprintf ( stderr, "%s: Couldn't close fd: %s.\n", 
+                            progName, strerror(errno) );
+         setExit(1);
+         exit(exitValue);
+	   }
+       if ((close(outfd)) < 0){
+		 fprintf ( stderr, "%s: Couldn't close fd: %s.\n", 
+					 progName, strerror(errno) );
+		 setExit(1);
+		 exit(exitValue);
+	   }
+      if (sendfd(sv[1], infd) < 0){
+         fprintf ( stderr, "%s: Couldn't send infd: %s.\n", 
+                     progName, strerror(errno) );
+   		 if (kill(forkpid, SIGTERM) < 0)
+            printf("Couldn't kill the child process, please press ^C\n");
+         wait(NULL);
+         setExit(1);
+         exit(exitValue);
+      }
+      if (sendfd(sv[1], outfd) < 0){
+         fprintf ( stderr, "%s: Couldn't send outfd: %s.\n", 
+                     progName, strerror(errno) );
+   		 if (kill(forkpid, SIGTERM) < 0)
+            printf("Couldn't kill the child process, please press ^C\n");
+         wait(NULL);
+         setExit(1);
+         exit(exitValue);
+      }
+      /* Let the children compress */
+      wait(NULL);
+      return;
 
-     case ( 0 ):
-	   if ((close(sv[1])) < 0){
+   } else if (forkpid == 0){
+       if ((close(sv[1])) < 0){
 		 fprintf ( stderr, "%s: Couldn't close socket: %s.\n", 
 					 progName, strerror(errno) );
 		 setExit(1);
 		 exit(exitValue);
 	   }
-         printf("About to recv infd\n");
-         sleep(1);
        if ((infd = recvfd(sv[0])) < 0){
          fprintf ( stderr, "%s: Couldn't get infd: %s.\n", 
                      progName, strerror(errno) );
          setExit(1);
          exit(exitValue);
        }
-		 printf("About to recv outfd\n");
        if ((outfd = recvfd(sv[0])) < 0){
          fprintf ( stderr, "%s: Couldn't get outfd: %s.\n", 
                      progName, strerror(errno) );
          setExit(1);
          exit(exitValue);
        }
-    printf("Child: infd:%d, outfd:%d\n", infd, outfd);
+       printf("Child: infd:%d, outfd:%d\n", infd, outfd);
        inStr = fdopen(infd, "rb");
        outStr = fdopen(outfd, "wb");
        
@@ -1437,62 +1480,28 @@
 			setExit(1);
 			exit(exitValue);
 		}
-
 #  endif
-         /*--- Now the input and output handles are sane.  Do the Biz. ---*/
-         outputHandleJustInCase = outStr;
-         deleteOutputOnInterrupt = True;
-         compressStream ( inStr, outStr );
-         outputHandleJustInCase = NULL;
-       
-         /*--- If there was an I/O error, we won't get here. ---*/
-         if ( srcMode == SM_F2F ) {
-           applySavedTimeInfoToOutputFile ( outName );
-           deleteOutputOnInterrupt = False;
-           if ( !keepInputFiles ) {
-             IntNative retVal = remove ( inName );
-             ERROR_IF_NOT_ZERO ( retVal );
-           }
-         }
-       
-         deleteOutputOnInterrupt = False;
-#  if CAPSICUM
-		   exit(0);
-         break;
-
-	  default:
-	    printf("Parent: infd:%d, outfd:%d\n", infd, outfd);
-		printf("Child's pid: %d\n", forkpid);
-      if ((close(sv[0])) < 0){
-         fprintf ( stderr, "%s: Couldn't close fd: %s.\n", 
-                     progName, strerror(errno) );
-         wait(NULL);
-         setExit(1);
-         exit(exitValue);
+      /*--- Now the input and output handles are sane.  Do the Biz. ---*/
+      outputHandleJustInCase = outStr;
+      deleteOutputOnInterrupt = True;
+      compressStream ( inStr, outStr );
+      outputHandleJustInCase = NULL;
+      
+      /*--- If there was an I/O error, we won't get here. ---*/
+      if ( srcMode == SM_F2F ) {
+      applySavedTimeInfoToOutputFile ( outName );
+      deleteOutputOnInterrupt = False;
+      if ( !keepInputFiles ) {
+      IntNative retVal = remove ( inName );
+      ERROR_IF_NOT_ZERO ( retVal );
       }
-      if (sendfd(sv[1], infd) < 0){
-         fprintf ( stderr, "%s: Couldn't send infd: %s.\n", 
-                     progName, strerror(errno) );
-		 if (kill(forkpid, SIGTERM) < 0)
-            printf("Couldn't kill the child process, please press ^C\n");
-         wait(NULL);
-         setExit(1);
-         exit(exitValue);
       }
-      if (sendfd(sv[1], outfd) < 0){
-         fprintf ( stderr, "%s: Couldn't send outfd: %s.\n", 
-                     progName, strerror(errno) );
-         wait(NULL);
-         setExit(1);
-         exit(exitValue);
-      }
-      wait(NULL);
+      
+      deleteOutputOnInterrupt = False;
       return;
    }
-#  endif
 }
 
-
 /*---------------------------------------------*/
 static 
 void uncompress ( Char *name )
@@ -2147,7 +2156,6 @@
      setExit(1);
      exit(exitValue);
    }
-   printf("socketpair(): sv[0]:%d sv[1]:%d\n", sv[0], sv[1]);
 #  endif
 
    if (opMode == OM_Z) {
@@ -2195,7 +2203,7 @@
          if (aa->name[0] == '-' && decode) continue;
          numFilesProcessed++;
          testf ( aa->name );
-	 }
+        }
      }
      if (testFailsExist && noisy) {
        fprintf ( stderr,


More information about the svn-soc-all mailing list