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