xref: /dflybsd-src/sys/kern/tty_pty.c (revision 744c01d0dc2aa1481a40e5b0988d15691602f5c9)
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.18 2006/12/23 23:47:54 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);
143 	bzero(pt, sizeof(*pt));
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 	int minr;
163 #if 0
164 	cdev_t nextdev;
165 #endif
166 	struct pt_ioctl *pti;
167 
168 	minr = lminor(dev);
169 #if 0
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) && suser_cred(ap->a_cred, 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 	int error = 0;
237 
238 again:
239 	if (pti->pt_flags & PF_REMOTE) {
240 		while (isbackground(p, tp)) {
241 			if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
242 			    SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
243 			    p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
244 				return (EIO);
245 			pgsignal(p->p_pgrp, SIGTTIN, 1);
246 			error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
247 			if (error)
248 				return (error);
249 		}
250 		if (tp->t_canq.c_cc == 0) {
251 			if (ap->a_ioflag & IO_NDELAY)
252 				return (EWOULDBLOCK);
253 			error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
254 					 "ptsin", 0);
255 			if (error)
256 				return (error);
257 			goto again;
258 		}
259 		while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
260 			if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
261 				error = EFAULT;
262 				break;
263 			}
264 		if (tp->t_canq.c_cc == 1)
265 			clist_getc(&tp->t_canq);
266 		if (tp->t_canq.c_cc)
267 			return (error);
268 	} else
269 		if (tp->t_oproc)
270 			error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
271 	ptcwakeup(tp, FWRITE);
272 	return (error);
273 }
274 
275 /*
276  * Write to pseudo-tty.
277  * Wakeups of controlling tty will happen
278  * indirectly, when tty driver calls ptsstart.
279  */
280 static	int
281 ptswrite(struct dev_write_args *ap)
282 {
283 	cdev_t dev = ap->a_head.a_dev;
284 	struct tty *tp;
285 
286 	tp = dev->si_tty;
287 	if (tp->t_oproc == 0)
288 		return (EIO);
289 	return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
290 }
291 
292 /*
293  * Start output on pseudo-tty.
294  * Wake up process selecting or sleeping for input from controlling tty.
295  */
296 static void
297 ptsstart(struct tty *tp)
298 {
299 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
300 
301 	if (tp->t_state & TS_TTSTOP)
302 		return;
303 	if (pti->pt_flags & PF_STOPPED) {
304 		pti->pt_flags &= ~PF_STOPPED;
305 		pti->pt_send = TIOCPKT_START;
306 	}
307 	ptcwakeup(tp, FREAD);
308 }
309 
310 static void
311 ptcwakeup(struct tty *tp, int flag)
312 {
313 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
314 
315 	if (flag & FREAD) {
316 		selwakeup(&pti->pt_selr);
317 		wakeup(TSA_PTC_READ(tp));
318 	}
319 	if (flag & FWRITE) {
320 		selwakeup(&pti->pt_selw);
321 		wakeup(TSA_PTC_WRITE(tp));
322 	}
323 }
324 
325 static	int
326 ptcopen(struct dev_open_args *ap)
327 {
328 	cdev_t dev = ap->a_head.a_dev;
329 	struct tty *tp;
330 	struct pt_ioctl *pti;
331 
332 	if (!dev->si_drv1)
333 		ptyinit(minor(dev));
334 	if (!dev->si_drv1)
335 		return(ENXIO);
336 	tp = dev->si_tty;
337 	if (tp->t_oproc)
338 		return (EIO);
339 	tp->t_oproc = ptsstart;
340 	tp->t_stop = ptsstop;
341 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
342 	tp->t_lflag &= ~EXTPROC;
343 	pti = dev->si_drv1;
344 	pti->pt_prison = ap->a_cred->cr_prison;
345 	pti->pt_flags = 0;
346 	pti->pt_send = 0;
347 	pti->pt_ucntl = 0;
348 	return (0);
349 }
350 
351 static	int
352 ptcclose(struct dev_close_args *ap)
353 {
354 	cdev_t dev = ap->a_head.a_dev;
355 	struct tty *tp;
356 
357 	tp = dev->si_tty;
358 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
359 
360 	/*
361 	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
362 	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
363 	 * l_modem()s that ignore carrier drop make no sense for ptys but
364 	 * may be in use because other parts of the line discipline make
365 	 * sense for ptys.  Recover by doing everything that a normal
366 	 * ttymodem() would have done except for sending a SIGHUP.
367 	 */
368 	if (tp->t_state & TS_ISOPEN) {
369 		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
370 		tp->t_state |= TS_ZOMBIE;
371 		ttyflush(tp, FREAD | FWRITE);
372 	}
373 
374 	tp->t_oproc = 0;		/* mark closed */
375 	return (0);
376 }
377 
378 static	int
379 ptcread(struct dev_read_args *ap)
380 {
381 	cdev_t dev = ap->a_head.a_dev;
382 	struct tty *tp = dev->si_tty;
383 	struct pt_ioctl *pti = dev->si_drv1;
384 	char buf[BUFSIZ];
385 	int error = 0, cc;
386 
387 	/*
388 	 * We want to block until the slave
389 	 * is open, and there's something to read;
390 	 * but if we lost the slave or we're NBIO,
391 	 * then return the appropriate error instead.
392 	 */
393 	for (;;) {
394 		if (tp->t_state&TS_ISOPEN) {
395 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
396 				error = ureadc((int)pti->pt_send, ap->a_uio);
397 				if (error)
398 					return (error);
399 				if (pti->pt_send & TIOCPKT_IOCTL) {
400 					cc = min(ap->a_uio->uio_resid,
401 						sizeof(tp->t_termios));
402 					uiomove((caddr_t)&tp->t_termios, cc,
403 						ap->a_uio);
404 				}
405 				pti->pt_send = 0;
406 				return (0);
407 			}
408 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
409 				error = ureadc((int)pti->pt_ucntl, ap->a_uio);
410 				if (error)
411 					return (error);
412 				pti->pt_ucntl = 0;
413 				return (0);
414 			}
415 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
416 				break;
417 		}
418 		if ((tp->t_state & TS_CONNECTED) == 0)
419 			return (0);	/* EOF */
420 		if (ap->a_ioflag & IO_NDELAY)
421 			return (EWOULDBLOCK);
422 		error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
423 		if (error)
424 			return (error);
425 	}
426 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
427 		error = ureadc(0, ap->a_uio);
428 	while (ap->a_uio->uio_resid > 0 && error == 0) {
429 		cc = q_to_b(&tp->t_outq, buf, min(ap->a_uio->uio_resid, BUFSIZ));
430 		if (cc <= 0)
431 			break;
432 		error = uiomove(buf, cc, ap->a_uio);
433 	}
434 	ttwwakeup(tp);
435 	return (error);
436 }
437 
438 static	void
439 ptsstop(struct tty *tp, int flush)
440 {
441 	struct pt_ioctl *pti = tp->t_dev->si_drv1;
442 	int flag;
443 
444 	/* note: FLUSHREAD and FLUSHWRITE already ok */
445 	if (flush == 0) {
446 		flush = TIOCPKT_STOP;
447 		pti->pt_flags |= PF_STOPPED;
448 	} else
449 		pti->pt_flags &= ~PF_STOPPED;
450 	pti->pt_send |= flush;
451 	/* change of perspective */
452 	flag = 0;
453 	if (flush & FREAD)
454 		flag |= FWRITE;
455 	if (flush & FWRITE)
456 		flag |= FREAD;
457 	ptcwakeup(tp, flag);
458 }
459 
460 static	int
461 ptcpoll(struct dev_poll_args *ap)
462 {
463 	cdev_t dev = ap->a_head.a_dev;
464 	struct tty *tp = dev->si_tty;
465 	struct pt_ioctl *pti = dev->si_drv1;
466 	int revents = 0;
467 
468 	if ((tp->t_state & TS_CONNECTED) == 0) {
469 		ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
470 		return(0);
471 	}
472 
473 	/*
474 	 * Need to block timeouts (ttrstart).
475 	 */
476 	crit_enter();
477 
478 	if (ap->a_events & (POLLIN | POLLRDNORM))
479 		if ((tp->t_state & TS_ISOPEN) &&
480 		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
481 		     ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
482 		     ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
483 			revents |= ap->a_events & (POLLIN | POLLRDNORM);
484 
485 	if (ap->a_events & (POLLOUT | POLLWRNORM))
486 		if (tp->t_state & TS_ISOPEN &&
487 		    ((pti->pt_flags & PF_REMOTE) ?
488 		     (tp->t_canq.c_cc == 0) :
489 		     ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
490 		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
491 			revents |= ap->a_events & (POLLOUT | POLLWRNORM);
492 
493 	if (ap->a_events & POLLHUP)
494 		if ((tp->t_state & TS_CARR_ON) == 0)
495 			revents |= POLLHUP;
496 
497 	if (revents == 0) {
498 		if (ap->a_events & (POLLIN | POLLRDNORM))
499 			selrecord(curthread, &pti->pt_selr);
500 
501 		if (ap->a_events & (POLLOUT | POLLWRNORM))
502 			selrecord(curthread, &pti->pt_selw);
503 	}
504 	crit_exit();
505 
506 	ap->a_events = revents;
507 	return (0);
508 }
509 
510 static	int
511 ptcwrite(struct dev_write_args *ap)
512 {
513 	cdev_t dev = ap->a_head.a_dev;
514 	struct tty *tp = dev->si_tty;
515 	u_char *cp = 0;
516 	int cc = 0;
517 	u_char locbuf[BUFSIZ];
518 	int cnt = 0;
519 	struct pt_ioctl *pti = dev->si_drv1;
520 	int error = 0;
521 
522 again:
523 	if ((tp->t_state&TS_ISOPEN) == 0)
524 		goto block;
525 	if (pti->pt_flags & PF_REMOTE) {
526 		if (tp->t_canq.c_cc)
527 			goto block;
528 		while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
529 		       tp->t_canq.c_cc < TTYHOG - 1) {
530 			if (cc == 0) {
531 				cc = min(ap->a_uio->uio_resid, BUFSIZ);
532 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
533 				cp = locbuf;
534 				error = uiomove((caddr_t)cp, cc, ap->a_uio);
535 				if (error)
536 					return (error);
537 				/* check again for safety */
538 				if ((tp->t_state & TS_ISOPEN) == 0) {
539 					/* adjust as usual */
540 					ap->a_uio->uio_resid += cc;
541 					return (EIO);
542 				}
543 			}
544 			if (cc > 0) {
545 				cc = b_to_q((char *)cp, cc, &tp->t_canq);
546 				/*
547 				 * XXX we don't guarantee that the canq size
548 				 * is >= TTYHOG, so the above b_to_q() may
549 				 * leave some bytes uncopied.  However, space
550 				 * is guaranteed for the null terminator if
551 				 * we don't fail here since (TTYHOG - 1) is
552 				 * not a multiple of CBSIZE.
553 				 */
554 				if (cc > 0)
555 					break;
556 			}
557 		}
558 		/* adjust for data copied in but not written */
559 		ap->a_uio->uio_resid += cc;
560 		clist_putc(0, &tp->t_canq);
561 		ttwakeup(tp);
562 		wakeup(TSA_PTS_READ(tp));
563 		return (0);
564 	}
565 	while (ap->a_uio->uio_resid > 0 || cc > 0) {
566 		if (cc == 0) {
567 			cc = min(ap->a_uio->uio_resid, BUFSIZ);
568 			cp = locbuf;
569 			error = uiomove((caddr_t)cp, cc, ap->a_uio);
570 			if (error)
571 				return (error);
572 			/* check again for safety */
573 			if ((tp->t_state & TS_ISOPEN) == 0) {
574 				/* adjust for data copied in but not written */
575 				ap->a_uio->uio_resid += cc;
576 				return (EIO);
577 			}
578 		}
579 		while (cc > 0) {
580 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
581 			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
582 				wakeup(TSA_HUP_OR_INPUT(tp));
583 				goto block;
584 			}
585 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
586 			cnt++;
587 			cc--;
588 		}
589 		cc = 0;
590 	}
591 	return (0);
592 block:
593 	/*
594 	 * Come here to wait for slave to open, for space
595 	 * in outq, or space in rawq, or an empty canq.
596 	 */
597 	if ((tp->t_state & TS_CONNECTED) == 0) {
598 		/* adjust for data copied in but not written */
599 		ap->a_uio->uio_resid += cc;
600 		return (EIO);
601 	}
602 	if (ap->a_ioflag & IO_NDELAY) {
603 		/* adjust for data copied in but not written */
604 		ap->a_uio->uio_resid += cc;
605 		if (cnt == 0)
606 			return (EWOULDBLOCK);
607 		return (0);
608 	}
609 	error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
610 	if (error) {
611 		/* adjust for data copied in but not written */
612 		ap->a_uio->uio_resid += cc;
613 		return (error);
614 	}
615 	goto again;
616 }
617 
618 /*ARGSUSED*/
619 static	int
620 ptyioctl(struct dev_ioctl_args *ap)
621 {
622 	cdev_t dev = ap->a_head.a_dev;
623 	struct tty *tp = dev->si_tty;
624 	struct pt_ioctl *pti = dev->si_drv1;
625 	u_char *cc = tp->t_cc;
626 	int stop, error;
627 
628 	if (dev_dflags(dev) & D_MASTER) {
629 		switch (ap->a_cmd) {
630 
631 		case TIOCGPGRP:
632 			/*
633 			 * We avoid calling ttioctl on the controller since,
634 			 * in that case, tp must be the controlling terminal.
635 			 */
636 			*(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
637 			return (0);
638 
639 		case TIOCPKT:
640 			if (*(int *)ap->a_data) {
641 				if (pti->pt_flags & PF_UCNTL)
642 					return (EINVAL);
643 				pti->pt_flags |= PF_PKT;
644 			} else
645 				pti->pt_flags &= ~PF_PKT;
646 			return (0);
647 
648 		case TIOCUCNTL:
649 			if (*(int *)ap->a_data) {
650 				if (pti->pt_flags & PF_PKT)
651 					return (EINVAL);
652 				pti->pt_flags |= PF_UCNTL;
653 			} else
654 				pti->pt_flags &= ~PF_UCNTL;
655 			return (0);
656 
657 		case TIOCREMOTE:
658 			if (*(int *)ap->a_data)
659 				pti->pt_flags |= PF_REMOTE;
660 			else
661 				pti->pt_flags &= ~PF_REMOTE;
662 			ttyflush(tp, FREAD|FWRITE);
663 			return (0);
664 		}
665 
666 		/*
667 		 * The rest of the ioctls shouldn't be called until
668 		 * the slave is open.
669 		 */
670 		if ((tp->t_state & TS_ISOPEN) == 0)
671 			return (EAGAIN);
672 
673 		switch (ap->a_cmd) {
674 #ifdef COMPAT_43
675 		case TIOCSETP:
676 		case TIOCSETN:
677 #endif
678 		case TIOCSETD:
679 		case TIOCSETA:
680 		case TIOCSETAW:
681 		case TIOCSETAF:
682 			/*
683 			 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
684 			 * ttywflush(tp) will hang if there are characters in
685 			 * the outq.
686 			 */
687 			ndflush(&tp->t_outq, tp->t_outq.c_cc);
688 			break;
689 
690 		case TIOCSIG:
691 			if (*(unsigned int *)ap->a_data >= NSIG ||
692 			    *(unsigned int *)ap->a_data == 0)
693 				return(EINVAL);
694 			if ((tp->t_lflag&NOFLSH) == 0)
695 				ttyflush(tp, FREAD|FWRITE);
696 			pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
697 			if ((*(unsigned int *)ap->a_data == SIGINFO) &&
698 			    ((tp->t_lflag&NOKERNINFO) == 0))
699 				ttyinfo(tp);
700 			return(0);
701 		}
702 	}
703 	if (ap->a_cmd == TIOCEXT) {
704 		/*
705 		 * When the EXTPROC bit is being toggled, we need
706 		 * to send an TIOCPKT_IOCTL if the packet driver
707 		 * is turned on.
708 		 */
709 		if (*(int *)ap->a_data) {
710 			if (pti->pt_flags & PF_PKT) {
711 				pti->pt_send |= TIOCPKT_IOCTL;
712 				ptcwakeup(tp, FREAD);
713 			}
714 			tp->t_lflag |= EXTPROC;
715 		} else {
716 			if ((tp->t_lflag & EXTPROC) &&
717 			    (pti->pt_flags & PF_PKT)) {
718 				pti->pt_send |= TIOCPKT_IOCTL;
719 				ptcwakeup(tp, FREAD);
720 			}
721 			tp->t_lflag &= ~EXTPROC;
722 		}
723 		return(0);
724 	}
725 	error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
726 					      ap->a_fflag, ap->a_cred);
727 	if (error == ENOIOCTL)
728 		 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
729 	if (error == ENOIOCTL) {
730 		if (pti->pt_flags & PF_UCNTL &&
731 		    (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
732 			if (ap->a_cmd & 0xff) {
733 				pti->pt_ucntl = (u_char)ap->a_cmd;
734 				ptcwakeup(tp, FREAD);
735 			}
736 			return (0);
737 		}
738 		error = ENOTTY;
739 	}
740 	/*
741 	 * If external processing and packet mode send ioctl packet.
742 	 */
743 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
744 		switch(ap->a_cmd) {
745 		case TIOCSETA:
746 		case TIOCSETAW:
747 		case TIOCSETAF:
748 #ifdef COMPAT_43
749 		case TIOCSETP:
750 		case TIOCSETN:
751 #endif
752 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
753 		case TIOCSETC:
754 		case TIOCSLTC:
755 		case TIOCLBIS:
756 		case TIOCLBIC:
757 		case TIOCLSET:
758 #endif
759 			pti->pt_send |= TIOCPKT_IOCTL;
760 			ptcwakeup(tp, FREAD);
761 		default:
762 			break;
763 		}
764 	}
765 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
766 		&& CCEQ(cc[VSTART], CTRL('q'));
767 	if (pti->pt_flags & PF_NOSTOP) {
768 		if (stop) {
769 			pti->pt_send &= ~TIOCPKT_NOSTOP;
770 			pti->pt_send |= TIOCPKT_DOSTOP;
771 			pti->pt_flags &= ~PF_NOSTOP;
772 			ptcwakeup(tp, FREAD);
773 		}
774 	} else {
775 		if (!stop) {
776 			pti->pt_send &= ~TIOCPKT_DOSTOP;
777 			pti->pt_send |= TIOCPKT_NOSTOP;
778 			pti->pt_flags |= PF_NOSTOP;
779 			ptcwakeup(tp, FREAD);
780 		}
781 	}
782 	return (error);
783 }
784 
785 
786 static void ptc_drvinit (void *unused);
787 
788 static void
789 ptc_drvinit(void *unused)
790 {
791 	dev_ops_add(&pts_ops, 0, 0);
792 	dev_ops_add(&ptc_ops, 0, 0);
793 	/* XXX: Gross hack for DEVFS */
794 	/* XXX: DEVFS is no more, should this be removed? */
795 	ptyinit(0);
796 }
797 
798 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
799