Lines Matching refs:tty

110 # define tty_debug_hangup(tty, f, args...)	tty_debug(tty, f, ##args)  argument
112 # define tty_debug_hangup(tty, f, args...) do { } while (0) argument
160 static void release_tty(struct tty_struct *tty, int idx);
171 void free_tty_struct(struct tty_struct *tty) in free_tty_struct() argument
173 if (!tty) in free_tty_struct()
175 put_device(tty->dev); in free_tty_struct()
176 kfree(tty->write_buf); in free_tty_struct()
177 tty->magic = 0xDEADDEAD; in free_tty_struct()
178 kfree(tty); in free_tty_struct()
183 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
200 void tty_add_file(struct tty_struct *tty, struct file *file) in tty_add_file() argument
204 priv->tty = tty; in tty_add_file()
208 list_add(&priv->list, &tty->tty_files); in tty_add_file()
238 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) argument
250 const char *tty_name(const struct tty_struct *tty) in tty_name() argument
252 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
254 return tty->name; in tty_name()
259 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
263 if (!tty) { in tty_paranoia_check()
269 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
280 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
287 list_for_each(p, &tty->tty_files) { in check_tty_count()
291 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
292 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
293 tty->link && tty->link->count) in check_tty_count()
295 if (tty->count != count) { in check_tty_count()
298 tty->name, tty->count, count, routine); in check_tty_count()
393 int __tty_check_change(struct tty_struct *tty, int sig) in __tty_check_change() argument
399 if (current->signal->tty != tty) in __tty_check_change()
405 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_check_change()
406 tty_pgrp = tty->pgrp; in __tty_check_change()
407 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_check_change()
409 if (tty_pgrp && pgrp != tty->pgrp) { in __tty_check_change()
425 tty_name(tty), sig); in __tty_check_change()
431 int tty_check_change(struct tty_struct *tty) in tty_check_change() argument
433 return __tty_check_change(tty, SIGTTOU); in tty_check_change()
508 struct tty_struct *tty; in proc_clear_tty() local
510 tty = p->signal->tty; in proc_clear_tty()
511 p->signal->tty = NULL; in proc_clear_tty()
513 tty_kref_put(tty); in proc_clear_tty()
526 static void __proc_set_tty(struct tty_struct *tty) in __proc_set_tty() argument
530 spin_lock_irqsave(&tty->ctrl_lock, flags); in __proc_set_tty()
535 put_pid(tty->session); in __proc_set_tty()
536 put_pid(tty->pgrp); in __proc_set_tty()
537 tty->pgrp = get_pid(task_pgrp(current)); in __proc_set_tty()
538 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __proc_set_tty()
539 tty->session = get_pid(task_session(current)); in __proc_set_tty()
540 if (current->signal->tty) { in __proc_set_tty()
541 tty_debug(tty, "current tty %s not NULL!!\n", in __proc_set_tty()
542 current->signal->tty->name); in __proc_set_tty()
543 tty_kref_put(current->signal->tty); in __proc_set_tty()
546 current->signal->tty = tty_kref_get(tty); in __proc_set_tty()
550 static void proc_set_tty(struct tty_struct *tty) in proc_set_tty() argument
553 __proc_set_tty(tty); in proc_set_tty()
559 struct tty_struct *tty; in get_current_tty() local
563 tty = tty_kref_get(current->signal->tty); in get_current_tty()
565 return tty; in get_current_tty()
586 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
590 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
591 ld = tty_ldisc_ref(tty); in tty_wakeup()
594 ld->ops->write_wakeup(tty); in tty_wakeup()
598 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); in tty_wakeup()
615 static int tty_signal_session_leader(struct tty_struct *tty, int exit_session) in tty_signal_session_leader() argument
622 if (tty->session) { in tty_signal_session_leader()
623 do_each_pid_task(tty->session, PIDTYPE_SID, p) { in tty_signal_session_leader()
625 if (p->signal->tty == tty) { in tty_signal_session_leader()
626 p->signal->tty = NULL; in tty_signal_session_leader()
638 spin_lock(&tty->ctrl_lock); in tty_signal_session_leader()
639 tty_pgrp = get_pid(tty->pgrp); in tty_signal_session_leader()
640 if (tty->pgrp) in tty_signal_session_leader()
641 p->signal->tty_old_pgrp = get_pid(tty->pgrp); in tty_signal_session_leader()
642 spin_unlock(&tty->ctrl_lock); in tty_signal_session_leader()
644 } while_each_pid_task(tty->session, PIDTYPE_SID, p); in tty_signal_session_leader()
679 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
687 if (!tty) in __tty_hangup()
692 if (redirect && file_tty(redirect) == tty) { in __tty_hangup()
698 tty_lock(tty); in __tty_hangup()
700 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
701 tty_unlock(tty); in __tty_hangup()
708 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
712 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
724 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
727 tty_kref_put(tty); in __tty_hangup()
729 tty_ldisc_hangup(tty); in __tty_hangup()
731 spin_lock_irq(&tty->ctrl_lock); in __tty_hangup()
732 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
733 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
734 put_pid(tty->session); in __tty_hangup()
735 put_pid(tty->pgrp); in __tty_hangup()
736 tty->session = NULL; in __tty_hangup()
737 tty->pgrp = NULL; in __tty_hangup()
738 tty->ctrl_status = 0; in __tty_hangup()
739 spin_unlock_irq(&tty->ctrl_lock); in __tty_hangup()
748 if (tty->ops->close) in __tty_hangup()
750 tty->ops->close(tty, cons_filp); in __tty_hangup()
751 } else if (tty->ops->hangup) in __tty_hangup()
752 tty->ops->hangup(tty); in __tty_hangup()
759 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
760 tty_unlock(tty); in __tty_hangup()
768 struct tty_struct *tty = in do_tty_hangup() local
771 __tty_hangup(tty, 0); in do_tty_hangup()
782 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
784 tty_debug_hangup(tty, "\n"); in tty_hangup()
785 schedule_work(&tty->hangup_work); in tty_hangup()
799 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
801 tty_debug_hangup(tty, "\n"); in tty_vhangup()
802 __tty_hangup(tty, 0); in tty_vhangup()
816 struct tty_struct *tty; in tty_vhangup_self() local
818 tty = get_current_tty(); in tty_vhangup_self()
819 if (tty) { in tty_vhangup_self()
820 tty_vhangup(tty); in tty_vhangup_self()
821 tty_kref_put(tty); in tty_vhangup_self()
836 static void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
838 tty_debug_hangup(tty, "\n"); in tty_vhangup_session()
839 __tty_hangup(tty, 1); in tty_vhangup_session()
884 struct tty_struct *tty; in disassociate_ctty() local
889 tty = get_current_tty(); in disassociate_ctty()
890 if (tty) { in disassociate_ctty()
891 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) { in disassociate_ctty()
892 tty_vhangup_session(tty); in disassociate_ctty()
894 struct pid *tty_pgrp = tty_get_pgrp(tty); in disassociate_ctty()
902 tty_kref_put(tty); in disassociate_ctty()
922 tty = tty_kref_get(current->signal->tty); in disassociate_ctty()
923 if (tty) { in disassociate_ctty()
925 spin_lock_irqsave(&tty->ctrl_lock, flags); in disassociate_ctty()
926 put_pid(tty->session); in disassociate_ctty()
927 put_pid(tty->pgrp); in disassociate_ctty()
928 tty->session = NULL; in disassociate_ctty()
929 tty->pgrp = NULL; in disassociate_ctty()
930 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in disassociate_ctty()
931 tty_kref_put(tty); in disassociate_ctty()
933 tty_debug_hangup(tty, "no current tty\n"); in disassociate_ctty()
974 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
976 if (tty->stopped) in __stop_tty()
978 tty->stopped = 1; in __stop_tty()
979 if (tty->ops->stop) in __stop_tty()
980 tty->ops->stop(tty); in __stop_tty()
983 void stop_tty(struct tty_struct *tty) in stop_tty() argument
987 spin_lock_irqsave(&tty->flow_lock, flags); in stop_tty()
988 __stop_tty(tty); in stop_tty()
989 spin_unlock_irqrestore(&tty->flow_lock, flags); in stop_tty()
1005 void __start_tty(struct tty_struct *tty) in __start_tty() argument
1007 if (!tty->stopped || tty->flow_stopped) in __start_tty()
1009 tty->stopped = 0; in __start_tty()
1010 if (tty->ops->start) in __start_tty()
1011 tty->ops->start(tty); in __start_tty()
1012 tty_wakeup(tty); in __start_tty()
1015 void start_tty(struct tty_struct *tty) in start_tty() argument
1019 spin_lock_irqsave(&tty->flow_lock, flags); in start_tty()
1020 __start_tty(tty); in start_tty()
1021 spin_unlock_irqrestore(&tty->flow_lock, flags); in start_tty()
1059 struct tty_struct *tty = file_tty(file); in tty_read() local
1062 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
1064 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) in tty_read()
1069 ld = tty_ldisc_ref_wait(tty); in tty_read()
1071 i = ld->ops->read(tty, file, buf, count); in tty_read()
1082 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
1084 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
1085 wake_up_interruptible_poll(&tty->write_wait, POLLOUT); in tty_write_unlock()
1088 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
1090 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
1093 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
1105 struct tty_struct *tty, in do_tty_write() argument
1113 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
1134 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
1140 if (tty->write_cnt < chunk) { in do_tty_write()
1151 kfree(tty->write_buf); in do_tty_write()
1152 tty->write_cnt = chunk; in do_tty_write()
1153 tty->write_buf = buf_chunk; in do_tty_write()
1162 if (copy_from_user(tty->write_buf, buf, size)) in do_tty_write()
1164 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
1182 tty_write_unlock(tty); in do_tty_write()
1198 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1200 if (tty) { in tty_write_message()
1201 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1202 tty_lock(tty); in tty_write_message()
1203 if (tty->ops->write && tty->count > 0) in tty_write_message()
1204 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1205 tty_unlock(tty); in tty_write_message()
1206 tty_write_unlock(tty); in tty_write_message()
1231 struct tty_struct *tty = file_tty(file); in tty_write() local
1235 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in tty_write()
1237 if (!tty || !tty->ops->write || in tty_write()
1238 (test_bit(TTY_IO_ERROR, &tty->flags))) in tty_write()
1241 if (tty->ops->write_room == NULL) in tty_write()
1243 tty->driver->name); in tty_write()
1244 ld = tty_ldisc_ref_wait(tty); in tty_write()
1248 ret = do_tty_write(ld->ops->write, tty, file, buf, count); in tty_write()
1280 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1282 int was_stopped = tty->stopped; in tty_send_xchar()
1284 if (tty->ops->send_xchar) { in tty_send_xchar()
1285 down_read(&tty->termios_rwsem); in tty_send_xchar()
1286 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1287 up_read(&tty->termios_rwsem); in tty_send_xchar()
1291 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1294 down_read(&tty->termios_rwsem); in tty_send_xchar()
1296 start_tty(tty); in tty_send_xchar()
1297 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1299 stop_tty(tty); in tty_send_xchar()
1300 up_read(&tty->termios_rwsem); in tty_send_xchar()
1301 tty_write_unlock(tty); in tty_send_xchar()
1360 struct tty_struct *tty; in tty_driver_lookup_tty() local
1363 tty = driver->ops->lookup(driver, inode, idx); in tty_driver_lookup_tty()
1365 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1367 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1368 tty_kref_get(tty); in tty_driver_lookup_tty()
1369 return tty; in tty_driver_lookup_tty()
1380 int tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1383 int idx = tty->index; in tty_init_termios()
1385 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1386 tty->termios = tty->driver->init_termios; in tty_init_termios()
1389 tp = tty->driver->termios[idx]; in tty_init_termios()
1391 tty->termios = *tp; in tty_init_termios()
1393 tty->termios = tty->driver->init_termios; in tty_init_termios()
1396 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1397 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1402 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1404 int ret = tty_init_termios(tty); in tty_standard_install()
1409 tty->count++; in tty_standard_install()
1410 driver->ttys[tty->index] = tty; in tty_standard_install()
1428 struct tty_struct *tty) in tty_driver_install_tty() argument
1430 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1431 tty_standard_install(driver, tty); in tty_driver_install_tty()
1444 void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1447 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1449 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1461 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1463 struct tty_driver *driver = tty->driver; in tty_reopen()
1469 if (!tty->count) in tty_reopen()
1472 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1475 tty->count++; in tty_reopen()
1477 WARN_ON(!tty->ldisc); in tty_reopen()
1508 struct tty_struct *tty; in tty_init_dev() local
1522 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1523 if (!tty) { in tty_init_dev()
1528 tty_lock(tty); in tty_init_dev()
1529 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1533 if (!tty->port) in tty_init_dev()
1534 tty->port = driver->ports[idx]; in tty_init_dev()
1536 WARN_RATELIMIT(!tty->port, in tty_init_dev()
1538 __func__, tty->driver->name); in tty_init_dev()
1540 tty->port->itty = tty; in tty_init_dev()
1547 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1551 return tty; in tty_init_dev()
1554 tty_unlock(tty); in tty_init_dev()
1555 deinitialize_tty_struct(tty); in tty_init_dev()
1556 free_tty_struct(tty); in tty_init_dev()
1563 tty_unlock(tty); in tty_init_dev()
1566 release_tty(tty, idx); in tty_init_dev()
1570 void tty_free_termios(struct tty_struct *tty) in tty_free_termios() argument
1573 int idx = tty->index; in tty_free_termios()
1576 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_free_termios()
1580 tp = tty->driver->termios[idx]; in tty_free_termios()
1587 tty->driver->termios[idx] = tp; in tty_free_termios()
1589 *tp = tty->termios; in tty_free_termios()
1599 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1601 flush_work(&tty->SAK_work); in tty_flush_works()
1602 flush_work(&tty->hangup_work); in tty_flush_works()
1603 if (tty->link) { in tty_flush_works()
1604 flush_work(&tty->link->SAK_work); in tty_flush_works()
1605 flush_work(&tty->link->hangup_work); in tty_flush_works()
1626 struct tty_struct *tty = in release_one_tty() local
1628 struct tty_driver *driver = tty->driver; in release_one_tty()
1631 if (tty->ops->cleanup) in release_one_tty()
1632 tty->ops->cleanup(tty); in release_one_tty()
1634 tty->magic = 0; in release_one_tty()
1639 list_del_init(&tty->tty_files); in release_one_tty()
1642 put_pid(tty->pgrp); in release_one_tty()
1643 put_pid(tty->session); in release_one_tty()
1644 free_tty_struct(tty); in release_one_tty()
1649 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1653 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1654 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1665 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1667 if (tty) in tty_kref_put()
1668 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1684 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1687 WARN_ON(tty->index != idx); in release_tty()
1689 if (tty->ops->shutdown) in release_tty()
1690 tty->ops->shutdown(tty); in release_tty()
1691 tty_free_termios(tty); in release_tty()
1692 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1693 tty->port->itty = NULL; in release_tty()
1694 if (tty->link) in release_tty()
1695 tty->link->port->itty = NULL; in release_tty()
1696 tty_buffer_cancel_work(tty->port); in release_tty()
1698 tty_kref_put(tty->link); in release_tty()
1699 tty_kref_put(tty); in release_tty()
1711 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1714 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1715 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1720 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1723 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1724 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1725 idx, tty->driver->ttys[idx]); in tty_release_checks()
1728 if (tty->driver->other) { in tty_release_checks()
1729 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1731 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1732 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1733 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1736 if (o_tty->link != tty) { in tty_release_checks()
1737 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1766 struct tty_struct *tty = file_tty(filp); in tty_release() local
1773 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1776 tty_lock(tty); in tty_release()
1777 check_tty_count(tty, __func__); in tty_release()
1781 idx = tty->index; in tty_release()
1782 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1783 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1784 o_tty = tty->link; in tty_release()
1786 if (tty_release_checks(tty, idx)) { in tty_release()
1787 tty_unlock(tty); in tty_release()
1791 tty_debug_hangup(tty, "(tty count=%d)...\n", tty->count); in tty_release()
1793 if (tty->ops->close) in tty_release()
1794 tty->ops->close(tty, filp); in tty_release()
1815 if (tty->count <= 1) { in tty_release()
1816 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1817 wake_up_poll(&tty->read_wait, POLLIN); in tty_release()
1820 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1821 wake_up_poll(&tty->write_wait, POLLOUT); in tty_release()
1841 __func__, tty_name(tty)); in tty_release()
1857 if (--tty->count < 0) { in tty_release()
1859 __func__, tty->count, tty_name(tty)); in tty_release()
1860 tty->count = 0; in tty_release()
1881 if (!tty->count) { in tty_release()
1883 session_clear_tty(tty->session); in tty_release()
1890 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1893 tty_unlock(tty); in tty_release()
1901 tty_debug_hangup(tty, "final close\n"); in tty_release()
1905 tty_ldisc_release(tty); in tty_release()
1908 tty_flush_works(tty); in tty_release()
1910 tty_debug_hangup(tty, "freeing structure...\n"); in tty_release()
1918 release_tty(tty, idx); in tty_release()
1937 struct tty_struct *tty; in tty_open_current_tty() local
1943 tty = get_current_tty(); in tty_open_current_tty()
1944 if (!tty) in tty_open_current_tty()
1949 tty_lock(tty); in tty_open_current_tty()
1950 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1952 retval = tty_reopen(tty); in tty_open_current_tty()
1954 tty_unlock(tty); in tty_open_current_tty()
1955 tty = ERR_PTR(retval); in tty_open_current_tty()
1957 return tty; in tty_open_current_tty()
2036 struct tty_struct *tty; in tty_open() local
2054 tty = tty_open_current_tty(device, filp); in tty_open()
2055 if (!tty) { in tty_open()
2064 tty = tty_driver_lookup_tty(driver, inode, index); in tty_open()
2065 if (IS_ERR(tty)) { in tty_open()
2066 retval = PTR_ERR(tty); in tty_open()
2070 if (tty) { in tty_open()
2072 retval = tty_lock_interruptible(tty); in tty_open()
2079 tty_kref_put(tty); in tty_open()
2080 retval = tty_reopen(tty); in tty_open()
2082 tty_unlock(tty); in tty_open()
2083 tty = ERR_PTR(retval); in tty_open()
2086 tty = tty_init_dev(driver, index); in tty_open()
2093 if (IS_ERR(tty)) { in tty_open()
2094 retval = PTR_ERR(tty); in tty_open()
2102 tty_add_file(tty, filp); in tty_open()
2104 check_tty_count(tty, __func__); in tty_open()
2105 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2106 tty->driver->subtype == PTY_TYPE_MASTER) in tty_open()
2109 tty_debug_hangup(tty, "(tty count=%d)\n", tty->count); in tty_open()
2111 if (tty->ops->open) in tty_open()
2112 retval = tty->ops->open(tty, filp); in tty_open()
2118 tty_debug_hangup(tty, "error %d, releasing...\n", retval); in tty_open()
2120 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2136 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2143 !current->signal->tty && in tty_open()
2144 tty->session == NULL) { in tty_open()
2160 __proc_set_tty(tty); in tty_open()
2164 tty_unlock(tty); in tty_open()
2193 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2197 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2200 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2202 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2209 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2214 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2217 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2221 ldisc = tty_ldisc_ref(tty); in __tty_fasync()
2224 ldisc->ops->fasync(tty, on); in __tty_fasync()
2232 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_fasync()
2233 if (tty->pgrp) { in __tty_fasync()
2234 pid = tty->pgrp; in __tty_fasync()
2241 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_fasync()
2252 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2255 tty_lock(tty); in tty_fasync()
2257 tty_unlock(tty); in tty_fasync()
2279 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2284 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2288 tty_audit_tiocsti(tty, ch); in tiocsti()
2289 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2290 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2306 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2310 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2311 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2312 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2327 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2332 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2333 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2337 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2342 tty->winsize = *ws; in tty_do_resize()
2344 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2364 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2370 if (tty->ops->resize) in tiocswinsz()
2371 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2373 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2451 static int tiocsctty(struct tty_struct *tty, struct file *file, int arg) in tiocsctty() argument
2455 tty_lock(tty); in tiocsctty()
2458 if (current->signal->leader && (task_session(current) == tty->session)) in tiocsctty()
2465 if (!current->signal->leader || current->signal->tty) { in tiocsctty()
2470 if (tty->session) { in tiocsctty()
2479 session_clear_tty(tty->session); in tiocsctty()
2492 proc_set_tty(tty); in tiocsctty()
2495 tty_unlock(tty); in tiocsctty()
2507 struct pid *tty_get_pgrp(struct tty_struct *tty) in tty_get_pgrp() argument
2512 spin_lock_irqsave(&tty->ctrl_lock, flags); in tty_get_pgrp()
2513 pgrp = get_pid(tty->pgrp); in tty_get_pgrp()
2514 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in tty_get_pgrp()
2553 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocgpgrp() argument
2561 if (tty == real_tty && current->signal->tty != real_tty) in tiocgpgrp()
2581 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocspgrp() argument
2591 if (!current->signal->tty || in tiocspgrp()
2592 (current->signal->tty != real_tty) || in tiocspgrp()
2608 spin_lock_irq(&tty->ctrl_lock); in tiocspgrp()
2611 spin_unlock_irq(&tty->ctrl_lock); in tiocspgrp()
2629 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) in tiocgsid() argument
2635 if (tty == real_tty && current->signal->tty != real_tty) in tiocgsid()
2652 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2660 ret = tty_set_ldisc(tty, ldisc); in tiocsetd()
2676 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2681 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2700 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2704 if (tty->ops->break_ctl == NULL) in send_break()
2707 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2708 retval = tty->ops->break_ctl(tty, duration); in send_break()
2711 if (tty_write_lock(tty, 0) < 0) in send_break()
2713 retval = tty->ops->break_ctl(tty, -1); in send_break()
2718 retval = tty->ops->break_ctl(tty, 0); in send_break()
2720 tty_write_unlock(tty); in send_break()
2739 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2743 if (tty->ops->tiocmget) { in tty_tiocmget()
2744 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2764 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2770 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2791 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2794 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2799 if (tty->ops->get_icount) in tty_tiocgicount()
2800 retval = tty->ops->get_icount(tty, &icount); in tty_tiocgicount()
2830 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2832 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2833 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2834 tty = tty->link; in tty_pair_get_tty()
2835 return tty; in tty_pair_get_tty()
2843 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2849 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2852 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2863 retval = tty_check_change(tty); in tty_ioctl()
2867 tty_wait_until_sent(tty, 0); in tty_ioctl()
2879 return tiocsti(tty, p); in tty_ioctl()
2885 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2889 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2892 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2896 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2900 if (current->signal->tty != tty) in tty_ioctl()
2905 return tiocsctty(tty, file, arg); in tty_ioctl()
2907 return tiocgpgrp(tty, real_tty, p); in tty_ioctl()
2909 return tiocspgrp(tty, real_tty, p); in tty_ioctl()
2911 return tiocgsid(tty, real_tty, p); in tty_ioctl()
2913 return tiocgetd(tty, p); in tty_ioctl()
2915 return tiocsetd(tty, p); in tty_ioctl()
2919 tty_vhangup(tty); in tty_ioctl()
2930 if (tty->ops->break_ctl) in tty_ioctl()
2931 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2934 if (tty->ops->break_ctl) in tty_ioctl()
2935 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2943 return send_break(tty, 250); in tty_ioctl()
2946 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2949 return tty_tiocmget(tty, p); in tty_ioctl()
2953 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2955 retval = tty_tiocgicount(tty, p); in tty_ioctl()
2965 tty_buffer_flush(tty, NULL); in tty_ioctl()
2973 if (tty->ops->ioctl) { in tty_ioctl()
2974 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2978 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2981 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
2993 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2997 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
3000 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
3001 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
3006 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
3008 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
3010 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); in tty_compat_ioctl()
3043 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3046 tty_hangup(tty); in __do_SAK()
3052 if (!tty) in __do_SAK()
3054 session = tty->session; in __do_SAK()
3056 tty_ldisc_flush(tty); in __do_SAK()
3058 tty_driver_flush_buffer(tty); in __do_SAK()
3072 if (p->signal->tty == tty) { in __do_SAK()
3080 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3095 struct tty_struct *tty = in do_SAK_work() local
3097 __do_SAK(tty); in do_SAK_work()
3106 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3108 if (!tty) in do_SAK()
3110 schedule_work(&tty->SAK_work); in do_SAK()
3122 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3124 dev_t devt = tty_devnum(tty); in tty_get_device()
3139 struct tty_struct *tty; in alloc_tty_struct() local
3141 tty = kzalloc(sizeof(*tty), GFP_KERNEL); in alloc_tty_struct()
3142 if (!tty) in alloc_tty_struct()
3145 kref_init(&tty->kref); in alloc_tty_struct()
3146 tty->magic = TTY_MAGIC; in alloc_tty_struct()
3147 tty_ldisc_init(tty); in alloc_tty_struct()
3148 tty->session = NULL; in alloc_tty_struct()
3149 tty->pgrp = NULL; in alloc_tty_struct()
3150 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3151 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3152 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3153 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3154 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3155 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3156 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3157 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3158 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3159 spin_lock_init(&tty->ctrl_lock); in alloc_tty_struct()
3160 spin_lock_init(&tty->flow_lock); in alloc_tty_struct()
3161 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3162 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3164 tty->driver = driver; in alloc_tty_struct()
3165 tty->ops = driver->ops; in alloc_tty_struct()
3166 tty->index = idx; in alloc_tty_struct()
3167 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3168 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3170 return tty; in alloc_tty_struct()
3182 void deinitialize_tty_struct(struct tty_struct *tty) in deinitialize_tty_struct() argument
3184 tty_ldisc_deinit(tty); in deinitialize_tty_struct()
3199 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3201 if (tty->ops->put_char) in tty_put_char()
3202 return tty->ops->put_char(tty, ch); in tty_put_char()
3203 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3559 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3561 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()