149594Sbostic /*- 2*63180Sbostic * Copyright (c) 1982, 1986, 1991, 1993 3*63180Sbostic * The Regents of the University of California. All rights reserved. 423393Smckusick * 549594Sbostic * %sccs.include.redist.c% 649594Sbostic * 7*63180Sbostic * @(#)tty_tty.c 8.1 (Berkeley) 06/10/93 823393Smckusick */ 935Sbill 1035Sbill /* 113117Swnj * Indirect driver for controlling tty. 1235Sbill */ 1356517Sbostic #include <sys/param.h> 1456517Sbostic #include <sys/systm.h> 1556517Sbostic #include <sys/conf.h> 1656517Sbostic #include <sys/ioctl.h> 1756517Sbostic #include <sys/proc.h> 1856517Sbostic #include <sys/tty.h> 1956517Sbostic #include <sys/vnode.h> 2056517Sbostic #include <sys/file.h> 2135Sbill 2239554Smarc #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL) 2339554Smarc 2435Sbill /*ARGSUSED*/ 2547648Skarels cttyopen(dev, flag, mode, p) 268558Sroot dev_t dev; 2747648Skarels int flag, mode; 2847648Skarels struct proc *p; 2935Sbill { 3047545Skarels struct vnode *ttyvp = cttyvp(p); 3139554Smarc int error; 3235Sbill 3339554Smarc if (ttyvp == NULL) 348558Sroot return (ENXIO); 3541191Smckusick VOP_LOCK(ttyvp); 3659454Smckusick #ifdef PARANOID 3759454Smckusick /* 3859490Smckusick * Since group is tty and mode is 620 on most terminal lines 3959454Smckusick * and since sessions protect terminals from processes outside 4059454Smckusick * your session, this check is probably no longer necessary. 4159454Smckusick * Since it inhibits setuid root programs that later switch 4259454Smckusick * to another user from accessing /dev/tty, we have decided 4359490Smckusick * to delete this test. (mckusick 5/93) 4459454Smckusick */ 4541191Smckusick error = VOP_ACCESS(ttyvp, 4648018Smckusick (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p); 4749915Smckusick if (!error) 4859454Smckusick #endif /* PARANOID */ 4949915Smckusick error = VOP_OPEN(ttyvp, flag, NOCRED, p); 5041191Smckusick VOP_UNLOCK(ttyvp); 5149915Smckusick return (error); 5235Sbill } 5335Sbill 5435Sbill /*ARGSUSED*/ 5549177Skarels cttyread(dev, uio, flag) 567824Sroot dev_t dev; 577824Sroot struct uio *uio; 5852502Storek int flag; 5935Sbill { 6049177Skarels register struct vnode *ttyvp = cttyvp(uio->uio_procp); 6139589Smckusick int error; 6235Sbill 6339554Smarc if (ttyvp == NULL) 6447648Skarels return (EIO); 6539589Smckusick VOP_LOCK(ttyvp); 6639589Smckusick error = VOP_READ(ttyvp, uio, flag, NOCRED); 6739589Smckusick VOP_UNLOCK(ttyvp); 6839589Smckusick return (error); 6935Sbill } 7035Sbill 7135Sbill /*ARGSUSED*/ 7249177Skarels cttywrite(dev, uio, flag) 737824Sroot dev_t dev; 747824Sroot struct uio *uio; 7552502Storek int flag; 7635Sbill { 7749177Skarels register struct vnode *ttyvp = cttyvp(uio->uio_procp); 7839589Smckusick int error; 7935Sbill 8039554Smarc if (ttyvp == NULL) 8147648Skarels return (EIO); 8239589Smckusick VOP_LOCK(ttyvp); 8339589Smckusick error = VOP_WRITE(ttyvp, uio, flag, NOCRED); 8439589Smckusick VOP_UNLOCK(ttyvp); 8539589Smckusick return (error); 8635Sbill } 8735Sbill 8835Sbill /*ARGSUSED*/ 8947648Skarels cttyioctl(dev, cmd, addr, flag, p) 905618Sroot dev_t dev; 915618Sroot int cmd; 925618Sroot caddr_t addr; 935618Sroot int flag; 9447648Skarels struct proc *p; 9535Sbill { 9647648Skarels struct vnode *ttyvp = cttyvp(p); 9735Sbill 9839554Smarc if (ttyvp == NULL) 9947648Skarels return (EIO); 1005390Swnj if (cmd == TIOCNOTTY) { 10147648Skarels if (!SESS_LEADER(p)) { 10247648Skarels p->p_flag &= ~SCTTY; 10339554Smarc return (0); 10439554Smarc } else 10539554Smarc return (EINVAL); 1065390Swnj } 10748018Smckusick return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p)); 10835Sbill } 1094487Swnj 1108591Sroot /*ARGSUSED*/ 11147648Skarels cttyselect(dev, flag, p) 1128591Sroot dev_t dev; 1138591Sroot int flag; 11447648Skarels struct proc *p; 1154487Swnj { 11647648Skarels struct vnode *ttyvp = cttyvp(p); 1174487Swnj 11839554Smarc if (ttyvp == NULL) 11943382Skarels return (1); /* try operation to get EOF/failure */ 12048018Smckusick return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p)); 1214487Swnj } 122