Lines Matching refs:tty
116 # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args) argument
118 # define tty_debug_hangup(tty, f, args...) do { } while (0) argument
159 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 kvfree(tty->write_buf); in free_tty_struct()
174 kfree(tty); in free_tty_struct()
179 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
196 void tty_add_file(struct tty_struct *tty, struct file *file) in tty_add_file() argument
200 priv->tty = tty; in tty_add_file()
203 spin_lock(&tty->files_lock); in tty_add_file()
204 list_add(&priv->list, &tty->tty_files); in tty_add_file()
205 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()
244 const char *tty_name(const struct tty_struct *tty) in tty_name() argument
246 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
248 return tty->name; in tty_name()
252 const char *tty_driver_name(const struct tty_struct *tty) in tty_driver_name() argument
254 if (!tty || !tty->driver) in tty_driver_name()
256 return tty->driver->name; in tty_driver_name()
259 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
263 if (!tty) { in tty_paranoia_check()
273 static void check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
279 spin_lock(&tty->files_lock); in check_tty_count()
280 list_for_each(p, &tty->tty_files) { in check_tty_count()
283 spin_unlock(&tty->files_lock); in check_tty_count()
284 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
285 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
286 tty->link && tty->link->count) in check_tty_count()
288 if (tty_port_kopened(tty->port)) in check_tty_count()
290 if (tty->count != (count + kopen_count)) { in check_tty_count()
291 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
292 routine, tty->count, count, kopen_count); in check_tty_count()
458 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
460 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
461 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
515 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
519 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
520 ld = tty_ldisc_ref(tty); in tty_wakeup()
523 ld->ops->write_wakeup(tty); in tty_wakeup()
527 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
538 static struct file *tty_release_redirect(struct tty_struct *tty) in tty_release_redirect() argument
543 if (redirect && file_tty(redirect) == tty) { in tty_release_redirect()
577 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
585 if (!tty) in __tty_hangup()
588 f = tty_release_redirect(tty); in __tty_hangup()
590 tty_lock(tty); in __tty_hangup()
592 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
593 tty_unlock(tty); in __tty_hangup()
603 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
609 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
611 spin_lock(&tty->files_lock); in __tty_hangup()
613 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
623 spin_unlock(&tty->files_lock); in __tty_hangup()
625 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
628 tty_kref_put(tty); in __tty_hangup()
630 tty_ldisc_hangup(tty, cons_filp != NULL); in __tty_hangup()
632 spin_lock_irq(&tty->ctrl.lock); in __tty_hangup()
633 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
634 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
635 put_pid(tty->ctrl.session); in __tty_hangup()
636 put_pid(tty->ctrl.pgrp); in __tty_hangup()
637 tty->ctrl.session = NULL; in __tty_hangup()
638 tty->ctrl.pgrp = NULL; in __tty_hangup()
639 tty->ctrl.pktstatus = 0; in __tty_hangup()
640 spin_unlock_irq(&tty->ctrl.lock); in __tty_hangup()
649 if (tty->ops->close) in __tty_hangup()
651 tty->ops->close(tty, cons_filp); in __tty_hangup()
652 } else if (tty->ops->hangup) in __tty_hangup()
653 tty->ops->hangup(tty); in __tty_hangup()
659 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
660 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
661 tty_unlock(tty); in __tty_hangup()
669 struct tty_struct *tty = in do_tty_hangup() local
672 __tty_hangup(tty, 0); in do_tty_hangup()
682 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
684 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
685 schedule_work(&tty->hangup_work); in tty_hangup()
697 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
699 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
700 __tty_hangup(tty, 0); in tty_vhangup()
712 struct tty_struct *tty; in tty_vhangup_self() local
714 tty = get_current_tty(); in tty_vhangup_self()
715 if (tty) { in tty_vhangup_self()
716 tty_vhangup(tty); in tty_vhangup_self()
717 tty_kref_put(tty); in tty_vhangup_self()
731 void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
733 tty_debug_hangup(tty, "session hangup\n"); in tty_vhangup_session()
734 __tty_hangup(tty, 1); in tty_vhangup_session()
749 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
751 if (tty->flow.stopped) in __stop_tty()
753 tty->flow.stopped = true; in __stop_tty()
754 if (tty->ops->stop) in __stop_tty()
755 tty->ops->stop(tty); in __stop_tty()
772 void stop_tty(struct tty_struct *tty) in stop_tty() argument
776 spin_lock_irqsave(&tty->flow.lock, flags); in stop_tty()
777 __stop_tty(tty); in stop_tty()
778 spin_unlock_irqrestore(&tty->flow.lock, flags); in stop_tty()
782 void __start_tty(struct tty_struct *tty) in __start_tty() argument
784 if (!tty->flow.stopped || tty->flow.tco_stopped) in __start_tty()
786 tty->flow.stopped = false; in __start_tty()
787 if (tty->ops->start) in __start_tty()
788 tty->ops->start(tty); in __start_tty()
789 tty_wakeup(tty); in __start_tty()
803 void start_tty(struct tty_struct *tty) in start_tty() argument
807 spin_lock_irqsave(&tty->flow.lock, flags); in start_tty()
808 __start_tty(tty); in start_tty()
809 spin_unlock_irqrestore(&tty->flow.lock, flags); in start_tty()
813 static void tty_update_time(struct tty_struct *tty, bool mtime) in tty_update_time() argument
818 spin_lock(&tty->files_lock); in tty_update_time()
819 list_for_each_entry(priv, &tty->tty_files, list) { in tty_update_time()
832 spin_unlock(&tty->files_lock); in tty_update_time()
846 static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_read() argument
858 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
916 struct tty_struct *tty = file_tty(file); in tty_read() local
920 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
922 if (!tty || tty_io_error(tty)) in tty_read()
928 ld = tty_ldisc_ref_wait(tty); in tty_read()
933 ret = iterate_tty_read(ld, tty, file, to); in tty_read()
937 tty_update_time(tty, false); in tty_read()
942 void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
944 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
945 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
948 int tty_write_lock(struct tty_struct *tty, bool ndelay) in tty_write_lock() argument
950 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
953 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
963 static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_write() argument
969 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in iterate_tty_write()
987 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in iterate_tty_write()
993 if (tty->write_cnt < chunk) { in iterate_tty_write()
1004 kvfree(tty->write_buf); in iterate_tty_write()
1005 tty->write_cnt = chunk; in iterate_tty_write()
1006 tty->write_buf = buf_chunk; in iterate_tty_write()
1014 if (copy_from_iter(tty->write_buf, size, from) != size) in iterate_tty_write()
1017 ret = ld->ops->write(tty, file, tty->write_buf, size); in iterate_tty_write()
1038 tty_update_time(tty, true); in iterate_tty_write()
1042 tty_write_unlock(tty); in iterate_tty_write()
1057 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1059 if (tty) { in tty_write_message()
1060 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1061 tty_lock(tty); in tty_write_message()
1062 if (tty->ops->write && tty->count > 0) in tty_write_message()
1063 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1064 tty_unlock(tty); in tty_write_message()
1065 tty_write_unlock(tty); in tty_write_message()
1071 struct tty_struct *tty = file_tty(file); in file_tty_write() local
1075 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in file_tty_write()
1077 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1080 if (tty->ops->write_room == NULL) in file_tty_write()
1081 tty_err(tty, "missing write_room method\n"); in file_tty_write()
1082 ld = tty_ldisc_ref_wait(tty); in file_tty_write()
1088 ret = iterate_tty_write(ld, tty, file, from); in file_tty_write()
1144 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1146 bool was_stopped = tty->flow.stopped; in tty_send_xchar()
1148 if (tty->ops->send_xchar) { in tty_send_xchar()
1149 down_read(&tty->termios_rwsem); in tty_send_xchar()
1150 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1151 up_read(&tty->termios_rwsem); in tty_send_xchar()
1155 if (tty_write_lock(tty, false) < 0) in tty_send_xchar()
1158 down_read(&tty->termios_rwsem); in tty_send_xchar()
1160 start_tty(tty); in tty_send_xchar()
1161 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1163 stop_tty(tty); in tty_send_xchar()
1164 up_read(&tty->termios_rwsem); in tty_send_xchar()
1165 tty_write_unlock(tty); in tty_send_xchar()
1224 struct tty_struct *tty; in tty_driver_lookup_tty() local
1228 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1230 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1234 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1236 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1237 tty_kref_get(tty); in tty_driver_lookup_tty()
1238 return tty; in tty_driver_lookup_tty()
1248 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1251 int idx = tty->index; in tty_init_termios()
1253 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1254 tty->termios = tty->driver->init_termios; in tty_init_termios()
1257 tp = tty->driver->termios[idx]; in tty_init_termios()
1259 tty->termios = *tp; in tty_init_termios()
1260 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1262 tty->termios = tty->driver->init_termios; in tty_init_termios()
1265 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1266 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1278 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1280 tty_init_termios(tty); in tty_standard_install()
1282 tty->count++; in tty_standard_install()
1283 driver->ttys[tty->index] = tty; in tty_standard_install()
1300 struct tty_struct *tty) in tty_driver_install_tty() argument
1302 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1303 tty_standard_install(driver, tty); in tty_driver_install_tty()
1316 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1319 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1321 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1333 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1335 struct tty_driver *driver = tty->driver; in tty_reopen()
1343 if (!tty->count) in tty_reopen()
1346 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1349 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1353 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1357 if (!tty->ldisc) in tty_reopen()
1358 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1359 tty_ldisc_unlock(tty); in tty_reopen()
1363 tty->count++; in tty_reopen()
1393 struct tty_struct *tty; in tty_init_dev() local
1407 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1408 if (!tty) { in tty_init_dev()
1413 tty_lock(tty); in tty_init_dev()
1414 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1418 if (!tty->port) in tty_init_dev()
1419 tty->port = driver->ports[idx]; in tty_init_dev()
1421 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1423 __func__, tty->driver->name)) { in tty_init_dev()
1428 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1431 tty->port->itty = tty; in tty_init_dev()
1438 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1441 tty_ldisc_unlock(tty); in tty_init_dev()
1443 return tty; in tty_init_dev()
1446 tty_unlock(tty); in tty_init_dev()
1447 free_tty_struct(tty); in tty_init_dev()
1454 tty_ldisc_unlock(tty); in tty_init_dev()
1455 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1458 tty_unlock(tty); in tty_init_dev()
1459 release_tty(tty, idx); in tty_init_dev()
1469 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1472 int idx = tty->index; in tty_save_termios()
1475 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1479 tp = tty->driver->termios[idx]; in tty_save_termios()
1484 tty->driver->termios[idx] = tp; in tty_save_termios()
1486 *tp = tty->termios; in tty_save_termios()
1496 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1498 flush_work(&tty->SAK_work); in tty_flush_works()
1499 flush_work(&tty->hangup_work); in tty_flush_works()
1500 if (tty->link) { in tty_flush_works()
1501 flush_work(&tty->link->SAK_work); in tty_flush_works()
1502 flush_work(&tty->link->hangup_work); in tty_flush_works()
1523 struct tty_struct *tty = in release_one_tty() local
1525 struct tty_driver *driver = tty->driver; in release_one_tty()
1528 if (tty->ops->cleanup) in release_one_tty()
1529 tty->ops->cleanup(tty); in release_one_tty()
1534 spin_lock(&tty->files_lock); in release_one_tty()
1535 list_del_init(&tty->tty_files); in release_one_tty()
1536 spin_unlock(&tty->files_lock); in release_one_tty()
1538 put_pid(tty->ctrl.pgrp); in release_one_tty()
1539 put_pid(tty->ctrl.session); in release_one_tty()
1540 free_tty_struct(tty); in release_one_tty()
1545 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1550 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1551 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1561 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1563 if (tty) in tty_kref_put()
1564 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1581 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1584 WARN_ON(tty->index != idx); in release_tty()
1586 if (tty->ops->shutdown) in release_tty()
1587 tty->ops->shutdown(tty); in release_tty()
1588 tty_save_termios(tty); in release_tty()
1589 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1590 if (tty->port) in release_tty()
1591 tty->port->itty = NULL; in release_tty()
1592 if (tty->link) in release_tty()
1593 tty->link->port->itty = NULL; in release_tty()
1594 if (tty->port) in release_tty()
1595 tty_buffer_cancel_work(tty->port); in release_tty()
1596 if (tty->link) in release_tty()
1597 tty_buffer_cancel_work(tty->link->port); in release_tty()
1599 tty_kref_put(tty->link); in release_tty()
1600 tty_kref_put(tty); in release_tty()
1611 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1614 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1615 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1620 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1623 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1624 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1625 idx, tty->driver->ttys[idx]); in tty_release_checks()
1628 if (tty->driver->other) { in tty_release_checks()
1629 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1631 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1632 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1633 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1636 if (o_tty->link != tty) { in tty_release_checks()
1637 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1653 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1658 tty_ldisc_release(tty); in tty_kclose()
1661 tty_flush_works(tty); in tty_kclose()
1663 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1669 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1670 release_tty(tty, tty->index); in tty_kclose()
1683 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1688 tty_ldisc_release(tty); in tty_release_struct()
1691 tty_flush_works(tty); in tty_release_struct()
1693 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1699 release_tty(tty, idx); in tty_release_struct()
1724 struct tty_struct *tty = file_tty(filp); in tty_release() local
1731 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1734 tty_lock(tty); in tty_release()
1735 check_tty_count(tty, __func__); in tty_release()
1739 idx = tty->index; in tty_release()
1740 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1741 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1742 o_tty = tty->link; in tty_release()
1744 if (tty_release_checks(tty, idx)) { in tty_release()
1745 tty_unlock(tty); in tty_release()
1749 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1751 if (tty->ops->close) in tty_release()
1752 tty->ops->close(tty, filp); in tty_release()
1773 if (tty->count <= 1) { in tty_release()
1774 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1775 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1778 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1779 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1798 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1809 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1813 if (--tty->count < 0) { in tty_release()
1814 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1815 tty->count = 0; in tty_release()
1836 if (!tty->count) { in tty_release()
1838 session_clear_tty(tty->ctrl.session); in tty_release()
1845 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1848 tty_unlock(tty); in tty_release()
1857 tty_debug_hangup(tty, "final close\n"); in tty_release()
1859 tty_release_struct(tty, idx); in tty_release()
1876 struct tty_struct *tty; in tty_open_current_tty() local
1882 tty = get_current_tty(); in tty_open_current_tty()
1883 if (!tty) in tty_open_current_tty()
1888 tty_lock(tty); in tty_open_current_tty()
1889 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1891 retval = tty_reopen(tty); in tty_open_current_tty()
1893 tty_unlock(tty); in tty_open_current_tty()
1894 tty = ERR_PTR(retval); in tty_open_current_tty()
1896 return tty; in tty_open_current_tty()
1953 struct tty_struct *tty; in tty_kopen() local
1965 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1966 if (IS_ERR(tty) || shared) in tty_kopen()
1969 if (tty) { in tty_kopen()
1971 tty_kref_put(tty); in tty_kopen()
1972 tty = ERR_PTR(-EBUSY); in tty_kopen()
1974 tty = tty_init_dev(driver, index); in tty_kopen()
1975 if (IS_ERR(tty)) in tty_kopen()
1977 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1982 return tty; in tty_kopen()
2039 struct tty_struct *tty; in tty_open_by_driver() local
2052 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
2053 if (IS_ERR(tty)) { in tty_open_by_driver()
2058 if (tty) { in tty_open_by_driver()
2059 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2060 tty_kref_put(tty); in tty_open_by_driver()
2062 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2066 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
2067 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
2071 tty = ERR_PTR(retval); in tty_open_by_driver()
2074 retval = tty_reopen(tty); in tty_open_by_driver()
2076 tty_unlock(tty); in tty_open_by_driver()
2077 tty = ERR_PTR(retval); in tty_open_by_driver()
2080 tty = tty_init_dev(driver, index); in tty_open_by_driver()
2085 return tty; in tty_open_by_driver()
2113 struct tty_struct *tty; in tty_open() local
2125 tty = tty_open_current_tty(device, filp); in tty_open()
2126 if (!tty) in tty_open()
2127 tty = tty_open_by_driver(device, filp); in tty_open()
2129 if (IS_ERR(tty)) { in tty_open()
2131 retval = PTR_ERR(tty); in tty_open()
2138 tty_add_file(tty, filp); in tty_open()
2140 check_tty_count(tty, __func__); in tty_open()
2141 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2143 if (tty->ops->open) in tty_open()
2144 retval = tty->ops->open(tty, filp); in tty_open()
2150 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2152 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2168 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2173 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2174 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2176 tty_open_proc_set_tty(filp, tty); in tty_open()
2177 tty_unlock(tty); in tty_open()
2195 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2199 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2202 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2206 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2213 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2217 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2220 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2228 spin_lock_irqsave(&tty->ctrl.lock, flags); in __tty_fasync()
2229 if (tty->ctrl.pgrp) { in __tty_fasync()
2230 pid = tty->ctrl.pgrp; in __tty_fasync()
2237 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __tty_fasync()
2248 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2251 tty_lock(tty); in tty_fasync()
2254 tty_unlock(tty); in tty_fasync()
2273 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2281 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2285 tty_audit_tiocsti(tty, ch); in tiocsti()
2286 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2289 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2291 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2292 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2307 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2311 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2312 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2313 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2326 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2331 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2332 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2336 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2341 tty->winsize = *ws; in tty_do_resize()
2343 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2362 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2369 if (tty->ops->resize) in tiocswinsz()
2370 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2372 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2423 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2431 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2446 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2451 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2470 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2474 if (tty->ops->break_ctl == NULL) in send_break()
2477 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2478 retval = tty->ops->break_ctl(tty, duration); in send_break()
2481 if (tty_write_lock(tty, false) < 0) in send_break()
2483 retval = tty->ops->break_ctl(tty, -1); in send_break()
2488 retval = tty->ops->break_ctl(tty, 0); in send_break()
2490 tty_write_unlock(tty); in send_break()
2507 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2511 if (tty->ops->tiocmget) { in tty_tiocmget()
2512 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2531 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2537 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2558 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2570 int tty_get_icount(struct tty_struct *tty, in tty_get_icount() argument
2575 if (tty->ops->get_icount) in tty_get_icount()
2576 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2582 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2587 retval = tty_get_icount(tty, &icount); in tty_tiocgicount()
2596 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2607 if (!tty->ops->set_serial) in tty_set_serial()
2610 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2613 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2620 return tty_set_serial(tty, &v); in tty_tiocsserial()
2623 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2629 if (!tty->ops->get_serial) in tty_tiocgserial()
2631 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2641 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2643 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2644 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2645 tty = tty->link; in tty_pair_get_tty()
2646 return tty; in tty_pair_get_tty()
2654 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2660 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2663 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2674 retval = tty_check_change(tty); in tty_ioctl()
2678 tty_wait_until_sent(tty, 0); in tty_ioctl()
2690 return tiocsti(tty, p); in tty_ioctl()
2696 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2698 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2701 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2705 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2710 return tiocgetd(tty, p); in tty_ioctl()
2712 return tiocsetd(tty, p); in tty_ioctl()
2716 tty_vhangup(tty); in tty_ioctl()
2728 if (tty->ops->break_ctl) in tty_ioctl()
2729 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2732 if (tty->ops->break_ctl) in tty_ioctl()
2733 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2741 return send_break(tty, 250); in tty_ioctl()
2744 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2747 return tty_tiocmget(tty, p); in tty_ioctl()
2751 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2753 return tty_tiocgicount(tty, p); in tty_ioctl()
2759 tty_buffer_flush(tty, NULL); in tty_ioctl()
2764 return tty_tiocsserial(tty, p); in tty_ioctl()
2766 return tty_tiocgserial(tty, p); in tty_ioctl()
2769 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2771 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2775 if (tty->ops->ioctl) { in tty_ioctl()
2776 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2780 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2785 retval = ld->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2817 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2832 return tty_set_serial(tty, &v); in compat_tty_tiocsserial()
2835 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2845 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2847 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2862 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2945 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2950 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2952 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2954 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2955 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2960 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2964 retval = ld->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2966 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd), in tty_compat_ioctl()
3000 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3007 spin_lock_irqsave(&tty->ctrl.lock, flags); in __do_SAK()
3008 session = get_pid(tty->ctrl.session); in __do_SAK()
3009 spin_unlock_irqrestore(&tty->ctrl.lock, flags); in __do_SAK()
3011 tty_ldisc_flush(tty); in __do_SAK()
3013 tty_driver_flush_buffer(tty); in __do_SAK()
3018 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
3025 if (p->signal->tty == tty) { in __do_SAK()
3026 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
3033 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3035 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
3048 struct tty_struct *tty = in do_SAK_work() local
3050 __do_SAK(tty); in do_SAK_work()
3059 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3061 if (!tty) in do_SAK()
3063 schedule_work(&tty->SAK_work); in do_SAK()
3068 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3070 dev_t devt = tty_devnum(tty); in tty_get_device()
3087 struct tty_struct *tty; in alloc_tty_struct() local
3089 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT); in alloc_tty_struct()
3090 if (!tty) in alloc_tty_struct()
3093 kref_init(&tty->kref); in alloc_tty_struct()
3094 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
3095 kfree(tty); in alloc_tty_struct()
3098 tty->ctrl.session = NULL; in alloc_tty_struct()
3099 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3100 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3101 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3102 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3103 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3104 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3105 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3106 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3107 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3108 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3109 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3110 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3111 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3112 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3113 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3115 tty->driver = driver; in alloc_tty_struct()
3116 tty->ops = driver->ops; in alloc_tty_struct()
3117 tty->index = idx; in alloc_tty_struct()
3118 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3119 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3121 return tty; in alloc_tty_struct()
3137 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3139 if (tty->ops->put_char) in tty_put_char()
3140 return tty->ops->put_char(tty, ch); in tty_put_char()
3141 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3488 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3490 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()