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 | |
| 7 | #include <asm/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> |
Dr. Werner Fink | f4a3e0b | 2010-09-22 12:45:40 +0200 | [diff] [blame] | 15 | #include <linux/tty_driver.h> |
| 16 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
Dr. Werner Fink | f4a3e0b | 2010-09-22 12:45:40 +0200 | [diff] [blame] | 18 | #include <linux/fdtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/bitops.h> |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | /* |
| 22 | * The /proc/tty directory inodes... |
| 23 | */ |
| 24 | static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver; |
| 25 | |
| 26 | /* |
| 27 | * This is the handler for /proc/tty/drivers |
| 28 | */ |
| 29 | static void show_tty_range(struct seq_file *m, struct tty_driver *p, |
| 30 | dev_t from, int num) |
| 31 | { |
| 32 | seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown"); |
| 33 | seq_printf(m, "/dev/%-8s ", p->name); |
| 34 | if (p->num > 1) { |
| 35 | seq_printf(m, "%3d %d-%d ", MAJOR(from), MINOR(from), |
| 36 | MINOR(from) + num - 1); |
| 37 | } else { |
| 38 | seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from)); |
| 39 | } |
| 40 | switch (p->type) { |
| 41 | case TTY_DRIVER_TYPE_SYSTEM: |
| 42 | seq_printf(m, "system"); |
| 43 | if (p->subtype == SYSTEM_TYPE_TTY) |
| 44 | seq_printf(m, ":/dev/tty"); |
| 45 | else if (p->subtype == SYSTEM_TYPE_SYSCONS) |
| 46 | seq_printf(m, ":console"); |
| 47 | else if (p->subtype == SYSTEM_TYPE_CONSOLE) |
| 48 | seq_printf(m, ":vtmaster"); |
| 49 | break; |
| 50 | case TTY_DRIVER_TYPE_CONSOLE: |
| 51 | seq_printf(m, "console"); |
| 52 | break; |
| 53 | case TTY_DRIVER_TYPE_SERIAL: |
| 54 | seq_printf(m, "serial"); |
| 55 | break; |
| 56 | case TTY_DRIVER_TYPE_PTY: |
| 57 | if (p->subtype == PTY_TYPE_MASTER) |
| 58 | seq_printf(m, "pty:master"); |
| 59 | else if (p->subtype == PTY_TYPE_SLAVE) |
| 60 | seq_printf(m, "pty:slave"); |
| 61 | else |
| 62 | seq_printf(m, "pty"); |
| 63 | break; |
| 64 | default: |
| 65 | seq_printf(m, "type:%d.%d", p->type, p->subtype); |
| 66 | } |
| 67 | seq_putc(m, '\n'); |
| 68 | } |
| 69 | |
| 70 | static int show_tty_driver(struct seq_file *m, void *v) |
| 71 | { |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 72 | struct tty_driver *p = list_entry(v, struct tty_driver, tty_drivers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | dev_t from = MKDEV(p->major, p->minor_start); |
| 74 | dev_t to = from + p->num; |
| 75 | |
| 76 | if (&p->tty_drivers == tty_drivers.next) { |
| 77 | /* pseudo-drivers first */ |
| 78 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty"); |
| 79 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0); |
| 80 | seq_printf(m, "system:/dev/tty\n"); |
| 81 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console"); |
| 82 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1); |
| 83 | seq_printf(m, "system:console\n"); |
| 84 | #ifdef CONFIG_UNIX98_PTYS |
| 85 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx"); |
| 86 | seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2); |
| 87 | seq_printf(m, "system\n"); |
| 88 | #endif |
| 89 | #ifdef CONFIG_VT |
| 90 | seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0"); |
| 91 | seq_printf(m, "%3d %7d ", TTY_MAJOR, 0); |
| 92 | seq_printf(m, "system:vtmaster\n"); |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | while (MAJOR(from) < MAJOR(to)) { |
| 97 | dev_t next = MKDEV(MAJOR(from)+1, 0); |
| 98 | show_tty_range(m, p, from, next - from); |
| 99 | from = next; |
| 100 | } |
| 101 | if (from != to) |
| 102 | show_tty_range(m, p, from, to - from); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* iterator */ |
| 107 | static void *t_start(struct seq_file *m, loff_t *pos) |
| 108 | { |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 109 | mutex_lock(&tty_mutex); |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 110 | return seq_list_start(&tty_drivers, *pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void *t_next(struct seq_file *m, void *v, loff_t *pos) |
| 114 | { |
Pavel Emelianov | 25216b0 | 2007-07-15 23:39:54 -0700 | [diff] [blame] | 115 | return seq_list_next(v, &tty_drivers, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void t_stop(struct seq_file *m, void *v) |
| 119 | { |
Alexey Dobriyan | ca509f6 | 2007-05-08 00:27:12 -0700 | [diff] [blame] | 120 | mutex_unlock(&tty_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Jan Engelhardt | 03a4482 | 2008-02-08 04:21:19 -0800 | [diff] [blame] | 123 | static const struct seq_operations tty_drivers_op = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .start = t_start, |
| 125 | .next = t_next, |
| 126 | .stop = t_stop, |
| 127 | .show = show_tty_driver |
| 128 | }; |
| 129 | |
| 130 | static int tty_drivers_open(struct inode *inode, struct file *file) |
| 131 | { |
| 132 | return seq_open(file, &tty_drivers_op); |
| 133 | } |
| 134 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 135 | static const struct file_operations proc_tty_drivers_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .open = tty_drivers_open, |
| 137 | .read = seq_read, |
| 138 | .llseek = seq_lseek, |
| 139 | .release = seq_release, |
| 140 | }; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* |
Dr. Werner Fink | f4a3e0b | 2010-09-22 12:45:40 +0200 | [diff] [blame] | 143 | * The device ID of file descriptor 0 of the current reading |
| 144 | * task if a character device... |
| 145 | */ |
| 146 | static dev_t current_dev; |
| 147 | |
| 148 | /* |
| 149 | * This is the handler for /proc/tty/consoles |
| 150 | */ |
| 151 | static int show_console_dev(struct seq_file *m, void *v) |
| 152 | { |
| 153 | const struct tty_driver *driver; |
| 154 | struct console *con; |
| 155 | int index, len; |
| 156 | char flags[10]; |
| 157 | dev_t dev; |
| 158 | |
| 159 | if (v == SEQ_START_TOKEN) |
| 160 | return 0; |
| 161 | con = (struct console *)v; |
| 162 | if (!con) |
| 163 | return 0; |
| 164 | driver = con->device(con, &index); |
| 165 | if (!driver) |
| 166 | return 0; |
| 167 | dev = MKDEV(driver->major, driver->minor_start) + index; |
| 168 | |
| 169 | index = 0; |
| 170 | if (con->flags & CON_ENABLED) |
| 171 | flags[index++] = 'E'; |
| 172 | if (con->flags & CON_CONSDEV) |
| 173 | flags[index++] = 'C'; |
| 174 | if (con->flags & CON_BOOT) |
| 175 | flags[index++] = 'B'; |
| 176 | if (con->flags & CON_PRINTBUFFER) |
| 177 | flags[index++] = 'p'; |
| 178 | if (con->flags & CON_BRL) |
| 179 | flags[index++] = 'b'; |
| 180 | if (con->flags & CON_ANYTIME) |
| 181 | flags[index++] = 'a'; |
| 182 | if (current_dev == dev) |
| 183 | flags[index++] = '*'; |
| 184 | flags[index] = 0; |
| 185 | |
| 186 | seq_printf(m, "%s%d%n", con->name, con->index, &len); |
| 187 | len = 21 - len; |
| 188 | if (len < 1) |
| 189 | len = 1; |
| 190 | seq_printf(m, "%*c", len, ' '); |
| 191 | seq_printf(m, "%c%c%c (%s)%n", con->read ? 'R' : '-', |
| 192 | con->write ? 'W' : '-', con->unblank ? 'U' : '-', |
| 193 | flags, &len); |
| 194 | len = 13 - len; |
| 195 | if (len < 1) |
| 196 | len = 1; |
| 197 | seq_printf(m, "%*c%4d:%d\n", len, ' ', MAJOR(dev), MINOR(dev)); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /* iterator for consoles */ |
| 203 | static void *c_start(struct seq_file *m, loff_t *pos) |
| 204 | { |
| 205 | struct console *con; |
| 206 | loff_t off = 0; |
| 207 | |
| 208 | if (*pos == 0) |
| 209 | return SEQ_START_TOKEN; |
| 210 | |
| 211 | acquire_console_sem(); |
| 212 | for (con = console_drivers; con; con = con->next) { |
| 213 | if (!con->device) |
| 214 | continue; |
| 215 | if (++off == *pos) |
| 216 | break; |
| 217 | } |
| 218 | release_console_sem(); |
| 219 | |
| 220 | return con; |
| 221 | } |
| 222 | |
| 223 | static void *c_next(struct seq_file *m, void *v, loff_t *pos) |
| 224 | { |
| 225 | struct console *con; |
| 226 | |
| 227 | acquire_console_sem(); |
| 228 | if (v == SEQ_START_TOKEN) |
| 229 | con = console_drivers; |
| 230 | else |
| 231 | con = ((struct console *)v)->next; |
| 232 | for (; con; con = con->next) { |
| 233 | if (!con->device) |
| 234 | continue; |
| 235 | ++*pos; |
| 236 | break; |
| 237 | } |
| 238 | release_console_sem(); |
| 239 | |
| 240 | return con; |
| 241 | } |
| 242 | |
| 243 | static void c_stop(struct seq_file *m, void *v) |
| 244 | { |
| 245 | } |
| 246 | |
| 247 | static const struct seq_operations tty_consoles_op = { |
| 248 | .start = c_start, |
| 249 | .next = c_next, |
| 250 | .stop = c_stop, |
| 251 | .show = show_console_dev |
| 252 | }; |
| 253 | |
| 254 | /* |
| 255 | * Used for open /proc/tty/consoles. Before this detect |
| 256 | * the device ID of file descriptor 0 of the current |
| 257 | * reading task if a character device... |
| 258 | */ |
| 259 | static int tty_consoles_open(struct inode *inode, struct file *file) |
| 260 | { |
| 261 | struct files_struct *curfiles; |
| 262 | |
| 263 | current_dev = 0; |
| 264 | curfiles = get_files_struct(current); |
| 265 | if (curfiles) { |
| 266 | const struct file *curfp; |
| 267 | spin_lock(&curfiles->file_lock); |
| 268 | curfp = fcheck_files(curfiles, 0); |
| 269 | if (curfp && curfp->private_data) { |
| 270 | const struct inode *inode; |
| 271 | dget(curfp->f_dentry); |
| 272 | inode = curfp->f_dentry->d_inode; |
| 273 | if (S_ISCHR(inode->i_mode)) { |
| 274 | struct tty_struct *tty; |
| 275 | tty = (struct tty_struct *)curfp->private_data; |
| 276 | if (tty && tty->magic == TTY_MAGIC) { |
| 277 | tty = tty_pair_get_tty(tty); |
| 278 | current_dev = tty_devnum(tty); |
| 279 | } |
| 280 | } |
| 281 | dput(curfp->f_dentry); |
| 282 | } |
| 283 | spin_unlock(&curfiles->file_lock); |
| 284 | put_files_struct(curfiles); |
| 285 | } |
| 286 | return seq_open(file, &tty_consoles_op); |
| 287 | } |
| 288 | |
| 289 | static const struct file_operations proc_tty_consoles_operations = { |
| 290 | .open = tty_consoles_open, |
| 291 | .read = seq_read, |
| 292 | .llseek = seq_lseek, |
| 293 | .release = seq_release, |
| 294 | }; |
| 295 | |
| 296 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | * This function is called by tty_register_driver() to handle |
| 298 | * registering the driver's /proc handler into /proc/tty/driver/<foo> |
| 299 | */ |
| 300 | void proc_tty_register_driver(struct tty_driver *driver) |
| 301 | { |
| 302 | struct proc_dir_entry *ent; |
| 303 | |
Alexey Dobriyan | 0f043a8 | 2009-03-31 15:19:25 -0700 | [diff] [blame] | 304 | if (!driver->driver_name || driver->proc_entry || |
| 305 | !driver->ops->proc_fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return; |
| 307 | |
Alexey Dobriyan | 0f043a8 | 2009-03-31 15:19:25 -0700 | [diff] [blame] | 308 | ent = proc_create_data(driver->driver_name, 0, proc_tty_driver, |
| 309 | driver->ops->proc_fops, driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | driver->proc_entry = ent; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * This function is called by tty_unregister_driver() |
| 315 | */ |
| 316 | void proc_tty_unregister_driver(struct tty_driver *driver) |
| 317 | { |
| 318 | struct proc_dir_entry *ent; |
| 319 | |
| 320 | ent = driver->proc_entry; |
| 321 | if (!ent) |
| 322 | return; |
| 323 | |
| 324 | remove_proc_entry(driver->driver_name, proc_tty_driver); |
| 325 | |
| 326 | driver->proc_entry = NULL; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Called by proc_root_init() to initialize the /proc/tty subtree |
| 331 | */ |
| 332 | void __init proc_tty_init(void) |
| 333 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | if (!proc_mkdir("tty", NULL)) |
| 335 | return; |
| 336 | proc_tty_ldisc = proc_mkdir("tty/ldisc", NULL); |
| 337 | /* |
| 338 | * /proc/tty/driver/serial reveals the exact character counts for |
| 339 | * serial links which is just too easy to abuse for inferring |
| 340 | * password lengths and inter-keystroke timings during password |
| 341 | * entry. |
| 342 | */ |
Alexey Dobriyan | b640a89 | 2008-04-29 01:01:58 -0700 | [diff] [blame] | 343 | proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR|S_IXUSR, NULL); |
| 344 | proc_create("tty/ldiscs", 0, NULL, &tty_ldiscs_proc_fops); |
Alexey Dobriyan | 0d5c9f5 | 2008-04-29 01:01:37 -0700 | [diff] [blame] | 345 | proc_create("tty/drivers", 0, NULL, &proc_tty_drivers_operations); |
Dr. Werner Fink | f4a3e0b | 2010-09-22 12:45:40 +0200 | [diff] [blame] | 346 | proc_create("tty/consoles", 0, NULL, &proc_tty_consoles_operations); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |