proc->thread stage 4: rework the VFS and DEVICE subsystems to take thread
[dragonfly.git] / sys / kern / tty_tty.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tty_tty.c 8.2 (Berkeley) 9/23/93
34 * $FreeBSD: src/sys/kern/tty_tty.c,v 1.30 1999/09/25 18:24:24 phk Exp $
dadab5e9 35 * $DragonFly: src/sys/kern/tty_tty.c,v 1.4 2003/06/25 03:55:57 dillon Exp $
984263bc
MD
36 */
37
38/*
39 * Indirect driver for controlling tty.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/lock.h>
46#include <sys/proc.h>
47#include <sys/ttycom.h>
48#include <sys/vnode.h>
49#include <sys/kernel.h>
50
51static d_open_t cttyopen;
52static d_read_t cttyread;
53static d_write_t cttywrite;
54static d_ioctl_t cttyioctl;
55static d_poll_t cttypoll;
56
57#define CDEV_MAJOR 1
58/* Don't make this static, since fdesc_vnops uses it. */
59struct cdevsw ctty_cdevsw = {
60 /* open */ cttyopen,
61 /* close */ nullclose,
62 /* read */ cttyread,
63 /* write */ cttywrite,
64 /* ioctl */ cttyioctl,
65 /* poll */ cttypoll,
66 /* mmap */ nommap,
67 /* strategy */ nostrategy,
68 /* name */ "ctty",
69 /* maj */ CDEV_MAJOR,
70 /* dump */ nodump,
71 /* psize */ nopsize,
72 /* flags */ D_TTY,
73 /* bmaj */ -1
74};
75
76#define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
77
78/*ARGSUSED*/
79static int
dadab5e9 80cttyopen(dev_t dev, int flag, int mode, struct thread *td)
984263bc 81{
dadab5e9 82 struct proc *p = td->td_proc;
41c20dac 83 struct vnode *ttyvp;
984263bc
MD
84 int error;
85
dadab5e9
MD
86 KKASSERT(p);
87 ttyvp = cttyvp(p);
41c20dac 88
984263bc
MD
89 if (ttyvp == NULL)
90 return (ENXIO);
dadab5e9 91 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc
MD
92#ifdef PARANOID
93 /*
94 * Since group is tty and mode is 620 on most terminal lines
95 * and since sessions protect terminals from processes outside
96 * your session, this check is probably no longer necessary.
97 * Since it inhibits setuid root programs that later switch
98 * to another user from accessing /dev/tty, we have decided
99 * to delete this test. (mckusick 5/93)
100 */
101 error = VOP_ACCESS(ttyvp,
dadab5e9 102 (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, td);
984263bc
MD
103 if (!error)
104#endif /* PARANOID */
dadab5e9
MD
105 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
106 VOP_UNLOCK(ttyvp, 0, td);
984263bc
MD
107 return (error);
108}
109
110/*ARGSUSED*/
111static int
112cttyread(dev, uio, flag)
113 dev_t dev;
114 struct uio *uio;
115 int flag;
116{
dadab5e9
MD
117 struct thread *td = uio->uio_td;
118 struct proc *p = td->td_proc;
119 struct vnode *ttyvp;
984263bc
MD
120 int error;
121
dadab5e9
MD
122 KKASSERT(p);
123 ttyvp = cttyvp(p);
984263bc
MD
124 if (ttyvp == NULL)
125 return (EIO);
dadab5e9 126 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 127 error = VOP_READ(ttyvp, uio, flag, NOCRED);
dadab5e9 128 VOP_UNLOCK(ttyvp, 0, td);
984263bc
MD
129 return (error);
130}
131
132/*ARGSUSED*/
133static int
134cttywrite(dev, uio, flag)
135 dev_t dev;
136 struct uio *uio;
137 int flag;
138{
dadab5e9
MD
139 struct thread *td = uio->uio_td;
140 struct proc *p = td->td_proc;
141 struct vnode *ttyvp;
984263bc
MD
142 int error;
143
dadab5e9
MD
144 KKASSERT(p);
145 ttyvp = cttyvp(p);
984263bc
MD
146 if (ttyvp == NULL)
147 return (EIO);
dadab5e9 148 vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td);
984263bc 149 error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
dadab5e9 150 VOP_UNLOCK(ttyvp, 0, td);
984263bc
MD
151 return (error);
152}
153
154/*ARGSUSED*/
155static int
41c20dac 156cttyioctl(dev, cmd, addr, flag, td)
984263bc
MD
157 dev_t dev;
158 u_long cmd;
159 caddr_t addr;
160 int flag;
41c20dac 161 struct thread *td;
984263bc 162{
41c20dac 163 struct vnode *ttyvp;
dadab5e9 164 struct proc *p = td->td_proc;
984263bc 165
dadab5e9
MD
166 KKASSERT(p);
167 ttyvp = cttyvp(p);
984263bc
MD
168 if (ttyvp == NULL)
169 return (EIO);
170 if (cmd == TIOCSCTTY) /* don't allow controlling tty to be set */
171 return EINVAL; /* to controlling tty -- infinite recursion */
172 if (cmd == TIOCNOTTY) {
dadab5e9
MD
173 if (!SESS_LEADER(p)) {
174 p->p_flag &= ~P_CONTROLT;
984263bc
MD
175 return (0);
176 } else
177 return (EINVAL);
178 }
dadab5e9 179 return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
984263bc
MD
180}
181
182/*ARGSUSED*/
183static int
dadab5e9 184cttypoll(dev_t dev, int events, struct thread *td)
984263bc 185{
41c20dac 186 struct vnode *ttyvp;
dadab5e9 187 struct proc *p = td->td_proc;
984263bc 188
dadab5e9
MD
189 KKASSERT(p);
190 ttyvp = cttyvp(p);
984263bc
MD
191 if (ttyvp == NULL)
192 /* try operation to get EOF/failure */
41c20dac 193 return (seltrue(dev, events, td));
dadab5e9 194 return (VOP_POLL(ttyvp, events, p->p_ucred, td));
984263bc
MD
195}
196
197static void ctty_drvinit __P((void *unused));
198static void
199ctty_drvinit(unused)
200 void *unused;
201{
202
203 make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
204}
205
206SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)