tty_ptm.c revision 1.28.2.1 1 1.28.2.1 rmind /* $NetBSD: tty_ptm.c,v 1.28.2.1 2014/05/18 17:46:08 rmind 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 *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.1 christos */
28 1.1 christos
29 1.1 christos #include <sys/cdefs.h>
30 1.28.2.1 rmind __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.28.2.1 2014/05/18 17:46:08 rmind Exp $");
31 1.1 christos
32 1.28 apb #include "opt_compat_netbsd.h"
33 1.1 christos #include "opt_ptm.h"
34 1.1 christos
35 1.1 christos /* pty multiplexor driver /dev/ptm{,x} */
36 1.1 christos
37 1.1 christos #include <sys/param.h>
38 1.1 christos #include <sys/systm.h>
39 1.1 christos #include <sys/ioctl.h>
40 1.1 christos #include <sys/proc.h>
41 1.1 christos #include <sys/tty.h>
42 1.1 christos #include <sys/stat.h>
43 1.1 christos #include <sys/file.h>
44 1.1 christos #include <sys/uio.h>
45 1.1 christos #include <sys/kernel.h>
46 1.1 christos #include <sys/vnode.h>
47 1.1 christos #include <sys/namei.h>
48 1.1 christos #include <sys/signalvar.h>
49 1.1 christos #include <sys/filedesc.h>
50 1.1 christos #include <sys/conf.h>
51 1.1 christos #include <sys/poll.h>
52 1.1 christos #include <sys/pty.h>
53 1.9 elad #include <sys/kauth.h>
54 1.1 christos
55 1.23 ad #include <miscfs/specfs/specdev.h>
56 1.23 ad
57 1.28 apb #ifdef COMPAT_60
58 1.28 apb #include <compat/sys/ttycom.h>
59 1.28 apb #endif /* COMPAT_60 */
60 1.28 apb
61 1.1 christos #ifdef DEBUG_PTM
62 1.1 christos #define DPRINTF(a) printf a
63 1.1 christos #else
64 1.1 christos #define DPRINTF(a)
65 1.1 christos #endif
66 1.1 christos
67 1.1 christos #ifdef NO_DEV_PTM
68 1.1 christos const struct cdevsw ptm_cdevsw = {
69 1.28.2.1 rmind .d_open = noopen,
70 1.28.2.1 rmind .d_close = noclose,
71 1.28.2.1 rmind .d_read = noread,
72 1.28.2.1 rmind .d_write = nowrite,
73 1.28.2.1 rmind .d_ioctl = noioctl,
74 1.28.2.1 rmind .d_stop = nostop,
75 1.28.2.1 rmind .d_tty = notty,
76 1.28.2.1 rmind .d_poll = nopoll,
77 1.28.2.1 rmind .d_mmap = nommap,
78 1.28.2.1 rmind .d_kqfilter = nokqfilter,
79 1.28.2.1 rmind .d_flag = D_TTY
80 1.1 christos };
81 1.1 christos #else
82 1.1 christos
83 1.1 christos static struct ptm_pty *ptm;
84 1.1 christos int pts_major, ptc_major;
85 1.1 christos
86 1.1 christos static dev_t pty_getfree(void);
87 1.28.2.1 rmind static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *);
88 1.28.2.1 rmind static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
89 1.1 christos
90 1.1 christos void ptmattach(int);
91 1.1 christos
92 1.28.2.1 rmind int
93 1.28.2.1 rmind pty_getmp(struct lwp *l, struct mount **mpp)
94 1.28.2.1 rmind {
95 1.28.2.1 rmind if (ptm == NULL)
96 1.28.2.1 rmind return EOPNOTSUPP;
97 1.28.2.1 rmind
98 1.28.2.1 rmind return (*ptm->getmp)(l, mpp);
99 1.28.2.1 rmind }
100 1.28.2.1 rmind
101 1.1 christos dev_t
102 1.1 christos pty_makedev(char ms, int minor)
103 1.1 christos {
104 1.1 christos return makedev(ms == 't' ? pts_major : ptc_major, minor);
105 1.1 christos }
106 1.1 christos
107 1.5 thorpej
108 1.1 christos static dev_t
109 1.1 christos pty_getfree(void)
110 1.1 christos {
111 1.18 ad extern kmutex_t pt_softc_mutex;
112 1.1 christos int i;
113 1.1 christos
114 1.18 ad mutex_enter(&pt_softc_mutex);
115 1.1 christos for (i = 0; i < npty; i++) {
116 1.1 christos if (pty_isfree(i, 0))
117 1.1 christos break;
118 1.1 christos }
119 1.18 ad mutex_exit(&pt_softc_mutex);
120 1.1 christos return pty_makedev('t', i);
121 1.1 christos }
122 1.1 christos
123 1.1 christos /*
124 1.1 christos * Hacked up version of vn_open. We _only_ handle ptys and only open
125 1.1 christos * them with FREAD|FWRITE and never deal with creat or stuff like that.
126 1.1 christos *
127 1.1 christos * We need it because we have to fake up root credentials to open the pty.
128 1.1 christos */
129 1.1 christos int
130 1.7 christos pty_vn_open(struct vnode *vp, struct lwp *l)
131 1.1 christos {
132 1.1 christos int error;
133 1.1 christos
134 1.1 christos if (vp->v_type != VCHR) {
135 1.1 christos vput(vp);
136 1.1 christos return EINVAL;
137 1.1 christos }
138 1.1 christos
139 1.21 pooka error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred);
140 1.1 christos
141 1.1 christos if (error) {
142 1.1 christos vput(vp);
143 1.1 christos return error;
144 1.1 christos }
145 1.1 christos
146 1.1 christos vp->v_writecount++;
147 1.1 christos
148 1.1 christos return 0;
149 1.1 christos }
150 1.1 christos
151 1.1 christos static int
152 1.28.2.1 rmind pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp)
153 1.1 christos {
154 1.1 christos int error;
155 1.1 christos struct file *fp;
156 1.1 christos struct vnode *vp;
157 1.1 christos int md;
158 1.1 christos
159 1.24 ad if ((error = fd_allocfile(&fp, fd)) != 0) {
160 1.24 ad DPRINTF(("fd_allocfile %d\n", error));
161 1.1 christos return error;
162 1.1 christos }
163 1.1 christos retry:
164 1.1 christos /* Find and open a free master pty. */
165 1.1 christos *dev = pty_getfree();
166 1.1 christos md = minor(*dev);
167 1.1 christos if ((error = pty_check(md)) != 0) {
168 1.1 christos DPRINTF(("pty_check %d\n", error));
169 1.1 christos goto bad;
170 1.1 christos }
171 1.1 christos if (ptm == NULL) {
172 1.13 christos DPRINTF(("no ptm\n"));
173 1.1 christos error = EOPNOTSUPP;
174 1.1 christos goto bad;
175 1.1 christos }
176 1.28.2.1 rmind /*
177 1.28.2.1 rmind * XXX Since PTYFS has now multiple instance support, if we mounted
178 1.28.2.1 rmind * more than one PTYFS we must check here the ptyfs_used_tbl, to find
179 1.28.2.1 rmind * out if the ptyfsnode is under the appropriate mount and skip the
180 1.28.2.1 rmind * node if not, because the pty could has been released, but
181 1.28.2.1 rmind * ptyfs_reclaim didn't get a chance to release the corresponding
182 1.28.2.1 rmind * node other mount point yet.
183 1.28.2.1 rmind *
184 1.28.2.1 rmind * It's important to have only one mount point's ptyfsnode for each
185 1.28.2.1 rmind * appropriate device in ptyfs_used_tbl, else we will have a security
186 1.28.2.1 rmind * problem, because every entry will have access to this device.
187 1.28.2.1 rmind *
188 1.28.2.1 rmind * Also we will not have not efficient vnode and memory usage.
189 1.28.2.1 rmind * You can test this by changing a_recycle from true to false
190 1.28.2.1 rmind * in ptyfs_inactive.
191 1.28.2.1 rmind */
192 1.28.2.1 rmind if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) {
193 1.13 christos DPRINTF(("pty_allocvp %d\n", error));
194 1.1 christos goto bad;
195 1.13 christos }
196 1.1 christos
197 1.7 christos if ((error = pty_vn_open(vp, l)) != 0) {
198 1.13 christos DPRINTF(("pty_vn_open %d\n", error));
199 1.1 christos /*
200 1.1 christos * Check if the master open failed because we lost
201 1.1 christos * the race to grab it.
202 1.1 christos */
203 1.1 christos if (error != EIO)
204 1.1 christos goto bad;
205 1.1 christos error = !pty_isfree(md, 1);
206 1.13 christos DPRINTF(("pty_isfree %d\n", error));
207 1.1 christos if (error)
208 1.1 christos goto retry;
209 1.1 christos else
210 1.1 christos goto bad;
211 1.1 christos }
212 1.1 christos fp->f_flag = FREAD|FWRITE;
213 1.1 christos fp->f_type = DTYPE_VNODE;
214 1.1 christos fp->f_ops = &vnops;
215 1.1 christos fp->f_data = vp;
216 1.27 hannken VOP_UNLOCK(vp);
217 1.24 ad fd_affix(curproc, fp, *fd);
218 1.1 christos return 0;
219 1.1 christos bad:
220 1.24 ad fd_abort(curproc, fp, *fd);
221 1.1 christos return error;
222 1.1 christos }
223 1.1 christos
224 1.1 christos int
225 1.28.2.1 rmind pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
226 1.1 christos {
227 1.1 christos int error;
228 1.1 christos struct vnode *vp;
229 1.1 christos
230 1.1 christos /*
231 1.1 christos * Open the slave.
232 1.1 christos * namei -> setattr -> unlock -> revoke -> vrele ->
233 1.1 christos * namei -> open -> unlock
234 1.1 christos * Three stage rocket:
235 1.1 christos * 1. Change the owner and permissions on the slave.
236 1.1 christos * 2. Revoke all the users of the slave.
237 1.1 christos * 3. open the slave.
238 1.1 christos */
239 1.1 christos if (ptm == NULL)
240 1.1 christos return EOPNOTSUPP;
241 1.28.2.1 rmind if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
242 1.1 christos return error;
243 1.1 christos
244 1.1 christos if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
245 1.1 christos struct vattr vattr;
246 1.28.2.1 rmind (*ptm->getvattr)(mp, l, &vattr);
247 1.10 ad /* Do the VOP_SETATTR() as root. */
248 1.21 pooka error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
249 1.1 christos if (error) {
250 1.1 christos DPRINTF(("setattr %d\n", error));
251 1.27 hannken VOP_UNLOCK(vp);
252 1.1 christos vrele(vp);
253 1.1 christos return error;
254 1.1 christos }
255 1.1 christos }
256 1.27 hannken VOP_UNLOCK(vp);
257 1.23 ad VOP_REVOKE(vp, REVOKEALL);
258 1.1 christos
259 1.1 christos /*
260 1.1 christos * The vnode is useless after the revoke, we need to get it again.
261 1.1 christos */
262 1.1 christos vrele(vp);
263 1.1 christos return 0;
264 1.1 christos }
265 1.1 christos
266 1.1 christos static int
267 1.28.2.1 rmind pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp)
268 1.1 christos {
269 1.1 christos int error;
270 1.1 christos struct file *fp;
271 1.1 christos struct vnode *vp;
272 1.1 christos
273 1.1 christos /* Grab a filedescriptor for the slave */
274 1.24 ad if ((error = fd_allocfile(&fp, fd)) != 0) {
275 1.24 ad DPRINTF(("fd_allocfile %d\n", error));
276 1.1 christos return error;
277 1.1 christos }
278 1.1 christos
279 1.1 christos if (ptm == NULL) {
280 1.1 christos error = EOPNOTSUPP;
281 1.1 christos goto bad;
282 1.1 christos }
283 1.1 christos
284 1.28.2.1 rmind if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
285 1.1 christos goto bad;
286 1.7 christos if ((error = pty_vn_open(vp, l)) != 0)
287 1.1 christos goto bad;
288 1.1 christos
289 1.1 christos fp->f_flag = FREAD|FWRITE;
290 1.1 christos fp->f_type = DTYPE_VNODE;
291 1.1 christos fp->f_ops = &vnops;
292 1.1 christos fp->f_data = vp;
293 1.27 hannken VOP_UNLOCK(vp);
294 1.24 ad fd_affix(curproc, fp, *fd);
295 1.1 christos return 0;
296 1.1 christos bad:
297 1.24 ad fd_abort(curproc, fp, *fd);
298 1.1 christos return error;
299 1.1 christos }
300 1.1 christos
301 1.1 christos struct ptm_pty *
302 1.1 christos pty_sethandler(struct ptm_pty *nptm)
303 1.1 christos {
304 1.1 christos struct ptm_pty *optm = ptm;
305 1.1 christos ptm = nptm;
306 1.1 christos return optm;
307 1.1 christos }
308 1.1 christos
309 1.1 christos int
310 1.28.2.1 rmind pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp)
311 1.1 christos {
312 1.1 christos struct ptmget *ptmg = data;
313 1.1 christos int error;
314 1.1 christos
315 1.1 christos if (ptm == NULL)
316 1.1 christos return EOPNOTSUPP;
317 1.1 christos
318 1.2 christos ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
319 1.2 christos ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
320 1.1 christos
321 1.28.2.1 rmind error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
322 1.1 christos if (error)
323 1.1 christos return error;
324 1.1 christos
325 1.28.2.1 rmind return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
326 1.1 christos }
327 1.1 christos
328 1.1 christos void
329 1.1 christos /*ARGSUSED*/
330 1.15 yamt ptmattach(int n)
331 1.1 christos {
332 1.1 christos extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
333 1.1 christos /* find the major and minor of the pty devices */
334 1.1 christos if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
335 1.1 christos panic("ptmattach: Can't find pty slave in cdevsw");
336 1.1 christos if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
337 1.1 christos panic("ptmattach: Can't find pty master in cdevsw");
338 1.1 christos #ifdef COMPAT_BSDPTY
339 1.1 christos ptm = &ptm_bsdpty;
340 1.1 christos #endif
341 1.1 christos }
342 1.1 christos
343 1.5 thorpej static int
344 1.1 christos /*ARGSUSED*/
345 1.15 yamt ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
346 1.1 christos {
347 1.1 christos int error;
348 1.1 christos int fd;
349 1.13 christos dev_t ttydev;
350 1.28.2.1 rmind struct mount *mp;
351 1.1 christos
352 1.1 christos switch(minor(dev)) {
353 1.1 christos case 0: /* /dev/ptmx */
354 1.12 christos case 2: /* /emul/linux/dev/ptmx */
355 1.28.2.1 rmind if ((error = pty_getmp(l, &mp)) != 0)
356 1.28.2.1 rmind return error;
357 1.28.2.1 rmind if ((error = pty_alloc_master(l, &fd, &ttydev, mp)) != 0)
358 1.1 christos return error;
359 1.12 christos if (minor(dev) == 2) {
360 1.12 christos /*
361 1.12 christos * Linux ptyfs grants the pty right here.
362 1.12 christos * Handle this case here, instead of writing
363 1.12 christos * a new linux module.
364 1.12 christos */
365 1.28.2.1 rmind if ((error = pty_grant_slave(l, ttydev, mp)) != 0) {
366 1.24 ad file_t *fp = fd_getfile(fd);
367 1.16 alc if (fp != NULL) {
368 1.24 ad fd_close(fd);
369 1.16 alc }
370 1.12 christos return error;
371 1.12 christos }
372 1.12 christos }
373 1.1 christos curlwp->l_dupfd = fd;
374 1.4 christos return EMOVEFD;
375 1.1 christos case 1: /* /dev/ptm */
376 1.1 christos return 0;
377 1.1 christos default:
378 1.1 christos return ENODEV;
379 1.1 christos }
380 1.1 christos }
381 1.1 christos
382 1.5 thorpej static int
383 1.1 christos /*ARGSUSED*/
384 1.15 yamt ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
385 1.1 christos {
386 1.15 yamt
387 1.1 christos return (0);
388 1.1 christos }
389 1.1 christos
390 1.5 thorpej static int
391 1.1 christos /*ARGSUSED*/
392 1.17 christos ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
393 1.1 christos {
394 1.1 christos int error;
395 1.1 christos dev_t newdev;
396 1.1 christos int cfd, sfd;
397 1.24 ad file_t *fp;
398 1.28.2.1 rmind struct mount *mp;
399 1.1 christos
400 1.1 christos error = 0;
401 1.1 christos switch (cmd) {
402 1.1 christos case TIOCPTMGET:
403 1.28.2.1 rmind if ((error = pty_getmp(l, &mp)) != 0)
404 1.28.2.1 rmind return error;
405 1.28.2.1 rmind
406 1.28.2.1 rmind if ((error = pty_alloc_master(l, &cfd, &newdev, mp)) != 0)
407 1.1 christos return error;
408 1.1 christos
409 1.28.2.1 rmind if ((error = pty_grant_slave(l, newdev, mp)) != 0)
410 1.1 christos goto bad;
411 1.1 christos
412 1.28.2.1 rmind if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0)
413 1.1 christos goto bad;
414 1.1 christos
415 1.1 christos /* now, put the indices and names into struct ptmget */
416 1.28.2.1 rmind if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0)
417 1.28.2.1 rmind goto bad2;
418 1.28.2.1 rmind return 0;
419 1.1 christos default:
420 1.28 apb #ifdef COMPAT_60
421 1.28 apb error = compat_60_ptmioctl(dev, cmd, data, flag, l);
422 1.28 apb if (error != EPASSTHROUGH)
423 1.28 apb return error;
424 1.28 apb #endif /* COMPAT_60 */
425 1.1 christos DPRINTF(("ptmioctl EINVAL\n"));
426 1.1 christos return EINVAL;
427 1.1 christos }
428 1.28.2.1 rmind bad2:
429 1.28.2.1 rmind fp = fd_getfile(sfd);
430 1.28.2.1 rmind if (fp != NULL) {
431 1.28.2.1 rmind fd_close(sfd);
432 1.28.2.1 rmind }
433 1.24 ad bad:
434 1.24 ad fp = fd_getfile(cfd);
435 1.16 alc if (fp != NULL) {
436 1.24 ad fd_close(cfd);
437 1.16 alc }
438 1.1 christos return error;
439 1.1 christos }
440 1.5 thorpej
441 1.5 thorpej const struct cdevsw ptm_cdevsw = {
442 1.28.2.1 rmind .d_open = ptmopen,
443 1.28.2.1 rmind .d_close = ptmclose,
444 1.28.2.1 rmind .d_read = noread,
445 1.28.2.1 rmind .d_write = nowrite,
446 1.28.2.1 rmind .d_ioctl = ptmioctl,
447 1.28.2.1 rmind .d_stop = nullstop,
448 1.28.2.1 rmind .d_tty = notty,
449 1.28.2.1 rmind .d_poll = nopoll,
450 1.28.2.1 rmind .d_mmap = nommap,
451 1.28.2.1 rmind .d_kqfilter = nokqfilter,
452 1.28.2.1 rmind .d_flag = D_TTY
453 1.5 thorpej };
454 1.6 he #endif
455