tty_pty.c revision 1.74 1 /* $NetBSD: tty_pty.c,v 1.74 2004/03/05 07:27:22 dbj 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
32 */
33
34 /*
35 * Pseudo-teletype Driver
36 * (Actually two drivers, requiring two entries in 'cdevsw')
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.74 2004/03/05 07:27:22 dbj Exp $");
41
42 #include "opt_compat_sunos.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/ioctl.h>
47 #include <sys/proc.h>
48 #include <sys/tty.h>
49 #include <sys/file.h>
50 #include <sys/uio.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <sys/signalvar.h>
54 #include <sys/uio.h>
55 #include <sys/conf.h>
56 #include <sys/poll.h>
57 #include <sys/malloc.h>
58
59 #define DEFAULT_NPTYS 16 /* default number of initial ptys */
60 #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
61
62 /* Macros to clear/set/test flags. */
63 #define SET(t, f) (t) |= (f)
64 #define CLR(t, f) (t) &= ~((unsigned)(f))
65 #define ISSET(t, f) ((t) & (f))
66
67 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
68
69 /*
70 * pts == /dev/tty[pqrs]?
71 * ptc == /dev/pty[pqrs]?
72 */
73 struct pt_softc {
74 struct tty *pt_tty;
75 int pt_flags;
76 struct selinfo pt_selr, pt_selw;
77 u_char pt_send;
78 u_char pt_ucntl;
79 };
80
81 static struct pt_softc **pt_softc = NULL; /* pty array */
82 static int npty = 0; /* for pstat -t */
83 static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
84 static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
85
86 #define PF_PKT 0x08 /* packet mode */
87 #define PF_STOPPED 0x10 /* user told stopped */
88 #define PF_REMOTE 0x20 /* remote and flow controlled input */
89 #define PF_NOSTOP 0x40
90 #define PF_UCNTL 0x80 /* user control mode */
91
92 void ptyattach __P((int));
93 void ptcwakeup __P((struct tty *, int));
94 void ptsstart __P((struct tty *));
95 int pty_maxptys __P((int, int));
96
97 static struct pt_softc **ptyarralloc __P((int));
98 static int check_pty(int);
99
100 dev_type_open(ptcopen);
101 dev_type_close(ptcclose);
102 dev_type_read(ptcread);
103 dev_type_write(ptcwrite);
104 dev_type_poll(ptcpoll);
105 dev_type_kqfilter(ptckqfilter);
106
107 dev_type_open(ptsopen);
108 dev_type_close(ptsclose);
109 dev_type_read(ptsread);
110 dev_type_write(ptswrite);
111 dev_type_stop(ptsstop);
112 dev_type_poll(ptspoll);
113
114 dev_type_ioctl(ptyioctl);
115 dev_type_tty(ptytty);
116
117 const struct cdevsw ptc_cdevsw = {
118 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
119 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
120 };
121
122 const struct cdevsw pts_cdevsw = {
123 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
124 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
125 };
126
127 #if defined(pmax)
128 const struct cdevsw ptc_ultrix_cdevsw = {
129 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
130 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
131 };
132
133 const struct cdevsw pts_ultrix_cdevsw = {
134 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
135 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
136 };
137 #endif /* defined(pmax) */
138
139 /*
140 * Allocate and zero array of nelem elements.
141 */
142 static struct pt_softc **
143 ptyarralloc(nelem)
144 int nelem;
145 {
146 struct pt_softc **pt;
147 nelem += 10;
148 pt = malloc(nelem * sizeof *pt, M_DEVBUF, M_WAITOK | M_ZERO);
149 return pt;
150 }
151
152 /*
153 * Check if the minor is correct and ensure necessary structures
154 * are properly allocated.
155 */
156 static int
157 check_pty(int ptn)
158 {
159 struct pt_softc *pti;
160
161 if (ptn >= npty) {
162 struct pt_softc **newpt, **oldpt;
163 int newnpty;
164
165 /* check if the requested pty can be granted */
166 if (ptn >= maxptys) {
167 limit_reached:
168 tablefull("pty", "increase kern.maxptys");
169 return (ENXIO);
170 }
171
172 /* Allocate a larger pty array */
173 for (newnpty = npty; newnpty <= ptn;)
174 newnpty *= 2;
175 if (newnpty > maxptys)
176 newnpty = maxptys;
177 newpt = ptyarralloc(newnpty);
178
179 /*
180 * Now grab the pty array mutex - we need to ensure
181 * that the pty array is consistent while copying it's
182 * content to newly allocated, larger space; we also
183 * need to be safe against pty_maxptys().
184 */
185 simple_lock(&pt_softc_mutex);
186
187 if (newnpty >= maxptys) {
188 /* limit cut away beneath us... */
189 newnpty = maxptys;
190 if (ptn >= newnpty) {
191 simple_unlock(&pt_softc_mutex);
192 free(newpt, M_DEVBUF);
193 goto limit_reached;
194 }
195 }
196
197 /*
198 * If the pty array was not enlarged while we were waiting
199 * for mutex, copy current contents of pt_softc[] to newly
200 * allocated array and start using the new bigger array.
201 */
202 if (newnpty > npty) {
203 memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
204 oldpt = pt_softc;
205 pt_softc = newpt;
206 npty = newnpty;
207 } else {
208 /* was enlarged when waited for lock, free new space */
209 oldpt = newpt;
210 }
211
212 simple_unlock(&pt_softc_mutex);
213 free(oldpt, M_DEVBUF);
214 }
215
216 /*
217 * If the entry is not yet allocated, allocate one. The mutex is
218 * needed so that the state of pt_softc[] array is consistant
219 * in case it has been lengthened above.
220 */
221 if (!pt_softc[ptn]) {
222 MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
223 M_DEVBUF, M_WAITOK);
224 memset(pti, 0, sizeof(struct pt_softc));
225
226 pti->pt_tty = ttymalloc();
227
228 simple_lock(&pt_softc_mutex);
229
230 /*
231 * Check the entry again - it might have been
232 * added while we were waiting for mutex.
233 */
234 if (!pt_softc[ptn]) {
235 tty_attach(pti->pt_tty);
236 pt_softc[ptn] = pti;
237 } else {
238 ttyfree(pti->pt_tty);
239 free(pti, M_DEVBUF);
240 }
241
242 simple_unlock(&pt_softc_mutex);
243 }
244
245 return (0);
246 }
247
248 /*
249 * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
250 * new value of maxptys.
251 */
252 int
253 pty_maxptys(newmax, set)
254 int newmax, set;
255 {
256 if (!set)
257 return (maxptys);
258
259 /*
260 * We have to grab the pt_softc lock, so that we would pick correct
261 * value of npty (might be modified in check_pty()).
262 */
263 simple_lock(&pt_softc_mutex);
264
265 /*
266 * The value cannot be set to value lower than the highest pty
267 * number ever allocated.
268 */
269 if (newmax >= npty)
270 maxptys = newmax;
271 else
272 newmax = 0;
273
274 simple_unlock(&pt_softc_mutex);
275
276 return newmax;
277 }
278
279 /*
280 * Establish n (or default if n is 1) ptys in the system.
281 */
282 void
283 ptyattach(n)
284 int n;
285 {
286 /* maybe should allow 0 => none? */
287 if (n <= 1)
288 n = DEFAULT_NPTYS;
289 pt_softc = ptyarralloc(n);
290 npty = n;
291 }
292
293 /*ARGSUSED*/
294 int
295 ptsopen(dev, flag, devtype, p)
296 dev_t dev;
297 int flag, devtype;
298 struct proc *p;
299 {
300 struct pt_softc *pti;
301 struct tty *tp;
302 int error;
303 int ptn = minor(dev);
304
305 if ((error = check_pty(ptn)))
306 return (error);
307
308 pti = pt_softc[ptn];
309 tp = pti->pt_tty;
310
311 if (!ISSET(tp->t_state, TS_ISOPEN)) {
312 ttychars(tp); /* Set up default chars */
313 tp->t_iflag = TTYDEF_IFLAG;
314 tp->t_oflag = TTYDEF_OFLAG;
315 tp->t_lflag = TTYDEF_LFLAG;
316 tp->t_cflag = TTYDEF_CFLAG;
317 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
318 ttsetwater(tp); /* would be done in xxparam() */
319 } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
320 return (EBUSY);
321 if (tp->t_oproc) /* Ctrlr still around. */
322 SET(tp->t_state, TS_CARR_ON);
323
324 if (!ISSET(flag, O_NONBLOCK)) {
325 TTY_LOCK(tp);
326 while (!ISSET(tp->t_state, TS_CARR_ON)) {
327 tp->t_wopen++;
328 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
329 ttopen, 0);
330 tp->t_wopen--;
331 if (error) {
332 TTY_UNLOCK(tp);
333 return (error);
334 }
335 }
336 TTY_UNLOCK(tp);
337 }
338 error = (*tp->t_linesw->l_open)(dev, tp);
339 ptcwakeup(tp, FREAD|FWRITE);
340 return (error);
341 }
342
343 int
344 ptsclose(dev, flag, mode, p)
345 dev_t dev;
346 int flag, mode;
347 struct proc *p;
348 {
349 struct pt_softc *pti = pt_softc[minor(dev)];
350 struct tty *tp = pti->pt_tty;
351 int error;
352
353 error = (*tp->t_linesw->l_close)(tp, flag);
354 error |= ttyclose(tp);
355 ptcwakeup(tp, FREAD|FWRITE);
356 return (error);
357 }
358
359 int
360 ptsread(dev, uio, flag)
361 dev_t dev;
362 struct uio *uio;
363 int flag;
364 {
365 struct proc *p = curproc;
366 struct pt_softc *pti = pt_softc[minor(dev)];
367 struct tty *tp = pti->pt_tty;
368 int error = 0;
369 int cc;
370
371 again:
372 if (pti->pt_flags & PF_REMOTE) {
373 while (isbackground(p, tp)) {
374 if (sigismasked(p, SIGTTIN) ||
375 p->p_pgrp->pg_jobc == 0 ||
376 p->p_flag & P_PPWAIT)
377 return (EIO);
378 pgsignal(p->p_pgrp, SIGTTIN, 1);
379 TTY_LOCK(tp);
380 error = ttysleep(tp, (caddr_t)&lbolt,
381 TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
382 if (error)
383 return (error);
384 }
385 TTY_LOCK(tp);
386 if (tp->t_canq.c_cc == 0) {
387 if (flag & IO_NDELAY) {
388 TTY_UNLOCK(tp);
389 return (EWOULDBLOCK);
390 }
391 error = ttysleep(tp, (caddr_t)&tp->t_canq,
392 TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
393 if (error)
394 return (error);
395 goto again;
396 }
397 while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
398 TTY_UNLOCK(tp);
399 error = ureadc(getc(&tp->t_canq), uio);
400 TTY_LOCK(tp);
401 /* Re-check terminal state here? */
402 }
403 if (tp->t_canq.c_cc == 1)
404 (void) getc(&tp->t_canq);
405 cc = tp->t_canq.c_cc;
406 TTY_UNLOCK(tp);
407 if (cc)
408 return (error);
409 } else
410 if (tp->t_oproc)
411 error = (*tp->t_linesw->l_read)(tp, uio, flag);
412 ptcwakeup(tp, FWRITE);
413 return (error);
414 }
415
416 /*
417 * Write to pseudo-tty.
418 * Wakeups of controlling tty will happen
419 * indirectly, when tty driver calls ptsstart.
420 */
421 int
422 ptswrite(dev, uio, flag)
423 dev_t dev;
424 struct uio *uio;
425 int flag;
426 {
427 struct pt_softc *pti = pt_softc[minor(dev)];
428 struct tty *tp = pti->pt_tty;
429
430 if (tp->t_oproc == 0)
431 return (EIO);
432 return ((*tp->t_linesw->l_write)(tp, uio, flag));
433 }
434
435 /*
436 * Poll pseudo-tty.
437 */
438 int
439 ptspoll(dev, events, p)
440 dev_t dev;
441 int events;
442 struct proc *p;
443 {
444 struct pt_softc *pti = pt_softc[minor(dev)];
445 struct tty *tp = pti->pt_tty;
446
447 if (tp->t_oproc == 0)
448 return (EIO);
449
450 return ((*tp->t_linesw->l_poll)(tp, events, p));
451 }
452
453 /*
454 * Start output on pseudo-tty.
455 * Wake up process polling or sleeping for input from controlling tty.
456 * Called with tty slock held.
457 */
458 void
459 ptsstart(tp)
460 struct tty *tp;
461 {
462 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
463
464 if (ISSET(tp->t_state, TS_TTSTOP))
465 return;
466 if (pti->pt_flags & PF_STOPPED) {
467 pti->pt_flags &= ~PF_STOPPED;
468 pti->pt_send = TIOCPKT_START;
469 }
470
471 selnotify(&pti->pt_selr, NOTE_SUBMIT);
472 wakeup((caddr_t)&tp->t_outq.c_cf);
473 }
474
475 /*
476 * Stop output.
477 * Called with tty slock held.
478 */
479 void
480 ptsstop(tp, flush)
481 struct tty *tp;
482 int flush;
483 {
484 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
485
486 /* note: FLUSHREAD and FLUSHWRITE already ok */
487 if (flush == 0) {
488 flush = TIOCPKT_STOP;
489 pti->pt_flags |= PF_STOPPED;
490 } else
491 pti->pt_flags &= ~PF_STOPPED;
492 pti->pt_send |= flush;
493
494 /* change of perspective */
495 if (flush & FREAD) {
496 selnotify(&pti->pt_selw, NOTE_SUBMIT);
497 wakeup((caddr_t)&tp->t_rawq.c_cf);
498 }
499 if (flush & FWRITE) {
500 selnotify(&pti->pt_selr, NOTE_SUBMIT);
501 wakeup((caddr_t)&tp->t_outq.c_cf);
502 }
503 }
504
505 void
506 ptcwakeup(tp, flag)
507 struct tty *tp;
508 int flag;
509 {
510 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
511 int s;
512
513 s = spltty();
514 TTY_LOCK(tp);
515 if (flag & FREAD) {
516 selnotify(&pti->pt_selr, NOTE_SUBMIT);
517 wakeup((caddr_t)&tp->t_outq.c_cf);
518 }
519 if (flag & FWRITE) {
520 selnotify(&pti->pt_selw, NOTE_SUBMIT);
521 wakeup((caddr_t)&tp->t_rawq.c_cf);
522 }
523 TTY_UNLOCK(tp);
524 splx(s);
525 }
526
527 /*ARGSUSED*/
528 int
529 ptcopen(dev, flag, devtype, p)
530 dev_t dev;
531 int flag, devtype;
532 struct proc *p;
533 {
534 struct pt_softc *pti;
535 struct tty *tp;
536 int error;
537 int ptn = minor(dev);
538
539 if ((error = check_pty(ptn)))
540 return (error);
541
542 pti = pt_softc[ptn];
543 tp = pti->pt_tty;
544
545 TTY_LOCK(tp);
546 if (tp->t_oproc) {
547 TTY_UNLOCK(tp);
548 return (EIO);
549 }
550 tp->t_oproc = ptsstart;
551 TTY_UNLOCK(tp);
552 (void)(*tp->t_linesw->l_modem)(tp, 1);
553 CLR(tp->t_lflag, EXTPROC);
554 pti->pt_flags = 0;
555 pti->pt_send = 0;
556 pti->pt_ucntl = 0;
557 return (0);
558 }
559
560 /*ARGSUSED*/
561 int
562 ptcclose(dev, flag, devtype, p)
563 dev_t dev;
564 int flag, devtype;
565 struct proc *p;
566 {
567 struct pt_softc *pti = pt_softc[minor(dev)];
568 struct tty *tp = pti->pt_tty;
569
570 (void)(*tp->t_linesw->l_modem)(tp, 0);
571 CLR(tp->t_state, TS_CARR_ON);
572 TTY_LOCK(tp);
573 tp->t_oproc = 0; /* mark closed */
574 TTY_UNLOCK(tp);
575 return (0);
576 }
577
578 int
579 ptcread(dev, uio, flag)
580 dev_t dev;
581 struct uio *uio;
582 int flag;
583 {
584 struct pt_softc *pti = pt_softc[minor(dev)];
585 struct tty *tp = pti->pt_tty;
586 u_char buf[BUFSIZ];
587 int error = 0, cc;
588
589 /*
590 * We want to block until the slave
591 * is open, and there's something to read;
592 * but if we lost the slave or we're NBIO,
593 * then return the appropriate error instead.
594 */
595 TTY_LOCK(tp);
596 for (;;) {
597 if (ISSET(tp->t_state, TS_ISOPEN)) {
598 if (pti->pt_flags & PF_PKT && pti->pt_send) {
599 TTY_UNLOCK(tp);
600 error = ureadc((int)pti->pt_send, uio);
601 if (error)
602 return (error);
603 /*
604 * Since we don't have the tty locked, there's
605 * a risk of messing up `t_termios'. This is
606 * relevant only if the tty got closed and then
607 * opened again while we were out uiomoving.
608 */
609 if (pti->pt_send & TIOCPKT_IOCTL) {
610 cc = min(uio->uio_resid,
611 sizeof(tp->t_termios));
612 uiomove((caddr_t) &tp->t_termios,
613 cc, uio);
614 }
615 pti->pt_send = 0;
616 return (0);
617 }
618 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
619 TTY_UNLOCK(tp);
620 error = ureadc((int)pti->pt_ucntl, uio);
621 if (error)
622 return (error);
623 pti->pt_ucntl = 0;
624 return (0);
625 }
626 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
627 break;
628 }
629 if (!ISSET(tp->t_state, TS_CARR_ON)) {
630 error = 0; /* EOF */
631 goto out;
632 }
633 if (flag & IO_NDELAY) {
634 error = EWOULDBLOCK;
635 goto out;
636 }
637 error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
638 ttyin, 0, &tp->t_slock);
639 if (error)
640 goto out;
641 }
642
643 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
644 TTY_UNLOCK(tp);
645 error = ureadc(0, uio);
646 TTY_LOCK(tp);
647 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
648 error = EIO;
649 }
650 while (uio->uio_resid > 0 && error == 0) {
651 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
652 if (cc <= 0)
653 break;
654 TTY_UNLOCK(tp);
655 error = uiomove(buf, cc, uio);
656 TTY_LOCK(tp);
657 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
658 error = EIO;
659 }
660
661 if (tp->t_outq.c_cc <= tp->t_lowat) {
662 if (ISSET(tp->t_state, TS_ASLEEP)) {
663 CLR(tp->t_state, TS_ASLEEP);
664 wakeup((caddr_t)&tp->t_outq);
665 }
666 selnotify(&tp->t_wsel, NOTE_SUBMIT);
667 }
668 out:
669 TTY_UNLOCK(tp);
670 return (error);
671 }
672
673
674 int
675 ptcwrite(dev, uio, flag)
676 dev_t dev;
677 struct uio *uio;
678 int flag;
679 {
680 struct pt_softc *pti = pt_softc[minor(dev)];
681 struct tty *tp = pti->pt_tty;
682 u_char *cp = NULL;
683 int cc = 0;
684 u_char locbuf[BUFSIZ];
685 int cnt = 0;
686 int error = 0;
687
688 again:
689 TTY_LOCK(tp);
690 if (!ISSET(tp->t_state, TS_ISOPEN))
691 goto block;
692 if (pti->pt_flags & PF_REMOTE) {
693 if (tp->t_canq.c_cc)
694 goto block;
695 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
696 if (cc == 0) {
697 cc = min(uio->uio_resid, BUFSIZ);
698 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
699 cp = locbuf;
700 TTY_UNLOCK(tp);
701 error = uiomove((caddr_t)cp, cc, uio);
702 if (error)
703 return (error);
704 TTY_LOCK(tp);
705 /* check again for safety */
706 if (!ISSET(tp->t_state, TS_ISOPEN)) {
707 error = EIO;
708 goto out;
709 }
710 }
711 if (cc)
712 (void) b_to_q(cp, cc, &tp->t_canq);
713 cc = 0;
714 }
715 (void) putc(0, &tp->t_canq);
716 ttwakeup(tp);
717 wakeup((caddr_t)&tp->t_canq);
718 error = 0;
719 goto out;
720 }
721 while (uio->uio_resid > 0) {
722 if (cc == 0) {
723 cc = min(uio->uio_resid, BUFSIZ);
724 cp = locbuf;
725 TTY_UNLOCK(tp);
726 error = uiomove((caddr_t)cp, cc, uio);
727 if (error)
728 return (error);
729 TTY_LOCK(tp);
730 /* check again for safety */
731 if (!ISSET(tp->t_state, TS_ISOPEN)) {
732 error = EIO;
733 goto out;
734 }
735 }
736 while (cc > 0) {
737 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
738 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
739 wakeup((caddr_t)&tp->t_rawq);
740 goto block;
741 }
742 /* XXX - should change l_rint to be called with lock
743 * see also tty.c:ttyinput_wlock()
744 */
745 TTY_UNLOCK(tp);
746 (*tp->t_linesw->l_rint)(*cp++, tp);
747 TTY_LOCK(tp);
748 cnt++;
749 cc--;
750 }
751 cc = 0;
752 }
753 error = 0;
754 goto out;
755
756 block:
757 /*
758 * Come here to wait for slave to open, for space
759 * in outq, or space in rawq.
760 */
761 if (!ISSET(tp->t_state, TS_CARR_ON)) {
762 error = EIO;
763 goto out;
764 }
765 if (flag & IO_NDELAY) {
766 /* adjust for data copied in but not written */
767 uio->uio_resid += cc;
768 error = cnt == 0 ? EWOULDBLOCK : 0;
769 goto out;
770 }
771 error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
772 ttyout, 0, &tp->t_slock);
773 if (error) {
774 /* adjust for data copied in but not written */
775 uio->uio_resid += cc;
776 return (error);
777 }
778 goto again;
779
780 out:
781 TTY_UNLOCK(tp);
782 return (error);
783 }
784
785 int
786 ptcpoll(dev, events, p)
787 dev_t dev;
788 int events;
789 struct proc *p;
790 {
791 struct pt_softc *pti = pt_softc[minor(dev)];
792 struct tty *tp = pti->pt_tty;
793 int revents = 0;
794 int s = splsoftclock();
795
796 if (events & (POLLIN | POLLRDNORM))
797 if (ISSET(tp->t_state, TS_ISOPEN) &&
798 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
799 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
800 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
801 revents |= events & (POLLIN | POLLRDNORM);
802
803 if (events & (POLLOUT | POLLWRNORM))
804 if (ISSET(tp->t_state, TS_ISOPEN) &&
805 ((pti->pt_flags & PF_REMOTE) ?
806 (tp->t_canq.c_cc == 0) :
807 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
808 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
809 revents |= events & (POLLOUT | POLLWRNORM);
810
811 if (events & POLLHUP)
812 if (!ISSET(tp->t_state, TS_CARR_ON))
813 revents |= POLLHUP;
814
815 if (revents == 0) {
816 if (events & (POLLIN | POLLHUP | POLLRDNORM))
817 selrecord(p, &pti->pt_selr);
818
819 if (events & (POLLOUT | POLLWRNORM))
820 selrecord(p, &pti->pt_selw);
821 }
822
823 splx(s);
824 return (revents);
825 }
826
827 static void
828 filt_ptcrdetach(struct knote *kn)
829 {
830 struct pt_softc *pti;
831 int s;
832
833 pti = kn->kn_hook;
834 s = spltty();
835 SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
836 splx(s);
837 }
838
839 static int
840 filt_ptcread(struct knote *kn, long hint)
841 {
842 struct pt_softc *pti;
843 struct tty *tp;
844 int canread;
845
846 pti = kn->kn_hook;
847 tp = pti->pt_tty;
848
849 canread = (ISSET(tp->t_state, TS_ISOPEN) &&
850 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
851 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
852 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
853
854 if (canread) {
855 /*
856 * c_cc is number of characters after output post-processing;
857 * the amount of data actually read(2) depends on
858 * setting of input flags for the terminal.
859 */
860 kn->kn_data = tp->t_outq.c_cc;
861 if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
862 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
863 kn->kn_data++;
864 }
865
866 return (canread);
867 }
868
869 static void
870 filt_ptcwdetach(struct knote *kn)
871 {
872 struct pt_softc *pti;
873 int s;
874
875 pti = kn->kn_hook;
876 s = spltty();
877 SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
878 splx(s);
879 }
880
881 static int
882 filt_ptcwrite(struct knote *kn, long hint)
883 {
884 struct pt_softc *pti;
885 struct tty *tp;
886 int canwrite;
887 int nwrite;
888
889 pti = kn->kn_hook;
890 tp = pti->pt_tty;
891
892 canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
893 ((pti->pt_flags & PF_REMOTE) ?
894 (tp->t_canq.c_cc == 0) :
895 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
896 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
897
898 if (canwrite) {
899 if (pti->pt_flags & PF_REMOTE)
900 nwrite = tp->t_canq.c_cn;
901 else {
902 /* this is guaranteed to be > 0 due to above check */
903 nwrite = tp->t_canq.c_cn
904 - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
905 }
906 kn->kn_data = nwrite;
907 }
908
909 return (canwrite);
910 }
911
912 static const struct filterops ptcread_filtops =
913 { 1, NULL, filt_ptcrdetach, filt_ptcread };
914 static const struct filterops ptcwrite_filtops =
915 { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
916
917 int
918 ptckqfilter(dev_t dev, struct knote *kn)
919 {
920 struct pt_softc *pti = pt_softc[minor(dev)];
921 struct klist *klist;
922 int s;
923
924 switch (kn->kn_filter) {
925 case EVFILT_READ:
926 klist = &pti->pt_selr.sel_klist;
927 kn->kn_fop = &ptcread_filtops;
928 break;
929 case EVFILT_WRITE:
930 klist = &pti->pt_selw.sel_klist;
931 kn->kn_fop = &ptcwrite_filtops;
932 break;
933 default:
934 return (1);
935 }
936
937 kn->kn_hook = pti;
938
939 s = spltty();
940 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
941 splx(s);
942
943 return (0);
944 }
945
946 struct tty *
947 ptytty(dev)
948 dev_t dev;
949 {
950 struct pt_softc *pti = pt_softc[minor(dev)];
951 struct tty *tp = pti->pt_tty;
952
953 return (tp);
954 }
955
956 /*ARGSUSED*/
957 int
958 ptyioctl(dev, cmd, data, flag, p)
959 dev_t dev;
960 u_long cmd;
961 caddr_t data;
962 int flag;
963 struct proc *p;
964 {
965 struct pt_softc *pti = pt_softc[minor(dev)];
966 struct tty *tp = pti->pt_tty;
967 const struct cdevsw *cdev;
968 u_char *cc = tp->t_cc;
969 int stop, error, sig;
970
971 cdev = cdevsw_lookup(dev);
972 /*
973 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
974 * ttywflush(tp) will hang if there are characters in the outq.
975 */
976 if (cmd == TIOCEXT) {
977 /*
978 * When the EXTPROC bit is being toggled, we need
979 * to send an TIOCPKT_IOCTL if the packet driver
980 * is turned on.
981 */
982 if (*(int *)data) {
983 if (pti->pt_flags & PF_PKT) {
984 pti->pt_send |= TIOCPKT_IOCTL;
985 ptcwakeup(tp, FREAD);
986 }
987 SET(tp->t_lflag, EXTPROC);
988 } else {
989 if (ISSET(tp->t_lflag, EXTPROC) &&
990 (pti->pt_flags & PF_PKT)) {
991 pti->pt_send |= TIOCPKT_IOCTL;
992 ptcwakeup(tp, FREAD);
993 }
994 CLR(tp->t_lflag, EXTPROC);
995 }
996 return(0);
997 }
998
999 if (cdev != NULL && cdev->d_open == ptcopen)
1000 switch (cmd) {
1001
1002 case TIOCGPGRP:
1003 /*
1004 * We avoid calling ttioctl on the controller since,
1005 * in that case, tp must be the controlling terminal.
1006 */
1007 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1008 return (0);
1009
1010 case TIOCPKT:
1011 if (*(int *)data) {
1012 if (pti->pt_flags & PF_UCNTL)
1013 return (EINVAL);
1014 pti->pt_flags |= PF_PKT;
1015 } else
1016 pti->pt_flags &= ~PF_PKT;
1017 return (0);
1018
1019 case TIOCUCNTL:
1020 if (*(int *)data) {
1021 if (pti->pt_flags & PF_PKT)
1022 return (EINVAL);
1023 pti->pt_flags |= PF_UCNTL;
1024 } else
1025 pti->pt_flags &= ~PF_UCNTL;
1026 return (0);
1027
1028 case TIOCREMOTE:
1029 if (*(int *)data)
1030 pti->pt_flags |= PF_REMOTE;
1031 else
1032 pti->pt_flags &= ~PF_REMOTE;
1033 TTY_LOCK(tp);
1034 ttyflush(tp, FREAD|FWRITE);
1035 TTY_UNLOCK(tp);
1036 return (0);
1037
1038 #ifdef COMPAT_OLDTTY
1039 case TIOCSETP:
1040 case TIOCSETN:
1041 #endif
1042 case TIOCSETD:
1043 case TIOCSETA:
1044 case TIOCSETAW:
1045 case TIOCSETAF:
1046 TTY_LOCK(tp);
1047 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1048 TTY_UNLOCK(tp);
1049 break;
1050
1051 case TIOCSIG:
1052 sig = (int)(long)*(caddr_t *)data;
1053 if (sig <= 0 || sig >= NSIG)
1054 return (EINVAL);
1055 TTY_LOCK(tp);
1056 if (!ISSET(tp->t_lflag, NOFLSH))
1057 ttyflush(tp, FREAD|FWRITE);
1058 if ((sig == SIGINFO) &&
1059 (!ISSET(tp->t_lflag, NOKERNINFO)))
1060 ttyinfo(tp);
1061 TTY_UNLOCK(tp);
1062 pgsignal(tp->t_pgrp, sig, 1);
1063 return(0);
1064 }
1065
1066 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
1067 if (error == EPASSTHROUGH)
1068 error = ttioctl(tp, cmd, data, flag, p);
1069 if (error == EPASSTHROUGH) {
1070 if (pti->pt_flags & PF_UCNTL &&
1071 (cmd & ~0xff) == UIOCCMD(0)) {
1072 if (cmd & 0xff) {
1073 pti->pt_ucntl = (u_char)cmd;
1074 ptcwakeup(tp, FREAD);
1075 }
1076 return (0);
1077 }
1078 }
1079 /*
1080 * If external processing and packet mode send ioctl packet.
1081 */
1082 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1083 switch(cmd) {
1084 case TIOCSETA:
1085 case TIOCSETAW:
1086 case TIOCSETAF:
1087 #ifdef COMPAT_OLDTTY
1088 case TIOCSETP:
1089 case TIOCSETN:
1090 case TIOCSETC:
1091 case TIOCSLTC:
1092 case TIOCLBIS:
1093 case TIOCLBIC:
1094 case TIOCLSET:
1095 #endif
1096 pti->pt_send |= TIOCPKT_IOCTL;
1097 ptcwakeup(tp, FREAD);
1098 default:
1099 break;
1100 }
1101 }
1102 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1103 && CCEQ(cc[VSTART], CTRL('q'));
1104 if (pti->pt_flags & PF_NOSTOP) {
1105 if (stop) {
1106 pti->pt_send &= ~TIOCPKT_NOSTOP;
1107 pti->pt_send |= TIOCPKT_DOSTOP;
1108 pti->pt_flags &= ~PF_NOSTOP;
1109 ptcwakeup(tp, FREAD);
1110 }
1111 } else {
1112 if (!stop) {
1113 pti->pt_send &= ~TIOCPKT_DOSTOP;
1114 pti->pt_send |= TIOCPKT_NOSTOP;
1115 pti->pt_flags |= PF_NOSTOP;
1116 ptcwakeup(tp, FREAD);
1117 }
1118 }
1119 return (error);
1120 }
1121