Read data from fd which was received from fusermount failed

266 Views Asked by At

I want to test fuse and copy some code from libfuse like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>

#define xlog(_fmt_, ...)                                                \
  printf("\033[0;33m[%s:%s:%d]\033[0m " _fmt_ "\n", __FILE__, __func__, \
         __LINE__, ##__VA_ARGS__)

static int receive_fd(int fd) {
  struct msghdr msg;
  struct iovec iov;
  char buf[1];
  int rv;
  size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)];
  struct cmsghdr *cmsg;

  iov.iov_base = buf;
  iov.iov_len = 1;

  memset(&msg, 0, sizeof(msg));
  msg.msg_name = 0;
  msg.msg_namelen = 0;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = ccmsg;
  msg.msg_controllen = sizeof(ccmsg);

  while (((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR) {
  }
  if (rv == -1) {
    xlog("recvmsg: %s", strerror(errno));
    return -1;
  }
  if (!rv) {
    return -1;
  }

  cmsg = CMSG_FIRSTHDR(&msg);
  if (cmsg->cmsg_type != SCM_RIGHTS) {
    xlog("got control message of unknown type %d", cmsg->cmsg_type);
    return -1;
  }
  return *(int *)CMSG_DATA(cmsg);
}

static int fuse_mount(const char *mountpoint) {
  int fds[2];
  int res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds);
  if (res != 0) {
    xlog("socketpair: %s", strerror(errno));
    return -1;
  }

  pid_t pid = fork();
  if (pid == -1) {
    close(fds[0]);
    close(fds[1]);
    return -1;
  }

  if (pid == 0) {
    const char *argv[32];
    unsigned argc = 0;
    argv[argc++] = "--";
    argv[argc++] = mountpoint;
    argv[argc++] = NULL;

    char env[16];
    snprintf(env, sizeof(env), "%i", fds[0]);
    setenv("_FUSE_COMMFD", env, 1);
    execvp("fusermount", (char **)argv);

    xlog("exec: %s", strerror(errno));
    exit(1);
  }

  int rv = receive_fd(fds[1]);
  if (rv >= 0) {
    fcntl(rv, F_SETFD, FD_CLOEXEC);
  }
  waitpid(pid, NULL, 0);

  close(fds[0]);
  close(fds[1]);
  return rv;
}

#define MOUNT_POINT "/tmp/xx"
#define BUFFER_SIZE 1024

int main(int argc, char **argv) {
  int fd = fuse_mount(MOUNT_POINT);
  if (fd < 0) {
    xlog("fuse mount fail");
    return -1;
  }

  char buf[BUFFER_SIZE];
  ssize_t n = read(fd, buf, sizeof(buf));
  if (n < 0) {
    xlog("read: %s", strerror(errno));
  } else {
    xlog("ok");
  }

  return 0;
}

It received the /dev/fuse filehandle from fusermount and read data from it.

The code run ok on most platforms, but when I run it at virtualbox, I got the error read: Invalid argument at line 107:

ssize_t n = read(fd, buf, sizeof(buf));

Then I check the code of libfuse, it set the buffer size like this:

se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE;

So I set a larger BUFFER_SIZE like 10240 in my code and it run ok. I feel confused because I think 1024 was large enough and the data size I read from fd was only 56.

My question is in system call:

ssize_t read(int fd, void *buf, size_t count);
  1. Why the size of count would cause Invalid argument error?
  2. Why it was ok when I set count to 1024 on normal platforms, but it was failed on virtualbox?
1

There are 1 best solutions below

0
On BEST ANSWER

Thanks for @user253751 tip, I read the code fs/fuse/dev.c and found this:

static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
                                struct fuse_copy_state *cs, size_t nbytes) {
  // ...
  if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
                     sizeof(struct fuse_in_header) +
                         sizeof(struct fuse_write_in) + fc->max_write))
    return -EINVAL;
  // ...
}

The nbytes check was added since linux v5.4, so if the kernel version newer than v5.3.18, the small nbytes would cause EINVAL error.