/*-
* 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.
*/
/*
* 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.
*/
static struct puffs_usermount *perfused_mount = NULL;
static FILE *perfused_trace = NULL;
/*
* Flags for new_mount()
*/
#define PMNT_DEVFUSE 0x0 /* We use /dev/fuse */
#define PMNT_SOCKPAIR 0x1 /* We use socketpair */
#define PMNT_DGRAM 0x2 /* We use SOCK_DGRAM sockets */
static int
access_mount(const char *mnt, uid_t uid, int ro)
{
struct stat st;
mode_t mode;
if (pmo == NULL) {
if (shutdown(fd, SHUT_RDWR) != 0)
DERR(EX_OSERR, "shutdown failed");
exit(EX_PROTOCOL);
}
/*
* We do not need peer creds beyond this point
*/
opt = 0;
if (setsockopt(fd, SOL_LOCAL, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
/*
* Connect to the remote socket if provided ans using SOCK_DGRAM
*/
if ((sock_type == SOCK_DGRAM) && sock) {
const struct sockaddr *sa;
struct sockaddr_un sun;
/*
* Mount information (source, target, mount flags...)
*/
sock_type = pmnt_flags & PMNT_DGRAM ? SOCK_DGRAM : SOCK_SEQPACKET;
get_mount_info(fd, &pmi, sock_type);
/*
* 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)
DERRX(EX_NOPERM, "insuficient privileges to mount on %s",
pmi.pmi_target);
if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_READ|PUFFS_FBIO_WRITE) == -1)
DERR(EX_SOFTWARE, "puffs_framev_addfd failed");
perfuse_setspecific(pu, (void *)(intptr_t)fd);
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
* This must be done after perfused_fs_init
*/
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");