/*-
* 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.
*/
int
perfused_open_sock(void)
{
int s;
struct sockaddr_un sun;
const struct sockaddr *sa;
uint32_t opt;
int sock_type = SOCK_SEQPACKET;
(void)unlink(_PATH_FUSE);
/*
* 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.");
/*
* Set a buffer length large enough so that a few FUSE packets
* will fit.
*/
opt = perfuse_bufvar_from_env("PERFUSE_BUFSIZE", 16 * FUSE_BUFSIZE);
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) != 0)
DWARN("%s: setsockopt SO_SNDBUF = %d failed", __func__, opt);
/*
* 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;
}
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
xchg_pb_early(struct puffs_usermount *pu, struct puffs_framebuf *pb, int fd,
enum perfuse_xchg_pb_reply reply)
{
int done;
int error;
done = 0;
while (done == 0) {
if ((error = perfused_writeframe(pu, pb, fd, &done)) != 0)
return error;
}
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
perfused_readframe(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
int fd, int *done)
{
struct fuse_out_header foh;
size_t len;
ssize_t readen;
void *data;
/*
* Read the header
*/
len = sizeof(foh);
PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
switch (readen = recv(fd, data, len, MSG_NOSIGNAL|MSG_PEEK)) {
case 0:
DPRINTF("Filesystem exit\n");
/* NOTREACHED */
exit(0);
break;
case -1:
if (errno == EAGAIN)
return 0;
DWARN("%s: recv returned -1", __func__);
return errno;
/* NOTREACHED */
break;
default:
break;
}
#ifdef PERFUSE_DEBUG
if (readen != (ssize_t)len)
DERRX(EX_SOFTWARE, "%s: short recv %zd/%zd",
__func__, readen, len);
#endif
/*
* We have a header, get remaining length to read
*/
if (puffs_framebuf_getdata_atoff(pufbuf, 0, &foh, sizeof(foh)) != 0)
DERR(EX_SOFTWARE, "puffs_framebuf_getdata_atoff failed");