tty_ptm.c revision 1.35 1 /* $NetBSD: tty_ptm.c,v 1.35 2014/10/15 15:00:03 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
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.
15 *
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.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.35 2014/10/15 15:00:03 christos Exp $");
31
32 #include "opt_compat_netbsd.h"
33 #include "opt_ptm.h"
34
35 /* pty multiplexor driver /dev/ptm{,x} */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ioctl.h>
40 #include <sys/proc.h>
41 #include <sys/tty.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 #include <sys/uio.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/namei.h>
48 #include <sys/signalvar.h>
49 #include <sys/filedesc.h>
50 #include <sys/conf.h>
51 #include <sys/poll.h>
52 #include <sys/pty.h>
53 #include <sys/kauth.h>
54
55 #include <miscfs/specfs/specdev.h>
56
57 #ifdef COMPAT_60
58 #include <compat/sys/ttycom.h>
59 #endif /* COMPAT_60 */
60
61 #ifdef DEBUG_PTM
62 #define DPRINTF(a) printf a
63 #else
64 #define DPRINTF(a)
65 #endif
66
67 #ifdef NO_DEV_PTM
68 const struct cdevsw ptm_cdevsw = {
69 .d_open = noopen,
70 .d_close = noclose,
71 .d_read = noread,
72 .d_write = nowrite,
73 .d_ioctl = noioctl,
74 .d_stop = nostop,
75 .d_tty = notty,
76 .d_poll = nopoll,
77 .d_mmap = nommap,
78 .d_kqfilter = nokqfilter,
79 .d_discard = nodiscard,
80 .d_flag = D_TTY
81 };
82 #else
83
84 static struct ptm_pty *ptm;
85 int pts_major, ptc_major;
86
87 static dev_t pty_getfree(void);
88 static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *);
89 static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
90 static int pty_vn_open(struct vnode *, struct lwp *);
91
92 void ptmattach(int);
93
94 int
95 pty_getmp(struct lwp *l, struct mount **mpp)
96 {
97 if (ptm == NULL)
98 return EOPNOTSUPP;
99
100 return (*ptm->getmp)(l, mpp);
101 }
102
103 dev_t
104 pty_makedev(char ms, int minor)
105 {
106 return makedev(ms == 't' ? pts_major : ptc_major, minor);
107 }
108
109
110 static dev_t
111 pty_getfree(void)
112 {
113 extern kmutex_t pt_softc_mutex;
114 int i;
115
116 mutex_enter(&pt_softc_mutex);
117 for (i = 0; i < npty; i++) {
118 if (pty_isfree(i, 0))
119 break;
120 }
121 mutex_exit(&pt_softc_mutex);
122 return pty_makedev('t', i);
123 }
124
125 /*
126 * Hacked up version of vn_open. We _only_ handle ptys and only open
127 * them with FREAD|FWRITE and never deal with creat or stuff like that.
128 *
129 * We need it because we have to fake up root credentials to open the pty.
130 */
131 int
132 pty_vn_open(struct vnode *vp, struct lwp *l)
133 {
134 int error;
135
136 if (vp->v_type != VCHR) {
137 vput(vp);
138 return EINVAL;
139 }
140
141 error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred);
142
143 if (error) {
144 vput(vp);
145 return error;
146 }
147
148 vp->v_writecount++;
149
150 return 0;
151 }
152
153 static int
154 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp)
155 {
156 int error;
157 struct file *fp;
158 struct vnode *vp;
159 int md;
160
161 if ((error = fd_allocfile(&fp, fd)) != 0) {
162 DPRINTF(("fd_allocfile %d\n", error));
163 return error;
164 }
165 retry:
166 /* Find and open a free master pty. */
167 *dev = pty_getfree();
168 md = minor(*dev);
169 if ((error = pty_check(md)) != 0) {
170 DPRINTF(("pty_check %d\n", error));
171 goto bad;
172 }
173 if (ptm == NULL) {
174 DPRINTF(("no ptm\n"));
175 error = EOPNOTSUPP;
176 goto bad;
177 }
178 if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) {
179 DPRINTF(("pty_allocvp %d\n", error));
180 goto bad;
181 }
182
183 if ((error = pty_vn_open(vp, l)) != 0) {
184 DPRINTF(("pty_vn_open %d\n", error));
185 /*
186 * Check if the master open failed because we lost
187 * the race to grab it.
188 */
189 if (error != EIO)
190 goto bad;
191 error = !pty_isfree(md, 1);
192 DPRINTF(("pty_isfree %d\n", error));
193 if (error)
194 goto retry;
195 else
196 goto bad;
197 }
198 fp->f_flag = FREAD|FWRITE;
199 fp->f_type = DTYPE_VNODE;
200 fp->f_ops = &vnops;
201 fp->f_vnode = vp;
202 VOP_UNLOCK(vp);
203 fd_affix(curproc, fp, *fd);
204 return 0;
205 bad:
206 fd_abort(curproc, fp, *fd);
207 return error;
208 }
209
210 int
211 pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
212 {
213 int error;
214 struct vnode *vp;
215
216 /*
217 * Open the slave.
218 * namei -> setattr -> unlock -> revoke -> vrele ->
219 * namei -> open -> unlock
220 * Three stage rocket:
221 * 1. Change the owner and permissions on the slave.
222 * 2. Revoke all the users of the slave.
223 * 3. open the slave.
224 */
225 if (ptm == NULL)
226 return EOPNOTSUPP;
227 if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
228 return error;
229
230 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
231 struct vattr vattr;
232 (*ptm->getvattr)(mp, l, &vattr);
233 /* Do the VOP_SETATTR() as root. */
234 error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
235 if (error) {
236 DPRINTF(("setattr %d\n", error));
237 VOP_UNLOCK(vp);
238 vrele(vp);
239 return error;
240 }
241 }
242 VOP_UNLOCK(vp);
243 VOP_REVOKE(vp, REVOKEALL);
244
245 /*
246 * The vnode is useless after the revoke, we need to get it again.
247 */
248 vrele(vp);
249 return 0;
250 }
251
252 static int
253 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp)
254 {
255 int error;
256 struct file *fp;
257 struct vnode *vp;
258
259 /* Grab a filedescriptor for the slave */
260 if ((error = fd_allocfile(&fp, fd)) != 0) {
261 DPRINTF(("fd_allocfile %d\n", error));
262 return error;
263 }
264
265 if (ptm == NULL) {
266 error = EOPNOTSUPP;
267 goto bad;
268 }
269
270 if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
271 goto bad;
272 if ((error = pty_vn_open(vp, l)) != 0)
273 goto bad;
274
275 fp->f_flag = FREAD|FWRITE;
276 fp->f_type = DTYPE_VNODE;
277 fp->f_ops = &vnops;
278 fp->f_vnode = vp;
279 VOP_UNLOCK(vp);
280 fd_affix(curproc, fp, *fd);
281 return 0;
282 bad:
283 fd_abort(curproc, fp, *fd);
284 return error;
285 }
286
287 struct ptm_pty *
288 pty_sethandler(struct ptm_pty *nptm)
289 {
290 struct ptm_pty *optm = ptm;
291 ptm = nptm;
292 return optm;
293 }
294
295 int
296 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp)
297 {
298 struct ptmget *ptmg = data;
299 int error;
300
301 if (ptm == NULL)
302 return EOPNOTSUPP;
303
304 ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
305 ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
306
307 error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
308 if (error)
309 return error;
310
311 return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
312 }
313
314 void
315 /*ARGSUSED*/
316 ptmattach(int n)
317 {
318 extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
319 /* find the major and minor of the pty devices */
320 if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
321 panic("ptmattach: Can't find pty slave in cdevsw");
322 if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
323 panic("ptmattach: Can't find pty master in cdevsw");
324 #ifdef COMPAT_BSDPTY
325 ptm = &ptm_bsdpty;
326 #endif
327 }
328
329 static int
330 /*ARGSUSED*/
331 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
332 {
333 int error;
334 int fd;
335 dev_t ttydev;
336 struct mount *mp;
337
338 switch(minor(dev)) {
339 case 0: /* /dev/ptmx */
340 case 2: /* /emul/linux/dev/ptmx */
341 if ((error = pty_getmp(l, &mp)) != 0)
342 return error;
343 if ((error = pty_alloc_master(l, &fd, &ttydev, mp)) != 0)
344 return error;
345 if (minor(dev) == 2) {
346 /*
347 * Linux ptyfs grants the pty right here.
348 * Handle this case here, instead of writing
349 * a new linux module.
350 */
351 if ((error = pty_grant_slave(l, ttydev, mp)) != 0) {
352 file_t *fp = fd_getfile(fd);
353 if (fp != NULL) {
354 fd_close(fd);
355 }
356 return error;
357 }
358 }
359 curlwp->l_dupfd = fd;
360 return EMOVEFD;
361 case 1: /* /dev/ptm */
362 return 0;
363 default:
364 return ENODEV;
365 }
366 }
367
368 static int
369 /*ARGSUSED*/
370 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
371 {
372
373 return (0);
374 }
375
376 static int
377 /*ARGSUSED*/
378 ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
379 {
380 int error;
381 dev_t newdev;
382 int cfd, sfd;
383 file_t *fp;
384 struct mount *mp;
385
386 error = 0;
387 switch (cmd) {
388 case TIOCPTMGET:
389 if ((error = pty_getmp(l, &mp)) != 0)
390 return error;
391
392 if ((error = pty_alloc_master(l, &cfd, &newdev, mp)) != 0)
393 return error;
394
395 if ((error = pty_grant_slave(l, newdev, mp)) != 0)
396 goto bad;
397
398 if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0)
399 goto bad;
400
401 /* now, put the indices and names into struct ptmget */
402 if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0)
403 goto bad2;
404 return 0;
405 default:
406 #ifdef COMPAT_60
407 error = compat_60_ptmioctl(dev, cmd, data, flag, l);
408 if (error != EPASSTHROUGH)
409 return error;
410 #endif /* COMPAT_60 */
411 DPRINTF(("ptmioctl EINVAL\n"));
412 return EINVAL;
413 }
414 bad2:
415 fp = fd_getfile(sfd);
416 if (fp != NULL) {
417 fd_close(sfd);
418 }
419 bad:
420 fp = fd_getfile(cfd);
421 if (fp != NULL) {
422 fd_close(cfd);
423 }
424 return error;
425 }
426
427 const struct cdevsw ptm_cdevsw = {
428 .d_open = ptmopen,
429 .d_close = ptmclose,
430 .d_read = noread,
431 .d_write = nowrite,
432 .d_ioctl = ptmioctl,
433 .d_stop = nullstop,
434 .d_tty = notty,
435 .d_poll = nopoll,
436 .d_mmap = nommap,
437 .d_kqfilter = nokqfilter,
438 .d_discard = nodiscard,
439 .d_flag = D_TTY
440 };
441 #endif
442