Lines Matching refs:tty

115 # define tty_debug_hangup(tty, f, args...)	tty_debug(tty, f, ##args)  argument
117 # define tty_debug_hangup(tty, f, args...) do { } while (0) argument
158 static void release_tty(struct tty_struct *tty, int idx);
168 static void free_tty_struct(struct tty_struct *tty) in free_tty_struct() argument
170 tty_ldisc_deinit(tty); in free_tty_struct()
171 put_device(tty->dev); in free_tty_struct()
172 kvfree(tty->write_buf); 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()
202 spin_lock(&tty->files_lock); in tty_add_file()
203 list_add(&priv->list, &tty->tty_files); in tty_add_file()
204 spin_unlock(&tty->files_lock); in tty_add_file()
226 struct tty_struct *tty = priv->tty; in tty_del_file() local
228 spin_lock(&tty->files_lock); in tty_del_file()
230 spin_unlock(&tty->files_lock); in tty_del_file()
243 const char *tty_name(const struct tty_struct *tty) in tty_name() argument
245 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
247 return tty->name; in tty_name()
251 const char *tty_driver_name(const struct tty_struct *tty) in tty_driver_name() argument
253 if (!tty || !tty->driver) in tty_driver_name()
255 return tty->driver->name; in tty_driver_name()
258 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
262 if (!tty) { in tty_paranoia_check()
272 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
278 spin_lock(&tty->files_lock); in check_tty_count()
279 list_for_each(p, &tty->tty_files) { in check_tty_count()
282 spin_unlock(&tty->files_lock); in check_tty_count()
283 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
284 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
285 tty->link && tty->link->count) in check_tty_count()
287 if (tty_port_kopened(tty->port)) in check_tty_count()
289 if (tty->count != (count + kopen_count)) { in check_tty_count()
290 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
291 routine, tty->count, count, kopen_count); in check_tty_count()
459 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
461 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
462 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
516 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
520 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
521 ld = tty_ldisc_ref(tty); in tty_wakeup()
524 ld->ops->write_wakeup(tty); in tty_wakeup()
528 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
539 static struct file *tty_release_redirect(struct tty_struct *tty) in tty_release_redirect() argument
544 if (redirect && file_tty(redirect) == tty) { in tty_release_redirect()
578 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
586 if (!tty) in __tty_hangup()
589 f = tty_release_redirect(tty); in __tty_hangup()
591 tty_lock(tty); in __tty_hangup()
593 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
594 tty_unlock(tty); in __tty_hangup()
604 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
610 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
612 spin_lock(&tty->files_lock); in __tty_hangup()
614 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
624 spin_unlock(&tty->files_lock); in __tty_hangup()
626 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
629 tty_kref_put(tty); in __tty_hangup()
631 tty_ldisc_hangup(tty, cons_filp != NULL); in __tty_hangup()
633 spin_lock_irq(&tty->ctrl.lock); in __tty_hangup()
634 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
635 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
636 put_pid(tty->ctrl.session); in __tty_hangup()
637 put_pid(tty->ctrl.pgrp); in __tty_hangup()
638 tty->ctrl.session = NULL; in __tty_hangup()
639 tty->ctrl.pgrp = NULL; in __tty_hangup()
640 tty->ctrl.pktstatus = 0; in __tty_hangup()
641 spin_unlock_irq(&tty->ctrl.lock); in __tty_hangup()
650 if (tty->ops->close) in __tty_hangup()
652 tty->ops->close(tty, cons_filp); in __tty_hangup()
653 } else if (tty->ops->hangup) in __tty_hangup()
654 tty->ops->hangup(tty); in __tty_hangup()
660 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
661 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
662 tty_unlock(tty); in __tty_hangup()
670 struct tty_struct *tty = in do_tty_hangup() local
673 __tty_hangup(tty, 0); in do_tty_hangup()
683 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
685 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
686 schedule_work(&tty->hangup_work); in tty_hangup()
698 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
700 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
701 __tty_hangup(tty, 0); in tty_vhangup()
713 struct tty_struct *tty; in tty_vhangup_self() local
715 tty = get_current_tty(); in tty_vhangup_self()
716 if (tty) { in tty_vhangup_self()
717 tty_vhangup(tty); in tty_vhangup_self()
718 tty_kref_put(tty); in tty_vhangup_self()
732 void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
734 tty_debug_hangup(tty, "session hangup\n"); in tty_vhangup_session()
735 __tty_hangup(tty, 1); in tty_vhangup_session()
750 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
752 if (tty->flow.stopped) in __stop_tty()
754 tty->flow.stopped = true; in __stop_tty()
755 if (tty->ops->stop) in __stop_tty()
756 tty->ops->stop(tty); in __stop_tty()
773 void stop_tty(struct tty_struct *tty) in stop_tty() argument
777 spin_lock_irqsave(&tty->flow.lock, flags); in stop_tty()
778 __stop_tty(tty); in stop_tty()
779 spin_unlock_irqrestore(&tty->flow.lock, flags); in stop_tty()
783 void __start_tty(struct tty_struct *tty) in __start_tty() argument
785 if (!tty->flow.stopped || tty->flow.tco_stopped) in __start_tty()
787 tty->flow.stopped = false; in __start_tty()
788 if (tty->ops->start) in __start_tty()
789 tty->ops->start(tty); in __start_tty()
790 tty_wakeup(tty); in __start_tty()
804 void start_tty(struct tty_struct *tty) in start_tty() argument
808 spin_lock_irqsave(&tty->flow.lock, flags); in start_tty()
809 __start_tty(tty); in start_tty()
810 spin_unlock_irqrestore(&tty->flow.lock, flags); in start_tty()
839 static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_read() argument
852 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
911 struct tty_struct *tty = file_tty(file); in tty_read() local
914 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
916 if (!tty || tty_io_error(tty)) in tty_read()
922 ld = tty_ldisc_ref_wait(tty); in tty_read()
927 i = iterate_tty_read(ld, tty, file, to); in tty_read()
936 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
938 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
939 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
942 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
944 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
947 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
959 struct tty_struct *tty, in do_tty_write() argument
967 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
985 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
991 if (tty->write_cnt < chunk) { in do_tty_write()
1002 kvfree(tty->write_buf); in do_tty_write()
1003 tty->write_cnt = chunk; in do_tty_write()
1004 tty->write_buf = buf_chunk; in do_tty_write()
1015 if (copy_from_iter(tty->write_buf, size, from) != size) in do_tty_write()
1018 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
1043 tty_write_unlock(tty); in do_tty_write()
1058 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1060 if (tty) { in tty_write_message()
1061 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1062 tty_lock(tty); in tty_write_message()
1063 if (tty->ops->write && tty->count > 0) in tty_write_message()
1064 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1065 tty_unlock(tty); in tty_write_message()
1066 tty_write_unlock(tty); in tty_write_message()
1072 struct tty_struct *tty = file_tty(file); in file_tty_write() local
1076 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in file_tty_write()
1078 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1081 if (tty->ops->write_room == NULL) in file_tty_write()
1082 tty_err(tty, "missing write_room method\n"); in file_tty_write()
1083 ld = tty_ldisc_ref_wait(tty); in file_tty_write()
1089 ret = do_tty_write(ld->ops->write, tty, file, from); in file_tty_write()
1145 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1147 bool was_stopped = tty->flow.stopped; in tty_send_xchar()
1149 if (tty->ops->send_xchar) { in tty_send_xchar()
1150 down_read(&tty->termios_rwsem); in tty_send_xchar()
1151 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1152 up_read(&tty->termios_rwsem); in tty_send_xchar()
1156 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1159 down_read(&tty->termios_rwsem); in tty_send_xchar()
1161 start_tty(tty); in tty_send_xchar()
1162 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1164 stop_tty(tty); in tty_send_xchar()
1165 up_read(&tty->termios_rwsem); in tty_send_xchar()
1166 tty_write_unlock(tty); in tty_send_xchar()
1225 struct tty_struct *tty; in tty_driver_lookup_tty() local
1229 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1231 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1235 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1237 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1238 tty_kref_get(tty); in tty_driver_lookup_tty()
1239 return tty; in tty_driver_lookup_tty()
1249 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1252 int idx = tty->index; in tty_init_termios()
1254 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1255 tty->termios = tty->driver->init_termios; in tty_init_termios()
1258 tp = tty->driver->termios[idx]; in tty_init_termios()
1260 tty->termios = *tp; in tty_init_termios()
1261 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1263 tty->termios = tty->driver->init_termios; in tty_init_termios()
1266 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1267 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1279 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1281 tty_init_termios(tty); in tty_standard_install()
1283 tty->count++; in tty_standard_install()
1284 driver->ttys[tty->index] = tty; in tty_standard_install()
1301 struct tty_struct *tty) in tty_driver_install_tty() argument
1303 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1304 tty_standard_install(driver, tty); in tty_driver_install_tty()
1317 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1320 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1322 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1334 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1336 struct tty_driver *driver = tty->driver; in tty_reopen()
1344 if (!tty->count) in tty_reopen()
1347 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1350 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1354 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1358 if (!tty->ldisc) in tty_reopen()
1359 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1360 tty_ldisc_unlock(tty); in tty_reopen()
1364 tty->count++; in tty_reopen()
1394 struct tty_struct *tty; in tty_init_dev() local
1408 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1409 if (!tty) { in tty_init_dev()
1414 tty_lock(tty); in tty_init_dev()
1415 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1419 if (!tty->port) in tty_init_dev()
1420 tty->port = driver->ports[idx]; in tty_init_dev()
1422 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1424 __func__, tty->driver->name)) { in tty_init_dev()
1429 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1432 tty->port->itty = tty; in tty_init_dev()
1439 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1442 tty_ldisc_unlock(tty); in tty_init_dev()
1444 return tty; in tty_init_dev()
1447 tty_unlock(tty); in tty_init_dev()
1448 free_tty_struct(tty); in tty_init_dev()
1455 tty_ldisc_unlock(tty); in tty_init_dev()
1456 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1459 tty_unlock(tty); in tty_init_dev()
1460 release_tty(tty, idx); in tty_init_dev()
1470 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1473 int idx = tty->index; in tty_save_termios()
1476 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1480 tp = tty->driver->termios[idx]; in tty_save_termios()
1485 tty->driver->termios[idx] = tp; in tty_save_termios()
1487 *tp = tty->termios; in tty_save_termios()
1497 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1499 flush_work(&tty->SAK_work); in tty_flush_works()
1500 flush_work(&tty->hangup_work); in tty_flush_works()
1501 if (tty->link) { in tty_flush_works()
1502 flush_work(&tty->link->SAK_work); in tty_flush_works()
1503 flush_work(&tty->link->hangup_work); in tty_flush_works()
1524 struct tty_struct *tty = in release_one_tty() local
1526 struct tty_driver *driver = tty->driver; in release_one_tty()
1529 if (tty->ops->cleanup) in release_one_tty()
1530 tty->ops->cleanup(tty); in release_one_tty()
1535 spin_lock(&tty->files_lock); in release_one_tty()
1536 list_del_init(&tty->tty_files); in release_one_tty()
1537 spin_unlock(&tty->files_lock); in release_one_tty()
1539 put_pid(tty->ctrl.pgrp); in release_one_tty()
1540 put_pid(tty->ctrl.session); in release_one_tty()
1541 free_tty_struct(tty); in release_one_tty()
1546 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1551 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1552 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1562 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1564 if (tty) in tty_kref_put()
1565 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1582 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1585 WARN_ON(tty->index != idx); in release_tty()
1587 if (tty->ops->shutdown) in release_tty()
1588 tty->ops->shutdown(tty); in release_tty()
1589 tty_save_termios(tty); in release_tty()
1590 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1591 if (tty->port) in release_tty()
1592 tty->port->itty = NULL; in release_tty()
1593 if (tty->link) in release_tty()
1594 tty->link->port->itty = NULL; in release_tty()
1595 if (tty->port) in release_tty()
1596 tty_buffer_cancel_work(tty->port); in release_tty()
1597 if (tty->link) in release_tty()
1598 tty_buffer_cancel_work(tty->link->port); in release_tty()
1600 tty_kref_put(tty->link); in release_tty()
1601 tty_kref_put(tty); in release_tty()
1612 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1615 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1616 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1621 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1624 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1625 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1626 idx, tty->driver->ttys[idx]); in tty_release_checks()
1629 if (tty->driver->other) { in tty_release_checks()
1630 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1632 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1633 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1634 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1637 if (o_tty->link != tty) { in tty_release_checks()
1638 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1654 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1659 tty_ldisc_release(tty); in tty_kclose()
1662 tty_flush_works(tty); in tty_kclose()
1664 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1670 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1671 release_tty(tty, tty->index); in tty_kclose()
1684 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1689 tty_ldisc_release(tty); in tty_release_struct()
1692 tty_flush_works(tty); in tty_release_struct()
1694 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1700 release_tty(tty, idx); in tty_release_struct()
1725 struct tty_struct *tty = file_tty(filp); in tty_release() local
1732 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1735 tty_lock(tty); in tty_release()
1736 check_tty_count(tty, __func__); in tty_release()
1740 idx = tty->index; in tty_release()
1741 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1742 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1743 o_tty = tty->link; in tty_release()
1745 if (tty_release_checks(tty, idx)) { in tty_release()
1746 tty_unlock(tty); in tty_release()
1750 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1752 if (tty->ops->close) in tty_release()
1753 tty->ops->close(tty, filp); in tty_release()
1774 if (tty->count <= 1) { in tty_release()
1775 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1776 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1779 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1780 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1799 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1810 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1814 if (--tty->count < 0) { in tty_release()
1815 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1816 tty->count = 0; in tty_release()
1837 if (!tty->count) { in tty_release()
1839 session_clear_tty(tty->ctrl.session); in tty_release()
1846 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1849 tty_unlock(tty); in tty_release()
1858 tty_debug_hangup(tty, "final close\n"); in tty_release()
1860 tty_release_struct(tty, idx); in tty_release()
1877 struct tty_struct *tty; in tty_open_current_tty() local
1883 tty = get_current_tty(); in tty_open_current_tty()
1884 if (!tty) in tty_open_current_tty()
1889 tty_lock(tty); in tty_open_current_tty()
1890 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1892 retval = tty_reopen(tty); in tty_open_current_tty()
1894 tty_unlock(tty); in tty_open_current_tty()
1895 tty = ERR_PTR(retval); in tty_open_current_tty()
1897 return tty; in tty_open_current_tty()
1954 struct tty_struct *tty; in tty_kopen() local
1966 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1967 if (IS_ERR(tty) || shared) in tty_kopen()
1970 if (tty) { in tty_kopen()
1972 tty_kref_put(tty); in tty_kopen()
1973 tty = ERR_PTR(-EBUSY); in tty_kopen()
1975 tty = tty_init_dev(driver, index); in tty_kopen()
1976 if (IS_ERR(tty)) in tty_kopen()
1978 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1983 return tty; in tty_kopen()
2040 struct tty_struct *tty; in tty_open_by_driver() local
2053 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
2054 if (IS_ERR(tty)) { in tty_open_by_driver()
2059 if (tty) { in tty_open_by_driver()
2060 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2061 tty_kref_put(tty); in tty_open_by_driver()
2063 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2067 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
2068 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
2072 tty = ERR_PTR(retval); in tty_open_by_driver()
2075 retval = tty_reopen(tty); in tty_open_by_driver()
2077 tty_unlock(tty); in tty_open_by_driver()
2078 tty = ERR_PTR(retval); in tty_open_by_driver()
2081 tty = tty_init_dev(driver, index); in tty_open_by_driver()
2086 return tty; in tty_open_by_driver()
2114 struct tty_struct *tty; in tty_open() local
2126 tty = tty_open_current_tty(device, filp); in tty_open()
2127 if (!tty) in tty_open()
2128 tty = tty_open_by_driver(device, filp); in tty_open()
2130 if (IS_ERR(tty)) { in tty_open()
2132 retval = PTR_ERR(tty); in tty_open()
2139 tty_add_file(tty, filp); in tty_open()
2141 check_tty_count(tty, __func__); in tty_open()
2142 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2144 if (tty->ops->open) in tty_open()
2145 retval = tty->ops->open(tty, filp); in tty_open()
2151 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2153 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2169 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2174 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2175 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2177 tty_open_proc_set_tty(filp, tty); in tty_open()
2178 tty_unlock(tty); in tty_open()
2196 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2200 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2203 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2207 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2214 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2218 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2221 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2229 spin_lock_irqsave(&tty->ctrl.lock, flags); in __tty_fasync()
2230 if (tty->ctrl.pgrp) { in __tty_fasync()
2231 pid = tty->ctrl.pgrp; in __tty_fasync()
2238 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __tty_fasync()
2249 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2252 tty_lock(tty); in tty_fasync()
2255 tty_unlock(tty); in tty_fasync()
2274 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2282 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2286 tty_audit_tiocsti(tty, ch); in tiocsti()
2287 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2290 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2292 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2293 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2308 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2312 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2313 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2314 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()
2363 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()
2424 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2432 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2447 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2452 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2471 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2475 if (tty->ops->break_ctl == NULL) in send_break()
2478 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2479 retval = tty->ops->break_ctl(tty, duration); in send_break()
2482 if (tty_write_lock(tty, 0) < 0) in send_break()
2484 retval = tty->ops->break_ctl(tty, -1); in send_break()
2489 retval = tty->ops->break_ctl(tty, 0); in send_break()
2491 tty_write_unlock(tty); in send_break()
2508 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2512 if (tty->ops->tiocmget) { in tty_tiocmget()
2513 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2532 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2538 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2559 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2571 int tty_get_icount(struct tty_struct *tty, in tty_get_icount() argument
2576 if (tty->ops->get_icount) in tty_get_icount()
2577 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2583 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2588 retval = tty_get_icount(tty, &icount); in tty_tiocgicount()
2597 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2608 if (!tty->ops->set_serial) in tty_set_serial()
2611 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2614 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2621 return tty_set_serial(tty, &v); in tty_tiocsserial()
2624 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2630 if (!tty->ops->get_serial) in tty_tiocgserial()
2632 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2642 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2644 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2645 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2646 tty = tty->link; in tty_pair_get_tty()
2647 return tty; in tty_pair_get_tty()
2655 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2661 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2664 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2675 retval = tty_check_change(tty); in tty_ioctl()
2679 tty_wait_until_sent(tty, 0); in tty_ioctl()
2691 return tiocsti(tty, p); in tty_ioctl()
2697 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2699 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2702 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2706 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2711 return tiocgetd(tty, p); in tty_ioctl()
2713 return tiocsetd(tty, p); in tty_ioctl()
2717 tty_vhangup(tty); in tty_ioctl()
2729 if (tty->ops->break_ctl) in tty_ioctl()
2730 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2733 if (tty->ops->break_ctl) in tty_ioctl()
2734 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2742 return send_break(tty, 250); in tty_ioctl()
2745 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2748 return tty_tiocmget(tty, p); in tty_ioctl()
2752 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2754 return tty_tiocgicount(tty, p); in tty_ioctl()
2760 tty_buffer_flush(tty, NULL); in tty_ioctl()
2765 return tty_tiocsserial(tty, p); in tty_ioctl()
2767 return tty_tiocgserial(tty, p); in tty_ioctl()
2770 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2772 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2776 if (tty->ops->ioctl) { in tty_ioctl()
2777 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2781 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2786 retval = ld->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2818 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2833 return tty_set_serial(tty, &v); in compat_tty_tiocsserial()
2836 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2846 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2848 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2863 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2946 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2951 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2953 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2955 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2956 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2961 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2965 retval = ld->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2967 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd), in tty_compat_ioctl()
3001 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3008 spin_lock_irqsave(&tty->ctrl.lock, flags); in __do_SAK()
3009 session = get_pid(tty->ctrl.session); in __do_SAK()
3010 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __do_SAK()
3012 tty_ldisc_flush(tty); in __do_SAK()
3014 tty_driver_flush_buffer(tty); in __do_SAK()
3019 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
3026 if (p->signal->tty == tty) { in __do_SAK()
3027 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
3034 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3036 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
3049 struct tty_struct *tty = in do_SAK_work() local
3051 __do_SAK(tty); in do_SAK_work()
3060 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3062 if (!tty) in do_SAK()
3064 schedule_work(&tty->SAK_work); in do_SAK()
3069 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3071 dev_t devt = tty_devnum(tty); in tty_get_device()
3088 struct tty_struct *tty; in alloc_tty_struct() local
3090 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT); in alloc_tty_struct()
3091 if (!tty) in alloc_tty_struct()
3094 kref_init(&tty->kref); in alloc_tty_struct()
3095 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
3096 kfree(tty); in alloc_tty_struct()
3099 tty->ctrl.session = NULL; in alloc_tty_struct()
3100 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3101 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3102 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3103 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3104 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3105 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3106 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3107 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3108 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3109 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3110 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3111 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3112 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3113 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3114 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3116 tty->driver = driver; in alloc_tty_struct()
3117 tty->ops = driver->ops; in alloc_tty_struct()
3118 tty->index = idx; in alloc_tty_struct()
3119 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3120 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3122 return tty; in alloc_tty_struct()
3138 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3140 if (tty->ops->put_char) in tty_put_char()
3141 return tty->ops->put_char(tty, ch); in tty_put_char()
3142 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3492 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3494 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()