tty_ptm.c revision 1.10 1 /* $NetBSD: tty_ptm.c,v 1.10 2006/07/17 14:49:16 ad 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.10 2006/07/17 14:49:16 ad 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 #include <sys/kauth.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 noopen, noclose, noread, nowrite, noioctl,
72 nostop, notty, nopoll, nommap, nokqfilter, D_TTY
73 };
74 #else
75
76 static struct ptm_pty *ptm;
77 int pts_major, ptc_major;
78
79 static dev_t pty_getfree(void);
80 static int pty_alloc_master(struct lwp *, int *, dev_t *);
81 static int pty_alloc_slave(struct lwp *, int *, dev_t);
82
83 void ptmattach(int);
84
85 dev_t
86 pty_makedev(char ms, int minor)
87 {
88 return makedev(ms == 't' ? pts_major : ptc_major, minor);
89 }
90
91
92 static dev_t
93 pty_getfree(void)
94 {
95 extern struct simplelock pt_softc_mutex;
96 int i;
97
98 simple_lock(&pt_softc_mutex);
99 for (i = 0; i < npty; i++) {
100 if (pty_isfree(i, 0))
101 break;
102 }
103 simple_unlock(&pt_softc_mutex);
104 return pty_makedev('t', i);
105 }
106
107 /*
108 * Hacked up version of vn_open. We _only_ handle ptys and only open
109 * them with FREAD|FWRITE and never deal with creat or stuff like that.
110 *
111 * We need it because we have to fake up root credentials to open the pty.
112 */
113 int
114 pty_vn_open(struct vnode *vp, struct lwp *l)
115 {
116 int error;
117
118 if (vp->v_type != VCHR) {
119 vput(vp);
120 return EINVAL;
121 }
122
123 error = VOP_OPEN(vp, FREAD|FWRITE, proc0.p_cred, l);
124
125 if (error) {
126 vput(vp);
127 return error;
128 }
129
130 vp->v_writecount++;
131
132 return 0;
133 }
134
135 static int
136 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev)
137 {
138 int error;
139 struct file *fp;
140 struct vnode *vp;
141 struct proc *p = l->l_proc;
142 int md;
143
144 if ((error = falloc(p, &fp, fd)) != 0) {
145 DPRINTF(("falloc %d\n", error));
146 return error;
147 }
148 retry:
149 /* Find and open a free master pty. */
150 *dev = pty_getfree();
151 md = minor(*dev);
152 if ((error = pty_check(md)) != 0) {
153 DPRINTF(("pty_check %d\n", error));
154 goto bad;
155 }
156 if (ptm == NULL) {
157 error = EOPNOTSUPP;
158 goto bad;
159 }
160 if ((error = (*ptm->allocvp)(ptm, l, &vp, *dev, 'p')) != 0)
161 goto bad;
162
163 if ((error = pty_vn_open(vp, l)) != 0) {
164 /*
165 * Check if the master open failed because we lost
166 * the race to grab it.
167 */
168 if (error != EIO)
169 goto bad;
170 error = !pty_isfree(md, 1);
171 if (error)
172 goto retry;
173 else
174 goto bad;
175 }
176 fp->f_flag = FREAD|FWRITE;
177 fp->f_type = DTYPE_VNODE;
178 fp->f_ops = &vnops;
179 fp->f_data = vp;
180 VOP_UNLOCK(vp, 0);
181 FILE_SET_MATURE(fp);
182 FILE_UNUSE(fp, l);
183 return 0;
184 bad:
185 FILE_UNUSE(fp, l);
186 fdremove(p->p_fd, *fd);
187 ffree(fp);
188 return error;
189 }
190
191 int
192 pty_grant_slave(struct lwp *l, dev_t dev)
193 {
194 int error;
195 struct vnode *vp;
196
197 /*
198 * Open the slave.
199 * namei -> setattr -> unlock -> revoke -> vrele ->
200 * namei -> open -> unlock
201 * Three stage rocket:
202 * 1. Change the owner and permissions on the slave.
203 * 2. Revoke all the users of the slave.
204 * 3. open the slave.
205 */
206 if (ptm == NULL)
207 return EOPNOTSUPP;
208
209 if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
210 return error;
211
212 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
213 struct vattr vattr;
214 (*ptm->getvattr)(ptm, l->l_proc, &vattr);
215 /* Do the VOP_SETATTR() as root. */
216 error = VOP_SETATTR(vp, &vattr, proc0.p_cred, l);
217 if (error) {
218 DPRINTF(("setattr %d\n", error));
219 VOP_UNLOCK(vp, 0);
220 vrele(vp);
221 return error;
222 }
223 }
224 VOP_UNLOCK(vp, 0);
225 if (vp->v_usecount > 1 ||
226 (vp->v_flag & (VALIASED | VLAYER)))
227 VOP_REVOKE(vp, REVOKEALL);
228
229 /*
230 * The vnode is useless after the revoke, we need to get it again.
231 */
232 vrele(vp);
233 return 0;
234 }
235
236 static int
237 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev)
238 {
239 int error;
240 struct file *fp;
241 struct vnode *vp;
242 struct proc *p = l->l_proc;
243
244 /* Grab a filedescriptor for the slave */
245 if ((error = falloc(p, &fp, fd)) != 0) {
246 DPRINTF(("falloc %d\n", error));
247 return error;
248 }
249
250 if (ptm == NULL) {
251 error = EOPNOTSUPP;
252 goto bad;
253 }
254
255 if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
256 goto bad;
257 if ((error = pty_vn_open(vp, l)) != 0)
258 goto bad;
259
260 fp->f_flag = FREAD|FWRITE;
261 fp->f_type = DTYPE_VNODE;
262 fp->f_ops = &vnops;
263 fp->f_data = vp;
264 VOP_UNLOCK(vp, 0);
265 FILE_SET_MATURE(fp);
266 FILE_UNUSE(fp, l);
267 return 0;
268 bad:
269 FILE_UNUSE(fp, l);
270 fdremove(p->p_fd, *fd);
271 ffree(fp);
272 return error;
273 }
274
275 struct ptm_pty *
276 pty_sethandler(struct ptm_pty *nptm)
277 {
278 struct ptm_pty *optm = ptm;
279 ptm = nptm;
280 return optm;
281 }
282
283 int
284 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data)
285 {
286 struct ptmget *ptmg = data;
287 int error;
288
289 if (ptm == NULL)
290 return EOPNOTSUPP;
291
292 ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
293 ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
294
295 error = (*ptm->makename)(ptm, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
296 if (error)
297 return error;
298
299 return (*ptm->makename)(ptm, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
300 }
301
302 void
303 /*ARGSUSED*/
304 ptmattach(int n)
305 {
306 extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
307 /* find the major and minor of the pty devices */
308 if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
309 panic("ptmattach: Can't find pty slave in cdevsw");
310 if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
311 panic("ptmattach: Can't find pty master in cdevsw");
312 #ifdef COMPAT_BSDPTY
313 ptm = &ptm_bsdpty;
314 #endif
315 }
316
317 static int
318 /*ARGSUSED*/
319 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
320 {
321 int error;
322 int fd;
323
324 switch(minor(dev)) {
325 case 0: /* /dev/ptmx */
326 if ((error = pty_alloc_master(l, &fd, &dev)) != 0)
327 return error;
328 curlwp->l_dupfd = fd;
329 return EMOVEFD;
330 case 1: /* /dev/ptm */
331 return 0;
332 default:
333 return ENODEV;
334 }
335 }
336
337 static int
338 /*ARGSUSED*/
339 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
340 {
341 return (0);
342 }
343
344 static int
345 /*ARGSUSED*/
346 ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
347 {
348 int error;
349 dev_t newdev;
350 int cfd, sfd;
351 struct file *fp;
352 struct proc *p = l->l_proc;
353
354 error = 0;
355 switch (cmd) {
356 case TIOCPTMGET:
357 if ((error = pty_alloc_master(l, &cfd, &newdev)) != 0)
358 return error;
359
360 if ((error = pty_grant_slave(l, newdev)) != 0)
361 goto bad;
362
363 if ((error = pty_alloc_slave(l, &sfd, newdev)) != 0)
364 goto bad;
365
366 /* now, put the indices and names into struct ptmget */
367 return pty_fill_ptmget(l, newdev, cfd, sfd, data);
368 default:
369 DPRINTF(("ptmioctl EINVAL\n"));
370 return EINVAL;
371 }
372 bad:
373 fp = fd_getfile(p->p_fd, cfd);
374 fdremove(p->p_fd, cfd);
375 ffree(fp);
376 return error;
377 }
378
379 const struct cdevsw ptm_cdevsw = {
380 ptmopen, ptmclose, noread, nowrite, ptmioctl,
381 nullstop, notty, nopoll, nommap, nokqfilter, D_TTY
382 };
383 #endif
384