Enhance the pmap_kenter*() API and friends, separating out entries which
[dragonfly.git] / sys / kern / tty_tty.c
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 $
35  * $DragonFly: src/sys/kern/tty_tty.c,v 1.7 2004/03/01 06:33:17 dillon Exp $
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
51 static  d_open_t        cttyopen;
52 static  d_read_t        cttyread;
53 static  d_write_t       cttywrite;
54 static  d_ioctl_t       cttyioctl;
55 static  d_poll_t        cttypoll;
56
57 #define CDEV_MAJOR      1
58 /* Don't make this static, since fdesc_vnops uses it. */
59 struct cdevsw ctty_cdevsw = {
60         /* name */      "ctty",
61         /* maj */       CDEV_MAJOR,
62         /* flags */     D_TTY,
63         /* port */      NULL,
64         /* autoq */     0,
65
66         /* open */      cttyopen,
67         /* close */     nullclose,
68         /* read */      cttyread,
69         /* write */     cttywrite,
70         /* ioctl */     cttyioctl,
71         /* poll */      cttypoll,
72         /* mmap */      nommap,
73         /* strategy */  nostrategy,
74         /* dump */      nodump,
75         /* psize */     nopsize
76 };
77
78 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
79
80 /*ARGSUSED*/
81 static  int
82 cttyopen(dev_t dev, int flag, int mode, struct thread *td)
83 {
84         struct proc *p = td->td_proc;
85         struct vnode *ttyvp;
86         int error;
87
88         KKASSERT(p);
89         ttyvp = cttyvp(p);
90
91         if (ttyvp == NULL)
92                 return (ENXIO);
93         vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
94 #ifdef PARANOID
95         /*
96          * Since group is tty and mode is 620 on most terminal lines
97          * and since sessions protect terminals from processes outside
98          * your session, this check is probably no longer necessary.
99          * Since it inhibits setuid root programs that later switch
100          * to another user from accessing /dev/tty, we have decided
101          * to delete this test. (mckusick 5/93)
102          */
103         error = VOP_ACCESS(ttyvp,
104           (flag&FREAD ? VREAD : 0) | (flag&FWRITE ? VWRITE : 0), p->p_ucred, td);
105         if (!error)
106 #endif /* PARANOID */
107                 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
108         VOP_UNLOCK(ttyvp, NULL, 0, td);
109         return (error);
110 }
111
112 /*ARGSUSED*/
113 static  int
114 cttyread(dev, uio, flag)
115         dev_t dev;
116         struct uio *uio;
117         int flag;
118 {
119         struct thread *td = uio->uio_td;
120         struct proc *p = td->td_proc;
121         struct vnode *ttyvp;
122         int error;
123
124         KKASSERT(p);
125         ttyvp = cttyvp(p);
126         if (ttyvp == NULL)
127                 return (EIO);
128         vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
129         error = VOP_READ(ttyvp, uio, flag, NOCRED);
130         VOP_UNLOCK(ttyvp, NULL, 0, td);
131         return (error);
132 }
133
134 /*ARGSUSED*/
135 static  int
136 cttywrite(dev, uio, flag)
137         dev_t dev;
138         struct uio *uio;
139         int flag;
140 {
141         struct thread *td = uio->uio_td;
142         struct proc *p = td->td_proc;
143         struct vnode *ttyvp;
144         int error;
145
146         KKASSERT(p);
147         ttyvp = cttyvp(p);
148         if (ttyvp == NULL)
149                 return (EIO);
150         vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
151         error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
152         VOP_UNLOCK(ttyvp, NULL, 0, td);
153         return (error);
154 }
155
156 /*ARGSUSED*/
157 static  int
158 cttyioctl(dev, cmd, addr, flag, td)
159         dev_t dev;
160         u_long cmd;
161         caddr_t addr;
162         int flag;
163         struct thread *td;
164 {
165         struct vnode *ttyvp;
166         struct proc *p = td->td_proc;
167
168         KKASSERT(p);
169         ttyvp = cttyvp(p);
170         if (ttyvp == NULL)
171                 return (EIO);
172         if (cmd == TIOCSCTTY)  /* don't allow controlling tty to be set    */
173                 return EINVAL; /* to controlling tty -- infinite recursion */
174         if (cmd == TIOCNOTTY) {
175                 if (!SESS_LEADER(p)) {
176                         p->p_flag &= ~P_CONTROLT;
177                         return (0);
178                 } else
179                         return (EINVAL);
180         }
181         return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
182 }
183
184 /*ARGSUSED*/
185 static  int
186 cttypoll(dev_t dev, int events, struct thread *td)
187 {
188         struct vnode *ttyvp;
189         struct proc *p = td->td_proc;
190
191         KKASSERT(p);
192         ttyvp = cttyvp(p);
193         if (ttyvp == NULL)
194                 /* try operation to get EOF/failure */
195                 return (seltrue(dev, events, td));
196         return (VOP_POLL(ttyvp, events, p->p_ucred, td));
197 }
198
199 static void ctty_drvinit (void *unused);
200 static void
201 ctty_drvinit(unused)
202         void *unused;
203 {
204
205         make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
206 }
207
208 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)