1/* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
| 1/* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
|
34 * $FreeBSD: head/sys/kern/tty_pty.c 53483 1999-11-21 02:54:54Z jkh $
| 34 * $FreeBSD: head/sys/kern/tty_pty.c 57070 2000-02-09 03:32:11Z rwatson $
|
35 */ 36 37/* 38 * Pseudo-teletype Driver 39 * (Actually two drivers, requiring two entries in 'cdevsw') 40 */ 41#include "pty.h" /* XXX */ 42#include "opt_compat.h" 43#include <sys/param.h> 44#include <sys/systm.h> 45#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 46#include <sys/ioctl_compat.h> 47#endif 48#include <sys/proc.h> 49#include <sys/tty.h> 50#include <sys/conf.h> 51#include <sys/fcntl.h> 52#include <sys/poll.h> 53#include <sys/kernel.h> 54#include <sys/vnode.h> 55#include <sys/signalvar.h> 56#include <sys/malloc.h> 57 58MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 59 60static void ptsstart __P((struct tty *tp)); 61static void ptsstop __P((struct tty *tp, int rw)); 62static void ptcwakeup __P((struct tty *tp, int flag)); 63static void ptyinit __P((int n)); 64 65static d_open_t ptsopen; 66static d_close_t ptsclose; 67static d_read_t ptsread; 68static d_write_t ptswrite; 69static d_ioctl_t ptyioctl; 70static d_open_t ptcopen; 71static d_close_t ptcclose; 72static d_read_t ptcread; 73static d_write_t ptcwrite; 74static d_poll_t ptcpoll; 75 76#define CDEV_MAJOR_S 5 77static struct cdevsw pts_cdevsw = { 78 /* open */ ptsopen, 79 /* close */ ptsclose, 80 /* read */ ptsread, 81 /* write */ ptswrite, 82 /* ioctl */ ptyioctl, 83 /* poll */ ttypoll, 84 /* mmap */ nommap, 85 /* strategy */ nostrategy, 86 /* name */ "pts", 87 /* maj */ CDEV_MAJOR_S, 88 /* dump */ nodump, 89 /* psize */ nopsize, 90 /* flags */ D_TTY, 91 /* bmaj */ -1 92}; 93 94#define CDEV_MAJOR_C 6 95static struct cdevsw ptc_cdevsw = { 96 /* open */ ptcopen, 97 /* close */ ptcclose, 98 /* read */ ptcread, 99 /* write */ ptcwrite, 100 /* ioctl */ ptyioctl, 101 /* poll */ ptcpoll, 102 /* mmap */ nommap, 103 /* strategy */ nostrategy, 104 /* name */ "ptc", 105 /* maj */ CDEV_MAJOR_C, 106 /* dump */ nodump, 107 /* psize */ nopsize, 108 /* flags */ D_TTY, 109 /* bmaj */ -1 110}; 111 112#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 113 114struct pt_ioctl { 115 int pt_flags; 116 struct selinfo pt_selr, pt_selw; 117 u_char pt_send; 118 u_char pt_ucntl; 119 struct tty pt_tty; 120 dev_t devs, devc;
| 35 */ 36 37/* 38 * Pseudo-teletype Driver 39 * (Actually two drivers, requiring two entries in 'cdevsw') 40 */ 41#include "pty.h" /* XXX */ 42#include "opt_compat.h" 43#include <sys/param.h> 44#include <sys/systm.h> 45#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 46#include <sys/ioctl_compat.h> 47#endif 48#include <sys/proc.h> 49#include <sys/tty.h> 50#include <sys/conf.h> 51#include <sys/fcntl.h> 52#include <sys/poll.h> 53#include <sys/kernel.h> 54#include <sys/vnode.h> 55#include <sys/signalvar.h> 56#include <sys/malloc.h> 57 58MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); 59 60static void ptsstart __P((struct tty *tp)); 61static void ptsstop __P((struct tty *tp, int rw)); 62static void ptcwakeup __P((struct tty *tp, int flag)); 63static void ptyinit __P((int n)); 64 65static d_open_t ptsopen; 66static d_close_t ptsclose; 67static d_read_t ptsread; 68static d_write_t ptswrite; 69static d_ioctl_t ptyioctl; 70static d_open_t ptcopen; 71static d_close_t ptcclose; 72static d_read_t ptcread; 73static d_write_t ptcwrite; 74static d_poll_t ptcpoll; 75 76#define CDEV_MAJOR_S 5 77static struct cdevsw pts_cdevsw = { 78 /* open */ ptsopen, 79 /* close */ ptsclose, 80 /* read */ ptsread, 81 /* write */ ptswrite, 82 /* ioctl */ ptyioctl, 83 /* poll */ ttypoll, 84 /* mmap */ nommap, 85 /* strategy */ nostrategy, 86 /* name */ "pts", 87 /* maj */ CDEV_MAJOR_S, 88 /* dump */ nodump, 89 /* psize */ nopsize, 90 /* flags */ D_TTY, 91 /* bmaj */ -1 92}; 93 94#define CDEV_MAJOR_C 6 95static struct cdevsw ptc_cdevsw = { 96 /* open */ ptcopen, 97 /* close */ ptcclose, 98 /* read */ ptcread, 99 /* write */ ptcwrite, 100 /* ioctl */ ptyioctl, 101 /* poll */ ptcpoll, 102 /* mmap */ nommap, 103 /* strategy */ nostrategy, 104 /* name */ "ptc", 105 /* maj */ CDEV_MAJOR_C, 106 /* dump */ nodump, 107 /* psize */ nopsize, 108 /* flags */ D_TTY, 109 /* bmaj */ -1 110}; 111 112#define BUFSIZ 100 /* Chunk size iomoved to/from user */ 113 114struct pt_ioctl { 115 int pt_flags; 116 struct selinfo pt_selr, pt_selw; 117 u_char pt_send; 118 u_char pt_ucntl; 119 struct tty pt_tty; 120 dev_t devs, devc;
|
| 121 struct prison *pt_prison;
|
121}; 122 123#define PF_PKT 0x08 /* packet mode */ 124#define PF_STOPPED 0x10 /* user told stopped */ 125#define PF_REMOTE 0x20 /* remote and flow controlled input */ 126#define PF_NOSTOP 0x40 127#define PF_UCNTL 0x80 /* user control mode */ 128 129/* 130 * This function creates and initializes a pts/ptc pair 131 * 132 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 133 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 134 * 135 * XXX: define and add mapping of upper minor bits to allow more 136 * than 256 ptys. 137 */ 138static void 139ptyinit(n) 140 int n; 141{ 142 dev_t devs, devc; 143 char *names = "pqrsPQRS"; 144 struct pt_ioctl *pt; 145 146 /* For now we only map the lower 8 bits of the minor */ 147 if (n & ~0xff) 148 return; 149 150 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK); 151 bzero(pt, sizeof(*pt)); 152 pt->devs = devs = make_dev(&pts_cdevsw, n, 153 0, 0, 0666, "tty%c%r", names[n / 32], n % 32); 154 pt->devc = devc = make_dev(&ptc_cdevsw, n, 155 0, 0, 0666, "pty%c%r", names[n / 32], n % 32); 156 157 devs->si_drv1 = devc->si_drv1 = pt; 158 devs->si_tty = devc->si_tty = &pt->pt_tty; 159 ttyregister(&pt->pt_tty); 160} 161 162/*ARGSUSED*/ 163static int 164ptsopen(dev, flag, devtype, p) 165 dev_t dev; 166 int flag, devtype; 167 struct proc *p; 168{ 169 register struct tty *tp; 170 int error; 171 int minr; 172 dev_t nextdev;
| 122}; 123 124#define PF_PKT 0x08 /* packet mode */ 125#define PF_STOPPED 0x10 /* user told stopped */ 126#define PF_REMOTE 0x20 /* remote and flow controlled input */ 127#define PF_NOSTOP 0x40 128#define PF_UCNTL 0x80 /* user control mode */ 129 130/* 131 * This function creates and initializes a pts/ptc pair 132 * 133 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 134 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv] 135 * 136 * XXX: define and add mapping of upper minor bits to allow more 137 * than 256 ptys. 138 */ 139static void 140ptyinit(n) 141 int n; 142{ 143 dev_t devs, devc; 144 char *names = "pqrsPQRS"; 145 struct pt_ioctl *pt; 146 147 /* For now we only map the lower 8 bits of the minor */ 148 if (n & ~0xff) 149 return; 150 151 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK); 152 bzero(pt, sizeof(*pt)); 153 pt->devs = devs = make_dev(&pts_cdevsw, n, 154 0, 0, 0666, "tty%c%r", names[n / 32], n % 32); 155 pt->devc = devc = make_dev(&ptc_cdevsw, n, 156 0, 0, 0666, "pty%c%r", names[n / 32], n % 32); 157 158 devs->si_drv1 = devc->si_drv1 = pt; 159 devs->si_tty = devc->si_tty = &pt->pt_tty; 160 ttyregister(&pt->pt_tty); 161} 162 163/*ARGSUSED*/ 164static int 165ptsopen(dev, flag, devtype, p) 166 dev_t dev; 167 int flag, devtype; 168 struct proc *p; 169{ 170 register struct tty *tp; 171 int error; 172 int minr; 173 dev_t nextdev;
|
| 174 struct pt_ioctl *pti;
|
173 174 /* 175 * XXX: Gross hack for DEVFS: 176 * If we openned this device, ensure we have the 177 * next one too, so people can open it. 178 */ 179 minr = lminor(dev); 180 if (minr < 255) { 181 nextdev = makedev(major(dev), minr + 1); 182 if (!nextdev->si_drv1) { 183 ptyinit(minr + 1); 184 } 185 } 186 if (!dev->si_drv1) 187 ptyinit(minor(dev)); 188 if (!dev->si_drv1) 189 return(ENXIO);
| 175 176 /* 177 * XXX: Gross hack for DEVFS: 178 * If we openned this device, ensure we have the 179 * next one too, so people can open it. 180 */ 181 minr = lminor(dev); 182 if (minr < 255) { 183 nextdev = makedev(major(dev), minr + 1); 184 if (!nextdev->si_drv1) { 185 ptyinit(minr + 1); 186 } 187 } 188 if (!dev->si_drv1) 189 ptyinit(minor(dev)); 190 if (!dev->si_drv1) 191 return(ENXIO);
|
| 192 pti = dev->si_drv1;
|
190 tp = dev->si_tty; 191 if ((tp->t_state & TS_ISOPEN) == 0) { 192 ttychars(tp); /* Set up default chars */ 193 tp->t_iflag = TTYDEF_IFLAG; 194 tp->t_oflag = TTYDEF_OFLAG; 195 tp->t_lflag = TTYDEF_LFLAG; 196 tp->t_cflag = TTYDEF_CFLAG; 197 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
| 193 tp = dev->si_tty; 194 if ((tp->t_state & TS_ISOPEN) == 0) { 195 ttychars(tp); /* Set up default chars */ 196 tp->t_iflag = TTYDEF_IFLAG; 197 tp->t_oflag = TTYDEF_OFLAG; 198 tp->t_lflag = TTYDEF_LFLAG; 199 tp->t_cflag = TTYDEF_CFLAG; 200 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
|
198 } else if (tp->t_state & TS_XCLUDE && suser(p))
| 201 } else if (tp->t_state & TS_XCLUDE && suser(p)) {
|
199 return (EBUSY);
| 202 return (EBUSY);
|
| 203 } else if (pti->pt_prison != p->p_prison) { 204 return (EBUSY); 205 }
|
200 if (tp->t_oproc) /* Ctrlr still around. */ 201 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 202 while ((tp->t_state & TS_CARR_ON) == 0) { 203 if (flag&FNONBLOCK) 204 break; 205 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 206 "ptsopn", 0); 207 if (error) 208 return (error); 209 } 210 error = (*linesw[tp->t_line].l_open)(dev, tp); 211 if (error == 0) 212 ptcwakeup(tp, FREAD|FWRITE); 213 return (error); 214} 215 216static int 217ptsclose(dev, flag, mode, p) 218 dev_t dev; 219 int flag, mode; 220 struct proc *p; 221{ 222 register struct tty *tp; 223 int err; 224 225 tp = dev->si_tty; 226 err = (*linesw[tp->t_line].l_close)(tp, flag); 227 ptsstop(tp, FREAD|FWRITE); 228 (void) ttyclose(tp); 229 return (err); 230} 231 232static int 233ptsread(dev, uio, flag) 234 dev_t dev; 235 struct uio *uio; 236 int flag; 237{ 238 struct proc *p = curproc; 239 register struct tty *tp = dev->si_tty; 240 register struct pt_ioctl *pti = dev->si_drv1; 241 int error = 0; 242 243again: 244 if (pti->pt_flags & PF_REMOTE) { 245 while (isbackground(p, tp)) { 246 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 247 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 248 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) 249 return (EIO); 250 pgsignal(p->p_pgrp, SIGTTIN, 1); 251 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 252 0); 253 if (error) 254 return (error); 255 } 256 if (tp->t_canq.c_cc == 0) { 257 if (flag & IO_NDELAY) 258 return (EWOULDBLOCK); 259 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 260 "ptsin", 0); 261 if (error) 262 return (error); 263 goto again; 264 } 265 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 266 if (ureadc(getc(&tp->t_canq), uio) < 0) { 267 error = EFAULT; 268 break; 269 } 270 if (tp->t_canq.c_cc == 1) 271 (void) getc(&tp->t_canq); 272 if (tp->t_canq.c_cc) 273 return (error); 274 } else 275 if (tp->t_oproc) 276 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 277 ptcwakeup(tp, FWRITE); 278 return (error); 279} 280 281/* 282 * Write to pseudo-tty. 283 * Wakeups of controlling tty will happen 284 * indirectly, when tty driver calls ptsstart. 285 */ 286static int 287ptswrite(dev, uio, flag) 288 dev_t dev; 289 struct uio *uio; 290 int flag; 291{ 292 register struct tty *tp; 293 294 tp = dev->si_tty; 295 if (tp->t_oproc == 0) 296 return (EIO); 297 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 298} 299 300/* 301 * Start output on pseudo-tty. 302 * Wake up process selecting or sleeping for input from controlling tty. 303 */ 304static void 305ptsstart(tp) 306 struct tty *tp; 307{ 308 register struct pt_ioctl *pti = tp->t_dev->si_drv1; 309 310 if (tp->t_state & TS_TTSTOP) 311 return; 312 if (pti->pt_flags & PF_STOPPED) { 313 pti->pt_flags &= ~PF_STOPPED; 314 pti->pt_send = TIOCPKT_START; 315 } 316 ptcwakeup(tp, FREAD); 317} 318 319static void 320ptcwakeup(tp, flag) 321 struct tty *tp; 322 int flag; 323{ 324 struct pt_ioctl *pti = tp->t_dev->si_drv1; 325 326 if (flag & FREAD) { 327 selwakeup(&pti->pt_selr); 328 wakeup(TSA_PTC_READ(tp)); 329 } 330 if (flag & FWRITE) { 331 selwakeup(&pti->pt_selw); 332 wakeup(TSA_PTC_WRITE(tp)); 333 } 334} 335 336static int 337ptcopen(dev, flag, devtype, p) 338 dev_t dev; 339 int flag, devtype; 340 struct proc *p; 341{ 342 register struct tty *tp; 343 struct pt_ioctl *pti; 344 345 if (!dev->si_drv1) 346 ptyinit(minor(dev)); 347 if (!dev->si_drv1) 348 return(ENXIO); 349 tp = dev->si_tty; 350 if (tp->t_oproc) 351 return (EIO); 352 tp->t_oproc = ptsstart; 353 tp->t_stop = ptsstop; 354 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 355 tp->t_lflag &= ~EXTPROC; 356 pti = dev->si_drv1;
| 206 if (tp->t_oproc) /* Ctrlr still around. */ 207 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 208 while ((tp->t_state & TS_CARR_ON) == 0) { 209 if (flag&FNONBLOCK) 210 break; 211 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, 212 "ptsopn", 0); 213 if (error) 214 return (error); 215 } 216 error = (*linesw[tp->t_line].l_open)(dev, tp); 217 if (error == 0) 218 ptcwakeup(tp, FREAD|FWRITE); 219 return (error); 220} 221 222static int 223ptsclose(dev, flag, mode, p) 224 dev_t dev; 225 int flag, mode; 226 struct proc *p; 227{ 228 register struct tty *tp; 229 int err; 230 231 tp = dev->si_tty; 232 err = (*linesw[tp->t_line].l_close)(tp, flag); 233 ptsstop(tp, FREAD|FWRITE); 234 (void) ttyclose(tp); 235 return (err); 236} 237 238static int 239ptsread(dev, uio, flag) 240 dev_t dev; 241 struct uio *uio; 242 int flag; 243{ 244 struct proc *p = curproc; 245 register struct tty *tp = dev->si_tty; 246 register struct pt_ioctl *pti = dev->si_drv1; 247 int error = 0; 248 249again: 250 if (pti->pt_flags & PF_REMOTE) { 251 while (isbackground(p, tp)) { 252 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || 253 SIGISMEMBER(p->p_sigmask, SIGTTIN) || 254 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) 255 return (EIO); 256 pgsignal(p->p_pgrp, SIGTTIN, 1); 257 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg", 258 0); 259 if (error) 260 return (error); 261 } 262 if (tp->t_canq.c_cc == 0) { 263 if (flag & IO_NDELAY) 264 return (EWOULDBLOCK); 265 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH, 266 "ptsin", 0); 267 if (error) 268 return (error); 269 goto again; 270 } 271 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) 272 if (ureadc(getc(&tp->t_canq), uio) < 0) { 273 error = EFAULT; 274 break; 275 } 276 if (tp->t_canq.c_cc == 1) 277 (void) getc(&tp->t_canq); 278 if (tp->t_canq.c_cc) 279 return (error); 280 } else 281 if (tp->t_oproc) 282 error = (*linesw[tp->t_line].l_read)(tp, uio, flag); 283 ptcwakeup(tp, FWRITE); 284 return (error); 285} 286 287/* 288 * Write to pseudo-tty. 289 * Wakeups of controlling tty will happen 290 * indirectly, when tty driver calls ptsstart. 291 */ 292static int 293ptswrite(dev, uio, flag) 294 dev_t dev; 295 struct uio *uio; 296 int flag; 297{ 298 register struct tty *tp; 299 300 tp = dev->si_tty; 301 if (tp->t_oproc == 0) 302 return (EIO); 303 return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); 304} 305 306/* 307 * Start output on pseudo-tty. 308 * Wake up process selecting or sleeping for input from controlling tty. 309 */ 310static void 311ptsstart(tp) 312 struct tty *tp; 313{ 314 register struct pt_ioctl *pti = tp->t_dev->si_drv1; 315 316 if (tp->t_state & TS_TTSTOP) 317 return; 318 if (pti->pt_flags & PF_STOPPED) { 319 pti->pt_flags &= ~PF_STOPPED; 320 pti->pt_send = TIOCPKT_START; 321 } 322 ptcwakeup(tp, FREAD); 323} 324 325static void 326ptcwakeup(tp, flag) 327 struct tty *tp; 328 int flag; 329{ 330 struct pt_ioctl *pti = tp->t_dev->si_drv1; 331 332 if (flag & FREAD) { 333 selwakeup(&pti->pt_selr); 334 wakeup(TSA_PTC_READ(tp)); 335 } 336 if (flag & FWRITE) { 337 selwakeup(&pti->pt_selw); 338 wakeup(TSA_PTC_WRITE(tp)); 339 } 340} 341 342static int 343ptcopen(dev, flag, devtype, p) 344 dev_t dev; 345 int flag, devtype; 346 struct proc *p; 347{ 348 register struct tty *tp; 349 struct pt_ioctl *pti; 350 351 if (!dev->si_drv1) 352 ptyinit(minor(dev)); 353 if (!dev->si_drv1) 354 return(ENXIO); 355 tp = dev->si_tty; 356 if (tp->t_oproc) 357 return (EIO); 358 tp->t_oproc = ptsstart; 359 tp->t_stop = ptsstop; 360 (void)(*linesw[tp->t_line].l_modem)(tp, 1); 361 tp->t_lflag &= ~EXTPROC; 362 pti = dev->si_drv1;
|
| 363 pti->pt_prison = p->p_prison;
|
357 pti->pt_flags = 0; 358 pti->pt_send = 0; 359 pti->pt_ucntl = 0; 360 return (0); 361} 362 363static int 364ptcclose(dev, flags, fmt, p) 365 dev_t dev; 366 int flags; 367 int fmt; 368 struct proc *p; 369{ 370 register struct tty *tp; 371 372 tp = dev->si_tty; 373 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 374 375 /* 376 * XXX MDMBUF makes no sense for ptys but would inhibit the above 377 * l_modem(). CLOCAL makes sense but isn't supported. Special 378 * l_modem()s that ignore carrier drop make no sense for ptys but 379 * may be in use because other parts of the line discipline make 380 * sense for ptys. Recover by doing everything that a normal 381 * ttymodem() would have done except for sending a SIGHUP. 382 */ 383 if (tp->t_state & TS_ISOPEN) { 384 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 385 tp->t_state |= TS_ZOMBIE; 386 ttyflush(tp, FREAD | FWRITE); 387 } 388 389 tp->t_oproc = 0; /* mark closed */ 390 return (0); 391} 392 393static int 394ptcread(dev, uio, flag) 395 dev_t dev; 396 struct uio *uio; 397 int flag; 398{ 399 register struct tty *tp = dev->si_tty; 400 struct pt_ioctl *pti = dev->si_drv1; 401 char buf[BUFSIZ]; 402 int error = 0, cc; 403 404 /* 405 * We want to block until the slave 406 * is open, and there's something to read; 407 * but if we lost the slave or we're NBIO, 408 * then return the appropriate error instead. 409 */ 410 for (;;) { 411 if (tp->t_state&TS_ISOPEN) { 412 if (pti->pt_flags&PF_PKT && pti->pt_send) { 413 error = ureadc((int)pti->pt_send, uio); 414 if (error) 415 return (error); 416 if (pti->pt_send & TIOCPKT_IOCTL) { 417 cc = min(uio->uio_resid, 418 sizeof(tp->t_termios)); 419 uiomove((caddr_t)&tp->t_termios, cc, 420 uio); 421 } 422 pti->pt_send = 0; 423 return (0); 424 } 425 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 426 error = ureadc((int)pti->pt_ucntl, uio); 427 if (error) 428 return (error); 429 pti->pt_ucntl = 0; 430 return (0); 431 } 432 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 433 break; 434 } 435 if ((tp->t_state & TS_CONNECTED) == 0) 436 return (0); /* EOF */ 437 if (flag & IO_NDELAY) 438 return (EWOULDBLOCK); 439 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 440 if (error) 441 return (error); 442 } 443 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 444 error = ureadc(0, uio); 445 while (uio->uio_resid > 0 && error == 0) { 446 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 447 if (cc <= 0) 448 break; 449 error = uiomove(buf, cc, uio); 450 } 451 ttwwakeup(tp); 452 return (error); 453} 454 455static void 456ptsstop(tp, flush) 457 register struct tty *tp; 458 int flush; 459{ 460 struct pt_ioctl *pti = tp->t_dev->si_drv1; 461 int flag; 462 463 /* note: FLUSHREAD and FLUSHWRITE already ok */ 464 if (flush == 0) { 465 flush = TIOCPKT_STOP; 466 pti->pt_flags |= PF_STOPPED; 467 } else 468 pti->pt_flags &= ~PF_STOPPED; 469 pti->pt_send |= flush; 470 /* change of perspective */ 471 flag = 0; 472 if (flush & FREAD) 473 flag |= FWRITE; 474 if (flush & FWRITE) 475 flag |= FREAD; 476 ptcwakeup(tp, flag); 477} 478 479static int 480ptcpoll(dev, events, p) 481 dev_t dev; 482 int events; 483 struct proc *p; 484{ 485 register struct tty *tp = dev->si_tty; 486 struct pt_ioctl *pti = dev->si_drv1; 487 int revents = 0; 488 int s; 489 490 if ((tp->t_state & TS_CONNECTED) == 0) 491 return (seltrue(dev, events, p) | POLLHUP); 492 493 /* 494 * Need to block timeouts (ttrstart). 495 */ 496 s = spltty(); 497 498 if (events & (POLLIN | POLLRDNORM)) 499 if ((tp->t_state & TS_ISOPEN) && 500 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 501 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 502 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 503 revents |= events & (POLLIN | POLLRDNORM); 504 505 if (events & (POLLOUT | POLLWRNORM)) 506 if (tp->t_state & TS_ISOPEN && 507 ((pti->pt_flags & PF_REMOTE) ? 508 (tp->t_canq.c_cc == 0) : 509 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 510 (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON))))) 511 revents |= events & (POLLOUT | POLLWRNORM); 512 513 if (events & POLLHUP) 514 if ((tp->t_state & TS_CARR_ON) == 0) 515 revents |= POLLHUP; 516 517 if (revents == 0) { 518 if (events & (POLLIN | POLLRDNORM)) 519 selrecord(p, &pti->pt_selr); 520 521 if (events & (POLLOUT | POLLWRNORM)) 522 selrecord(p, &pti->pt_selw); 523 } 524 splx(s); 525 526 return (revents); 527} 528 529static int 530ptcwrite(dev, uio, flag) 531 dev_t dev; 532 register struct uio *uio; 533 int flag; 534{ 535 register struct tty *tp = dev->si_tty; 536 register u_char *cp = 0; 537 register int cc = 0; 538 u_char locbuf[BUFSIZ]; 539 int cnt = 0; 540 struct pt_ioctl *pti = dev->si_drv1; 541 int error = 0; 542 543again: 544 if ((tp->t_state&TS_ISOPEN) == 0) 545 goto block; 546 if (pti->pt_flags & PF_REMOTE) { 547 if (tp->t_canq.c_cc) 548 goto block; 549 while ((uio->uio_resid > 0 || cc > 0) && 550 tp->t_canq.c_cc < TTYHOG - 1) { 551 if (cc == 0) { 552 cc = min(uio->uio_resid, BUFSIZ); 553 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 554 cp = locbuf; 555 error = uiomove((caddr_t)cp, cc, uio); 556 if (error) 557 return (error); 558 /* check again for safety */ 559 if ((tp->t_state & TS_ISOPEN) == 0) { 560 /* adjust as usual */ 561 uio->uio_resid += cc; 562 return (EIO); 563 } 564 } 565 if (cc > 0) { 566 cc = b_to_q((char *)cp, cc, &tp->t_canq); 567 /* 568 * XXX we don't guarantee that the canq size 569 * is >= TTYHOG, so the above b_to_q() may 570 * leave some bytes uncopied. However, space 571 * is guaranteed for the null terminator if 572 * we don't fail here since (TTYHOG - 1) is 573 * not a multiple of CBSIZE. 574 */ 575 if (cc > 0) 576 break; 577 } 578 } 579 /* adjust for data copied in but not written */ 580 uio->uio_resid += cc; 581 (void) putc(0, &tp->t_canq); 582 ttwakeup(tp); 583 wakeup(TSA_PTS_READ(tp)); 584 return (0); 585 } 586 while (uio->uio_resid > 0 || cc > 0) { 587 if (cc == 0) { 588 cc = min(uio->uio_resid, BUFSIZ); 589 cp = locbuf; 590 error = uiomove((caddr_t)cp, cc, uio); 591 if (error) 592 return (error); 593 /* check again for safety */ 594 if ((tp->t_state & TS_ISOPEN) == 0) { 595 /* adjust for data copied in but not written */ 596 uio->uio_resid += cc; 597 return (EIO); 598 } 599 } 600 while (cc > 0) { 601 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 602 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 603 wakeup(TSA_HUP_OR_INPUT(tp)); 604 goto block; 605 } 606 (*linesw[tp->t_line].l_rint)(*cp++, tp); 607 cnt++; 608 cc--; 609 } 610 cc = 0; 611 } 612 return (0); 613block: 614 /* 615 * Come here to wait for slave to open, for space 616 * in outq, or space in rawq, or an empty canq. 617 */ 618 if ((tp->t_state & TS_CONNECTED) == 0) { 619 /* adjust for data copied in but not written */ 620 uio->uio_resid += cc; 621 return (EIO); 622 } 623 if (flag & IO_NDELAY) { 624 /* adjust for data copied in but not written */ 625 uio->uio_resid += cc; 626 if (cnt == 0) 627 return (EWOULDBLOCK); 628 return (0); 629 } 630 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 631 if (error) { 632 /* adjust for data copied in but not written */ 633 uio->uio_resid += cc; 634 return (error); 635 } 636 goto again; 637} 638 639/*ARGSUSED*/ 640static int 641ptyioctl(dev, cmd, data, flag, p) 642 dev_t dev; 643 u_long cmd; 644 caddr_t data; 645 int flag; 646 struct proc *p; 647{ 648 register struct tty *tp = dev->si_tty; 649 register struct pt_ioctl *pti = dev->si_drv1; 650 register u_char *cc = tp->t_cc; 651 int stop, error; 652 653 if (devsw(dev)->d_open == ptcopen) { 654 switch (cmd) { 655 656 case TIOCGPGRP: 657 /* 658 * We avoid calling ttioctl on the controller since, 659 * in that case, tp must be the controlling terminal. 660 */ 661 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 662 return (0); 663 664 case TIOCPKT: 665 if (*(int *)data) { 666 if (pti->pt_flags & PF_UCNTL) 667 return (EINVAL); 668 pti->pt_flags |= PF_PKT; 669 } else 670 pti->pt_flags &= ~PF_PKT; 671 return (0); 672 673 case TIOCUCNTL: 674 if (*(int *)data) { 675 if (pti->pt_flags & PF_PKT) 676 return (EINVAL); 677 pti->pt_flags |= PF_UCNTL; 678 } else 679 pti->pt_flags &= ~PF_UCNTL; 680 return (0); 681 682 case TIOCREMOTE: 683 if (*(int *)data) 684 pti->pt_flags |= PF_REMOTE; 685 else 686 pti->pt_flags &= ~PF_REMOTE; 687 ttyflush(tp, FREAD|FWRITE); 688 return (0); 689 } 690 691 /* 692 * The rest of the ioctls shouldn't be called until 693 * the slave is open. 694 */ 695 if ((tp->t_state & TS_ISOPEN) == 0) 696 return (EAGAIN); 697 698 switch (cmd) { 699#ifdef COMPAT_43 700 case TIOCSETP: 701 case TIOCSETN: 702#endif 703 case TIOCSETD: 704 case TIOCSETA: 705 case TIOCSETAW: 706 case TIOCSETAF: 707 /* 708 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 709 * ttywflush(tp) will hang if there are characters in 710 * the outq. 711 */ 712 ndflush(&tp->t_outq, tp->t_outq.c_cc); 713 break; 714 715 case TIOCSIG: 716 if (*(unsigned int *)data >= NSIG || 717 *(unsigned int *)data == 0) 718 return(EINVAL); 719 if ((tp->t_lflag&NOFLSH) == 0) 720 ttyflush(tp, FREAD|FWRITE); 721 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 722 if ((*(unsigned int *)data == SIGINFO) && 723 ((tp->t_lflag&NOKERNINFO) == 0)) 724 ttyinfo(tp); 725 return(0); 726 } 727 } 728 if (cmd == TIOCEXT) { 729 /* 730 * When the EXTPROC bit is being toggled, we need 731 * to send an TIOCPKT_IOCTL if the packet driver 732 * is turned on. 733 */ 734 if (*(int *)data) { 735 if (pti->pt_flags & PF_PKT) { 736 pti->pt_send |= TIOCPKT_IOCTL; 737 ptcwakeup(tp, FREAD); 738 } 739 tp->t_lflag |= EXTPROC; 740 } else { 741 if ((tp->t_lflag & EXTPROC) && 742 (pti->pt_flags & PF_PKT)) { 743 pti->pt_send |= TIOCPKT_IOCTL; 744 ptcwakeup(tp, FREAD); 745 } 746 tp->t_lflag &= ~EXTPROC; 747 } 748 return(0); 749 } 750 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 751 if (error == ENOIOCTL) 752 error = ttioctl(tp, cmd, data, flag); 753 if (error == ENOIOCTL) { 754 if (pti->pt_flags & PF_UCNTL && 755 (cmd & ~0xff) == UIOCCMD(0)) { 756 if (cmd & 0xff) { 757 pti->pt_ucntl = (u_char)cmd; 758 ptcwakeup(tp, FREAD); 759 } 760 return (0); 761 } 762 error = ENOTTY; 763 } 764 /* 765 * If external processing and packet mode send ioctl packet. 766 */ 767 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 768 switch(cmd) { 769 case TIOCSETA: 770 case TIOCSETAW: 771 case TIOCSETAF: 772#ifdef COMPAT_43 773 case TIOCSETP: 774 case TIOCSETN: 775#endif 776#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 777 case TIOCSETC: 778 case TIOCSLTC: 779 case TIOCLBIS: 780 case TIOCLBIC: 781 case TIOCLSET: 782#endif 783 pti->pt_send |= TIOCPKT_IOCTL; 784 ptcwakeup(tp, FREAD); 785 default: 786 break; 787 } 788 } 789 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 790 && CCEQ(cc[VSTART], CTRL('q')); 791 if (pti->pt_flags & PF_NOSTOP) { 792 if (stop) { 793 pti->pt_send &= ~TIOCPKT_NOSTOP; 794 pti->pt_send |= TIOCPKT_DOSTOP; 795 pti->pt_flags &= ~PF_NOSTOP; 796 ptcwakeup(tp, FREAD); 797 } 798 } else { 799 if (!stop) { 800 pti->pt_send &= ~TIOCPKT_DOSTOP; 801 pti->pt_send |= TIOCPKT_NOSTOP; 802 pti->pt_flags |= PF_NOSTOP; 803 ptcwakeup(tp, FREAD); 804 } 805 } 806 return (error); 807} 808 809 810static void ptc_drvinit __P((void *unused)); 811 812static void 813ptc_drvinit(unused) 814 void *unused; 815{ 816 cdevsw_add(&pts_cdevsw); 817 cdevsw_add(&ptc_cdevsw); 818 /* XXX: Gross hack for DEVFS */ 819 ptyinit(0); 820} 821 822SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
| 364 pti->pt_flags = 0; 365 pti->pt_send = 0; 366 pti->pt_ucntl = 0; 367 return (0); 368} 369 370static int 371ptcclose(dev, flags, fmt, p) 372 dev_t dev; 373 int flags; 374 int fmt; 375 struct proc *p; 376{ 377 register struct tty *tp; 378 379 tp = dev->si_tty; 380 (void)(*linesw[tp->t_line].l_modem)(tp, 0); 381 382 /* 383 * XXX MDMBUF makes no sense for ptys but would inhibit the above 384 * l_modem(). CLOCAL makes sense but isn't supported. Special 385 * l_modem()s that ignore carrier drop make no sense for ptys but 386 * may be in use because other parts of the line discipline make 387 * sense for ptys. Recover by doing everything that a normal 388 * ttymodem() would have done except for sending a SIGHUP. 389 */ 390 if (tp->t_state & TS_ISOPEN) { 391 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); 392 tp->t_state |= TS_ZOMBIE; 393 ttyflush(tp, FREAD | FWRITE); 394 } 395 396 tp->t_oproc = 0; /* mark closed */ 397 return (0); 398} 399 400static int 401ptcread(dev, uio, flag) 402 dev_t dev; 403 struct uio *uio; 404 int flag; 405{ 406 register struct tty *tp = dev->si_tty; 407 struct pt_ioctl *pti = dev->si_drv1; 408 char buf[BUFSIZ]; 409 int error = 0, cc; 410 411 /* 412 * We want to block until the slave 413 * is open, and there's something to read; 414 * but if we lost the slave or we're NBIO, 415 * then return the appropriate error instead. 416 */ 417 for (;;) { 418 if (tp->t_state&TS_ISOPEN) { 419 if (pti->pt_flags&PF_PKT && pti->pt_send) { 420 error = ureadc((int)pti->pt_send, uio); 421 if (error) 422 return (error); 423 if (pti->pt_send & TIOCPKT_IOCTL) { 424 cc = min(uio->uio_resid, 425 sizeof(tp->t_termios)); 426 uiomove((caddr_t)&tp->t_termios, cc, 427 uio); 428 } 429 pti->pt_send = 0; 430 return (0); 431 } 432 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { 433 error = ureadc((int)pti->pt_ucntl, uio); 434 if (error) 435 return (error); 436 pti->pt_ucntl = 0; 437 return (0); 438 } 439 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) 440 break; 441 } 442 if ((tp->t_state & TS_CONNECTED) == 0) 443 return (0); /* EOF */ 444 if (flag & IO_NDELAY) 445 return (EWOULDBLOCK); 446 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0); 447 if (error) 448 return (error); 449 } 450 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) 451 error = ureadc(0, uio); 452 while (uio->uio_resid > 0 && error == 0) { 453 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); 454 if (cc <= 0) 455 break; 456 error = uiomove(buf, cc, uio); 457 } 458 ttwwakeup(tp); 459 return (error); 460} 461 462static void 463ptsstop(tp, flush) 464 register struct tty *tp; 465 int flush; 466{ 467 struct pt_ioctl *pti = tp->t_dev->si_drv1; 468 int flag; 469 470 /* note: FLUSHREAD and FLUSHWRITE already ok */ 471 if (flush == 0) { 472 flush = TIOCPKT_STOP; 473 pti->pt_flags |= PF_STOPPED; 474 } else 475 pti->pt_flags &= ~PF_STOPPED; 476 pti->pt_send |= flush; 477 /* change of perspective */ 478 flag = 0; 479 if (flush & FREAD) 480 flag |= FWRITE; 481 if (flush & FWRITE) 482 flag |= FREAD; 483 ptcwakeup(tp, flag); 484} 485 486static int 487ptcpoll(dev, events, p) 488 dev_t dev; 489 int events; 490 struct proc *p; 491{ 492 register struct tty *tp = dev->si_tty; 493 struct pt_ioctl *pti = dev->si_drv1; 494 int revents = 0; 495 int s; 496 497 if ((tp->t_state & TS_CONNECTED) == 0) 498 return (seltrue(dev, events, p) | POLLHUP); 499 500 /* 501 * Need to block timeouts (ttrstart). 502 */ 503 s = spltty(); 504 505 if (events & (POLLIN | POLLRDNORM)) 506 if ((tp->t_state & TS_ISOPEN) && 507 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || 508 ((pti->pt_flags & PF_PKT) && pti->pt_send) || 509 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) 510 revents |= events & (POLLIN | POLLRDNORM); 511 512 if (events & (POLLOUT | POLLWRNORM)) 513 if (tp->t_state & TS_ISOPEN && 514 ((pti->pt_flags & PF_REMOTE) ? 515 (tp->t_canq.c_cc == 0) : 516 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || 517 (tp->t_canq.c_cc == 0 && (tp->t_iflag & ICANON))))) 518 revents |= events & (POLLOUT | POLLWRNORM); 519 520 if (events & POLLHUP) 521 if ((tp->t_state & TS_CARR_ON) == 0) 522 revents |= POLLHUP; 523 524 if (revents == 0) { 525 if (events & (POLLIN | POLLRDNORM)) 526 selrecord(p, &pti->pt_selr); 527 528 if (events & (POLLOUT | POLLWRNORM)) 529 selrecord(p, &pti->pt_selw); 530 } 531 splx(s); 532 533 return (revents); 534} 535 536static int 537ptcwrite(dev, uio, flag) 538 dev_t dev; 539 register struct uio *uio; 540 int flag; 541{ 542 register struct tty *tp = dev->si_tty; 543 register u_char *cp = 0; 544 register int cc = 0; 545 u_char locbuf[BUFSIZ]; 546 int cnt = 0; 547 struct pt_ioctl *pti = dev->si_drv1; 548 int error = 0; 549 550again: 551 if ((tp->t_state&TS_ISOPEN) == 0) 552 goto block; 553 if (pti->pt_flags & PF_REMOTE) { 554 if (tp->t_canq.c_cc) 555 goto block; 556 while ((uio->uio_resid > 0 || cc > 0) && 557 tp->t_canq.c_cc < TTYHOG - 1) { 558 if (cc == 0) { 559 cc = min(uio->uio_resid, BUFSIZ); 560 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); 561 cp = locbuf; 562 error = uiomove((caddr_t)cp, cc, uio); 563 if (error) 564 return (error); 565 /* check again for safety */ 566 if ((tp->t_state & TS_ISOPEN) == 0) { 567 /* adjust as usual */ 568 uio->uio_resid += cc; 569 return (EIO); 570 } 571 } 572 if (cc > 0) { 573 cc = b_to_q((char *)cp, cc, &tp->t_canq); 574 /* 575 * XXX we don't guarantee that the canq size 576 * is >= TTYHOG, so the above b_to_q() may 577 * leave some bytes uncopied. However, space 578 * is guaranteed for the null terminator if 579 * we don't fail here since (TTYHOG - 1) is 580 * not a multiple of CBSIZE. 581 */ 582 if (cc > 0) 583 break; 584 } 585 } 586 /* adjust for data copied in but not written */ 587 uio->uio_resid += cc; 588 (void) putc(0, &tp->t_canq); 589 ttwakeup(tp); 590 wakeup(TSA_PTS_READ(tp)); 591 return (0); 592 } 593 while (uio->uio_resid > 0 || cc > 0) { 594 if (cc == 0) { 595 cc = min(uio->uio_resid, BUFSIZ); 596 cp = locbuf; 597 error = uiomove((caddr_t)cp, cc, uio); 598 if (error) 599 return (error); 600 /* check again for safety */ 601 if ((tp->t_state & TS_ISOPEN) == 0) { 602 /* adjust for data copied in but not written */ 603 uio->uio_resid += cc; 604 return (EIO); 605 } 606 } 607 while (cc > 0) { 608 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && 609 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { 610 wakeup(TSA_HUP_OR_INPUT(tp)); 611 goto block; 612 } 613 (*linesw[tp->t_line].l_rint)(*cp++, tp); 614 cnt++; 615 cc--; 616 } 617 cc = 0; 618 } 619 return (0); 620block: 621 /* 622 * Come here to wait for slave to open, for space 623 * in outq, or space in rawq, or an empty canq. 624 */ 625 if ((tp->t_state & TS_CONNECTED) == 0) { 626 /* adjust for data copied in but not written */ 627 uio->uio_resid += cc; 628 return (EIO); 629 } 630 if (flag & IO_NDELAY) { 631 /* adjust for data copied in but not written */ 632 uio->uio_resid += cc; 633 if (cnt == 0) 634 return (EWOULDBLOCK); 635 return (0); 636 } 637 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0); 638 if (error) { 639 /* adjust for data copied in but not written */ 640 uio->uio_resid += cc; 641 return (error); 642 } 643 goto again; 644} 645 646/*ARGSUSED*/ 647static int 648ptyioctl(dev, cmd, data, flag, p) 649 dev_t dev; 650 u_long cmd; 651 caddr_t data; 652 int flag; 653 struct proc *p; 654{ 655 register struct tty *tp = dev->si_tty; 656 register struct pt_ioctl *pti = dev->si_drv1; 657 register u_char *cc = tp->t_cc; 658 int stop, error; 659 660 if (devsw(dev)->d_open == ptcopen) { 661 switch (cmd) { 662 663 case TIOCGPGRP: 664 /* 665 * We avoid calling ttioctl on the controller since, 666 * in that case, tp must be the controlling terminal. 667 */ 668 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 669 return (0); 670 671 case TIOCPKT: 672 if (*(int *)data) { 673 if (pti->pt_flags & PF_UCNTL) 674 return (EINVAL); 675 pti->pt_flags |= PF_PKT; 676 } else 677 pti->pt_flags &= ~PF_PKT; 678 return (0); 679 680 case TIOCUCNTL: 681 if (*(int *)data) { 682 if (pti->pt_flags & PF_PKT) 683 return (EINVAL); 684 pti->pt_flags |= PF_UCNTL; 685 } else 686 pti->pt_flags &= ~PF_UCNTL; 687 return (0); 688 689 case TIOCREMOTE: 690 if (*(int *)data) 691 pti->pt_flags |= PF_REMOTE; 692 else 693 pti->pt_flags &= ~PF_REMOTE; 694 ttyflush(tp, FREAD|FWRITE); 695 return (0); 696 } 697 698 /* 699 * The rest of the ioctls shouldn't be called until 700 * the slave is open. 701 */ 702 if ((tp->t_state & TS_ISOPEN) == 0) 703 return (EAGAIN); 704 705 switch (cmd) { 706#ifdef COMPAT_43 707 case TIOCSETP: 708 case TIOCSETN: 709#endif 710 case TIOCSETD: 711 case TIOCSETA: 712 case TIOCSETAW: 713 case TIOCSETAF: 714 /* 715 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. 716 * ttywflush(tp) will hang if there are characters in 717 * the outq. 718 */ 719 ndflush(&tp->t_outq, tp->t_outq.c_cc); 720 break; 721 722 case TIOCSIG: 723 if (*(unsigned int *)data >= NSIG || 724 *(unsigned int *)data == 0) 725 return(EINVAL); 726 if ((tp->t_lflag&NOFLSH) == 0) 727 ttyflush(tp, FREAD|FWRITE); 728 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); 729 if ((*(unsigned int *)data == SIGINFO) && 730 ((tp->t_lflag&NOKERNINFO) == 0)) 731 ttyinfo(tp); 732 return(0); 733 } 734 } 735 if (cmd == TIOCEXT) { 736 /* 737 * When the EXTPROC bit is being toggled, we need 738 * to send an TIOCPKT_IOCTL if the packet driver 739 * is turned on. 740 */ 741 if (*(int *)data) { 742 if (pti->pt_flags & PF_PKT) { 743 pti->pt_send |= TIOCPKT_IOCTL; 744 ptcwakeup(tp, FREAD); 745 } 746 tp->t_lflag |= EXTPROC; 747 } else { 748 if ((tp->t_lflag & EXTPROC) && 749 (pti->pt_flags & PF_PKT)) { 750 pti->pt_send |= TIOCPKT_IOCTL; 751 ptcwakeup(tp, FREAD); 752 } 753 tp->t_lflag &= ~EXTPROC; 754 } 755 return(0); 756 } 757 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 758 if (error == ENOIOCTL) 759 error = ttioctl(tp, cmd, data, flag); 760 if (error == ENOIOCTL) { 761 if (pti->pt_flags & PF_UCNTL && 762 (cmd & ~0xff) == UIOCCMD(0)) { 763 if (cmd & 0xff) { 764 pti->pt_ucntl = (u_char)cmd; 765 ptcwakeup(tp, FREAD); 766 } 767 return (0); 768 } 769 error = ENOTTY; 770 } 771 /* 772 * If external processing and packet mode send ioctl packet. 773 */ 774 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { 775 switch(cmd) { 776 case TIOCSETA: 777 case TIOCSETAW: 778 case TIOCSETAF: 779#ifdef COMPAT_43 780 case TIOCSETP: 781 case TIOCSETN: 782#endif 783#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 784 case TIOCSETC: 785 case TIOCSLTC: 786 case TIOCLBIS: 787 case TIOCLBIC: 788 case TIOCLSET: 789#endif 790 pti->pt_send |= TIOCPKT_IOCTL; 791 ptcwakeup(tp, FREAD); 792 default: 793 break; 794 } 795 } 796 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) 797 && CCEQ(cc[VSTART], CTRL('q')); 798 if (pti->pt_flags & PF_NOSTOP) { 799 if (stop) { 800 pti->pt_send &= ~TIOCPKT_NOSTOP; 801 pti->pt_send |= TIOCPKT_DOSTOP; 802 pti->pt_flags &= ~PF_NOSTOP; 803 ptcwakeup(tp, FREAD); 804 } 805 } else { 806 if (!stop) { 807 pti->pt_send &= ~TIOCPKT_DOSTOP; 808 pti->pt_send |= TIOCPKT_NOSTOP; 809 pti->pt_flags |= PF_NOSTOP; 810 ptcwakeup(tp, FREAD); 811 } 812 } 813 return (error); 814} 815 816 817static void ptc_drvinit __P((void *unused)); 818 819static void 820ptc_drvinit(unused) 821 void *unused; 822{ 823 cdevsw_add(&pts_cdevsw); 824 cdevsw_add(&ptc_cdevsw); 825 /* XXX: Gross hack for DEVFS */ 826 ptyinit(0); 827} 828 829SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
|