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