puffs_portal.c revision 1.9.10.1 1 /* $NetBSD: puffs_portal.c,v 1.9.10.1 2019/06/10 22:05:34 christos 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.9.10.1 2019/06/10 22:05:34 christos 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 extern char *optarg;
400 extern int optind;
401 struct puffs_usermount *pu;
402 struct puffs_ops *pops;
403 mntoptparse_t mp;
404 int pflags, mntflags;
405 int detach;
406 int ch;
407
408 setprogname(argv[0]);
409
410 mntflags = pflags = 0;
411 detach = 1;
412 while ((ch = getopt(argc, argv, "o:s")) != -1) {
413 switch (ch) {
414 case 'o':
415 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
416 if (mp == NULL)
417 err(1, "getmntopts");
418 freemntopts(mp);
419 break;
420 case 's': /* stay on top */
421 detach = 0;
422 break;
423 default:
424 usage();
425 /*NOTREACHED*/
426 }
427 }
428 pflags |= PUFFS_KFLAG_NOCACHE | PUFFS_KFLAG_LOOKUP_FULLPNBUF;
429 if (pflags & PUFFS_FLAG_OPDUMP)
430 detach = 0;
431 argc -= optind;
432 argv += optind;
433
434 if (argc != 2)
435 usage();
436
437 PUFFSOP_INIT(pops);
438
439 PUFFSOP_SETFSNOP(pops, unmount);
440 PUFFSOP_SETFSNOP(pops, sync);
441 PUFFSOP_SETFSNOP(pops, statvfs);
442
443 PUFFSOP_SET(pops, portal, node, lookup);
444 PUFFSOP_SET(pops, portal, node, getattr);
445 PUFFSOP_SET(pops, portal, node, setattr);
446 PUFFSOP_SET(pops, portal, node, open);
447 PUFFSOP_SET(pops, portal, node, read);
448 PUFFSOP_SET(pops, portal, node, write);
449 PUFFSOP_SET(pops, portal, node, seek);
450 PUFFSOP_SET(pops, portal, node, poll);
451 PUFFSOP_SET(pops, portal, node, inactive);
452 PUFFSOP_SET(pops, portal, node, reclaim);
453
454 pu = puffs_init(pops, _PATH_PUFFS, "portal", NULL, pflags);
455 if (pu == NULL)
456 err(1, "init");
457
458 if (signal(SIGHUP, sighup) == SIG_ERR)
459 warn("cannot set sighup handler");
460 if (signal(SIGCHLD, sigcry) == SIG_ERR)
461 err(1, "cannot set sigchild handler");
462 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
463 err(1, "cannot ignore sigpipe");
464
465 readcfg = 0;
466 cfg = argv[0];
467 if (*cfg != '/')
468 errx(1, "need absolute path for config");
469 q.q_forw = q.q_back = &q;
470 if (conf_read(&q, cfg) == -1)
471 err(1, "cannot read cfg \"%s\"", cfg);
472
473 puffs_ml_setloopfn(pu, portal_loopfn);
474 puffs_framev_init(pu, portal_frame_rf, portal_frame_wf, NULL,NULL,NULL);
475
476 if (detach)
477 if (puffs_daemon(pu, 1, 1) == -1)
478 err(1, "puffs_daemon");
479
480 if (puffs_mount(pu, argv[1], mntflags, PORTAL_ROOT) == -1)
481 err(1, "mount");
482 if (puffs_mainloop(pu) == -1)
483 err(1, "mainloop");
484
485 return 0;
486 }
487
488 static struct portal_node *
489 makenode(const char *path)
490 {
491 struct portal_node *portn;
492
493 portn = emalloc(sizeof(struct portal_node));
494 portn->path = estrdup(path);
495 portn->fd = -1;
496
497 return portn;
498 }
499
500 static void
501 credtr(struct portal_cred *portc, const struct puffs_cred *puffc, int mode)
502 {
503 memset(portc, 0, sizeof(struct portal_cred));
504
505 portc->pcr_flag = mode;
506 puffs_cred_getuid(puffc, &portc->pcr_uid);
507 puffs_cred_getgid(puffc, &portc->pcr_gid);
508 puffs_cred_getgroups(puffc, portc->pcr_groups,
509 (short *)&portc->pcr_ngroups);
510 }
511
512 /*
513 * XXX: we could also simply already resolve the name at this stage
514 * instead of deferring it to open. But doing it in open is how the
515 * original portald does it, and I don't want to introduce any funny
516 * incompatibilities.
517 */
518 int
519 portal_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc,
520 struct puffs_newinfo *pni, const struct puffs_cn *pcn)
521 {
522 struct portal_node *portn;
523
524 assert(opc == PORTAL_ROOT);
525
526 if (pcn->pcn_nameiop != NAMEI_LOOKUP
527 && pcn->pcn_nameiop != NAMEI_CREATE)
528 return EOPNOTSUPP;
529
530 portn = makenode(pcn->pcn_name);
531 puffs_newinfo_setcookie(pni, portn);
532 puffs_newinfo_setvtype(pni, VREG);
533
534 pcn->pcn_flags &= ~NAMEI_REQUIREDIR;
535 pcn->pcn_consume = strlen(pcn->pcn_name) - pcn->pcn_namelen;
536
537 return 0;
538 }
539
540 unsigned int fakeid = 3;
541
542 /* XXX: libpuffs'ize */
543 int
544 portal_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc,
545 struct vattr *va, const struct puffs_cred *pcr)
546 {
547 struct timeval tv;
548 struct timespec ts;
549 int res = 0;
550
551 puffs_vattr_null(va);
552 if (opc == PORTAL_ROOT) {
553 va->va_type = VDIR;
554 va->va_mode = 0777;
555 va->va_nlink = 2;
556 va->va_uid = va->va_gid = 0;
557 #if 0 /* XXX Huh? */
558 va->va_fileid = fakeid++;
559 #else
560 va->va_fileid = 2; /*XXX; ROOTINO*/
561 #endif
562 va->va_size = va->va_bytes = 0;
563 va->va_gen = 0;
564 va->va_rdev = PUFFS_VNOVAL;
565 va->va_blocksize = DEV_BSIZE;
566
567 gettimeofday(&tv, NULL);
568 TIMEVAL_TO_TIMESPEC(&tv, &ts);
569 va->va_atime = va->va_ctime = va->va_mtime =
570 va->va_birthtime = ts;
571 } else {
572 /* cheat for now */
573 int error;
574 int newfd;
575 struct stat st;
576 struct portal_node *portn = opc;
577 struct portal_cred portc;
578 char **v = conf_match(&q, portn->path);
579
580 if (v == NULL)
581 return ENOENT;
582 if (portn->fd == -1) {
583 credtr(&portc, pcr, 0777);
584 error = provide(pu, portn, &portc, v);
585 if (error)
586 return error;
587 newfd = 1;
588 } else
589 newfd = 0;
590
591 if (fstat(portn->fd, &st) == -1)
592 res = errno;
593 else {
594 #if 0
595 va->va_type = S_ISDIR(st.st_mode) ? VDIR : VREG; /* XXX */
596 #else
597 va->va_type = puffs_mode2vt(st.st_mode);
598 #endif
599 /* puffs supplies S_IFMT bits later */
600 va->va_mode = (st.st_mode & ~S_IFMT);
601
602 va->va_nlink = st.st_nlink ? st.st_nlink : 1;
603 va->va_uid = st.st_uid;
604 va->va_gid = st.st_gid;
605 va->va_fileid = st.st_ino ? st.st_ino : fakeid++;
606 va->va_size = va->va_bytes = st.st_size;
607 va->va_gen = 0;
608 va->va_rdev = st.st_rdev;
609 va->va_blocksize = st.st_blksize;
610 va->va_atime = st.st_atim;
611 va->va_ctime = st.st_ctim;
612 va->va_mtime = st.st_mtim;
613 va->va_birthtime = st.st_birthtim;
614 }
615 if (newfd) {
616 puffs_framev_removefd(pu, portn->fd, 0);
617 close(portn->fd);
618 portn->fd = -1;
619 }
620 }
621
622 return res;
623 }
624
625 /* for writing, just pretend we care */
626 int
627 portal_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc,
628 const struct vattr *va, const struct puffs_cred *pcr)
629 {
630
631 return 0;
632 }
633
634 int
635 portal_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode,
636 const struct puffs_cred *pcr)
637 {
638 struct portal_node *portn = opc;
639 struct portal_cred portc;
640 char **v;
641
642 if (opc == PORTAL_ROOT)
643 return 0;
644
645 if (mode & O_NONBLOCK)
646 return EOPNOTSUPP;
647
648 v = conf_match(&q, portn->path);
649 if (v == NULL)
650 return ENOENT;
651
652 credtr(&portc, pcr, mode);
653 return provide(pu, portn, &portc, v);
654 }
655
656 int
657 portal_node_read(struct puffs_usermount *pu, puffs_cookie_t opc,
658 uint8_t *buf, off_t offset, size_t *resid,
659 const struct puffs_cred *pcr, int ioflag)
660 {
661 struct puffs_cc *pcc = puffs_cc_getcc(pu);
662 struct portal_node *portn = opc;
663 struct puffs_framebuf *pufbuf;
664 size_t xfersize, winsize, boff;
665 void *win;
666 int rv, error;
667 int data, dummy;
668
669 assert(opc != PORTAL_ROOT);
670 error = 0;
671
672 /* if we can't (re-)enable it, treat it as EOF */
673 rv = puffs_framev_enablefd(pu, portn->fd, PUFFS_FBIO_READ);
674 if (rv == -1)
675 return 0;
676
677 pufbuf = puffs_framebuf_make();
678 data = PUFBUF_DATA;
679 puffs_framebuf_putdata(pufbuf, &data, sizeof(int));
680 puffs_framebuf_putdata(pufbuf, resid, sizeof(size_t));
681
682 /* if we are doing nodelay, do read directly */
683 if (ioflag & PUFFS_IO_NDELAY) {
684 rv = readdata(pufbuf, portn->fd, &dummy);
685 if (rv != 0) {
686 error = rv;
687 goto out;
688 }
689 } else {
690 rv = puffs_framev_enqueue_directreceive(pcc,
691 portn->fd, pufbuf, 0);
692
693 if (rv == -1) {
694 error = errno;
695 goto out;
696 }
697 }
698
699 xfersize = puffs_framebuf_tellsize(pufbuf) - METADATASIZE;
700 if (xfersize == 0) {
701 assert(ioflag & PUFFS_IO_NDELAY);
702 error = EAGAIN;
703 goto out;
704 }
705
706 *resid -= xfersize;
707 boff = 0;
708 while (xfersize > 0) {
709 winsize = xfersize;
710 rv = puffs_framebuf_getwindow(pufbuf, METADATASIZE,
711 &win, &winsize);
712 assert(rv == 0);
713 assert(winsize > 0);
714
715 memcpy(buf + boff, win, winsize);
716 xfersize -= winsize;
717 boff += winsize;
718 }
719
720 out:
721 puffs_framev_disablefd(pu, portn->fd, PUFFS_FBIO_READ);
722 puffs_framebuf_destroy(pufbuf);
723
724 /* a trickery, from readdata() */
725 if (error == -1)
726 return 0;
727 return error;
728 }
729
730 int
731 portal_node_write(struct puffs_usermount *pu, puffs_cookie_t opc,
732 uint8_t *buf, off_t offset, size_t *resid,
733 const struct puffs_cred *pcr, int ioflag)
734 {
735 struct puffs_cc *pcc = puffs_cc_getcc(pu);
736 struct portal_node *portn = opc;
737 struct puffs_framebuf *pufbuf;
738 size_t written;
739 int error, rv, dummy;
740
741 assert(opc != PORTAL_ROOT);
742
743 pufbuf = puffs_framebuf_make();
744 puffs_framebuf_putdata(pufbuf, buf, *resid);
745
746 error = 0;
747 if (ioflag & PUFFS_IO_NDELAY) {
748 rv = portal_frame_wf(pu, pufbuf, portn->fd, &dummy);
749 if (rv) {
750 error = rv;
751 goto out;
752 }
753 } else {
754 rv = puffs_framev_enqueue_directsend(pcc, portn->fd, pufbuf, 0);
755 if (rv == -1) {
756 error = errno;
757 goto out;
758 }
759 }
760
761 rv = puffs_framebuf_getdata_atoff(pufbuf, 0, &written, sizeof(size_t));
762 assert(rv == 0);
763 assert(written <= *resid);
764 *resid -= written;
765
766 out:
767 puffs_framebuf_destroy(pufbuf);
768 if (error == -1)
769 error = 0;
770 return 0;
771 }
772
773 int
774 portal_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc,
775 off_t oldoff, off_t newoff, const struct puffs_cred *pcr)
776 {
777 struct portal_node *portn = opc;
778
779 if (opc == PORTAL_ROOT || portn->fd == -1)
780 return EOPNOTSUPP;
781
782 if (lseek(portn->fd, newoff, SEEK_SET) == -1)
783 return errno;
784 return 0;
785 }
786
787 int
788 portal_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events)
789 {
790 struct puffs_cc *pcc = puffs_cc_getcc(pu);
791 struct portal_node *portn = opc;
792 int what;
793
794 what = 0;
795 if (*events & POLLIN)
796 what |= PUFFS_FBIO_READ;
797 if (*events & POLLOUT)
798 what |= PUFFS_FBIO_WRITE;
799 if (*events & POLLERR)
800 what |= PUFFS_FBIO_ERROR;
801
802 if (puffs_framev_enqueue_waitevent(pcc, portn->fd, &what) == -1) {
803 *events = POLLERR;
804 return errno;
805 }
806
807 *events = 0;
808 if (what & PUFFS_FBIO_READ)
809 *events |= POLLIN;
810 if (what & PUFFS_FBIO_WRITE)
811 *events |= POLLOUT;
812 if (what & PUFFS_FBIO_ERROR)
813 *events |= POLLERR;
814
815 return 0;
816 }
817
818 int
819 portal_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc)
820 {
821
822 if (opc == PORTAL_ROOT)
823 return 0;
824
825 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1);
826 return 0;
827 }
828
829 int
830 portal_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc)
831 {
832 struct portal_node *portn = opc;
833
834 if (portn->fd != -1) {
835 puffs_framev_removefd(pu, portn->fd, 0);
836 close(portn->fd);
837 }
838 free(portn->path);
839 free(portn);
840
841 return 0;
842 }
843