1 /*- 2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Portions of this software were developed under sponsorship from Snow 6 * B.V., the Netherlands. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_tty.h" 34 35 /* Add compatibility bits for FreeBSD. */ 36 #define PTS_COMPAT 37 #ifdef DEV_PTY 38 /* Add /dev/ptyXX compat bits. */ 39 #define PTS_EXTERNAL 40 #endif /* DEV_PTY */ 41 /* Add bits to make Linux binaries work. */ 42 #define PTS_LINUX 43 44 #include <sys/param.h> 45 #include <sys/lock.h> 46 #include <sys/condvar.h> 47 #include <sys/conf.h> 48 #include <sys/fcntl.h> 49 #include <sys/file.h> 50 #include <sys/filedesc.h> 51 #include <sys/filio.h> 52 #include <sys/kernel.h> 53 #include <sys/malloc.h> 54 #include <sys/poll.h> 55 #include <sys/proc.h> 56 #include <sys/resourcevar.h> 57 #include <sys/serial.h> 58 #include <sys/stat.h> 59 #include <sys/syscall.h> 60 #include <sys/syscallsubr.h> 61 #include <sys/sysent.h> 62 #include <sys/sysproto.h> 63 #include <sys/systm.h> 64 #include <sys/tty.h> 65 #include <sys/ttycom.h> 66 67 #include <machine/stdarg.h> 68 69 static struct unrhdr *pts_pool; 70 #define MAXPTSDEVS 999 71 72 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device"); 73 74 /* 75 * Per-PTS structure. 76 * 77 * List of locks 78 * (t) locked by tty_lock() 79 * (c) const until freeing 80 */ 81 struct pts_softc { 82 int pts_unit; /* (c) Device unit number. */ 83 unsigned int pts_flags; /* (t) Device flags. */ 84 #define PTS_PKT 0x1 /* Packet mode. */ 85 #define PTS_FINISHED 0x2 /* Return errors on read()/write(). */ 86 char pts_pkt; /* (t) Unread packet mode data. */ 87 88 struct cv pts_inwait; /* (t) Blocking write() on master. */ 89 struct selinfo pts_inpoll; /* (t) Select queue for write(). */ 90 struct cv pts_outwait; /* (t) Blocking read() on master. */ 91 struct selinfo pts_outpoll; /* (t) Select queue for read(). */ 92 93 #ifdef PTS_EXTERNAL 94 struct cdev *pts_cdev; /* (c) Master device node. */ 95 #endif /* PTS_EXTERNAL */ 96 97 struct uidinfo *pts_uidinfo; /* (c) Resource limit. */ 98 }; 99 100 /* 101 * Controller-side file operations. 102 */ 103 104 static int 105 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred, 106 int flags, struct thread *td) 107 { 108 struct tty *tp = fp->f_data; 109 struct pts_softc *psc = tty_softc(tp); 110 int error = 0; 111 char pkt; 112 113 if (uio->uio_resid == 0) 114 return (0); 115 116 tty_lock(tp); 117 118 for (;;) { 119 /* 120 * Implement packet mode. When packet mode is turned on, 121 * the first byte contains a bitmask of events that 122 * occured (start, stop, flush, window size, etc). 123 */ 124 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) { 125 pkt = psc->pts_pkt; 126 psc->pts_pkt = 0; 127 tty_unlock(tp); 128 129 error = ureadc(pkt, uio); 130 return (error); 131 } 132 133 /* 134 * Transmit regular data. 135 * 136 * XXX: We shouldn't use ttydisc_getc_poll()! Even 137 * though in this implementation, there is likely going 138 * to be data, we should just call ttydisc_getc_uio() 139 * and use its return value to sleep. 140 */ 141 if (ttydisc_getc_poll(tp)) { 142 if (psc->pts_flags & PTS_PKT) { 143 /* 144 * XXX: Small race. Fortunately PTY 145 * consumers aren't multithreaded. 146 */ 147 148 tty_unlock(tp); 149 error = ureadc(TIOCPKT_DATA, uio); 150 if (error) 151 return (error); 152 tty_lock(tp); 153 } 154 155 error = ttydisc_getc_uio(tp, uio); 156 break; 157 } 158 159 /* Maybe the device isn't used anyway. */ 160 if (psc->pts_flags & PTS_FINISHED) 161 break; 162 163 /* Wait for more data. */ 164 if (fp->f_flag & O_NONBLOCK) { 165 error = EWOULDBLOCK; 166 break; 167 } 168 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx); 169 if (error != 0) 170 break; 171 } 172 173 tty_unlock(tp); 174 175 return (error); 176 } 177 178 static int 179 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred, 180 int flags, struct thread *td) 181 { 182 struct tty *tp = fp->f_data; 183 struct pts_softc *psc = tty_softc(tp); 184 char ib[256], *ibstart; 185 size_t iblen, rintlen; 186 int error = 0; 187 188 if (uio->uio_resid == 0) 189 return (0); 190 191 for (;;) { 192 ibstart = ib; 193 iblen = MIN(uio->uio_resid, sizeof ib); 194 error = uiomove(ib, iblen, uio); 195 196 tty_lock(tp); 197 if (error != 0) 198 goto done; 199 200 /* 201 * When possible, avoid the slow path. rint_bypass() 202 * copies all input to the input queue at once. 203 */ 204 MPASS(iblen > 0); 205 do { 206 if (ttydisc_can_bypass(tp)) { 207 /* Store data at once. */ 208 rintlen = ttydisc_rint_bypass(tp, 209 ibstart, iblen); 210 ibstart += rintlen; 211 iblen -= rintlen; 212 213 if (iblen == 0) { 214 /* All data written. */ 215 break; 216 } 217 } else { 218 error = ttydisc_rint(tp, *ibstart, 0); 219 if (error == 0) { 220 /* Character stored successfully. */ 221 ibstart++; 222 iblen--; 223 continue; 224 } 225 } 226 227 /* Maybe the device isn't used anyway. */ 228 if (psc->pts_flags & PTS_FINISHED) { 229 error = EIO; 230 goto done; 231 } 232 233 /* Wait for more data. */ 234 if (fp->f_flag & O_NONBLOCK) { 235 error = EWOULDBLOCK; 236 goto done; 237 } 238 239 /* Wake up users on the slave side. */ 240 ttydisc_rint_done(tp); 241 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx); 242 if (error != 0) 243 goto done; 244 } while (iblen > 0); 245 246 if (uio->uio_resid == 0) 247 break; 248 tty_unlock(tp); 249 } 250 251 done: ttydisc_rint_done(tp); 252 tty_unlock(tp); 253 return (error); 254 } 255 256 static int 257 ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred, 258 struct thread *td) 259 { 260 261 return (EINVAL); 262 } 263 264 static int 265 ptsdev_ioctl(struct file *fp, u_long cmd, void *data, 266 struct ucred *active_cred, struct thread *td) 267 { 268 struct tty *tp = fp->f_data; 269 struct pts_softc *psc = tty_softc(tp); 270 int error = 0, sig; 271 272 switch (cmd) { 273 case FIONBIO: 274 /* This device supports non-blocking operation. */ 275 return (0); 276 case FIONREAD: 277 tty_lock(tp); 278 if (psc->pts_flags & PTS_FINISHED) { 279 /* Force read() to be called. */ 280 *(int *)data = 1; 281 } else { 282 *(int *)data = ttydisc_getc_poll(tp); 283 } 284 tty_unlock(tp); 285 return (0); 286 case FIODGNAME: { 287 struct fiodgname_arg *fgn; 288 const char *p; 289 int i; 290 291 /* Reverse device name lookups, for ptsname() and ttyname(). */ 292 fgn = data; 293 #ifdef PTS_EXTERNAL 294 if (psc->pts_cdev != NULL) 295 p = devtoname(psc->pts_cdev); 296 else 297 #endif /* PTS_EXTERNAL */ 298 p = tty_devname(tp); 299 i = strlen(p) + 1; 300 if (i > fgn->len) 301 return (EINVAL); 302 return copyout(p, fgn->buf, i); 303 } 304 305 /* 306 * We need to implement TIOCGPGRP and TIOCGSID here again. When 307 * called on the pseudo-terminal master, it should not check if 308 * the terminal is the foreground terminal of the calling 309 * process. 310 * 311 * TIOCGETA is also implemented here. Various Linux PTY routines 312 * often call isatty(), which is implemented by tcgetattr(). 313 */ 314 #ifdef PTS_LINUX 315 case TIOCGETA: 316 /* Obtain terminal flags through tcgetattr(). */ 317 tty_lock(tp); 318 bcopy(&tp->t_termios, data, sizeof(struct termios)); 319 tty_unlock(tp); 320 return (0); 321 #endif /* PTS_LINUX */ 322 case TIOCSETAF: 323 case TIOCSETAW: 324 /* 325 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and 326 * TCSADRAIN into something different. If an application would 327 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may 328 * deadlock waiting for all data to be read. 329 */ 330 cmd = TIOCSETA; 331 break; 332 #if defined(PTS_COMPAT) || defined(PTS_LINUX) 333 case TIOCGPTN: 334 /* 335 * Get the device unit number. 336 */ 337 if (psc->pts_unit < 0) 338 return (ENOTTY); 339 *(unsigned int *)data = psc->pts_unit; 340 return (0); 341 #endif /* PTS_COMPAT || PTS_LINUX */ 342 case TIOCGPGRP: 343 /* Get the foreground process group ID. */ 344 tty_lock(tp); 345 if (tp->t_pgrp != NULL) 346 *(int *)data = tp->t_pgrp->pg_id; 347 else 348 *(int *)data = NO_PID; 349 tty_unlock(tp); 350 return (0); 351 case TIOCGSID: 352 /* Get the session leader process ID. */ 353 tty_lock(tp); 354 if (tp->t_session == NULL) 355 error = ENOTTY; 356 else 357 *(int *)data = tp->t_session->s_sid; 358 tty_unlock(tp); 359 return (error); 360 case TIOCPTMASTER: 361 /* Yes, we are a pseudo-terminal master. */ 362 return (0); 363 case TIOCSIG: 364 /* Signal the foreground process group. */ 365 sig = *(int *)data; 366 if (sig < 1 || sig >= NSIG) 367 return (EINVAL); 368 369 tty_lock(tp); 370 tty_signal_pgrp(tp, sig); 371 tty_unlock(tp); 372 return (0); 373 case TIOCPKT: 374 /* Enable/disable packet mode. */ 375 tty_lock(tp); 376 if (*(int *)data) 377 psc->pts_flags |= PTS_PKT; 378 else 379 psc->pts_flags &= ~PTS_PKT; 380 tty_unlock(tp); 381 return (0); 382 } 383 384 /* Just redirect this ioctl to the slave device. */ 385 tty_lock(tp); 386 error = tty_ioctl(tp, cmd, data, td); 387 tty_unlock(tp); 388 389 return (error); 390 } 391 392 static int 393 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred, 394 struct thread *td) 395 { 396 struct tty *tp = fp->f_data; 397 struct pts_softc *psc = tty_softc(tp); 398 int revents = 0; 399 400 tty_lock(tp); 401 402 if (psc->pts_flags & PTS_FINISHED) { 403 /* Slave device is not opened. */ 404 tty_unlock(tp); 405 return (events & 406 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 407 } 408 409 if (events & (POLLIN|POLLRDNORM)) { 410 /* See if we can getc something. */ 411 if (ttydisc_getc_poll(tp) || 412 (psc->pts_flags & PTS_PKT && psc->pts_pkt)) 413 revents |= events & (POLLIN|POLLRDNORM); 414 } 415 if (events & (POLLOUT|POLLWRNORM)) { 416 /* See if we can rint something. */ 417 if (ttydisc_rint_poll(tp)) 418 revents |= events & (POLLOUT|POLLWRNORM); 419 } 420 421 /* 422 * No need to check for POLLHUP here. This device cannot be used 423 * as a callout device, which means we always have a carrier, 424 * because the master is. 425 */ 426 427 if (revents == 0) { 428 /* 429 * This code might look misleading, but the naming of 430 * poll events on this side is the opposite of the slave 431 * device. 432 */ 433 if (events & (POLLIN|POLLRDNORM)) 434 selrecord(td, &psc->pts_outpoll); 435 if (events & (POLLOUT|POLLWRNORM)) 436 selrecord(td, &psc->pts_inpoll); 437 } 438 439 tty_unlock(tp); 440 441 return (revents); 442 } 443 444 /* 445 * kqueue support. 446 */ 447 448 static void 449 pts_kqops_read_detach(struct knote *kn) 450 { 451 struct file *fp = kn->kn_fp; 452 struct tty *tp = fp->f_data; 453 struct pts_softc *psc = tty_softc(tp); 454 455 knlist_remove(&psc->pts_outpoll.si_note, kn, 0); 456 } 457 458 static int 459 pts_kqops_read_event(struct knote *kn, long hint) 460 { 461 struct file *fp = kn->kn_fp; 462 struct tty *tp = fp->f_data; 463 struct pts_softc *psc = tty_softc(tp); 464 465 if (psc->pts_flags & PTS_FINISHED) { 466 kn->kn_flags |= EV_EOF; 467 return (1); 468 } else { 469 kn->kn_data = ttydisc_getc_poll(tp); 470 return (kn->kn_data > 0); 471 } 472 } 473 474 static void 475 pts_kqops_write_detach(struct knote *kn) 476 { 477 struct file *fp = kn->kn_fp; 478 struct tty *tp = fp->f_data; 479 struct pts_softc *psc = tty_softc(tp); 480 481 knlist_remove(&psc->pts_inpoll.si_note, kn, 0); 482 } 483 484 static int 485 pts_kqops_write_event(struct knote *kn, long hint) 486 { 487 struct file *fp = kn->kn_fp; 488 struct tty *tp = fp->f_data; 489 struct pts_softc *psc = tty_softc(tp); 490 491 if (psc->pts_flags & PTS_FINISHED) { 492 kn->kn_flags |= EV_EOF; 493 return (1); 494 } else { 495 kn->kn_data = ttydisc_rint_poll(tp); 496 return (kn->kn_data > 0); 497 } 498 } 499 500 static struct filterops pts_kqops_read = 501 { 1, NULL, pts_kqops_read_detach, pts_kqops_read_event }; 502 static struct filterops pts_kqops_write = 503 { 1, NULL, pts_kqops_write_detach, pts_kqops_write_event }; 504 505 static int 506 ptsdev_kqfilter(struct file *fp, struct knote *kn) 507 { 508 struct tty *tp = fp->f_data; 509 struct pts_softc *psc = tty_softc(tp); 510 int error = 0; 511 512 tty_lock(tp); 513 514 switch (kn->kn_filter) { 515 case EVFILT_READ: 516 kn->kn_fop = &pts_kqops_read; 517 knlist_add(&psc->pts_outpoll.si_note, kn, 1); 518 break; 519 case EVFILT_WRITE: 520 kn->kn_fop = &pts_kqops_write; 521 knlist_add(&psc->pts_inpoll.si_note, kn, 1); 522 break; 523 default: 524 error = EINVAL; 525 break; 526 } 527 528 tty_unlock(tp); 529 return (error); 530 } 531 532 static int 533 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, 534 struct thread *td) 535 { 536 struct tty *tp = fp->f_data; 537 #ifdef PTS_EXTERNAL 538 struct pts_softc *psc = tty_softc(tp); 539 #endif /* PTS_EXTERNAL */ 540 struct cdev *dev = tp->t_dev; 541 542 /* 543 * According to POSIX, we must implement an fstat(). This also 544 * makes this implementation compatible with Linux binaries, 545 * because Linux calls fstat() on the pseudo-terminal master to 546 * obtain st_rdev. 547 * 548 * XXX: POSIX also mentions we must fill in st_dev, but how? 549 */ 550 551 bzero(sb, sizeof *sb); 552 #ifdef PTS_EXTERNAL 553 if (psc->pts_cdev != NULL) 554 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev); 555 else 556 #endif /* PTS_EXTERNAL */ 557 sb->st_ino = sb->st_rdev = tty_udev(tp); 558 559 sb->st_atimespec = dev->si_atime; 560 sb->st_ctimespec = dev->si_ctime; 561 sb->st_mtimespec = dev->si_mtime; 562 sb->st_uid = dev->si_uid; 563 sb->st_gid = dev->si_gid; 564 sb->st_mode = dev->si_mode | S_IFCHR; 565 566 return (0); 567 } 568 569 static int 570 ptsdev_close(struct file *fp, struct thread *td) 571 { 572 struct tty *tp = fp->f_data; 573 574 /* Deallocate TTY device. */ 575 tty_lock(tp); 576 tty_rel_gone(tp); 577 578 return (0); 579 } 580 581 static struct fileops ptsdev_ops = { 582 .fo_read = ptsdev_read, 583 .fo_write = ptsdev_write, 584 .fo_truncate = ptsdev_truncate, 585 .fo_ioctl = ptsdev_ioctl, 586 .fo_poll = ptsdev_poll, 587 .fo_kqfilter = ptsdev_kqfilter, 588 .fo_stat = ptsdev_stat, 589 .fo_close = ptsdev_close, 590 .fo_flags = DFLAG_PASSABLE, 591 }; 592 593 /* 594 * Driver-side hooks. 595 */ 596 597 static void 598 ptsdrv_outwakeup(struct tty *tp) 599 { 600 struct pts_softc *psc = tty_softc(tp); 601 602 cv_broadcast(&psc->pts_outwait); 603 selwakeup(&psc->pts_outpoll); 604 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0); 605 } 606 607 static void 608 ptsdrv_inwakeup(struct tty *tp) 609 { 610 struct pts_softc *psc = tty_softc(tp); 611 612 cv_broadcast(&psc->pts_inwait); 613 selwakeup(&psc->pts_inpoll); 614 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0); 615 } 616 617 static int 618 ptsdrv_open(struct tty *tp) 619 { 620 struct pts_softc *psc = tty_softc(tp); 621 622 psc->pts_flags &= ~PTS_FINISHED; 623 624 return (0); 625 } 626 627 static void 628 ptsdrv_close(struct tty *tp) 629 { 630 struct pts_softc *psc = tty_softc(tp); 631 632 /* Wake up any blocked readers/writers. */ 633 psc->pts_flags |= PTS_FINISHED; 634 ptsdrv_outwakeup(tp); 635 ptsdrv_inwakeup(tp); 636 } 637 638 static void 639 ptsdrv_pktnotify(struct tty *tp, char event) 640 { 641 struct pts_softc *psc = tty_softc(tp); 642 643 /* 644 * Clear conflicting flags. 645 */ 646 647 switch (event) { 648 case TIOCPKT_STOP: 649 psc->pts_pkt &= ~TIOCPKT_START; 650 break; 651 case TIOCPKT_START: 652 psc->pts_pkt &= ~TIOCPKT_STOP; 653 break; 654 case TIOCPKT_NOSTOP: 655 psc->pts_pkt &= ~TIOCPKT_DOSTOP; 656 break; 657 case TIOCPKT_DOSTOP: 658 psc->pts_pkt &= ~TIOCPKT_NOSTOP; 659 break; 660 } 661 662 psc->pts_pkt |= event; 663 ptsdrv_outwakeup(tp); 664 } 665 666 static void 667 ptsdrv_free(void *softc) 668 { 669 struct pts_softc *psc = softc; 670 671 /* Make device number available again. */ 672 if (psc->pts_unit >= 0) 673 free_unr(pts_pool, psc->pts_unit); 674 675 chgptscnt(psc->pts_uidinfo, -1, 0); 676 uifree(psc->pts_uidinfo); 677 678 knlist_destroy(&psc->pts_inpoll.si_note); 679 knlist_destroy(&psc->pts_outpoll.si_note); 680 681 #ifdef PTS_EXTERNAL 682 /* Destroy master device as well. */ 683 if (psc->pts_cdev != NULL) 684 destroy_dev_sched(psc->pts_cdev); 685 #endif /* PTS_EXTERNAL */ 686 687 free(psc, M_PTS); 688 } 689 690 static struct ttydevsw pts_class = { 691 .tsw_flags = TF_NOPREFIX, 692 .tsw_outwakeup = ptsdrv_outwakeup, 693 .tsw_inwakeup = ptsdrv_inwakeup, 694 .tsw_open = ptsdrv_open, 695 .tsw_close = ptsdrv_close, 696 .tsw_pktnotify = ptsdrv_pktnotify, 697 .tsw_free = ptsdrv_free, 698 }; 699 700 static int 701 pts_alloc(int fflags, struct thread *td, struct file *fp) 702 { 703 int unit, ok; 704 struct tty *tp; 705 struct pts_softc *psc; 706 struct proc *p = td->td_proc; 707 struct uidinfo *uid = td->td_ucred->cr_ruidinfo; 708 709 /* Resource limiting. */ 710 PROC_LOCK(p); 711 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS)); 712 PROC_UNLOCK(p); 713 if (!ok) 714 return (EAGAIN); 715 716 /* Try to allocate a new pts unit number. */ 717 unit = alloc_unr(pts_pool); 718 if (unit < 0) { 719 chgptscnt(uid, -1, 0); 720 return (EAGAIN); 721 } 722 723 /* Allocate TTY and softc. */ 724 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO); 725 cv_init(&psc->pts_inwait, "pts inwait"); 726 cv_init(&psc->pts_outwait, "pts outwait"); 727 728 psc->pts_unit = unit; 729 psc->pts_uidinfo = uid; 730 uihold(uid); 731 732 tp = tty_alloc(&pts_class, psc, NULL); 733 knlist_init(&psc->pts_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 734 knlist_init(&psc->pts_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 735 736 /* Expose the slave device as well. */ 737 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit); 738 739 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops); 740 741 return (0); 742 } 743 744 #ifdef PTS_EXTERNAL 745 int 746 pts_alloc_external(int fflags, struct thread *td, struct file *fp, 747 struct cdev *dev, const char *name) 748 { 749 int ok; 750 struct tty *tp; 751 struct pts_softc *psc; 752 struct proc *p = td->td_proc; 753 struct uidinfo *uid = td->td_ucred->cr_ruidinfo; 754 755 /* Resource limiting. */ 756 PROC_LOCK(p); 757 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS)); 758 PROC_UNLOCK(p); 759 if (!ok) 760 return (EAGAIN); 761 762 /* Allocate TTY and softc. */ 763 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO); 764 cv_init(&psc->pts_inwait, "pts inwait"); 765 cv_init(&psc->pts_outwait, "pts outwait"); 766 767 psc->pts_unit = -1; 768 psc->pts_cdev = dev; 769 psc->pts_uidinfo = uid; 770 uihold(uid); 771 772 tp = tty_alloc(&pts_class, psc, NULL); 773 knlist_init(&psc->pts_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 774 knlist_init(&psc->pts_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL); 775 776 /* Expose the slave device as well. */ 777 tty_makedev(tp, td->td_ucred, "%s", name); 778 779 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops); 780 781 return (0); 782 } 783 #endif /* PTS_EXTERNAL */ 784 785 int 786 posix_openpt(struct thread *td, struct posix_openpt_args *uap) 787 { 788 int error, fd; 789 struct file *fp; 790 791 /* 792 * POSIX states it's unspecified when other flags are passed. We 793 * don't allow this. 794 */ 795 if (uap->flags & ~(O_RDWR|O_NOCTTY)) 796 return (EINVAL); 797 798 error = falloc(td, &fp, &fd); 799 if (error) 800 return (error); 801 802 /* Allocate the actual pseudo-TTY. */ 803 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp); 804 if (error != 0) { 805 fdclose(td->td_proc->p_fd, fp, fd, td); 806 return (error); 807 } 808 809 /* Pass it back to userspace. */ 810 td->td_retval[0] = fd; 811 fdrop(fp, td); 812 813 return (0); 814 } 815 816 #if defined(PTS_COMPAT) || defined(PTS_LINUX) 817 static int 818 ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp) 819 { 820 821 return (pts_alloc(fflags & (FREAD|FWRITE), td, fp)); 822 } 823 824 static struct cdevsw ptmx_cdevsw = { 825 .d_version = D_VERSION, 826 .d_fdopen = ptmx_fdopen, 827 .d_name = "ptmx", 828 }; 829 #endif /* PTS_COMPAT || PTS_LINUX */ 830 831 static void 832 pts_init(void *unused) 833 { 834 835 pts_pool = new_unrhdr(0, MAXPTSDEVS, NULL); 836 #if defined(PTS_COMPAT) || defined(PTS_LINUX) 837 make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx"); 838 #endif /* PTS_COMPAT || PTS_LINUX */ 839 } 840 841 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL); 842