Why does FUSE seem to block all threads?

I took the hello.c fuse and changed the bottom to show what I'm talking about. In my application, I need to do something after my FS fuse. I also need another thread for IPC and an update of some things. Since fuse_main not returning, I threw it into my own thread.

When I comment on fuse_main , A and B are displayed on the console. However, if I do not comment on fuse_main (which is in another thread), only A. is printed. How heck is a fuse that stops my main thread and how do I run code after FUSE doing his job?

 #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(); } 
+8
c ++ c fuse
source share
2 answers

fuse_main , i.e. calls fork() and calls _exit(0) in the parent process, so the process terminates, so you only see listing A

If you give the -f option to ./hello -f /tmp/fuse , fuse_main does not call _exit , but remains alive in the foreground, and you can see both A and B

You will probably need a way to end the fuse_main stream 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(); } 

hello options:

 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 [...] 
+8
source share

From what I read in the documentation http://fuse.sourceforge.net/doxygen/hello_8c.html about using the hello.c program, it says that the program terminates and disappears in the background, that is, the nature of the fuse_main API. Why don't you try starting from 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 path in line

err = fuse_session_loop(se);

in the main function that you have control in order to have additions and do other things that you want to do.

There is also a C ++ implementation for FUSE, https://code.google.com/p/fusekit/

Hope this helps.

+4
source share

All Articles