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 scoped_guard(spinlock, &tty->files_lock) in check_tty_count()
280 list_for_each(p, &tty->tty_files) 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()
453 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
455 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
456 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
507 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
511 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
512 ld = tty_ldisc_ref(tty); in tty_wakeup()
515 ld->ops->write_wakeup(tty); in tty_wakeup()
519 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
530 static struct file *tty_release_redirect(struct tty_struct *tty) in tty_release_redirect() argument
534 if (redirect && file_tty(redirect) == tty) { in tty_release_redirect()
568 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
576 if (!tty) in __tty_hangup()
579 f = tty_release_redirect(tty); in __tty_hangup()
581 tty_lock(tty); in __tty_hangup()
583 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
584 tty_unlock(tty); in __tty_hangup()
594 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
600 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
602 spin_lock(&tty->files_lock); in __tty_hangup()
604 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
614 spin_unlock(&tty->files_lock); in __tty_hangup()
616 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
619 tty_kref_put(tty); in __tty_hangup()
621 tty_ldisc_hangup(tty, cons_filp != NULL); in __tty_hangup()
623 spin_lock_irq(&tty->ctrl.lock); in __tty_hangup()
624 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
625 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
626 put_pid(tty->ctrl.session); in __tty_hangup()
627 put_pid(tty->ctrl.pgrp); in __tty_hangup()
628 tty->ctrl.session = NULL; in __tty_hangup()
629 tty->ctrl.pgrp = NULL; in __tty_hangup()
630 tty->ctrl.pktstatus = 0; in __tty_hangup()
631 spin_unlock_irq(&tty->ctrl.lock); in __tty_hangup()
640 if (tty->ops->close) in __tty_hangup()
642 tty->ops->close(tty, cons_filp); in __tty_hangup()
643 } else if (tty->ops->hangup) in __tty_hangup()
644 tty->ops->hangup(tty); in __tty_hangup()
650 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
651 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
652 tty_unlock(tty); in __tty_hangup()
660 struct tty_struct *tty = in do_tty_hangup() local
663 __tty_hangup(tty, 0); in do_tty_hangup()
673 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
675 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
676 schedule_work(&tty->hangup_work); in tty_hangup()
688 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
690 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
691 __tty_hangup(tty, 0); in tty_vhangup()
703 struct tty_struct *tty; in tty_vhangup_self() local
705 tty = get_current_tty(); in tty_vhangup_self()
706 if (tty) { in tty_vhangup_self()
707 tty_vhangup(tty); in tty_vhangup_self()
708 tty_kref_put(tty); in tty_vhangup_self()
722 void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
724 tty_debug_hangup(tty, "session hangup\n"); in tty_vhangup_session()
725 __tty_hangup(tty, 1); in tty_vhangup_session()
740 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
742 if (tty->flow.stopped) in __stop_tty()
744 tty->flow.stopped = true; in __stop_tty()
745 if (tty->ops->stop) in __stop_tty()
746 tty->ops->stop(tty); in __stop_tty()
763 void stop_tty(struct tty_struct *tty) in stop_tty() argument
765 guard(spinlock_irqsave)(&tty->flow.lock); in stop_tty()
766 __stop_tty(tty); in stop_tty()
770 void __start_tty(struct tty_struct *tty) in __start_tty() argument
772 if (!tty->flow.stopped || tty->flow.tco_stopped) in __start_tty()
774 tty->flow.stopped = false; in __start_tty()
775 if (tty->ops->start) in __start_tty()
776 tty->ops->start(tty); in __start_tty()
777 tty_wakeup(tty); in __start_tty()
791 void start_tty(struct tty_struct *tty) in start_tty() argument
793 guard(spinlock_irqsave)(&tty->flow.lock); in start_tty()
794 __start_tty(tty); in start_tty()
798 static void tty_update_time(struct tty_struct *tty, bool mtime) in tty_update_time() argument
803 guard(spinlock)(&tty->files_lock); in tty_update_time()
805 list_for_each_entry(priv, &tty->tty_files, list) { in tty_update_time()
835 static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_read() argument
847 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset); in iterate_tty_read()
905 struct tty_struct *tty = file_tty(file); in tty_read() local
909 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
911 if (!tty || tty_io_error(tty)) in tty_read()
917 ld = tty_ldisc_ref_wait(tty); in tty_read()
922 ret = iterate_tty_read(ld, tty, file, to); in tty_read()
926 tty_update_time(tty, false); in tty_read()
931 void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
933 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
934 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
937 int tty_write_lock(struct tty_struct *tty, bool ndelay) in tty_write_lock() argument
939 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
942 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
952 static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty, in iterate_tty_write() argument
958 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in iterate_tty_write()
976 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in iterate_tty_write()
982 if (tty->write_cnt < chunk) { in iterate_tty_write()
993 kvfree(tty->write_buf); in iterate_tty_write()
994 tty->write_cnt = chunk; in iterate_tty_write()
995 tty->write_buf = buf_chunk; in iterate_tty_write()
1003 if (copy_from_iter(tty->write_buf, size, from) != size) in iterate_tty_write()
1006 ret = ld->ops->write(tty, file, tty->write_buf, size); in iterate_tty_write()
1027 tty_update_time(tty, true); in iterate_tty_write()
1031 tty_write_unlock(tty); in iterate_tty_write()
1049 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
1051 if (tty) { in tty_write_message()
1052 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
1053 tty_lock(tty); in tty_write_message()
1054 if (tty->ops->write && tty->count > 0) in tty_write_message()
1055 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1056 tty_unlock(tty); in tty_write_message()
1057 tty_write_unlock(tty); in tty_write_message()
1064 struct tty_struct *tty = file_tty(file); in file_tty_write() local
1068 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in file_tty_write()
1070 if (!tty || !tty->ops->write || tty_io_error(tty)) in file_tty_write()
1073 if (tty->ops->write_room == NULL) in file_tty_write()
1074 tty_err(tty, "missing write_room method\n"); in file_tty_write()
1075 ld = tty_ldisc_ref_wait(tty); in file_tty_write()
1081 ret = iterate_tty_write(ld, tty, file, from); in file_tty_write()
1137 int tty_send_xchar(struct tty_struct *tty, u8 ch) in tty_send_xchar() argument
1139 bool was_stopped = tty->flow.stopped; in tty_send_xchar()
1141 if (tty->ops->send_xchar) { in tty_send_xchar()
1142 down_read(&tty->termios_rwsem); in tty_send_xchar()
1143 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1144 up_read(&tty->termios_rwsem); in tty_send_xchar()
1148 if (tty_write_lock(tty, false) < 0) in tty_send_xchar()
1151 down_read(&tty->termios_rwsem); in tty_send_xchar()
1153 start_tty(tty); in tty_send_xchar()
1154 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1156 stop_tty(tty); in tty_send_xchar()
1157 up_read(&tty->termios_rwsem); in tty_send_xchar()
1158 tty_write_unlock(tty); in tty_send_xchar()
1217 struct tty_struct *tty; in tty_driver_lookup_tty() local
1221 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1223 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1227 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1229 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1230 tty_kref_get(tty); in tty_driver_lookup_tty()
1231 return tty; in tty_driver_lookup_tty()
1241 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1244 int idx = tty->index; in tty_init_termios()
1246 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1247 tty->termios = tty->driver->init_termios; in tty_init_termios()
1250 tp = tty->driver->termios[idx]; in tty_init_termios()
1252 tty->termios = *tp; in tty_init_termios()
1253 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1255 tty->termios = tty->driver->init_termios; in tty_init_termios()
1258 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1259 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1271 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1273 tty_init_termios(tty); in tty_standard_install()
1275 tty->count++; in tty_standard_install()
1276 driver->ttys[tty->index] = tty; in tty_standard_install()
1293 struct tty_struct *tty) in tty_driver_install_tty() argument
1295 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1296 tty_standard_install(driver, tty); in tty_driver_install_tty()
1309 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1312 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1314 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1326 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1328 struct tty_driver *driver = tty->driver; in tty_reopen()
1336 if (!tty->count) in tty_reopen()
1339 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1342 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1346 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1350 if (!tty->ldisc) in tty_reopen()
1351 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1352 tty_ldisc_unlock(tty); in tty_reopen()
1356 tty->count++; in tty_reopen()
1386 struct tty_struct *tty; in tty_init_dev() local
1400 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1401 if (!tty) { in tty_init_dev()
1406 tty_lock(tty); in tty_init_dev()
1407 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1411 if (!tty->port) in tty_init_dev()
1412 tty->port = driver->ports[idx]; in tty_init_dev()
1414 if (WARN_RATELIMIT(!tty->port, in tty_init_dev()
1416 __func__, tty->driver->name)) { in tty_init_dev()
1421 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1424 tty->port->itty = tty; in tty_init_dev()
1431 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1434 tty_ldisc_unlock(tty); in tty_init_dev()
1436 return tty; in tty_init_dev()
1439 tty_unlock(tty); in tty_init_dev()
1440 free_tty_struct(tty); in tty_init_dev()
1447 tty_ldisc_unlock(tty); in tty_init_dev()
1448 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1451 tty_unlock(tty); in tty_init_dev()
1452 release_tty(tty, idx); in tty_init_dev()
1462 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1465 int idx = tty->index; in tty_save_termios()
1468 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1472 tp = tty->driver->termios[idx]; in tty_save_termios()
1477 tty->driver->termios[idx] = tp; in tty_save_termios()
1479 *tp = tty->termios; in tty_save_termios()
1489 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1491 flush_work(&tty->SAK_work); in tty_flush_works()
1492 flush_work(&tty->hangup_work); in tty_flush_works()
1493 if (tty->link) { in tty_flush_works()
1494 flush_work(&tty->link->SAK_work); in tty_flush_works()
1495 flush_work(&tty->link->hangup_work); in tty_flush_works()
1516 struct tty_struct *tty = in release_one_tty() local
1518 struct tty_driver *driver = tty->driver; in release_one_tty()
1521 if (tty->ops->cleanup) in release_one_tty()
1522 tty->ops->cleanup(tty); in release_one_tty()
1527 spin_lock(&tty->files_lock); in release_one_tty()
1528 list_del_init(&tty->tty_files); in release_one_tty()
1529 spin_unlock(&tty->files_lock); in release_one_tty()
1531 put_pid(tty->ctrl.pgrp); in release_one_tty()
1532 put_pid(tty->ctrl.session); in release_one_tty()
1533 free_tty_struct(tty); in release_one_tty()
1538 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1543 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1544 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1554 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1556 if (tty) in tty_kref_put()
1557 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1574 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1577 WARN_ON(tty->index != idx); in release_tty()
1579 if (tty->ops->shutdown) in release_tty()
1580 tty->ops->shutdown(tty); in release_tty()
1581 tty_save_termios(tty); in release_tty()
1582 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1583 if (tty->port) in release_tty()
1584 tty->port->itty = NULL; in release_tty()
1585 if (tty->link) in release_tty()
1586 tty->link->port->itty = NULL; in release_tty()
1587 if (tty->port) in release_tty()
1588 tty_buffer_cancel_work(tty->port); in release_tty()
1589 if (tty->link) in release_tty()
1590 tty_buffer_cancel_work(tty->link->port); in release_tty()
1592 tty_kref_put(tty->link); in release_tty()
1593 tty_kref_put(tty); in release_tty()
1604 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1607 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1608 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1613 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1616 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1617 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1618 idx, tty->driver->ttys[idx]); in tty_release_checks()
1621 if (tty->driver->other) { in tty_release_checks()
1622 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1624 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1625 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1626 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1629 if (o_tty->link != tty) { in tty_release_checks()
1630 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1646 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1651 tty_ldisc_release(tty); in tty_kclose()
1654 tty_flush_works(tty); in tty_kclose()
1656 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1662 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1663 release_tty(tty, tty->index); in tty_kclose()
1676 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1681 tty_ldisc_release(tty); in tty_release_struct()
1684 tty_flush_works(tty); in tty_release_struct()
1686 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1692 release_tty(tty, idx); in tty_release_struct()
1717 struct tty_struct *tty = file_tty(filp); in tty_release() local
1724 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1727 tty_lock(tty); in tty_release()
1728 check_tty_count(tty, __func__); in tty_release()
1732 idx = tty->index; in tty_release()
1733 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1734 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1735 o_tty = tty->link; in tty_release()
1737 if (tty_release_checks(tty, idx)) { in tty_release()
1738 tty_unlock(tty); in tty_release()
1742 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1744 if (tty->ops->close) in tty_release()
1745 tty->ops->close(tty, filp); in tty_release()
1766 if (tty->count <= 1) { in tty_release()
1767 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1768 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1771 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1772 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1791 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1802 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1806 if (--tty->count < 0) { in tty_release()
1807 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1808 tty->count = 0; in tty_release()
1829 if (!tty->count) { in tty_release()
1831 session_clear_tty(tty->ctrl.session); in tty_release()
1838 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1841 tty_unlock(tty); in tty_release()
1850 tty_debug_hangup(tty, "final close\n"); in tty_release()
1852 tty_release_struct(tty, idx); in tty_release()
1869 struct tty_struct *tty; in tty_open_current_tty() local
1875 tty = get_current_tty(); in tty_open_current_tty()
1876 if (!tty) in tty_open_current_tty()
1881 tty_lock(tty); in tty_open_current_tty()
1882 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1884 retval = tty_reopen(tty); in tty_open_current_tty()
1886 tty_unlock(tty); in tty_open_current_tty()
1887 tty = ERR_PTR(retval); in tty_open_current_tty()
1889 return tty; in tty_open_current_tty()
1946 struct tty_struct *tty; in tty_kopen() local
1958 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1959 if (IS_ERR(tty) || shared) in tty_kopen()
1962 if (tty) { in tty_kopen()
1964 tty_kref_put(tty); in tty_kopen()
1965 tty = ERR_PTR(-EBUSY); in tty_kopen()
1967 tty = tty_init_dev(driver, index); in tty_kopen()
1968 if (IS_ERR(tty)) in tty_kopen()
1970 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1975 return tty; in tty_kopen()
2032 struct tty_struct *tty; in tty_open_by_driver() local
2045 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
2046 if (IS_ERR(tty)) { in tty_open_by_driver()
2051 if (tty) { in tty_open_by_driver()
2052 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
2053 tty_kref_put(tty); in tty_open_by_driver()
2055 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
2059 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
2060 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
2064 tty = ERR_PTR(retval); in tty_open_by_driver()
2067 retval = tty_reopen(tty); in tty_open_by_driver()
2069 tty_unlock(tty); in tty_open_by_driver()
2070 tty = ERR_PTR(retval); in tty_open_by_driver()
2073 tty = tty_init_dev(driver, index); in tty_open_by_driver()
2078 return tty; in tty_open_by_driver()
2106 struct tty_struct *tty; in tty_open() local
2118 tty = tty_open_current_tty(device, filp); in tty_open()
2119 if (!tty) in tty_open()
2120 tty = tty_open_by_driver(device, filp); in tty_open()
2122 if (IS_ERR(tty)) { in tty_open()
2124 retval = PTR_ERR(tty); in tty_open()
2131 tty_add_file(tty, filp); in tty_open()
2133 check_tty_count(tty, __func__); in tty_open()
2134 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2136 if (tty->ops->open) in tty_open()
2137 retval = tty->ops->open(tty, filp); in tty_open()
2143 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2145 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2161 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2166 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2167 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2169 tty_open_proc_set_tty(filp, tty); in tty_open()
2170 tty_unlock(tty); in tty_open()
2188 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2192 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2195 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2199 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2206 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2210 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()
2272 static int tiocsti(struct tty_struct *tty, u8 __user *p) in tiocsti() argument
2280 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2284 tty_audit_tiocsti(tty, ch); in tiocsti()
2285 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2288 tty_buffer_lock_exclusive(tty->port); in tiocsti()
2290 ld->ops->receive_buf(tty, &ch, NULL, 1); in tiocsti()
2291 tty_buffer_unlock_exclusive(tty->port); in tiocsti()
2306 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2308 guard(mutex)(&tty->winsize_mutex); in tiocgwinsz()
2310 if (copy_to_user(arg, &tty->winsize, sizeof(*arg))) in tiocgwinsz()
2324 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2328 guard(mutex)(&tty->winsize_mutex); in tty_do_resize()
2330 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2334 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2339 tty->winsize = *ws; in tty_do_resize()
2359 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2366 if (tty->ops->resize) in tiocswinsz()
2367 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2369 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2421 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2429 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2444 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2449 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2468 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2472 if (tty->ops->break_ctl == NULL) in send_break()
2475 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2476 return tty->ops->break_ctl(tty, duration); in send_break()
2479 if (tty_write_lock(tty, false) < 0) in send_break()
2482 retval = tty->ops->break_ctl(tty, -1); in send_break()
2485 retval = tty->ops->break_ctl(tty, 0); in send_break()
2490 tty_write_unlock(tty); in send_break()
2505 int tty_get_tiocm(struct tty_struct *tty) in tty_get_tiocm() argument
2509 if (tty->ops->tiocmget) in tty_get_tiocm()
2510 retval = tty->ops->tiocmget(tty); in tty_get_tiocm()
2526 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2530 retval = tty_get_tiocm(tty); in tty_tiocmget()
2548 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2554 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2575 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2587 int tty_get_icount(struct tty_struct *tty, in tty_get_icount() argument
2592 if (tty->ops->get_icount) in tty_get_icount()
2593 return tty->ops->get_icount(tty, icount); in tty_get_icount()
2599 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2604 retval = tty_get_icount(tty, &icount); in tty_tiocgicount()
2613 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss) in tty_set_serial() argument
2623 if (!tty->ops->set_serial) in tty_set_serial()
2626 return tty->ops->set_serial(tty, ss); in tty_set_serial()
2629 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2636 return tty_set_serial(tty, &v); in tty_tiocsserial()
2639 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2645 if (!tty->ops->get_serial) in tty_tiocgserial()
2647 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2657 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2659 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2660 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2661 tty = tty->link; in tty_pair_get_tty()
2662 return tty; in tty_pair_get_tty()
2670 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2676 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2679 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2690 retval = tty_check_change(tty); in tty_ioctl()
2694 tty_wait_until_sent(tty, 0); in tty_ioctl()
2706 return tiocsti(tty, p); in tty_ioctl()
2712 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2714 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2717 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2721 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2726 return tiocgetd(tty, p); in tty_ioctl()
2728 return tiocsetd(tty, p); in tty_ioctl()
2732 tty_vhangup(tty); in tty_ioctl()
2744 if (tty->ops->break_ctl) in tty_ioctl()
2745 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2748 if (tty->ops->break_ctl) in tty_ioctl()
2749 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2757 return send_break(tty, 250); in tty_ioctl()
2760 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2763 return tty_tiocmget(tty, p); in tty_ioctl()
2767 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2769 return tty_tiocgicount(tty, p); in tty_ioctl()
2775 tty_buffer_flush(tty, NULL); in tty_ioctl()
2780 return tty_tiocsserial(tty, p); in tty_ioctl()
2782 return tty_tiocgserial(tty, p); in tty_ioctl()
2785 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2787 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2791 if (tty->ops->ioctl) { in tty_ioctl()
2792 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2796 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2801 retval = ld->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2833 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2848 return tty_set_serial(tty, &v); in compat_tty_tiocsserial()
2851 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2861 if (!tty->ops->get_serial) in compat_tty_tiocgserial()
2863 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2878 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2961 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2966 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2968 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2970 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2971 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2976 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2980 retval = ld->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2982 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd), in tty_compat_ioctl()
3016 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
3022 scoped_guard(spinlock_irqsave, &tty->ctrl.lock) in __do_SAK()
3023 session = get_pid(tty->ctrl.session); in __do_SAK()
3025 tty_ldisc_flush(tty); in __do_SAK()
3027 tty_driver_flush_buffer(tty); in __do_SAK()
3032 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
3039 if (p->signal->tty == tty) { in __do_SAK()
3040 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
3047 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
3049 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
3061 struct tty_struct *tty = in do_SAK_work() local
3063 __do_SAK(tty); in do_SAK_work()
3072 void do_SAK(struct tty_struct *tty) in do_SAK() argument
3074 if (!tty) in do_SAK()
3076 schedule_work(&tty->SAK_work); in do_SAK()
3081 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
3083 dev_t devt = tty_devnum(tty); in tty_get_device()
3100 struct tty_struct *tty; in alloc_tty_struct() local
3102 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT); in alloc_tty_struct()
3103 if (!tty) in alloc_tty_struct()
3106 kref_init(&tty->kref); in alloc_tty_struct()
3107 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
3108 kfree(tty); in alloc_tty_struct()
3111 tty->ctrl.session = NULL; in alloc_tty_struct()
3112 tty->ctrl.pgrp = NULL; in alloc_tty_struct()
3113 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
3114 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
3115 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
3116 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
3117 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
3118 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
3119 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
3120 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
3121 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
3122 spin_lock_init(&tty->ctrl.lock); in alloc_tty_struct()
3123 spin_lock_init(&tty->flow.lock); in alloc_tty_struct()
3124 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
3125 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3126 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3128 tty->driver = driver; in alloc_tty_struct()
3129 tty->ops = driver->ops; in alloc_tty_struct()
3130 tty->index = idx; in alloc_tty_struct()
3131 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3132 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3134 return tty; in alloc_tty_struct()
3150 int tty_put_char(struct tty_struct *tty, u8 ch) in tty_put_char() argument
3152 if (tty->ops->put_char) in tty_put_char()
3153 return tty->ops->put_char(tty, ch); in tty_put_char()
3154 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3500 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3502 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()