pty.c revision 120513
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 341541Srgrimes */ 351541Srgrimes 36116182Sobrien#include <sys/cdefs.h> 37116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 120513 2003-09-27 12:44:06Z phk $"); 38116182Sobrien 391541Srgrimes/* 401541Srgrimes * Pseudo-teletype Driver 411541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw') 421541Srgrimes */ 4331778Seivind#include "opt_compat.h" 44111899Sdas#include "opt_tty.h" 451541Srgrimes#include <sys/param.h> 461541Srgrimes#include <sys/systm.h> 4791140Stanimura#include <sys/lock.h> 4891140Stanimura#include <sys/mutex.h> 4991140Stanimura#include <sys/sx.h> 5024207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 5124207Sbde#include <sys/ioctl_compat.h> 5224207Sbde#endif 531541Srgrimes#include <sys/proc.h> 541541Srgrimes#include <sys/tty.h> 551541Srgrimes#include <sys/conf.h> 5624131Sbde#include <sys/fcntl.h> 5729354Speter#include <sys/poll.h> 581541Srgrimes#include <sys/kernel.h> 591541Srgrimes#include <sys/vnode.h> 603308Sphk#include <sys/signalvar.h> 6149536Sphk#include <sys/malloc.h> 621541Srgrimes 6369774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 6412517Sjulian 6592723Salfredstatic void ptsstart(struct tty *tp); 6692723Salfredstatic void ptsstop(struct tty *tp, int rw); 6792723Salfredstatic void ptcwakeup(struct tty *tp, int flag); 6892723Salfredstatic dev_t ptyinit(dev_t cdev); 6911789Sbde 7012675Sjulianstatic d_open_t ptsopen; 7112675Sjulianstatic d_close_t ptsclose; 7212675Sjulianstatic d_read_t ptsread; 7312675Sjulianstatic d_write_t ptswrite; 7412675Sjulianstatic d_ioctl_t ptyioctl; 7512675Sjulianstatic d_open_t ptcopen; 7612675Sjulianstatic d_close_t ptcclose; 7712675Sjulianstatic d_read_t ptcread; 7812675Sjulianstatic d_write_t ptcwrite; 7929354Speterstatic d_poll_t ptcpoll; 8012675Sjulian 8138485Sbde#define CDEV_MAJOR_S 5 8247625Sphkstatic struct cdevsw pts_cdevsw = { 83111815Sphk .d_open = ptsopen, 84111815Sphk .d_close = ptsclose, 85111815Sphk .d_read = ptsread, 86111815Sphk .d_write = ptswrite, 87111815Sphk .d_ioctl = ptyioctl, 88111815Sphk .d_poll = ttypoll, 89111815Sphk .d_name = "pts", 90111815Sphk .d_maj = CDEV_MAJOR_S, 91111821Sphk .d_flags = D_TTY, 92111815Sphk .d_kqfilter = ttykqfilter, 9338485Sbde}; 9412675Sjulian 9538485Sbde#define CDEV_MAJOR_C 6 9647625Sphkstatic struct cdevsw ptc_cdevsw = { 97111815Sphk .d_open = ptcopen, 98111815Sphk .d_close = ptcclose, 99111815Sphk .d_read = ptcread, 100111815Sphk .d_write = ptcwrite, 101111815Sphk .d_ioctl = ptyioctl, 102111815Sphk .d_poll = ptcpoll, 103111815Sphk .d_name = "ptc", 104111815Sphk .d_maj = CDEV_MAJOR_C, 105111821Sphk .d_flags = D_TTY, 106111815Sphk .d_kqfilter = ttykqfilter, 10738485Sbde}; 10812675Sjulian 1091541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1101541Srgrimes 11149536Sphkstruct pt_ioctl { 1121541Srgrimes int pt_flags; 1131541Srgrimes struct selinfo pt_selr, pt_selw; 1141541Srgrimes u_char pt_send; 1151541Srgrimes u_char pt_ucntl; 11649536Sphk struct tty pt_tty; 11750092Sjulian dev_t devs, devc; 11857070Srwatson struct prison *pt_prison; 11949536Sphk}; 1201541Srgrimes 1211541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1221541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1231541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1241541Srgrimes#define PF_NOSTOP 0x40 1251541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1261541Srgrimes 12777176Sphkstatic char *names = "pqrsPQRS"; 1281541Srgrimes/* 12949536Sphk * This function creates and initializes a pts/ptc pair 1301541Srgrimes * 13149536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13249536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13349536Sphk * 134111742Sdes * XXX: define and add mapping of upper minor bits to allow more 13549536Sphk * than 256 ptys. 1361541Srgrimes */ 13764880Sphkstatic dev_t 13877176Sphkptyinit(dev_t devc) 1391541Srgrimes{ 14077176Sphk dev_t devs; 14149536Sphk struct pt_ioctl *pt; 14277176Sphk int n; 1431541Srgrimes 14477176Sphk n = minor(devc); 14549536Sphk /* For now we only map the lower 8 bits of the minor */ 14649536Sphk if (n & ~0xff) 14764880Sphk return (NODEV); 14849536Sphk 14978405Sbrian devc->si_flags &= ~SI_CHEAPCLONE; 15078405Sbrian 151111119Simp pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO); 15250092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 15366067Sphk UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32); 15477176Sphk pt->devc = devc; 15549536Sphk 15649536Sphk devs->si_drv1 = devc->si_drv1 = pt; 15750652Sphk devs->si_tty = devc->si_tty = &pt->pt_tty; 15877360Sphk pt->pt_tty.t_dev = devs; 15949540Sphk ttyregister(&pt->pt_tty); 16064880Sphk return (devc); 16116322Sgpalmer} 1621541Srgrimes 1631541Srgrimes/*ARGSUSED*/ 16412675Sjulianstatic int 16583366Sjulianptsopen(dev, flag, devtype, td) 1661541Srgrimes dev_t dev; 1671541Srgrimes int flag, devtype; 16883366Sjulian struct thread *td; 1691541Srgrimes{ 170111742Sdes struct tty *tp; 1711541Srgrimes int error; 17257070Srwatson struct pt_ioctl *pti; 1731541Srgrimes 17449536Sphk if (!dev->si_drv1) 175111742Sdes return(ENXIO); 17657070Srwatson pti = dev->si_drv1; 17750652Sphk tp = dev->si_tty; 1781541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1791541Srgrimes ttychars(tp); /* Set up default chars */ 1801541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1811541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1821541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1831541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1841541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 18593593Sjhb } else if (tp->t_state & TS_XCLUDE && suser(td)) { 1861541Srgrimes return (EBUSY); 18791406Sjhb } else if (pti->pt_prison != td->td_ucred->cr_prison) { 18857070Srwatson return (EBUSY); 18957070Srwatson } 1901541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1919824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1921541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1931541Srgrimes if (flag&FNONBLOCK) 1941541Srgrimes break; 1959639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1969624Sbde "ptsopn", 0); 1973308Sphk if (error) 1981541Srgrimes return (error); 1991541Srgrimes } 2001541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 2017724Sache if (error == 0) 2027724Sache ptcwakeup(tp, FREAD|FWRITE); 2031541Srgrimes return (error); 2041541Srgrimes} 2051541Srgrimes 20612675Sjulianstatic int 20783366Sjulianptsclose(dev, flag, mode, td) 2081541Srgrimes dev_t dev; 2091541Srgrimes int flag, mode; 21083366Sjulian struct thread *td; 2111541Srgrimes{ 212111742Sdes struct tty *tp; 2131541Srgrimes int err; 2141541Srgrimes 21550652Sphk tp = dev->si_tty; 2161541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2177730Sache ptsstop(tp, FREAD|FWRITE); 2187724Sache (void) ttyclose(tp); 2191541Srgrimes return (err); 2201541Srgrimes} 2211541Srgrimes 22212675Sjulianstatic int 2231541Srgrimesptsread(dev, uio, flag) 2241541Srgrimes dev_t dev; 2251541Srgrimes struct uio *uio; 2261541Srgrimes int flag; 2271541Srgrimes{ 22883366Sjulian struct thread *td = curthread; 22983366Sjulian struct proc *p = td->td_proc; 230111742Sdes struct tty *tp = dev->si_tty; 231111742Sdes struct pt_ioctl *pti = dev->si_drv1; 23294860Sjhb struct pgrp *pg; 2331541Srgrimes int error = 0; 2341541Srgrimes 2351541Srgrimesagain: 2361541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2371541Srgrimes while (isbackground(p, tp)) { 23894860Sjhb sx_slock(&proctree_lock); 23991140Stanimura PROC_LOCK(p); 240114983Sjhb if (SIGISMEMBER(p->p_sigacts->ps_sigignore, SIGTTIN) || 241112888Sjeff SIGISMEMBER(td->td_sigmask, SIGTTIN) || 24291140Stanimura p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) { 24391140Stanimura PROC_UNLOCK(p); 24494860Sjhb sx_sunlock(&proctree_lock); 2451541Srgrimes return (EIO); 24691140Stanimura } 24794860Sjhb pg = p->p_pgrp; 24891140Stanimura PROC_UNLOCK(p); 24994860Sjhb PGRP_LOCK(pg); 25094860Sjhb sx_sunlock(&proctree_lock); 25194860Sjhb pgsignal(pg, SIGTTIN, 1); 25294860Sjhb PGRP_UNLOCK(pg); 2539624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2549624Sbde 0); 2553308Sphk if (error) 2561541Srgrimes return (error); 2571541Srgrimes } 2581541Srgrimes if (tp->t_canq.c_cc == 0) { 2591541Srgrimes if (flag & IO_NDELAY) 2601541Srgrimes return (EWOULDBLOCK); 2619639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2629624Sbde "ptsin", 0); 2633308Sphk if (error) 2641541Srgrimes return (error); 2651541Srgrimes goto again; 2661541Srgrimes } 2671541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2681541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2691541Srgrimes error = EFAULT; 2701541Srgrimes break; 2711541Srgrimes } 2721541Srgrimes if (tp->t_canq.c_cc == 1) 2731541Srgrimes (void) getc(&tp->t_canq); 2741541Srgrimes if (tp->t_canq.c_cc) 2751541Srgrimes return (error); 2761541Srgrimes } else 2771541Srgrimes if (tp->t_oproc) 2781541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2791541Srgrimes ptcwakeup(tp, FWRITE); 2801541Srgrimes return (error); 2811541Srgrimes} 2821541Srgrimes 2831541Srgrimes/* 2841541Srgrimes * Write to pseudo-tty. 2851541Srgrimes * Wakeups of controlling tty will happen 2861541Srgrimes * indirectly, when tty driver calls ptsstart. 2871541Srgrimes */ 28812675Sjulianstatic int 2891541Srgrimesptswrite(dev, uio, flag) 2901541Srgrimes dev_t dev; 2911541Srgrimes struct uio *uio; 2921541Srgrimes int flag; 2931541Srgrimes{ 294111742Sdes struct tty *tp; 2951541Srgrimes 29650652Sphk tp = dev->si_tty; 2971541Srgrimes if (tp->t_oproc == 0) 2981541Srgrimes return (EIO); 2991541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 3001541Srgrimes} 3011541Srgrimes 3021541Srgrimes/* 3031541Srgrimes * Start output on pseudo-tty. 3041541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3051541Srgrimes */ 30612819Sphkstatic void 3071541Srgrimesptsstart(tp) 3081541Srgrimes struct tty *tp; 3091541Srgrimes{ 310111742Sdes struct pt_ioctl *pti = tp->t_dev->si_drv1; 3111541Srgrimes 3121541Srgrimes if (tp->t_state & TS_TTSTOP) 3131541Srgrimes return; 3141541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3151541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3161541Srgrimes pti->pt_send = TIOCPKT_START; 3171541Srgrimes } 3181541Srgrimes ptcwakeup(tp, FREAD); 3191541Srgrimes} 3201541Srgrimes 32112819Sphkstatic void 3221541Srgrimesptcwakeup(tp, flag) 3231541Srgrimes struct tty *tp; 3241541Srgrimes int flag; 3251541Srgrimes{ 32649536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3271541Srgrimes 3281541Srgrimes if (flag & FREAD) { 3291541Srgrimes selwakeup(&pti->pt_selr); 3309639Sbde wakeup(TSA_PTC_READ(tp)); 3311541Srgrimes } 3321541Srgrimes if (flag & FWRITE) { 3331541Srgrimes selwakeup(&pti->pt_selw); 3349639Sbde wakeup(TSA_PTC_WRITE(tp)); 3351541Srgrimes } 3361541Srgrimes} 3371541Srgrimes 33812675Sjulianstatic int 33983366Sjulianptcopen(dev, flag, devtype, td) 3401541Srgrimes dev_t dev; 3411541Srgrimes int flag, devtype; 34283366Sjulian struct thread *td; 3431541Srgrimes{ 344111742Sdes struct tty *tp; 3451541Srgrimes struct pt_ioctl *pti; 3461541Srgrimes 34749536Sphk if (!dev->si_drv1) 34877176Sphk ptyinit(dev); 34949536Sphk if (!dev->si_drv1) 350111742Sdes return(ENXIO); 35150652Sphk tp = dev->si_tty; 3521541Srgrimes if (tp->t_oproc) 3531541Srgrimes return (EIO); 35459818Sache tp->t_timeout = -1; 3551541Srgrimes tp->t_oproc = ptsstart; 35651654Sphk tp->t_stop = ptsstop; 3571541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3581541Srgrimes tp->t_lflag &= ~EXTPROC; 35949536Sphk pti = dev->si_drv1; 36091406Sjhb pti->pt_prison = td->td_ucred->cr_prison; 3611541Srgrimes pti->pt_flags = 0; 3621541Srgrimes pti->pt_send = 0; 3631541Srgrimes pti->pt_ucntl = 0; 3641541Srgrimes return (0); 3651541Srgrimes} 3661541Srgrimes 36712675Sjulianstatic int 36883366Sjulianptcclose(dev, flags, fmt, td) 3691541Srgrimes dev_t dev; 37010624Sbde int flags; 37110624Sbde int fmt; 37283366Sjulian struct thread *td; 3731541Srgrimes{ 374111742Sdes struct tty *tp; 3751541Srgrimes 37650652Sphk tp = dev->si_tty; 3771541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3789824Sbde 3799824Sbde /* 3809824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3819824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3829824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3839824Sbde * may be in use because other parts of the line discipline make 3849824Sbde * sense for ptys. Recover by doing everything that a normal 3859824Sbde * ttymodem() would have done except for sending a SIGHUP. 3869824Sbde */ 3879850Sbde if (tp->t_state & TS_ISOPEN) { 3889850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3899850Sbde tp->t_state |= TS_ZOMBIE; 3909850Sbde ttyflush(tp, FREAD | FWRITE); 3919850Sbde } 3929824Sbde 3931541Srgrimes tp->t_oproc = 0; /* mark closed */ 3941541Srgrimes return (0); 3951541Srgrimes} 3961541Srgrimes 39712675Sjulianstatic int 3981541Srgrimesptcread(dev, uio, flag) 3991541Srgrimes dev_t dev; 4001541Srgrimes struct uio *uio; 4011541Srgrimes int flag; 4021541Srgrimes{ 403111742Sdes struct tty *tp = dev->si_tty; 40449536Sphk struct pt_ioctl *pti = dev->si_drv1; 4051541Srgrimes char buf[BUFSIZ]; 4061541Srgrimes int error = 0, cc; 4071541Srgrimes 4081541Srgrimes /* 4091541Srgrimes * We want to block until the slave 4101541Srgrimes * is open, and there's something to read; 4111541Srgrimes * but if we lost the slave or we're NBIO, 4121541Srgrimes * then return the appropriate error instead. 4131541Srgrimes */ 4141541Srgrimes for (;;) { 4151541Srgrimes if (tp->t_state&TS_ISOPEN) { 4161541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4171541Srgrimes error = ureadc((int)pti->pt_send, uio); 4181541Srgrimes if (error) 4191541Srgrimes return (error); 4201541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4211541Srgrimes cc = min(uio->uio_resid, 4221541Srgrimes sizeof(tp->t_termios)); 423111741Sdes uiomove(&tp->t_termios, cc, uio); 4241541Srgrimes } 4251541Srgrimes pti->pt_send = 0; 4261541Srgrimes return (0); 4271541Srgrimes } 4281541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4291541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4301541Srgrimes if (error) 4311541Srgrimes return (error); 4321541Srgrimes pti->pt_ucntl = 0; 4331541Srgrimes return (0); 4341541Srgrimes } 4351541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4361541Srgrimes break; 4371541Srgrimes } 4389824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4391541Srgrimes return (0); /* EOF */ 4401541Srgrimes if (flag & IO_NDELAY) 4411541Srgrimes return (EWOULDBLOCK); 4429639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4433308Sphk if (error) 4441541Srgrimes return (error); 4451541Srgrimes } 4461541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4471541Srgrimes error = ureadc(0, uio); 4481541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4491541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4501541Srgrimes if (cc <= 0) 4511541Srgrimes break; 4521541Srgrimes error = uiomove(buf, cc, uio); 4531541Srgrimes } 4549626Sbde ttwwakeup(tp); 4551541Srgrimes return (error); 4561541Srgrimes} 4571541Srgrimes 45812675Sjulianstatic void 4591541Srgrimesptsstop(tp, flush) 460111742Sdes struct tty *tp; 4611541Srgrimes int flush; 4621541Srgrimes{ 46349536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 4641541Srgrimes int flag; 4651541Srgrimes 4661541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4671541Srgrimes if (flush == 0) { 4681541Srgrimes flush = TIOCPKT_STOP; 4691541Srgrimes pti->pt_flags |= PF_STOPPED; 4701541Srgrimes } else 4711541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4721541Srgrimes pti->pt_send |= flush; 4731541Srgrimes /* change of perspective */ 4741541Srgrimes flag = 0; 4751541Srgrimes if (flush & FREAD) 4761541Srgrimes flag |= FWRITE; 4771541Srgrimes if (flush & FWRITE) 4781541Srgrimes flag |= FREAD; 4791541Srgrimes ptcwakeup(tp, flag); 4801541Srgrimes} 4811541Srgrimes 48212675Sjulianstatic int 48383366Sjulianptcpoll(dev, events, td) 4841541Srgrimes dev_t dev; 48529354Speter int events; 48683366Sjulian struct thread *td; 4871541Srgrimes{ 488111742Sdes struct tty *tp = dev->si_tty; 48949536Sphk struct pt_ioctl *pti = dev->si_drv1; 49029354Speter int revents = 0; 4911541Srgrimes int s; 4921541Srgrimes 4939824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 494120513Sphk return (events & 495120513Sphk (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM)); 4961541Srgrimes 49729354Speter /* 49829354Speter * Need to block timeouts (ttrstart). 49929354Speter */ 50029354Speter s = spltty(); 5011541Srgrimes 50229354Speter if (events & (POLLIN | POLLRDNORM)) 50329354Speter if ((tp->t_state & TS_ISOPEN) && 50429354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 50529354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 50629354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 50729354Speter revents |= events & (POLLIN | POLLRDNORM); 5081541Srgrimes 50929354Speter if (events & (POLLOUT | POLLWRNORM)) 51029354Speter if (tp->t_state & TS_ISOPEN && 51129354Speter ((pti->pt_flags & PF_REMOTE) ? 512111742Sdes (tp->t_canq.c_cc == 0) : 51329354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 51490831Sdillon (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) 51529354Speter revents |= events & (POLLOUT | POLLWRNORM); 5161541Srgrimes 51729354Speter if (events & POLLHUP) 51829354Speter if ((tp->t_state & TS_CARR_ON) == 0) 51929354Speter revents |= POLLHUP; 5201541Srgrimes 52129354Speter if (revents == 0) { 52229354Speter if (events & (POLLIN | POLLRDNORM)) 52383803Sjhb selrecord(td, &pti->pt_selr); 52429354Speter 525111742Sdes if (events & (POLLOUT | POLLWRNORM)) 52683803Sjhb selrecord(td, &pti->pt_selw); 5271541Srgrimes } 52829354Speter splx(s); 52929354Speter 53029354Speter return (revents); 5311541Srgrimes} 5321541Srgrimes 53312675Sjulianstatic int 5341541Srgrimesptcwrite(dev, uio, flag) 5351541Srgrimes dev_t dev; 536111742Sdes struct uio *uio; 5371541Srgrimes int flag; 5381541Srgrimes{ 539111742Sdes struct tty *tp = dev->si_tty; 540111742Sdes u_char *cp = 0; 541111742Sdes int cc = 0; 5421541Srgrimes u_char locbuf[BUFSIZ]; 5431541Srgrimes int cnt = 0; 54449536Sphk struct pt_ioctl *pti = dev->si_drv1; 5451541Srgrimes int error = 0; 5461541Srgrimes 5471541Srgrimesagain: 5481541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5491541Srgrimes goto block; 5501541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5511541Srgrimes if (tp->t_canq.c_cc) 5521541Srgrimes goto block; 55311789Sbde while ((uio->uio_resid > 0 || cc > 0) && 55411789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5551541Srgrimes if (cc == 0) { 5561541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5571541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5581541Srgrimes cp = locbuf; 559111741Sdes error = uiomove(cp, cc, uio); 5601541Srgrimes if (error) 5611541Srgrimes return (error); 5621541Srgrimes /* check again for safety */ 56311789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 56411789Sbde /* adjust as usual */ 56511789Sbde uio->uio_resid += cc; 5661541Srgrimes return (EIO); 56711789Sbde } 5681541Srgrimes } 56915199Sbde if (cc > 0) { 57015199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 57115199Sbde /* 57215199Sbde * XXX we don't guarantee that the canq size 57315199Sbde * is >= TTYHOG, so the above b_to_q() may 57415199Sbde * leave some bytes uncopied. However, space 57515199Sbde * is guaranteed for the null terminator if 57615199Sbde * we don't fail here since (TTYHOG - 1) is 57715199Sbde * not a multiple of CBSIZE. 57815199Sbde */ 57915199Sbde if (cc > 0) 58015199Sbde break; 58115199Sbde } 5821541Srgrimes } 58311789Sbde /* adjust for data copied in but not written */ 58411789Sbde uio->uio_resid += cc; 5851541Srgrimes (void) putc(0, &tp->t_canq); 5861541Srgrimes ttwakeup(tp); 5879639Sbde wakeup(TSA_PTS_READ(tp)); 5881541Srgrimes return (0); 5891541Srgrimes } 59011789Sbde while (uio->uio_resid > 0 || cc > 0) { 5911541Srgrimes if (cc == 0) { 5921541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5931541Srgrimes cp = locbuf; 594111741Sdes error = uiomove(cp, cc, uio); 5951541Srgrimes if (error) 5961541Srgrimes return (error); 5971541Srgrimes /* check again for safety */ 59811789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 59911789Sbde /* adjust for data copied in but not written */ 60011789Sbde uio->uio_resid += cc; 6011541Srgrimes return (EIO); 60211789Sbde } 6031541Srgrimes } 6041541Srgrimes while (cc > 0) { 6051541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 60690831Sdillon (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { 6079824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 6081541Srgrimes goto block; 6091541Srgrimes } 6101541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 6111541Srgrimes cnt++; 6121541Srgrimes cc--; 6131541Srgrimes } 6141541Srgrimes cc = 0; 6151541Srgrimes } 6161541Srgrimes return (0); 6171541Srgrimesblock: 6181541Srgrimes /* 6191541Srgrimes * Come here to wait for slave to open, for space 62015199Sbde * in outq, or space in rawq, or an empty canq. 6211541Srgrimes */ 62211789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 62311789Sbde /* adjust for data copied in but not written */ 62411789Sbde uio->uio_resid += cc; 6251541Srgrimes return (EIO); 62611789Sbde } 6271541Srgrimes if (flag & IO_NDELAY) { 6281541Srgrimes /* adjust for data copied in but not written */ 6291541Srgrimes uio->uio_resid += cc; 6301541Srgrimes if (cnt == 0) 6311541Srgrimes return (EWOULDBLOCK); 6321541Srgrimes return (0); 6331541Srgrimes } 6349639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6353308Sphk if (error) { 6361541Srgrimes /* adjust for data copied in but not written */ 6371541Srgrimes uio->uio_resid += cc; 6381541Srgrimes return (error); 6391541Srgrimes } 6401541Srgrimes goto again; 6411541Srgrimes} 6421541Srgrimes 6431541Srgrimes/*ARGSUSED*/ 64412675Sjulianstatic int 64583366Sjulianptyioctl(dev, cmd, data, flag, td) 6461541Srgrimes dev_t dev; 64736735Sdfr u_long cmd; 6481541Srgrimes caddr_t data; 6491541Srgrimes int flag; 65083366Sjulian struct thread *td; 6511541Srgrimes{ 652111742Sdes struct tty *tp = dev->si_tty; 653111742Sdes struct pt_ioctl *pti = dev->si_drv1; 654111742Sdes u_char *cc = tp->t_cc; 6551541Srgrimes int stop, error; 6561541Srgrimes 65747301Sluoqi if (devsw(dev)->d_open == ptcopen) { 6581541Srgrimes switch (cmd) { 6591541Srgrimes 6601541Srgrimes case TIOCGPGRP: 6611541Srgrimes /* 66233826Sbde * We avoid calling ttioctl on the controller since, 6631541Srgrimes * in that case, tp must be the controlling terminal. 6641541Srgrimes */ 6651541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6661541Srgrimes return (0); 6671541Srgrimes 6681541Srgrimes case TIOCPKT: 6691541Srgrimes if (*(int *)data) { 6701541Srgrimes if (pti->pt_flags & PF_UCNTL) 6711541Srgrimes return (EINVAL); 6721541Srgrimes pti->pt_flags |= PF_PKT; 6731541Srgrimes } else 6741541Srgrimes pti->pt_flags &= ~PF_PKT; 6751541Srgrimes return (0); 6761541Srgrimes 6771541Srgrimes case TIOCUCNTL: 6781541Srgrimes if (*(int *)data) { 6791541Srgrimes if (pti->pt_flags & PF_PKT) 6801541Srgrimes return (EINVAL); 6811541Srgrimes pti->pt_flags |= PF_UCNTL; 6821541Srgrimes } else 6831541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6841541Srgrimes return (0); 6851541Srgrimes 6861541Srgrimes case TIOCREMOTE: 6871541Srgrimes if (*(int *)data) 6881541Srgrimes pti->pt_flags |= PF_REMOTE; 6891541Srgrimes else 6901541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6911541Srgrimes ttyflush(tp, FREAD|FWRITE); 6921541Srgrimes return (0); 69347203Sluoqi } 6941541Srgrimes 69547203Sluoqi /* 696111742Sdes * The rest of the ioctls shouldn't be called until 69747301Sluoqi * the slave is open. 69847203Sluoqi */ 69947203Sluoqi if ((tp->t_state & TS_ISOPEN) == 0) 70047301Sluoqi return (EAGAIN); 70147203Sluoqi 70247203Sluoqi switch (cmd) { 7031541Srgrimes#ifdef COMPAT_43 7048876Srgrimes case TIOCSETP: 7051541Srgrimes case TIOCSETN: 7061541Srgrimes#endif 7071541Srgrimes case TIOCSETD: 7081541Srgrimes case TIOCSETA: 7091541Srgrimes case TIOCSETAW: 7109858Sache case TIOCSETAF: 71147301Sluoqi /* 71247301Sluoqi * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 71347301Sluoqi * ttywflush(tp) will hang if there are characters in 71447301Sluoqi * the outq. 71547301Sluoqi */ 7161541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7171541Srgrimes break; 7181541Srgrimes 7191541Srgrimes case TIOCSIG: 72027770Sjmg if (*(unsigned int *)data >= NSIG || 72127770Sjmg *(unsigned int *)data == 0) 7221541Srgrimes return(EINVAL); 7231541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7241541Srgrimes ttyflush(tp, FREAD|FWRITE); 72591140Stanimura if (tp->t_pgrp != NULL) { 72691140Stanimura PGRP_LOCK(tp->t_pgrp); 72791140Stanimura pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 72891140Stanimura PGRP_UNLOCK(tp->t_pgrp); 72991140Stanimura } 7301541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7311541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7321541Srgrimes ttyinfo(tp); 7331541Srgrimes return(0); 7341541Srgrimes } 73547203Sluoqi } 73647301Sluoqi if (cmd == TIOCEXT) { 73747301Sluoqi /* 73847301Sluoqi * When the EXTPROC bit is being toggled, we need 73947301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 74047301Sluoqi * is turned on. 74147301Sluoqi */ 74247301Sluoqi if (*(int *)data) { 74347301Sluoqi if (pti->pt_flags & PF_PKT) { 74447301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74547301Sluoqi ptcwakeup(tp, FREAD); 74647301Sluoqi } 74747301Sluoqi tp->t_lflag |= EXTPROC; 74847301Sluoqi } else { 74947301Sluoqi if ((tp->t_lflag & EXTPROC) && 75047301Sluoqi (pti->pt_flags & PF_PKT)) { 75147301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 75247301Sluoqi ptcwakeup(tp, FREAD); 75347301Sluoqi } 75447301Sluoqi tp->t_lflag &= ~EXTPROC; 75547301Sluoqi } 75647301Sluoqi return(0); 75747301Sluoqi } 75883366Sjulian error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 75931577Sbde if (error == ENOIOCTL) 7601541Srgrimes error = ttioctl(tp, cmd, data, flag); 76131577Sbde if (error == ENOIOCTL) { 7621541Srgrimes if (pti->pt_flags & PF_UCNTL && 7631541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7641541Srgrimes if (cmd & 0xff) { 7651541Srgrimes pti->pt_ucntl = (u_char)cmd; 7661541Srgrimes ptcwakeup(tp, FREAD); 7671541Srgrimes } 7681541Srgrimes return (0); 7691541Srgrimes } 7701541Srgrimes error = ENOTTY; 7711541Srgrimes } 7721541Srgrimes /* 7731541Srgrimes * If external processing and packet mode send ioctl packet. 7741541Srgrimes */ 7751541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7761541Srgrimes switch(cmd) { 7771541Srgrimes case TIOCSETA: 7781541Srgrimes case TIOCSETAW: 7791541Srgrimes case TIOCSETAF: 7801541Srgrimes#ifdef COMPAT_43 7811541Srgrimes case TIOCSETP: 7821541Srgrimes case TIOCSETN: 7831541Srgrimes#endif 7841541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7851541Srgrimes case TIOCSETC: 7861541Srgrimes case TIOCSLTC: 7871541Srgrimes case TIOCLBIS: 7881541Srgrimes case TIOCLBIC: 7891541Srgrimes case TIOCLSET: 7901541Srgrimes#endif 7911541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7921541Srgrimes ptcwakeup(tp, FREAD); 793115463Sphk break; 7941541Srgrimes default: 7951541Srgrimes break; 7961541Srgrimes } 7971541Srgrimes } 7988876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7991541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 8001541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 8011541Srgrimes if (stop) { 8021541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 8031541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 8041541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 8051541Srgrimes ptcwakeup(tp, FREAD); 8061541Srgrimes } 8071541Srgrimes } else { 8081541Srgrimes if (!stop) { 8091541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8101541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8111541Srgrimes pti->pt_flags |= PF_NOSTOP; 8121541Srgrimes ptcwakeup(tp, FREAD); 8131541Srgrimes } 8141541Srgrimes } 8151541Srgrimes return (error); 8161541Srgrimes} 81712517Sjulian 81812517Sjulian 81992723Salfredstatic void ptc_drvinit(void *unused); 82049536Sphk 82192723Salfredstatic void pty_clone(void *arg, char *name, int namelen, dev_t *dev); 82264880Sphk 82312675Sjulianstatic void 82464880Sphkpty_clone(arg, name, namelen, dev) 82564880Sphk void *arg; 82664880Sphk char *name; 82764880Sphk int namelen; 82864880Sphk dev_t *dev; 82964880Sphk{ 83064880Sphk int u; 83164880Sphk 83264880Sphk if (*dev != NODEV) 83364880Sphk return; 83464880Sphk if (bcmp(name, "pty", 3) != 0) 83564880Sphk return; 83664880Sphk if (name[5] != '\0') 83764880Sphk return; 83864880Sphk switch (name[3]) { 83964880Sphk case 'p': u = 0; break; 84064880Sphk case 'q': u = 32; break; 84164880Sphk case 'r': u = 64; break; 84264880Sphk case 's': u = 96; break; 84364880Sphk case 'P': u = 128; break; 84464880Sphk case 'Q': u = 160; break; 84564880Sphk case 'R': u = 192; break; 84664880Sphk case 'S': u = 224; break; 84764880Sphk default: return; 84864880Sphk } 84964880Sphk if (name[4] >= '0' && name[4] <= '9') 85064880Sphk u += name[4] - '0'; 85164880Sphk else if (name[4] >= 'a' && name[4] <= 'v') 85264880Sphk u += name[4] - 'a' + 10; 85364880Sphk else 85464880Sphk return; 85577176Sphk *dev = make_dev(&ptc_cdevsw, u, 85677176Sphk UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32); 85777176Sphk (*dev)->si_flags |= SI_CHEAPCLONE; 85864880Sphk return; 85964880Sphk} 86064880Sphk 86164880Sphkstatic void 86229506Sbdeptc_drvinit(unused) 86329506Sbde void *unused; 86412517Sjulian{ 865108363Sphk 86665374Sphk EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 86712517Sjulian} 86812517Sjulian 86912517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 870