tty_pty.c revision 1.49 1 /* $NetBSD: tty_pty.c,v 1.49 2000/09/11 13:51:29 pk 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 = (*linesw[tp->t_line].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 = (*linesw[tp->t_line].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 (sigismember(&p->p_sigignore, SIGTTIN) ||
340 sigismember(&p->p_sigmask, SIGTTIN) ||
341 p->p_pgrp->pg_jobc == 0 ||
342 p->p_flag & P_PPWAIT)
343 return (EIO);
344 pgsignal(p->p_pgrp, SIGTTIN, 1);
345 error = ttysleep(tp, (caddr_t)&lbolt,
346 TTIPRI | PCATCH, ttybg, 0);
347 if (error)
348 return (error);
349 }
350 if (tp->t_canq.c_cc == 0) {
351 if (flag & IO_NDELAY)
352 return (EWOULDBLOCK);
353 error = ttysleep(tp, (caddr_t)&tp->t_canq,
354 TTIPRI | PCATCH, ttyin, 0);
355 if (error)
356 return (error);
357 goto again;
358 }
359 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
360 if (ureadc(getc(&tp->t_canq), uio) < 0) {
361 error = EFAULT;
362 break;
363 }
364 if (tp->t_canq.c_cc == 1)
365 (void) getc(&tp->t_canq);
366 if (tp->t_canq.c_cc)
367 return (error);
368 } else
369 if (tp->t_oproc)
370 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
371 ptcwakeup(tp, FWRITE);
372 return (error);
373 }
374
375 /*
376 * Write to pseudo-tty.
377 * Wakeups of controlling tty will happen
378 * indirectly, when tty driver calls ptsstart.
379 */
380 int
381 ptswrite(dev, uio, flag)
382 dev_t dev;
383 struct uio *uio;
384 int flag;
385 {
386 struct pt_softc *pti = pt_softc[minor(dev)];
387 struct tty *tp = pti->pt_tty;
388
389 if (tp->t_oproc == 0)
390 return (EIO);
391 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
392 }
393
394 /*
395 * Start output on pseudo-tty.
396 * Wake up process polling or sleeping for input from controlling tty.
397 */
398 void
399 ptsstart(tp)
400 struct tty *tp;
401 {
402 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
403
404 if (ISSET(tp->t_state, TS_TTSTOP))
405 return;
406 if (pti->pt_flags & PF_STOPPED) {
407 pti->pt_flags &= ~PF_STOPPED;
408 pti->pt_send = TIOCPKT_START;
409 }
410 ptcwakeup(tp, FREAD);
411 }
412
413 void
414 ptsstop(tp, flush)
415 struct tty *tp;
416 int flush;
417 {
418 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
419 int flag;
420
421 /* note: FLUSHREAD and FLUSHWRITE already ok */
422 if (flush == 0) {
423 flush = TIOCPKT_STOP;
424 pti->pt_flags |= PF_STOPPED;
425 } else
426 pti->pt_flags &= ~PF_STOPPED;
427 pti->pt_send |= flush;
428 /* change of perspective */
429 flag = 0;
430 if (flush & FREAD)
431 flag |= FWRITE;
432 if (flush & FWRITE)
433 flag |= FREAD;
434 ptcwakeup(tp, flag);
435 }
436
437 void
438 ptcwakeup(tp, flag)
439 struct tty *tp;
440 int flag;
441 {
442 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
443
444 if (flag & FREAD) {
445 selwakeup(&pti->pt_selr);
446 wakeup((caddr_t)&tp->t_outq.c_cf);
447 }
448 if (flag & FWRITE) {
449 selwakeup(&pti->pt_selw);
450 wakeup((caddr_t)&tp->t_rawq.c_cf);
451 }
452 }
453
454 /*ARGSUSED*/
455 int
456 ptcopen(dev, flag, devtype, p)
457 dev_t dev;
458 int flag, devtype;
459 struct proc *p;
460 {
461 struct pt_softc *pti;
462 struct tty *tp;
463 int error;
464
465 if ((error = check_pty(dev)))
466 return (error);
467
468 pti = pt_softc[minor(dev)];
469 tp = pti->pt_tty;
470
471 if (tp->t_oproc)
472 return (EIO);
473 tp->t_oproc = ptsstart;
474 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
475 CLR(tp->t_lflag, EXTPROC);
476 pti->pt_flags = 0;
477 pti->pt_send = 0;
478 pti->pt_ucntl = 0;
479 return (0);
480 }
481
482 /*ARGSUSED*/
483 int
484 ptcclose(dev, flag, devtype, p)
485 dev_t dev;
486 int flag, devtype;
487 struct proc *p;
488 {
489 struct pt_softc *pti = pt_softc[minor(dev)];
490 struct tty *tp = pti->pt_tty;
491
492 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
493 CLR(tp->t_state, TS_CARR_ON);
494 tp->t_oproc = 0; /* mark closed */
495 return (0);
496 }
497
498 int
499 ptcread(dev, uio, flag)
500 dev_t dev;
501 struct uio *uio;
502 int flag;
503 {
504 struct pt_softc *pti = pt_softc[minor(dev)];
505 struct tty *tp = pti->pt_tty;
506 char buf[BUFSIZ];
507 int error = 0, cc;
508
509 /*
510 * We want to block until the slave
511 * is open, and there's something to read;
512 * but if we lost the slave or we're NBIO,
513 * then return the appropriate error instead.
514 */
515 for (;;) {
516 if (ISSET(tp->t_state, TS_ISOPEN)) {
517 if (pti->pt_flags&PF_PKT && pti->pt_send) {
518 error = ureadc((int)pti->pt_send, uio);
519 if (error)
520 return (error);
521 if (pti->pt_send & TIOCPKT_IOCTL) {
522 cc = min(uio->uio_resid,
523 sizeof(tp->t_termios));
524 uiomove((caddr_t) &tp->t_termios,
525 cc, uio);
526 }
527 pti->pt_send = 0;
528 return (0);
529 }
530 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
531 error = ureadc((int)pti->pt_ucntl, uio);
532 if (error)
533 return (error);
534 pti->pt_ucntl = 0;
535 return (0);
536 }
537 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
538 break;
539 }
540 if (!ISSET(tp->t_state, TS_CARR_ON))
541 return (0); /* EOF */
542 if (flag & IO_NDELAY)
543 return (EWOULDBLOCK);
544 error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
545 ttyin, 0);
546 if (error)
547 return (error);
548 }
549 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
550 error = ureadc(0, uio);
551 while (uio->uio_resid > 0 && error == 0) {
552 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
553 if (cc <= 0)
554 break;
555 error = uiomove(buf, cc, uio);
556 }
557 if (tp->t_outq.c_cc <= tp->t_lowat) {
558 if (ISSET(tp->t_state, TS_ASLEEP)) {
559 CLR(tp->t_state, TS_ASLEEP);
560 wakeup((caddr_t)&tp->t_outq);
561 }
562 selwakeup(&tp->t_wsel);
563 }
564 return (error);
565 }
566
567
568 int
569 ptcwrite(dev, uio, flag)
570 dev_t dev;
571 struct uio *uio;
572 int flag;
573 {
574 struct pt_softc *pti = pt_softc[minor(dev)];
575 struct tty *tp = pti->pt_tty;
576 u_char *cp = NULL;
577 int cc = 0;
578 u_char locbuf[BUFSIZ];
579 int cnt = 0;
580 int error = 0;
581
582 again:
583 if (!ISSET(tp->t_state, TS_ISOPEN))
584 goto block;
585 if (pti->pt_flags & PF_REMOTE) {
586 if (tp->t_canq.c_cc)
587 goto block;
588 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
589 if (cc == 0) {
590 cc = min(uio->uio_resid, BUFSIZ);
591 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
592 cp = locbuf;
593 error = uiomove((caddr_t)cp, cc, uio);
594 if (error)
595 return (error);
596 /* check again for safety */
597 if (!ISSET(tp->t_state, TS_ISOPEN))
598 return (EIO);
599 }
600 if (cc)
601 (void) b_to_q((char *)cp, cc, &tp->t_canq);
602 cc = 0;
603 }
604 (void) putc(0, &tp->t_canq);
605 ttwakeup(tp);
606 wakeup((caddr_t)&tp->t_canq);
607 return (0);
608 }
609 while (uio->uio_resid > 0) {
610 if (cc == 0) {
611 cc = min(uio->uio_resid, BUFSIZ);
612 cp = locbuf;
613 error = uiomove((caddr_t)cp, cc, uio);
614 if (error)
615 return (error);
616 /* check again for safety */
617 if (!ISSET(tp->t_state, TS_ISOPEN))
618 return (EIO);
619 }
620 while (cc > 0) {
621 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
622 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
623 wakeup((caddr_t)&tp->t_rawq);
624 goto block;
625 }
626 (*linesw[tp->t_line].l_rint)(*cp++, tp);
627 cnt++;
628 cc--;
629 }
630 cc = 0;
631 }
632 return (0);
633 block:
634 /*
635 * Come here to wait for slave to open, for space
636 * in outq, or space in rawq.
637 */
638 if (!ISSET(tp->t_state, TS_CARR_ON))
639 return (EIO);
640 if (flag & IO_NDELAY) {
641 /* adjust for data copied in but not written */
642 uio->uio_resid += cc;
643 if (cnt == 0)
644 return (EWOULDBLOCK);
645 return (0);
646 }
647 error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
648 ttyout, 0);
649 if (error) {
650 /* adjust for data copied in but not written */
651 uio->uio_resid += cc;
652 return (error);
653 }
654 goto again;
655 }
656
657 int
658 ptcpoll(dev, events, p)
659 dev_t dev;
660 int events;
661 struct proc *p;
662 {
663 struct pt_softc *pti = pt_softc[minor(dev)];
664 struct tty *tp = pti->pt_tty;
665 int revents = 0;
666 int s = splsoftclock();
667
668 if (events & (POLLIN | POLLRDNORM))
669 if (ISSET(tp->t_state, TS_ISOPEN) &&
670 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
671 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
672 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
673 revents |= events & (POLLIN | POLLRDNORM);
674
675 if (events & (POLLOUT | POLLWRNORM))
676 if (ISSET(tp->t_state, TS_ISOPEN) &&
677 ((pti->pt_flags & PF_REMOTE) ?
678 (tp->t_canq.c_cc == 0) :
679 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
680 (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
681 revents |= events & (POLLOUT | POLLWRNORM);
682
683 if (events & POLLHUP)
684 if (!ISSET(tp->t_state, TS_CARR_ON))
685 revents |= POLLHUP;
686
687 if (revents == 0) {
688 if (events & (POLLIN | POLLHUP | POLLRDNORM))
689 selrecord(p, &pti->pt_selr);
690
691 if (events & (POLLOUT | POLLWRNORM))
692 selrecord(p, &pti->pt_selw);
693 }
694
695 splx(s);
696 return (revents);
697 }
698
699
700 struct tty *
701 ptytty(dev)
702 dev_t dev;
703 {
704 struct pt_softc *pti = pt_softc[minor(dev)];
705 struct tty *tp = pti->pt_tty;
706
707 return (tp);
708 }
709
710 /*ARGSUSED*/
711 int
712 ptyioctl(dev, cmd, data, flag, p)
713 dev_t dev;
714 u_long cmd;
715 caddr_t data;
716 int flag;
717 struct proc *p;
718 {
719 struct pt_softc *pti = pt_softc[minor(dev)];
720 struct tty *tp = pti->pt_tty;
721 u_char *cc = tp->t_cc;
722 int stop, error, sig;
723
724 /*
725 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
726 * ttywflush(tp) will hang if there are characters in the outq.
727 */
728 if (cmd == TIOCEXT) {
729 /*
730 * When the EXTPROC bit is being toggled, we need
731 * to send an TIOCPKT_IOCTL if the packet driver
732 * is turned on.
733 */
734 if (*(int *)data) {
735 if (pti->pt_flags & PF_PKT) {
736 pti->pt_send |= TIOCPKT_IOCTL;
737 ptcwakeup(tp, FREAD);
738 }
739 SET(tp->t_lflag, EXTPROC);
740 } else {
741 if (ISSET(tp->t_lflag, EXTPROC) &&
742 (pti->pt_flags & PF_PKT)) {
743 pti->pt_send |= TIOCPKT_IOCTL;
744 ptcwakeup(tp, FREAD);
745 }
746 CLR(tp->t_lflag, EXTPROC);
747 }
748 return(0);
749 } else
750 if (cdevsw[major(dev)].d_open == ptcopen)
751 switch (cmd) {
752
753 case TIOCGPGRP:
754 #ifdef COMPAT_SUNOS
755 {
756 /*
757 * I'm not sure about SunOS TIOCGPGRP semantics
758 * on PTYs, but it's something like this:
759 */
760 extern struct emul emul_sunos;
761 if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
762 return (EIO);
763 *(int *)data = tp->t_pgrp->pg_id;
764 return (0);
765 }
766 #endif
767 /*
768 * We avoid calling ttioctl on the controller since,
769 * in that case, tp must be the controlling terminal.
770 */
771 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
772 return (0);
773
774 case TIOCPKT:
775 if (*(int *)data) {
776 if (pti->pt_flags & PF_UCNTL)
777 return (EINVAL);
778 pti->pt_flags |= PF_PKT;
779 } else
780 pti->pt_flags &= ~PF_PKT;
781 return (0);
782
783 case TIOCUCNTL:
784 if (*(int *)data) {
785 if (pti->pt_flags & PF_PKT)
786 return (EINVAL);
787 pti->pt_flags |= PF_UCNTL;
788 } else
789 pti->pt_flags &= ~PF_UCNTL;
790 return (0);
791
792 case TIOCREMOTE:
793 if (*(int *)data)
794 pti->pt_flags |= PF_REMOTE;
795 else
796 pti->pt_flags &= ~PF_REMOTE;
797 ttyflush(tp, FREAD|FWRITE);
798 return (0);
799
800 #ifdef COMPAT_OLDTTY
801 case TIOCSETP:
802 case TIOCSETN:
803 #endif
804 case TIOCSETD:
805 case TIOCSETA:
806 case TIOCSETAW:
807 case TIOCSETAF:
808 ndflush(&tp->t_outq, tp->t_outq.c_cc);
809 break;
810
811 case TIOCSIG:
812 sig = (int)(long)*(caddr_t *)data;
813 if (sig <= 0 || sig >= NSIG)
814 return (EINVAL);
815 if (!ISSET(tp->t_lflag, NOFLSH))
816 ttyflush(tp, FREAD|FWRITE);
817 pgsignal(tp->t_pgrp, sig, 1);
818 if ((sig == SIGINFO) &&
819 (!ISSET(tp->t_lflag, NOKERNINFO)))
820 ttyinfo(tp);
821 return(0);
822 }
823 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
824 if (error < 0)
825 error = ttioctl(tp, cmd, data, flag, p);
826 if (error < 0) {
827 if (pti->pt_flags & PF_UCNTL &&
828 (cmd & ~0xff) == UIOCCMD(0)) {
829 if (cmd & 0xff) {
830 pti->pt_ucntl = (u_char)cmd;
831 ptcwakeup(tp, FREAD);
832 }
833 return (0);
834 }
835 error = ENOTTY;
836 }
837 /*
838 * If external processing and packet mode send ioctl packet.
839 */
840 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
841 switch(cmd) {
842 case TIOCSETA:
843 case TIOCSETAW:
844 case TIOCSETAF:
845 #ifdef COMPAT_OLDTTY
846 case TIOCSETP:
847 case TIOCSETN:
848 case TIOCSETC:
849 case TIOCSLTC:
850 case TIOCLBIS:
851 case TIOCLBIC:
852 case TIOCLSET:
853 #endif
854 pti->pt_send |= TIOCPKT_IOCTL;
855 ptcwakeup(tp, FREAD);
856 default:
857 break;
858 }
859 }
860 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
861 && CCEQ(cc[VSTART], CTRL('q'));
862 if (pti->pt_flags & PF_NOSTOP) {
863 if (stop) {
864 pti->pt_send &= ~TIOCPKT_NOSTOP;
865 pti->pt_send |= TIOCPKT_DOSTOP;
866 pti->pt_flags &= ~PF_NOSTOP;
867 ptcwakeup(tp, FREAD);
868 }
869 } else {
870 if (!stop) {
871 pti->pt_send &= ~TIOCPKT_DOSTOP;
872 pti->pt_send |= TIOCPKT_NOSTOP;
873 pti->pt_flags |= PF_NOSTOP;
874 ptcwakeup(tp, FREAD);
875 }
876 }
877 return (error);
878 }
879