Lines Matching refs:tty

7  * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
16 * dynamically allocated only when the tty is open.
19 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
20 * the low-level tty routines (serial.c, pty.c, console.c). This
31 * Added functionality to the OPOST tty handling. No delays, but all
76 #include <linux/tty.h>
112 #include "tty.h"
116 # define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
118 # define tty_debug_hangup(tty, f, args...) do { } while (0)
124 struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
138 * could do with some rationalisation such as pulling the tty proc function
142 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
144 /* Mutex to protect creating and releasing a tty */
159 static void release_tty(struct tty_struct *tty, int idx);
162 * free_tty_struct - free a disused tty
163 * @tty: tty struct to free
165 * Free the write buffers, tty queue and tty memory itself.
167 * Locking: none. Must be called after tty is definitely unused
169 static void free_tty_struct(struct tty_struct *tty)
171 tty_ldisc_deinit(tty);
172 put_device(tty->dev);
173 kvfree(tty->write_buf);
174 kfree(tty);
179 return ((struct tty_file_private *)file->private_data)->tty;
195 /* Associate a new file with the tty structure */
196 void tty_add_file(struct tty_struct *tty, struct file *file)
200 priv->tty = tty;
203 spin_lock(&tty->files_lock);
204 list_add(&priv->list, &tty->tty_files);
205 spin_unlock(&tty->files_lock);
223 /* Delete file from its tty */
227 struct tty_struct *tty = priv->tty;
229 spin_lock(&tty->files_lock);
231 spin_unlock(&tty->files_lock);
236 * tty_name - return tty naming
237 * @tty: tty structure
239 * Convert a tty structure into a name. The name reflects the kernel naming
244 const char *tty_name(const struct tty_struct *tty)
246 if (!tty) /* Hmm. NULL pointer. That's fun. */
247 return "NULL tty";
248 return tty->name;
252 const char *tty_driver_name(const struct tty_struct *tty)
254 if (!tty || !tty->driver)
256 return tty->driver->name;
259 static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
263 if (!tty) {
264 pr_warn("(%d:%d): %s: NULL tty\n",
273 static void check_tty_count(struct tty_struct *tty, const char *routine)
279 spin_lock(&tty->files_lock);
280 list_for_each(p, &tty->tty_files) {
283 spin_unlock(&tty->files_lock);
284 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
285 tty->driver->subtype == PTY_TYPE_SLAVE &&
286 tty->link && tty->link->count)
288 if (tty_port_kopened(tty->port))
290 if (tty->count != (count + kopen_count)) {
291 tty_warn(tty, "%s: tty->count(%d) != (#fd's(%d) + #kopen's(%d))\n",
292 routine, tty->count, count, kopen_count);
298 * get_tty_driver - find device of a tty
300 * @index: returns the index of the tty
302 * This routine returns a tty driver structure, given a device number and also
372 * tty_find_polling_driver - find device of a polled tty
374 * @line: pointer to resulting tty line nr
376 * This routine returns a tty driver structure, given a name and the condition
377 * that the tty driver is capable of polled operation.
396 /* Search through the tty devices to look for a match */
455 struct tty_struct *tty = file_tty(file);
457 if (tty && tty->ops && tty->ops->show_fdinfo)
458 tty->ops->show_fdinfo(tty, m);
503 * @tty: terminal
505 * Internal and external helper for wakeups of tty. This function informs the
509 void tty_wakeup(struct tty_struct *tty)
513 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
514 ld = tty_ldisc_ref(tty);
517 ld->ops->write_wakeup(tty);
521 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
527 * @tty: tty device
532 static struct file *tty_release_redirect(struct tty_struct *tty)
537 if (redirect && file_tty(redirect) == tty) {
548 * @tty: tty device
557 * hangup and signal delivery. The tty object itself remains intact.
571 static void __tty_hangup(struct tty_struct *tty, int exit_session)
579 if (!tty)
582 f = tty_release_redirect(tty);
584 tty_lock(tty);
586 if (test_bit(TTY_HUPPED, &tty->flags)) {
587 tty_unlock(tty);
597 set_bit(TTY_HUPPING, &tty->flags);
599 /* inuse_filps is protected by the single tty lock,
603 check_tty_count(tty, "tty_hangup");
605 spin_lock(&tty->files_lock);
607 list_for_each_entry(priv, &tty->tty_files, list) {
617 spin_unlock(&tty->files_lock);
619 refs = tty_signal_session_leader(tty, exit_session);
622 tty_kref_put(tty);
624 tty_ldisc_hangup(tty, cons_filp != NULL);
626 spin_lock_irq(&tty->ctrl.lock);
627 clear_bit(TTY_THROTTLED, &tty->flags);
628 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
629 put_pid(tty->ctrl.session);
630 put_pid(tty->ctrl.pgrp);
631 tty->ctrl.session = NULL;
632 tty->ctrl.pgrp = NULL;
633 tty->ctrl.pktstatus = 0;
634 spin_unlock_irq(&tty->ctrl.lock);
639 * tty->count and state->count to go out of sync.
643 if (tty->ops->close)
645 tty->ops->close(tty, cons_filp);
646 } else if (tty->ops->hangup)
647 tty->ops->hangup(tty);
653 set_bit(TTY_HUPPED, &tty->flags);
654 clear_bit(TTY_HUPPING, &tty->flags);
655 tty_unlock(tty);
663 struct tty_struct *tty =
666 __tty_hangup(tty, 0);
671 * @tty: tty to hangup
673 * A carrier loss (virtual or otherwise) has occurred on @tty. Schedule a
676 void tty_hangup(struct tty_struct *tty)
678 tty_debug_hangup(tty, "hangup\n");
679 schedule_work(&tty->hangup_work);
685 * @tty: tty to hangup
691 void tty_vhangup(struct tty_struct *tty)
693 tty_debug_hangup(tty, "vhangup\n");
694 __tty_hangup(tty, 0);
702 * Perform a vhangup on the current controlling tty
706 struct tty_struct *tty;
708 tty = get_current_tty();
709 if (tty) {
710 tty_vhangup(tty);
711 tty_kref_put(tty);
717 * @tty: tty to hangup
725 void tty_vhangup_session(struct tty_struct *tty)
727 tty_debug_hangup(tty, "session hangup\n");
728 __tty_hangup(tty, 1);
732 * tty_hung_up_p - was tty hung up
733 * @filp: file pointer of tty
735 * Return: true if the tty has been subject to a vhangup or a carrier loss
743 void __stop_tty(struct tty_struct *tty)
745 if (tty->flow.stopped)
747 tty->flow.stopped = true;
748 if (tty->ops->stop)
749 tty->ops->stop(tty);
754 * @tty: tty to stop
761 * under the tty %atomic_write_lock but not always.
766 void stop_tty(struct tty_struct *tty)
770 spin_lock_irqsave(&tty->flow.lock, flags);
771 __stop_tty(tty);
772 spin_unlock_irqrestore(&tty->flow.lock, flags);
776 void __start_tty(struct tty_struct *tty)
778 if (!tty->flow.stopped || tty->flow.tco_stopped)
780 tty->flow.stopped = false;
781 if (tty->ops->start)
782 tty->ops->start(tty);
783 tty_wakeup(tty);
788 * @tty: tty to start
790 * Start a tty that has been stopped if at all possible. If @tty was previously
797 void start_tty(struct tty_struct *tty)
801 spin_lock_irqsave(&tty->flow.lock, flags);
802 __start_tty(tty);
803 spin_unlock_irqrestore(&tty->flow.lock, flags);
807 static void tty_update_time(struct tty_struct *tty, bool mtime)
812 spin_lock(&tty->files_lock);
813 list_for_each_entry(priv, &tty->tty_files, list) {
820 * the time of the tty device, otherwise it could be construded as a
821 * security leak to let userspace know the exact timing of the tty.
830 spin_unlock(&tty->files_lock);
844 static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
856 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
892 /* We always clear tty buffer in case they contained passwords */
899 * tty_read - read method for tty device files
914 struct tty_struct *tty = file_tty(file);
918 if (tty_paranoia_check(tty, inode, "tty_read"))
920 if (!tty || tty_io_error(tty))
926 ld = tty_ldisc_ref_wait(tty);
931 ret = iterate_tty_read(ld, tty, file, to);
935 tty_update_time(tty, false);
940 void tty_write_unlock(struct tty_struct *tty)
942 mutex_unlock(&tty->atomic_write_lock);
943 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
946 int tty_write_lock(struct tty_struct *tty, bool ndelay)
948 if (!mutex_trylock(&tty->atomic_write_lock)) {
951 if (mutex_lock_interruptible(&tty->atomic_write_lock))
961 static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty,
967 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
985 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
991 if (tty->write_cnt < chunk) {
1002 kvfree(tty->write_buf);
1003 tty->write_cnt = chunk;
1004 tty->write_buf = buf_chunk;
1012 if (copy_from_iter(tty->write_buf, size, from) != size)
1015 ret = ld->ops->write(tty, file, tty->write_buf, size);
1036 tty_update_time(tty, true);
1040 tty_write_unlock(tty);
1046 * tty_write_message - write a message to a certain tty, not just the console.
1047 * @tty: the destination tty_struct
1050 * This is used for messages that need to be redirected to a specific tty. We
1058 void tty_write_message(struct tty_struct *tty, char *msg)
1060 if (tty) {
1061 mutex_lock(&tty->atomic_write_lock);
1062 tty_lock(tty);
1063 if (tty->ops->write && tty->count > 0)
1064 tty->ops->write(tty, msg, strlen(msg));
1065 tty_unlock(tty);
1066 tty_write_unlock(tty);
1073 struct tty_struct *tty = file_tty(file);
1077 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
1079 if (!tty || !tty->ops->write || tty_io_error(tty))
1082 if (tty->ops->write_room == NULL)
1083 tty_err(tty, "missing write_room method\n");
1084 ld = tty_ldisc_ref_wait(tty);
1090 ret = iterate_tty_write(ld, tty, file, from);
1096 * tty_write - write method for tty device file
1100 * Write data to a tty device via the line discipline.
1104 * Writes to the tty driver are serialized by the atomic_write_lock
1124 * We know the redirected tty is just another tty, we can
1139 * @tty: the tty to send to
1142 * Send a high priority character to the tty even if stopped.
1146 int tty_send_xchar(struct tty_struct *tty, u8 ch)
1148 bool was_stopped = tty->flow.stopped;
1150 if (tty->ops->send_xchar) {
1151 down_read(&tty->termios_rwsem);
1152 tty->ops->send_xchar(tty, ch);
1153 up_read(&tty->termios_rwsem);
1157 if (tty_write_lock(tty, false) < 0)
1160 down_read(&tty->termios_rwsem);
1162 start_tty(tty);
1163 tty->ops->write(tty, &ch, 1);
1165 stop_tty(tty);
1166 up_read(&tty->termios_rwsem);
1167 tty_write_unlock(tty);
1173 * @driver: the tty driver in use
1186 /* ->name is initialized to "ttyp", but "tty" is expected */
1188 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1193 * tty_line_name - generate name for a tty
1194 * @driver: the tty driver in use
1213 * tty_driver_lookup_tty() - find an existing tty, if any
1214 * @driver: the driver for the tty
1218 * Return: the tty, if found. If not found, return %NULL or ERR_PTR() if the
1221 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
1226 struct tty_struct *tty;
1230 tty = ERR_PTR(-EIO);
1232 tty = driver->ops->lookup(driver, file, idx);
1236 tty = driver->ttys[idx];
1238 if (!IS_ERR(tty))
1239 tty_kref_get(tty);
1240 return tty;
1245 * @tty: the tty to set up
1247 * Initialise the termios structure for this tty. This runs under the
1250 void tty_init_termios(struct tty_struct *tty)
1253 int idx = tty->index;
1255 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1256 tty->termios = tty->driver->init_termios;
1259 tp = tty->driver->termios[idx];
1261 tty->termios = *tp;
1262 tty->termios.c_line = tty->driver->init_termios.c_line;
1264 tty->termios = tty->driver->init_termios;
1267 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1268 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
1273 * tty_standard_install - usual tty->ops->install
1274 * @driver: the driver for the tty
1275 * @tty: the tty
1277 * If the @driver overrides @tty->ops->install, it still can call this function
1280 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1282 tty_init_termios(tty);
1284 tty->count++;
1285 driver->ttys[tty->index] = tty;
1291 * tty_driver_install_tty() - install a tty entry in the driver
1292 * @driver: the driver for the tty
1293 * @tty: the tty
1295 * Install a tty object into the driver tables. The @tty->index field will be
1302 struct tty_struct *tty)
1304 return driver->ops->install ? driver->ops->install(driver, tty) :
1305 tty_standard_install(driver, tty);
1309 * tty_driver_remove_tty() - remove a tty from the driver tables
1310 * @driver: the driver for the tty
1311 * @tty: tty to remove
1313 * Remove a tty object from the driver tables. The tty->index field will be set
1318 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
1321 driver->ops->remove(driver, tty);
1323 driver->ttys[tty->index] = NULL;
1327 * tty_reopen() - fast re-open of an open tty
1328 * @tty: the tty to open
1335 static int tty_reopen(struct tty_struct *tty)
1337 struct tty_driver *driver = tty->driver;
1345 if (!tty->count)
1348 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1351 ld = tty_ldisc_ref_wait(tty);
1355 retval = tty_ldisc_lock(tty, 5 * HZ);
1359 if (!tty->ldisc)
1360 retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1361 tty_ldisc_unlock(tty);
1365 tty->count++;
1371 * tty_init_dev - initialise a tty device
1372 * @driver: tty driver we are opening a device on
1375 * Prepare a tty device. This may not be a "new" clean device but could also be
1380 * tty struct or driver itself going away.
1382 * On exit the tty device has the line discipline attached and a reference
1383 * count of 1. If a pair was created for pty/tty use and the other was a pty
1389 * common) case of reopening a tty.
1391 * Return: new tty structure
1395 struct tty_struct *tty;
1409 tty = alloc_tty_struct(driver, idx);
1410 if (!tty) {
1415 tty_lock(tty);
1416 retval = tty_driver_install_tty(driver, tty);
1420 if (!tty->port)
1421 tty->port = driver->ports[idx];
1423 if (WARN_RATELIMIT(!tty->port,
1424 "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n",
1425 __func__, tty->driver->name)) {
1430 retval = tty_ldisc_lock(tty, 5 * HZ);
1433 tty->port->itty = tty;
1440 retval = tty_ldisc_setup(tty, tty->link);
1443 tty_ldisc_unlock(tty);
1444 /* Return the tty locked so that it cannot vanish under the caller */
1445 return tty;
1448 tty_unlock(tty);
1449 free_tty_struct(tty);
1454 /* call the tty release_tty routine to clean out this slot */
1456 tty_ldisc_unlock(tty);
1457 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1460 tty_unlock(tty);
1461 release_tty(tty, idx);
1466 * tty_save_termios() - save tty termios data in driver table
1467 * @tty: tty whose termios data to save
1471 void tty_save_termios(struct tty_struct *tty)
1474 int idx = tty->index;
1477 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1481 tp = tty->driver->termios[idx];
1486 tty->driver->termios[idx] = tp;
1488 *tp = tty->termios;
1493 * tty_flush_works - flush all works of a tty/pty pair
1494 * @tty: tty device to flush works for (or either end of a pty pair)
1496 * Sync flush all works belonging to @tty (and the 'other' tty).
1498 static void tty_flush_works(struct tty_struct *tty)
1500 flush_work(&tty->SAK_work);
1501 flush_work(&tty->hangup_work);
1502 if (tty->link) {
1503 flush_work(&tty->link->SAK_work);
1504 flush_work(&tty->link->hangup_work);
1509 * release_one_tty - release tty structure memory
1510 * @work: work of tty we are obliterating
1512 * Releases memory associated with a tty structure, and clears out the
1525 struct tty_struct *tty =
1527 struct tty_driver *driver = tty->driver;
1530 if (tty->ops->cleanup)
1531 tty->ops->cleanup(tty);
1536 spin_lock(&tty->files_lock);
1537 list_del_init(&tty->tty_files);
1538 spin_unlock(&tty->files_lock);
1540 put_pid(tty->ctrl.pgrp);
1541 put_pid(tty->ctrl.session);
1542 free_tty_struct(tty);
1547 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1552 INIT_WORK(&tty->hangup_work, release_one_tty);
1553 schedule_work(&tty->hangup_work);
1557 * tty_kref_put - release a tty kref
1558 * @tty: tty device
1560 * Release a reference to the @tty device and if need be let the kref layer
1563 void tty_kref_put(struct tty_struct *tty)
1565 if (tty)
1566 kref_put(&tty->kref, queue_release_one_tty);
1571 * release_tty - release tty structure memory
1572 * @tty: tty device release
1573 * @idx: index of the tty device release
1575 * Release both @tty and a possible linked partner (think pty pair),
1583 static void release_tty(struct tty_struct *tty, int idx)
1586 WARN_ON(tty->index != idx);
1588 if (tty->ops->shutdown)
1589 tty->ops->shutdown(tty);
1590 tty_save_termios(tty);
1591 tty_driver_remove_tty(tty->driver, tty);
1592 if (tty->port)
1593 tty->port->itty = NULL;
1594 if (tty->link)
1595 tty->link->port->itty = NULL;
1596 if (tty->port)
1597 tty_buffer_cancel_work(tty->port);
1598 if (tty->link)
1599 tty_buffer_cancel_work(tty->link->port);
1601 tty_kref_put(tty->link);
1602 tty_kref_put(tty);
1606 * tty_release_checks - check a tty before real release
1607 * @tty: tty to check
1608 * @idx: index of the tty
1610 * Performs some paranoid checking before true release of the @tty. This is a
1613 static int tty_release_checks(struct tty_struct *tty, int idx)
1616 if (idx < 0 || idx >= tty->driver->num) {
1617 tty_debug(tty, "bad idx %d\n", idx);
1622 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1625 if (tty != tty->driver->ttys[idx]) {
1626 tty_debug(tty, "bad driver table[%d] = %p\n",
1627 idx, tty->driver->ttys[idx]);
1630 if (tty->driver->other) {
1631 struct tty_struct *o_tty = tty->link;
1633 if (o_tty != tty->driver->other->ttys[idx]) {
1634 tty_debug(tty, "bad other table[%d] = %p\n",
1635 idx, tty->driver->other->ttys[idx]);
1638 if (o_tty->link != tty) {
1639 tty_debug(tty, "bad link = %p\n", o_tty->link);
1648 * tty_kclose - closes tty opened by tty_kopen
1649 * @tty: tty device
1651 * Performs the final steps to release and free a tty device. It is the same as
1653 * @tty->port.
1655 void tty_kclose(struct tty_struct *tty)
1660 tty_ldisc_release(tty);
1662 /* Wait for pending work before tty destruction commences */
1663 tty_flush_works(tty);
1665 tty_debug_hangup(tty, "freeing structure\n");
1671 tty_port_set_kopened(tty->port, 0);
1672 release_tty(tty, tty->index);
1678 * tty_release_struct - release a tty struct
1679 * @tty: tty device
1680 * @idx: index of the tty
1682 * Performs the final steps to release and free a tty device. It is roughly the
1685 void tty_release_struct(struct tty_struct *tty, int idx)
1690 tty_ldisc_release(tty);
1692 /* Wait for pending work before tty destruction commmences */
1693 tty_flush_works(tty);
1695 tty_debug_hangup(tty, "freeing structure\n");
1701 release_tty(tty, idx);
1708 * @inode: inode of tty
1709 * @filp: file pointer for handle to tty
1711 * Called the last time each file handle is closed that references this tty.
1717 * Even releasing the tty structures is a tricky business. We have to be very
1726 struct tty_struct *tty = file_tty(filp);
1733 if (tty_paranoia_check(tty, inode, __func__))
1736 tty_lock(tty);
1737 check_tty_count(tty, __func__);
1741 idx = tty->index;
1742 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1743 tty->driver->subtype == PTY_TYPE_MASTER)
1744 o_tty = tty->link;
1746 if (tty_release_checks(tty, idx)) {
1747 tty_unlock(tty);
1751 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
1753 if (tty->ops->close)
1754 tty->ops->close(tty, filp);
1756 /* If tty is pty master, lock the slave pty (stable lock order) */
1760 * Sanity check: if tty->count is going to zero, there shouldn't be
1761 * any waiters on tty->read_wait or tty->write_wait. We test the
1763 * close. This ensures that we won't block while releasing the tty
1775 if (tty->count <= 1) {
1776 if (waitqueue_active(&tty->read_wait)) {
1777 wake_up_poll(&tty->read_wait, EPOLLIN);
1780 if (waitqueue_active(&tty->write_wait)) {
1781 wake_up_poll(&tty->write_wait, EPOLLOUT);
1800 tty_warn(tty, "read/write wait queue active!\n");
1811 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
1815 if (--tty->count < 0) {
1816 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
1817 tty->count = 0;
1821 * We've decremented tty->count, so we need to remove this file
1822 * descriptor off the tty->tty_files list; this serves two
1825 * associated with this tty.
1835 * processes that still think tty or o_tty is their controlling
1836 * tty.
1838 if (!tty->count) {
1840 session_clear_tty(tty->ctrl.session);
1847 final = !tty->count && !(o_tty && o_tty->count);
1850 tty_unlock(tty);
1852 /* At this point, the tty->count == 0 should ensure a dead tty
1859 tty_debug_hangup(tty, "final close\n");
1861 tty_release_struct(tty, idx);
1866 * tty_open_current_tty - get locked tty of current task
1868 * @filp: file pointer to tty
1869 * @return: locked tty of the current task iff @device is /dev/tty
1871 * Performs a re-open of the current task's controlling tty.
1878 struct tty_struct *tty;
1884 tty = get_current_tty();
1885 if (!tty)
1888 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1890 tty_lock(tty);
1891 tty_kref_put(tty); /* safe to drop the kref now */
1893 retval = tty_reopen(tty);
1895 tty_unlock(tty);
1896 tty = ERR_PTR(retval);
1898 return tty;
1902 * tty_lookup_driver - lookup a tty driver for a given device file
1904 * @filp: file pointer to tty
1955 struct tty_struct *tty;
1966 /* check whether we're reopening an existing tty */
1967 tty = tty_driver_lookup_tty(driver, NULL, index);
1968 if (IS_ERR(tty) || shared)
1971 if (tty) {
1973 tty_kref_put(tty);
1974 tty = ERR_PTR(-EBUSY);
1975 } else { /* tty_init_dev returns tty with the tty_lock held */
1976 tty = tty_init_dev(driver, index);
1977 if (IS_ERR(tty))
1979 tty_port_set_kopened(tty->port, 1);
1984 return tty;
1988 * tty_kopen_exclusive - open a tty device for kernel
1991 * Opens tty exclusively for kernel. Performs the driver lookup, makes sure
1992 * it's not already opened and performs the first-time tty initialization.
1995 * * concurrent first-time tty initialization
1996 * * concurrent tty driver removal w/ lookup
1997 * * concurrent tty removal from driver table
2008 * tty_kopen_shared - open a tty device for shared in-kernel use
2011 * Opens an already existing tty for in-kernel use. Compared to
2023 * tty_open_by_driver - open a tty device
2025 * @filp: file pointer to tty
2028 * first-time tty initialization.
2032 * * concurrent first-time tty initialization
2033 * * concurrent tty driver removal w/ lookup
2034 * * concurrent tty removal from driver table
2041 struct tty_struct *tty;
2053 /* check whether we're reopening an existing tty */
2054 tty = tty_driver_lookup_tty(driver, filp, index);
2055 if (IS_ERR(tty)) {
2060 if (tty) {
2061 if (tty_port_kopened(tty->port)) {
2062 tty_kref_put(tty);
2064 tty = ERR_PTR(-EBUSY);
2068 retval = tty_lock_interruptible(tty);
2069 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
2073 tty = ERR_PTR(retval);
2076 retval = tty_reopen(tty);
2078 tty_unlock(tty);
2079 tty = ERR_PTR(retval);
2082 tty = tty_init_dev(driver, index);
2087 return tty;
2091 * tty_open - open a tty device
2093 * @filp: file pointer to tty
2095 * tty_open() and tty_release() keep up the tty count that contains the number
2096 * of opens done on a tty. We cannot use the inode-count, as different inodes
2097 * might point to the same tty.
2101 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2107 * * %tty_mutex protects tty, tty_lookup_driver() and tty_init_dev().
2108 * * @tty->count should protect the rest.
2115 struct tty_struct *tty;
2127 tty = tty_open_current_tty(device, filp);
2128 if (!tty)
2129 tty = tty_open_by_driver(device, filp);
2131 if (IS_ERR(tty)) {
2133 retval = PTR_ERR(tty);
2140 tty_add_file(tty, filp);
2142 check_tty_count(tty, __func__);
2143 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
2145 if (tty->ops->open)
2146 retval = tty->ops->open(tty, filp);
2152 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
2154 tty_unlock(tty); /* need to call tty_release without BTM */
2170 clear_bit(TTY_HUPPED, &tty->flags);
2175 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2176 tty->driver->subtype == PTY_TYPE_MASTER);
2178 tty_open_proc_set_tty(filp, tty);
2179 tty_unlock(tty);
2185 * tty_poll - check tty status
2197 struct tty_struct *tty = file_tty(filp);
2201 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
2204 ld = tty_ldisc_ref_wait(tty);
2208 ret = ld->ops->poll(tty, filp, wait);
2215 struct tty_struct *tty = file_tty(filp);
2219 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
2228 retval = fasync_helper(fd, filp, on, &tty->fasync);
2236 spin_lock_irqsave(&tty->ctrl.lock, flags);
2237 if (tty->ctrl.pgrp) {
2238 pid = tty->ctrl.pgrp;
2245 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
2256 struct tty_struct *tty = file_tty(filp);
2259 tty_lock(tty);
2262 tty_unlock(tty);
2270 * @tty: tty to fake input into
2273 * Fake input to a tty device. Does the necessary locking and input management.
2279 * * current->signal->tty check is safe without locks
2281 static int tiocsti(struct tty_struct *tty, u8 __user *p)
2289 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2293 tty_audit_tiocsti(tty, ch);
2294 ld = tty_ldisc_ref_wait(tty);
2297 tty_buffer_lock_exclusive(tty->port);
2299 ld->ops->receive_buf(tty, &ch, NULL, 1);
2300 tty_buffer_unlock_exclusive(tty->port);
2307 * @tty: tty
2312 * Locking: @tty->winsize_mutex is taken to ensure the winsize data is
2315 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
2319 mutex_lock(&tty->winsize_mutex);
2320 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2321 mutex_unlock(&tty->winsize_mutex);
2328 * @tty: tty being resized
2334 int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2338 /* Lock the tty */
2339 mutex_lock(&tty->winsize_mutex);
2340 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2344 pgrp = tty_get_pgrp(tty);
2349 tty->winsize = *ws;
2351 mutex_unlock(&tty->winsize_mutex);
2358 * @tty: tty side of tty
2366 * Driver dependent. The default do_resize method takes the tty termios
2370 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2377 if (tty->ops->resize)
2378 return tty->ops->resize(tty, &tmp_ws);
2380 return tty_do_resize(tty, &tmp_ws);
2424 * @tty: tty device
2431 static int tiocsetd(struct tty_struct *tty, int __user *p)
2439 ret = tty_set_ldisc(tty, disc);
2446 * @tty: tty device
2452 * or the @tty is being hungup)
2454 static int tiocgetd(struct tty_struct *tty, int __user *p)
2459 ld = tty_ldisc_ref_wait(tty);
2469 * @tty: device to break on
2476 * @tty->atomic_write_lock serializes
2478 static int send_break(struct tty_struct *tty, unsigned int duration)
2482 if (tty->ops->break_ctl == NULL)
2485 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2486 return tty->ops->break_ctl(tty, duration);
2489 if (tty_write_lock(tty, false) < 0)
2492 retval = tty->ops->break_ctl(tty, -1);
2495 retval = tty->ops->break_ctl(tty, 0);
2500 tty_write_unlock(tty);
2510 * @tty: tty device
2512 * Obtain the modem status bits from the tty driver if the feature
2515 int tty_get_tiocm(struct tty_struct *tty)
2519 if (tty->ops->tiocmget)
2520 retval = tty->ops->tiocmget(tty);
2528 * @tty: tty device
2531 * Obtain the modem status bits from the tty driver if the feature is
2536 static int tty_tiocmget(struct tty_struct *tty, int __user *p)
2540 retval = tty_get_tiocm(tty);
2549 * @tty: tty device
2553 * Set the modem status bits from the tty driver if the feature
2558 static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
2564 if (tty->ops->tiocmset == NULL)
2585 return tty->ops->tiocmset(tty, set, clear);
2589 * tty_get_icount - get tty statistics
2590 * @tty: tty device
2593 * Gets a copy of the @tty's icount statistics.
2597 int tty_get_icount(struct tty_struct *tty,
2602 if (tty->ops->get_icount)
2603 return tty->ops->get_icount(tty, icount);
2609 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2614 retval = tty_get_icount(tty, &icount);
2623 static int tty_set_serial(struct tty_struct *tty, struct serial_struct *ss)
2633 if (!tty->ops->set_serial)
2636 return tty->ops->set_serial(tty, ss);
2639 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss)
2646 return tty_set_serial(tty, &v);
2649 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss)
2655 if (!tty->ops->get_serial)
2657 err = tty->ops->get_serial(tty, &v);
2667 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2669 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2670 tty->driver->subtype == PTY_TYPE_MASTER)
2671 tty = tty->link;
2672 return tty;
2680 struct tty_struct *tty = file_tty(file);
2686 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2689 real_tty = tty_pair_get_tty(tty);
2700 retval = tty_check_change(tty);
2704 tty_wait_until_sent(tty, 0);
2716 return tiocsti(tty, p);
2722 return real_tty != tty ? -EINVAL : tioccons(file);
2724 set_bit(TTY_EXCLUSIVE, &tty->flags);
2727 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2731 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2736 return tiocgetd(tty, p);
2738 return tiocsetd(tty, p);
2742 tty_vhangup(tty);
2754 if (tty->ops->break_ctl)
2755 return tty->ops->break_ctl(tty, -1);
2758 if (tty->ops->break_ctl)
2759 return tty->ops->break_ctl(tty, 0);
2767 return send_break(tty, 250);
2770 return send_break(tty, arg ? arg*100 : 250);
2773 return tty_tiocmget(tty, p);
2777 return tty_tiocmset(tty, cmd, p);
2779 return tty_tiocgicount(tty, p);
2784 /* flush tty buffer and allow ldisc to process ioctl */
2785 tty_buffer_flush(tty, NULL);
2790 return tty_tiocsserial(tty, p);
2792 return tty_tiocgserial(tty, p);
2795 return ptm_open_peer(file, tty, (int)arg);
2797 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2801 if (tty->ops->ioctl) {
2802 retval = tty->ops->ioctl(tty, cmd, arg);
2806 ld = tty_ldisc_ref_wait(tty);
2811 retval = ld->ops->ioctl(tty, cmd, arg);
2843 static int compat_tty_tiocsserial(struct tty_struct *tty,
2858 return tty_set_serial(tty, &v);
2861 static int compat_tty_tiocgserial(struct tty_struct *tty,
2871 if (!tty->ops->get_serial)
2873 err = tty->ops->get_serial(tty, &v);
2888 struct tty_struct *tty = file_tty(file);
2971 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2976 return compat_tty_tiocsserial(tty, compat_ptr(arg));
2978 return compat_tty_tiocgserial(tty, compat_ptr(arg));
2980 if (tty->ops->compat_ioctl) {
2981 retval = tty->ops->compat_ioctl(tty, cmd, arg);
2986 ld = tty_ldisc_ref_wait(tty);
2990 retval = ld->ops->compat_ioctl(tty, cmd, arg);
2992 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd),
3010 * tty when the user hits the "Secure Attention Key". Required for
3016 * with the current tty must be dead before the new getty is allowed
3026 void __do_SAK(struct tty_struct *tty)
3033 spin_lock_irqsave(&tty->ctrl.lock, flags);
3034 session = get_pid(tty->ctrl.session);
3035 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
3037 tty_ldisc_flush(tty);
3039 tty_driver_flush_buffer(tty);
3044 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
3049 /* Now kill any processes that happen to have the tty open */
3051 if (p->signal->tty == tty) {
3052 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
3059 i = iterate_fd(p->files, 0, this_tty, tty);
3061 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3074 struct tty_struct *tty =
3076 __do_SAK(tty);
3080 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3085 void do_SAK(struct tty_struct *tty)
3087 if (!tty)
3089 schedule_work(&tty->SAK_work);
3094 static struct device *tty_get_device(struct tty_struct *tty)
3096 dev_t devt = tty_devnum(tty);
3103 * alloc_tty_struct - allocate a new tty
3104 * @driver: driver which will handle the returned tty
3105 * @idx: minor of the tty
3107 * This subroutine allocates and initializes a tty structure.
3109 * Locking: none - @tty in question is not exposed at this point
3113 struct tty_struct *tty;
3115 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
3116 if (!tty)
3119 kref_init(&tty->kref);
3120 if (tty_ldisc_init(tty)) {
3121 kfree(tty);
3124 tty->ctrl.session = NULL;
3125 tty->ctrl.pgrp = NULL;
3126 mutex_init(&tty->legacy_mutex);
3127 mutex_init(&tty->throttle_mutex);
3128 init_rwsem(&tty->termios_rwsem);
3129 mutex_init(&tty->winsize_mutex);
3130 init_ldsem(&tty->ldisc_sem);
3131 init_waitqueue_head(&tty->write_wait);
3132 init_waitqueue_head(&tty->read_wait);
3133 INIT_WORK(&tty->hangup_work, do_tty_hangup);
3134 mutex_init(&tty->atomic_write_lock);
3135 spin_lock_init(&tty->ctrl.lock);
3136 spin_lock_init(&tty->flow.lock);
3137 spin_lock_init(&tty->files_lock);
3138 INIT_LIST_HEAD(&tty->tty_files);
3139 INIT_WORK(&tty->SAK_work, do_SAK_work);
3141 tty->driver = driver;
3142 tty->ops = driver->ops;
3143 tty->index = idx;
3144 tty_line_name(driver, idx, tty->name);
3145 tty->dev = tty_get_device(tty);
3147 return tty;
3151 * tty_put_char - write one character to a tty
3152 * @tty: tty
3155 * Write one byte to the @tty using the provided @tty->ops->put_char() method
3163 int tty_put_char(struct tty_struct *tty, u8 ch)
3165 if (tty->ops->put_char)
3166 return tty->ops->put_char(tty, ch);
3167 return tty->ops->write(tty, &ch, 1);
3189 * tty_register_device - register a tty device
3190 * @driver: the tty driver that describes the tty device
3191 * @index: the index in the tty driver for this tty device
3192 * @device: a struct device that is associated with this tty device.
3194 * for this tty device it can be set to NULL safely.
3196 * This call is required to be made to register an individual tty device
3197 * if the tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If
3198 * that bit is not set, this function should not be called by a tty
3203 * Return: A pointer to the struct device for this tty device (or
3220 * tty_register_device_attr - register a tty device
3221 * @driver: the tty driver that describes the tty device
3222 * @index: the index in the tty driver for this tty device
3223 * @device: a struct device that is associated with this tty device.
3225 * for this tty device it can be set to %NULL safely.
3229 * This call is required to be made to register an individual tty device if the
3230 * tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If that bit is
3231 * not set, this function should not be called by a tty driver.
3235 * Return: A pointer to the struct device for this tty device (or
3250 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3309 * tty_unregister_device - unregister a tty device
3310 * @driver: the tty driver that describes the tty device
3311 * @index: the index in the tty driver for this tty device
3313 * If a tty device is registered with a call to tty_register_device() then
3314 * this function must be called when the tty device is gone.
3329 * __tty_alloc_driver - allocate tty driver
3422 * tty_driver_kref_put - drop a reference to a tty driver
3434 * tty_register_driver - register a tty driver
3437 * Called by a tty driver to register itself.
3499 * tty_unregister_driver - unregister a tty driver
3502 * Called by a tty driver to unregister itself.
3514 dev_t tty_devnum(struct tty_struct *tty)
3516 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3536 .name = "tty",
3640 * Ok, now we can initialize the rest of the tty devices and can count
3645 register_sysctl_init("dev/tty", tty_table);
3648 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3649 panic("Couldn't register /dev/tty driver\n");
3650 device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");