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