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