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