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
375 * tty_find_polling_driver - find device of a polled tty
377 * @line: pointer to resulting tty line nr
379 * This routine returns a tty driver structure, given a name and the condition
380 * that the tty driver is capable of polled operation.
399 /* Search through the tty devices to look for a match */
458 struct tty_struct *tty = file_tty(file);
460 if (tty && tty->ops && tty->ops->show_fdinfo)
461 tty->ops->show_fdinfo(tty, m);
509 * @tty: terminal
511 * Internal and external helper for wakeups of tty. This function informs the
515 void tty_wakeup(struct tty_struct *tty)
519 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
520 ld = tty_ldisc_ref(tty);
523 ld->ops->write_wakeup(tty);
527 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
533 * @tty: tty device
538 static struct file *tty_release_redirect(struct tty_struct *tty)
543 if (redirect && file_tty(redirect) == tty) {
554 * @tty: tty device
563 * hangup and signal delivery. The tty object itself remains intact.
577 static void __tty_hangup(struct tty_struct *tty, int exit_session)
585 if (!tty)
588 f = tty_release_redirect(tty);
590 tty_lock(tty);
592 if (test_bit(TTY_HUPPED, &tty->flags)) {
593 tty_unlock(tty);
603 set_bit(TTY_HUPPING, &tty->flags);
605 /* inuse_filps is protected by the single tty lock,
609 check_tty_count(tty, "tty_hangup");
611 spin_lock(&tty->files_lock);
613 list_for_each_entry(priv, &tty->tty_files, list) {
623 spin_unlock(&tty->files_lock);
625 refs = tty_signal_session_leader(tty, exit_session);
628 tty_kref_put(tty);
630 tty_ldisc_hangup(tty, cons_filp != NULL);
632 spin_lock_irq(&tty->ctrl.lock);
633 clear_bit(TTY_THROTTLED, &tty->flags);
634 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
635 put_pid(tty->ctrl.session);
636 put_pid(tty->ctrl.pgrp);
637 tty->ctrl.session = NULL;
638 tty->ctrl.pgrp = NULL;
639 tty->ctrl.pktstatus = 0;
640 spin_unlock_irq(&tty->ctrl.lock);
645 * tty->count and state->count to go out of sync.
649 if (tty->ops->close)
651 tty->ops->close(tty, cons_filp);
652 } else if (tty->ops->hangup)
653 tty->ops->hangup(tty);
659 set_bit(TTY_HUPPED, &tty->flags);
660 clear_bit(TTY_HUPPING, &tty->flags);
661 tty_unlock(tty);
669 struct tty_struct *tty =
672 __tty_hangup(tty, 0);
677 * @tty: tty to hangup
679 * A carrier loss (virtual or otherwise) has occurred on @tty. Schedule a
682 void tty_hangup(struct tty_struct *tty)
684 tty_debug_hangup(tty, "hangup\n");
685 schedule_work(&tty->hangup_work);
691 * @tty: tty to hangup
697 void tty_vhangup(struct tty_struct *tty)
699 tty_debug_hangup(tty, "vhangup\n");
700 __tty_hangup(tty, 0);
708 * Perform a vhangup on the current controlling tty
712 struct tty_struct *tty;
714 tty = get_current_tty();
715 if (tty) {
716 tty_vhangup(tty);
717 tty_kref_put(tty);
723 * @tty: tty to hangup
731 void tty_vhangup_session(struct tty_struct *tty)
733 tty_debug_hangup(tty, "session hangup\n");
734 __tty_hangup(tty, 1);
738 * tty_hung_up_p - was tty hung up
739 * @filp: file pointer of tty
741 * Return: true if the tty has been subject to a vhangup or a carrier loss
749 void __stop_tty(struct tty_struct *tty)
751 if (tty->flow.stopped)
753 tty->flow.stopped = true;
754 if (tty->ops->stop)
755 tty->ops->stop(tty);
760 * @tty: tty to stop
767 * under the tty %atomic_write_lock but not always.
772 void stop_tty(struct tty_struct *tty)
776 spin_lock_irqsave(&tty->flow.lock, flags);
777 __stop_tty(tty);
778 spin_unlock_irqrestore(&tty->flow.lock, flags);
782 void __start_tty(struct tty_struct *tty)
784 if (!tty->flow.stopped || tty->flow.tco_stopped)
786 tty->flow.stopped = false;
787 if (tty->ops->start)
788 tty->ops->start(tty);
789 tty_wakeup(tty);
794 * @tty: tty to start
796 * Start a tty that has been stopped if at all possible. If @tty was previously
803 void start_tty(struct tty_struct *tty)
807 spin_lock_irqsave(&tty->flow.lock, flags);
808 __start_tty(tty);
809 spin_unlock_irqrestore(&tty->flow.lock, flags);
813 static void tty_update_time(struct tty_struct *tty, bool mtime)
818 spin_lock(&tty->files_lock);
819 list_for_each_entry(priv, &tty->tty_files, list) {
826 * the time of the tty device, otherwise it could be construded as a
827 * security leak to let userspace know the exact timing of the tty.
836 spin_unlock(&tty->files_lock);
850 static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
862 size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
898 /* We always clear tty buffer in case they contained passwords */
905 * tty_read - read method for tty device files
920 struct tty_struct *tty = file_tty(file);
924 if (tty_paranoia_check(tty, inode, "tty_read"))
926 if (!tty || tty_io_error(tty))
932 ld = tty_ldisc_ref_wait(tty);
937 ret = iterate_tty_read(ld, tty, file, to);
941 tty_update_time(tty, false);
946 void tty_write_unlock(struct tty_struct *tty)
948 mutex_unlock(&tty->atomic_write_lock);
949 wake_up_interruptible_poll(&tty->write_wait, EPOLLOUT);
952 int tty_write_lock(struct tty_struct *tty, bool ndelay)
954 if (!mutex_trylock(&tty->atomic_write_lock)) {
957 if (mutex_lock_interruptible(&tty->atomic_write_lock))
967 static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty,
973 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
991 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
997 if (tty->write_cnt < chunk) {
1008 kvfree(tty->write_buf);
1009 tty->write_cnt = chunk;
1010 tty->write_buf = buf_chunk;
1018 if (copy_from_iter(tty->write_buf, size, from) != size)
1021 ret = ld->ops->write(tty, file, tty->write_buf, size);
1042 tty_update_time(tty, true);
1046 tty_write_unlock(tty);
1052 * tty_write_message - write a message to a certain tty, not just the console.
1053 * @tty: the destination tty_struct
1056 * This is used for messages that need to be redirected to a specific tty. We
1064 void tty_write_message(struct tty_struct *tty, char *msg)
1066 if (tty) {
1067 mutex_lock(&tty->atomic_write_lock);
1068 tty_lock(tty);
1069 if (tty->ops->write && tty->count > 0)
1070 tty->ops->write(tty, msg, strlen(msg));
1071 tty_unlock(tty);
1072 tty_write_unlock(tty);
1079 struct tty_struct *tty = file_tty(file);
1083 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
1085 if (!tty || !tty->ops->write || tty_io_error(tty))
1088 if (tty->ops->write_room == NULL)
1089 tty_err(tty, "missing write_room method\n");
1090 ld = tty_ldisc_ref_wait(tty);
1096 ret = iterate_tty_write(ld, tty, file, from);
1102 * tty_write - write method for tty device file
1106 * Write data to a tty device via the line discipline.
1110 * Writes to the tty driver are serialized by the atomic_write_lock
1130 * We know the redirected tty is just another tty, we can
1145 * @tty: the tty to send to
1148 * Send a high priority character to the tty even if stopped.
1152 int tty_send_xchar(struct tty_struct *tty, u8 ch)
1154 bool was_stopped = tty->flow.stopped;
1156 if (tty->ops->send_xchar) {
1157 down_read(&tty->termios_rwsem);
1158 tty->ops->send_xchar(tty, ch);
1159 up_read(&tty->termios_rwsem);
1163 if (tty_write_lock(tty, false) < 0)
1166 down_read(&tty->termios_rwsem);
1168 start_tty(tty);
1169 tty->ops->write(tty, &ch, 1);
1171 stop_tty(tty);
1172 up_read(&tty->termios_rwsem);
1173 tty_write_unlock(tty);
1179 * @driver: the tty driver in use
1192 /* ->name is initialized to "ttyp", but "tty" is expected */
1194 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1199 * tty_line_name - generate name for a tty
1200 * @driver: the tty driver in use
1219 * tty_driver_lookup_tty() - find an existing tty, if any
1220 * @driver: the driver for the tty
1224 * Return: the tty, if found. If not found, return %NULL or ERR_PTR() if the
1227 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
1232 struct tty_struct *tty;
1236 tty = ERR_PTR(-EIO);
1238 tty = driver->ops->lookup(driver, file, idx);
1242 tty = driver->ttys[idx];
1244 if (!IS_ERR(tty))
1245 tty_kref_get(tty);
1246 return tty;
1251 * @tty: the tty to set up
1253 * Initialise the termios structure for this tty. This runs under the
1256 void tty_init_termios(struct tty_struct *tty)
1259 int idx = tty->index;
1261 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1262 tty->termios = tty->driver->init_termios;
1265 tp = tty->driver->termios[idx];
1267 tty->termios = *tp;
1268 tty->termios.c_line = tty->driver->init_termios.c_line;
1270 tty->termios = tty->driver->init_termios;
1273 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1274 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
1279 * tty_standard_install - usual tty->ops->install
1280 * @driver: the driver for the tty
1281 * @tty: the tty
1283 * If the @driver overrides @tty->ops->install, it still can call this function
1286 int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1288 tty_init_termios(tty);
1290 tty->count++;
1291 driver->ttys[tty->index] = tty;
1297 * tty_driver_install_tty() - install a tty entry in the driver
1298 * @driver: the driver for the tty
1299 * @tty: the tty
1301 * Install a tty object into the driver tables. The @tty->index field will be
1308 struct tty_struct *tty)
1310 return driver->ops->install ? driver->ops->install(driver, tty) :
1311 tty_standard_install(driver, tty);
1315 * tty_driver_remove_tty() - remove a tty from the driver tables
1316 * @driver: the driver for the tty
1317 * @tty: tty to remove
1319 * Remove a tty object from the driver tables. The tty->index field will be set
1324 static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
1327 driver->ops->remove(driver, tty);
1329 driver->ttys[tty->index] = NULL;
1333 * tty_reopen() - fast re-open of an open tty
1334 * @tty: the tty to open
1341 static int tty_reopen(struct tty_struct *tty)
1343 struct tty_driver *driver = tty->driver;
1351 if (!tty->count)
1354 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1357 ld = tty_ldisc_ref_wait(tty);
1361 retval = tty_ldisc_lock(tty, 5 * HZ);
1365 if (!tty->ldisc)
1366 retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1367 tty_ldisc_unlock(tty);
1371 tty->count++;
1377 * tty_init_dev - initialise a tty device
1378 * @driver: tty driver we are opening a device on
1381 * Prepare a tty device. This may not be a "new" clean device but could also be
1386 * tty struct or driver itself going away.
1388 * On exit the tty device has the line discipline attached and a reference
1389 * count of 1. If a pair was created for pty/tty use and the other was a pty
1395 * common) case of reopening a tty.
1397 * Return: new tty structure
1401 struct tty_struct *tty;
1415 tty = alloc_tty_struct(driver, idx);
1416 if (!tty) {
1421 tty_lock(tty);
1422 retval = tty_driver_install_tty(driver, tty);
1426 if (!tty->port)
1427 tty->port = driver->ports[idx];
1429 if (WARN_RATELIMIT(!tty->port,
1430 "%s: %s driver does not set tty->port. This would crash the kernel. Fix the driver!\n",
1431 __func__, tty->driver->name)) {
1436 retval = tty_ldisc_lock(tty, 5 * HZ);
1439 tty->port->itty = tty;
1446 retval = tty_ldisc_setup(tty, tty->link);
1449 tty_ldisc_unlock(tty);
1450 /* Return the tty locked so that it cannot vanish under the caller */
1451 return tty;
1454 tty_unlock(tty);
1455 free_tty_struct(tty);
1460 /* call the tty release_tty routine to clean out this slot */
1462 tty_ldisc_unlock(tty);
1463 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1466 tty_unlock(tty);
1467 release_tty(tty, idx);
1472 * tty_save_termios() - save tty termios data in driver table
1473 * @tty: tty whose termios data to save
1477 void tty_save_termios(struct tty_struct *tty)
1480 int idx = tty->index;
1483 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1487 tp = tty->driver->termios[idx];
1492 tty->driver->termios[idx] = tp;
1494 *tp = tty->termios;
1499 * tty_flush_works - flush all works of a tty/pty pair
1500 * @tty: tty device to flush works for (or either end of a pty pair)
1502 * Sync flush all works belonging to @tty (and the 'other' tty).
1504 static void tty_flush_works(struct tty_struct *tty)
1506 flush_work(&tty->SAK_work);
1507 flush_work(&tty->hangup_work);
1508 if (tty->link) {
1509 flush_work(&tty->link->SAK_work);
1510 flush_work(&tty->link->hangup_work);
1515 * release_one_tty - release tty structure memory
1516 * @work: work of tty we are obliterating
1518 * Releases memory associated with a tty structure, and clears out the
1531 struct tty_struct *tty =
1533 struct tty_driver *driver = tty->driver;
1536 if (tty->ops->cleanup)
1537 tty->ops->cleanup(tty);
1542 spin_lock(&tty->files_lock);
1543 list_del_init(&tty->tty_files);
1544 spin_unlock(&tty->files_lock);
1546 put_pid(tty->ctrl.pgrp);
1547 put_pid(tty->ctrl.session);
1548 free_tty_struct(tty);
1553 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1558 INIT_WORK(&tty->hangup_work, release_one_tty);
1559 schedule_work(&tty->hangup_work);
1563 * tty_kref_put - release a tty kref
1564 * @tty: tty device
1566 * Release a reference to the @tty device and if need be let the kref layer
1569 void tty_kref_put(struct tty_struct *tty)
1571 if (tty)
1572 kref_put(&tty->kref, queue_release_one_tty);
1577 * release_tty - release tty structure memory
1578 * @tty: tty device release
1579 * @idx: index of the tty device release
1581 * Release both @tty and a possible linked partner (think pty pair),
1589 static void release_tty(struct tty_struct *tty, int idx)
1592 WARN_ON(tty->index != idx);
1594 if (tty->ops->shutdown)
1595 tty->ops->shutdown(tty);
1596 tty_save_termios(tty);
1597 tty_driver_remove_tty(tty->driver, tty);
1598 if (tty->port)
1599 tty->port->itty = NULL;
1600 if (tty->link)
1601 tty->link->port->itty = NULL;
1602 if (tty->port)
1603 tty_buffer_cancel_work(tty->port);
1604 if (tty->link)
1605 tty_buffer_cancel_work(tty->link->port);
1607 tty_kref_put(tty->link);
1608 tty_kref_put(tty);
1612 * tty_release_checks - check a tty before real release
1613 * @tty: tty to check
1614 * @idx: index of the tty
1616 * Performs some paranoid checking before true release of the @tty. This is a
1619 static int tty_release_checks(struct tty_struct *tty, int idx)
1622 if (idx < 0 || idx >= tty->driver->num) {
1623 tty_debug(tty, "bad idx %d\n", idx);
1628 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1631 if (tty != tty->driver->ttys[idx]) {
1632 tty_debug(tty, "bad driver table[%d] = %p\n",
1633 idx, tty->driver->ttys[idx]);
1636 if (tty->driver->other) {
1637 struct tty_struct *o_tty = tty->link;
1639 if (o_tty != tty->driver->other->ttys[idx]) {
1640 tty_debug(tty, "bad other table[%d] = %p\n",
1641 idx, tty->driver->other->ttys[idx]);
1644 if (o_tty->link != tty) {
1645 tty_debug(tty, "bad link = %p\n", o_tty->link);
1654 * tty_kclose - closes tty opened by tty_kopen
1655 * @tty: tty device
1657 * Performs the final steps to release and free a tty device. It is the same as
1659 * @tty->port.
1661 void tty_kclose(struct tty_struct *tty)
1666 tty_ldisc_release(tty);
1668 /* Wait for pending work before tty destruction commences */
1669 tty_flush_works(tty);
1671 tty_debug_hangup(tty, "freeing structure\n");
1677 tty_port_set_kopened(tty->port, 0);
1678 release_tty(tty, tty->index);
1684 * tty_release_struct - release a tty struct
1685 * @tty: tty device
1686 * @idx: index of the tty
1688 * Performs the final steps to release and free a tty device. It is roughly the
1691 void tty_release_struct(struct tty_struct *tty, int idx)
1696 tty_ldisc_release(tty);
1698 /* Wait for pending work before tty destruction commmences */
1699 tty_flush_works(tty);
1701 tty_debug_hangup(tty, "freeing structure\n");
1707 release_tty(tty, idx);
1714 * @inode: inode of tty
1715 * @filp: file pointer for handle to tty
1717 * Called the last time each file handle is closed that references this tty.
1723 * Even releasing the tty structures is a tricky business. We have to be very
1732 struct tty_struct *tty = file_tty(filp);
1739 if (tty_paranoia_check(tty, inode, __func__))
1742 tty_lock(tty);
1743 check_tty_count(tty, __func__);
1747 idx = tty->index;
1748 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1749 tty->driver->subtype == PTY_TYPE_MASTER)
1750 o_tty = tty->link;
1752 if (tty_release_checks(tty, idx)) {
1753 tty_unlock(tty);
1757 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
1759 if (tty->ops->close)
1760 tty->ops->close(tty, filp);
1762 /* If tty is pty master, lock the slave pty (stable lock order) */
1766 * Sanity check: if tty->count is going to zero, there shouldn't be
1767 * any waiters on tty->read_wait or tty->write_wait. We test the
1769 * close. This ensures that we won't block while releasing the tty
1781 if (tty->count <= 1) {
1782 if (waitqueue_active(&tty->read_wait)) {
1783 wake_up_poll(&tty->read_wait, EPOLLIN);
1786 if (waitqueue_active(&tty->write_wait)) {
1787 wake_up_poll(&tty->write_wait, EPOLLOUT);
1806 tty_warn(tty, "read/write wait queue active!\n");
1817 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
1821 if (--tty->count < 0) {
1822 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
1823 tty->count = 0;
1827 * We've decremented tty->count, so we need to remove this file
1828 * descriptor off the tty->tty_files list; this serves two
1831 * associated with this tty.
1841 * processes that still think tty or o_tty is their controlling
1842 * tty.
1844 if (!tty->count) {
1846 session_clear_tty(tty->ctrl.session);
1853 final = !tty->count && !(o_tty && o_tty->count);
1856 tty_unlock(tty);
1858 /* At this point, the tty->count == 0 should ensure a dead tty
1865 tty_debug_hangup(tty, "final close\n");
1867 tty_release_struct(tty, idx);
1872 * tty_open_current_tty - get locked tty of current task
1874 * @filp: file pointer to tty
1875 * @return: locked tty of the current task iff @device is /dev/tty
1877 * Performs a re-open of the current task's controlling tty.
1884 struct tty_struct *tty;
1890 tty = get_current_tty();
1891 if (!tty)
1894 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1896 tty_lock(tty);
1897 tty_kref_put(tty); /* safe to drop the kref now */
1899 retval = tty_reopen(tty);
1901 tty_unlock(tty);
1902 tty = ERR_PTR(retval);
1904 return tty;
1908 * tty_lookup_driver - lookup a tty driver for a given device file
1910 * @filp: file pointer to tty
1961 struct tty_struct *tty;
1972 /* check whether we're reopening an existing tty */
1973 tty = tty_driver_lookup_tty(driver, NULL, index);
1974 if (IS_ERR(tty) || shared)
1977 if (tty) {
1979 tty_kref_put(tty);
1980 tty = ERR_PTR(-EBUSY);
1981 } else { /* tty_init_dev returns tty with the tty_lock held */
1982 tty = tty_init_dev(driver, index);
1983 if (IS_ERR(tty))
1985 tty_port_set_kopened(tty->port, 1);
1990 return tty;
1994 * tty_kopen_exclusive - open a tty device for kernel
1997 * Opens tty exclusively for kernel. Performs the driver lookup, makes sure
1998 * it's not already opened and performs the first-time tty initialization.
2001 * * concurrent first-time tty initialization
2002 * * concurrent tty driver removal w/ lookup
2003 * * concurrent tty removal from driver table
2014 * tty_kopen_shared - open a tty device for shared in-kernel use
2017 * Opens an already existing tty for in-kernel use. Compared to
2029 * tty_open_by_driver - open a tty device
2031 * @filp: file pointer to tty
2034 * first-time tty initialization.
2038 * * concurrent first-time tty initialization
2039 * * concurrent tty driver removal w/ lookup
2040 * * concurrent tty removal from driver table
2047 struct tty_struct *tty;
2059 /* check whether we're reopening an existing tty */
2060 tty = tty_driver_lookup_tty(driver, filp, index);
2061 if (IS_ERR(tty)) {
2066 if (tty) {
2067 if (tty_port_kopened(tty->port)) {
2068 tty_kref_put(tty);
2070 tty = ERR_PTR(-EBUSY);
2074 retval = tty_lock_interruptible(tty);
2075 tty_kref_put(tty); /* drop kref from tty_driver_lookup_tty() */
2079 tty = ERR_PTR(retval);
2082 retval = tty_reopen(tty);
2084 tty_unlock(tty);
2085 tty = ERR_PTR(retval);
2088 tty = tty_init_dev(driver, index);
2093 return tty;
2097 * tty_open - open a tty device
2099 * @filp: file pointer to tty
2101 * tty_open() and tty_release() keep up the tty count that contains the number
2102 * of opens done on a tty. We cannot use the inode-count, as different inodes
2103 * might point to the same tty.
2107 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2113 * * %tty_mutex protects tty, tty_lookup_driver() and tty_init_dev().
2114 * * @tty->count should protect the rest.
2121 struct tty_struct *tty;
2133 tty = tty_open_current_tty(device, filp);
2134 if (!tty)
2135 tty = tty_open_by_driver(device, filp);
2137 if (IS_ERR(tty)) {
2139 retval = PTR_ERR(tty);
2146 tty_add_file(tty, filp);
2148 check_tty_count(tty, __func__);
2149 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
2151 if (tty->ops->open)
2152 retval = tty->ops->open(tty, filp);
2158 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
2160 tty_unlock(tty); /* need to call tty_release without BTM */
2176 clear_bit(TTY_HUPPED, &tty->flags);
2181 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2182 tty->driver->subtype == PTY_TYPE_MASTER);
2184 tty_open_proc_set_tty(filp, tty);
2185 tty_unlock(tty);
2191 * tty_poll - check tty status
2203 struct tty_struct *tty = file_tty(filp);
2207 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
2210 ld = tty_ldisc_ref_wait(tty);
2214 ret = ld->ops->poll(tty, filp, wait);
2221 struct tty_struct *tty = file_tty(filp);
2225 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)
2634 if (!tty->ops->set_serial)
2637 return tty->ops->set_serial(tty, ss);
2640 static int tty_tiocsserial(struct tty_struct *tty, struct serial_struct __user *ss)
2647 return tty_set_serial(tty, &v);
2650 static int tty_tiocgserial(struct tty_struct *tty, struct serial_struct __user *ss)
2656 if (!tty->ops->get_serial)
2658 err = tty->ops->get_serial(tty, &v);
2668 static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2670 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2671 tty->driver->subtype == PTY_TYPE_MASTER)
2672 tty = tty->link;
2673 return tty;
2681 struct tty_struct *tty = file_tty(file);
2687 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2690 real_tty = tty_pair_get_tty(tty);
2701 retval = tty_check_change(tty);
2705 tty_wait_until_sent(tty, 0);
2717 return tiocsti(tty, p);
2723 return real_tty != tty ? -EINVAL : tioccons(file);
2725 set_bit(TTY_EXCLUSIVE, &tty->flags);
2728 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2732 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2737 return tiocgetd(tty, p);
2739 return tiocsetd(tty, p);
2743 tty_vhangup(tty);
2755 if (tty->ops->break_ctl)
2756 return tty->ops->break_ctl(tty, -1);
2759 if (tty->ops->break_ctl)
2760 return tty->ops->break_ctl(tty, 0);
2768 return send_break(tty, 250);
2771 return send_break(tty, arg ? arg*100 : 250);
2774 return tty_tiocmget(tty, p);
2778 return tty_tiocmset(tty, cmd, p);
2780 return tty_tiocgicount(tty, p);
2785 /* flush tty buffer and allow ldisc to process ioctl */
2786 tty_buffer_flush(tty, NULL);
2791 return tty_tiocsserial(tty, p);
2793 return tty_tiocgserial(tty, p);
2796 return ptm_open_peer(file, tty, (int)arg);
2798 retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2802 if (tty->ops->ioctl) {
2803 retval = tty->ops->ioctl(tty, cmd, arg);
2807 ld = tty_ldisc_ref_wait(tty);
2812 retval = ld->ops->ioctl(tty, cmd, arg);
2844 static int compat_tty_tiocsserial(struct tty_struct *tty,
2859 return tty_set_serial(tty, &v);
2862 static int compat_tty_tiocgserial(struct tty_struct *tty,
2872 if (!tty->ops->get_serial)
2874 err = tty->ops->get_serial(tty, &v);
2889 struct tty_struct *tty = file_tty(file);
2972 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2977 return compat_tty_tiocsserial(tty, compat_ptr(arg));
2979 return compat_tty_tiocgserial(tty, compat_ptr(arg));
2981 if (tty->ops->compat_ioctl) {
2982 retval = tty->ops->compat_ioctl(tty, cmd, arg);
2987 ld = tty_ldisc_ref_wait(tty);
2991 retval = ld->ops->compat_ioctl(tty, cmd, arg);
2993 retval = ld->ops->ioctl(tty, (unsigned long)compat_ptr(cmd),
3011 * tty when the user hits the "Secure Attention Key". Required for
3017 * with the current tty must be dead before the new getty is allowed
3027 void __do_SAK(struct tty_struct *tty)
3034 spin_lock_irqsave(&tty->ctrl.lock, flags);
3035 session = get_pid(tty->ctrl.session);
3036 spin_unlock_irqrestore(&tty->ctrl.lock, flags);
3038 tty_ldisc_flush(tty);
3040 tty_driver_flush_buffer(tty);
3045 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
3050 /* Now kill any processes that happen to have the tty open */
3052 if (p->signal->tty == tty) {
3053 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
3060 i = iterate_fd(p->files, 0, this_tty, tty);
3062 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3075 struct tty_struct *tty =
3077 __do_SAK(tty);
3081 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3086 void do_SAK(struct tty_struct *tty)
3088 if (!tty)
3090 schedule_work(&tty->SAK_work);
3095 static struct device *tty_get_device(struct tty_struct *tty)
3097 dev_t devt = tty_devnum(tty);
3104 * alloc_tty_struct - allocate a new tty
3105 * @driver: driver which will handle the returned tty
3106 * @idx: minor of the tty
3108 * This subroutine allocates and initializes a tty structure.
3110 * Locking: none - @tty in question is not exposed at this point
3114 struct tty_struct *tty;
3116 tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
3117 if (!tty)
3120 kref_init(&tty->kref);
3121 if (tty_ldisc_init(tty)) {
3122 kfree(tty);
3125 tty->ctrl.session = NULL;
3126 tty->ctrl.pgrp = NULL;
3127 mutex_init(&tty->legacy_mutex);
3128 mutex_init(&tty->throttle_mutex);
3129 init_rwsem(&tty->termios_rwsem);
3130 mutex_init(&tty->winsize_mutex);
3131 init_ldsem(&tty->ldisc_sem);
3132 init_waitqueue_head(&tty->write_wait);
3133 init_waitqueue_head(&tty->read_wait);
3134 INIT_WORK(&tty->hangup_work, do_tty_hangup);
3135 mutex_init(&tty->atomic_write_lock);
3136 spin_lock_init(&tty->ctrl.lock);
3137 spin_lock_init(&tty->flow.lock);
3138 spin_lock_init(&tty->files_lock);
3139 INIT_LIST_HEAD(&tty->tty_files);
3140 INIT_WORK(&tty->SAK_work, do_SAK_work);
3142 tty->driver = driver;
3143 tty->ops = driver->ops;
3144 tty->index = idx;
3145 tty_line_name(driver, idx, tty->name);
3146 tty->dev = tty_get_device(tty);
3148 return tty;
3152 * tty_put_char - write one character to a tty
3153 * @tty: tty
3156 * Write one byte to the @tty using the provided @tty->ops->put_char() method
3164 int tty_put_char(struct tty_struct *tty, u8 ch)
3166 if (tty->ops->put_char)
3167 return tty->ops->put_char(tty, ch);
3168 return tty->ops->write(tty, &ch, 1);
3190 * tty_register_device - register a tty device
3191 * @driver: the tty driver that describes the tty device
3192 * @index: the index in the tty driver for this tty device
3193 * @device: a struct device that is associated with this tty device.
3195 * for this tty device it can be set to NULL safely.
3197 * This call is required to be made to register an individual tty device
3198 * if the tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If
3199 * that bit is not set, this function should not be called by a tty
3204 * Return: A pointer to the struct device for this tty device (or
3221 * tty_register_device_attr - register a tty device
3222 * @driver: the tty driver that describes the tty device
3223 * @index: the index in the tty driver for this tty device
3224 * @device: a struct device that is associated with this tty device.
3226 * for this tty device it can be set to %NULL safely.
3230 * This call is required to be made to register an individual tty device if the
3231 * tty driver's flags have the %TTY_DRIVER_DYNAMIC_DEV bit set. If that bit is
3232 * not set, this function should not be called by a tty driver.
3236 * Return: A pointer to the struct device for this tty device (or
3251 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3310 * tty_unregister_device - unregister a tty device
3311 * @driver: the tty driver that describes the tty device
3312 * @index: the index in the tty driver for this tty device
3314 * If a tty device is registered with a call to tty_register_device() then
3315 * this function must be called when the tty device is gone.
3330 * __tty_alloc_driver - allocate tty driver
3423 * tty_driver_kref_put - drop a reference to a tty driver
3435 * tty_register_driver - register a tty driver
3438 * Called by a tty driver to register itself.
3500 * tty_unregister_driver - unregister a tty driver
3503 * Called by a tty driver to unregister itself.
3515 dev_t tty_devnum(struct tty_struct *tty)
3517 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3537 .name = "tty",
3641 * Ok, now we can initialize the rest of the tty devices and can count
3646 register_sysctl_init("dev/tty", tty_table);
3649 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3650 panic("Couldn't register /dev/tty driver\n");
3651 device_create(&tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");