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