xref: /netbsd-src/sys/kern/tty_pty.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: tty_pty.c,v 1.43 2000/03/30 09:27:13 augustss 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
36  */
37 
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two entries in 'cdevsw')
41  */
42 
43 #include "opt_compat_sunos.h"
44 
45 #include "pty.h"		/* XXX */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/ioctl.h>
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/file.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
56 #include <sys/signalvar.h>
57 #include <sys/uio.h>
58 #include <sys/conf.h>
59 #include <sys/poll.h>
60 
61 
62 
63 #if NPTY == 1
64 #undef NPTY
65 #define	NPTY	32		/* crude XXX */
66 #endif
67 
68 /* Macros to clear/set/test flags. */
69 #define	SET(t, f)	(t) |= (f)
70 #define	CLR(t, f)	(t) &= ~((unsigned)(f))
71 #define	ISSET(t, f)	((t) & (f))
72 
73 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
74 
75 /*
76  * pts == /dev/tty[pqrs]?
77  * ptc == /dev/pty[pqrs]?
78  */
79 struct	pt_softc {
80 	struct	tty *pt_tty;
81 	int	pt_flags;
82 	struct	selinfo pt_selr, pt_selw;
83 	u_char	pt_send;
84 	u_char	pt_ucntl;
85 } pt_softc[NPTY];		/* XXX */
86 int	npty = NPTY;		/* for pstat -t */
87 
88 #define	PF_PKT		0x08		/* packet mode */
89 #define	PF_STOPPED	0x10		/* user told stopped */
90 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
91 #define	PF_NOSTOP	0x40
92 #define PF_UCNTL	0x80		/* user control mode */
93 
94 void	ptyattach __P((int));
95 void	ptcwakeup __P((struct tty *, int));
96 struct tty *ptytty __P((dev_t));
97 void	ptsstart __P((struct tty *));
98 
99 /*
100  * Establish n (or default if n is 1) ptys in the system.
101  */
102 void
103 ptyattach(n)
104 	int n;
105 {
106 #ifdef notyet
107 #define	DEFAULT_NPTY	32
108 
109 	/* maybe should allow 0 => none? */
110 	if (n <= 1)
111 		n = DEFAULT_NPTY;
112 	pt_softc = malloc(n * sizeof(struct pt_softc), M_DEVBUF, M_WAITOK);
113 	npty = n;
114 #endif
115 }
116 
117 /*ARGSUSED*/
118 int
119 ptsopen(dev, flag, devtype, p)
120 	dev_t dev;
121 	int flag, devtype;
122 	struct proc *p;
123 {
124 	struct pt_softc *pti;
125 	struct tty *tp;
126 	int error;
127 
128 	if (minor(dev) >= npty)
129 		return (ENXIO);
130 	pti = &pt_softc[minor(dev)];
131 	if (!pti->pt_tty) {
132 		tp = pti->pt_tty = ttymalloc();
133 		tty_attach(tp);
134 	}
135 	else
136 		tp = pti->pt_tty;
137 	if (!ISSET(tp->t_state, TS_ISOPEN)) {
138 		ttychars(tp);		/* Set up default chars */
139 		tp->t_iflag = TTYDEF_IFLAG;
140 		tp->t_oflag = TTYDEF_OFLAG;
141 		tp->t_lflag = TTYDEF_LFLAG;
142 		tp->t_cflag = TTYDEF_CFLAG;
143 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
144 		ttsetwater(tp);		/* would be done in xxparam() */
145 	} else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
146 		return (EBUSY);
147 	if (tp->t_oproc)			/* Ctrlr still around. */
148 		SET(tp->t_state, TS_CARR_ON);
149 	if (!ISSET(flag, O_NONBLOCK))
150 		while (!ISSET(tp->t_state, TS_CARR_ON)) {
151 			tp->t_wopen++;
152 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
153 			    ttopen, 0);
154 			tp->t_wopen--;
155 			if (error)
156 				return (error);
157 		}
158 	error = (*linesw[tp->t_line].l_open)(dev, tp);
159 	ptcwakeup(tp, FREAD|FWRITE);
160 	return (error);
161 }
162 
163 int
164 ptsclose(dev, flag, mode, p)
165 	dev_t dev;
166 	int flag, mode;
167 	struct proc *p;
168 {
169 	struct pt_softc *pti = &pt_softc[minor(dev)];
170 	struct tty *tp = pti->pt_tty;
171 	int error;
172 
173 	error = (*linesw[tp->t_line].l_close)(tp, flag);
174 	error |= ttyclose(tp);
175 	ptcwakeup(tp, FREAD|FWRITE);
176 	return (error);
177 }
178 
179 int
180 ptsread(dev, uio, flag)
181 	dev_t dev;
182 	struct uio *uio;
183 	int flag;
184 {
185 	struct proc *p = curproc;
186 	struct pt_softc *pti = &pt_softc[minor(dev)];
187 	struct tty *tp = pti->pt_tty;
188 	int error = 0;
189 
190 again:
191 	if (pti->pt_flags & PF_REMOTE) {
192 		while (isbackground(p, tp)) {
193 			if (sigismember(&p->p_sigignore, SIGTTIN) ||
194 			    sigismember(&p->p_sigmask, SIGTTIN) ||
195 			    p->p_pgrp->pg_jobc == 0 ||
196 			    p->p_flag & P_PPWAIT)
197 				return (EIO);
198 			pgsignal(p->p_pgrp, SIGTTIN, 1);
199 			error = ttysleep(tp, (caddr_t)&lbolt,
200 					 TTIPRI | PCATCH, ttybg, 0);
201 			if (error)
202 				return (error);
203 		}
204 		if (tp->t_canq.c_cc == 0) {
205 			if (flag & IO_NDELAY)
206 				return (EWOULDBLOCK);
207 			error = ttysleep(tp, (caddr_t)&tp->t_canq,
208 					 TTIPRI | PCATCH, ttyin, 0);
209 			if (error)
210 				return (error);
211 			goto again;
212 		}
213 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
214 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
215 				error = EFAULT;
216 				break;
217 			}
218 		if (tp->t_canq.c_cc == 1)
219 			(void) getc(&tp->t_canq);
220 		if (tp->t_canq.c_cc)
221 			return (error);
222 	} else
223 		if (tp->t_oproc)
224 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
225 	ptcwakeup(tp, FWRITE);
226 	return (error);
227 }
228 
229 /*
230  * Write to pseudo-tty.
231  * Wakeups of controlling tty will happen
232  * indirectly, when tty driver calls ptsstart.
233  */
234 int
235 ptswrite(dev, uio, flag)
236 	dev_t dev;
237 	struct uio *uio;
238 	int flag;
239 {
240 	struct pt_softc *pti = &pt_softc[minor(dev)];
241 	struct tty *tp = pti->pt_tty;
242 
243 	if (tp->t_oproc == 0)
244 		return (EIO);
245 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
246 }
247 
248 /*
249  * Start output on pseudo-tty.
250  * Wake up process polling or sleeping for input from controlling tty.
251  */
252 void
253 ptsstart(tp)
254 	struct tty *tp;
255 {
256 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
257 
258 	if (ISSET(tp->t_state, TS_TTSTOP))
259 		return;
260 	if (pti->pt_flags & PF_STOPPED) {
261 		pti->pt_flags &= ~PF_STOPPED;
262 		pti->pt_send = TIOCPKT_START;
263 	}
264 	ptcwakeup(tp, FREAD);
265 }
266 
267 void
268 ptsstop(tp, flush)
269 	struct tty *tp;
270 	int flush;
271 {
272 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
273 	int flag;
274 
275 	/* note: FLUSHREAD and FLUSHWRITE already ok */
276 	if (flush == 0) {
277 		flush = TIOCPKT_STOP;
278 		pti->pt_flags |= PF_STOPPED;
279 	} else
280 		pti->pt_flags &= ~PF_STOPPED;
281 	pti->pt_send |= flush;
282 	/* change of perspective */
283 	flag = 0;
284 	if (flush & FREAD)
285 		flag |= FWRITE;
286 	if (flush & FWRITE)
287 		flag |= FREAD;
288 	ptcwakeup(tp, flag);
289 }
290 
291 void
292 ptcwakeup(tp, flag)
293 	struct tty *tp;
294 	int flag;
295 {
296 	struct pt_softc *pti = &pt_softc[minor(tp->t_dev)];
297 
298 	if (flag & FREAD) {
299 		selwakeup(&pti->pt_selr);
300 		wakeup((caddr_t)&tp->t_outq.c_cf);
301 	}
302 	if (flag & FWRITE) {
303 		selwakeup(&pti->pt_selw);
304 		wakeup((caddr_t)&tp->t_rawq.c_cf);
305 	}
306 }
307 
308 int ptcopen __P((dev_t, int, int, struct proc *));
309 
310 /*ARGSUSED*/
311 int
312 ptcopen(dev, flag, devtype, p)
313 	dev_t dev;
314 	int flag, devtype;
315 	struct proc *p;
316 {
317 	struct pt_softc *pti;
318 	struct tty *tp;
319 
320 	if (minor(dev) >= npty)
321 		return (ENXIO);
322 	pti = &pt_softc[minor(dev)];
323 	if (!pti->pt_tty) {
324 		tp = pti->pt_tty = ttymalloc();
325 		tty_attach(tp);
326 	}
327 	else
328 		tp = pti->pt_tty;
329 	if (tp->t_oproc)
330 		return (EIO);
331 	tp->t_oproc = ptsstart;
332 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
333 	CLR(tp->t_lflag, EXTPROC);
334 	pti->pt_flags = 0;
335 	pti->pt_send = 0;
336 	pti->pt_ucntl = 0;
337 	return (0);
338 }
339 
340 /*ARGSUSED*/
341 int
342 ptcclose(dev, flag, devtype, p)
343 	dev_t dev;
344 	int flag, devtype;
345 	struct proc *p;
346 {
347 	struct pt_softc *pti = &pt_softc[minor(dev)];
348 	struct tty *tp = pti->pt_tty;
349 
350 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
351 	CLR(tp->t_state, TS_CARR_ON);
352 	tp->t_oproc = 0;		/* mark closed */
353 	return (0);
354 }
355 
356 int
357 ptcread(dev, uio, flag)
358 	dev_t dev;
359 	struct uio *uio;
360 	int flag;
361 {
362 	struct pt_softc *pti = &pt_softc[minor(dev)];
363 	struct tty *tp = pti->pt_tty;
364 	char buf[BUFSIZ];
365 	int error = 0, cc;
366 
367 	/*
368 	 * We want to block until the slave
369 	 * is open, and there's something to read;
370 	 * but if we lost the slave or we're NBIO,
371 	 * then return the appropriate error instead.
372 	 */
373 	for (;;) {
374 		if (ISSET(tp->t_state, TS_ISOPEN)) {
375 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
376 				error = ureadc((int)pti->pt_send, uio);
377 				if (error)
378 					return (error);
379 				if (pti->pt_send & TIOCPKT_IOCTL) {
380 					cc = min(uio->uio_resid,
381 						sizeof(tp->t_termios));
382 					uiomove((caddr_t) &tp->t_termios,
383 						cc, uio);
384 				}
385 				pti->pt_send = 0;
386 				return (0);
387 			}
388 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
389 				error = ureadc((int)pti->pt_ucntl, uio);
390 				if (error)
391 					return (error);
392 				pti->pt_ucntl = 0;
393 				return (0);
394 			}
395 			if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
396 				break;
397 		}
398 		if (!ISSET(tp->t_state, TS_CARR_ON))
399 			return (0);	/* EOF */
400 		if (flag & IO_NDELAY)
401 			return (EWOULDBLOCK);
402 		error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
403 			       ttyin, 0);
404 		if (error)
405 			return (error);
406 	}
407 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
408 		error = ureadc(0, uio);
409 	while (uio->uio_resid > 0 && error == 0) {
410 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
411 		if (cc <= 0)
412 			break;
413 		error = uiomove(buf, cc, uio);
414 	}
415 	if (tp->t_outq.c_cc <= tp->t_lowat) {
416 		if (ISSET(tp->t_state, TS_ASLEEP)) {
417 			CLR(tp->t_state, TS_ASLEEP);
418 			wakeup((caddr_t)&tp->t_outq);
419 		}
420 		selwakeup(&tp->t_wsel);
421 	}
422 	return (error);
423 }
424 
425 
426 int
427 ptcwrite(dev, uio, flag)
428 	dev_t dev;
429 	struct uio *uio;
430 	int flag;
431 {
432 	struct pt_softc *pti = &pt_softc[minor(dev)];
433 	struct tty *tp = pti->pt_tty;
434 	u_char *cp = NULL;
435 	int cc = 0;
436 	u_char locbuf[BUFSIZ];
437 	int cnt = 0;
438 	int error = 0;
439 
440 again:
441 	if (!ISSET(tp->t_state, TS_ISOPEN))
442 		goto block;
443 	if (pti->pt_flags & PF_REMOTE) {
444 		if (tp->t_canq.c_cc)
445 			goto block;
446 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
447 			if (cc == 0) {
448 				cc = min(uio->uio_resid, BUFSIZ);
449 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
450 				cp = locbuf;
451 				error = uiomove((caddr_t)cp, cc, uio);
452 				if (error)
453 					return (error);
454 				/* check again for safety */
455 				if (!ISSET(tp->t_state, TS_ISOPEN))
456 					return (EIO);
457 			}
458 			if (cc)
459 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
460 			cc = 0;
461 		}
462 		(void) putc(0, &tp->t_canq);
463 		ttwakeup(tp);
464 		wakeup((caddr_t)&tp->t_canq);
465 		return (0);
466 	}
467 	while (uio->uio_resid > 0) {
468 		if (cc == 0) {
469 			cc = min(uio->uio_resid, BUFSIZ);
470 			cp = locbuf;
471 			error = uiomove((caddr_t)cp, cc, uio);
472 			if (error)
473 				return (error);
474 			/* check again for safety */
475 			if (!ISSET(tp->t_state, TS_ISOPEN))
476 				return (EIO);
477 		}
478 		while (cc > 0) {
479 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
480 			   (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
481 				wakeup((caddr_t)&tp->t_rawq);
482 				goto block;
483 			}
484 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
485 			cnt++;
486 			cc--;
487 		}
488 		cc = 0;
489 	}
490 	return (0);
491 block:
492 	/*
493 	 * Come here to wait for slave to open, for space
494 	 * in outq, or space in rawq.
495 	 */
496 	if (!ISSET(tp->t_state, TS_CARR_ON))
497 		return (EIO);
498 	if (flag & IO_NDELAY) {
499 		/* adjust for data copied in but not written */
500 		uio->uio_resid += cc;
501 		if (cnt == 0)
502 			return (EWOULDBLOCK);
503 		return (0);
504 	}
505 	error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
506 		       ttyout, 0);
507 	if (error) {
508 		/* adjust for data copied in but not written */
509 		uio->uio_resid += cc;
510 		return (error);
511 	}
512 	goto again;
513 }
514 
515 int
516 ptcpoll(dev, events, p)
517 	dev_t dev;
518 	int events;
519 	struct proc *p;
520 {
521 	struct pt_softc *pti = &pt_softc[minor(dev)];
522 	struct tty *tp = pti->pt_tty;
523 	int revents = 0;
524 	int s = splsoftclock();
525 
526 	if (events & (POLLIN | POLLRDNORM))
527 		if (ISSET(tp->t_state, TS_ISOPEN) &&
528 		    ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
529 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
530 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
531 			revents |= events & (POLLIN | POLLRDNORM);
532 
533 	if (events & (POLLOUT | POLLWRNORM))
534 		if (ISSET(tp->t_state, TS_ISOPEN) &&
535 		    ((pti->pt_flags & PF_REMOTE) ?
536 		     (tp->t_canq.c_cc == 0) :
537 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
538 		      (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
539 			revents |= events & (POLLOUT | POLLWRNORM);
540 
541 	if (events & POLLHUP)
542 		if (!ISSET(tp->t_state, TS_CARR_ON))
543 			revents |= POLLHUP;
544 
545 	if (revents == 0) {
546 		if (events & (POLLIN | POLLHUP | POLLRDNORM))
547 			selrecord(p, &pti->pt_selr);
548 
549 		if (events & (POLLOUT | POLLWRNORM))
550 			selrecord(p, &pti->pt_selw);
551 	}
552 
553 	splx(s);
554 	return (revents);
555 }
556 
557 
558 struct tty *
559 ptytty(dev)
560 	dev_t dev;
561 {
562 	struct pt_softc *pti = &pt_softc[minor(dev)];
563 	struct tty *tp = pti->pt_tty;
564 
565 	return (tp);
566 }
567 
568 /*ARGSUSED*/
569 int
570 ptyioctl(dev, cmd, data, flag, p)
571 	dev_t dev;
572 	u_long cmd;
573 	caddr_t data;
574 	int flag;
575 	struct proc *p;
576 {
577 	struct pt_softc *pti = &pt_softc[minor(dev)];
578 	struct tty *tp = pti->pt_tty;
579 	u_char *cc = tp->t_cc;
580 	int stop, error;
581 
582 	/*
583 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
584 	 * ttywflush(tp) will hang if there are characters in the outq.
585 	 */
586 	if (cmd == TIOCEXT) {
587 		/*
588 		 * When the EXTPROC bit is being toggled, we need
589 		 * to send an TIOCPKT_IOCTL if the packet driver
590 		 * is turned on.
591 		 */
592 		if (*(int *)data) {
593 			if (pti->pt_flags & PF_PKT) {
594 				pti->pt_send |= TIOCPKT_IOCTL;
595 				ptcwakeup(tp, FREAD);
596 			}
597 			SET(tp->t_lflag, EXTPROC);
598 		} else {
599 			if (ISSET(tp->t_lflag, EXTPROC) &&
600 			    (pti->pt_flags & PF_PKT)) {
601 				pti->pt_send |= TIOCPKT_IOCTL;
602 				ptcwakeup(tp, FREAD);
603 			}
604 			CLR(tp->t_lflag, EXTPROC);
605 		}
606 		return(0);
607 	} else
608 	if (cdevsw[major(dev)].d_open == ptcopen)
609 		switch (cmd) {
610 
611 		case TIOCGPGRP:
612 #ifdef COMPAT_SUNOS
613 			{
614 			/*
615 			 * I'm not sure about SunOS TIOCGPGRP semantics
616 			 * on PTYs, but it's something like this:
617 			 */
618 			extern struct emul emul_sunos;
619 			if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
620 				return (EIO);
621 			*(int *)data = tp->t_pgrp->pg_id;
622 			return (0);
623 			}
624 #endif
625 			/*
626 			 * We avoid calling ttioctl on the controller since,
627 			 * in that case, tp must be the controlling terminal.
628 			 */
629 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
630 			return (0);
631 
632 		case TIOCPKT:
633 			if (*(int *)data) {
634 				if (pti->pt_flags & PF_UCNTL)
635 					return (EINVAL);
636 				pti->pt_flags |= PF_PKT;
637 			} else
638 				pti->pt_flags &= ~PF_PKT;
639 			return (0);
640 
641 		case TIOCUCNTL:
642 			if (*(int *)data) {
643 				if (pti->pt_flags & PF_PKT)
644 					return (EINVAL);
645 				pti->pt_flags |= PF_UCNTL;
646 			} else
647 				pti->pt_flags &= ~PF_UCNTL;
648 			return (0);
649 
650 		case TIOCREMOTE:
651 			if (*(int *)data)
652 				pti->pt_flags |= PF_REMOTE;
653 			else
654 				pti->pt_flags &= ~PF_REMOTE;
655 			ttyflush(tp, FREAD|FWRITE);
656 			return (0);
657 
658 #ifdef COMPAT_OLDTTY
659 		case TIOCSETP:
660 		case TIOCSETN:
661 #endif
662 		case TIOCSETD:
663 		case TIOCSETA:
664 		case TIOCSETAW:
665 		case TIOCSETAF:
666 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
667 			break;
668 
669 		case TIOCSIG:
670 			if (*(unsigned int *)data >= NSIG)
671 				return(EINVAL);
672 			if (!ISSET(tp->t_lflag, NOFLSH))
673 				ttyflush(tp, FREAD|FWRITE);
674 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
675 			if ((*(unsigned int *)data == SIGINFO) &&
676 			    (!ISSET(tp->t_lflag, NOKERNINFO)))
677 				ttyinfo(tp);
678 			return(0);
679 		}
680 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
681 	if (error < 0)
682 		 error = ttioctl(tp, cmd, data, flag, p);
683 	if (error < 0) {
684 		if (pti->pt_flags & PF_UCNTL &&
685 		    (cmd & ~0xff) == UIOCCMD(0)) {
686 			if (cmd & 0xff) {
687 				pti->pt_ucntl = (u_char)cmd;
688 				ptcwakeup(tp, FREAD);
689 			}
690 			return (0);
691 		}
692 		error = ENOTTY;
693 	}
694 	/*
695 	 * If external processing and packet mode send ioctl packet.
696 	 */
697 	if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
698 		switch(cmd) {
699 		case TIOCSETA:
700 		case TIOCSETAW:
701 		case TIOCSETAF:
702 #ifdef COMPAT_OLDTTY
703 		case TIOCSETP:
704 		case TIOCSETN:
705 		case TIOCSETC:
706 		case TIOCSLTC:
707 		case TIOCLBIS:
708 		case TIOCLBIC:
709 		case TIOCLSET:
710 #endif
711 			pti->pt_send |= TIOCPKT_IOCTL;
712 			ptcwakeup(tp, FREAD);
713 		default:
714 			break;
715 		}
716 	}
717 	stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
718 		&& CCEQ(cc[VSTART], CTRL('q'));
719 	if (pti->pt_flags & PF_NOSTOP) {
720 		if (stop) {
721 			pti->pt_send &= ~TIOCPKT_NOSTOP;
722 			pti->pt_send |= TIOCPKT_DOSTOP;
723 			pti->pt_flags &= ~PF_NOSTOP;
724 			ptcwakeup(tp, FREAD);
725 		}
726 	} else {
727 		if (!stop) {
728 			pti->pt_send &= ~TIOCPKT_DOSTOP;
729 			pti->pt_send |= TIOCPKT_NOSTOP;
730 			pti->pt_flags |= PF_NOSTOP;
731 			ptcwakeup(tp, FREAD);
732 		}
733 	}
734 	return (error);
735 }
736