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