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