Lines Matching refs:tty
113 # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args) argument
115 # define tty_debug_hangup(tty, f, args...) do { } while (0) argument
159 static void release_tty(struct tty_struct *tty, int idx);
170 static void free_tty_struct(struct tty_struct *tty) in free_tty_struct() argument
172 tty_ldisc_deinit(tty); in free_tty_struct()
173 put_device(tty->dev); in free_tty_struct()
174 kfree(tty->write_buf); in free_tty_struct()
175 tty->magic = 0xDEADDEAD; in free_tty_struct()
176 kfree(tty); in free_tty_struct()
181 return ((struct tty_file_private *)file->private_data)->tty; in file_tty()
198 void tty_add_file(struct tty_struct *tty, struct file *file) in tty_add_file() argument
202 priv->tty = tty; in tty_add_file()
205 spin_lock(&tty->files_lock); in tty_add_file()
206 list_add(&priv->list, &tty->tty_files); in tty_add_file()
207 spin_unlock(&tty->files_lock); in tty_add_file()
228 struct tty_struct *tty = priv->tty; in tty_del_file() local
230 spin_lock(&tty->files_lock); in tty_del_file()
232 spin_unlock(&tty->files_lock); in tty_del_file()
246 const char *tty_name(const struct tty_struct *tty) in tty_name() argument
248 if (!tty) /* Hmm. NULL pointer. That's fun. */ in tty_name()
250 return tty->name; in tty_name()
255 const char *tty_driver_name(const struct tty_struct *tty) in tty_driver_name() argument
257 if (!tty || !tty->driver) in tty_driver_name()
259 return tty->driver->name; in tty_driver_name()
262 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, in tty_paranoia_check() argument
266 if (!tty) { in tty_paranoia_check()
271 if (tty->magic != TTY_MAGIC) { in tty_paranoia_check()
281 static int check_tty_count(struct tty_struct *tty, const char *routine) in check_tty_count() argument
287 spin_lock(&tty->files_lock); in check_tty_count()
288 list_for_each(p, &tty->tty_files) { in check_tty_count()
291 spin_unlock(&tty->files_lock); in check_tty_count()
292 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in check_tty_count()
293 tty->driver->subtype == PTY_TYPE_SLAVE && in check_tty_count()
294 tty->link && tty->link->count) in check_tty_count()
296 if (tty_port_kopened(tty->port)) in check_tty_count()
298 if (tty->count != (count + kopen_count)) { in check_tty_count()
299 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n", in check_tty_count()
300 routine, tty->count, count, kopen_count); in check_tty_count()
471 struct tty_struct *tty = file_tty(file); in tty_show_fdinfo() local
473 if (tty && tty->ops && tty->ops->show_fdinfo) in tty_show_fdinfo()
474 tty->ops->show_fdinfo(tty, m); in tty_show_fdinfo()
527 void tty_wakeup(struct tty_struct *tty) in tty_wakeup() argument
531 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { in tty_wakeup()
532 ld = tty_ldisc_ref(tty); in tty_wakeup()
535 ld->ops->write_wakeup(tty); in tty_wakeup()
539 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_wakeup()
566 static void __tty_hangup(struct tty_struct *tty, int exit_session) in __tty_hangup() argument
574 if (!tty) in __tty_hangup()
579 if (redirect && file_tty(redirect) == tty) { in __tty_hangup()
585 tty_lock(tty); in __tty_hangup()
587 if (test_bit(TTY_HUPPED, &tty->flags)) { in __tty_hangup()
588 tty_unlock(tty); in __tty_hangup()
598 set_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
603 check_tty_count(tty, "tty_hangup"); in __tty_hangup()
605 spin_lock(&tty->files_lock); in __tty_hangup()
607 list_for_each_entry(priv, &tty->tty_files, list) { in __tty_hangup()
617 spin_unlock(&tty->files_lock); in __tty_hangup()
619 refs = tty_signal_session_leader(tty, exit_session); in __tty_hangup()
622 tty_kref_put(tty); in __tty_hangup()
624 tty_ldisc_hangup(tty, cons_filp != NULL); in __tty_hangup()
626 spin_lock_irq(&tty->ctrl_lock); in __tty_hangup()
627 clear_bit(TTY_THROTTLED, &tty->flags); in __tty_hangup()
628 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in __tty_hangup()
629 put_pid(tty->session); in __tty_hangup()
630 put_pid(tty->pgrp); in __tty_hangup()
631 tty->session = NULL; in __tty_hangup()
632 tty->pgrp = NULL; in __tty_hangup()
633 tty->ctrl_status = 0; in __tty_hangup()
634 spin_unlock_irq(&tty->ctrl_lock); in __tty_hangup()
643 if (tty->ops->close) in __tty_hangup()
645 tty->ops->close(tty, cons_filp); in __tty_hangup()
646 } else if (tty->ops->hangup) in __tty_hangup()
647 tty->ops->hangup(tty); in __tty_hangup()
653 set_bit(TTY_HUPPED, &tty->flags); in __tty_hangup()
654 clear_bit(TTY_HUPPING, &tty->flags); in __tty_hangup()
655 tty_unlock(tty); in __tty_hangup()
663 struct tty_struct *tty = in do_tty_hangup() local
666 __tty_hangup(tty, 0); in do_tty_hangup()
677 void tty_hangup(struct tty_struct *tty) in tty_hangup() argument
679 tty_debug_hangup(tty, "hangup\n"); in tty_hangup()
680 schedule_work(&tty->hangup_work); in tty_hangup()
694 void tty_vhangup(struct tty_struct *tty) in tty_vhangup() argument
696 tty_debug_hangup(tty, "vhangup\n"); in tty_vhangup()
697 __tty_hangup(tty, 0); in tty_vhangup()
711 struct tty_struct *tty; in tty_vhangup_self() local
713 tty = get_current_tty(); in tty_vhangup_self()
714 if (tty) { in tty_vhangup_self()
715 tty_vhangup(tty); in tty_vhangup_self()
716 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()
769 void __stop_tty(struct tty_struct *tty) in __stop_tty() argument
771 if (tty->stopped) in __stop_tty()
773 tty->stopped = 1; in __stop_tty()
774 if (tty->ops->stop) in __stop_tty()
775 tty->ops->stop(tty); in __stop_tty()
778 void stop_tty(struct tty_struct *tty) in stop_tty() argument
782 spin_lock_irqsave(&tty->flow_lock, flags); in stop_tty()
783 __stop_tty(tty); in stop_tty()
784 spin_unlock_irqrestore(&tty->flow_lock, flags); in stop_tty()
800 void __start_tty(struct tty_struct *tty) in __start_tty() argument
802 if (!tty->stopped || tty->flow_stopped) in __start_tty()
804 tty->stopped = 0; in __start_tty()
805 if (tty->ops->start) in __start_tty()
806 tty->ops->start(tty); in __start_tty()
807 tty_wakeup(tty); in __start_tty()
810 void start_tty(struct tty_struct *tty) in start_tty() argument
814 spin_lock_irqsave(&tty->flow_lock, flags); in start_tty()
815 __start_tty(tty); in start_tty()
816 spin_unlock_irqrestore(&tty->flow_lock, flags); in start_tty()
854 struct tty_struct *tty = file_tty(file); in tty_read() local
857 if (tty_paranoia_check(tty, inode, "tty_read")) in tty_read()
859 if (!tty || tty_io_error(tty)) in tty_read()
864 ld = tty_ldisc_ref_wait(tty); in tty_read()
868 i = ld->ops->read(tty, file, buf, count); in tty_read()
879 static void tty_write_unlock(struct tty_struct *tty) in tty_write_unlock() argument
881 mutex_unlock(&tty->atomic_write_lock); in tty_write_unlock()
882 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT); in tty_write_unlock()
885 static int tty_write_lock(struct tty_struct *tty, int ndelay) in tty_write_lock() argument
887 if (!mutex_trylock(&tty->atomic_write_lock)) { in tty_write_lock()
890 if (mutex_lock_interruptible(&tty->atomic_write_lock)) in tty_write_lock()
902 struct tty_struct *tty, in do_tty_write() argument
910 ret = tty_write_lock(tty, file->f_flags & O_NDELAY); in do_tty_write()
931 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) in do_tty_write()
937 if (tty->write_cnt < chunk) { in do_tty_write()
948 kfree(tty->write_buf); in do_tty_write()
949 tty->write_cnt = chunk; in do_tty_write()
950 tty->write_buf = buf_chunk; in do_tty_write()
959 if (copy_from_user(tty->write_buf, buf, size)) in do_tty_write()
961 ret = write(tty, file, tty->write_buf, size); in do_tty_write()
979 tty_write_unlock(tty); in do_tty_write()
995 void tty_write_message(struct tty_struct *tty, char *msg) in tty_write_message() argument
997 if (tty) { in tty_write_message()
998 mutex_lock(&tty->atomic_write_lock); in tty_write_message()
999 tty_lock(tty); in tty_write_message()
1000 if (tty->ops->write && tty->count > 0) in tty_write_message()
1001 tty->ops->write(tty, msg, strlen(msg)); in tty_write_message()
1002 tty_unlock(tty); in tty_write_message()
1003 tty_write_unlock(tty); in tty_write_message()
1028 struct tty_struct *tty = file_tty(file); in tty_write() local
1032 if (tty_paranoia_check(tty, file_inode(file), "tty_write")) in tty_write()
1034 if (!tty || !tty->ops->write || tty_io_error(tty)) in tty_write()
1037 if (tty->ops->write_room == NULL) in tty_write()
1038 tty_err(tty, "missing write_room method\n"); in tty_write()
1039 ld = tty_ldisc_ref_wait(tty); in tty_write()
1045 ret = do_tty_write(ld->ops->write, tty, file, buf, count); in tty_write()
1077 int tty_send_xchar(struct tty_struct *tty, char ch) in tty_send_xchar() argument
1079 int was_stopped = tty->stopped; in tty_send_xchar()
1081 if (tty->ops->send_xchar) { in tty_send_xchar()
1082 down_read(&tty->termios_rwsem); in tty_send_xchar()
1083 tty->ops->send_xchar(tty, ch); in tty_send_xchar()
1084 up_read(&tty->termios_rwsem); in tty_send_xchar()
1088 if (tty_write_lock(tty, 0) < 0) in tty_send_xchar()
1091 down_read(&tty->termios_rwsem); in tty_send_xchar()
1093 start_tty(tty); in tty_send_xchar()
1094 tty->ops->write(tty, &ch, 1); in tty_send_xchar()
1096 stop_tty(tty); in tty_send_xchar()
1097 up_read(&tty->termios_rwsem); in tty_send_xchar()
1098 tty_write_unlock(tty); in tty_send_xchar()
1157 struct tty_struct *tty; in tty_driver_lookup_tty() local
1161 tty = ERR_PTR(-EIO); in tty_driver_lookup_tty()
1163 tty = driver->ops->lookup(driver, file, idx); in tty_driver_lookup_tty()
1165 tty = driver->ttys[idx]; in tty_driver_lookup_tty()
1167 if (!IS_ERR(tty)) in tty_driver_lookup_tty()
1168 tty_kref_get(tty); in tty_driver_lookup_tty()
1169 return tty; in tty_driver_lookup_tty()
1180 void tty_init_termios(struct tty_struct *tty) in tty_init_termios() argument
1183 int idx = tty->index; in tty_init_termios()
1185 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_init_termios()
1186 tty->termios = tty->driver->init_termios; in tty_init_termios()
1189 tp = tty->driver->termios[idx]; in tty_init_termios()
1191 tty->termios = *tp; in tty_init_termios()
1192 tty->termios.c_line = tty->driver->init_termios.c_line; in tty_init_termios()
1194 tty->termios = tty->driver->init_termios; in tty_init_termios()
1197 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); in tty_init_termios()
1198 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); in tty_init_termios()
1202 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) in tty_standard_install() argument
1204 tty_init_termios(tty); in tty_standard_install()
1206 tty->count++; in tty_standard_install()
1207 driver->ttys[tty->index] = tty; in tty_standard_install()
1225 struct tty_struct *tty) in tty_driver_install_tty() argument
1227 return driver->ops->install ? driver->ops->install(driver, tty) : in tty_driver_install_tty()
1228 tty_standard_install(driver, tty); in tty_driver_install_tty()
1241 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) in tty_driver_remove_tty() argument
1244 driver->ops->remove(driver, tty); in tty_driver_remove_tty()
1246 driver->ttys[tty->index] = NULL; in tty_driver_remove_tty()
1258 static int tty_reopen(struct tty_struct *tty) in tty_reopen() argument
1260 struct tty_driver *driver = tty->driver; in tty_reopen()
1268 if (!tty->count) in tty_reopen()
1271 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) in tty_reopen()
1274 ld = tty_ldisc_ref_wait(tty); in tty_reopen()
1278 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_reopen()
1282 if (!tty->ldisc) in tty_reopen()
1283 retval = tty_ldisc_reinit(tty, tty->termios.c_line); in tty_reopen()
1284 tty_ldisc_unlock(tty); in tty_reopen()
1288 tty->count++; in tty_reopen()
1319 struct tty_struct *tty; in tty_init_dev() local
1333 tty = alloc_tty_struct(driver, idx); in tty_init_dev()
1334 if (!tty) { in tty_init_dev()
1339 tty_lock(tty); in tty_init_dev()
1340 retval = tty_driver_install_tty(driver, tty); in tty_init_dev()
1344 if (!tty->port) in tty_init_dev()
1345 tty->port = driver->ports[idx]; in tty_init_dev()
1347 WARN_RATELIMIT(!tty->port, in tty_init_dev()
1349 __func__, tty->driver->name); in tty_init_dev()
1351 retval = tty_ldisc_lock(tty, 5 * HZ); in tty_init_dev()
1354 tty->port->itty = tty; in tty_init_dev()
1361 retval = tty_ldisc_setup(tty, tty->link); in tty_init_dev()
1364 tty_ldisc_unlock(tty); in tty_init_dev()
1366 return tty; in tty_init_dev()
1369 tty_unlock(tty); in tty_init_dev()
1370 free_tty_struct(tty); in tty_init_dev()
1377 tty_ldisc_unlock(tty); in tty_init_dev()
1378 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n", in tty_init_dev()
1381 tty_unlock(tty); in tty_init_dev()
1382 release_tty(tty, idx); in tty_init_dev()
1392 void tty_save_termios(struct tty_struct *tty) in tty_save_termios() argument
1395 int idx = tty->index; in tty_save_termios()
1398 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) in tty_save_termios()
1402 tp = tty->driver->termios[idx]; in tty_save_termios()
1407 tty->driver->termios[idx] = tp; in tty_save_termios()
1409 *tp = tty->termios; in tty_save_termios()
1419 static void tty_flush_works(struct tty_struct *tty) in tty_flush_works() argument
1421 flush_work(&tty->SAK_work); in tty_flush_works()
1422 flush_work(&tty->hangup_work); in tty_flush_works()
1423 if (tty->link) { in tty_flush_works()
1424 flush_work(&tty->link->SAK_work); in tty_flush_works()
1425 flush_work(&tty->link->hangup_work); in tty_flush_works()
1446 struct tty_struct *tty = in release_one_tty() local
1448 struct tty_driver *driver = tty->driver; in release_one_tty()
1451 if (tty->ops->cleanup) in release_one_tty()
1452 tty->ops->cleanup(tty); in release_one_tty()
1454 tty->magic = 0; in release_one_tty()
1458 spin_lock(&tty->files_lock); in release_one_tty()
1459 list_del_init(&tty->tty_files); in release_one_tty()
1460 spin_unlock(&tty->files_lock); in release_one_tty()
1462 put_pid(tty->pgrp); in release_one_tty()
1463 put_pid(tty->session); in release_one_tty()
1464 free_tty_struct(tty); in release_one_tty()
1469 struct tty_struct *tty = container_of(kref, struct tty_struct, kref); in queue_release_one_tty() local
1473 INIT_WORK(&tty->hangup_work, release_one_tty); in queue_release_one_tty()
1474 schedule_work(&tty->hangup_work); in queue_release_one_tty()
1485 void tty_kref_put(struct tty_struct *tty) in tty_kref_put() argument
1487 if (tty) in tty_kref_put()
1488 kref_put(&tty->kref, queue_release_one_tty); in tty_kref_put()
1504 static void release_tty(struct tty_struct *tty, int idx) in release_tty() argument
1507 WARN_ON(tty->index != idx); in release_tty()
1509 if (tty->ops->shutdown) in release_tty()
1510 tty->ops->shutdown(tty); in release_tty()
1511 tty_save_termios(tty); in release_tty()
1512 tty_driver_remove_tty(tty->driver, tty); in release_tty()
1513 tty->port->itty = NULL; in release_tty()
1514 if (tty->link) in release_tty()
1515 tty->link->port->itty = NULL; in release_tty()
1516 tty_buffer_cancel_work(tty->port); in release_tty()
1517 if (tty->link) in release_tty()
1518 tty_buffer_cancel_work(tty->link->port); in release_tty()
1520 tty_kref_put(tty->link); in release_tty()
1521 tty_kref_put(tty); in release_tty()
1533 static int tty_release_checks(struct tty_struct *tty, int idx) in tty_release_checks() argument
1536 if (idx < 0 || idx >= tty->driver->num) { in tty_release_checks()
1537 tty_debug(tty, "bad idx %d\n", idx); in tty_release_checks()
1542 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) in tty_release_checks()
1545 if (tty != tty->driver->ttys[idx]) { in tty_release_checks()
1546 tty_debug(tty, "bad driver table[%d] = %p\n", in tty_release_checks()
1547 idx, tty->driver->ttys[idx]); in tty_release_checks()
1550 if (tty->driver->other) { in tty_release_checks()
1551 struct tty_struct *o_tty = tty->link; in tty_release_checks()
1553 if (o_tty != tty->driver->other->ttys[idx]) { in tty_release_checks()
1554 tty_debug(tty, "bad other table[%d] = %p\n", in tty_release_checks()
1555 idx, tty->driver->other->ttys[idx]); in tty_release_checks()
1558 if (o_tty->link != tty) { in tty_release_checks()
1559 tty_debug(tty, "bad link = %p\n", o_tty->link); in tty_release_checks()
1575 void tty_kclose(struct tty_struct *tty) in tty_kclose() argument
1580 tty_ldisc_release(tty); in tty_kclose()
1583 tty_flush_works(tty); in tty_kclose()
1585 tty_debug_hangup(tty, "freeing structure\n"); in tty_kclose()
1593 tty_port_set_kopened(tty->port, 0); in tty_kclose()
1594 release_tty(tty, tty->index); in tty_kclose()
1607 void tty_release_struct(struct tty_struct *tty, int idx) in tty_release_struct() argument
1612 tty_ldisc_release(tty); in tty_release_struct()
1615 tty_flush_works(tty); in tty_release_struct()
1617 tty_debug_hangup(tty, "freeing structure\n"); in tty_release_struct()
1625 release_tty(tty, idx); in tty_release_struct()
1651 struct tty_struct *tty = file_tty(filp); in tty_release() local
1658 if (tty_paranoia_check(tty, inode, __func__)) in tty_release()
1661 tty_lock(tty); in tty_release()
1662 check_tty_count(tty, __func__); in tty_release()
1666 idx = tty->index; in tty_release()
1667 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_release()
1668 tty->driver->subtype == PTY_TYPE_MASTER) in tty_release()
1669 o_tty = tty->link; in tty_release()
1671 if (tty_release_checks(tty, idx)) { in tty_release()
1672 tty_unlock(tty); in tty_release()
1676 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count); in tty_release()
1678 if (tty->ops->close) in tty_release()
1679 tty->ops->close(tty, filp); in tty_release()
1700 if (tty->count <= 1) { in tty_release()
1701 if (waitqueue_active(&tty->read_wait)) { in tty_release()
1702 wake_up_poll(&tty->read_wait, EPOLLIN); in tty_release()
1705 if (waitqueue_active(&tty->write_wait)) { in tty_release()
1706 wake_up_poll(&tty->write_wait, EPOLLOUT); in tty_release()
1725 tty_warn(tty, "read/write wait queue active!\n"); in tty_release()
1736 tty_warn(tty, "bad slave count (%d)\n", o_tty->count); in tty_release()
1740 if (--tty->count < 0) { in tty_release()
1741 tty_warn(tty, "bad tty->count (%d)\n", tty->count); in tty_release()
1742 tty->count = 0; in tty_release()
1763 if (!tty->count) { in tty_release()
1765 session_clear_tty(tty->session); in tty_release()
1772 final = !tty->count && !(o_tty && o_tty->count); in tty_release()
1775 tty_unlock(tty); in tty_release()
1783 tty_debug_hangup(tty, "final close\n"); in tty_release()
1785 tty_release_struct(tty, idx); in tty_release()
1802 struct tty_struct *tty; in tty_open_current_tty() local
1808 tty = get_current_tty(); in tty_open_current_tty()
1809 if (!tty) in tty_open_current_tty()
1814 tty_lock(tty); in tty_open_current_tty()
1815 tty_kref_put(tty); /* safe to drop the kref now */ in tty_open_current_tty()
1817 retval = tty_reopen(tty); in tty_open_current_tty()
1819 tty_unlock(tty); in tty_open_current_tty()
1820 tty = ERR_PTR(retval); in tty_open_current_tty()
1822 return tty; in tty_open_current_tty()
1891 struct tty_struct *tty; in tty_kopen() local
1903 tty = tty_driver_lookup_tty(driver, NULL, index); in tty_kopen()
1904 if (IS_ERR(tty)) in tty_kopen()
1907 if (tty) { in tty_kopen()
1909 tty_kref_put(tty); in tty_kopen()
1910 tty = ERR_PTR(-EBUSY); in tty_kopen()
1912 tty = tty_init_dev(driver, index); in tty_kopen()
1913 if (IS_ERR(tty)) in tty_kopen()
1915 tty_port_set_kopened(tty->port, 1); in tty_kopen()
1920 return tty; in tty_kopen()
1943 struct tty_struct *tty; in tty_open_by_driver() local
1956 tty = tty_driver_lookup_tty(driver, filp, index); in tty_open_by_driver()
1957 if (IS_ERR(tty)) { in tty_open_by_driver()
1962 if (tty) { in tty_open_by_driver()
1963 if (tty_port_kopened(tty->port)) { in tty_open_by_driver()
1964 tty_kref_put(tty); in tty_open_by_driver()
1966 tty = ERR_PTR(-EBUSY); in tty_open_by_driver()
1970 retval = tty_lock_interruptible(tty); in tty_open_by_driver()
1971 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */ in tty_open_by_driver()
1975 tty = ERR_PTR(retval); in tty_open_by_driver()
1978 retval = tty_reopen(tty); in tty_open_by_driver()
1980 tty_unlock(tty); in tty_open_by_driver()
1981 tty = ERR_PTR(retval); in tty_open_by_driver()
1984 tty = tty_init_dev(driver, index); in tty_open_by_driver()
1989 return tty; in tty_open_by_driver()
2018 struct tty_struct *tty; in tty_open() local
2030 tty = tty_open_current_tty(device, filp); in tty_open()
2031 if (!tty) in tty_open()
2032 tty = tty_open_by_driver(device, inode, filp); in tty_open()
2034 if (IS_ERR(tty)) { in tty_open()
2036 retval = PTR_ERR(tty); in tty_open()
2043 tty_add_file(tty, filp); in tty_open()
2045 check_tty_count(tty, __func__); in tty_open()
2046 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); in tty_open()
2048 if (tty->ops->open) in tty_open()
2049 retval = tty->ops->open(tty, filp); in tty_open()
2055 tty_debug_hangup(tty, "open error %d, releasing\n", retval); in tty_open()
2057 tty_unlock(tty); /* need to call tty_release without BTM */ in tty_open()
2073 clear_bit(TTY_HUPPED, &tty->flags); in tty_open()
2078 (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_open()
2079 tty->driver->subtype == PTY_TYPE_MASTER); in tty_open()
2081 tty_open_proc_set_tty(filp, tty); in tty_open()
2082 tty_unlock(tty); in tty_open()
2102 struct tty_struct *tty = file_tty(filp); in tty_poll() local
2106 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) in tty_poll()
2109 ld = tty_ldisc_ref_wait(tty); in tty_poll()
2113 ret = ld->ops->poll(tty, filp, wait); in tty_poll()
2120 struct tty_struct *tty = file_tty(filp); in __tty_fasync() local
2124 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) in __tty_fasync()
2127 retval = fasync_helper(fd, filp, on, &tty->fasync); in __tty_fasync()
2135 spin_lock_irqsave(&tty->ctrl_lock, flags); in __tty_fasync()
2136 if (tty->pgrp) { in __tty_fasync()
2137 pid = tty->pgrp; in __tty_fasync()
2144 spin_unlock_irqrestore(&tty->ctrl_lock, flags); in __tty_fasync()
2155 struct tty_struct *tty = file_tty(filp); in tty_fasync() local
2158 tty_lock(tty); in tty_fasync()
2161 tty_unlock(tty); in tty_fasync()
2183 static int tiocsti(struct tty_struct *tty, char __user *p) in tiocsti() argument
2188 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) in tiocsti()
2192 tty_audit_tiocsti(tty, ch); in tiocsti()
2193 ld = tty_ldisc_ref_wait(tty); in tiocsti()
2197 ld->ops->receive_buf(tty, &ch, &mbz, 1); in tiocsti()
2213 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocgwinsz() argument
2217 mutex_lock(&tty->winsize_mutex); in tiocgwinsz()
2218 err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); in tiocgwinsz()
2219 mutex_unlock(&tty->winsize_mutex); in tiocgwinsz()
2234 int tty_do_resize(struct tty_struct *tty, struct winsize *ws) in tty_do_resize() argument
2239 mutex_lock(&tty->winsize_mutex); in tty_do_resize()
2240 if (!memcmp(ws, &tty->winsize, sizeof(*ws))) in tty_do_resize()
2244 pgrp = tty_get_pgrp(tty); in tty_do_resize()
2249 tty->winsize = *ws; in tty_do_resize()
2251 mutex_unlock(&tty->winsize_mutex); in tty_do_resize()
2271 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) in tiocswinsz() argument
2277 if (tty->ops->resize) in tiocswinsz()
2278 return tty->ops->resize(tty, &tmp_ws); in tiocswinsz()
2280 return tty_do_resize(tty, &tmp_ws); in tiocswinsz()
2326 static int tiocsetd(struct tty_struct *tty, int __user *p) in tiocsetd() argument
2334 ret = tty_set_ldisc(tty, disc); in tiocsetd()
2350 static int tiocgetd(struct tty_struct *tty, int __user *p) in tiocgetd() argument
2355 ld = tty_ldisc_ref_wait(tty); in tiocgetd()
2376 static int send_break(struct tty_struct *tty, unsigned int duration) in send_break() argument
2380 if (tty->ops->break_ctl == NULL) in send_break()
2383 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) in send_break()
2384 retval = tty->ops->break_ctl(tty, duration); in send_break()
2387 if (tty_write_lock(tty, 0) < 0) in send_break()
2389 retval = tty->ops->break_ctl(tty, -1); in send_break()
2394 retval = tty->ops->break_ctl(tty, 0); in send_break()
2396 tty_write_unlock(tty); in send_break()
2415 static int tty_tiocmget(struct tty_struct *tty, int __user *p) in tty_tiocmget() argument
2419 if (tty->ops->tiocmget) { in tty_tiocmget()
2420 retval = tty->ops->tiocmget(tty); in tty_tiocmget()
2440 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, in tty_tiocmset() argument
2446 if (tty->ops->tiocmset == NULL) in tty_tiocmset()
2467 return tty->ops->tiocmset(tty, set, clear); in tty_tiocmset()
2470 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) in tty_tiocgicount() argument
2475 if (tty->ops->get_icount) in tty_tiocgicount()
2476 retval = tty->ops->get_icount(tty, &icount); in tty_tiocgicount()
2484 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocsserial() argument
2501 if (!tty->ops->set_serial) in tty_tiocsserial()
2503 return tty->ops->set_serial(tty, &v); in tty_tiocsserial()
2506 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss) in tty_tiocgserial() argument
2512 if (!tty->ops->get_serial) in tty_tiocgserial()
2514 err = tty->ops->get_serial(tty, &v); in tty_tiocgserial()
2524 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) in tty_pair_get_tty() argument
2526 if (tty->driver->type == TTY_DRIVER_TYPE_PTY && in tty_pair_get_tty()
2527 tty->driver->subtype == PTY_TYPE_MASTER) in tty_pair_get_tty()
2528 tty = tty->link; in tty_pair_get_tty()
2529 return tty; in tty_pair_get_tty()
2537 struct tty_struct *tty = file_tty(file); in tty_ioctl() local
2543 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_ioctl()
2546 real_tty = tty_pair_get_tty(tty); in tty_ioctl()
2557 retval = tty_check_change(tty); in tty_ioctl()
2561 tty_wait_until_sent(tty, 0); in tty_ioctl()
2573 return tiocsti(tty, p); in tty_ioctl()
2579 return real_tty != tty ? -EINVAL : tioccons(file); in tty_ioctl()
2581 set_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2584 clear_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2588 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); in tty_ioctl()
2592 return tiocgetd(tty, p); in tty_ioctl()
2594 return tiocsetd(tty, p); in tty_ioctl()
2598 tty_vhangup(tty); in tty_ioctl()
2609 if (tty->ops->break_ctl) in tty_ioctl()
2610 return tty->ops->break_ctl(tty, -1); in tty_ioctl()
2613 if (tty->ops->break_ctl) in tty_ioctl()
2614 return tty->ops->break_ctl(tty, 0); in tty_ioctl()
2622 return send_break(tty, 250); in tty_ioctl()
2625 return send_break(tty, arg ? arg*100 : 250); in tty_ioctl()
2628 return tty_tiocmget(tty, p); in tty_ioctl()
2632 return tty_tiocmset(tty, cmd, p); in tty_ioctl()
2634 return tty_tiocgicount(tty, p); in tty_ioctl()
2640 tty_buffer_flush(tty, NULL); in tty_ioctl()
2645 return tty_tiocsserial(tty, p); in tty_ioctl()
2647 return tty_tiocgserial(tty, p); in tty_ioctl()
2650 return ptm_open_peer(file, tty, (int)arg); in tty_ioctl()
2652 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg); in tty_ioctl()
2656 if (tty->ops->ioctl) { in tty_ioctl()
2657 retval = tty->ops->ioctl(tty, cmd, arg); in tty_ioctl()
2661 ld = tty_ldisc_ref_wait(tty); in tty_ioctl()
2666 retval = ld->ops->ioctl(tty, file, cmd, arg); in tty_ioctl()
2698 static int compat_tty_tiocsserial(struct tty_struct *tty, in compat_tty_tiocsserial() argument
2723 if (!tty->ops->set_serial) in compat_tty_tiocsserial()
2725 return tty->ops->set_serial(tty, &v); in compat_tty_tiocsserial()
2728 static int compat_tty_tiocgserial(struct tty_struct *tty, in compat_tty_tiocgserial() argument
2736 if (!tty->ops->set_serial) in compat_tty_tiocgserial()
2738 err = tty->ops->get_serial(tty, &v); in compat_tty_tiocgserial()
2753 struct tty_struct *tty = file_tty(file); in tty_compat_ioctl() local
2832 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) in tty_compat_ioctl()
2837 return compat_tty_tiocsserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2839 return compat_tty_tiocgserial(tty, compat_ptr(arg)); in tty_compat_ioctl()
2841 if (tty->ops->compat_ioctl) { in tty_compat_ioctl()
2842 retval = tty->ops->compat_ioctl(tty, cmd, arg); in tty_compat_ioctl()
2847 ld = tty_ldisc_ref_wait(tty); in tty_compat_ioctl()
2851 retval = ld->ops->compat_ioctl(tty, file, cmd, arg); in tty_compat_ioctl()
2853 retval = ld->ops->ioctl(tty, file, in tty_compat_ioctl()
2887 void __do_SAK(struct tty_struct *tty) in __do_SAK() argument
2890 tty_hangup(tty); in __do_SAK()
2896 if (!tty) in __do_SAK()
2898 session = tty->session; in __do_SAK()
2900 tty_ldisc_flush(tty); in __do_SAK()
2902 tty_driver_flush_buffer(tty); in __do_SAK()
2907 tty_notice(tty, "SAK: killed process %d (%s): by session\n", in __do_SAK()
2914 if (p->signal->tty == tty) { in __do_SAK()
2915 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n", in __do_SAK()
2921 i = iterate_fd(p->files, 0, this_tty, tty); in __do_SAK()
2923 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n", in __do_SAK()
2935 struct tty_struct *tty = in do_SAK_work() local
2937 __do_SAK(tty); in do_SAK_work()
2946 void do_SAK(struct tty_struct *tty) in do_SAK() argument
2948 if (!tty) in do_SAK()
2950 schedule_work(&tty->SAK_work); in do_SAK()
2956 static struct device *tty_get_device(struct tty_struct *tty) in tty_get_device() argument
2958 dev_t devt = tty_devnum(tty); in tty_get_device()
2973 struct tty_struct *tty; in alloc_tty_struct() local
2975 tty = kzalloc(sizeof(*tty), GFP_KERNEL); in alloc_tty_struct()
2976 if (!tty) in alloc_tty_struct()
2979 kref_init(&tty->kref); in alloc_tty_struct()
2980 tty->magic = TTY_MAGIC; in alloc_tty_struct()
2981 if (tty_ldisc_init(tty)) { in alloc_tty_struct()
2982 kfree(tty); in alloc_tty_struct()
2985 tty->session = NULL; in alloc_tty_struct()
2986 tty->pgrp = NULL; in alloc_tty_struct()
2987 mutex_init(&tty->legacy_mutex); in alloc_tty_struct()
2988 mutex_init(&tty->throttle_mutex); in alloc_tty_struct()
2989 init_rwsem(&tty->termios_rwsem); in alloc_tty_struct()
2990 mutex_init(&tty->winsize_mutex); in alloc_tty_struct()
2991 init_ldsem(&tty->ldisc_sem); in alloc_tty_struct()
2992 init_waitqueue_head(&tty->write_wait); in alloc_tty_struct()
2993 init_waitqueue_head(&tty->read_wait); in alloc_tty_struct()
2994 INIT_WORK(&tty->hangup_work, do_tty_hangup); in alloc_tty_struct()
2995 mutex_init(&tty->atomic_write_lock); in alloc_tty_struct()
2996 spin_lock_init(&tty->ctrl_lock); in alloc_tty_struct()
2997 spin_lock_init(&tty->flow_lock); in alloc_tty_struct()
2998 spin_lock_init(&tty->files_lock); in alloc_tty_struct()
2999 INIT_LIST_HEAD(&tty->tty_files); in alloc_tty_struct()
3000 INIT_WORK(&tty->SAK_work, do_SAK_work); in alloc_tty_struct()
3002 tty->driver = driver; in alloc_tty_struct()
3003 tty->ops = driver->ops; in alloc_tty_struct()
3004 tty->index = idx; in alloc_tty_struct()
3005 tty_line_name(driver, idx, tty->name); in alloc_tty_struct()
3006 tty->dev = tty_get_device(tty); in alloc_tty_struct()
3008 return tty; in alloc_tty_struct()
3023 int tty_put_char(struct tty_struct *tty, unsigned char ch) in tty_put_char() argument
3025 if (tty->ops->put_char) in tty_put_char()
3026 return tty->ops->put_char(tty, ch); in tty_put_char()
3027 return tty->ops->write(tty, &ch, 1); in tty_put_char()
3389 dev_t tty_devnum(struct tty_struct *tty) in tty_devnum() argument
3391 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; in tty_devnum()