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