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.c541
1 files changed, 266 insertions, 275 deletions
diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index bb0d9199e994..4044c864fdd4 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -109,13 +109,15 @@
109#define TTY_PARANOIA_CHECK 1 109#define TTY_PARANOIA_CHECK 1
110#define CHECK_TTY_COUNT 1 110#define CHECK_TTY_COUNT 1
111 111
112struct termios tty_std_termios = { /* for the benefit of tty drivers */ 112struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
113 .c_iflag = ICRNL | IXON, 113 .c_iflag = ICRNL | IXON,
114 .c_oflag = OPOST | ONLCR, 114 .c_oflag = OPOST | ONLCR,
115 .c_cflag = B38400 | CS8 | CREAD | HUPCL, 115 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
116 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | 116 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
117 ECHOCTL | ECHOKE | IEXTEN, 117 ECHOCTL | ECHOKE | IEXTEN,
118 .c_cc = INIT_C_CC 118 .c_cc = INIT_C_CC,
119 .c_ispeed = 38400,
120 .c_ospeed = 38400
119}; 121};
120 122
121EXPORT_SYMBOL(tty_std_termios); 123EXPORT_SYMBOL(tty_std_termios);
@@ -126,9 +128,10 @@ EXPORT_SYMBOL(tty_std_termios);
126 128
127LIST_HEAD(tty_drivers); /* linked list of tty drivers */ 129LIST_HEAD(tty_drivers); /* linked list of tty drivers */
128 130
129/* Semaphore to protect creating and releasing a tty. This is shared with 131/* Mutex to protect creating and releasing a tty. This is shared with
130 vt.c for deeply disgusting hack reasons */ 132 vt.c for deeply disgusting hack reasons */
131DEFINE_MUTEX(tty_mutex); 133DEFINE_MUTEX(tty_mutex);
134EXPORT_SYMBOL(tty_mutex);
132 135
133#ifdef CONFIG_UNIX98_PTYS 136#ifdef CONFIG_UNIX98_PTYS
134extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */ 137extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
@@ -160,17 +163,11 @@ static void release_mem(struct tty_struct *tty, int idx);
160 * been initialized in any way but has been zeroed 163 * been initialized in any way but has been zeroed
161 * 164 *
162 * Locking: none 165 * Locking: none
163 * FIXME: use kzalloc
164 */ 166 */
165 167
166static struct tty_struct *alloc_tty_struct(void) 168static struct tty_struct *alloc_tty_struct(void)
167{ 169{
168 struct tty_struct *tty; 170 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
169
170 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
171 if (tty)
172 memset(tty, 0, sizeof(struct tty_struct));
173 return tty;
174} 171}
175 172
176static void tty_buffer_free_all(struct tty_struct *); 173static void tty_buffer_free_all(struct tty_struct *);
@@ -255,7 +252,7 @@ static int check_tty_count(struct tty_struct *tty, const char *routine)
255 "!= #fd's(%d) in %s\n", 252 "!= #fd's(%d) in %s\n",
256 tty->name, tty->count, count, routine); 253 tty->name, tty->count, count, routine);
257 return count; 254 return count;
258 } 255 }
259#endif 256#endif
260 return 0; 257 return 0;
261} 258}
@@ -264,18 +261,6 @@ static int check_tty_count(struct tty_struct *tty, const char *routine)
264 * Tty buffer allocation management 261 * Tty buffer allocation management
265 */ 262 */
266 263
267
268/**
269 * tty_buffer_free_all - free buffers used by a tty
270 * @tty: tty to free from
271 *
272 * Remove all the buffers pending on a tty whether queued with data
273 * or in the free ring. Must be called when the tty is no longer in use
274 *
275 * Locking: none
276 */
277
278
279/** 264/**
280 * tty_buffer_free_all - free buffers used by a tty 265 * tty_buffer_free_all - free buffers used by a tty
281 * @tty: tty to free from 266 * @tty: tty to free from
@@ -483,10 +468,9 @@ int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
483 tb->used += space; 468 tb->used += space;
484 copied += space; 469 copied += space;
485 chars += space; 470 chars += space;
486 } 471 /* There is a small chance that we need to split the data over
487 /* There is a small chance that we need to split the data over 472 several buffers. If this is the case we must loop */
488 several buffers. If this is the case we must loop */ 473 } while (unlikely(size > copied));
489 while (unlikely(size > copied));
490 return copied; 474 return copied;
491} 475}
492EXPORT_SYMBOL(tty_insert_flip_string); 476EXPORT_SYMBOL(tty_insert_flip_string);
@@ -521,10 +505,9 @@ int tty_insert_flip_string_flags(struct tty_struct *tty,
521 copied += space; 505 copied += space;
522 chars += space; 506 chars += space;
523 flags += space; 507 flags += space;
524 } 508 /* There is a small chance that we need to split the data over
525 /* There is a small chance that we need to split the data over 509 several buffers. If this is the case we must loop */
526 several buffers. If this is the case we must loop */ 510 } while (unlikely(size > copied));
527 while (unlikely(size > copied));
528 return copied; 511 return copied;
529} 512}
530EXPORT_SYMBOL(tty_insert_flip_string_flags); 513EXPORT_SYMBOL(tty_insert_flip_string_flags);
@@ -621,14 +604,14 @@ EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
621 * they are not on hot paths so a little discipline won't do 604 * they are not on hot paths so a little discipline won't do
622 * any harm. 605 * any harm.
623 * 606 *
624 * Locking: takes termios_sem 607 * Locking: takes termios_mutex
625 */ 608 */
626 609
627static void tty_set_termios_ldisc(struct tty_struct *tty, int num) 610static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
628{ 611{
629 down(&tty->termios_sem); 612 mutex_lock(&tty->termios_mutex);
630 tty->termios->c_line = num; 613 tty->termios->c_line = num;
631 up(&tty->termios_sem); 614 mutex_unlock(&tty->termios_mutex);
632} 615}
633 616
634/* 617/*
@@ -922,7 +905,7 @@ static void tty_ldisc_enable(struct tty_struct *tty)
922 * context. 905 * context.
923 * 906 *
924 * Locking: takes tty_ldisc_lock. 907 * Locking: takes tty_ldisc_lock.
925 * called functions take termios_sem 908 * called functions take termios_mutex
926 */ 909 */
927 910
928static int tty_set_ldisc(struct tty_struct *tty, int ldisc) 911static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
@@ -1258,10 +1241,26 @@ void tty_ldisc_flush(struct tty_struct *tty)
1258} 1241}
1259 1242
1260EXPORT_SYMBOL_GPL(tty_ldisc_flush); 1243EXPORT_SYMBOL_GPL(tty_ldisc_flush);
1244
1245/**
1246 * tty_reset_termios - reset terminal state
1247 * @tty: tty to reset
1248 *
1249 * Restore a terminal to the driver default state
1250 */
1251
1252static void tty_reset_termios(struct tty_struct *tty)
1253{
1254 mutex_lock(&tty->termios_mutex);
1255 *tty->termios = tty->driver->init_termios;
1256 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1257 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1258 mutex_unlock(&tty->termios_mutex);
1259}
1261 1260
1262/** 1261/**
1263 * do_tty_hangup - actual handler for hangup events 1262 * do_tty_hangup - actual handler for hangup events
1264 * @data: tty device 1263 * @work: tty device
1265 * 1264 *
1266 * This can be called by the "eventd" kernel thread. That is process 1265 * This can be called by the "eventd" kernel thread. That is process
1267 * synchronous but doesn't hold any locks, so we need to make sure we 1266 * synchronous but doesn't hold any locks, so we need to make sure we
@@ -1274,16 +1273,17 @@ EXPORT_SYMBOL_GPL(tty_ldisc_flush);
1274 * 1273 *
1275 * Locking: 1274 * Locking:
1276 * BKL 1275 * BKL
1277 * redirect lock for undoing redirection 1276 * redirect lock for undoing redirection
1278 * file list lock for manipulating list of ttys 1277 * file list lock for manipulating list of ttys
1279 * tty_ldisc_lock from called functions 1278 * tty_ldisc_lock from called functions
1280 * termios_sem resetting termios data 1279 * termios_mutex resetting termios data
1281 * tasklist_lock to walk task list for hangup event 1280 * tasklist_lock to walk task list for hangup event
1282 * 1281 * ->siglock to protect ->signal/->sighand
1283 */ 1282 */
1284static void do_tty_hangup(void *data) 1283static void do_tty_hangup(struct work_struct *work)
1285{ 1284{
1286 struct tty_struct *tty = (struct tty_struct *) data; 1285 struct tty_struct *tty =
1286 container_of(work, struct tty_struct, hangup_work);
1287 struct file * cons_filp = NULL; 1287 struct file * cons_filp = NULL;
1288 struct file *filp, *f = NULL; 1288 struct file *filp, *f = NULL;
1289 struct task_struct *p; 1289 struct task_struct *p;
@@ -1345,11 +1345,7 @@ static void do_tty_hangup(void *data)
1345 * N_TTY. 1345 * N_TTY.
1346 */ 1346 */
1347 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) 1347 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1348 { 1348 tty_reset_termios(tty);
1349 down(&tty->termios_sem);
1350 *tty->termios = tty->driver->init_termios;
1351 up(&tty->termios_sem);
1352 }
1353 1349
1354 /* Defer ldisc switch */ 1350 /* Defer ldisc switch */
1355 /* tty_deferred_ldisc_switch(N_TTY); 1351 /* tty_deferred_ldisc_switch(N_TTY);
@@ -1360,14 +1356,18 @@ static void do_tty_hangup(void *data)
1360 read_lock(&tasklist_lock); 1356 read_lock(&tasklist_lock);
1361 if (tty->session > 0) { 1357 if (tty->session > 0) {
1362 do_each_task_pid(tty->session, PIDTYPE_SID, p) { 1358 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
1359 spin_lock_irq(&p->sighand->siglock);
1363 if (p->signal->tty == tty) 1360 if (p->signal->tty == tty)
1364 p->signal->tty = NULL; 1361 p->signal->tty = NULL;
1365 if (!p->signal->leader) 1362 if (!p->signal->leader) {
1363 spin_unlock_irq(&p->sighand->siglock);
1366 continue; 1364 continue;
1367 group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); 1365 }
1368 group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); 1366 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1367 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
1369 if (tty->pgrp > 0) 1368 if (tty->pgrp > 0)
1370 p->signal->tty_old_pgrp = tty->pgrp; 1369 p->signal->tty_old_pgrp = tty->pgrp;
1370 spin_unlock_irq(&p->sighand->siglock);
1371 } while_each_task_pid(tty->session, PIDTYPE_SID, p); 1371 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
1372 } 1372 }
1373 read_unlock(&tasklist_lock); 1373 read_unlock(&tasklist_lock);
@@ -1440,7 +1440,7 @@ void tty_vhangup(struct tty_struct * tty)
1440 1440
1441 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); 1441 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1442#endif 1442#endif
1443 do_tty_hangup((void *) tty); 1443 do_tty_hangup(&tty->hangup_work);
1444} 1444}
1445EXPORT_SYMBOL(tty_vhangup); 1445EXPORT_SYMBOL(tty_vhangup);
1446 1446
@@ -1459,6 +1459,14 @@ int tty_hung_up_p(struct file * filp)
1459 1459
1460EXPORT_SYMBOL(tty_hung_up_p); 1460EXPORT_SYMBOL(tty_hung_up_p);
1461 1461
1462static void session_clear_tty(pid_t session)
1463{
1464 struct task_struct *p;
1465 do_each_task_pid(session, PIDTYPE_SID, p) {
1466 proc_clear_tty(p);
1467 } while_each_task_pid(session, PIDTYPE_SID, p);
1468}
1469
1462/** 1470/**
1463 * disassociate_ctty - disconnect controlling tty 1471 * disassociate_ctty - disconnect controlling tty
1464 * @on_exit: true if exiting so need to "hang up" the session 1472 * @on_exit: true if exiting so need to "hang up" the session
@@ -1475,31 +1483,35 @@ EXPORT_SYMBOL(tty_hung_up_p);
1475 * The argument on_exit is set to 1 if called when a process is 1483 * The argument on_exit is set to 1 if called when a process is
1476 * exiting; it is 0 if called by the ioctl TIOCNOTTY. 1484 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1477 * 1485 *
1478 * Locking: tty_mutex is taken to protect current->signal->tty 1486 * Locking:
1479 * BKL is taken for hysterical raisins 1487 * BKL is taken for hysterical raisins
1480 * Tasklist lock is taken (under tty_mutex) to walk process 1488 * tty_mutex is taken to protect tty
1481 * lists for the session. 1489 * ->siglock is taken to protect ->signal/->sighand
1490 * tasklist_lock is taken to walk process list for sessions
1491 * ->siglock is taken to protect ->signal/->sighand
1482 */ 1492 */
1483 1493
1484void disassociate_ctty(int on_exit) 1494void disassociate_ctty(int on_exit)
1485{ 1495{
1486 struct tty_struct *tty; 1496 struct tty_struct *tty;
1487 struct task_struct *p;
1488 int tty_pgrp = -1; 1497 int tty_pgrp = -1;
1498 int session;
1489 1499
1490 lock_kernel(); 1500 lock_kernel();
1491 1501
1492 mutex_lock(&tty_mutex); 1502 mutex_lock(&tty_mutex);
1493 tty = current->signal->tty; 1503 tty = get_current_tty();
1494 if (tty) { 1504 if (tty) {
1495 tty_pgrp = tty->pgrp; 1505 tty_pgrp = tty->pgrp;
1496 mutex_unlock(&tty_mutex); 1506 mutex_unlock(&tty_mutex);
1507 /* XXX: here we race, there is nothing protecting tty */
1497 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) 1508 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1498 tty_vhangup(tty); 1509 tty_vhangup(tty);
1499 } else { 1510 } else {
1500 if (current->signal->tty_old_pgrp) { 1511 pid_t old_pgrp = current->signal->tty_old_pgrp;
1501 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit); 1512 if (old_pgrp) {
1502 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit); 1513 kill_pg(old_pgrp, SIGHUP, on_exit);
1514 kill_pg(old_pgrp, SIGCONT, on_exit);
1503 } 1515 }
1504 mutex_unlock(&tty_mutex); 1516 mutex_unlock(&tty_mutex);
1505 unlock_kernel(); 1517 unlock_kernel();
@@ -1511,19 +1523,29 @@ void disassociate_ctty(int on_exit)
1511 kill_pg(tty_pgrp, SIGCONT, on_exit); 1523 kill_pg(tty_pgrp, SIGCONT, on_exit);
1512 } 1524 }
1513 1525
1514 /* Must lock changes to tty_old_pgrp */ 1526 spin_lock_irq(&current->sighand->siglock);
1515 mutex_lock(&tty_mutex);
1516 current->signal->tty_old_pgrp = 0; 1527 current->signal->tty_old_pgrp = 0;
1517 tty->session = 0; 1528 session = process_session(current);
1518 tty->pgrp = -1; 1529 spin_unlock_irq(&current->sighand->siglock);
1530
1531 mutex_lock(&tty_mutex);
1532 /* It is possible that do_tty_hangup has free'd this tty */
1533 tty = get_current_tty();
1534 if (tty) {
1535 tty->session = 0;
1536 tty->pgrp = 0;
1537 } else {
1538#ifdef TTY_DEBUG_HANGUP
1539 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1540 " = NULL", tty);
1541#endif
1542 }
1543 mutex_unlock(&tty_mutex);
1519 1544
1520 /* Now clear signal->tty under the lock */ 1545 /* Now clear signal->tty under the lock */
1521 read_lock(&tasklist_lock); 1546 read_lock(&tasklist_lock);
1522 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) { 1547 session_clear_tty(session);
1523 p->signal->tty = NULL;
1524 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
1525 read_unlock(&tasklist_lock); 1548 read_unlock(&tasklist_lock);
1526 mutex_unlock(&tty_mutex);
1527 unlock_kernel(); 1549 unlock_kernel();
1528} 1550}
1529 1551
@@ -1621,7 +1643,7 @@ static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
1621 struct tty_ldisc *ld; 1643 struct tty_ldisc *ld;
1622 1644
1623 tty = (struct tty_struct *)file->private_data; 1645 tty = (struct tty_struct *)file->private_data;
1624 inode = file->f_dentry->d_inode; 1646 inode = file->f_path.dentry->d_inode;
1625 if (tty_paranoia_check(tty, inode, "tty_read")) 1647 if (tty_paranoia_check(tty, inode, "tty_read"))
1626 return -EIO; 1648 return -EIO;
1627 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) 1649 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
@@ -1724,7 +1746,7 @@ static inline ssize_t do_tty_write(
1724 cond_resched(); 1746 cond_resched();
1725 } 1747 }
1726 if (written) { 1748 if (written) {
1727 struct inode *inode = file->f_dentry->d_inode; 1749 struct inode *inode = file->f_path.dentry->d_inode;
1728 inode->i_mtime = current_fs_time(inode->i_sb); 1750 inode->i_mtime = current_fs_time(inode->i_sb);
1729 ret = written; 1751 ret = written;
1730 } 1752 }
@@ -1755,7 +1777,7 @@ static ssize_t tty_write(struct file * file, const char __user * buf, size_t cou
1755 loff_t *ppos) 1777 loff_t *ppos)
1756{ 1778{
1757 struct tty_struct * tty; 1779 struct tty_struct * tty;
1758 struct inode *inode = file->f_dentry->d_inode; 1780 struct inode *inode = file->f_path.dentry->d_inode;
1759 ssize_t ret; 1781 ssize_t ret;
1760 struct tty_ldisc *ld; 1782 struct tty_ldisc *ld;
1761 1783
@@ -1862,8 +1884,8 @@ static int init_dev(struct tty_driver *driver, int idx,
1862 struct tty_struct **ret_tty) 1884 struct tty_struct **ret_tty)
1863{ 1885{
1864 struct tty_struct *tty, *o_tty; 1886 struct tty_struct *tty, *o_tty;
1865 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc; 1887 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
1866 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; 1888 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
1867 int retval = 0; 1889 int retval = 0;
1868 1890
1869 /* check whether we're reopening an existing tty */ 1891 /* check whether we're reopening an existing tty */
@@ -1910,7 +1932,7 @@ static int init_dev(struct tty_driver *driver, int idx,
1910 } 1932 }
1911 1933
1912 if (!*tp_loc) { 1934 if (!*tp_loc) {
1913 tp = (struct termios *) kmalloc(sizeof(struct termios), 1935 tp = (struct ktermios *) kmalloc(sizeof(struct ktermios),
1914 GFP_KERNEL); 1936 GFP_KERNEL);
1915 if (!tp) 1937 if (!tp)
1916 goto free_mem_out; 1938 goto free_mem_out;
@@ -1918,11 +1940,11 @@ static int init_dev(struct tty_driver *driver, int idx,
1918 } 1940 }
1919 1941
1920 if (!*ltp_loc) { 1942 if (!*ltp_loc) {
1921 ltp = (struct termios *) kmalloc(sizeof(struct termios), 1943 ltp = (struct ktermios *) kmalloc(sizeof(struct ktermios),
1922 GFP_KERNEL); 1944 GFP_KERNEL);
1923 if (!ltp) 1945 if (!ltp)
1924 goto free_mem_out; 1946 goto free_mem_out;
1925 memset(ltp, 0, sizeof(struct termios)); 1947 memset(ltp, 0, sizeof(struct ktermios));
1926 } 1948 }
1927 1949
1928 if (driver->type == TTY_DRIVER_TYPE_PTY) { 1950 if (driver->type == TTY_DRIVER_TYPE_PTY) {
@@ -1943,19 +1965,19 @@ static int init_dev(struct tty_driver *driver, int idx,
1943 } 1965 }
1944 1966
1945 if (!*o_tp_loc) { 1967 if (!*o_tp_loc) {
1946 o_tp = (struct termios *) 1968 o_tp = (struct ktermios *)
1947 kmalloc(sizeof(struct termios), GFP_KERNEL); 1969 kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1948 if (!o_tp) 1970 if (!o_tp)
1949 goto free_mem_out; 1971 goto free_mem_out;
1950 *o_tp = driver->other->init_termios; 1972 *o_tp = driver->other->init_termios;
1951 } 1973 }
1952 1974
1953 if (!*o_ltp_loc) { 1975 if (!*o_ltp_loc) {
1954 o_ltp = (struct termios *) 1976 o_ltp = (struct ktermios *)
1955 kmalloc(sizeof(struct termios), GFP_KERNEL); 1977 kmalloc(sizeof(struct ktermios), GFP_KERNEL);
1956 if (!o_ltp) 1978 if (!o_ltp)
1957 goto free_mem_out; 1979 goto free_mem_out;
1958 memset(o_ltp, 0, sizeof(struct termios)); 1980 memset(o_ltp, 0, sizeof(struct ktermios));
1959 } 1981 }
1960 1982
1961 /* 1983 /*
@@ -1994,6 +2016,9 @@ static int init_dev(struct tty_driver *driver, int idx,
1994 *ltp_loc = ltp; 2016 *ltp_loc = ltp;
1995 tty->termios = *tp_loc; 2017 tty->termios = *tp_loc;
1996 tty->termios_locked = *ltp_loc; 2018 tty->termios_locked = *ltp_loc;
2019 /* Compatibility until drivers always set this */
2020 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
2021 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1997 driver->refcount++; 2022 driver->refcount++;
1998 tty->count++; 2023 tty->count++;
1999 2024
@@ -2072,8 +2097,9 @@ fail_no_mem:
2072 2097
2073 /* call the tty release_mem routine to clean out this slot */ 2098 /* call the tty release_mem routine to clean out this slot */
2074release_mem_out: 2099release_mem_out:
2075 printk(KERN_INFO "init_dev: ldisc open failed, " 2100 if (printk_ratelimit())
2076 "clearing slot %d\n", idx); 2101 printk(KERN_INFO "init_dev: ldisc open failed, "
2102 "clearing slot %d\n", idx);
2077 release_mem(tty, idx); 2103 release_mem(tty, idx);
2078 goto end_init; 2104 goto end_init;
2079} 2105}
@@ -2095,7 +2121,7 @@ release_mem_out:
2095static void release_mem(struct tty_struct *tty, int idx) 2121static void release_mem(struct tty_struct *tty, int idx)
2096{ 2122{
2097 struct tty_struct *o_tty; 2123 struct tty_struct *o_tty;
2098 struct termios *tp; 2124 struct ktermios *tp;
2099 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM; 2125 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
2100 2126
2101 if ((o_tty = tty->link) != NULL) { 2127 if ((o_tty = tty->link) != NULL) {
@@ -2161,7 +2187,7 @@ static void release_dev(struct file * filp)
2161 unsigned long flags; 2187 unsigned long flags;
2162 2188
2163 tty = (struct tty_struct *)filp->private_data; 2189 tty = (struct tty_struct *)filp->private_data;
2164 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev")) 2190 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "release_dev"))
2165 return; 2191 return;
2166 2192
2167 check_tty_count(tty, "release_dev"); 2193 check_tty_count(tty, "release_dev");
@@ -2342,16 +2368,10 @@ static void release_dev(struct file * filp)
2342 * tty. 2368 * tty.
2343 */ 2369 */
2344 if (tty_closing || o_tty_closing) { 2370 if (tty_closing || o_tty_closing) {
2345 struct task_struct *p;
2346
2347 read_lock(&tasklist_lock); 2371 read_lock(&tasklist_lock);
2348 do_each_task_pid(tty->session, PIDTYPE_SID, p) { 2372 session_clear_tty(tty->session);
2349 p->signal->tty = NULL;
2350 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2351 if (o_tty) 2373 if (o_tty)
2352 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) { 2374 session_clear_tty(o_tty->session);
2353 p->signal->tty = NULL;
2354 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
2355 read_unlock(&tasklist_lock); 2375 read_unlock(&tasklist_lock);
2356 } 2376 }
2357 2377
@@ -2448,9 +2468,9 @@ static void release_dev(struct file * filp)
2448 * The termios state of a pty is reset on first open so that 2468 * The termios state of a pty is reset on first open so that
2449 * settings don't persist across reuse. 2469 * settings don't persist across reuse.
2450 * 2470 *
2451 * Locking: tty_mutex protects current->signal->tty, get_tty_driver and 2471 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2452 * init_dev work. tty->count should protect the rest. 2472 * tty->count should protect the rest.
2453 * task_lock is held to update task details for sessions 2473 * ->siglock protects ->signal/->sighand
2454 */ 2474 */
2455 2475
2456static int tty_open(struct inode * inode, struct file * filp) 2476static int tty_open(struct inode * inode, struct file * filp)
@@ -2472,12 +2492,13 @@ retry_open:
2472 mutex_lock(&tty_mutex); 2492 mutex_lock(&tty_mutex);
2473 2493
2474 if (device == MKDEV(TTYAUX_MAJOR,0)) { 2494 if (device == MKDEV(TTYAUX_MAJOR,0)) {
2475 if (!current->signal->tty) { 2495 tty = get_current_tty();
2496 if (!tty) {
2476 mutex_unlock(&tty_mutex); 2497 mutex_unlock(&tty_mutex);
2477 return -ENXIO; 2498 return -ENXIO;
2478 } 2499 }
2479 driver = current->signal->tty->driver; 2500 driver = tty->driver;
2480 index = current->signal->tty->index; 2501 index = tty->index;
2481 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ 2502 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2482 /* noctty = 1; */ 2503 /* noctty = 1; */
2483 goto got_driver; 2504 goto got_driver;
@@ -2552,17 +2573,16 @@ got_driver:
2552 filp->f_op = &tty_fops; 2573 filp->f_op = &tty_fops;
2553 goto retry_open; 2574 goto retry_open;
2554 } 2575 }
2576
2577 mutex_lock(&tty_mutex);
2578 spin_lock_irq(&current->sighand->siglock);
2555 if (!noctty && 2579 if (!noctty &&
2556 current->signal->leader && 2580 current->signal->leader &&
2557 !current->signal->tty && 2581 !current->signal->tty &&
2558 tty->session == 0) { 2582 tty->session == 0)
2559 task_lock(current); 2583 __proc_set_tty(current, tty);
2560 current->signal->tty = tty; 2584 spin_unlock_irq(&current->sighand->siglock);
2561 task_unlock(current); 2585 mutex_unlock(&tty_mutex);
2562 current->signal->tty_old_pgrp = 0;
2563 tty->session = current->signal->session;
2564 tty->pgrp = process_group(current);
2565 }
2566 return 0; 2586 return 0;
2567} 2587}
2568 2588
@@ -2677,7 +2697,7 @@ static unsigned int tty_poll(struct file * filp, poll_table * wait)
2677 int ret = 0; 2697 int ret = 0;
2678 2698
2679 tty = (struct tty_struct *)filp->private_data; 2699 tty = (struct tty_struct *)filp->private_data;
2680 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll")) 2700 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
2681 return 0; 2701 return 0;
2682 2702
2683 ld = tty_ldisc_ref_wait(tty); 2703 ld = tty_ldisc_ref_wait(tty);
@@ -2693,7 +2713,7 @@ static int tty_fasync(int fd, struct file * filp, int on)
2693 int retval; 2713 int retval;
2694 2714
2695 tty = (struct tty_struct *)filp->private_data; 2715 tty = (struct tty_struct *)filp->private_data;
2696 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync")) 2716 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
2697 return 0; 2717 return 0;
2698 2718
2699 retval = fasync_helper(fd, filp, on, &tty->fasync); 2719 retval = fasync_helper(fd, filp, on, &tty->fasync);
@@ -2726,6 +2746,8 @@ static int tty_fasync(int fd, struct file * filp, int on)
2726 * Locking: 2746 * Locking:
2727 * Called functions take tty_ldisc_lock 2747 * Called functions take tty_ldisc_lock
2728 * current->signal->tty check is safe without locks 2748 * current->signal->tty check is safe without locks
2749 *
2750 * FIXME: may race normal receive processing
2729 */ 2751 */
2730 2752
2731static int tiocsti(struct tty_struct *tty, char __user *p) 2753static int tiocsti(struct tty_struct *tty, char __user *p)
@@ -2748,18 +2770,21 @@ static int tiocsti(struct tty_struct *tty, char __user *p)
2748 * @tty; tty 2770 * @tty; tty
2749 * @arg: user buffer for result 2771 * @arg: user buffer for result
2750 * 2772 *
2751 * Copies the kernel idea of the window size into the user buffer. No 2773 * Copies the kernel idea of the window size into the user buffer.
2752 * locking is done.
2753 * 2774 *
2754 * FIXME: Returning random values racing a window size set is wrong 2775 * Locking: tty->termios_mutex is taken to ensure the winsize data
2755 * should lock here against that 2776 * is consistent.
2756 */ 2777 */
2757 2778
2758static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg) 2779static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2759{ 2780{
2760 if (copy_to_user(arg, &tty->winsize, sizeof(*arg))) 2781 int err;
2761 return -EFAULT; 2782
2762 return 0; 2783 mutex_lock(&tty->termios_mutex);
2784 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2785 mutex_unlock(&tty->termios_mutex);
2786
2787 return err ? -EFAULT: 0;
2763} 2788}
2764 2789
2765/** 2790/**
@@ -2772,12 +2797,11 @@ static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
2772 * actually has driver level meaning and triggers a VC resize. 2797 * actually has driver level meaning and triggers a VC resize.
2773 * 2798 *
2774 * Locking: 2799 * Locking:
2775 * The console_sem is used to ensure we do not try and resize 2800 * Called function use the console_sem is used to ensure we do
2776 * the console twice at once. 2801 * not try and resize the console twice at once.
2777 * FIXME: Two racing size sets may leave the console and kernel 2802 * The tty->termios_mutex is used to ensure we don't double
2778 * parameters disagreeing. Is this exploitable ? 2803 * resize and get confused. Lock order - tty->termios_mutex before
2779 * FIXME: Random values racing a window size get is wrong 2804 * console sem
2780 * should lock here against that
2781 */ 2805 */
2782 2806
2783static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty, 2807static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
@@ -2787,17 +2811,18 @@ static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2787 2811
2788 if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) 2812 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2789 return -EFAULT; 2813 return -EFAULT;
2814
2815 mutex_lock(&tty->termios_mutex);
2790 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg))) 2816 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
2791 return 0; 2817 goto done;
2818
2792#ifdef CONFIG_VT 2819#ifdef CONFIG_VT
2793 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) { 2820 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
2794 int rc; 2821 if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
2795 2822 tmp_ws.ws_row)) {
2796 acquire_console_sem(); 2823 mutex_unlock(&tty->termios_mutex);
2797 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row); 2824 return -ENXIO;
2798 release_console_sem(); 2825 }
2799 if (rc)
2800 return -ENXIO;
2801 } 2826 }
2802#endif 2827#endif
2803 if (tty->pgrp > 0) 2828 if (tty->pgrp > 0)
@@ -2806,6 +2831,8 @@ static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2806 kill_pg(real_tty->pgrp, SIGWINCH, 1); 2831 kill_pg(real_tty->pgrp, SIGWINCH, 1);
2807 tty->winsize = tmp_ws; 2832 tty->winsize = tmp_ws;
2808 real_tty->winsize = tmp_ws; 2833 real_tty->winsize = tmp_ws;
2834done:
2835 mutex_unlock(&tty->termios_mutex);
2809 return 0; 2836 return 0;
2810} 2837}
2811 2838
@@ -2878,27 +2905,28 @@ static int fionbio(struct file *file, int __user *p)
2878 * leader to set this tty as the controlling tty for the session. 2905 * leader to set this tty as the controlling tty for the session.
2879 * 2906 *
2880 * Locking: 2907 * Locking:
2881 * Takes tasklist lock internally to walk sessions 2908 * Takes tty_mutex() to protect tty instance
2882 * Takes task_lock() when updating signal->tty 2909 * Takes tasklist_lock internally to walk sessions
2883 * 2910 * Takes ->siglock() when updating signal->tty
2884 * FIXME: tty_mutex is needed to protect signal->tty references.
2885 * FIXME: why task_lock on the signal->tty reference ??
2886 *
2887 */ 2911 */
2888 2912
2889static int tiocsctty(struct tty_struct *tty, int arg) 2913static int tiocsctty(struct tty_struct *tty, int arg)
2890{ 2914{
2891 struct task_struct *p; 2915 int ret = 0;
2892
2893 if (current->signal->leader && 2916 if (current->signal->leader &&
2894 (current->signal->session == tty->session)) 2917 (process_session(current) == tty->session))
2895 return 0; 2918 return ret;
2919
2920 mutex_lock(&tty_mutex);
2896 /* 2921 /*
2897 * The process must be a session leader and 2922 * The process must be a session leader and
2898 * not have a controlling tty already. 2923 * not have a controlling tty already.
2899 */ 2924 */
2900 if (!current->signal->leader || current->signal->tty) 2925 if (!current->signal->leader || current->signal->tty) {
2901 return -EPERM; 2926 ret = -EPERM;
2927 goto unlock;
2928 }
2929
2902 if (tty->session > 0) { 2930 if (tty->session > 0) {
2903 /* 2931 /*
2904 * This tty is already the controlling 2932 * This tty is already the controlling
@@ -2908,22 +2936,18 @@ static int tiocsctty(struct tty_struct *tty, int arg)
2908 /* 2936 /*
2909 * Steal it away 2937 * Steal it away
2910 */ 2938 */
2911
2912 read_lock(&tasklist_lock); 2939 read_lock(&tasklist_lock);
2913 do_each_task_pid(tty->session, PIDTYPE_SID, p) { 2940 session_clear_tty(tty->session);
2914 p->signal->tty = NULL;
2915 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
2916 read_unlock(&tasklist_lock); 2941 read_unlock(&tasklist_lock);
2917 } else 2942 } else {
2918 return -EPERM; 2943 ret = -EPERM;
2944 goto unlock;
2945 }
2919 } 2946 }
2920 task_lock(current); 2947 proc_set_tty(current, tty);
2921 current->signal->tty = tty; 2948unlock:
2922 task_unlock(current); 2949 mutex_unlock(&tty_mutex);
2923 current->signal->tty_old_pgrp = 0; 2950 return ret;
2924 tty->session = current->signal->session;
2925 tty->pgrp = process_group(current);
2926 return 0;
2927} 2951}
2928 2952
2929/** 2953/**
@@ -2935,7 +2959,7 @@ static int tiocsctty(struct tty_struct *tty, int arg)
2935 * Obtain the process group of the tty. If there is no process group 2959 * Obtain the process group of the tty. If there is no process group
2936 * return an error. 2960 * return an error.
2937 * 2961 *
2938 * Locking: none. Reference to ->signal->tty is safe. 2962 * Locking: none. Reference to current->signal->tty is safe.
2939 */ 2963 */
2940 2964
2941static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 2965static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
@@ -2959,8 +2983,6 @@ static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
2959 * permitted where the tty session is our session. 2983 * permitted where the tty session is our session.
2960 * 2984 *
2961 * Locking: None 2985 * Locking: None
2962 *
2963 * FIXME: current->signal->tty referencing is unsafe.
2964 */ 2986 */
2965 2987
2966static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 2988static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
@@ -2974,13 +2996,13 @@ static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
2974 return retval; 2996 return retval;
2975 if (!current->signal->tty || 2997 if (!current->signal->tty ||
2976 (current->signal->tty != real_tty) || 2998 (current->signal->tty != real_tty) ||
2977 (real_tty->session != current->signal->session)) 2999 (real_tty->session != process_session(current)))
2978 return -ENOTTY; 3000 return -ENOTTY;
2979 if (get_user(pgrp, p)) 3001 if (get_user(pgrp, p))
2980 return -EFAULT; 3002 return -EFAULT;
2981 if (pgrp < 0) 3003 if (pgrp < 0)
2982 return -EINVAL; 3004 return -EINVAL;
2983 if (session_of_pgrp(pgrp) != current->signal->session) 3005 if (session_of_pgrp(pgrp) != process_session(current))
2984 return -EPERM; 3006 return -EPERM;
2985 real_tty->pgrp = pgrp; 3007 real_tty->pgrp = pgrp;
2986 return 0; 3008 return 0;
@@ -2995,7 +3017,7 @@ static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t
2995 * Obtain the session id of the tty. If there is no session 3017 * Obtain the session id of the tty. If there is no session
2996 * return an error. 3018 * return an error.
2997 * 3019 *
2998 * Locking: none. Reference to ->signal->tty is safe. 3020 * Locking: none. Reference to current->signal->tty is safe.
2999 */ 3021 */
3000 3022
3001static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) 3023static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
@@ -3039,19 +3061,20 @@ static int tiocsetd(struct tty_struct *tty, int __user *p)
3039 * timed break functionality. 3061 * timed break functionality.
3040 * 3062 *
3041 * Locking: 3063 * Locking:
3042 * None 3064 * atomic_write_lock serializes
3043 * 3065 *
3044 * FIXME:
3045 * What if two overlap
3046 */ 3066 */
3047 3067
3048static int send_break(struct tty_struct *tty, unsigned int duration) 3068static int send_break(struct tty_struct *tty, unsigned int duration)
3049{ 3069{
3070 if (mutex_lock_interruptible(&tty->atomic_write_lock))
3071 return -EINTR;
3050 tty->driver->break_ctl(tty, -1); 3072 tty->driver->break_ctl(tty, -1);
3051 if (!signal_pending(current)) { 3073 if (!signal_pending(current)) {
3052 msleep_interruptible(duration); 3074 msleep_interruptible(duration);
3053 } 3075 }
3054 tty->driver->break_ctl(tty, 0); 3076 tty->driver->break_ctl(tty, 0);
3077 mutex_unlock(&tty->atomic_write_lock);
3055 if (signal_pending(current)) 3078 if (signal_pending(current))
3056 return -EINTR; 3079 return -EINTR;
3057 return 0; 3080 return 0;
@@ -3144,6 +3167,8 @@ int tty_ioctl(struct inode * inode, struct file * file,
3144 if (tty_paranoia_check(tty, inode, "tty_ioctl")) 3167 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3145 return -EINVAL; 3168 return -EINVAL;
3146 3169
3170 /* CHECKME: is this safe as one end closes ? */
3171
3147 real_tty = tty; 3172 real_tty = tty;
3148 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && 3173 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3149 tty->driver->subtype == PTY_TYPE_MASTER) 3174 tty->driver->subtype == PTY_TYPE_MASTER)
@@ -3211,14 +3236,11 @@ int tty_ioctl(struct inode * inode, struct file * file,
3211 clear_bit(TTY_EXCLUSIVE, &tty->flags); 3236 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3212 return 0; 3237 return 0;
3213 case TIOCNOTTY: 3238 case TIOCNOTTY:
3214 /* FIXME: taks lock or tty_mutex ? */
3215 if (current->signal->tty != tty) 3239 if (current->signal->tty != tty)
3216 return -ENOTTY; 3240 return -ENOTTY;
3217 if (current->signal->leader) 3241 if (current->signal->leader)
3218 disassociate_ctty(0); 3242 disassociate_ctty(0);
3219 task_lock(current); 3243 proc_clear_tty(current);
3220 current->signal->tty = NULL;
3221 task_unlock(current);
3222 return 0; 3244 return 0;
3223 case TIOCSCTTY: 3245 case TIOCSCTTY:
3224 return tiocsctty(tty, arg); 3246 return tiocsctty(tty, arg);
@@ -3302,12 +3324,13 @@ int tty_ioctl(struct inode * inode, struct file * file,
3302 * Nasty bug: do_SAK is being called in interrupt context. This can 3324 * Nasty bug: do_SAK is being called in interrupt context. This can
3303 * deadlock. We punt it up to process context. AKPM - 16Mar2001 3325 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3304 */ 3326 */
3305static void __do_SAK(void *arg) 3327static void __do_SAK(struct work_struct *work)
3306{ 3328{
3329 struct tty_struct *tty =
3330 container_of(work, struct tty_struct, SAK_work);
3307#ifdef TTY_SOFT_SAK 3331#ifdef TTY_SOFT_SAK
3308 tty_hangup(tty); 3332 tty_hangup(tty);
3309#else 3333#else
3310 struct tty_struct *tty = arg;
3311 struct task_struct *g, *p; 3334 struct task_struct *g, *p;
3312 int session; 3335 int session;
3313 int i; 3336 int i;
@@ -3317,7 +3340,7 @@ static void __do_SAK(void *arg)
3317 3340
3318 if (!tty) 3341 if (!tty)
3319 return; 3342 return;
3320 session = tty->session; 3343 session = tty->session;
3321 3344
3322 /* We don't want an ldisc switch during this */ 3345 /* We don't want an ldisc switch during this */
3323 disc = tty_ldisc_ref(tty); 3346 disc = tty_ldisc_ref(tty);
@@ -3332,7 +3355,7 @@ static void __do_SAK(void *arg)
3332 /* Kill the entire session */ 3355 /* Kill the entire session */
3333 do_each_task_pid(session, PIDTYPE_SID, p) { 3356 do_each_task_pid(session, PIDTYPE_SID, p) {
3334 printk(KERN_NOTICE "SAK: killed process %d" 3357 printk(KERN_NOTICE "SAK: killed process %d"
3335 " (%s): p->signal->session==tty->session\n", 3358 " (%s): process_session(p)==tty->session\n",
3336 p->pid, p->comm); 3359 p->pid, p->comm);
3337 send_sig(SIGKILL, p, 1); 3360 send_sig(SIGKILL, p, 1);
3338 } while_each_task_pid(session, PIDTYPE_SID, p); 3361 } while_each_task_pid(session, PIDTYPE_SID, p);
@@ -3342,7 +3365,7 @@ static void __do_SAK(void *arg)
3342 do_each_thread(g, p) { 3365 do_each_thread(g, p) {
3343 if (p->signal->tty == tty) { 3366 if (p->signal->tty == tty) {
3344 printk(KERN_NOTICE "SAK: killed process %d" 3367 printk(KERN_NOTICE "SAK: killed process %d"
3345 " (%s): p->signal->session==tty->session\n", 3368 " (%s): process_session(p)==tty->session\n",
3346 p->pid, p->comm); 3369 p->pid, p->comm);
3347 send_sig(SIGKILL, p, 1); 3370 send_sig(SIGKILL, p, 1);
3348 continue; 3371 continue;
@@ -3386,7 +3409,7 @@ void do_SAK(struct tty_struct *tty)
3386{ 3409{
3387 if (!tty) 3410 if (!tty)
3388 return; 3411 return;
3389 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty); 3412 PREPARE_WORK(&tty->SAK_work, __do_SAK);
3390 schedule_work(&tty->SAK_work); 3413 schedule_work(&tty->SAK_work);
3391} 3414}
3392 3415
@@ -3394,7 +3417,7 @@ EXPORT_SYMBOL(do_SAK);
3394 3417
3395/** 3418/**
3396 * flush_to_ldisc 3419 * flush_to_ldisc
3397 * @private_: tty structure passed from work queue. 3420 * @work: tty structure passed from work queue.
3398 * 3421 *
3399 * This routine is called out of the software interrupt to flush data 3422 * This routine is called out of the software interrupt to flush data
3400 * from the buffer chain to the line discipline. 3423 * from the buffer chain to the line discipline.
@@ -3404,9 +3427,10 @@ EXPORT_SYMBOL(do_SAK);
3404 * receive_buf method is single threaded for each tty instance. 3427 * receive_buf method is single threaded for each tty instance.
3405 */ 3428 */
3406 3429
3407static void flush_to_ldisc(void *private_) 3430static void flush_to_ldisc(struct work_struct *work)
3408{ 3431{
3409 struct tty_struct *tty = (struct tty_struct *) private_; 3432 struct tty_struct *tty =
3433 container_of(work, struct tty_struct, buf.work.work);
3410 unsigned long flags; 3434 unsigned long flags;
3411 struct tty_ldisc *disc; 3435 struct tty_ldisc *disc;
3412 struct tty_buffer *tbuf, *head; 3436 struct tty_buffer *tbuf, *head;
@@ -3451,84 +3475,6 @@ static void flush_to_ldisc(void *private_)
3451 tty_ldisc_deref(disc); 3475 tty_ldisc_deref(disc);
3452} 3476}
3453 3477
3454/*
3455 * Routine which returns the baud rate of the tty
3456 *
3457 * Note that the baud_table needs to be kept in sync with the
3458 * include/asm/termbits.h file.
3459 */
3460static int baud_table[] = {
3461 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
3462 9600, 19200, 38400, 57600, 115200, 230400, 460800,
3463#ifdef __sparc__
3464 76800, 153600, 307200, 614400, 921600
3465#else
3466 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000,
3467 2500000, 3000000, 3500000, 4000000
3468#endif
3469};
3470
3471static int n_baud_table = ARRAY_SIZE(baud_table);
3472
3473/**
3474 * tty_termios_baud_rate
3475 * @termios: termios structure
3476 *
3477 * Convert termios baud rate data into a speed. This should be called
3478 * with the termios lock held if this termios is a terminal termios
3479 * structure. May change the termios data.
3480 *
3481 * Locking: none
3482 */
3483
3484int tty_termios_baud_rate(struct termios *termios)
3485{
3486 unsigned int cbaud;
3487
3488 cbaud = termios->c_cflag & CBAUD;
3489
3490 if (cbaud & CBAUDEX) {
3491 cbaud &= ~CBAUDEX;
3492
3493 if (cbaud < 1 || cbaud + 15 > n_baud_table)
3494 termios->c_cflag &= ~CBAUDEX;
3495 else
3496 cbaud += 15;
3497 }
3498 return baud_table[cbaud];
3499}
3500
3501EXPORT_SYMBOL(tty_termios_baud_rate);
3502
3503/**
3504 * tty_get_baud_rate - get tty bit rates
3505 * @tty: tty to query
3506 *
3507 * Returns the baud rate as an integer for this terminal. The
3508 * termios lock must be held by the caller and the terminal bit
3509 * flags may be updated.
3510 *
3511 * Locking: none
3512 */
3513
3514int tty_get_baud_rate(struct tty_struct *tty)
3515{
3516 int baud = tty_termios_baud_rate(tty->termios);
3517
3518 if (baud == 38400 && tty->alt_speed) {
3519 if (!tty->warned) {
3520 printk(KERN_WARNING "Use of setserial/setrocket to "
3521 "set SPD_* flags is deprecated\n");
3522 tty->warned = 1;
3523 }
3524 baud = tty->alt_speed;
3525 }
3526
3527 return baud;
3528}
3529
3530EXPORT_SYMBOL(tty_get_baud_rate);
3531
3532/** 3478/**
3533 * tty_flip_buffer_push - terminal 3479 * tty_flip_buffer_push - terminal
3534 * @tty: tty to push 3480 * @tty: tty to push
@@ -3551,7 +3497,7 @@ void tty_flip_buffer_push(struct tty_struct *tty)
3551 spin_unlock_irqrestore(&tty->buf.lock, flags); 3497 spin_unlock_irqrestore(&tty->buf.lock, flags);
3552 3498
3553 if (tty->low_latency) 3499 if (tty->low_latency)
3554 flush_to_ldisc((void *) tty); 3500 flush_to_ldisc(&tty->buf.work.work);
3555 else 3501 else
3556 schedule_delayed_work(&tty->buf.work, 1); 3502 schedule_delayed_work(&tty->buf.work, 1);
3557} 3503}
@@ -3578,17 +3524,17 @@ static void initialize_tty_struct(struct tty_struct *tty)
3578 tty->overrun_time = jiffies; 3524 tty->overrun_time = jiffies;
3579 tty->buf.head = tty->buf.tail = NULL; 3525 tty->buf.head = tty->buf.tail = NULL;
3580 tty_buffer_init(tty); 3526 tty_buffer_init(tty);
3581 INIT_WORK(&tty->buf.work, flush_to_ldisc, tty); 3527 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
3582 init_MUTEX(&tty->buf.pty_sem); 3528 init_MUTEX(&tty->buf.pty_sem);
3583 init_MUTEX(&tty->termios_sem); 3529 mutex_init(&tty->termios_mutex);
3584 init_waitqueue_head(&tty->write_wait); 3530 init_waitqueue_head(&tty->write_wait);
3585 init_waitqueue_head(&tty->read_wait); 3531 init_waitqueue_head(&tty->read_wait);
3586 INIT_WORK(&tty->hangup_work, do_tty_hangup, tty); 3532 INIT_WORK(&tty->hangup_work, do_tty_hangup);
3587 mutex_init(&tty->atomic_read_lock); 3533 mutex_init(&tty->atomic_read_lock);
3588 mutex_init(&tty->atomic_write_lock); 3534 mutex_init(&tty->atomic_write_lock);
3589 spin_lock_init(&tty->read_lock); 3535 spin_lock_init(&tty->read_lock);
3590 INIT_LIST_HEAD(&tty->tty_files); 3536 INIT_LIST_HEAD(&tty->tty_files);
3591 INIT_WORK(&tty->SAK_work, NULL, NULL); 3537 INIT_WORK(&tty->SAK_work, NULL);
3592} 3538}
3593 3539
3594/* 3540/*
@@ -3610,7 +3556,8 @@ static struct class *tty_class;
3610 * This field is optional, if there is no known struct device 3556 * This field is optional, if there is no known struct device
3611 * for this tty device it can be set to NULL safely. 3557 * for this tty device it can be set to NULL safely.
3612 * 3558 *
3613 * Returns a pointer to the class device (or ERR_PTR(-EFOO) on error). 3559 * Returns a pointer to the struct device for this tty device
3560 * (or ERR_PTR(-EFOO) on error).
3614 * 3561 *
3615 * This call is required to be made to register an individual tty device 3562 * This call is required to be made to register an individual tty device
3616 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If 3563 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
@@ -3620,8 +3567,8 @@ static struct class *tty_class;
3620 * Locking: ?? 3567 * Locking: ??
3621 */ 3568 */
3622 3569
3623struct class_device *tty_register_device(struct tty_driver *driver, 3570struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3624 unsigned index, struct device *device) 3571 struct device *device)
3625{ 3572{
3626 char name[64]; 3573 char name[64];
3627 dev_t dev = MKDEV(driver->major, driver->minor_start) + index; 3574 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
@@ -3637,7 +3584,7 @@ struct class_device *tty_register_device(struct tty_driver *driver,
3637 else 3584 else
3638 tty_line_name(driver, index, name); 3585 tty_line_name(driver, index, name);
3639 3586
3640 return class_device_create(tty_class, NULL, dev, device, "%s", name); 3587 return device_create(tty_class, device, dev, name);
3641} 3588}
3642 3589
3643/** 3590/**
@@ -3653,7 +3600,7 @@ struct class_device *tty_register_device(struct tty_driver *driver,
3653 3600
3654void tty_unregister_device(struct tty_driver *driver, unsigned index) 3601void tty_unregister_device(struct tty_driver *driver, unsigned index)
3655{ 3602{
3656 class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index); 3603 device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index);
3657} 3604}
3658 3605
3659EXPORT_SYMBOL(tty_register_device); 3606EXPORT_SYMBOL(tty_register_device);
@@ -3678,7 +3625,8 @@ void put_tty_driver(struct tty_driver *driver)
3678 kfree(driver); 3625 kfree(driver);
3679} 3626}
3680 3627
3681void tty_set_operations(struct tty_driver *driver, struct tty_operations *op) 3628void tty_set_operations(struct tty_driver *driver,
3629 const struct tty_operations *op)
3682{ 3630{
3683 driver->open = op->open; 3631 driver->open = op->open;
3684 driver->close = op->close; 3632 driver->close = op->close;
@@ -3749,8 +3697,8 @@ int tty_register_driver(struct tty_driver *driver)
3749 3697
3750 if (p) { 3698 if (p) {
3751 driver->ttys = (struct tty_struct **)p; 3699 driver->ttys = (struct tty_struct **)p;
3752 driver->termios = (struct termios **)(p + driver->num); 3700 driver->termios = (struct ktermios **)(p + driver->num);
3753 driver->termios_locked = (struct termios **)(p + driver->num * 2); 3701 driver->termios_locked = (struct ktermios **)(p + driver->num * 2);
3754 } else { 3702 } else {
3755 driver->ttys = NULL; 3703 driver->ttys = NULL;
3756 driver->termios = NULL; 3704 driver->termios = NULL;
@@ -3789,7 +3737,7 @@ EXPORT_SYMBOL(tty_register_driver);
3789int tty_unregister_driver(struct tty_driver *driver) 3737int tty_unregister_driver(struct tty_driver *driver)
3790{ 3738{
3791 int i; 3739 int i;
3792 struct termios *tp; 3740 struct ktermios *tp;
3793 void *p; 3741 void *p;
3794 3742
3795 if (driver->refcount) 3743 if (driver->refcount)
@@ -3827,9 +3775,52 @@ int tty_unregister_driver(struct tty_driver *driver)
3827 cdev_del(&driver->cdev); 3775 cdev_del(&driver->cdev);
3828 return 0; 3776 return 0;
3829} 3777}
3830
3831EXPORT_SYMBOL(tty_unregister_driver); 3778EXPORT_SYMBOL(tty_unregister_driver);
3832 3779
3780dev_t tty_devnum(struct tty_struct *tty)
3781{
3782 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3783}
3784EXPORT_SYMBOL(tty_devnum);
3785
3786void proc_clear_tty(struct task_struct *p)
3787{
3788 spin_lock_irq(&p->sighand->siglock);
3789 p->signal->tty = NULL;
3790 spin_unlock_irq(&p->sighand->siglock);
3791}
3792EXPORT_SYMBOL(proc_clear_tty);
3793
3794void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3795{
3796 if (tty) {
3797 tty->session = process_session(tsk);
3798 tty->pgrp = process_group(tsk);
3799 }
3800 tsk->signal->tty = tty;
3801 tsk->signal->tty_old_pgrp = 0;
3802}
3803
3804void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3805{
3806 spin_lock_irq(&tsk->sighand->siglock);
3807 __proc_set_tty(tsk, tty);
3808 spin_unlock_irq(&tsk->sighand->siglock);
3809}
3810
3811struct tty_struct *get_current_tty(void)
3812{
3813 struct tty_struct *tty;
3814 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
3815 tty = current->signal->tty;
3816 /*
3817 * session->tty can be changed/cleared from under us, make sure we
3818 * issue the load. The obtained pointer, when not NULL, is valid as
3819 * long as we hold tty_mutex.
3820 */
3821 barrier();
3822 return tty;
3823}
3833 3824
3834/* 3825/*
3835 * Initialize the console device. This is called *early*, so 3826 * Initialize the console device. This is called *early*, so
@@ -3892,20 +3883,20 @@ static int __init tty_init(void)
3892 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || 3883 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3893 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) 3884 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3894 panic("Couldn't register /dev/tty driver\n"); 3885 panic("Couldn't register /dev/tty driver\n");
3895 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); 3886 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
3896 3887
3897 cdev_init(&console_cdev, &console_fops); 3888 cdev_init(&console_cdev, &console_fops);
3898 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || 3889 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3899 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) 3890 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3900 panic("Couldn't register /dev/console driver\n"); 3891 panic("Couldn't register /dev/console driver\n");
3901 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console"); 3892 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
3902 3893
3903#ifdef CONFIG_UNIX98_PTYS 3894#ifdef CONFIG_UNIX98_PTYS
3904 cdev_init(&ptmx_cdev, &ptmx_fops); 3895 cdev_init(&ptmx_cdev, &ptmx_fops);
3905 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || 3896 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3906 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) 3897 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3907 panic("Couldn't register /dev/ptmx driver\n"); 3898 panic("Couldn't register /dev/ptmx driver\n");
3908 class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); 3899 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
3909#endif 3900#endif
3910 3901
3911#ifdef CONFIG_VT 3902#ifdef CONFIG_VT
@@ -3913,7 +3904,7 @@ static int __init tty_init(void)
3913 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || 3904 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3914 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) 3905 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3915 panic("Couldn't register /dev/tty0 driver\n"); 3906 panic("Couldn't register /dev/tty0 driver\n");
3916 class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0"); 3907 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
3917 3908
3918 vty_init(); 3909 vty_init();
3919#endif 3910#endif