Home | History | Annotate | Line # | Download | only in perfused
msg.c revision 1.16
      1 /*  $NetBSD: msg.c,v 1.16 2011/08/30 20:17:01 joerg Exp $ */
      2 
      3 /*-
      4  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
      5  *
      6  *  Redistribution and use in source and binary forms, with or without
      7  *  modification, are permitted provided that the following conditions
      8  *  are met:
      9  *  1. Redistributions of source code must retain the above copyright
     10  *     notice, this list of conditions and the following disclaimer.
     11  *  2. Redistributions in binary form must reproduce the above copyright
     12  *     notice, this list of conditions and the following disclaimer in the
     13  *     documentation and/or other materials provided with the distribution.
     14  *
     15  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  *  POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include <stdio.h>
     29 #include <stdlib.h>
     30 #include <unistd.h>
     31 #include <err.h>
     32 #include <errno.h>
     33 #include <string.h>
     34 #include <sysexits.h>
     35 #include <syslog.h>
     36 #include <paths.h>
     37 #include <puffs.h>
     38 #include <limits.h>
     39 #include <sys/types.h>
     40 #include <sys/socket.h>
     41 #include <sys/un.h>
     42 #include <machine/vmparam.h>
     43 
     44 #include "../../lib/libperfuse/perfuse_if.h"
     45 #include "perfused.h"
     46 
     47 static int xchg_pb_inloop(struct puffs_usermount *a, struct puffs_framebuf *,
     48 	int, enum perfuse_xchg_pb_reply);
     49 static int xchg_pb_early(struct puffs_usermount *a, struct puffs_framebuf *,
     50 	int, enum perfuse_xchg_pb_reply);
     51 
     52 int
     53 perfuse_open_sock(void)
     54 {
     55 	int s;
     56 	struct sockaddr_un sun;
     57 	const struct sockaddr *sa;
     58 	uint32_t opt;
     59 	int sock_type = SOCK_SEQPACKET;
     60 
     61 	(void)unlink(_PATH_FUSE);
     62 
     63 	/*
     64 	 * Try SOCK_SEQPACKET and fallback to SOCK_DGRAM
     65 	 * if unavaible
     66 	 */
     67 	if ((s = socket(PF_LOCAL, SOCK_SEQPACKET, 0)) == -1) {
     68 		warnx("SEQPACKET local sockets unavailable, using less "
     69 		      "reliable DGRAM sockets. Expect file operation hangs.");
     70 
     71 		sock_type = SOCK_DGRAM;
     72 		if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) == -1)
     73 			err(EX_OSERR, "socket failed");
     74 	}
     75 
     76 	sa = (const struct sockaddr *)(void *)&sun;
     77 	sun.sun_len = sizeof(sun);
     78 	sun.sun_family = AF_LOCAL;
     79 	(void)strcpy(sun.sun_path, _PATH_FUSE);
     80 
     81 	/*
     82 	 * Set a buffer lentgh large enough so that a few FUSE packets
     83 	 * will fit.
     84 	 * XXX We will have to find how many packets we need
     85 	 */
     86 	opt = 4 * FUSE_BUFSIZE;
     87 	if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)) != 0)
     88 		DWARN("%s: setsockopt SO_SNDBUF to %d failed", __func__, opt);
     89 
     90 	opt = 4 * FUSE_BUFSIZE;
     91 	if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) != 0)
     92 		DWARN("%s: setsockopt SO_RCVBUF to %d failed", __func__, opt);
     93 
     94 	/*
     95 	 * Request peer credentials
     96 	 */
     97 	opt = 1;
     98 	if (setsockopt(s, 0, LOCAL_CREDS, &opt, sizeof(opt)) != 0)
     99 		DWARN("%s: setsockopt LOCAL_CREDS failed", __func__);
    100 
    101 	if (bind(s, sa, (socklen_t )sun.sun_len) == -1)
    102 		err(EX_OSERR, "cannot open \"%s\" socket", _PATH_FUSE);
    103 
    104 	if (sock_type == SOCK_DGRAM) {
    105 		if (connect(s, sa, (socklen_t )sun.sun_len) == -1)
    106 			err(EX_OSERR, "cannot open \"%s\" socket", _PATH_FUSE);
    107 	}
    108 
    109 	return s;
    110 }
    111 
    112 
    113 void *
    114 perfuse_recv_early(int fd, struct sockcred *sockcred, size_t sockcred_len)
    115 {
    116 	struct fuse_out_header foh;
    117 	size_t len;
    118 	char *buf;
    119 	struct msghdr msg;
    120 	char cmsg_buf[sizeof(struct cmsghdr) + SOCKCREDSIZE(NGROUPS_MAX)];
    121 	struct cmsghdr *cmsg = (struct cmsghdr *)(void *)&cmsg_buf;
    122 	struct sockcred *sc = (struct sockcred *)(void *)(cmsg + 1);
    123 	struct iovec iov;
    124 
    125 	len = sizeof(foh);
    126 
    127 	/*
    128 	 * We use the complicated recvmsg because we want peer creds.
    129 	 */
    130 	iov.iov_base = &foh;
    131 	iov.iov_len = len;
    132 	msg.msg_name = NULL;
    133 	msg.msg_namelen = 0;
    134 	msg.msg_iov = &iov;
    135 	msg.msg_iovlen = 1;
    136 	msg.msg_control = cmsg;
    137 	msg.msg_controllen = sizeof(cmsg_buf);
    138 	msg.msg_flags = 0;
    139 
    140 	if (recvmsg(fd, &msg, MSG_NOSIGNAL|MSG_PEEK) != (ssize_t)len) {
    141 		DWARN("short recv (header)");
    142 		return NULL;
    143 	}
    144 
    145 	if (cmsg->cmsg_type != SCM_CREDS) {
    146 		DWARNX("No SCM_CREDS");
    147 		return NULL;
    148 	}
    149 
    150 	if (sockcred != NULL)
    151 		(void)memcpy(sockcred, sc,
    152 			     MIN(cmsg->cmsg_len - sizeof(*cmsg), sockcred_len));
    153 
    154 
    155 	len = foh.len;
    156 	if ((buf = malloc(len)) == NULL)
    157 		err(EX_OSERR, "malloc(%zd) failed", len);
    158 
    159 	if (recv(fd, buf, len, MSG_NOSIGNAL) != (ssize_t)len) {
    160 		DWARN("short recv (frame)");
    161 		return NULL;
    162 	}
    163 
    164 	return buf;
    165 }
    166 
    167 
    168 perfuse_msg_t *
    169 perfuse_new_pb (struct puffs_usermount *pu, puffs_cookie_t opc, int opcode,
    170     size_t payload_len, const struct puffs_cred *cred)
    171 {
    172 	struct puffs_framebuf *pb;
    173 	struct fuse_in_header *fih;
    174 	struct puffs_cc *pcc;
    175 	uint64_t nodeid;
    176 	void *data;
    177 	size_t len;
    178 
    179 	if ((pb = puffs_framebuf_make()) == NULL)
    180 		DERR(EX_OSERR, "puffs_framebuf_make failed");
    181 
    182 	len = payload_len + sizeof(*fih);
    183 	nodeid = (opc != 0) ? perfuse_get_ino(pu, opc) : PERFUSE_UNKNOWN_INO;
    184 
    185 	if (puffs_framebuf_reserve_space(pb, len) != 0)
    186 		DERR(EX_OSERR, "puffs_framebuf_reserve_space failed");
    187 
    188 	if (puffs_framebuf_getwindow(pb, 0, &data, &len) != 0)
    189 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    190 	if (len != payload_len + sizeof(*fih))
    191 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short len");
    192 
    193 	(void)memset(data, 0, len);
    194 	fih = (struct fuse_in_header *)data;
    195 	fih->len = (uint32_t)len;
    196 	fih->opcode = opcode;
    197 	fih->unique = perfuse_next_unique(pu);
    198 	fih->nodeid = nodeid;
    199 	fih->pid = 0;
    200 
    201 	/*
    202 	 * NULL creds is taken as FUSE root. This currently happens for:
    203 	 * - mount	root cred assumed
    204 	 * - umount	root cred assumed
    205 	 * - inactive	kernel cred used
    206 	 * - statvfs	root cred assumed
    207 	 * - poll	checks have been done at open() time
    208 	 * - addvlock	checks have been done at open() time
    209 	 */
    210 	if ((cred != NULL) && !puffs_cred_isjuggernaut(cred)) {
    211 		if (puffs_cred_getuid(cred, &fih->uid) != 0)
    212 			DERRX(EX_SOFTWARE, "puffs_cred_getuid failed");
    213 		if (puffs_cred_getgid(cred, &fih->gid) != 0)
    214 			DERRX(EX_SOFTWARE, "puffs_cred_getgid failed");
    215 	} else {
    216 		fih->uid = (uid_t)0;
    217 		fih->gid = (gid_t)0;
    218 	}
    219 
    220 	if ((pcc = puffs_cc_getcc(pu)) != NULL)
    221 		(void)puffs_cc_getcaller(pcc, (pid_t *)&fih->pid, NULL);
    222 
    223 	return (perfuse_msg_t *)(void *)pb;
    224 }
    225 
    226 /*
    227  * framebuf send/receive primitives based on pcc are
    228  * not available until puffs mainloop is entered.
    229  * This xchg_pb_inloop() variant allow early communication.
    230  */
    231 static int
    232 xchg_pb_early(struct puffs_usermount *pu, struct puffs_framebuf *pb, int fd,
    233     enum perfuse_xchg_pb_reply reply)
    234 {
    235 	int done;
    236 	int error;
    237 
    238 	done = 0;
    239 	while (done == 0) {
    240 		if ((error = perfuse_writeframe(pu, pb, fd, &done)) != 0)
    241 			return error;
    242 	}
    243 
    244 	if (reply == no_reply) {
    245 		puffs_framebuf_destroy(pb);
    246 		return 0;
    247 	} else {
    248 		puffs_framebuf_recycle(pb);
    249 	}
    250 
    251 	done = 0;
    252 	while (done == 0) {
    253 		if ((error = perfuse_readframe(pu, pb, fd, &done)) != 0)
    254 			return error;
    255 	}
    256 
    257 	return 0;
    258 }
    259 
    260 static int
    261 xchg_pb_inloop(struct puffs_usermount *pu, struct puffs_framebuf *pb, int fd,
    262     enum perfuse_xchg_pb_reply reply)
    263 {
    264 	struct puffs_cc *pcc;
    265 	int error;
    266 
    267 	if (reply == no_reply) {
    268 		error = puffs_framev_enqueue_justsend(pu, fd, pb, 0, 0);
    269 	} else {
    270 		pcc = puffs_cc_getcc(pu);
    271 		error = puffs_framev_enqueue_cc(pcc, fd, pb, 0);
    272 	}
    273 
    274 	return error;
    275 }
    276 
    277 int
    278 perfuse_xchg_pb(struct puffs_usermount *pu, perfuse_msg_t *pm,
    279     size_t expected_len, enum perfuse_xchg_pb_reply reply)
    280 {
    281 	struct puffs_framebuf *pb = (struct puffs_framebuf *)(void *)pm;
    282 	int fd;
    283 	int error;
    284 	struct fuse_out_header *foh;
    285 #ifdef PERFUSE_DEBUG
    286 	struct fuse_in_header *fih;
    287 	uint64_t nodeid;
    288 	int opcode;
    289 	uint64_t unique_in;
    290 	uint64_t unique_out;
    291 
    292 	fih = perfuse_get_inhdr(pm);
    293 	unique_in = fih->unique;
    294 	nodeid = fih->nodeid;
    295 	opcode = fih->opcode;
    296 
    297 	if (perfuse_diagflags & PDF_FUSE)
    298 		DPRINTF("> unique = %"PRId64", nodeid = %"PRId64", "
    299 			"opcode = %s (%d)\n",
    300 			unique_in, nodeid, perfuse_opname(opcode), opcode);
    301 
    302 	if (perfuse_diagflags & PDF_DUMP)
    303 		perfuse_hexdump((char *)fih, fih->len);
    304 
    305 #endif /* PERFUSE_DEBUG */
    306 
    307 	fd = (int)(long)perfuse_getspecific(pu);
    308 
    309 	if (perfuse_inloop(pu))
    310 		error = xchg_pb_inloop(pu, pb, fd, reply);
    311 	else
    312 		error = xchg_pb_early(pu, pb, fd, reply);
    313 
    314 	if (error)
    315 		DERR(EX_SOFTWARE, "xchg_pb failed");
    316 
    317 	if (reply == no_reply)
    318 		return 0;
    319 
    320 	foh = perfuse_get_outhdr((perfuse_msg_t *)(void *)pb);
    321 #ifdef PERFUSE_DEBUG
    322 	unique_out = foh->unique;
    323 
    324 	if (perfuse_diagflags & PDF_FUSE)
    325 		DPRINTF("< unique = %"PRId64", nodeid = %"PRId64", "
    326 			"opcode = %s (%d), "
    327 			"error = %d\n", unique_out, nodeid,
    328 			perfuse_opname(opcode), opcode, foh->error);
    329 
    330 	if (perfuse_diagflags & PDF_DUMP)
    331 		perfuse_hexdump((char *)foh, foh->len);
    332 
    333 	if (unique_in != unique_out) {
    334 		printf("%s: packet mismatch unique %"PRId64" vs %"PRId64"\n",
    335 		     __func__, unique_in, unique_out);
    336 		abort();
    337 		errx(EX_SOFTWARE, "%s: packet mismatch unique "
    338 		     "%"PRId64" vs %"PRId64"\n",
    339 		     __func__, unique_in, unique_out);
    340 	}
    341 #endif /* PERFUSE_DEBUG */
    342 
    343 	if ((expected_len != PERFUSE_UNSPEC_REPLY_LEN) &&
    344 	    (foh->len - sizeof(*foh) < expected_len) &&
    345 	    (foh->error == 0)) {
    346 		DERRX(EX_PROTOCOL,
    347 		     "Unexpected short reply: received %zd bytes, expected %zd",
    348 		     foh->len - sizeof(*foh), expected_len);
    349 	}
    350 
    351 	if ((expected_len != 0) &&
    352 	    (foh->len - sizeof(*foh) > expected_len))
    353 		DWARNX("Unexpected long reply");
    354 
    355 	/*
    356 	 * Negative Linux errno...
    357 	 */
    358 	foh->error = -foh->error;
    359 
    360 	return foh->error;
    361 }
    362 
    363 
    364 struct fuse_in_header *
    365 perfuse_get_inhdr(perfuse_msg_t *pm)
    366 {
    367 	struct puffs_framebuf *pb;
    368 	struct fuse_in_header *fih;
    369 	void *hdr;
    370 	size_t len;
    371 
    372 	pb = (struct puffs_framebuf *)(void *)pm;
    373 	len = sizeof(*fih);
    374 	if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
    375 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    376 	if (len != sizeof(*fih))
    377 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    378 
    379 	fih = (struct fuse_in_header *)hdr;
    380 
    381 	return fih;
    382 }
    383 
    384 struct fuse_out_header *
    385 perfuse_get_outhdr(perfuse_msg_t *pm)
    386 {
    387 	struct puffs_framebuf *pb;
    388 	struct fuse_out_header *foh;
    389 	void *hdr;
    390 	size_t len;
    391 
    392 	pb = (struct puffs_framebuf *)(void *)pm;
    393 	len = sizeof(*foh);
    394 	if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
    395 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    396 	if (len != sizeof(*foh))
    397 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    398 
    399 	foh = (struct fuse_out_header *)hdr;
    400 
    401 	return foh;
    402 }
    403 
    404 char *
    405 perfuse_get_inpayload(perfuse_msg_t *pm)
    406 {
    407 	struct puffs_framebuf *pb;
    408 	struct fuse_in_header *fih;
    409 	void *hdr;
    410 	void *payload;
    411 	size_t len;
    412 
    413 	pb = (struct puffs_framebuf *)(void *)pm;
    414 	len = sizeof(*fih);
    415 	if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
    416 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    417 	if (len != sizeof(*fih))
    418 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    419 
    420 	fih = (struct fuse_in_header *)hdr;
    421 
    422 	len = fih->len - sizeof(*fih);
    423 	if (puffs_framebuf_getwindow(pb, sizeof(*fih), &payload, &len) != 0)
    424 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    425 	if (len != fih->len - sizeof(*fih))
    426 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    427 
    428 	return (char *)payload;
    429 }
    430 
    431 char *
    432 perfuse_get_outpayload(perfuse_msg_t *pm)
    433 {
    434 	struct puffs_framebuf *pb;
    435 	struct fuse_out_header *foh;
    436 	void *hdr;
    437 	void *payload;
    438 	size_t len;
    439 
    440 	pb = (struct puffs_framebuf *)(void *)pm;
    441 	len = sizeof(*foh);
    442 	if (puffs_framebuf_getwindow(pb, 0, &hdr, &len) != 0)
    443 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    444 	if (len != sizeof(*foh))
    445 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    446 
    447 	foh = (struct fuse_out_header *)hdr;
    448 
    449 	len = foh->len - sizeof(*foh);
    450 	if (puffs_framebuf_getwindow(pb, sizeof(*foh), &payload, &len) != 0)
    451 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");
    452 	if (len != foh->len - sizeof(*foh))
    453 		DERR(EX_SOFTWARE, "puffs_framebuf_getwindow short header");
    454 
    455 	return (char *)payload;
    456 }
    457 
    458 #define PUFFS_FRAMEBUF_GETWINDOW(pb, offset, data, len) 		     \
    459 	do {								     \
    460 		int pfg_error;						     \
    461 		size_t pfg_len = *(len);				     \
    462 									     \
    463 		pfg_error = puffs_framebuf_getwindow(pb, offset, data, len); \
    464 		if (pfg_error != 0)					     \
    465 			DERR(EX_SOFTWARE, "puffs_framebuf_getwindow failed");\
    466 									     \
    467 		if (*(len) != pfg_len)					     \
    468 			DERRX(EX_SOFTWARE, "puffs_framebuf_getwindow size"); \
    469 	} while (0 /* CONSTCOND */);
    470 
    471 /* ARGSUSED0 */
    472 int
    473 perfuse_readframe(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
    474     int fd, int *done)
    475 {
    476 	struct fuse_out_header foh;
    477 	size_t len;
    478 	ssize_t readen;
    479 	void *data;
    480 
    481 	/*
    482 	 * Read the header
    483 	 */
    484 	len = sizeof(foh);
    485 	PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
    486 
    487 	switch (readen = recv(fd, data, len, MSG_NOSIGNAL|MSG_PEEK)) {
    488 	case 0:
    489 		DWARNX("%s: recv retunred 0", __func__);
    490 		return ECONNRESET;
    491 		/* NOTREACHED */
    492 		break;
    493 	case -1:
    494 		if (errno == EAGAIN)
    495 			return 0;
    496 		DWARN("%s: recv retunred -1", __func__);
    497 		return errno;
    498 		/* NOTREACHED */
    499 		break;
    500 	default:
    501 		break;
    502 	}
    503 
    504 #ifdef PERFUSE_DEBUG
    505 	if (readen != (ssize_t)len)
    506 		DERRX(EX_SOFTWARE, "%s: short recv %zd/%zd",
    507 		      __func__, readen, len);
    508 #endif
    509 
    510 	/*
    511 	 * We have a header, get remaing length to read
    512 	 */
    513 	if (puffs_framebuf_getdata_atoff(pufbuf, 0, &foh, sizeof(foh)) != 0)
    514 		DERR(EX_SOFTWARE, "puffs_framebuf_getdata_atoff failed");
    515 
    516 	len = foh.len;
    517 
    518 #ifdef PERFUSE_DEBUG
    519 	if (len > (size_t)FUSE_BUFSIZE)
    520 		DERRX(EX_SOFTWARE, "%s: foh.len = %zu", __func__, len);
    521 #endif
    522 
    523 	/*
    524 	 * This is time to reserve space.
    525 	 */
    526 	if (puffs_framebuf_reserve_space(pufbuf, len) == -1)
    527 		DERR(EX_OSERR, "puffs_framebuf_reserve_space failed");
    528 
    529 	/*
    530 	 * And read the remaining data
    531 	 */
    532 	PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
    533 
    534 	switch (readen = recv(fd, data, len, MSG_NOSIGNAL)) {
    535 	case 0:
    536 		DWARNX("%s: recv retunred 0", __func__);
    537 		return ECONNRESET;
    538 		/* NOTREACHED */
    539 		break;
    540 	case -1:
    541 		if (errno == EAGAIN)
    542 			return 0;
    543 		DWARN("%s: recv retunred -1", __func__);
    544 		return errno;
    545 		/* NOTREACHED */
    546 		break;
    547 	default:
    548 		break;
    549 	}
    550 
    551 #ifdef PERFUSE_DEBUG
    552 	if (readen != (ssize_t)len)
    553 		DERRX(EX_SOFTWARE, "%s: short recv %zd/%zd",
    554 		      __func__, readen, len);
    555 #endif
    556 
    557 	*done = 1;
    558 	return 0;
    559 }
    560 
    561 /* ARGSUSED0 */
    562 int
    563 perfuse_writeframe(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf,
    564     int fd, int *done)
    565 {
    566 	size_t len;
    567 	ssize_t written;
    568 	void *data;
    569 
    570 	len = puffs_framebuf_tellsize(pufbuf);
    571 	PUFFS_FRAMEBUF_GETWINDOW(pufbuf, 0, &data, &len);
    572 
    573 	switch (written = send(fd, data, len, MSG_NOSIGNAL)) {
    574 	case 0:
    575 		DWARNX("%s: send retunred 0", __func__);
    576 		return ECONNRESET;
    577 		/* NOTREACHED */
    578 		break;
    579 	case -1:
    580 		DWARN("%s: send retunred -1, errno = %d", __func__, errno);
    581 		switch(errno) {
    582 		case EAGAIN:
    583 		case ENOBUFS:
    584 		case EMSGSIZE:
    585 			return 0;
    586 			break;
    587 		default:
    588 			return errno;
    589 			break;
    590 		}
    591 		/* NOTREACHED */
    592 		break;
    593 	default:
    594 		break;
    595 	}
    596 
    597 #ifdef PERFUSE_DEBUG
    598 	if (written != (ssize_t)len)
    599 		DERRX(EX_SOFTWARE, "%s: short send %zd/%zd",
    600 		      __func__, written, len);
    601 #endif
    602 
    603 	*done = 1;
    604 	return 0;
    605 }
    606 
    607 /* ARGSUSED0 */
    608 int
    609 perfuse_cmpframe(struct puffs_usermount *pu, struct puffs_framebuf *pb1,
    610     struct puffs_framebuf *pb2, int *match)
    611 {
    612 	struct fuse_in_header *fih;
    613 	struct fuse_out_header *foh;
    614 	uint64_t unique_in;
    615 	uint64_t unique_out;
    616 	size_t len;
    617 
    618 	len = sizeof(*fih);
    619 	PUFFS_FRAMEBUF_GETWINDOW(pb1, 0, (void **)(void *)&fih, &len);
    620 	unique_in = fih->unique;
    621 
    622 	len = sizeof(*foh);
    623 	PUFFS_FRAMEBUF_GETWINDOW(pb2, 0, (void **)(void *)&foh, &len);
    624 	unique_out = foh->unique;
    625 
    626 	return unique_in != unique_out;
    627 }
    628 
    629 /* ARGSUSED0 */
    630 void
    631 perfuse_gotframe(struct puffs_usermount *pu, struct puffs_framebuf *pb)
    632 {
    633 	struct fuse_out_header *foh;
    634 	size_t len;
    635 
    636 	len = sizeof(*foh);
    637 	PUFFS_FRAMEBUF_GETWINDOW(pb, 0, (void **)(void *)&foh, &len);
    638 
    639 	DWARNX("Unexpected frame: unique = %"PRId64", error = %d",
    640 	       foh->unique, foh->error);
    641 #ifdef PERFUSE_DEBUG
    642 	perfuse_hexdump((char *)(void *)foh, foh->len);
    643 #endif
    644 
    645 	return;
    646 }
    647 
    648 void
    649 perfuse_fdnotify(struct puffs_usermount *pu, int fd, int what)
    650 {
    651 	if (fd != (int)(long)perfuse_getspecific(pu))
    652 		DERRX(EX_SOFTWARE, "%s: unexpected notification for fd = %d",
    653 		      __func__, fd);
    654 
    655 	if ((what != PUFFS_FBIO_READ) && (what != PUFFS_FBIO_WRITE))
    656 		DERRX(EX_SOFTWARE, "%s: unexpected notification what = 0x%x",
    657 		      __func__, what);
    658 
    659 	if (perfuse_unmount(pu) != 0)
    660 		DWARN("unmount() failed");
    661 
    662 	if (shutdown(fd, SHUT_RDWR) != 0)
    663 		DWARN("shutdown() failed");
    664 
    665 	if (perfuse_diagflags & PDF_MISC)
    666 		DPRINTF("Exit");
    667 
    668 	exit(0);
    669 }
    670 
    671 void
    672 perfuse_umount(struct puffs_usermount *pu)
    673 {
    674 	int fd;
    675 
    676 	fd = (int)(long)perfuse_getspecific(pu);
    677 
    678 	if (shutdown(fd, SHUT_RDWR) != 0)
    679 		DWARN("shutdown() failed");
    680 
    681 	if (perfuse_diagflags & PDF_MISC)
    682 		DPRINTF("unmount");
    683 
    684 	return;
    685 }
    686