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