2011-08-31 00:17:00 +04:00
|
|
|
/* $NetBSD: msg.c,v 1.16 2011/08/30 20:17:01 joerg 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 <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <puffs.h>
|
2010-09-15 05:51:43 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <sys/types.h>
|
2010-08-25 11:18:01 +04:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <machine/vmparam.h>
|
|
|
|
|
|
|
|
#include "../../lib/libperfuse/perfuse_if.h"
|
|
|
|
#include "perfused.h"
|
|
|
|
|
|
|
|
static int xchg_pb_inloop(struct puffs_usermount *a, struct puffs_framebuf *,
|
|
|
|
int, enum perfuse_xchg_pb_reply);
|
|
|
|
static int xchg_pb_early(struct puffs_usermount *a, struct puffs_framebuf *,
|
|
|
|
int, enum perfuse_xchg_pb_reply);
|
|
|
|
|
|
|
|
int
|
|
|
|
perfuse_open_sock(void)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_un sun;
|
|
|
|
const struct sockaddr *sa;
|
2010-09-15 05:51:43 +04:00
|
|
|
uint32_t opt;
|
2011-05-30 18:50:08 +04:00
|
|
|
int sock_type = SOCK_SEQPACKET;
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
(void)unlink(_PATH_FUSE);
|
|
|
|
|
2011-05-30 18:50:08 +04:00
|
|
|
/*
|
|
|
|
* Try SOCK_SEQPACKET and fallback to SOCK_DGRAM
|
|
|
|
* if unavaible
|
|
|
|
*/
|
|
|
|
if ((s = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) == -1) {
|
|
|
|
warnx("SEQPACKET local sockets unavailable, using less "
|
|
|
|
"reliable DGRAM sockets. Expect file operation hangs.");
|
|
|
|
|
|
|
|
sock_type = SOCK_DGRAM;
|
|
|
|
if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1)
|
|
|
|
err(EX_OSERR, "socket failed");
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
sa = (const struct sockaddr *)(void *)&sun;
|
|
|
|
sun.sun_len = sizeof(sun);
|
|
|
|
sun.sun_family = AF_LOCAL;
|
|
|
|
(void)strcpy(sun.sun_path, _PATH_FUSE);
|
2010-09-15 05:51:43 +04:00
|
|
|
|
|
|
|
/*
|
2010-10-11 05:12:25 +04:00
|
|
|
* Set a buffer lentgh large enough so that a few FUSE packets
|
|
|
|
* will fit.
|
|
|
|
* XXX We will have to find how many packets we need
|
2010-09-15 05:51:43 +04:00
|
|
|
*/
|
2010-10-11 05:12:25 +04:00
|
|
|
opt = 4 * FUSE_BUFSIZE;
|
2010-09-15 05:51:43 +04:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) != 0)
|
|
|
|
DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
|
|
|
|
|
2010-10-11 05:12:25 +04:00
|
|
|
opt = 4 * FUSE_BUFSIZE;
|
2010-09-15 05:51:43 +04:00
|
|
|
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) != 0)
|
|
|
|
DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request peer credentials
|
|
|
|
*/
|
|
|
|
opt = 1;
|
|
|
|
if (setsockopt(s, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
|
|
|
|
DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (bind(s, sa, (socklen_t )sun.sun_len) == -1)
|
|
|
|
err(EX_OSERR, "cannot open \"%s\" socket", _PATH_FUSE);
|
|
|
|
|
2011-05-30 18:50:08 +04:00
|
|
|
if (sock_type == SOCK_DGRAM) {
|
|
|
|
if (connect(s, sa, (socklen_t )sun.sun_len) == -1)
|
|
|
|
err(EX_OSERR, "cannot open \"%s\" socket", _PATH_FUSE);
|
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_recv_early(int fd, struct sockcred *sockcred, size_t sockcred_len)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
2010-09-15 05:51:43 +04:00
|
|
|
struct fuse_out_header foh;
|
|
|
|
size_t len;
|
2010-08-25 11:18:01 +04:00
|
|
|
char *buf;
|
2010-09-15 05:51:43 +04:00
|
|
|
struct msghdr msg;
|
|
|
|
char cmsg_buf[sizeof(struct cmsghdr) + SOCKCREDSIZE(NGROUPS_MAX)];
|
|
|
|
struct cmsghdr *cmsg = (struct cmsghdr *)(void *)&cmsg_buf;
|
|
|
|
struct sockcred *sc = (struct sockcred *)(void *)(cmsg + 1);
|
|
|
|
struct iovec iov;
|
|
|
|
|
|
|
|
len = sizeof(foh);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
/*
|
|
|
|
* We use the complicated recvmsg because we want peer creds.
|
|
|
|
*/
|
|
|
|
iov.iov_base = &foh;
|
|
|
|
iov.iov_len = len;
|
|
|
|
msg.msg_name = NULL;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = cmsg;
|
|
|
|
msg.msg_controllen = sizeof(cmsg_buf);
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
|
|
|
if (recvmsg(fd, &msg, MSG_NOSIGNAL|MSG_PEEK) != (ssize_t)len) {
|
|
|
|
DWARN("short recv (header)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmsg->cmsg_type != SCM_CREDS) {
|
|
|
|
DWARNX("No SCM_CREDS");
|
2010-08-25 11:18:01 +04:00
|
|
|
return NULL;
|
2010-09-15 05:51:43 +04:00
|
|
|
}
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
if (sockcred != NULL)
|
|
|
|
(void)memcpy(sockcred, sc,
|
|
|
|
MIN(cmsg->cmsg_len - sizeof(*cmsg), sockcred_len));
|
|
|
|
|
|
|
|
|
|
|
|
len = foh.len;
|
|
|
|
if ((buf = malloc(len)) == NULL)
|
2010-09-01 18:57:24 +04:00
|
|
|
err(EX_OSERR, "malloc(%zd) failed", len);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-15 05:51:43 +04:00
|
|
|
if (recv(fd, buf, len, MSG_NOSIGNAL) != (ssize_t)len) {
|
|
|
|
DWARN("short recv (frame)");
|
2010-08-25 11:18:01 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
perfuse_msg_t *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_new_pb (struct puffs_usermount *pu, puffs_cookie_t opc, int opcode,
|
|
|
|
size_t payload_len, const struct puffs_cred *cred)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct fuse_in_header *fih;
|
|
|
|
struct puffs_cc *pcc;
|
|
|
|
uint64_t nodeid;
|
|
|
|
void *data;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
if ((pb = puffs_framebuf_make()) == NULL)
|
|
|
|
DERR(EX_OSERR, "puffs_framebuf_make failed");
|
|
|
|
|
|
|
|
len = payload_len + sizeof(*fih);
|
|
|
|
nodeid = (opc != 0) ? perfuse_get_ino(pu, opc) : PERFUSE_UNKNOWN_INO;
|
|
|
|
|
|
|
|
if (puffs_framebuf_reserve_space(pb, len) != 0)
|
|
|
|
DERR(EX_OSERR, "puffs_framebuf_reserve_space failed");
|
|
|
|
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &data, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != payload_len + sizeof(*fih))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short len");
|
|
|
|
|
|
|
|
(void)memset(data, 0, len);
|
|
|
|
fih = (struct fuse_in_header *)data;
|
2010-09-06 05:40:24 +04:00
|
|
|
fih->len = (uint32_t)len;
|
2010-08-25 11:18:01 +04:00
|
|
|
fih->opcode = opcode;
|
|
|
|
fih->unique = perfuse_next_unique(pu);
|
|
|
|
fih->nodeid = nodeid;
|
|
|
|
fih->pid = 0;
|
2011-08-02 18:53:38 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NULL creds is taken as FUSE root. This currently happens for:
|
|
|
|
* - mount root cred assumed
|
|
|
|
* - umount root cred assumed
|
|
|
|
* - inactive kernel cred used
|
|
|
|
* - statvfs root cred assumed
|
|
|
|
* - poll checks have been done at open() time
|
|
|
|
* - addvlock checks have been done at open() time
|
|
|
|
*/
|
|
|
|
if ((cred != NULL) && !puffs_cred_isjuggernaut(cred)) {
|
|
|
|
if (puffs_cred_getuid(cred, &fih->uid) != 0)
|
|
|
|
DERRX(EX_SOFTWARE, "puffs_cred_getuid failed");
|
|
|
|
if (puffs_cred_getgid(cred, &fih->gid) != 0)
|
|
|
|
DERRX(EX_SOFTWARE, "puffs_cred_getgid failed");
|
|
|
|
} else {
|
|
|
|
fih->uid = (uid_t)0;
|
|
|
|
fih->gid = (gid_t)0;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
2011-08-02 18:53:38 +04:00
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
if ((pcc = puffs_cc_getcc(pu)) != NULL)
|
|
|
|
(void)puffs_cc_getcaller(pcc, (pid_t *)&fih->pid, NULL);
|
|
|
|
|
|
|
|
return (perfuse_msg_t *)(void *)pb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* framebuf send/receive primitives based on pcc are
|
|
|
|
* not available until puffs mainloop is entered.
|
|
|
|
* This xchg_pb_inloop() variant allow early communication.
|
|
|
|
*/
|
|
|
|
static int
|
2011-08-31 00:17:00 +04:00
|
|
|
xchg_pb_early(struct puffs_usermount *pu, struct puffs_framebuf *pb, int fd,
|
|
|
|
enum perfuse_xchg_pb_reply reply)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
int done;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
done = 0;
|
|
|
|
while (done == 0) {
|
|
|
|
if ((error = perfuse_writeframe(pu, pb, fd, &done)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reply == no_reply) {
|
|
|
|
puffs_framebuf_destroy(pb);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
puffs_framebuf_recycle(pb);
|
|
|
|
}
|
|
|
|
|
|
|
|
done = 0;
|
|
|
|
while (done == 0) {
|
|
|
|
if ((error = perfuse_readframe(pu, pb, fd, &done)) != 0)
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-08-31 00:17:00 +04:00
|
|
|
xchg_pb_inloop(struct puffs_usermount *pu, struct puffs_framebuf *pb, int fd,
|
|
|
|
enum perfuse_xchg_pb_reply reply)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_cc *pcc;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (reply == no_reply) {
|
|
|
|
error = puffs_framev_enqueue_justsend(pu, fd, pb, 0, 0);
|
|
|
|
} else {
|
|
|
|
pcc = puffs_cc_getcc(pu);
|
|
|
|
error = puffs_framev_enqueue_cc(pcc, fd, pb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_xchg_pb(struct puffs_usermount *pu, perfuse_msg_t *pm,
|
|
|
|
size_t expected_len, enum perfuse_xchg_pb_reply reply)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb = (struct puffs_framebuf *)(void *)pm;
|
|
|
|
int fd;
|
|
|
|
int error;
|
|
|
|
struct fuse_out_header *foh;
|
|
|
|
#ifdef PERFUSE_DEBUG
|
|
|
|
struct fuse_in_header *fih;
|
|
|
|
uint64_t nodeid;
|
|
|
|
int opcode;
|
|
|
|
uint64_t unique_in;
|
|
|
|
uint64_t unique_out;
|
|
|
|
|
|
|
|
fih = perfuse_get_inhdr(pm);
|
|
|
|
unique_in = fih->unique;
|
|
|
|
nodeid = fih->nodeid;
|
|
|
|
opcode = fih->opcode;
|
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_FUSE)
|
2010-09-01 18:57:24 +04:00
|
|
|
DPRINTF("> unique = %"PRId64", nodeid = %"PRId64", "
|
|
|
|
"opcode = %s (%d)\n",
|
2010-08-25 11:18:01 +04:00
|
|
|
unique_in, nodeid, perfuse_opname(opcode), opcode);
|
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_DUMP)
|
|
|
|
perfuse_hexdump((char *)fih, fih->len);
|
|
|
|
|
|
|
|
#endif /* PERFUSE_DEBUG */
|
|
|
|
|
2010-09-01 18:57:24 +04:00
|
|
|
fd = (int)(long)perfuse_getspecific(pu);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (perfuse_inloop(pu))
|
|
|
|
error = xchg_pb_inloop(pu, pb, fd, reply);
|
|
|
|
else
|
|
|
|
error = xchg_pb_early(pu, pb, fd, reply);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
DERR(EX_SOFTWARE, "xchg_pb failed");
|
|
|
|
|
|
|
|
if (reply == no_reply)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
foh = perfuse_get_outhdr((perfuse_msg_t *)(void *)pb);
|
|
|
|
#ifdef PERFUSE_DEBUG
|
|
|
|
unique_out = foh->unique;
|
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_FUSE)
|
2010-09-01 18:57:24 +04:00
|
|
|
DPRINTF("< unique = %"PRId64", nodeid = %"PRId64", "
|
|
|
|
"opcode = %s (%d), "
|
2010-08-25 11:18:01 +04:00
|
|
|
"error = %d\n", unique_out, nodeid,
|
2011-05-06 17:45:35 +04:00
|
|
|
perfuse_opname(opcode), opcode, foh->error);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_DUMP)
|
|
|
|
perfuse_hexdump((char *)foh, foh->len);
|
|
|
|
|
|
|
|
if (unique_in != unique_out) {
|
2010-09-01 18:57:24 +04:00
|
|
|
printf("%s: packet mismatch unique %"PRId64" vs %"PRId64"\n",
|
2010-08-25 11:18:01 +04:00
|
|
|
__func__, unique_in, unique_out);
|
|
|
|
abort();
|
2010-09-01 18:57:24 +04:00
|
|
|
errx(EX_SOFTWARE, "%s: packet mismatch unique "
|
|
|
|
"%"PRId64" vs %"PRId64"\n",
|
2010-08-25 11:18:01 +04:00
|
|
|
__func__, unique_in, unique_out);
|
|
|
|
}
|
|
|
|
#endif /* PERFUSE_DEBUG */
|
|
|
|
|
|
|
|
if ((expected_len != PERFUSE_UNSPEC_REPLY_LEN) &&
|
|
|
|
(foh->len - sizeof(*foh) < expected_len) &&
|
|
|
|
(foh->error == 0)) {
|
|
|
|
DERRX(EX_PROTOCOL,
|
2010-09-01 18:57:24 +04:00
|
|
|
"Unexpected short reply: received %zd bytes, expected %zd",
|
2010-08-25 11:18:01 +04:00
|
|
|
foh->len - sizeof(*foh), expected_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((expected_len != 0) &&
|
|
|
|
(foh->len - sizeof(*foh) > expected_len))
|
|
|
|
DWARNX("Unexpected long reply");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Negative Linux errno...
|
|
|
|
*/
|
|
|
|
foh->error = -foh->error;
|
|
|
|
|
|
|
|
return foh->error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct fuse_in_header *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_get_inhdr(perfuse_msg_t *pm)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct fuse_in_header *fih;
|
|
|
|
void *hdr;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pb = (struct puffs_framebuf *)(void *)pm;
|
|
|
|
len = sizeof(*fih);
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != sizeof(*fih))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
fih = (struct fuse_in_header *)hdr;
|
|
|
|
|
|
|
|
return fih;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct fuse_out_header *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_get_outhdr(perfuse_msg_t *pm)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct fuse_out_header *foh;
|
|
|
|
void *hdr;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pb = (struct puffs_framebuf *)(void *)pm;
|
|
|
|
len = sizeof(*foh);
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != sizeof(*foh))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
foh = (struct fuse_out_header *)hdr;
|
|
|
|
|
|
|
|
return foh;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_get_inpayload(perfuse_msg_t *pm)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct fuse_in_header *fih;
|
|
|
|
void *hdr;
|
|
|
|
void *payload;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pb = (struct puffs_framebuf *)(void *)pm;
|
|
|
|
len = sizeof(*fih);
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != sizeof(*fih))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
fih = (struct fuse_in_header *)hdr;
|
|
|
|
|
|
|
|
len = fih->len - sizeof(*fih);
|
|
|
|
if (puffs_framebuf_getwindow(pb, sizeof(*fih), &payload, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != fih->len - sizeof(*fih))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
return (char *)payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_get_outpayload(perfuse_msg_t *pm)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct puffs_framebuf *pb;
|
|
|
|
struct fuse_out_header *foh;
|
|
|
|
void *hdr;
|
|
|
|
void *payload;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
pb = (struct puffs_framebuf *)(void *)pm;
|
|
|
|
len = sizeof(*foh);
|
|
|
|
if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != sizeof(*foh))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
foh = (struct fuse_out_header *)hdr;
|
|
|
|
|
|
|
|
len = foh->len - sizeof(*foh);
|
|
|
|
if (puffs_framebuf_getwindow(pb, sizeof(*foh), &payload, &len) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
|
|
|
|
if (len != foh->len - sizeof(*foh))
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
|
|
|
|
|
|
|
|
return (char *)payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PUFFS_FRAMEBUF_GETWINDOW(pb, offset, data, len) \
|
|
|
|
do { \
|
|
|
|
int pfg_error; \
|
|
|
|
size_t pfg_len = *(len); \
|
|
|
|
\
|
|
|
|
pfg_error = puffs_framebuf_getwindow(pb, offset, data, len); \
|
|
|
|
if (pfg_error != 0) \
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");\
|
|
|
|
\
|
|
|
|
if (*(len) != pfg_len) \
|
|
|
|
DERRX(EX_SOFTWARE, "puffs_framebuf_getwindow size"); \
|
|
|
|
} while (0 /* CONSTCOND */);
|
|
|
|
|
|
|
|
/* ARGSUSED0 */
|
|
|
|
int
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_readframe(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
|
|
|
|
int fd, int *done)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct fuse_out_header foh;
|
2010-10-11 09:37:58 +04:00
|
|
|
size_t len;
|
2010-08-25 11:18:01 +04:00
|
|
|
ssize_t readen;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the header
|
|
|
|
*/
|
2010-10-11 09:37:58 +04:00
|
|
|
len = sizeof(foh);
|
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
|
|
|
|
|
|
|
|
switch (readen = recv(fd, data, len, MSG_NOSIGNAL|MSG_PEEK)) {
|
|
|
|
case 0:
|
|
|
|
DWARNX("%s: recv retunred 0", __func__);
|
|
|
|
return ECONNRESET;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
DWARN("%s: recv retunred -1", __func__);
|
|
|
|
return errno;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 09:37:58 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2011-04-25 08:30:59 +04:00
|
|
|
if (readen != (ssize_t)len)
|
2010-10-11 09:37:58 +04:00
|
|
|
DERRX(EX_SOFTWARE, "%s: short recv %zd/%zd",
|
|
|
|
__func__, readen, len);
|
2010-09-15 05:51:43 +04:00
|
|
|
#endif
|
2010-10-11 09:37:58 +04:00
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
/*
|
|
|
|
* We have a header, get remaing length to read
|
|
|
|
*/
|
|
|
|
if (puffs_framebuf_getdata_atoff(pufbuf, 0, &foh, sizeof(foh)) != 0)
|
|
|
|
DERR(EX_SOFTWARE, "puffs_framebuf_getdata_atoff failed");
|
2010-10-11 09:37:58 +04:00
|
|
|
|
|
|
|
len = foh.len;
|
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2011-08-09 11:00:13 +04:00
|
|
|
if (len > (size_t)FUSE_BUFSIZE)
|
2011-04-25 08:30:59 +04:00
|
|
|
DERRX(EX_SOFTWARE, "%s: foh.len = %zu", __func__, len);
|
2010-08-25 11:18:01 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2010-10-11 09:37:58 +04:00
|
|
|
* This is time to reserve space.
|
2010-08-25 11:18:01 +04:00
|
|
|
*/
|
2010-10-11 09:37:58 +04:00
|
|
|
if (puffs_framebuf_reserve_space(pufbuf, len) == -1)
|
|
|
|
DERR(EX_OSERR, "puffs_framebuf_reserve_space failed");
|
2010-08-25 11:18:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And read the remaining data
|
|
|
|
*/
|
2010-10-11 09:37:58 +04:00
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
|
|
|
|
|
|
|
|
switch (readen = recv(fd, data, len, MSG_NOSIGNAL)) {
|
|
|
|
case 0:
|
|
|
|
DWARNX("%s: recv retunred 0", __func__);
|
|
|
|
return ECONNRESET;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return 0;
|
|
|
|
DWARN("%s: recv retunred -1", __func__);
|
|
|
|
return errno;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 09:37:58 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2011-04-25 08:30:59 +04:00
|
|
|
if (readen != (ssize_t)len)
|
2010-10-11 09:37:58 +04:00
|
|
|
DERRX(EX_SOFTWARE, "%s: short recv %zd/%zd",
|
|
|
|
__func__, readen, len);
|
|
|
|
#endif
|
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
*done = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED0 */
|
|
|
|
int
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_writeframe(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
|
|
|
|
int fd, int *done)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
ssize_t written;
|
|
|
|
void *data;
|
|
|
|
|
2010-10-11 09:37:58 +04:00
|
|
|
len = puffs_framebuf_tellsize(pufbuf);
|
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
|
|
|
|
|
|
|
|
switch (written = send(fd, data, len, MSG_NOSIGNAL)) {
|
|
|
|
case 0:
|
|
|
|
DWARNX("%s: send retunred 0", __func__);
|
|
|
|
return ECONNRESET;
|
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
case -1:
|
2011-04-25 08:30:59 +04:00
|
|
|
DWARN("%s: send retunred -1, errno = %d", __func__, errno);
|
|
|
|
switch(errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case ENOBUFS:
|
2011-05-30 18:50:08 +04:00
|
|
|
case EMSGSIZE:
|
2010-10-11 09:37:58 +04:00
|
|
|
return 0;
|
2011-04-25 08:30:59 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return errno;
|
|
|
|
break;
|
|
|
|
}
|
2010-10-11 09:37:58 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-08-25 11:18:01 +04:00
|
|
|
}
|
|
|
|
|
2010-10-11 09:37:58 +04:00
|
|
|
#ifdef PERFUSE_DEBUG
|
2011-04-25 08:30:59 +04:00
|
|
|
if (written != (ssize_t)len)
|
2010-10-11 09:37:58 +04:00
|
|
|
DERRX(EX_SOFTWARE, "%s: short send %zd/%zd",
|
|
|
|
__func__, written, len);
|
|
|
|
#endif
|
|
|
|
|
2010-08-25 11:18:01 +04:00
|
|
|
*done = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED0 */
|
|
|
|
int
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_cmpframe(struct puffs_usermount *pu, struct puffs_framebuf *pb1,
|
|
|
|
struct puffs_framebuf *pb2, int *match)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct fuse_in_header *fih;
|
|
|
|
struct fuse_out_header *foh;
|
|
|
|
uint64_t unique_in;
|
|
|
|
uint64_t unique_out;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(*fih);
|
2011-05-09 12:51:08 +04:00
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pb1, 0, (void **)(void *)&fih, &len);
|
2010-08-25 11:18:01 +04:00
|
|
|
unique_in = fih->unique;
|
|
|
|
|
|
|
|
len = sizeof(*foh);
|
2011-05-09 12:51:08 +04:00
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pb2, 0, (void **)(void *)&foh, &len);
|
2010-08-25 11:18:01 +04:00
|
|
|
unique_out = foh->unique;
|
|
|
|
|
|
|
|
return unique_in != unique_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ARGSUSED0 */
|
|
|
|
void
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
|
2010-08-25 11:18:01 +04:00
|
|
|
{
|
|
|
|
struct fuse_out_header *foh;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(*foh);
|
2011-05-09 12:51:08 +04:00
|
|
|
PUFFS_FRAMEBUF_GETWINDOW(pb, 0, (void **)(void *)&foh, &len);
|
2010-08-25 11:18:01 +04:00
|
|
|
|
2010-09-01 18:57:24 +04:00
|
|
|
DWARNX("Unexpected frame: unique = %"PRId64", error = %d",
|
2010-08-25 11:18:01 +04:00
|
|
|
foh->unique, foh->error);
|
|
|
|
#ifdef PERFUSE_DEBUG
|
|
|
|
perfuse_hexdump((char *)(void *)foh, foh->len);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-08-27 13:58:17 +04:00
|
|
|
void
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_fdnotify(struct puffs_usermount *pu, int fd, int what)
|
2010-08-27 13:58:17 +04:00
|
|
|
{
|
2010-09-06 05:40:24 +04:00
|
|
|
if (fd != (int)(long)perfuse_getspecific(pu))
|
2010-08-27 13:58:17 +04:00
|
|
|
DERRX(EX_SOFTWARE, "%s: unexpected notification for fd = %d",
|
|
|
|
__func__, fd);
|
|
|
|
|
|
|
|
if ((what != PUFFS_FBIO_READ) && (what != PUFFS_FBIO_WRITE))
|
|
|
|
DERRX(EX_SOFTWARE, "%s: unexpected notification what = 0x%x",
|
|
|
|
__func__, what);
|
|
|
|
|
|
|
|
if (perfuse_unmount(pu) != 0)
|
|
|
|
DWARN("unmount() failed");
|
|
|
|
|
|
|
|
if (shutdown(fd, SHUT_RDWR) != 0)
|
|
|
|
DWARN("shutdown() failed");
|
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_MISC)
|
|
|
|
DPRINTF("Exit");
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
2011-05-30 18:50:08 +04:00
|
|
|
|
|
|
|
void
|
2011-08-31 00:17:00 +04:00
|
|
|
perfuse_umount(struct puffs_usermount *pu)
|
2011-05-30 18:50:08 +04:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = (int)(long)perfuse_getspecific(pu);
|
|
|
|
|
|
|
|
if (shutdown(fd, SHUT_RDWR) != 0)
|
|
|
|
DWARN("shutdown() failed");
|
|
|
|
|
|
|
|
if (perfuse_diagflags & PDF_MISC)
|
|
|
|
DPRINTF("unmount");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|