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()
1233 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1235 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1236 tty_kref_get(tty); in tty_driver_lookup_tty()
1237 return tty; in tty_driver_lookup_tty()
1247 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1250 int idx = tty->index; in tty_init_termios()
1252 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1253 tty->termios = tty->driver->init_termios; in tty_init_termios()
1256 tp = tty->driver->termios[idx]; in tty_init_termios()
1258 tty->termios = *tp; in tty_init_termios()
1259 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1261 tty->termios = tty->driver->init_termios; in tty_init_termios()
1264 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1265 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1277 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1279 tty_init_termios(tty); in tty_standard_install()
1281 tty->count++; in tty_standard_install()
1282 driver->ttys[tty->index] = tty; in tty_standard_install()
1299 struct tty_struct *tty) in tty_driver_install_tty() argument
1301 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1302 tty_standard_install(driver, tty); in tty_driver_install_tty()
1315 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1318 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1320 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1332 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1334 struct tty_driver *driver = tty->driver; in tty_reopen()
1342 if (!tty->count) in tty_reopen()
1345 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1348 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1352 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1356 if (!tty->ldisc) in tty_reopen()
1357 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1358 tty_ldisc_unlock(tty); in tty_reopen()
1362 tty->count++; in tty_reopen()
1392 struct tty_struct *tty; in tty_init_dev() local
1406 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1407 if (!tty) { in tty_init_dev()
1412 tty_lock(tty); in tty_init_dev()
1413 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1417 if (!tty->port) in tty_init_dev()
1418 tty->port = driver->ports[idx]; in tty_init_dev()
1420 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1422 __func__, tty->driver->name)) { in tty_init_dev()
1427 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1430 tty->port->itty = tty; in tty_init_dev()
1437 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1440 tty_ldisc_unlock(tty); in tty_init_dev()
1442 return tty; in tty_init_dev()
1445 tty_unlock(tty); in tty_init_dev()
1446 free_tty_struct(tty); in tty_init_dev()
1453 tty_ldisc_unlock(tty); in tty_init_dev()
1454 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1457 tty_unlock(tty); in tty_init_dev()
1458 release_tty(tty, idx); in tty_init_dev()
1468 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1471 int idx = tty->index; in tty_save_termios()
1474 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1478 tp = tty->driver->termios[idx]; in tty_save_termios()
1483 tty->driver->termios[idx] = tp; in tty_save_termios()
1485 *tp = tty->termios; in tty_save_termios()
1495 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1497 flush_work(&tty->SAK_work); in tty_flush_works()
1498 flush_work(&tty->hangup_work); in tty_flush_works()
1499 if (tty->link) { in tty_flush_works()
1500 flush_work(&tty->link->SAK_work); in tty_flush_works()
1501 flush_work(&tty->link->hangup_work); in tty_flush_works()
1522 struct tty_struct *tty = in release_one_tty() local
1524 struct tty_driver *driver = tty->driver; in release_one_tty()
1527 if (tty->ops->cleanup) in release_one_tty()
1528 tty->ops->cleanup(tty); in release_one_tty()
1533 spin_lock(&tty->files_lock); in release_one_tty()
1534 list_del_init(&tty->tty_files); in release_one_tty()
1535 spin_unlock(&tty->files_lock); in release_one_tty()
1537 put_pid(tty->ctrl.pgrp); in release_one_tty()
1538 put_pid(tty->ctrl.session); in release_one_tty()
1539 free_tty_struct(tty); in release_one_tty()
1544 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1549 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1550 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1560 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1562 if (tty) in tty_kref_put()
1563 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1580 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1583 WARN_ON(tty->index != idx); in release_tty()
1585 if (tty->ops->shutdown) in release_tty()
1586 tty->ops->shutdown(tty); in release_tty()
1587 tty_save_termios(tty); in release_tty()
1588 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1589 if (tty->port) in release_tty()
1590 tty->port->itty = NULL; in release_tty()
1591 if (tty->link) in release_tty()
1592 tty->link->port->itty = NULL; in release_tty()
1593 if (tty->port) in release_tty()
1594 tty_buffer_cancel_work(tty->port); in release_tty()
1595 if (tty->link) in release_tty()
1596 tty_buffer_cancel_work(tty->link->port); in release_tty()
1598 tty_kref_put(tty->link); in release_tty()
1599 tty_kref_put(tty); in release_tty()
1610 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1613 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1614 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1619 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1622 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1623 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1624 idx, tty->driver->ttys[idx]); in tty_release_checks()
1627 if (tty->driver->other) { in tty_release_checks()
1628 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1630 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1631 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1632 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1635 if (o_tty->link != tty) { in tty_release_checks()
1636 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1652 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1657 tty_ldisc_release(tty); in tty_kclose()
1660 tty_flush_works(tty); in tty_kclose()
1662 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1668 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1669 release_tty(tty, tty->index); in tty_kclose()
1682 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1687 tty_ldisc_release(tty); in tty_release_struct()
1690 tty_flush_works(tty); in tty_release_struct()
1692 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1698 release_tty(tty, idx); in tty_release_struct()
1723 struct tty_struct *tty = file_tty(filp); in tty_release() local
1730 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1733 tty_lock(tty); in tty_release()
1734 check_tty_count(tty, __func__); in tty_release()
1738 idx = tty->index; in tty_release()
1739 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1740 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1741 o_tty = tty->link; in tty_release()
1743 if (tty_release_checks(tty, idx)) { in tty_release()
1744 tty_unlock(tty); in tty_release()
1748 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1750 if (tty->ops->close) in tty_release()
1751 tty->ops->close(tty, filp); in tty_release()
1772 if (tty->count <= 1) { in tty_release()
1773 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1774 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1777 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1778 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1797 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1808 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1812 if (--tty->count < 0) { in tty_release()
1813 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1814 tty->count = 0; in tty_release()
1835 if (!tty->count) { in tty_release()
1837 session_clear_tty(tty->ctrl.session); in tty_release()
1844 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1847 tty_unlock(tty); in tty_release()
1856 tty_debug_hangup(tty, "final close\n"); in tty_release()
1858 tty_release_struct(tty, idx); in tty_release()
1875 struct tty_struct *tty; in tty_open_current_tty() local
1881 tty = get_current_tty(); in tty_open_current_tty()
1882 if (!tty) in tty_open_current_tty()
1887 tty_lock(tty); in tty_open_current_tty()
1888 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1890 retval = tty_reopen(tty); in tty_open_current_tty()
1892 tty_unlock(tty); in tty_open_current_tty()
1893 tty = ERR_PTR(retval); in tty_open_current_tty()
1895 return tty; in tty_open_current_tty()
1952 struct tty_struct *tty; in tty_kopen() local
1964 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1965 if (IS_ERR(tty) || shared) in tty_kopen()
1968 if (tty) { in tty_kopen()
1970 tty_kref_put(tty); in tty_kopen()
1971 tty = ERR_PTR(-EBUSY); in tty_kopen()
1973 tty = tty_init_dev(driver, index); in tty_kopen()
1974 if (IS_ERR(tty)) in tty_kopen()
1976 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1981 return tty; in tty_kopen()
2038 struct tty_struct *tty; in tty_open_by_driver() local
2051 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
2052 if (IS_ERR(tty)) { in tty_open_by_driver()
2057 if (tty) { in tty_open_by_driver()
2058 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2059 tty_kref_put(tty); in tty_open_by_driver()
2061 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2065 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
2066 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
2070 tty = ERR_PTR(retval); in tty_open_by_driver()
2073 retval = tty_reopen(tty); in tty_open_by_driver()
2075 tty_unlock(tty); in tty_open_by_driver()
2076 tty = ERR_PTR(retval); in tty_open_by_driver()
2079 tty = tty_init_dev(driver, index); in tty_open_by_driver()
2084 return tty; in tty_open_by_driver()
2112 struct tty_struct *tty; in tty_open() local
2124 tty = tty_open_current_tty(device, filp); in tty_open()
2125 if (!tty) in tty_open()
2126 tty = tty_open_by_driver(device, filp); in tty_open()
2128 if (IS_ERR(tty)) { in tty_open()
2130 retval = PTR_ERR(tty); in tty_open()
2137 tty_add_file(tty, filp); in tty_open()
2139 check_tty_count(tty, __func__); in tty_open()
2140 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2142 if (tty->ops->open) in tty_open()
2143 retval = tty->ops->open(tty, filp); in tty_open()
2149 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2151 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2167 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2172 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2173 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2175 tty_open_proc_set_tty(filp, tty); in tty_open()
2176 tty_unlock(tty); in tty_open()
2194 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2198 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2201 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2205 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2212 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2216 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2219 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2227 spin_lock_irqsave(&tty->ctrl.lock, flags); in __tty_fasync()
2228 if (tty->ctrl.pgrp) { in __tty_fasync()
2229 pid = tty->ctrl.pgrp; in __tty_fasync()
2236 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __tty_fasync()
2247 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2250 tty_lock(tty); in tty_fasync()
2253 tty_unlock(tty); in tty_fasync()
2271 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2276 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2280 tty_audit_tiocsti(tty, ch); in tiocsti()
2281 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2284 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2286 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2287 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2302 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2306 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2307 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2308 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2321 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2326 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2327 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2331 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2336 tty->winsize = *ws; in tty_do_resize()
2338 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2357 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2364 if (tty->ops->resize) in tiocswinsz()
2365 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2367 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2418 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2426 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2441 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2446 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2465 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2469 if (tty->ops->break_ctl == NULL) in send_break()
2472 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2473 retval = tty->ops->break_ctl(tty, duration); in send_break()
2476 if (tty_write_lock(tty, 0) < 0) in send_break()
2478 retval = tty->ops->break_ctl(tty, -1); in send_break()
2483 retval = tty->ops->break_ctl(tty, 0); in send_break()
2485 tty_write_unlock(tty); in send_break()
2502 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2506 if (tty->ops->tiocmget) { in tty_tiocmget()
2507 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2526 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2532 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2553 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2565 int tty_get_icount(struct tty_struct *tty, in tty_get_icount() argument
2570 if (tty->ops->get_icount) in tty_get_icount()
2571 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2577 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2582 retval = tty_get_icount(tty, &icount); in tty_tiocgicount()
2591 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2602 if (!tty->ops->set_serial) in tty_set_serial()
2605 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2608 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2615 return tty_set_serial(tty, &v); in tty_tiocsserial()
2618 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2624 if (!tty->ops->get_serial) in tty_tiocgserial()
2626 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2636 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2638 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2639 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2640 tty = tty->link; in tty_pair_get_tty()
2641 return tty; in tty_pair_get_tty()
2649 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2655 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2658 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2669 retval = tty_check_change(tty); in tty_ioctl()
2673 tty_wait_until_sent(tty, 0); in tty_ioctl()
2685 return tiocsti(tty, p); in tty_ioctl()
2691 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2693 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2696 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2700 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2705 return tiocgetd(tty, p); in tty_ioctl()
2707 return tiocsetd(tty, p); in tty_ioctl()
2711 tty_vhangup(tty); in tty_ioctl()
2723 if (tty->ops->break_ctl) in tty_ioctl()
2724 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2727 if (tty->ops->break_ctl) in tty_ioctl()
2728 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2736 return send_break(tty, 250); in tty_ioctl()
2739 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2742 return tty_tiocmget(tty, p); in tty_ioctl()
2746 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2748 return tty_tiocgicount(tty, p); in tty_ioctl()
2754 tty_buffer_flush(tty, NULL); in tty_ioctl()
2759 return tty_tiocsserial(tty, p); in tty_ioctl()
2761 return tty_tiocgserial(tty, p); in tty_ioctl()
2764 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2766 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2770 if (tty->ops->ioctl) { in tty_ioctl()
2771 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2775 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2780 retval = ld->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2812 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2827 return tty_set_serial(tty, &v); in compat_tty_tiocsserial()
2830 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2840 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2842 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2857 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2940 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2945 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2947 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2949 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2950 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2955 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2959 retval = ld->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2961 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd), in tty_compat_ioctl()
2995 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3002 spin_lock_irqsave(&tty->ctrl.lock, flags); in __do_SAK()
3003 session = get_pid(tty->ctrl.session); in __do_SAK()
3004 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __do_SAK()
3006 tty_ldisc_flush(tty); in __do_SAK()
3008 tty_driver_flush_buffer(tty); in __do_SAK()
3013 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
3020 if (p->signal->tty == tty) { in __do_SAK()
3021 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
3028 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3030 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
3043 struct tty_struct *tty = in do_SAK_work() local
3045 __do_SAK(tty); in do_SAK_work()
3054 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3056 if (!tty) in do_SAK()
3058 schedule_work(&tty->SAK_work); in do_SAK()
3063 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3065 dev_t devt = tty_devnum(tty); in tty_get_device()
3082 struct tty_struct *tty; in alloc_tty_struct() local
3084 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT); in alloc_tty_struct()
3085 if (!tty) in alloc_tty_struct()
3088 kref_init(&tty->kref); in alloc_tty_struct()
3089 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
3090 kfree(tty); in alloc_tty_struct()
3093 tty->ctrl.session = NULL; in alloc_tty_struct()
3094 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3095 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3096 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3097 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3098 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3099 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3100 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3101 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3102 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3103 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3104 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3105 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3106 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3107 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3108 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3110 tty->driver = driver; in alloc_tty_struct()
3111 tty->ops = driver->ops; in alloc_tty_struct()
3112 tty->index = idx; in alloc_tty_struct()
3113 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3114 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3116 return tty; in alloc_tty_struct()
3132 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3134 if (tty->ops->put_char) in tty_put_char()
3135 return tty->ops->put_char(tty, ch); in tty_put_char()
3136 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3486 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3488 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()