pty.c revision 91406
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 91406 2002-02-27 18:32:23Z jhb $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Pseudo-teletype Driver 391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw') 401541Srgrimes */ 4131778Seivind#include "opt_compat.h" 421541Srgrimes#include <sys/param.h> 431541Srgrimes#include <sys/systm.h> 4491140Stanimura#include <sys/lock.h> 4591140Stanimura#include <sys/mutex.h> 4691140Stanimura#include <sys/sx.h> 4724207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 4824207Sbde#include <sys/ioctl_compat.h> 4924207Sbde#endif 501541Srgrimes#include <sys/proc.h> 511541Srgrimes#include <sys/tty.h> 521541Srgrimes#include <sys/conf.h> 5324131Sbde#include <sys/fcntl.h> 5429354Speter#include <sys/poll.h> 551541Srgrimes#include <sys/kernel.h> 561541Srgrimes#include <sys/vnode.h> 573308Sphk#include <sys/signalvar.h> 5849536Sphk#include <sys/malloc.h> 591541Srgrimes 6069774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 6112517Sjulian 6212819Sphkstatic void ptsstart __P((struct tty *tp)); 6351654Sphkstatic void ptsstop __P((struct tty *tp, int rw)); 6412819Sphkstatic void ptcwakeup __P((struct tty *tp, int flag)); 6577176Sphkstatic dev_t ptyinit __P((dev_t cdev)); 6611789Sbde 6712675Sjulianstatic d_open_t ptsopen; 6812675Sjulianstatic d_close_t ptsclose; 6912675Sjulianstatic d_read_t ptsread; 7012675Sjulianstatic d_write_t ptswrite; 7112675Sjulianstatic d_ioctl_t ptyioctl; 7212675Sjulianstatic d_open_t ptcopen; 7312675Sjulianstatic d_close_t ptcclose; 7412675Sjulianstatic d_read_t ptcread; 7512675Sjulianstatic d_write_t ptcwrite; 7629354Speterstatic d_poll_t ptcpoll; 7712675Sjulian 7838485Sbde#define CDEV_MAJOR_S 5 7947625Sphkstatic struct cdevsw pts_cdevsw = { 8047625Sphk /* open */ ptsopen, 8147625Sphk /* close */ ptsclose, 8247625Sphk /* read */ ptsread, 8347625Sphk /* write */ ptswrite, 8447625Sphk /* ioctl */ ptyioctl, 8551654Sphk /* poll */ ttypoll, 8647625Sphk /* mmap */ nommap, 8747625Sphk /* strategy */ nostrategy, 8847625Sphk /* name */ "pts", 8947625Sphk /* maj */ CDEV_MAJOR_S, 9047625Sphk /* dump */ nodump, 9147625Sphk /* psize */ nopsize, 9272521Sjlemon /* flags */ D_TTY | D_KQFILTER, 9372521Sjlemon /* kqfilter */ ttykqfilter, 9438485Sbde}; 9512675Sjulian 9638485Sbde#define CDEV_MAJOR_C 6 9747625Sphkstatic struct cdevsw ptc_cdevsw = { 9847625Sphk /* open */ ptcopen, 9947625Sphk /* close */ ptcclose, 10047625Sphk /* read */ ptcread, 10147625Sphk /* write */ ptcwrite, 10247625Sphk /* ioctl */ ptyioctl, 10347625Sphk /* poll */ ptcpoll, 10447625Sphk /* mmap */ nommap, 10547625Sphk /* strategy */ nostrategy, 10647625Sphk /* name */ "ptc", 10747625Sphk /* maj */ CDEV_MAJOR_C, 10847625Sphk /* dump */ nodump, 10947625Sphk /* psize */ nopsize, 11072521Sjlemon /* flags */ D_TTY | D_KQFILTER, 11172521Sjlemon /* kqfilter */ ttykqfilter, 11238485Sbde}; 11312675Sjulian 1141541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1151541Srgrimes 11649536Sphkstruct pt_ioctl { 1171541Srgrimes int pt_flags; 1181541Srgrimes struct selinfo pt_selr, pt_selw; 1191541Srgrimes u_char pt_send; 1201541Srgrimes u_char pt_ucntl; 12149536Sphk struct tty pt_tty; 12250092Sjulian dev_t devs, devc; 12357070Srwatson struct prison *pt_prison; 12449536Sphk}; 1251541Srgrimes 1261541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1271541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1281541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1291541Srgrimes#define PF_NOSTOP 0x40 1301541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1311541Srgrimes 13277176Sphkstatic char *names = "pqrsPQRS"; 1331541Srgrimes/* 13449536Sphk * This function creates and initializes a pts/ptc pair 1351541Srgrimes * 13649536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13749536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13849536Sphk * 13949536Sphk * XXX: define and add mapping of upper minor bits to allow more 14049536Sphk * than 256 ptys. 1411541Srgrimes */ 14264880Sphkstatic dev_t 14377176Sphkptyinit(dev_t devc) 1441541Srgrimes{ 14577176Sphk dev_t devs; 14649536Sphk struct pt_ioctl *pt; 14777176Sphk int n; 1481541Srgrimes 14977176Sphk n = minor(devc); 15049536Sphk /* For now we only map the lower 8 bits of the minor */ 15149536Sphk if (n & ~0xff) 15264880Sphk return (NODEV); 15349536Sphk 15478405Sbrian devc->si_flags &= ~SI_CHEAPCLONE; 15578405Sbrian 15669781Sdwmalone pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO); 15750092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 15866067Sphk UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32); 15977176Sphk pt->devc = devc; 16049536Sphk 16149536Sphk devs->si_drv1 = devc->si_drv1 = pt; 16250652Sphk devs->si_tty = devc->si_tty = &pt->pt_tty; 16377360Sphk pt->pt_tty.t_dev = devs; 16449540Sphk ttyregister(&pt->pt_tty); 16564880Sphk return (devc); 16616322Sgpalmer} 1671541Srgrimes 1681541Srgrimes/*ARGSUSED*/ 16912675Sjulianstatic int 17083366Sjulianptsopen(dev, flag, devtype, td) 1711541Srgrimes dev_t dev; 1721541Srgrimes int flag, devtype; 17383366Sjulian struct thread *td; 1741541Srgrimes{ 1751541Srgrimes register struct tty *tp; 1761541Srgrimes int error; 17757070Srwatson struct pt_ioctl *pti; 1781541Srgrimes 17949536Sphk if (!dev->si_drv1) 18049536Sphk return(ENXIO); 18157070Srwatson pti = dev->si_drv1; 18250652Sphk tp = dev->si_tty; 1831541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1841541Srgrimes ttychars(tp); /* Set up default chars */ 1851541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1861541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1871541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1881541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1891541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 19091406Sjhb } else if (tp->t_state & TS_XCLUDE && suser_xxx(td->td_ucred, NULL, 0)) { 1911541Srgrimes return (EBUSY); 19291406Sjhb } else if (pti->pt_prison != td->td_ucred->cr_prison) { 19357070Srwatson return (EBUSY); 19457070Srwatson } 1951541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1969824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1971541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1981541Srgrimes if (flag&FNONBLOCK) 1991541Srgrimes break; 2009639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 2019624Sbde "ptsopn", 0); 2023308Sphk if (error) 2031541Srgrimes return (error); 2041541Srgrimes } 2051541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 2067724Sache if (error == 0) 2077724Sache ptcwakeup(tp, FREAD|FWRITE); 2081541Srgrimes return (error); 2091541Srgrimes} 2101541Srgrimes 21112675Sjulianstatic int 21283366Sjulianptsclose(dev, flag, mode, td) 2131541Srgrimes dev_t dev; 2141541Srgrimes int flag, mode; 21583366Sjulian struct thread *td; 2161541Srgrimes{ 2171541Srgrimes register struct tty *tp; 2181541Srgrimes int err; 2191541Srgrimes 22050652Sphk tp = dev->si_tty; 2211541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2227730Sache ptsstop(tp, FREAD|FWRITE); 2237724Sache (void) ttyclose(tp); 2241541Srgrimes return (err); 2251541Srgrimes} 2261541Srgrimes 22712675Sjulianstatic int 2281541Srgrimesptsread(dev, uio, flag) 2291541Srgrimes dev_t dev; 2301541Srgrimes struct uio *uio; 2311541Srgrimes int flag; 2321541Srgrimes{ 23383366Sjulian struct thread *td = curthread; 23483366Sjulian struct proc *p = td->td_proc; 23550652Sphk register struct tty *tp = dev->si_tty; 23649536Sphk register struct pt_ioctl *pti = dev->si_drv1; 2371541Srgrimes int error = 0; 2381541Srgrimes 2391541Srgrimesagain: 2401541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2411541Srgrimes while (isbackground(p, tp)) { 24291140Stanimura PGRPSESS_SLOCK(); 24391140Stanimura PROC_LOCK(p); 24451791Smarcel if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 24551791Smarcel SIGISMEMBER(p->p_sigmask, SIGTTIN) || 24691140Stanimura p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) { 24791140Stanimura PROC_UNLOCK(p); 2481541Srgrimes return (EIO); 24991140Stanimura } 25091140Stanimura PROC_UNLOCK(p); 25191140Stanimura PGRP_LOCK(p->p_pgrp); 25291140Stanimura PGRPSESS_SUNLOCK(); 2531541Srgrimes pgsignal(p->p_pgrp, SIGTTIN, 1); 25491140Stanimura PGRP_UNLOCK(p->p_pgrp); 2559624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2569624Sbde 0); 2573308Sphk if (error) 2581541Srgrimes return (error); 2591541Srgrimes } 2601541Srgrimes if (tp->t_canq.c_cc == 0) { 2611541Srgrimes if (flag & IO_NDELAY) 2621541Srgrimes return (EWOULDBLOCK); 2639639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2649624Sbde "ptsin", 0); 2653308Sphk if (error) 2661541Srgrimes return (error); 2671541Srgrimes goto again; 2681541Srgrimes } 2691541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2701541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2711541Srgrimes error = EFAULT; 2721541Srgrimes break; 2731541Srgrimes } 2741541Srgrimes if (tp->t_canq.c_cc == 1) 2751541Srgrimes (void) getc(&tp->t_canq); 2761541Srgrimes if (tp->t_canq.c_cc) 2771541Srgrimes return (error); 2781541Srgrimes } else 2791541Srgrimes if (tp->t_oproc) 2801541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2811541Srgrimes ptcwakeup(tp, FWRITE); 2821541Srgrimes return (error); 2831541Srgrimes} 2841541Srgrimes 2851541Srgrimes/* 2861541Srgrimes * Write to pseudo-tty. 2871541Srgrimes * Wakeups of controlling tty will happen 2881541Srgrimes * indirectly, when tty driver calls ptsstart. 2891541Srgrimes */ 29012675Sjulianstatic int 2911541Srgrimesptswrite(dev, uio, flag) 2921541Srgrimes dev_t dev; 2931541Srgrimes struct uio *uio; 2941541Srgrimes int flag; 2951541Srgrimes{ 2961541Srgrimes register struct tty *tp; 2971541Srgrimes 29850652Sphk tp = dev->si_tty; 2991541Srgrimes if (tp->t_oproc == 0) 3001541Srgrimes return (EIO); 3011541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 3021541Srgrimes} 3031541Srgrimes 3041541Srgrimes/* 3051541Srgrimes * Start output on pseudo-tty. 3061541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3071541Srgrimes */ 30812819Sphkstatic void 3091541Srgrimesptsstart(tp) 3101541Srgrimes struct tty *tp; 3111541Srgrimes{ 31249536Sphk register struct pt_ioctl *pti = tp->t_dev->si_drv1; 3131541Srgrimes 3141541Srgrimes if (tp->t_state & TS_TTSTOP) 3151541Srgrimes return; 3161541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3171541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3181541Srgrimes pti->pt_send = TIOCPKT_START; 3191541Srgrimes } 3201541Srgrimes ptcwakeup(tp, FREAD); 3211541Srgrimes} 3221541Srgrimes 32312819Sphkstatic void 3241541Srgrimesptcwakeup(tp, flag) 3251541Srgrimes struct tty *tp; 3261541Srgrimes int flag; 3271541Srgrimes{ 32849536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3291541Srgrimes 3301541Srgrimes if (flag & FREAD) { 3311541Srgrimes selwakeup(&pti->pt_selr); 3329639Sbde wakeup(TSA_PTC_READ(tp)); 3331541Srgrimes } 3341541Srgrimes if (flag & FWRITE) { 3351541Srgrimes selwakeup(&pti->pt_selw); 3369639Sbde wakeup(TSA_PTC_WRITE(tp)); 3371541Srgrimes } 3381541Srgrimes} 3391541Srgrimes 34012675Sjulianstatic int 34183366Sjulianptcopen(dev, flag, devtype, td) 3421541Srgrimes dev_t dev; 3431541Srgrimes int flag, devtype; 34483366Sjulian struct thread *td; 3451541Srgrimes{ 3461541Srgrimes register struct tty *tp; 3471541Srgrimes struct pt_ioctl *pti; 3481541Srgrimes 34949536Sphk if (!dev->si_drv1) 35077176Sphk ptyinit(dev); 35149536Sphk if (!dev->si_drv1) 35249536Sphk return(ENXIO); 35350652Sphk tp = dev->si_tty; 3541541Srgrimes if (tp->t_oproc) 3551541Srgrimes return (EIO); 35659818Sache tp->t_timeout = -1; 3571541Srgrimes tp->t_oproc = ptsstart; 35851654Sphk tp->t_stop = ptsstop; 3591541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3601541Srgrimes tp->t_lflag &= ~EXTPROC; 36149536Sphk pti = dev->si_drv1; 36291406Sjhb pti->pt_prison = td->td_ucred->cr_prison; 3631541Srgrimes pti->pt_flags = 0; 3641541Srgrimes pti->pt_send = 0; 3651541Srgrimes pti->pt_ucntl = 0; 3661541Srgrimes return (0); 3671541Srgrimes} 3681541Srgrimes 36912675Sjulianstatic int 37083366Sjulianptcclose(dev, flags, fmt, td) 3711541Srgrimes dev_t dev; 37210624Sbde int flags; 37310624Sbde int fmt; 37483366Sjulian struct thread *td; 3751541Srgrimes{ 3761541Srgrimes register struct tty *tp; 3771541Srgrimes 37850652Sphk tp = dev->si_tty; 3791541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3809824Sbde 3819824Sbde /* 3829824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3839824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3849824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3859824Sbde * may be in use because other parts of the line discipline make 3869824Sbde * sense for ptys. Recover by doing everything that a normal 3879824Sbde * ttymodem() would have done except for sending a SIGHUP. 3889824Sbde */ 3899850Sbde if (tp->t_state & TS_ISOPEN) { 3909850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3919850Sbde tp->t_state |= TS_ZOMBIE; 3929850Sbde ttyflush(tp, FREAD | FWRITE); 3939850Sbde } 3949824Sbde 3951541Srgrimes tp->t_oproc = 0; /* mark closed */ 3961541Srgrimes return (0); 3971541Srgrimes} 3981541Srgrimes 39912675Sjulianstatic int 4001541Srgrimesptcread(dev, uio, flag) 4011541Srgrimes dev_t dev; 4021541Srgrimes struct uio *uio; 4031541Srgrimes int flag; 4041541Srgrimes{ 40550652Sphk register struct tty *tp = dev->si_tty; 40649536Sphk struct pt_ioctl *pti = dev->si_drv1; 4071541Srgrimes char buf[BUFSIZ]; 4081541Srgrimes int error = 0, cc; 4091541Srgrimes 4101541Srgrimes /* 4111541Srgrimes * We want to block until the slave 4121541Srgrimes * is open, and there's something to read; 4131541Srgrimes * but if we lost the slave or we're NBIO, 4141541Srgrimes * then return the appropriate error instead. 4151541Srgrimes */ 4161541Srgrimes for (;;) { 4171541Srgrimes if (tp->t_state&TS_ISOPEN) { 4181541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4191541Srgrimes error = ureadc((int)pti->pt_send, uio); 4201541Srgrimes if (error) 4211541Srgrimes return (error); 4221541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4231541Srgrimes cc = min(uio->uio_resid, 4241541Srgrimes sizeof(tp->t_termios)); 4252807Sbde uiomove((caddr_t)&tp->t_termios, cc, 4262807Sbde uio); 4271541Srgrimes } 4281541Srgrimes pti->pt_send = 0; 4291541Srgrimes return (0); 4301541Srgrimes } 4311541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4321541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4331541Srgrimes if (error) 4341541Srgrimes return (error); 4351541Srgrimes pti->pt_ucntl = 0; 4361541Srgrimes return (0); 4371541Srgrimes } 4381541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4391541Srgrimes break; 4401541Srgrimes } 4419824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4421541Srgrimes return (0); /* EOF */ 4431541Srgrimes if (flag & IO_NDELAY) 4441541Srgrimes return (EWOULDBLOCK); 4459639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4463308Sphk if (error) 4471541Srgrimes return (error); 4481541Srgrimes } 4491541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4501541Srgrimes error = ureadc(0, uio); 4511541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4521541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4531541Srgrimes if (cc <= 0) 4541541Srgrimes break; 4551541Srgrimes error = uiomove(buf, cc, uio); 4561541Srgrimes } 4579626Sbde ttwwakeup(tp); 4581541Srgrimes return (error); 4591541Srgrimes} 4601541Srgrimes 46112675Sjulianstatic void 4621541Srgrimesptsstop(tp, flush) 4631541Srgrimes register struct tty *tp; 4641541Srgrimes int flush; 4651541Srgrimes{ 46649536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 4671541Srgrimes int flag; 4681541Srgrimes 4691541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4701541Srgrimes if (flush == 0) { 4711541Srgrimes flush = TIOCPKT_STOP; 4721541Srgrimes pti->pt_flags |= PF_STOPPED; 4731541Srgrimes } else 4741541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4751541Srgrimes pti->pt_send |= flush; 4761541Srgrimes /* change of perspective */ 4771541Srgrimes flag = 0; 4781541Srgrimes if (flush & FREAD) 4791541Srgrimes flag |= FWRITE; 4801541Srgrimes if (flush & FWRITE) 4811541Srgrimes flag |= FREAD; 4821541Srgrimes ptcwakeup(tp, flag); 4831541Srgrimes} 4841541Srgrimes 48512675Sjulianstatic int 48683366Sjulianptcpoll(dev, events, td) 4871541Srgrimes dev_t dev; 48829354Speter int events; 48983366Sjulian struct thread *td; 4901541Srgrimes{ 49150652Sphk register struct tty *tp = dev->si_tty; 49249536Sphk struct pt_ioctl *pti = dev->si_drv1; 49329354Speter int revents = 0; 4941541Srgrimes int s; 4951541Srgrimes 4969824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 49783366Sjulian return (seltrue(dev, events, td) | POLLHUP); 4981541Srgrimes 49929354Speter /* 50029354Speter * Need to block timeouts (ttrstart). 50129354Speter */ 50229354Speter s = spltty(); 5031541Srgrimes 50429354Speter if (events & (POLLIN | POLLRDNORM)) 50529354Speter if ((tp->t_state & TS_ISOPEN) && 50629354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 50729354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 50829354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 50929354Speter revents |= events & (POLLIN | POLLRDNORM); 5101541Srgrimes 51129354Speter if (events & (POLLOUT | POLLWRNORM)) 51229354Speter if (tp->t_state & TS_ISOPEN && 51329354Speter ((pti->pt_flags & PF_REMOTE) ? 51429354Speter (tp->t_canq.c_cc == 0) : 51529354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 51690831Sdillon (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) 51729354Speter revents |= events & (POLLOUT | POLLWRNORM); 5181541Srgrimes 51929354Speter if (events & POLLHUP) 52029354Speter if ((tp->t_state & TS_CARR_ON) == 0) 52129354Speter revents |= POLLHUP; 5221541Srgrimes 52329354Speter if (revents == 0) { 52429354Speter if (events & (POLLIN | POLLRDNORM)) 52583803Sjhb selrecord(td, &pti->pt_selr); 52629354Speter 52729354Speter if (events & (POLLOUT | POLLWRNORM)) 52883803Sjhb selrecord(td, &pti->pt_selw); 5291541Srgrimes } 53029354Speter splx(s); 53129354Speter 53229354Speter return (revents); 5331541Srgrimes} 5341541Srgrimes 53512675Sjulianstatic int 5361541Srgrimesptcwrite(dev, uio, flag) 5371541Srgrimes dev_t dev; 5381541Srgrimes register struct uio *uio; 5391541Srgrimes int flag; 5401541Srgrimes{ 54150652Sphk register struct tty *tp = dev->si_tty; 5421549Srgrimes register u_char *cp = 0; 5431541Srgrimes register int cc = 0; 5441541Srgrimes u_char locbuf[BUFSIZ]; 5451541Srgrimes int cnt = 0; 54649536Sphk struct pt_ioctl *pti = dev->si_drv1; 5471541Srgrimes int error = 0; 5481541Srgrimes 5491541Srgrimesagain: 5501541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5511541Srgrimes goto block; 5521541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5531541Srgrimes if (tp->t_canq.c_cc) 5541541Srgrimes goto block; 55511789Sbde while ((uio->uio_resid > 0 || cc > 0) && 55611789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5571541Srgrimes if (cc == 0) { 5581541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5591541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5601541Srgrimes cp = locbuf; 5611541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5621541Srgrimes if (error) 5631541Srgrimes return (error); 5641541Srgrimes /* check again for safety */ 56511789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 56611789Sbde /* adjust as usual */ 56711789Sbde uio->uio_resid += cc; 5681541Srgrimes return (EIO); 56911789Sbde } 5701541Srgrimes } 57115199Sbde if (cc > 0) { 57215199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 57315199Sbde /* 57415199Sbde * XXX we don't guarantee that the canq size 57515199Sbde * is >= TTYHOG, so the above b_to_q() may 57615199Sbde * leave some bytes uncopied. However, space 57715199Sbde * is guaranteed for the null terminator if 57815199Sbde * we don't fail here since (TTYHOG - 1) is 57915199Sbde * not a multiple of CBSIZE. 58015199Sbde */ 58115199Sbde if (cc > 0) 58215199Sbde break; 58315199Sbde } 5841541Srgrimes } 58511789Sbde /* adjust for data copied in but not written */ 58611789Sbde uio->uio_resid += cc; 5871541Srgrimes (void) putc(0, &tp->t_canq); 5881541Srgrimes ttwakeup(tp); 5899639Sbde wakeup(TSA_PTS_READ(tp)); 5901541Srgrimes return (0); 5911541Srgrimes } 59211789Sbde while (uio->uio_resid > 0 || cc > 0) { 5931541Srgrimes if (cc == 0) { 5941541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5951541Srgrimes cp = locbuf; 5961541Srgrimes error = uiomove((caddr_t)cp, cc, uio); 5971541Srgrimes if (error) 5981541Srgrimes return (error); 5991541Srgrimes /* check again for safety */ 60011789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 60111789Sbde /* adjust for data copied in but not written */ 60211789Sbde uio->uio_resid += cc; 6031541Srgrimes return (EIO); 60411789Sbde } 6051541Srgrimes } 6061541Srgrimes while (cc > 0) { 6071541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 60890831Sdillon (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { 6099824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 6101541Srgrimes goto block; 6111541Srgrimes } 6121541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 6131541Srgrimes cnt++; 6141541Srgrimes cc--; 6151541Srgrimes } 6161541Srgrimes cc = 0; 6171541Srgrimes } 6181541Srgrimes return (0); 6191541Srgrimesblock: 6201541Srgrimes /* 6211541Srgrimes * Come here to wait for slave to open, for space 62215199Sbde * in outq, or space in rawq, or an empty canq. 6231541Srgrimes */ 62411789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 62511789Sbde /* adjust for data copied in but not written */ 62611789Sbde uio->uio_resid += cc; 6271541Srgrimes return (EIO); 62811789Sbde } 6291541Srgrimes if (flag & IO_NDELAY) { 6301541Srgrimes /* adjust for data copied in but not written */ 6311541Srgrimes uio->uio_resid += cc; 6321541Srgrimes if (cnt == 0) 6331541Srgrimes return (EWOULDBLOCK); 6341541Srgrimes return (0); 6351541Srgrimes } 6369639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6373308Sphk if (error) { 6381541Srgrimes /* adjust for data copied in but not written */ 6391541Srgrimes uio->uio_resid += cc; 6401541Srgrimes return (error); 6411541Srgrimes } 6421541Srgrimes goto again; 6431541Srgrimes} 6441541Srgrimes 6451541Srgrimes/*ARGSUSED*/ 64612675Sjulianstatic int 64783366Sjulianptyioctl(dev, cmd, data, flag, td) 6481541Srgrimes dev_t dev; 64936735Sdfr u_long cmd; 6501541Srgrimes caddr_t data; 6511541Srgrimes int flag; 65283366Sjulian struct thread *td; 6531541Srgrimes{ 65450652Sphk register struct tty *tp = dev->si_tty; 65549536Sphk register struct pt_ioctl *pti = dev->si_drv1; 6561541Srgrimes register u_char *cc = tp->t_cc; 6571541Srgrimes int stop, error; 6581541Srgrimes 65947301Sluoqi if (devsw(dev)->d_open == ptcopen) { 6601541Srgrimes switch (cmd) { 6611541Srgrimes 6621541Srgrimes case TIOCGPGRP: 6631541Srgrimes /* 66433826Sbde * We avoid calling ttioctl on the controller since, 6651541Srgrimes * in that case, tp must be the controlling terminal. 6661541Srgrimes */ 6671541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6681541Srgrimes return (0); 6691541Srgrimes 6701541Srgrimes case TIOCPKT: 6711541Srgrimes if (*(int *)data) { 6721541Srgrimes if (pti->pt_flags & PF_UCNTL) 6731541Srgrimes return (EINVAL); 6741541Srgrimes pti->pt_flags |= PF_PKT; 6751541Srgrimes } else 6761541Srgrimes pti->pt_flags &= ~PF_PKT; 6771541Srgrimes return (0); 6781541Srgrimes 6791541Srgrimes case TIOCUCNTL: 6801541Srgrimes if (*(int *)data) { 6811541Srgrimes if (pti->pt_flags & PF_PKT) 6821541Srgrimes return (EINVAL); 6831541Srgrimes pti->pt_flags |= PF_UCNTL; 6841541Srgrimes } else 6851541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6861541Srgrimes return (0); 6871541Srgrimes 6881541Srgrimes case TIOCREMOTE: 6891541Srgrimes if (*(int *)data) 6901541Srgrimes pti->pt_flags |= PF_REMOTE; 6911541Srgrimes else 6921541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6931541Srgrimes ttyflush(tp, FREAD|FWRITE); 6941541Srgrimes return (0); 69547203Sluoqi } 6961541Srgrimes 69747203Sluoqi /* 69847203Sluoqi * The rest of the ioctls shouldn't be called until 69947301Sluoqi * the slave is open. 70047203Sluoqi */ 70147203Sluoqi if ((tp->t_state & TS_ISOPEN) == 0) 70247301Sluoqi return (EAGAIN); 70347203Sluoqi 70447203Sluoqi switch (cmd) { 7051541Srgrimes#ifdef COMPAT_43 7068876Srgrimes case TIOCSETP: 7071541Srgrimes case TIOCSETN: 7081541Srgrimes#endif 7091541Srgrimes case TIOCSETD: 7101541Srgrimes case TIOCSETA: 7111541Srgrimes case TIOCSETAW: 7129858Sache case TIOCSETAF: 71347301Sluoqi /* 71447301Sluoqi * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 71547301Sluoqi * ttywflush(tp) will hang if there are characters in 71647301Sluoqi * the outq. 71747301Sluoqi */ 7181541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7191541Srgrimes break; 7201541Srgrimes 7211541Srgrimes case TIOCSIG: 72227770Sjmg if (*(unsigned int *)data >= NSIG || 72327770Sjmg *(unsigned int *)data == 0) 7241541Srgrimes return(EINVAL); 7251541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7261541Srgrimes ttyflush(tp, FREAD|FWRITE); 72791140Stanimura if (tp->t_pgrp != NULL) { 72891140Stanimura PGRP_LOCK(tp->t_pgrp); 72991140Stanimura pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 73091140Stanimura PGRP_UNLOCK(tp->t_pgrp); 73191140Stanimura } 7321541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7331541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7341541Srgrimes ttyinfo(tp); 7351541Srgrimes return(0); 7361541Srgrimes } 73747203Sluoqi } 73847301Sluoqi if (cmd == TIOCEXT) { 73947301Sluoqi /* 74047301Sluoqi * When the EXTPROC bit is being toggled, we need 74147301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 74247301Sluoqi * is turned on. 74347301Sluoqi */ 74447301Sluoqi if (*(int *)data) { 74547301Sluoqi if (pti->pt_flags & PF_PKT) { 74647301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74747301Sluoqi ptcwakeup(tp, FREAD); 74847301Sluoqi } 74947301Sluoqi tp->t_lflag |= EXTPROC; 75047301Sluoqi } else { 75147301Sluoqi if ((tp->t_lflag & EXTPROC) && 75247301Sluoqi (pti->pt_flags & PF_PKT)) { 75347301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 75447301Sluoqi ptcwakeup(tp, FREAD); 75547301Sluoqi } 75647301Sluoqi tp->t_lflag &= ~EXTPROC; 75747301Sluoqi } 75847301Sluoqi return(0); 75947301Sluoqi } 76083366Sjulian error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 76131577Sbde if (error == ENOIOCTL) 7621541Srgrimes error = ttioctl(tp, cmd, data, flag); 76331577Sbde if (error == ENOIOCTL) { 7641541Srgrimes if (pti->pt_flags & PF_UCNTL && 7651541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7661541Srgrimes if (cmd & 0xff) { 7671541Srgrimes pti->pt_ucntl = (u_char)cmd; 7681541Srgrimes ptcwakeup(tp, FREAD); 7691541Srgrimes } 7701541Srgrimes return (0); 7711541Srgrimes } 7721541Srgrimes error = ENOTTY; 7731541Srgrimes } 7741541Srgrimes /* 7751541Srgrimes * If external processing and packet mode send ioctl packet. 7761541Srgrimes */ 7771541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7781541Srgrimes switch(cmd) { 7791541Srgrimes case TIOCSETA: 7801541Srgrimes case TIOCSETAW: 7811541Srgrimes case TIOCSETAF: 7821541Srgrimes#ifdef COMPAT_43 7831541Srgrimes case TIOCSETP: 7841541Srgrimes case TIOCSETN: 7851541Srgrimes#endif 7861541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7871541Srgrimes case TIOCSETC: 7881541Srgrimes case TIOCSLTC: 7891541Srgrimes case TIOCLBIS: 7901541Srgrimes case TIOCLBIC: 7911541Srgrimes case TIOCLSET: 7921541Srgrimes#endif 7931541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7941541Srgrimes ptcwakeup(tp, FREAD); 7951541Srgrimes default: 7961541Srgrimes break; 7971541Srgrimes } 7981541Srgrimes } 7998876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 8001541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 8011541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 8021541Srgrimes if (stop) { 8031541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 8041541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 8051541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 8061541Srgrimes ptcwakeup(tp, FREAD); 8071541Srgrimes } 8081541Srgrimes } else { 8091541Srgrimes if (!stop) { 8101541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8111541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8121541Srgrimes pti->pt_flags |= PF_NOSTOP; 8131541Srgrimes ptcwakeup(tp, FREAD); 8141541Srgrimes } 8151541Srgrimes } 8161541Srgrimes return (error); 8171541Srgrimes} 81812517Sjulian 81912517Sjulian 82029506Sbdestatic void ptc_drvinit __P((void *unused)); 82149536Sphk 82264880Sphkstatic void pty_clone __P((void *arg, char *name, int namelen, dev_t *dev)); 82364880Sphk 82412675Sjulianstatic void 82564880Sphkpty_clone(arg, name, namelen, dev) 82664880Sphk void *arg; 82764880Sphk char *name; 82864880Sphk int namelen; 82964880Sphk dev_t *dev; 83064880Sphk{ 83164880Sphk int u; 83264880Sphk 83364880Sphk if (*dev != NODEV) 83464880Sphk return; 83564880Sphk if (bcmp(name, "pty", 3) != 0) 83664880Sphk return; 83764880Sphk if (name[5] != '\0') 83864880Sphk return; 83964880Sphk switch (name[3]) { 84064880Sphk case 'p': u = 0; break; 84164880Sphk case 'q': u = 32; break; 84264880Sphk case 'r': u = 64; break; 84364880Sphk case 's': u = 96; break; 84464880Sphk case 'P': u = 128; break; 84564880Sphk case 'Q': u = 160; break; 84664880Sphk case 'R': u = 192; break; 84764880Sphk case 'S': u = 224; break; 84864880Sphk default: return; 84964880Sphk } 85064880Sphk if (name[4] >= '0' && name[4] <= '9') 85164880Sphk u += name[4] - '0'; 85264880Sphk else if (name[4] >= 'a' && name[4] <= 'v') 85364880Sphk u += name[4] - 'a' + 10; 85464880Sphk else 85564880Sphk return; 85677176Sphk *dev = make_dev(&ptc_cdevsw, u, 85777176Sphk UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32); 85877176Sphk (*dev)->si_flags |= SI_CHEAPCLONE; 85964880Sphk return; 86064880Sphk} 86164880Sphk 86264880Sphkstatic void 86329506Sbdeptc_drvinit(unused) 86429506Sbde void *unused; 86512517Sjulian{ 86665374Sphk EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 86753483Sjkh cdevsw_add(&pts_cdevsw); 86853483Sjkh cdevsw_add(&ptc_cdevsw); 86912517Sjulian} 87012517Sjulian 87112517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 872