sys/kern/tty_pty.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 | /* * (MPSAFE) * * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $ */ /* * Most functions here could use a separate lock to deal with concurrent * access to the 'pt's. */ /* * Pseudo-teletype Driver * (Actually two drivers, requiring two dev_ops structures) */ #include <sys/param.h> #include <sys/systm.h> #include <sys/uio.h> #include <sys/proc.h> #include <sys/caps.h> #include <sys/tty.h> #include <sys/ttydefaults.h> /* for TTYDEF_* */ #include <sys/conf.h> #include <sys/fcntl.h> #include <sys/kernel.h> #include <sys/vnode.h> #include <sys/signalvar.h> #include <sys/malloc.h> #include <sys/device.h> #include <sys/devfs.h> #include <sys/stat.h> #include <sys/sysctl.h> MALLOC_DEFINE(M_PTY, "ptys", "pty data structures"); static void ptsstart (struct tty *tp); static void ptsstop (struct tty *tp, int rw); static void ptsunhold (struct tty *tp); static void ptcwakeup (struct tty *tp, int flag); static int filt_ptcread (struct knote *kn, long hint); static void filt_ptcrdetach (struct knote *kn); static int filt_ptcwrite (struct knote *kn, long hint); static void filt_ptcwdetach (struct knote *kn); static d_open_t ptsopen; static d_close_t ptsclose; static d_read_t ptsread; static d_write_t ptswrite; static d_ioctl_t ptyioctl; static d_open_t ptcopen; static d_close_t ptcclose; static d_read_t ptcread; static d_write_t ptcwrite; static d_kqfilter_t ptckqfilter; DEVFS_DEFINE_CLONE_BITMAP(pty); static struct pt_ioctl **ptis; /* keep pti's intact */ static d_clone_t ptyclone; static int pty_debug_level = 0; static struct dev_ops pts98_ops = { { "pts98", 0, D_TTY | D_MPSAFE }, .d_open = ptsopen, .d_close = ptsclose, .d_read = ptsread, .d_write = ptswrite, .d_ioctl = ptyioctl, .d_kqfilter = ttykqfilter, .d_revoke = ttyrevoke }; static struct dev_ops ptc98_ops = { { "ptc98", 0, D_TTY | D_MASTER | D_MPSAFE }, .d_open = ptcopen, .d_close = ptcclose, .d_read = ptcread, .d_write = ptcwrite, .d_ioctl = ptyioctl, .d_kqfilter = ptckqfilter, .d_revoke = ttyrevoke }; static struct dev_ops ptc_ops = { { "ptc", 0, D_TTY | D_MASTER | D_MPSAFE }, .d_open = ptcopen, .d_close = ptcclose, .d_read = ptcread, .d_write = ptcwrite, .d_ioctl = ptyioctl, .d_kqfilter = ptckqfilter, .d_revoke = ttyrevoke }; #define BUFSIZ 100 /* Chunk size iomoved to/from user */ #define MAXPTYS 1000 /* Maximum cloneable ptys */ struct pt_ioctl { int pt_flags; int pt_refs; /* Structural references interlock S/MOPEN */ int pt_uminor; struct kqinfo pt_kqr, pt_kqw; u_char pt_send; u_char pt_ucntl; struct tty pt_tty; cdev_t devs, devc; struct prison *pt_prison; }; /* * pt_flags ptc state */ #define PF_PKT 0x0008 /* packet mode */ #define PF_STOPPED 0x0010 /* user told stopped */ #define PF_REMOTE 0x0020 /* remote and flow controlled input */ #define PF_NOSTOP 0x0040 #define PF_UCNTL 0x0080 /* user control mode */ #define PF_PTCSTATEMASK 0x00FF /* * pt_flags open state. Note that PF_SCLOSED is used to activate * read EOF on the ptc so it is only set after the slave has been * opened and then closed, and cleared again if the slave is opened * again. */ #define PF_UNIX98 0x0100 #define PF_SOPEN 0x0200 #define PF_MOPEN 0x0400 #define PF_SCLOSED 0x0800 #define PF_TERMINATED 0x8000 static int ptyclone(struct dev_clone_args *ap) { int unit; struct pt_ioctl *pti; /* * Limit the number of unix98 pty (slave) devices to 1000 for now. * * If this limit is reached, we don't clone and return an error * to devfs. */ unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), MAXPTYS); if (unit < 0) { ap->a_dev = NULL; return 1; } /* * pti structures must be persistent once allocated. */ if ((pti = ptis[unit]) == NULL) { lwkt_gettoken(&tty_token); pti = kmalloc(sizeof(*pti), M_PTY, M_WAITOK | M_ZERO); if (ptis[unit] == NULL) { ptis[unit] = pti; ttyinit(&pti->pt_tty); } else { kfree(pti, M_PTY); } lwkt_reltoken(&tty_token); } /* * The cloning bitmap should guarantee isolation during * initialization. */ pti->devc = make_only_dev(&ptc98_ops, unit, ap->a_cred->cr_ruid, 0, 0600, "ptm/%d", unit); pti->devs = make_dev(&pts98_ops, unit, ap->a_cred->cr_ruid, GID_TTY, 0620, "pts/%d", unit); ap->a_dev = pti->devc; pti->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */ pti->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */ pti->pt_tty.t_dev = pti->devs; pti->pt_flags = PF_UNIX98; pti->pt_uminor = unit; pti->devs->si_drv1 = pti->devc->si_drv1 = pti; pti->devs->si_tty = pti->devc->si_tty = &pti->pt_tty; ttyregister(&pti->pt_tty); return 0; } /* * pti_hold() prevents the pti from being destroyed due to a termination * while a pt*open() is blocked. * * This function returns non-zero if we cannot hold due to a termination * interlock. */ static int pti_hold(struct pt_ioctl *pti) { if (pti->pt_flags & PF_TERMINATED) return(ENXIO); ++pti->pt_refs; return(0); } /* * pti_done() releases the reference and checks to see if both sides have * been closed on a unix98 pty, allowing us to destroy the device and * release resources. * * We do not release resources on non-unix98 ptys. Those are left * statically allocated. */ static void pti_done(struct pt_ioctl *pti) { lwkt_gettoken(&pti->pt_tty.t_token); if (--pti->pt_refs == 0) { cdev_t dev; int uminor_no; /* * Only unix09 ptys are freed up (the pti structure itself * is never freed, regardless). */ if ((pti->pt_flags & PF_UNIX98) == 0) { lwkt_reltoken(&pti->pt_tty.t_token); return; } /* * Interlock open attempts against termination by setting * PF_TERMINATED. This allows us to block while cleaning * out the device infrastructure. * * Do not terminate the tty if it still has a session * association (t_refs). */ if ((pti->pt_flags & (PF_SOPEN|PF_MOPEN)) == 0 && pti->pt_tty.t_refs == 0) { pti->pt_flags |= PF_TERMINATED; uminor_no = pti->pt_uminor; if ((dev = pti->devs) != NULL) { dev->si_drv1 = NULL; pti->devs = NULL; destroy_dev(dev); } if ((dev = pti->devc) != NULL) { dev->si_drv1 = NULL; pti->devc = NULL; destroy_dev(dev); } ttyunregister(&pti->pt_tty); pti->pt_tty.t_dev = NULL; devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), uminor_no); /* pti structure remains intact */ } } lwkt_reltoken(&pti->pt_tty.t_token); } /*ARGSUSED*/ static int ptsopen(struct dev_open_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp; int error; struct pt_ioctl *pti; /* * The pti will already be assigned by the clone code or * pre-created if a non-unix 98 pty. If si_drv1 is NULL * we are somehow racing a unix98 termination. */ if (dev->si_drv1 == NULL) return(ENXIO); pti = dev->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); if (pti_hold(pti)) { lwkt_reltoken(&pti->pt_tty.t_token); return(ENXIO); } tp = dev->si_tty; /* * Reinit most of the tty state if it isn't open. Handle * exclusive access. */ if ((tp->t_state & TS_ISOPEN) == 0) { ttychars(tp); /* Set up default chars */ tp->t_iflag = TTYDEF_IFLAG; tp->t_oflag = TTYDEF_OFLAG; tp->t_lflag = TTYDEF_LFLAG; tp->t_cflag = TTYDEF_CFLAG; tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; } else if ((tp->t_state & TS_XCLUDE) && caps_priv_check(ap->a_cred, SYSCAP_RESTRICTEDROOT)) { pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return (EBUSY); } else if (pti->pt_prison != ap->a_cred->cr_prison) { pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return (EBUSY); } /* * If the ptc is already present this will connect us up. It * is unclear if this is actually needed. * * If neither side is open be sure to clear any left over * ZOMBIE state before continuing. */ if (tp->t_oproc) (void)(*linesw[tp->t_line].l_modem)(tp, 1); else if ((pti->pt_flags & PF_SOPEN) == 0) tp->t_state &= ~TS_ZOMBIE; /* * Wait for the carrier (ptc side) */ while ((tp->t_state & TS_CARR_ON) == 0) { if (ap->a_oflags & FNONBLOCK) break; error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0); if (error) { pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } } /* * Mark the tty open and mark the slave side as being open. */ error = (*linesw[tp->t_line].l_open)(dev, tp); if (error == 0) { pti->pt_flags |= PF_SOPEN; pti->pt_flags &= ~PF_SCLOSED; ptcwakeup(tp, FREAD|FWRITE); } pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } static int ptsclose(struct dev_close_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp; struct pt_ioctl *pti = dev->si_drv1; int err; lwkt_gettoken(&pti->pt_tty.t_token); if (pti_hold(pti)) panic("ptsclose on terminated pti"); /* * Disconnect the slave side */ tp = dev->si_tty; err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag); ptsstop(tp, FREAD|FWRITE); ttyclose(tp); /* clears t_state */ /* * Mark the pts side closed and signal the ptc. Do not mark the * tty a zombie... that is, allow the tty to be re-opened as long * as the ptc is still open. The ptc will read() EOFs until the * pts side is reopened or the ptc is closed. * * xterm() depends on this behavior as it will revoke() the pts * and then reopen it after the (unnecessary old code) chmod. */ pti->pt_flags &= ~PF_SOPEN; pti->pt_flags |= PF_SCLOSED; if (tp->t_oproc) ptcwakeup(tp, FREAD); pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return (err); } static int ptsread(struct dev_read_args *ap) { cdev_t dev = ap->a_head.a_dev; struct proc *p = curproc; struct tty *tp = dev->si_tty; struct pt_ioctl *pti = dev->si_drv1; struct lwp *lp; int error = 0; lp = curthread->td_lwp; lwkt_gettoken(&pti->pt_tty.t_token); again: if (pti->pt_flags & PF_REMOTE) { while (isbackground(p, tp)) { if (SIGISMEMBER(p->p_sigignore, SIGTTIN) || SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) || p->p_pgrp->pg_jobc == 0 || (p->p_flags & P_PPWAIT)) { lwkt_reltoken(&pti->pt_tty.t_token); return (EIO); } pgsignal(p->p_pgrp, SIGTTIN, 1); error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0); if (error) { lwkt_reltoken(&pti->pt_tty.t_token); return (error); } } if (tp->t_canq.c_cc == 0) { if (ap->a_ioflag & IO_NDELAY) { lwkt_reltoken(&pti->pt_tty.t_token); return (EWOULDBLOCK); } error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH, "ptsin", 0); if (error) { lwkt_reltoken(&pti->pt_tty.t_token); return (error); } goto again; } while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0) if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) { error = EFAULT; break; } if (tp->t_canq.c_cc == 1) clist_getc(&tp->t_canq); if (tp->t_canq.c_cc) { lwkt_reltoken(&pti->pt_tty.t_token); return (error); } } else if (tp->t_oproc) error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag); ptcwakeup(tp, FWRITE); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } /* * Write to pseudo-tty. * Wakeups of controlling tty will happen * indirectly, when tty driver calls ptsstart. */ static int ptswrite(struct dev_write_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp; int ret; tp = dev->si_tty; lwkt_gettoken(&tp->t_token); if (tp->t_oproc == NULL) { lwkt_reltoken(&tp->t_token); return (EIO); } ret = ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag)); lwkt_reltoken(&tp->t_token); return ret; } /* * Start output on pseudo-tty. * Wake up process selecting or sleeping for input from controlling tty. */ static void ptsstart(struct tty *tp) { struct pt_ioctl *pti = tp->t_dev->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); if (tp->t_state & TS_TTSTOP) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return; } if (pti) { if (pti->pt_flags & PF_STOPPED) { pti->pt_flags &= ~PF_STOPPED; pti->pt_send = TIOCPKT_START; } } ptcwakeup(tp, FREAD); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); } /* * NOTE: Must be called with tp->t_token held */ static void ptcwakeup(struct tty *tp, int flag) { if (flag & FREAD) { wakeup(TSA_PTC_READ(tp)); KNOTE(&tp->t_rkq.ki_note, 0); } if (flag & FWRITE) { wakeup(TSA_PTC_WRITE(tp)); KNOTE(&tp->t_wkq.ki_note, 0); } } static int ptcopen(struct dev_open_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp; struct pt_ioctl *pti; /* * The pti will already be assigned by the clone code or * pre-created if a non-unix 98 pty. If si_drv1 is NULL * we are somehow racing a unix98 termination. */ pti = dev->si_drv1; if (pti == NULL) return(ENXIO); lwkt_gettoken(&pti->pt_tty.t_token); if (pti_hold(pti)) { lwkt_reltoken(&pti->pt_tty.t_token); return(ENXIO); } if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) { pti_done(pti); lwkt_reltoken(&pti->pt_tty.t_token); return(EBUSY); } tp = dev->si_tty; lwkt_gettoken(&tp->t_token); if (tp->t_oproc) { pti_done(pti); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EIO); } /* * If the slave side is not yet open clear any left over zombie * state before doing our modem control. */ if ((pti->pt_flags & PF_SOPEN) == 0) tp->t_state &= ~TS_ZOMBIE; tp->t_oproc = ptsstart; tp->t_stop = ptsstop; tp->t_unhold = ptsunhold; /* * Carrier on! */ (void)(*linesw[tp->t_line].l_modem)(tp, 1); tp->t_lflag &= ~EXTPROC; pti->pt_prison = ap->a_cred->cr_prison; pti->pt_flags &= ~PF_PTCSTATEMASK; pti->pt_send = 0; pti->pt_ucntl = 0; pti->devs->si_uid = ap->a_cred->cr_uid; pti->devs->si_gid = ap->a_cred->cr_uid ? GID_TTY : 0; pti->devs->si_perms = 0600; pti->devc->si_uid = ap->a_cred->cr_uid; pti->devc->si_gid = 0; pti->devc->si_perms = 0600; /* * Mark master side open. This does not cause any events * on the slave side. */ pti->pt_flags |= PF_MOPEN; pti_done(pti); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } static int ptcclose(struct dev_close_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp; struct pt_ioctl *pti = dev->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); if (pti_hold(pti)) { lwkt_reltoken(&pti->pt_tty.t_token); panic("ptcclose on terminated pti"); } tp = dev->si_tty; lwkt_gettoken(&tp->t_token); (void)(*linesw[tp->t_line].l_modem)(tp, 0); /* * Mark the master side closed. If the slave is still open * mark the tty ZOMBIE, preventing any new action until both * sides have closed. * * NOTE: The ttyflush() will wake up the slave once we've * set appropriate flags. The ZOMBIE flag will be * cleared when the slave side is closed. */ pti->pt_flags &= ~PF_MOPEN; if (pti->pt_flags & PF_SOPEN) tp->t_state |= TS_ZOMBIE; /* * Turn off the carrier and disconnect. This will notify the slave * side. */ if (tp->t_state & TS_ISOPEN) { tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED); ttyflush(tp, FREAD | FWRITE); } tp->t_oproc = NULL; /* mark closed */ pti->pt_prison = NULL; pti->devs->si_uid = 0; pti->devs->si_gid = 0; pti->devs->si_perms = 0666; pti->devc->si_uid = 0; pti->devc->si_gid = 0; pti->devc->si_perms = 0666; pti_done(pti); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } static int ptcread(struct dev_read_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp = dev->si_tty; struct pt_ioctl *pti = dev->si_drv1; char buf[BUFSIZ]; int error = 0, cc; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); /* * We want to block until the slave * is open, and there's something to read; * but if we lost the slave or we're NBIO, * then return the appropriate error instead. */ for (;;) { if (tp->t_state&TS_ISOPEN) { if ((pti->pt_flags & PF_PKT) && pti->pt_send) { error = ureadc((int)pti->pt_send, ap->a_uio); if (error) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } if (pti->pt_send & TIOCPKT_IOCTL) { cc = (int)szmin(ap->a_uio->uio_resid, sizeof(tp->t_termios)); uiomove((caddr_t)&tp->t_termios, cc, ap->a_uio); } pti->pt_send = 0; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } if ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl) { error = ureadc((int)pti->pt_ucntl, ap->a_uio); if (error) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } pti->pt_ucntl = 0; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) break; } if ((tp->t_state & TS_CONNECTED) == 0) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); /* EOF */ } if (ap->a_ioflag & IO_NDELAY) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EWOULDBLOCK); } error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0); if (error) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } } if (pti->pt_flags & (PF_PKT|PF_UCNTL)) error = ureadc(0, ap->a_uio); while (ap->a_uio->uio_resid > 0 && error == 0) { cc = clist_qtob(&tp->t_outq, buf, (int)szmin(ap->a_uio->uio_resid, BUFSIZ)); if (cc <= 0) break; error = uiomove(buf, (size_t)cc, ap->a_uio); } ttwwakeup(tp); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } static void ptsstop(struct tty *tp, int flush) { struct pt_ioctl *pti = tp->t_dev->si_drv1; int flag; lwkt_gettoken(&pti->pt_tty.t_token); /* note: FLUSHREAD and FLUSHWRITE already ok */ if (pti) { if (flush == 0) { flush = TIOCPKT_STOP; pti->pt_flags |= PF_STOPPED; } else { pti->pt_flags &= ~PF_STOPPED; } pti->pt_send |= flush; /* change of perspective */ } flag = 0; if (flush & FREAD) flag |= FWRITE; if (flush & FWRITE) flag |= FREAD; ptcwakeup(tp, flag); lwkt_reltoken(&pti->pt_tty.t_token); } /* * ttyunhold() calls us instead of just decrementing tp->t_refs. This * is needed because a session can hold onto a pts (half closed state) * even if there are no live file descriptors. Without the callback * we can't clean up. */ static void ptsunhold(struct tty *tp) { struct pt_ioctl *pti = tp->t_dev->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); pti_hold(pti); --tp->t_refs; pti_done(pti); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); } /* * kqueue ops for pseudo-terminals. */ static struct filterops ptcread_filtops = { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcrdetach, filt_ptcread }; static struct filterops ptcwrite_filtops = { FILTEROP_ISFD|FILTEROP_MPSAFE, NULL, filt_ptcwdetach, filt_ptcwrite }; static int ptckqfilter(struct dev_kqfilter_args *ap) { cdev_t dev = ap->a_head.a_dev; struct knote *kn = ap->a_kn; struct tty *tp = dev->si_tty; struct klist *klist; ap->a_result = 0; switch (kn->kn_filter) { case EVFILT_READ: klist = &tp->t_rkq.ki_note; kn->kn_fop = &ptcread_filtops; break; case EVFILT_WRITE: klist = &tp->t_wkq.ki_note; kn->kn_fop = &ptcwrite_filtops; break; default: ap->a_result = EOPNOTSUPP; return (0); } kn->kn_hook = (caddr_t)dev; knote_insert(klist, kn); return (0); } static int filt_ptcread (struct knote *kn, long hint) { struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty; struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); if ((tp->t_state & TS_ZOMBIE) || (pti->pt_flags & PF_SCLOSED)) { kn->kn_flags |= (EV_EOF | EV_NODATA); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (1); } if ((tp->t_state & TS_ISOPEN) && ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) || ((pti->pt_flags & PF_PKT) && pti->pt_send) || ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) { kn->kn_data = tp->t_outq.c_cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(1); } else { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(0); } } static int filt_ptcwrite (struct knote *kn, long hint) { struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty; struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); if (tp->t_state & TS_ZOMBIE) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); kn->kn_flags |= (EV_EOF | EV_NODATA); return (1); } if (tp->t_state & TS_ISOPEN && ((pti->pt_flags & PF_REMOTE) ? (tp->t_canq.c_cc == 0) : ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) || (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) { kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(1); } else { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(0); } /* NOTREACHED */ } static void filt_ptcrdetach (struct knote *kn) { struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty; knote_remove(&tp->t_rkq.ki_note, kn); } static void filt_ptcwdetach (struct knote *kn) { struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty; knote_remove(&tp->t_wkq.ki_note, kn); } /* * I/O ops */ static int ptcwrite(struct dev_write_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp = dev->si_tty; u_char *cp = NULL; int cc = 0; u_char locbuf[BUFSIZ]; int cnt = 0; struct pt_ioctl *pti = dev->si_drv1; int error = 0; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); again: if ((tp->t_state&TS_ISOPEN) == 0) goto block; if (pti->pt_flags & PF_REMOTE) { if (tp->t_canq.c_cc) goto block; while ((ap->a_uio->uio_resid > 0 || cc > 0) && tp->t_canq.c_cc < TTYHOG - 1) { if (cc == 0) { cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ); cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc); cp = locbuf; error = uiomove(cp, (size_t)cc, ap->a_uio); if (error) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } /* check again for safety */ if ((tp->t_state & TS_ISOPEN) == 0) { /* adjust as usual */ ap->a_uio->uio_resid += cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EIO); } } if (cc > 0) { cc = clist_btoq((char *)cp, cc, &tp->t_canq); /* * XXX we don't guarantee that the canq size * is >= TTYHOG, so the above btoq() may * leave some bytes uncopied. However, space * is guaranteed for the null terminator if * we don't fail here since (TTYHOG - 1) is * not a multiple of CBSIZE. */ if (cc > 0) break; } } /* adjust for data copied in but not written */ ap->a_uio->uio_resid += cc; clist_putc(0, &tp->t_canq); ttwakeup(tp); wakeup(TSA_PTS_READ(tp)); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } while (ap->a_uio->uio_resid > 0 || cc > 0) { if (cc == 0) { cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ); cp = locbuf; error = uiomove(cp, (size_t)cc, ap->a_uio); if (error) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } /* check again for safety */ if ((tp->t_state & TS_ISOPEN) == 0) { /* adjust for data copied in but not written */ ap->a_uio->uio_resid += cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EIO); } } while (cc > 0) { if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) { wakeup(TSA_HUP_OR_INPUT(tp)); goto block; } (*linesw[tp->t_line].l_rint)(*cp++, tp); cnt++; cc--; } cc = 0; } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); block: /* * Come here to wait for slave to open, for space * in outq, or space in rawq, or an empty canq. */ if ((tp->t_state & TS_CONNECTED) == 0) { /* adjust for data copied in but not written */ ap->a_uio->uio_resid += cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EIO); } if (ap->a_ioflag & IO_NDELAY) { /* adjust for data copied in but not written */ ap->a_uio->uio_resid += cc; if (cnt == 0) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EWOULDBLOCK); } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0); if (error) { /* adjust for data copied in but not written */ ap->a_uio->uio_resid += cc; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } goto again; } /*ARGSUSED*/ static int ptyioctl(struct dev_ioctl_args *ap) { cdev_t dev = ap->a_head.a_dev; struct tty *tp = dev->si_tty; struct pt_ioctl *pti = dev->si_drv1; u_char *cc = tp->t_cc; int stop, error; lwkt_gettoken(&pti->pt_tty.t_token); lwkt_gettoken(&tp->t_token); if (dev_dflags(dev) & D_MASTER) { switch (ap->a_cmd) { case TIOCGPGRP: /* * We avoid calling ttioctl on the controller since, * in that case, tp must be the controlling terminal. */ *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); case TIOCPKT: if (*(int *)ap->a_data) { if (pti->pt_flags & PF_UCNTL) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EINVAL); } pti->pt_flags |= PF_PKT; } else { pti->pt_flags &= ~PF_PKT; } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); case TIOCUCNTL: if (*(int *)ap->a_data) { if (pti->pt_flags & PF_PKT) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EINVAL); } pti->pt_flags |= PF_UCNTL; } else { pti->pt_flags &= ~PF_UCNTL; } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); case TIOCREMOTE: if (*(int *)ap->a_data) pti->pt_flags |= PF_REMOTE; else pti->pt_flags &= ~PF_REMOTE; ttyflush(tp, FREAD|FWRITE); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); case TIOCISPTMASTER: if ((pti->pt_flags & PF_UNIX98) && (pti->devc == dev)) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } else { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EINVAL); } } /* * The rest of the ioctls shouldn't be called until * the slave is open. */ if ((tp->t_state & TS_ISOPEN) == 0) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (EAGAIN); } switch (ap->a_cmd) { case TIOCSETD: case TIOCSETA: case TIOCSETAW: case TIOCSETAF: /* * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. * ttywflush(tp) will hang if there are characters in * the outq. */ ndflush(&tp->t_outq, tp->t_outq.c_cc); break; case TIOCSIG: if (*(unsigned int *)ap->a_data >= NSIG || *(unsigned int *)ap->a_data == 0) { lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(EINVAL); } if ((tp->t_lflag&NOFLSH) == 0) ttyflush(tp, FREAD|FWRITE); pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1); if ((*(unsigned int *)ap->a_data == SIGINFO) && ((tp->t_lflag&NOKERNINFO) == 0)) ttyinfo(tp); lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(0); } } if (ap->a_cmd == TIOCEXT) { /* * When the EXTPROC bit is being toggled, we need * to send an TIOCPKT_IOCTL if the packet driver * is turned on. */ if (*(int *)ap->a_data) { if (pti->pt_flags & PF_PKT) { pti->pt_send |= TIOCPKT_IOCTL; ptcwakeup(tp, FREAD); } tp->t_lflag |= EXTPROC; } else { if ((tp->t_lflag & EXTPROC) && (pti->pt_flags & PF_PKT)) { pti->pt_send |= TIOCPKT_IOCTL; ptcwakeup(tp, FREAD); } tp->t_lflag &= ~EXTPROC; } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return(0); } error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data, ap->a_fflag, ap->a_cred); if (error == ENOIOCTL) error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag); if (error == ENOIOCTL) { if (pti->pt_flags & PF_UCNTL && (ap->a_cmd & ~0xff) == UIOCCMD(0)) { if (ap->a_cmd & 0xff) { pti->pt_ucntl = (u_char)ap->a_cmd; ptcwakeup(tp, FREAD); } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (0); } error = ENOTTY; } /* * If external processing and packet mode send ioctl packet. */ if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { switch(ap->a_cmd) { case TIOCSETA: case TIOCSETAW: case TIOCSETAF: pti->pt_send |= TIOCPKT_IOCTL; ptcwakeup(tp, FREAD); default: break; } } stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) && CCEQ(cc[VSTART], CTRL('q')); if (pti->pt_flags & PF_NOSTOP) { if (stop) { pti->pt_send &= ~TIOCPKT_NOSTOP; pti->pt_send |= TIOCPKT_DOSTOP; pti->pt_flags &= ~PF_NOSTOP; ptcwakeup(tp, FREAD); } } else { if (!stop) { pti->pt_send &= ~TIOCPKT_DOSTOP; pti->pt_send |= TIOCPKT_NOSTOP; pti->pt_flags |= PF_NOSTOP; ptcwakeup(tp, FREAD); } } lwkt_reltoken(&tp->t_token); lwkt_reltoken(&pti->pt_tty.t_token); return (error); } static void ptc_drvinit (void *unused); SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level, 0, "Change pty debug level"); static void ptc_drvinit(void *unused) { /* * Unix98 pty stuff. * Create the clonable base device. */ make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone, 0, 0, 0666, "ptmx"); ptis = kmalloc(sizeof(struct pt_ioctl *) * MAXPTYS, M_PTY, M_WAITOK | M_ZERO); } SYSINIT(ptcdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, ptc_drvinit, NULL); |