cryptospore / rpms / qemu-kvm

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