pty.c revision 50477
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 50477 1999-08-28 01:08:13Z peter $ 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)); 6112819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag)); 6249536Sphkstatic void ptyinit __P((int n)); 6311789Sbde 6412675Sjulianstatic d_open_t ptsopen; 6512675Sjulianstatic d_close_t ptsclose; 6612675Sjulianstatic d_read_t ptsread; 6712675Sjulianstatic d_write_t ptswrite; 6812675Sjulianstatic d_ioctl_t ptyioctl; 6912675Sjulianstatic d_stop_t ptsstop; 7012731Sbdestatic d_devtotty_t ptydevtotty; 7112675Sjulianstatic d_open_t ptcopen; 7212675Sjulianstatic d_close_t ptcclose; 7312675Sjulianstatic d_read_t ptcread; 7412675Sjulianstatic d_write_t ptcwrite; 7529354Speterstatic d_poll_t ptcpoll; 7612675Sjulian 7738485Sbde#define CDEV_MAJOR_S 5 7847625Sphkstatic struct cdevsw pts_cdevsw = { 7947625Sphk /* open */ ptsopen, 8047625Sphk /* close */ ptsclose, 8147625Sphk /* read */ ptsread, 8247625Sphk /* write */ ptswrite, 8347625Sphk /* ioctl */ ptyioctl, 8447625Sphk /* stop */ ptsstop, 8547625Sphk /* reset */ noreset, 8647625Sphk /* devtotty */ ptydevtotty, 8747625Sphk /* poll */ ttpoll, 8847625Sphk /* mmap */ nommap, 8947625Sphk /* strategy */ nostrategy, 9047625Sphk /* name */ "pts", 9147625Sphk /* parms */ noparms, 9247625Sphk /* maj */ CDEV_MAJOR_S, 9347625Sphk /* dump */ nodump, 9447625Sphk /* psize */ nopsize, 9547625Sphk /* flags */ D_TTY, 9647625Sphk /* maxio */ 0, 9747625Sphk /* bmaj */ -1 9838485Sbde}; 9912675Sjulian 10038485Sbde#define CDEV_MAJOR_C 6 10147625Sphkstatic struct cdevsw ptc_cdevsw = { 10247625Sphk /* open */ ptcopen, 10347625Sphk /* close */ ptcclose, 10447625Sphk /* read */ ptcread, 10547625Sphk /* write */ ptcwrite, 10647625Sphk /* ioctl */ ptyioctl, 10747625Sphk /* stop */ nostop, 10847625Sphk /* reset */ noreset, 10947625Sphk /* devtotty */ ptydevtotty, 11047625Sphk /* poll */ ptcpoll, 11147625Sphk /* mmap */ nommap, 11247625Sphk /* strategy */ nostrategy, 11347625Sphk /* name */ "ptc", 11447625Sphk /* parms */ noparms, 11547625Sphk /* maj */ CDEV_MAJOR_C, 11647625Sphk /* dump */ nodump, 11747625Sphk /* psize */ nopsize, 11847625Sphk /* flags */ D_TTY, 11947625Sphk /* maxio */ 0, 12047625Sphk /* bmaj */ -1 12138485Sbde}; 12212675Sjulian 1231541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1241541Srgrimes 12549536Sphkstruct pt_ioctl { 1261541Srgrimes int pt_flags; 1271541Srgrimes struct selinfo pt_selr, pt_selw; 1281541Srgrimes u_char pt_send; 1291541Srgrimes u_char pt_ucntl; 13049536Sphk struct tty pt_tty; 13150092Sjulian dev_t devs, devc; 13249536Sphk}; 1331541Srgrimes 1341541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1351541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1361541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1371541Srgrimes#define PF_NOSTOP 0x40 1381541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1391541Srgrimes 1401541Srgrimes/* 14149536Sphk * This function creates and initializes a pts/ptc pair 1421541Srgrimes * 14349536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 14449536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 14549536Sphk * 14649536Sphk * XXX: define and add mapping of upper minor bits to allow more 14749536Sphk * than 256 ptys. 1481541Srgrimes */ 14912819Sphkstatic void 15049536Sphkptyinit(n) 1511541Srgrimes int n; 1521541Srgrimes{ 15349536Sphk dev_t devs, devc; 15449536Sphk char *names = "pqrsPQRS"; 15549536Sphk struct pt_ioctl *pt; 1561541Srgrimes 15749536Sphk /* For now we only map the lower 8 bits of the minor */ 15849536Sphk if (n & ~0xff) 15949536Sphk return; 16049536Sphk 16150092Sjulian pt = malloc(sizeof(*pt), M_PTY, M_WAITOK); 16250092Sjulian bzero(pt, sizeof(*pt)); 16350092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 16449536Sphk 0, 0, 0666, "tty%c%r", names[n / 32], n % 32); 16550092Sjulian pt->devc = devc = make_dev(&ptc_cdevsw, n, 16649536Sphk 0, 0, 0666, "pty%c%r", names[n / 32], n % 32); 16749536Sphk 16849536Sphk devs->si_drv1 = devc->si_drv1 = pt; 16949536Sphk devs->si_tty_tty = devc->si_tty_tty = &pt->pt_tty; 17049540Sphk ttyregister(&pt->pt_tty); 17116322Sgpalmer} 1721541Srgrimes 1731541Srgrimes/*ARGSUSED*/ 17412675Sjulianstatic int 1751541Srgrimesptsopen(dev, flag, devtype, p) 1761541Srgrimes dev_t dev; 1771541Srgrimes int flag, devtype; 1781541Srgrimes struct proc *p; 1791541Srgrimes{ 1801541Srgrimes register struct tty *tp; 1811541Srgrimes int error; 18249992Sjulian int minr; 18349992Sjulian dev_t nextdev; 1841541Srgrimes 18549992Sjulian /* 18650254Sphk * XXX: Gross hack for DEVFS: 18749992Sjulian * If we openned this device, ensure we have the 18850254Sphk * next one too, so people can open it. 18949992Sjulian */ 19049992Sjulian minr = lminor(dev); 19149992Sjulian if (minr < 255) { 19249992Sjulian nextdev = makedev(major(dev), minr + 1); 19349992Sjulian if (!nextdev->si_drv1) { 19449992Sjulian ptyinit(minr + 1); 19549992Sjulian } 19649992Sjulian } 19749536Sphk if (!dev->si_drv1) 19849536Sphk ptyinit(minor(dev)); 19949536Sphk if (!dev->si_drv1) 20049536Sphk return(ENXIO); 20149536Sphk tp = dev->si_tty_tty; 2021541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 2031541Srgrimes ttychars(tp); /* Set up default chars */ 2041541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 2051541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 2061541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 2071541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 2081541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 20946112Sphk } else if (tp->t_state & TS_XCLUDE && suser(p)) 2101541Srgrimes return (EBUSY); 2111541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 2129824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 2131541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 2141541Srgrimes if (flag&FNONBLOCK) 2151541Srgrimes break; 2169639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 2179624Sbde "ptsopn", 0); 2183308Sphk if (error) 2191541Srgrimes return (error); 2201541Srgrimes } 2211541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 2227724Sache if (error == 0) 2237724Sache ptcwakeup(tp, FREAD|FWRITE); 2241541Srgrimes return (error); 2251541Srgrimes} 2261541Srgrimes 22712675Sjulianstatic int 2281541Srgrimesptsclose(dev, flag, mode, p) 2291541Srgrimes dev_t dev; 2301541Srgrimes int flag, mode; 2311541Srgrimes struct proc *p; 2321541Srgrimes{ 2331541Srgrimes register struct tty *tp; 2341541Srgrimes int err; 2351541Srgrimes 23649536Sphk tp = dev->si_tty_tty; 2371541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2387730Sache ptsstop(tp, FREAD|FWRITE); 2397724Sache (void) ttyclose(tp); 2401541Srgrimes return (err); 2411541Srgrimes} 2421541Srgrimes 24312675Sjulianstatic int 2441541Srgrimesptsread(dev, uio, flag) 2451541Srgrimes dev_t dev; 2461541Srgrimes struct uio *uio; 2471541Srgrimes int flag; 2481541Srgrimes{ 2491541Srgrimes struct proc *p = curproc; 25049536Sphk register struct tty *tp = dev->si_tty_tty; 25149536Sphk register struct pt_ioctl *pti = dev->si_drv1; 2521541Srgrimes int error = 0; 2531541Srgrimes 2541541Srgrimesagain: 2551541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2561541Srgrimes while (isbackground(p, tp)) { 2571541Srgrimes if ((p->p_sigignore & sigmask(SIGTTIN)) || 2581541Srgrimes (p->p_sigmask & sigmask(SIGTTIN)) || 2591541Srgrimes p->p_pgrp->pg_jobc == 0 || 2601541Srgrimes p->p_flag & P_PPWAIT) 2611541Srgrimes return (EIO); 2621541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 2639624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2649624Sbde 0); 2653308Sphk if (error) 2661541Srgrimes return (error); 2671541Srgrimes } 2681541Srgrimes if (tp->t_canq.c_cc == 0) { 2691541Srgrimes if (flag & IO_NDELAY) 2701541Srgrimes return (EWOULDBLOCK); 2719639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2729624Sbde "ptsin", 0); 2733308Sphk if (error) 2741541Srgrimes return (error); 2751541Srgrimes goto again; 2761541Srgrimes } 2771541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2781541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2791541Srgrimes error = EFAULT; 2801541Srgrimes break; 2811541Srgrimes } 2821541Srgrimes if (tp->t_canq.c_cc == 1) 2831541Srgrimes (void) getc(&tp->t_canq); 2841541Srgrimes if (tp->t_canq.c_cc) 2851541Srgrimes return (error); 2861541Srgrimes } else 2871541Srgrimes if (tp->t_oproc) 2881541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2891541Srgrimes ptcwakeup(tp, FWRITE); 2901541Srgrimes return (error); 2911541Srgrimes} 2921541Srgrimes 2931541Srgrimes/* 2941541Srgrimes * Write to pseudo-tty. 2951541Srgrimes * Wakeups of controlling tty will happen 2961541Srgrimes * indirectly, when tty driver calls ptsstart. 2971541Srgrimes */ 29812675Sjulianstatic int 2991541Srgrimesptswrite(dev, uio, flag) 3001541Srgrimes dev_t dev; 3011541Srgrimes struct uio *uio; 3021541Srgrimes int flag; 3031541Srgrimes{ 3041541Srgrimes register struct tty *tp; 3051541Srgrimes 30649536Sphk tp = dev->si_tty_tty; 3071541Srgrimes if (tp->t_oproc == 0) 3081541Srgrimes return (EIO); 3091541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 3101541Srgrimes} 3111541Srgrimes 3121541Srgrimes/* 3131541Srgrimes * Start output on pseudo-tty. 3141541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3151541Srgrimes */ 31612819Sphkstatic void 3171541Srgrimesptsstart(tp) 3181541Srgrimes struct tty *tp; 3191541Srgrimes{ 32049536Sphk register struct pt_ioctl *pti = tp->t_dev->si_drv1; 3211541Srgrimes 3221541Srgrimes if (tp->t_state & TS_TTSTOP) 3231541Srgrimes return; 3241541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3251541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3261541Srgrimes pti->pt_send = TIOCPKT_START; 3271541Srgrimes } 3281541Srgrimes ptcwakeup(tp, FREAD); 3291541Srgrimes} 3301541Srgrimes 33112819Sphkstatic void 3321541Srgrimesptcwakeup(tp, flag) 3331541Srgrimes struct tty *tp; 3341541Srgrimes int flag; 3351541Srgrimes{ 33649536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3371541Srgrimes 3381541Srgrimes if (flag & FREAD) { 3391541Srgrimes selwakeup(&pti->pt_selr); 3409639Sbde wakeup(TSA_PTC_READ(tp)); 3411541Srgrimes } 3421541Srgrimes if (flag & FWRITE) { 3431541Srgrimes selwakeup(&pti->pt_selw); 3449639Sbde wakeup(TSA_PTC_WRITE(tp)); 3451541Srgrimes } 3461541Srgrimes} 3471541Srgrimes 34812675Sjulianstatic int 3491541Srgrimesptcopen(dev, flag, devtype, p) 3501541Srgrimes dev_t dev; 3511541Srgrimes int flag, devtype; 3521541Srgrimes struct proc *p; 3531541Srgrimes{ 3541541Srgrimes register struct tty *tp; 3551541Srgrimes struct pt_ioctl *pti; 3561541Srgrimes 35749536Sphk if (!dev->si_drv1) 35849536Sphk ptyinit(minor(dev)); 35949536Sphk if (!dev->si_drv1) 36049536Sphk return(ENXIO); 36149536Sphk tp = dev->si_tty_tty; 3621541Srgrimes if (tp->t_oproc) 3631541Srgrimes return (EIO); 3641541Srgrimes tp->t_oproc = ptsstart; 3651541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3661541Srgrimes tp->t_lflag &= ~EXTPROC; 36749536Sphk pti = dev->si_drv1; 3681541Srgrimes pti->pt_flags = 0; 3691541Srgrimes pti->pt_send = 0; 3701541Srgrimes pti->pt_ucntl = 0; 3711541Srgrimes return (0); 3721541Srgrimes} 3731541Srgrimes 37412675Sjulianstatic int 37510624Sbdeptcclose(dev, flags, fmt, p) 3761541Srgrimes dev_t dev; 37710624Sbde int flags; 37810624Sbde int fmt; 37910624Sbde struct proc *p; 3801541Srgrimes{ 3811541Srgrimes register struct tty *tp; 3821541Srgrimes 38349536Sphk tp = dev->si_tty_tty; 3841541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3859824Sbde 3869824Sbde /* 3879824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3889824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3899824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3909824Sbde * may be in use because other parts of the line discipline make 3919824Sbde * sense for ptys. Recover by doing everything that a normal 3929824Sbde * ttymodem() would have done except for sending a SIGHUP. 3939824Sbde */ 3949850Sbde if (tp->t_state & TS_ISOPEN) { 3959850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3969850Sbde tp->t_state |= TS_ZOMBIE; 3979850Sbde ttyflush(tp, FREAD | FWRITE); 3989850Sbde } 3999824Sbde 4001541Srgrimes tp->t_oproc = 0; /* mark closed */ 4011541Srgrimes return (0); 4021541Srgrimes} 4031541Srgrimes 40412675Sjulianstatic int 4051541Srgrimesptcread(dev, uio, flag) 4061541Srgrimes dev_t dev; 4071541Srgrimes struct uio *uio; 4081541Srgrimes int flag; 4091541Srgrimes{ 41049536Sphk register struct tty *tp = dev->si_tty_tty; 41149536Sphk struct pt_ioctl *pti = dev->si_drv1; 4121541Srgrimes char buf[BUFSIZ]; 4131541Srgrimes int error = 0, cc; 4141541Srgrimes 4151541Srgrimes /* 4161541Srgrimes * We want to block until the slave 4171541Srgrimes * is open, and there's something to read; 4181541Srgrimes * but if we lost the slave or we're NBIO, 4191541Srgrimes * then return the appropriate error instead. 4201541Srgrimes */ 4211541Srgrimes for (;;) { 4221541Srgrimes if (tp->t_state&TS_ISOPEN) { 4231541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4241541Srgrimes error = ureadc((int)pti->pt_send, uio); 4251541Srgrimes if (error) 4261541Srgrimes return (error); 4271541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4281541Srgrimes cc = min(uio->uio_resid, 4291541Srgrimes sizeof(tp->t_termios)); 4302807Sbde uiomove((caddr_t)&tp->t_termios, cc, 4312807Sbde uio); 4321541Srgrimes } 4331541Srgrimes pti->pt_send = 0; 4341541Srgrimes return (0); 4351541Srgrimes } 4361541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4371541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4381541Srgrimes if (error) 4391541Srgrimes return (error); 4401541Srgrimes pti->pt_ucntl = 0; 4411541Srgrimes return (0); 4421541Srgrimes } 4431541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4441541Srgrimes break; 4451541Srgrimes } 4469824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4471541Srgrimes return (0); /* EOF */ 4481541Srgrimes if (flag & IO_NDELAY) 4491541Srgrimes return (EWOULDBLOCK); 4509639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4513308Sphk if (error) 4521541Srgrimes return (error); 4531541Srgrimes } 4541541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4551541Srgrimes error = ureadc(0, uio); 4561541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4571541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4581541Srgrimes if (cc <= 0) 4591541Srgrimes break; 4601541Srgrimes error = uiomove(buf, cc, uio); 4611541Srgrimes } 4629626Sbde ttwwakeup(tp); 4631541Srgrimes return (error); 4641541Srgrimes} 4651541Srgrimes 46612675Sjulianstatic void 4671541Srgrimesptsstop(tp, flush) 4681541Srgrimes register struct tty *tp; 4691541Srgrimes int flush; 4701541Srgrimes{ 47149536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 4721541Srgrimes int flag; 4731541Srgrimes 4741541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4751541Srgrimes if (flush == 0) { 4761541Srgrimes flush = TIOCPKT_STOP; 4771541Srgrimes pti->pt_flags |= PF_STOPPED; 4781541Srgrimes } else 4791541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4801541Srgrimes pti->pt_send |= flush; 4811541Srgrimes /* change of perspective */ 4821541Srgrimes flag = 0; 4831541Srgrimes if (flush & FREAD) 4841541Srgrimes flag |= FWRITE; 4851541Srgrimes if (flush & FWRITE) 4861541Srgrimes flag |= FREAD; 4871541Srgrimes ptcwakeup(tp, flag); 4881541Srgrimes} 4891541Srgrimes 49012675Sjulianstatic int 49129354Speterptcpoll(dev, events, p) 4921541Srgrimes dev_t dev; 49329354Speter int events; 4941541Srgrimes struct proc *p; 4951541Srgrimes{ 49649536Sphk register struct tty *tp = dev->si_tty_tty; 49749536Sphk struct pt_ioctl *pti = dev->si_drv1; 49829354Speter int revents = 0; 4991541Srgrimes int s; 5001541Srgrimes 5019824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 50229354Speter return (seltrue(dev, events, p) | POLLHUP); 5031541Srgrimes 50429354Speter /* 50529354Speter * Need to block timeouts (ttrstart). 50629354Speter */ 50729354Speter s = spltty(); 5081541Srgrimes 50929354Speter if (events & (POLLIN | POLLRDNORM)) 51029354Speter if ((tp->t_state & TS_ISOPEN) && 51129354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 51229354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 51329354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 51429354Speter revents |= events & (POLLIN | POLLRDNORM); 5151541Srgrimes 51629354Speter if (events & (POLLOUT | POLLWRNORM)) 51729354Speter if (tp->t_state & TS_ISOPEN && 51829354Speter ((pti->pt_flags & PF_REMOTE) ? 51929354Speter (tp->t_canq.c_cc == 0) : 52029354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 52129354Speter (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON))))) 52229354Speter revents |= events & (POLLOUT | POLLWRNORM); 5231541Srgrimes 52429354Speter if (events & POLLHUP) 52529354Speter if ((tp->t_state & TS_CARR_ON) == 0) 52629354Speter revents |= POLLHUP; 5271541Srgrimes 52829354Speter if (revents == 0) { 52929354Speter if (events & (POLLIN | POLLRDNORM)) 53029354Speter selrecord(p, &pti->pt_selr); 53129354Speter 53229354Speter if (events & (POLLOUT | POLLWRNORM)) 53329354Speter selrecord(p, &pti->pt_selw); 5341541Srgrimes } 53529354Speter splx(s); 53629354Speter 53729354Speter return (revents); 5381541Srgrimes} 5391541Srgrimes 54012675Sjulianstatic int 5411541Srgrimesptcwrite(dev, uio, flag) 5421541Srgrimes dev_t dev; 5431541Srgrimes register struct uio *uio; 5441541Srgrimes int flag; 5451541Srgrimes{ 54649536Sphk register struct tty *tp = dev->si_tty_tty; 5471549Srgrimes register u_char *cp = 0; 5481541Srgrimes register int cc = 0; 5491541Srgrimes u_char locbuf[BUFSIZ]; 5501541Srgrimes int cnt = 0; 55149536Sphk struct pt_ioctl *pti = dev->si_drv1; 5521541Srgrimes int error = 0; 5531541Srgrimes 5541541Srgrimesagain: 5551541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5561541Srgrimes goto block; 5571541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5581541Srgrimes if (tp->t_canq.c_cc) 5591541Srgrimes goto block; 56011789Sbde while ((uio->uio_resid > 0 || cc > 0) && 56111789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5621541Srgrimes if (cc == 0) { 5631541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5641541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5651541Srgrimes cp = locbuf; 5661541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5671541Srgrimes if (error) 5681541Srgrimes return (error); 5691541Srgrimes /* check again for safety */ 57011789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 57111789Sbde /* adjust as usual */ 57211789Sbde uio->uio_resid += cc; 5731541Srgrimes return (EIO); 57411789Sbde } 5751541Srgrimes } 57615199Sbde if (cc > 0) { 57715199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 57815199Sbde /* 57915199Sbde * XXX we don't guarantee that the canq size 58015199Sbde * is >= TTYHOG, so the above b_to_q() may 58115199Sbde * leave some bytes uncopied. However, space 58215199Sbde * is guaranteed for the null terminator if 58315199Sbde * we don't fail here since (TTYHOG - 1) is 58415199Sbde * not a multiple of CBSIZE. 58515199Sbde */ 58615199Sbde if (cc > 0) 58715199Sbde break; 58815199Sbde } 5891541Srgrimes } 59011789Sbde /* adjust for data copied in but not written */ 59111789Sbde uio->uio_resid += cc; 5921541Srgrimes (void) putc(0, &tp->t_canq); 5931541Srgrimes ttwakeup(tp); 5949639Sbde wakeup(TSA_PTS_READ(tp)); 5951541Srgrimes return (0); 5961541Srgrimes } 59711789Sbde while (uio->uio_resid > 0 || cc > 0) { 5981541Srgrimes if (cc == 0) { 5991541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 6001541Srgrimes cp = locbuf; 6011541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 6021541Srgrimes if (error) 6031541Srgrimes return (error); 6041541Srgrimes /* check again for safety */ 60511789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 60611789Sbde /* adjust for data copied in but not written */ 60711789Sbde uio->uio_resid += cc; 6081541Srgrimes return (EIO); 60911789Sbde } 6101541Srgrimes } 6111541Srgrimes while (cc > 0) { 6121541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 6131541Srgrimes (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 6149824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 6151541Srgrimes goto block; 6161541Srgrimes } 6171541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 6181541Srgrimes cnt++; 6191541Srgrimes cc--; 6201541Srgrimes } 6211541Srgrimes cc = 0; 6221541Srgrimes } 6231541Srgrimes return (0); 6241541Srgrimesblock: 6251541Srgrimes /* 6261541Srgrimes * Come here to wait for slave to open, for space 62715199Sbde * in outq, or space in rawq, or an empty canq. 6281541Srgrimes */ 62911789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 63011789Sbde /* adjust for data copied in but not written */ 63111789Sbde uio->uio_resid += cc; 6321541Srgrimes return (EIO); 63311789Sbde } 6341541Srgrimes if (flag & IO_NDELAY) { 6351541Srgrimes /* adjust for data copied in but not written */ 6361541Srgrimes uio->uio_resid += cc; 6371541Srgrimes if (cnt == 0) 6381541Srgrimes return (EWOULDBLOCK); 6391541Srgrimes return (0); 6401541Srgrimes } 6419639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6423308Sphk if (error) { 6431541Srgrimes /* adjust for data copied in but not written */ 6441541Srgrimes uio->uio_resid += cc; 6451541Srgrimes return (error); 6461541Srgrimes } 6471541Srgrimes goto again; 6481541Srgrimes} 6491541Srgrimes 65012675Sjulianstatic struct tty * 6516712Spstptydevtotty(dev) 6526712Spst dev_t dev; 6536712Spst{ 65449536Sphk if (minor(dev) & ~0xff) 6556712Spst return (NULL); 6566712Spst 65749536Sphk return dev->si_tty_tty; 6586712Spst} 6596712Spst 6601541Srgrimes/*ARGSUSED*/ 66112675Sjulianstatic int 6621541Srgrimesptyioctl(dev, cmd, data, flag, p) 6631541Srgrimes dev_t dev; 66436735Sdfr u_long cmd; 6651541Srgrimes caddr_t data; 6661541Srgrimes int flag; 6671541Srgrimes struct proc *p; 6681541Srgrimes{ 66949536Sphk register struct tty *tp = dev->si_tty_tty; 67049536Sphk register struct pt_ioctl *pti = dev->si_drv1; 6711541Srgrimes register u_char *cc = tp->t_cc; 6721541Srgrimes int stop, error; 6731541Srgrimes 67447301Sluoqi if (devsw(dev)->d_open == ptcopen) { 6751541Srgrimes switch (cmd) { 6761541Srgrimes 6771541Srgrimes case TIOCGPGRP: 6781541Srgrimes /* 67933826Sbde * We avoid calling ttioctl on the controller since, 6801541Srgrimes * in that case, tp must be the controlling terminal. 6811541Srgrimes */ 6821541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6831541Srgrimes return (0); 6841541Srgrimes 6851541Srgrimes case TIOCPKT: 6861541Srgrimes if (*(int *)data) { 6871541Srgrimes if (pti->pt_flags & PF_UCNTL) 6881541Srgrimes return (EINVAL); 6891541Srgrimes pti->pt_flags |= PF_PKT; 6901541Srgrimes } else 6911541Srgrimes pti->pt_flags &= ~PF_PKT; 6921541Srgrimes return (0); 6931541Srgrimes 6941541Srgrimes case TIOCUCNTL: 6951541Srgrimes if (*(int *)data) { 6961541Srgrimes if (pti->pt_flags & PF_PKT) 6971541Srgrimes return (EINVAL); 6981541Srgrimes pti->pt_flags |= PF_UCNTL; 6991541Srgrimes } else 7001541Srgrimes pti->pt_flags &= ~PF_UCNTL; 7011541Srgrimes return (0); 7021541Srgrimes 7031541Srgrimes case TIOCREMOTE: 7041541Srgrimes if (*(int *)data) 7051541Srgrimes pti->pt_flags |= PF_REMOTE; 7061541Srgrimes else 7071541Srgrimes pti->pt_flags &= ~PF_REMOTE; 7081541Srgrimes ttyflush(tp, FREAD|FWRITE); 7091541Srgrimes return (0); 71047203Sluoqi } 7111541Srgrimes 71247203Sluoqi /* 71347203Sluoqi * The rest of the ioctls shouldn't be called until 71447301Sluoqi * the slave is open. 71547203Sluoqi */ 71647203Sluoqi if ((tp->t_state & TS_ISOPEN) == 0) 71747301Sluoqi return (EAGAIN); 71847203Sluoqi 71947203Sluoqi switch (cmd) { 7201541Srgrimes#ifdef COMPAT_43 7218876Srgrimes case TIOCSETP: 7221541Srgrimes case TIOCSETN: 7231541Srgrimes#endif 7241541Srgrimes case TIOCSETD: 7251541Srgrimes case TIOCSETA: 7261541Srgrimes case TIOCSETAW: 7279858Sache case TIOCSETAF: 72847301Sluoqi /* 72947301Sluoqi * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 73047301Sluoqi * ttywflush(tp) will hang if there are characters in 73147301Sluoqi * the outq. 73247301Sluoqi */ 7331541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7341541Srgrimes break; 7351541Srgrimes 7361541Srgrimes case TIOCSIG: 73727770Sjmg if (*(unsigned int *)data >= NSIG || 73827770Sjmg *(unsigned int *)data == 0) 7391541Srgrimes return(EINVAL); 7401541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7411541Srgrimes ttyflush(tp, FREAD|FWRITE); 7421541Srgrimes pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 7431541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7441541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7451541Srgrimes ttyinfo(tp); 7461541Srgrimes return(0); 7471541Srgrimes } 74847203Sluoqi } 74947301Sluoqi if (cmd == TIOCEXT) { 75047301Sluoqi /* 75147301Sluoqi * When the EXTPROC bit is being toggled, we need 75247301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 75347301Sluoqi * is turned on. 75447301Sluoqi */ 75547301Sluoqi if (*(int *)data) { 75647301Sluoqi if (pti->pt_flags & PF_PKT) { 75747301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 75847301Sluoqi ptcwakeup(tp, FREAD); 75947301Sluoqi } 76047301Sluoqi tp->t_lflag |= EXTPROC; 76147301Sluoqi } else { 76247301Sluoqi if ((tp->t_lflag & EXTPROC) && 76347301Sluoqi (pti->pt_flags & PF_PKT)) { 76447301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 76547301Sluoqi ptcwakeup(tp, FREAD); 76647301Sluoqi } 76747301Sluoqi tp->t_lflag &= ~EXTPROC; 76847301Sluoqi } 76947301Sluoqi return(0); 77047301Sluoqi } 7711541Srgrimes error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 77231577Sbde if (error == ENOIOCTL) 7731541Srgrimes error = ttioctl(tp, cmd, data, flag); 77431577Sbde if (error == ENOIOCTL) { 7751541Srgrimes if (pti->pt_flags & PF_UCNTL && 7761541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7771541Srgrimes if (cmd & 0xff) { 7781541Srgrimes pti->pt_ucntl = (u_char)cmd; 7791541Srgrimes ptcwakeup(tp, FREAD); 7801541Srgrimes } 7811541Srgrimes return (0); 7821541Srgrimes } 7831541Srgrimes error = ENOTTY; 7841541Srgrimes } 7851541Srgrimes /* 7861541Srgrimes * If external processing and packet mode send ioctl packet. 7871541Srgrimes */ 7881541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7891541Srgrimes switch(cmd) { 7901541Srgrimes case TIOCSETA: 7911541Srgrimes case TIOCSETAW: 7921541Srgrimes case TIOCSETAF: 7931541Srgrimes#ifdef COMPAT_43 7941541Srgrimes case TIOCSETP: 7951541Srgrimes case TIOCSETN: 7961541Srgrimes#endif 7971541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7981541Srgrimes case TIOCSETC: 7991541Srgrimes case TIOCSLTC: 8001541Srgrimes case TIOCLBIS: 8011541Srgrimes case TIOCLBIC: 8021541Srgrimes case TIOCLSET: 8031541Srgrimes#endif 8041541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 8051541Srgrimes ptcwakeup(tp, FREAD); 8061541Srgrimes default: 8071541Srgrimes break; 8081541Srgrimes } 8091541Srgrimes } 8108876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 8111541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 8121541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 8131541Srgrimes if (stop) { 8141541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 8151541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 8161541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 8171541Srgrimes ptcwakeup(tp, FREAD); 8181541Srgrimes } 8191541Srgrimes } else { 8201541Srgrimes if (!stop) { 8211541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8221541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8231541Srgrimes pti->pt_flags |= PF_NOSTOP; 8241541Srgrimes ptcwakeup(tp, FREAD); 8251541Srgrimes } 8261541Srgrimes } 8271541Srgrimes return (error); 8281541Srgrimes} 82912517Sjulian 83012517Sjulian 83129506Sbdestatic void ptc_drvinit __P((void *unused)); 83249536Sphk 83312675Sjulianstatic void 83429506Sbdeptc_drvinit(unused) 83529506Sbde void *unused; 83612517Sjulian{ 83750254Sphk cdevsw_add(&pts_cdevsw); 83850254Sphk cdevsw_add(&ptc_cdevsw); 83950254Sphk /* XXX: Gross hack for DEVFS */ 84050254Sphk ptyinit(0); 84112517Sjulian} 84212517Sjulian 84312517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 844