pty.c revision 111899
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 111899 2003-03-05 08:16:29Z das $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes/* 381541Srgrimes * Pseudo-teletype Driver 391541Srgrimes * (Actually two drivers, requiring two entries in 'cdevsw') 401541Srgrimes */ 4131778Seivind#include "opt_compat.h" 42111899Sdas#include "opt_tty.h" 431541Srgrimes#include <sys/param.h> 441541Srgrimes#include <sys/systm.h> 4591140Stanimura#include <sys/lock.h> 4691140Stanimura#include <sys/mutex.h> 4791140Stanimura#include <sys/sx.h> 4824207Sbde#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 4924207Sbde#include <sys/ioctl_compat.h> 5024207Sbde#endif 511541Srgrimes#include <sys/proc.h> 521541Srgrimes#include <sys/tty.h> 531541Srgrimes#include <sys/conf.h> 5424131Sbde#include <sys/fcntl.h> 5529354Speter#include <sys/poll.h> 561541Srgrimes#include <sys/kernel.h> 571541Srgrimes#include <sys/vnode.h> 583308Sphk#include <sys/signalvar.h> 5949536Sphk#include <sys/malloc.h> 601541Srgrimes 6169774Sphkstatic MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 6212517Sjulian 6392723Salfredstatic void ptsstart(struct tty *tp); 6492723Salfredstatic void ptsstop(struct tty *tp, int rw); 6592723Salfredstatic void ptcwakeup(struct tty *tp, int flag); 6692723Salfredstatic dev_t ptyinit(dev_t cdev); 6711789Sbde 6812675Sjulianstatic d_open_t ptsopen; 6912675Sjulianstatic d_close_t ptsclose; 7012675Sjulianstatic d_read_t ptsread; 7112675Sjulianstatic d_write_t ptswrite; 7212675Sjulianstatic d_ioctl_t ptyioctl; 7312675Sjulianstatic d_open_t ptcopen; 7412675Sjulianstatic d_close_t ptcclose; 7512675Sjulianstatic d_read_t ptcread; 7612675Sjulianstatic d_write_t ptcwrite; 7729354Speterstatic d_poll_t ptcpoll; 7812675Sjulian 7938485Sbde#define CDEV_MAJOR_S 5 8047625Sphkstatic struct cdevsw pts_cdevsw = { 81111815Sphk .d_open = ptsopen, 82111815Sphk .d_close = ptsclose, 83111815Sphk .d_read = ptsread, 84111815Sphk .d_write = ptswrite, 85111815Sphk .d_ioctl = ptyioctl, 86111815Sphk .d_poll = ttypoll, 87111815Sphk .d_name = "pts", 88111815Sphk .d_maj = CDEV_MAJOR_S, 89111821Sphk .d_flags = D_TTY, 90111815Sphk .d_kqfilter = ttykqfilter, 9138485Sbde}; 9212675Sjulian 9338485Sbde#define CDEV_MAJOR_C 6 9447625Sphkstatic struct cdevsw ptc_cdevsw = { 95111815Sphk .d_open = ptcopen, 96111815Sphk .d_close = ptcclose, 97111815Sphk .d_read = ptcread, 98111815Sphk .d_write = ptcwrite, 99111815Sphk .d_ioctl = ptyioctl, 100111815Sphk .d_poll = ptcpoll, 101111815Sphk .d_name = "ptc", 102111815Sphk .d_maj = CDEV_MAJOR_C, 103111821Sphk .d_flags = D_TTY, 104111815Sphk .d_kqfilter = ttykqfilter, 10538485Sbde}; 10612675Sjulian 1071541Srgrimes#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 1081541Srgrimes 10949536Sphkstruct pt_ioctl { 1101541Srgrimes int pt_flags; 1111541Srgrimes struct selinfo pt_selr, pt_selw; 1121541Srgrimes u_char pt_send; 1131541Srgrimes u_char pt_ucntl; 11449536Sphk struct tty pt_tty; 11550092Sjulian dev_t devs, devc; 11657070Srwatson struct prison *pt_prison; 11749536Sphk}; 1181541Srgrimes 1191541Srgrimes#define PF_PKT 0x08 /* packet mode */ 1201541Srgrimes#define PF_STOPPED 0x10 /* user told stopped */ 1211541Srgrimes#define PF_REMOTE 0x20 /* remote and flow controlled input */ 1221541Srgrimes#define PF_NOSTOP 0x40 1231541Srgrimes#define PF_UCNTL 0x80 /* user control mode */ 1241541Srgrimes 12577176Sphkstatic char *names = "pqrsPQRS"; 1261541Srgrimes/* 12749536Sphk * This function creates and initializes a pts/ptc pair 1281541Srgrimes * 12949536Sphk * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13049536Sphk * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 13149536Sphk * 132111742Sdes * XXX: define and add mapping of upper minor bits to allow more 13349536Sphk * than 256 ptys. 1341541Srgrimes */ 13564880Sphkstatic dev_t 13677176Sphkptyinit(dev_t devc) 1371541Srgrimes{ 13877176Sphk dev_t devs; 13949536Sphk struct pt_ioctl *pt; 14077176Sphk int n; 1411541Srgrimes 14277176Sphk n = minor(devc); 14349536Sphk /* For now we only map the lower 8 bits of the minor */ 14449536Sphk if (n & ~0xff) 14564880Sphk return (NODEV); 14649536Sphk 14778405Sbrian devc->si_flags &= ~SI_CHEAPCLONE; 14878405Sbrian 149111119Simp pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO); 15050092Sjulian pt->devs = devs = make_dev(&pts_cdevsw, n, 15166067Sphk UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32); 15277176Sphk pt->devc = devc; 15349536Sphk 15449536Sphk devs->si_drv1 = devc->si_drv1 = pt; 15550652Sphk devs->si_tty = devc->si_tty = &pt->pt_tty; 15677360Sphk pt->pt_tty.t_dev = devs; 15749540Sphk ttyregister(&pt->pt_tty); 15864880Sphk return (devc); 15916322Sgpalmer} 1601541Srgrimes 1611541Srgrimes/*ARGSUSED*/ 16212675Sjulianstatic int 16383366Sjulianptsopen(dev, flag, devtype, td) 1641541Srgrimes dev_t dev; 1651541Srgrimes int flag, devtype; 16683366Sjulian struct thread *td; 1671541Srgrimes{ 168111742Sdes struct tty *tp; 1691541Srgrimes int error; 17057070Srwatson struct pt_ioctl *pti; 1711541Srgrimes 17249536Sphk if (!dev->si_drv1) 173111742Sdes return(ENXIO); 17457070Srwatson pti = dev->si_drv1; 17550652Sphk tp = dev->si_tty; 1761541Srgrimes if ((tp->t_state & TS_ISOPEN) == 0) { 1771541Srgrimes ttychars(tp); /* Set up default chars */ 1781541Srgrimes tp->t_iflag = TTYDEF_IFLAG; 1791541Srgrimes tp->t_oflag = TTYDEF_OFLAG; 1801541Srgrimes tp->t_lflag = TTYDEF_LFLAG; 1811541Srgrimes tp->t_cflag = TTYDEF_CFLAG; 1821541Srgrimes tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; 18393593Sjhb } else if (tp->t_state & TS_XCLUDE && suser(td)) { 1841541Srgrimes return (EBUSY); 18591406Sjhb } else if (pti->pt_prison != td->td_ucred->cr_prison) { 18657070Srwatson return (EBUSY); 18757070Srwatson } 1881541Srgrimes if (tp->t_oproc) /* Ctrlr still around. */ 1899824Sbde (void)(*linesw[tp->t_line].l_modem)(tp, 1); 1901541Srgrimes while ((tp->t_state & TS_CARR_ON) == 0) { 1911541Srgrimes if (flag&FNONBLOCK) 1921541Srgrimes break; 1939639Sbde error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 1949624Sbde "ptsopn", 0); 1953308Sphk if (error) 1961541Srgrimes return (error); 1971541Srgrimes } 1981541Srgrimes error = (*linesw[tp->t_line].l_open)(dev, tp); 1997724Sache if (error == 0) 2007724Sache ptcwakeup(tp, FREAD|FWRITE); 2011541Srgrimes return (error); 2021541Srgrimes} 2031541Srgrimes 20412675Sjulianstatic int 20583366Sjulianptsclose(dev, flag, mode, td) 2061541Srgrimes dev_t dev; 2071541Srgrimes int flag, mode; 20883366Sjulian struct thread *td; 2091541Srgrimes{ 210111742Sdes struct tty *tp; 2111541Srgrimes int err; 2121541Srgrimes 21350652Sphk tp = dev->si_tty; 2141541Srgrimes err = (*linesw[tp->t_line].l_close)(tp, flag); 2157730Sache ptsstop(tp, FREAD|FWRITE); 2167724Sache (void) ttyclose(tp); 2171541Srgrimes return (err); 2181541Srgrimes} 2191541Srgrimes 22012675Sjulianstatic int 2211541Srgrimesptsread(dev, uio, flag) 2221541Srgrimes dev_t dev; 2231541Srgrimes struct uio *uio; 2241541Srgrimes int flag; 2251541Srgrimes{ 22683366Sjulian struct thread *td = curthread; 22783366Sjulian struct proc *p = td->td_proc; 228111742Sdes struct tty *tp = dev->si_tty; 229111742Sdes struct pt_ioctl *pti = dev->si_drv1; 23094860Sjhb struct pgrp *pg; 2311541Srgrimes int error = 0; 2321541Srgrimes 2331541Srgrimesagain: 2341541Srgrimes if (pti->pt_flags & PF_REMOTE) { 2351541Srgrimes while (isbackground(p, tp)) { 23694860Sjhb sx_slock(&proctree_lock); 23791140Stanimura PROC_LOCK(p); 23851791Smarcel if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 23951791Smarcel SIGISMEMBER(p->p_sigmask, SIGTTIN) || 24091140Stanimura p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) { 24191140Stanimura PROC_UNLOCK(p); 24294860Sjhb sx_sunlock(&proctree_lock); 2431541Srgrimes return (EIO); 24491140Stanimura } 24594860Sjhb pg = p->p_pgrp; 24691140Stanimura PROC_UNLOCK(p); 24794860Sjhb PGRP_LOCK(pg); 24894860Sjhb sx_sunlock(&proctree_lock); 24994860Sjhb pgsignal(pg, SIGTTIN, 1); 25094860Sjhb PGRP_UNLOCK(pg); 2519624Sbde error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 2529624Sbde 0); 2533308Sphk if (error) 2541541Srgrimes return (error); 2551541Srgrimes } 2561541Srgrimes if (tp->t_canq.c_cc == 0) { 2571541Srgrimes if (flag & IO_NDELAY) 2581541Srgrimes return (EWOULDBLOCK); 2599639Sbde error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 2609624Sbde "ptsin", 0); 2613308Sphk if (error) 2621541Srgrimes return (error); 2631541Srgrimes goto again; 2641541Srgrimes } 2651541Srgrimes while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 2661541Srgrimes if (ureadc(getc(&tp->t_canq), uio) < 0) { 2671541Srgrimes error = EFAULT; 2681541Srgrimes break; 2691541Srgrimes } 2701541Srgrimes if (tp->t_canq.c_cc == 1) 2711541Srgrimes (void) getc(&tp->t_canq); 2721541Srgrimes if (tp->t_canq.c_cc) 2731541Srgrimes return (error); 2741541Srgrimes } else 2751541Srgrimes if (tp->t_oproc) 2761541Srgrimes error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 2771541Srgrimes ptcwakeup(tp, FWRITE); 2781541Srgrimes return (error); 2791541Srgrimes} 2801541Srgrimes 2811541Srgrimes/* 2821541Srgrimes * Write to pseudo-tty. 2831541Srgrimes * Wakeups of controlling tty will happen 2841541Srgrimes * indirectly, when tty driver calls ptsstart. 2851541Srgrimes */ 28612675Sjulianstatic int 2871541Srgrimesptswrite(dev, uio, flag) 2881541Srgrimes dev_t dev; 2891541Srgrimes struct uio *uio; 2901541Srgrimes int flag; 2911541Srgrimes{ 292111742Sdes struct tty *tp; 2931541Srgrimes 29450652Sphk tp = dev->si_tty; 2951541Srgrimes if (tp->t_oproc == 0) 2961541Srgrimes return (EIO); 2971541Srgrimes return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 2981541Srgrimes} 2991541Srgrimes 3001541Srgrimes/* 3011541Srgrimes * Start output on pseudo-tty. 3021541Srgrimes * Wake up process selecting or sleeping for input from controlling tty. 3031541Srgrimes */ 30412819Sphkstatic void 3051541Srgrimesptsstart(tp) 3061541Srgrimes struct tty *tp; 3071541Srgrimes{ 308111742Sdes struct pt_ioctl *pti = tp->t_dev->si_drv1; 3091541Srgrimes 3101541Srgrimes if (tp->t_state & TS_TTSTOP) 3111541Srgrimes return; 3121541Srgrimes if (pti->pt_flags & PF_STOPPED) { 3131541Srgrimes pti->pt_flags &= ~PF_STOPPED; 3141541Srgrimes pti->pt_send = TIOCPKT_START; 3151541Srgrimes } 3161541Srgrimes ptcwakeup(tp, FREAD); 3171541Srgrimes} 3181541Srgrimes 31912819Sphkstatic void 3201541Srgrimesptcwakeup(tp, flag) 3211541Srgrimes struct tty *tp; 3221541Srgrimes int flag; 3231541Srgrimes{ 32449536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 3251541Srgrimes 3261541Srgrimes if (flag & FREAD) { 3271541Srgrimes selwakeup(&pti->pt_selr); 3289639Sbde wakeup(TSA_PTC_READ(tp)); 3291541Srgrimes } 3301541Srgrimes if (flag & FWRITE) { 3311541Srgrimes selwakeup(&pti->pt_selw); 3329639Sbde wakeup(TSA_PTC_WRITE(tp)); 3331541Srgrimes } 3341541Srgrimes} 3351541Srgrimes 33612675Sjulianstatic int 33783366Sjulianptcopen(dev, flag, devtype, td) 3381541Srgrimes dev_t dev; 3391541Srgrimes int flag, devtype; 34083366Sjulian struct thread *td; 3411541Srgrimes{ 342111742Sdes struct tty *tp; 3431541Srgrimes struct pt_ioctl *pti; 3441541Srgrimes 34549536Sphk if (!dev->si_drv1) 34677176Sphk ptyinit(dev); 34749536Sphk if (!dev->si_drv1) 348111742Sdes return(ENXIO); 34950652Sphk tp = dev->si_tty; 3501541Srgrimes if (tp->t_oproc) 3511541Srgrimes return (EIO); 35259818Sache tp->t_timeout = -1; 3531541Srgrimes tp->t_oproc = ptsstart; 35451654Sphk tp->t_stop = ptsstop; 3551541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 1); 3561541Srgrimes tp->t_lflag &= ~EXTPROC; 35749536Sphk pti = dev->si_drv1; 35891406Sjhb pti->pt_prison = td->td_ucred->cr_prison; 3591541Srgrimes pti->pt_flags = 0; 3601541Srgrimes pti->pt_send = 0; 3611541Srgrimes pti->pt_ucntl = 0; 3621541Srgrimes return (0); 3631541Srgrimes} 3641541Srgrimes 36512675Sjulianstatic int 36683366Sjulianptcclose(dev, flags, fmt, td) 3671541Srgrimes dev_t dev; 36810624Sbde int flags; 36910624Sbde int fmt; 37083366Sjulian struct thread *td; 3711541Srgrimes{ 372111742Sdes struct tty *tp; 3731541Srgrimes 37450652Sphk tp = dev->si_tty; 3751541Srgrimes (void)(*linesw[tp->t_line].l_modem)(tp, 0); 3769824Sbde 3779824Sbde /* 3789824Sbde * XXX MDMBUF makes no sense for ptys but would inhibit the above 3799824Sbde * l_modem(). CLOCAL makes sense but isn't supported. Special 3809824Sbde * l_modem()s that ignore carrier drop make no sense for ptys but 3819824Sbde * may be in use because other parts of the line discipline make 3829824Sbde * sense for ptys. Recover by doing everything that a normal 3839824Sbde * ttymodem() would have done except for sending a SIGHUP. 3849824Sbde */ 3859850Sbde if (tp->t_state & TS_ISOPEN) { 3869850Sbde tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 3879850Sbde tp->t_state |= TS_ZOMBIE; 3889850Sbde ttyflush(tp, FREAD | FWRITE); 3899850Sbde } 3909824Sbde 3911541Srgrimes tp->t_oproc = 0; /* mark closed */ 3921541Srgrimes return (0); 3931541Srgrimes} 3941541Srgrimes 39512675Sjulianstatic int 3961541Srgrimesptcread(dev, uio, flag) 3971541Srgrimes dev_t dev; 3981541Srgrimes struct uio *uio; 3991541Srgrimes int flag; 4001541Srgrimes{ 401111742Sdes struct tty *tp = dev->si_tty; 40249536Sphk struct pt_ioctl *pti = dev->si_drv1; 4031541Srgrimes char buf[BUFSIZ]; 4041541Srgrimes int error = 0, cc; 4051541Srgrimes 4061541Srgrimes /* 4071541Srgrimes * We want to block until the slave 4081541Srgrimes * is open, and there's something to read; 4091541Srgrimes * but if we lost the slave or we're NBIO, 4101541Srgrimes * then return the appropriate error instead. 4111541Srgrimes */ 4121541Srgrimes for (;;) { 4131541Srgrimes if (tp->t_state&TS_ISOPEN) { 4141541Srgrimes if (pti->pt_flags&PF_PKT && pti->pt_send) { 4151541Srgrimes error = ureadc((int)pti->pt_send, uio); 4161541Srgrimes if (error) 4171541Srgrimes return (error); 4181541Srgrimes if (pti->pt_send & TIOCPKT_IOCTL) { 4191541Srgrimes cc = min(uio->uio_resid, 4201541Srgrimes sizeof(tp->t_termios)); 421111741Sdes uiomove(&tp->t_termios, cc, uio); 4221541Srgrimes } 4231541Srgrimes pti->pt_send = 0; 4241541Srgrimes return (0); 4251541Srgrimes } 4261541Srgrimes if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 4271541Srgrimes error = ureadc((int)pti->pt_ucntl, uio); 4281541Srgrimes if (error) 4291541Srgrimes return (error); 4301541Srgrimes pti->pt_ucntl = 0; 4311541Srgrimes return (0); 4321541Srgrimes } 4331541Srgrimes if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 4341541Srgrimes break; 4351541Srgrimes } 4369824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 4371541Srgrimes return (0); /* EOF */ 4381541Srgrimes if (flag & IO_NDELAY) 4391541Srgrimes return (EWOULDBLOCK); 4409639Sbde error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 4413308Sphk if (error) 4421541Srgrimes return (error); 4431541Srgrimes } 4441541Srgrimes if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 4451541Srgrimes error = ureadc(0, uio); 4461541Srgrimes while (uio->uio_resid > 0 && error == 0) { 4471541Srgrimes cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 4481541Srgrimes if (cc <= 0) 4491541Srgrimes break; 4501541Srgrimes error = uiomove(buf, cc, uio); 4511541Srgrimes } 4529626Sbde ttwwakeup(tp); 4531541Srgrimes return (error); 4541541Srgrimes} 4551541Srgrimes 45612675Sjulianstatic void 4571541Srgrimesptsstop(tp, flush) 458111742Sdes struct tty *tp; 4591541Srgrimes int flush; 4601541Srgrimes{ 46149536Sphk struct pt_ioctl *pti = tp->t_dev->si_drv1; 4621541Srgrimes int flag; 4631541Srgrimes 4641541Srgrimes /* note: FLUSHREAD and FLUSHWRITE already ok */ 4651541Srgrimes if (flush == 0) { 4661541Srgrimes flush = TIOCPKT_STOP; 4671541Srgrimes pti->pt_flags |= PF_STOPPED; 4681541Srgrimes } else 4691541Srgrimes pti->pt_flags &= ~PF_STOPPED; 4701541Srgrimes pti->pt_send |= flush; 4711541Srgrimes /* change of perspective */ 4721541Srgrimes flag = 0; 4731541Srgrimes if (flush & FREAD) 4741541Srgrimes flag |= FWRITE; 4751541Srgrimes if (flush & FWRITE) 4761541Srgrimes flag |= FREAD; 4771541Srgrimes ptcwakeup(tp, flag); 4781541Srgrimes} 4791541Srgrimes 48012675Sjulianstatic int 48183366Sjulianptcpoll(dev, events, td) 4821541Srgrimes dev_t dev; 48329354Speter int events; 48483366Sjulian struct thread *td; 4851541Srgrimes{ 486111742Sdes struct tty *tp = dev->si_tty; 48749536Sphk struct pt_ioctl *pti = dev->si_drv1; 48829354Speter int revents = 0; 4891541Srgrimes int s; 4901541Srgrimes 4919824Sbde if ((tp->t_state & TS_CONNECTED) == 0) 49283366Sjulian return (seltrue(dev, events, td) | POLLHUP); 4931541Srgrimes 49429354Speter /* 49529354Speter * Need to block timeouts (ttrstart). 49629354Speter */ 49729354Speter s = spltty(); 4981541Srgrimes 49929354Speter if (events & (POLLIN | POLLRDNORM)) 50029354Speter if ((tp->t_state & TS_ISOPEN) && 50129354Speter ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 50229354Speter ((pti->pt_flags & PF_PKT) && pti->pt_send) || 50329354Speter ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 50429354Speter revents |= events & (POLLIN | POLLRDNORM); 5051541Srgrimes 50629354Speter if (events & (POLLOUT | POLLWRNORM)) 50729354Speter if (tp->t_state & TS_ISOPEN && 50829354Speter ((pti->pt_flags & PF_REMOTE) ? 509111742Sdes (tp->t_canq.c_cc == 0) : 51029354Speter ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 51190831Sdillon (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) 51229354Speter revents |= events & (POLLOUT | POLLWRNORM); 5131541Srgrimes 51429354Speter if (events & POLLHUP) 51529354Speter if ((tp->t_state & TS_CARR_ON) == 0) 51629354Speter revents |= POLLHUP; 5171541Srgrimes 51829354Speter if (revents == 0) { 51929354Speter if (events & (POLLIN | POLLRDNORM)) 52083803Sjhb selrecord(td, &pti->pt_selr); 52129354Speter 522111742Sdes if (events & (POLLOUT | POLLWRNORM)) 52383803Sjhb selrecord(td, &pti->pt_selw); 5241541Srgrimes } 52529354Speter splx(s); 52629354Speter 52729354Speter return (revents); 5281541Srgrimes} 5291541Srgrimes 53012675Sjulianstatic int 5311541Srgrimesptcwrite(dev, uio, flag) 5321541Srgrimes dev_t dev; 533111742Sdes struct uio *uio; 5341541Srgrimes int flag; 5351541Srgrimes{ 536111742Sdes struct tty *tp = dev->si_tty; 537111742Sdes u_char *cp = 0; 538111742Sdes int cc = 0; 5391541Srgrimes u_char locbuf[BUFSIZ]; 5401541Srgrimes int cnt = 0; 54149536Sphk struct pt_ioctl *pti = dev->si_drv1; 5421541Srgrimes int error = 0; 5431541Srgrimes 5441541Srgrimesagain: 5451541Srgrimes if ((tp->t_state&TS_ISOPEN) == 0) 5461541Srgrimes goto block; 5471541Srgrimes if (pti->pt_flags & PF_REMOTE) { 5481541Srgrimes if (tp->t_canq.c_cc) 5491541Srgrimes goto block; 55011789Sbde while ((uio->uio_resid > 0 || cc > 0) && 55111789Sbde tp->t_canq.c_cc < TTYHOG - 1) { 5521541Srgrimes if (cc == 0) { 5531541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5541541Srgrimes cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 5551541Srgrimes cp = locbuf; 556111741Sdes error = uiomove(cp, cc, uio); 5571541Srgrimes if (error) 5581541Srgrimes return (error); 5591541Srgrimes /* check again for safety */ 56011789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 56111789Sbde /* adjust as usual */ 56211789Sbde uio->uio_resid += cc; 5631541Srgrimes return (EIO); 56411789Sbde } 5651541Srgrimes } 56615199Sbde if (cc > 0) { 56715199Sbde cc = b_to_q((char *)cp, cc, &tp->t_canq); 56815199Sbde /* 56915199Sbde * XXX we don't guarantee that the canq size 57015199Sbde * is >= TTYHOG, so the above b_to_q() may 57115199Sbde * leave some bytes uncopied. However, space 57215199Sbde * is guaranteed for the null terminator if 57315199Sbde * we don't fail here since (TTYHOG - 1) is 57415199Sbde * not a multiple of CBSIZE. 57515199Sbde */ 57615199Sbde if (cc > 0) 57715199Sbde break; 57815199Sbde } 5791541Srgrimes } 58011789Sbde /* adjust for data copied in but not written */ 58111789Sbde uio->uio_resid += cc; 5821541Srgrimes (void) putc(0, &tp->t_canq); 5831541Srgrimes ttwakeup(tp); 5849639Sbde wakeup(TSA_PTS_READ(tp)); 5851541Srgrimes return (0); 5861541Srgrimes } 58711789Sbde while (uio->uio_resid > 0 || cc > 0) { 5881541Srgrimes if (cc == 0) { 5891541Srgrimes cc = min(uio->uio_resid, BUFSIZ); 5901541Srgrimes cp = locbuf; 591111741Sdes error = uiomove(cp, cc, uio); 5921541Srgrimes if (error) 5931541Srgrimes return (error); 5941541Srgrimes /* check again for safety */ 59511789Sbde if ((tp->t_state & TS_ISOPEN) == 0) { 59611789Sbde /* adjust for data copied in but not written */ 59711789Sbde uio->uio_resid += cc; 5981541Srgrimes return (EIO); 59911789Sbde } 6001541Srgrimes } 6011541Srgrimes while (cc > 0) { 6021541Srgrimes if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 60390831Sdillon (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { 6049824Sbde wakeup(TSA_HUP_OR_INPUT(tp)); 6051541Srgrimes goto block; 6061541Srgrimes } 6071541Srgrimes (*linesw[tp->t_line].l_rint)(*cp++, tp); 6081541Srgrimes cnt++; 6091541Srgrimes cc--; 6101541Srgrimes } 6111541Srgrimes cc = 0; 6121541Srgrimes } 6131541Srgrimes return (0); 6141541Srgrimesblock: 6151541Srgrimes /* 6161541Srgrimes * Come here to wait for slave to open, for space 61715199Sbde * in outq, or space in rawq, or an empty canq. 6181541Srgrimes */ 61911789Sbde if ((tp->t_state & TS_CONNECTED) == 0) { 62011789Sbde /* adjust for data copied in but not written */ 62111789Sbde uio->uio_resid += cc; 6221541Srgrimes return (EIO); 62311789Sbde } 6241541Srgrimes if (flag & IO_NDELAY) { 6251541Srgrimes /* adjust for data copied in but not written */ 6261541Srgrimes uio->uio_resid += cc; 6271541Srgrimes if (cnt == 0) 6281541Srgrimes return (EWOULDBLOCK); 6291541Srgrimes return (0); 6301541Srgrimes } 6319639Sbde error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 6323308Sphk if (error) { 6331541Srgrimes /* adjust for data copied in but not written */ 6341541Srgrimes uio->uio_resid += cc; 6351541Srgrimes return (error); 6361541Srgrimes } 6371541Srgrimes goto again; 6381541Srgrimes} 6391541Srgrimes 6401541Srgrimes/*ARGSUSED*/ 64112675Sjulianstatic int 64283366Sjulianptyioctl(dev, cmd, data, flag, td) 6431541Srgrimes dev_t dev; 64436735Sdfr u_long cmd; 6451541Srgrimes caddr_t data; 6461541Srgrimes int flag; 64783366Sjulian struct thread *td; 6481541Srgrimes{ 649111742Sdes struct tty *tp = dev->si_tty; 650111742Sdes struct pt_ioctl *pti = dev->si_drv1; 651111742Sdes u_char *cc = tp->t_cc; 6521541Srgrimes int stop, error; 6531541Srgrimes 65447301Sluoqi if (devsw(dev)->d_open == ptcopen) { 6551541Srgrimes switch (cmd) { 6561541Srgrimes 6571541Srgrimes case TIOCGPGRP: 6581541Srgrimes /* 65933826Sbde * We avoid calling ttioctl on the controller since, 6601541Srgrimes * in that case, tp must be the controlling terminal. 6611541Srgrimes */ 6621541Srgrimes *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 6631541Srgrimes return (0); 6641541Srgrimes 6651541Srgrimes case TIOCPKT: 6661541Srgrimes if (*(int *)data) { 6671541Srgrimes if (pti->pt_flags & PF_UCNTL) 6681541Srgrimes return (EINVAL); 6691541Srgrimes pti->pt_flags |= PF_PKT; 6701541Srgrimes } else 6711541Srgrimes pti->pt_flags &= ~PF_PKT; 6721541Srgrimes return (0); 6731541Srgrimes 6741541Srgrimes case TIOCUCNTL: 6751541Srgrimes if (*(int *)data) { 6761541Srgrimes if (pti->pt_flags & PF_PKT) 6771541Srgrimes return (EINVAL); 6781541Srgrimes pti->pt_flags |= PF_UCNTL; 6791541Srgrimes } else 6801541Srgrimes pti->pt_flags &= ~PF_UCNTL; 6811541Srgrimes return (0); 6821541Srgrimes 6831541Srgrimes case TIOCREMOTE: 6841541Srgrimes if (*(int *)data) 6851541Srgrimes pti->pt_flags |= PF_REMOTE; 6861541Srgrimes else 6871541Srgrimes pti->pt_flags &= ~PF_REMOTE; 6881541Srgrimes ttyflush(tp, FREAD|FWRITE); 6891541Srgrimes return (0); 69047203Sluoqi } 6911541Srgrimes 69247203Sluoqi /* 693111742Sdes * The rest of the ioctls shouldn't be called until 69447301Sluoqi * the slave is open. 69547203Sluoqi */ 69647203Sluoqi if ((tp->t_state & TS_ISOPEN) == 0) 69747301Sluoqi return (EAGAIN); 69847203Sluoqi 69947203Sluoqi switch (cmd) { 7001541Srgrimes#ifdef COMPAT_43 7018876Srgrimes case TIOCSETP: 7021541Srgrimes case TIOCSETN: 7031541Srgrimes#endif 7041541Srgrimes case TIOCSETD: 7051541Srgrimes case TIOCSETA: 7061541Srgrimes case TIOCSETAW: 7079858Sache case TIOCSETAF: 70847301Sluoqi /* 70947301Sluoqi * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 71047301Sluoqi * ttywflush(tp) will hang if there are characters in 71147301Sluoqi * the outq. 71247301Sluoqi */ 7131541Srgrimes ndflush(&tp->t_outq, tp->t_outq.c_cc); 7141541Srgrimes break; 7151541Srgrimes 7161541Srgrimes case TIOCSIG: 71727770Sjmg if (*(unsigned int *)data >= NSIG || 71827770Sjmg *(unsigned int *)data == 0) 7191541Srgrimes return(EINVAL); 7201541Srgrimes if ((tp->t_lflag&NOFLSH) == 0) 7211541Srgrimes ttyflush(tp, FREAD|FWRITE); 72291140Stanimura if (tp->t_pgrp != NULL) { 72391140Stanimura PGRP_LOCK(tp->t_pgrp); 72491140Stanimura pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 72591140Stanimura PGRP_UNLOCK(tp->t_pgrp); 72691140Stanimura } 7271541Srgrimes if ((*(unsigned int *)data == SIGINFO) && 7281541Srgrimes ((tp->t_lflag&NOKERNINFO) == 0)) 7291541Srgrimes ttyinfo(tp); 7301541Srgrimes return(0); 7311541Srgrimes } 73247203Sluoqi } 73347301Sluoqi if (cmd == TIOCEXT) { 73447301Sluoqi /* 73547301Sluoqi * When the EXTPROC bit is being toggled, we need 73647301Sluoqi * to send an TIOCPKT_IOCTL if the packet driver 73747301Sluoqi * is turned on. 73847301Sluoqi */ 73947301Sluoqi if (*(int *)data) { 74047301Sluoqi if (pti->pt_flags & PF_PKT) { 74147301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74247301Sluoqi ptcwakeup(tp, FREAD); 74347301Sluoqi } 74447301Sluoqi tp->t_lflag |= EXTPROC; 74547301Sluoqi } else { 74647301Sluoqi if ((tp->t_lflag & EXTPROC) && 74747301Sluoqi (pti->pt_flags & PF_PKT)) { 74847301Sluoqi pti->pt_send |= TIOCPKT_IOCTL; 74947301Sluoqi ptcwakeup(tp, FREAD); 75047301Sluoqi } 75147301Sluoqi tp->t_lflag &= ~EXTPROC; 75247301Sluoqi } 75347301Sluoqi return(0); 75447301Sluoqi } 75583366Sjulian error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 75631577Sbde if (error == ENOIOCTL) 7571541Srgrimes error = ttioctl(tp, cmd, data, flag); 75831577Sbde if (error == ENOIOCTL) { 7591541Srgrimes if (pti->pt_flags & PF_UCNTL && 7601541Srgrimes (cmd & ~0xff) == UIOCCMD(0)) { 7611541Srgrimes if (cmd & 0xff) { 7621541Srgrimes pti->pt_ucntl = (u_char)cmd; 7631541Srgrimes ptcwakeup(tp, FREAD); 7641541Srgrimes } 7651541Srgrimes return (0); 7661541Srgrimes } 7671541Srgrimes error = ENOTTY; 7681541Srgrimes } 7691541Srgrimes /* 7701541Srgrimes * If external processing and packet mode send ioctl packet. 7711541Srgrimes */ 7721541Srgrimes if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 7731541Srgrimes switch(cmd) { 7741541Srgrimes case TIOCSETA: 7751541Srgrimes case TIOCSETAW: 7761541Srgrimes case TIOCSETAF: 7771541Srgrimes#ifdef COMPAT_43 7781541Srgrimes case TIOCSETP: 7791541Srgrimes case TIOCSETN: 7801541Srgrimes#endif 7811541Srgrimes#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 7821541Srgrimes case TIOCSETC: 7831541Srgrimes case TIOCSLTC: 7841541Srgrimes case TIOCLBIS: 7851541Srgrimes case TIOCLBIC: 7861541Srgrimes case TIOCLSET: 7871541Srgrimes#endif 7881541Srgrimes pti->pt_send |= TIOCPKT_IOCTL; 7891541Srgrimes ptcwakeup(tp, FREAD); 7901541Srgrimes default: 7911541Srgrimes break; 7921541Srgrimes } 7931541Srgrimes } 7948876Srgrimes stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 7951541Srgrimes && CCEQ(cc[VSTART], CTRL('q')); 7961541Srgrimes if (pti->pt_flags & PF_NOSTOP) { 7971541Srgrimes if (stop) { 7981541Srgrimes pti->pt_send &= ~TIOCPKT_NOSTOP; 7991541Srgrimes pti->pt_send |= TIOCPKT_DOSTOP; 8001541Srgrimes pti->pt_flags &= ~PF_NOSTOP; 8011541Srgrimes ptcwakeup(tp, FREAD); 8021541Srgrimes } 8031541Srgrimes } else { 8041541Srgrimes if (!stop) { 8051541Srgrimes pti->pt_send &= ~TIOCPKT_DOSTOP; 8061541Srgrimes pti->pt_send |= TIOCPKT_NOSTOP; 8071541Srgrimes pti->pt_flags |= PF_NOSTOP; 8081541Srgrimes ptcwakeup(tp, FREAD); 8091541Srgrimes } 8101541Srgrimes } 8111541Srgrimes return (error); 8121541Srgrimes} 81312517Sjulian 81412517Sjulian 81592723Salfredstatic void ptc_drvinit(void *unused); 81649536Sphk 81792723Salfredstatic void pty_clone(void *arg, char *name, int namelen, dev_t *dev); 81864880Sphk 81912675Sjulianstatic void 82064880Sphkpty_clone(arg, name, namelen, dev) 82164880Sphk void *arg; 82264880Sphk char *name; 82364880Sphk int namelen; 82464880Sphk dev_t *dev; 82564880Sphk{ 82664880Sphk int u; 82764880Sphk 82864880Sphk if (*dev != NODEV) 82964880Sphk return; 83064880Sphk if (bcmp(name, "pty", 3) != 0) 83164880Sphk return; 83264880Sphk if (name[5] != '\0') 83364880Sphk return; 83464880Sphk switch (name[3]) { 83564880Sphk case 'p': u = 0; break; 83664880Sphk case 'q': u = 32; break; 83764880Sphk case 'r': u = 64; break; 83864880Sphk case 's': u = 96; break; 83964880Sphk case 'P': u = 128; break; 84064880Sphk case 'Q': u = 160; break; 84164880Sphk case 'R': u = 192; break; 84264880Sphk case 'S': u = 224; break; 84364880Sphk default: return; 84464880Sphk } 84564880Sphk if (name[4] >= '0' && name[4] <= '9') 84664880Sphk u += name[4] - '0'; 84764880Sphk else if (name[4] >= 'a' && name[4] <= 'v') 84864880Sphk u += name[4] - 'a' + 10; 84964880Sphk else 85064880Sphk return; 85177176Sphk *dev = make_dev(&ptc_cdevsw, u, 85277176Sphk UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32); 85377176Sphk (*dev)->si_flags |= SI_CHEAPCLONE; 85464880Sphk return; 85564880Sphk} 85664880Sphk 85764880Sphkstatic void 85829506Sbdeptc_drvinit(unused) 85929506Sbde void *unused; 86012517Sjulian{ 861108363Sphk 86265374Sphk EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000); 86312517Sjulian} 86412517Sjulian 86512517SjulianSYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL) 866