cleaned up semantics of fd-passing and error handling
authorMark Glines <mark@glines.org>
Wed, 26 Jun 2002 13:47:21 +0000 (13:47 +0000)
committerMark Glines <mark@glines.org>
Wed, 26 Jun 2002 13:47:21 +0000 (13:47 +0000)
lib/helper.c
util/fuse_ioslave.c

index ff59f4af7f3880e1f2d30f9221c40df3d129d9ca..f523f99f8a16a631b9d4bbe679cb46631d1cb51f 100644 (file)
@@ -75,6 +75,10 @@ static int fuse_mount_obsolete(int *argcp, char **argv)
     return 0;
 }
 
+/* return value:
+ * >= 0  => fd
+ * -1    => error
+ */
 int receive_fd(int fd) {
     struct msghdr msg;
     struct iovec iov;
@@ -96,12 +100,17 @@ int receive_fd(int fd) {
     msg.msg_control = ccmsg;
     msg.msg_controllen = sizeof(ccmsg);
 
-    rv = recvmsg(fd, &msg, 0);
+    while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR);
     if (rv == -1) {
         perror("recvmsg");
         return -1;
     }
-
+    if(!rv) {
+        /* EOF */
+        fprintf(stderr, "got EOF\n");
+        return -1;
+    }
+    
     cmsg = CMSG_FIRSTHDR(&msg);
     if (!cmsg->cmsg_type == SCM_RIGHTS) {
         fprintf(stderr, "got control message of unknown type %d\n",
@@ -120,8 +129,13 @@ int fuse_mount(const char *mountpoint, const char *mount_args)
     /* FIXME: parse mount_args (or just pass it to fusermount ???) */
     mount_args = mount_args;
 
-    if(socketpair(PF_UNIX,SOCK_DGRAM,0,fds)) {
+    /* make sure the socket fds are greater than 0 */
+    fd = open("/dev/null",O_RDONLY); 
+    rv = socketpair(PF_UNIX,SOCK_STREAM,0,fds);
+    close(fd);
+    if(rv) {
         fprintf(stderr,"fuse: failed to socketpair()\n");
+        close(fd);
         return -1;
     }
     pid = fork();
@@ -143,10 +157,9 @@ int fuse_mount(const char *mountpoint, const char *mount_args)
 
     fd = fds[1];
     close(fds[0]);
-    while((rv = receive_fd(fd)) < 0)
-        sleep(1);
+    rv = receive_fd(fd);
     close(fd);
-    while(wait(NULL) != pid); /* bury zombie */
+    waitpid(pid,NULL,0); /* bury zombie */
     return rv;
 }
 
index 1fa3bbf5b803a57ac183f7385847b0bf089c0611..df7c66da1e80f2af4ca921178ff0050554fe7417 100644 (file)
@@ -1,5 +1,5 @@
-#include <stdio.h>                /* fprintf */
-#include <errno.h>                /* errno */
+#include <stdio.h>                 /* fprintf */
+#include <errno.h>                 /* errno */
 #include <string.h>                /* strerror */
 #include <unistd.h>                /* read,write,close */
 #include <stdlib.h>                /* getenv,strtol */
 #undef IOSLAVE_DEBUG
 char *scratch;
 
+/* return values:
+ * 0  => success
+ * -1 => error condition
+ */
 int send_fd(int sock_fd, int send_fd) {
     int retval;
     struct msghdr msg;
@@ -37,11 +41,12 @@ int send_fd(int sock_fd, int send_fd) {
      */
     vec.iov_base = &sendchar;
     vec.iov_len = sizeof(sendchar);
-    retval = sendmsg(sock_fd, &msg, 0);
+    while((retval = sendmsg(sock_fd, &msg, 0)) == -1 && errno == EINTR);
     if (retval != 1) {
         perror("sendmsg");
+        return -1;
     }
-    return retval;
+    return 0;
 }
 
 int main() {
@@ -50,8 +55,7 @@ int main() {
     if (!env)
         exit(fprintf(stderr, "fuse_ioslave: do not run me directly\n"));
     fd = strtol(env, NULL, 0);
-    while (send_fd(fd, 0) < 0) {
-        sleep(5);
-    }
+    if(send_fd(fd, 0) == -1)
+       fprintf(stderr,"failed to send fd\n");
     return 0;
 }