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