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

dpl at FreeBSD.org dpl at FreeBSD.org
Wed Jul 3 16:05:54 UTC 2013


Author: dpl
Date: Wed Jul  3 16:05:53 2013
New Revision: 253907
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253907

Log:
  Stop passing fd, start using a new architecture.
  

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

Modified: soc2013/dpl/head/contrib/bzip2/bzip2.c
==============================================================================
--- soc2013/dpl/head/contrib/bzip2/bzip2.c	Wed Jul  3 14:58:11 2013	(r253906)
+++ soc2013/dpl/head/contrib/bzip2/bzip2.c	Wed Jul  3 16:05:53 2013	(r253907)
@@ -223,73 +223,7 @@
 Int32   workFactor;
 
 #if CAPSICUM
-int sv[2], len;
-
-int sendfd(int, int);
-int recvfd(int);
-
-#define DATALEN CMSG_LEN(sizeof(int))
-
-int
-sendfd(int s, int fd)
-{   
-   printf("sendfd sending: %d\n", fd);
-   struct msghdr msg;   
-   struct cmsghdr *cmsg;
-   struct iovec io[1];
-   char buf[1] = { "!" };
-   int ret;
-
-   io[0].iov_base = buf;
-   io[0].iov_len = 1;
-
-   if( (cmsg = malloc(DATALEN)) == NULL )
-      return -1;
-
-   msg.msg_name = NULL;
-   msg.msg_namelen = 0;
-   msg.msg_iov = io;
-   msg.msg_iovlen = 1;
-   msg.msg_control =cmsg;
-   msg.msg_controllen = DATALEN;
-
-   cmsg->cmsg_len = DATALEN;
-   cmsg->cmsg_level = SOL_SOCKET;
-   cmsg->cmsg_type = SCM_RIGHTS;
-   *(int *)CMSG_DATA(cmsg) = fd;
-   if( (ret = sendmsg(s, &msg, 0)) <= 0)
-      return -1;
-   free(cmsg);
-   return 0;
-}
-
-int
-recvfd(int s)
-{
-   int fd;
-   struct msghdr msg;   
-   struct cmsghdr cmsg;
-   char buf[1] = { "!" };
-   struct iovec io[1];
-
-   io[0].iov_base = buf;
-   io[0].iov_len = 1;
-
-   msg.msg_name = NULL;
-   msg.msg_namelen = 0;
-   msg.msg_iov = io;
-   msg.msg_iovlen = 1;
-   msg.msg_control = &cmsg;
-   msg.msg_controllen = DATALEN;
-
-   cmsg.cmsg_len = DATALEN;
-   cmsg.cmsg_level = SOL_SOCKET;
-   cmsg.cmsg_type = SCM_RIGHTS;
-   if(recvmsg(s, &msg, 0) < 0)
-      return -1;
-   fd = *(int *)CMSG_DATA(&cmsg);
-   return 0;
-}
+int capret;
 #endif
 
 static void   panic             ( const Char* ) NORETURN;
@@ -1218,7 +1152,6 @@
    FILE  *outStr;
    Int32 n, i;
 #  if CAPSICUM
-   Int32 infd, outfd;
    pid_t forkpid;
 #  endif
    struct MY_STAT statBuf;
@@ -1376,108 +1309,33 @@
    }
 
 #  if CAPSICUM
-   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);
-
-   if(fclose(inStr) < 0){
-     fprintf ( stderr, "%s: Couldn't close inStr: %s.\n", 
-                    progName, strerror(errno) );
-     setExit(1);
-     exit(exitValue);
-   }
-   if(fclose(outStr) < 0){
-     fprintf ( stderr, "%s: Couldn't close outStr: %s.\n", 
-                  progName, strerror(errno) );
-     setExit(1);
-     exit(exitValue);
-   }
-
    /* 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) {
-      printf("Parent= infd:%d, outfd:%d\n", infd, outfd);
-      if ((close(sv[0])) < 0){
-         fprintf ( stderr, "%s: Couldn't close fd: %s.\n", 
-                     progName, strerror(errno) );
-         wait(NULL);
-         setExit(1);
-         exit(exitValue);
-      }
-      if ((close(infd)) < 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;
 
    } 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);
-	   }
-       if ((infd = recvfd(sv[0])) < 0){
-         fprintf ( stderr, "%s: Couldn't get infd: %s.\n", 
-                     progName, strerror(errno) );
+      capret = cap_rights_limit(fileno(inStr), CAP_READ);
+      capret |= cap_rights_limit(fileno(outStr), CAP_WRITE);
+      
+      if ( capret ){
+         fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
+            progName, strerror(errno) );
          setExit(1);
          exit(exitValue);
-       }
-       if ((outfd = recvfd(sv[0])) < 0){
-         fprintf ( stderr, "%s: Couldn't get outfd: %s.\n", 
-                     progName, strerror(errno) );
+      }
+      if (cap_enter() < 0){
+         fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
+            progName, strerror(errno) );
          setExit(1);
          exit(exitValue);
-       }
-       printf("Child: infd:%d, outfd:%d\n", infd, outfd);
-       inStr = fdopen(infd, "rb");
-       outStr = fdopen(outfd, "wb");
-       
-       if (cap_enter() < 0) {
-		   fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
-                      progName, strerror(errno) );
-			setExit(1);
-			exit(exitValue);
-		}
-#  endif
+      }
+#     endif
       /*--- Now the input and output handles are sane.  Do the Biz. ---*/
       outputHandleJustInCase = outStr;
       deleteOutputOnInterrupt = True;
@@ -1486,17 +1344,19 @@
       
       /*--- 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 );
-      }
+         applySavedTimeInfoToOutputFile ( outName );
+         deleteOutputOnInterrupt = False;
+         if ( !keepInputFiles ) {
+            IntNative retVal = remove ( inName );
+            ERROR_IF_NOT_ZERO ( retVal );
+         }
       }
       
       deleteOutputOnInterrupt = False;
-      return;
+#     if CAPSICUM
+      exit(0);
    }
+#  endif
 }
 
 /*---------------------------------------------*/
@@ -2146,15 +2006,6 @@
 #    endif
    }
 
-#  if CAPSICUM   
-   if ( socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) < 0 ){
-     fprintf (stderr, "%s: Can't create socket: %s.\n", 
-	               progName, strerror(errno) );
-     setExit(1);
-     exit(exitValue);
-   }
-#  endif
-
    if (opMode == OM_Z) {
     if (srcMode == SM_I2O) {
       compress ( NULL );


More information about the svn-soc-all mailing list