|
|
ddf19c |
From 55b4059d6399c212109c758190e15b574accdd07 Mon Sep 17 00:00:00 2001
|
|
|
ddf19c |
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
|
|
|
ddf19c |
Date: Mon, 27 Jan 2020 19:00:41 +0100
|
|
|
ddf19c |
Subject: [PATCH 010/116] virtiofsd: Add auxiliary .c's
|
|
|
ddf19c |
MIME-Version: 1.0
|
|
|
ddf19c |
Content-Type: text/plain; charset=UTF-8
|
|
|
ddf19c |
Content-Transfer-Encoding: 8bit
|
|
|
ddf19c |
|
|
|
ddf19c |
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
|
|
|
ddf19c |
Message-id: <20200127190227.40942-7-dgilbert@redhat.com>
|
|
|
ddf19c |
Patchwork-id: 93461
|
|
|
ddf19c |
O-Subject: [RHEL-AV-8.2 qemu-kvm PATCH 006/112] virtiofsd: Add auxiliary .c's
|
|
|
ddf19c |
Bugzilla: 1694164
|
|
|
ddf19c |
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
|
|
|
ddf19c |
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
|
|
|
ddf19c |
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
|
|
|
ddf19c |
|
|
|
ddf19c |
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
|
|
|
ddf19c |
|
|
|
ddf19c |
Add most of the non-main .c files we need from upstream fuse-3.8.0
|
|
|
ddf19c |
|
|
|
ddf19c |
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
|
|
|
ddf19c |
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
|
|
|
ddf19c |
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
|
|
|
ddf19c |
(cherry picked from commit ffcf8d9f8649c6e56b1193bbbc9c9f7388920043)
|
|
|
ddf19c |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
ddf19c |
---
|
|
|
ddf19c |
tools/virtiofsd/buffer.c | 321 ++++++++++++++++++++++++++++++
|
|
|
ddf19c |
tools/virtiofsd/fuse_log.c | 40 ++++
|
|
|
ddf19c |
tools/virtiofsd/fuse_opt.c | 423 +++++++++++++++++++++++++++++++++++++++
|
|
|
ddf19c |
tools/virtiofsd/fuse_signals.c | 91 +++++++++
|
|
|
ddf19c |
tools/virtiofsd/helper.c | 440 +++++++++++++++++++++++++++++++++++++++++
|
|
|
ddf19c |
5 files changed, 1315 insertions(+)
|
|
|
ddf19c |
create mode 100644 tools/virtiofsd/buffer.c
|
|
|
ddf19c |
create mode 100644 tools/virtiofsd/fuse_log.c
|
|
|
ddf19c |
create mode 100644 tools/virtiofsd/fuse_opt.c
|
|
|
ddf19c |
create mode 100644 tools/virtiofsd/fuse_signals.c
|
|
|
ddf19c |
create mode 100644 tools/virtiofsd/helper.c
|
|
|
ddf19c |
|
|
|
ddf19c |
diff --git a/tools/virtiofsd/buffer.c b/tools/virtiofsd/buffer.c
|
|
|
ddf19c |
new file mode 100644
|
|
|
ddf19c |
index 0000000..5ab9b87
|
|
|
ddf19c |
--- /dev/null
|
|
|
ddf19c |
+++ b/tools/virtiofsd/buffer.c
|
|
|
ddf19c |
@@ -0,0 +1,321 @@
|
|
|
ddf19c |
+/*
|
|
|
ddf19c |
+ FUSE: Filesystem in Userspace
|
|
|
ddf19c |
+ Copyright (C) 2010 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ Functions for dealing with `struct fuse_buf` and `struct
|
|
|
ddf19c |
+ fuse_bufvec`.
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
ddf19c |
+ See the file COPYING.LIB
|
|
|
ddf19c |
+*/
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#define _GNU_SOURCE
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include "config.h"
|
|
|
ddf19c |
+#include "fuse_i.h"
|
|
|
ddf19c |
+#include "fuse_lowlevel.h"
|
|
|
ddf19c |
+#include <string.h>
|
|
|
ddf19c |
+#include <unistd.h>
|
|
|
ddf19c |
+#include <errno.h>
|
|
|
ddf19c |
+#include <assert.h>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+size_t fuse_buf_size(const struct fuse_bufvec *bufv)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ size_t i;
|
|
|
ddf19c |
+ size_t size = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ for (i = 0; i < bufv->count; i++) {
|
|
|
ddf19c |
+ if (bufv->buf[i].size == SIZE_MAX)
|
|
|
ddf19c |
+ size = SIZE_MAX;
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ size += bufv->buf[i].size;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return size;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static size_t min_size(size_t s1, size_t s2)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return s1 < s2 ? s1 : s2;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ ssize_t res = 0;
|
|
|
ddf19c |
+ size_t copied = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ while (len) {
|
|
|
ddf19c |
+ if (dst->flags & FUSE_BUF_FD_SEEK) {
|
|
|
ddf19c |
+ res = pwrite(dst->fd, (char *)src->mem + src_off, len,
|
|
|
ddf19c |
+ dst->pos + dst_off);
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ res = write(dst->fd, (char *)src->mem + src_off, len);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == -1) {
|
|
|
ddf19c |
+ if (!copied)
|
|
|
ddf19c |
+ return -errno;
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == 0)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ copied += res;
|
|
|
ddf19c |
+ if (!(dst->flags & FUSE_BUF_FD_RETRY))
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ src_off += res;
|
|
|
ddf19c |
+ dst_off += res;
|
|
|
ddf19c |
+ len -= res;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return copied;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ ssize_t res = 0;
|
|
|
ddf19c |
+ size_t copied = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ while (len) {
|
|
|
ddf19c |
+ if (src->flags & FUSE_BUF_FD_SEEK) {
|
|
|
ddf19c |
+ res = pread(src->fd, (char *)dst->mem + dst_off, len,
|
|
|
ddf19c |
+ src->pos + src_off);
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ res = read(src->fd, (char *)dst->mem + dst_off, len);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == -1) {
|
|
|
ddf19c |
+ if (!copied)
|
|
|
ddf19c |
+ return -errno;
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == 0)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ copied += res;
|
|
|
ddf19c |
+ if (!(src->flags & FUSE_BUF_FD_RETRY))
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ dst_off += res;
|
|
|
ddf19c |
+ src_off += res;
|
|
|
ddf19c |
+ len -= res;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return copied;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ char buf[4096];
|
|
|
ddf19c |
+ struct fuse_buf tmp = {
|
|
|
ddf19c |
+ .size = sizeof(buf),
|
|
|
ddf19c |
+ .flags = 0,
|
|
|
ddf19c |
+ };
|
|
|
ddf19c |
+ ssize_t res;
|
|
|
ddf19c |
+ size_t copied = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ tmp.mem = buf;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ while (len) {
|
|
|
ddf19c |
+ size_t this_len = min_size(tmp.size, len);
|
|
|
ddf19c |
+ size_t read_len;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
|
|
|
ddf19c |
+ if (res < 0) {
|
|
|
ddf19c |
+ if (!copied)
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == 0)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ read_len = res;
|
|
|
ddf19c |
+ res = fuse_buf_write(dst, dst_off, &tmp, 0, read_len);
|
|
|
ddf19c |
+ if (res < 0) {
|
|
|
ddf19c |
+ if (!copied)
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == 0)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ copied += res;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (res < this_len)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ dst_off += res;
|
|
|
ddf19c |
+ src_off += res;
|
|
|
ddf19c |
+ len -= res;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return copied;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#ifdef HAVE_SPLICE
|
|
|
ddf19c |
+static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len, enum fuse_buf_copy_flags flags)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int splice_flags = 0;
|
|
|
ddf19c |
+ off_t *srcpos = NULL;
|
|
|
ddf19c |
+ off_t *dstpos = NULL;
|
|
|
ddf19c |
+ off_t srcpos_val;
|
|
|
ddf19c |
+ off_t dstpos_val;
|
|
|
ddf19c |
+ ssize_t res;
|
|
|
ddf19c |
+ size_t copied = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (flags & FUSE_BUF_SPLICE_MOVE)
|
|
|
ddf19c |
+ splice_flags |= SPLICE_F_MOVE;
|
|
|
ddf19c |
+ if (flags & FUSE_BUF_SPLICE_NONBLOCK)
|
|
|
ddf19c |
+ splice_flags |= SPLICE_F_NONBLOCK;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (src->flags & FUSE_BUF_FD_SEEK) {
|
|
|
ddf19c |
+ srcpos_val = src->pos + src_off;
|
|
|
ddf19c |
+ srcpos = &srcpos_val;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (dst->flags & FUSE_BUF_FD_SEEK) {
|
|
|
ddf19c |
+ dstpos_val = dst->pos + dst_off;
|
|
|
ddf19c |
+ dstpos = &dstpos_val;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ while (len) {
|
|
|
ddf19c |
+ res = splice(src->fd, srcpos, dst->fd, dstpos, len,
|
|
|
ddf19c |
+ splice_flags);
|
|
|
ddf19c |
+ if (res == -1) {
|
|
|
ddf19c |
+ if (copied)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (errno != EINVAL || (flags & FUSE_BUF_FORCE_SPLICE))
|
|
|
ddf19c |
+ return -errno;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ /* Maybe splice is not supported for this combination */
|
|
|
ddf19c |
+ return fuse_buf_fd_to_fd(dst, dst_off, src, src_off,
|
|
|
ddf19c |
+ len);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res == 0)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ copied += res;
|
|
|
ddf19c |
+ if (!(src->flags & FUSE_BUF_FD_RETRY) &&
|
|
|
ddf19c |
+ !(dst->flags & FUSE_BUF_FD_RETRY)) {
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ len -= res;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return copied;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+#else
|
|
|
ddf19c |
+static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len, enum fuse_buf_copy_flags flags)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ (void) flags;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+#endif
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
|
|
|
ddf19c |
+ const struct fuse_buf *src, size_t src_off,
|
|
|
ddf19c |
+ size_t len, enum fuse_buf_copy_flags flags)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int src_is_fd = src->flags & FUSE_BUF_IS_FD;
|
|
|
ddf19c |
+ int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!src_is_fd && !dst_is_fd) {
|
|
|
ddf19c |
+ char *dstmem = (char *)dst->mem + dst_off;
|
|
|
ddf19c |
+ char *srcmem = (char *)src->mem + src_off;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (dstmem != srcmem) {
|
|
|
ddf19c |
+ if (dstmem + len <= srcmem || srcmem + len <= dstmem)
|
|
|
ddf19c |
+ memcpy(dstmem, srcmem, len);
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ memmove(dstmem, srcmem, len);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return len;
|
|
|
ddf19c |
+ } else if (!src_is_fd) {
|
|
|
ddf19c |
+ return fuse_buf_write(dst, dst_off, src, src_off, len);
|
|
|
ddf19c |
+ } else if (!dst_is_fd) {
|
|
|
ddf19c |
+ return fuse_buf_read(dst, dst_off, src, src_off, len);
|
|
|
ddf19c |
+ } else if (flags & FUSE_BUF_NO_SPLICE) {
|
|
|
ddf19c |
+ return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ return fuse_buf_splice(dst, dst_off, src, src_off, len, flags);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (bufv->idx < bufv->count)
|
|
|
ddf19c |
+ return &bufv->buf[bufv->idx];
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ return NULL;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ const struct fuse_buf *buf = fuse_bufvec_current(bufv);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ bufv->off += len;
|
|
|
ddf19c |
+ assert(bufv->off <= buf->size);
|
|
|
ddf19c |
+ if (bufv->off == buf->size) {
|
|
|
ddf19c |
+ assert(bufv->idx < bufv->count);
|
|
|
ddf19c |
+ bufv->idx++;
|
|
|
ddf19c |
+ if (bufv->idx == bufv->count)
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+ bufv->off = 0;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 1;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv,
|
|
|
ddf19c |
+ enum fuse_buf_copy_flags flags)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ size_t copied = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (dstv == srcv)
|
|
|
ddf19c |
+ return fuse_buf_size(dstv);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ for (;;) {
|
|
|
ddf19c |
+ const struct fuse_buf *src = fuse_bufvec_current(srcv);
|
|
|
ddf19c |
+ const struct fuse_buf *dst = fuse_bufvec_current(dstv);
|
|
|
ddf19c |
+ size_t src_len;
|
|
|
ddf19c |
+ size_t dst_len;
|
|
|
ddf19c |
+ size_t len;
|
|
|
ddf19c |
+ ssize_t res;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (src == NULL || dst == NULL)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ src_len = src->size - srcv->off;
|
|
|
ddf19c |
+ dst_len = dst->size - dstv->off;
|
|
|
ddf19c |
+ len = min_size(src_len, dst_len);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len, flags);
|
|
|
ddf19c |
+ if (res < 0) {
|
|
|
ddf19c |
+ if (!copied)
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ copied += res;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!fuse_bufvec_advance(srcv, res) ||
|
|
|
ddf19c |
+ !fuse_bufvec_advance(dstv, res))
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (res < len)
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return copied;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
diff --git a/tools/virtiofsd/fuse_log.c b/tools/virtiofsd/fuse_log.c
|
|
|
ddf19c |
new file mode 100644
|
|
|
ddf19c |
index 0000000..0d268ab
|
|
|
ddf19c |
--- /dev/null
|
|
|
ddf19c |
+++ b/tools/virtiofsd/fuse_log.c
|
|
|
ddf19c |
@@ -0,0 +1,40 @@
|
|
|
ddf19c |
+/*
|
|
|
ddf19c |
+ FUSE: Filesystem in Userspace
|
|
|
ddf19c |
+ Copyright (C) 2019 Red Hat, Inc.
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ Logging API.
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
ddf19c |
+ See the file COPYING.LIB
|
|
|
ddf19c |
+*/
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include "fuse_log.h"
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include <stdarg.h>
|
|
|
ddf19c |
+#include <stdio.h>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static void default_log_func(
|
|
|
ddf19c |
+ __attribute__(( unused )) enum fuse_log_level level,
|
|
|
ddf19c |
+ const char *fmt, va_list ap)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ vfprintf(stderr, fmt, ap);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static fuse_log_func_t log_func = default_log_func;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_set_log_func(fuse_log_func_t func)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (!func)
|
|
|
ddf19c |
+ func = default_log_func;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ log_func = func;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_log(enum fuse_log_level level, const char *fmt, ...)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ va_list ap;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ va_start(ap, fmt);
|
|
|
ddf19c |
+ log_func(level, fmt, ap);
|
|
|
ddf19c |
+ va_end(ap);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
diff --git a/tools/virtiofsd/fuse_opt.c b/tools/virtiofsd/fuse_opt.c
|
|
|
ddf19c |
new file mode 100644
|
|
|
ddf19c |
index 0000000..93066b9
|
|
|
ddf19c |
--- /dev/null
|
|
|
ddf19c |
+++ b/tools/virtiofsd/fuse_opt.c
|
|
|
ddf19c |
@@ -0,0 +1,423 @@
|
|
|
ddf19c |
+/*
|
|
|
ddf19c |
+ FUSE: Filesystem in Userspace
|
|
|
ddf19c |
+ Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ Implementation of option parsing routines (dealing with `struct
|
|
|
ddf19c |
+ fuse_args`).
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
ddf19c |
+ See the file COPYING.LIB
|
|
|
ddf19c |
+*/
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include "config.h"
|
|
|
ddf19c |
+#include "fuse_i.h"
|
|
|
ddf19c |
+#include "fuse_opt.h"
|
|
|
ddf19c |
+#include "fuse_misc.h"
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include <stdio.h>
|
|
|
ddf19c |
+#include <stdlib.h>
|
|
|
ddf19c |
+#include <string.h>
|
|
|
ddf19c |
+#include <assert.h>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+struct fuse_opt_context {
|
|
|
ddf19c |
+ void *data;
|
|
|
ddf19c |
+ const struct fuse_opt *opt;
|
|
|
ddf19c |
+ fuse_opt_proc_t proc;
|
|
|
ddf19c |
+ int argctr;
|
|
|
ddf19c |
+ int argc;
|
|
|
ddf19c |
+ char **argv;
|
|
|
ddf19c |
+ struct fuse_args outargs;
|
|
|
ddf19c |
+ char *opts;
|
|
|
ddf19c |
+ int nonopt;
|
|
|
ddf19c |
+};
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_opt_free_args(struct fuse_args *args)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (args) {
|
|
|
ddf19c |
+ if (args->argv && args->allocated) {
|
|
|
ddf19c |
+ int i;
|
|
|
ddf19c |
+ for (i = 0; i < args->argc; i++)
|
|
|
ddf19c |
+ free(args->argv[i]);
|
|
|
ddf19c |
+ free(args->argv);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ args->argc = 0;
|
|
|
ddf19c |
+ args->argv = NULL;
|
|
|
ddf19c |
+ args->allocated = 0;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int alloc_failed(void)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ char **newargv;
|
|
|
ddf19c |
+ char *newarg;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ assert(!args->argv || args->allocated);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ newarg = strdup(arg);
|
|
|
ddf19c |
+ if (!newarg)
|
|
|
ddf19c |
+ return alloc_failed();
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *));
|
|
|
ddf19c |
+ if (!newargv) {
|
|
|
ddf19c |
+ free(newarg);
|
|
|
ddf19c |
+ return alloc_failed();
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ args->argv = newargv;
|
|
|
ddf19c |
+ args->allocated = 1;
|
|
|
ddf19c |
+ args->argv[args->argc++] = newarg;
|
|
|
ddf19c |
+ args->argv[args->argc] = NULL;
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos,
|
|
|
ddf19c |
+ const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ assert(pos <= args->argc);
|
|
|
ddf19c |
+ if (fuse_opt_add_arg(args, arg) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (pos != args->argc - 1) {
|
|
|
ddf19c |
+ char *newarg = args->argv[args->argc - 1];
|
|
|
ddf19c |
+ memmove(&args->argv[pos + 1], &args->argv[pos],
|
|
|
ddf19c |
+ sizeof(char *) * (args->argc - pos - 1));
|
|
|
ddf19c |
+ args->argv[pos] = newarg;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return fuse_opt_insert_arg_common(args, pos, arg);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int next_arg(struct fuse_opt_context *ctx, const char *opt)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (ctx->argctr + 1 >= ctx->argc) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: missing argument after `%s'\n", opt);
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ ctx->argctr++;
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int add_arg(struct fuse_opt_context *ctx, const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return fuse_opt_add_arg(&ctx->outargs, arg);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int add_opt_common(char **opts, const char *opt, int esc)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ unsigned oldlen = *opts ? strlen(*opts) : 0;
|
|
|
ddf19c |
+ char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!d)
|
|
|
ddf19c |
+ return alloc_failed();
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ *opts = d;
|
|
|
ddf19c |
+ if (oldlen) {
|
|
|
ddf19c |
+ d += oldlen;
|
|
|
ddf19c |
+ *d++ = ',';
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ for (; *opt; opt++) {
|
|
|
ddf19c |
+ if (esc && (*opt == ',' || *opt == '\\'))
|
|
|
ddf19c |
+ *d++ = '\\';
|
|
|
ddf19c |
+ *d++ = *opt;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ *d = '\0';
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_add_opt(char **opts, const char *opt)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return add_opt_common(opts, opt, 0);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_add_opt_escaped(char **opts, const char *opt)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return add_opt_common(opts, opt, 1);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int add_opt(struct fuse_opt_context *ctx, const char *opt)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ return add_opt_common(&ctx->opts, opt, 1);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key,
|
|
|
ddf19c |
+ int iso)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (key == FUSE_OPT_KEY_DISCARD)
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (key != FUSE_OPT_KEY_KEEP && ctx->proc) {
|
|
|
ddf19c |
+ int res = ctx->proc(ctx->data, arg, key, &ctx->outargs);
|
|
|
ddf19c |
+ if (res == -1 || !res)
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (iso)
|
|
|
ddf19c |
+ return add_opt(ctx, arg);
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ return add_arg(ctx, arg);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int match_template(const char *t, const char *arg, unsigned *sepp)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int arglen = strlen(arg);
|
|
|
ddf19c |
+ const char *sep = strchr(t, '=');
|
|
|
ddf19c |
+ sep = sep ? sep : strchr(t, ' ');
|
|
|
ddf19c |
+ if (sep && (!sep[1] || sep[1] == '%')) {
|
|
|
ddf19c |
+ int tlen = sep - t;
|
|
|
ddf19c |
+ if (sep[0] == '=')
|
|
|
ddf19c |
+ tlen ++;
|
|
|
ddf19c |
+ if (arglen >= tlen && strncmp(arg, t, tlen) == 0) {
|
|
|
ddf19c |
+ *sepp = sep - t;
|
|
|
ddf19c |
+ return 1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (strcmp(t, arg) == 0) {
|
|
|
ddf19c |
+ *sepp = 0;
|
|
|
ddf19c |
+ return 1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static const struct fuse_opt *find_opt(const struct fuse_opt *opt,
|
|
|
ddf19c |
+ const char *arg, unsigned *sepp)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ for (; opt && opt->templ; opt++)
|
|
|
ddf19c |
+ if (match_template(opt->templ, arg, sepp))
|
|
|
ddf19c |
+ return opt;
|
|
|
ddf19c |
+ return NULL;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_match(const struct fuse_opt *opts, const char *opt)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ unsigned dummy;
|
|
|
ddf19c |
+ return find_opt(opts, opt, &dummy) ? 1 : 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_opt_param(void *var, const char *format, const char *param,
|
|
|
ddf19c |
+ const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ assert(format[0] == '%');
|
|
|
ddf19c |
+ if (format[1] == 's') {
|
|
|
ddf19c |
+ char **s = var;
|
|
|
ddf19c |
+ char *copy = strdup(param);
|
|
|
ddf19c |
+ if (!copy)
|
|
|
ddf19c |
+ return alloc_failed();
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ free(*s);
|
|
|
ddf19c |
+ *s = copy;
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ if (sscanf(param, format, var) != 1) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: invalid parameter in option `%s'\n", arg);
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_opt(struct fuse_opt_context *ctx,
|
|
|
ddf19c |
+ const struct fuse_opt *opt, unsigned sep,
|
|
|
ddf19c |
+ const char *arg, int iso)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (opt->offset == -1U) {
|
|
|
ddf19c |
+ if (call_proc(ctx, arg, opt->value, iso) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ void *var = (char *)ctx->data + opt->offset;
|
|
|
ddf19c |
+ if (sep && opt->templ[sep + 1]) {
|
|
|
ddf19c |
+ const char *param = arg + sep;
|
|
|
ddf19c |
+ if (opt->templ[sep] == '=')
|
|
|
ddf19c |
+ param ++;
|
|
|
ddf19c |
+ if (process_opt_param(var, opt->templ + sep + 1,
|
|
|
ddf19c |
+ param, arg) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ } else
|
|
|
ddf19c |
+ *(int *)var = opt->value;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_opt_sep_arg(struct fuse_opt_context *ctx,
|
|
|
ddf19c |
+ const struct fuse_opt *opt, unsigned sep,
|
|
|
ddf19c |
+ const char *arg, int iso)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+ char *newarg;
|
|
|
ddf19c |
+ char *param;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (next_arg(ctx, arg) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ param = ctx->argv[ctx->argctr];
|
|
|
ddf19c |
+ newarg = malloc(sep + strlen(param) + 1);
|
|
|
ddf19c |
+ if (!newarg)
|
|
|
ddf19c |
+ return alloc_failed();
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ memcpy(newarg, arg, sep);
|
|
|
ddf19c |
+ strcpy(newarg + sep, param);
|
|
|
ddf19c |
+ res = process_opt(ctx, opt, sep, newarg, iso);
|
|
|
ddf19c |
+ free(newarg);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ unsigned sep;
|
|
|
ddf19c |
+ const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep;;
|
|
|
ddf19c |
+ if (opt) {
|
|
|
ddf19c |
+ for (; opt; opt = find_opt(opt + 1, arg, &sep)) {
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+ if (sep && opt->templ[sep] == ' ' && !arg[sep])
|
|
|
ddf19c |
+ res = process_opt_sep_arg(ctx, opt, sep, arg,
|
|
|
ddf19c |
+ iso);
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ res = process_opt(ctx, opt, sep, arg, iso);
|
|
|
ddf19c |
+ if (res == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+ } else
|
|
|
ddf19c |
+ return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_real_option_group(struct fuse_opt_context *ctx, char *opts)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ char *s = opts;
|
|
|
ddf19c |
+ char *d = s;
|
|
|
ddf19c |
+ int end = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ while (!end) {
|
|
|
ddf19c |
+ if (*s == '\0')
|
|
|
ddf19c |
+ end = 1;
|
|
|
ddf19c |
+ if (*s == ',' || end) {
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ *d = '\0';
|
|
|
ddf19c |
+ res = process_gopt(ctx, opts, 1);
|
|
|
ddf19c |
+ if (res == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ d = opts;
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ if (s[0] == '\\' && s[1] != '\0') {
|
|
|
ddf19c |
+ s++;
|
|
|
ddf19c |
+ if (s[0] >= '0' && s[0] <= '3' &&
|
|
|
ddf19c |
+ s[1] >= '0' && s[1] <= '7' &&
|
|
|
ddf19c |
+ s[2] >= '0' && s[2] <= '7') {
|
|
|
ddf19c |
+ *d++ = (s[0] - '0') * 0100 +
|
|
|
ddf19c |
+ (s[1] - '0') * 0010 +
|
|
|
ddf19c |
+ (s[2] - '0');
|
|
|
ddf19c |
+ s += 2;
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ *d++ = *s;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ *d++ = *s;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ s++;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_option_group(struct fuse_opt_context *ctx, const char *opts)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+ char *copy = strdup(opts);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!copy) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ res = process_real_option_group(ctx, copy);
|
|
|
ddf19c |
+ free(copy);
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int process_one(struct fuse_opt_context *ctx, const char *arg)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (ctx->nonopt || arg[0] != '-')
|
|
|
ddf19c |
+ return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0);
|
|
|
ddf19c |
+ else if (arg[1] == 'o') {
|
|
|
ddf19c |
+ if (arg[2])
|
|
|
ddf19c |
+ return process_option_group(ctx, arg + 2);
|
|
|
ddf19c |
+ else {
|
|
|
ddf19c |
+ if (next_arg(ctx, arg) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return process_option_group(ctx,
|
|
|
ddf19c |
+ ctx->argv[ctx->argctr]);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ } else if (arg[1] == '-' && !arg[2]) {
|
|
|
ddf19c |
+ if (add_arg(ctx, arg) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ ctx->nonopt = ctx->outargs.argc;
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+ } else
|
|
|
ddf19c |
+ return process_gopt(ctx, arg, 0);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int opt_parse(struct fuse_opt_context *ctx)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (ctx->argc) {
|
|
|
ddf19c |
+ if (add_arg(ctx, ctx->argv[0]) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++)
|
|
|
ddf19c |
+ if (process_one(ctx, ctx->argv[ctx->argctr]) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (ctx->opts) {
|
|
|
ddf19c |
+ if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 ||
|
|
|
ddf19c |
+ fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ /* If option separator ("--") is the last argument, remove it */
|
|
|
ddf19c |
+ if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc &&
|
|
|
ddf19c |
+ strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) {
|
|
|
ddf19c |
+ free(ctx->outargs.argv[ctx->outargs.argc - 1]);
|
|
|
ddf19c |
+ ctx->outargs.argv[--ctx->outargs.argc] = NULL;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_opt_parse(struct fuse_args *args, void *data,
|
|
|
ddf19c |
+ const struct fuse_opt opts[], fuse_opt_proc_t proc)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+ struct fuse_opt_context ctx = {
|
|
|
ddf19c |
+ .data = data,
|
|
|
ddf19c |
+ .opt = opts,
|
|
|
ddf19c |
+ .proc = proc,
|
|
|
ddf19c |
+ };
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!args || !args->argv || !args->argc)
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ ctx.argc = args->argc;
|
|
|
ddf19c |
+ ctx.argv = args->argv;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ res = opt_parse(&ctx;;
|
|
|
ddf19c |
+ if (res != -1) {
|
|
|
ddf19c |
+ struct fuse_args tmp = *args;
|
|
|
ddf19c |
+ *args = ctx.outargs;
|
|
|
ddf19c |
+ ctx.outargs = tmp;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ free(ctx.opts);
|
|
|
ddf19c |
+ fuse_opt_free_args(&ctx.outargs);
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
diff --git a/tools/virtiofsd/fuse_signals.c b/tools/virtiofsd/fuse_signals.c
|
|
|
ddf19c |
new file mode 100644
|
|
|
ddf19c |
index 0000000..4271947
|
|
|
ddf19c |
--- /dev/null
|
|
|
ddf19c |
+++ b/tools/virtiofsd/fuse_signals.c
|
|
|
ddf19c |
@@ -0,0 +1,91 @@
|
|
|
ddf19c |
+/*
|
|
|
ddf19c |
+ FUSE: Filesystem in Userspace
|
|
|
ddf19c |
+ Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ Utility functions for setting signal handlers.
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
ddf19c |
+ See the file COPYING.LIB
|
|
|
ddf19c |
+*/
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include "config.h"
|
|
|
ddf19c |
+#include "fuse_lowlevel.h"
|
|
|
ddf19c |
+#include "fuse_i.h"
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include <stdio.h>
|
|
|
ddf19c |
+#include <string.h>
|
|
|
ddf19c |
+#include <signal.h>
|
|
|
ddf19c |
+#include <stdlib.h>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static struct fuse_session *fuse_instance;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static void exit_handler(int sig)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (fuse_instance) {
|
|
|
ddf19c |
+ fuse_session_exit(fuse_instance);
|
|
|
ddf19c |
+ if(sig <= 0) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "assertion error: signal value <= 0\n");
|
|
|
ddf19c |
+ abort();
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ fuse_instance->error = sig;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static void do_nothing(int sig)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ (void) sig;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int set_one_signal_handler(int sig, void (*handler)(int), int remove)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ struct sigaction sa;
|
|
|
ddf19c |
+ struct sigaction old_sa;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ memset(&sa, 0, sizeof(struct sigaction));
|
|
|
ddf19c |
+ sa.sa_handler = remove ? SIG_DFL : handler;
|
|
|
ddf19c |
+ sigemptyset(&(sa.sa_mask));
|
|
|
ddf19c |
+ sa.sa_flags = 0;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (sigaction(sig, NULL, &old_sa) == -1) {
|
|
|
ddf19c |
+ perror("fuse: cannot get old signal handler");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (old_sa.sa_handler == (remove ? handler : SIG_DFL) &&
|
|
|
ddf19c |
+ sigaction(sig, &sa, NULL) == -1) {
|
|
|
ddf19c |
+ perror("fuse: cannot set signal handler");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_set_signal_handlers(struct fuse_session *se)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ /* If we used SIG_IGN instead of the do_nothing function,
|
|
|
ddf19c |
+ then we would be unable to tell if we set SIG_IGN (and
|
|
|
ddf19c |
+ thus should reset to SIG_DFL in fuse_remove_signal_handlers)
|
|
|
ddf19c |
+ or if it was already set to SIG_IGN (and should be left
|
|
|
ddf19c |
+ untouched. */
|
|
|
ddf19c |
+ if (set_one_signal_handler(SIGHUP, exit_handler, 0) == -1 ||
|
|
|
ddf19c |
+ set_one_signal_handler(SIGINT, exit_handler, 0) == -1 ||
|
|
|
ddf19c |
+ set_one_signal_handler(SIGTERM, exit_handler, 0) == -1 ||
|
|
|
ddf19c |
+ set_one_signal_handler(SIGPIPE, do_nothing, 0) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ fuse_instance = se;
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_remove_signal_handlers(struct fuse_session *se)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (fuse_instance != se)
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR,
|
|
|
ddf19c |
+ "fuse: fuse_remove_signal_handlers: unknown session\n");
|
|
|
ddf19c |
+ else
|
|
|
ddf19c |
+ fuse_instance = NULL;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ set_one_signal_handler(SIGHUP, exit_handler, 1);
|
|
|
ddf19c |
+ set_one_signal_handler(SIGINT, exit_handler, 1);
|
|
|
ddf19c |
+ set_one_signal_handler(SIGTERM, exit_handler, 1);
|
|
|
ddf19c |
+ set_one_signal_handler(SIGPIPE, do_nothing, 1);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c
|
|
|
ddf19c |
new file mode 100644
|
|
|
ddf19c |
index 0000000..64ff7ad
|
|
|
ddf19c |
--- /dev/null
|
|
|
ddf19c |
+++ b/tools/virtiofsd/helper.c
|
|
|
ddf19c |
@@ -0,0 +1,440 @@
|
|
|
ddf19c |
+/*
|
|
|
ddf19c |
+ FUSE: Filesystem in Userspace
|
|
|
ddf19c |
+ Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ Helper functions to create (simple) standalone programs. With the
|
|
|
ddf19c |
+ aid of these functions it should be possible to create full FUSE
|
|
|
ddf19c |
+ file system by implementing nothing but the request handlers.
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ This program can be distributed under the terms of the GNU LGPLv2.
|
|
|
ddf19c |
+ See the file COPYING.LIB.
|
|
|
ddf19c |
+*/
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include "config.h"
|
|
|
ddf19c |
+#include "fuse_i.h"
|
|
|
ddf19c |
+#include "fuse_misc.h"
|
|
|
ddf19c |
+#include "fuse_opt.h"
|
|
|
ddf19c |
+#include "fuse_lowlevel.h"
|
|
|
ddf19c |
+#include "mount_util.h"
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#include <stdio.h>
|
|
|
ddf19c |
+#include <stdlib.h>
|
|
|
ddf19c |
+#include <stddef.h>
|
|
|
ddf19c |
+#include <unistd.h>
|
|
|
ddf19c |
+#include <string.h>
|
|
|
ddf19c |
+#include <limits.h>
|
|
|
ddf19c |
+#include <errno.h>
|
|
|
ddf19c |
+#include <sys/param.h>
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#define FUSE_HELPER_OPT(t, p) \
|
|
|
ddf19c |
+ { t, offsetof(struct fuse_cmdline_opts, p), 1 }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static const struct fuse_opt fuse_helper_opts[] = {
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-h", show_help),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("--help", show_help),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-V", show_version),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("--version", show_version),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-d", debug),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("debug", debug),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-d", foreground),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("debug", foreground),
|
|
|
ddf19c |
+ FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
|
|
|
ddf19c |
+ FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-f", foreground),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("-s", singlethread),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("fsname=", nodefault_subtype),
|
|
|
ddf19c |
+ FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
|
|
|
ddf19c |
+#ifndef __FreeBSD__
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("subtype=", nodefault_subtype),
|
|
|
ddf19c |
+ FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
|
|
|
ddf19c |
+#endif
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("clone_fd", clone_fd),
|
|
|
ddf19c |
+ FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
|
|
|
ddf19c |
+ FUSE_OPT_END
|
|
|
ddf19c |
+};
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+struct fuse_conn_info_opts {
|
|
|
ddf19c |
+ int atomic_o_trunc;
|
|
|
ddf19c |
+ int no_remote_posix_lock;
|
|
|
ddf19c |
+ int no_remote_flock;
|
|
|
ddf19c |
+ int splice_write;
|
|
|
ddf19c |
+ int splice_move;
|
|
|
ddf19c |
+ int splice_read;
|
|
|
ddf19c |
+ int no_splice_write;
|
|
|
ddf19c |
+ int no_splice_move;
|
|
|
ddf19c |
+ int no_splice_read;
|
|
|
ddf19c |
+ int auto_inval_data;
|
|
|
ddf19c |
+ int no_auto_inval_data;
|
|
|
ddf19c |
+ int no_readdirplus;
|
|
|
ddf19c |
+ int no_readdirplus_auto;
|
|
|
ddf19c |
+ int async_dio;
|
|
|
ddf19c |
+ int no_async_dio;
|
|
|
ddf19c |
+ int writeback_cache;
|
|
|
ddf19c |
+ int no_writeback_cache;
|
|
|
ddf19c |
+ int async_read;
|
|
|
ddf19c |
+ int sync_read;
|
|
|
ddf19c |
+ unsigned max_write;
|
|
|
ddf19c |
+ unsigned max_readahead;
|
|
|
ddf19c |
+ unsigned max_background;
|
|
|
ddf19c |
+ unsigned congestion_threshold;
|
|
|
ddf19c |
+ unsigned time_gran;
|
|
|
ddf19c |
+ int set_max_write;
|
|
|
ddf19c |
+ int set_max_readahead;
|
|
|
ddf19c |
+ int set_max_background;
|
|
|
ddf19c |
+ int set_congestion_threshold;
|
|
|
ddf19c |
+ int set_time_gran;
|
|
|
ddf19c |
+};
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#define CONN_OPTION(t, p, v) \
|
|
|
ddf19c |
+ { t, offsetof(struct fuse_conn_info_opts, p), v }
|
|
|
ddf19c |
+static const struct fuse_opt conn_info_opt_spec[] = {
|
|
|
ddf19c |
+ CONN_OPTION("max_write=%u", max_write, 0),
|
|
|
ddf19c |
+ CONN_OPTION("max_write=", set_max_write, 1),
|
|
|
ddf19c |
+ CONN_OPTION("max_readahead=%u", max_readahead, 0),
|
|
|
ddf19c |
+ CONN_OPTION("max_readahead=", set_max_readahead, 1),
|
|
|
ddf19c |
+ CONN_OPTION("max_background=%u", max_background, 0),
|
|
|
ddf19c |
+ CONN_OPTION("max_background=", set_max_background, 1),
|
|
|
ddf19c |
+ CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
|
|
|
ddf19c |
+ CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
|
|
|
ddf19c |
+ CONN_OPTION("sync_read", sync_read, 1),
|
|
|
ddf19c |
+ CONN_OPTION("async_read", async_read, 1),
|
|
|
ddf19c |
+ CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_remote_lock", no_remote_flock, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_remote_flock", no_remote_flock, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
|
|
|
ddf19c |
+ CONN_OPTION("splice_write", splice_write, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_splice_write", no_splice_write, 1),
|
|
|
ddf19c |
+ CONN_OPTION("splice_move", splice_move, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_splice_move", no_splice_move, 1),
|
|
|
ddf19c |
+ CONN_OPTION("splice_read", splice_read, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_splice_read", no_splice_read, 1),
|
|
|
ddf19c |
+ CONN_OPTION("auto_inval_data", auto_inval_data, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
|
|
|
ddf19c |
+ CONN_OPTION("readdirplus=no", no_readdirplus, 1),
|
|
|
ddf19c |
+ CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
|
|
|
ddf19c |
+ CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
|
|
|
ddf19c |
+ CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
|
|
|
ddf19c |
+ CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
|
|
|
ddf19c |
+ CONN_OPTION("async_dio", async_dio, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_async_dio", no_async_dio, 1),
|
|
|
ddf19c |
+ CONN_OPTION("writeback_cache", writeback_cache, 1),
|
|
|
ddf19c |
+ CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
|
|
|
ddf19c |
+ CONN_OPTION("time_gran=%u", time_gran, 0),
|
|
|
ddf19c |
+ CONN_OPTION("time_gran=", set_time_gran, 1),
|
|
|
ddf19c |
+ FUSE_OPT_END
|
|
|
ddf19c |
+};
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_cmdline_help(void)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ printf(" -h --help print help\n"
|
|
|
ddf19c |
+ " -V --version print version\n"
|
|
|
ddf19c |
+ " -d -o debug enable debug output (implies -f)\n"
|
|
|
ddf19c |
+ " -f foreground operation\n"
|
|
|
ddf19c |
+ " -s disable multi-threaded operation\n"
|
|
|
ddf19c |
+ " -o clone_fd use separate fuse device fd for each thread\n"
|
|
|
ddf19c |
+ " (may improve performance)\n"
|
|
|
ddf19c |
+ " -o max_idle_threads the maximum number of idle worker threads\n"
|
|
|
ddf19c |
+ " allowed (default: 10)\n");
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+static int fuse_helper_opt_proc(void *data, const char *arg, int key,
|
|
|
ddf19c |
+ struct fuse_args *outargs)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ (void) outargs;
|
|
|
ddf19c |
+ struct fuse_cmdline_opts *opts = data;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ switch (key) {
|
|
|
ddf19c |
+ case FUSE_OPT_KEY_NONOPT:
|
|
|
ddf19c |
+ if (!opts->mountpoint) {
|
|
|
ddf19c |
+ if (fuse_mnt_parse_fuse_fd(arg) != -1) {
|
|
|
ddf19c |
+ return fuse_opt_add_opt(&opts->mountpoint, arg);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ char mountpoint[PATH_MAX] = "";
|
|
|
ddf19c |
+ if (realpath(arg, mountpoint) == NULL) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR,
|
|
|
ddf19c |
+ "fuse: bad mount point `%s': %s\n",
|
|
|
ddf19c |
+ arg, strerror(errno));
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return fuse_opt_add_opt(&opts->mountpoint, mountpoint);
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ default:
|
|
|
ddf19c |
+ /* Pass through unknown options */
|
|
|
ddf19c |
+ return 1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+/* Under FreeBSD, there is no subtype option so this
|
|
|
ddf19c |
+ function actually sets the fsname */
|
|
|
ddf19c |
+static int add_default_subtype(const char *progname, struct fuse_args *args)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+ char *subtype_opt;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ const char *basename = strrchr(progname, '/');
|
|
|
ddf19c |
+ if (basename == NULL)
|
|
|
ddf19c |
+ basename = progname;
|
|
|
ddf19c |
+ else if (basename[1] != '\0')
|
|
|
ddf19c |
+ basename++;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ subtype_opt = (char *) malloc(strlen(basename) + 64);
|
|
|
ddf19c |
+ if (subtype_opt == NULL) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+#ifdef __FreeBSD__
|
|
|
ddf19c |
+ sprintf(subtype_opt, "-ofsname=%s", basename);
|
|
|
ddf19c |
+#else
|
|
|
ddf19c |
+ sprintf(subtype_opt, "-osubtype=%s", basename);
|
|
|
ddf19c |
+#endif
|
|
|
ddf19c |
+ res = fuse_opt_add_arg(args, subtype_opt);
|
|
|
ddf19c |
+ free(subtype_opt);
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_parse_cmdline(struct fuse_args *args,
|
|
|
ddf19c |
+ struct fuse_cmdline_opts *opts)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ memset(opts, 0, sizeof(struct fuse_cmdline_opts));
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ opts->max_idle_threads = 10;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (fuse_opt_parse(args, opts, fuse_helper_opts,
|
|
|
ddf19c |
+ fuse_helper_opt_proc) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ /* *Linux*: if neither -o subtype nor -o fsname are specified,
|
|
|
ddf19c |
+ set subtype to program's basename.
|
|
|
ddf19c |
+ *FreeBSD*: if fsname is not specified, set to program's
|
|
|
ddf19c |
+ basename. */
|
|
|
ddf19c |
+ if (!opts->nodefault_subtype)
|
|
|
ddf19c |
+ if (add_default_subtype(args->argv[0], args) == -1)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_daemonize(int foreground)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if (!foreground) {
|
|
|
ddf19c |
+ int nullfd;
|
|
|
ddf19c |
+ int waiter[2];
|
|
|
ddf19c |
+ char completed;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (pipe(waiter)) {
|
|
|
ddf19c |
+ perror("fuse_daemonize: pipe");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ /*
|
|
|
ddf19c |
+ * demonize current process by forking it and killing the
|
|
|
ddf19c |
+ * parent. This makes current process as a child of 'init'.
|
|
|
ddf19c |
+ */
|
|
|
ddf19c |
+ switch(fork()) {
|
|
|
ddf19c |
+ case -1:
|
|
|
ddf19c |
+ perror("fuse_daemonize: fork");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ case 0:
|
|
|
ddf19c |
+ break;
|
|
|
ddf19c |
+ default:
|
|
|
ddf19c |
+ (void) read(waiter[0], &completed, sizeof(completed));
|
|
|
ddf19c |
+ _exit(0);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (setsid() == -1) {
|
|
|
ddf19c |
+ perror("fuse_daemonize: setsid");
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ (void) chdir("/");
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ nullfd = open("/dev/null", O_RDWR, 0);
|
|
|
ddf19c |
+ if (nullfd != -1) {
|
|
|
ddf19c |
+ (void) dup2(nullfd, 0);
|
|
|
ddf19c |
+ (void) dup2(nullfd, 1);
|
|
|
ddf19c |
+ (void) dup2(nullfd, 2);
|
|
|
ddf19c |
+ if (nullfd > 2)
|
|
|
ddf19c |
+ close(nullfd);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ /* Propagate completion of daemon initialization */
|
|
|
ddf19c |
+ completed = 1;
|
|
|
ddf19c |
+ (void) write(waiter[1], &completed, sizeof(completed));
|
|
|
ddf19c |
+ close(waiter[0]);
|
|
|
ddf19c |
+ close(waiter[1]);
|
|
|
ddf19c |
+ } else {
|
|
|
ddf19c |
+ (void) chdir("/");
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return 0;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op,
|
|
|
ddf19c |
+ size_t op_size, void *user_data)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
|
|
ddf19c |
+ struct fuse *fuse;
|
|
|
ddf19c |
+ struct fuse_cmdline_opts opts;
|
|
|
ddf19c |
+ int res;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (fuse_parse_cmdline(&args, &opts) != 0)
|
|
|
ddf19c |
+ return 1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (opts.show_version) {
|
|
|
ddf19c |
+ printf("FUSE library version %s\n", PACKAGE_VERSION);
|
|
|
ddf19c |
+ fuse_lowlevel_version();
|
|
|
ddf19c |
+ res = 0;
|
|
|
ddf19c |
+ goto out1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (opts.show_help) {
|
|
|
ddf19c |
+ if(args.argv[0][0] != '\0')
|
|
|
ddf19c |
+ printf("usage: %s [options] <mountpoint>\n\n",
|
|
|
ddf19c |
+ args.argv[0]);
|
|
|
ddf19c |
+ printf("FUSE options:\n");
|
|
|
ddf19c |
+ fuse_cmdline_help();
|
|
|
ddf19c |
+ fuse_lib_help(&args);
|
|
|
ddf19c |
+ res = 0;
|
|
|
ddf19c |
+ goto out1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (!opts.show_help &&
|
|
|
ddf19c |
+ !opts.mountpoint) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "error: no mountpoint specified\n");
|
|
|
ddf19c |
+ res = 2;
|
|
|
ddf19c |
+ goto out1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ fuse = fuse_new_31(&args, op, op_size, user_data);
|
|
|
ddf19c |
+ if (fuse == NULL) {
|
|
|
ddf19c |
+ res = 3;
|
|
|
ddf19c |
+ goto out1;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (fuse_mount(fuse,opts.mountpoint) != 0) {
|
|
|
ddf19c |
+ res = 4;
|
|
|
ddf19c |
+ goto out2;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (fuse_daemonize(opts.foreground) != 0) {
|
|
|
ddf19c |
+ res = 5;
|
|
|
ddf19c |
+ goto out3;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ struct fuse_session *se = fuse_get_session(fuse);
|
|
|
ddf19c |
+ if (fuse_set_signal_handlers(se) != 0) {
|
|
|
ddf19c |
+ res = 6;
|
|
|
ddf19c |
+ goto out3;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ if (opts.singlethread)
|
|
|
ddf19c |
+ res = fuse_loop(fuse);
|
|
|
ddf19c |
+ else {
|
|
|
ddf19c |
+ struct fuse_loop_config loop_config;
|
|
|
ddf19c |
+ loop_config.clone_fd = opts.clone_fd;
|
|
|
ddf19c |
+ loop_config.max_idle_threads = opts.max_idle_threads;
|
|
|
ddf19c |
+ res = fuse_loop_mt_32(fuse, &loop_config);
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if (res)
|
|
|
ddf19c |
+ res = 7;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ fuse_remove_signal_handlers(se);
|
|
|
ddf19c |
+out3:
|
|
|
ddf19c |
+ fuse_unmount(fuse);
|
|
|
ddf19c |
+out2:
|
|
|
ddf19c |
+ fuse_destroy(fuse);
|
|
|
ddf19c |
+out1:
|
|
|
ddf19c |
+ free(opts.mountpoint);
|
|
|
ddf19c |
+ fuse_opt_free_args(&args);
|
|
|
ddf19c |
+ return res;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
|
|
|
ddf19c |
+ struct fuse_conn_info *conn)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ if(opts->set_max_write)
|
|
|
ddf19c |
+ conn->max_write = opts->max_write;
|
|
|
ddf19c |
+ if(opts->set_max_background)
|
|
|
ddf19c |
+ conn->max_background = opts->max_background;
|
|
|
ddf19c |
+ if(opts->set_congestion_threshold)
|
|
|
ddf19c |
+ conn->congestion_threshold = opts->congestion_threshold;
|
|
|
ddf19c |
+ if(opts->set_time_gran)
|
|
|
ddf19c |
+ conn->time_gran = opts->time_gran;
|
|
|
ddf19c |
+ if(opts->set_max_readahead)
|
|
|
ddf19c |
+ conn->max_readahead = opts->max_readahead;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+#define LL_ENABLE(cond,cap) \
|
|
|
ddf19c |
+ if (cond) conn->want |= (cap)
|
|
|
ddf19c |
+#define LL_DISABLE(cond,cap) \
|
|
|
ddf19c |
+ if (cond) conn->want &= ~(cap)
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
|
|
|
ddf19c |
+ LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
|
|
|
ddf19c |
+ LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+struct fuse_conn_info_opts* fuse_parse_conn_info_opts(struct fuse_args *args)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ struct fuse_conn_info_opts *opts;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ opts = calloc(1, sizeof(struct fuse_conn_info_opts));
|
|
|
ddf19c |
+ if(opts == NULL) {
|
|
|
ddf19c |
+ fuse_log(FUSE_LOG_ERR, "calloc failed\n");
|
|
|
ddf19c |
+ return NULL;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ if(fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
|
|
|
ddf19c |
+ free(opts);
|
|
|
ddf19c |
+ return NULL;
|
|
|
ddf19c |
+ }
|
|
|
ddf19c |
+ return opts;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+int fuse_open_channel(const char *mountpoint, const char* options)
|
|
|
ddf19c |
+{
|
|
|
ddf19c |
+ struct mount_opts *opts = NULL;
|
|
|
ddf19c |
+ int fd = -1;
|
|
|
ddf19c |
+ const char *argv[] = { "", "-o", options };
|
|
|
ddf19c |
+ int argc = sizeof(argv) / sizeof(argv[0]);
|
|
|
ddf19c |
+ struct fuse_args args = FUSE_ARGS_INIT(argc, (char**) argv);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ opts = parse_mount_opts(&args);
|
|
|
ddf19c |
+ if (opts == NULL)
|
|
|
ddf19c |
+ return -1;
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ fd = fuse_kern_mount(mountpoint, opts);
|
|
|
ddf19c |
+ destroy_mount_opts(opts);
|
|
|
ddf19c |
+
|
|
|
ddf19c |
+ return fd;
|
|
|
ddf19c |
+}
|
|
|
ddf19c |
--
|
|
|
ddf19c |
1.8.3.1
|
|
|
ddf19c |
|