fdesc_vnops.c revision 1.92.10.2 1 /* $NetBSD: fdesc_vnops.c,v 1.92.10.2 2006/12/10 07:18:59 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)fdesc_vnops.c 8.17 (Berkeley) 5/22/95
35 *
36 * #Id: fdesc_vnops.c,v 1.12 1993/04/06 16:17:17 jsp Exp #
37 */
38
39 /*
40 * /dev/fd Filesystem
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: fdesc_vnops.c,v 1.92.10.2 2006/12/10 07:18:59 yamt Exp $");
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/time.h>
49 #include <sys/proc.h>
50 #include <sys/kernel.h> /* boottime */
51 #include <sys/resourcevar.h>
52 #include <sys/socketvar.h>
53 #include <sys/filedesc.h>
54 #include <sys/vnode.h>
55 #include <sys/malloc.h>
56 #include <sys/conf.h>
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #include <sys/mount.h>
60 #include <sys/namei.h>
61 #include <sys/buf.h>
62 #include <sys/dirent.h>
63 #include <sys/tty.h>
64 #include <sys/kauth.h>
65
66 #include <miscfs/fdesc/fdesc.h>
67 #include <miscfs/genfs/genfs.h>
68
69 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
70
71 #define FDL_WANT 0x01
72 #define FDL_LOCKED 0x02
73 static int fdcache_lock;
74
75 dev_t devctty;
76
77 #if (FD_STDIN != FD_STDOUT-1) || (FD_STDOUT != FD_STDERR-1)
78 FD_STDIN, FD_STDOUT, FD_STDERR must be a sequence n, n+1, n+2
79 #endif
80
81 #define NFDCACHE 4
82
83 #define FD_NHASH(ix) \
84 (&fdhashtbl[(ix) & fdhash])
85 LIST_HEAD(fdhashhead, fdescnode) *fdhashtbl;
86 u_long fdhash;
87
88 int fdesc_lookup(void *);
89 #define fdesc_create genfs_eopnotsupp
90 #define fdesc_mknod genfs_eopnotsupp
91 int fdesc_open(void *);
92 #define fdesc_close genfs_nullop
93 #define fdesc_access genfs_nullop
94 int fdesc_getattr(void *);
95 int fdesc_setattr(void *);
96 int fdesc_read(void *);
97 int fdesc_write(void *);
98 int fdesc_ioctl(void *);
99 int fdesc_poll(void *);
100 int fdesc_kqfilter(void *);
101 #define fdesc_mmap genfs_eopnotsupp
102 #define fdesc_fcntl genfs_fcntl
103 #define fdesc_fsync genfs_nullop
104 #define fdesc_seek genfs_seek
105 #define fdesc_remove genfs_eopnotsupp
106 int fdesc_link(void *);
107 #define fdesc_rename genfs_eopnotsupp
108 #define fdesc_mkdir genfs_eopnotsupp
109 #define fdesc_rmdir genfs_eopnotsupp
110 int fdesc_symlink(void *);
111 int fdesc_readdir(void *);
112 int fdesc_readlink(void *);
113 #define fdesc_abortop genfs_abortop
114 int fdesc_inactive(void *);
115 int fdesc_reclaim(void *);
116 #define fdesc_lock genfs_lock
117 #define fdesc_unlock genfs_unlock
118 #define fdesc_bmap genfs_badop
119 #define fdesc_strategy genfs_badop
120 int fdesc_print(void *);
121 int fdesc_pathconf(void *);
122 #define fdesc_islocked genfs_islocked
123 #define fdesc_advlock genfs_einval
124 #define fdesc_bwrite genfs_eopnotsupp
125 #define fdesc_revoke genfs_revoke
126 #define fdesc_putpages genfs_null_putpages
127
128 static int fdesc_attr(int, struct vattr *, kauth_cred_t, struct lwp *);
129
130 int (**fdesc_vnodeop_p)(void *);
131 const struct vnodeopv_entry_desc fdesc_vnodeop_entries[] = {
132 { &vop_default_desc, vn_default_error },
133 { &vop_lookup_desc, fdesc_lookup }, /* lookup */
134 { &vop_create_desc, fdesc_create }, /* create */
135 { &vop_mknod_desc, fdesc_mknod }, /* mknod */
136 { &vop_open_desc, fdesc_open }, /* open */
137 { &vop_close_desc, fdesc_close }, /* close */
138 { &vop_access_desc, fdesc_access }, /* access */
139 { &vop_getattr_desc, fdesc_getattr }, /* getattr */
140 { &vop_setattr_desc, fdesc_setattr }, /* setattr */
141 { &vop_read_desc, fdesc_read }, /* read */
142 { &vop_write_desc, fdesc_write }, /* write */
143 { &vop_ioctl_desc, fdesc_ioctl }, /* ioctl */
144 { &vop_fcntl_desc, fdesc_fcntl }, /* fcntl */
145 { &vop_poll_desc, fdesc_poll }, /* poll */
146 { &vop_kqfilter_desc, fdesc_kqfilter }, /* kqfilter */
147 { &vop_revoke_desc, fdesc_revoke }, /* revoke */
148 { &vop_mmap_desc, fdesc_mmap }, /* mmap */
149 { &vop_fsync_desc, fdesc_fsync }, /* fsync */
150 { &vop_seek_desc, fdesc_seek }, /* seek */
151 { &vop_remove_desc, fdesc_remove }, /* remove */
152 { &vop_link_desc, fdesc_link }, /* link */
153 { &vop_rename_desc, fdesc_rename }, /* rename */
154 { &vop_mkdir_desc, fdesc_mkdir }, /* mkdir */
155 { &vop_rmdir_desc, fdesc_rmdir }, /* rmdir */
156 { &vop_symlink_desc, fdesc_symlink }, /* symlink */
157 { &vop_readdir_desc, fdesc_readdir }, /* readdir */
158 { &vop_readlink_desc, fdesc_readlink }, /* readlink */
159 { &vop_abortop_desc, fdesc_abortop }, /* abortop */
160 { &vop_inactive_desc, fdesc_inactive }, /* inactive */
161 { &vop_reclaim_desc, fdesc_reclaim }, /* reclaim */
162 { &vop_lock_desc, fdesc_lock }, /* lock */
163 { &vop_unlock_desc, fdesc_unlock }, /* unlock */
164 { &vop_bmap_desc, fdesc_bmap }, /* bmap */
165 { &vop_strategy_desc, fdesc_strategy }, /* strategy */
166 { &vop_print_desc, fdesc_print }, /* print */
167 { &vop_islocked_desc, fdesc_islocked }, /* islocked */
168 { &vop_pathconf_desc, fdesc_pathconf }, /* pathconf */
169 { &vop_advlock_desc, fdesc_advlock }, /* advlock */
170 { &vop_bwrite_desc, fdesc_bwrite }, /* bwrite */
171 { &vop_putpages_desc, fdesc_putpages }, /* putpages */
172 { NULL, NULL }
173 };
174
175 const struct vnodeopv_desc fdesc_vnodeop_opv_desc =
176 { &fdesc_vnodeop_p, fdesc_vnodeop_entries };
177
178 extern const struct cdevsw ctty_cdevsw;
179
180 /*
181 * Initialise cache headers
182 */
183 void
184 fdesc_init()
185 {
186 int cttymajor;
187
188 /* locate the major number */
189 cttymajor = cdevsw_lookup_major(&ctty_cdevsw);
190 devctty = makedev(cttymajor, 0);
191 fdhashtbl = hashinit(NFDCACHE, HASH_LIST, M_CACHE, M_NOWAIT, &fdhash);
192 }
193
194 /*
195 * Free hash table.
196 */
197 void
198 fdesc_done()
199 {
200 hashdone(fdhashtbl, M_CACHE);
201 }
202
203 /*
204 * Return a locked vnode of the correct type.
205 */
206 int
207 fdesc_allocvp(ftype, ix, mp, vpp)
208 fdntype ftype;
209 int ix;
210 struct mount *mp;
211 struct vnode **vpp;
212 {
213 struct fdhashhead *fc;
214 struct fdescnode *fd;
215 int error = 0;
216
217 fc = FD_NHASH(ix);
218 loop:
219 for (fd = fc->lh_first; fd != 0; fd = fd->fd_hash.le_next) {
220 if (fd->fd_ix == ix && fd->fd_vnode->v_mount == mp) {
221 if (vget(fd->fd_vnode, LK_EXCLUSIVE))
222 goto loop;
223 *vpp = fd->fd_vnode;
224 return (error);
225 }
226 }
227
228 /*
229 * otherwise lock the array while we call getnewvnode
230 * since that can block.
231 */
232 if (fdcache_lock & FDL_LOCKED) {
233 fdcache_lock |= FDL_WANT;
234 (void) tsleep(&fdcache_lock, PINOD, "fdcache", 0);
235 goto loop;
236 }
237 fdcache_lock |= FDL_LOCKED;
238
239 error = getnewvnode(VT_FDESC, mp, fdesc_vnodeop_p, vpp);
240 if (error)
241 goto out;
242 MALLOC(fd, void *, sizeof(struct fdescnode), M_TEMP, M_WAITOK);
243 (*vpp)->v_data = fd;
244 fd->fd_vnode = *vpp;
245 fd->fd_type = ftype;
246 fd->fd_fd = -1;
247 fd->fd_link = 0;
248 fd->fd_ix = ix;
249 VOP_LOCK(*vpp, LK_EXCLUSIVE);
250 LIST_INSERT_HEAD(fc, fd, fd_hash);
251
252 out:;
253 fdcache_lock &= ~FDL_LOCKED;
254
255 if (fdcache_lock & FDL_WANT) {
256 fdcache_lock &= ~FDL_WANT;
257 wakeup(&fdcache_lock);
258 }
259
260 return (error);
261 }
262
263 /*
264 * vp is the current namei directory
265 * ndp is the name to locate in that directory...
266 */
267 int
268 fdesc_lookup(v)
269 void *v;
270 {
271 struct vop_lookup_args /* {
272 struct vnode * a_dvp;
273 struct vnode ** a_vpp;
274 struct componentname * a_cnp;
275 } */ *ap = v;
276 struct vnode **vpp = ap->a_vpp;
277 struct vnode *dvp = ap->a_dvp;
278 struct componentname *cnp = ap->a_cnp;
279 struct lwp *l = cnp->cn_lwp;
280 const char *pname = cnp->cn_nameptr;
281 struct proc *p = l->l_proc;
282 int numfiles = p->p_fd->fd_nfiles;
283 unsigned fd = 0;
284 int error;
285 struct vnode *fvp;
286 const char *ln;
287
288 if (cnp->cn_namelen == 1 && *pname == '.') {
289 *vpp = dvp;
290 VREF(dvp);
291 return (0);
292 }
293
294 switch (VTOFDESC(dvp)->fd_type) {
295 default:
296 case Flink:
297 case Fdesc:
298 case Fctty:
299 error = ENOTDIR;
300 goto bad;
301
302 case Froot:
303 if (cnp->cn_namelen == 2 && memcmp(pname, "fd", 2) == 0) {
304 error = fdesc_allocvp(Fdevfd, FD_DEVFD, dvp->v_mount, &fvp);
305 if (error)
306 goto bad;
307 *vpp = fvp;
308 fvp->v_type = VDIR;
309 goto good;
310 }
311
312 if (cnp->cn_namelen == 3 && memcmp(pname, "tty", 3) == 0) {
313 struct vnode *ttyvp = cttyvp(p);
314 if (ttyvp == NULL) {
315 error = ENXIO;
316 goto bad;
317 }
318 error = fdesc_allocvp(Fctty, FD_CTTY, dvp->v_mount, &fvp);
319 if (error)
320 goto bad;
321 *vpp = fvp;
322 fvp->v_type = VCHR;
323 goto good;
324 }
325
326 ln = 0;
327 switch (cnp->cn_namelen) {
328 case 5:
329 if (memcmp(pname, "stdin", 5) == 0) {
330 ln = "fd/0";
331 fd = FD_STDIN;
332 }
333 break;
334 case 6:
335 if (memcmp(pname, "stdout", 6) == 0) {
336 ln = "fd/1";
337 fd = FD_STDOUT;
338 } else
339 if (memcmp(pname, "stderr", 6) == 0) {
340 ln = "fd/2";
341 fd = FD_STDERR;
342 }
343 break;
344 }
345
346 if (ln) {
347 error = fdesc_allocvp(Flink, fd, dvp->v_mount, &fvp);
348 if (error)
349 goto bad;
350 /* XXXUNCONST */
351 VTOFDESC(fvp)->fd_link = __UNCONST(ln);
352 *vpp = fvp;
353 fvp->v_type = VLNK;
354 goto good;
355 } else {
356 error = ENOENT;
357 goto bad;
358 }
359
360 /* FALL THROUGH */
361
362 case Fdevfd:
363 if (cnp->cn_namelen == 2 && memcmp(pname, "..", 2) == 0) {
364 VOP_UNLOCK(dvp, 0);
365 error = fdesc_root(dvp->v_mount, vpp);
366 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
367 if (error)
368 goto bad;
369 return (error);
370 }
371
372 fd = 0;
373 while (*pname >= '0' && *pname <= '9') {
374 fd = 10 * fd + *pname++ - '0';
375 if (fd >= numfiles)
376 break;
377 }
378
379 if (*pname != '\0') {
380 error = ENOENT;
381 goto bad;
382 }
383
384 if (fd >= numfiles || p->p_fd->fd_ofiles[fd] == NULL ||
385 FILE_IS_USABLE(p->p_fd->fd_ofiles[fd]) == 0) {
386 error = EBADF;
387 goto bad;
388 }
389
390 error = fdesc_allocvp(Fdesc, FD_DESC+fd, dvp->v_mount, &fvp);
391 if (error)
392 goto bad;
393 VTOFDESC(fvp)->fd_fd = fd;
394 *vpp = fvp;
395 goto good;
396 }
397
398 bad:
399 *vpp = NULL;
400 return (error);
401
402 good:
403 return (0);
404 }
405
406 int
407 fdesc_open(v)
408 void *v;
409 {
410 struct vop_open_args /* {
411 struct vnode *a_vp;
412 int a_mode;
413 kauth_cred_t a_cred;
414 struct lwp *a_l;
415 } */ *ap = v;
416 struct vnode *vp = ap->a_vp;
417
418 switch (VTOFDESC(vp)->fd_type) {
419 case Fdesc:
420 /*
421 * XXX Kludge: set dupfd to contain the value of the
422 * the file descriptor being sought for duplication. The error
423 * return ensures that the vnode for this device will be
424 * released by vn_open. Open will detect this special error and
425 * take the actions in dupfdopen. Other callers of vn_open or
426 * VOP_OPEN will simply report the error.
427 */
428 curlwp->l_dupfd = VTOFDESC(vp)->fd_fd; /* XXX */
429 return EDUPFD;
430
431 case Fctty:
432 return ((*ctty_cdevsw.d_open)(devctty, ap->a_mode, 0, ap->a_l));
433 case Froot:
434 case Fdevfd:
435 case Flink:
436 break;
437 }
438
439 return (0);
440 }
441
442 static int
443 fdesc_attr(fd, vap, cred, l)
444 int fd;
445 struct vattr *vap;
446 kauth_cred_t cred;
447 struct lwp *l;
448 {
449 struct proc *p = l->l_proc;
450 struct filedesc *fdp = p->p_fd;
451 struct file *fp;
452 struct stat stb;
453 int error;
454
455 if ((fp = fd_getfile(fdp, fd)) == NULL)
456 return (EBADF);
457
458 switch (fp->f_type) {
459 case DTYPE_VNODE:
460 simple_unlock(&fp->f_slock);
461 error = VOP_GETATTR((struct vnode *) fp->f_data, vap, cred, l);
462 if (error == 0 && vap->va_type == VDIR) {
463 /*
464 * directories can cause loops in the namespace,
465 * so turn off the 'x' bits to avoid trouble.
466 */
467 vap->va_mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
468 }
469 break;
470
471 default:
472 FILE_USE(fp);
473 memset(&stb, 0, sizeof(stb));
474 error = (*fp->f_ops->fo_stat)(fp, &stb, l);
475 FILE_UNUSE(fp, l);
476 if (error)
477 break;
478
479 vattr_null(vap);
480 switch(fp->f_type) {
481 case DTYPE_SOCKET:
482 vap->va_type = VSOCK;
483 break;
484 case DTYPE_PIPE:
485 vap->va_type = VFIFO;
486 break;
487 default:
488 /* use VNON perhaps? */
489 vap->va_type = VBAD;
490 break;
491 }
492 vap->va_mode = stb.st_mode;
493 vap->va_nlink = stb.st_nlink;
494 vap->va_uid = stb.st_uid;
495 vap->va_gid = stb.st_gid;
496 vap->va_fsid = stb.st_dev;
497 vap->va_fileid = stb.st_ino;
498 vap->va_size = stb.st_size;
499 vap->va_blocksize = stb.st_blksize;
500 vap->va_atime = stb.st_atimespec;
501 vap->va_mtime = stb.st_mtimespec;
502 vap->va_ctime = stb.st_ctimespec;
503 vap->va_gen = stb.st_gen;
504 vap->va_flags = stb.st_flags;
505 vap->va_rdev = stb.st_rdev;
506 vap->va_bytes = stb.st_blocks * stb.st_blksize;
507 break;
508 }
509
510 return (error);
511 }
512
513 int
514 fdesc_getattr(v)
515 void *v;
516 {
517 struct vop_getattr_args /* {
518 struct vnode *a_vp;
519 struct vattr *a_vap;
520 kauth_cred_t a_cred;
521 struct lwp *a_l;
522 } */ *ap = v;
523 struct vnode *vp = ap->a_vp;
524 struct vattr *vap = ap->a_vap;
525 unsigned fd;
526 int error = 0;
527
528 switch (VTOFDESC(vp)->fd_type) {
529 case Froot:
530 case Fdevfd:
531 case Flink:
532 case Fctty:
533 VATTR_NULL(vap);
534 vap->va_fileid = VTOFDESC(vp)->fd_ix;
535
536 #define R_ALL (S_IRUSR|S_IRGRP|S_IROTH)
537 #define W_ALL (S_IWUSR|S_IWGRP|S_IWOTH)
538 #define X_ALL (S_IXUSR|S_IXGRP|S_IXOTH)
539
540 switch (VTOFDESC(vp)->fd_type) {
541 case Flink:
542 vap->va_mode = R_ALL|X_ALL;
543 vap->va_type = VLNK;
544 vap->va_rdev = 0;
545 vap->va_nlink = 1;
546 vap->va_size = strlen(VTOFDESC(vp)->fd_link);
547 break;
548
549 case Fctty:
550 vap->va_mode = R_ALL|W_ALL;
551 vap->va_type = VCHR;
552 vap->va_rdev = devctty;
553 vap->va_nlink = 1;
554 vap->va_size = 0;
555 break;
556
557 default:
558 vap->va_mode = R_ALL|X_ALL;
559 vap->va_type = VDIR;
560 vap->va_rdev = 0;
561 vap->va_nlink = 2;
562 vap->va_size = DEV_BSIZE;
563 break;
564 }
565 vap->va_uid = 0;
566 vap->va_gid = 0;
567 vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
568 vap->va_blocksize = DEV_BSIZE;
569 vap->va_atime.tv_sec = boottime.tv_sec;
570 vap->va_atime.tv_nsec = 0;
571 vap->va_mtime = vap->va_atime;
572 vap->va_ctime = vap->va_mtime;
573 vap->va_gen = 0;
574 vap->va_flags = 0;
575 vap->va_bytes = 0;
576 break;
577
578 case Fdesc:
579 fd = VTOFDESC(vp)->fd_fd;
580 error = fdesc_attr(fd, vap, ap->a_cred, ap->a_l);
581 break;
582
583 default:
584 panic("fdesc_getattr");
585 break;
586 }
587
588 if (error == 0)
589 vp->v_type = vap->va_type;
590
591 return (error);
592 }
593
594 int
595 fdesc_setattr(v)
596 void *v;
597 {
598 struct vop_setattr_args /* {
599 struct vnode *a_vp;
600 struct vattr *a_vap;
601 kauth_cred_t a_cred;
602 struct lwp *a_l;
603 } */ *ap = v;
604 struct filedesc *fdp = ap->a_l->l_proc->p_fd;
605 struct file *fp;
606 unsigned fd;
607
608 /*
609 * Can't mess with the root vnode
610 */
611 switch (VTOFDESC(ap->a_vp)->fd_type) {
612 case Fdesc:
613 break;
614
615 case Fctty:
616 return (0);
617
618 default:
619 return (EACCES);
620 }
621
622 fd = VTOFDESC(ap->a_vp)->fd_fd;
623 if ((fp = fd_getfile(fdp, fd)) == NULL)
624 return (EBADF);
625
626 /*
627 * XXX: Can't reasonably set the attr's on any types currently.
628 * On vnode's this will cause truncation and socket/pipes make
629 * no sense.
630 */
631 simple_unlock(&fp->f_slock);
632 return (0);
633 }
634
635
636 struct fdesc_target {
637 ino_t ft_fileno;
638 u_char ft_type;
639 u_char ft_namlen;
640 const char *ft_name;
641 } fdesc_targets[] = {
642 #define N(s) sizeof(s)-1, s
643 { FD_DEVFD, DT_DIR, N("fd") },
644 { FD_STDIN, DT_LNK, N("stdin") },
645 { FD_STDOUT, DT_LNK, N("stdout") },
646 { FD_STDERR, DT_LNK, N("stderr") },
647 { FD_CTTY, DT_UNKNOWN, N("tty") },
648 #undef N
649 #define UIO_MX _DIRENT_RECLEN((struct dirent *)NULL, sizeof("stderr") - 1)
650 };
651 static int nfdesc_targets = sizeof(fdesc_targets) / sizeof(fdesc_targets[0]);
652
653 int
654 fdesc_readdir(v)
655 void *v;
656 {
657 struct vop_readdir_args /* {
658 struct vnode *a_vp;
659 struct uio *a_uio;
660 kauth_cred_t a_cred;
661 int *a_eofflag;
662 off_t **a_cookies;
663 int *a_ncookies;
664 } */ *ap = v;
665 struct uio *uio = ap->a_uio;
666 struct dirent d;
667 struct filedesc *fdp;
668 off_t i;
669 int j;
670 int error;
671 off_t *cookies = NULL;
672 int ncookies;
673
674 switch (VTOFDESC(ap->a_vp)->fd_type) {
675 case Fctty:
676 return 0;
677
678 case Fdesc:
679 return ENOTDIR;
680
681 default:
682 break;
683 }
684
685 fdp = curproc->p_fd;
686
687 if (uio->uio_resid < UIO_MX)
688 return EINVAL;
689 if (uio->uio_offset < 0)
690 return EINVAL;
691
692 error = 0;
693 i = uio->uio_offset;
694 (void)memset(&d, 0, UIO_MX);
695 d.d_reclen = UIO_MX;
696 if (ap->a_ncookies)
697 ncookies = uio->uio_resid / UIO_MX;
698 else
699 ncookies = 0;
700
701 if (VTOFDESC(ap->a_vp)->fd_type == Froot) {
702 struct fdesc_target *ft;
703
704 if (i >= nfdesc_targets)
705 return 0;
706
707 if (ap->a_ncookies) {
708 ncookies = min(ncookies, (nfdesc_targets - i));
709 cookies = malloc(ncookies * sizeof(off_t),
710 M_TEMP, M_WAITOK);
711 *ap->a_cookies = cookies;
712 *ap->a_ncookies = ncookies;
713 }
714
715 for (ft = &fdesc_targets[i]; uio->uio_resid >= UIO_MX &&
716 i < nfdesc_targets; ft++, i++) {
717 switch (ft->ft_fileno) {
718 case FD_CTTY:
719 if (cttyvp(curproc) == NULL)
720 continue;
721 break;
722
723 case FD_STDIN:
724 case FD_STDOUT:
725 case FD_STDERR:
726 if (fdp == NULL)
727 continue;
728 if ((ft->ft_fileno - FD_STDIN) >=
729 fdp->fd_nfiles)
730 continue;
731 if (fdp->fd_ofiles[ft->ft_fileno - FD_STDIN]
732 == NULL
733 || FILE_IS_USABLE(
734 fdp->fd_ofiles[ft->ft_fileno - FD_STDIN])
735 == 0)
736 continue;
737 break;
738 }
739
740 d.d_fileno = ft->ft_fileno;
741 d.d_namlen = ft->ft_namlen;
742 (void)memcpy(d.d_name, ft->ft_name, ft->ft_namlen + 1);
743 d.d_type = ft->ft_type;
744
745 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
746 break;
747 if (cookies)
748 *cookies++ = i + 1;
749 }
750 } else {
751 int nfdp = fdp ? fdp->fd_nfiles : 0;
752 if (ap->a_ncookies) {
753 ncookies = min(ncookies, nfdp + 2);
754 cookies = malloc(ncookies * sizeof(off_t),
755 M_TEMP, M_WAITOK);
756 *ap->a_cookies = cookies;
757 *ap->a_ncookies = ncookies;
758 }
759 for (; i - 2 < nfdp && uio->uio_resid >= UIO_MX; i++) {
760 switch (i) {
761 case 0:
762 case 1:
763 d.d_fileno = FD_ROOT; /* XXX */
764 d.d_namlen = i + 1;
765 (void)memcpy(d.d_name, "..", d.d_namlen);
766 d.d_name[i + 1] = '\0';
767 d.d_type = DT_DIR;
768 break;
769
770 default:
771 KASSERT(fdp != NULL);
772 j = (int)i - 2;
773 if (fdp == NULL || fdp->fd_ofiles[j] == NULL ||
774 FILE_IS_USABLE(fdp->fd_ofiles[j]) == 0)
775 continue;
776 d.d_fileno = j + FD_STDIN;
777 d.d_namlen = sprintf(d.d_name, "%d", j);
778 d.d_type = DT_UNKNOWN;
779 break;
780 }
781
782 if ((error = uiomove(&d, UIO_MX, uio)) != 0)
783 break;
784 if (cookies)
785 *cookies++ = i + 1;
786 }
787 }
788
789 if (ap->a_ncookies && error) {
790 free(*ap->a_cookies, M_TEMP);
791 *ap->a_ncookies = 0;
792 *ap->a_cookies = NULL;
793 }
794
795 uio->uio_offset = i;
796 return error;
797 }
798
799 int
800 fdesc_readlink(v)
801 void *v;
802 {
803 struct vop_readlink_args /* {
804 struct vnode *a_vp;
805 struct uio *a_uio;
806 kauth_cred_t a_cred;
807 } */ *ap = v;
808 struct vnode *vp = ap->a_vp;
809 int error;
810
811 if (vp->v_type != VLNK)
812 return (EPERM);
813
814 if (VTOFDESC(vp)->fd_type == Flink) {
815 char *ln = VTOFDESC(vp)->fd_link;
816 error = uiomove(ln, strlen(ln), ap->a_uio);
817 } else {
818 error = EOPNOTSUPP;
819 }
820
821 return (error);
822 }
823
824 int
825 fdesc_read(v)
826 void *v;
827 {
828 struct vop_read_args /* {
829 struct vnode *a_vp;
830 struct uio *a_uio;
831 int a_ioflag;
832 kauth_cred_t a_cred;
833 } */ *ap = v;
834 int error = EOPNOTSUPP;
835 struct vnode *vp = ap->a_vp;
836
837 switch (VTOFDESC(vp)->fd_type) {
838 case Fctty:
839 VOP_UNLOCK(vp, 0);
840 error = (*ctty_cdevsw.d_read)(devctty, ap->a_uio, ap->a_ioflag);
841 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
842 break;
843
844 default:
845 error = EOPNOTSUPP;
846 break;
847 }
848
849 return (error);
850 }
851
852 int
853 fdesc_write(v)
854 void *v;
855 {
856 struct vop_write_args /* {
857 struct vnode *a_vp;
858 struct uio *a_uio;
859 int a_ioflag;
860 kauth_cred_t a_cred;
861 } */ *ap = v;
862 int error = EOPNOTSUPP;
863 struct vnode *vp = ap->a_vp;
864
865 switch (VTOFDESC(vp)->fd_type) {
866 case Fctty:
867 VOP_UNLOCK(vp, 0);
868 error = (*ctty_cdevsw.d_write)(devctty, ap->a_uio,
869 ap->a_ioflag);
870 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
871 break;
872
873 default:
874 error = EOPNOTSUPP;
875 break;
876 }
877
878 return (error);
879 }
880
881 int
882 fdesc_ioctl(v)
883 void *v;
884 {
885 struct vop_ioctl_args /* {
886 struct vnode *a_vp;
887 u_long a_command;
888 void *a_data;
889 int a_fflag;
890 kauth_cred_t a_cred;
891 struct lwp *a_l;
892 } */ *ap = v;
893 int error = EOPNOTSUPP;
894
895 switch (VTOFDESC(ap->a_vp)->fd_type) {
896 case Fctty:
897 error = (*ctty_cdevsw.d_ioctl)(devctty, ap->a_command,
898 ap->a_data, ap->a_fflag,
899 ap->a_l);
900 break;
901
902 default:
903 error = EOPNOTSUPP;
904 break;
905 }
906
907 return (error);
908 }
909
910 int
911 fdesc_poll(v)
912 void *v;
913 {
914 struct vop_poll_args /* {
915 struct vnode *a_vp;
916 int a_events;
917 struct lwp *a_l;
918 } */ *ap = v;
919 int revents;
920
921 switch (VTOFDESC(ap->a_vp)->fd_type) {
922 case Fctty:
923 revents = (*ctty_cdevsw.d_poll)(devctty, ap->a_events, ap->a_l);
924 break;
925
926 default:
927 revents = genfs_poll(v);
928 break;
929 }
930
931 return (revents);
932 }
933
934 int
935 fdesc_kqfilter(v)
936 void *v;
937 {
938 struct vop_kqfilter_args /* {
939 struct vnode *a_vp;
940 struct knote *a_kn;
941 } */ *ap = v;
942 int error;
943 struct proc *p;
944 struct lwp *l;
945 struct file *fp;
946
947 switch (VTOFDESC(ap->a_vp)->fd_type) {
948 case Fctty:
949 error = (*ctty_cdevsw.d_kqfilter)(devctty, ap->a_kn);
950 break;
951
952 case Fdesc:
953 /* just invoke kqfilter for the underlying descriptor */
954 l = curlwp; /* XXX hopefully ok to use curproc here */
955 p = l->l_proc;
956 if ((fp = fd_getfile(p->p_fd, VTOFDESC(ap->a_vp)->fd_fd)) == NULL)
957 return (1);
958
959 FILE_USE(fp);
960 error = (*fp->f_ops->fo_kqfilter)(fp, ap->a_kn);
961 FILE_UNUSE(fp, l);
962 break;
963
964 default:
965 return (genfs_kqfilter(v));
966 }
967
968 return (error);
969 }
970
971 int
972 fdesc_inactive(v)
973 void *v;
974 {
975 struct vop_inactive_args /* {
976 struct vnode *a_vp;
977 struct lwp *a_l;
978 } */ *ap = v;
979 struct vnode *vp = ap->a_vp;
980
981 /*
982 * Clear out the v_type field to avoid
983 * nasty things happening in vgone().
984 */
985 VOP_UNLOCK(vp, 0);
986 vp->v_type = VNON;
987 return (0);
988 }
989
990 int
991 fdesc_reclaim(v)
992 void *v;
993 {
994 struct vop_reclaim_args /* {
995 struct vnode *a_vp;
996 } */ *ap = v;
997 struct vnode *vp = ap->a_vp;
998 struct fdescnode *fd = VTOFDESC(vp);
999
1000 LIST_REMOVE(fd, fd_hash);
1001 FREE(vp->v_data, M_TEMP);
1002 vp->v_data = 0;
1003
1004 return (0);
1005 }
1006
1007 /*
1008 * Return POSIX pathconf information applicable to special devices.
1009 */
1010 int
1011 fdesc_pathconf(v)
1012 void *v;
1013 {
1014 struct vop_pathconf_args /* {
1015 struct vnode *a_vp;
1016 int a_name;
1017 register_t *a_retval;
1018 } */ *ap = v;
1019
1020 switch (ap->a_name) {
1021 case _PC_LINK_MAX:
1022 *ap->a_retval = LINK_MAX;
1023 return (0);
1024 case _PC_MAX_CANON:
1025 *ap->a_retval = MAX_CANON;
1026 return (0);
1027 case _PC_MAX_INPUT:
1028 *ap->a_retval = MAX_INPUT;
1029 return (0);
1030 case _PC_PIPE_BUF:
1031 *ap->a_retval = PIPE_BUF;
1032 return (0);
1033 case _PC_CHOWN_RESTRICTED:
1034 *ap->a_retval = 1;
1035 return (0);
1036 case _PC_VDISABLE:
1037 *ap->a_retval = _POSIX_VDISABLE;
1038 return (0);
1039 case _PC_SYNC_IO:
1040 *ap->a_retval = 1;
1041 return (0);
1042 default:
1043 return (EINVAL);
1044 }
1045 /* NOTREACHED */
1046 }
1047
1048 /*
1049 * Print out the contents of a /dev/fd vnode.
1050 */
1051 /* ARGSUSED */
1052 int
1053 fdesc_print(void *v)
1054 {
1055 printf("tag VT_NON, fdesc vnode\n");
1056 return (0);
1057 }
1058
1059 int
1060 fdesc_link(v)
1061 void *v;
1062 {
1063 struct vop_link_args /* {
1064 struct vnode *a_dvp;
1065 struct vnode *a_vp;
1066 struct componentname *a_cnp;
1067 } */ *ap = v;
1068
1069 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1070 vput(ap->a_dvp);
1071 return (EROFS);
1072 }
1073
1074 int
1075 fdesc_symlink(v)
1076 void *v;
1077 {
1078 struct vop_symlink_args /* {
1079 struct vnode *a_dvp;
1080 struct vnode **a_vpp;
1081 struct componentname *a_cnp;
1082 struct vattr *a_vap;
1083 char *a_target;
1084 } */ *ap = v;
1085
1086 VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
1087 vput(ap->a_dvp);
1088 return (EROFS);
1089 }
1090