Home | History | Annotate | Line # | Download | only in kern
tty_ptm.c revision 1.2.2.2
      1 /*	$NetBSD: tty_ptm.c,v 1.2.2.2 2004/11/14 08:15:57 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.2 2004/11/14 08:15:57 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 		/* get real uid */
    229 		VATTR_NULL(&vattr);
    230 		vattr.va_uid = l->l_proc->p_cred->p_ruid;
    231 		vattr.va_gid = _TTY_GID;
    232 		vattr.va_mode = S_IRUSR|S_IWUSR|S_IWGRP;
    233 		/* Get a fake cred to pretend we're root. */
    234 		cred = crget();
    235 		error = VOP_SETATTR(vp, &vattr, cred, l);
    236 		crfree(cred);
    237 		if (error) {
    238 			DPRINTF(("setattr %d\n", error));
    239 			VOP_UNLOCK(vp, 0);
    240 			vrele(vp);
    241 			return error;
    242 		}
    243 	}
    244 	VOP_UNLOCK(vp, 0);
    245 	if (vp->v_usecount > 1 ||
    246 	    (vp->v_flag & (VALIASED | VLAYER)))
    247 		VOP_REVOKE(vp, REVOKEALL);
    248 
    249 	/*
    250 	 * The vnode is useless after the revoke, we need to get it again.
    251 	 */
    252 	vrele(vp);
    253 	return 0;
    254 }
    255 
    256 static int
    257 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev)
    258 {
    259 	int error;
    260 	struct file *fp;
    261 	struct vnode *vp;
    262 	struct proc *p = l->l_proc;
    263 
    264 	/* Grab a filedescriptor for the slave */
    265 	if ((error = falloc(p, &fp, fd)) != 0) {
    266 		DPRINTF(("falloc %d\n", error));
    267 		return error;
    268 	}
    269 
    270 	if (ptm == NULL) {
    271 		error = EOPNOTSUPP;
    272 		goto bad;
    273 	}
    274 
    275 	if ((error = (*ptm->allocvp)(ptm, l, &vp, dev, 't')) != 0)
    276 		goto bad;
    277 	if ((error = pty_vn_open(vp, l)) != 0)
    278 		goto bad;
    279 
    280 	fp->f_flag = FREAD|FWRITE;
    281 	fp->f_type = DTYPE_VNODE;
    282 	fp->f_ops = &vnops;
    283 	fp->f_data = vp;
    284 	VOP_UNLOCK(vp, 0);
    285 	FILE_SET_MATURE(fp);
    286 	FILE_UNUSE(fp, l);
    287 	return 0;
    288 bad:
    289 	FILE_UNUSE(fp, l);
    290 	fdremove(p->p_fd, *fd);
    291 	ffree(fp);
    292 	return error;
    293 }
    294 
    295 struct ptm_pty *
    296 pty_sethandler(struct ptm_pty *nptm)
    297 {
    298 	struct ptm_pty *optm = ptm;
    299 	ptm = nptm;
    300 	return optm;
    301 }
    302 
    303 int
    304 pty_fill_ptmget(dev_t dev, int cfd, int sfd, void *data)
    305 {
    306 	struct ptmget *ptmg = data;
    307 	int error;
    308 
    309 	if (ptm == NULL)
    310 		return EOPNOTSUPP;
    311 
    312 	ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
    313 	ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
    314 
    315 	error = (*ptm->makename)(ptm, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
    316 	if (error)
    317 		return error;
    318 
    319 	return (*ptm->makename)(ptm, ptmg->sn, sizeof(ptmg->sn), dev, 't');
    320 }
    321 
    322 void
    323 /*ARGSUSED*/
    324 ptmattach(int n)
    325 {
    326 	extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
    327 	/* find the major and minor of the pty devices */
    328 	if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
    329 		panic("ptmattach: Can't find pty slave in cdevsw");
    330 	if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
    331 		panic("ptmattach: Can't find pty master in cdevsw");
    332 #ifdef COMPAT_BSDPTY
    333 	ptm = &ptm_bsdpty;
    334 #endif
    335 }
    336 
    337 int
    338 /*ARGSUSED*/
    339 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
    340 {
    341 	int error;
    342 	int fd;
    343 
    344 	switch(minor(dev)) {
    345 	case 0:		/* /dev/ptmx */
    346 		if ((error = pty_alloc_master(l, &fd, &dev)) != 0)
    347 			return error;
    348 		curlwp->l_dupfd = fd;
    349 		return ENXIO;
    350 	case 1:		/* /dev/ptm */
    351 		return 0;
    352 	default:
    353 		return ENODEV;
    354 	}
    355 }
    356 
    357 int
    358 /*ARGSUSED*/
    359 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
    360 {
    361 	return (0);
    362 }
    363 
    364 int
    365 /*ARGSUSED*/
    366 ptmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    367 {
    368 	int error;
    369 	dev_t newdev;
    370 	int cfd, sfd;
    371 	struct file *fp;
    372 	struct proc *p = l->l_proc;
    373 
    374 	error = 0;
    375 	switch (cmd) {
    376 	case TIOCPTMGET:
    377 		if ((error = pty_alloc_master(l, &cfd, &newdev)) != 0)
    378 			return error;
    379 
    380 		if ((error = pty_grant_slave(l, newdev)) != 0)
    381 			goto bad;
    382 
    383 		if ((error = pty_alloc_slave(l, &sfd, newdev)) != 0)
    384 			goto bad;
    385 
    386 		/* now, put the indices and names into struct ptmget */
    387 		return pty_fill_ptmget(newdev, cfd, sfd, data);
    388 	default:
    389 		DPRINTF(("ptmioctl EINVAL\n"));
    390 		return EINVAL;
    391 	}
    392 bad:
    393 	fp = fd_getfile(p->p_fd, cfd);
    394 	fdremove(p->p_fd, cfd);
    395 	ffree(fp);
    396 	return error;
    397 }
    398 #endif
    399