xref: /csrg-svn/sys/kern/tty_tty.c (revision 49177)
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.13 (Berkeley) 05/05/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 "ioctl.h"
16 #include "tty.h"
17 #include "proc.h"
18 #include "vnode.h"
19 #include "file.h"
20 
21 #define cttyvp(p) ((p)->p_flag&SCTTY ? (p)->p_session->s_ttyvp : NULL)
22 
23 /*ARGSUSED*/
24 cttyopen(dev, flag, mode, p)
25 	dev_t dev;
26 	int flag, mode;
27 	struct proc *p;
28 {
29 	struct vnode *ttyvp = cttyvp(p);
30 	int error;
31 
32 	if (ttyvp == NULL)
33 		return (ENXIO);
34 	VOP_LOCK(ttyvp);
35 	error = VOP_ACCESS(ttyvp,
36 	  (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, p);
37 	VOP_UNLOCK(ttyvp);
38 	if (error)
39 		return (error);
40 	return (VOP_OPEN(ttyvp, flag, NOCRED, p));
41 }
42 
43 /*ARGSUSED*/
44 cttyread(dev, uio, flag)
45 	dev_t dev;
46 	struct uio *uio;
47 {
48 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
49 	int error;
50 
51 	if (ttyvp == NULL)
52 		return (EIO);
53 	VOP_LOCK(ttyvp);
54 	error = VOP_READ(ttyvp, uio, flag, NOCRED);
55 	VOP_UNLOCK(ttyvp);
56 	return (error);
57 }
58 
59 /*ARGSUSED*/
60 cttywrite(dev, uio, flag)
61 	dev_t dev;
62 	struct uio *uio;
63 {
64 	register struct vnode *ttyvp = cttyvp(uio->uio_procp);
65 	int error;
66 
67 	if (ttyvp == NULL)
68 		return (EIO);
69 	VOP_LOCK(ttyvp);
70 	error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
71 	VOP_UNLOCK(ttyvp);
72 	return (error);
73 }
74 
75 /*ARGSUSED*/
76 cttyioctl(dev, cmd, addr, flag, p)
77 	dev_t dev;
78 	int cmd;
79 	caddr_t addr;
80 	int flag;
81 	struct proc *p;
82 {
83 	struct vnode *ttyvp = cttyvp(p);
84 
85 	if (ttyvp == NULL)
86 		return (EIO);
87 	if (cmd == TIOCNOTTY) {
88 		if (!SESS_LEADER(p)) {
89 			p->p_flag &= ~SCTTY;
90 			return (0);
91 		} else
92 			return (EINVAL);
93 	}
94 	return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, p));
95 }
96 
97 /*ARGSUSED*/
98 cttyselect(dev, flag, p)
99 	dev_t dev;
100 	int flag;
101 	struct proc *p;
102 {
103 	struct vnode *ttyvp = cttyvp(p);
104 
105 	if (ttyvp == NULL)
106 		return (1);	/* try operation to get EOF/failure */
107 	return (VOP_SELECT(ttyvp, flag, FREAD|FWRITE, NOCRED, p));
108 }
109