aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/tty/tty_io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/tty/tty_io.c')
-rw-r--r--drivers/tty/tty_io.c440
1 files changed, 223 insertions, 217 deletions
diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index 0508a1d8e4cd..4f35b43e2475 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -153,8 +153,6 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,
153static int __tty_fasync(int fd, struct file *filp, int on); 153static int __tty_fasync(int fd, struct file *filp, int on);
154static int tty_fasync(int fd, struct file *filp, int on); 154static int tty_fasync(int fd, struct file *filp, int on);
155static void release_tty(struct tty_struct *tty, int idx); 155static void release_tty(struct tty_struct *tty, int idx);
156static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
157static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
158 156
159/** 157/**
160 * free_tty_struct - free a disused tty 158 * free_tty_struct - free a disused tty
@@ -169,8 +167,7 @@ void free_tty_struct(struct tty_struct *tty)
169{ 167{
170 if (!tty) 168 if (!tty)
171 return; 169 return;
172 if (tty->dev) 170 put_device(tty->dev);
173 put_device(tty->dev);
174 kfree(tty->write_buf); 171 kfree(tty->write_buf);
175 tty->magic = 0xDEADDEAD; 172 tty->magic = 0xDEADDEAD;
176 kfree(tty); 173 kfree(tty);
@@ -277,6 +274,7 @@ int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
277 return 0; 274 return 0;
278} 275}
279 276
277/* Caller must hold tty_lock */
280static int check_tty_count(struct tty_struct *tty, const char *routine) 278static int check_tty_count(struct tty_struct *tty, const char *routine)
281{ 279{
282#ifdef CHECK_TTY_COUNT 280#ifdef CHECK_TTY_COUNT
@@ -492,6 +490,78 @@ static const struct file_operations hung_up_tty_fops = {
492static DEFINE_SPINLOCK(redirect_lock); 490static DEFINE_SPINLOCK(redirect_lock);
493static struct file *redirect; 491static struct file *redirect;
494 492
493
494void proc_clear_tty(struct task_struct *p)
495{
496 unsigned long flags;
497 struct tty_struct *tty;
498 spin_lock_irqsave(&p->sighand->siglock, flags);
499 tty = p->signal->tty;
500 p->signal->tty = NULL;
501 spin_unlock_irqrestore(&p->sighand->siglock, flags);
502 tty_kref_put(tty);
503}
504
505/**
506 * proc_set_tty - set the controlling terminal
507 *
508 * Only callable by the session leader and only if it does not already have
509 * a controlling terminal.
510 *
511 * Caller must hold: tty_lock()
512 * a readlock on tasklist_lock
513 * sighand lock
514 */
515static void __proc_set_tty(struct tty_struct *tty)
516{
517 unsigned long flags;
518
519 spin_lock_irqsave(&tty->ctrl_lock, flags);
520 /*
521 * The session and fg pgrp references will be non-NULL if
522 * tiocsctty() is stealing the controlling tty
523 */
524 put_pid(tty->session);
525 put_pid(tty->pgrp);
526 tty->pgrp = get_pid(task_pgrp(current));
527 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
528 tty->session = get_pid(task_session(current));
529 if (current->signal->tty) {
530 printk(KERN_DEBUG "tty not NULL!!\n");
531 tty_kref_put(current->signal->tty);
532 }
533 put_pid(current->signal->tty_old_pgrp);
534 current->signal->tty = tty_kref_get(tty);
535 current->signal->tty_old_pgrp = NULL;
536}
537
538static void proc_set_tty(struct tty_struct *tty)
539{
540 spin_lock_irq(&current->sighand->siglock);
541 __proc_set_tty(tty);
542 spin_unlock_irq(&current->sighand->siglock);
543}
544
545struct tty_struct *get_current_tty(void)
546{
547 struct tty_struct *tty;
548 unsigned long flags;
549
550 spin_lock_irqsave(&current->sighand->siglock, flags);
551 tty = tty_kref_get(current->signal->tty);
552 spin_unlock_irqrestore(&current->sighand->siglock, flags);
553 return tty;
554}
555EXPORT_SYMBOL_GPL(get_current_tty);
556
557static void session_clear_tty(struct pid *session)
558{
559 struct task_struct *p;
560 do_each_pid_task(session, PIDTYPE_SID, p) {
561 proc_clear_tty(p);
562 } while_each_pid_task(session, PIDTYPE_SID, p);
563}
564
495/** 565/**
496 * tty_wakeup - request more data 566 * tty_wakeup - request more data
497 * @tty: terminal 567 * @tty: terminal
@@ -620,9 +690,6 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
620 return; 690 return;
621 } 691 }
622 692
623 /* some functions below drop BTM, so we need this bit */
624 set_bit(TTY_HUPPING, &tty->flags);
625
626 /* inuse_filps is protected by the single tty lock, 693 /* inuse_filps is protected by the single tty lock,
627 this really needs to change if we want to flush the 694 this really needs to change if we want to flush the
628 workqueue with the lock held */ 695 workqueue with the lock held */
@@ -647,10 +714,6 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
647 while (refs--) 714 while (refs--)
648 tty_kref_put(tty); 715 tty_kref_put(tty);
649 716
650 /*
651 * it drops BTM and thus races with reopen
652 * we protect the race by TTY_HUPPING
653 */
654 tty_ldisc_hangup(tty); 717 tty_ldisc_hangup(tty);
655 718
656 spin_lock_irq(&tty->ctrl_lock); 719 spin_lock_irq(&tty->ctrl_lock);
@@ -682,8 +745,6 @@ static void __tty_hangup(struct tty_struct *tty, int exit_session)
682 * can't yet guarantee all that. 745 * can't yet guarantee all that.
683 */ 746 */
684 set_bit(TTY_HUPPED, &tty->flags); 747 set_bit(TTY_HUPPED, &tty->flags);
685 clear_bit(TTY_HUPPING, &tty->flags);
686
687 tty_unlock(tty); 748 tty_unlock(tty);
688 749
689 if (f) 750 if (f)
@@ -792,14 +853,6 @@ int tty_hung_up_p(struct file *filp)
792 853
793EXPORT_SYMBOL(tty_hung_up_p); 854EXPORT_SYMBOL(tty_hung_up_p);
794 855
795static void session_clear_tty(struct pid *session)
796{
797 struct task_struct *p;
798 do_each_pid_task(session, PIDTYPE_SID, p) {
799 proc_clear_tty(p);
800 } while_each_pid_task(session, PIDTYPE_SID, p);
801}
802
803/** 856/**
804 * disassociate_ctty - disconnect controlling tty 857 * disassociate_ctty - disconnect controlling tty
805 * @on_exit: true if exiting so need to "hang up" the session 858 * @on_exit: true if exiting so need to "hang up" the session
@@ -927,7 +980,7 @@ void __stop_tty(struct tty_struct *tty)
927 return; 980 return;
928 tty->stopped = 1; 981 tty->stopped = 1;
929 if (tty->ops->stop) 982 if (tty->ops->stop)
930 (tty->ops->stop)(tty); 983 tty->ops->stop(tty);
931} 984}
932 985
933void stop_tty(struct tty_struct *tty) 986void stop_tty(struct tty_struct *tty)
@@ -958,7 +1011,7 @@ void __start_tty(struct tty_struct *tty)
958 return; 1011 return;
959 tty->stopped = 0; 1012 tty->stopped = 0;
960 if (tty->ops->start) 1013 if (tty->ops->start)
961 (tty->ops->start)(tty); 1014 tty->ops->start(tty);
962 tty_wakeup(tty); 1015 tty_wakeup(tty);
963} 1016}
964 1017
@@ -1012,7 +1065,7 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1012 situation */ 1065 situation */
1013 ld = tty_ldisc_ref_wait(tty); 1066 ld = tty_ldisc_ref_wait(tty);
1014 if (ld->ops->read) 1067 if (ld->ops->read)
1015 i = (ld->ops->read)(tty, file, buf, count); 1068 i = ld->ops->read(tty, file, buf, count);
1016 else 1069 else
1017 i = -EIO; 1070 i = -EIO;
1018 tty_ldisc_deref(ld); 1071 tty_ldisc_deref(ld);
@@ -1024,14 +1077,12 @@ static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
1024} 1077}
1025 1078
1026static void tty_write_unlock(struct tty_struct *tty) 1079static void tty_write_unlock(struct tty_struct *tty)
1027 __releases(&tty->atomic_write_lock)
1028{ 1080{
1029 mutex_unlock(&tty->atomic_write_lock); 1081 mutex_unlock(&tty->atomic_write_lock);
1030 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); 1082 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
1031} 1083}
1032 1084
1033static int tty_write_lock(struct tty_struct *tty, int ndelay) 1085static int tty_write_lock(struct tty_struct *tty, int ndelay)
1034 __acquires(&tty->atomic_write_lock)
1035{ 1086{
1036 if (!mutex_trylock(&tty->atomic_write_lock)) { 1087 if (!mutex_trylock(&tty->atomic_write_lock)) {
1037 if (ndelay) 1088 if (ndelay)
@@ -1146,7 +1197,7 @@ void tty_write_message(struct tty_struct *tty, char *msg)
1146 if (tty) { 1197 if (tty) {
1147 mutex_lock(&tty->atomic_write_lock); 1198 mutex_lock(&tty->atomic_write_lock);
1148 tty_lock(tty); 1199 tty_lock(tty);
1149 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { 1200 if (tty->ops->write && tty->count > 0) {
1150 tty_unlock(tty); 1201 tty_unlock(tty);
1151 tty->ops->write(tty, msg, strlen(msg)); 1202 tty->ops->write(tty, msg, strlen(msg));
1152 } else 1203 } else
@@ -1293,19 +1344,24 @@ static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
1293 * @driver: the driver for the tty 1344 * @driver: the driver for the tty
1294 * @idx: the minor number 1345 * @idx: the minor number
1295 * 1346 *
1296 * Return the tty, if found or ERR_PTR() otherwise. 1347 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1348 * driver lookup() method returns an error.
1297 * 1349 *
1298 * Locking: tty_mutex must be held. If tty is found, the mutex must 1350 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
1299 * be held until the 'fast-open' is also done. Will change once we
1300 * have refcounting in the driver and per driver locking
1301 */ 1351 */
1302static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, 1352static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1303 struct inode *inode, int idx) 1353 struct inode *inode, int idx)
1304{ 1354{
1355 struct tty_struct *tty;
1356
1305 if (driver->ops->lookup) 1357 if (driver->ops->lookup)
1306 return driver->ops->lookup(driver, inode, idx); 1358 tty = driver->ops->lookup(driver, inode, idx);
1359 else
1360 tty = driver->ttys[idx];
1307 1361
1308 return driver->ttys[idx]; 1362 if (!IS_ERR(tty))
1363 tty_kref_get(tty);
1364 return tty;
1309} 1365}
1310 1366
1311/** 1367/**
@@ -1393,29 +1449,21 @@ void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
1393 * @tty - the tty to open 1449 * @tty - the tty to open
1394 * 1450 *
1395 * Return 0 on success, -errno on error. 1451 * Return 0 on success, -errno on error.
1452 * Re-opens on master ptys are not allowed and return -EIO.
1396 * 1453 *
1397 * Locking: tty_mutex must be held from the time the tty was found 1454 * Locking: Caller must hold tty_lock
1398 * till this open completes.
1399 */ 1455 */
1400static int tty_reopen(struct tty_struct *tty) 1456static int tty_reopen(struct tty_struct *tty)
1401{ 1457{
1402 struct tty_driver *driver = tty->driver; 1458 struct tty_driver *driver = tty->driver;
1403 1459
1404 if (test_bit(TTY_CLOSING, &tty->flags) || 1460 if (!tty->count)
1405 test_bit(TTY_HUPPING, &tty->flags))
1406 return -EIO; 1461 return -EIO;
1407 1462
1408 if (driver->type == TTY_DRIVER_TYPE_PTY && 1463 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1409 driver->subtype == PTY_TYPE_MASTER) { 1464 driver->subtype == PTY_TYPE_MASTER)
1410 /* 1465 return -EIO;
1411 * special case for PTY masters: only one open permitted,
1412 * and the slave side open count is incremented as well.
1413 */
1414 if (tty->count)
1415 return -EIO;
1416 1466
1417 tty->link->count++;
1418 }
1419 tty->count++; 1467 tty->count++;
1420 1468
1421 WARN_ON(!tty->ldisc); 1469 WARN_ON(!tty->ldisc);
@@ -1535,15 +1583,19 @@ void tty_free_termios(struct tty_struct *tty)
1535EXPORT_SYMBOL(tty_free_termios); 1583EXPORT_SYMBOL(tty_free_termios);
1536 1584
1537/** 1585/**
1538 * tty_flush_works - flush all works of a tty 1586 * tty_flush_works - flush all works of a tty/pty pair
1539 * @tty: tty device to flush works for 1587 * @tty: tty device to flush works for (or either end of a pty pair)
1540 * 1588 *
1541 * Sync flush all works belonging to @tty. 1589 * Sync flush all works belonging to @tty (and the 'other' tty).
1542 */ 1590 */
1543static void tty_flush_works(struct tty_struct *tty) 1591static void tty_flush_works(struct tty_struct *tty)
1544{ 1592{
1545 flush_work(&tty->SAK_work); 1593 flush_work(&tty->SAK_work);
1546 flush_work(&tty->hangup_work); 1594 flush_work(&tty->hangup_work);
1595 if (tty->link) {
1596 flush_work(&tty->link->SAK_work);
1597 flush_work(&tty->link->hangup_work);
1598 }
1547} 1599}
1548 1600
1549/** 1601/**
@@ -1635,8 +1687,7 @@ static void release_tty(struct tty_struct *tty, int idx)
1635 tty->link->port->itty = NULL; 1687 tty->link->port->itty = NULL;
1636 cancel_work_sync(&tty->port->buf.work); 1688 cancel_work_sync(&tty->port->buf.work);
1637 1689
1638 if (tty->link) 1690 tty_kref_put(tty->link);
1639 tty_kref_put(tty->link);
1640 tty_kref_put(tty); 1691 tty_kref_put(tty);
1641} 1692}
1642 1693
@@ -1649,8 +1700,7 @@ static void release_tty(struct tty_struct *tty, int idx)
1649 * Performs some paranoid checking before true release of the @tty. 1700 * Performs some paranoid checking before true release of the @tty.
1650 * This is a no-op unless TTY_PARANOIA_CHECK is defined. 1701 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1651 */ 1702 */
1652static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty, 1703static int tty_release_checks(struct tty_struct *tty, int idx)
1653 int idx)
1654{ 1704{
1655#ifdef TTY_PARANOIA_CHECK 1705#ifdef TTY_PARANOIA_CHECK
1656 if (idx < 0 || idx >= tty->driver->num) { 1706 if (idx < 0 || idx >= tty->driver->num) {
@@ -1669,6 +1719,8 @@ static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1669 return -1; 1719 return -1;
1670 } 1720 }
1671 if (tty->driver->other) { 1721 if (tty->driver->other) {
1722 struct tty_struct *o_tty = tty->link;
1723
1672 if (o_tty != tty->driver->other->ttys[idx]) { 1724 if (o_tty != tty->driver->other->ttys[idx]) {
1673 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n", 1725 printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n",
1674 __func__, idx, tty->name); 1726 __func__, idx, tty->name);
@@ -1705,8 +1757,8 @@ static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty,
1705int tty_release(struct inode *inode, struct file *filp) 1757int tty_release(struct inode *inode, struct file *filp)
1706{ 1758{
1707 struct tty_struct *tty = file_tty(filp); 1759 struct tty_struct *tty = file_tty(filp);
1708 struct tty_struct *o_tty; 1760 struct tty_struct *o_tty = NULL;
1709 int pty_master, tty_closing, o_tty_closing, do_sleep; 1761 int do_sleep, final;
1710 int idx; 1762 int idx;
1711 char buf[64]; 1763 char buf[64];
1712 long timeout = 0; 1764 long timeout = 0;
@@ -1721,12 +1773,11 @@ int tty_release(struct inode *inode, struct file *filp)
1721 __tty_fasync(-1, filp, 0); 1773 __tty_fasync(-1, filp, 0);
1722 1774
1723 idx = tty->index; 1775 idx = tty->index;
1724 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && 1776 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1725 tty->driver->subtype == PTY_TYPE_MASTER); 1777 tty->driver->subtype == PTY_TYPE_MASTER)
1726 /* Review: parallel close */ 1778 o_tty = tty->link;
1727 o_tty = tty->link;
1728 1779
1729 if (tty_release_checks(tty, o_tty, idx)) { 1780 if (tty_release_checks(tty, idx)) {
1730 tty_unlock(tty); 1781 tty_unlock(tty);
1731 return 0; 1782 return 0;
1732 } 1783 }
@@ -1739,7 +1790,9 @@ int tty_release(struct inode *inode, struct file *filp)
1739 if (tty->ops->close) 1790 if (tty->ops->close)
1740 tty->ops->close(tty, filp); 1791 tty->ops->close(tty, filp);
1741 1792
1742 tty_unlock(tty); 1793 /* If tty is pty master, lock the slave pty (stable lock order) */
1794 tty_lock_slave(o_tty);
1795
1743 /* 1796 /*
1744 * Sanity check: if tty->count is going to zero, there shouldn't be 1797 * Sanity check: if tty->count is going to zero, there shouldn't be
1745 * any waiters on tty->read_wait or tty->write_wait. We test the 1798 * any waiters on tty->read_wait or tty->write_wait. We test the
@@ -1750,25 +1803,13 @@ int tty_release(struct inode *inode, struct file *filp)
1750 * The test for the o_tty closing is necessary, since the master and 1803 * The test for the o_tty closing is necessary, since the master and
1751 * slave sides may close in any order. If the slave side closes out 1804 * slave sides may close in any order. If the slave side closes out
1752 * first, its count will be one, since the master side holds an open. 1805 * first, its count will be one, since the master side holds an open.
1753 * Thus this test wouldn't be triggered at the time the slave closes, 1806 * Thus this test wouldn't be triggered at the time the slave closed,
1754 * so we do it now. 1807 * so we do it now.
1755 *
1756 * Note that it's possible for the tty to be opened again while we're
1757 * flushing out waiters. By recalculating the closing flags before
1758 * each iteration we avoid any problems.
1759 */ 1808 */
1760 while (1) { 1809 while (1) {
1761 /* Guard against races with tty->count changes elsewhere and
1762 opens on /dev/tty */
1763
1764 mutex_lock(&tty_mutex);
1765 tty_lock_pair(tty, o_tty);
1766 tty_closing = tty->count <= 1;
1767 o_tty_closing = o_tty &&
1768 (o_tty->count <= (pty_master ? 1 : 0));
1769 do_sleep = 0; 1810 do_sleep = 0;
1770 1811
1771 if (tty_closing) { 1812 if (tty->count <= 1) {
1772 if (waitqueue_active(&tty->read_wait)) { 1813 if (waitqueue_active(&tty->read_wait)) {
1773 wake_up_poll(&tty->read_wait, POLLIN); 1814 wake_up_poll(&tty->read_wait, POLLIN);
1774 do_sleep++; 1815 do_sleep++;
@@ -1778,7 +1819,7 @@ int tty_release(struct inode *inode, struct file *filp)
1778 do_sleep++; 1819 do_sleep++;
1779 } 1820 }
1780 } 1821 }
1781 if (o_tty_closing) { 1822 if (o_tty && o_tty->count <= 1) {
1782 if (waitqueue_active(&o_tty->read_wait)) { 1823 if (waitqueue_active(&o_tty->read_wait)) {
1783 wake_up_poll(&o_tty->read_wait, POLLIN); 1824 wake_up_poll(&o_tty->read_wait, POLLIN);
1784 do_sleep++; 1825 do_sleep++;
@@ -1796,8 +1837,6 @@ int tty_release(struct inode *inode, struct file *filp)
1796 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", 1837 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
1797 __func__, tty_name(tty, buf)); 1838 __func__, tty_name(tty, buf));
1798 } 1839 }
1799 tty_unlock_pair(tty, o_tty);
1800 mutex_unlock(&tty_mutex);
1801 schedule_timeout_killable(timeout); 1840 schedule_timeout_killable(timeout);
1802 if (timeout < 120 * HZ) 1841 if (timeout < 120 * HZ)
1803 timeout = 2 * timeout + 1; 1842 timeout = 2 * timeout + 1;
@@ -1805,15 +1844,7 @@ int tty_release(struct inode *inode, struct file *filp)
1805 timeout = MAX_SCHEDULE_TIMEOUT; 1844 timeout = MAX_SCHEDULE_TIMEOUT;
1806 } 1845 }
1807 1846
1808 /* 1847 if (o_tty) {
1809 * The closing flags are now consistent with the open counts on
1810 * both sides, and we've completed the last operation that could
1811 * block, so it's safe to proceed with closing.
1812 *
1813 * We must *not* drop the tty_mutex until we ensure that a further
1814 * entry into tty_open can not pick up this tty.
1815 */
1816 if (pty_master) {
1817 if (--o_tty->count < 0) { 1848 if (--o_tty->count < 0) {
1818 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n", 1849 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
1819 __func__, o_tty->count, tty_name(o_tty, buf)); 1850 __func__, o_tty->count, tty_name(o_tty, buf));
@@ -1840,21 +1871,11 @@ int tty_release(struct inode *inode, struct file *filp)
1840 /* 1871 /*
1841 * Perform some housekeeping before deciding whether to return. 1872 * Perform some housekeeping before deciding whether to return.
1842 * 1873 *
1843 * Set the TTY_CLOSING flag if this was the last open. In the
1844 * case of a pty we may have to wait around for the other side
1845 * to close, and TTY_CLOSING makes sure we can't be reopened.
1846 */
1847 if (tty_closing)
1848 set_bit(TTY_CLOSING, &tty->flags);
1849 if (o_tty_closing)
1850 set_bit(TTY_CLOSING, &o_tty->flags);
1851
1852 /*
1853 * If _either_ side is closing, make sure there aren't any 1874 * If _either_ side is closing, make sure there aren't any
1854 * processes that still think tty or o_tty is their controlling 1875 * processes that still think tty or o_tty is their controlling
1855 * tty. 1876 * tty.
1856 */ 1877 */
1857 if (tty_closing || o_tty_closing) { 1878 if (!tty->count) {
1858 read_lock(&tasklist_lock); 1879 read_lock(&tasklist_lock);
1859 session_clear_tty(tty->session); 1880 session_clear_tty(tty->session);
1860 if (o_tty) 1881 if (o_tty)
@@ -1862,13 +1883,16 @@ int tty_release(struct inode *inode, struct file *filp)
1862 read_unlock(&tasklist_lock); 1883 read_unlock(&tasklist_lock);
1863 } 1884 }
1864 1885
1865 mutex_unlock(&tty_mutex); 1886 /* check whether both sides are closing ... */
1866 tty_unlock_pair(tty, o_tty); 1887 final = !tty->count && !(o_tty && o_tty->count);
1867 /* At this point the TTY_CLOSING flag should ensure a dead tty 1888
1889 tty_unlock_slave(o_tty);
1890 tty_unlock(tty);
1891
1892 /* At this point, the tty->count == 0 should ensure a dead tty
1868 cannot be re-opened by a racing opener */ 1893 cannot be re-opened by a racing opener */
1869 1894
1870 /* check whether both sides are closing ... */ 1895 if (!final)
1871 if (!tty_closing || (o_tty && !o_tty_closing))
1872 return 0; 1896 return 0;
1873 1897
1874#ifdef TTY_DEBUG_HANGUP 1898#ifdef TTY_DEBUG_HANGUP
@@ -1877,12 +1901,10 @@ int tty_release(struct inode *inode, struct file *filp)
1877 /* 1901 /*
1878 * Ask the line discipline code to release its structures 1902 * Ask the line discipline code to release its structures
1879 */ 1903 */
1880 tty_ldisc_release(tty, o_tty); 1904 tty_ldisc_release(tty);
1881 1905
1882 /* Wait for pending work before tty destruction commmences */ 1906 /* Wait for pending work before tty destruction commmences */
1883 tty_flush_works(tty); 1907 tty_flush_works(tty);
1884 if (o_tty)
1885 tty_flush_works(o_tty);
1886 1908
1887#ifdef TTY_DEBUG_HANGUP 1909#ifdef TTY_DEBUG_HANGUP
1888 printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf)); 1910 printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf));
@@ -1901,20 +1923,20 @@ int tty_release(struct inode *inode, struct file *filp)
1901} 1923}
1902 1924
1903/** 1925/**
1904 * tty_open_current_tty - get tty of current task for open 1926 * tty_open_current_tty - get locked tty of current task
1905 * @device: device number 1927 * @device: device number
1906 * @filp: file pointer to tty 1928 * @filp: file pointer to tty
1907 * @return: tty of the current task iff @device is /dev/tty 1929 * @return: locked tty of the current task iff @device is /dev/tty
1930 *
1931 * Performs a re-open of the current task's controlling tty.
1908 * 1932 *
1909 * We cannot return driver and index like for the other nodes because 1933 * We cannot return driver and index like for the other nodes because
1910 * devpts will not work then. It expects inodes to be from devpts FS. 1934 * devpts will not work then. It expects inodes to be from devpts FS.
1911 *
1912 * We need to move to returning a refcounted object from all the lookup
1913 * paths including this one.
1914 */ 1935 */
1915static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) 1936static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1916{ 1937{
1917 struct tty_struct *tty; 1938 struct tty_struct *tty;
1939 int retval;
1918 1940
1919 if (device != MKDEV(TTYAUX_MAJOR, 0)) 1941 if (device != MKDEV(TTYAUX_MAJOR, 0))
1920 return NULL; 1942 return NULL;
@@ -1925,9 +1947,14 @@ static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1925 1947
1926 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 1948 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1927 /* noctty = 1; */ 1949 /* noctty = 1; */
1928 tty_kref_put(tty); 1950 tty_lock(tty);
1929 /* FIXME: we put a reference and return a TTY! */ 1951 tty_kref_put(tty); /* safe to drop the kref now */
1930 /* This is only safe because the caller holds tty_mutex */ 1952
1953 retval = tty_reopen(tty);
1954 if (retval < 0) {
1955 tty_unlock(tty);
1956 tty = ERR_PTR(retval);
1957 }
1931 return tty; 1958 return tty;
1932} 1959}
1933 1960
@@ -2025,13 +2052,9 @@ retry_open:
2025 index = -1; 2052 index = -1;
2026 retval = 0; 2053 retval = 0;
2027 2054
2028 mutex_lock(&tty_mutex);
2029 /* This is protected by the tty_mutex */
2030 tty = tty_open_current_tty(device, filp); 2055 tty = tty_open_current_tty(device, filp);
2031 if (IS_ERR(tty)) { 2056 if (!tty) {
2032 retval = PTR_ERR(tty); 2057 mutex_lock(&tty_mutex);
2033 goto err_unlock;
2034 } else if (!tty) {
2035 driver = tty_lookup_driver(device, filp, &noctty, &index); 2058 driver = tty_lookup_driver(device, filp, &noctty, &index);
2036 if (IS_ERR(driver)) { 2059 if (IS_ERR(driver)) {
2037 retval = PTR_ERR(driver); 2060 retval = PTR_ERR(driver);
@@ -2044,21 +2067,25 @@ retry_open:
2044 retval = PTR_ERR(tty); 2067 retval = PTR_ERR(tty);
2045 goto err_unlock; 2068 goto err_unlock;
2046 } 2069 }
2047 }
2048 2070
2049 if (tty) { 2071 if (tty) {
2050 tty_lock(tty); 2072 mutex_unlock(&tty_mutex);
2051 retval = tty_reopen(tty); 2073 tty_lock(tty);
2052 if (retval < 0) { 2074 /* safe to drop the kref from tty_driver_lookup_tty() */
2053 tty_unlock(tty); 2075 tty_kref_put(tty);
2054 tty = ERR_PTR(retval); 2076 retval = tty_reopen(tty);
2077 if (retval < 0) {
2078 tty_unlock(tty);
2079 tty = ERR_PTR(retval);
2080 }
2081 } else { /* Returns with the tty_lock held for now */
2082 tty = tty_init_dev(driver, index);
2083 mutex_unlock(&tty_mutex);
2055 } 2084 }
2056 } else /* Returns with the tty_lock held for now */
2057 tty = tty_init_dev(driver, index);
2058 2085
2059 mutex_unlock(&tty_mutex);
2060 if (driver)
2061 tty_driver_kref_put(driver); 2086 tty_driver_kref_put(driver);
2087 }
2088
2062 if (IS_ERR(tty)) { 2089 if (IS_ERR(tty)) {
2063 retval = PTR_ERR(tty); 2090 retval = PTR_ERR(tty);
2064 goto err_file; 2091 goto err_file;
@@ -2100,25 +2127,23 @@ retry_open:
2100 /* 2127 /*
2101 * Need to reset f_op in case a hangup happened. 2128 * Need to reset f_op in case a hangup happened.
2102 */ 2129 */
2103 if (filp->f_op == &hung_up_tty_fops) 2130 if (tty_hung_up_p(filp))
2104 filp->f_op = &tty_fops; 2131 filp->f_op = &tty_fops;
2105 goto retry_open; 2132 goto retry_open;
2106 } 2133 }
2107 clear_bit(TTY_HUPPED, &tty->flags); 2134 clear_bit(TTY_HUPPED, &tty->flags);
2108 tty_unlock(tty);
2109 2135
2110 2136
2111 mutex_lock(&tty_mutex); 2137 read_lock(&tasklist_lock);
2112 tty_lock(tty);
2113 spin_lock_irq(&current->sighand->siglock); 2138 spin_lock_irq(&current->sighand->siglock);
2114 if (!noctty && 2139 if (!noctty &&
2115 current->signal->leader && 2140 current->signal->leader &&
2116 !current->signal->tty && 2141 !current->signal->tty &&
2117 tty->session == NULL) 2142 tty->session == NULL)
2118 __proc_set_tty(current, tty); 2143 __proc_set_tty(tty);
2119 spin_unlock_irq(&current->sighand->siglock); 2144 spin_unlock_irq(&current->sighand->siglock);
2145 read_unlock(&tasklist_lock);
2120 tty_unlock(tty); 2146 tty_unlock(tty);
2121 mutex_unlock(&tty_mutex);
2122 return 0; 2147 return 0;
2123err_unlock: 2148err_unlock:
2124 mutex_unlock(&tty_mutex); 2149 mutex_unlock(&tty_mutex);
@@ -2155,7 +2180,7 @@ static unsigned int tty_poll(struct file *filp, poll_table *wait)
2155 2180
2156 ld = tty_ldisc_ref_wait(tty); 2181 ld = tty_ldisc_ref_wait(tty);
2157 if (ld->ops->poll) 2182 if (ld->ops->poll)
2158 ret = (ld->ops->poll)(tty, filp, wait); 2183 ret = ld->ops->poll(tty, filp, wait);
2159 tty_ldisc_deref(ld); 2184 tty_ldisc_deref(ld);
2160 return ret; 2185 return ret;
2161} 2186}
@@ -2283,18 +2308,14 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2283int tty_do_resize(struct tty_struct *tty, struct winsize *ws) 2308int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2284{ 2309{
2285 struct pid *pgrp; 2310 struct pid *pgrp;
2286 unsigned long flags;
2287 2311
2288 /* Lock the tty */ 2312 /* Lock the tty */
2289 mutex_lock(&tty->winsize_mutex); 2313 mutex_lock(&tty->winsize_mutex);
2290 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) 2314 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2291 goto done; 2315 goto done;
2292 /* Get the PID values and reference them so we can
2293 avoid holding the tty ctrl lock while sending signals */
2294 spin_lock_irqsave(&tty->ctrl_lock, flags);
2295 pgrp = get_pid(tty->pgrp);
2296 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2297 2316
2317 /* Signal the foreground process group */
2318 pgrp = tty_get_pgrp(tty);
2298 if (pgrp) 2319 if (pgrp)
2299 kill_pgrp(pgrp, SIGWINCH, 1); 2320 kill_pgrp(pgrp, SIGWINCH, 1);
2300 put_pid(pgrp); 2321 put_pid(pgrp);
@@ -2403,7 +2424,7 @@ static int fionbio(struct file *file, int __user *p)
2403 * leader to set this tty as the controlling tty for the session. 2424 * leader to set this tty as the controlling tty for the session.
2404 * 2425 *
2405 * Locking: 2426 * Locking:
2406 * Takes tty_mutex() to protect tty instance 2427 * Takes tty_lock() to serialize proc_set_tty() for this tty
2407 * Takes tasklist_lock internally to walk sessions 2428 * Takes tasklist_lock internally to walk sessions
2408 * Takes ->siglock() when updating signal->tty 2429 * Takes ->siglock() when updating signal->tty
2409 */ 2430 */
@@ -2411,10 +2432,13 @@ static int fionbio(struct file *file, int __user *p)
2411static int tiocsctty(struct tty_struct *tty, int arg) 2432static int tiocsctty(struct tty_struct *tty, int arg)
2412{ 2433{
2413 int ret = 0; 2434 int ret = 0;
2435
2436 tty_lock(tty);
2437 read_lock(&tasklist_lock);
2438
2414 if (current->signal->leader && (task_session(current) == tty->session)) 2439 if (current->signal->leader && (task_session(current) == tty->session))
2415 return ret; 2440 goto unlock;
2416 2441
2417 mutex_lock(&tty_mutex);
2418 /* 2442 /*
2419 * The process must be a session leader and 2443 * The process must be a session leader and
2420 * not have a controlling tty already. 2444 * not have a controlling tty already.
@@ -2433,17 +2457,16 @@ static int tiocsctty(struct tty_struct *tty, int arg)
2433 /* 2457 /*
2434 * Steal it away 2458 * Steal it away
2435 */ 2459 */
2436 read_lock(&tasklist_lock);
2437 session_clear_tty(tty->session); 2460 session_clear_tty(tty->session);
2438 read_unlock(&tasklist_lock);
2439 } else { 2461 } else {
2440 ret = -EPERM; 2462 ret = -EPERM;
2441 goto unlock; 2463 goto unlock;
2442 } 2464 }
2443 } 2465 }
2444 proc_set_tty(current, tty); 2466 proc_set_tty(tty);
2445unlock: 2467unlock:
2446 mutex_unlock(&tty_mutex); 2468 read_unlock(&tasklist_lock);
2469 tty_unlock(tty);
2447 return ret; 2470 return ret;
2448} 2471}
2449 2472
@@ -2468,6 +2491,27 @@ struct pid *tty_get_pgrp(struct tty_struct *tty)
2468} 2491}
2469EXPORT_SYMBOL_GPL(tty_get_pgrp); 2492EXPORT_SYMBOL_GPL(tty_get_pgrp);
2470 2493
2494/*
2495 * This checks not only the pgrp, but falls back on the pid if no
2496 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2497 * without this...
2498 *
2499 * The caller must hold rcu lock or the tasklist lock.
2500 */
2501static struct pid *session_of_pgrp(struct pid *pgrp)
2502{
2503 struct task_struct *p;
2504 struct pid *sid = NULL;
2505
2506 p = pid_task(pgrp, PIDTYPE_PGID);
2507 if (p == NULL)
2508 p = pid_task(pgrp, PIDTYPE_PID);
2509 if (p != NULL)
2510 sid = task_session(p);
2511
2512 return sid;
2513}
2514
2471/** 2515/**
2472 * tiocgpgrp - get process group 2516 * tiocgpgrp - get process group
2473 * @tty: tty passed by user 2517 * @tty: tty passed by user
@@ -2714,23 +2758,35 @@ static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2714 return 0; 2758 return 0;
2715} 2759}
2716 2760
2717struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) 2761static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2718{ 2762{
2719 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 2763 static DEFINE_RATELIMIT_STATE(depr_flags,
2720 tty->driver->subtype == PTY_TYPE_MASTER) 2764 DEFAULT_RATELIMIT_INTERVAL,
2721 tty = tty->link; 2765 DEFAULT_RATELIMIT_BURST);
2722 return tty; 2766 char comm[TASK_COMM_LEN];
2767 int flags;
2768
2769 if (get_user(flags, &ss->flags))
2770 return;
2771
2772 flags &= ASYNC_DEPRECATED;
2773
2774 if (flags && __ratelimit(&depr_flags))
2775 pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2776 __func__, get_task_comm(comm, current), flags);
2723} 2777}
2724EXPORT_SYMBOL(tty_pair_get_tty);
2725 2778
2726struct tty_struct *tty_pair_get_pty(struct tty_struct *tty) 2779/*
2780 * if pty, return the slave side (real_tty)
2781 * otherwise, return self
2782 */
2783static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2727{ 2784{
2728 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 2785 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2729 tty->driver->subtype == PTY_TYPE_MASTER) 2786 tty->driver->subtype == PTY_TYPE_MASTER)
2730 return tty; 2787 tty = tty->link;
2731 return tty->link; 2788 return tty;
2732} 2789}
2733EXPORT_SYMBOL(tty_pair_get_pty);
2734 2790
2735/* 2791/*
2736 * Split this up, as gcc can choke on it otherwise.. 2792 * Split this up, as gcc can choke on it otherwise..
@@ -2859,13 +2915,16 @@ long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2859 case TCIFLUSH: 2915 case TCIFLUSH:
2860 case TCIOFLUSH: 2916 case TCIOFLUSH:
2861 /* flush tty buffer and allow ldisc to process ioctl */ 2917 /* flush tty buffer and allow ldisc to process ioctl */
2862 tty_buffer_flush(tty); 2918 tty_buffer_flush(tty, NULL);
2863 break; 2919 break;
2864 } 2920 }
2865 break; 2921 break;
2922 case TIOCSSERIAL:
2923 tty_warn_deprecated_flags(p);
2924 break;
2866 } 2925 }
2867 if (tty->ops->ioctl) { 2926 if (tty->ops->ioctl) {
2868 retval = (tty->ops->ioctl)(tty, cmd, arg); 2927 retval = tty->ops->ioctl(tty, cmd, arg);
2869 if (retval != -ENOIOCTLCMD) 2928 if (retval != -ENOIOCTLCMD)
2870 return retval; 2929 return retval;
2871 } 2930 }
@@ -2892,7 +2951,7 @@ static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2892 return -EINVAL; 2951 return -EINVAL;
2893 2952
2894 if (tty->ops->compat_ioctl) { 2953 if (tty->ops->compat_ioctl) {
2895 retval = (tty->ops->compat_ioctl)(tty, cmd, arg); 2954 retval = tty->ops->compat_ioctl(tty, cmd, arg);
2896 if (retval != -ENOIOCTLCMD) 2955 if (retval != -ENOIOCTLCMD)
2897 return retval; 2956 return retval;
2898 } 2957 }
@@ -3443,59 +3502,6 @@ dev_t tty_devnum(struct tty_struct *tty)
3443} 3502}
3444EXPORT_SYMBOL(tty_devnum); 3503EXPORT_SYMBOL(tty_devnum);
3445 3504
3446void proc_clear_tty(struct task_struct *p)
3447{
3448 unsigned long flags;
3449 struct tty_struct *tty;
3450 spin_lock_irqsave(&p->sighand->siglock, flags);
3451 tty = p->signal->tty;
3452 p->signal->tty = NULL;
3453 spin_unlock_irqrestore(&p->sighand->siglock, flags);
3454 tty_kref_put(tty);
3455}
3456
3457/* Called under the sighand lock */
3458
3459static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3460{
3461 if (tty) {
3462 unsigned long flags;
3463 /* We should not have a session or pgrp to put here but.... */
3464 spin_lock_irqsave(&tty->ctrl_lock, flags);
3465 put_pid(tty->session);
3466 put_pid(tty->pgrp);
3467 tty->pgrp = get_pid(task_pgrp(tsk));
3468 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3469 tty->session = get_pid(task_session(tsk));
3470 if (tsk->signal->tty) {
3471 printk(KERN_DEBUG "tty not NULL!!\n");
3472 tty_kref_put(tsk->signal->tty);
3473 }
3474 }
3475 put_pid(tsk->signal->tty_old_pgrp);
3476 tsk->signal->tty = tty_kref_get(tty);
3477 tsk->signal->tty_old_pgrp = NULL;
3478}
3479
3480static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3481{
3482 spin_lock_irq(&tsk->sighand->siglock);
3483 __proc_set_tty(tsk, tty);
3484 spin_unlock_irq(&tsk->sighand->siglock);
3485}
3486
3487struct tty_struct *get_current_tty(void)
3488{
3489 struct tty_struct *tty;
3490 unsigned long flags;
3491
3492 spin_lock_irqsave(&current->sighand->siglock, flags);
3493 tty = tty_kref_get(current->signal->tty);
3494 spin_unlock_irqrestore(&current->sighand->siglock, flags);
3495 return tty;
3496}
3497EXPORT_SYMBOL_GPL(get_current_tty);
3498
3499void tty_default_fops(struct file_operations *fops) 3505void tty_default_fops(struct file_operations *fops)
3500{ 3506{
3501 *fops = tty_fops; 3507 *fops = tty_fops;