2016-10-05 21:33:37 +08:00
|
|
|
/*
|
|
|
|
FUSE: Filesystem in Userspace
|
|
|
|
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
|
|
|
|
|
This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
|
See the file COPYING.LIB
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fuse_i.h"
|
|
|
|
#include "fuse_misc.h"
|
2022-11-06 12:25:25 +08:00
|
|
|
#include "fuse_kernel.h"
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2016-10-05 21:33:37 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-11-06 12:25:25 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_chan
|
|
|
|
{
|
|
|
|
struct fuse_session *se;
|
|
|
|
int fd;
|
|
|
|
size_t bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
};
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
struct fuse_session *fuse_session_new(void *data,
|
|
|
|
void *receive_buf,
|
|
|
|
void *process_buf,
|
|
|
|
void *destroy)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
|
|
|
|
if (se == NULL) {
|
|
|
|
fprintf(stderr, "fuse: failed to allocate session\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
memset(se, 0, sizeof(*se));
|
|
|
|
se->data = data;
|
2022-11-06 12:25:25 +08:00
|
|
|
se->receive_buf = receive_buf;
|
|
|
|
se->process_buf = process_buf;
|
|
|
|
se->destroy = destroy;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
assert(se->ch == NULL);
|
|
|
|
assert(ch->se == NULL);
|
|
|
|
se->ch = ch;
|
|
|
|
ch->se = se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void fuse_session_remove_chan(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
struct fuse_session *se = ch->se;
|
|
|
|
if (se) {
|
|
|
|
assert(se->ch == ch);
|
|
|
|
se->ch = NULL;
|
|
|
|
ch->se = NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_session_destroy(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se->destroy(se->data);
|
|
|
|
if(se->ch != NULL)
|
|
|
|
fuse_chan_destroy(se->ch);
|
|
|
|
free(se);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void fuse_session_reset(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se->exited = 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int
|
|
|
|
fuse_session_exited(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
return se->exited;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_session_exit(struct fuse_session *se_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se_->exited = 1;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void *fuse_session_data(struct fuse_session *se)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
return se->data;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int
|
|
|
|
fuse_session_receive(struct fuse_session *se_,
|
|
|
|
struct fuse_buf *buf_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
return se_->receive_buf(se_,buf_,se_->ch);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_session_process(struct fuse_session *se_,
|
|
|
|
const struct fuse_buf *buf_)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
se_->process_buf(se_->data,buf_,se_->ch);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
struct fuse_chan *
|
|
|
|
fuse_chan_new(int fd,
|
|
|
|
size_t bufsize)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
struct fuse_chan *ch;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
ch = (struct fuse_chan*)malloc(sizeof(*ch));
|
|
|
|
if(ch == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "fuse: failed to allocate channel\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
memset(ch, 0, sizeof(*ch));
|
2022-11-06 12:25:25 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
ch->fd = fd;
|
|
|
|
ch->bufsize = bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fuse_chan_fd(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->fd;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fuse_chan_clearfd(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
int fd = ch->fd;
|
|
|
|
ch->fd = -1;
|
|
|
|
return fd;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t fuse_chan_bufsize(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->bufsize;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
|
|
|
|
{
|
2020-08-01 11:50:47 +08:00
|
|
|
return ch->se;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int
|
|
|
|
fuse_chan_recv(struct fuse_chan *ch,
|
|
|
|
char *buf,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
ssize_t res;
|
|
|
|
struct fuse_session *se = fuse_chan_session(ch);
|
|
|
|
assert(se != NULL);
|
|
|
|
|
|
|
|
restart:
|
|
|
|
res = read(fuse_chan_fd(ch), buf, size);
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
if(fuse_session_exited(se))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if(res == -1)
|
|
|
|
{
|
|
|
|
/* ENOENT means the operation was interrupted, it's safe
|
|
|
|
to restart */
|
|
|
|
if (err == ENOENT)
|
|
|
|
goto restart;
|
|
|
|
|
|
|
|
if(err == ENODEV)
|
|
|
|
{
|
|
|
|
se->exited = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Errors occurring during normal operation: EINTR (read
|
|
|
|
interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
|
|
|
|
umounted) */
|
|
|
|
if(err != EINTR && err != EAGAIN)
|
|
|
|
perror("fuse: reading device");
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((size_t) res < sizeof(struct fuse_in_header))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "short read on fuse device\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
2020-08-02 10:44:00 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
return res;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int
|
|
|
|
fuse_chan_send(struct fuse_chan *ch,
|
|
|
|
const struct iovec iov[],
|
|
|
|
size_t count)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
if(!iov)
|
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
int err;
|
|
|
|
ssize_t res;
|
2016-10-05 21:33:37 +08:00
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
res = writev(fuse_chan_fd(ch), iov, count);
|
|
|
|
err = errno;
|
|
|
|
|
|
|
|
if(res == -1)
|
|
|
|
{
|
|
|
|
struct fuse_session *se = fuse_chan_session(ch);
|
|
|
|
|
|
|
|
assert(se != NULL);
|
|
|
|
|
|
|
|
/* ENOENT means the operation was interrupted */
|
|
|
|
if(!fuse_session_exited(se) && err != ENOENT)
|
|
|
|
perror("fuse: writing device");
|
|
|
|
return -err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|
|
|
|
|
2022-11-06 12:25:25 +08:00
|
|
|
void
|
|
|
|
fuse_chan_destroy(struct fuse_chan *ch)
|
2016-10-05 21:33:37 +08:00
|
|
|
{
|
2022-11-06 12:25:25 +08:00
|
|
|
int fd;
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
fuse_session_remove_chan(ch);
|
2022-11-06 12:25:25 +08:00
|
|
|
|
|
|
|
fd = fuse_chan_fd(ch);
|
|
|
|
if(fd != -1)
|
|
|
|
close(fd);
|
|
|
|
|
2020-08-01 11:50:47 +08:00
|
|
|
free(ch);
|
2016-10-05 21:33:37 +08:00
|
|
|
}
|