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.10 (Berkeley) 03/17/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 "user.h" 16 #include "ioctl.h" 17 #include "tty.h" 18 #include "proc.h" 19 #include "vnode.h" 20 #include "file.h" 21 #include "uio.h" 22 23 #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL) 24 25 /*ARGSUSED*/ 26 cttyopen(dev, flag) 27 dev_t dev; 28 int flag; 29 { 30 struct proc *p = curproc; 31 struct vnode *ttyvp = cttyvp(p); 32 int error; 33 34 if (ttyvp == NULL) 35 return (ENXIO); 36 VOP_LOCK(ttyvp); 37 error = VOP_ACCESS(ttyvp, 38 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred); 39 VOP_UNLOCK(ttyvp); 40 if (error) 41 return (error); 42 return (VOP_OPEN(ttyvp, flag, NOCRED)); 43 } 44 45 /*ARGSUSED*/ 46 cttyread(dev, uio, flag) 47 dev_t dev; 48 struct uio *uio; 49 { 50 register struct vnode *ttyvp = cttyvp(curproc); 51 int error; 52 53 if (ttyvp == NULL) 54 return (ENXIO); 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 { 66 register struct vnode *ttyvp = cttyvp(curproc); 67 int error; 68 69 if (ttyvp == NULL) 70 return (ENXIO); 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) 79 dev_t dev; 80 int cmd; 81 caddr_t addr; 82 int flag; 83 { 84 struct vnode *ttyvp = cttyvp(curproc); 85 86 if (ttyvp == NULL) 87 return (ENXIO); 88 if (cmd == TIOCNOTTY) { 89 if (!SESS_LEADER(curproc)) { 90 curproc->p_flag &= ~SCTTY; 91 return (0); 92 } else 93 return (EINVAL); 94 } 95 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED)); 96 } 97 98 /*ARGSUSED*/ 99 cttyselect(dev, flag) 100 dev_t dev; 101 int flag; 102 { 103 struct vnode *ttyvp = cttyvp(curproc); 104 105 if (ttyvp == NULL) 106 return (1); /* try operation to get EOF/failure */ 107 return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED)); 108 } 109