xref: /csrg-svn/sys/kern/tty_tty.c (revision 47545)
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*47545Skarels  *	@(#)tty_tty.c	7.10 (Berkeley) 03/17/91
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*/
26*47545Skarels cttyopen(dev, flag)
278558Sroot 	dev_t dev;
288558Sroot 	int flag;
2935Sbill {
30*47545Skarels 	struct proc *p = curproc;
31*47545Skarels 	struct vnode *ttyvp = cttyvp(p);
3239554Smarc 	int error;
3335Sbill 
3439554Smarc 	if (ttyvp == NULL)
358558Sroot 		return (ENXIO);
3641191Smckusick 	VOP_LOCK(ttyvp);
3741191Smckusick 	error = VOP_ACCESS(ttyvp,
38*47545Skarels 	   (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred);
3941191Smckusick 	VOP_UNLOCK(ttyvp);
4041191Smckusick 	if (error)
4139554Smarc 		return (error);
4239554Smarc 	return (VOP_OPEN(ttyvp, flag, NOCRED));
4335Sbill }
4435Sbill 
4535Sbill /*ARGSUSED*/
46*47545Skarels cttyread(dev, uio, flag)
477824Sroot 	dev_t dev;
487824Sroot 	struct uio *uio;
4935Sbill {
50*47545Skarels 	register struct vnode *ttyvp = cttyvp(curproc);
5139589Smckusick 	int error;
5235Sbill 
5339554Smarc 	if (ttyvp == NULL)
548522Sroot 		return (ENXIO);
5539589Smckusick 	VOP_LOCK(ttyvp);
5639589Smckusick 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
5739589Smckusick 	VOP_UNLOCK(ttyvp);
5839589Smckusick 	return (error);
5935Sbill }
6035Sbill 
6135Sbill /*ARGSUSED*/
62*47545Skarels cttywrite(dev, uio, flag)
637824Sroot 	dev_t dev;
647824Sroot 	struct uio *uio;
6535Sbill {
66*47545Skarels 	register struct vnode *ttyvp = cttyvp(curproc);
6739589Smckusick 	int error;
6835Sbill 
6939554Smarc 	if (ttyvp == NULL)
708522Sroot 		return (ENXIO);
7139589Smckusick 	VOP_LOCK(ttyvp);
7239589Smckusick 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
7339589Smckusick 	VOP_UNLOCK(ttyvp);
7439589Smckusick 	return (error);
7535Sbill }
7635Sbill 
7735Sbill /*ARGSUSED*/
78*47545Skarels cttyioctl(dev, cmd, addr, flag)
795618Sroot 	dev_t dev;
805618Sroot 	int cmd;
815618Sroot 	caddr_t addr;
825618Sroot 	int flag;
8335Sbill {
84*47545Skarels 	struct vnode *ttyvp = cttyvp(curproc);
8535Sbill 
8639554Smarc 	if (ttyvp == NULL)
8739554Smarc 		return (ENXIO);
885390Swnj 	if (cmd == TIOCNOTTY) {
89*47545Skarels 		if (!SESS_LEADER(curproc)) {
90*47545Skarels 			curproc->p_flag &= ~SCTTY;
9139554Smarc 			return (0);
9239554Smarc 		} else
9339554Smarc 			return (EINVAL);
945390Swnj 	}
9539554Smarc 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED));
9635Sbill }
974487Swnj 
988591Sroot /*ARGSUSED*/
99*47545Skarels cttyselect(dev, flag)
1008591Sroot 	dev_t dev;
1018591Sroot 	int flag;
1024487Swnj {
103*47545Skarels 	struct vnode *ttyvp = cttyvp(curproc);
1044487Swnj 
10539554Smarc 	if (ttyvp == NULL)
10643382Skarels 		return (1);	/* try operation to get EOF/failure */
10740187Smckusick 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED));
1084487Swnj }
109