简体   繁体   中英

Why does FUSE seem to be locking up all threads?

I took fuse hello.c and modified the bottom to show what I am talking about. In my app I need to do things after my fuse FS is available. I also need another thread for IPC and keeping certain things up to date. Because fuse_main doesn't appear to return I threw it in its own thread.

When I comment out fuse_main the console shows A and B printed. However if I don't comment out fuse_main (which is in a different thread) only A is printed. How the heck is fuse stopping my main thread and how do I run code after FUSE does its thing?

#define FUSE_USE_VERSION 26

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

static const char *hello_str = "Hello World!\n";
static const char *hello_path = "/hello";

static int hello_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));
    if (strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
    } else if (strcmp(path, hello_path) == 0) {
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(hello_str);
    } else
        res = -ENOENT;

    return res;
}

static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
             off_t offset, struct fuse_file_info *fi)
{
    (void) offset;
    (void) fi;

    if (strcmp(path, "/") != 0)
        return -ENOENT;

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, hello_path + 1, NULL, 0);

    return 0;
}

static int hello_open(const char *path, struct fuse_file_info *fi)
{
    if (strcmp(path, hello_path) != 0)
        return -ENOENT;

    if ((fi->flags & 3) != O_RDONLY)
        return -EACCES;

    return 0;
}

static int hello_read(const char *path, char *buf, size_t size, off_t offset,
              struct fuse_file_info *fi)
{
    size_t len;
    (void) fi;
    if(strcmp(path, hello_path) != 0)
        return -ENOENT;

    len = strlen(hello_str);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, hello_str + offset, size);
    } else
        size = 0;

    return size;
}

static struct fuse_operations hello_oper;
//modification starts below this line
#include<thread>
#include<unistd.h>
int main(int argc, char *argv[])
{
    std::thread t([&]{
        hello_oper.getattr  = hello_getattr;
        hello_oper.readdir  = hello_readdir;
        hello_oper.open     = hello_open;
        hello_oper.read     = hello_read;

        return fuse_main(argc, argv, &hello_oper, NULL);
    });
    printf("A\n");
    sleep(5);
    printf("B\n");
    t.join();
}

fuse_main daemonizes, ie calls fork() and calls _exit(0) in the parent process, so the process exits, hence you only see the A printout.

If you give the option -f in ./hello -f /tmp/fuse the fuse_main does not call _exit but stays alive in the foreground and both A and B can be seen.

You surely need a way to end the fuse_main thread gracefully when your program wants to exit:

//modification starts below this line
#include<thread>
#include<unistd.h>
#include <signal.h>
#include <sys/syscall.h>

int main(int argc, char *argv[])
{
    pid_t tid;
    std::thread t([&]{
        hello_oper.getattr  = hello_getattr;
        hello_oper.readdir  = hello_readdir;
        hello_oper.open     = hello_open;
        hello_oper.read     = hello_read;

        tid = syscall(SYS_gettid);
        return fuse_main(argc, argv, &hello_oper, NULL);
    });
    printf("A\n");
    sleep(5);
    printf("B\n");
    kill(tid, SIGTERM);
    t.join();
}

Options for hello :

general options:
    -o opt,[opt...]        mount options
    -h   --help            print help
    -V   --version         print version

FUSE options:
    -d   -o debug          enable debug output (implies -f)
    -f                     foreground operation
    -s                     disable multi-threaded operation
[...]

From what I read in the documentation here http://fuse.sourceforge.net/doxygen/hello_8c.html on the usage of the hello.c program, it says that the program exits and vanishes into the background, that is the nature of the fuse_main API . Why don't you give a try, starting with this code http://fuse.sourceforge.net/doxygen/hello__ll_8c.html , from their description, unlike hello.c this example will stay in the foreground. it also replaced the convenience function fuse_main(..) with a more low level approach. unlike hello.c this example will stay in the foreground. it also replaced the convenience function fuse_main(..) with a more low level approach. This way in the line,

err = fuse_session_loop(se);

in the main function you have control to have additions and do other things you wish to do.

Also there is a c++ implementation for FUSE, https://code.google.com/p/fusekit/

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM