title: Using syscall open() to open a file in FUSE
body: <p>I'm recently working on fuse, but yet still cant have a grasp of how it works since i have very little knowledge about filesystem in linux. One of the most confusing thing is that <strong>what will happen if i use syscall open() to open a file within the function for the fuse_operation &quot;open&quot;</strong>, like the <a href="https://www.cs.nmsu.edu/%7Epfeiffer/fuse-tutorial/html/callbacks.html" rel="nofollow noreferrer">code</a> below</p>
  <pre class="lang-c prettyprint-override"><code>int bb_open(const char *path, struct fuse_file_info *fi)
  {
      int retstat = 0;
      int fd;
      char fpath[PATH_MAX];
  
      bb_fullpath(fpath, path);
      
      log_msg(&quot;bb_open(fpath\&quot;%s\&quot;, fi=0x%08x)\n&quot;,
          fpath,  (int) fi);
      
      fd = open(fpath, fi-&gt;flags);
      if (fd &lt; 0)
      retstat = bb_error(&quot;bb_open open&quot;);
      
      fi-&gt;fh = fd;
      log_fi(fi);
      
      return retstat;
  }
  
  </code></pre>
  <p>My assumption was:</p>
  <ol>
  <li>A file within this filesystem(&quot;myfilesystem&quot;) is being opened, the request eventually gets accepted by fuse and the <strong>bb_open()</strong> is invoked.</li>
  <li>When the execution reaches <strong>&quot;fd = open(fpath, fi-&gt;flags)&quot;</strong>, it is going to <strong>open() a file within this filesystem again which goes back to step one</strong>, then comes a loop.</li>
  </ol>
  <p><strong>BUT</strong> after trying the code, it actually end up opening the file successfully, which confuses me. Did i miss something important? Hope i make this problem understandable, and thanks in advance!</p>
  <hr />
  <p><strong>EDIT</strong></p>
  <p>thanks for the help guys! but i still couldnt get the deadlock that user253751 have metioned, i think i should post more details here:</p>
  <pre class="lang-c prettyprint-override"><code>static int hello_getattr(const char *path, struct stat *stbuf,
                           struct fuse_file_info *fi)
  {
          (void) fi;
          int res = 0;
          memset(stbuf, 0, sizeof(struct stat));
          if (strcmp(path, &quot;/&quot;) == 0) {
                  stbuf-&gt;st_mode = S_IFDIR | 0755;
                  stbuf-&gt;st_nlink = 2;
          } else if (strcmp(path+1, &quot;hello&quot;) == 0) {
                  stbuf-&gt;st_mode = S_IFREG | 0444;
                  stbuf-&gt;st_nlink = 1;
                  stbuf-&gt;st_size = strlen(&quot;hello there&quot;);
          } 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,
                           enum fuse_readdir_flags flags)
  {
          (void) offset;
          (void) fi;
          (void) flags;
          if (strcmp(path, &quot;/&quot;) != 0)
                  return -ENOENT;
          filler(buf, &quot;.&quot;, NULL, 0, 0);
          filler(buf, &quot;..&quot;, NULL, 0, 0);
          filler(buf, &quot;hello&quot;, NULL, 0, 0);
          return 0;
  }
  static int hello_open(const char *path, struct fuse_file_info *fi)
  {
          char* fpath = get_full_path(path);
          int fd = open(fpath,O_RDONLY);
          fi-&gt;fh = fd;
          log1(fpath);
          log1(&quot;  open\n&quot;);
          return 0;
  }
  static int hello_read(const char *path, char *buf, size_t size, off_t offset,
                        struct fuse_file_info *fi)
  {
          char* fpath = get_full_path(path);
          read(fi-&gt;fh,buf,size); 
          log1(fpath);
          log1(&quot;  read\n&quot;);
          return size;
  }
  
  </code></pre>
  <p>Basically i use the hello_readdir() to fill a filename to the directory,then tell the hello_getattr() to get some random info for the file. Then i <strong>&quot;tail&quot;</strong> the file five times, and this is what i get from the <strong>log</strong></p>
  <pre><code>/home/P1G3s/WorkSpace/PIT/libfuse-fuse-3.9.2/example/hello  open
  /home/P1G3s/WorkSpace/PIT/libfuse-fuse-3.9.2/example/hello  read
  /home/P1G3s/WorkSpace/PIT/libfuse-fuse-3.9.2/example/hello  open
  /home/P1G3s/WorkSpace/PIT/libfuse-fuse-3.9.2/example/hello  open
  /home/P1G3s/WorkSpace/PIT/libfuse-fuse-3.9.2/example/hello  open
  </code></pre>
  <p>i thought it should not be opening a file that does not exist, but it seems the open() is doing fine and no deadlock happened, however the read wont show up after the first tail.</p>