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