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);
169 static void free_tty_struct(struct tty_struct *tty) in free_tty_struct() argument
171 tty_ldisc_deinit(tty); in free_tty_struct()
172 put_device(tty->dev); in free_tty_struct()
173 kfree(tty->write_buf); in free_tty_struct()
174 tty->magic = 0xDEADDEAD; in free_tty_struct()
175 kfree(tty); in free_tty_struct()
180 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
197 void tty_add_file(struct tty_struct *tty, struct file *file) in tty_add_file() argument
201 priv->tty = tty; in tty_add_file()
204 spin_lock(&tty->files_lock); in tty_add_file()
205 list_add(&priv->list, &tty->tty_files); in tty_add_file()
206 spin_unlock(&tty->files_lock); in tty_add_file()
227 struct tty_struct *tty = priv->tty; in tty_del_file() local
229 spin_lock(&tty->files_lock); in tty_del_file()
231 spin_unlock(&tty->files_lock); in tty_del_file()
245 const char *tty_name(const struct tty_struct *tty) in tty_name() argument
247 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
249 return tty->name; in tty_name()
253 const char *tty_driver_name(const struct tty_struct *tty) in tty_driver_name() argument
255 if (!tty || !tty->driver) in tty_driver_name()
257 return tty->driver->name; in tty_driver_name()
260 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
264 if (!tty) { in tty_paranoia_check()
269 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
279 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
285 spin_lock(&tty->files_lock); in check_tty_count()
286 list_for_each(p, &tty->tty_files) { in check_tty_count()
289 spin_unlock(&tty->files_lock); in check_tty_count()
290 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
291 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
292 tty->link && tty->link->count) in check_tty_count()
294 if (tty_port_kopened(tty->port)) in check_tty_count()
296 if (tty->count != (count + kopen_count)) { in check_tty_count()
297 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
298 routine, tty->count, count, kopen_count); in check_tty_count()
468 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
470 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
471 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
526 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
530 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
531 ld = tty_ldisc_ref(tty); in tty_wakeup()
534 ld->ops->write_wakeup(tty); in tty_wakeup()
538 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
549 static struct file *tty_release_redirect(struct tty_struct *tty) in tty_release_redirect() argument
554 if (redirect && file_tty(redirect) == tty) { in tty_release_redirect()
586 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
594 if (!tty) in __tty_hangup()
597 f = tty_release_redirect(tty); in __tty_hangup()
599 tty_lock(tty); in __tty_hangup()
601 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
602 tty_unlock(tty); in __tty_hangup()
612 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
618 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
620 spin_lock(&tty->files_lock); in __tty_hangup()
622 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
632 spin_unlock(&tty->files_lock); in __tty_hangup()
634 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
637 tty_kref_put(tty); in __tty_hangup()
639 tty_ldisc_hangup(tty, cons_filp != NULL); in __tty_hangup()
641 spin_lock_irq(&tty->ctrl.lock); in __tty_hangup()
642 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
643 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
644 put_pid(tty->ctrl.session); in __tty_hangup()
645 put_pid(tty->ctrl.pgrp); in __tty_hangup()
646 tty->ctrl.session = NULL; in __tty_hangup()
647 tty->ctrl.pgrp = NULL; in __tty_hangup()
648 tty->ctrl.pktstatus = 0; in __tty_hangup()
649 spin_unlock_irq(&tty->ctrl.lock); in __tty_hangup()
658 if (tty->ops->close) in __tty_hangup()
660 tty->ops->close(tty, cons_filp); in __tty_hangup()
661 } else if (tty->ops->hangup) in __tty_hangup()
662 tty->ops->hangup(tty); in __tty_hangup()
668 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
669 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
670 tty_unlock(tty); in __tty_hangup()
678 struct tty_struct *tty = in do_tty_hangup() local
681 __tty_hangup(tty, 0); in do_tty_hangup()
692 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
694 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
695 schedule_work(&tty->hangup_work); in tty_hangup()
708 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
710 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
711 __tty_hangup(tty, 0); in tty_vhangup()
724 struct tty_struct *tty; in tty_vhangup_self() local
726 tty = get_current_tty(); in tty_vhangup_self()
727 if (tty) { in tty_vhangup_self()
728 tty_vhangup(tty); in tty_vhangup_self()
729 tty_kref_put(tty); in tty_vhangup_self()
744 void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
746 tty_debug_hangup(tty, "session hangup\n"); in tty_vhangup_session()
747 __tty_hangup(tty, 1); in tty_vhangup_session()
764 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
766 if (tty->flow.stopped) in __stop_tty()
768 tty->flow.stopped = true; in __stop_tty()
769 if (tty->ops->stop) in __stop_tty()
770 tty->ops->stop(tty); in __stop_tty()
789 void stop_tty(struct tty_struct *tty) in stop_tty() argument
793 spin_lock_irqsave(&tty->flow.lock, flags); in stop_tty()
794 __stop_tty(tty); in stop_tty()
795 spin_unlock_irqrestore(&tty->flow.lock, flags); in stop_tty()
799 void __start_tty(struct tty_struct *tty) in __start_tty() argument
801 if (!tty->flow.stopped || tty->flow.tco_stopped) in __start_tty()
803 tty->flow.stopped = false; in __start_tty()
804 if (tty->ops->start) in __start_tty()
805 tty->ops->start(tty); in __start_tty()
806 tty_wakeup(tty); in __start_tty()
820 void start_tty(struct tty_struct *tty) in start_tty() argument
824 spin_lock_irqsave(&tty->flow.lock, flags); in start_tty()
825 __start_tty(tty); in start_tty()
826 spin_unlock_irqrestore(&tty->flow.lock, flags); in start_tty()
855 static int iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_read() argument
868 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
928 struct tty_struct *tty = file_tty(file); in tty_read() local
931 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
933 if (!tty || tty_io_error(tty)) in tty_read()
939 ld = tty_ldisc_ref_wait(tty); in tty_read()
944 i = iterate_tty_read(ld, tty, file, to); in tty_read()
953 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
955 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
956 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
959 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
961 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
964 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
976 struct tty_struct *tty, in do_tty_write() argument
984 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
1005 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
1011 if (tty->write_cnt < chunk) { in do_tty_write()
1022 kfree(tty->write_buf); in do_tty_write()
1023 tty->write_cnt = chunk; in do_tty_write()
1024 tty->write_buf = buf_chunk; in do_tty_write()
1035 if (copy_from_iter(tty->write_buf, size, from) != size) in do_tty_write()
1038 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
1063 tty_write_unlock(tty); in do_tty_write()
1079 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1081 if (tty) { in tty_write_message()
1082 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1083 tty_lock(tty); in tty_write_message()
1084 if (tty->ops->write && tty->count > 0) in tty_write_message()
1085 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1086 tty_unlock(tty); in tty_write_message()
1087 tty_write_unlock(tty); in tty_write_message()
1093 struct tty_struct *tty = file_tty(file); in file_tty_write() local
1097 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in file_tty_write()
1099 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1102 if (tty->ops->write_room == NULL) in file_tty_write()
1103 tty_err(tty, "missing write_room method\n"); in file_tty_write()
1104 ld = tty_ldisc_ref_wait(tty); in file_tty_write()
1110 ret = do_tty_write(ld->ops->write, tty, file, from); in file_tty_write()
1165 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1167 bool was_stopped = tty->flow.stopped; in tty_send_xchar()
1169 if (tty->ops->send_xchar) { in tty_send_xchar()
1170 down_read(&tty->termios_rwsem); in tty_send_xchar()
1171 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1172 up_read(&tty->termios_rwsem); in tty_send_xchar()
1176 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1179 down_read(&tty->termios_rwsem); in tty_send_xchar()
1181 start_tty(tty); in tty_send_xchar()
1182 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1184 stop_tty(tty); in tty_send_xchar()
1185 up_read(&tty->termios_rwsem); in tty_send_xchar()
1186 tty_write_unlock(tty); in tty_send_xchar()
1245 struct tty_struct *tty; in tty_driver_lookup_tty() local
1249 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1251 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1253 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1255 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1256 tty_kref_get(tty); in tty_driver_lookup_tty()
1257 return tty; in tty_driver_lookup_tty()
1268 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1271 int idx = tty->index; in tty_init_termios()
1273 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1274 tty->termios = tty->driver->init_termios; in tty_init_termios()
1277 tp = tty->driver->termios[idx]; in tty_init_termios()
1279 tty->termios = *tp; in tty_init_termios()
1280 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1282 tty->termios = tty->driver->init_termios; in tty_init_termios()
1285 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1286 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1290 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1292 tty_init_termios(tty); in tty_standard_install()
1294 tty->count++; in tty_standard_install()
1295 driver->ttys[tty->index] = tty; in tty_standard_install()
1313 struct tty_struct *tty) in tty_driver_install_tty() argument
1315 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1316 tty_standard_install(driver, tty); in tty_driver_install_tty()
1329 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1332 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1334 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1346 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1348 struct tty_driver *driver = tty->driver; in tty_reopen()
1356 if (!tty->count) in tty_reopen()
1359 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1362 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1366 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1370 if (!tty->ldisc) in tty_reopen()
1371 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1372 tty_ldisc_unlock(tty); in tty_reopen()
1376 tty->count++; in tty_reopen()
1408 struct tty_struct *tty; in tty_init_dev() local
1422 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1423 if (!tty) { in tty_init_dev()
1428 tty_lock(tty); in tty_init_dev()
1429 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1433 if (!tty->port) in tty_init_dev()
1434 tty->port = driver->ports[idx]; in tty_init_dev()
1436 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1438 __func__, tty->driver->name)) { in tty_init_dev()
1443 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1446 tty->port->itty = tty; in tty_init_dev()
1453 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1456 tty_ldisc_unlock(tty); in tty_init_dev()
1458 return tty; in tty_init_dev()
1461 tty_unlock(tty); in tty_init_dev()
1462 free_tty_struct(tty); in tty_init_dev()
1469 tty_ldisc_unlock(tty); in tty_init_dev()
1470 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1473 tty_unlock(tty); in tty_init_dev()
1474 release_tty(tty, idx); in tty_init_dev()
1484 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1487 int idx = tty->index; in tty_save_termios()
1490 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1494 tp = tty->driver->termios[idx]; in tty_save_termios()
1499 tty->driver->termios[idx] = tp; in tty_save_termios()
1501 *tp = tty->termios; in tty_save_termios()
1511 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1513 flush_work(&tty->SAK_work); in tty_flush_works()
1514 flush_work(&tty->hangup_work); in tty_flush_works()
1515 if (tty->link) { in tty_flush_works()
1516 flush_work(&tty->link->SAK_work); in tty_flush_works()
1517 flush_work(&tty->link->hangup_work); in tty_flush_works()
1538 struct tty_struct *tty = in release_one_tty() local
1540 struct tty_driver *driver = tty->driver; in release_one_tty()
1543 if (tty->ops->cleanup) in release_one_tty()
1544 tty->ops->cleanup(tty); in release_one_tty()
1546 tty->magic = 0; in release_one_tty()
1550 spin_lock(&tty->files_lock); in release_one_tty()
1551 list_del_init(&tty->tty_files); in release_one_tty()
1552 spin_unlock(&tty->files_lock); in release_one_tty()
1554 put_pid(tty->ctrl.pgrp); in release_one_tty()
1555 put_pid(tty->ctrl.session); in release_one_tty()
1556 free_tty_struct(tty); in release_one_tty()
1561 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1566 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1567 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1578 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1580 if (tty) in tty_kref_put()
1581 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1599 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1602 WARN_ON(tty->index != idx); in release_tty()
1604 if (tty->ops->shutdown) in release_tty()
1605 tty->ops->shutdown(tty); in release_tty()
1606 tty_save_termios(tty); in release_tty()
1607 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1608 if (tty->port) in release_tty()
1609 tty->port->itty = NULL; in release_tty()
1610 if (tty->link) in release_tty()
1611 tty->link->port->itty = NULL; in release_tty()
1612 if (tty->port) in release_tty()
1613 tty_buffer_cancel_work(tty->port); in release_tty()
1614 if (tty->link) in release_tty()
1615 tty_buffer_cancel_work(tty->link->port); in release_tty()
1617 tty_kref_put(tty->link); in release_tty()
1618 tty_kref_put(tty); in release_tty()
1629 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1632 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1633 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1638 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1641 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1642 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1643 idx, tty->driver->ttys[idx]); in tty_release_checks()
1646 if (tty->driver->other) { in tty_release_checks()
1647 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1649 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1650 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1651 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1654 if (o_tty->link != tty) { in tty_release_checks()
1655 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1671 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1676 tty_ldisc_release(tty); in tty_kclose()
1679 tty_flush_works(tty); in tty_kclose()
1681 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1687 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1688 release_tty(tty, tty->index); in tty_kclose()
1701 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1706 tty_ldisc_release(tty); in tty_release_struct()
1709 tty_flush_works(tty); in tty_release_struct()
1711 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1717 release_tty(tty, idx); in tty_release_struct()
1743 struct tty_struct *tty = file_tty(filp); in tty_release() local
1750 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1753 tty_lock(tty); in tty_release()
1754 check_tty_count(tty, __func__); in tty_release()
1758 idx = tty->index; in tty_release()
1759 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1760 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1761 o_tty = tty->link; in tty_release()
1763 if (tty_release_checks(tty, idx)) { in tty_release()
1764 tty_unlock(tty); in tty_release()
1768 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1770 if (tty->ops->close) in tty_release()
1771 tty->ops->close(tty, filp); in tty_release()
1792 if (tty->count <= 1) { in tty_release()
1793 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1794 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1797 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1798 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1817 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1828 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1832 if (--tty->count < 0) { in tty_release()
1833 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1834 tty->count = 0; in tty_release()
1855 if (!tty->count) { in tty_release()
1857 session_clear_tty(tty->ctrl.session); in tty_release()
1864 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1867 tty_unlock(tty); in tty_release()
1876 tty_debug_hangup(tty, "final close\n"); in tty_release()
1878 tty_release_struct(tty, idx); in tty_release()
1895 struct tty_struct *tty; in tty_open_current_tty() local
1901 tty = get_current_tty(); in tty_open_current_tty()
1902 if (!tty) in tty_open_current_tty()
1907 tty_lock(tty); in tty_open_current_tty()
1908 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1910 retval = tty_reopen(tty); in tty_open_current_tty()
1912 tty_unlock(tty); in tty_open_current_tty()
1913 tty = ERR_PTR(retval); in tty_open_current_tty()
1915 return tty; in tty_open_current_tty()
1971 struct tty_struct *tty; in tty_kopen() local
1983 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1984 if (IS_ERR(tty) || shared) in tty_kopen()
1987 if (tty) { in tty_kopen()
1989 tty_kref_put(tty); in tty_kopen()
1990 tty = ERR_PTR(-EBUSY); in tty_kopen()
1992 tty = tty_init_dev(driver, index); in tty_kopen()
1993 if (IS_ERR(tty)) in tty_kopen()
1995 tty_port_set_kopened(tty->port, 1); in tty_kopen()
2000 return tty; in tty_kopen()
2057 struct tty_struct *tty; in tty_open_by_driver() local
2070 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
2071 if (IS_ERR(tty)) { in tty_open_by_driver()
2076 if (tty) { in tty_open_by_driver()
2077 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2078 tty_kref_put(tty); in tty_open_by_driver()
2080 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2084 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
2085 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
2089 tty = ERR_PTR(retval); in tty_open_by_driver()
2092 retval = tty_reopen(tty); in tty_open_by_driver()
2094 tty_unlock(tty); in tty_open_by_driver()
2095 tty = ERR_PTR(retval); in tty_open_by_driver()
2098 tty = tty_init_dev(driver, index); in tty_open_by_driver()
2103 return tty; in tty_open_by_driver()
2132 struct tty_struct *tty; in tty_open() local
2144 tty = tty_open_current_tty(device, filp); in tty_open()
2145 if (!tty) in tty_open()
2146 tty = tty_open_by_driver(device, filp); in tty_open()
2148 if (IS_ERR(tty)) { in tty_open()
2150 retval = PTR_ERR(tty); in tty_open()
2157 tty_add_file(tty, filp); in tty_open()
2159 check_tty_count(tty, __func__); in tty_open()
2160 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2162 if (tty->ops->open) in tty_open()
2163 retval = tty->ops->open(tty, filp); in tty_open()
2169 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2171 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2187 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2192 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2193 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2195 tty_open_proc_set_tty(filp, tty); in tty_open()
2196 tty_unlock(tty); in tty_open()
2216 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2220 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2223 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2227 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2234 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2238 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2241 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2249 spin_lock_irqsave(&tty->ctrl.lock, flags); in __tty_fasync()
2250 if (tty->ctrl.pgrp) { in __tty_fasync()
2251 pid = tty->ctrl.pgrp; in __tty_fasync()
2258 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __tty_fasync()
2269 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2272 tty_lock(tty); in tty_fasync()
2275 tty_unlock(tty); in tty_fasync()
2295 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2300 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2304 tty_audit_tiocsti(tty, ch); in tiocsti()
2305 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2308 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2310 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2311 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2327 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2331 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2332 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2333 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2347 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2352 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2353 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2357 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2362 tty->winsize = *ws; in tty_do_resize()
2364 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2384 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2391 if (tty->ops->resize) in tiocswinsz()
2392 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2394 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2447 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2455 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2471 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2476 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2497 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2501 if (tty->ops->break_ctl == NULL) in send_break()
2504 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2505 retval = tty->ops->break_ctl(tty, duration); in send_break()
2508 if (tty_write_lock(tty, 0) < 0) in send_break()
2510 retval = tty->ops->break_ctl(tty, -1); in send_break()
2515 retval = tty->ops->break_ctl(tty, 0); in send_break()
2517 tty_write_unlock(tty); in send_break()
2535 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2539 if (tty->ops->tiocmget) { in tty_tiocmget()
2540 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2560 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2566 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2587 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2599 int tty_get_icount(struct tty_struct *tty, in tty_get_icount() argument
2604 if (tty->ops->get_icount) in tty_get_icount()
2605 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2611 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2616 retval = tty_get_icount(tty, &icount); in tty_tiocgicount()
2625 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2636 if (!tty->ops->set_serial) in tty_set_serial()
2639 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2642 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2649 return tty_set_serial(tty, &v); in tty_tiocsserial()
2652 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2658 if (!tty->ops->get_serial) in tty_tiocgserial()
2660 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2670 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2672 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2673 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2674 tty = tty->link; in tty_pair_get_tty()
2675 return tty; in tty_pair_get_tty()
2683 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2689 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2692 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2703 retval = tty_check_change(tty); in tty_ioctl()
2707 tty_wait_until_sent(tty, 0); in tty_ioctl()
2719 return tiocsti(tty, p); in tty_ioctl()
2725 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2727 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2730 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2734 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2739 return tiocgetd(tty, p); in tty_ioctl()
2741 return tiocsetd(tty, p); in tty_ioctl()
2745 tty_vhangup(tty); in tty_ioctl()
2757 if (tty->ops->break_ctl) in tty_ioctl()
2758 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2761 if (tty->ops->break_ctl) in tty_ioctl()
2762 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2770 return send_break(tty, 250); in tty_ioctl()
2773 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2776 return tty_tiocmget(tty, p); in tty_ioctl()
2780 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2782 return tty_tiocgicount(tty, p); in tty_ioctl()
2788 tty_buffer_flush(tty, NULL); in tty_ioctl()
2793 return tty_tiocsserial(tty, p); in tty_ioctl()
2795 return tty_tiocgserial(tty, p); in tty_ioctl()
2798 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2800 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2804 if (tty->ops->ioctl) { in tty_ioctl()
2805 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2809 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2814 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
2846 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2861 return tty_set_serial(tty, &v); in compat_tty_tiocsserial()
2864 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2874 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2876 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2891 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2974 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2979 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2981 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2983 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2984 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2989 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2993 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
2995 retval = ld->ops->ioctl(tty, file, in tty_compat_ioctl()
3029 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3032 tty_hangup(tty); in __do_SAK()
3039 if (!tty) in __do_SAK()
3042 spin_lock_irqsave(&tty->ctrl.lock, flags); in __do_SAK()
3043 session = get_pid(tty->ctrl.session); in __do_SAK()
3044 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __do_SAK()
3046 tty_ldisc_flush(tty); in __do_SAK()
3048 tty_driver_flush_buffer(tty); in __do_SAK()
3053 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
3060 if (p->signal->tty == tty) { in __do_SAK()
3061 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
3067 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3069 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
3082 struct tty_struct *tty = in do_SAK_work() local
3084 __do_SAK(tty); in do_SAK_work()
3093 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3095 if (!tty) in do_SAK()
3097 schedule_work(&tty->SAK_work); in do_SAK()
3102 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3104 dev_t devt = tty_devnum(tty); in tty_get_device()
3120 struct tty_struct *tty; in alloc_tty_struct() local
3122 tty = kzalloc(sizeof(*tty), GFP_KERNEL); in alloc_tty_struct()
3123 if (!tty) in alloc_tty_struct()
3126 kref_init(&tty->kref); in alloc_tty_struct()
3127 tty->magic = TTY_MAGIC; in alloc_tty_struct()
3128 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
3129 kfree(tty); in alloc_tty_struct()
3132 tty->ctrl.session = NULL; in alloc_tty_struct()
3133 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3134 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3135 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3136 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3137 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3138 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3139 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3140 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3141 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3142 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3143 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3144 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3145 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3146 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3147 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3149 tty->driver = driver; in alloc_tty_struct()
3150 tty->ops = driver->ops; in alloc_tty_struct()
3151 tty->index = idx; in alloc_tty_struct()
3152 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3153 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3155 return tty; in alloc_tty_struct()
3170 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3172 if (tty->ops->put_char) in tty_put_char()
3173 return tty->ops->put_char(tty, ch); in tty_put_char()
3174 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3516 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3518 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()