[BACK]Return to fdesc_vnops.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / miscfs / fdesc

Annotation of src/sys/miscfs/fdesc/fdesc_vnops.c, Revision 1.27

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

CVSweb <webmaster@jp.NetBSD.org>