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