Home | History | Annotate | Line # | Download | only in kern
tty_ptm.c revision 1.31
      1 /*	$NetBSD: tty_ptm.c,v 1.31 2014/03/27 17:31:56 christos 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: tty_ptm.c,v 1.31 2014/03/27 17:31:56 christos Exp $");
     31 
     32 #include "opt_compat_netbsd.h"
     33 #include "opt_ptm.h"
     34 
     35 /* pty multiplexor driver /dev/ptm{,x} */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/ioctl.h>
     40 #include <sys/proc.h>
     41 #include <sys/tty.h>
     42 #include <sys/stat.h>
     43 #include <sys/file.h>
     44 #include <sys/uio.h>
     45 #include <sys/kernel.h>
     46 #include <sys/vnode.h>
     47 #include <sys/namei.h>
     48 #include <sys/signalvar.h>
     49 #include <sys/filedesc.h>
     50 #include <sys/conf.h>
     51 #include <sys/poll.h>
     52 #include <sys/pty.h>
     53 #include <sys/kauth.h>
     54 
     55 #include <miscfs/specfs/specdev.h>
     56 
     57 #ifdef COMPAT_60
     58 #include <compat/sys/ttycom.h>
     59 #endif /* COMPAT_60 */
     60 
     61 #ifdef DEBUG_PTM
     62 #define DPRINTF(a)	printf a
     63 #else
     64 #define DPRINTF(a)
     65 #endif
     66 
     67 #ifdef NO_DEV_PTM
     68 const struct cdevsw ptm_cdevsw = {
     69 	.d_open = noopen,
     70 	.d_close = noclose,
     71 	.d_read = noread,
     72 	.d_write = nowrite,
     73 	.d_ioctl = noioctl,
     74 	.d_stop = nostop,
     75 	.d_tty = notty,
     76 	.d_poll = nopoll,
     77 	.d_mmap = nommap,
     78 	.d_kqfilter = nokqfilter,
     79 	.d_flag = D_TTY
     80 };
     81 #else
     82 
     83 static struct ptm_pty *ptm;
     84 int pts_major, ptc_major;
     85 
     86 static dev_t pty_getfree(void);
     87 static int pty_alloc_master(struct lwp *, int *, dev_t *, struct mount *);
     88 static int pty_alloc_slave(struct lwp *, int *, dev_t, struct mount *);
     89 
     90 void ptmattach(int);
     91 
     92 int
     93 ptyfs_getmp(struct lwp *l, struct mount **mpp) {
     94 	struct cwdinfo *cwdi = l->l_proc->p_cwdi;
     95 	struct mount *mp;
     96 
     97 	if (ptm == NULL)
     98 		return EOPNOTSUPP;
     99 
    100 	if (ptm->arg == NULL) { /* BSDPTY */
    101 		*mpp = NULL;
    102 		return 0;
    103 	}
    104 
    105 	mp = ptm->arg;	/* PTYFS */
    106 
    107 	if (cwdi->cwdi_rdir == NULL)
    108 		goto ok;
    109 
    110 	if (vn_isunder(mp->mnt_vnodecovered, cwdi->cwdi_rdir, l))
    111 		goto ok;
    112 
    113 	*mpp = NULL;
    114 	return EOPNOTSUPP;
    115 ok:
    116 	*mpp = mp;
    117 	return 0;
    118 }
    119 
    120 dev_t
    121 pty_makedev(char ms, int minor)
    122 {
    123 	return makedev(ms == 't' ? pts_major : ptc_major, minor);
    124 }
    125 
    126 
    127 static dev_t
    128 pty_getfree(void)
    129 {
    130 	extern kmutex_t pt_softc_mutex;
    131 	int i;
    132 
    133 	mutex_enter(&pt_softc_mutex);
    134 	for (i = 0; i < npty; i++) {
    135 		if (pty_isfree(i, 0))
    136 			break;
    137 	}
    138 	mutex_exit(&pt_softc_mutex);
    139 	return pty_makedev('t', i);
    140 }
    141 
    142 /*
    143  * Hacked up version of vn_open. We _only_ handle ptys and only open
    144  * them with FREAD|FWRITE and never deal with creat or stuff like that.
    145  *
    146  * We need it because we have to fake up root credentials to open the pty.
    147  */
    148 int
    149 pty_vn_open(struct vnode *vp, struct lwp *l)
    150 {
    151 	int error;
    152 
    153 	if (vp->v_type != VCHR) {
    154 		vput(vp);
    155 		return EINVAL;
    156 	}
    157 
    158 	error = VOP_OPEN(vp, FREAD|FWRITE, lwp0.l_cred);
    159 
    160 	if (error) {
    161 		vput(vp);
    162 		return error;
    163 	}
    164 
    165 	vp->v_writecount++;
    166 
    167 	return 0;
    168 }
    169 
    170 static int
    171 pty_alloc_master(struct lwp *l, int *fd, dev_t *dev, struct mount *mp)
    172 {
    173 	int error;
    174 	struct file *fp;
    175 	struct vnode *vp;
    176 	int md;
    177 
    178 	if ((error = fd_allocfile(&fp, fd)) != 0) {
    179 		DPRINTF(("fd_allocfile %d\n", error));
    180 		return error;
    181 	}
    182 retry:
    183 	/* Find and open a free master pty. */
    184 	*dev = pty_getfree();
    185 	md = minor(*dev);
    186 	if ((error = pty_check(md)) != 0) {
    187 		DPRINTF(("pty_check %d\n", error));
    188 		goto bad;
    189 	}
    190 	if (ptm == NULL) {
    191 		DPRINTF(("no ptm\n"));
    192 		error = EOPNOTSUPP;
    193 		goto bad;
    194 	}
    195 	if ((error = (*ptm->allocvp)(mp, l, &vp, *dev, 'p')) != 0) {
    196 		DPRINTF(("pty_allocvp %d\n", error));
    197 		goto bad;
    198 	}
    199 
    200 	if ((error = pty_vn_open(vp, l)) != 0) {
    201 		DPRINTF(("pty_vn_open %d\n", error));
    202 		/*
    203 		 * Check if the master open failed because we lost
    204 		 * the race to grab it.
    205 		 */
    206 		if (error != EIO)
    207 			goto bad;
    208 		error = !pty_isfree(md, 1);
    209 		DPRINTF(("pty_isfree %d\n", error));
    210 		if (error)
    211 			goto retry;
    212 		else
    213 			goto bad;
    214 	}
    215 	fp->f_flag = FREAD|FWRITE;
    216 	fp->f_type = DTYPE_VNODE;
    217 	fp->f_ops = &vnops;
    218 	fp->f_data = vp;
    219 	VOP_UNLOCK(vp);
    220 	fd_affix(curproc, fp, *fd);
    221 	return 0;
    222 bad:
    223 	fd_abort(curproc, fp, *fd);
    224 	return error;
    225 }
    226 
    227 int
    228 pty_grant_slave(struct lwp *l, dev_t dev, struct mount *mp)
    229 {
    230 	int error;
    231 	struct vnode *vp;
    232 
    233 	/*
    234 	 * Open the slave.
    235 	 * namei -> setattr -> unlock -> revoke -> vrele ->
    236 	 * namei -> open -> unlock
    237 	 * Three stage rocket:
    238 	 * 1. Change the owner and permissions on the slave.
    239 	 * 2. Revoke all the users of the slave.
    240 	 * 3. open the slave.
    241 	 */
    242 	if (ptm == NULL)
    243 		return EOPNOTSUPP;
    244 	if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
    245 		return error;
    246 
    247 	if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
    248 		struct vattr vattr;
    249 		(*ptm->getvattr)(mp, l, &vattr);
    250 		/* Do the VOP_SETATTR() as root. */
    251 		error = VOP_SETATTR(vp, &vattr, lwp0.l_cred);
    252 		if (error) {
    253 			DPRINTF(("setattr %d\n", error));
    254 			VOP_UNLOCK(vp);
    255 			vrele(vp);
    256 			return error;
    257 		}
    258 	}
    259 	VOP_UNLOCK(vp);
    260 	VOP_REVOKE(vp, REVOKEALL);
    261 
    262 	/*
    263 	 * The vnode is useless after the revoke, we need to get it again.
    264 	 */
    265 	vrele(vp);
    266 	return 0;
    267 }
    268 
    269 static int
    270 pty_alloc_slave(struct lwp *l, int *fd, dev_t dev, struct mount *mp)
    271 {
    272 	int error;
    273 	struct file *fp;
    274 	struct vnode *vp;
    275 
    276 	/* Grab a filedescriptor for the slave */
    277 	if ((error = fd_allocfile(&fp, fd)) != 0) {
    278 		DPRINTF(("fd_allocfile %d\n", error));
    279 		return error;
    280 	}
    281 
    282 	if (ptm == NULL) {
    283 		error = EOPNOTSUPP;
    284 		goto bad;
    285 	}
    286 
    287 	if ((error = (*ptm->allocvp)(mp, l, &vp, dev, 't')) != 0)
    288 		goto bad;
    289 	if ((error = pty_vn_open(vp, l)) != 0)
    290 		goto bad;
    291 
    292 	fp->f_flag = FREAD|FWRITE;
    293 	fp->f_type = DTYPE_VNODE;
    294 	fp->f_ops = &vnops;
    295 	fp->f_data = vp;
    296 	VOP_UNLOCK(vp);
    297 	fd_affix(curproc, fp, *fd);
    298 	return 0;
    299 bad:
    300 	fd_abort(curproc, fp, *fd);
    301 	return error;
    302 }
    303 
    304 struct ptm_pty *
    305 pty_sethandler(struct ptm_pty *nptm)
    306 {
    307 	struct ptm_pty *optm = ptm;
    308 	ptm = nptm;
    309 	return optm;
    310 }
    311 
    312 int
    313 pty_fill_ptmget(struct lwp *l, dev_t dev, int cfd, int sfd, void *data, struct mount *mp)
    314 {
    315 	struct ptmget *ptmg = data;
    316 	int error;
    317 
    318 	if (ptm == NULL)
    319 		return EOPNOTSUPP;
    320 
    321 	ptmg->cfd = cfd == -1 ? minor(dev) : cfd;
    322 	ptmg->sfd = sfd == -1 ? minor(dev) : sfd;
    323 
    324 	error = (*ptm->makename)(mp, l, ptmg->cn, sizeof(ptmg->cn), dev, 'p');
    325 	if (error)
    326 		return error;
    327 
    328 	return (*ptm->makename)(mp, l, ptmg->sn, sizeof(ptmg->sn), dev, 't');
    329 }
    330 
    331 void
    332 /*ARGSUSED*/
    333 ptmattach(int n)
    334 {
    335 	extern const struct cdevsw pts_cdevsw, ptc_cdevsw;
    336 	/* find the major and minor of the pty devices */
    337 	if ((pts_major = cdevsw_lookup_major(&pts_cdevsw)) == -1)
    338 		panic("ptmattach: Can't find pty slave in cdevsw");
    339 	if ((ptc_major = cdevsw_lookup_major(&ptc_cdevsw)) == -1)
    340 		panic("ptmattach: Can't find pty master in cdevsw");
    341 #ifdef COMPAT_BSDPTY
    342 	ptm = &ptm_bsdpty;
    343 #endif
    344 }
    345 
    346 static int
    347 /*ARGSUSED*/
    348 ptmopen(dev_t dev, int flag, int mode, struct lwp *l)
    349 {
    350 	int error;
    351 	int fd;
    352 	dev_t ttydev;
    353 	struct mount *mp;
    354 
    355 	switch(minor(dev)) {
    356 	case 0:		/* /dev/ptmx */
    357 	case 2:		/* /emul/linux/dev/ptmx */
    358 		if ((error = ptyfs_getmp(l, &mp)) != 0)
    359 			return error;
    360 		if ((error = pty_alloc_master(l, &fd, &ttydev, mp)) != 0)
    361 			return error;
    362 		if (minor(dev) == 2) {
    363 			/*
    364 			 * Linux ptyfs grants the pty right here.
    365 			 * Handle this case here, instead of writing
    366 			 * a new linux module.
    367 			 */
    368 			if ((error = pty_grant_slave(l, ttydev, mp)) != 0) {
    369 				file_t *fp = fd_getfile(fd);
    370 				if (fp != NULL) {
    371 					fd_close(fd);
    372 				}
    373 				return error;
    374 			}
    375 		}
    376 		curlwp->l_dupfd = fd;
    377 		return EMOVEFD;
    378 	case 1:		/* /dev/ptm */
    379 		return 0;
    380 	default:
    381 		return ENODEV;
    382 	}
    383 }
    384 
    385 static int
    386 /*ARGSUSED*/
    387 ptmclose(dev_t dev, int flag, int mode, struct lwp *l)
    388 {
    389 
    390 	return (0);
    391 }
    392 
    393 static int
    394 /*ARGSUSED*/
    395 ptmioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    396 {
    397 	int error;
    398 	dev_t newdev;
    399 	int cfd, sfd;
    400 	file_t *fp;
    401 	struct mount *mp;
    402 
    403 	error = 0;
    404 	switch (cmd) {
    405 	case TIOCPTMGET:
    406 		if ((error = ptyfs_getmp(l, &mp)) != 0)
    407 			return error;
    408 
    409 		if ((error = pty_alloc_master(l, &cfd, &newdev, mp)) != 0)
    410 			return error;
    411 
    412 		if ((error = pty_grant_slave(l, newdev, mp)) != 0)
    413 			goto bad;
    414 
    415 		if ((error = pty_alloc_slave(l, &sfd, newdev, mp)) != 0)
    416 			goto bad;
    417 
    418 		/* now, put the indices and names into struct ptmget */
    419 		if ((error = pty_fill_ptmget(l, newdev, cfd, sfd, data, mp)) != 0)
    420 			goto bad2;
    421 		return 0;
    422 	default:
    423 #ifdef COMPAT_60
    424 		error = compat_60_ptmioctl(dev, cmd, data, flag, l);
    425 		if (error != EPASSTHROUGH)
    426 			return error;
    427 #endif /* COMPAT_60 */
    428 		DPRINTF(("ptmioctl EINVAL\n"));
    429 		return EINVAL;
    430 	}
    431 bad2:
    432 	fp = fd_getfile(sfd);
    433 	if (fp != NULL) {
    434 		fd_close(sfd);
    435 	}
    436  bad:
    437 	fp = fd_getfile(cfd);
    438 	if (fp != NULL) {
    439 		fd_close(cfd);
    440 	}
    441 	return error;
    442 }
    443 
    444 const struct cdevsw ptm_cdevsw = {
    445 	.d_open = ptmopen,
    446 	.d_close = ptmclose,
    447 	.d_read = noread,
    448 	.d_write = nowrite,
    449 	.d_ioctl = ptmioctl,
    450 	.d_stop = nullstop,
    451 	.d_tty = notty,
    452 	.d_poll = nopoll,
    453 	.d_mmap = nommap,
    454 	.d_kqfilter = nokqfilter,
    455 	.d_flag = D_TTY
    456 };
    457 #endif
    458