Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * proc_tty.c -- handles /proc/tty |
| 3 | * |
| 4 | * Copyright 1997, Theodore Ts'o |
| 5 | */ |
| 6 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 7 | #include <linux/uaccess.h> |
Alexey Dobriyan | b640a89 | 2008-04-29 01:01:58 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/init.h> |
| 10 | #include <linux/errno.h> |
| 11 | #include <linux/time.h> |
| 12 | #include <linux/proc_fs.h> |
| 13 | #include <linux/stat.h> |
| 14 | #include <linux/tty.h> |
| 15 | #include <linux/seq_file.h> |
| 16 | #include <linux/bitops.h> |
| 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
| 19 | * The /proc/tty directory inodes... |
| 20 | */ |
Alexey Dobriyan | 849d20a | 2014-08-08 14:21:31 -0700 | [diff] [blame] | 21 | static struct proc_dir_entry *proc_tty_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * This is the handler for /proc/tty/drivers |
| 25 | */ |
| 26 | static void show_tty_range(struct seq_file *m, struct tty_driver *p, |
| 27 | dev_t from, int num) |
| 28 | { |
| 29 | seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown"); |
| 30 | seq_printf(m, "/dev/%-8s ", p->name); |
| 31 | if (p->num > 1) { |
| 32 | seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from), |
| 33 | MINOR(from) + num - 1); |
| 34 | } else { |
| 35 | seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from)); |
| 36 | } |
| 37 | switch (p->type) { |
| 38 | case TTY_DRIVER_TYPE_SYSTEM: |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 39 | seq_puts(m, "system"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | if (p->subtype == SYSTEM_TYPE_TTY) |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 41 | seq_puts(m, ":/dev/tty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | else if (p->subtype == SYSTEM_TYPE_SYSCONS) |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 43 | seq_puts(m, ":console"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | else if (p->subtype == SYSTEM_TYPE_CONSOLE) |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 45 | seq_puts(m, ":vtmaster"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | break; |
| 47 | case TTY_DRIVER_TYPE_CONSOLE: |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 48 | seq_puts(m, "console"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | break; |
| 50 | case TTY_DRIVER_TYPE_SERIAL: |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 51 | seq_puts(m, "serial"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | break; |
| 53 | case TTY_DRIVER_TYPE_PTY: |
| 54 | if (p->subtype == PTY_TYPE_MASTER) |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 55 | seq_puts(m, "pty:master"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | else if (p->subtype == PTY_TYPE_SLAVE) |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 57 | seq_puts(m, "pty:slave"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | else |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 59 | seq_puts(m, "pty"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | break; |
| 61 | default: |
| 62 | seq_printf(m, "type:%d.%d", p->type, p->subtype); |
| 63 | } |
| 64 | seq_putc(m, '\n'); |
| 65 | } |
| 66 | |
| 67 | static int show_tty_driver(struct seq_file *m, void *v) |
| 68 | { |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 69 | struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | dev_t from = MKDEV(p->major, p->minor_start); |
| 71 | dev_t to = from + p->num; |
| 72 | |
| 73 | if (&p->tty_drivers == tty_drivers.next) { |
| 74 | /* pseudo-drivers first */ |
| 75 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty"); |
| 76 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0); |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 77 | seq_puts(m, "system:/dev/tty\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console"); |
| 79 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1); |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 80 | seq_puts(m, "system:console\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #ifdef CONFIG_UNIX98_PTYS |
| 82 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx"); |
| 83 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2); |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 84 | seq_puts(m, "system\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #endif |
| 86 | #ifdef CONFIG_VT |
| 87 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0"); |
| 88 | seq_printf(m, "%3d %7d ", TTY_MAJOR, 0); |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 89 | seq_puts(m, "system:vtmaster\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #endif |
| 91 | } |
| 92 | |
| 93 | while (MAJOR(from) < MAJOR(to)) { |
| 94 | dev_t next = MKDEV(MAJOR(from)+1, 0); |
| 95 | show_tty_range(m, p, from, next - from); |
| 96 | from = next; |
| 97 | } |
| 98 | if (from != to) |
| 99 | show_tty_range(m, p, from, to - from); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* iterator */ |
| 104 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 105 | { |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 106 | mutex_lock(&tty_mutex); |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 107 | return seq_list_start(&tty_drivers, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void *t_next(struct seq_file *m, void *v, loff_t *pos) |
| 111 | { |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 112 | return seq_list_next(v, &tty_drivers, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void t_stop(struct seq_file *m, void *v) |
| 116 | { |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 117 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 120 | static const struct seq_operations tty_drivers_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | .start = t_start, |
| 122 | .next = t_next, |
| 123 | .stop = t_stop, |
| 124 | .show = show_tty_driver |
| 125 | }; |
| 126 | |
| 127 | static int tty_drivers_open(struct inode *inode, struct file *file) |
| 128 | { |
| 129 | return seq_open(file, &tty_drivers_op); |
| 130 | } |
| 131 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 132 | static const struct file_operations proc_tty_drivers_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .open = tty_drivers_open, |
| 134 | .read = seq_read, |
| 135 | .llseek = seq_lseek, |
| 136 | .release = seq_release, |
| 137 | }; |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | /* |
| 140 | * This function is called by tty_register_driver() to handle |
| 141 | * registering the driver's /proc handler into /proc/tty/driver/<foo> |
| 142 | */ |
| 143 | void proc_tty_register_driver(struct tty_driver *driver) |
| 144 | { |
| 145 | struct proc_dir_entry *ent; |
| 146 | |
Alexey Dobriyan | 0f043a8 | 2009-03-31 15:19:25 -0700 | [diff] [blame] | 147 | if (!driver->driver_name || driver->proc_entry || |
| 148 | !driver->ops->proc_fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return; |
| 150 | |
Alexey Dobriyan | 0f043a8 | 2009-03-31 15:19:25 -0700 | [diff] [blame] | 151 | ent = proc_create_data(driver->driver_name, 0, proc_tty_driver, |
| 152 | driver->ops->proc_fops, driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | driver->proc_entry = ent; |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * This function is called by tty_unregister_driver() |
| 158 | */ |
| 159 | void proc_tty_unregister_driver(struct tty_driver *driver) |
| 160 | { |
| 161 | struct proc_dir_entry *ent; |
| 162 | |
| 163 | ent = driver->proc_entry; |
| 164 | if (!ent) |
| 165 | return; |
| 166 | |
| 167 | remove_proc_entry(driver->driver_name, proc_tty_driver); |
| 168 | |
| 169 | driver->proc_entry = NULL; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Called by proc_root_init() to initialize the /proc/tty subtree |
| 174 | */ |
| 175 | void __init proc_tty_init(void) |
| 176 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (!proc_mkdir("tty", NULL)) |
| 178 | return; |
Alexey Dobriyan | 849d20a | 2014-08-08 14:21:31 -0700 | [diff] [blame] | 179 | proc_mkdir("tty/ldisc", NULL); /* Preserved: it's userspace visible */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* |
| 181 | * /proc/tty/driver/serial reveals the exact character counts for |
| 182 | * serial links which is just too easy to abuse for inferring |
| 183 | * password lengths and inter-keystroke timings during password |
| 184 | * entry. |
| 185 | */ |
Alexey Dobriyan | b640a89 | 2008-04-29 01:01:58 -0700 | [diff] [blame] | 186 | proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL); |
| 187 | proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops); |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 188 | proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |