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