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