proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[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.3 2003/06/23 17:55:41 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         /* 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*/
79 static  int
80 cttyopen(dev, flag, mode, td)
81         dev_t dev;
82         int flag, mode;
83         struct thread *td;
84 {
85         struct vnode *ttyvp;
86         int error;
87
88         KKASSERT(td->td_proc != NULL);
89         ttyvp = cttyvp(td->td_proc);
90
91         if (ttyvp == NULL)
92                 return (ENXIO);
93         vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, td->td_proc);
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, p);
105         if (!error)
106 #endif /* PARANOID */
107                 error = VOP_OPEN(ttyvp, flag, NOCRED, td->td_proc);
108         VOP_UNLOCK(ttyvp, 0, td->td_proc);
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 proc *p = uio->uio_procp;
120         register struct vnode *ttyvp = cttyvp(p);
121         int error;
122
123         if (ttyvp == NULL)
124                 return (EIO);
125         vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
126         error = VOP_READ(ttyvp, uio, flag, NOCRED);
127         VOP_UNLOCK(ttyvp, 0, p);
128         return (error);
129 }
130
131 /*ARGSUSED*/
132 static  int
133 cttywrite(dev, uio, flag)
134         dev_t dev;
135         struct uio *uio;
136         int flag;
137 {
138         struct proc *p = uio->uio_procp;
139         struct vnode *ttyvp = cttyvp(uio->uio_procp);
140         int error;
141
142         if (ttyvp == NULL)
143                 return (EIO);
144         vn_lock(ttyvp, LK_EXCLUSIVE | LK_RETRY, p);
145         error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
146         VOP_UNLOCK(ttyvp, 0, p);
147         return (error);
148 }
149
150 /*ARGSUSED*/
151 static  int
152 cttyioctl(dev, cmd, addr, flag, td)
153         dev_t dev;
154         u_long cmd;
155         caddr_t addr;
156         int flag;
157         struct thread *td;
158 {
159         struct vnode *ttyvp;
160
161         KKASSERT(td->td_proc != NULL);
162         ttyvp = cttyvp(td->td_proc);
163         if (ttyvp == NULL)
164                 return (EIO);
165         if (cmd == TIOCSCTTY)  /* don't allow controlling tty to be set    */
166                 return EINVAL; /* to controlling tty -- infinite recursion */
167         if (cmd == TIOCNOTTY) {
168                 if (!SESS_LEADER(td->td_proc)) {
169                         td->td_proc->p_flag &= ~P_CONTROLT;
170                         return (0);
171                 } else
172                         return (EINVAL);
173         }
174         return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td->td_proc));
175 }
176
177 /*ARGSUSED*/
178 static  int
179 cttypoll(dev, events, td)
180         dev_t dev;
181         int events;
182         struct thread *td;
183 {
184         struct vnode *ttyvp;
185
186         KKASSERT(td->td_proc != NULL);
187         ttyvp = cttyvp(td->td_proc);
188         if (ttyvp == NULL)
189                 /* try operation to get EOF/failure */
190                 return (seltrue(dev, events, td));
191         return (VOP_POLL(ttyvp, events, td->td_proc->p_ucred, td->td_proc));
192 }
193
194 static void ctty_drvinit __P((void *unused));
195 static void
196 ctty_drvinit(unused)
197         void *unused;
198 {
199
200         make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
201 }
202
203 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)