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

dpl at FreeBSD.org dpl at FreeBSD.org
Mon Jun 24 20:27:50 UTC 2013


Author: dpl
Date: Mon Jun 24 20:27:50 2013
New Revision: 253445
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=253445

Log:
  Passing FDs :D
  

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

Modified: soc2013/dpl/head/contrib/bzip2/bzip2.c
==============================================================================
--- soc2013/dpl/head/contrib/bzip2/bzip2.c	Mon Jun 24 19:57:25 2013	(r253444)
+++ soc2013/dpl/head/contrib/bzip2/bzip2.c	Mon Jun 24 20:27:50 2013	(r253445)
@@ -222,72 +222,64 @@
 Int32   workFactor;
 
 #if CAPSICUM == 1
-int sv[2], fds[2], len;
-int sendfds(int, int*);
-int recvfds(int);
+int sv[2], len;
+
+int sendfd(int, int);
+int recvfd(int);
 
 int
-sendfds(int s, int fd[2])
+sendfd(int s, int fd)
 {
-	char buf[1];
-	struct iovec iov;
-	struct msghdr msg;
-	struct cmsghdr *cmsg;
-	char cms[CMSG_SPACE(2*sizeof(int))];
-	
-	buf[0] = 0;
-	iov.iov_base = buf;
-	iov.iov_len = 1;
-
-	memset(&msg, 0, sizeof msg);
-	msg.msg_iov = &iov;
-	msg.msg_iovlen = 1;
-	msg.msg_control = (caddr_t)cms;
-	msg.msg_controllen = CMSG_LEN(2*sizeof(int));
-
-	cmsg = CMSG_FIRSTHDR(&msg);
-	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
-	cmsg->cmsg_level = SOL_SOCKET;
-	cmsg->cmsg_type = SCM_RIGHTS;
-	memmove(CMSG_DATA(cmsg), &fd, 2*sizeof(int));
-
-	if((sendmsg(s, &msg, 0)) != (int)iov.iov_len)
-		return -1;
-	return 0;
+   char buf[1] = { 0 };
+   struct iovec iov;
+   iov.iov_base = buf;
+   iov.iov_len = 1;
+   
+   struct msghdr msg;
+   char cms[CMSG_SPACE(sizeof(int))];
+   bzero(&msg, sizeof msg);
+   msg.msg_iov = &iov;
+   msg.msg_iovlen = 1;
+   msg.msg_control = (caddr_t)cms;
+   msg.msg_controllen = CMSG_LEN(sizeof(int));
+   
+   struct cmsghdr *cmsg;
+   cmsg = CMSG_FIRSTHDR(&msg);
+   cmsg->cmsg_len = CMSG_LEN(sizeof(int));
+   cmsg->cmsg_level = SOL_SOCKET;
+   cmsg->cmsg_type = SCM_RIGHTS;
+   memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
+   
+   if((sendmsg(s, &msg, 0)) != (int)iov.iov_len)
+      return -1;
+   return 0;
 }
 
 int
-recvfds(int s)
+recvfd(int s)
 {
-	int n;
-	int fds[2];
-	char buf[1];
-	struct iovec iov;
-	struct msghdr msg;
-	struct cmsghdr *cmsg;
-	char cms[CMSG_SPACE(2*sizeof(int))];
-
-	iov.iov_base = buf;
-	iov.iov_len = 1;
-
-	memset(&msg, 0, sizeof msg);
-	msg.msg_iov = &iov;
-	msg.msg_iovlen = 1;
-
-	msg.msg_control = (caddr_t)cms;
-	msg.msg_controllen = sizeof cms;
-
-	if((n = recvmsg(s, &msg, 0)) < 0)
-		return -1;
-	if(n == 0){
-		fprintf ( stderr, 
-				  "\n%s: recvfds: Unexpected EOF.\n",
-				  progName);
-		return -1;
-	}
-	cmsg = CMSG_FIRSTHDR(&msg);
-	memmove(&fds, CMSG_DATA(cmsg), sizeof(int));
-	return fds;
+   int fd;
+   struct cmsghdr *cmsg;
+   
+   char buf[1] = { 0 };
+   struct iovec iov;
+   iov.iov_base = buf;
+   iov.iov_len = 1;
+   
+   struct msghdr msg;
+   char cms[CMSG_SPACE(sizeof(int))];
+   msg.msg_name = 0;
+   msg.msg_namelen = 0;
+   msg.msg_iov = &iov;
+   msg.msg_iovlen = 1;
+   msg.msg_control = (caddr_t)cms;
+   msg.msg_controllen = sizeof cms;
+   
+   if((recvmsg(s, &msg, 0)) <= 0)
+      return -1;
+   cmsg = CMSG_FIRSTHDR(&msg);
+   memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
+   return fd;
 }
 #endif
 
@@ -1044,9 +1036,6 @@
    FILE*     fp;
    IntNative fh;
    fh = open(name, O_WRONLY|O_CREAT|O_EXCL, S_IWUSR|S_IRUSR);
-#  if CAPSICUM
-   cap_rights_limit(fh, CAP_WRITE);
-#  endif
    if (fh == -1) return NULL;
    fp = fdopen(fh, mode);
    if (fp == NULL) close(fh);
@@ -1220,7 +1209,7 @@
    FILE  *outStr;
    Int32 n, i;
 #  if CAPSICUM
-   Int32 infd;
+   Int32 fd;
    pid_t forkpid;
 #  endif
    struct MY_STAT statBuf;
@@ -1310,16 +1299,6 @@
       saveInputFileMetaInfo ( inName );
    }
 
-   if ( srcMode != SM_I2O ){
-#	if CAPSICUM
-		 infd = open( inName, O_RDONLY );
-		 cap_rights_limit(infd, CAP_READ);
-         inStr = fdopen ( infd, "rb" );
-#	else
-         inStr = fopen ( inName, "rb" );
-#	endif
-   }
-
    switch ( srcMode ) {
 
       case SM_I2O:
@@ -1344,6 +1323,7 @@
                       progName );
             fprintf ( stderr, "%s: For help, type: `%s --help'.\n",
                               progName, progName );
+            inStr = fopen ( inName, "rb" );
             if ( inStr != NULL ) fclose ( inStr );
             setExit(1);
             return;
@@ -1361,6 +1341,7 @@
          if ( outStr == NULL) {
             fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
                       progName, outName, strerror(errno) );
+            inStr = fopen ( inName, "rb" );
             if ( inStr != NULL ) fclose ( inStr );
             setExit(1);
             return;
@@ -1389,10 +1370,25 @@
    /* Pass the limited file descriptors with a unix domain socket. */
    switch( forkpid = rfork(RFPROC | RFCFDG) ) {
       case ( 0 ):
-	   /* revmsg() the FDs */
-		if (cap_enter() < 0) {
+         if (fd == recvfd(sv[1]) <= 0){
 		   fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
-							  progName, strerror(errno) );
+                             progName, strerror(errno) );
+			exit(1);
+          }
+		printf("inStr fd receiverd: %d\n",fd);
+         inStr = fdopen(fd, "rb");
+
+         if (fd == recvfd(sv[1]) <= 0){
+		   fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
+                             progName, strerror(errno) );
+			exit(1);
+          }
+         outStr = fdopen(fd, "wb");
+		printf("outStr fd receiverd: %d\n",fd);
+         
+         if (cap_enter() < 0) {
+		   fprintf ( stderr, "%s: Couldn't enter capability mode: %s.\n", 
+                             progName, strerror(errno) );
 			exit(1);
 		}
 
@@ -1423,12 +1419,18 @@
          exit(1);
 
 	  default:
-
+	     fd = fileno(inStr);
+		 cap_rights_limit(fd, CAP_READ);
+		 sendfd(sv[0], fd);
+		 printf("Sending inStr fd: %d\n", fd);
+	     fd = fileno(outStr);
+		 cap_rights_limit(fd, CAP_WRITE);
+		 sendfd(sv[0], fd);
+		 printf("Sending outStr fd: %d\n", fd);
          wait(NULL);
          return;
    }
 #  endif
-
 }
 
 
@@ -1439,6 +1441,10 @@
    FILE  *inStr;
    FILE  *outStr;
    Int32 n, i;
+/*#  if CAPSICUM*/
+   /*Int32 infd;*/
+   /*pid_t forkpid;*/
+/*#  endif*/
    Bool  magicNumberOK;
    Bool  cantGuess;
    struct MY_STAT statBuf;
@@ -1531,16 +1537,6 @@
       saveInputFileMetaInfo ( inName );
    }
 
-   if ( srcMode != SM_I2O ){
-/*#	if CAPSICUM*/
-		 /*infd = open( inName, O_RDONLY );*/
-		 /*cap_rights_limit(infd, CAP_READ);*/
-         /*inStr = fdopen ( infd, "rb" );*/
-/*#	else*/
-         inStr = fopen ( inName, "rb" );
-/*#	endif*/
-   }
-
    switch ( srcMode ) {
 
       case SM_I2O:
@@ -1558,6 +1554,7 @@
          break;
 
       case SM_F2O:
+         inStr = fopen ( inName, "rb" );
          outStr = stdout;
          if ( inStr == NULL ) {
             fprintf ( stderr, "%s: Can't open input file %s:%s.\n",
@@ -1569,6 +1566,7 @@
          break;
 
       case SM_F2F:
+         inStr = fopen ( inName, "rb" );
          outStr = fopen_output_safely ( outName, "wb" );
          if ( outStr == NULL) {
             fprintf ( stderr, "%s: Can't create output file %s: %s.\n",
@@ -2088,19 +2086,10 @@
    }
 
 #  if CAPSICUM
-
    if ( socketpair(PF_LOCAL, SOCK_STREAM, 0, sv) == -1 ){
       fprintf ( stderr, "%s: Can't create socket: %s.\n", progName, strerror(errno) );
 	  exit(1);
    }
-
-   /* XXX - Factorize code here */
-   /*if (srcMode == SM_I2O) {*/
-      /*if (opMode == OM_Z )*/
-		 /*compress( NULL );*/
-	  /*else if (opMode == OM_UNZ )*/
-		 /*uncompress (NULL);*/
-   /*}*/
 #  endif
 
    if (opMode == OM_Z) {


More information about the svn-soc-all mailing list