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