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