xref: /dragonfly/sys/kern/tty_pty.c (revision 99dd49c5)
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.21 2008/08/13 10:29:38 swildner Exp $
36  */
37 
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two dev_ops structures)
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/priv.h>
52 #include <sys/tty.h>
53 #include <sys/conf.h>
54 #include <sys/fcntl.h>
55 #include <sys/poll.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/thread2.h>
62 
63 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
64 
65 static void ptsstart (struct tty *tp);
66 static void ptsstop (struct tty *tp, int rw);
67 static void ptcwakeup (struct tty *tp, int flag);
68 static void ptyinit (int n);
69 
70 static	d_open_t	ptsopen;
71 static	d_close_t	ptsclose;
72 static	d_read_t	ptsread;
73 static	d_write_t	ptswrite;
74 static	d_ioctl_t	ptyioctl;
75 static	d_open_t	ptcopen;
76 static	d_close_t	ptcclose;
77 static	d_read_t	ptcread;
78 static	d_write_t	ptcwrite;
79 static	d_poll_t	ptcpoll;
80 
81 #define	CDEV_MAJOR_S	5
82 static struct dev_ops pts_ops = {
83 	{ "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
84 	.d_open =	ptsopen,
85 	.d_close =	ptsclose,
86 	.d_read =	ptsread,
87 	.d_write =	ptswrite,
88 	.d_ioctl =	ptyioctl,
89 	.d_poll =	ttypoll,
90 	.d_kqfilter =	ttykqfilter
91 };
92 
93 #define	CDEV_MAJOR_C	6
94 static struct dev_ops ptc_ops = {
95 	{ "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
96 	.d_open =	ptcopen,
97 	.d_close =	ptcclose,
98 	.d_read =	ptcread,
99 	.d_write =	ptcwrite,
100 	.d_ioctl =	ptyioctl,
101 	.d_poll =	ptcpoll,
102 	.d_kqfilter =	ttykqfilter
103 };
104 
105 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
106 
107 struct	pt_ioctl {
108 	int	pt_flags;
109 	struct	selinfo pt_selr, pt_selw;
110 	u_char	pt_send;
111 	u_char	pt_ucntl;
112 	struct tty pt_tty;
113 	cdev_t	devs, devc;
114 	struct	prison *pt_prison;
115 };
116 
117 #define	PF_PKT		0x08		/* packet mode */
118 #define	PF_STOPPED	0x10		/* user told stopped */
119 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
120 #define	PF_NOSTOP	0x40
121 #define PF_UCNTL	0x80		/* user control mode */
122 
123 /*
124  * This function creates and initializes a pts/ptc pair
125  *
126  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
127  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
128  *
129  * XXX: define and add mapping of upper minor bits to allow more
130  *      than 256 ptys.
131  */
132 static void
133 ptyinit(int n)
134 {
135 	cdev_t devs, devc;
136 	char *names = "pqrsPQRS";
137 	struct pt_ioctl *pt;
138 
139 	/* For now we only map the lower 8 bits of the minor */
140 	if (n & ~0xff)
141 		return;
142 
143 	pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
144 	pt->devs = devs = make_dev(&pts_ops, n,
145 	    0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
146 	pt->devc = devc = make_dev(&ptc_ops, n,
147 	    0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
148 
149 	devs->si_drv1 = devc->si_drv1 = pt;
150 	devs->si_tty = devc->si_tty = &pt->pt_tty;
151 	pt->pt_tty.t_dev = devs;
152 	ttyregister(&pt->pt_tty);
153 }
154 
155 /*ARGSUSED*/
156 static	int
157 ptsopen(struct dev_open_args *ap)
158 {
159 	cdev_t dev = ap->a_head.a_dev;
160 	struct tty *tp;
161 	int error;
162 #if 0
163 	int minr;
164 	cdev_t nextdev;
165 #endif
166 	struct pt_ioctl *pti;
167 
168 #if 0
169 	minr = lminor(dev);
170 	/*
171 	 * REMOVED gross hack for devfs.  also note that makedev()
172 	 * no longer exists in this form and reference counting makes
173 	 * this a problem if we don't clean it out later.
174 	 */
175 	if (minr < 255) {
176 		nextdev = make_sub_dev(dev, minr + 1);
177 		if (!nextdev->si_drv1) {
178 			ptyinit(minr + 1);
179 		}
180 	}
181 #endif
182 	if (!dev->si_drv1)
183 		ptyinit(minor(dev));
184 	if (!dev->si_drv1)
185 		return(ENXIO);
186 	pti = dev->si_drv1;
187 	tp = dev->si_tty;
188 	if ((tp->t_state & TS_ISOPEN) == 0) {
189 		ttychars(tp);		/* Set up default chars */
190 		tp->t_iflag = TTYDEF_IFLAG;
191 		tp->t_oflag = TTYDEF_OFLAG;
192 		tp->t_lflag = TTYDEF_LFLAG;
193 		tp->t_cflag = TTYDEF_CFLAG;
194 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
195 	} else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
196 		return (EBUSY);
197 	} else if (pti->pt_prison != ap->a_cred->cr_prison) {
198 		return (EBUSY);
199 	}
200 	if (tp->t_oproc)			/* Ctrlr still around. */
201 		(void)(*linesw[tp->t_line].l_modem)(tp, 1);
202 	while ((tp->t_state & TS_CARR_ON) == 0) {
203 		if (ap->a_oflags & FNONBLOCK)
204 			break;
205 		error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
206 		if (error)
207 			return (error);
208 	}
209 	error = (*linesw[tp->t_line].l_open)(dev, tp);
210 	if (error == 0)
211 		ptcwakeup(tp, FREAD|FWRITE);
212 	return (error);
213 }
214 
215 static	int
216 ptsclose(struct dev_close_args *ap)
217 {
218 	cdev_t dev = ap->a_head.a_dev;
219 	struct tty *tp;
220 	int err;
221 
222 	tp = dev->si_tty;
223 	err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
224 	ptsstop(tp, FREAD|FWRITE);
225 	(void) ttyclose(tp);
226 	return (err);
227 }
228 
229 static	int
230 ptsread(struct dev_read_args *ap)
231 {
232 	cdev_t dev = ap->a_head.a_dev;
233 	struct proc *p = curproc;
234 	struct tty *tp = dev->si_tty;
235 	struct pt_ioctl *pti = dev->si_drv1;
236 	struct lwp *lp;
237 
238 	int error = 0;
239 
240 	lp = curthread->td_lwp;
241 
242 again:
243 	if (pti->pt_flags & PF_REMOTE) {
244 		while (isbackground(p, tp)) {
245 			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
246 			    SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
247 			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
248 				return (EIO);
249 			pgsignal(p->p_pgrp, SIGTTIN, 1);
250 			error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
251 			if (error)
252 				return (error);
253 		}
254 		if (tp->t_canq.c_cc == 0) {
255 			if (ap->a_ioflag & IO_NDELAY)
256 				return (EWOULDBLOCK);
257 			error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
258 					 "ptsin", 0);
259 			if (error)
260 				return (error);
261 			goto again;
262 		}
263 		while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
264 			if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
265 				error = EFAULT;
266 				break;
267 			}
268 		if (tp->t_canq.c_cc == 1)
269 			clist_getc(&tp->t_canq);
270 		if (tp->t_canq.c_cc)
271 			return (error);
272 	} else
273 		if (tp->t_oproc)
274 			error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
275 	ptcwakeup(tp, FWRITE);
276 	return (error);
277 }
278 
279 /*
280  * Write to pseudo-tty.
281  * Wakeups of controlling tty will happen
282  * indirectly, when tty driver calls ptsstart.
283  */
284 static	int
285 ptswrite(struct dev_write_args *ap)
286 {
287 	cdev_t dev = ap->a_head.a_dev;
288 	struct tty *tp;
289 
290 	tp = dev->si_tty;
291 	if (tp->t_oproc == 0)
292 		return (EIO);
293 	return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
294 }
295 
296 /*
297  * Start output on pseudo-tty.
298  * Wake up process selecting or sleeping for input from controlling tty.
299  */
300 static void
301 ptsstart(struct tty *tp)
302 {
303 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
304 
305 	if (tp->t_state & TS_TTSTOP)
306 		return;
307 	if (pti->pt_flags & PF_STOPPED) {
308 		pti->pt_flags &= ~PF_STOPPED;
309 		pti->pt_send = TIOCPKT_START;
310 	}
311 	ptcwakeup(tp, FREAD);
312 }
313 
314 static void
315 ptcwakeup(struct tty *tp, int flag)
316 {
317 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
318 
319 	if (flag & FREAD) {
320 		selwakeup(&pti->pt_selr);
321 		wakeup(TSA_PTC_READ(tp));
322 	}
323 	if (flag & FWRITE) {
324 		selwakeup(&pti->pt_selw);
325 		wakeup(TSA_PTC_WRITE(tp));
326 	}
327 }
328 
329 static	int
330 ptcopen(struct dev_open_args *ap)
331 {
332 	cdev_t dev = ap->a_head.a_dev;
333 	struct tty *tp;
334 	struct pt_ioctl *pti;
335 
336 	if (!dev->si_drv1)
337 		ptyinit(minor(dev));
338 	if (!dev->si_drv1)
339 		return(ENXIO);
340 	tp = dev->si_tty;
341 	if (tp->t_oproc)
342 		return (EIO);
343 	tp->t_oproc = ptsstart;
344 	tp->t_stop = ptsstop;
345 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
346 	tp->t_lflag &= ~EXTPROC;
347 	pti = dev->si_drv1;
348 	pti->pt_prison = ap->a_cred->cr_prison;
349 	pti->pt_flags = 0;
350 	pti->pt_send = 0;
351 	pti->pt_ucntl = 0;
352 	return (0);
353 }
354 
355 static	int
356 ptcclose(struct dev_close_args *ap)
357 {
358 	cdev_t dev = ap->a_head.a_dev;
359 	struct tty *tp;
360 
361 	tp = dev->si_tty;
362 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
363 
364 	/*
365 	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
366 	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
367 	 * l_modem()s that ignore carrier drop make no sense for ptys but
368 	 * may be in use because other parts of the line discipline make
369 	 * sense for ptys.  Recover by doing everything that a normal
370 	 * ttymodem() would have done except for sending a SIGHUP.
371 	 */
372 	if (tp->t_state & TS_ISOPEN) {
373 		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
374 		tp->t_state |= TS_ZOMBIE;
375 		ttyflush(tp, FREAD | FWRITE);
376 	}
377 
378 	tp->t_oproc = 0;		/* mark closed */
379 	return (0);
380 }
381 
382 static	int
383 ptcread(struct dev_read_args *ap)
384 {
385 	cdev_t dev = ap->a_head.a_dev;
386 	struct tty *tp = dev->si_tty;
387 	struct pt_ioctl *pti = dev->si_drv1;
388 	char buf[BUFSIZ];
389 	int error = 0, cc;
390 
391 	/*
392 	 * We want to block until the slave
393 	 * is open, and there's something to read;
394 	 * but if we lost the slave or we're NBIO,
395 	 * then return the appropriate error instead.
396 	 */
397 	for (;;) {
398 		if (tp->t_state&TS_ISOPEN) {
399 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
400 				error = ureadc((int)pti->pt_send, ap->a_uio);
401 				if (error)
402 					return (error);
403 				if (pti->pt_send & TIOCPKT_IOCTL) {
404 					cc = min(ap->a_uio->uio_resid,
405 						sizeof(tp->t_termios));
406 					uiomove((caddr_t)&tp->t_termios, cc,
407 						ap->a_uio);
408 				}
409 				pti->pt_send = 0;
410 				return (0);
411 			}
412 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
413 				error = ureadc((int)pti->pt_ucntl, ap->a_uio);
414 				if (error)
415 					return (error);
416 				pti->pt_ucntl = 0;
417 				return (0);
418 			}
419 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
420 				break;
421 		}
422 		if ((tp->t_state & TS_CONNECTED) == 0)
423 			return (0);	/* EOF */
424 		if (ap->a_ioflag & IO_NDELAY)
425 			return (EWOULDBLOCK);
426 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
427 		if (error)
428 			return (error);
429 	}
430 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
431 		error = ureadc(0, ap->a_uio);
432 	while (ap->a_uio->uio_resid > 0 && error == 0) {
433 		cc = q_to_b(&tp->t_outq, buf, min(ap->a_uio->uio_resid, BUFSIZ));
434 		if (cc <= 0)
435 			break;
436 		error = uiomove(buf, cc, ap->a_uio);
437 	}
438 	ttwwakeup(tp);
439 	return (error);
440 }
441 
442 static	void
443 ptsstop(struct tty *tp, int flush)
444 {
445 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
446 	int flag;
447 
448 	/* note: FLUSHREAD and FLUSHWRITE already ok */
449 	if (flush == 0) {
450 		flush = TIOCPKT_STOP;
451 		pti->pt_flags |= PF_STOPPED;
452 	} else
453 		pti->pt_flags &= ~PF_STOPPED;
454 	pti->pt_send |= flush;
455 	/* change of perspective */
456 	flag = 0;
457 	if (flush & FREAD)
458 		flag |= FWRITE;
459 	if (flush & FWRITE)
460 		flag |= FREAD;
461 	ptcwakeup(tp, flag);
462 }
463 
464 static	int
465 ptcpoll(struct dev_poll_args *ap)
466 {
467 	cdev_t dev = ap->a_head.a_dev;
468 	struct tty *tp = dev->si_tty;
469 	struct pt_ioctl *pti = dev->si_drv1;
470 	int revents = 0;
471 
472 	if ((tp->t_state & TS_CONNECTED) == 0) {
473 		ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
474 		return(0);
475 	}
476 
477 	/*
478 	 * Need to block timeouts (ttrstart).
479 	 */
480 	crit_enter();
481 
482 	if (ap->a_events & (POLLIN | POLLRDNORM))
483 		if ((tp->t_state & TS_ISOPEN) &&
484 		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
485 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
486 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
487 			revents |= ap->a_events & (POLLIN | POLLRDNORM);
488 
489 	if (ap->a_events & (POLLOUT | POLLWRNORM))
490 		if (tp->t_state & TS_ISOPEN &&
491 		    ((pti->pt_flags & PF_REMOTE) ?
492 		     (tp->t_canq.c_cc == 0) :
493 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
494 		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
495 			revents |= ap->a_events & (POLLOUT | POLLWRNORM);
496 
497 	if (ap->a_events & POLLHUP)
498 		if ((tp->t_state & TS_CARR_ON) == 0)
499 			revents |= POLLHUP;
500 
501 	if (revents == 0) {
502 		if (ap->a_events & (POLLIN | POLLRDNORM))
503 			selrecord(curthread, &pti->pt_selr);
504 
505 		if (ap->a_events & (POLLOUT | POLLWRNORM))
506 			selrecord(curthread, &pti->pt_selw);
507 	}
508 	crit_exit();
509 
510 	ap->a_events = revents;
511 	return (0);
512 }
513 
514 static	int
515 ptcwrite(struct dev_write_args *ap)
516 {
517 	cdev_t dev = ap->a_head.a_dev;
518 	struct tty *tp = dev->si_tty;
519 	u_char *cp = 0;
520 	int cc = 0;
521 	u_char locbuf[BUFSIZ];
522 	int cnt = 0;
523 	struct pt_ioctl *pti = dev->si_drv1;
524 	int error = 0;
525 
526 again:
527 	if ((tp->t_state&TS_ISOPEN) == 0)
528 		goto block;
529 	if (pti->pt_flags & PF_REMOTE) {
530 		if (tp->t_canq.c_cc)
531 			goto block;
532 		while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
533 		       tp->t_canq.c_cc < TTYHOG - 1) {
534 			if (cc == 0) {
535 				cc = min(ap->a_uio->uio_resid, BUFSIZ);
536 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
537 				cp = locbuf;
538 				error = uiomove((caddr_t)cp, cc, ap->a_uio);
539 				if (error)
540 					return (error);
541 				/* check again for safety */
542 				if ((tp->t_state & TS_ISOPEN) == 0) {
543 					/* adjust as usual */
544 					ap->a_uio->uio_resid += cc;
545 					return (EIO);
546 				}
547 			}
548 			if (cc > 0) {
549 				cc = b_to_q((char *)cp, cc, &tp->t_canq);
550 				/*
551 				 * XXX we don't guarantee that the canq size
552 				 * is >= TTYHOG, so the above b_to_q() may
553 				 * leave some bytes uncopied.  However, space
554 				 * is guaranteed for the null terminator if
555 				 * we don't fail here since (TTYHOG - 1) is
556 				 * not a multiple of CBSIZE.
557 				 */
558 				if (cc > 0)
559 					break;
560 			}
561 		}
562 		/* adjust for data copied in but not written */
563 		ap->a_uio->uio_resid += cc;
564 		clist_putc(0, &tp->t_canq);
565 		ttwakeup(tp);
566 		wakeup(TSA_PTS_READ(tp));
567 		return (0);
568 	}
569 	while (ap->a_uio->uio_resid > 0 || cc > 0) {
570 		if (cc == 0) {
571 			cc = min(ap->a_uio->uio_resid, BUFSIZ);
572 			cp = locbuf;
573 			error = uiomove((caddr_t)cp, cc, ap->a_uio);
574 			if (error)
575 				return (error);
576 			/* check again for safety */
577 			if ((tp->t_state & TS_ISOPEN) == 0) {
578 				/* adjust for data copied in but not written */
579 				ap->a_uio->uio_resid += cc;
580 				return (EIO);
581 			}
582 		}
583 		while (cc > 0) {
584 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
585 			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
586 				wakeup(TSA_HUP_OR_INPUT(tp));
587 				goto block;
588 			}
589 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
590 			cnt++;
591 			cc--;
592 		}
593 		cc = 0;
594 	}
595 	return (0);
596 block:
597 	/*
598 	 * Come here to wait for slave to open, for space
599 	 * in outq, or space in rawq, or an empty canq.
600 	 */
601 	if ((tp->t_state & TS_CONNECTED) == 0) {
602 		/* adjust for data copied in but not written */
603 		ap->a_uio->uio_resid += cc;
604 		return (EIO);
605 	}
606 	if (ap->a_ioflag & IO_NDELAY) {
607 		/* adjust for data copied in but not written */
608 		ap->a_uio->uio_resid += cc;
609 		if (cnt == 0)
610 			return (EWOULDBLOCK);
611 		return (0);
612 	}
613 	error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
614 	if (error) {
615 		/* adjust for data copied in but not written */
616 		ap->a_uio->uio_resid += cc;
617 		return (error);
618 	}
619 	goto again;
620 }
621 
622 /*ARGSUSED*/
623 static	int
624 ptyioctl(struct dev_ioctl_args *ap)
625 {
626 	cdev_t dev = ap->a_head.a_dev;
627 	struct tty *tp = dev->si_tty;
628 	struct pt_ioctl *pti = dev->si_drv1;
629 	u_char *cc = tp->t_cc;
630 	int stop, error;
631 
632 	if (dev_dflags(dev) & D_MASTER) {
633 		switch (ap->a_cmd) {
634 
635 		case TIOCGPGRP:
636 			/*
637 			 * We avoid calling ttioctl on the controller since,
638 			 * in that case, tp must be the controlling terminal.
639 			 */
640 			*(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
641 			return (0);
642 
643 		case TIOCPKT:
644 			if (*(int *)ap->a_data) {
645 				if (pti->pt_flags & PF_UCNTL)
646 					return (EINVAL);
647 				pti->pt_flags |= PF_PKT;
648 			} else
649 				pti->pt_flags &= ~PF_PKT;
650 			return (0);
651 
652 		case TIOCUCNTL:
653 			if (*(int *)ap->a_data) {
654 				if (pti->pt_flags & PF_PKT)
655 					return (EINVAL);
656 				pti->pt_flags |= PF_UCNTL;
657 			} else
658 				pti->pt_flags &= ~PF_UCNTL;
659 			return (0);
660 
661 		case TIOCREMOTE:
662 			if (*(int *)ap->a_data)
663 				pti->pt_flags |= PF_REMOTE;
664 			else
665 				pti->pt_flags &= ~PF_REMOTE;
666 			ttyflush(tp, FREAD|FWRITE);
667 			return (0);
668 		}
669 
670 		/*
671 		 * The rest of the ioctls shouldn't be called until
672 		 * the slave is open.
673 		 */
674 		if ((tp->t_state & TS_ISOPEN) == 0)
675 			return (EAGAIN);
676 
677 		switch (ap->a_cmd) {
678 #ifdef COMPAT_43
679 		case TIOCSETP:
680 		case TIOCSETN:
681 #endif
682 		case TIOCSETD:
683 		case TIOCSETA:
684 		case TIOCSETAW:
685 		case TIOCSETAF:
686 			/*
687 			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
688 			 * ttywflush(tp) will hang if there are characters in
689 			 * the outq.
690 			 */
691 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
692 			break;
693 
694 		case TIOCSIG:
695 			if (*(unsigned int *)ap->a_data >= NSIG ||
696 			    *(unsigned int *)ap->a_data == 0)
697 				return(EINVAL);
698 			if ((tp->t_lflag&NOFLSH) == 0)
699 				ttyflush(tp, FREAD|FWRITE);
700 			pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
701 			if ((*(unsigned int *)ap->a_data == SIGINFO) &&
702 			    ((tp->t_lflag&NOKERNINFO) == 0))
703 				ttyinfo(tp);
704 			return(0);
705 		}
706 	}
707 	if (ap->a_cmd == TIOCEXT) {
708 		/*
709 		 * When the EXTPROC bit is being toggled, we need
710 		 * to send an TIOCPKT_IOCTL if the packet driver
711 		 * is turned on.
712 		 */
713 		if (*(int *)ap->a_data) {
714 			if (pti->pt_flags & PF_PKT) {
715 				pti->pt_send |= TIOCPKT_IOCTL;
716 				ptcwakeup(tp, FREAD);
717 			}
718 			tp->t_lflag |= EXTPROC;
719 		} else {
720 			if ((tp->t_lflag & EXTPROC) &&
721 			    (pti->pt_flags & PF_PKT)) {
722 				pti->pt_send |= TIOCPKT_IOCTL;
723 				ptcwakeup(tp, FREAD);
724 			}
725 			tp->t_lflag &= ~EXTPROC;
726 		}
727 		return(0);
728 	}
729 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
730 					      ap->a_fflag, ap->a_cred);
731 	if (error == ENOIOCTL)
732 		 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
733 	if (error == ENOIOCTL) {
734 		if (pti->pt_flags & PF_UCNTL &&
735 		    (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
736 			if (ap->a_cmd & 0xff) {
737 				pti->pt_ucntl = (u_char)ap->a_cmd;
738 				ptcwakeup(tp, FREAD);
739 			}
740 			return (0);
741 		}
742 		error = ENOTTY;
743 	}
744 	/*
745 	 * If external processing and packet mode send ioctl packet.
746 	 */
747 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
748 		switch(ap->a_cmd) {
749 		case TIOCSETA:
750 		case TIOCSETAW:
751 		case TIOCSETAF:
752 #ifdef COMPAT_43
753 		case TIOCSETP:
754 		case TIOCSETN:
755 #endif
756 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
757 		case TIOCSETC:
758 		case TIOCSLTC:
759 		case TIOCLBIS:
760 		case TIOCLBIC:
761 		case TIOCLSET:
762 #endif
763 			pti->pt_send |= TIOCPKT_IOCTL;
764 			ptcwakeup(tp, FREAD);
765 		default:
766 			break;
767 		}
768 	}
769 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
770 		&& CCEQ(cc[VSTART], CTRL('q'));
771 	if (pti->pt_flags & PF_NOSTOP) {
772 		if (stop) {
773 			pti->pt_send &= ~TIOCPKT_NOSTOP;
774 			pti->pt_send |= TIOCPKT_DOSTOP;
775 			pti->pt_flags &= ~PF_NOSTOP;
776 			ptcwakeup(tp, FREAD);
777 		}
778 	} else {
779 		if (!stop) {
780 			pti->pt_send &= ~TIOCPKT_DOSTOP;
781 			pti->pt_send |= TIOCPKT_NOSTOP;
782 			pti->pt_flags |= PF_NOSTOP;
783 			ptcwakeup(tp, FREAD);
784 		}
785 	}
786 	return (error);
787 }
788 
789 
790 static void ptc_drvinit (void *unused);
791 
792 static void
793 ptc_drvinit(void *unused)
794 {
795 	dev_ops_add(&pts_ops, 0, 0);
796 	dev_ops_add(&ptc_ops, 0, 0);
797 	/* XXX: Gross hack for DEVFS */
798 	/* XXX: DEVFS is no more, should this be removed? */
799 	ptyinit(0);
800 }
801 
802 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
803