67 #include <linux/types.h>
69 #include <linux/errno.h>
70 #include <linux/signal.h>
71 #include <linux/fcntl.h>
72 #include <linux/sched.h>
74 #include <linux/tty.h>
82 #include <linux/ctype.h>
85 #include <linux/string.h>
86 #include <linux/slab.h>
87 #include <linux/poll.h>
90 #include <linux/module.h>
91 #include <linux/device.h>
92 #include <linux/wait.h>
93 #include <linux/bitops.h>
96 #include <linux/serial.h>
108 #undef TTY_DEBUG_HANGUP
110 #define TTY_PARANOIA_CHECK 1
111 #define CHECK_TTY_COUNT 1
140 static ssize_t tty_read(
struct file *,
char __user *,
size_t, loff_t *);
141 static ssize_t tty_write(
struct file *,
const char __user *,
size_t, loff_t *);
145 static int tty_open(
struct inode *,
struct file *);
151 #define tty_compat_ioctl NULL
153 static int __tty_fasync(
int fd,
struct file *filp,
int on);
154 static int tty_fasync(
int fd,
struct file *filp,
int on);
190 tty->
magic = 0xDEADDEAD;
251 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
278 #ifdef TTY_PARANOIA_CHECK
281 "null TTY for (%d:%d) in %s\n",
282 imajor(inode), iminor(inode), routine);
287 "bad magic number for tty struct (%d:%d) in %s\n",
288 imajor(inode), iminor(inode), routine);
297 #ifdef CHECK_TTY_COUNT
301 spin_lock(&tty_files_lock);
305 spin_unlock(&tty_files_lock);
310 if (tty->
count != count) {
312 "!= #fd's(%d) in %s\n",
339 *index = device - base;
340 return tty_driver_kref_get(p);
345 #ifdef CONFIG_CONSOLE_POLL
363 for (str = name; *
str; str++)
364 if ((*str >=
'0' && *str <=
'9') || *str ==
',')
383 if (tty_line >= 0 && tty_line < p->
num && p->
ops &&
384 p->
ops->poll_init && !p->
ops->poll_init(p, tty_line, stp)) {
385 res = tty_driver_kref_get(p);
413 if (
current->signal->tty != tty)
424 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
437 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
443 static ssize_t hung_up_tty_read(
struct file *file,
char __user *
buf,
444 size_t count, loff_t *ppos)
449 static ssize_t hung_up_tty_write(
struct file *file,
const char __user *
buf,
450 size_t count, loff_t *ppos)
456 static unsigned int hung_up_tty_poll(
struct file *filp,
poll_table *
wait)
461 static long hung_up_tty_ioctl(
struct file *file,
unsigned int cmd,
467 static long hung_up_tty_compat_ioctl(
struct file *file,
468 unsigned int cmd,
unsigned long arg)
482 .fasync = tty_fasync,
494 .fasync = tty_fasync,
499 .read = hung_up_tty_read,
500 .write = hung_up_tty_write,
501 .poll = hung_up_tty_poll,
502 .unlocked_ioctl = hung_up_tty_ioctl,
503 .compat_ioctl = hung_up_tty_compat_ioctl,
508 static struct file *redirect;
526 if (ld->
ops->write_wakeup)
527 ld->
ops->write_wakeup(tty);
560 struct file *cons_filp =
NULL;
561 struct file *filp, *
f =
NULL;
564 int closecount = 0,
n;
572 spin_lock(&redirect_lock);
573 if (redirect && file_tty(redirect) == tty) {
577 spin_unlock(&redirect_lock);
587 check_tty_count(tty,
"tty_hangup");
595 if (filp->
f_op->write != tty_write)
598 __tty_fasync(-1, filp, 0);
599 filp->
f_op = &hung_up_tty_fops;
612 spin_lock_irq(&p->
sighand->siglock);
613 if (p->
signal->tty == tty) {
620 spin_unlock_irq(&p->
sighand->siglock);
629 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
630 spin_unlock_irq(&p->
sighand->siglock);
644 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
658 for (
n = 0;
n < closecount;
n++)
659 tty->
ops->close(tty, cons_filp);
660 }
else if (tty->
ops->hangup)
661 (tty->
ops->hangup)(tty);
696 #ifdef TTY_DEBUG_HANGUP
716 #ifdef TTY_DEBUG_HANGUP
754 return (filp->
f_op == &hung_up_tty_fops);
759 static void session_clear_tty(
struct pid *
session)
801 struct pid *tty_pgrp = get_pid(tty->
pgrp);
813 }
else if (on_exit) {
814 struct pid *old_pgrp;
815 spin_lock_irq(&
current->sighand->siglock);
816 old_pgrp =
current->signal->tty_old_pgrp;
818 spin_unlock_irq(&
current->sighand->siglock);
827 spin_lock_irq(&
current->sighand->siglock);
830 spin_unlock_irq(&
current->sighand->siglock);
840 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
843 #ifdef TTY_DEBUG_HANGUP
851 session_clear_tty(task_session(
current));
893 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
897 if (tty->
link && tty->
link->packet) {
902 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
904 (tty->
ops->stop)(tty);
927 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
931 if (tty->
link && tty->
link->packet) {
936 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
938 (tty->
ops->start)(tty);
960 static ssize_t tty_read(
struct file *file,
char __user *
buf,
size_t count,
977 i = (ld->
ops->read)(tty, file, buf, count);
1009 static inline ssize_t do_tty_write(
1013 const char __user *buf,
1047 unsigned char *buf_chunk;
1084 struct inode *inode = file->
f_path.dentry->d_inode;
1137 static ssize_t tty_write(
struct file *file,
const char __user *buf,
1138 size_t count, loff_t *ppos)
1140 struct inode *inode = file->
f_path.dentry->d_inode;
1147 if (!tty || !tty->
ops->write ||
1151 if (tty->
ops->write_room ==
NULL)
1155 if (!ld->
ops->write)
1158 ret = do_tty_write(ld->
ops->write, tty, file, buf, count);
1164 size_t count, loff_t *ppos)
1166 struct file *p =
NULL;
1168 spin_lock(&redirect_lock);
1170 p = get_file(redirect);
1171 spin_unlock(&redirect_lock);
1179 return tty_write(file, buf, count, ppos);
1182 static char ptychar[] =
"pqrstuvwxyzabcde";
1201 ptychar[i >> 4 & 0xf], i & 0xf);
1235 struct inode *inode,
int idx)
1237 if (driver->
ops->lookup)
1238 return driver->
ops->lookup(driver, inode, idx);
1254 int idx = tty->
index;
1257 tty->termios = tty->
driver->init_termios;
1264 tty->termios = tty->
driver->init_termios;
1279 tty_driver_kref_get(driver);
1298 static int tty_driver_install_tty(
struct tty_driver *driver,
1301 return driver->
ops->install ? driver->
ops->install(driver, tty) :
1317 if (driver->
ops->remove)
1318 driver->
ops->remove(driver, tty);
1332 static int tty_reopen(
struct tty_struct *tty)
1398 if (!try_module_get(driver->
owner))
1404 goto err_module_put;
1409 retval = tty_driver_install_tty(driver, tty);
1411 goto err_deinit_tty;
1417 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1418 __func__, tty->
driver->name);
1427 goto err_release_tty;
1436 module_put(driver->
owner);
1437 return ERR_PTR(retval);
1443 "clearing slot %d\n", idx);
1444 release_tty(tty, idx);
1445 return ERR_PTR(retval);
1451 int idx = tty->
index;
1462 pr_warn(
"tty: no memory to save termios state.\n");
1493 if (tty->
ops->cleanup)
1494 tty->
ops->cleanup(tty);
1498 module_put(driver->
owner);
1500 spin_lock(&tty_files_lock);
1502 spin_unlock(&tty_files_lock);
1509 static void queue_release_one_tty(
struct kref *
kref)
1530 kref_put(&tty->
kref, queue_release_one_tty);
1546 static void release_tty(
struct tty_struct *tty,
int idx)
1550 WARN_ON(!mutex_is_locked(&tty_mutex));
1551 if (tty->
ops->shutdown)
1552 tty->
ops->shutdown(tty);
1573 #ifdef TTY_PARANOIA_CHECK
1574 if (idx < 0 || idx >= tty->
driver->num) {
1576 __func__, tty->
name);
1584 if (tty != tty->
driver->ttys[idx]) {
1586 __func__, idx, tty->
name);
1589 if (tty->
driver->other) {
1590 if (o_tty != tty->
driver->other->ttys[idx]) {
1592 __func__, idx, tty->
name);
1595 if (o_tty->
link != tty) {
1627 int pty_master, tty_closing, o_tty_closing, do_sleep;
1636 check_tty_count(tty, __func__);
1638 __tty_fasync(-1, filp, 0);
1647 if (tty_release_checks(tty, o_tty, idx)) {
1652 #ifdef TTY_DEBUG_HANGUP
1657 if (tty->
ops->close)
1658 tty->
ops->close(tty, filp);
1684 tty_closing = tty->
count <= 1;
1685 o_tty_closing = o_tty &&
1686 (o_tty->
count <= (pty_master ? 1 : 0));
1690 if (waitqueue_active(&tty->
read_wait)) {
1699 if (o_tty_closing) {
1700 if (waitqueue_active(&o_tty->
read_wait)) {
1728 if (--o_tty->
count < 0) {
1734 if (--tty->
count < 0) {
1768 if (tty_closing || o_tty_closing) {
1770 session_clear_tty(tty->
session);
1772 session_clear_tty(o_tty->
session);
1782 if (!tty_closing || (o_tty && !o_tty_closing))
1785 #ifdef TTY_DEBUG_HANGUP
1799 release_tty(tty, idx);
1820 static struct tty_struct *tty_open_current_tty(
dev_t device,
struct file *filp)
1829 return ERR_PTR(-
ENXIO);
1852 static struct tty_driver *tty_lookup_driver(
dev_t device,
struct file *filp,
1853 int *noctty,
int *index)
1861 driver = tty_driver_kref_get(console_driver);
1869 if (console_driver) {
1870 driver = tty_driver_kref_get(console_driver);
1881 driver = get_tty_driver(device, index);
1913 static int tty_open(
struct inode *inode,
struct file *filp)
1920 unsigned saved_flags = filp->
f_flags;
1935 tty = tty_open_current_tty(device, filp);
1937 retval = PTR_ERR(tty);
1940 driver = tty_lookup_driver(device, filp, &noctty, &index);
1941 if (IS_ERR(driver)) {
1942 retval = PTR_ERR(driver);
1947 tty = tty_driver_lookup_tty(driver, inode, index);
1949 retval = PTR_ERR(tty);
1956 retval = tty_reopen(tty);
1959 tty = ERR_PTR(retval);
1968 retval = PTR_ERR(tty);
1974 check_tty_count(tty, __func__);
1978 #ifdef TTY_DEBUG_HANGUP
1982 retval = tty->
ops->open(tty, filp);
1992 #ifdef TTY_DEBUG_HANGUP
2008 if (filp->
f_op == &hung_up_tty_fops)
2009 filp->
f_op = &tty_fops;
2017 spin_lock_irq(&
current->sighand->siglock);
2023 spin_unlock_irq(&
current->sighand->siglock);
2030 if (!IS_ERR_OR_NULL(driver))
2051 static unsigned int tty_poll(
struct file *filp,
poll_table *
wait)
2062 ret = (ld->
ops->poll)(tty, filp, wait);
2067 static int __tty_fasync(
int fd,
struct file *filp,
int on)
2070 unsigned long flags;
2094 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
2108 static int tty_fasync(
int fd,
struct file *filp,
int on)
2114 retval = __tty_fasync(fd, filp, on);
2137 static int tiocsti(
struct tty_struct *tty,
char __user *p)
2148 ld->
ops->receive_buf(tty, &ch, &mbz, 1);
2188 unsigned long flags;
2197 pgrp = get_pid(tty->
pgrp);
2198 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
2231 if (tty->
ops->resize)
2232 return tty->
ops->resize(tty, &tmp_ws);
2246 static int tioccons(
struct file *file)
2252 spin_lock(&redirect_lock);
2255 spin_unlock(&redirect_lock);
2260 spin_lock(&redirect_lock);
2262 spin_unlock(&redirect_lock);
2265 redirect = get_file(file);
2266 spin_unlock(&redirect_lock);
2282 static int fionbio(
struct file *file,
int __user *p)
2289 spin_lock(&file->
f_lock);
2294 spin_unlock(&file->
f_lock);
2312 static int tiocsctty(
struct tty_struct *tty,
int arg)
2338 session_clear_tty(tty->
session);
2361 unsigned long flags;
2365 pgrp = get_pid(tty->
pgrp);
2366 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
2392 if (tty == real_tty &&
current->signal->tty != real_tty)
2417 unsigned long flags;
2424 (
current->signal->tty != real_tty) ||
2442 real_tty->
pgrp = get_pid(pgrp);
2443 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
2467 if (tty == real_tty &&
current->signal->tty != real_tty)
2484 static int tiocsetd(
struct tty_struct *tty,
int __user *p)
2514 if (tty->
ops->break_ctl ==
NULL)
2518 retval = tty->
ops->break_ctl(tty, duration);
2523 retval = tty->
ops->break_ctl(tty, -1);
2528 retval = tty->
ops->break_ctl(tty, 0);
2549 static int tty_tiocmget(
struct tty_struct *tty,
int __user *p)
2553 if (tty->
ops->tiocmget) {
2554 retval = tty->
ops->tiocmget(tty);
2574 static int tty_tiocmset(
struct tty_struct *tty,
unsigned int cmd,
2580 if (tty->
ops->tiocmset ==
NULL)
2601 return tty->
ops->tiocmset(tty,
set, clear);
2604 static int tty_tiocgicount(
struct tty_struct *tty,
void __user *arg)
2608 memset(&icount, 0,
sizeof(icount));
2609 if (tty->
ops->get_icount)
2610 retval = tty->
ops->get_icount(tty, &icount);
2639 long tty_ioctl(
struct file *file,
unsigned int cmd,
unsigned long arg)
2646 struct inode *inode = file->f_dentry->d_inode;
2678 return tiocsti(tty, p);
2680 return tiocgwinsz(real_tty, p);
2682 return tiocswinsz(real_tty, p);
2684 return real_tty != tty ? -
EINVAL : tioccons(file);
2686 return fionbio(file, p);
2694 if (
current->signal->tty != tty)
2699 return tiocsctty(tty, arg);
2701 return tiocgpgrp(tty, real_tty, p);
2703 return tiocspgrp(tty, real_tty, p);
2705 return tiocgsid(tty, real_tty, p);
2709 return tiocsetd(tty, p);
2717 unsigned int ret = new_encode_dev(
tty_devnum(real_tty));
2724 if (tty->
ops->break_ctl)
2725 return tty->
ops->break_ctl(tty, -1);
2728 if (tty->
ops->break_ctl)
2729 return tty->
ops->break_ctl(tty, 0);
2737 return send_break(tty, 250);
2740 return send_break(tty, arg ? arg*100 : 250);
2743 return tty_tiocmget(tty, p);
2747 return tty_tiocmset(tty, cmd, p);
2749 retval = tty_tiocgicount(tty, p);
2764 if (tty->
ops->ioctl) {
2765 retval = (tty->
ops->ioctl)(tty, cmd, arg);
2771 if (ld->
ops->ioctl) {
2772 retval = ld->
ops->ioctl(tty, file, cmd, arg);
2780 #ifdef CONFIG_COMPAT
2784 struct inode *inode = file->f_dentry->d_inode;
2792 if (tty->
ops->compat_ioctl) {
2793 retval = (tty->
ops->compat_ioctl)(tty, cmd, arg);
2799 if (ld->
ops->compat_ioctl)
2800 retval = ld->
ops->compat_ioctl(tty, file, cmd, arg);
2809 static int this_tty(
const void *
t,
struct file *file,
unsigned fd)
2813 return file_tty(file) != t ? 0 : fd + 1;
2841 struct pid *session;
2856 " (%s): task_session(p)==tty->session\n",
2857 task_pid_nr(p), p->
comm);
2864 if (p->
signal->tty == tty) {
2866 " (%s): task_session(p)==tty->session\n",
2867 task_pid_nr(p), p->
comm);
2875 " (%s): fd#%d opened to the tty\n",
2876 task_pid_nr(p), p->
comm, i - 1);
2907 static int dev_match_devt(
struct device *
dev,
void *
data)
2914 static struct device *tty_get_device(
struct tty_struct *tty)
2935 kref_init(&tty->
kref);
2960 tty_line_name(driver, idx, tty->
name);
2961 tty->
dev = tty_get_device(tty);
2992 if (tty->
ops->put_char)
2993 return tty->
ops->put_char(tty, ch);
2994 return tty->
ops->write(tty, &ch, 1);
3001 unsigned int index,
unsigned int count)
3029 struct device *device)
3035 static void tty_device_create_release(
struct device *
dev)
3037 pr_debug(
"device: '%s': %s\n", dev_name(dev), __func__);
3062 unsigned index,
struct device *device,
3068 struct device *dev =
NULL;
3072 if (index >= driver->
num) {
3079 pty_line_name(driver, index, name);
3081 tty_line_name(driver, index, name);
3084 retval = tty_cdev_add(driver, devt, index, 1);
3099 dev->
release = tty_device_create_release;
3114 return ERR_PTR(retval);
3148 unsigned long flags)
3151 unsigned int cdevs = 1;
3161 kref_init(&driver->
kref);
3163 driver->
num = lines;
3168 driver->
ttys = kcalloc(lines,
sizeof(*driver->
ttys),
3179 driver->
ports = kcalloc(lines,
sizeof(*driver->
ports),
3181 if (!driver->
ports) {
3189 if (!driver->
cdevs) {
3200 return ERR_PTR(err);
3204 static void destruct_tty_driver(
struct kref *
kref)
3216 for (i = 0; i < driver->
num; i++) {
3238 kref_put(&driver->
kref, destruct_tty_driver);
3265 if (!driver->
major) {
3280 error = tty_cdev_add(driver, dev, 0, driver->
num);
3282 goto err_unreg_char;
3290 for (i = 0; i < driver->
num; i++) {
3294 goto err_unreg_devs;
3303 for (i--; i >= 0; i--)
3324 if (driver->refcount)
3345 unsigned long flags;
3350 spin_unlock_irqrestore(&p->
sighand->siglock, flags);
3359 unsigned long flags;
3364 tty->
pgrp = get_pid(task_pgrp(tsk));
3365 spin_unlock_irqrestore(&tty->
ctrl_lock, flags);
3366 tty->
session = get_pid(task_session(tsk));
3373 tsk->
signal->tty = tty_kref_get(tty);
3379 spin_lock_irq(&tsk->
sighand->siglock);
3380 __proc_set_tty(tsk, tty);
3381 spin_unlock_irq(&tsk->
sighand->siglock);
3387 unsigned long flags;
3390 tty = tty_kref_get(
current->signal->tty);
3391 spin_unlock_irqrestore(&
current->sighand->siglock, flags);
3425 static char *tty_devnode(
struct device *dev,
umode_t *
mode)
3435 static int __init tty_class_init(
void)
3438 if (IS_ERR(tty_class))
3439 return PTR_ERR(tty_class);
3440 tty_class->
devnode = tty_devnode;
3447 static struct cdev tty_cdev, console_cdev;
3449 static ssize_t show_cons_active(
struct device *dev,
3470 count +=
sprintf(buf + count,
"%s%d%c",
3471 cs[i]->name, cs[i]->index, i ?
' ':
'\n');
3478 static struct device *consdev;
3495 panic(
"Couldn't register /dev/tty driver\n");
3498 cdev_init(&console_cdev, &console_fops);
3501 panic(
"Couldn't register /dev/console driver\n");
3504 if (IS_ERR(consdev))