xref: /csrg-svn/sys/kern/tty_tty.c (revision 56517)
149594Sbostic /*-
249594Sbostic  * Copyright (c) 1982, 1986, 1991 The Regents of the University of California.
349594Sbostic  * All rights reserved.
423393Smckusick  *
549594Sbostic  * %sccs.include.redist.c%
649594Sbostic  *
7*56517Sbostic  *	@(#)tty_tty.c	7.20 (Berkeley) 10/11/92
823393Smckusick  */
935Sbill 
1035Sbill /*
113117Swnj  * Indirect driver for controlling tty.
1235Sbill  */
13*56517Sbostic #include <sys/param.h>
14*56517Sbostic #include <sys/systm.h>
15*56517Sbostic #include <sys/conf.h>
16*56517Sbostic #include <sys/ioctl.h>
17*56517Sbostic #include <sys/proc.h>
18*56517Sbostic #include <sys/tty.h>
19*56517Sbostic #include <sys/vnode.h>
20*56517Sbostic #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);
3641191Smckusick 	error = VOP_ACCESS(ttyvp,
3748018Smckusick 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
3849915Smckusick 	if (!error)
3949915Smckusick 		error = VOP_OPEN(ttyvp, flag, NOCRED, p);
4041191Smckusick 	VOP_UNLOCK(ttyvp);
4149915Smckusick 	return (error);
4235Sbill }
4335Sbill 
4435Sbill /*ARGSUSED*/
4549177Skarels cttyread(dev, uio, flag)
467824Sroot 	dev_t dev;
477824Sroot 	struct uio *uio;
4852502Storek 	int flag;
4935Sbill {
5049177Skarels 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
5139589Smckusick 	int error;
5235Sbill 
5339554Smarc 	if (ttyvp == NULL)
5447648Skarels 		return (EIO);
5539589Smckusick 	VOP_LOCK(ttyvp);
5639589Smckusick 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
5739589Smckusick 	VOP_UNLOCK(ttyvp);
5839589Smckusick 	return (error);
5935Sbill }
6035Sbill 
6135Sbill /*ARGSUSED*/
6249177Skarels cttywrite(dev, uio, flag)
637824Sroot 	dev_t dev;
647824Sroot 	struct uio *uio;
6552502Storek 	int flag;
6635Sbill {
6749177Skarels 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
6839589Smckusick 	int error;
6935Sbill 
7039554Smarc 	if (ttyvp == NULL)
7147648Skarels 		return (EIO);
7239589Smckusick 	VOP_LOCK(ttyvp);
7339589Smckusick 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
7439589Smckusick 	VOP_UNLOCK(ttyvp);
7539589Smckusick 	return (error);
7635Sbill }
7735Sbill 
7835Sbill /*ARGSUSED*/
7947648Skarels cttyioctl(dev, cmd, addr, flag, p)
805618Sroot 	dev_t dev;
815618Sroot 	int cmd;
825618Sroot 	caddr_t addr;
835618Sroot 	int flag;
8447648Skarels 	struct proc *p;
8535Sbill {
8647648Skarels 	struct vnode *ttyvp = cttyvp(p);
8735Sbill 
8839554Smarc 	if (ttyvp == NULL)
8947648Skarels 		return (EIO);
905390Swnj 	if (cmd == TIOCNOTTY) {
9147648Skarels 		if (!SESS_LEADER(p)) {
9247648Skarels 			p->p_flag &= ~SCTTY;
9339554Smarc 			return (0);
9439554Smarc 		} else
9539554Smarc 			return (EINVAL);
965390Swnj 	}
9748018Smckusick 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
9835Sbill }
994487Swnj 
1008591Sroot /*ARGSUSED*/
10147648Skarels cttyselect(dev, flag, p)
1028591Sroot 	dev_t dev;
1038591Sroot 	int flag;
10447648Skarels 	struct proc *p;
1054487Swnj {
10647648Skarels 	struct vnode *ttyvp = cttyvp(p);
1074487Swnj 
10839554Smarc 	if (ttyvp == NULL)
10943382Skarels 		return (1);	/* try operation to get EOF/failure */
11048018Smckusick 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
1114487Swnj }
112