pty.c revision 111821
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 111821 2003-03-03 16:24:47Z phk $ 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 6292723Salfredstatic void ptsstart(struct tty *tp); 6392723Salfredstatic void ptsstop(struct tty *tp, int rw); 6492723Salfredstatic void ptcwakeup(struct tty *tp, int flag); 6592723Salfredstatic dev_t ptyinit(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 = { 80111815Sphk .d_open = ptsopen, 81111815Sphk .d_close = ptsclose, 82111815Sphk .d_read = ptsread, 83111815Sphk .d_write = ptswrite, 84111815Sphk .d_ioctl = ptyioctl, 85111815Sphk .d_poll = ttypoll, 86111815Sphk .d_name = "pts", 87111815Sphk .d_maj = CDEV_MAJOR_S, 88111821Sphk .d_flags = D_TTY, 89111815Sphk .d_kqfilter = ttykqfilter, 9038485Sbde}; 9112675Sjulian 9238485Sbde#define CDEV_MAJOR_C 6 9347625Sphkstatic struct cdevsw ptc_cdevsw = { 94111815Sphk .d_open = ptcopen, 95111815Sphk .d_close = ptcclose, 96111815Sphk .d_read = ptcread, 97111815Sphk .d_write = ptcwrite, 98111815Sphk .d_ioctl = ptyioctl, 99111815Sphk .d_poll = ptcpoll, 100111815Sphk .d_name = "ptc", 101111815Sphk .d_maj = CDEV_MAJOR_C, 102111821Sphk .d_flags = D_TTY, 103111815Sphk .d_kqfilter = ttykqfilter, 10438485Sbde}; 10512675Sjulian 1061541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1071541Srgrimes 10849536Sphkstruct pt_ioctl { 1091541Srgrimes int pt_flags; 1101541Srgrimes struct selinfo pt_selr, pt_selw; 1111541Srgrimes u_char pt_send; 1121541Srgrimes u_char pt_ucntl; 11349536Sphk struct tty pt_tty; 11450092Sjulian dev_t devs, devc; 11557070Srwatson struct prison *pt_prison; 11649536Sphk}; 1171541Srgrimes 1181541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1191541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1201541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1211541Srgrimes#define PF_NOSTOP 0x40 1221541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1231541Srgrimes 12477176Sphkstatic char *names = "pqrsPQRS"; 1251541Srgrimes/* 12649536Sphk * This function creates and initializes a pts/ptc pair 1271541Srgrimes * 12849536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 12949536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13049536Sphk * 131111742Sdes * XXX: define and add mapping of upper minor bits to allow more 13249536Sphk * than 256 ptys. 1331541Srgrimes */ 13464880Sphkstatic dev_t 13577176Sphkptyinit(dev_t devc) 1361541Srgrimes{ 13777176Sphk dev_t devs; 13849536Sphk struct pt_ioctl *pt; 13977176Sphk int n; 1401541Srgrimes 14177176Sphk n = minor(devc); 14249536Sphk /* For now we only map the lower 8 bits of the minor */ 14349536Sphk if (n & ~0xff) 14464880Sphk return (NODEV); 14549536Sphk 14678405Sbrian devc->si_flags &= ~SI_CHEAPCLONE; 14778405Sbrian 148111119Simp pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO); 14950092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 15066067Sphk UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32); 15177176Sphk pt->devc = devc; 15249536Sphk 15349536Sphk devs->si_drv1 = devc->si_drv1 = pt; 15450652Sphk devs->si_tty = devc->si_tty = &pt->pt_tty; 15577360Sphk pt->pt_tty.t_dev = devs; 15649540Sphk ttyregister(&pt->pt_tty); 15764880Sphk return (devc); 15816322Sgpalmer} 1591541Srgrimes 1601541Srgrimes/*ARGSUSED*/ 16112675Sjulianstatic int 16283366Sjulianptsopen(dev, flag, devtype, td) 1631541Srgrimes dev_t dev; 1641541Srgrimes int flag, devtype; 16583366Sjulian struct thread *td; 1661541Srgrimes{ 167111742Sdes struct tty *tp; 1681541Srgrimes int error; 16957070Srwatson struct pt_ioctl *pti; 1701541Srgrimes 17149536Sphk if (!dev->si_drv1) 172111742Sdes return(ENXIO); 17357070Srwatson pti = dev->si_drv1; 17450652Sphk tp = dev->si_tty; 1751541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1761541Srgrimes ttychars(tp); /* Set up default chars */ 1771541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1781541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1791541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1801541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1811541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 18293593Sjhb } else if (tp->t_state & TS_XCLUDE && suser(td)) { 1831541Srgrimes return (EBUSY); 18491406Sjhb } else if (pti->pt_prison != td->td_ucred->cr_prison) { 18557070Srwatson return (EBUSY); 18657070Srwatson } 1871541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1889824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1891541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1901541Srgrimes if (flag&FNONBLOCK) 1911541Srgrimes break; 1929639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1939624Sbde "ptsopn", 0); 1943308Sphk if (error) 1951541Srgrimes return (error); 1961541Srgrimes } 1971541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1987724Sache if (error == 0) 1997724Sache ptcwakeup(tp, FREAD|FWRITE); 2001541Srgrimes return (error); 2011541Srgrimes} 2021541Srgrimes 20312675Sjulianstatic int 20483366Sjulianptsclose(dev, flag, mode, td) 2051541Srgrimes dev_t dev; 2061541Srgrimes int flag, mode; 20783366Sjulian struct thread *td; 2081541Srgrimes{ 209111742Sdes struct tty *tp; 2101541Srgrimes int err; 2111541Srgrimes 21250652Sphk tp = dev->si_tty; 2131541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2147730Sache ptsstop(tp, FREAD|FWRITE); 2157724Sache (void) ttyclose(tp); 2161541Srgrimes return (err); 2171541Srgrimes} 2181541Srgrimes 21912675Sjulianstatic int 2201541Srgrimesptsread(dev, uio, flag) 2211541Srgrimes dev_t dev; 2221541Srgrimes struct uio *uio; 2231541Srgrimes int flag; 2241541Srgrimes{ 22583366Sjulian struct thread *td = curthread; 22683366Sjulian struct proc *p = td->td_proc; 227111742Sdes struct tty *tp = dev->si_tty; 228111742Sdes struct pt_ioctl *pti = dev->si_drv1; 22994860Sjhb struct pgrp *pg; 2301541Srgrimes int error = 0; 2311541Srgrimes 2321541Srgrimesagain: 2331541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2341541Srgrimes while (isbackground(p, tp)) { 23594860Sjhb sx_slock(&proctree_lock); 23691140Stanimura PROC_LOCK(p); 23751791Smarcel if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 23851791Smarcel SIGISMEMBER(p->p_sigmask, SIGTTIN) || 23991140Stanimura p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) { 24091140Stanimura PROC_UNLOCK(p); 24194860Sjhb sx_sunlock(&proctree_lock); 2421541Srgrimes return (EIO); 24391140Stanimura } 24494860Sjhb pg = p->p_pgrp; 24591140Stanimura PROC_UNLOCK(p); 24694860Sjhb PGRP_LOCK(pg); 24794860Sjhb sx_sunlock(&proctree_lock); 24894860Sjhb pgsignal(pg, SIGTTIN, 1); 24994860Sjhb PGRP_UNLOCK(pg); 2509624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2519624Sbde 0); 2523308Sphk if (error) 2531541Srgrimes return (error); 2541541Srgrimes } 2551541Srgrimes if (tp->t_canq.c_cc == 0) { 2561541Srgrimes if (flag & IO_NDELAY) 2571541Srgrimes return (EWOULDBLOCK); 2589639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2599624Sbde "ptsin", 0); 2603308Sphk if (error) 2611541Srgrimes return (error); 2621541Srgrimes goto again; 2631541Srgrimes } 2641541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2651541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2661541Srgrimes error = EFAULT; 2671541Srgrimes break; 2681541Srgrimes } 2691541Srgrimes if (tp->t_canq.c_cc == 1) 2701541Srgrimes (void) getc(&tp->t_canq); 2711541Srgrimes if (tp->t_canq.c_cc) 2721541Srgrimes return (error); 2731541Srgrimes } else 2741541Srgrimes if (tp->t_oproc) 2751541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2761541Srgrimes ptcwakeup(tp, FWRITE); 2771541Srgrimes return (error); 2781541Srgrimes} 2791541Srgrimes 2801541Srgrimes/* 2811541Srgrimes * Write to pseudo-tty. 2821541Srgrimes * Wakeups of controlling tty will happen 2831541Srgrimes * indirectly, when tty driver calls ptsstart. 2841541Srgrimes */ 28512675Sjulianstatic int 2861541Srgrimesptswrite(dev, uio, flag) 2871541Srgrimes dev_t dev; 2881541Srgrimes struct uio *uio; 2891541Srgrimes int flag; 2901541Srgrimes{ 291111742Sdes struct tty *tp; 2921541Srgrimes 29350652Sphk tp = dev->si_tty; 2941541Srgrimes if (tp->t_oproc == 0) 2951541Srgrimes return (EIO); 2961541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2971541Srgrimes} 2981541Srgrimes 2991541Srgrimes/* 3001541Srgrimes * Start output on pseudo-tty. 3011541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3021541Srgrimes */ 30312819Sphkstatic void 3041541Srgrimesptsstart(tp) 3051541Srgrimes struct tty *tp; 3061541Srgrimes{ 307111742Sdes struct pt_ioctl *pti = tp->t_dev->si_drv1; 3081541Srgrimes 3091541Srgrimes if (tp->t_state & TS_TTSTOP) 3101541Srgrimes return; 3111541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3121541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3131541Srgrimes pti->pt_send = TIOCPKT_START; 3141541Srgrimes } 3151541Srgrimes ptcwakeup(tp, FREAD); 3161541Srgrimes} 3171541Srgrimes 31812819Sphkstatic void 3191541Srgrimesptcwakeup(tp, flag) 3201541Srgrimes struct tty *tp; 3211541Srgrimes int flag; 3221541Srgrimes{ 32349536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3241541Srgrimes 3251541Srgrimes if (flag & FREAD) { 3261541Srgrimes selwakeup(&pti->pt_selr); 3279639Sbde wakeup(TSA_PTC_READ(tp)); 3281541Srgrimes } 3291541Srgrimes if (flag & FWRITE) { 3301541Srgrimes selwakeup(&pti->pt_selw); 3319639Sbde wakeup(TSA_PTC_WRITE(tp)); 3321541Srgrimes } 3331541Srgrimes} 3341541Srgrimes 33512675Sjulianstatic int 33683366Sjulianptcopen(dev, flag, devtype, td) 3371541Srgrimes dev_t dev; 3381541Srgrimes int flag, devtype; 33983366Sjulian struct thread *td; 3401541Srgrimes{ 341111742Sdes struct tty *tp; 3421541Srgrimes struct pt_ioctl *pti; 3431541Srgrimes 34449536Sphk if (!dev->si_drv1) 34577176Sphk ptyinit(dev); 34649536Sphk if (!dev->si_drv1) 347111742Sdes return(ENXIO); 34850652Sphk tp = dev->si_tty; 3491541Srgrimes if (tp->t_oproc) 3501541Srgrimes return (EIO); 35159818Sache tp->t_timeout = -1; 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; 35791406Sjhb pti->pt_prison = td->td_ucred->cr_prison; 3581541Srgrimes pti->pt_flags = 0; 3591541Srgrimes pti->pt_send = 0; 3601541Srgrimes pti->pt_ucntl = 0; 3611541Srgrimes return (0); 3621541Srgrimes} 3631541Srgrimes 36412675Sjulianstatic int 36583366Sjulianptcclose(dev, flags, fmt, td) 3661541Srgrimes dev_t dev; 36710624Sbde int flags; 36810624Sbde int fmt; 36983366Sjulian struct thread *td; 3701541Srgrimes{ 371111742Sdes struct tty *tp; 3721541Srgrimes 37350652Sphk tp = dev->si_tty; 3741541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3759824Sbde 3769824Sbde /* 3779824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3789824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3799824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3809824Sbde * may be in use because other parts of the line discipline make 3819824Sbde * sense for ptys. Recover by doing everything that a normal 3829824Sbde * ttymodem() would have done except for sending a SIGHUP. 3839824Sbde */ 3849850Sbde if (tp->t_state & TS_ISOPEN) { 3859850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3869850Sbde tp->t_state |= TS_ZOMBIE; 3879850Sbde ttyflush(tp, FREAD | FWRITE); 3889850Sbde } 3899824Sbde 3901541Srgrimes tp->t_oproc = 0; /* mark closed */ 3911541Srgrimes return (0); 3921541Srgrimes} 3931541Srgrimes 39412675Sjulianstatic int 3951541Srgrimesptcread(dev, uio, flag) 3961541Srgrimes dev_t dev; 3971541Srgrimes struct uio *uio; 3981541Srgrimes int flag; 3991541Srgrimes{ 400111742Sdes struct tty *tp = dev->si_tty; 40149536Sphk struct pt_ioctl *pti = dev->si_drv1; 4021541Srgrimes char buf[BUFSIZ]; 4031541Srgrimes int error = 0, cc; 4041541Srgrimes 4051541Srgrimes /* 4061541Srgrimes * We want to block until the slave 4071541Srgrimes * is open, and there's something to read; 4081541Srgrimes * but if we lost the slave or we're NBIO, 4091541Srgrimes * then return the appropriate error instead. 4101541Srgrimes */ 4111541Srgrimes for (;;) { 4121541Srgrimes if (tp->t_state&TS_ISOPEN) { 4131541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4141541Srgrimes error = ureadc((int)pti->pt_send, uio); 4151541Srgrimes if (error) 4161541Srgrimes return (error); 4171541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4181541Srgrimes cc = min(uio->uio_resid, 4191541Srgrimes sizeof(tp->t_termios)); 420111741Sdes uiomove(&tp->t_termios, cc, 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) 457111742Sdes 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 48083366Sjulianptcpoll(dev, events, td) 4811541Srgrimes dev_t dev; 48229354Speter int events; 48383366Sjulian struct thread *td; 4841541Srgrimes{ 485111742Sdes 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) 49183366Sjulian return (seltrue(dev, events, td) | 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) ? 508111742Sdes (tp->t_canq.c_cc == 0) : 50929354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 51090831Sdillon (tp->t_canq.c_cc == 0 && (tp->t_lflag & 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)) 51983803Sjhb selrecord(td, &pti->pt_selr); 52029354Speter 521111742Sdes if (events & (POLLOUT | POLLWRNORM)) 52283803Sjhb selrecord(td, &pti->pt_selw); 5231541Srgrimes } 52429354Speter splx(s); 52529354Speter 52629354Speter return (revents); 5271541Srgrimes} 5281541Srgrimes 52912675Sjulianstatic int 5301541Srgrimesptcwrite(dev, uio, flag) 5311541Srgrimes dev_t dev; 532111742Sdes struct uio *uio; 5331541Srgrimes int flag; 5341541Srgrimes{ 535111742Sdes struct tty *tp = dev->si_tty; 536111742Sdes u_char *cp = 0; 537111742Sdes 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; 555111741Sdes error = uiomove(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; 590111741Sdes error = uiomove(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 && 60290831Sdillon (tp->t_canq.c_cc > 0 || !(tp->t_lflag&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 64183366Sjulianptyioctl(dev, cmd, data, flag, td) 6421541Srgrimes dev_t dev; 64336735Sdfr u_long cmd; 6441541Srgrimes caddr_t data; 6451541Srgrimes int flag; 64683366Sjulian struct thread *td; 6471541Srgrimes{ 648111742Sdes struct tty *tp = dev->si_tty; 649111742Sdes struct pt_ioctl *pti = dev->si_drv1; 650111742Sdes 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 /* 692111742Sdes * 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); 72191140Stanimura if (tp->t_pgrp != NULL) { 72291140Stanimura PGRP_LOCK(tp->t_pgrp); 72391140Stanimura pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 72491140Stanimura PGRP_UNLOCK(tp->t_pgrp); 72591140Stanimura } 7261541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7271541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7281541Srgrimes ttyinfo(tp); 7291541Srgrimes return(0); 7301541Srgrimes } 73147203Sluoqi } 73247301Sluoqi if (cmd == TIOCEXT) { 73347301Sluoqi /* 73447301Sluoqi * When the EXTPROC bit is being toggled, we need 73547301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 73647301Sluoqi * is turned on. 73747301Sluoqi */ 73847301Sluoqi if (*(int *)data) { 73947301Sluoqi if (pti->pt_flags & PF_PKT) { 74047301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74147301Sluoqi ptcwakeup(tp, FREAD); 74247301Sluoqi } 74347301Sluoqi tp->t_lflag |= EXTPROC; 74447301Sluoqi } else { 74547301Sluoqi if ((tp->t_lflag & EXTPROC) && 74647301Sluoqi (pti->pt_flags & PF_PKT)) { 74747301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74847301Sluoqi ptcwakeup(tp, FREAD); 74947301Sluoqi } 75047301Sluoqi tp->t_lflag &= ~EXTPROC; 75147301Sluoqi } 75247301Sluoqi return(0); 75347301Sluoqi } 75483366Sjulian error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 75531577Sbde if (error == ENOIOCTL) 7561541Srgrimes error = ttioctl(tp, cmd, data, flag); 75731577Sbde if (error == ENOIOCTL) { 7581541Srgrimes if (pti->pt_flags & PF_UCNTL && 7591541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7601541Srgrimes if (cmd & 0xff) { 7611541Srgrimes pti->pt_ucntl = (u_char)cmd; 7621541Srgrimes ptcwakeup(tp, FREAD); 7631541Srgrimes } 7641541Srgrimes return (0); 7651541Srgrimes } 7661541Srgrimes error = ENOTTY; 7671541Srgrimes } 7681541Srgrimes /* 7691541Srgrimes * If external processing and packet mode send ioctl packet. 7701541Srgrimes */ 7711541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7721541Srgrimes switch(cmd) { 7731541Srgrimes case TIOCSETA: 7741541Srgrimes case TIOCSETAW: 7751541Srgrimes case TIOCSETAF: 7761541Srgrimes#ifdef COMPAT_43 7771541Srgrimes case TIOCSETP: 7781541Srgrimes case TIOCSETN: 7791541Srgrimes#endif 7801541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7811541Srgrimes case TIOCSETC: 7821541Srgrimes case TIOCSLTC: 7831541Srgrimes case TIOCLBIS: 7841541Srgrimes case TIOCLBIC: 7851541Srgrimes case TIOCLSET: 7861541Srgrimes#endif 7871541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7881541Srgrimes ptcwakeup(tp, FREAD); 7891541Srgrimes default: 7901541Srgrimes break; 7911541Srgrimes } 7921541Srgrimes } 7938876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7941541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7951541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7961541Srgrimes if (stop) { 7971541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7981541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 7991541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 8001541Srgrimes ptcwakeup(tp, FREAD); 8011541Srgrimes } 8021541Srgrimes } else { 8031541Srgrimes if (!stop) { 8041541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8051541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8061541Srgrimes pti->pt_flags |= PF_NOSTOP; 8071541Srgrimes ptcwakeup(tp, FREAD); 8081541Srgrimes } 8091541Srgrimes } 8101541Srgrimes return (error); 8111541Srgrimes} 81212517Sjulian 81312517Sjulian 81492723Salfredstatic void ptc_drvinit(void *unused); 81549536Sphk 81692723Salfredstatic void pty_clone(void *arg, char *name, int namelen, dev_t *dev); 81764880Sphk 81812675Sjulianstatic void 81964880Sphkpty_clone(arg, name, namelen, dev) 82064880Sphk void *arg; 82164880Sphk char *name; 82264880Sphk int namelen; 82364880Sphk dev_t *dev; 82464880Sphk{ 82564880Sphk int u; 82664880Sphk 82764880Sphk if (*dev != NODEV) 82864880Sphk return; 82964880Sphk if (bcmp(name, "pty", 3) != 0) 83064880Sphk return; 83164880Sphk if (name[5] != '\0') 83264880Sphk return; 83364880Sphk switch (name[3]) { 83464880Sphk case 'p': u = 0; break; 83564880Sphk case 'q': u = 32; break; 83664880Sphk case 'r': u = 64; break; 83764880Sphk case 's': u = 96; break; 83864880Sphk case 'P': u = 128; break; 83964880Sphk case 'Q': u = 160; break; 84064880Sphk case 'R': u = 192; break; 84164880Sphk case 'S': u = 224; break; 84264880Sphk default: return; 84364880Sphk } 84464880Sphk if (name[4] >= '0' && name[4] <= '9') 84564880Sphk u += name[4] - '0'; 84664880Sphk else if (name[4] >= 'a' && name[4] <= 'v') 84764880Sphk u += name[4] - 'a' + 10; 84864880Sphk else 84964880Sphk return; 85077176Sphk *dev = make_dev(&ptc_cdevsw, u, 85177176Sphk UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32); 85277176Sphk (*dev)->si_flags |= SI_CHEAPCLONE; 85364880Sphk return; 85464880Sphk} 85564880Sphk 85664880Sphkstatic void 85729506Sbdeptc_drvinit(unused) 85829506Sbde void *unused; 85912517Sjulian{ 860108363Sphk 86165374Sphk EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 86212517Sjulian} 86312517Sjulian 86412517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 865