fuse_main() goes into background after successful mount
authorMiklos Szeredi <miklos@szeredi.hu>
Thu, 3 Jun 2004 13:52:40 +0000 (13:52 +0000)
committerMiklos Szeredi <miklos@szeredi.hu>
Thu, 3 Jun 2004 13:52:40 +0000 (13:52 +0000)
ChangeLog
TODO [deleted file]
lib/helper.c

index f9c6a857de667c70603027aeb871ac4e4c58af8d..3d32987a4abac05fb9eaa1148da41040aaf08ba4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,11 @@
        * Change FUSE_MINOR_VERSION from 1 to 0.  I know it's illegal but
        there has not been a release with the previous minor number, and I
        hope nobody is using it for anything.
+
+       * Change fuse_main(), so that default behavior is to go into
+       background if mount is successful.  '-f' and '-d' options disable
+       backgrounding.  This fixes the "Why does my FUSE daemon hang?"
+       newbie complaint.
        
 2004-05-18  Miklos Szeredi <mszeredi@inf.bme.hu>
 
diff --git a/TODO b/TODO
deleted file mode 100644 (file)
index 971ea9e..0000000
--- a/TODO
+++ /dev/null
@@ -1,5 +0,0 @@
- - Better (but not too complex) library interface for open/read/write/close
-
- - Integrate (parts of) fusermount into mount(8)
-
- - Etc, etc...
index 0a4948cdefb277be77052e786a280f6040bb0f9a..a5e48540a717afca9cc33ab6b573fa787ef6ce34 100644 (file)
@@ -27,7 +27,8 @@ static void usage(char *progname)
     fprintf(stderr,
             "usage: %s mountpoint [options] [-- [fusermount options]]\n"
             "Options:\n"
-            "    -d      enable debug output\n"
+            "    -d      enable debug output (implies -f)\n"
+            "    -f      foreground operation\n"
             "    -s      disable multithreaded operation\n"
             "    -h      print help\n"
             "\n"
@@ -45,7 +46,7 @@ static void invalid_option(char *argv[], int argctr)
 
 static void exit_handler()
 {
-    if(fuse != NULL)
+    if (fuse != NULL)
         fuse_exit(fuse);
 }
 
@@ -67,33 +68,69 @@ static void set_signal_handlers()
 
     sa.sa_handler = SIG_IGN;
     
-    if(sigaction(SIGPIPE, &sa, NULL) == -1) {
+    if (sigaction(SIGPIPE, &sa, NULL) == -1) {
        perror("Cannot set ignored signals");
         exit(1);
     }
 }
 
+static int fuse_start(int fuse_fd, int flags, int multithreaded,
+                      int background, const struct fuse_operations *op)
+{
+    int pid;
+
+    fuse = fuse_new(fuse_fd, flags, op);
+    if (fuse == NULL)
+        return 1;
+    
+    if (background) {
+        pid = fork();
+        if (pid == -1)
+            return 1;
+
+        if (pid)
+            exit(0);
+    }
+
+    set_signal_handlers();
+
+    if (multithreaded)
+        fuse_loop_mt(fuse);
+    else
+        fuse_loop(fuse);
+
+    return 0;
+}
+
 void fuse_main(int argc, char *argv[], const struct fuse_operations *op)
 {
     int argctr;
     int flags;
     int multithreaded;
+    int background;
     int fuse_fd;
     char *fuse_mountpoint = NULL;
     char **fusermount_args = NULL;
     char *newargs[3];
     char *basename;
+    int err;
     
     flags = 0;
     multithreaded = 1;
-    for(argctr = 1; argctr < argc && !fusermount_args; argctr ++) {
-        if(argv[argctr][0] == '-') {
-            if(strlen(argv[argctr]) == 2)
-                switch(argv[argctr][1]) {
+    background = 1;
+    for (argctr = 1; argctr < argc && !fusermount_args; argctr ++) {
+        if (argv[argctr][0] == '-') {
+            if (strlen(argv[argctr]) == 2)
+                switch (argv[argctr][1]) {
                 case 'd':
                     flags |= FUSE_DEBUG;
+                    background = 0;
                     break;
                     
+                case 'f':
+                    background = 0;
+                    break;
+
                 case 's':
                     multithreaded = 0;
                     break;
@@ -111,17 +148,17 @@ void fuse_main(int argc, char *argv[], const struct fuse_operations *op)
                 }
             else
                 invalid_option(argv, argctr);
-        } else if(fuse_mountpoint == NULL)
+        } else if (fuse_mountpoint == NULL)
             fuse_mountpoint = strdup(argv[argctr]);
         else
             invalid_option(argv, argctr);
     }
 
-    if(fuse_mountpoint == NULL) {
+    if (fuse_mountpoint == NULL) {
         fprintf(stderr, "missing mountpoint\n");
         usage(argv[0]);
     }
-    if(fusermount_args != NULL)
+    if (fusermount_args != NULL)
         fusermount_args -= 2; /* Hack! */
     else {
         fusermount_args = newargs;
@@ -129,30 +166,22 @@ void fuse_main(int argc, char *argv[], const struct fuse_operations *op)
     }
     
     basename = strrchr(argv[0], '/');
-    if(basename == NULL)
+    if (basename == NULL)
         basename = argv[0];
-    else if(basename[1] != '\0')
+    else if (basename[1] != '\0')
         basename++;
 
     fusermount_args[0] = "-n";
     fusermount_args[1] = basename;
 
     fuse_fd = fuse_mount(fuse_mountpoint, (const char **) fusermount_args);
-    if(fuse_fd == -1)
+    if (fuse_fd == -1)
         exit(1);
 
-    set_signal_handlers();
-
-    fuse = fuse_new(fuse_fd, flags, op);
-    if(fuse == NULL)
-        exit(1);
-
-    if(multithreaded)
-        fuse_loop_mt(fuse);
-    else
-        fuse_loop(fuse);
-
+    err = fuse_start(fuse_fd, flags, multithreaded, background, op);
     close(fuse_fd);
     fuse_unmount(fuse_mountpoint);
+    if (err)
+        exit(err);
 }