Loading...
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 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 | /*
* linux/drivers/char/tty_io.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/*
* 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
* or rs-channels. It also implements echoing, cooked mode etc.
*
* Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
*
* Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
* tty_struct and tty_queue structures. Previously there was a array
* of 256 tty_struct's which was statically allocated, and the
* tty_queue structures were allocated at boot time. Both are now
* dynamically allocated only when the tty is open.
*
* Also restructured routines so that there is more of a separation
* between the high-level tty routines (tty_io.c and tty_ioctl.c) and
* the low-level tty routines (serial.c, pty.c, console.c). This
* makes for cleaner and more compact code. -TYT, 9/17/92
*
* Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
* which can be dynamically activated and de-activated by the line
* discipline handling modules (like SLIP).
*
* NOTE: pay no attention to the line discipline code (yet); its
* interface is still subject to change in this version...
* -- TYT, 1/31/92
*
* Added functionality to the OPOST tty handling. No delays, but all
* other bits should be there.
* -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
*
* Rewrote canonical mode and added more termios flags.
* -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
*/
#include <linux/types.h>
#include <linux/major.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/timer.h>
#include <linux/ctype.h>
#include <linux/kd.h>
#include <linux/mm.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <linux/config.h>
#include <asm/segment.h>
#include <asm/system.h>
#include <asm/bitops.h>
#include <linux/scc.h>
#include "kbd_kern.h"
#include "vt_kern.h"
#include "selection.h"
#define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
#define TTY_DEV MKDEV(TTYAUX_MAJOR,0)
#undef TTY_DEBUG_HANGUP
#define TTY_PARANOIA_CHECK
#define CHECK_TTY_COUNT
extern void do_blank_screen(int nopowersave);
extern void set_vesa_blanking(const unsigned long arg);
struct termios tty_std_termios; /* for the benefit of tty drivers */
struct tty_driver *tty_drivers = NULL; /* linked list of tty drivers */
struct tty_ldisc ldiscs[NR_LDISCS]; /* line disc dispatch table */
/*
* fg_console is the current virtual console,
* last_console is the last used one
* redirect is the pseudo-tty that console output
* is redirected to if asked by TIOCCONS.
*/
int fg_console = 0;
int last_console = 0;
struct tty_struct * redirect = NULL;
struct wait_queue * keypress_wait = NULL;
static void initialize_tty_struct(struct tty_struct *tty);
static int tty_read(struct inode *, struct file *, char *, int);
static int tty_write(struct inode *, struct file *, const char *, int);
static int tty_select(struct inode *, struct file *, int, select_table *);
static int tty_open(struct inode *, struct file *);
static void tty_release(struct inode *, struct file *);
static int tty_ioctl(struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg);
static int tty_fasync(struct inode * inode, struct file * filp, int on);
extern void reset_palette(int currcons) ;
extern void set_palette(void) ;
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
/*
* These two routines return the name of tty. tty_name() should NOT
* be used in interrupt drivers, since it's not re-entrant. Use
* _tty_name() instead.
*/
char *_tty_name(struct tty_struct *tty, char *buf)
{
if (tty)
sprintf(buf, "%s%d", tty->driver.name,
MINOR(tty->device) - tty->driver.minor_start +
tty->driver.name_base);
else
strcpy(buf, "NULL tty");
return buf;
}
char *tty_name(struct tty_struct *tty)
{
static char buf[64];
return(_tty_name(tty, buf));
}
inline int tty_paranoia_check(struct tty_struct *tty, dev_t device,
const char *routine)
{
#ifdef TTY_PARANOIA_CHECK
static const char *badmagic =
"Warning: bad magic number for tty struct (%d, %d) in %s\n";
static const char *badtty =
"Warning: null TTY for (%d, %d) in %s\n";
if (!tty) {
printk(badtty, MAJOR(device), MINOR(device), routine);
return 1;
}
if (tty->magic != TTY_MAGIC) {
printk(badmagic, MAJOR(device), MINOR(device), routine);
return 1;
}
#endif
return 0;
}
static int check_tty_count(struct tty_struct *tty, const char *routine)
{
#ifdef CHECK_TTY_COUNT
struct file *f;
int i, count = 0;
for (f = first_file, i=0; i<nr_files; i++, f = f->f_next) {
if (!f->f_count)
continue;
if (f->private_data == tty) {
count++;
}
}
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
tty->driver.subtype == PTY_TYPE_SLAVE &&
tty->link && tty->link->count)
count++;
if (tty->count != count) {
printk("Warning: dev (%d, %d) tty->count(%d) != #fd's(%d) in %s\n",
MAJOR(tty->device), MINOR(tty->device), tty->count,
count, routine);
return count;
}
#endif
return 0;
}
int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
{
if (disc < N_TTY || disc >= NR_LDISCS)
return -EINVAL;
if (new_ldisc) {
ldiscs[disc] = *new_ldisc;
ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
ldiscs[disc].num = disc;
} else
memset(&ldiscs[disc], 0, sizeof(struct tty_ldisc));
return 0;
}
/* Set the discipline of a tty line. */
static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
{
int retval = 0;
struct tty_ldisc o_ldisc;
if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS) ||
!(ldiscs[ldisc].flags & LDISC_FLAG_DEFINED))
return -EINVAL;
if (tty->ldisc.num == ldisc)
return 0; /* We are already in the desired discipline */
o_ldisc = tty->ldisc;
tty_wait_until_sent(tty, 0);
/* Shutdown the current discipline. */
if (tty->ldisc.close)
(tty->ldisc.close)(tty);
/* Now set up the new line discipline. */
tty->ldisc = ldiscs[ldisc];
tty->termios->c_line = ldisc;
if (tty->ldisc.open)
retval = (tty->ldisc.open)(tty);
if (retval < 0) {
tty->ldisc = o_ldisc;
tty->termios->c_line = tty->ldisc.num;
if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
tty->ldisc = ldiscs[N_TTY];
tty->termios->c_line = N_TTY;
if (tty->ldisc.open) {
int r = tty->ldisc.open(tty);
if (r < 0)
panic("Couldn't open N_TTY ldisc for "
"%s --- error %d.",
tty_name(tty), r);
}
}
}
if (tty->ldisc.num != o_ldisc.num && tty->driver.set_ldisc)
tty->driver.set_ldisc(tty);
return retval;
}
/*
* This routine returns a tty driver structure, given a device number
*/
struct tty_driver *get_tty_driver(dev_t device)
{
int major, minor;
struct tty_driver *p;
minor = MINOR(device);
major = MAJOR(device);
for (p = tty_drivers; p; p = p->next) {
if (p->major != major)
continue;
if (minor < p->minor_start)
continue;
if (minor >= p->minor_start + p->num)
continue;
return p;
}
return NULL;
}
/*
* If we try to write to, or set the state of, a terminal and we're
* not in the foreground, send a SIGTTOU. If the signal is blocked or
* ignored, go ahead and perform the operation. (POSIX 7.2)
*/
int tty_check_change(struct tty_struct * tty)
{
if (current->tty != tty)
return 0;
if (tty->pgrp <= 0) {
printk("tty_check_change: tty->pgrp <= 0!\n");
return 0;
}
if (current->pgrp == tty->pgrp)
return 0;
if (is_ignored(SIGTTOU))
return 0;
if (is_orphaned_pgrp(current->pgrp))
return -EIO;
(void) kill_pg(current->pgrp,SIGTTOU,1);
return -ERESTARTSYS;
}
static int hung_up_tty_read(struct inode * inode, struct file * file, char * buf, int count)
{
return 0;
}
static int hung_up_tty_write(struct inode * inode, struct file * file, const char * buf, int count)
{
return -EIO;
}
static int hung_up_tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
{
return 1;
}
static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg)
{
return -EIO;
}
static int tty_lseek(struct inode * inode, struct file * file, off_t offset, int orig)
{
return -ESPIPE;
}
static struct file_operations tty_fops = {
tty_lseek,
tty_read,
tty_write,
NULL, /* tty_readdir */
tty_select,
tty_ioctl,
NULL, /* tty_mmap */
tty_open,
tty_release,
NULL, /* tty_fsync */
tty_fasync
};
static struct file_operations hung_up_tty_fops = {
tty_lseek,
hung_up_tty_read,
hung_up_tty_write,
NULL, /* hung_up_tty_readdir */
hung_up_tty_select,
hung_up_tty_ioctl,
NULL, /* hung_up_tty_mmap */
NULL, /* hung_up_tty_open */
tty_release, /* hung_up_tty_release */
NULL, /* hung_up_tty_fsync */
NULL /* hung_up_tty_fasync */
};
void do_tty_hangup(struct tty_struct * tty, struct file_operations *fops)
{
int i;
struct file * filp;
struct task_struct *p;
if (!tty)
return;
check_tty_count(tty, "do_tty_hangup");
for (filp = first_file, i=0; i<nr_files; i++, filp = filp->f_next) {
if (!filp->f_count)
continue;
if (filp->private_data != tty)
continue;
if (filp->f_inode && filp->f_inode->i_rdev == CONSOLE_DEV)
continue;
if (filp->f_op != &tty_fops)
continue;
tty_fasync(filp->f_inode, filp, 0);
filp->f_op = fops;
}
if (tty->ldisc.flush_buffer)
tty->ldisc.flush_buffer(tty);
if (tty->driver.flush_buffer)
tty->driver.flush_buffer(tty);
if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
tty->ldisc.write_wakeup)
(tty->ldisc.write_wakeup)(tty);
wake_up_interruptible(&tty->write_wait);
wake_up_interruptible(&tty->read_wait);
/*
* Shutdown the current line discipline, and reset it to
* N_TTY.
*/
if (tty->ldisc.num != ldiscs[N_TTY].num) {
if (tty->ldisc.close)
(tty->ldisc.close)(tty);
tty->ldisc = ldiscs[N_TTY];
tty->termios->c_line = N_TTY;
if (tty->ldisc.open) {
i = (tty->ldisc.open)(tty);
if (i < 0)
printk("do_tty_hangup: N_TTY open: error %d\n",
-i);
}
}
for_each_task(p) {
if ((tty->session > 0) && (p->session == tty->session) &&
p->leader) {
send_sig(SIGHUP,p,1);
send_sig(SIGCONT,p,1);
if (tty->pgrp > 0)
p->tty_old_pgrp = tty->pgrp;
}
if (p->tty == tty)
p->tty = NULL;
}
tty->flags = 0;
tty->session = 0;
tty->pgrp = -1;
tty->ctrl_status = 0;
if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS)
*tty->termios = tty->driver.init_termios;
if (tty->driver.hangup)
(tty->driver.hangup)(tty);
}
void tty_hangup(struct tty_struct * tty)
{
#ifdef TTY_DEBUG_HANGUP
printk("%s hangup...\n", tty_name(tty));
#endif
do_tty_hangup(tty, &hung_up_tty_fops);
}
void tty_vhangup(struct tty_struct * tty)
{
#ifdef TTY_DEBUG_HANGUP
printk("%s vhangup...\n", tty_name(tty));
#endif
do_tty_hangup(tty, &hung_up_tty_fops);
}
int tty_hung_up_p(struct file * filp)
{
return (filp->f_op == &hung_up_tty_fops);
}
/*
* This function is typically called only by the session leader, when
* it wants to disassociate itself from its controlling tty.
*
* It performs the following functions:
* (1) Sends a SIGHUP and SIGCONT to the foreground process group
* (2) Clears the tty from being controlling the session
* (3) Clears the controlling tty for all processes in the
* session group.
*/
void disassociate_ctty(int priv)
{
struct tty_struct *tty = current->tty;
struct task_struct *p;
if (!tty) {
if (current->tty_old_pgrp) {
kill_pg(current->tty_old_pgrp, SIGHUP, priv);
kill_pg(current->tty_old_pgrp, SIGCONT, priv);
}
return;
}
if (tty->pgrp > 0) {
kill_pg(tty->pgrp, SIGHUP, priv);
kill_pg(tty->pgrp, SIGCONT, priv);
}
current->tty_old_pgrp = 0;
tty->session = 0;
tty->pgrp = -1;
for_each_task(p)
if (p->session == current->session)
p->tty = NULL;
}
/*
* Sometimes we want to wait until a particular VT has been activated. We
* do it in a very simple manner. Everybody waits on a single queue and
* get woken up at once. Those that are satisfied go on with their business,
* while those not ready go back to sleep. Seems overkill to add a wait
* to each vt just for this - usually this does nothing!
*/
static struct wait_queue *vt_activate_queue = NULL;
/*
* Sleeps until a vt is activated, or the task is interrupted. Returns
* 0 if activation, -1 if interrupted.
*/
int vt_waitactive(void)
{
interruptible_sleep_on(&vt_activate_queue);
return (current->signal & ~current->blocked) ? -1 : 0;
}
#define vt_wake_waitactive() wake_up(&vt_activate_queue)
void reset_vc(unsigned int new_console)
{
vt_cons[new_console]->vc_mode = KD_TEXT;
kbd_table[new_console].kbdmode = VC_XLATE;
vt_cons[new_console]->vt_mode.mode = VT_AUTO;
vt_cons[new_console]->vt_mode.waitv = 0;
vt_cons[new_console]->vt_mode.relsig = 0;
vt_cons[new_console]->vt_mode.acqsig = 0;
vt_cons[new_console]->vt_mode.frsig = 0;
vt_cons[new_console]->vt_pid = -1;
vt_cons[new_console]->vt_newvt = -1;
reset_palette (new_console) ;
}
/*
* Performs the back end of a vt switch
*/
void complete_change_console(unsigned int new_console)
{
unsigned char old_vc_mode;
if (new_console == fg_console)
return;
if (!vc_cons_allocated(new_console))
return;
last_console = fg_console;
/*
* If we're switching, we could be going from KD_GRAPHICS to
* KD_TEXT mode or vice versa, which means we need to blank or
* unblank the screen later.
*/
old_vc_mode = vt_cons[fg_console]->vc_mode;
update_screen(new_console);
/*
* If this new console is under process control, send it a signal
* telling it that it has acquired. Also check if it has died and
* clean up (similar to logic employed in change_console())
*/
if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)
{
/*
* Send the signal as privileged - kill_proc() will
* tell us if the process has gone or something else
* is awry
*/
if (kill_proc(vt_cons[new_console]->vt_pid,
vt_cons[new_console]->vt_mode.acqsig,
1) != 0)
{
/*
* The controlling process has died, so we revert back to
* normal operation. In this case, we'll also change back
* to KD_TEXT mode. I'm not sure if this is strictly correct
* but it saves the agony when the X server dies and the screen
* remains blanked due to KD_GRAPHICS! It would be nice to do
* this outside of VT_PROCESS but there is no single process
* to account for and tracking tty count may be undesirable.
*/
reset_vc(new_console);
}
}
/*
* We do this here because the controlling process above may have
* gone, and so there is now a new vc_mode
*/
if (old_vc_mode != vt_cons[new_console]->vc_mode)
{
if (vt_cons[new_console]->vc_mode == KD_TEXT)
do_unblank_screen();
else
do_blank_screen(1);
}
/* Set the colour palette for this VT */
if (vt_cons[new_console]->vc_mode == KD_TEXT)
set_palette() ;
/*
* Wake anyone waiting for their VT to activate
*/
vt_wake_waitactive();
return;
}
/*
* Performs the front-end of a vt switch
*/
void change_console(unsigned int new_console)
{
if (new_console == fg_console)
return;
if (!vc_cons_allocated(new_console))
return;
/*
* If this vt is in process mode, then we need to handshake with
* that process before switching. Essentially, we store where that
* vt wants to switch to and wait for it to tell us when it's done
* (via VT_RELDISP ioctl).
*
* We also check to see if the controlling process still exists.
* If it doesn't, we reset this vt to auto mode and continue.
* This is a cheap way to track process control. The worst thing
* that can happen is: we send a signal to a process, it dies, and
* the switch gets "lost" waiting for a response; hopefully, the
* user will try again, we'll detect the process is gone (unless
* the user waits just the right amount of time :-) and revert the
* vt to auto control.
*/
if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)
{
/*
* Send the signal as privileged - kill_proc() will
* tell us if the process has gone or something else
* is awry
*/
if (kill_proc(vt_cons[fg_console]->vt_pid,
vt_cons[fg_console]->vt_mode.relsig,
1) == 0)
{
/*
* It worked. Mark the vt to switch to and
* return. The process needs to send us a
* VT_RELDISP ioctl to complete the switch.
*/
vt_cons[fg_console]->vt_newvt = new_console;
return;
}
/*
* The controlling process has died, so we revert back to
* normal operation. In this case, we'll also change back
* to KD_TEXT mode. I'm not sure if this is strictly correct
* but it saves the agony when the X server dies and the screen
* remains blanked due to KD_GRAPHICS! It would be nice to do
* this outside of VT_PROCESS but there is no single process
* to account for and tracking tty count may be undesirable.
*/
reset_vc(fg_console);
/*
* Fall through to normal (VT_AUTO) handling of the switch...
*/
}
/*
* Ignore all switches in KD_GRAPHICS+VT_AUTO mode
*/
if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
return;
complete_change_console(new_console);
}
void wait_for_keypress(void)
{
sleep_on(&keypress_wait);
}
void stop_tty(struct tty_struct *tty)
{
if (tty->stopped)
return;
tty->stopped = 1;
if (tty->link && tty->link->packet) {
tty->ctrl_status &= ~TIOCPKT_START;
tty->ctrl_status |= TIOCPKT_STOP;
wake_up_interruptible(&tty->link->read_wait);
}
if (tty->driver.stop)
(tty->driver.stop)(tty);
}
void start_tty(struct tty_struct *tty)
{
if (!tty->stopped)
return;
tty->stopped = 0;
if (tty->link && tty->link->packet) {
tty->ctrl_status &= ~TIOCPKT_STOP;
tty->ctrl_status |= TIOCPKT_START;
wake_up_interruptible(&tty->link->read_wait);
}
if (tty->driver.start)
(tty->driver.start)(tty);
if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
tty->ldisc.write_wakeup)
(tty->ldisc.write_wakeup)(tty);
wake_up_interruptible(&tty->write_wait);
}
static int tty_read(struct inode * inode, struct file * file, char * buf, int count)
{
int i;
struct tty_struct * tty;
tty = (struct tty_struct *)file->private_data;
if (tty_paranoia_check(tty, inode->i_rdev, "tty_read"))
return -EIO;
if (!tty || (tty->flags & (1 << TTY_IO_ERROR)))
return -EIO;
/* This check not only needs to be done before reading, but also
whenever read_chan() gets woken up after sleeping, so I've
moved it to there. This should only be done for the N_TTY
line discipline, anyway. Same goes for write_chan(). -- jlc. */
#if 0
if ((inode->i_rdev != CONSOLE_DEV) && /* don't stop on /dev/console */
(tty->pgrp > 0) &&
(current->tty == tty) &&
(tty->pgrp != current->pgrp))
if (is_ignored(SIGTTIN) || is_orphaned_pgrp(current->pgrp))
return -EIO;
else {
(void) kill_pg(current->pgrp, SIGTTIN, 1);
return -ERESTARTSYS;
}
#endif
if (tty->ldisc.read)
/* XXX casts are for what kernel-wide prototypes should be. */
i = (tty->ldisc.read)(tty,file,(unsigned char *)buf,(unsigned int)count);
else
i = -EIO;
if (i > 0)
inode->i_atime = CURRENT_TIME;
return i;
}
static int tty_write(struct inode * inode, struct file * file, const char * buf, int count)
{
int i, is_console;
struct tty_struct * tty;
is_console = (inode->i_rdev == CONSOLE_DEV);
if (is_console && redirect)
tty = redirect;
else
tty = (struct tty_struct *)file->private_data;
if (tty_paranoia_check(tty, inode->i_rdev, "tty_write"))
return -EIO;
if (!tty || !tty->driver.write || (tty->flags & (1 << TTY_IO_ERROR)))
return -EIO;
#if 0
if (!is_console && L_TOSTOP(tty) && (tty->pgrp > 0) &&
(current->tty == tty) && (tty->pgrp != current->pgrp)) {
if (is_orphaned_pgrp(current->pgrp))
return -EIO;
if (!is_ignored(SIGTTOU)) {
(void) kill_pg(current->pgrp, SIGTTOU, 1);
return -ERESTARTSYS;
}
}
#endif
if (tty->ldisc.write)
/* XXX casts are for what kernel-wide prototypes should be. */
i = (tty->ldisc.write)(tty,file,(const unsigned char *)buf,(unsigned int)count);
else
i = -EIO;
if (i > 0)
inode->i_mtime = CURRENT_TIME;
return i;
}
/*
* This is so ripe with races that you should *really* not touch this
* unless you know exactly what you are doing. All the changes have to be
* made atomically, or there may be incorrect pointers all over the place.
*/
static int init_dev(dev_t device, struct tty_struct **ret_tty)
{
struct tty_struct *tty, **tty_loc, *o_tty, **o_tty_loc;
struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
struct tty_driver *driver;
int retval;
int idx;
driver = get_tty_driver(device);
if (!driver)
return -ENODEV;
idx = MINOR(device) - driver->minor_start;
tty = o_tty = NULL;
tp = o_tp = NULL;
ltp = o_ltp = NULL;
o_tty_loc = NULL;
o_tp_loc = o_ltp_loc = NULL;
tty_loc = &driver->table[idx];
tp_loc = &driver->termios[idx];
ltp_loc = &driver->termios_locked[idx];
repeat:
retval = -EAGAIN;
if (driver->type == TTY_DRIVER_TYPE_PTY &&
driver->subtype == PTY_TYPE_MASTER &&
*tty_loc && (*tty_loc)->count)
goto end_init;
retval = -ENOMEM;
if (!*tty_loc && !tty) {
if (!(tty = (struct tty_struct*) get_free_page(GFP_KERNEL)))
goto end_init;
initialize_tty_struct(tty);
tty->device = device;
tty->driver = *driver;
goto repeat;
}
if (!*tp_loc && !tp) {
tp = (struct termios *) kmalloc(sizeof(struct termios),
GFP_KERNEL);
if (!tp)
goto end_init;
*tp = driver->init_termios;
goto repeat;
}
if (!*ltp_loc && !ltp) {
ltp = (struct termios *) kmalloc(sizeof(struct termios),
GFP_KERNEL);
if (!ltp)
goto end_init;
memset(ltp, 0, sizeof(struct termios));
goto repeat;
}
if (driver->type == TTY_DRIVER_TYPE_PTY) {
o_tty_loc = &driver->other->table[idx];
o_tp_loc = &driver->other->termios[idx];
o_ltp_loc = &driver->other->termios_locked[idx];
if (!*o_tty_loc && !o_tty) {
dev_t o_device;
o_tty = (struct tty_struct *)
get_free_page(GFP_KERNEL);
if (!o_tty)
goto end_init;
o_device = MKDEV(driver->other->major,
driver->other->minor_start + idx);
initialize_tty_struct(o_tty);
o_tty->device = o_device;
o_tty->driver = *driver->other;
goto repeat;
}
if (!*o_tp_loc && !o_tp) {
o_tp = (struct termios *)
kmalloc(sizeof(struct termios), GFP_KERNEL);
if (!o_tp)
goto end_init;
*o_tp = driver->other->init_termios;
goto repeat;
}
if (!*o_ltp_loc && !o_ltp) {
o_ltp = (struct termios *)
kmalloc(sizeof(struct termios), GFP_KERNEL);
if (!o_ltp)
goto end_init;
memset(o_ltp, 0, sizeof(struct termios));
goto repeat;
}
}
/* Now we have allocated all the structures: update all the pointers.. */
if (!*tp_loc) {
*tp_loc = tp;
tp = NULL;
}
if (!*ltp_loc) {
*ltp_loc = ltp;
ltp = NULL;
}
if (!*tty_loc) {
tty->termios = *tp_loc;
tty->termios_locked = *ltp_loc;
*tty_loc = tty;
(*driver->refcount)++;
(*tty_loc)->count++;
if (tty->ldisc.open) {
retval = (tty->ldisc.open)(tty);
if (retval < 0) {
(*tty_loc)->count--;
tty = NULL;
goto end_init;
}
}
tty = NULL;
} else {
if ((*tty_loc)->flags & (1 << TTY_CLOSING)) {
printk("Attempt to open closing tty %s.\n",
tty_name(*tty_loc));
printk("Ack!!!! This should never happen!!\n");
return -EINVAL;
}
(*tty_loc)->count++;
}
if (driver->type == TTY_DRIVER_TYPE_PTY) {
if (!*o_tp_loc) {
*o_tp_loc = o_tp;
o_tp = NULL;
}
if (!*o_ltp_loc) {
*o_ltp_loc = o_ltp;
o_ltp = NULL;
}
if (!*o_tty_loc) {
o_tty->termios = *o_tp_loc;
o_tty->termios_locked = *o_ltp_loc;
*o_tty_loc = o_tty;
(*driver->other->refcount)++;
if (o_tty->ldisc.open) {
retval = (o_tty->ldisc.open)(o_tty);
if (retval < 0) {
(*tty_loc)->count--;
o_tty = NULL;
goto end_init;
}
}
o_tty = NULL;
}
(*tty_loc)->link = *o_tty_loc;
(*o_tty_loc)->link = *tty_loc;
if (driver->subtype == PTY_TYPE_MASTER)
(*o_tty_loc)->count++;
}
(*tty_loc)->driver = *driver;
*ret_tty = *tty_loc;
retval = 0;
end_init:
if (tty)
free_page((unsigned long) tty);
if (o_tty)
free_page((unsigned long) o_tty);
if (tp)
kfree_s(tp, sizeof(struct termios));
if (o_tp)
kfree_s(o_tp, sizeof(struct termios));
if (ltp)
kfree_s(ltp, sizeof(struct termios));
if (o_ltp)
kfree_s(o_ltp, sizeof(struct termios));
return retval;
}
/*
* Even releasing the tty structures is a tricky business.. We have
* to be very careful that the structures are all released at the
* same time, as interrupts might otherwise get the wrong pointers.
*/
static void release_dev(struct file * filp)
{
struct tty_struct *tty, *o_tty;
struct termios *tp, *o_tp, *ltp, *o_ltp;
struct task_struct **p;
int idx;
tty = (struct tty_struct *)filp->private_data;
if (tty_paranoia_check(tty, filp->f_inode->i_rdev, "release_dev"))
return;
check_tty_count(tty, "release_dev");
tty_fasync(filp->f_inode, filp, 0);
tp = tty->termios;
ltp = tty->termios_locked;
idx = MINOR(tty->device) - tty->driver.minor_start;
#ifdef TTY_PARANOIA_CHECK
if (idx < 0 || idx >= tty->driver.num) {
printk("release_dev: bad idx when trying to free (%d, %d)\n",
MAJOR(tty->device), MINOR(tty->device));
return;
}
if (tty != tty->driver.table[idx]) {
printk("release_dev: driver.table[%d] not tty for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
if (tp != tty->driver.termios[idx]) {
printk("release_dev: driver.termios[%d] not termios for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
if (ltp != tty->driver.termios_locked[idx]) {
printk("release_dev: driver.termios_locked[%d] not termios_locked for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
#endif
#ifdef TTY_DEBUG_HANGUP
printk("release_dev of %s (tty count=%d)...", tty_name(tty),
tty->count);
#endif
o_tty = tty->link;
o_tp = (o_tty) ? o_tty->termios : NULL;
o_ltp = (o_tty) ? o_tty->termios_locked : NULL;
#ifdef TTY_PARANOIA_CHECK
if (tty->driver.other) {
if (o_tty != tty->driver.other->table[idx]) {
printk("release_dev: other->table[%d] not o_tty for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
if (o_tp != tty->driver.other->termios[idx]) {
printk("release_dev: other->termios[%d] not o_termios for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
if (o_ltp != tty->driver.other->termios_locked[idx]) {
printk("release_dev: other->termios_locked[%d] not o_termios_locked for (%d, %d)\n",
idx, MAJOR(tty->device), MINOR(tty->device));
return;
}
if (o_tty->link != tty) {
printk("release_dev: bad pty pointers\n");
return;
}
}
#endif
if (tty->driver.close)
tty->driver.close(tty, filp);
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
tty->driver.subtype == PTY_TYPE_MASTER) {
if (--tty->link->count < 0) {
printk("release_dev: bad pty slave count (%d) for %s\n",
tty->count, tty_name(tty));
tty->link->count = 0;
}
}
if (--tty->count < 0) {
printk("release_dev: bad tty->count (%d) for %s\n",
tty->count, tty_name(tty));
tty->count = 0;
}
if (tty->count)
return;
/*
* We're committed; at this point, we must not block!
*/
if (o_tty) {
if (o_tty->count)
return;
tty->driver.other->table[idx] = NULL;
tty->driver.other->termios[idx] = NULL;
kfree_s(o_tp, sizeof(struct termios));
}
#ifdef TTY_DEBUG_HANGUP
printk("freeing tty structure...");
#endif
tty->flags |= (1 << TTY_CLOSING);
/*
* Make sure there aren't any processes that still think this
* tty is their controlling tty.
*/
for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
if (*p == 0)
continue;
if ((*p)->tty == tty)
(*p)->tty = NULL;
if (o_tty && (*p)->tty == o_tty)
(*p)->tty = NULL;
}
/*
* Shutdown the current line discipline, and reset it to
* N_TTY.
*/
if (tty->ldisc.close)
(tty->ldisc.close)(tty);
tty->ldisc = ldiscs[N_TTY];
tty->termios->c_line = N_TTY;
if (o_tty) {
if (o_tty->ldisc.close)
(o_tty->ldisc.close)(o_tty);
o_tty->ldisc = ldiscs[N_TTY];
}
tty->driver.table[idx] = NULL;
if (tty->driver.flags & TTY_DRIVER_RESET_TERMIOS) {
tty->driver.termios[idx] = NULL;
kfree_s(tp, sizeof(struct termios));
}
if (tty == redirect || o_tty == redirect)
redirect = NULL;
/*
* Make sure that the tty's task queue isn't activated. If it
* is, take it out of the linked list.
*/
cli();
if (tty->flip.tqueue.sync) {
struct tq_struct *tq, *prev;
for (tq=tq_timer, prev=0; tq; prev=tq, tq=tq->next) {
if (tq == &tty->flip.tqueue) {
if (prev)
prev->next = tq->next;
else
tq_timer = tq->next;
break;
}
}
}
sti();
tty->magic = 0;
(*tty->driver.refcount)--;
free_page((unsigned long) tty);
filp->private_data = 0;
if (o_tty) {
o_tty->magic = 0;
(*o_tty->driver.refcount)--;
free_page((unsigned long) o_tty);
}
}
/*
* tty_open and tty_release keep up the tty count that contains the
* number of opens done on a tty. We cannot use the inode-count, as
* different inodes might point to the same tty.
*
* Open-counting is needed for pty masters, as well as for keeping
* track of serial lines: DTR is dropped when the last close happens.
* (This is not done solely through tty->count, now. - Ted 1/27/92)
*
* The termios state of a pty is reset on first open so that
* settings don't persist across reuse.
*/
static int tty_open(struct inode * inode, struct file * filp)
{
struct tty_struct *tty;
int minor;
int noctty, retval;
dev_t device;
retry_open:
noctty = filp->f_flags & O_NOCTTY;
device = inode->i_rdev;
if (device == TTY_DEV) {
if (!current->tty)
return -ENXIO;
device = current->tty->device;
/* noctty = 1; */
}
if (device == CONSOLE_DEV) {
device = MKDEV(TTY_MAJOR, fg_console+1);
noctty = 1;
}
minor = MINOR(device);
retval = init_dev(device, &tty);
if (retval)
return retval;
filp->private_data = tty;
check_tty_count(tty, "tty_open");
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
tty->driver.subtype == PTY_TYPE_MASTER)
noctty = 1;
#ifdef TTY_DEBUG_HANGUP
printk("opening %s...", tty_name(tty));
#endif
if (tty->driver.open)
retval = tty->driver.open(tty, filp);
else
retval = -ENODEV;
if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !suser())
retval = -EBUSY;
if (retval) {
#ifdef TTY_DEBUG_HANGUP
printk("error %d in opening %s...", retval, tty_name(tty));
#endif
release_dev(filp);
if (retval != -ERESTARTSYS)
return retval;
if (current->signal & ~current->blocked)
return retval;
schedule();
/*
* Need to reset f_op in case a hangup happened.
*/
filp->f_op = &tty_fops;
goto retry_open;
}
if (!noctty &&
current->leader &&
!current->tty &&
tty->session == 0) {
current->tty = tty;
current->tty_old_pgrp = 0;
tty->session = current->session;
tty->pgrp = current->pgrp;
}
return 0;
}
/*
* Note that releasing a pty master also releases the child, so
* we have to make the redirection checks after that and on both
* sides of a pty.
*/
static void tty_release(struct inode * inode, struct file * filp)
{
release_dev(filp);
}
static int tty_select(struct inode * inode, struct file * filp, int sel_type, select_table * wait)
{
struct tty_struct * tty;
tty = (struct tty_struct *)filp->private_data;
if (tty_paranoia_check(tty, inode->i_rdev, "tty_select"))
return 0;
if (tty->ldisc.select)
return (tty->ldisc.select)(tty, inode, filp, sel_type, wait);
return 0;
}
static int tty_fasync(struct inode * inode, struct file * filp, int on)
{
struct tty_struct * tty;
struct fasync_struct *fa, *prev;
tty = (struct tty_struct *)filp->private_data;
if (tty_paranoia_check(tty, inode->i_rdev, "tty_fasync"))
return 0;
for (fa = tty->fasync, prev = 0; fa; prev= fa, fa = fa->fa_next) {
if (fa->fa_file == filp)
break;
}
if (on) {
if (fa)
return 0;
fa = (struct fasync_struct *)kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
if (!fa)
return -ENOMEM;
fa->magic = FASYNC_MAGIC;
fa->fa_file = filp;
fa->fa_next = tty->fasync;
tty->fasync = fa;
if (!tty->read_wait)
tty->minimum_to_wake = 1;
if (filp->f_owner == 0) {
if (tty->pgrp)
filp->f_owner = -tty->pgrp;
else
filp->f_owner = current->pid;
}
} else {
if (!fa)
return 0;
if (prev)
prev->fa_next = fa->fa_next;
else
tty->fasync = fa->fa_next;
kfree_s(fa, sizeof(struct fasync_struct));
if (!tty->fasync && !tty->read_wait)
tty->minimum_to_wake = N_TTY_BUF_SIZE;
}
return 0;
}
#if 0
/*
* XXX does anyone use this anymore?!?
*/
static int do_get_ps_info(unsigned long arg)
{
struct tstruct {
int flag;
int present[NR_TASKS];
struct task_struct tasks[NR_TASKS];
};
struct tstruct *ts = (struct tstruct *)arg;
struct task_struct **p;
char *c, *d;
int i, n = 0;
i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct tstruct));
if (i)
return i;
for (p = &FIRST_TASK ; p <= &LAST_TASK ; p++, n++)
if (*p)
{
c = (char *)(*p);
d = (char *)(ts->tasks+n);
for (i=0 ; i<sizeof(struct task_struct) ; i++)
put_user(*c++, d++);
put_user(1, ts->present+n);
}
else
put_user(0, ts->present+n);
return(0);
}
#endif
static int tty_ioctl(struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg)
{
int retval;
struct tty_struct * tty;
struct tty_struct * real_tty;
struct winsize tmp_ws;
pid_t pgrp;
unsigned char ch;
char mbz = 0;
tty = (struct tty_struct *)file->private_data;
if (tty_paranoia_check(tty, inode->i_rdev, "tty_ioctl"))
return -EINVAL;
if (tty->driver.type == TTY_DRIVER_TYPE_PTY &&
tty->driver.subtype == PTY_TYPE_MASTER)
real_tty = tty->link;
else
real_tty = tty;
switch (cmd) {
case TIOCSTI:
if ((current->tty != tty) && !suser())
return -EPERM;
retval = verify_area(VERIFY_READ, (void *) arg, 1);
if (retval)
return retval;
ch = get_user((char *) arg);
tty->ldisc.receive_buf(tty, &ch, &mbz, 1);
return 0;
case TIOCGWINSZ:
retval = verify_area(VERIFY_WRITE, (void *) arg,
sizeof (struct winsize));
if (retval)
return retval;
memcpy_tofs((struct winsize *) arg, &tty->winsize,
sizeof (struct winsize));
return 0;
case TIOCSWINSZ:
retval = verify_area(VERIFY_READ, (void *) arg,
sizeof (struct winsize));
if (retval)
return retval;
memcpy_fromfs(&tmp_ws, (struct winsize *) arg,
sizeof (struct winsize));
if (memcmp(&tmp_ws, &tty->winsize,
sizeof(struct winsize))) {
if (tty->pgrp > 0)
kill_pg(tty->pgrp, SIGWINCH, 1);
if ((real_tty->pgrp != tty->pgrp) &&
(real_tty->pgrp > 0))
kill_pg(real_tty->pgrp, SIGWINCH, 1);
}
tty->winsize = tmp_ws;
real_tty->winsize = tmp_ws;
return 0;
case TIOCCONS:
if (tty->driver.type == TTY_DRIVER_TYPE_CONSOLE) {
if (!suser())
return -EPERM;
redirect = NULL;
return 0;
}
if (redirect)
return -EBUSY;
redirect = real_tty;
return 0;
case FIONBIO:
retval = verify_area(VERIFY_READ, (void *) arg, sizeof(int));
if (retval)
return retval;
arg = get_user((unsigned int *) arg);
if (arg)
file->f_flags |= O_NONBLOCK;
else
file->f_flags &= ~O_NONBLOCK;
return 0;
case TIOCEXCL:
set_bit(TTY_EXCLUSIVE, &tty->flags);
return 0;
case TIOCNXCL:
clear_bit(TTY_EXCLUSIVE, &tty->flags);
return 0;
case TIOCNOTTY:
if (current->tty != tty)
return -ENOTTY;
if (current->leader)
disassociate_ctty(0);
current->tty = NULL;
return 0;
case TIOCSCTTY:
if (current->leader &&
(current->session == tty->session))
return 0;
/*
* The process must be a session leader and
* not have a controlling tty already.
*/
if (!current->leader || current->tty)
return -EPERM;
if (tty->session > 0) {
/*
* This tty is already the controlling
* tty for another session group!
*/
if ((arg == 1) && suser()) {
/*
* Steal it away
*/
struct task_struct *p;
for_each_task(p)
if (p->tty == tty)
p->tty = NULL;
} else
return -EPERM;
}
current->tty = tty;
current->tty_old_pgrp = 0;
tty->session = current->session;
tty->pgrp = current->pgrp;
return 0;
case TIOCGPGRP:
/*
* (tty == real_tty) is a cheap way of
* testing if the tty is NOT a master pty.
*/
if (tty == real_tty && current->tty != real_tty)
return -ENOTTY;
retval = verify_area(VERIFY_WRITE, (void *) arg,
sizeof (pid_t));
if (retval)
return retval;
put_user(real_tty->pgrp, (pid_t *) arg);
return 0;
case TIOCSPGRP:
retval = tty_check_change(real_tty);
if (retval)
return retval;
if (!current->tty ||
(current->tty != real_tty) ||
(real_tty->session != current->session))
return -ENOTTY;
pgrp = get_user((pid_t *) arg);
if (pgrp < 0)
return -EINVAL;
if (session_of_pgrp(pgrp) != current->session)
return -EPERM;
real_tty->pgrp = pgrp;
return 0;
case TIOCGETD:
retval = verify_area(VERIFY_WRITE, (void *) arg,
sizeof (int));
if (retval)
return retval;
put_user(tty->ldisc.num, (int *) arg);
return 0;
case TIOCSETD:
retval = tty_check_change(tty);
if (retval)
return retval;
arg = get_user((int *) arg);
return tty_set_ldisc(tty, arg);
case TIOCLINUX:
if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
return -EINVAL;
if (current->tty != tty && !suser())
return -EPERM;
retval = verify_area(VERIFY_READ, (void *) arg, 1);
if (retval)
return retval;
switch (retval = get_user((char *)arg))
{
case 0:
case 8:
case 9:
printk("TIOCLINUX (0/8/9) ioctl is gone - use /dev/vcs\n");
return -EINVAL;
#if 0
case 1:
printk("Deprecated TIOCLINUX (1) ioctl\n");
return do_get_ps_info(arg);
#endif
case 2:
return set_selection(arg, tty);
case 3:
return paste_selection(tty);
case 4:
do_unblank_screen();
return 0;
case 5:
return sel_loadlut(arg);
case 6:
/*
* Make it possible to react to Shift+Mousebutton.
* Note that 'shift_state' is an undocumented
* kernel-internal variable; programs not closely
* related to the kernel should not use this.
*/
put_user(shift_state,(char *) arg);
return 0;
case 7:
put_user(mouse_reporting(),(char *) arg);
return 0;
case 10:
set_vesa_blanking(arg);
return 0;
default:
return -EINVAL;
}
case TIOCTTYGSTRUCT:
retval = verify_area(VERIFY_WRITE, (void *) arg,
sizeof(struct tty_struct));
if (retval)
return retval;
memcpy_tofs((struct tty_struct *) arg,
tty, sizeof(struct tty_struct));
return 0;
default:
if (tty->driver.ioctl) {
retval = (tty->driver.ioctl)(tty, file,
cmd, arg);
if (retval != -ENOIOCTLCMD)
return retval;
}
if (tty->ldisc.ioctl) {
retval = (tty->ldisc.ioctl)(tty, file,
cmd, arg);
if (retval != -ENOIOCTLCMD)
return retval;
}
return -EINVAL;
}
}
/*
* This implements the "Secure Attention Key" --- the idea is to
* prevent trojan horses by killing all processes associated with this
* tty when the user hits the "Secure Attention Key". Required for
* super-paranoid applications --- see the Orange Book for more details.
*
* This code could be nicer; ideally it should send a HUP, wait a few
* seconds, then send a INT, and then a KILL signal. But you then
* have to coordinate with the init process, since all processes associated
* with the current tty must be dead before the new getty is allowed
* to spawn.
*/
void do_SAK( struct tty_struct *tty)
{
#ifdef TTY_SOFT_SAK
tty_hangup(tty);
#else
struct task_struct **p;
int session;
int i;
struct file *filp;
if (!tty)
return;
session = tty->session;
if (tty->ldisc.flush_buffer)
tty->ldisc.flush_buffer(tty);
if (tty->driver.flush_buffer)
tty->driver.flush_buffer(tty);
for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
if (!(*p))
continue;
if (((*p)->tty == tty) ||
((session > 0) && ((*p)->session == session)))
send_sig(SIGKILL, *p, 1);
else {
for (i=0; i < NR_OPEN; i++) {
filp = (*p)->files->fd[i];
if (filp && (filp->f_op == &tty_fops) &&
(filp->private_data == tty)) {
send_sig(SIGKILL, *p, 1);
break;
}
}
}
}
#endif
}
/*
* This routine is called out of the software interrupt to flush data
* from the flip buffer to the line discipline.
*/
static void flush_to_ldisc(void *private_)
{
struct tty_struct *tty = (struct tty_struct *) private_;
unsigned char *cp;
char *fp;
int count;
if (tty->flip.buf_num) {
cp = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
fp = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
tty->flip.buf_num = 0;
cli();
tty->flip.char_buf_ptr = tty->flip.char_buf;
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
} else {
cp = tty->flip.char_buf;
fp = tty->flip.flag_buf;
tty->flip.buf_num = 1;
cli();
tty->flip.char_buf_ptr = tty->flip.char_buf + TTY_FLIPBUF_SIZE;
tty->flip.flag_buf_ptr = tty->flip.flag_buf + TTY_FLIPBUF_SIZE;
}
count = tty->flip.count;
tty->flip.count = 0;
sti();
#if 0
if (count > tty->max_flip_cnt)
tty->max_flip_cnt = count;
#endif
tty->ldisc.receive_buf(tty, cp, fp, count);
}
/*
* This subroutine initializes a tty structure.
*/
static void initialize_tty_struct(struct tty_struct *tty)
{
memset(tty, 0, sizeof(struct tty_struct));
tty->magic = TTY_MAGIC;
tty->ldisc = ldiscs[N_TTY];
tty->pgrp = -1;
tty->flip.char_buf_ptr = tty->flip.char_buf;
tty->flip.flag_buf_ptr = tty->flip.flag_buf;
tty->flip.tqueue.routine = flush_to_ldisc;
tty->flip.tqueue.data = tty;
}
/*
* The default put_char routine if the driver did not define one.
*/
void tty_default_put_char(struct tty_struct *tty, unsigned char ch)
{
tty->driver.write(tty, 0, &ch, 1);
}
/*
* Called by a tty driver to register itself.
*/
int tty_register_driver(struct tty_driver *driver)
{
int error;
if (driver->flags & TTY_DRIVER_INSTALLED)
return 0;
error = register_chrdev(driver->major, driver->name, &tty_fops);
if (error < 0)
return error;
else if(driver->major == 0)
driver->major = error;
if (!driver->put_char)
driver->put_char = tty_default_put_char;
driver->prev = 0;
driver->next = tty_drivers;
if (tty_drivers) tty_drivers->prev = driver;
tty_drivers = driver;
return error;
}
/*
* Called by a tty driver to unregister itself.
*/
int tty_unregister_driver(struct tty_driver *driver)
{
int retval;
struct tty_driver *p;
int found = 0;
const char *othername = NULL;
if (*driver->refcount)
return -EBUSY;
for (p = tty_drivers; p; p = p->next) {
if (p == driver)
found++;
else if (p->major == driver->major)
othername = p->name;
}
if (othername == NULL) {
retval = unregister_chrdev(driver->major, driver->name);
if (retval)
return retval;
} else
register_chrdev(driver->major, othername, &tty_fops);
if (driver->prev)
driver->prev->next = driver->next;
else
tty_drivers = driver->next;
if (driver->next)
driver->next->prev = driver->prev;
return 0;
}
/*
* Initialize the console device. This is called *early*, so
* we can't necessarily depend on lots of kernel help here.
* Just do some early initializations, and do the complex setup
* later.
*/
long console_init(long kmem_start, long kmem_end)
{
/* Setup the default TTY line discipline. */
memset(ldiscs, 0, sizeof(ldiscs));
(void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
/*
* Set up the standard termios. Individual tty drivers may
* deviate from this; this is used as a template.
*/
memset(&tty_std_termios, 0, sizeof(struct termios));
memcpy(tty_std_termios.c_cc, INIT_C_CC, NCCS);
tty_std_termios.c_iflag = ICRNL | IXON;
tty_std_termios.c_oflag = OPOST | ONLCR;
tty_std_termios.c_cflag = B38400 | CS8 | CREAD;
tty_std_termios.c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
ECHOCTL | ECHOKE | IEXTEN;
/*
* set up the console device so that later boot sequences can
* inform about problems etc..
*/
return con_init(kmem_start);
}
/*
* Ok, now we can initialize the rest of the tty devices and can count
* on memory allocations, interrupts etc..
*/
long tty_init(long kmem_start)
{
if (sizeof(struct tty_struct) > PAGE_SIZE)
panic("size of tty structure > PAGE_SIZE!");
if (register_chrdev(TTY_MAJOR,"tty",&tty_fops))
panic("unable to get major %d for tty device", TTY_MAJOR);
if (register_chrdev(TTYAUX_MAJOR,"cua",&tty_fops))
panic("unable to get major %d for tty device", TTYAUX_MAJOR);
kmem_start = kbd_init(kmem_start);
kmem_start = rs_init(kmem_start);
#ifdef CONFIG_SCC
kmem_start = scc_init(kmem_start);
#endif
#ifdef CONFIG_CYCLADES
kmem_start = cy_init(kmem_start);
#endif
kmem_start = pty_init(kmem_start);
kmem_start = vcs_init(kmem_start);
return kmem_start;
}
|