1 /* $NetBSD: puffs_portal.c,v 1.11 2023/04/04 20:39:36 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Antti Kantee. All Rights Reserved. 5 * Development was supported by the Finnish Cultural Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 #ifndef lint 31 __RCSID("$NetBSD: puffs_portal.c,v 1.11 2023/04/04 20:39:36 rillig Exp $"); 32 #endif /* !lint */ 33 34 #include <sys/types.h> 35 #include <sys/wait.h> 36 #include <sys/socket.h> 37 38 #include <assert.h> 39 #include <err.h> 40 #include <errno.h> 41 #include <mntopts.h> 42 #include <paths.h> 43 #include <poll.h> 44 #include <puffs.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <util.h> 50 51 #include "portald.h" 52 53 struct portal_node { 54 char *path; 55 int fd; 56 }; 57 58 __dead static void usage(void); 59 60 PUFFSOP_PROTOS(portal) 61 62 #define PORTAL_ROOT NULL 63 #define METADATASIZE (sizeof(int) + sizeof(size_t)) 64 65 qelem q; 66 int readcfg, sigchild; 67 const char *cfg; 68 69 static void 70 usage(void) 71 { 72 73 errx(1, "usage: %s [-o options] /path/portal.conf mount_point", 74 getprogname()); 75 } 76 77 static void 78 sighup(int sig) 79 { 80 81 readcfg = 1; 82 } 83 84 static void 85 sigcry(int sig) 86 { 87 88 sigchild = 1; 89 } 90 91 static void 92 portal_loopfn(struct puffs_usermount *pu) 93 { 94 95 if (readcfg) 96 conf_read(&q, cfg); 97 readcfg = 0; 98 99 if (sigchild) { 100 sigchild = 0; 101 while (waitpid(-1, NULL, WNOHANG) != -1) 102 continue; 103 } 104 } 105 106 #define PUFBUF_FD 1 107 #define PUFBUF_DATA 2 108 109 #define CMSIZE (sizeof(struct cmsghdr) + sizeof(int)) 110 111 /* receive file descriptor produced by our child process */ 112 static int 113 readfd(struct puffs_framebuf *pufbuf, int fd, int *done) 114 { 115 struct cmsghdr *cmp; 116 struct msghdr msg; 117 struct iovec iov; 118 ssize_t n; 119 int error, rv; 120 121 rv = 0; 122 cmp = emalloc(CMSG_SPACE(sizeof(int))); 123 124 iov.iov_base = &error; 125 iov.iov_len = sizeof(int); 126 msg.msg_iov = &iov; 127 msg.msg_iovlen = 1; 128 msg.msg_name = NULL; 129 msg.msg_namelen = 0; 130 msg.msg_control = cmp; 131 msg.msg_controllen = CMSG_SPACE(sizeof(int)); 132 133 n = recvmsg(fd, &msg, 0); 134 if (n == -1) { 135 rv = errno; 136 goto out; 137 } 138 if (n == 0) { 139 rv = ECONNRESET; 140 goto out; 141 } 142 143 /* the data for the server */ 144 puffs_framebuf_putdata_atoff(pufbuf, 0, &error, sizeof(int)); 145 if (error) { 146 rv = error; 147 goto out; 148 } 149 puffs_framebuf_putdata_atoff(pufbuf, sizeof(int), 150 CMSG_DATA(cmp), sizeof(int)); 151 *done = 1; 152 153 out: 154 free(cmp); 155 return rv; 156 } 157 158 /* 159 * receive data from provider 160 * 161 * XXX: should read directly into the buffer and adjust offsets 162 * instead of doing memcpy 163 */ 164 static int 165 readdata(struct puffs_framebuf *pufbuf, int fd, int *done) 166 { 167 char buf[1024]; 168 size_t max; 169 ssize_t n; 170 size_t moved; 171 172 /* don't override metadata */ 173 if (puffs_framebuf_telloff(pufbuf) == 0) 174 puffs_framebuf_seekset(pufbuf, METADATASIZE); 175 puffs_framebuf_getdata_atoff(pufbuf, sizeof(int), &max, sizeof(size_t)); 176 moved = puffs_framebuf_tellsize(pufbuf) - METADATASIZE; 177 assert(max >= moved); 178 max -= moved; 179 180 do { 181 n = read(fd, buf, MIN(sizeof(buf), max)); 182 if (n == 0) { 183 /* 184 * Deal with EOF here by closing the file descriptor 185 * and thus causing an error on subsequent accesses. 186 * This is the last kevent notification we are going 187 * to be getting for regular files. 188 */ 189 close(fd); 190 if (moved) 191 break; 192 else 193 return -1; /* caught by read */ 194 } 195 if (n < 0) { 196 if (moved) 197 return 0; 198 199 if (errno != EAGAIN) 200 return errno; 201 else 202 return 0; 203 } 204 205 puffs_framebuf_putdata(pufbuf, buf, n); 206 moved += n; 207 max -= n; 208 } while (max > 0); 209 210 *done = 1; 211 212 return 0; 213 } 214 215 static int 216 portal_frame_rf(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf, 217 int fd, int *done) 218 { 219 int type; 220 221 if (puffs_framebuf_getdata_atoff(pufbuf, 0, &type, sizeof(int)) == -1) 222 return EINVAL; 223 224 if (type == PUFBUF_FD) 225 return readfd(pufbuf, fd, done); 226 else if (type == PUFBUF_DATA) 227 return readdata(pufbuf, fd, done); 228 else 229 abort(); 230 } 231 232 static int 233 portal_frame_wf(struct puffs_usermount *pu, struct puffs_framebuf *pufbuf, 234 int fd, int *done) 235 { 236 void *win; 237 size_t pbsize, pboff, winlen; 238 ssize_t n; 239 int error; 240 241 pboff = puffs_framebuf_telloff(pufbuf); 242 pbsize = puffs_framebuf_tellsize(pufbuf); 243 error = 0; 244 245 do { 246 assert(pbsize > pboff); 247 winlen = pbsize - pboff; 248 if (puffs_framebuf_getwindow(pufbuf, pboff, &win, &winlen)==-1) 249 return errno; 250 n = write(fd, win, winlen); 251 if (n == 0) { 252 if (pboff != 0) 253 break; 254 else 255 return -1; /* caught by node_write */ 256 } 257 if (n < 0) { 258 if (pboff != 0) 259 break; 260 261 if (errno != EAGAIN) 262 return errno; 263 return 0; 264 } 265 266 pboff += n; 267 puffs_framebuf_seekset(pufbuf, pboff); 268 } while (pboff != pbsize); 269 270 *done = 1; 271 puffs_framebuf_putdata_atoff(pufbuf, 0, &pboff, sizeof(size_t)); 272 return error; 273 } 274 275 /* transfer file descriptor to master file server */ 276 static int 277 sendfd(int s, int fd, int error) 278 { 279 struct cmsghdr *cmp; 280 struct msghdr msg; 281 struct iovec iov; 282 ssize_t n; 283 int rv; 284 285 rv = 0; 286 cmp = emalloc(CMSG_LEN(sizeof(int))); 287 288 iov.iov_base = &error; 289 iov.iov_len = sizeof(int); 290 291 msg.msg_iov = &iov; 292 msg.msg_iovlen = 1; 293 msg.msg_name = NULL; 294 msg.msg_namelen = 0; 295 if (error == 0) { 296 cmp->cmsg_level = SOL_SOCKET; 297 cmp->cmsg_type = SCM_RIGHTS; 298 cmp->cmsg_len = CMSG_LEN(sizeof(int)); 299 300 msg.msg_control = cmp; 301 msg.msg_controllen = CMSG_LEN(sizeof(int)); 302 *(int *)CMSG_DATA(cmp) = fd; 303 } else { 304 msg.msg_control = NULL; 305 msg.msg_controllen = 0; 306 } 307 308 n = sendmsg(s, &msg, 0); 309 if (n == -1) 310 rv = errno; 311 else if (n < (ssize_t)sizeof(int)) 312 rv = EPROTO; 313 314 free(cmp); 315 return rv; 316 } 317 318 /* 319 * Produce I/O file descriptor by forking (like original portald). 320 * 321 * child: run provider and transfer produced fd to parent 322 * parent: yield until child produces fd. receive it and store it. 323 */ 324 static int 325 provide(struct puffs_usermount *pu, struct portal_node *portn, 326 struct portal_cred *portc, char **v) 327 { 328 struct puffs_cc *pcc = puffs_cc_getcc(pu); 329 struct puffs_framebuf *pufbuf; 330 int s[2]; 331 int fd, error; 332 int data; 333 334 pufbuf = puffs_framebuf_make(); 335 if (pufbuf == NULL) 336 return ENOMEM; 337 338 data = PUFBUF_FD; 339 if (puffs_framebuf_putdata(pufbuf, &data, sizeof(int)) == -1) 340 goto bad; 341 342 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, s) == -1) 343 goto bad; 344 345 switch (fork()) { 346 case -1: 347 close(s[0]); close(s[1]); 348 goto bad; 349 case 0: 350 close(s[0]); 351 error = activate_argv(portc, portn->path, v, &fd); 352 sendfd(s[1], fd, error); 353 exit(0); 354 default: 355 close(s[1]); 356 puffs_framev_addfd(pu, s[0], PUFFS_FBIO_READ); 357 puffs_framev_enqueue_directreceive(pcc, s[0], pufbuf, 0); 358 puffs_framev_removefd(pu, s[0], 0); 359 close(s[0]); 360 361 if (puffs_framebuf_tellsize(pufbuf) < sizeof(int)) { 362 errno = EIO; 363 goto bad; 364 } 365 puffs_framebuf_getdata_atoff(pufbuf, 0, &error, sizeof(int)); 366 if (error) { 367 errno = error; 368 goto bad; 369 } 370 371 if (puffs_framebuf_tellsize(pufbuf) != 2*sizeof(int)) { 372 errno = EIO; 373 goto bad; 374 } 375 376 puffs_framebuf_getdata_atoff(pufbuf, sizeof(int), 377 &fd, sizeof(int)); 378 puffs_framebuf_destroy(pufbuf); 379 380 data = 1; 381 if (ioctl(fd, FIONBIO, &data) == -1) 382 return errno; 383 384 if (puffs_framev_addfd(pu, fd, PUFFS_FBIO_WRITE) == -1) 385 return errno; 386 387 portn->fd = fd; 388 return 0; 389 } 390 391 bad: 392 puffs_framebuf_destroy(pufbuf); 393 return errno; 394 } 395 396 int 397 main(int argc, char *argv[]) 398 { 399 struct puffs_usermount *pu; 400 struct puffs_ops *pops; 401 mntoptparse_t mp; 402 int pflags, mntflags; 403 int detach; 404 int ch; 405 406 setprogname(argv[0]); 407 408 mntflags = pflags = 0; 409 detach = 1; 410 while ((ch = getopt(argc, argv, "o:s")) != -1) { 411 switch (ch) { 412 case 'o': 413 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags); 414 if (mp == NULL) 415 err(1, "getmntopts"); 416 freemntopts(mp); 417 break; 418 case 's': /* stay on top */ 419 detach = 0; 420 break; 421 default: 422 usage(); 423 /*NOTREACHED*/ 424 } 425 } 426 pflags |= PUFFS_KFLAG_NOCACHE | PUFFS_KFLAG_LOOKUP_FULLPNBUF; 427 if (pflags & PUFFS_FLAG_OPDUMP) 428 detach = 0; 429 argc -= optind; 430 argv += optind; 431 432 if (argc != 2) 433 usage(); 434 435 PUFFSOP_INIT(pops); 436 437 PUFFSOP_SETFSNOP(pops, unmount); 438 PUFFSOP_SETFSNOP(pops, sync); 439 PUFFSOP_SETFSNOP(pops, statvfs); 440 441 PUFFSOP_SET(pops, portal, node, lookup); 442 PUFFSOP_SET(pops, portal, node, getattr); 443 PUFFSOP_SET(pops, portal, node, setattr); 444 PUFFSOP_SET(pops, portal, node, open); 445 PUFFSOP_SET(pops, portal, node, read); 446 PUFFSOP_SET(pops, portal, node, write); 447 PUFFSOP_SET(pops, portal, node, seek); 448 PUFFSOP_SET(pops, portal, node, poll); 449 PUFFSOP_SET(pops, portal, node, inactive); 450 PUFFSOP_SET(pops, portal, node, reclaim); 451 452 pu = puffs_init(pops, _PATH_PUFFS, "portal", NULL, pflags); 453 if (pu == NULL) 454 err(1, "init"); 455 456 if (signal(SIGHUP, sighup) == SIG_ERR) 457 warn("cannot set sighup handler"); 458 if (signal(SIGCHLD, sigcry) == SIG_ERR) 459 err(1, "cannot set sigchild handler"); 460 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) 461 err(1, "cannot ignore sigpipe"); 462 463 readcfg = 0; 464 cfg = argv[0]; 465 if (*cfg != '/') 466 errx(1, "need absolute path for config"); 467 q.q_forw = q.q_back = &q; 468 if (conf_read(&q, cfg) == -1) 469 err(1, "cannot read cfg \"%s\"", cfg); 470 471 puffs_ml_setloopfn(pu, portal_loopfn); 472 puffs_framev_init(pu, portal_frame_rf, portal_frame_wf, NULL,NULL,NULL); 473 474 if (detach) 475 if (puffs_daemon(pu, 1, 1) == -1) 476 err(1, "puffs_daemon"); 477 478 if (puffs_mount(pu, argv[1], mntflags, PORTAL_ROOT) == -1) 479 err(1, "mount"); 480 if (puffs_mainloop(pu) == -1) 481 err(1, "mainloop"); 482 483 return 0; 484 } 485 486 static struct portal_node * 487 makenode(const char *path) 488 { 489 struct portal_node *portn; 490 491 portn = emalloc(sizeof(struct portal_node)); 492 portn->path = estrdup(path); 493 portn->fd = -1; 494 495 return portn; 496 } 497 498 static void 499 credtr(struct portal_cred *portc, const struct puffs_cred *puffc, int mode) 500 { 501 memset(portc, 0, sizeof(struct portal_cred)); 502 503 portc->pcr_flag = mode; 504 puffs_cred_getuid(puffc, &portc->pcr_uid); 505 puffs_cred_getgid(puffc, &portc->pcr_gid); 506 puffs_cred_getgroups(puffc, portc->pcr_groups, 507 (short *)&portc->pcr_ngroups); 508 } 509 510 /* 511 * XXX: we could also simply already resolve the name at this stage 512 * instead of deferring it to open. But doing it in open is how the 513 * original portald does it, and I don't want to introduce any funny 514 * incompatibilities. 515 */ 516 int 517 portal_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc, 518 struct puffs_newinfo *pni, const struct puffs_cn *pcn) 519 { 520 struct portal_node *portn; 521 522 assert(opc == PORTAL_ROOT); 523 524 if (pcn->pcn_nameiop != NAMEI_LOOKUP 525 && pcn->pcn_nameiop != NAMEI_CREATE) 526 return EOPNOTSUPP; 527 528 portn = makenode(pcn->pcn_name); 529 puffs_newinfo_setcookie(pni, portn); 530 puffs_newinfo_setvtype(pni, VREG); 531 532 pcn->pcn_flags &= ~NAMEI_REQUIREDIR; 533 pcn->pcn_consume = strlen(pcn->pcn_name) - pcn->pcn_namelen; 534 535 return 0; 536 } 537 538 unsigned int fakeid = 3; 539 540 /* XXX: libpuffs'ize */ 541 int 542 portal_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc, 543 struct vattr *va, const struct puffs_cred *pcr) 544 { 545 struct timeval tv; 546 struct timespec ts; 547 int res = 0; 548 549 puffs_vattr_null(va); 550 if (opc == PORTAL_ROOT) { 551 va->va_type = VDIR; 552 va->va_mode = 0777; 553 va->va_nlink = 2; 554 va->va_uid = va->va_gid = 0; 555 #if 0 /* XXX Huh? */ 556 va->va_fileid = fakeid++; 557 #else 558 va->va_fileid = 2; /*XXX; ROOTINO*/ 559 #endif 560 va->va_size = va->va_bytes = 0; 561 va->va_gen = 0; 562 va->va_rdev = PUFFS_VNOVAL; 563 va->va_blocksize = DEV_BSIZE; 564 565 gettimeofday(&tv, NULL); 566 TIMEVAL_TO_TIMESPEC(&tv, &ts); 567 va->va_atime = va->va_ctime = va->va_mtime = 568 va->va_birthtime = ts; 569 } else { 570 /* cheat for now */ 571 int error; 572 int newfd; 573 struct stat st; 574 struct portal_node *portn = opc; 575 struct portal_cred portc; 576 char **v = conf_match(&q, portn->path); 577 578 if (v == NULL) 579 return ENOENT; 580 if (portn->fd == -1) { 581 credtr(&portc, pcr, 0777); 582 error = provide(pu, portn, &portc, v); 583 if (error) 584 return error; 585 newfd = 1; 586 } else 587 newfd = 0; 588 589 if (fstat(portn->fd, &st) == -1) 590 res = errno; 591 else { 592 #if 0 593 va->va_type = S_ISDIR(st.st_mode) ? VDIR : VREG; /* XXX */ 594 #else 595 va->va_type = puffs_mode2vt(st.st_mode); 596 #endif 597 /* puffs supplies S_IFMT bits later */ 598 va->va_mode = (st.st_mode & ~S_IFMT); 599 600 va->va_nlink = st.st_nlink ? st.st_nlink : 1; 601 va->va_uid = st.st_uid; 602 va->va_gid = st.st_gid; 603 va->va_fileid = st.st_ino ? st.st_ino : fakeid++; 604 va->va_size = va->va_bytes = st.st_size; 605 va->va_gen = 0; 606 va->va_rdev = st.st_rdev; 607 va->va_blocksize = st.st_blksize; 608 va->va_atime = st.st_atim; 609 va->va_ctime = st.st_ctim; 610 va->va_mtime = st.st_mtim; 611 va->va_birthtime = st.st_birthtim; 612 } 613 if (newfd) { 614 puffs_framev_removefd(pu, portn->fd, 0); 615 close(portn->fd); 616 portn->fd = -1; 617 } 618 } 619 620 return res; 621 } 622 623 /* for writing, just pretend we care */ 624 int 625 portal_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc, 626 const struct vattr *va, const struct puffs_cred *pcr) 627 { 628 629 return 0; 630 } 631 632 int 633 portal_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode, 634 const struct puffs_cred *pcr) 635 { 636 struct portal_node *portn = opc; 637 struct portal_cred portc; 638 char **v; 639 640 if (opc == PORTAL_ROOT) 641 return 0; 642 643 if (mode & O_NONBLOCK) 644 return EOPNOTSUPP; 645 646 v = conf_match(&q, portn->path); 647 if (v == NULL) 648 return ENOENT; 649 650 credtr(&portc, pcr, mode); 651 return provide(pu, portn, &portc, v); 652 } 653 654 int 655 portal_node_read(struct puffs_usermount *pu, puffs_cookie_t opc, 656 uint8_t *buf, off_t offset, size_t *resid, 657 const struct puffs_cred *pcr, int ioflag) 658 { 659 struct puffs_cc *pcc = puffs_cc_getcc(pu); 660 struct portal_node *portn = opc; 661 struct puffs_framebuf *pufbuf; 662 size_t xfersize, winsize, boff; 663 void *win; 664 int rv, error; 665 int data, dummy; 666 667 assert(opc != PORTAL_ROOT); 668 error = 0; 669 670 /* if we can't (re-)enable it, treat it as EOF */ 671 rv = puffs_framev_enablefd(pu, portn->fd, PUFFS_FBIO_READ); 672 if (rv == -1) 673 return 0; 674 675 pufbuf = puffs_framebuf_make(); 676 data = PUFBUF_DATA; 677 puffs_framebuf_putdata(pufbuf, &data, sizeof(int)); 678 puffs_framebuf_putdata(pufbuf, resid, sizeof(size_t)); 679 680 /* if we are doing nodelay, do read directly */ 681 if (ioflag & PUFFS_IO_NDELAY) { 682 rv = readdata(pufbuf, portn->fd, &dummy); 683 if (rv != 0) { 684 error = rv; 685 goto out; 686 } 687 } else { 688 rv = puffs_framev_enqueue_directreceive(pcc, 689 portn->fd, pufbuf, 0); 690 691 if (rv == -1) { 692 error = errno; 693 goto out; 694 } 695 } 696 697 xfersize = puffs_framebuf_tellsize(pufbuf) - METADATASIZE; 698 if (xfersize == 0) { 699 assert(ioflag & PUFFS_IO_NDELAY); 700 error = EAGAIN; 701 goto out; 702 } 703 704 *resid -= xfersize; 705 boff = 0; 706 while (xfersize > 0) { 707 winsize = xfersize; 708 rv = puffs_framebuf_getwindow(pufbuf, METADATASIZE, 709 &win, &winsize); 710 assert(rv == 0); 711 assert(winsize > 0); 712 713 memcpy(buf + boff, win, winsize); 714 xfersize -= winsize; 715 boff += winsize; 716 } 717 718 out: 719 puffs_framev_disablefd(pu, portn->fd, PUFFS_FBIO_READ); 720 puffs_framebuf_destroy(pufbuf); 721 722 /* a trickery, from readdata() */ 723 if (error == -1) 724 return 0; 725 return error; 726 } 727 728 int 729 portal_node_write(struct puffs_usermount *pu, puffs_cookie_t opc, 730 uint8_t *buf, off_t offset, size_t *resid, 731 const struct puffs_cred *pcr, int ioflag) 732 { 733 struct puffs_cc *pcc = puffs_cc_getcc(pu); 734 struct portal_node *portn = opc; 735 struct puffs_framebuf *pufbuf; 736 size_t written; 737 int error, rv, dummy; 738 739 assert(opc != PORTAL_ROOT); 740 741 pufbuf = puffs_framebuf_make(); 742 puffs_framebuf_putdata(pufbuf, buf, *resid); 743 744 error = 0; 745 if (ioflag & PUFFS_IO_NDELAY) { 746 rv = portal_frame_wf(pu, pufbuf, portn->fd, &dummy); 747 if (rv) { 748 error = rv; 749 goto out; 750 } 751 } else { 752 rv = puffs_framev_enqueue_directsend(pcc, portn->fd, pufbuf, 0); 753 if (rv == -1) { 754 error = errno; 755 goto out; 756 } 757 } 758 759 rv = puffs_framebuf_getdata_atoff(pufbuf, 0, &written, sizeof(size_t)); 760 assert(rv == 0); 761 assert(written <= *resid); 762 *resid -= written; 763 764 out: 765 puffs_framebuf_destroy(pufbuf); 766 if (error == -1) 767 error = 0; 768 return 0; 769 } 770 771 int 772 portal_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc, 773 off_t oldoff, off_t newoff, const struct puffs_cred *pcr) 774 { 775 struct portal_node *portn = opc; 776 777 if (opc == PORTAL_ROOT || portn->fd == -1) 778 return EOPNOTSUPP; 779 780 if (lseek(portn->fd, newoff, SEEK_SET) == -1) 781 return errno; 782 return 0; 783 } 784 785 int 786 portal_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events) 787 { 788 struct puffs_cc *pcc = puffs_cc_getcc(pu); 789 struct portal_node *portn = opc; 790 int what; 791 792 what = 0; 793 if (*events & POLLIN) 794 what |= PUFFS_FBIO_READ; 795 if (*events & POLLOUT) 796 what |= PUFFS_FBIO_WRITE; 797 if (*events & POLLERR) 798 what |= PUFFS_FBIO_ERROR; 799 800 if (puffs_framev_enqueue_waitevent(pcc, portn->fd, &what) == -1) { 801 *events = POLLERR; 802 return errno; 803 } 804 805 *events = 0; 806 if (what & PUFFS_FBIO_READ) 807 *events |= POLLIN; 808 if (what & PUFFS_FBIO_WRITE) 809 *events |= POLLOUT; 810 if (what & PUFFS_FBIO_ERROR) 811 *events |= POLLERR; 812 813 return 0; 814 } 815 816 int 817 portal_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc) 818 { 819 820 if (opc == PORTAL_ROOT) 821 return 0; 822 823 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1); 824 return 0; 825 } 826 827 int 828 portal_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc) 829 { 830 struct portal_node *portn = opc; 831 832 if (portn->fd != -1) { 833 puffs_framev_removefd(pu, portn->fd, 0); 834 close(portn->fd); 835 } 836 free(portn->path); 837 free(portn); 838 839 return 0; 840 } 841