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