No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / tty_ptm.c
blob01721065812bcda72d2ef6c484cef7a8bd7cdfb2
1 /* $NetBSD: tty_ptm.c,v 1.25 2008/04/28 20:24:05 martin Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.25 2008/04/28 20:24:05 martin Exp $");
32 #include "opt_ptm.h"
34 /* pty multiplexor driver /dev/ptm{,x} */
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/ioctl.h>
39 #include <sys/proc.h>
40 #include <sys/tty.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43 #include <sys/uio.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/namei.h>
47 #include <sys/signalvar.h>
48 #include <sys/filedesc.h>
49 #include <sys/conf.h>
50 #include <sys/poll.h>
51 #include <sys/pty.h>
52 #include <sys/kauth.h>
54 #include <miscfs/specfs/specdev.h>
56 #ifdef DEBUG_PTM
57 #define DPRINTF(a) printf a
58 #else
59 #define DPRINTF(a)
60 #endif
62 #ifdef NO_DEV_PTM
63 const struct cdevsw ptm_cdevsw = {
64 noopen, noclose, noread, nowrite, noioctl,
65 nostop, notty, nopoll, nommap, nokqfilter, D_TTY
67 #else
69 static struct ptm_pty *ptm;
70 int pts_major, ptc_major;
72 static dev_t pty_getfree(void);
73 static int pty_alloc_master(struct lwp *, int *, dev_t *);
74 static int pty_alloc_slave(struct lwp *, int *, dev_t);
76 void ptmattach(int);
78 dev_t
79 pty_makedev(char ms, int minor)
81 return makedev(ms == 't' ? pts_major : ptc_major, minor);
85 static dev_t
86 pty_getfree(void)
88 extern kmutex_t pt_softc_mutex;
89 int i;
91 mutex_enter(&pt_softc_mutex);
92 for (i = 0; i < npty; i++) {
93 if (pty_isfree(i, 0))
94 break;
96 mutex_exit(&pt_softc_mutex);
97 return pty_makedev('t', i);
101 * Hacked up version of vn_open. We _only_ handle ptys and only open
102 * them with FREAD|FWRITE and never deal with creat or stuff like that.
104 * We need it because we have to fake up root credentials to open the pty.
107 pty_vn_open(struct vnode *vp, struct lwp *l)
109 int error;
111 if (vp->v_type != VCHR) {
112 vput(vp);
113 return EINVAL;
116 error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred);
118 if (error) {
119 vput(vp);
120 return error;
123 vp->v_writecount++;
125 return 0;
128 static int
129 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev)
131 int error;
132 struct file *fp;
133 struct vnode *vp;
134 int md;
136 if ((error = fd_allocfile(&fp, fd)) != 0) {
137 DPRINTF(("fd_allocfile %d\n", error));
138 return error;
140 retry:
141 /* Find and open a free master pty. */
142 *dev = pty_getfree();
143 md = minor(*dev);
144 if ((error = pty_check(md)) != 0) {
145 DPRINTF(("pty_check %d\n", error));
146 goto bad;
148 if (ptm == NULL) {
149 DPRINTF(("no ptm\n"));
150 error = EOPNOTSUPP;
151 goto bad;
153 if ((error = (*ptm->allocvp)(ptm, l, &vp, *dev, 'p')) != 0) {
154 DPRINTF(("pty_allocvp %d\n", error));
155 goto bad;
158 if ((error = pty_vn_open(vp, l)) != 0) {
159 DPRINTF(("pty_vn_open %d\n", error));
161 * Check if the master open failed because we lost
162 * the race to grab it.
164 if (error != EIO)
165 goto bad;
166 error = !pty_isfree(md, 1);
167 DPRINTF(("pty_isfree %d\n", error));
168 if (error)
169 goto retry;
170 else
171 goto bad;
173 fp->f_flag = FREAD|FWRITE;
174 fp->f_type = DTYPE_VNODE;
175 fp->f_ops = &vnops;
176 fp->f_data = vp;
177 VOP_UNLOCK(vp, 0);
178 fd_affix(curproc, fp, *fd);
179 return 0;
180 bad:
181 fd_abort(curproc, fp, *fd);
182 return error;
186 pty_grant_slave(struct lwp *l, dev_t dev)
188 int error;
189 struct vnode *vp;
192 * Open the slave.
193 * namei -> setattr -> unlock -> revoke -> vrele ->
194 * namei -> open -> unlock
195 * Three stage rocket:
196 * 1. Change the owner and permissions on the slave.
197 * 2. Revoke all the users of the slave.
198 * 3. open the slave.
200 if (ptm == NULL)
201 return EOPNOTSUPP;
202 if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
203 return error;
205 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
206 struct vattr vattr;
207 (*ptm->getvattr)(ptm, l, &vattr);
208 /* Do the VOP_SETATTR() as root. */
209 error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
210 if (error) {
211 DPRINTF(("setattr %d\n", error));
212 VOP_UNLOCK(vp, 0);
213 vrele(vp);
214 return error;
217 VOP_UNLOCK(vp, 0);
218 VOP_REVOKE(vp, REVOKEALL);
221 * The vnode is useless after the revoke, we need to get it again.
223 vrele(vp);
224 return 0;
227 static int
228 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev)
230 int error;
231 struct file *fp;
232 struct vnode *vp;
234 /* Grab a filedescriptor for the slave */
235 if ((error = fd_allocfile(&fp, fd)) != 0) {
236 DPRINTF(("fd_allocfile %d\n", error));
237 return error;
240 if (ptm == NULL) {
241 error = EOPNOTSUPP;
242 goto bad;
245 if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
246 goto bad;
247 if ((error = pty_vn_open(vp, l)) != 0)
248 goto bad;
250 fp->f_flag = FREAD|FWRITE;
251 fp->f_type = DTYPE_VNODE;
252 fp->f_ops = &vnops;
253 fp->f_data = vp;
254 VOP_UNLOCK(vp, 0);
255 fd_affix(curproc, fp, *fd);
256 return 0;
257 bad:
258 fd_abort(curproc, fp, *fd);
259 return error;
262 struct ptm_pty *
263 pty_sethandler(struct ptm_pty *nptm)
265 struct ptm_pty *optm = ptm;
266 ptm = nptm;
267 return optm;
271 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data)
273 struct ptmget *ptmg = data;
274 int error;
276 if (ptm == NULL)
277 return EOPNOTSUPP;
279 ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
280 ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
282 error = (*ptm->makename)(ptm, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
283 if (error)
284 return error;
286 return (*ptm->makename)(ptm, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
289 void
290 /*ARGSUSED*/
291 ptmattach(int n)
293 extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
294 /* find the major and minor of the pty devices */
295 if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
296 panic("ptmattach: Can't find pty slave in cdevsw");
297 if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
298 panic("ptmattach: Can't find pty master in cdevsw");
299 #ifdef COMPAT_BSDPTY
300 ptm = &ptm_bsdpty;
301 #endif
304 static int
305 /*ARGSUSED*/
306 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
308 int error;
309 int fd;
310 dev_t ttydev;
312 switch(minor(dev)) {
313 case 0: /* /dev/ptmx */
314 case 2: /* /emul/linux/dev/ptmx */
315 if ((error = pty_alloc_master(l, &fd, &ttydev)) != 0)
316 return error;
317 if (minor(dev) == 2) {
319 * Linux ptyfs grants the pty right here.
320 * Handle this case here, instead of writing
321 * a new linux module.
323 if ((error = pty_grant_slave(l, ttydev)) != 0) {
324 file_t *fp = fd_getfile(fd);
325 if (fp != NULL) {
326 fd_close(fd);
328 return error;
331 curlwp->l_dupfd = fd;
332 return EMOVEFD;
333 case 1: /* /dev/ptm */
334 return 0;
335 default:
336 return ENODEV;
340 static int
341 /*ARGSUSED*/
342 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
345 return (0);
348 static int
349 /*ARGSUSED*/
350 ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
352 int error;
353 dev_t newdev;
354 int cfd, sfd;
355 file_t *fp;
357 error = 0;
358 switch (cmd) {
359 case TIOCPTMGET:
360 if ((error = pty_alloc_master(l, &cfd, &newdev)) != 0)
361 return error;
363 if ((error = pty_grant_slave(l, newdev)) != 0)
364 goto bad;
366 if ((error = pty_alloc_slave(l, &sfd, newdev)) != 0)
367 goto bad;
369 /* now, put the indices and names into struct ptmget */
370 return pty_fill_ptmget(l, newdev, cfd, sfd, data);
371 default:
372 DPRINTF(("ptmioctl EINVAL\n"));
373 return EINVAL;
375 bad:
376 fp = fd_getfile(cfd);
377 if (fp != NULL) {
378 fd_close(cfd);
380 return error;
383 const struct cdevsw ptm_cdevsw = {
384 ptmopen, ptmclose, noread, nowrite, ptmioctl,
385 nullstop, notty, nopoll, nommap, nokqfilter, D_TTY
387 #endif