Lines Matching refs:tty

112 # define tty_debug_hangup(tty, f, args...)	tty_debug(tty, f, ##args)  argument
114 # 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()
254 const char *tty_driver_name(const struct tty_struct *tty) in tty_driver_name() argument
256 if (!tty || !tty->driver) in tty_driver_name()
258 return tty->driver->name; in tty_driver_name()
261 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
265 if (!tty) { in tty_paranoia_check()
270 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
280 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
286 spin_lock(&tty->files_lock); in check_tty_count()
287 list_for_each(p, &tty->tty_files) { in check_tty_count()
290 spin_unlock(&tty->files_lock); in check_tty_count()
291 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
292 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
293 tty->link && tty->link->count) in check_tty_count()
295 if (tty_port_kopened(tty->port)) in check_tty_count()
297 if (tty->count != (count + kopen_count)) { in check_tty_count()
298 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
299 routine, tty->count, count, kopen_count); in check_tty_count()
470 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
472 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
473 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
524 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
528 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
529 ld = tty_ldisc_ref(tty); in tty_wakeup()
532 ld->ops->write_wakeup(tty); in tty_wakeup()
536 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
563 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
571 if (!tty) in __tty_hangup()
576 if (redirect && file_tty(redirect) == tty) { in __tty_hangup()
582 tty_lock(tty); in __tty_hangup()
584 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
585 tty_unlock(tty); in __tty_hangup()
595 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->session); in __tty_hangup()
627 put_pid(tty->pgrp); in __tty_hangup()
628 tty->session = NULL; in __tty_hangup()
629 tty->pgrp = NULL; in __tty_hangup()
630 tty->ctrl_status = 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()
674 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
676 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
677 schedule_work(&tty->hangup_work); in tty_hangup()
691 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
693 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
694 __tty_hangup(tty, 0); in tty_vhangup()
708 struct tty_struct *tty; in tty_vhangup_self() local
710 tty = get_current_tty(); in tty_vhangup_self()
711 if (tty) { in tty_vhangup_self()
712 tty_vhangup(tty); in tty_vhangup_self()
713 tty_kref_put(tty); in tty_vhangup_self()
728 void tty_vhangup_session(struct tty_struct *tty) in tty_vhangup_session() argument
730 tty_debug_hangup(tty, "session hangup\n"); in tty_vhangup_session()
731 __tty_hangup(tty, 1); in tty_vhangup_session()
766 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
768 if (tty->stopped) in __stop_tty()
770 tty->stopped = 1; in __stop_tty()
771 if (tty->ops->stop) in __stop_tty()
772 tty->ops->stop(tty); in __stop_tty()
775 void stop_tty(struct tty_struct *tty) in stop_tty() argument
779 spin_lock_irqsave(&tty->flow_lock, flags); in stop_tty()
780 __stop_tty(tty); in stop_tty()
781 spin_unlock_irqrestore(&tty->flow_lock, flags); in stop_tty()
797 void __start_tty(struct tty_struct *tty) in __start_tty() argument
799 if (!tty->stopped || tty->flow_stopped) in __start_tty()
801 tty->stopped = 0; in __start_tty()
802 if (tty->ops->start) in __start_tty()
803 tty->ops->start(tty); in __start_tty()
804 tty_wakeup(tty); in __start_tty()
807 void start_tty(struct tty_struct *tty) in start_tty() argument
811 spin_lock_irqsave(&tty->flow_lock, flags); in start_tty()
812 __start_tty(tty); in start_tty()
813 spin_unlock_irqrestore(&tty->flow_lock, flags); in start_tty()
851 struct tty_struct *tty = file_tty(file); in tty_read() local
854 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
856 if (!tty || tty_io_error(tty)) in tty_read()
861 ld = tty_ldisc_ref_wait(tty); in tty_read()
865 i = ld->ops->read(tty, file, buf, count); in tty_read()
876 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
878 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
879 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
882 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
884 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
887 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
899 struct tty_struct *tty, in do_tty_write() argument
907 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
928 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
934 if (tty->write_cnt < chunk) { in do_tty_write()
945 kfree(tty->write_buf); in do_tty_write()
946 tty->write_cnt = chunk; in do_tty_write()
947 tty->write_buf = buf_chunk; in do_tty_write()
956 if (copy_from_user(tty->write_buf, buf, size)) in do_tty_write()
958 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
976 tty_write_unlock(tty); in do_tty_write()
992 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
994 if (tty) { in tty_write_message()
995 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
996 tty_lock(tty); in tty_write_message()
997 if (tty->ops->write && tty->count > 0) in tty_write_message()
998 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
999 tty_unlock(tty); in tty_write_message()
1000 tty_write_unlock(tty); in tty_write_message()
1025 struct tty_struct *tty = file_tty(file); in tty_write() local
1029 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in tty_write()
1031 if (!tty || !tty->ops->write || tty_io_error(tty)) in tty_write()
1034 if (tty->ops->write_room == NULL) in tty_write()
1035 tty_err(tty, "missing write_room method\n"); in tty_write()
1036 ld = tty_ldisc_ref_wait(tty); in tty_write()
1042 ret = do_tty_write(ld->ops->write, tty, file, buf, count); in tty_write()
1074 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1076 int was_stopped = tty->stopped; in tty_send_xchar()
1078 if (tty->ops->send_xchar) { in tty_send_xchar()
1079 down_read(&tty->termios_rwsem); in tty_send_xchar()
1080 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1081 up_read(&tty->termios_rwsem); in tty_send_xchar()
1085 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1088 down_read(&tty->termios_rwsem); in tty_send_xchar()
1090 start_tty(tty); in tty_send_xchar()
1091 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1093 stop_tty(tty); in tty_send_xchar()
1094 up_read(&tty->termios_rwsem); in tty_send_xchar()
1095 tty_write_unlock(tty); in tty_send_xchar()
1154 struct tty_struct *tty; in tty_driver_lookup_tty() local
1158 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1160 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1162 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1164 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1165 tty_kref_get(tty); in tty_driver_lookup_tty()
1166 return tty; in tty_driver_lookup_tty()
1177 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1180 int idx = tty->index; in tty_init_termios()
1182 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1183 tty->termios = tty->driver->init_termios; in tty_init_termios()
1186 tp = tty->driver->termios[idx]; in tty_init_termios()
1188 tty->termios = *tp; in tty_init_termios()
1189 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1191 tty->termios = tty->driver->init_termios; in tty_init_termios()
1194 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1195 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1199 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1201 tty_init_termios(tty); in tty_standard_install()
1203 tty->count++; in tty_standard_install()
1204 driver->ttys[tty->index] = tty; in tty_standard_install()
1222 struct tty_struct *tty) in tty_driver_install_tty() argument
1224 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1225 tty_standard_install(driver, tty); in tty_driver_install_tty()
1238 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1241 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1243 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1255 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1257 struct tty_driver *driver = tty->driver; in tty_reopen()
1264 if (!tty->count) in tty_reopen()
1267 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1270 tty->count++; in tty_reopen()
1272 if (tty->ldisc) in tty_reopen()
1275 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1277 tty->count--; in tty_reopen()
1308 struct tty_struct *tty; in tty_init_dev() local
1322 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1323 if (!tty) { in tty_init_dev()
1328 tty_lock(tty); in tty_init_dev()
1329 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1333 if (!tty->port) in tty_init_dev()
1334 tty->port = driver->ports[idx]; in tty_init_dev()
1336 WARN_RATELIMIT(!tty->port, in tty_init_dev()
1338 __func__, tty->driver->name); in tty_init_dev()
1340 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1343 tty->port->itty = tty; in tty_init_dev()
1350 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1353 tty_ldisc_unlock(tty); in tty_init_dev()
1355 return tty; in tty_init_dev()
1358 tty_unlock(tty); in tty_init_dev()
1359 free_tty_struct(tty); in tty_init_dev()
1366 tty_ldisc_unlock(tty); in tty_init_dev()
1367 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1370 tty_unlock(tty); in tty_init_dev()
1371 release_tty(tty, idx); in tty_init_dev()
1375 static void tty_free_termios(struct tty_struct *tty) in tty_free_termios() argument
1378 int idx = tty->index; in tty_free_termios()
1381 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_free_termios()
1385 tp = tty->driver->termios[idx]; in tty_free_termios()
1390 tty->driver->termios[idx] = tp; in tty_free_termios()
1392 *tp = tty->termios; in tty_free_termios()
1401 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1403 flush_work(&tty->SAK_work); in tty_flush_works()
1404 flush_work(&tty->hangup_work); in tty_flush_works()
1405 if (tty->link) { in tty_flush_works()
1406 flush_work(&tty->link->SAK_work); in tty_flush_works()
1407 flush_work(&tty->link->hangup_work); in tty_flush_works()
1428 struct tty_struct *tty = in release_one_tty() local
1430 struct tty_driver *driver = tty->driver; in release_one_tty()
1433 if (tty->ops->cleanup) in release_one_tty()
1434 tty->ops->cleanup(tty); in release_one_tty()
1436 tty->magic = 0; in release_one_tty()
1440 spin_lock(&tty->files_lock); in release_one_tty()
1441 list_del_init(&tty->tty_files); in release_one_tty()
1442 spin_unlock(&tty->files_lock); in release_one_tty()
1444 put_pid(tty->pgrp); in release_one_tty()
1445 put_pid(tty->session); in release_one_tty()
1446 free_tty_struct(tty); in release_one_tty()
1451 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1455 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1456 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1467 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1469 if (tty) in tty_kref_put()
1470 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1486 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1489 WARN_ON(tty->index != idx); in release_tty()
1491 if (tty->ops->shutdown) in release_tty()
1492 tty->ops->shutdown(tty); in release_tty()
1493 tty_free_termios(tty); in release_tty()
1494 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1495 tty->port->itty = NULL; in release_tty()
1496 if (tty->link) in release_tty()
1497 tty->link->port->itty = NULL; in release_tty()
1498 tty_buffer_cancel_work(tty->port); in release_tty()
1499 if (tty->link) in release_tty()
1500 tty_buffer_cancel_work(tty->link->port); in release_tty()
1502 tty_kref_put(tty->link); in release_tty()
1503 tty_kref_put(tty); in release_tty()
1515 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1518 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1519 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1524 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1527 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1528 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1529 idx, tty->driver->ttys[idx]); in tty_release_checks()
1532 if (tty->driver->other) { in tty_release_checks()
1533 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1535 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1536 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1537 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1540 if (o_tty->link != tty) { in tty_release_checks()
1541 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1557 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1562 tty_ldisc_release(tty); in tty_kclose()
1565 tty_flush_works(tty); in tty_kclose()
1567 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1575 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1576 release_tty(tty, tty->index); in tty_kclose()
1589 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1594 tty_ldisc_release(tty); in tty_release_struct()
1597 tty_flush_works(tty); in tty_release_struct()
1599 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1607 release_tty(tty, idx); in tty_release_struct()
1633 struct tty_struct *tty = file_tty(filp); in tty_release() local
1640 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1643 tty_lock(tty); in tty_release()
1644 check_tty_count(tty, __func__); in tty_release()
1648 idx = tty->index; in tty_release()
1649 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1650 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1651 o_tty = tty->link; in tty_release()
1653 if (tty_release_checks(tty, idx)) { in tty_release()
1654 tty_unlock(tty); in tty_release()
1658 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1660 if (tty->ops->close) in tty_release()
1661 tty->ops->close(tty, filp); in tty_release()
1682 if (tty->count <= 1) { in tty_release()
1683 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1684 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1687 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1688 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1707 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1718 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1722 if (--tty->count < 0) { in tty_release()
1723 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1724 tty->count = 0; in tty_release()
1745 if (!tty->count) { in tty_release()
1747 session_clear_tty(tty->session); in tty_release()
1754 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1757 tty_unlock(tty); in tty_release()
1765 tty_debug_hangup(tty, "final close\n"); in tty_release()
1767 tty_release_struct(tty, idx); in tty_release()
1784 struct tty_struct *tty; in tty_open_current_tty() local
1790 tty = get_current_tty(); in tty_open_current_tty()
1791 if (!tty) in tty_open_current_tty()
1796 tty_lock(tty); in tty_open_current_tty()
1797 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1799 retval = tty_reopen(tty); in tty_open_current_tty()
1801 tty_unlock(tty); in tty_open_current_tty()
1802 tty = ERR_PTR(retval); in tty_open_current_tty()
1804 return tty; in tty_open_current_tty()
1871 struct tty_struct *tty; in tty_kopen() local
1883 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1884 if (IS_ERR(tty)) in tty_kopen()
1887 if (tty) { in tty_kopen()
1889 tty_kref_put(tty); in tty_kopen()
1890 tty = ERR_PTR(-EBUSY); in tty_kopen()
1892 tty = tty_init_dev(driver, index); in tty_kopen()
1893 if (IS_ERR(tty)) in tty_kopen()
1895 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1900 return tty; in tty_kopen()
1923 struct tty_struct *tty; in tty_open_by_driver() local
1936 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
1937 if (IS_ERR(tty)) { in tty_open_by_driver()
1942 if (tty) { in tty_open_by_driver()
1943 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
1944 tty_kref_put(tty); in tty_open_by_driver()
1946 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
1950 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
1951 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
1955 tty = ERR_PTR(retval); in tty_open_by_driver()
1958 retval = tty_reopen(tty); in tty_open_by_driver()
1960 tty_unlock(tty); in tty_open_by_driver()
1961 tty = ERR_PTR(retval); in tty_open_by_driver()
1964 tty = tty_init_dev(driver, index); in tty_open_by_driver()
1969 return tty; in tty_open_by_driver()
1998 struct tty_struct *tty; in tty_open() local
2010 tty = tty_open_current_tty(device, filp); in tty_open()
2011 if (!tty) in tty_open()
2012 tty = tty_open_by_driver(device, inode, filp); in tty_open()
2014 if (IS_ERR(tty)) { in tty_open()
2016 retval = PTR_ERR(tty); in tty_open()
2023 tty_add_file(tty, filp); in tty_open()
2025 check_tty_count(tty, __func__); in tty_open()
2026 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2028 if (tty->ops->open) in tty_open()
2029 retval = tty->ops->open(tty, filp); in tty_open()
2035 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2037 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2053 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2058 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2059 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2061 tty_open_proc_set_tty(filp, tty); in tty_open()
2062 tty_unlock(tty); in tty_open()
2082 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2086 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2089 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2093 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2100 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2104 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2107 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2115 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_fasync()
2116 if (tty->pgrp) { in __tty_fasync()
2117 pid = tty->pgrp; in __tty_fasync()
2124 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_fasync()
2135 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2138 tty_lock(tty); in tty_fasync()
2141 tty_unlock(tty); in tty_fasync()
2163 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2168 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2172 tty_audit_tiocsti(tty, ch); in tiocsti()
2173 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2176 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2192 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2196 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2197 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2198 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2213 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2218 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2219 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2223 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2228 tty->winsize = *ws; in tty_do_resize()
2230 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2250 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2256 if (tty->ops->resize) in tiocswinsz()
2257 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2259 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2333 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2341 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2357 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2362 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2383 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2387 if (tty->ops->break_ctl == NULL) in send_break()
2390 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2391 retval = tty->ops->break_ctl(tty, duration); in send_break()
2394 if (tty_write_lock(tty, 0) < 0) in send_break()
2396 retval = tty->ops->break_ctl(tty, -1); in send_break()
2401 retval = tty->ops->break_ctl(tty, 0); in send_break()
2403 tty_write_unlock(tty); in send_break()
2422 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2426 if (tty->ops->tiocmget) { in tty_tiocmget()
2427 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2447 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2453 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2474 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2477 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2482 if (tty->ops->get_icount) in tty_tiocgicount()
2483 retval = tty->ops->get_icount(tty, &icount); in tty_tiocgicount()
2513 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2515 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2516 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2517 tty = tty->link; in tty_pair_get_tty()
2518 return tty; in tty_pair_get_tty()
2526 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2532 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2535 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2546 retval = tty_check_change(tty); in tty_ioctl()
2550 tty_wait_until_sent(tty, 0); in tty_ioctl()
2562 return tiocsti(tty, p); in tty_ioctl()
2568 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2572 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2575 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2579 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2583 return tiocgetd(tty, p); in tty_ioctl()
2585 return tiocsetd(tty, p); in tty_ioctl()
2589 tty_vhangup(tty); in tty_ioctl()
2600 if (tty->ops->break_ctl) in tty_ioctl()
2601 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2604 if (tty->ops->break_ctl) in tty_ioctl()
2605 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2613 return send_break(tty, 250); in tty_ioctl()
2616 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2619 return tty_tiocmget(tty, p); in tty_ioctl()
2623 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2625 retval = tty_tiocgicount(tty, p); in tty_ioctl()
2635 tty_buffer_flush(tty, NULL); in tty_ioctl()
2644 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2646 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2650 if (tty->ops->ioctl) { in tty_ioctl()
2651 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2655 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2660 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
2672 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2676 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2679 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2680 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2685 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2689 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
2691 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); in tty_compat_ioctl()
2724 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
2727 tty_hangup(tty); in __do_SAK()
2733 if (!tty) in __do_SAK()
2735 session = tty->session; in __do_SAK()
2737 tty_ldisc_flush(tty); in __do_SAK()
2739 tty_driver_flush_buffer(tty); in __do_SAK()
2744 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
2751 if (p->signal->tty == tty) { in __do_SAK()
2752 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
2758 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
2760 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
2772 struct tty_struct *tty = in do_SAK_work() local
2774 __do_SAK(tty); in do_SAK_work()
2783 void do_SAK(struct tty_struct *tty) in do_SAK() argument
2785 if (!tty) in do_SAK()
2787 schedule_work(&tty->SAK_work); in do_SAK()
2799 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
2801 dev_t devt = tty_devnum(tty); in tty_get_device()
2816 struct tty_struct *tty; in alloc_tty_struct() local
2818 tty = kzalloc(sizeof(*tty), GFP_KERNEL); in alloc_tty_struct()
2819 if (!tty) in alloc_tty_struct()
2822 kref_init(&tty->kref); in alloc_tty_struct()
2823 tty->magic = TTY_MAGIC; in alloc_tty_struct()
2824 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
2825 kfree(tty); in alloc_tty_struct()
2828 tty->session = NULL; in alloc_tty_struct()
2829 tty->pgrp = NULL; in alloc_tty_struct()
2830 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
2831 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
2832 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
2833 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
2834 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
2835 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
2836 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
2837 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
2838 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
2839 spin_lock_init(&tty->ctrl_lock); in alloc_tty_struct()
2840 spin_lock_init(&tty->flow_lock); in alloc_tty_struct()
2841 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
2842 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
2843 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
2845 tty->driver = driver; in alloc_tty_struct()
2846 tty->ops = driver->ops; in alloc_tty_struct()
2847 tty->index = idx; in alloc_tty_struct()
2848 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
2849 tty->dev = tty_get_device(tty); in alloc_tty_struct()
2851 return tty; in alloc_tty_struct()
2866 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
2868 if (tty->ops->put_char) in tty_put_char()
2869 return tty->ops->put_char(tty, ch); in tty_put_char()
2870 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3232 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3234 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()