aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/char/tty_io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/char/tty_io.c')
-rw-r--r--drivers/char/tty_io.c151
1 files changed, 86 insertions, 65 deletions
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index 59499ee0fe6a..684f0e0b175e 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -142,7 +142,6 @@ ssize_t redirected_tty_write(struct file *, const char __user *,
142 size_t, loff_t *); 142 size_t, loff_t *);
143static unsigned int tty_poll(struct file *, poll_table *); 143static unsigned int tty_poll(struct file *, poll_table *);
144static int tty_open(struct inode *, struct file *); 144static int tty_open(struct inode *, struct file *);
145static int tty_release(struct inode *, struct file *);
146long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); 145long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
147#ifdef CONFIG_COMPAT 146#ifdef CONFIG_COMPAT
148static long tty_compat_ioctl(struct file *file, unsigned int cmd, 147static long tty_compat_ioctl(struct file *file, unsigned int cmd,
@@ -506,8 +505,6 @@ static void do_tty_hangup(struct work_struct *work)
506 if (!tty) 505 if (!tty)
507 return; 506 return;
508 507
509 /* inuse_filps is protected by the single kernel lock */
510 lock_kernel();
511 508
512 spin_lock(&redirect_lock); 509 spin_lock(&redirect_lock);
513 if (redirect && redirect->private_data == tty) { 510 if (redirect && redirect->private_data == tty) {
@@ -516,7 +513,11 @@ static void do_tty_hangup(struct work_struct *work)
516 } 513 }
517 spin_unlock(&redirect_lock); 514 spin_unlock(&redirect_lock);
518 515
516 /* inuse_filps is protected by the single kernel lock */
517 lock_kernel();
519 check_tty_count(tty, "do_tty_hangup"); 518 check_tty_count(tty, "do_tty_hangup");
519 unlock_kernel();
520
520 file_list_lock(); 521 file_list_lock();
521 /* This breaks for file handles being sent over AF_UNIX sockets ? */ 522 /* This breaks for file handles being sent over AF_UNIX sockets ? */
522 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) { 523 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
@@ -530,6 +531,7 @@ static void do_tty_hangup(struct work_struct *work)
530 } 531 }
531 file_list_unlock(); 532 file_list_unlock();
532 533
534 lock_kernel();
533 tty_ldisc_hangup(tty); 535 tty_ldisc_hangup(tty);
534 536
535 read_lock(&tasklist_lock); 537 read_lock(&tasklist_lock);
@@ -708,6 +710,8 @@ void disassociate_ctty(int on_exit)
708 struct tty_struct *tty; 710 struct tty_struct *tty;
709 struct pid *tty_pgrp = NULL; 711 struct pid *tty_pgrp = NULL;
710 712
713 if (!current->signal->leader)
714 return;
711 715
712 tty = get_current_tty(); 716 tty = get_current_tty();
713 if (tty) { 717 if (tty) {
@@ -773,8 +777,7 @@ void no_tty(void)
773{ 777{
774 struct task_struct *tsk = current; 778 struct task_struct *tsk = current;
775 lock_kernel(); 779 lock_kernel();
776 if (tsk->signal->leader) 780 disassociate_ctty(0);
777 disassociate_ctty(0);
778 unlock_kernel(); 781 unlock_kernel();
779 proc_clear_tty(tsk); 782 proc_clear_tty(tsk);
780} 783}
@@ -1017,14 +1020,16 @@ out:
1017 1020
1018void tty_write_message(struct tty_struct *tty, char *msg) 1021void tty_write_message(struct tty_struct *tty, char *msg)
1019{ 1022{
1020 lock_kernel();
1021 if (tty) { 1023 if (tty) {
1022 mutex_lock(&tty->atomic_write_lock); 1024 mutex_lock(&tty->atomic_write_lock);
1023 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) 1025 lock_kernel();
1026 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) {
1027 unlock_kernel();
1024 tty->ops->write(tty, msg, strlen(msg)); 1028 tty->ops->write(tty, msg, strlen(msg));
1029 } else
1030 unlock_kernel();
1025 tty_write_unlock(tty); 1031 tty_write_unlock(tty);
1026 } 1032 }
1027 unlock_kernel();
1028 return; 1033 return;
1029} 1034}
1030 1035
@@ -1202,14 +1207,21 @@ static int tty_driver_install_tty(struct tty_driver *driver,
1202 struct tty_struct *tty) 1207 struct tty_struct *tty)
1203{ 1208{
1204 int idx = tty->index; 1209 int idx = tty->index;
1210 int ret;
1205 1211
1206 if (driver->ops->install) 1212 if (driver->ops->install) {
1207 return driver->ops->install(driver, tty); 1213 lock_kernel();
1214 ret = driver->ops->install(driver, tty);
1215 unlock_kernel();
1216 return ret;
1217 }
1208 1218
1209 if (tty_init_termios(tty) == 0) { 1219 if (tty_init_termios(tty) == 0) {
1220 lock_kernel();
1210 tty_driver_kref_get(driver); 1221 tty_driver_kref_get(driver);
1211 tty->count++; 1222 tty->count++;
1212 driver->ttys[idx] = tty; 1223 driver->ttys[idx] = tty;
1224 unlock_kernel();
1213 return 0; 1225 return 0;
1214 } 1226 }
1215 return -ENOMEM; 1227 return -ENOMEM;
@@ -1302,10 +1314,14 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1302 struct tty_struct *tty; 1314 struct tty_struct *tty;
1303 int retval; 1315 int retval;
1304 1316
1317 lock_kernel();
1305 /* Check if pty master is being opened multiple times */ 1318 /* Check if pty master is being opened multiple times */
1306 if (driver->subtype == PTY_TYPE_MASTER && 1319 if (driver->subtype == PTY_TYPE_MASTER &&
1307 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) 1320 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok) {
1321 unlock_kernel();
1308 return ERR_PTR(-EIO); 1322 return ERR_PTR(-EIO);
1323 }
1324 unlock_kernel();
1309 1325
1310 /* 1326 /*
1311 * First time open is complex, especially for PTY devices. 1327 * First time open is complex, especially for PTY devices.
@@ -1335,7 +1351,6 @@ struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1335 * If we fail here just call release_tty to clean up. No need 1351 * If we fail here just call release_tty to clean up. No need
1336 * to decrement the use counts, as release_tty doesn't care. 1352 * to decrement the use counts, as release_tty doesn't care.
1337 */ 1353 */
1338
1339 retval = tty_ldisc_setup(tty, tty->link); 1354 retval = tty_ldisc_setup(tty, tty->link);
1340 if (retval) 1355 if (retval)
1341 goto release_mem_out; 1356 goto release_mem_out;
@@ -1350,7 +1365,9 @@ release_mem_out:
1350 if (printk_ratelimit()) 1365 if (printk_ratelimit())
1351 printk(KERN_INFO "tty_init_dev: ldisc open failed, " 1366 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1352 "clearing slot %d\n", idx); 1367 "clearing slot %d\n", idx);
1368 lock_kernel();
1353 release_tty(tty, idx); 1369 release_tty(tty, idx);
1370 unlock_kernel();
1354 return ERR_PTR(retval); 1371 return ERR_PTR(retval);
1355} 1372}
1356 1373
@@ -1464,7 +1481,17 @@ static void release_tty(struct tty_struct *tty, int idx)
1464 tty_kref_put(tty); 1481 tty_kref_put(tty);
1465} 1482}
1466 1483
1467/* 1484/**
1485 * tty_release - vfs callback for close
1486 * @inode: inode of tty
1487 * @filp: file pointer for handle to tty
1488 *
1489 * Called the last time each file handle is closed that references
1490 * this tty. There may however be several such references.
1491 *
1492 * Locking:
1493 * Takes bkl. See tty_release_dev
1494 *
1468 * Even releasing the tty structures is a tricky business.. We have 1495 * Even releasing the tty structures is a tricky business.. We have
1469 * to be very careful that the structures are all released at the 1496 * to be very careful that the structures are all released at the
1470 * same time, as interrupts might otherwise get the wrong pointers. 1497 * same time, as interrupts might otherwise get the wrong pointers.
@@ -1472,20 +1499,20 @@ static void release_tty(struct tty_struct *tty, int idx)
1472 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could 1499 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1473 * lead to double frees or releasing memory still in use. 1500 * lead to double frees or releasing memory still in use.
1474 */ 1501 */
1475void tty_release_dev(struct file *filp) 1502
1503int tty_release(struct inode *inode, struct file *filp)
1476{ 1504{
1477 struct tty_struct *tty, *o_tty; 1505 struct tty_struct *tty, *o_tty;
1478 int pty_master, tty_closing, o_tty_closing, do_sleep; 1506 int pty_master, tty_closing, o_tty_closing, do_sleep;
1479 int devpts; 1507 int devpts;
1480 int idx; 1508 int idx;
1481 char buf[64]; 1509 char buf[64];
1482 struct inode *inode;
1483 1510
1484 inode = filp->f_path.dentry->d_inode;
1485 tty = (struct tty_struct *)filp->private_data; 1511 tty = (struct tty_struct *)filp->private_data;
1486 if (tty_paranoia_check(tty, inode, "tty_release_dev")) 1512 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1487 return; 1513 return 0;
1488 1514
1515 lock_kernel();
1489 check_tty_count(tty, "tty_release_dev"); 1516 check_tty_count(tty, "tty_release_dev");
1490 1517
1491 tty_fasync(-1, filp, 0); 1518 tty_fasync(-1, filp, 0);
@@ -1500,19 +1527,22 @@ void tty_release_dev(struct file *filp)
1500 if (idx < 0 || idx >= tty->driver->num) { 1527 if (idx < 0 || idx >= tty->driver->num) {
1501 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to " 1528 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1502 "free (%s)\n", tty->name); 1529 "free (%s)\n", tty->name);
1503 return; 1530 unlock_kernel();
1531 return 0;
1504 } 1532 }
1505 if (!devpts) { 1533 if (!devpts) {
1506 if (tty != tty->driver->ttys[idx]) { 1534 if (tty != tty->driver->ttys[idx]) {
1535 unlock_kernel();
1507 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty " 1536 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1508 "for (%s)\n", idx, tty->name); 1537 "for (%s)\n", idx, tty->name);
1509 return; 1538 return 0;
1510 } 1539 }
1511 if (tty->termios != tty->driver->termios[idx]) { 1540 if (tty->termios != tty->driver->termios[idx]) {
1541 unlock_kernel();
1512 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios " 1542 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1513 "for (%s)\n", 1543 "for (%s)\n",
1514 idx, tty->name); 1544 idx, tty->name);
1515 return; 1545 return 0;
1516 } 1546 }
1517 } 1547 }
1518#endif 1548#endif
@@ -1526,26 +1556,30 @@ void tty_release_dev(struct file *filp)
1526 if (tty->driver->other && 1556 if (tty->driver->other &&
1527 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { 1557 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1528 if (o_tty != tty->driver->other->ttys[idx]) { 1558 if (o_tty != tty->driver->other->ttys[idx]) {
1559 unlock_kernel();
1529 printk(KERN_DEBUG "tty_release_dev: other->table[%d] " 1560 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1530 "not o_tty for (%s)\n", 1561 "not o_tty for (%s)\n",
1531 idx, tty->name); 1562 idx, tty->name);
1532 return; 1563 return 0 ;
1533 } 1564 }
1534 if (o_tty->termios != tty->driver->other->termios[idx]) { 1565 if (o_tty->termios != tty->driver->other->termios[idx]) {
1566 unlock_kernel();
1535 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] " 1567 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1536 "not o_termios for (%s)\n", 1568 "not o_termios for (%s)\n",
1537 idx, tty->name); 1569 idx, tty->name);
1538 return; 1570 return 0;
1539 } 1571 }
1540 if (o_tty->link != tty) { 1572 if (o_tty->link != tty) {
1573 unlock_kernel();
1541 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n"); 1574 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1542 return; 1575 return 0;
1543 } 1576 }
1544 } 1577 }
1545#endif 1578#endif
1546 if (tty->ops->close) 1579 if (tty->ops->close)
1547 tty->ops->close(tty, filp); 1580 tty->ops->close(tty, filp);
1548 1581
1582 unlock_kernel();
1549 /* 1583 /*
1550 * Sanity check: if tty->count is going to zero, there shouldn't be 1584 * Sanity check: if tty->count is going to zero, there shouldn't be
1551 * any waiters on tty->read_wait or tty->write_wait. We test the 1585 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1568,6 +1602,7 @@ void tty_release_dev(struct file *filp)
1568 opens on /dev/tty */ 1602 opens on /dev/tty */
1569 1603
1570 mutex_lock(&tty_mutex); 1604 mutex_lock(&tty_mutex);
1605 lock_kernel();
1571 tty_closing = tty->count <= 1; 1606 tty_closing = tty->count <= 1;
1572 o_tty_closing = o_tty && 1607 o_tty_closing = o_tty &&
1573 (o_tty->count <= (pty_master ? 1 : 0)); 1608 (o_tty->count <= (pty_master ? 1 : 0));
@@ -1598,6 +1633,7 @@ void tty_release_dev(struct file *filp)
1598 1633
1599 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue " 1634 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1600 "active!\n", tty_name(tty, buf)); 1635 "active!\n", tty_name(tty, buf));
1636 unlock_kernel();
1601 mutex_unlock(&tty_mutex); 1637 mutex_unlock(&tty_mutex);
1602 schedule(); 1638 schedule();
1603 } 1639 }
@@ -1661,8 +1697,10 @@ void tty_release_dev(struct file *filp)
1661 mutex_unlock(&tty_mutex); 1697 mutex_unlock(&tty_mutex);
1662 1698
1663 /* check whether both sides are closing ... */ 1699 /* check whether both sides are closing ... */
1664 if (!tty_closing || (o_tty && !o_tty_closing)) 1700 if (!tty_closing || (o_tty && !o_tty_closing)) {
1665 return; 1701 unlock_kernel();
1702 return 0;
1703 }
1666 1704
1667#ifdef TTY_DEBUG_HANGUP 1705#ifdef TTY_DEBUG_HANGUP
1668 printk(KERN_DEBUG "freeing tty structure..."); 1706 printk(KERN_DEBUG "freeing tty structure...");
@@ -1680,10 +1718,12 @@ void tty_release_dev(struct file *filp)
1680 /* Make this pty number available for reallocation */ 1718 /* Make this pty number available for reallocation */
1681 if (devpts) 1719 if (devpts)
1682 devpts_kill_index(inode, idx); 1720 devpts_kill_index(inode, idx);
1721 unlock_kernel();
1722 return 0;
1683} 1723}
1684 1724
1685/** 1725/**
1686 * __tty_open - open a tty device 1726 * tty_open - open a tty device
1687 * @inode: inode of device file 1727 * @inode: inode of device file
1688 * @filp: file pointer to tty 1728 * @filp: file pointer to tty
1689 * 1729 *
@@ -1703,7 +1743,7 @@ void tty_release_dev(struct file *filp)
1703 * ->siglock protects ->signal/->sighand 1743 * ->siglock protects ->signal/->sighand
1704 */ 1744 */
1705 1745
1706static int __tty_open(struct inode *inode, struct file *filp) 1746static int tty_open(struct inode *inode, struct file *filp)
1707{ 1747{
1708 struct tty_struct *tty = NULL; 1748 struct tty_struct *tty = NULL;
1709 int noctty, retval; 1749 int noctty, retval;
@@ -1720,10 +1760,12 @@ retry_open:
1720 retval = 0; 1760 retval = 0;
1721 1761
1722 mutex_lock(&tty_mutex); 1762 mutex_lock(&tty_mutex);
1763 lock_kernel();
1723 1764
1724 if (device == MKDEV(TTYAUX_MAJOR, 0)) { 1765 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1725 tty = get_current_tty(); 1766 tty = get_current_tty();
1726 if (!tty) { 1767 if (!tty) {
1768 unlock_kernel();
1727 mutex_unlock(&tty_mutex); 1769 mutex_unlock(&tty_mutex);
1728 return -ENXIO; 1770 return -ENXIO;
1729 } 1771 }
@@ -1755,12 +1797,14 @@ retry_open:
1755 goto got_driver; 1797 goto got_driver;
1756 } 1798 }
1757 } 1799 }
1800 unlock_kernel();
1758 mutex_unlock(&tty_mutex); 1801 mutex_unlock(&tty_mutex);
1759 return -ENODEV; 1802 return -ENODEV;
1760 } 1803 }
1761 1804
1762 driver = get_tty_driver(device, &index); 1805 driver = get_tty_driver(device, &index);
1763 if (!driver) { 1806 if (!driver) {
1807 unlock_kernel();
1764 mutex_unlock(&tty_mutex); 1808 mutex_unlock(&tty_mutex);
1765 return -ENODEV; 1809 return -ENODEV;
1766 } 1810 }
@@ -1770,6 +1814,7 @@ got_driver:
1770 tty = tty_driver_lookup_tty(driver, inode, index); 1814 tty = tty_driver_lookup_tty(driver, inode, index);
1771 1815
1772 if (IS_ERR(tty)) { 1816 if (IS_ERR(tty)) {
1817 unlock_kernel();
1773 mutex_unlock(&tty_mutex); 1818 mutex_unlock(&tty_mutex);
1774 return PTR_ERR(tty); 1819 return PTR_ERR(tty);
1775 } 1820 }
@@ -1784,8 +1829,10 @@ got_driver:
1784 1829
1785 mutex_unlock(&tty_mutex); 1830 mutex_unlock(&tty_mutex);
1786 tty_driver_kref_put(driver); 1831 tty_driver_kref_put(driver);
1787 if (IS_ERR(tty)) 1832 if (IS_ERR(tty)) {
1833 unlock_kernel();
1788 return PTR_ERR(tty); 1834 return PTR_ERR(tty);
1835 }
1789 1836
1790 filp->private_data = tty; 1837 filp->private_data = tty;
1791 file_move(filp, &tty->tty_files); 1838 file_move(filp, &tty->tty_files);
@@ -1813,11 +1860,15 @@ got_driver:
1813 printk(KERN_DEBUG "error %d in opening %s...", retval, 1860 printk(KERN_DEBUG "error %d in opening %s...", retval,
1814 tty->name); 1861 tty->name);
1815#endif 1862#endif
1816 tty_release_dev(filp); 1863 tty_release(inode, filp);
1817 if (retval != -ERESTARTSYS) 1864 if (retval != -ERESTARTSYS) {
1865 unlock_kernel();
1818 return retval; 1866 return retval;
1819 if (signal_pending(current)) 1867 }
1868 if (signal_pending(current)) {
1869 unlock_kernel();
1820 return retval; 1870 return retval;
1871 }
1821 schedule(); 1872 schedule();
1822 /* 1873 /*
1823 * Need to reset f_op in case a hangup happened. 1874 * Need to reset f_op in case a hangup happened.
@@ -1826,8 +1877,11 @@ got_driver:
1826 filp->f_op = &tty_fops; 1877 filp->f_op = &tty_fops;
1827 goto retry_open; 1878 goto retry_open;
1828 } 1879 }
1880 unlock_kernel();
1881
1829 1882
1830 mutex_lock(&tty_mutex); 1883 mutex_lock(&tty_mutex);
1884 lock_kernel();
1831 spin_lock_irq(&current->sighand->siglock); 1885 spin_lock_irq(&current->sighand->siglock);
1832 if (!noctty && 1886 if (!noctty &&
1833 current->signal->leader && 1887 current->signal->leader &&
@@ -1835,43 +1889,12 @@ got_driver:
1835 tty->session == NULL) 1889 tty->session == NULL)
1836 __proc_set_tty(current, tty); 1890 __proc_set_tty(current, tty);
1837 spin_unlock_irq(&current->sighand->siglock); 1891 spin_unlock_irq(&current->sighand->siglock);
1892 unlock_kernel();
1838 mutex_unlock(&tty_mutex); 1893 mutex_unlock(&tty_mutex);
1839 return 0; 1894 return 0;
1840} 1895}
1841 1896
1842/* BKL pushdown: scary code avoidance wrapper */
1843static int tty_open(struct inode *inode, struct file *filp)
1844{
1845 int ret;
1846
1847 lock_kernel();
1848 ret = __tty_open(inode, filp);
1849 unlock_kernel();
1850 return ret;
1851}
1852
1853
1854
1855
1856/**
1857 * tty_release - vfs callback for close
1858 * @inode: inode of tty
1859 * @filp: file pointer for handle to tty
1860 *
1861 * Called the last time each file handle is closed that references
1862 * this tty. There may however be several such references.
1863 *
1864 * Locking:
1865 * Takes bkl. See tty_release_dev
1866 */
1867 1897
1868static int tty_release(struct inode *inode, struct file *filp)
1869{
1870 lock_kernel();
1871 tty_release_dev(filp);
1872 unlock_kernel();
1873 return 0;
1874}
1875 1898
1876/** 1899/**
1877 * tty_poll - check tty status 1900 * tty_poll - check tty status
@@ -2317,9 +2340,7 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)
2317 if (get_user(ldisc, p)) 2340 if (get_user(ldisc, p))
2318 return -EFAULT; 2341 return -EFAULT;
2319 2342
2320 lock_kernel();
2321 ret = tty_set_ldisc(tty, ldisc); 2343 ret = tty_set_ldisc(tty, ldisc);
2322 unlock_kernel();
2323 2344
2324 return ret; 2345 return ret;
2325} 2346}