| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * %sccs.include.redist.c% |
| 6 | * |
| 7 | * @(#)tty_pty.c 7.29 (Berkeley) %G% |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Pseudo-teletype Driver |
| 12 | * (Actually two drivers, requiring two entries in 'cdevsw') |
| 13 | */ |
| 14 | #include "pty.h" /* XXX */ |
| 15 | |
| 16 | #include <sys/param.h> |
| 17 | #include <sys/systm.h> |
| 18 | #include <sys/ioctl.h> |
| 19 | #include <sys/proc.h> |
| 20 | #include <sys/tty.h> |
| 21 | #include <sys/conf.h> |
| 22 | #include <sys/file.h> |
| 23 | #include <sys/uio.h> |
| 24 | #include <sys/kernel.h> |
| 25 | #include <sys/vnode.h> |
| 26 | #include "tsleep.h" |
| 27 | |
| 28 | #if NPTY == 1 |
| 29 | #undef NPTY |
| 30 | #define NPTY 32 /* crude XXX */ |
| 31 | #endif |
| 32 | |
| 33 | #define BUFSIZ 100 /* Chunk size iomoved to/from user */ |
| 34 | |
| 35 | /* |
| 36 | * pts == /dev/tty[pqrs]? |
| 37 | * ptc == /dev/pty[pqrs]? |
| 38 | */ |
| 39 | struct tty pt_tty[NPTY]; /* XXX */ |
| 40 | struct pt_ioctl { |
| 41 | int pt_flags; |
| 42 | struct selinfo pt_selr, pt_selw; |
| 43 | u_char pt_send; |
| 44 | u_char pt_ucntl; |
| 45 | struct clist pt_ioc; |
| 46 | } pt_ioctl[NPTY]; /* XXX */ |
| 47 | int npty = NPTY; /* for pstat -t */ |
| 48 | |
| 49 | #define PF_RCOLL 0x0001 |
| 50 | #define PF_WCOLL 0x0002 |
| 51 | #define PF_NBIO 0x0004 |
| 52 | #define PF_PKT 0x0008 /* packet mode */ |
| 53 | #define PF_STOPPED 0x0010 /* user told stopped */ |
| 54 | #define PF_REMOTE 0x0020 /* remote and flow controlled input */ |
| 55 | #define PF_NOSTOP 0x0040 |
| 56 | #define PF_UCNTL 0x0080 /* user control mode */ |
| 57 | #define PF_TIOC 0x0100 /* transparent control mode */ |
| 58 | #define PF_LIOC 0x0200 /* transparent control locked */ |
| 59 | #define PF_WIOC 0x0400 /* waiting for PF_LIOC to clear */ |
| 60 | #define PF_BLOCK 0x0800 /* block writes to slave */ |
| 61 | #define PF_OWAIT 0x1000 /* waiting for PF_BLOCK to clear */ |
| 62 | |
| 63 | void ptsstop __P((struct tty *, int)); |
| 64 | |
| 65 | /* |
| 66 | * Establish n (or default if n is 1) ptys in the system. |
| 67 | * |
| 68 | * XXX cdevsw & pstat require the array `pty[]' to be an array |
| 69 | */ |
| 70 | void |
| 71 | ptyattach(n) |
| 72 | int n; |
| 73 | { |
| 74 | #ifdef notyet |
| 75 | char *mem; |
| 76 | register u_long ntb; |
| 77 | #define DEFAULT_NPTY 32 |
| 78 | |
| 79 | /* maybe should allow 0 => none? */ |
| 80 | if (n <= 1) |
| 81 | n = DEFAULT_NPTY; |
| 82 | ntb = n * sizeof(struct tty); |
| 83 | mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl), |
| 84 | M_DEVBUF, M_WAITOK); |
| 85 | pt_tty = (struct tty *)mem; |
| 86 | mem = (char *)ALIGN(mem + ntb); |
| 87 | pt_ioctl = (struct pt_ioctl *)mem; |
| 88 | npty = n; |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | /*ARGSUSED*/ |
| 93 | ptsopen(dev, flag, devtype, p) |
| 94 | dev_t dev; |
| 95 | int flag, devtype; |
| 96 | struct proc *p; |
| 97 | { |
| 98 | register struct tty *tp; |
| 99 | int error; |
| 100 | |
| 101 | if (minor(dev) >= npty) |
| 102 | return (ENXIO); |
| 103 | tp = &pt_tty[minor(dev)]; |
| 104 | if ((tp->t_state & TS_ISOPEN) == 0) { |
| 105 | tp->t_state |= TS_WOPEN; |
| 106 | ttychars(tp); /* Set up default chars */ |
| 107 | tp->t_iflag = TTYDEF_IFLAG; |
| 108 | tp->t_oflag = TTYDEF_OFLAG; |
| 109 | tp->t_lflag = TTYDEF_LFLAG; |
| 110 | tp->t_cflag = TTYDEF_CFLAG; |
| 111 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; |
| 112 | ttsetwater(tp); /* would be done in xxparam() */ |
| 113 | } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) |
| 114 | return (EBUSY); |
| 115 | if (tp->t_oproc) /* Ctrlr still around. */ |
| 116 | tp->t_state |= TS_CARR_ON; |
| 117 | while ((tp->t_state & TS_CARR_ON) == 0) { |
| 118 | tp->t_state |= TS_WOPEN; |
| 119 | if (flag&FNONBLOCK) |
| 120 | break; |
| 121 | if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, |
| 122 | ttopen, 0)) |
| 123 | return (error); |
| 124 | } |
| 125 | error = (*linesw[tp->t_line].l_open)(dev, tp); |
| 126 | ptcwakeup(tp, FREAD|FWRITE); |
| 127 | return (error); |
| 128 | } |
| 129 | |
| 130 | ptsclose(dev, flag, mode, p) |
| 131 | dev_t dev; |
| 132 | int flag, mode; |
| 133 | struct proc *p; |
| 134 | { |
| 135 | register struct tty *tp; |
| 136 | int err; |
| 137 | |
| 138 | tp = &pt_tty[minor(dev)]; |
| 139 | err = (*linesw[tp->t_line].l_close)(tp, flag); |
| 140 | err |= ttyclose(tp); |
| 141 | ptcwakeup(tp, FREAD|FWRITE); |
| 142 | return (err); |
| 143 | return (0); |
| 144 | } |
| 145 | |
| 146 | ptsread(dev, uio, flag) |
| 147 | dev_t dev; |
| 148 | struct uio *uio; |
| 149 | int flag; |
| 150 | { |
| 151 | struct proc *p = curproc; |
| 152 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 153 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 154 | int error = 0; |
| 155 | |
| 156 | again: |
| 157 | if (pti->pt_flags & PF_REMOTE) { |
| 158 | while (isbackground(p, tp)) { |
| 159 | if ((p->p_sigignore & sigmask(SIGTTIN)) || |
| 160 | (p->p_sigmask & sigmask(SIGTTIN)) || |
| 161 | p->p_pgrp->pg_jobc == 0 || |
| 162 | p->p_flag&SPPWAIT) |
| 163 | return (EIO); |
| 164 | pgsignal(p->p_pgrp, SIGTTIN, 1); |
| 165 | if (error = ttysleep(tp, (caddr_t)&lbolt, |
| 166 | TTIPRI | PCATCH, ttybg, 0)) |
| 167 | return (error); |
| 168 | } |
| 169 | if (tp->t_canq.c_cc == 0) { |
| 170 | if (flag & IO_NDELAY) |
| 171 | return (EWOULDBLOCK); |
| 172 | if (error = ttysleep(tp, (caddr_t)&tp->t_canq, |
| 173 | TTIPRI | PCATCH, ttyin, 0)) |
| 174 | return (error); |
| 175 | goto again; |
| 176 | } |
| 177 | while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) |
| 178 | if (ureadc(getc(&tp->t_canq), uio) < 0) { |
| 179 | error = EFAULT; |
| 180 | break; |
| 181 | } |
| 182 | if (tp->t_canq.c_cc == 1) |
| 183 | (void) getc(&tp->t_canq); |
| 184 | if (tp->t_canq.c_cc) |
| 185 | return (error); |
| 186 | } else |
| 187 | if (tp->t_oproc) |
| 188 | error = (*linesw[tp->t_line].l_read)(tp, uio, flag); |
| 189 | ptcwakeup(tp, FWRITE); |
| 190 | return (error); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Write to pseudo-tty. |
| 195 | * Wakeups of controlling tty will happen |
| 196 | * indirectly, when tty driver calls ptsstart. |
| 197 | */ |
| 198 | ptswrite(dev, uio, flag) |
| 199 | dev_t dev; |
| 200 | struct uio *uio; |
| 201 | int flag; |
| 202 | { |
| 203 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 204 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 205 | |
| 206 | if (tp->t_oproc == 0) |
| 207 | return (EIO); |
| 208 | |
| 209 | while (pti->pt_flags & PF_BLOCK) { |
| 210 | pti->pt_flags |= PF_OWAIT; |
| 211 | sleep((caddr_t)pti + 1, TTOPRI); |
| 212 | } |
| 213 | |
| 214 | return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Start output on pseudo-tty. |
| 219 | * Wake up process selecting or sleeping for input from controlling tty. |
| 220 | */ |
| 221 | void |
| 222 | ptsstart(tp) |
| 223 | struct tty *tp; |
| 224 | { |
| 225 | register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; |
| 226 | |
| 227 | if (tp->t_state & TS_TTSTOP) |
| 228 | return; |
| 229 | if (pti->pt_flags & PF_STOPPED) { |
| 230 | pti->pt_flags &= ~PF_STOPPED; |
| 231 | pti->pt_send = TIOCPKT_START; |
| 232 | } |
| 233 | ptcwakeup(tp, FREAD); |
| 234 | } |
| 235 | |
| 236 | ptcwakeup(tp, flag) |
| 237 | struct tty *tp; |
| 238 | int flag; |
| 239 | { |
| 240 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; |
| 241 | |
| 242 | if (flag & FREAD) { |
| 243 | selwakeup(&pti->pt_selr); |
| 244 | wakeup((caddr_t)&tp->t_outq.c_cf); |
| 245 | } |
| 246 | if (flag & FWRITE) { |
| 247 | selwakeup(&pti->pt_selw); |
| 248 | wakeup((caddr_t)&tp->t_rawq.c_cf); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /*ARGSUSED*/ |
| 253 | #ifdef __STDC__ |
| 254 | ptcopen(dev_t dev, int flag, int devtype, struct proc *p) |
| 255 | #else |
| 256 | ptcopen(dev, flag, devtype, p) |
| 257 | dev_t dev; |
| 258 | int flag, devtype; |
| 259 | struct proc *p; |
| 260 | #endif |
| 261 | { |
| 262 | register struct tty *tp; |
| 263 | struct pt_ioctl *pti; |
| 264 | |
| 265 | if (minor(dev) >= npty) |
| 266 | return (ENXIO); |
| 267 | tp = &pt_tty[minor(dev)]; |
| 268 | if (tp->t_oproc) |
| 269 | return (EIO); |
| 270 | tp->t_oproc = ptsstart; |
| 271 | #ifdef sun4c |
| 272 | tp->t_stop = ptsstop; |
| 273 | #endif |
| 274 | (void)(*linesw[tp->t_line].l_modem)(tp, 1); |
| 275 | tp->t_lflag &= ~EXTPROC; |
| 276 | pti = &pt_ioctl[minor(dev)]; |
| 277 | pti->pt_flags = 0; |
| 278 | pti->pt_send = 0; |
| 279 | pti->pt_ucntl = 0; |
| 280 | return (0); |
| 281 | } |
| 282 | |
| 283 | ptcclose(dev) |
| 284 | dev_t dev; |
| 285 | { |
| 286 | register struct tty *tp; |
| 287 | |
| 288 | tp = &pt_tty[minor(dev)]; |
| 289 | (void)(*linesw[tp->t_line].l_modem)(tp, 0); |
| 290 | tp->t_state &= ~TS_CARR_ON; |
| 291 | tp->t_oproc = 0; /* mark closed */ |
| 292 | tp->t_session = 0; |
| 293 | return (0); |
| 294 | return (0); |
| 295 | } |
| 296 | |
| 297 | ptcread(dev, uio, flag) |
| 298 | dev_t dev; |
| 299 | struct uio *uio; |
| 300 | int flag; |
| 301 | { |
| 302 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 303 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 304 | char buf[BUFSIZ]; |
| 305 | int error = 0, cc; |
| 306 | |
| 307 | /* |
| 308 | * We want to block until the slave |
| 309 | * is open, and there's something to read; |
| 310 | * but if we lost the slave or we're NBIO, |
| 311 | * then return the appropriate error instead. |
| 312 | */ |
| 313 | for (;;) { |
| 314 | if (tp->t_state&TS_ISOPEN) { |
| 315 | if (pti->pt_flags&PF_PKT && pti->pt_send) { |
| 316 | error = ureadc((int)pti->pt_send, uio); |
| 317 | if (error) |
| 318 | return (error); |
| 319 | if (pti->pt_send & TIOCPKT_IOCTL) { |
| 320 | cc = min(uio->uio_resid, |
| 321 | sizeof(tp->t_termios)); |
| 322 | uiomove(&tp->t_termios, cc, uio); |
| 323 | } |
| 324 | pti->pt_send = 0; |
| 325 | return (0); |
| 326 | } |
| 327 | if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { |
| 328 | error = ureadc((int)pti->pt_ucntl, uio); |
| 329 | if (error) |
| 330 | return (error); |
| 331 | pti->pt_ucntl = 0; |
| 332 | return (0); |
| 333 | } |
| 334 | if (pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc) { |
| 335 | if (uio->uio_resid < pti->pt_ioc.c_cc + 1) |
| 336 | return (E2BIG); |
| 337 | error = ureadc(TIOCPKT_TIOC, uio); |
| 338 | while (error == 0 && pti->pt_ioc.c_cc > 0) { |
| 339 | cc = q_to_b(&pti->pt_ioc, buf, |
| 340 | MIN(pti->pt_ioc.c_cc, BUFSIZ)); |
| 341 | if (cc <= 0) /* impossible? */ |
| 342 | break; |
| 343 | error = uiomove(buf, cc, UIO_READ, uio); |
| 344 | } |
| 345 | return (error); |
| 346 | } |
| 347 | if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) |
| 348 | break; |
| 349 | } |
| 350 | if ((tp->t_state&TS_CARR_ON) == 0) |
| 351 | return (0); /* EOF */ |
| 352 | if (flag & IO_NDELAY) |
| 353 | return (EWOULDBLOCK); |
| 354 | if (error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH, |
| 355 | ttyin, 0)) |
| 356 | return (error); |
| 357 | } |
| 358 | if (pti->pt_flags & (PF_PKT|PF_UCNTL|PF_TIOC)) |
| 359 | error = ureadc(0, uio); |
| 360 | while (uio->uio_resid > 0 && error == 0) { |
| 361 | cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ)); |
| 362 | if (cc <= 0) |
| 363 | break; |
| 364 | error = uiomove(buf, cc, uio); |
| 365 | } |
| 366 | if (tp->t_outq.c_cc <= TTLOWAT(tp) && !(pti->pt_flags & PF_BLOCK)) |
| 367 | ptswake(tp); |
| 368 | return (error); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | ptswake(tp) |
| 373 | register struct tty *tp; |
| 374 | { |
| 375 | if (tp->t_state&TS_ASLEEP) { |
| 376 | tp->t_state &= ~TS_ASLEEP; |
| 377 | wakeup((caddr_t)&tp->t_outq); |
| 378 | } |
| 379 | if (tp->t_wsel) { |
| 380 | selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); |
| 381 | tp->t_wsel = 0; |
| 382 | tp->t_state &= ~TS_WCOLL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | ptsstop(tp, flush) |
| 387 | register struct tty *tp; |
| 388 | int flush; |
| 389 | { |
| 390 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; |
| 391 | int flag; |
| 392 | |
| 393 | /* note: FLUSHREAD and FLUSHWRITE already ok */ |
| 394 | if (flush == 0) { |
| 395 | flush = TIOCPKT_STOP; |
| 396 | pti->pt_flags |= PF_STOPPED; |
| 397 | } else |
| 398 | pti->pt_flags &= ~PF_STOPPED; |
| 399 | pti->pt_send |= flush; |
| 400 | /* change of perspective */ |
| 401 | flag = 0; |
| 402 | if (flush & FREAD) |
| 403 | flag |= FWRITE; |
| 404 | if (flush & FWRITE) |
| 405 | flag |= FREAD; |
| 406 | ptcwakeup(tp, flag); |
| 407 | } |
| 408 | |
| 409 | ptcselect(dev, rw, p) |
| 410 | dev_t dev; |
| 411 | int rw; |
| 412 | struct proc *p; |
| 413 | { |
| 414 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 415 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 416 | int s; |
| 417 | |
| 418 | if ((tp->t_state&TS_CARR_ON) == 0) |
| 419 | return (1); |
| 420 | switch (rw) { |
| 421 | |
| 422 | case FREAD: |
| 423 | /* |
| 424 | * Need to block timeouts (ttrstart). |
| 425 | */ |
| 426 | s = spltty(); |
| 427 | if ((tp->t_state&TS_ISOPEN) && |
| 428 | tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { |
| 429 | splx(s); |
| 430 | return (1); |
| 431 | } |
| 432 | splx(s); |
| 433 | /* FALLTHROUGH */ |
| 434 | |
| 435 | case 0: /* exceptional */ |
| 436 | if ((tp->t_state&TS_ISOPEN) && |
| 437 | (pti->pt_flags&PF_PKT && pti->pt_send || |
| 438 | pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc || |
| 439 | pti->pt_flags&PF_UCNTL && pti->pt_ucntl)) |
| 440 | return (1); |
| 441 | selrecord(p, &pti->pt_selr); |
| 442 | break; |
| 443 | |
| 444 | |
| 445 | case FWRITE: |
| 446 | if (tp->t_state&TS_ISOPEN) { |
| 447 | if (pti->pt_flags & PF_REMOTE) { |
| 448 | if (tp->t_canq.c_cc == 0) |
| 449 | return (1); |
| 450 | } else { |
| 451 | if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) |
| 452 | return (1); |
| 453 | if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) |
| 454 | return (1); |
| 455 | } |
| 456 | } |
| 457 | selrecord(p, &pti->pt_selw); |
| 458 | break; |
| 459 | |
| 460 | } |
| 461 | return (0); |
| 462 | } |
| 463 | |
| 464 | ptcwrite(dev, uio, flag) |
| 465 | dev_t dev; |
| 466 | register struct uio *uio; |
| 467 | int flag; |
| 468 | { |
| 469 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 470 | register u_char *cp; |
| 471 | register int cc = 0; |
| 472 | u_char locbuf[BUFSIZ]; |
| 473 | int cnt = 0; |
| 474 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 475 | int error = 0; |
| 476 | |
| 477 | again: |
| 478 | if ((tp->t_state&TS_ISOPEN) == 0) |
| 479 | goto block; |
| 480 | if (pti->pt_flags & PF_REMOTE) { |
| 481 | if (tp->t_canq.c_cc) |
| 482 | goto block; |
| 483 | while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) { |
| 484 | if (cc == 0) { |
| 485 | cc = min(uio->uio_resid, BUFSIZ); |
| 486 | cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc); |
| 487 | cp = locbuf; |
| 488 | error = uiomove((caddr_t)cp, cc, uio); |
| 489 | if (error) |
| 490 | return (error); |
| 491 | /* check again for safety */ |
| 492 | if ((tp->t_state&TS_ISOPEN) == 0) |
| 493 | return (EIO); |
| 494 | } |
| 495 | if (cc) |
| 496 | (void) b_to_q((char *)cp, cc, &tp->t_canq); |
| 497 | cc = 0; |
| 498 | } |
| 499 | (void) putc(0, &tp->t_canq); |
| 500 | ttwakeup(tp); |
| 501 | wakeup((caddr_t)&tp->t_canq); |
| 502 | return (0); |
| 503 | } |
| 504 | while (uio->uio_resid > 0) { |
| 505 | if (cc == 0) { |
| 506 | cc = min(uio->uio_resid, BUFSIZ); |
| 507 | cp = locbuf; |
| 508 | error = uiomove((caddr_t)cp, cc, uio); |
| 509 | if (error) |
| 510 | return (error); |
| 511 | /* check again for safety */ |
| 512 | if ((tp->t_state&TS_ISOPEN) == 0) |
| 513 | return (EIO); |
| 514 | } |
| 515 | while (cc > 0) { |
| 516 | if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && |
| 517 | (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { |
| 518 | wakeup((caddr_t)&tp->t_rawq); |
| 519 | goto block; |
| 520 | } |
| 521 | (*linesw[tp->t_line].l_rint)(*cp++, tp); |
| 522 | cnt++; |
| 523 | cc--; |
| 524 | } |
| 525 | cc = 0; |
| 526 | } |
| 527 | return (0); |
| 528 | block: |
| 529 | /* |
| 530 | * Come here to wait for slave to open, for space |
| 531 | * in outq, or space in rawq. |
| 532 | */ |
| 533 | if ((tp->t_state&TS_CARR_ON) == 0) |
| 534 | return (EIO); |
| 535 | if (flag & IO_NDELAY) { |
| 536 | /* adjust for data copied in but not written */ |
| 537 | uio->uio_resid += cc; |
| 538 | if (cnt == 0) |
| 539 | return (EWOULDBLOCK); |
| 540 | return (0); |
| 541 | } |
| 542 | if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH, |
| 543 | ttyout, 0)) { |
| 544 | /* adjust for data copied in but not written */ |
| 545 | uio->uio_resid += cc; |
| 546 | return (error); |
| 547 | } |
| 548 | goto again; |
| 549 | } |
| 550 | |
| 551 | /*ARGSUSED*/ |
| 552 | ptyioctl(dev, cmd, data, flag, p) |
| 553 | dev_t dev; |
| 554 | int cmd; |
| 555 | caddr_t data; |
| 556 | int flag; |
| 557 | struct proc *p; |
| 558 | { |
| 559 | register struct tty *tp = &pt_tty[minor(dev)]; |
| 560 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
| 561 | register u_char *cc = tp->t_cc; |
| 562 | int stop, error; |
| 563 | |
| 564 | /* |
| 565 | * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. |
| 566 | * ttywflush(tp) will hang if there are characters in the outq. |
| 567 | */ |
| 568 | if (cmd == TIOCEXT) { |
| 569 | /* |
| 570 | * When the EXTPROC bit is being toggled, we need |
| 571 | * to send an TIOCPKT_IOCTL if the packet driver |
| 572 | * is turned on. |
| 573 | */ |
| 574 | if (*(int *)data) { |
| 575 | if (pti->pt_flags & PF_PKT) { |
| 576 | pti->pt_send |= TIOCPKT_IOCTL; |
| 577 | ptcwakeup(tp); |
| 578 | } |
| 579 | tp->t_lflag |= EXTPROC; |
| 580 | } else { |
| 581 | if ((tp->t_state & EXTPROC) && |
| 582 | (pti->pt_flags & PF_PKT)) { |
| 583 | pti->pt_send |= TIOCPKT_IOCTL; |
| 584 | ptcwakeup(tp); |
| 585 | } |
| 586 | tp->t_lflag &= ~EXTPROC; |
| 587 | } |
| 588 | return(0); |
| 589 | } else |
| 590 | if (cdevsw[major(dev)].d_open == ptcopen) { |
| 591 | if ((cmd & 0xffff) == (TIOCIOANS(0) & 0xffff)) { |
| 592 | if (!(pti->pt_flags & PF_LIOC) || pti->pt_ioc.c_cc) |
| 593 | return (EINVAL); |
| 594 | (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc); |
| 595 | wakeup((caddr_t)&pti->pt_ioc); |
| 596 | return (0); |
| 597 | } |
| 598 | switch (cmd) { |
| 599 | |
| 600 | case TIOCGPGRP: |
| 601 | /* |
| 602 | * We aviod calling ttioctl on the controller since, |
| 603 | * in that case, tp must be the controlling terminal. |
| 604 | */ |
| 605 | *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; |
| 606 | return (0); |
| 607 | |
| 608 | case TIOCPKT: |
| 609 | if (*(int *)data) { |
| 610 | if (pti->pt_flags & PF_UCNTL) |
| 611 | return (EINVAL); |
| 612 | pti->pt_flags |= PF_PKT; |
| 613 | } else |
| 614 | pti->pt_flags &= ~PF_PKT; |
| 615 | return (0); |
| 616 | |
| 617 | case TIOCUCNTL: |
| 618 | if (*(int *)data) { |
| 619 | if (pti->pt_flags & PF_PKT) |
| 620 | return (EINVAL); |
| 621 | pti->pt_flags |= PF_UCNTL; |
| 622 | } else |
| 623 | pti->pt_flags &= ~PF_UCNTL; |
| 624 | return (0); |
| 625 | |
| 626 | case TIOCTIOC: |
| 627 | if (*(int *)data) { |
| 628 | if (pti->pt_flags & PF_UCNTL) |
| 629 | return (EINVAL); |
| 630 | pti->pt_flags |= PF_TIOC; |
| 631 | } else { |
| 632 | pti->pt_flags &= ~(PF_TIOC|PF_LIOC|PF_WIOC); |
| 633 | while (pti->pt_ioc.c_cc) |
| 634 | (void) getc(&pti->pt_ioc); |
| 635 | wakeup((caddr_t)&pti->pt_ioc); |
| 636 | } |
| 637 | return (0); |
| 638 | |
| 639 | case TIOCBLK: |
| 640 | if (*(int *)data) |
| 641 | pti->pt_flags |= PF_BLOCK; |
| 642 | else { |
| 643 | if (pti->pt_flags & PF_OWAIT) |
| 644 | wakeup((caddr_t)pti + 1); |
| 645 | pti->pt_flags &= ~(PF_BLOCK|PF_OWAIT); |
| 646 | ptswake(tp); |
| 647 | } |
| 648 | return (0); |
| 649 | |
| 650 | case TIOCREMOTE: |
| 651 | if (*(int *)data) |
| 652 | pti->pt_flags |= PF_REMOTE; |
| 653 | else |
| 654 | pti->pt_flags &= ~PF_REMOTE; |
| 655 | ttyflush(tp, FREAD|FWRITE); |
| 656 | return (0); |
| 657 | |
| 658 | #ifdef COMPAT_43 |
| 659 | case FIONREAD: |
| 660 | *(int *)data = tp->t_outq.c_cc; |
| 661 | return (0); |
| 662 | |
| 663 | case TIOCSETP: |
| 664 | case TIOCSETN: |
| 665 | #endif |
| 666 | case TIOCSETD: |
| 667 | case TIOCSETA: |
| 668 | case TIOCSETAW: |
| 669 | case TIOCSETAF: |
| 670 | ndflush(&tp->t_outq, tp->t_outq.c_cc); |
| 671 | break; |
| 672 | |
| 673 | case TIOCSIG: |
| 674 | if (*(unsigned int *)data >= NSIG) |
| 675 | return(EINVAL); |
| 676 | if ((tp->t_lflag&NOFLSH) == 0) |
| 677 | ttyflush(tp, FREAD|FWRITE); |
| 678 | pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); |
| 679 | if ((*(unsigned int *)data == SIGINFO) && |
| 680 | ((tp->t_lflag&NOKERNINFO) == 0)) |
| 681 | ttyinfo(tp); |
| 682 | return(0); |
| 683 | } |
| 684 | } else if (pti->pt_flags & PF_TIOC) { |
| 685 | while (pti->pt_flags & PF_LIOC) { |
| 686 | pti->pt_flags |= PF_WIOC; |
| 687 | switch (tsleep((caddr_t)&pti->pt_flags,TTIPRI-1,5*hz)) { |
| 688 | case TS_OK: |
| 689 | continue; |
| 690 | case TS_SIG: |
| 691 | case TS_TIME: |
| 692 | return (EBUSY); |
| 693 | } |
| 694 | } |
| 695 | pti->pt_flags |= PF_LIOC | PF_BLOCK; |
| 696 | while (pti->pt_ioc.c_cc) |
| 697 | (void) getc(&pti->pt_ioc); |
| 698 | (void) b_to_q(&cmd, sizeof cmd, &pti->pt_ioc); |
| 699 | if (cmd & IOC_IN) |
| 700 | (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc); |
| 701 | ptcwakeup(tp, FREAD); |
| 702 | switch (tsleep((caddr_t)&pti->pt_ioc, TTIPRI-1, 5*hz)) { |
| 703 | case TS_SIG: |
| 704 | case TS_TIME: |
| 705 | while (pti->pt_ioc.c_cc) |
| 706 | (void) getc(&pti->pt_ioc); |
| 707 | if (pti->pt_flags & PF_WIOC) |
| 708 | wakeup((caddr_t)&pti->pt_flags); |
| 709 | if (pti->pt_flags & PF_OWAIT) |
| 710 | wakeup((caddr_t)pti + 1); |
| 711 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); |
| 712 | ptswake(tp); |
| 713 | return (EBUSY); |
| 714 | case TS_OK: |
| 715 | break; |
| 716 | } |
| 717 | if (pti->pt_ioc.c_cc == 0) { |
| 718 | if (pti->pt_flags & PF_WIOC) |
| 719 | wakeup((caddr_t)&pti->pt_flags); |
| 720 | if (pti->pt_flags & PF_OWAIT) |
| 721 | wakeup((caddr_t)pti + 1); |
| 722 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); |
| 723 | ptswake(tp); |
| 724 | goto doioctl; |
| 725 | } |
| 726 | if (q_to_b(&pti->pt_ioc, &error, sizeof error) != sizeof error) |
| 727 | error = EINVAL; |
| 728 | if (error == 0 && cmd & IOC_OUT) { |
| 729 | if (IOCPARM_LEN(cmd) != pti->pt_ioc.c_cc) |
| 730 | error = EINVAL; |
| 731 | else |
| 732 | (void) q_to_b(&pti->pt_ioc, data, |
| 733 | pti->pt_ioc.c_cc); |
| 734 | } |
| 735 | while (pti->pt_ioc.c_cc) |
| 736 | (void) getc(&pti->pt_ioc); |
| 737 | if (pti->pt_flags & PF_WIOC) |
| 738 | wakeup((caddr_t)&pti->pt_flags); |
| 739 | if (pti->pt_flags & PF_OWAIT) |
| 740 | wakeup((caddr_t)pti + 1); |
| 741 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); |
| 742 | ptswake(tp); |
| 743 | return (error); |
| 744 | } |
| 745 | |
| 746 | doioctl: |
| 747 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); |
| 748 | if (error < 0) |
| 749 | error = ttioctl(tp, cmd, data, flag); |
| 750 | |
| 751 | if (error < 0) { |
| 752 | if (pti->pt_flags & PF_UCNTL && |
| 753 | (cmd & ~0xff) == UIOCCMD(0)) { |
| 754 | if (cmd & 0xff) { |
| 755 | pti->pt_ucntl = (u_char)cmd; |
| 756 | ptcwakeup(tp, FREAD); |
| 757 | } |
| 758 | return (0); |
| 759 | } |
| 760 | error = ENOTTY; |
| 761 | } |
| 762 | /* |
| 763 | * If external processing and packet mode send ioctl packet. |
| 764 | */ |
| 765 | if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { |
| 766 | switch(cmd) { |
| 767 | case TIOCSETA: |
| 768 | case TIOCSETAW: |
| 769 | case TIOCSETAF: |
| 770 | #ifdef COMPAT_43 |
| 771 | case TIOCSETP: |
| 772 | case TIOCSETN: |
| 773 | #endif |
| 774 | #if defined(COMPAT_43) || defined(COMPAT_SUNOS) |
| 775 | case TIOCSETC: |
| 776 | case TIOCSLTC: |
| 777 | case TIOCLBIS: |
| 778 | case TIOCLBIC: |
| 779 | case TIOCLSET: |
| 780 | #endif |
| 781 | pti->pt_send |= TIOCPKT_IOCTL; |
| 782 | default: |
| 783 | break; |
| 784 | } |
| 785 | } |
| 786 | stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) |
| 787 | && CCEQ(cc[VSTART], CTRL('q')); |
| 788 | if (pti->pt_flags & PF_NOSTOP) { |
| 789 | if (stop) { |
| 790 | pti->pt_send &= ~TIOCPKT_NOSTOP; |
| 791 | pti->pt_send |= TIOCPKT_DOSTOP; |
| 792 | pti->pt_flags &= ~PF_NOSTOP; |
| 793 | ptcwakeup(tp, FREAD); |
| 794 | } |
| 795 | } else { |
| 796 | if (!stop) { |
| 797 | pti->pt_send &= ~TIOCPKT_DOSTOP; |
| 798 | pti->pt_send |= TIOCPKT_NOSTOP; |
| 799 | pti->pt_flags |= PF_NOSTOP; |
| 800 | ptcwakeup(tp, FREAD); |
| 801 | } |
| 802 | } |
| 803 | return (error); |
| 804 | } |