xref: /dragonfly/sys/kern/tty_pty.c (revision 38a690d7)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
34  * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
35  * $DragonFly: src/sys/kern/tty_pty.c,v 1.9 2003/08/07 21:17:23 dillon Exp $
36  */
37 
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two entries in 'cdevsw')
41  */
42 #include "use_pty.h"		/* XXX */
43 #include "opt_compat.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
49 #endif
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/poll.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
59 #include <sys/device.h>
60 
61 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62 
63 static void ptsstart __P((struct tty *tp));
64 static void ptsstop __P((struct tty *tp, int rw));
65 static void ptcwakeup __P((struct tty *tp, int flag));
66 static void ptyinit __P((int n));
67 
68 static	d_open_t	ptsopen;
69 static	d_close_t	ptsclose;
70 static	d_read_t	ptsread;
71 static	d_write_t	ptswrite;
72 static	d_ioctl_t	ptyioctl;
73 static	d_open_t	ptcopen;
74 static	d_close_t	ptcclose;
75 static	d_read_t	ptcread;
76 static	d_write_t	ptcwrite;
77 static	d_poll_t	ptcpoll;
78 
79 #define	CDEV_MAJOR_S	5
80 static struct cdevsw pts_cdevsw = {
81 	/* name */	"pts",
82 	/* maj */	CDEV_MAJOR_S,
83 	/* flags */	D_TTY | D_KQFILTER,
84 	/* port */	NULL,
85 	/* autoq */	0,
86 
87 	/* open */	ptsopen,
88 	/* close */	ptsclose,
89 	/* read */	ptsread,
90 	/* write */	ptswrite,
91 	/* ioctl */	ptyioctl,
92 	/* poll */	ttypoll,
93 	/* mmap */	nommap,
94 	/* strategy */	nostrategy,
95 	/* dump */	nodump,
96 	/* psize */	nopsize,
97 	/* kqfilter */	ttykqfilter
98 };
99 
100 #define	CDEV_MAJOR_C	6
101 static struct cdevsw ptc_cdevsw = {
102 	/* name */	"ptc",
103 	/* maj */	CDEV_MAJOR_C,
104 	/* flags */	D_TTY | D_KQFILTER | D_MASTER,
105 	/* port */	NULL,
106 	/* autoq */	0,
107 
108 	/* open */	ptcopen,
109 	/* close */	ptcclose,
110 	/* read */	ptcread,
111 	/* write */	ptcwrite,
112 	/* ioctl */	ptyioctl,
113 	/* poll */	ptcpoll,
114 	/* mmap */	nommap,
115 	/* strategy */	nostrategy,
116 	/* dump */	nodump,
117 	/* psize */	nopsize,
118 	/* kqfilter */	ttykqfilter
119 };
120 
121 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
122 
123 struct	pt_ioctl {
124 	int	pt_flags;
125 	struct	selinfo pt_selr, pt_selw;
126 	u_char	pt_send;
127 	u_char	pt_ucntl;
128 	struct tty pt_tty;
129 	dev_t	devs, devc;
130 	struct	prison *pt_prison;
131 };
132 
133 #define	PF_PKT		0x08		/* packet mode */
134 #define	PF_STOPPED	0x10		/* user told stopped */
135 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
136 #define	PF_NOSTOP	0x40
137 #define PF_UCNTL	0x80		/* user control mode */
138 
139 /*
140  * This function creates and initializes a pts/ptc pair
141  *
142  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
143  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
144  *
145  * XXX: define and add mapping of upper minor bits to allow more
146  *      than 256 ptys.
147  */
148 static void
149 ptyinit(n)
150 	int n;
151 {
152 	dev_t devs, devc;
153 	char *names = "pqrsPQRS";
154 	struct pt_ioctl *pt;
155 
156 	/* For now we only map the lower 8 bits of the minor */
157 	if (n & ~0xff)
158 		return;
159 
160 	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK);
161 	bzero(pt, sizeof(*pt));
162 	pt->devs = devs = make_dev(&pts_cdevsw, n,
163 	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
164 	pt->devc = devc = make_dev(&ptc_cdevsw, n,
165 	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
166 
167 	devs->si_drv1 = devc->si_drv1 = pt;
168 	devs->si_tty = devc->si_tty = &pt->pt_tty;
169 	pt->pt_tty.t_dev = devs;
170 	ttyregister(&pt->pt_tty);
171 }
172 
173 /*ARGSUSED*/
174 static	int
175 ptsopen(dev_t dev, int flag, int devtype, struct thread *td)
176 {
177 	struct tty *tp;
178 	int error;
179 	int minr;
180 	dev_t nextdev;
181 	struct pt_ioctl *pti;
182 	struct proc *p;
183 
184 	p = td->td_proc;
185 	KKASSERT(p != NULL);
186 
187 	/*
188 	 * XXX: Gross hack for DEVFS:
189 	 * XXX: DEVFS is no more, should this be removed?
190 	 * If we openned this device, ensure we have the
191 	 * next one too, so people can open it.
192 	 */
193 	minr = lminor(dev);
194 	if (minr < 255) {
195 		nextdev = makedev(major(dev), minr + 1);
196 		if (!nextdev->si_drv1) {
197 			ptyinit(minr + 1);
198 		}
199 	}
200 	if (!dev->si_drv1)
201 		ptyinit(minor(dev));
202 	if (!dev->si_drv1)
203 		return(ENXIO);
204 	pti = dev->si_drv1;
205 	tp = dev->si_tty;
206 	if ((tp->t_state & TS_ISOPEN) == 0) {
207 		ttychars(tp);		/* Set up default chars */
208 		tp->t_iflag = TTYDEF_IFLAG;
209 		tp->t_oflag = TTYDEF_OFLAG;
210 		tp->t_lflag = TTYDEF_LFLAG;
211 		tp->t_cflag = TTYDEF_CFLAG;
212 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
213 	} else if (tp->t_state & TS_XCLUDE && suser(td)) {
214 		return (EBUSY);
215 	} else if (pti->pt_prison != p->p_ucred->cr_prison) {
216 		return (EBUSY);
217 	}
218 	if (tp->t_oproc)			/* Ctrlr still around. */
219 		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
220 	while ((tp->t_state & TS_CARR_ON) == 0) {
221 		if (flag&FNONBLOCK)
222 			break;
223 		error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
224 		if (error)
225 			return (error);
226 	}
227 	error = (*linesw[tp->t_line].l_open)(dev, tp);
228 	if (error == 0)
229 		ptcwakeup(tp, FREAD|FWRITE);
230 	return (error);
231 }
232 
233 static	int
234 ptsclose(dev, flag, mode, td)
235 	dev_t dev;
236 	int flag, mode;
237 	struct thread *td;
238 {
239 	struct tty *tp;
240 	int err;
241 
242 	tp = dev->si_tty;
243 	err = (*linesw[tp->t_line].l_close)(tp, flag);
244 	ptsstop(tp, FREAD|FWRITE);
245 	(void) ttyclose(tp);
246 	return (err);
247 }
248 
249 static	int
250 ptsread(dev, uio, flag)
251 	dev_t dev;
252 	struct uio *uio;
253 	int flag;
254 {
255 	struct proc *p = curproc;
256 	struct tty *tp = dev->si_tty;
257 	struct pt_ioctl *pti = dev->si_drv1;
258 	int error = 0;
259 
260 again:
261 	if (pti->pt_flags & PF_REMOTE) {
262 		while (isbackground(p, tp)) {
263 			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
264 			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
265 			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
266 				return (EIO);
267 			pgsignal(p->p_pgrp, SIGTTIN, 1);
268 			error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
269 			if (error)
270 				return (error);
271 		}
272 		if (tp->t_canq.c_cc == 0) {
273 			if (flag & IO_NDELAY)
274 				return (EWOULDBLOCK);
275 			error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
276 					 "ptsin", 0);
277 			if (error)
278 				return (error);
279 			goto again;
280 		}
281 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
282 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
283 				error = EFAULT;
284 				break;
285 			}
286 		if (tp->t_canq.c_cc == 1)
287 			(void) getc(&tp->t_canq);
288 		if (tp->t_canq.c_cc)
289 			return (error);
290 	} else
291 		if (tp->t_oproc)
292 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
293 	ptcwakeup(tp, FWRITE);
294 	return (error);
295 }
296 
297 /*
298  * Write to pseudo-tty.
299  * Wakeups of controlling tty will happen
300  * indirectly, when tty driver calls ptsstart.
301  */
302 static	int
303 ptswrite(dev, uio, flag)
304 	dev_t dev;
305 	struct uio *uio;
306 	int flag;
307 {
308 	struct tty *tp;
309 
310 	tp = dev->si_tty;
311 	if (tp->t_oproc == 0)
312 		return (EIO);
313 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
314 }
315 
316 /*
317  * Start output on pseudo-tty.
318  * Wake up process selecting or sleeping for input from controlling tty.
319  */
320 static void
321 ptsstart(tp)
322 	struct tty *tp;
323 {
324 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
325 
326 	if (tp->t_state & TS_TTSTOP)
327 		return;
328 	if (pti->pt_flags & PF_STOPPED) {
329 		pti->pt_flags &= ~PF_STOPPED;
330 		pti->pt_send = TIOCPKT_START;
331 	}
332 	ptcwakeup(tp, FREAD);
333 }
334 
335 static void
336 ptcwakeup(tp, flag)
337 	struct tty *tp;
338 	int flag;
339 {
340 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
341 
342 	if (flag & FREAD) {
343 		selwakeup(&pti->pt_selr);
344 		wakeup(TSA_PTC_READ(tp));
345 	}
346 	if (flag & FWRITE) {
347 		selwakeup(&pti->pt_selw);
348 		wakeup(TSA_PTC_WRITE(tp));
349 	}
350 }
351 
352 static	int
353 ptcopen(dev, flag, devtype, td)
354 	dev_t dev;
355 	int flag, devtype;
356 	struct thread *td;
357 {
358 	struct tty *tp;
359 	struct pt_ioctl *pti;
360 
361 	if (!dev->si_drv1)
362 		ptyinit(minor(dev));
363 	if (!dev->si_drv1)
364 		return(ENXIO);
365 	tp = dev->si_tty;
366 	if (tp->t_oproc)
367 		return (EIO);
368 	KKASSERT(td->td_proc != NULL);
369 	tp->t_oproc = ptsstart;
370 	tp->t_stop = ptsstop;
371 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
372 	tp->t_lflag &= ~EXTPROC;
373 	pti = dev->si_drv1;
374 	pti->pt_prison = td->td_proc->p_ucred->cr_prison;
375 	pti->pt_flags = 0;
376 	pti->pt_send = 0;
377 	pti->pt_ucntl = 0;
378 	return (0);
379 }
380 
381 static	int
382 ptcclose(dev, flags, fmt, td)
383 	dev_t dev;
384 	int flags;
385 	int fmt;
386 	struct thread *td;
387 {
388 	struct tty *tp;
389 
390 	tp = dev->si_tty;
391 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
392 
393 	/*
394 	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
395 	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
396 	 * l_modem()s that ignore carrier drop make no sense for ptys but
397 	 * may be in use because other parts of the line discipline make
398 	 * sense for ptys.  Recover by doing everything that a normal
399 	 * ttymodem() would have done except for sending a SIGHUP.
400 	 */
401 	if (tp->t_state & TS_ISOPEN) {
402 		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
403 		tp->t_state |= TS_ZOMBIE;
404 		ttyflush(tp, FREAD | FWRITE);
405 	}
406 
407 	tp->t_oproc = 0;		/* mark closed */
408 	return (0);
409 }
410 
411 static	int
412 ptcread(dev, uio, flag)
413 	dev_t dev;
414 	struct uio *uio;
415 	int flag;
416 {
417 	struct tty *tp = dev->si_tty;
418 	struct pt_ioctl *pti = dev->si_drv1;
419 	char buf[BUFSIZ];
420 	int error = 0, cc;
421 
422 	/*
423 	 * We want to block until the slave
424 	 * is open, and there's something to read;
425 	 * but if we lost the slave or we're NBIO,
426 	 * then return the appropriate error instead.
427 	 */
428 	for (;;) {
429 		if (tp->t_state&TS_ISOPEN) {
430 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
431 				error = ureadc((int)pti->pt_send, uio);
432 				if (error)
433 					return (error);
434 				if (pti->pt_send & TIOCPKT_IOCTL) {
435 					cc = min(uio->uio_resid,
436 						sizeof(tp->t_termios));
437 					uiomove((caddr_t)&tp->t_termios, cc,
438 						uio);
439 				}
440 				pti->pt_send = 0;
441 				return (0);
442 			}
443 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
444 				error = ureadc((int)pti->pt_ucntl, uio);
445 				if (error)
446 					return (error);
447 				pti->pt_ucntl = 0;
448 				return (0);
449 			}
450 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
451 				break;
452 		}
453 		if ((tp->t_state & TS_CONNECTED) == 0)
454 			return (0);	/* EOF */
455 		if (flag & IO_NDELAY)
456 			return (EWOULDBLOCK);
457 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
458 		if (error)
459 			return (error);
460 	}
461 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
462 		error = ureadc(0, uio);
463 	while (uio->uio_resid > 0 && error == 0) {
464 		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
465 		if (cc <= 0)
466 			break;
467 		error = uiomove(buf, cc, uio);
468 	}
469 	ttwwakeup(tp);
470 	return (error);
471 }
472 
473 static	void
474 ptsstop(tp, flush)
475 	struct tty *tp;
476 	int flush;
477 {
478 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
479 	int flag;
480 
481 	/* note: FLUSHREAD and FLUSHWRITE already ok */
482 	if (flush == 0) {
483 		flush = TIOCPKT_STOP;
484 		pti->pt_flags |= PF_STOPPED;
485 	} else
486 		pti->pt_flags &= ~PF_STOPPED;
487 	pti->pt_send |= flush;
488 	/* change of perspective */
489 	flag = 0;
490 	if (flush & FREAD)
491 		flag |= FWRITE;
492 	if (flush & FWRITE)
493 		flag |= FREAD;
494 	ptcwakeup(tp, flag);
495 }
496 
497 static	int
498 ptcpoll(dev, events, td)
499 	dev_t dev;
500 	int events;
501 	struct thread *td;
502 {
503 	struct tty *tp = dev->si_tty;
504 	struct pt_ioctl *pti = dev->si_drv1;
505 	int revents = 0;
506 	int s;
507 
508 	if ((tp->t_state & TS_CONNECTED) == 0)
509 		return (seltrue(dev, events, td) | POLLHUP);
510 
511 	/*
512 	 * Need to block timeouts (ttrstart).
513 	 */
514 	s = spltty();
515 
516 	if (events & (POLLIN | POLLRDNORM))
517 		if ((tp->t_state & TS_ISOPEN) &&
518 		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
519 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
520 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
521 			revents |= events & (POLLIN | POLLRDNORM);
522 
523 	if (events & (POLLOUT | POLLWRNORM))
524 		if (tp->t_state & TS_ISOPEN &&
525 		    ((pti->pt_flags & PF_REMOTE) ?
526 		     (tp->t_canq.c_cc == 0) :
527 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
528 		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
529 			revents |= events & (POLLOUT | POLLWRNORM);
530 
531 	if (events & POLLHUP)
532 		if ((tp->t_state & TS_CARR_ON) == 0)
533 			revents |= POLLHUP;
534 
535 	if (revents == 0) {
536 		if (events & (POLLIN | POLLRDNORM))
537 			selrecord(td, &pti->pt_selr);
538 
539 		if (events & (POLLOUT | POLLWRNORM))
540 			selrecord(td, &pti->pt_selw);
541 	}
542 	splx(s);
543 
544 	return (revents);
545 }
546 
547 static	int
548 ptcwrite(dev, uio, flag)
549 	dev_t dev;
550 	struct uio *uio;
551 	int flag;
552 {
553 	struct tty *tp = dev->si_tty;
554 	u_char *cp = 0;
555 	int cc = 0;
556 	u_char locbuf[BUFSIZ];
557 	int cnt = 0;
558 	struct pt_ioctl *pti = dev->si_drv1;
559 	int error = 0;
560 
561 again:
562 	if ((tp->t_state&TS_ISOPEN) == 0)
563 		goto block;
564 	if (pti->pt_flags & PF_REMOTE) {
565 		if (tp->t_canq.c_cc)
566 			goto block;
567 		while ((uio->uio_resid > 0 || cc > 0) &&
568 		       tp->t_canq.c_cc < TTYHOG - 1) {
569 			if (cc == 0) {
570 				cc = min(uio->uio_resid, BUFSIZ);
571 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
572 				cp = locbuf;
573 				error = uiomove((caddr_t)cp, cc, uio);
574 				if (error)
575 					return (error);
576 				/* check again for safety */
577 				if ((tp->t_state & TS_ISOPEN) == 0) {
578 					/* adjust as usual */
579 					uio->uio_resid += cc;
580 					return (EIO);
581 				}
582 			}
583 			if (cc > 0) {
584 				cc = b_to_q((char *)cp, cc, &tp->t_canq);
585 				/*
586 				 * XXX we don't guarantee that the canq size
587 				 * is >= TTYHOG, so the above b_to_q() may
588 				 * leave some bytes uncopied.  However, space
589 				 * is guaranteed for the null terminator if
590 				 * we don't fail here since (TTYHOG - 1) is
591 				 * not a multiple of CBSIZE.
592 				 */
593 				if (cc > 0)
594 					break;
595 			}
596 		}
597 		/* adjust for data copied in but not written */
598 		uio->uio_resid += cc;
599 		(void) putc(0, &tp->t_canq);
600 		ttwakeup(tp);
601 		wakeup(TSA_PTS_READ(tp));
602 		return (0);
603 	}
604 	while (uio->uio_resid > 0 || cc > 0) {
605 		if (cc == 0) {
606 			cc = min(uio->uio_resid, BUFSIZ);
607 			cp = locbuf;
608 			error = uiomove((caddr_t)cp, cc, uio);
609 			if (error)
610 				return (error);
611 			/* check again for safety */
612 			if ((tp->t_state & TS_ISOPEN) == 0) {
613 				/* adjust for data copied in but not written */
614 				uio->uio_resid += cc;
615 				return (EIO);
616 			}
617 		}
618 		while (cc > 0) {
619 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
620 			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
621 				wakeup(TSA_HUP_OR_INPUT(tp));
622 				goto block;
623 			}
624 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
625 			cnt++;
626 			cc--;
627 		}
628 		cc = 0;
629 	}
630 	return (0);
631 block:
632 	/*
633 	 * Come here to wait for slave to open, for space
634 	 * in outq, or space in rawq, or an empty canq.
635 	 */
636 	if ((tp->t_state & TS_CONNECTED) == 0) {
637 		/* adjust for data copied in but not written */
638 		uio->uio_resid += cc;
639 		return (EIO);
640 	}
641 	if (flag & IO_NDELAY) {
642 		/* adjust for data copied in but not written */
643 		uio->uio_resid += cc;
644 		if (cnt == 0)
645 			return (EWOULDBLOCK);
646 		return (0);
647 	}
648 	error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
649 	if (error) {
650 		/* adjust for data copied in but not written */
651 		uio->uio_resid += cc;
652 		return (error);
653 	}
654 	goto again;
655 }
656 
657 /*ARGSUSED*/
658 static	int
659 ptyioctl(dev, cmd, data, flag, td)
660 	dev_t dev;
661 	u_long cmd;
662 	caddr_t data;
663 	int flag;
664 	struct thread *td;
665 {
666 	struct tty *tp = dev->si_tty;
667 	struct pt_ioctl *pti = dev->si_drv1;
668 	u_char *cc = tp->t_cc;
669 	int stop, error;
670 
671 	if (dev_dflags(dev) & D_MASTER) {
672 		switch (cmd) {
673 
674 		case TIOCGPGRP:
675 			/*
676 			 * We avoid calling ttioctl on the controller since,
677 			 * in that case, tp must be the controlling terminal.
678 			 */
679 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
680 			return (0);
681 
682 		case TIOCPKT:
683 			if (*(int *)data) {
684 				if (pti->pt_flags & PF_UCNTL)
685 					return (EINVAL);
686 				pti->pt_flags |= PF_PKT;
687 			} else
688 				pti->pt_flags &= ~PF_PKT;
689 			return (0);
690 
691 		case TIOCUCNTL:
692 			if (*(int *)data) {
693 				if (pti->pt_flags & PF_PKT)
694 					return (EINVAL);
695 				pti->pt_flags |= PF_UCNTL;
696 			} else
697 				pti->pt_flags &= ~PF_UCNTL;
698 			return (0);
699 
700 		case TIOCREMOTE:
701 			if (*(int *)data)
702 				pti->pt_flags |= PF_REMOTE;
703 			else
704 				pti->pt_flags &= ~PF_REMOTE;
705 			ttyflush(tp, FREAD|FWRITE);
706 			return (0);
707 		}
708 
709 		/*
710 		 * The rest of the ioctls shouldn't be called until
711 		 * the slave is open.
712 		 */
713 		if ((tp->t_state & TS_ISOPEN) == 0)
714 			return (EAGAIN);
715 
716 		switch (cmd) {
717 #ifdef COMPAT_43
718 		case TIOCSETP:
719 		case TIOCSETN:
720 #endif
721 		case TIOCSETD:
722 		case TIOCSETA:
723 		case TIOCSETAW:
724 		case TIOCSETAF:
725 			/*
726 			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
727 			 * ttywflush(tp) will hang if there are characters in
728 			 * the outq.
729 			 */
730 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
731 			break;
732 
733 		case TIOCSIG:
734 			if (*(unsigned int *)data >= NSIG ||
735 			    *(unsigned int *)data == 0)
736 				return(EINVAL);
737 			if ((tp->t_lflag&NOFLSH) == 0)
738 				ttyflush(tp, FREAD|FWRITE);
739 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
740 			if ((*(unsigned int *)data == SIGINFO) &&
741 			    ((tp->t_lflag&NOKERNINFO) == 0))
742 				ttyinfo(tp);
743 			return(0);
744 		}
745 	}
746 	if (cmd == TIOCEXT) {
747 		/*
748 		 * When the EXTPROC bit is being toggled, we need
749 		 * to send an TIOCPKT_IOCTL if the packet driver
750 		 * is turned on.
751 		 */
752 		if (*(int *)data) {
753 			if (pti->pt_flags & PF_PKT) {
754 				pti->pt_send |= TIOCPKT_IOCTL;
755 				ptcwakeup(tp, FREAD);
756 			}
757 			tp->t_lflag |= EXTPROC;
758 		} else {
759 			if ((tp->t_lflag & EXTPROC) &&
760 			    (pti->pt_flags & PF_PKT)) {
761 				pti->pt_send |= TIOCPKT_IOCTL;
762 				ptcwakeup(tp, FREAD);
763 			}
764 			tp->t_lflag &= ~EXTPROC;
765 		}
766 		return(0);
767 	}
768 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
769 	if (error == ENOIOCTL)
770 		 error = ttioctl(tp, cmd, data, flag);
771 	if (error == ENOIOCTL) {
772 		if (pti->pt_flags & PF_UCNTL &&
773 		    (cmd & ~0xff) == UIOCCMD(0)) {
774 			if (cmd & 0xff) {
775 				pti->pt_ucntl = (u_char)cmd;
776 				ptcwakeup(tp, FREAD);
777 			}
778 			return (0);
779 		}
780 		error = ENOTTY;
781 	}
782 	/*
783 	 * If external processing and packet mode send ioctl packet.
784 	 */
785 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
786 		switch(cmd) {
787 		case TIOCSETA:
788 		case TIOCSETAW:
789 		case TIOCSETAF:
790 #ifdef COMPAT_43
791 		case TIOCSETP:
792 		case TIOCSETN:
793 #endif
794 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
795 		case TIOCSETC:
796 		case TIOCSLTC:
797 		case TIOCLBIS:
798 		case TIOCLBIC:
799 		case TIOCLSET:
800 #endif
801 			pti->pt_send |= TIOCPKT_IOCTL;
802 			ptcwakeup(tp, FREAD);
803 		default:
804 			break;
805 		}
806 	}
807 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
808 		&& CCEQ(cc[VSTART], CTRL('q'));
809 	if (pti->pt_flags & PF_NOSTOP) {
810 		if (stop) {
811 			pti->pt_send &= ~TIOCPKT_NOSTOP;
812 			pti->pt_send |= TIOCPKT_DOSTOP;
813 			pti->pt_flags &= ~PF_NOSTOP;
814 			ptcwakeup(tp, FREAD);
815 		}
816 	} else {
817 		if (!stop) {
818 			pti->pt_send &= ~TIOCPKT_DOSTOP;
819 			pti->pt_send |= TIOCPKT_NOSTOP;
820 			pti->pt_flags |= PF_NOSTOP;
821 			ptcwakeup(tp, FREAD);
822 		}
823 	}
824 	return (error);
825 }
826 
827 
828 static void ptc_drvinit __P((void *unused));
829 
830 static void
831 ptc_drvinit(unused)
832 	void *unused;
833 {
834 	cdevsw_add(&pts_cdevsw);
835 	cdevsw_add(&ptc_cdevsw);
836 	/* XXX: Gross hack for DEVFS */
837 	/* XXX: DEVFS is no more, should this be removed? */
838 	ptyinit(0);
839 }
840 
841 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
842