async syscall work: The async syscall code got dated by recent LWKT
[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.9 2004/05/19 22:52:58 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_close_t       cttyclose;
53 static  d_read_t        cttyread;
54 static  d_write_t       cttywrite;
55 static  d_ioctl_t       cttyioctl;
56 static  d_poll_t        cttypoll;
57
58 #define CDEV_MAJOR      1
59 /* Don't make this static, since fdesc_vnops uses it. */
60 struct cdevsw ctty_cdevsw = {
61         /* name */      "ctty",
62         /* maj */       CDEV_MAJOR,
63         /* flags */     D_TTY,
64         /* port */      NULL,
65         /* clone */     NULL,
66
67         /* open */      cttyopen,
68         /* close */     cttyclose,
69         /* read */      cttyread,
70         /* write */     cttywrite,
71         /* ioctl */     cttyioctl,
72         /* poll */      cttypoll,
73         /* mmap */      nommap,
74         /* strategy */  nostrategy,
75         /* dump */      nodump,
76         /* psize */     nopsize
77 };
78
79 #define cttyvp(p) ((p)->p_flag & P_CONTROLT ? (p)->p_session->s_ttyvp : NULL)
80
81 /*ARGSUSED*/
82 static  int
83 cttyopen(dev_t dev, int flag, int mode, struct thread *td)
84 {
85         struct proc *p = td->td_proc;
86         struct vnode *ttyvp;
87         int error;
88
89         KKASSERT(p);
90         ttyvp = cttyvp(p);
91         if (ttyvp) {
92                 vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
93                 error = VOP_OPEN(ttyvp, flag, NOCRED, td);
94                 VOP_UNLOCK(ttyvp, NULL, 0, td);
95         } else {
96                 error = ENXIO;
97         }
98         return (error);
99 }
100
101 static int
102 cttyclose(dev_t dev, int fflag, int devtype, struct thread *td)
103 {
104         struct proc *p = td->td_proc;
105         struct vnode *ttyvp;
106         int error;
107
108         KKASSERT(p);
109         ttyvp = cttyvp(p);
110         if (ttyvp == NULL)
111                 error = EIO;
112         else
113                 error = VOP_CLOSE(ttyvp, fflag, td);
114         return(error);
115 }
116
117 /*ARGSUSED*/
118 static  int
119 cttyread(dev, uio, flag)
120         dev_t dev;
121         struct uio *uio;
122         int flag;
123 {
124         struct thread *td = uio->uio_td;
125         struct proc *p = td->td_proc;
126         struct vnode *ttyvp;
127         int error;
128
129         KKASSERT(p);
130         ttyvp = cttyvp(p);
131         if (ttyvp == NULL)
132                 return (EIO);
133         vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
134         error = VOP_READ(ttyvp, uio, flag, NOCRED);
135         VOP_UNLOCK(ttyvp, NULL, 0, td);
136         return (error);
137 }
138
139 /*ARGSUSED*/
140 static  int
141 cttywrite(dev, uio, flag)
142         dev_t dev;
143         struct uio *uio;
144         int flag;
145 {
146         struct thread *td = uio->uio_td;
147         struct proc *p = td->td_proc;
148         struct vnode *ttyvp;
149         int error;
150
151         KKASSERT(p);
152         ttyvp = cttyvp(p);
153         if (ttyvp == NULL)
154                 return (EIO);
155         vn_lock(ttyvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
156         error = VOP_WRITE(ttyvp, uio, flag, NOCRED);
157         VOP_UNLOCK(ttyvp, NULL, 0, td);
158         return (error);
159 }
160
161 /*ARGSUSED*/
162 static  int
163 cttyioctl(dev, cmd, addr, flag, td)
164         dev_t dev;
165         u_long cmd;
166         caddr_t addr;
167         int flag;
168         struct thread *td;
169 {
170         struct vnode *ttyvp;
171         struct proc *p = td->td_proc;
172
173         KKASSERT(p);
174         ttyvp = cttyvp(p);
175         if (ttyvp == NULL)
176                 return (EIO);
177         if (cmd == TIOCSCTTY)  /* don't allow controlling tty to be set    */
178                 return EINVAL; /* to controlling tty -- infinite recursion */
179         if (cmd == TIOCNOTTY) {
180                 if (!SESS_LEADER(p)) {
181                         p->p_flag &= ~P_CONTROLT;
182                         return (0);
183                 } else
184                         return (EINVAL);
185         }
186         return (VOP_IOCTL(ttyvp, cmd, addr, flag, NOCRED, td));
187 }
188
189 /*ARGSUSED*/
190 static  int
191 cttypoll(dev_t dev, int events, struct thread *td)
192 {
193         struct vnode *ttyvp;
194         struct proc *p = td->td_proc;
195
196         KKASSERT(p);
197         ttyvp = cttyvp(p);
198         if (ttyvp == NULL)
199                 /* try operation to get EOF/failure */
200                 return (seltrue(dev, events, td));
201         return (VOP_POLL(ttyvp, events, p->p_ucred, td));
202 }
203
204 static void ctty_drvinit (void *unused);
205 static void
206 ctty_drvinit(unused)
207         void *unused;
208 {
209         cdevsw_add(&ctty_cdevsw, 0, 0);
210         make_dev(&ctty_cdevsw, 0, 0, 0, 0666, "tty");
211 }
212
213 SYSINIT(cttydev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,ctty_drvinit,NULL)