Skip to content

Commit

Permalink
Add clone_fd to custom IO (#927)
Browse files Browse the repository at this point in the history
Define a new clone_fd() helper for fuse_custom_io, users
can implement their own clone fd logic.

Signed-off-by: Xiaoguang Wang <lege.wang@jaguarmicro.com>
  • Loading branch information
legezywzh committed Apr 18, 2024
1 parent 0800773 commit 73cd124
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/fuse_lowlevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ struct fuse_custom_io {
ssize_t (*splice_send)(int fdin, off_t *offin, int fdout,
off_t *offout, size_t len,
unsigned int flags, void *userdata);
int (*clone_fd)(int master_fd);
};

/**
Expand Down
29 changes: 24 additions & 5 deletions lib/fuse_loop_mt.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,11 @@ int fuse_start_thread(pthread_t *thread_id, void *(*func)(void *), void *arg)
return 0;
}

static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
static int fuse_clone_chan_fd_default(struct fuse_session *se)
{
int res;
int clonefd;
uint32_t masterfd;
struct fuse_chan *newch;
const char *devname = "/dev/fuse";

#ifndef O_CLOEXEC
Expand All @@ -257,20 +256,40 @@ static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
if (clonefd == -1) {
fuse_log(FUSE_LOG_ERR, "fuse: failed to open %s: %s\n", devname,
strerror(errno));
return NULL;
return -1;
}
#ifndef O_CLOEXEC
fcntl(clonefd, F_SETFD, FD_CLOEXEC);
#endif

masterfd = mt->se->fd;
masterfd = se->fd;
res = ioctl(clonefd, FUSE_DEV_IOC_CLONE, &masterfd);
if (res == -1) {
fuse_log(FUSE_LOG_ERR, "fuse: failed to clone device fd: %s\n",
strerror(errno));
close(clonefd);
return NULL;
return -1;
}
return clonefd;
}

static struct fuse_chan *fuse_clone_chan(struct fuse_mt *mt)
{
int clonefd;
struct fuse_session *se = mt->se;
struct fuse_chan *newch;

if (se->io != NULL) {
if (se->io->clone_fd != NULL)
clonefd = se->io->clone_fd(se->fd);
else
return NULL;
} else {
clonefd = fuse_clone_chan_fd_default(se);
}
if (clonefd < 0)
return NULL;

newch = fuse_chan_new(clonefd);
if (newch == NULL)
close(clonefd);
Expand Down

0 comments on commit 73cd124

Please sign in to comment.