Home | History | Annotate | Line # | Download | only in kern
tty_pty.c revision 1.142.8.2
      1 /*	$NetBSD: tty_pty.c,v 1.142.8.2 2017/04/29 11:12:15 pgoyette Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1993
      5  *	The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
     32  */
     33 
     34 /*
     35  * Pseudo-teletype Driver
     36  * (Actually two drivers, requiring two entries in 'cdevsw')
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.142.8.2 2017/04/29 11:12:15 pgoyette Exp $");
     41 
     42 #include "opt_ptm.h"
     43 
     44 #define TTY_ALLOW_PRIVATE
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/ioctl_compat.h>
     50 #include <sys/proc.h>
     51 #include <sys/tty.h>
     52 #include <sys/stat.h>
     53 #include <sys/file.h>
     54 #include <sys/kernel.h>
     55 #include <sys/vnode.h>
     56 #include <sys/namei.h>
     57 #include <sys/signalvar.h>
     58 #include <sys/uio.h>
     59 #include <sys/filedesc.h>
     60 #include <sys/conf.h>
     61 #include <sys/poll.h>
     62 #include <sys/pty.h>
     63 #include <sys/kauth.h>
     64 
     65 #include "ioconf.h"
     66 
     67 #define	DEFAULT_NPTYS		16	/* default number of initial ptys */
     68 #define DEFAULT_MAXPTYS		992	/* default maximum number of ptys */
     69 
     70 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
     71 
     72 struct	pt_softc {
     73 	struct	tty *pt_tty;
     74 	int	pt_flags;
     75 	struct	selinfo pt_selr, pt_selw;
     76 	u_char	pt_send;
     77 	u_char	pt_ucntl;
     78 };
     79 
     80 static struct pt_softc **pt_softc = NULL;	/* pty array */
     81 static int maxptys = DEFAULT_MAXPTYS;	/* maximum number of ptys (sysctable) */
     82 kmutex_t pt_softc_mutex;
     83 int npty = 0;			/* for pstat -t */
     84 
     85 #define	PF_PKT		0x08		/* packet mode */
     86 #define	PF_STOPPED	0x10		/* user told stopped */
     87 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
     88 #define	PF_NOSTOP	0x40
     89 #define PF_UCNTL	0x80		/* user control mode */
     90 
     91 void	ptcwakeup(struct tty *, int);
     92 void	ptsstart(struct tty *);
     93 int	pty_maxptys(int, int);
     94 
     95 static struct pt_softc **ptyarralloc(int);
     96 
     97 dev_type_open(ptcopen);
     98 dev_type_close(ptcclose);
     99 dev_type_read(ptcread);
    100 dev_type_write(ptcwrite);
    101 dev_type_poll(ptcpoll);
    102 dev_type_kqfilter(ptckqfilter);
    103 
    104 dev_type_open(ptsopen);
    105 dev_type_close(ptsclose);
    106 dev_type_read(ptsread);
    107 dev_type_write(ptswrite);
    108 dev_type_stop(ptsstop);
    109 dev_type_poll(ptspoll);
    110 
    111 dev_type_ioctl(ptyioctl);
    112 dev_type_tty(ptytty);
    113 
    114 const struct cdevsw ptc_cdevsw = {
    115 	DEVSW_MODULE_INIT
    116 	.d_open = ptcopen,
    117 	.d_close = ptcclose,
    118 	.d_read = ptcread,
    119 	.d_write = ptcwrite,
    120 	.d_ioctl = ptyioctl,
    121 	.d_stop = nullstop,
    122 	.d_tty = ptytty,
    123 	.d_poll = ptcpoll,
    124 	.d_mmap = nommap,
    125 	.d_kqfilter = ptckqfilter,
    126 	.d_discard = nodiscard,
    127 	.d_flag = D_TTY
    128 };
    129 
    130 const struct cdevsw pts_cdevsw = {
    131 	DEVSW_MODULE_INIT
    132 	.d_open = ptsopen,
    133 	.d_close = ptsclose,
    134 	.d_read = ptsread,
    135 	.d_write = ptswrite,
    136 	.d_ioctl = ptyioctl,
    137 	.d_stop = ptsstop,
    138 	.d_tty = ptytty,
    139 	.d_poll = ptspoll,
    140 	.d_mmap = nommap,
    141 	.d_kqfilter = ttykqfilter,
    142 	.d_discard = nodiscard,
    143 	.d_flag = D_TTY
    144 };
    145 
    146 #if defined(pmax)
    147 /*
    148  * Used by arch/pmax/conf/majors.pmax, which needs a second copy as it
    149  * needs to map this stuff to two pairs of majors.
    150  */
    151 
    152 const struct cdevsw ptc_ultrix_cdevsw = {
    153 	DEVSW_MODULE_INIT
    154 	.d_open = ptcopen,
    155 	.d_close = ptcclose,
    156 	.d_read = ptcread,
    157 	.d_write = ptcwrite,
    158 	.d_ioctl = ptyioctl,
    159 	.d_stop = nullstop,
    160 	.d_tty = ptytty,
    161 	.d_poll = ptcpoll,
    162 	.d_mmap = nommap,
    163 	.d_kqfilter = ptckqfilter,
    164 	.d_discard = nodiscard,
    165 	.d_flag = D_TTY
    166 };
    167 
    168 const struct cdevsw pts_ultrix_cdevsw = {
    169 	DEVSW_MODULE_INIT
    170 	.d_open = ptsopen,
    171 	.d_close = ptsclose,
    172 	.d_read = ptsread,
    173 	.d_write = ptswrite,
    174 	.d_ioctl = ptyioctl,
    175 	.d_stop = ptsstop,
    176 	.d_tty = ptytty,
    177 	.d_poll = ptspoll,
    178 	.d_mmap = nommap,
    179 	.d_kqfilter = ttykqfilter,
    180 	.d_discard = nodiscard,
    181 	.d_flag = D_TTY
    182 };
    183 #endif /* defined(pmax) */
    184 
    185 /*
    186  * Check if a pty is free to use.
    187  */
    188 int
    189 pty_isfree(int minor, int lock)
    190 {
    191 	struct pt_softc *pt = pt_softc[minor];
    192 	if (lock)
    193 		mutex_enter(&pt_softc_mutex);
    194 	minor = pt == NULL || pt->pt_tty == NULL ||
    195 	    pt->pt_tty->t_oproc == NULL;
    196 	if (lock)
    197 		mutex_exit(&pt_softc_mutex);
    198 	return minor;
    199 }
    200 
    201 /*
    202  * Allocate and zero array of nelem elements.
    203  */
    204 static struct pt_softc **
    205 ptyarralloc(int nelem)
    206 {
    207 	struct pt_softc **pt;
    208 	nelem += 10;
    209 	pt = kmem_zalloc(nelem * sizeof(*pt), KM_SLEEP);
    210 	return pt;
    211 }
    212 
    213 static void
    214 ptyarrfree(struct pt_softc **pt, int nelem)
    215 {
    216 
    217 	nelem += 10;
    218 	kmem_free(pt, nelem * sizeof(*pt));
    219 }
    220 
    221 /*
    222  * Check if the minor is correct and ensure necessary structures
    223  * are properly allocated.
    224  */
    225 int
    226 pty_check(int ptn)
    227 {
    228 	struct pt_softc *pti;
    229 
    230 	if (ptn >= npty) {
    231 		struct pt_softc **newpt, **oldpt;
    232 		int newnpty;
    233 		int oldnpty;
    234 
    235 		/* check if the requested pty can be granted */
    236 		if (ptn >= maxptys) {
    237 	    limit_reached:
    238 			tablefull("pty", "increase kern.maxptys");
    239 			return ENXIO;
    240 		}
    241 
    242 		/* Allocate a larger pty array */
    243 		for (newnpty = npty; newnpty <= ptn;)
    244 			newnpty *= 2;
    245 		if (newnpty > maxptys)
    246 			newnpty = maxptys;
    247 		newpt = ptyarralloc(newnpty);
    248 
    249 		/*
    250 		 * Now grab the pty array mutex - we need to ensure
    251 		 * that the pty array is consistent while copying its
    252 		 * content to newly allocated, larger space; we also
    253 		 * need to be safe against pty_maxptys().
    254 		 */
    255 		mutex_enter(&pt_softc_mutex);
    256 
    257 		if (newnpty >= maxptys) {
    258 			/* limit cut away beneath us... */
    259 			if (ptn >= maxptys) {
    260 				mutex_exit(&pt_softc_mutex);
    261 				ptyarrfree(newpt, newnpty);
    262 				goto limit_reached;
    263 			}
    264 			newnpty = maxptys;
    265 		}
    266 
    267 		/*
    268 		 * If the pty array was not enlarged while we were waiting
    269 		 * for mutex, copy current contents of pt_softc[] to newly
    270 		 * allocated array and start using the new bigger array.
    271 		 */
    272 		if (newnpty > npty) {
    273 			memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
    274 			oldpt = pt_softc;
    275 			oldnpty = npty;
    276 			pt_softc = newpt;
    277 			npty = newnpty;
    278 		} else {
    279 			/* was enlarged when waited for lock, free new space */
    280 			oldpt = newpt;
    281 			oldnpty = newnpty;
    282 		}
    283 
    284 		mutex_exit(&pt_softc_mutex);
    285 		ptyarrfree(oldpt, oldnpty);
    286 	}
    287 
    288 	/*
    289 	 * If the entry is not yet allocated, allocate one. The mutex is
    290 	 * needed so that the state of pt_softc[] array is consistant
    291 	 * in case it has been lengthened above.
    292 	 */
    293 	if (!pt_softc[ptn]) {
    294 		pti = kmem_zalloc(sizeof(*pti), KM_SLEEP);
    295 
    296 		selinit(&pti->pt_selr);
    297 		selinit(&pti->pt_selw);
    298 		pti->pt_tty = tty_alloc();
    299 
    300 		mutex_enter(&pt_softc_mutex);
    301 
    302 		/*
    303 		 * Check the entry again - it might have been
    304 		 * added while we were waiting for mutex.
    305 		 */
    306 		if (pt_softc[ptn]) {
    307 			mutex_exit(&pt_softc_mutex);
    308 			tty_free(pti->pt_tty);
    309 			seldestroy(&pti->pt_selr);
    310 			seldestroy(&pti->pt_selw);
    311 			kmem_free(pti, sizeof(*pti));
    312 			return 0;
    313 		}
    314 		tty_attach(pti->pt_tty);
    315 		pt_softc[ptn] = pti;
    316 
    317 		mutex_exit(&pt_softc_mutex);
    318 	}
    319 
    320 	return 0;
    321 }
    322 
    323 /*
    324  * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
    325  * new value of maxptys.
    326  */
    327 int
    328 pty_maxptys(int newmax, int set)
    329 {
    330 	if (!set)
    331 		return maxptys;
    332 
    333 	/*
    334 	 * We have to grab the pt_softc lock, so that we would pick correct
    335 	 * value of npty (might be modified in pty_check()).
    336 	 */
    337 	mutex_enter(&pt_softc_mutex);
    338 
    339 	/*
    340 	 * The value cannot be set to value lower than the highest pty
    341 	 * number ever allocated.
    342 	 */
    343 	if (newmax >= npty)
    344 		maxptys = newmax;
    345 	else
    346 		newmax = 0;
    347 
    348 	mutex_exit(&pt_softc_mutex);
    349 
    350 	return newmax;
    351 }
    352 
    353 /*
    354  * Establish n (or default if n is 1) ptys in the system.
    355  */
    356 void
    357 ptyattach(int n)
    358 {
    359 
    360 	mutex_init(&pt_softc_mutex, MUTEX_DEFAULT, IPL_NONE);
    361 
    362 	/* maybe should allow 0 => none? */
    363 	if (n <= 1)
    364 		n = DEFAULT_NPTYS;
    365 	pt_softc = ptyarralloc(n);
    366 	npty = n;
    367 #ifndef NO_DEV_PTM
    368 	ptmattach(1);
    369 #endif
    370 }
    371 
    372 /*ARGSUSED*/
    373 int
    374 ptsopen(dev_t dev, int flag, int devtype, struct lwp *l)
    375 {
    376 	struct pt_softc *pti;
    377 	struct tty *tp;
    378 	int error;
    379 	int ptn = minor(dev);
    380 
    381 	if ((error = pty_check(ptn)) != 0)
    382 		return error;
    383 
    384 	mutex_spin_enter(&tty_lock);
    385 	pti = pt_softc[ptn];
    386 	tp = pti->pt_tty;
    387 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
    388 		tp->t_dev = dev;
    389 		ttychars(tp);		/* Set up default chars */
    390 		tp->t_iflag = TTYDEF_IFLAG;
    391 		tp->t_oflag = TTYDEF_OFLAG;
    392 		tp->t_lflag = TTYDEF_LFLAG;
    393 		tp->t_cflag = TTYDEF_CFLAG;
    394 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    395 		ttsetwater(tp);		/* would be done in xxparam() */
    396 	} else if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
    397 	    tp) != 0) {
    398 		mutex_spin_exit(&tty_lock);
    399 		return EBUSY;
    400 	}
    401 	if (tp->t_oproc)			/* Ctrlr still around. */
    402 		SET(tp->t_state, TS_CARR_ON);
    403 	if (!ISSET(flag, O_NONBLOCK)) {
    404 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
    405 			tp->t_wopen++;
    406 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    407 			tp->t_wopen--;
    408 			if (error != 0) {
    409 				mutex_spin_exit(&tty_lock);
    410 				return error;
    411 			}
    412 		}
    413 	}
    414 	mutex_spin_exit(&tty_lock);
    415 	error = (*tp->t_linesw->l_open)(dev, tp);
    416 	ptcwakeup(tp, FREAD|FWRITE);
    417 	return error;
    418 }
    419 
    420 int
    421 ptsclose(dev_t dev, int flag, int mode, struct lwp *l)
    422 {
    423 	struct pt_softc *pti = pt_softc[minor(dev)];
    424 	struct tty *tp = pti->pt_tty;
    425 	int error;
    426 
    427 	error = (*tp->t_linesw->l_close)(tp, flag);
    428 	error |= ttyclose(tp);
    429 	ptcwakeup(tp, FREAD|FWRITE);
    430 	return error;
    431 }
    432 
    433 int
    434 ptsread(dev_t dev, struct uio *uio, int flag)
    435 {
    436 	struct proc *p = curproc;
    437 	struct pt_softc *pti = pt_softc[minor(dev)];
    438 	struct tty *tp = pti->pt_tty;
    439 	int error = 0;
    440 	int cc, c;
    441 
    442 again:
    443 	if (pti->pt_flags & PF_REMOTE) {
    444 		mutex_spin_enter(&tty_lock);
    445 		while (isbackground(p, tp)) {	/* XXXSMP */
    446 			if (sigismasked(curlwp, SIGTTIN) ||
    447 			    p->p_pgrp->pg_jobc == 0 ||
    448 			    p->p_lflag & PL_PPWAIT) {
    449 				mutex_spin_exit(&tty_lock);
    450 				return EIO;
    451 			}
    452 			ttysig(tp, TTYSIG_PG1, SIGTTIN);
    453 			error = ttypause(tp, hz);
    454 			if (error != 0) {
    455 				mutex_spin_exit(&tty_lock);
    456 				return error;
    457 			}
    458 		}
    459 		if (tp->t_canq.c_cc == 0) {
    460 			if (flag & IO_NDELAY) {
    461 				mutex_spin_exit(&tty_lock);
    462 				return EWOULDBLOCK;
    463 			}
    464 			error = ttysleep(tp, &tp->t_cancv, true, 0);
    465 			mutex_spin_exit(&tty_lock);
    466 			if (error != 0)
    467 				return error;
    468 			goto again;
    469 		}
    470 		while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
    471 			c = getc(&tp->t_canq);
    472 			mutex_spin_exit(&tty_lock);
    473 			error = ureadc(c, uio);
    474 			mutex_spin_enter(&tty_lock);
    475 			/* Re-check terminal state here? */
    476 		}
    477 		if (tp->t_canq.c_cc == 1)
    478 			(void) getc(&tp->t_canq);
    479 		cc = tp->t_canq.c_cc;
    480 		mutex_spin_exit(&tty_lock);
    481 		if (cc)
    482 			return error;
    483 	} else if (tp->t_oproc)
    484 		error = (*tp->t_linesw->l_read)(tp, uio, flag);
    485 	ptcwakeup(tp, FWRITE);
    486 	return error;
    487 }
    488 
    489 /*
    490  * Write to pseudo-tty.
    491  * Wakeups of controlling tty will happen
    492  * indirectly, when tty driver calls ptsstart.
    493  */
    494 int
    495 ptswrite(dev_t dev, struct uio *uio, int flag)
    496 {
    497 	struct pt_softc *pti = pt_softc[minor(dev)];
    498 	struct tty *tp = pti->pt_tty;
    499 
    500 	if (tp->t_oproc == NULL)
    501 		return EIO;
    502 	return (*tp->t_linesw->l_write)(tp, uio, flag);
    503 }
    504 
    505 /*
    506  * Poll pseudo-tty.
    507  */
    508 int
    509 ptspoll(dev_t dev, int events, struct lwp *l)
    510 {
    511 	struct pt_softc *pti = pt_softc[minor(dev)];
    512 	struct tty *tp = pti->pt_tty;
    513 
    514 	if (tp->t_oproc == NULL)
    515 		return POLLHUP;
    516 
    517 	return (*tp->t_linesw->l_poll)(tp, events, l);
    518 }
    519 
    520 /*
    521  * Start output on pseudo-tty.
    522  * Wake up process polling or sleeping for input from controlling tty.
    523  */
    524 void
    525 ptsstart(struct tty *tp)
    526 {
    527 	struct pt_softc *pti;
    528 
    529 	KASSERT(tp->t_dev != NODEV);
    530 	pti = pt_softc[minor(tp->t_dev)];
    531 
    532 	KASSERT(mutex_owned(&tty_lock));
    533 
    534 	if (ISSET(tp->t_state, TS_TTSTOP))
    535 		return;
    536 	if (pti->pt_flags & PF_STOPPED) {
    537 		pti->pt_flags &= ~PF_STOPPED;
    538 		pti->pt_send = TIOCPKT_START;
    539 	}
    540 
    541 	selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    542 	cv_broadcast(&tp->t_outcvf);
    543 }
    544 
    545 /*
    546  * Stop output.
    547  */
    548 void
    549 ptsstop(struct tty *tp, int flush)
    550 {
    551 	struct pt_softc *pti;
    552 
    553 	KASSERT(tp->t_dev != NODEV);
    554 	pti = pt_softc[minor(tp->t_dev)];
    555 
    556 	KASSERT(mutex_owned(&tty_lock));
    557 
    558 	/* note: FLUSHREAD and FLUSHWRITE already ok */
    559 	CTASSERT(TIOCPKT_FLUSHREAD == FREAD);
    560 	CTASSERT(TIOCPKT_FLUSHWRITE == FWRITE);
    561 	if (flush == 0) {
    562 		flush = TIOCPKT_STOP;
    563 		pti->pt_flags |= PF_STOPPED;
    564 	} else
    565 		pti->pt_flags &= ~PF_STOPPED;
    566 	pti->pt_send |= flush;
    567 
    568 	/* change of perspective */
    569 	if (flush & FREAD) {
    570 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    571 		cv_broadcast(&tp->t_rawcvf);
    572 	}
    573 	if (flush & FWRITE) {
    574 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    575 		cv_broadcast(&tp->t_outcvf);
    576 	}
    577 }
    578 
    579 void
    580 ptcwakeup(struct tty *tp, int flag)
    581 {
    582 	struct pt_softc *pti;
    583 
    584 	if (tp->t_dev == NODEV)
    585 		return;	/* client side not open yet */
    586 
    587 	pti = pt_softc[minor(tp->t_dev)];
    588 	KASSERT(pti != NULL);
    589 
    590 	mutex_spin_enter(&tty_lock);
    591 	if (flag & FREAD) {
    592 		selnotify(&pti->pt_selr, 0, NOTE_SUBMIT);
    593 		cv_broadcast(&tp->t_outcvf);
    594 	}
    595 	if (flag & FWRITE) {
    596 		selnotify(&pti->pt_selw, 0, NOTE_SUBMIT);
    597 		cv_broadcast(&tp->t_rawcvf);
    598 	}
    599 	mutex_spin_exit(&tty_lock);
    600 }
    601 
    602 /*ARGSUSED*/
    603 int
    604 ptcopen(dev_t dev, int flag, int devtype, struct lwp *l)
    605 {
    606 	struct pt_softc *pti;
    607 	struct tty *tp;
    608 	int error;
    609 	int ptn = minor(dev);
    610 
    611 	if ((error = pty_check(ptn)) != 0)
    612 		return error;
    613 
    614 	pti = pt_softc[ptn];
    615 	tp = pti->pt_tty;
    616 
    617 	mutex_spin_enter(&tty_lock);
    618 	if (tp->t_oproc) {
    619 		mutex_spin_exit(&tty_lock);
    620 		return EIO;
    621 	}
    622 	tp->t_dev = dev;
    623 	tp->t_oproc = ptsstart;
    624 	mutex_spin_exit(&tty_lock);
    625 	(void)(*tp->t_linesw->l_modem)(tp, 1);
    626 	CLR(tp->t_lflag, EXTPROC);
    627 	pti->pt_flags = 0;
    628 	pti->pt_send = 0;
    629 	pti->pt_ucntl = 0;
    630 	return 0;
    631 }
    632 
    633 /*ARGSUSED*/
    634 int
    635 ptcclose(dev_t dev, int flag, int devtype, struct lwp *l)
    636 {
    637 	struct pt_softc *pti = pt_softc[minor(dev)];
    638 	struct tty *tp = pti->pt_tty;
    639 
    640 	(void)(*tp->t_linesw->l_modem)(tp, 0);
    641 	mutex_spin_enter(&tty_lock);
    642 	CLR(tp->t_state, TS_CARR_ON);
    643 	tp->t_oproc = NULL;		/* mark closed */
    644 	mutex_spin_exit(&tty_lock);
    645 	return 0;
    646 }
    647 
    648 int
    649 ptcread(dev_t dev, struct uio *uio, int flag)
    650 {
    651 	struct pt_softc *pti = pt_softc[minor(dev)];
    652 	struct tty *tp = pti->pt_tty;
    653 	u_char bf[BUFSIZ];
    654 	int error = 0, cc;
    655 	int c;
    656 
    657 	if (uio->uio_resid <= 0)
    658 		return EINVAL;
    659 
    660 	/*
    661 	 * We want to block until the slave
    662 	 * is open, and there's something to read;
    663 	 * but if we lost the slave or we're NBIO,
    664 	 * then return the appropriate error instead.
    665 	 */
    666 	mutex_spin_enter(&tty_lock);
    667 	for (;;) {
    668 		if (ISSET(tp->t_state, TS_ISOPEN)) {
    669 			if (pti->pt_flags & PF_PKT && (c = pti->pt_send)) {
    670 				pti->pt_send = 0;
    671 				mutex_spin_exit(&tty_lock);
    672 				error = ureadc(c, uio);
    673 				if (error != 0)
    674 					return error;
    675 				/*
    676 				 * Since we don't have the tty locked, there's
    677 				 * a risk of messing up `t_termios'. This is
    678 				 * relevant only if the tty got closed and then
    679 				 * opened again while we were out uiomoving.
    680 				 */
    681 				if (c & TIOCPKT_IOCTL) {
    682 					cc = min(uio->uio_resid,
    683 						sizeof(tp->t_termios));
    684 					uiomove((void *) &tp->t_termios,
    685 						cc, uio);
    686 				}
    687 				return 0;
    688 			}
    689 			if (pti->pt_flags & PF_UCNTL && (c = pti->pt_ucntl)) {
    690 				pti->pt_ucntl = 0;
    691 				mutex_spin_exit(&tty_lock);
    692 				error = ureadc(c, uio);
    693 				if (error != 0)
    694 					return error;
    695 				return 0;
    696 			}
    697 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
    698 				break;
    699 		}
    700 		if (!ISSET(tp->t_state, TS_CARR_ON)) {
    701 			error = 0;	/* EOF */
    702 			goto out;
    703 		}
    704 		if (flag & IO_NDELAY) {
    705 			error = EWOULDBLOCK;
    706 			goto out;
    707 		}
    708 		error = cv_wait_sig(&tp->t_outcvf, &tty_lock);
    709 		if (error != 0)
    710 			goto out;
    711 	}
    712 
    713 	if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
    714 		mutex_spin_exit(&tty_lock);
    715 		error = ureadc(0, uio);
    716 		mutex_spin_enter(&tty_lock);
    717 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    718 			error = EIO;
    719 	}
    720 	while (uio->uio_resid > 0 && error == 0) {
    721 		cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
    722 		if (cc <= 0)
    723 			break;
    724 		mutex_spin_exit(&tty_lock);
    725 		error = uiomove(bf, cc, uio);
    726 		mutex_spin_enter(&tty_lock);
    727 		if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
    728 			error = EIO;
    729 	}
    730 	ttypull(tp);
    731 out:
    732 	mutex_spin_exit(&tty_lock);
    733 	return error;
    734 }
    735 
    736 
    737 int
    738 ptcwrite(dev_t dev, struct uio *uio, int flag)
    739 {
    740 	struct pt_softc *pti = pt_softc[minor(dev)];
    741 	struct tty *tp = pti->pt_tty;
    742 	u_char *cp = NULL;
    743 	int cc = 0;
    744 	u_char locbuf[BUFSIZ];
    745 	int cnt = 0;
    746 	int error = 0;
    747 
    748 again:
    749 	mutex_spin_enter(&tty_lock);
    750 	if (!ISSET(tp->t_state, TS_ISOPEN))
    751 		goto block;
    752 	if (pti->pt_flags & PF_REMOTE) {
    753 		if (tp->t_canq.c_cc)
    754 			goto block;
    755 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG) {
    756 			if (cc == 0) {
    757 				cc = min(uio->uio_resid, BUFSIZ);
    758 				cc = min(cc, TTYHOG - tp->t_canq.c_cc);
    759 				cp = locbuf;
    760 				mutex_spin_exit(&tty_lock);
    761 				error = uiomove(cp, cc, uio);
    762 				if (error != 0)
    763 					return error;
    764 				mutex_spin_enter(&tty_lock);
    765 				/* check again for safety */
    766 				if (!ISSET(tp->t_state, TS_ISOPEN)) {
    767 					/*
    768 					 * adjust for data copied in but not
    769 					 * written
    770 					 */
    771 					uio->uio_resid += cc;
    772 					error = EIO;
    773 					goto out;
    774 				}
    775 			}
    776 			if (cc) {
    777 				cc = b_to_q(cp, cc, &tp->t_outq);
    778 				if (cc > 0)
    779 					goto block;
    780 			}
    781 		}
    782 		(void) putc(0, &tp->t_canq);
    783 		ttwakeup(tp);
    784 		cv_broadcast(&tp->t_cancv);
    785 		error = 0;
    786 		goto out;
    787 	}
    788 	while (uio->uio_resid > 0) {
    789 		if (cc == 0) {
    790 			cc = min(uio->uio_resid, BUFSIZ);
    791 			cp = locbuf;
    792 			mutex_spin_exit(&tty_lock);
    793 			error = uiomove(cp, cc, uio);
    794 			if (error != 0)
    795 				return error;
    796 			mutex_spin_enter(&tty_lock);
    797 			/* check again for safety */
    798 			if (!ISSET(tp->t_state, TS_ISOPEN)) {
    799 				/* adjust for data copied in but not written */
    800 				uio->uio_resid += cc;
    801 				error = EIO;
    802 				goto out;
    803 			}
    804 		}
    805 		while (cc > 0) {
    806 			int used = tp->t_rawq.c_cc + tp->t_canq.c_cc;
    807 			int canon = ISSET(tp->t_lflag, ICANON) ? 1 : 0;
    808 			/*
    809 			 * We need space for 2 characters if canonical
    810 			 * because we might need to print ^C
    811 			 */
    812 			if (used >= (TTYHOG - canon) &&
    813 			   (tp->t_canq.c_cc > 0 || !canon)) {
    814 				cv_broadcast(&tp->t_rawcv);
    815 				goto block;
    816 			}
    817 			/*
    818 			 * XXX - should change l_rint to be called with lock
    819 			 *	 see also tty.c:ttyinput_wlock()
    820 			 */
    821 			mutex_spin_exit(&tty_lock);
    822 			(*tp->t_linesw->l_rint)(*cp++, tp);
    823 			mutex_spin_enter(&tty_lock);
    824 			cnt++;
    825 			cc--;
    826 		}
    827 	}
    828 	error = 0;
    829 	goto out;
    830 
    831 block:
    832 	/*
    833 	 * Come here to wait for slave to open, for space
    834 	 * in outq, or space in rawq.
    835 	 */
    836 	if (!ISSET(tp->t_state, TS_CARR_ON)) {
    837 		/* adjust for data copied in but not written */
    838 		uio->uio_resid += cc;
    839 		error = EIO;
    840 		goto out;
    841 	}
    842 	if (flag & IO_NDELAY) {
    843 		/* adjust for data copied in but not written */
    844 		uio->uio_resid += cc;
    845 		error = cnt == 0 ? EWOULDBLOCK : 0;
    846 		goto out;
    847 	}
    848 	error = cv_wait_sig(&tp->t_rawcvf, &tty_lock);
    849 	mutex_spin_exit(&tty_lock);
    850 	if (error != 0) {
    851 		/* adjust for data copied in but not written */
    852 		uio->uio_resid += cc;
    853 		return error;
    854 	}
    855 	goto again;
    856 
    857 out:
    858 	mutex_spin_exit(&tty_lock);
    859 	return error;
    860 }
    861 
    862 int
    863 ptcpoll(dev_t dev, int events, struct lwp *l)
    864 {
    865 	struct pt_softc *pti = pt_softc[minor(dev)];
    866 	struct tty *tp = pti->pt_tty;
    867 	int revents = 0;
    868 
    869 	mutex_spin_enter(&tty_lock);
    870 
    871 	if (events & (POLLIN | POLLRDNORM))
    872 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    873 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    874 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    875 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
    876 			revents |= events & (POLLIN | POLLRDNORM);
    877 
    878 	if (events & (POLLOUT | POLLWRNORM))
    879 		if (ISSET(tp->t_state, TS_ISOPEN) &&
    880 		    ((pti->pt_flags & PF_REMOTE) ?
    881 		     (tp->t_canq.c_cc == 0) :
    882 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    883 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
    884 			revents |= events & (POLLOUT | POLLWRNORM);
    885 
    886 	if (events & POLLHUP)
    887 		if (!ISSET(tp->t_state, TS_CARR_ON))
    888 			revents |= POLLHUP;
    889 
    890 	if (revents == 0) {
    891 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
    892 			selrecord(l, &pti->pt_selr);
    893 
    894 		if (events & (POLLOUT | POLLWRNORM))
    895 			selrecord(l, &pti->pt_selw);
    896 	}
    897 
    898 	mutex_spin_exit(&tty_lock);
    899 
    900 	return revents;
    901 }
    902 
    903 static void
    904 filt_ptcrdetach(struct knote *kn)
    905 {
    906 	struct pt_softc *pti;
    907 
    908 	pti = kn->kn_hook;
    909 
    910 	mutex_spin_enter(&tty_lock);
    911 	SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
    912 	mutex_spin_exit(&tty_lock);
    913 }
    914 
    915 static int
    916 filt_ptcread(struct knote *kn, long hint)
    917 {
    918 	struct pt_softc *pti;
    919 	struct tty	*tp;
    920 	int canread;
    921 
    922 	pti = kn->kn_hook;
    923 	tp = pti->pt_tty;
    924 
    925 	if ((hint & NOTE_SUBMIT) == 0) {
    926 		mutex_spin_enter(&tty_lock);
    927 	}
    928 
    929 	canread = (ISSET(tp->t_state, TS_ISOPEN) &&
    930 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
    931 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    932 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
    933 
    934 	if (canread) {
    935 		/*
    936 		 * c_cc is number of characters after output post-processing;
    937 		 * the amount of data actually read(2) depends on
    938 		 * setting of input flags for the terminal.
    939 		 */
    940 		kn->kn_data = tp->t_outq.c_cc;
    941 		if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
    942 		    ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
    943 			kn->kn_data++;
    944 	}
    945 
    946 	if ((hint & NOTE_SUBMIT) == 0) {
    947 		mutex_spin_exit(&tty_lock);
    948 	}
    949 
    950 	return canread;
    951 }
    952 
    953 static void
    954 filt_ptcwdetach(struct knote *kn)
    955 {
    956 	struct pt_softc *pti;
    957 
    958 	pti = kn->kn_hook;
    959 
    960 	mutex_spin_enter(&tty_lock);
    961 	SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
    962 	mutex_spin_exit(&tty_lock);
    963 }
    964 
    965 static int
    966 filt_ptcwrite(struct knote *kn, long hint)
    967 {
    968 	struct pt_softc *pti;
    969 	struct tty	*tp;
    970 	int canwrite;
    971 	int nwrite;
    972 
    973 	pti = kn->kn_hook;
    974 	tp = pti->pt_tty;
    975 
    976 	if ((hint & NOTE_SUBMIT) == 0) {
    977 		mutex_spin_enter(&tty_lock);
    978 	}
    979 
    980 	canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
    981 		    ((pti->pt_flags & PF_REMOTE) ?
    982 		     (tp->t_canq.c_cc == 0) :
    983 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
    984 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
    985 
    986 	if (canwrite) {
    987 		if (pti->pt_flags & PF_REMOTE)
    988 			nwrite = tp->t_canq.c_cn;
    989 		else {
    990 			/* this is guaranteed to be > 0 due to above check */
    991 			nwrite = tp->t_canq.c_cn
    992 				- (tp->t_rawq.c_cc + tp->t_canq.c_cc);
    993 		}
    994 		kn->kn_data = nwrite;
    995 	}
    996 
    997 	if ((hint & NOTE_SUBMIT) == 0) {
    998 		mutex_spin_exit(&tty_lock);
    999 	}
   1000 
   1001 	return canwrite;
   1002 }
   1003 
   1004 static const struct filterops ptcread_filtops =
   1005 	{ 1, NULL, filt_ptcrdetach, filt_ptcread };
   1006 static const struct filterops ptcwrite_filtops =
   1007 	{ 1, NULL, filt_ptcwdetach, filt_ptcwrite };
   1008 
   1009 int
   1010 ptckqfilter(dev_t dev, struct knote *kn)
   1011 {
   1012 	struct pt_softc *pti = pt_softc[minor(dev)];
   1013 	struct klist	*klist;
   1014 
   1015 	switch (kn->kn_filter) {
   1016 	case EVFILT_READ:
   1017 		klist = &pti->pt_selr.sel_klist;
   1018 		kn->kn_fop = &ptcread_filtops;
   1019 		break;
   1020 	case EVFILT_WRITE:
   1021 		klist = &pti->pt_selw.sel_klist;
   1022 		kn->kn_fop = &ptcwrite_filtops;
   1023 		break;
   1024 	default:
   1025 		return EINVAL;
   1026 	}
   1027 
   1028 	kn->kn_hook = pti;
   1029 
   1030 	mutex_spin_enter(&tty_lock);
   1031 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1032 	mutex_spin_exit(&tty_lock);
   1033 
   1034 	return 0;
   1035 }
   1036 
   1037 struct tty *
   1038 ptytty(dev_t dev)
   1039 {
   1040 	struct pt_softc *pti = pt_softc[minor(dev)];
   1041 	struct tty *tp = pti->pt_tty;
   1042 
   1043 	return tp;
   1044 }
   1045 
   1046 /*ARGSUSED*/
   1047 int
   1048 ptyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1049 {
   1050 /*
   1051  * XXX	We really should use device_lookup_acquire(...) to lock the
   1052  * XXX	device before fetching its softc pointer. Acquiring the
   1053  * XXX	cdevsw prevents the driver from being detached, but doesn't
   1054  * XXX	prevent the specific instance/unit from disappearing.
   1055  */
   1056 	struct pt_softc *pti = pt_softc[minor(dev)];
   1057 	struct tty *tp = pti->pt_tty;
   1058 	const struct cdevsw *cdev;
   1059 	u_char *cc = tp->t_cc;
   1060 	int stop, error, sig;
   1061 #ifndef NO_DEV_PTM
   1062 	struct mount *mp;
   1063 #endif
   1064 
   1065 	/*
   1066 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
   1067 	 * ttywflush(tp) will hang if there are characters in the outq.
   1068 	 */
   1069 	if (cmd == TIOCEXT) {
   1070 		/*
   1071 		 * When the EXTPROC bit is being toggled, we need
   1072 		 * to send an TIOCPKT_IOCTL if the packet driver
   1073 		 * is turned on.
   1074 		 */
   1075 		if (*(int *)data) {
   1076 			if (pti->pt_flags & PF_PKT) {
   1077 				pti->pt_send |= TIOCPKT_IOCTL;
   1078 				ptcwakeup(tp, FREAD);
   1079 			}
   1080 			SET(tp->t_lflag, EXTPROC);
   1081 		} else {
   1082 			if (ISSET(tp->t_lflag, EXTPROC) &&
   1083 			    (pti->pt_flags & PF_PKT)) {
   1084 				pti->pt_send |= TIOCPKT_IOCTL;
   1085 				ptcwakeup(tp, FREAD);
   1086 			}
   1087 			CLR(tp->t_lflag, EXTPROC);
   1088 		}
   1089 		return(0);
   1090 	}
   1091 
   1092 #ifndef NO_DEV_PTM
   1093 	/* Allow getting the name from either the master or the slave */
   1094 	if (cmd == TIOCPTSNAME) {
   1095 		if ((error = pty_getmp(l, &mp)) != 0)
   1096 			return error;
   1097 		return pty_fill_ptmget(l, dev, -1, -1, data, mp);
   1098 	}
   1099 #endif
   1100 
   1101 	cdev = cdevsw_lookup_acquire(dev);
   1102 	if (cdev != NULL && cdev->d_open == ptcopen) {
   1103 		switch (cmd) {
   1104 #ifndef NO_DEV_PTM
   1105 		case TIOCGRANTPT:
   1106 			if ((error = pty_getmp(l, &mp)) == 0)
   1107 				error = pty_grant_slave(l, dev, mp);
   1108 			cdevsw_release(cdev);
   1109 			return error;
   1110 #endif
   1111 
   1112 		case TIOCGPGRP:
   1113 			/*
   1114 			 * We avoid calling ttioctl on the controller since,
   1115 			 * in that case, tp must be the controlling terminal.
   1116 			 */
   1117 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
   1118 			cdevsw_release(cdev);
   1119 			return 0;
   1120 
   1121 		case TIOCPKT:
   1122 			if (*(int *)data) {
   1123 				if (pti->pt_flags & PF_UCNTL)
   1124 					return EINVAL;
   1125 				pti->pt_flags |= PF_PKT;
   1126 			} else
   1127 				pti->pt_flags &= ~PF_PKT;
   1128 			cdevsw_release(cdev);
   1129 			return 0;
   1130 
   1131 		case TIOCUCNTL:
   1132 			if (*(int *)data) {
   1133 				if (pti->pt_flags & PF_PKT)
   1134 					return EINVAL;
   1135 				pti->pt_flags |= PF_UCNTL;
   1136 			} else
   1137 				pti->pt_flags &= ~PF_UCNTL;
   1138 			cdevsw_release(cdev);
   1139 			return 0;
   1140 
   1141 		case TIOCREMOTE:
   1142 			if (*(int *)data)
   1143 				pti->pt_flags |= PF_REMOTE;
   1144 			else
   1145 				pti->pt_flags &= ~PF_REMOTE;
   1146 			mutex_spin_enter(&tty_lock);
   1147 			ttyflush(tp, FREAD|FWRITE);
   1148 			mutex_spin_exit(&tty_lock);
   1149 			cdevsw_release(cdev);
   1150 			return 0;
   1151 
   1152 		case TIOCSETP:
   1153 		case TIOCSETN:
   1154 		case TIOCSETD:
   1155 		case TIOCSETA:
   1156 		case TIOCSETAW:
   1157 		case TIOCSETAF:
   1158 			mutex_spin_enter(&tty_lock);
   1159 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
   1160 			mutex_spin_exit(&tty_lock);
   1161 			break;
   1162 
   1163 		case TIOCSIG:
   1164 			sig = (int)(long)*(void **)data;
   1165 			if (sig <= 0 || sig >= NSIG)
   1166 				return EINVAL;
   1167 			mutex_spin_enter(&tty_lock);
   1168 			if (!ISSET(tp->t_lflag, NOFLSH))
   1169 				ttyflush(tp, FREAD|FWRITE);
   1170 			tp->t_state |= TS_SIGINFO;
   1171 			ttysig(tp, TTYSIG_PG1, sig);
   1172 			mutex_spin_exit(&tty_lock);
   1173 			error = 0;
   1174 			cdevsw_release(cdev);
   1175 			return 0;
   1176 
   1177 		case FIONREAD:
   1178 			mutex_spin_enter(&tty_lock);
   1179 			*(int *)data = tp->t_outq.c_cc;
   1180 			mutex_spin_exit(&tty_lock);
   1181 			cdevsw_release(cdev);
   1182 			return 0;
   1183 		}
   1184 	}
   1185 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
   1186 	if (error == EPASSTHROUGH)
   1187 		 error = ttioctl(tp, cmd, data, flag, l);
   1188 	if (error == EPASSTHROUGH) {
   1189 		if (pti->pt_flags & PF_UCNTL &&
   1190 		    (cmd & ~0xff) == UIOCCMD(0)) {
   1191 			if (cmd & 0xff) {
   1192 				pti->pt_ucntl = (u_char)cmd;
   1193 				ptcwakeup(tp, FREAD);
   1194 			}
   1195 			cdevsw_release(cdev);
   1196 			return 0;
   1197 		}
   1198 	}
   1199 	/*
   1200 	 * If external processing and packet mode send ioctl packet.
   1201 	 */
   1202 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
   1203 		switch(cmd) {
   1204 		case TIOCSETA:
   1205 		case TIOCSETAW:
   1206 		case TIOCSETAF:
   1207 		case TIOCSETP:
   1208 		case TIOCSETN:
   1209 		case TIOCSETC:
   1210 		case TIOCSLTC:
   1211 		case TIOCLBIS:
   1212 		case TIOCLBIC:
   1213 		case TIOCLSET:
   1214 			pti->pt_send |= TIOCPKT_IOCTL;
   1215 			ptcwakeup(tp, FREAD);
   1216 		default:
   1217 			break;
   1218 		}
   1219 	}
   1220 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
   1221 		&& CCEQ(cc[VSTART], CTRL('q'));
   1222 	if (pti->pt_flags & PF_NOSTOP) {
   1223 		if (stop) {
   1224 			pti->pt_send &= ~TIOCPKT_NOSTOP;
   1225 			pti->pt_send |= TIOCPKT_DOSTOP;
   1226 			pti->pt_flags &= ~PF_NOSTOP;
   1227 			ptcwakeup(tp, FREAD);
   1228 		}
   1229 	} else {
   1230 		if (!stop) {
   1231 			pti->pt_send &= ~TIOCPKT_DOSTOP;
   1232 			pti->pt_send |= TIOCPKT_NOSTOP;
   1233 			pti->pt_flags |= PF_NOSTOP;
   1234 			ptcwakeup(tp, FREAD);
   1235 		}
   1236 	}
   1237 	cdevsw_release(cdev);
   1238 	return error;
   1239 }
   1240