2012-07-21 09:49:42 +04:00
|
|
|
/* $NetBSD: perfused.c,v 1.24 2012/07/21 05:49:42 manu Exp $ */
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <puffs.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <machine/vmparam.h>
|
|
|
|
|
|
|
|
#include "perfused.h"
|
|
|
|
|
2011-12-28 21:33:52 +04:00
|
|
|
/*
|
|
|
|
* This is used for trace file. of course it will not work if
|
|
|
|
* we ever mount multiple filesystems in a single perfused,
|
|
|
|
* but it is not sure we will ever want to do that.
|
|
|
|
*/
|
2012-01-31 02:49:03 +04:00
|
|
|
static struct puffs_usermount *perfused_mount = NULL;
|
|
|
|
static FILE *perfused_trace = NULL;
|
2011-12-28 21:33:52 +04:00
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
static int access_mount(const char *, uid_t, int);
|
2010-09-07 06:11:04 +04:00
|
|
|
static void new_mount(int, int);
|
2010-08-25 11:18:01 +04:00
|
|
|
static int parse_debug(char *);
|
|
|
|
static void siginfo_handler(int);
|
2011-12-28 21:33:52 +04:00
|
|
|
static void sigusr1_handler(int);
|
2010-08-27 13:58:17 +04:00
|
|
|
static int parse_options(int, char **);
|
2011-05-30 18:50:08 +04:00
|
|
|
static void get_mount_info(int, struct perfuse_mount_info *, int);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-07 06:11:04 +04:00
|
|
|
/*
|
|
|
|
* Flags for new_mount()
|
|
|
|
*/
|
|
|
|
#define PMNT_DEVFUSE 0x0 /* We use /dev/fuse */
|
|
|
|
#define PMNT_SOCKPAIR 0x1 /* We use socketpair */
|
2011-05-30 18:50:08 +04:00
|
|
|
#define PMNT_DGRAM 0x2 /* We use SOCK_DGRAM sockets */
|
2010-09-07 06:11:04 +04:00
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
static int
|
2011-08-31 00:17:00 +04:00
|
|
|
access_mount(const char *mnt, uid_t uid, int ro)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
mode_t mode;
|
|
|
|
|
|
|
|
if (uid == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (stat(mnt, &st) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (st.st_uid != uid)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
mode = S_IRUSR;
|
|
|
|
if (!ro)
|
|
|
|
mode |= S_IWUSR;
|
|
|
|
|
|
|
|
if ((st.st_mode & mode) == mode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-08-31 00:17:00 +04:00
|
|
|
get_mount_info(int fd, struct perfuse_mount_info *pmi, int sock_type)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct perfuse_mount_out *pmo;
|
2010-09-15 05:51:43 +04:00
|
|
|
struct sockcred cred;
|
2010-09-20 10:45:38 +04:00
|
|
|
int opt;
|
2010-09-15 05:51:43 +04:00
|
|
|
char *cp;
|
2010-08-25 11:18:01 +04:00
|
|
|
char *source = NULL;
|
|
|
|
char *target = NULL;
|
|
|
|
char *filesystemtype = NULL;
|
|
|
|
long mountflags = 0;
|
2010-09-15 05:51:43 +04:00
|
|
|
void *data = NULL;
|
|
|
|
char *sock = NULL;
|
|
|
|
|
|
|
|
pmo = (struct perfuse_mount_out *)
|
2012-01-31 02:49:03 +04:00
|
|
|
perfused_recv_early(fd, &cred, sizeof(cred));
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (pmo == NULL) {
|
|
|
|
if (shutdown(fd, SHUT_RDWR) != 0)
|
|
|
|
DERR(EX_OSERR, "shutdown failed");
|
|
|
|
exit(EX_PROTOCOL);
|
|
|
|
}
|
|
|
|
|
2010-09-20 10:45:38 +04:00
|
|
|
/*
|
|
|
|
* We do not need peer creds beyond this point
|
|
|
|
*/
|
|
|
|
opt = 0;
|
|
|
|
if (setsockopt(fd, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
|
|
|
|
DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
|
== file close operations ==
- use PUFFS_KFLAG_WTCACHE to puffs_init so that all writes are
immediatly send to the filesystem, and we do not have anymore write
after inactive. As a consequence, we can close files at inactive
stage, and there is not any concern left with files opened at
create time. We also do not have anymore to open ourselves in readdir and
fsync.
- Fsync on close (inactive stage). That makes sure we will not need to
do these operations once the file is closed (FUSE want an open file).
short sircuit the request that come after the close, bu not fsinc'ing
closed files,
- Use PUFFS_KFLAG_IAONDEMAND to get less inactive calls
== Removed nodes ==
- more ENOENT retunred for operations on removed node (but there
are probably some still missing): getattr, ooen, setattr, fsync
- set PND_REMOVE before sending the UNLINK/RMDIR operations so that we avoid
races during UNLINK completion. Also set PND_REMOVED on node we overwirte
in rename
== Filehandle fixes ==
- queue open operation to avoid getting two fh for one file
- set FH in getattr, if the file is open
- Just requires a read FH for fsyncdir, as we always opendir in read
mode. Ok, this is misleading :-)
== Misc ==
- do not set FUSE_FATTR_ATIME_NOW in setattr, as we provide the time
- short circuit nilpotent operations in setattr
- add a filename diagnostic flag to dump file names
2010-09-23 20:02:34 +04:00
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2010-09-07 06:11:04 +04:00
|
|
|
if (perfuse_diagflags & PDF_MISC)
|
|
|
|
DPRINTF("perfuse lengths: source = %"PRId32", "
|
|
|
|
"target = %"PRId32", filesystemtype = %"PRId32", "
|
2010-09-15 05:51:43 +04:00
|
|
|
"data = %"PRId32", sock = %"PRId32"\n",
|
|
|
|
pmo->pmo_source_len, pmo->pmo_target_len,
|
|
|
|
pmo->pmo_filesystemtype_len, pmo->pmo_data_len,
|
|
|
|
pmo->pmo_sock_len);
|
2010-08-25 11:18:01 +04:00
|
|
|
#endif
|
2010-09-15 05:51:43 +04:00
|
|
|
cp = (char *)(void *)(pmo + 1);
|
|
|
|
|
|
|
|
if (pmo->pmo_source_len != 0) {
|
|
|
|
source = cp;
|
|
|
|
cp += pmo->pmo_source_len;
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
if (pmo->pmo_target_len != 0) {
|
|
|
|
target = cp;
|
|
|
|
cp += pmo->pmo_target_len;
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
if (pmo->pmo_filesystemtype_len != 0) {
|
|
|
|
filesystemtype = cp;
|
|
|
|
cp += pmo->pmo_filesystemtype_len;
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
mountflags = pmo->pmo_mountflags;
|
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
if (pmo->pmo_data_len != 0) {
|
|
|
|
data = cp;
|
|
|
|
cp += pmo->pmo_data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pmo->pmo_sock_len != 0) {
|
|
|
|
sock = cp;
|
|
|
|
cp += pmo->pmo_sock_len;
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
#ifdef PERFUSE_DEBUG
|
2010-09-07 06:11:04 +04:00
|
|
|
if (perfuse_diagflags & PDF_MISC)
|
2010-09-15 05:51:43 +04:00
|
|
|
DPRINTF("%s(\"%s\", \"%s\", \"%s\", 0x%lx, \"%s\", \"%s\")\n",
|
2010-09-07 06:11:04 +04:00
|
|
|
__func__, source, target, filesystemtype,
|
2010-09-15 05:51:43 +04:00
|
|
|
mountflags, (const char *)data, sock);
|
2010-08-25 11:18:01 +04:00
|
|
|
#endif
|
|
|
|
pmi->pmi_source = source;
|
|
|
|
pmi->pmi_target = target;
|
|
|
|
pmi->pmi_filesystemtype = filesystemtype;
|
2010-09-06 05:40:24 +04:00
|
|
|
pmi->pmi_mountflags = (int)mountflags;
|
2010-08-25 11:18:01 +04:00
|
|
|
pmi->pmi_data = data;
|
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
pmi->pmi_uid = cred.sc_euid;
|
|
|
|
|
|
|
|
/*
|
2011-05-30 18:50:08 +04:00
|
|
|
* Connect to the remote socket if provided ans using SOCK_DGRAM
|
2010-09-15 05:51:43 +04:00
|
|
|
*/
|
2011-05-30 18:50:08 +04:00
|
|
|
if ((sock_type == SOCK_DGRAM) && sock) {
|
2010-09-15 05:51:43 +04:00
|
|
|
const struct sockaddr *sa;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
|
|
|
|
sa = (const struct sockaddr *)(void *)&sun;
|
|
|
|
sun.sun_len = sizeof(sun);
|
|
|
|
sun.sun_family = AF_LOCAL;
|
|
|
|
strcpy(sun.sun_path, sock);
|
|
|
|
|
|
|
|
if (connect(fd, sa, sun.sun_len) != 0)
|
|
|
|
DERR(EX_OSERR, "connect \"%s\" failed", sun.sun_path);
|
|
|
|
}
|
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
static void
|
2011-08-31 00:17:00 +04:00
|
|
|
new_mount(int fd, int pmnt_flags)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_usermount *pu;
|
|
|
|
struct perfuse_mount_info pmi;
|
|
|
|
struct perfuse_callbacks pc;
|
|
|
|
int ro_flag;
|
|
|
|
pid_t pid;
|
|
|
|
int flags;
|
2011-05-30 18:50:08 +04:00
|
|
|
int sock_type;
|
2012-01-29 10:22:01 +04:00
|
|
|
char trace_file[MAXPATHLEN + 1];
|
|
|
|
char trace_name[MAXPATHLEN + 1];
|
|
|
|
ssize_t trace_namelen;
|
|
|
|
int i;
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
pid = (perfuse_diagflags & PDF_FOREGROUND) ? 0 : fork();
|
|
|
|
switch(pid) {
|
|
|
|
case -1:
|
|
|
|
DERR(EX_OSERR, "cannot fork");
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
2010-08-27 13:58:17 +04:00
|
|
|
return;
|
2010-08-25 11:18:01 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mount information (source, target, mount flags...)
|
|
|
|
*/
|
2011-05-30 18:50:08 +04:00
|
|
|
sock_type = pmnt_flags & PMNT_DGRAM ? SOCK_DGRAM : SOCK_SEQPACKET;
|
|
|
|
get_mount_info(fd, &pmi, sock_type);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that peer owns mountpoint and read (and write) on it?
|
|
|
|
*/
|
|
|
|
ro_flag = pmi.pmi_mountflags & MNT_RDONLY;
|
|
|
|
if (access_mount(pmi.pmi_target, pmi.pmi_uid, ro_flag) != 0)
|
2010-09-07 06:11:04 +04:00
|
|
|
DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
|
2010-08-25 11:18:01 +04:00
|
|
|
pmi.pmi_target);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize libperfuse, which will initialize libpuffs
|
|
|
|
*/
|
2012-06-05 19:04:18 +04:00
|
|
|
(void)memset(&pc, 0, sizeof(pc));
|
2012-01-31 02:49:03 +04:00
|
|
|
pc.pc_new_msg = perfused_new_pb;
|
|
|
|
pc.pc_xchg_msg = perfused_xchg_pb;
|
2010-08-25 11:18:01 +04:00
|
|
|
pc.pc_destroy_msg = (perfuse_destroy_msg_fn)puffs_framebuf_destroy;
|
2012-01-31 02:49:03 +04:00
|
|
|
pc.pc_get_inhdr = perfused_get_inhdr;
|
|
|
|
pc.pc_get_inpayload = perfused_get_inpayload;
|
|
|
|
pc.pc_get_outhdr = perfused_get_outhdr;
|
|
|
|
pc.pc_get_outpayload = perfused_get_outpayload;
|
|
|
|
pc.pc_umount = perfused_umount;
|
2012-07-21 09:49:42 +04:00
|
|
|
pc.pc_fsreq = perfused_gotframe;
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
pu = perfuse_init(&pc, &pmi);
|
|
|
|
|
2012-01-31 02:49:03 +04:00
|
|
|
puffs_framev_init(pu, perfused_readframe, perfused_writeframe,
|
2012-07-21 09:49:42 +04:00
|
|
|
perfused_cmpframe, pc.pc_fsreq, perfused_fdnotify);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
|
|
|
|
|
2012-01-31 02:49:03 +04:00
|
|
|
perfuse_setspecific(pu, (void *)(intptr_t)fd);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
setproctitle("perfused %s", pmi.pmi_target);
|
|
|
|
(void)kill(getpid(), SIGINFO); /* This is for -s option */
|
|
|
|
|
|
|
|
perfuse_fs_init(pu);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Non blocking I/O on /dev/fuse
|
2012-01-31 02:49:03 +04:00
|
|
|
* This must be done after perfused_fs_init
|
2010-08-25 11:18:01 +04:00
|
|
|
*/
|
|
|
|
if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
|
|
|
|
DERR(EX_OSERR, "fcntl failed");
|
|
|
|
if (fcntl(fd, F_SETFL, flags|O_NONBLOCK) != 0)
|
|
|
|
DERR(EX_OSERR, "fcntl failed");
|
|
|
|
|
2011-12-28 21:33:52 +04:00
|
|
|
/*
|
|
|
|
* Setup trace file facility
|
|
|
|
*/
|
2012-01-31 02:49:03 +04:00
|
|
|
perfused_mount = pu;
|
2011-12-28 21:33:52 +04:00
|
|
|
|
2012-01-29 10:22:01 +04:00
|
|
|
trace_namelen = strlcpy(trace_name, pmi.pmi_target, MAXPATHLEN);
|
|
|
|
for (i = 0; i < trace_namelen; i++)
|
|
|
|
if (trace_name[i] == '/')
|
|
|
|
trace_name[i] = '-';
|
|
|
|
|
|
|
|
(void)snprintf(trace_file, MAXPATHLEN, _PATH_VAR_RUN_PERFUSE_TRACE,
|
|
|
|
trace_name);
|
|
|
|
|
2012-01-31 02:49:03 +04:00
|
|
|
if ((perfused_trace = fopen(trace_file, "w")) == NULL)
|
2011-12-28 21:33:52 +04:00
|
|
|
DERR(EX_OSFILE,
|
|
|
|
"could not open \"%s\"",
|
|
|
|
_PATH_VAR_RUN_PERFUSE_TRACE);
|
|
|
|
|
|
|
|
if (signal(SIGUSR1, sigusr1_handler) != 0)
|
|
|
|
DERR(EX_OSERR, "signal failed");
|
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
/*
|
|
|
|
* Hand over control to puffs main loop.
|
|
|
|
*/
|
2011-05-30 18:50:08 +04:00
|
|
|
if (perfuse_mainloop(pu) != 0)
|
2012-01-31 02:49:03 +04:00
|
|
|
DERRX(EX_SOFTWARE, "perfused_mainloop exit");
|
2010-08-27 13:58:17 +04:00
|
|
|
|
2011-05-30 18:50:08 +04:00
|
|
|
/*
|
|
|
|
* Normal return after unmount
|
|
|
|
*/
|
2010-08-27 13:58:17 +04:00
|
|
|
return;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-08-31 00:17:00 +04:00
|
|
|
parse_debug(char *optstr)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
int retval = PDF_SYSLOG;
|
|
|
|
char *opt;
|
|
|
|
char *lastp;
|
|
|
|
|
|
|
|
for (opt = strtok_r(optstr, ",", &lastp);
|
|
|
|
opt;
|
|
|
|
opt = strtok_r(NULL, ",", &lastp)) {
|
|
|
|
if (strcmp(opt, "fuse") == 0)
|
|
|
|
retval |= PDF_FUSE;
|
|
|
|
else if (strcmp(opt, "puffs") == 0)
|
|
|
|
retval |= PDF_PUFFS;
|
|
|
|
else if (strcmp(opt, "dump") == 0)
|
|
|
|
retval |= PDF_DUMP;
|
|
|
|
else if (strcmp(opt, "fh") == 0)
|
|
|
|
retval |= PDF_FH;
|
|
|
|
else if (strcmp(opt, "readdir") == 0)
|
|
|
|
retval |= PDF_READDIR;
|
|
|
|
else if (strcmp(opt, "reclaim") == 0)
|
|
|
|
retval |= PDF_RECLAIM;
|
|
|
|
else if (strcmp(opt, "requeue") == 0)
|
|
|
|
retval |= PDF_REQUEUE;
|
2010-08-26 17:29:01 +04:00
|
|
|
else if (strcmp(opt, "sync") == 0)
|
|
|
|
retval |= PDF_SYNC;
|
2010-08-25 11:18:01 +04:00
|
|
|
else if (strcmp(opt, "misc") == 0)
|
|
|
|
retval |= PDF_MISC;
|
== file close operations ==
- use PUFFS_KFLAG_WTCACHE to puffs_init so that all writes are
immediatly send to the filesystem, and we do not have anymore write
after inactive. As a consequence, we can close files at inactive
stage, and there is not any concern left with files opened at
create time. We also do not have anymore to open ourselves in readdir and
fsync.
- Fsync on close (inactive stage). That makes sure we will not need to
do these operations once the file is closed (FUSE want an open file).
short sircuit the request that come after the close, bu not fsinc'ing
closed files,
- Use PUFFS_KFLAG_IAONDEMAND to get less inactive calls
== Removed nodes ==
- more ENOENT retunred for operations on removed node (but there
are probably some still missing): getattr, ooen, setattr, fsync
- set PND_REMOVE before sending the UNLINK/RMDIR operations so that we avoid
races during UNLINK completion. Also set PND_REMOVED on node we overwirte
in rename
== Filehandle fixes ==
- queue open operation to avoid getting two fh for one file
- set FH in getattr, if the file is open
- Just requires a read FH for fsyncdir, as we always opendir in read
mode. Ok, this is misleading :-)
== Misc ==
- do not set FUSE_FATTR_ATIME_NOW in setattr, as we provide the time
- short circuit nilpotent operations in setattr
- add a filename diagnostic flag to dump file names
2010-09-23 20:02:34 +04:00
|
|
|
else if (strcmp(opt, "filename") == 0)
|
|
|
|
retval |= PDF_FILENAME;
|
2011-09-09 19:45:28 +04:00
|
|
|
else if (strcmp(opt, "reize") == 0)
|
|
|
|
retval |= PDF_RESIZE;
|
2010-08-25 11:18:01 +04:00
|
|
|
else
|
2011-05-30 18:50:08 +04:00
|
|
|
DWARNX("unknown debug flag \"%s\"", opt);
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED0 */
|
|
|
|
static void
|
2011-08-31 00:17:00 +04:00
|
|
|
siginfo_handler(int sig)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
static int old_flags = 0;
|
|
|
|
int swap;
|
|
|
|
|
|
|
|
swap = perfuse_diagflags;
|
|
|
|
perfuse_diagflags = old_flags;
|
|
|
|
old_flags = swap;
|
|
|
|
|
|
|
|
DWARNX("debug %sabled", old_flags == 0 ? "en" : "dis");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-28 21:33:52 +04:00
|
|
|
/* ARGSUSED0 */
|
|
|
|
static void
|
|
|
|
sigusr1_handler(int sig)
|
|
|
|
{
|
2012-01-29 10:22:01 +04:00
|
|
|
if (perfuse_diagflags & PDF_TRACE) {
|
2012-01-31 02:49:03 +04:00
|
|
|
perfuse_trace_dump(perfused_mount, perfused_trace);
|
2012-01-29 10:22:01 +04:00
|
|
|
perfuse_diagflags &= ~PDF_TRACE;
|
2012-07-21 09:49:42 +04:00
|
|
|
DPRINTF("trace dumped, trace disabled\n");
|
2012-01-29 10:22:01 +04:00
|
|
|
} else {
|
|
|
|
perfuse_diagflags |= PDF_TRACE;
|
2012-07-21 09:49:42 +04:00
|
|
|
DPRINTF("trace enabled\n");
|
2012-01-29 10:22:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2011-12-28 21:33:52 +04:00
|
|
|
}
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
static int
|
2011-08-31 00:17:00 +04:00
|
|
|
parse_options(int argc, char **argv)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
int foreground = 0;
|
2010-08-27 13:58:17 +04:00
|
|
|
int retval = -1;
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
perfuse_diagflags = PDF_FOREGROUND | PDF_SYSLOG;
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
while ((ch = getopt(argc, argv, "d:fsi:")) != -1) {
|
2010-08-25 11:18:01 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
perfuse_diagflags |= parse_debug(optarg);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
if (signal(SIGINFO, siginfo_handler) != 0)
|
|
|
|
DERR(EX_OSERR, "signal failed");
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
foreground = 1;
|
2010-09-07 06:11:04 +04:00
|
|
|
perfuse_diagflags |= PDF_MISC;
|
2010-08-25 11:18:01 +04:00
|
|
|
break;
|
2010-08-27 13:58:17 +04:00
|
|
|
case 'i':
|
|
|
|
retval = atoi(optarg);
|
|
|
|
foreground = 1;
|
|
|
|
break;
|
2010-08-25 11:18:01 +04:00
|
|
|
default:
|
2011-04-25 08:30:59 +04:00
|
|
|
DERRX(EX_USAGE, "%s [-fs] [-d classes] [-i fd]", argv[0]);
|
2010-08-25 11:18:01 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!foreground)
|
|
|
|
perfuse_diagflags &= ~PDF_FOREGROUND;
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
return retval;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-08-31 00:17:00 +04:00
|
|
|
main(int argc, char **argv)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
int s;
|
2011-05-30 18:50:08 +04:00
|
|
|
int sock_type;
|
|
|
|
socklen_t len;
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
s = parse_options(argc, argv);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_SYSLOG)
|
2011-10-23 09:03:37 +04:00
|
|
|
openlog("perfused", LOG_NDELAY, LOG_DAEMON);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (!(perfuse_diagflags & PDF_FOREGROUND))
|
|
|
|
if (daemon(0, 0) != 0)
|
|
|
|
DERR(EX_OSERR, "daemon failed");
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
if (s != -1) {
|
2010-09-07 06:11:04 +04:00
|
|
|
new_mount(s, PMNT_SOCKPAIR);
|
2011-05-30 18:50:08 +04:00
|
|
|
exit(0);
|
2010-08-27 13:58:17 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 02:49:03 +04:00
|
|
|
s = perfused_open_sock();
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2011-05-30 18:50:08 +04:00
|
|
|
if (perfuse_diagflags & PDF_MISC)
|
|
|
|
DPRINTF("perfused ready\n");
|
2010-08-27 13:58:17 +04:00
|
|
|
#endif
|
2011-05-30 18:50:08 +04:00
|
|
|
len = sizeof(sock_type);
|
|
|
|
if (getsockopt(s, SOL_SOCKET, SO_TYPE, &sock_type, &len) != 0)
|
|
|
|
DERR(EX_OSERR, "getsockopt SO_TYPE failed");
|
|
|
|
|
|
|
|
switch(sock_type) {
|
|
|
|
case SOCK_DGRAM:
|
|
|
|
new_mount(s, PMNT_DEVFUSE|PMNT_DGRAM);
|
|
|
|
exit(0);
|
|
|
|
case SOCK_SEQPACKET:
|
|
|
|
if (listen(s, 0) != 0)
|
|
|
|
DERR(EX_OSERR, "listen failed");
|
|
|
|
|
2012-02-04 22:36:30 +04:00
|
|
|
for (;;) {
|
2011-05-30 18:50:08 +04:00
|
|
|
int fd;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
struct sockaddr *sa;
|
|
|
|
|
|
|
|
len = sizeof(sun);
|
|
|
|
sa = (struct sockaddr *)(void *)&sun;
|
|
|
|
if ((fd = accept(s, sa, &len)) == -1)
|
|
|
|
DERR(EX_OSERR, "accept failed");
|
|
|
|
|
|
|
|
new_mount(fd, PMNT_DEVFUSE);
|
2012-02-04 22:36:30 +04:00
|
|
|
}
|
2011-05-30 18:50:08 +04:00
|
|
|
default:
|
|
|
|
DERRX(EX_SOFTWARE, "unexpected so_type %d", sock_type);
|
|
|
|
break;
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-03 19:54:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
perfused_panic(void)
|
|
|
|
{
|
|
|
|
DWARNX("filesystem crashed");
|
|
|
|
exit(EX_OK);
|
|
|
|
}
|