Lines Matching refs:tty

155 static void release_tty(struct tty_struct *tty, int idx);
166 void free_tty_struct(struct tty_struct *tty) in free_tty_struct() argument
168 if (!tty) in free_tty_struct()
170 put_device(tty->dev); in free_tty_struct()
171 kfree(tty->write_buf); in free_tty_struct()
172 tty->magic = 0xDEADDEAD; in free_tty_struct()
173 kfree(tty); in free_tty_struct()
178 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
195 void tty_add_file(struct tty_struct *tty, struct file *file) in tty_add_file() argument
199 priv->tty = tty; in tty_add_file()
203 list_add(&priv->list, &tty->tty_files); in tty_add_file()
233 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) argument
246 char *tty_name(struct tty_struct *tty, char *buf) in tty_name() argument
248 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
251 strcpy(buf, tty->name); in tty_name()
257 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
261 if (!tty) { in tty_paranoia_check()
267 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
278 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
285 list_for_each(p, &tty->tty_files) { in check_tty_count()
289 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
290 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
291 tty->link && tty->link->count) in check_tty_count()
293 if (tty->count != count) { in check_tty_count()
296 tty->name, tty->count, count, routine); in check_tty_count()
391 int tty_check_change(struct tty_struct *tty) in tty_check_change() argument
396 if (current->signal->tty != tty) in tty_check_change()
399 spin_lock_irqsave(&tty->ctrl_lock, flags); in tty_check_change()
401 if (!tty->pgrp) { in tty_check_change()
405 if (task_pgrp(current) == tty->pgrp) in tty_check_change()
407 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in tty_check_change()
420 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in tty_check_change()
497 struct tty_struct *tty; in proc_clear_tty() local
499 tty = p->signal->tty; in proc_clear_tty()
500 p->signal->tty = NULL; in proc_clear_tty()
502 tty_kref_put(tty); in proc_clear_tty()
515 static void __proc_set_tty(struct tty_struct *tty) in __proc_set_tty() argument
519 spin_lock_irqsave(&tty->ctrl_lock, flags); in __proc_set_tty()
524 put_pid(tty->session); in __proc_set_tty()
525 put_pid(tty->pgrp); in __proc_set_tty()
526 tty->pgrp = get_pid(task_pgrp(current)); in __proc_set_tty()
527 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __proc_set_tty()
528 tty->session = get_pid(task_session(current)); in __proc_set_tty()
529 if (current->signal->tty) { in __proc_set_tty()
531 tty_kref_put(current->signal->tty); in __proc_set_tty()
534 current->signal->tty = tty_kref_get(tty); in __proc_set_tty()
538 static void proc_set_tty(struct tty_struct *tty) in proc_set_tty() argument
541 __proc_set_tty(tty); in proc_set_tty()
547 struct tty_struct *tty; in get_current_tty() local
551 tty = tty_kref_get(current->signal->tty); in get_current_tty()
553 return tty; in get_current_tty()
574 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
578 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
579 ld = tty_ldisc_ref(tty); in tty_wakeup()
582 ld->ops->write_wakeup(tty); in tty_wakeup()
586 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); in tty_wakeup()
603 static int tty_signal_session_leader(struct tty_struct *tty, int exit_session) in tty_signal_session_leader() argument
610 if (tty->session) { in tty_signal_session_leader()
611 do_each_pid_task(tty->session, PIDTYPE_SID, p) { in tty_signal_session_leader()
613 if (p->signal->tty == tty) { in tty_signal_session_leader()
614 p->signal->tty = NULL; in tty_signal_session_leader()
626 spin_lock(&tty->ctrl_lock); in tty_signal_session_leader()
627 tty_pgrp = get_pid(tty->pgrp); in tty_signal_session_leader()
628 if (tty->pgrp) in tty_signal_session_leader()
629 p->signal->tty_old_pgrp = get_pid(tty->pgrp); in tty_signal_session_leader()
630 spin_unlock(&tty->ctrl_lock); in tty_signal_session_leader()
632 } while_each_pid_task(tty->session, PIDTYPE_SID, p); in tty_signal_session_leader()
667 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
675 if (!tty) in __tty_hangup()
680 if (redirect && file_tty(redirect) == tty) { in __tty_hangup()
686 tty_lock(tty); in __tty_hangup()
688 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
689 tty_unlock(tty); in __tty_hangup()
696 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
700 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
712 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
715 tty_kref_put(tty); in __tty_hangup()
717 tty_ldisc_hangup(tty); in __tty_hangup()
719 spin_lock_irq(&tty->ctrl_lock); in __tty_hangup()
720 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
721 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
722 put_pid(tty->session); in __tty_hangup()
723 put_pid(tty->pgrp); in __tty_hangup()
724 tty->session = NULL; in __tty_hangup()
725 tty->pgrp = NULL; in __tty_hangup()
726 tty->ctrl_status = 0; in __tty_hangup()
727 spin_unlock_irq(&tty->ctrl_lock); in __tty_hangup()
736 if (tty->ops->close) in __tty_hangup()
738 tty->ops->close(tty, cons_filp); in __tty_hangup()
739 } else if (tty->ops->hangup) in __tty_hangup()
740 tty->ops->hangup(tty); in __tty_hangup()
747 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
748 tty_unlock(tty); in __tty_hangup()
756 struct tty_struct *tty = in do_tty_hangup() local
759 __tty_hangup(tty, 0); in do_tty_hangup()
770 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
774 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); in tty_hangup()
776 schedule_work(&tty->hangup_work); in tty_hangup()
790 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
795 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); in tty_vhangup()
797 __tty_hangup(tty, 0); in tty_vhangup()
811 struct tty_struct *tty; in tty_vhangup_self() local
813 tty = get_current_tty(); in tty_vhangup_self()
814 if (tty) { in tty_vhangup_self()
815 tty_vhangup(tty); in tty_vhangup_self()
816 tty_kref_put(tty); in tty_vhangup_self()
831 static void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
836 printk(KERN_DEBUG "%s vhangup session...\n", tty_name(tty, buf)); in tty_vhangup_session()
838 __tty_hangup(tty, 1); in tty_vhangup_session()
883 struct tty_struct *tty; in disassociate_ctty() local
888 tty = get_current_tty(); in disassociate_ctty()
889 if (tty) { in disassociate_ctty()
890 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) { in disassociate_ctty()
891 tty_vhangup_session(tty); in disassociate_ctty()
893 struct pid *tty_pgrp = tty_get_pgrp(tty); in disassociate_ctty()
901 tty_kref_put(tty); in disassociate_ctty()
921 tty = tty_kref_get(current->signal->tty); in disassociate_ctty()
922 if (tty) { in disassociate_ctty()
924 spin_lock_irqsave(&tty->ctrl_lock, flags); in disassociate_ctty()
925 put_pid(tty->session); in disassociate_ctty()
926 put_pid(tty->pgrp); in disassociate_ctty()
927 tty->session = NULL; in disassociate_ctty()
928 tty->pgrp = NULL; in disassociate_ctty()
929 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in disassociate_ctty()
930 tty_kref_put(tty); in disassociate_ctty()
934 " = NULL", tty); in disassociate_ctty()
977 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
979 if (tty->stopped) in __stop_tty()
981 tty->stopped = 1; in __stop_tty()
982 if (tty->ops->stop) in __stop_tty()
983 tty->ops->stop(tty); in __stop_tty()
986 void stop_tty(struct tty_struct *tty) in stop_tty() argument
990 spin_lock_irqsave(&tty->flow_lock, flags); in stop_tty()
991 __stop_tty(tty); in stop_tty()
992 spin_unlock_irqrestore(&tty->flow_lock, flags); in stop_tty()
1008 void __start_tty(struct tty_struct *tty) in __start_tty() argument
1010 if (!tty->stopped || tty->flow_stopped) in __start_tty()
1012 tty->stopped = 0; in __start_tty()
1013 if (tty->ops->start) in __start_tty()
1014 tty->ops->start(tty); in __start_tty()
1015 tty_wakeup(tty); in __start_tty()
1018 void start_tty(struct tty_struct *tty) in start_tty() argument
1022 spin_lock_irqsave(&tty->flow_lock, flags); in start_tty()
1023 __start_tty(tty); in start_tty()
1024 spin_unlock_irqrestore(&tty->flow_lock, flags); in start_tty()
1062 struct tty_struct *tty = file_tty(file); in tty_read() local
1065 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
1067 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) in tty_read()
1072 ld = tty_ldisc_ref_wait(tty); in tty_read()
1074 i = ld->ops->read(tty, file, buf, count); in tty_read()
1085 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
1087 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
1088 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); in tty_write_unlock()
1091 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
1093 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
1096 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
1108 struct tty_struct *tty, in do_tty_write() argument
1116 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
1137 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
1143 if (tty->write_cnt < chunk) { in do_tty_write()
1154 kfree(tty->write_buf); in do_tty_write()
1155 tty->write_cnt = chunk; in do_tty_write()
1156 tty->write_buf = buf_chunk; in do_tty_write()
1165 if (copy_from_user(tty->write_buf, buf, size)) in do_tty_write()
1167 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
1185 tty_write_unlock(tty); in do_tty_write()
1201 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1203 if (tty) { in tty_write_message()
1204 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1205 tty_lock(tty); in tty_write_message()
1206 if (tty->ops->write && tty->count > 0) { in tty_write_message()
1207 tty_unlock(tty); in tty_write_message()
1208 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1210 tty_unlock(tty); in tty_write_message()
1211 tty_write_unlock(tty); in tty_write_message()
1236 struct tty_struct *tty = file_tty(file); in tty_write() local
1240 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in tty_write()
1242 if (!tty || !tty->ops->write || in tty_write()
1243 (test_bit(TTY_IO_ERROR, &tty->flags))) in tty_write()
1246 if (tty->ops->write_room == NULL) in tty_write()
1248 tty->driver->name); in tty_write()
1249 ld = tty_ldisc_ref_wait(tty); in tty_write()
1253 ret = do_tty_write(ld->ops->write, tty, file, buf, count); in tty_write()
1285 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1287 int was_stopped = tty->stopped; in tty_send_xchar()
1289 if (tty->ops->send_xchar) { in tty_send_xchar()
1290 down_read(&tty->termios_rwsem); in tty_send_xchar()
1291 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1292 up_read(&tty->termios_rwsem); in tty_send_xchar()
1296 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1299 down_read(&tty->termios_rwsem); in tty_send_xchar()
1301 start_tty(tty); in tty_send_xchar()
1302 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1304 stop_tty(tty); in tty_send_xchar()
1305 up_read(&tty->termios_rwsem); in tty_send_xchar()
1306 tty_write_unlock(tty); in tty_send_xchar()
1365 struct tty_struct *tty; in tty_driver_lookup_tty() local
1368 tty = driver->ops->lookup(driver, inode, idx); in tty_driver_lookup_tty()
1370 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1372 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1373 tty_kref_get(tty); in tty_driver_lookup_tty()
1374 return tty; in tty_driver_lookup_tty()
1385 int tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1388 int idx = tty->index; in tty_init_termios()
1390 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1391 tty->termios = tty->driver->init_termios; in tty_init_termios()
1394 tp = tty->driver->termios[idx]; in tty_init_termios()
1396 tty->termios = *tp; in tty_init_termios()
1398 tty->termios = tty->driver->init_termios; in tty_init_termios()
1401 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1402 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1407 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1409 int ret = tty_init_termios(tty); in tty_standard_install()
1414 tty->count++; in tty_standard_install()
1415 driver->ttys[tty->index] = tty; in tty_standard_install()
1433 struct tty_struct *tty) in tty_driver_install_tty() argument
1435 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1436 tty_standard_install(driver, tty); in tty_driver_install_tty()
1449 void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1452 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1454 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1466 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1468 struct tty_driver *driver = tty->driver; in tty_reopen()
1470 if (!tty->count) in tty_reopen()
1477 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1480 tty->count++; in tty_reopen()
1482 WARN_ON(!tty->ldisc); in tty_reopen()
1513 struct tty_struct *tty; in tty_init_dev() local
1527 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1528 if (!tty) { in tty_init_dev()
1533 tty_lock(tty); in tty_init_dev()
1534 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1538 if (!tty->port) in tty_init_dev()
1539 tty->port = driver->ports[idx]; in tty_init_dev()
1541 WARN_RATELIMIT(!tty->port, in tty_init_dev()
1543 __func__, tty->driver->name); in tty_init_dev()
1545 tty->port->itty = tty; in tty_init_dev()
1552 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1556 return tty; in tty_init_dev()
1559 tty_unlock(tty); in tty_init_dev()
1560 deinitialize_tty_struct(tty); in tty_init_dev()
1561 free_tty_struct(tty); in tty_init_dev()
1568 tty_unlock(tty); in tty_init_dev()
1571 release_tty(tty, idx); in tty_init_dev()
1575 void tty_free_termios(struct tty_struct *tty) in tty_free_termios() argument
1578 int idx = tty->index; in tty_free_termios()
1581 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_free_termios()
1585 tp = tty->driver->termios[idx]; in tty_free_termios()
1592 tty->driver->termios[idx] = tp; in tty_free_termios()
1594 *tp = tty->termios; in tty_free_termios()
1604 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1606 flush_work(&tty->SAK_work); in tty_flush_works()
1607 flush_work(&tty->hangup_work); in tty_flush_works()
1608 if (tty->link) { in tty_flush_works()
1609 flush_work(&tty->link->SAK_work); in tty_flush_works()
1610 flush_work(&tty->link->hangup_work); in tty_flush_works()
1631 struct tty_struct *tty = in release_one_tty() local
1633 struct tty_driver *driver = tty->driver; in release_one_tty()
1636 if (tty->ops->cleanup) in release_one_tty()
1637 tty->ops->cleanup(tty); in release_one_tty()
1639 tty->magic = 0; in release_one_tty()
1644 list_del_init(&tty->tty_files); in release_one_tty()
1647 put_pid(tty->pgrp); in release_one_tty()
1648 put_pid(tty->session); in release_one_tty()
1649 free_tty_struct(tty); in release_one_tty()
1654 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1658 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1659 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1670 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1672 if (tty) in tty_kref_put()
1673 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1689 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1692 WARN_ON(tty->index != idx); in release_tty()
1694 if (tty->ops->shutdown) in release_tty()
1695 tty->ops->shutdown(tty); in release_tty()
1696 tty_free_termios(tty); in release_tty()
1697 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1698 tty->port->itty = NULL; in release_tty()
1699 if (tty->link) in release_tty()
1700 tty->link->port->itty = NULL; in release_tty()
1701 cancel_work_sync(&tty->port->buf.work); in release_tty()
1703 tty_kref_put(tty->link); in release_tty()
1704 tty_kref_put(tty); in release_tty()
1716 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1719 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1721 __func__, tty->name); in tty_release_checks()
1726 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1729 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1731 __func__, idx, tty->name); in tty_release_checks()
1734 if (tty->driver->other) { in tty_release_checks()
1735 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1737 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1739 __func__, idx, tty->name); in tty_release_checks()
1742 if (o_tty->link != tty) { in tty_release_checks()
1772 struct tty_struct *tty = file_tty(filp); in tty_release() local
1780 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1783 tty_lock(tty); in tty_release()
1784 check_tty_count(tty, __func__); in tty_release()
1788 idx = tty->index; in tty_release()
1789 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1790 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1791 o_tty = tty->link; in tty_release()
1793 if (tty_release_checks(tty, idx)) { in tty_release()
1794 tty_unlock(tty); in tty_release()
1800 tty_name(tty, buf), tty->count); in tty_release()
1803 if (tty->ops->close) in tty_release()
1804 tty->ops->close(tty, filp); in tty_release()
1825 if (tty->count <= 1) { in tty_release()
1826 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1827 wake_up_poll(&tty->read_wait, POLLIN); in tty_release()
1830 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1831 wake_up_poll(&tty->write_wait, POLLOUT); in tty_release()
1851 __func__, tty_name(tty, buf)); in tty_release()
1867 if (--tty->count < 0) { in tty_release()
1869 __func__, tty->count, tty_name(tty, buf)); in tty_release()
1870 tty->count = 0; in tty_release()
1891 if (!tty->count) { in tty_release()
1893 session_clear_tty(tty->session); in tty_release()
1900 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1903 tty_unlock(tty); in tty_release()
1912 printk(KERN_DEBUG "%s: %s: final close\n", __func__, tty_name(tty, buf)); in tty_release()
1917 tty_ldisc_release(tty); in tty_release()
1920 tty_flush_works(tty); in tty_release()
1923 printk(KERN_DEBUG "%s: %s: freeing structure...\n", __func__, tty_name(tty, buf)); in tty_release()
1932 release_tty(tty, idx); in tty_release()
1951 struct tty_struct *tty; in tty_open_current_tty() local
1957 tty = get_current_tty(); in tty_open_current_tty()
1958 if (!tty) in tty_open_current_tty()
1963 tty_lock(tty); in tty_open_current_tty()
1964 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1966 retval = tty_reopen(tty); in tty_open_current_tty()
1968 tty_unlock(tty); in tty_open_current_tty()
1969 tty = ERR_PTR(retval); in tty_open_current_tty()
1971 return tty; in tty_open_current_tty()
2050 struct tty_struct *tty; in tty_open() local
2068 tty = tty_open_current_tty(device, filp); in tty_open()
2069 if (!tty) { in tty_open()
2078 tty = tty_driver_lookup_tty(driver, inode, index); in tty_open()
2079 if (IS_ERR(tty)) { in tty_open()
2080 retval = PTR_ERR(tty); in tty_open()
2084 if (tty) { in tty_open()
2086 tty_lock(tty); in tty_open()
2088 tty_kref_put(tty); in tty_open()
2089 retval = tty_reopen(tty); in tty_open()
2091 tty_unlock(tty); in tty_open()
2092 tty = ERR_PTR(retval); in tty_open()
2095 tty = tty_init_dev(driver, index); in tty_open()
2102 if (IS_ERR(tty)) { in tty_open()
2103 retval = PTR_ERR(tty); in tty_open()
2107 tty_add_file(tty, filp); in tty_open()
2109 check_tty_count(tty, __func__); in tty_open()
2110 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2111 tty->driver->subtype == PTY_TYPE_MASTER) in tty_open()
2114 printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name); in tty_open()
2116 if (tty->ops->open) in tty_open()
2117 retval = tty->ops->open(tty, filp); in tty_open()
2125 retval, tty->name); in tty_open()
2127 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2143 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2150 !current->signal->tty && in tty_open()
2151 tty->session == NULL) { in tty_open()
2167 __proc_set_tty(tty); in tty_open()
2171 tty_unlock(tty); in tty_open()
2199 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2203 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2206 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2208 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2215 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2220 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2223 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2227 ldisc = tty_ldisc_ref(tty); in __tty_fasync()
2230 ldisc->ops->fasync(tty, on); in __tty_fasync()
2238 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_fasync()
2239 if (tty->pgrp) { in __tty_fasync()
2240 pid = tty->pgrp; in __tty_fasync()
2247 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_fasync()
2258 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2261 tty_lock(tty); in tty_fasync()
2263 tty_unlock(tty); in tty_fasync()
2285 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2290 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2294 tty_audit_tiocsti(tty, ch); in tiocsti()
2295 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2296 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2312 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2316 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2317 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2318 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2333 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2338 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2339 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2343 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2348 tty->winsize = *ws; in tty_do_resize()
2350 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2370 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2376 if (tty->ops->resize) in tiocswinsz()
2377 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2379 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2457 static int tiocsctty(struct tty_struct *tty, struct file *file, int arg) in tiocsctty() argument
2461 tty_lock(tty); in tiocsctty()
2464 if (current->signal->leader && (task_session(current) == tty->session)) in tiocsctty()
2471 if (!current->signal->leader || current->signal->tty) { in tiocsctty()
2476 if (tty->session) { in tiocsctty()
2485 session_clear_tty(tty->session); in tiocsctty()
2498 proc_set_tty(tty); in tiocsctty()
2501 tty_unlock(tty); in tiocsctty()
2513 struct pid *tty_get_pgrp(struct tty_struct *tty) in tty_get_pgrp() argument
2518 spin_lock_irqsave(&tty->ctrl_lock, flags); in tty_get_pgrp()
2519 pgrp = get_pid(tty->pgrp); in tty_get_pgrp()
2520 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in tty_get_pgrp()
2559 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocgpgrp() argument
2567 if (tty == real_tty && current->signal->tty != real_tty) in tiocgpgrp()
2587 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocspgrp() argument
2598 if (!current->signal->tty || in tiocspgrp()
2599 (current->signal->tty != real_tty) || in tiocspgrp()
2615 spin_lock_irqsave(&tty->ctrl_lock, flags); in tiocspgrp()
2618 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in tiocspgrp()
2636 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocgsid() argument
2642 if (tty == real_tty && current->signal->tty != real_tty) in tiocgsid()
2659 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2667 ret = tty_set_ldisc(tty, ldisc); in tiocsetd()
2683 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2688 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2707 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2711 if (tty->ops->break_ctl == NULL) in send_break()
2714 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2715 retval = tty->ops->break_ctl(tty, duration); in send_break()
2718 if (tty_write_lock(tty, 0) < 0) in send_break()
2720 retval = tty->ops->break_ctl(tty, -1); in send_break()
2725 retval = tty->ops->break_ctl(tty, 0); in send_break()
2727 tty_write_unlock(tty); in send_break()
2746 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2750 if (tty->ops->tiocmget) { in tty_tiocmget()
2751 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2771 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2777 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2798 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2801 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2806 if (tty->ops->get_icount) in tty_tiocgicount()
2807 retval = tty->ops->get_icount(tty, &icount); in tty_tiocgicount()
2837 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2839 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2840 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2841 tty = tty->link; in tty_pair_get_tty()
2842 return tty; in tty_pair_get_tty()
2850 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2856 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2859 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2870 retval = tty_check_change(tty); in tty_ioctl()
2874 tty_wait_until_sent(tty, 0); in tty_ioctl()
2886 return tiocsti(tty, p); in tty_ioctl()
2892 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2896 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2899 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2903 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2907 if (current->signal->tty != tty) in tty_ioctl()
2912 return tiocsctty(tty, file, arg); in tty_ioctl()
2914 return tiocgpgrp(tty, real_tty, p); in tty_ioctl()
2916 return tiocspgrp(tty, real_tty, p); in tty_ioctl()
2918 return tiocgsid(tty, real_tty, p); in tty_ioctl()
2920 return tiocgetd(tty, p); in tty_ioctl()
2922 return tiocsetd(tty, p); in tty_ioctl()
2926 tty_vhangup(tty); in tty_ioctl()
2937 if (tty->ops->break_ctl) in tty_ioctl()
2938 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2941 if (tty->ops->break_ctl) in tty_ioctl()
2942 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2950 return send_break(tty, 250); in tty_ioctl()
2953 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2956 return tty_tiocmget(tty, p); in tty_ioctl()
2960 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2962 retval = tty_tiocgicount(tty, p); in tty_ioctl()
2972 tty_buffer_flush(tty, NULL); in tty_ioctl()
2980 if (tty->ops->ioctl) { in tty_ioctl()
2981 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2985 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2988 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
3000 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
3004 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
3007 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
3008 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
3013 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
3015 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
3017 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); in tty_compat_ioctl()
3050 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3053 tty_hangup(tty); in __do_SAK()
3059 if (!tty) in __do_SAK()
3061 session = tty->session; in __do_SAK()
3063 tty_ldisc_flush(tty); in __do_SAK()
3065 tty_driver_flush_buffer(tty); in __do_SAK()
3079 if (p->signal->tty == tty) { in __do_SAK()
3087 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3102 struct tty_struct *tty = in do_SAK_work() local
3104 __do_SAK(tty); in do_SAK_work()
3113 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3115 if (!tty) in do_SAK()
3117 schedule_work(&tty->SAK_work); in do_SAK()
3129 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3131 dev_t devt = tty_devnum(tty); in tty_get_device()
3146 struct tty_struct *tty; in alloc_tty_struct() local
3148 tty = kzalloc(sizeof(*tty), GFP_KERNEL); in alloc_tty_struct()
3149 if (!tty) in alloc_tty_struct()
3152 kref_init(&tty->kref); in alloc_tty_struct()
3153 tty->magic = TTY_MAGIC; in alloc_tty_struct()
3154 tty_ldisc_init(tty); in alloc_tty_struct()
3155 tty->session = NULL; in alloc_tty_struct()
3156 tty->pgrp = NULL; in alloc_tty_struct()
3157 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3158 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3159 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3160 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3161 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3162 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3163 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3164 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3165 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3166 spin_lock_init(&tty->ctrl_lock); in alloc_tty_struct()
3167 spin_lock_init(&tty->flow_lock); in alloc_tty_struct()
3168 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3169 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3171 tty->driver = driver; in alloc_tty_struct()
3172 tty->ops = driver->ops; in alloc_tty_struct()
3173 tty->index = idx; in alloc_tty_struct()
3174 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3175 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3177 return tty; in alloc_tty_struct()
3189 void deinitialize_tty_struct(struct tty_struct *tty) in deinitialize_tty_struct() argument
3191 tty_ldisc_deinit(tty); in deinitialize_tty_struct()
3206 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3208 if (tty->ops->put_char) in tty_put_char()
3209 return tty->ops->put_char(tty, ch); in tty_put_char()
3210 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3553 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3555 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()