| 1 | /*- |
| 2 | * Copyright (c) 1982, 1986, 1991 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | * |
| 7 | * @(#)tty_tty.c 7.20 (Berkeley) %G% |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Indirect driver for controlling tty. |
| 12 | */ |
| 13 | #include <sys/param.h> |
| 14 | #include <sys/systm.h> |
| 15 | #include <sys/conf.h> |
| 16 | #include <sys/ioctl.h> |
| 17 | #include <sys/proc.h> |
| 18 | #include <sys/tty.h> |
| 19 | #include <sys/vnode.h> |
| 20 | #include <sys/file.h> |
| 21 | |
| 22 | #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL) |
| 23 | |
| 24 | /*ARGSUSED*/ |
| 25 | cttyopen(dev, flag, mode, p) |
| 26 | dev_t dev; |
| 27 | int flag, mode; |
| 28 | struct proc *p; |
| 29 | { |
| 30 | struct vnode *ttyvp = cttyvp(p); |
| 31 | int error; |
| 32 | |
| 33 | if (ttyvp == NULL) |
| 34 | return (ENXIO); |
| 35 | VOP_LOCK(ttyvp); |
| 36 | error = VOP_ACCESS(ttyvp, |
| 37 | (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); |
| 38 | if (!error) |
| 39 | error = VOP_OPEN(ttyvp, flag, NOCRED, p); |
| 40 | VOP_UNLOCK(ttyvp); |
| 41 | return (error); |
| 42 | } |
| 43 | |
| 44 | /*ARGSUSED*/ |
| 45 | cttyread(dev, uio, flag) |
| 46 | dev_t dev; |
| 47 | struct uio *uio; |
| 48 | int flag; |
| 49 | { |
| 50 | register struct vnode *ttyvp = cttyvp(uio->uio_procp); |
| 51 | int error; |
| 52 | |
| 53 | if (ttyvp == NULL) |
| 54 | return (EIO); |
| 55 | VOP_LOCK(ttyvp); |
| 56 | error = VOP_READ(ttyvp, uio, flag, NOCRED); |
| 57 | VOP_UNLOCK(ttyvp); |
| 58 | return (error); |
| 59 | } |
| 60 | |
| 61 | /*ARGSUSED*/ |
| 62 | cttywrite(dev, uio, flag) |
| 63 | dev_t dev; |
| 64 | struct uio *uio; |
| 65 | int flag; |
| 66 | { |
| 67 | register struct vnode *ttyvp = cttyvp(uio->uio_procp); |
| 68 | int error; |
| 69 | |
| 70 | if (ttyvp == NULL) |
| 71 | return (EIO); |
| 72 | VOP_LOCK(ttyvp); |
| 73 | error = VOP_WRITE(ttyvp, uio, flag, NOCRED); |
| 74 | VOP_UNLOCK(ttyvp); |
| 75 | return (error); |
| 76 | } |
| 77 | |
| 78 | /*ARGSUSED*/ |
| 79 | cttyioctl(dev, cmd, addr, flag, p) |
| 80 | dev_t dev; |
| 81 | int cmd; |
| 82 | caddr_t addr; |
| 83 | int flag; |
| 84 | struct proc *p; |
| 85 | { |
| 86 | struct vnode *ttyvp = cttyvp(p); |
| 87 | |
| 88 | if (ttyvp == NULL) |
| 89 | return (EIO); |
| 90 | if (cmd == TIOCNOTTY) { |
| 91 | if (!SESS_LEADER(p)) { |
| 92 | p->p_flag &= ~SCTTY; |
| 93 | return (0); |
| 94 | } else |
| 95 | return (EINVAL); |
| 96 | } |
| 97 | return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); |
| 98 | } |
| 99 | |
| 100 | /*ARGSUSED*/ |
| 101 | cttyselect(dev, flag, p) |
| 102 | dev_t dev; |
| 103 | int flag; |
| 104 | struct proc *p; |
| 105 | { |
| 106 | struct vnode *ttyvp = cttyvp(p); |
| 107 | |
| 108 | if (ttyvp == NULL) |
| 109 | return (1); /* try operation to get EOF/failure */ |
| 110 | return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p)); |
| 111 | } |