tty_ptm.c revision 1.16 1 1.16 alc /* $NetBSD: tty_ptm.c,v 1.16 2006/12/27 18:45:30 alc Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. All advertising materials mentioning features or use of this software
16 1.1 christos * must display the following acknowledgement:
17 1.1 christos * This product includes software developed by the NetBSD
18 1.1 christos * Foundation, Inc. and its contributors.
19 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.1 christos * contributors may be used to endorse or promote products derived
21 1.1 christos * from this software without specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos #include <sys/cdefs.h>
37 1.16 alc __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.16 2006/12/27 18:45:30 alc Exp $");
38 1.1 christos
39 1.1 christos #include "opt_ptm.h"
40 1.1 christos
41 1.1 christos /* pty multiplexor driver /dev/ptm{,x} */
42 1.1 christos
43 1.1 christos #include <sys/param.h>
44 1.1 christos #include <sys/systm.h>
45 1.1 christos #include <sys/ioctl.h>
46 1.1 christos #include <sys/proc.h>
47 1.1 christos #include <sys/tty.h>
48 1.1 christos #include <sys/stat.h>
49 1.1 christos #include <sys/file.h>
50 1.1 christos #include <sys/uio.h>
51 1.1 christos #include <sys/kernel.h>
52 1.1 christos #include <sys/vnode.h>
53 1.1 christos #include <sys/namei.h>
54 1.1 christos #include <sys/signalvar.h>
55 1.1 christos #include <sys/uio.h>
56 1.1 christos #include <sys/filedesc.h>
57 1.1 christos #include <sys/conf.h>
58 1.1 christos #include <sys/poll.h>
59 1.1 christos #include <sys/malloc.h>
60 1.1 christos #include <sys/pty.h>
61 1.9 elad #include <sys/kauth.h>
62 1.1 christos
63 1.1 christos #ifdef DEBUG_PTM
64 1.1 christos #define DPRINTF(a) printf a
65 1.1 christos #else
66 1.1 christos #define DPRINTF(a)
67 1.1 christos #endif
68 1.1 christos
69 1.1 christos #ifdef NO_DEV_PTM
70 1.1 christos const struct cdevsw ptm_cdevsw = {
71 1.1 christos noopen, noclose, noread, nowrite, noioctl,
72 1.1 christos nostop, notty, nopoll, nommap, nokqfilter, D_TTY
73 1.1 christos };
74 1.1 christos #else
75 1.1 christos
76 1.1 christos static struct ptm_pty *ptm;
77 1.1 christos int pts_major, ptc_major;
78 1.1 christos
79 1.1 christos static dev_t pty_getfree(void);
80 1.7 christos static int pty_alloc_master(struct lwp *, int *, dev_t *);
81 1.7 christos static int pty_alloc_slave(struct lwp *, int *, dev_t);
82 1.1 christos
83 1.1 christos void ptmattach(int);
84 1.1 christos
85 1.1 christos dev_t
86 1.1 christos pty_makedev(char ms, int minor)
87 1.1 christos {
88 1.1 christos return makedev(ms == 't' ? pts_major : ptc_major, minor);
89 1.1 christos }
90 1.1 christos
91 1.5 thorpej
92 1.1 christos static dev_t
93 1.1 christos pty_getfree(void)
94 1.1 christos {
95 1.1 christos extern struct simplelock pt_softc_mutex;
96 1.1 christos int i;
97 1.1 christos
98 1.1 christos simple_lock(&pt_softc_mutex);
99 1.1 christos for (i = 0; i < npty; i++) {
100 1.1 christos if (pty_isfree(i, 0))
101 1.1 christos break;
102 1.1 christos }
103 1.1 christos simple_unlock(&pt_softc_mutex);
104 1.1 christos return pty_makedev('t', i);
105 1.1 christos }
106 1.1 christos
107 1.1 christos /*
108 1.1 christos * Hacked up version of vn_open. We _only_ handle ptys and only open
109 1.1 christos * them with FREAD|FWRITE and never deal with creat or stuff like that.
110 1.1 christos *
111 1.1 christos * We need it because we have to fake up root credentials to open the pty.
112 1.1 christos */
113 1.1 christos int
114 1.7 christos pty_vn_open(struct vnode *vp, struct lwp *l)
115 1.1 christos {
116 1.1 christos int error;
117 1.1 christos
118 1.1 christos if (vp->v_type != VCHR) {
119 1.1 christos vput(vp);
120 1.1 christos return EINVAL;
121 1.1 christos }
122 1.1 christos
123 1.11 ad error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred, l);
124 1.1 christos
125 1.1 christos if (error) {
126 1.1 christos vput(vp);
127 1.1 christos return error;
128 1.1 christos }
129 1.1 christos
130 1.1 christos vp->v_writecount++;
131 1.1 christos
132 1.1 christos return 0;
133 1.1 christos }
134 1.1 christos
135 1.1 christos static int
136 1.7 christos pty_alloc_master(struct lwp *l, int *fd, dev_t *dev)
137 1.1 christos {
138 1.1 christos int error;
139 1.1 christos struct file *fp;
140 1.1 christos struct vnode *vp;
141 1.1 christos int md;
142 1.1 christos
143 1.11 ad if ((error = falloc(l, &fp, fd)) != 0) {
144 1.1 christos DPRINTF(("falloc %d\n", error));
145 1.1 christos return error;
146 1.1 christos }
147 1.1 christos retry:
148 1.1 christos /* Find and open a free master pty. */
149 1.1 christos *dev = pty_getfree();
150 1.1 christos md = minor(*dev);
151 1.1 christos if ((error = pty_check(md)) != 0) {
152 1.1 christos DPRINTF(("pty_check %d\n", error));
153 1.1 christos goto bad;
154 1.1 christos }
155 1.1 christos if (ptm == NULL) {
156 1.13 christos DPRINTF(("no ptm\n"));
157 1.1 christos error = EOPNOTSUPP;
158 1.1 christos goto bad;
159 1.1 christos }
160 1.13 christos if ((error = (*ptm->allocvp)(ptm, l, &vp, *dev, 'p')) != 0) {
161 1.13 christos DPRINTF(("pty_allocvp %d\n", error));
162 1.1 christos goto bad;
163 1.13 christos }
164 1.1 christos
165 1.7 christos if ((error = pty_vn_open(vp, l)) != 0) {
166 1.13 christos DPRINTF(("pty_vn_open %d\n", error));
167 1.1 christos /*
168 1.1 christos * Check if the master open failed because we lost
169 1.1 christos * the race to grab it.
170 1.1 christos */
171 1.1 christos if (error != EIO)
172 1.1 christos goto bad;
173 1.1 christos error = !pty_isfree(md, 1);
174 1.13 christos DPRINTF(("pty_isfree %d\n", error));
175 1.1 christos if (error)
176 1.1 christos goto retry;
177 1.1 christos else
178 1.1 christos goto bad;
179 1.1 christos }
180 1.1 christos fp->f_flag = FREAD|FWRITE;
181 1.1 christos fp->f_type = DTYPE_VNODE;
182 1.1 christos fp->f_ops = &vnops;
183 1.1 christos fp->f_data = vp;
184 1.1 christos VOP_UNLOCK(vp, 0);
185 1.1 christos FILE_SET_MATURE(fp);
186 1.7 christos FILE_UNUSE(fp, l);
187 1.1 christos return 0;
188 1.1 christos bad:
189 1.7 christos FILE_UNUSE(fp, l);
190 1.11 ad fdremove(l->l_proc->p_fd, *fd);
191 1.1 christos ffree(fp);
192 1.1 christos return error;
193 1.1 christos }
194 1.1 christos
195 1.1 christos int
196 1.7 christos pty_grant_slave(struct lwp *l, dev_t dev)
197 1.1 christos {
198 1.1 christos int error;
199 1.1 christos struct vnode *vp;
200 1.1 christos
201 1.1 christos /*
202 1.1 christos * Open the slave.
203 1.1 christos * namei -> setattr -> unlock -> revoke -> vrele ->
204 1.1 christos * namei -> open -> unlock
205 1.1 christos * Three stage rocket:
206 1.1 christos * 1. Change the owner and permissions on the slave.
207 1.1 christos * 2. Revoke all the users of the slave.
208 1.1 christos * 3. open the slave.
209 1.1 christos */
210 1.1 christos if (ptm == NULL)
211 1.1 christos return EOPNOTSUPP;
212 1.7 christos if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
213 1.1 christos return error;
214 1.1 christos
215 1.1 christos if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
216 1.1 christos struct vattr vattr;
217 1.11 ad (*ptm->getvattr)(ptm, l, &vattr);
218 1.10 ad /* Do the VOP_SETATTR() as root. */
219 1.11 ad error = VOP_SETATTR(vp, &vattr, lwp0.l_cred, l);
220 1.1 christos if (error) {
221 1.1 christos DPRINTF(("setattr %d\n", error));
222 1.1 christos VOP_UNLOCK(vp, 0);
223 1.1 christos vrele(vp);
224 1.1 christos return error;
225 1.1 christos }
226 1.1 christos }
227 1.1 christos VOP_UNLOCK(vp, 0);
228 1.1 christos if (vp->v_usecount > 1 ||
229 1.1 christos (vp->v_flag & (VALIASED | VLAYER)))
230 1.1 christos VOP_REVOKE(vp, REVOKEALL);
231 1.1 christos
232 1.1 christos /*
233 1.1 christos * The vnode is useless after the revoke, we need to get it again.
234 1.1 christos */
235 1.1 christos vrele(vp);
236 1.1 christos return 0;
237 1.1 christos }
238 1.1 christos
239 1.1 christos static int
240 1.7 christos pty_alloc_slave(struct lwp *l, int *fd, dev_t dev)
241 1.1 christos {
242 1.1 christos int error;
243 1.1 christos struct file *fp;
244 1.1 christos struct vnode *vp;
245 1.1 christos
246 1.1 christos /* Grab a filedescriptor for the slave */
247 1.11 ad if ((error = falloc(l, &fp, fd)) != 0) {
248 1.1 christos DPRINTF(("falloc %d\n", error));
249 1.1 christos return error;
250 1.1 christos }
251 1.1 christos
252 1.1 christos if (ptm == NULL) {
253 1.1 christos error = EOPNOTSUPP;
254 1.1 christos goto bad;
255 1.1 christos }
256 1.1 christos
257 1.7 christos if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
258 1.1 christos goto bad;
259 1.7 christos if ((error = pty_vn_open(vp, l)) != 0)
260 1.1 christos goto bad;
261 1.1 christos
262 1.1 christos fp->f_flag = FREAD|FWRITE;
263 1.1 christos fp->f_type = DTYPE_VNODE;
264 1.1 christos fp->f_ops = &vnops;
265 1.1 christos fp->f_data = vp;
266 1.1 christos VOP_UNLOCK(vp, 0);
267 1.1 christos FILE_SET_MATURE(fp);
268 1.7 christos FILE_UNUSE(fp, l);
269 1.1 christos return 0;
270 1.1 christos bad:
271 1.7 christos FILE_UNUSE(fp, l);
272 1.11 ad fdremove(l->l_proc->p_fd, *fd);
273 1.1 christos ffree(fp);
274 1.1 christos return error;
275 1.1 christos }
276 1.1 christos
277 1.1 christos struct ptm_pty *
278 1.1 christos pty_sethandler(struct ptm_pty *nptm)
279 1.1 christos {
280 1.1 christos struct ptm_pty *optm = ptm;
281 1.1 christos ptm = nptm;
282 1.1 christos return optm;
283 1.1 christos }
284 1.1 christos
285 1.1 christos int
286 1.8 christos pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data)
287 1.1 christos {
288 1.1 christos struct ptmget *ptmg = data;
289 1.1 christos int error;
290 1.1 christos
291 1.1 christos if (ptm == NULL)
292 1.1 christos return EOPNOTSUPP;
293 1.1 christos
294 1.2 christos ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
295 1.2 christos ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
296 1.1 christos
297 1.8 christos error = (*ptm->makename)(ptm, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
298 1.1 christos if (error)
299 1.1 christos return error;
300 1.1 christos
301 1.8 christos return (*ptm->makename)(ptm, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
302 1.1 christos }
303 1.1 christos
304 1.1 christos void
305 1.1 christos /*ARGSUSED*/
306 1.15 yamt ptmattach(int n)
307 1.1 christos {
308 1.1 christos extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
309 1.1 christos /* find the major and minor of the pty devices */
310 1.1 christos if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
311 1.1 christos panic("ptmattach: Can't find pty slave in cdevsw");
312 1.1 christos if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
313 1.1 christos panic("ptmattach: Can't find pty master in cdevsw");
314 1.1 christos #ifdef COMPAT_BSDPTY
315 1.1 christos ptm = &ptm_bsdpty;
316 1.1 christos #endif
317 1.1 christos }
318 1.1 christos
319 1.5 thorpej static int
320 1.1 christos /*ARGSUSED*/
321 1.15 yamt ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
322 1.1 christos {
323 1.1 christos int error;
324 1.1 christos int fd;
325 1.13 christos dev_t ttydev;
326 1.1 christos
327 1.1 christos switch(minor(dev)) {
328 1.1 christos case 0: /* /dev/ptmx */
329 1.12 christos case 2: /* /emul/linux/dev/ptmx */
330 1.13 christos if ((error = pty_alloc_master(l, &fd, &ttydev)) != 0)
331 1.1 christos return error;
332 1.12 christos if (minor(dev) == 2) {
333 1.12 christos /*
334 1.12 christos * Linux ptyfs grants the pty right here.
335 1.12 christos * Handle this case here, instead of writing
336 1.12 christos * a new linux module.
337 1.12 christos */
338 1.13 christos if ((error = pty_grant_slave(l, ttydev)) != 0) {
339 1.12 christos struct file *fp =
340 1.12 christos fd_getfile(l->l_proc->p_fd, fd);
341 1.16 alc if (fp != NULL) {
342 1.16 alc FILE_UNUSE(fp, l);
343 1.16 alc fdremove(l->l_proc->p_fd, fd);
344 1.16 alc ffree(fp);
345 1.16 alc }
346 1.12 christos return error;
347 1.12 christos }
348 1.12 christos }
349 1.1 christos curlwp->l_dupfd = fd;
350 1.4 christos return EMOVEFD;
351 1.1 christos case 1: /* /dev/ptm */
352 1.1 christos return 0;
353 1.1 christos default:
354 1.1 christos return ENODEV;
355 1.1 christos }
356 1.1 christos }
357 1.1 christos
358 1.5 thorpej static int
359 1.1 christos /*ARGSUSED*/
360 1.15 yamt ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
361 1.1 christos {
362 1.15 yamt
363 1.1 christos return (0);
364 1.1 christos }
365 1.1 christos
366 1.5 thorpej static int
367 1.1 christos /*ARGSUSED*/
368 1.15 yamt ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
369 1.1 christos {
370 1.1 christos int error;
371 1.1 christos dev_t newdev;
372 1.1 christos int cfd, sfd;
373 1.1 christos struct file *fp;
374 1.7 christos struct proc *p = l->l_proc;
375 1.1 christos
376 1.1 christos error = 0;
377 1.1 christos switch (cmd) {
378 1.1 christos case TIOCPTMGET:
379 1.7 christos if ((error = pty_alloc_master(l, &cfd, &newdev)) != 0)
380 1.1 christos return error;
381 1.1 christos
382 1.7 christos if ((error = pty_grant_slave(l, newdev)) != 0)
383 1.1 christos goto bad;
384 1.1 christos
385 1.7 christos if ((error = pty_alloc_slave(l, &sfd, newdev)) != 0)
386 1.1 christos goto bad;
387 1.1 christos
388 1.1 christos /* now, put the indices and names into struct ptmget */
389 1.8 christos return pty_fill_ptmget(l, newdev, cfd, sfd, data);
390 1.1 christos default:
391 1.1 christos DPRINTF(("ptmioctl EINVAL\n"));
392 1.1 christos return EINVAL;
393 1.1 christos }
394 1.1 christos bad:
395 1.1 christos fp = fd_getfile(p->p_fd, cfd);
396 1.16 alc if (fp != NULL) {
397 1.16 alc FILE_UNUSE(fp, l);
398 1.16 alc fdremove(p->p_fd, cfd);
399 1.16 alc ffree(fp);
400 1.16 alc }
401 1.1 christos return error;
402 1.1 christos }
403 1.5 thorpej
404 1.5 thorpej const struct cdevsw ptm_cdevsw = {
405 1.5 thorpej ptmopen, ptmclose, noread, nowrite, ptmioctl,
406 1.5 thorpej nullstop, notty, nopoll, nommap, nokqfilter, D_TTY
407 1.5 thorpej };
408 1.6 he #endif
409