xref: /csrg-svn/sys/kern/tty_tty.c (revision 43382)
123393Smckusick /*
229112Smckusick  * Copyright (c) 1982, 1986 Regents of the University of California.
323393Smckusick  * All rights reserved.  The Berkeley software License Agreement
423393Smckusick  * specifies the terms and conditions for redistribution.
523393Smckusick  *
6*43382Skarels  *	@(#)tty_tty.c	7.9 (Berkeley) 06/21/90
723393Smckusick  */
835Sbill 
935Sbill /*
103117Swnj  * Indirect driver for controlling tty.
1135Sbill  */
1217097Sbloom #include "param.h"
1317097Sbloom #include "systm.h"
1417097Sbloom #include "conf.h"
1517097Sbloom #include "user.h"
1617097Sbloom #include "ioctl.h"
1717097Sbloom #include "tty.h"
1817097Sbloom #include "proc.h"
1939554Smarc #include "vnode.h"
2039554Smarc #include "file.h"
2117097Sbloom #include "uio.h"
2235Sbill 
2339554Smarc #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL)
2439554Smarc 
2535Sbill /*ARGSUSED*/
2635Sbill syopen(dev, flag)
278558Sroot 	dev_t dev;
288558Sroot 	int flag;
2935Sbill {
3039554Smarc 	struct vnode *ttyvp = cttyvp(u.u_procp);
3139554Smarc 	int error;
3235Sbill 
3339554Smarc 	if (ttyvp == NULL)
348558Sroot 		return (ENXIO);
3541191Smckusick 	VOP_LOCK(ttyvp);
3641191Smckusick 	error = VOP_ACCESS(ttyvp,
3741191Smckusick 	   (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), u.u_cred);
3841191Smckusick 	VOP_UNLOCK(ttyvp);
3941191Smckusick 	if (error)
4039554Smarc 		return (error);
4139554Smarc 	return (VOP_OPEN(ttyvp, flag, NOCRED));
4235Sbill }
4335Sbill 
4435Sbill /*ARGSUSED*/
4537588Smarc syread(dev, uio, flag)
467824Sroot 	dev_t dev;
477824Sroot 	struct uio *uio;
4835Sbill {
4939589Smckusick 	register struct vnode *ttyvp = cttyvp(u.u_procp);
5039589Smckusick 	int error;
5135Sbill 
5239554Smarc 	if (ttyvp == NULL)
538522Sroot 		return (ENXIO);
5439589Smckusick 	VOP_LOCK(ttyvp);
5539589Smckusick 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
5639589Smckusick 	VOP_UNLOCK(ttyvp);
5739589Smckusick 	return (error);
5835Sbill }
5935Sbill 
6035Sbill /*ARGSUSED*/
6137588Smarc sywrite(dev, uio, flag)
627824Sroot 	dev_t dev;
637824Sroot 	struct uio *uio;
6435Sbill {
6539589Smckusick 	register struct vnode *ttyvp = cttyvp(u.u_procp);
6639589Smckusick 	int error;
6735Sbill 
6839554Smarc 	if (ttyvp == NULL)
698522Sroot 		return (ENXIO);
7039589Smckusick 	VOP_LOCK(ttyvp);
7139589Smckusick 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
7239589Smckusick 	VOP_UNLOCK(ttyvp);
7339589Smckusick 	return (error);
7435Sbill }
7535Sbill 
7635Sbill /*ARGSUSED*/
7735Sbill syioctl(dev, cmd, addr, flag)
785618Sroot 	dev_t dev;
795618Sroot 	int cmd;
805618Sroot 	caddr_t addr;
815618Sroot 	int flag;
8235Sbill {
8339554Smarc 	struct vnode *ttyvp = cttyvp(u.u_procp);
8435Sbill 
8539554Smarc 	if (ttyvp == NULL)
8639554Smarc 		return (ENXIO);
875390Swnj 	if (cmd == TIOCNOTTY) {
8839554Smarc 		if (!SESS_LEADER(u.u_procp)) {
8939554Smarc 			u.u_procp->p_flag &= ~SCTTY;
9039554Smarc 			return (0);
9139554Smarc 		} else
9239554Smarc 			return (EINVAL);
935390Swnj 	}
9439554Smarc 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED));
9535Sbill }
964487Swnj 
978591Sroot /*ARGSUSED*/
984487Swnj syselect(dev, flag)
998591Sroot 	dev_t dev;
1008591Sroot 	int flag;
1014487Swnj {
10239554Smarc 	struct vnode *ttyvp = cttyvp(u.u_procp);
1034487Swnj 
10439554Smarc 	if (ttyvp == NULL)
105*43382Skarels 		return (1);	/* try operation to get EOF/failure */
10640187Smckusick 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED));
1074487Swnj }
108