pty.c revision 53483
11541Srgrimes/* 21541Srgrimes * Copyright (c) 1982, 1986, 1989, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 3314511Shsu * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 3450477Speter * $FreeBSD: head/sys/kern/tty_pty.c 53483 1999-11-21 02:54:54Z jkh $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Pseudo-teletype Driver 391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw') 401541Srgrimes */ 411541Srgrimes#include "pty.h" /* XXX */ 4231778Seivind#include "opt_compat.h" 431541Srgrimes#include <sys/param.h> 441541Srgrimes#include <sys/systm.h> 4524207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 4624207Sbde#include <sys/ioctl_compat.h> 4724207Sbde#endif 481541Srgrimes#include <sys/proc.h> 491541Srgrimes#include <sys/tty.h> 501541Srgrimes#include <sys/conf.h> 5124131Sbde#include <sys/fcntl.h> 5229354Speter#include <sys/poll.h> 531541Srgrimes#include <sys/kernel.h> 541541Srgrimes#include <sys/vnode.h> 553308Sphk#include <sys/signalvar.h> 5649536Sphk#include <sys/malloc.h> 571541Srgrimes 5849536SphkMALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 5912517Sjulian 6012819Sphkstatic void ptsstart __P((struct tty *tp)); 6151654Sphkstatic void ptsstop __P((struct tty *tp, int rw)); 6212819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag)); 6349536Sphkstatic void ptyinit __P((int n)); 6411789Sbde 6512675Sjulianstatic d_open_t ptsopen; 6612675Sjulianstatic d_close_t ptsclose; 6712675Sjulianstatic d_read_t ptsread; 6812675Sjulianstatic d_write_t ptswrite; 6912675Sjulianstatic d_ioctl_t ptyioctl; 7012675Sjulianstatic d_open_t ptcopen; 7112675Sjulianstatic d_close_t ptcclose; 7212675Sjulianstatic d_read_t ptcread; 7312675Sjulianstatic d_write_t ptcwrite; 7429354Speterstatic d_poll_t ptcpoll; 7512675Sjulian 7638485Sbde#define CDEV_MAJOR_S 5 7747625Sphkstatic struct cdevsw pts_cdevsw = { 7847625Sphk /* open */ ptsopen, 7947625Sphk /* close */ ptsclose, 8047625Sphk /* read */ ptsread, 8147625Sphk /* write */ ptswrite, 8247625Sphk /* ioctl */ ptyioctl, 8351654Sphk /* poll */ ttypoll, 8447625Sphk /* mmap */ nommap, 8547625Sphk /* strategy */ nostrategy, 8647625Sphk /* name */ "pts", 8747625Sphk /* maj */ CDEV_MAJOR_S, 8847625Sphk /* dump */ nodump, 8947625Sphk /* psize */ nopsize, 9047625Sphk /* flags */ D_TTY, 9147625Sphk /* bmaj */ -1 9238485Sbde}; 9312675Sjulian 9438485Sbde#define CDEV_MAJOR_C 6 9547625Sphkstatic struct cdevsw ptc_cdevsw = { 9647625Sphk /* open */ ptcopen, 9747625Sphk /* close */ ptcclose, 9847625Sphk /* read */ ptcread, 9947625Sphk /* write */ ptcwrite, 10047625Sphk /* ioctl */ ptyioctl, 10147625Sphk /* poll */ ptcpoll, 10247625Sphk /* mmap */ nommap, 10347625Sphk /* strategy */ nostrategy, 10447625Sphk /* name */ "ptc", 10547625Sphk /* maj */ CDEV_MAJOR_C, 10647625Sphk /* dump */ nodump, 10747625Sphk /* psize */ nopsize, 10847625Sphk /* flags */ D_TTY, 10947625Sphk /* bmaj */ -1 11038485Sbde}; 11112675Sjulian 1121541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1131541Srgrimes 11449536Sphkstruct pt_ioctl { 1151541Srgrimes int pt_flags; 1161541Srgrimes struct selinfo pt_selr, pt_selw; 1171541Srgrimes u_char pt_send; 1181541Srgrimes u_char pt_ucntl; 11949536Sphk struct tty pt_tty; 12050092Sjulian dev_t devs, devc; 12149536Sphk}; 1221541Srgrimes 1231541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1241541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1251541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1261541Srgrimes#define PF_NOSTOP 0x40 1271541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1281541Srgrimes 1291541Srgrimes/* 13049536Sphk * This function creates and initializes a pts/ptc pair 1311541Srgrimes * 13249536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13349536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13449536Sphk * 13549536Sphk * XXX: define and add mapping of upper minor bits to allow more 13649536Sphk * than 256 ptys. 1371541Srgrimes */ 13812819Sphkstatic void 13949536Sphkptyinit(n) 1401541Srgrimes int n; 1411541Srgrimes{ 14249536Sphk dev_t devs, devc; 14349536Sphk char *names = "pqrsPQRS"; 14449536Sphk struct pt_ioctl *pt; 1451541Srgrimes 14649536Sphk /* For now we only map the lower 8 bits of the minor */ 14749536Sphk if (n & ~0xff) 14849536Sphk return; 14949536Sphk 15050092Sjulian pt = malloc(sizeof(*pt), M_PTY, M_WAITOK); 15150092Sjulian bzero(pt, sizeof(*pt)); 15250092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 15349536Sphk 0, 0, 0666, "tty%c%r", names[n / 32], n % 32); 15450092Sjulian pt->devc = devc = make_dev(&ptc_cdevsw, n, 15549536Sphk 0, 0, 0666, "pty%c%r", names[n / 32], n % 32); 15649536Sphk 15749536Sphk devs->si_drv1 = devc->si_drv1 = pt; 15850652Sphk devs->si_tty = devc->si_tty = &pt->pt_tty; 15949540Sphk ttyregister(&pt->pt_tty); 16016322Sgpalmer} 1611541Srgrimes 1621541Srgrimes/*ARGSUSED*/ 16312675Sjulianstatic int 1641541Srgrimesptsopen(dev, flag, devtype, p) 1651541Srgrimes dev_t dev; 1661541Srgrimes int flag, devtype; 1671541Srgrimes struct proc *p; 1681541Srgrimes{ 1691541Srgrimes register struct tty *tp; 1701541Srgrimes int error; 17149992Sjulian int minr; 17249992Sjulian dev_t nextdev; 1731541Srgrimes 17449992Sjulian /* 17550254Sphk * XXX: Gross hack for DEVFS: 17649992Sjulian * If we openned this device, ensure we have the 17750254Sphk * next one too, so people can open it. 17849992Sjulian */ 17949992Sjulian minr = lminor(dev); 18049992Sjulian if (minr < 255) { 18149992Sjulian nextdev = makedev(major(dev), minr + 1); 18249992Sjulian if (!nextdev->si_drv1) { 18349992Sjulian ptyinit(minr + 1); 18449992Sjulian } 18549992Sjulian } 18649536Sphk if (!dev->si_drv1) 18749536Sphk ptyinit(minor(dev)); 18849536Sphk if (!dev->si_drv1) 18949536Sphk return(ENXIO); 19050652Sphk tp = dev->si_tty; 1911541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1921541Srgrimes ttychars(tp); /* Set up default chars */ 1931541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1941541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1951541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1961541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1971541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 19846112Sphk } else if (tp->t_state & TS_XCLUDE && suser(p)) 1991541Srgrimes return (EBUSY); 2001541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 2019824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 2021541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 2031541Srgrimes if (flag&FNONBLOCK) 2041541Srgrimes break; 2059639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 2069624Sbde "ptsopn", 0); 2073308Sphk if (error) 2081541Srgrimes return (error); 2091541Srgrimes } 2101541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 2117724Sache if (error == 0) 2127724Sache ptcwakeup(tp, FREAD|FWRITE); 2131541Srgrimes return (error); 2141541Srgrimes} 2151541Srgrimes 21612675Sjulianstatic int 2171541Srgrimesptsclose(dev, flag, mode, p) 2181541Srgrimes dev_t dev; 2191541Srgrimes int flag, mode; 2201541Srgrimes struct proc *p; 2211541Srgrimes{ 2221541Srgrimes register struct tty *tp; 2231541Srgrimes int err; 2241541Srgrimes 22550652Sphk tp = dev->si_tty; 2261541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2277730Sache ptsstop(tp, FREAD|FWRITE); 2287724Sache (void) ttyclose(tp); 2291541Srgrimes return (err); 2301541Srgrimes} 2311541Srgrimes 23212675Sjulianstatic int 2331541Srgrimesptsread(dev, uio, flag) 2341541Srgrimes dev_t dev; 2351541Srgrimes struct uio *uio; 2361541Srgrimes int flag; 2371541Srgrimes{ 2381541Srgrimes struct proc *p = curproc; 23950652Sphk register struct tty *tp = dev->si_tty; 24049536Sphk register struct pt_ioctl *pti = dev->si_drv1; 2411541Srgrimes int error = 0; 2421541Srgrimes 2431541Srgrimesagain: 2441541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2451541Srgrimes while (isbackground(p, tp)) { 24651791Smarcel if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 24751791Smarcel SIGISMEMBER(p->p_sigmask, SIGTTIN) || 24851791Smarcel p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) 2491541Srgrimes return (EIO); 2501541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 2519624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2529624Sbde 0); 2533308Sphk if (error) 2541541Srgrimes return (error); 2551541Srgrimes } 2561541Srgrimes if (tp->t_canq.c_cc == 0) { 2571541Srgrimes if (flag & IO_NDELAY) 2581541Srgrimes return (EWOULDBLOCK); 2599639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2609624Sbde "ptsin", 0); 2613308Sphk if (error) 2621541Srgrimes return (error); 2631541Srgrimes goto again; 2641541Srgrimes } 2651541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2661541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2671541Srgrimes error = EFAULT; 2681541Srgrimes break; 2691541Srgrimes } 2701541Srgrimes if (tp->t_canq.c_cc == 1) 2711541Srgrimes (void) getc(&tp->t_canq); 2721541Srgrimes if (tp->t_canq.c_cc) 2731541Srgrimes return (error); 2741541Srgrimes } else 2751541Srgrimes if (tp->t_oproc) 2761541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2771541Srgrimes ptcwakeup(tp, FWRITE); 2781541Srgrimes return (error); 2791541Srgrimes} 2801541Srgrimes 2811541Srgrimes/* 2821541Srgrimes * Write to pseudo-tty. 2831541Srgrimes * Wakeups of controlling tty will happen 2841541Srgrimes * indirectly, when tty driver calls ptsstart. 2851541Srgrimes */ 28612675Sjulianstatic int 2871541Srgrimesptswrite(dev, uio, flag) 2881541Srgrimes dev_t dev; 2891541Srgrimes struct uio *uio; 2901541Srgrimes int flag; 2911541Srgrimes{ 2921541Srgrimes register struct tty *tp; 2931541Srgrimes 29450652Sphk tp = dev->si_tty; 2951541Srgrimes if (tp->t_oproc == 0) 2961541Srgrimes return (EIO); 2971541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2981541Srgrimes} 2991541Srgrimes 3001541Srgrimes/* 3011541Srgrimes * Start output on pseudo-tty. 3021541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3031541Srgrimes */ 30412819Sphkstatic void 3051541Srgrimesptsstart(tp) 3061541Srgrimes struct tty *tp; 3071541Srgrimes{ 30849536Sphk register struct pt_ioctl *pti = tp->t_dev->si_drv1; 3091541Srgrimes 3101541Srgrimes if (tp->t_state & TS_TTSTOP) 3111541Srgrimes return; 3121541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3131541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3141541Srgrimes pti->pt_send = TIOCPKT_START; 3151541Srgrimes } 3161541Srgrimes ptcwakeup(tp, FREAD); 3171541Srgrimes} 3181541Srgrimes 31912819Sphkstatic void 3201541Srgrimesptcwakeup(tp, flag) 3211541Srgrimes struct tty *tp; 3221541Srgrimes int flag; 3231541Srgrimes{ 32449536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3251541Srgrimes 3261541Srgrimes if (flag & FREAD) { 3271541Srgrimes selwakeup(&pti->pt_selr); 3289639Sbde wakeup(TSA_PTC_READ(tp)); 3291541Srgrimes } 3301541Srgrimes if (flag & FWRITE) { 3311541Srgrimes selwakeup(&pti->pt_selw); 3329639Sbde wakeup(TSA_PTC_WRITE(tp)); 3331541Srgrimes } 3341541Srgrimes} 3351541Srgrimes 33612675Sjulianstatic int 3371541Srgrimesptcopen(dev, flag, devtype, p) 3381541Srgrimes dev_t dev; 3391541Srgrimes int flag, devtype; 3401541Srgrimes struct proc *p; 3411541Srgrimes{ 3421541Srgrimes register struct tty *tp; 3431541Srgrimes struct pt_ioctl *pti; 3441541Srgrimes 34549536Sphk if (!dev->si_drv1) 34649536Sphk ptyinit(minor(dev)); 34749536Sphk if (!dev->si_drv1) 34849536Sphk return(ENXIO); 34950652Sphk tp = dev->si_tty; 3501541Srgrimes if (tp->t_oproc) 3511541Srgrimes return (EIO); 3521541Srgrimes tp->t_oproc = ptsstart; 35351654Sphk tp->t_stop = ptsstop; 3541541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3551541Srgrimes tp->t_lflag &= ~EXTPROC; 35649536Sphk pti = dev->si_drv1; 3571541Srgrimes pti->pt_flags = 0; 3581541Srgrimes pti->pt_send = 0; 3591541Srgrimes pti->pt_ucntl = 0; 3601541Srgrimes return (0); 3611541Srgrimes} 3621541Srgrimes 36312675Sjulianstatic int 36410624Sbdeptcclose(dev, flags, fmt, p) 3651541Srgrimes dev_t dev; 36610624Sbde int flags; 36710624Sbde int fmt; 36810624Sbde struct proc *p; 3691541Srgrimes{ 3701541Srgrimes register struct tty *tp; 3711541Srgrimes 37250652Sphk tp = dev->si_tty; 3731541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3749824Sbde 3759824Sbde /* 3769824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3779824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3789824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3799824Sbde * may be in use because other parts of the line discipline make 3809824Sbde * sense for ptys. Recover by doing everything that a normal 3819824Sbde * ttymodem() would have done except for sending a SIGHUP. 3829824Sbde */ 3839850Sbde if (tp->t_state & TS_ISOPEN) { 3849850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3859850Sbde tp->t_state |= TS_ZOMBIE; 3869850Sbde ttyflush(tp, FREAD | FWRITE); 3879850Sbde } 3889824Sbde 3891541Srgrimes tp->t_oproc = 0; /* mark closed */ 3901541Srgrimes return (0); 3911541Srgrimes} 3921541Srgrimes 39312675Sjulianstatic int 3941541Srgrimesptcread(dev, uio, flag) 3951541Srgrimes dev_t dev; 3961541Srgrimes struct uio *uio; 3971541Srgrimes int flag; 3981541Srgrimes{ 39950652Sphk register struct tty *tp = dev->si_tty; 40049536Sphk struct pt_ioctl *pti = dev->si_drv1; 4011541Srgrimes char buf[BUFSIZ]; 4021541Srgrimes int error = 0, cc; 4031541Srgrimes 4041541Srgrimes /* 4051541Srgrimes * We want to block until the slave 4061541Srgrimes * is open, and there's something to read; 4071541Srgrimes * but if we lost the slave or we're NBIO, 4081541Srgrimes * then return the appropriate error instead. 4091541Srgrimes */ 4101541Srgrimes for (;;) { 4111541Srgrimes if (tp->t_state&TS_ISOPEN) { 4121541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4131541Srgrimes error = ureadc((int)pti->pt_send, uio); 4141541Srgrimes if (error) 4151541Srgrimes return (error); 4161541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4171541Srgrimes cc = min(uio->uio_resid, 4181541Srgrimes sizeof(tp->t_termios)); 4192807Sbde uiomove((caddr_t)&tp->t_termios, cc, 4202807Sbde uio); 4211541Srgrimes } 4221541Srgrimes pti->pt_send = 0; 4231541Srgrimes return (0); 4241541Srgrimes } 4251541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4261541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4271541Srgrimes if (error) 4281541Srgrimes return (error); 4291541Srgrimes pti->pt_ucntl = 0; 4301541Srgrimes return (0); 4311541Srgrimes } 4321541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4331541Srgrimes break; 4341541Srgrimes } 4359824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4361541Srgrimes return (0); /* EOF */ 4371541Srgrimes if (flag & IO_NDELAY) 4381541Srgrimes return (EWOULDBLOCK); 4399639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4403308Sphk if (error) 4411541Srgrimes return (error); 4421541Srgrimes } 4431541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4441541Srgrimes error = ureadc(0, uio); 4451541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4461541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4471541Srgrimes if (cc <= 0) 4481541Srgrimes break; 4491541Srgrimes error = uiomove(buf, cc, uio); 4501541Srgrimes } 4519626Sbde ttwwakeup(tp); 4521541Srgrimes return (error); 4531541Srgrimes} 4541541Srgrimes 45512675Sjulianstatic void 4561541Srgrimesptsstop(tp, flush) 4571541Srgrimes register struct tty *tp; 4581541Srgrimes int flush; 4591541Srgrimes{ 46049536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 4611541Srgrimes int flag; 4621541Srgrimes 4631541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4641541Srgrimes if (flush == 0) { 4651541Srgrimes flush = TIOCPKT_STOP; 4661541Srgrimes pti->pt_flags |= PF_STOPPED; 4671541Srgrimes } else 4681541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4691541Srgrimes pti->pt_send |= flush; 4701541Srgrimes /* change of perspective */ 4711541Srgrimes flag = 0; 4721541Srgrimes if (flush & FREAD) 4731541Srgrimes flag |= FWRITE; 4741541Srgrimes if (flush & FWRITE) 4751541Srgrimes flag |= FREAD; 4761541Srgrimes ptcwakeup(tp, flag); 4771541Srgrimes} 4781541Srgrimes 47912675Sjulianstatic int 48029354Speterptcpoll(dev, events, p) 4811541Srgrimes dev_t dev; 48229354Speter int events; 4831541Srgrimes struct proc *p; 4841541Srgrimes{ 48550652Sphk register struct tty *tp = dev->si_tty; 48649536Sphk struct pt_ioctl *pti = dev->si_drv1; 48729354Speter int revents = 0; 4881541Srgrimes int s; 4891541Srgrimes 4909824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 49129354Speter return (seltrue(dev, events, p) | POLLHUP); 4921541Srgrimes 49329354Speter /* 49429354Speter * Need to block timeouts (ttrstart). 49529354Speter */ 49629354Speter s = spltty(); 4971541Srgrimes 49829354Speter if (events & (POLLIN | POLLRDNORM)) 49929354Speter if ((tp->t_state & TS_ISOPEN) && 50029354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 50129354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 50229354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 50329354Speter revents |= events & (POLLIN | POLLRDNORM); 5041541Srgrimes 50529354Speter if (events & (POLLOUT | POLLWRNORM)) 50629354Speter if (tp->t_state & TS_ISOPEN && 50729354Speter ((pti->pt_flags & PF_REMOTE) ? 50829354Speter (tp->t_canq.c_cc == 0) : 50929354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 51029354Speter (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON))))) 51129354Speter revents |= events & (POLLOUT | POLLWRNORM); 5121541Srgrimes 51329354Speter if (events & POLLHUP) 51429354Speter if ((tp->t_state & TS_CARR_ON) == 0) 51529354Speter revents |= POLLHUP; 5161541Srgrimes 51729354Speter if (revents == 0) { 51829354Speter if (events & (POLLIN | POLLRDNORM)) 51929354Speter selrecord(p, &pti->pt_selr); 52029354Speter 52129354Speter if (events & (POLLOUT | POLLWRNORM)) 52229354Speter selrecord(p, &pti->pt_selw); 5231541Srgrimes } 52429354Speter splx(s); 52529354Speter 52629354Speter return (revents); 5271541Srgrimes} 5281541Srgrimes 52912675Sjulianstatic int 5301541Srgrimesptcwrite(dev, uio, flag) 5311541Srgrimes dev_t dev; 5321541Srgrimes register struct uio *uio; 5331541Srgrimes int flag; 5341541Srgrimes{ 53550652Sphk register struct tty *tp = dev->si_tty; 5361549Srgrimes register u_char *cp = 0; 5371541Srgrimes register int cc = 0; 5381541Srgrimes u_char locbuf[BUFSIZ]; 5391541Srgrimes int cnt = 0; 54049536Sphk struct pt_ioctl *pti = dev->si_drv1; 5411541Srgrimes int error = 0; 5421541Srgrimes 5431541Srgrimesagain: 5441541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5451541Srgrimes goto block; 5461541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5471541Srgrimes if (tp->t_canq.c_cc) 5481541Srgrimes goto block; 54911789Sbde while ((uio->uio_resid > 0 || cc > 0) && 55011789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5511541Srgrimes if (cc == 0) { 5521541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5531541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5541541Srgrimes cp = locbuf; 5551541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5561541Srgrimes if (error) 5571541Srgrimes return (error); 5581541Srgrimes /* check again for safety */ 55911789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 56011789Sbde /* adjust as usual */ 56111789Sbde uio->uio_resid += cc; 5621541Srgrimes return (EIO); 56311789Sbde } 5641541Srgrimes } 56515199Sbde if (cc > 0) { 56615199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 56715199Sbde /* 56815199Sbde * XXX we don't guarantee that the canq size 56915199Sbde * is >= TTYHOG, so the above b_to_q() may 57015199Sbde * leave some bytes uncopied. However, space 57115199Sbde * is guaranteed for the null terminator if 57215199Sbde * we don't fail here since (TTYHOG - 1) is 57315199Sbde * not a multiple of CBSIZE. 57415199Sbde */ 57515199Sbde if (cc > 0) 57615199Sbde break; 57715199Sbde } 5781541Srgrimes } 57911789Sbde /* adjust for data copied in but not written */ 58011789Sbde uio->uio_resid += cc; 5811541Srgrimes (void) putc(0, &tp->t_canq); 5821541Srgrimes ttwakeup(tp); 5839639Sbde wakeup(TSA_PTS_READ(tp)); 5841541Srgrimes return (0); 5851541Srgrimes } 58611789Sbde while (uio->uio_resid > 0 || cc > 0) { 5871541Srgrimes if (cc == 0) { 5881541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5891541Srgrimes cp = locbuf; 5901541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5911541Srgrimes if (error) 5921541Srgrimes return (error); 5931541Srgrimes /* check again for safety */ 59411789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 59511789Sbde /* adjust for data copied in but not written */ 59611789Sbde uio->uio_resid += cc; 5971541Srgrimes return (EIO); 59811789Sbde } 5991541Srgrimes } 6001541Srgrimes while (cc > 0) { 6011541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 6021541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 6039824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 6041541Srgrimes goto block; 6051541Srgrimes } 6061541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 6071541Srgrimes cnt++; 6081541Srgrimes cc--; 6091541Srgrimes } 6101541Srgrimes cc = 0; 6111541Srgrimes } 6121541Srgrimes return (0); 6131541Srgrimesblock: 6141541Srgrimes /* 6151541Srgrimes * Come here to wait for slave to open, for space 61615199Sbde * in outq, or space in rawq, or an empty canq. 6171541Srgrimes */ 61811789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 61911789Sbde /* adjust for data copied in but not written */ 62011789Sbde uio->uio_resid += cc; 6211541Srgrimes return (EIO); 62211789Sbde } 6231541Srgrimes if (flag & IO_NDELAY) { 6241541Srgrimes /* adjust for data copied in but not written */ 6251541Srgrimes uio->uio_resid += cc; 6261541Srgrimes if (cnt == 0) 6271541Srgrimes return (EWOULDBLOCK); 6281541Srgrimes return (0); 6291541Srgrimes } 6309639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6313308Sphk if (error) { 6321541Srgrimes /* adjust for data copied in but not written */ 6331541Srgrimes uio->uio_resid += cc; 6341541Srgrimes return (error); 6351541Srgrimes } 6361541Srgrimes goto again; 6371541Srgrimes} 6381541Srgrimes 6391541Srgrimes/*ARGSUSED*/ 64012675Sjulianstatic int 6411541Srgrimesptyioctl(dev, cmd, data, flag, p) 6421541Srgrimes dev_t dev; 64336735Sdfr u_long cmd; 6441541Srgrimes caddr_t data; 6451541Srgrimes int flag; 6461541Srgrimes struct proc *p; 6471541Srgrimes{ 64850652Sphk register struct tty *tp = dev->si_tty; 64949536Sphk register struct pt_ioctl *pti = dev->si_drv1; 6501541Srgrimes register u_char *cc = tp->t_cc; 6511541Srgrimes int stop, error; 6521541Srgrimes 65347301Sluoqi if (devsw(dev)->d_open == ptcopen) { 6541541Srgrimes switch (cmd) { 6551541Srgrimes 6561541Srgrimes case TIOCGPGRP: 6571541Srgrimes /* 65833826Sbde * We avoid calling ttioctl on the controller since, 6591541Srgrimes * in that case, tp must be the controlling terminal. 6601541Srgrimes */ 6611541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6621541Srgrimes return (0); 6631541Srgrimes 6641541Srgrimes case TIOCPKT: 6651541Srgrimes if (*(int *)data) { 6661541Srgrimes if (pti->pt_flags & PF_UCNTL) 6671541Srgrimes return (EINVAL); 6681541Srgrimes pti->pt_flags |= PF_PKT; 6691541Srgrimes } else 6701541Srgrimes pti->pt_flags &= ~PF_PKT; 6711541Srgrimes return (0); 6721541Srgrimes 6731541Srgrimes case TIOCUCNTL: 6741541Srgrimes if (*(int *)data) { 6751541Srgrimes if (pti->pt_flags & PF_PKT) 6761541Srgrimes return (EINVAL); 6771541Srgrimes pti->pt_flags |= PF_UCNTL; 6781541Srgrimes } else 6791541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6801541Srgrimes return (0); 6811541Srgrimes 6821541Srgrimes case TIOCREMOTE: 6831541Srgrimes if (*(int *)data) 6841541Srgrimes pti->pt_flags |= PF_REMOTE; 6851541Srgrimes else 6861541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6871541Srgrimes ttyflush(tp, FREAD|FWRITE); 6881541Srgrimes return (0); 68947203Sluoqi } 6901541Srgrimes 69147203Sluoqi /* 69247203Sluoqi * The rest of the ioctls shouldn't be called until 69347301Sluoqi * the slave is open. 69447203Sluoqi */ 69547203Sluoqi if ((tp->t_state & TS_ISOPEN) == 0) 69647301Sluoqi return (EAGAIN); 69747203Sluoqi 69847203Sluoqi switch (cmd) { 6991541Srgrimes#ifdef COMPAT_43 7008876Srgrimes case TIOCSETP: 7011541Srgrimes case TIOCSETN: 7021541Srgrimes#endif 7031541Srgrimes case TIOCSETD: 7041541Srgrimes case TIOCSETA: 7051541Srgrimes case TIOCSETAW: 7069858Sache case TIOCSETAF: 70747301Sluoqi /* 70847301Sluoqi * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 70947301Sluoqi * ttywflush(tp) will hang if there are characters in 71047301Sluoqi * the outq. 71147301Sluoqi */ 7121541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7131541Srgrimes break; 7141541Srgrimes 7151541Srgrimes case TIOCSIG: 71627770Sjmg if (*(unsigned int *)data >= NSIG || 71727770Sjmg *(unsigned int *)data == 0) 7181541Srgrimes return(EINVAL); 7191541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7201541Srgrimes ttyflush(tp, FREAD|FWRITE); 7211541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 7221541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7231541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7241541Srgrimes ttyinfo(tp); 7251541Srgrimes return(0); 7261541Srgrimes } 72747203Sluoqi } 72847301Sluoqi if (cmd == TIOCEXT) { 72947301Sluoqi /* 73047301Sluoqi * When the EXTPROC bit is being toggled, we need 73147301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 73247301Sluoqi * is turned on. 73347301Sluoqi */ 73447301Sluoqi if (*(int *)data) { 73547301Sluoqi if (pti->pt_flags & PF_PKT) { 73647301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 73747301Sluoqi ptcwakeup(tp, FREAD); 73847301Sluoqi } 73947301Sluoqi tp->t_lflag |= EXTPROC; 74047301Sluoqi } else { 74147301Sluoqi if ((tp->t_lflag & EXTPROC) && 74247301Sluoqi (pti->pt_flags & PF_PKT)) { 74347301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74447301Sluoqi ptcwakeup(tp, FREAD); 74547301Sluoqi } 74647301Sluoqi tp->t_lflag &= ~EXTPROC; 74747301Sluoqi } 74847301Sluoqi return(0); 74947301Sluoqi } 7501541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 75131577Sbde if (error == ENOIOCTL) 7521541Srgrimes error = ttioctl(tp, cmd, data, flag); 75331577Sbde if (error == ENOIOCTL) { 7541541Srgrimes if (pti->pt_flags & PF_UCNTL && 7551541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7561541Srgrimes if (cmd & 0xff) { 7571541Srgrimes pti->pt_ucntl = (u_char)cmd; 7581541Srgrimes ptcwakeup(tp, FREAD); 7591541Srgrimes } 7601541Srgrimes return (0); 7611541Srgrimes } 7621541Srgrimes error = ENOTTY; 7631541Srgrimes } 7641541Srgrimes /* 7651541Srgrimes * If external processing and packet mode send ioctl packet. 7661541Srgrimes */ 7671541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7681541Srgrimes switch(cmd) { 7691541Srgrimes case TIOCSETA: 7701541Srgrimes case TIOCSETAW: 7711541Srgrimes case TIOCSETAF: 7721541Srgrimes#ifdef COMPAT_43 7731541Srgrimes case TIOCSETP: 7741541Srgrimes case TIOCSETN: 7751541Srgrimes#endif 7761541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7771541Srgrimes case TIOCSETC: 7781541Srgrimes case TIOCSLTC: 7791541Srgrimes case TIOCLBIS: 7801541Srgrimes case TIOCLBIC: 7811541Srgrimes case TIOCLSET: 7821541Srgrimes#endif 7831541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7841541Srgrimes ptcwakeup(tp, FREAD); 7851541Srgrimes default: 7861541Srgrimes break; 7871541Srgrimes } 7881541Srgrimes } 7898876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7901541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7911541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7921541Srgrimes if (stop) { 7931541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7941541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7951541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 7961541Srgrimes ptcwakeup(tp, FREAD); 7971541Srgrimes } 7981541Srgrimes } else { 7991541Srgrimes if (!stop) { 8001541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8011541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8021541Srgrimes pti->pt_flags |= PF_NOSTOP; 8031541Srgrimes ptcwakeup(tp, FREAD); 8041541Srgrimes } 8051541Srgrimes } 8061541Srgrimes return (error); 8071541Srgrimes} 80812517Sjulian 80912517Sjulian 81029506Sbdestatic void ptc_drvinit __P((void *unused)); 81149536Sphk 81212675Sjulianstatic void 81329506Sbdeptc_drvinit(unused) 81429506Sbde void *unused; 81512517Sjulian{ 81653483Sjkh cdevsw_add(&pts_cdevsw); 81753483Sjkh cdevsw_add(&ptc_cdevsw); 81850254Sphk /* XXX: Gross hack for DEVFS */ 81950254Sphk ptyinit(0); 82012517Sjulian} 82112517Sjulian 82212517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 823