--- Net2/kern/tty_pty.c 2018/04/24 18:03:57 1.1.1.1 +++ Net2/kern/tty_pty.c 2018/04/24 18:14:47 1.1.1.5 @@ -30,7 +30,8 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)tty_pty.c 7.21 (Berkeley) 5/30/91 + * from: @(#)tty_pty.c 7.21 (Berkeley) 5/30/91 + * tty_pty.c,v 1.11 1993/07/19 05:52:32 mycroft Exp */ /* @@ -43,6 +44,7 @@ #include "param.h" #include "systm.h" #include "ioctl.h" +#include "select.h" #include "tty.h" #include "conf.h" #include "file.h" @@ -62,26 +64,30 @@ * pts == /dev/tty[pqrs]? * ptc == /dev/pty[pqrs]? */ -struct tty pt_tty[NPTY]; +struct tty *pt_tty[NPTY]; struct pt_ioctl { int pt_flags; - struct proc *pt_selr, *pt_selw; + struct selinfo pt_selr, pt_selw; u_char pt_send; u_char pt_ucntl; } pt_ioctl[NPTY]; int npty = NPTY; /* for pstat -t */ -#define PF_RCOLL 0x01 -#define PF_WCOLL 0x02 +#define PF_COPEN 0x01 /* master open */ +#define PF_SOPEN 0x02 /* slave open */ #define PF_PKT 0x08 /* packet mode */ #define PF_STOPPED 0x10 /* user told stopped */ #define PF_REMOTE 0x20 /* remote and flow controlled input */ #define PF_NOSTOP 0x40 #define PF_UCNTL 0x80 /* user control mode */ +void ptcwakeup __P((struct tty *tp, int flag)); + /*ARGSUSED*/ +int ptsopen(dev, flag, devtype, p) dev_t dev; + int flag, devtype; struct proc *p; { register struct tty *tp; @@ -92,7 +98,10 @@ ptsopen(dev, flag, devtype, p) #endif if (minor(dev) >= NPTY) return (ENXIO); - tp = &pt_tty[minor(dev)]; + if(!pt_tty[minor(dev)]) { + tp = pt_tty[minor(dev)] = ttymalloc(); + } else + tp = pt_tty[minor(dev)]; if ((tp->t_state & TS_ISOPEN) == 0) { tp->t_state |= TS_WOPEN; ttychars(tp); /* Set up default chars */ @@ -114,11 +123,14 @@ ptsopen(dev, flag, devtype, p) ttopen, 0)) return (error); } - error = (*linesw[tp->t_line].l_open)(dev, tp, flag); + if (error = (*linesw[tp->t_line].l_open)(dev, tp)) + return (error); + pt_ioctl[minor(dev)].pt_flags |= PF_SOPEN; ptcwakeup(tp, FREAD|FWRITE); - return (error); + return (0); } +int ptsclose(dev, flag, mode, p) dev_t dev; int flag, mode; @@ -126,18 +138,26 @@ ptsclose(dev, flag, mode, p) { register struct tty *tp; - tp = &pt_tty[minor(dev)]; + tp = pt_tty[minor(dev)]; (*linesw[tp->t_line].l_close)(tp, flag); ttyclose(tp); ptcwakeup(tp, FREAD|FWRITE); + pt_ioctl[minor(dev)].pt_flags &= ~PF_SOPEN; + if ((pt_ioctl[minor(dev)].pt_flags & PF_COPEN) == 0) { + ttyfree(tp); + pt_tty[minor(dev)] = (struct tty *)NULL; + } + return(0); } +int ptsread(dev, uio, flag) dev_t dev; struct uio *uio; + int flag; { struct proc *p = curproc; - register struct tty *tp = &pt_tty[minor(dev)]; + register struct tty *tp = pt_tty[minor(dev)]; register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; int error = 0; @@ -183,13 +203,15 @@ again: * Wakeups of controlling tty will happen * indirectly, when tty driver calls ptsstart. */ +int ptswrite(dev, uio, flag) dev_t dev; struct uio *uio; + int flag; { register struct tty *tp; - tp = &pt_tty[minor(dev)]; + tp = pt_tty[minor(dev)]; if (tp->t_oproc == 0) return (EIO); return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); @@ -199,47 +221,45 @@ ptswrite(dev, uio, flag) * Start output on pseudo-tty. * Wake up process selecting or sleeping for input from controlling tty. */ +int ptsstart(tp) struct tty *tp; { register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; if (tp->t_state & TS_TTSTOP) - return; + return 0; /* XXX should we return 1? */ if (pti->pt_flags & PF_STOPPED) { pti->pt_flags &= ~PF_STOPPED; pti->pt_send = TIOCPKT_START; } ptcwakeup(tp, FREAD); + return 0; } +void ptcwakeup(tp, flag) struct tty *tp; + int flag; { struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; if (flag & FREAD) { - if (pti->pt_selr) { - selwakeup(pti->pt_selr, pti->pt_flags & PF_RCOLL); - pti->pt_selr = 0; - pti->pt_flags &= ~PF_RCOLL; - } - wakeup((caddr_t)&tp->t_outq.c_cf); + selwakeup(&pti->pt_selr); + wakeup((caddr_t)&tp->t_outq.c_cl); } if (flag & FWRITE) { - if (pti->pt_selw) { - selwakeup(pti->pt_selw, pti->pt_flags & PF_WCOLL); - pti->pt_selw = 0; - pti->pt_flags &= ~PF_WCOLL; - } + selwakeup(&pti->pt_selw); wakeup((caddr_t)&tp->t_rawq.c_cf); } } /*ARGSUSED*/ #ifdef __STDC__ +int ptcopen(dev_t dev, int flag, int devtype, struct proc *p) #else +int ptcopen(dev, flag, devtype, p) dev_t dev; int flag, devtype; @@ -251,38 +271,58 @@ ptcopen(dev, flag, devtype, p) if (minor(dev) >= NPTY) return (ENXIO); - tp = &pt_tty[minor(dev)]; + if(!pt_tty[minor(dev)]) { + tp = pt_tty[minor(dev)] = ttymalloc(); + } else + tp = pt_tty[minor(dev)]; if (tp->t_oproc) return (EIO); tp->t_oproc = ptsstart; (void)(*linesw[tp->t_line].l_modem)(tp, 1); tp->t_lflag &= ~EXTPROC; pti = &pt_ioctl[minor(dev)]; - pti->pt_flags = 0; + pti->pt_flags &= PF_SOPEN; + pti->pt_flags |= PF_COPEN; pti->pt_send = 0; pti->pt_ucntl = 0; return (0); } +extern struct tty *constty; /* -hv- 06.Oct.92*/ + +int ptcclose(dev) dev_t dev; { register struct tty *tp; - tp = &pt_tty[minor(dev)]; + tp = pt_tty[minor(dev)]; (void)(*linesw[tp->t_line].l_modem)(tp, 0); tp->t_state &= ~TS_CARR_ON; tp->t_oproc = 0; /* mark closed */ tp->t_session = 0; + +/* XXX -hv- 6.Oct.92 this prevents the "hanging console bug" with X11 */ + if (constty==tp) + constty = 0; + + pt_ioctl[minor(dev)].pt_flags &= ~PF_COPEN; + if ((pt_ioctl[minor(dev)].pt_flags & PF_SOPEN) == 0) { + ttyfree(tp); + pt_tty[minor(dev)] = (struct tty *)NULL; + } + return (0); } +int ptcread(dev, uio, flag) dev_t dev; struct uio *uio; + int flag; { - register struct tty *tp = &pt_tty[minor(dev)]; + register struct tty *tp = pt_tty[minor(dev)]; struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; - char buf[BUFSIZ]; + u_char buf[BUFSIZ]; int error = 0, cc; /* @@ -300,7 +340,8 @@ ptcread(dev, uio, flag) if (pti->pt_send & TIOCPKT_IOCTL) { cc = MIN(uio->uio_resid, sizeof(tp->t_termios)); - uiomove(&tp->t_termios, cc, uio); + uiomove((caddr_t)&tp->t_termios, cc, + uio); } pti->pt_send = 0; return (0); @@ -319,7 +360,7 @@ ptcread(dev, uio, flag) return (0); /* EOF */ if (flag & IO_NDELAY) return (EWOULDBLOCK); - if (error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, + if (error = tsleep((caddr_t)&tp->t_outq.c_cl, TTIPRI | PCATCH, ttyin, 0)) return (error); } @@ -336,15 +377,12 @@ ptcread(dev, uio, flag) tp->t_state &= ~TS_ASLEEP; wakeup((caddr_t)&tp->t_outq); } - if (tp->t_wsel) { - selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); - tp->t_wsel = 0; - tp->t_state &= ~TS_WCOLL; - } + selwakeup(&tp->t_wsel); } return (error); } +void ptsstop(tp, flush) register struct tty *tp; int flush; @@ -368,14 +406,14 @@ ptsstop(tp, flush) ptcwakeup(tp, flag); } +int ptcselect(dev, rw, p) dev_t dev; int rw; struct proc *p; { - register struct tty *tp = &pt_tty[minor(dev)]; + register struct tty *tp = pt_tty[minor(dev)]; struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; - struct proc *prev; int s; if ((tp->t_state&TS_CARR_ON) == 0) @@ -400,10 +438,7 @@ ptcselect(dev, rw, p) (pti->pt_flags&PF_PKT && pti->pt_send || pti->pt_flags&PF_UCNTL && pti->pt_ucntl)) return (1); - if ((prev = pti->pt_selr) && prev->p_wchan == (caddr_t)&selwait) - pti->pt_flags |= PF_RCOLL; - else - pti->pt_selr = p; + selrecord(p, &pti->pt_selr); break; @@ -419,21 +454,20 @@ ptcselect(dev, rw, p) return (1); } } - if ((prev = pti->pt_selw) && prev->p_wchan == (caddr_t)&selwait) - pti->pt_flags |= PF_WCOLL; - else - pti->pt_selw = p; + selrecord(p, &pti->pt_selw); break; } return (0); } +int ptcwrite(dev, uio, flag) dev_t dev; register struct uio *uio; + int flag; { - register struct tty *tp = &pt_tty[minor(dev)]; + register struct tty *tp = pt_tty[minor(dev)]; register u_char *cp; register int cc = 0; u_char locbuf[BUFSIZ]; @@ -460,7 +494,7 @@ again: return (EIO); } if (cc) - (void) b_to_q((char *)cp, cc, &tp->t_canq); + (void) b_to_q(cp, cc, &tp->t_canq); cc = 0; } (void) putc(0, &tp->t_canq); @@ -516,15 +550,16 @@ block: } /*ARGSUSED*/ +int ptyioctl(dev, cmd, data, flag) caddr_t data; + int cmd, flag; dev_t dev; { - register struct tty *tp = &pt_tty[minor(dev)]; + register struct tty *tp = pt_tty[minor(dev)]; register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; register u_char *cc = tp->t_cc; int stop, error; - extern ttyinput(); /* * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. @@ -539,14 +574,14 @@ ptyioctl(dev, cmd, data, flag) if (*(int *)data) { if (pti->pt_flags & PF_PKT) { pti->pt_send |= TIOCPKT_IOCTL; - ptcwakeup(tp); + ptcwakeup(tp, FREAD); } tp->t_lflag |= EXTPROC; } else { if ((tp->t_state & EXTPROC) && (pti->pt_flags & PF_PKT)) { pti->pt_send |= TIOCPKT_IOCTL; - ptcwakeup(tp); + ptcwakeup(tp, FREAD); } tp->t_lflag &= ~EXTPROC; } @@ -589,14 +624,16 @@ ptyioctl(dev, cmd, data, flag) ttyflush(tp, FREAD|FWRITE); return (0); +#ifdef COMPAT_43 + /* wkt */ case TIOCSETP: case TIOCSETN: +#endif case TIOCSETD: case TIOCSETA: case TIOCSETAW: case TIOCSETAF: - while (getc(&tp->t_outq) >= 0) - ; + flushq(&tp->t_outq); break; case TIOCSIG: @@ -622,7 +659,7 @@ ptyioctl(dev, cmd, data, flag) if (linesw[tp->t_line].l_rint != ttyinput) { (*linesw[tp->t_line].l_close)(tp, flag); tp->t_line = TTYDISC; - (void)(*linesw[tp->t_line].l_open)(dev, tp, flag); + (void)(*linesw[tp->t_line].l_open)(dev, tp); error = ENOTTY; } if (error < 0) { @@ -644,9 +681,10 @@ ptyioctl(dev, cmd, data, flag) case TIOCSETA: case TIOCSETAW: case TIOCSETAF: +#ifdef COMPAT_43 + /* wkt */ case TIOCSETP: case TIOCSETN: -#ifdef COMPAT_43 case TIOCSETC: case TIOCSLTC: case TIOCLBIS: