ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / proc / proc_tty.c
1 /*
2  * proc_tty.c -- handles /proc/tty
3  *
4  * Copyright 1997, Theodore Ts'o
5  */
6
7 #include <asm/uaccess.h>
8
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 <asm/bitops.h>
17
18 extern struct tty_ldisc ldiscs[];
19
20
21 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
22                                 int count, int *eof, void *data);
23
24 /*
25  * The /proc/tty directory inodes...
26  */
27 static struct proc_dir_entry *proc_tty_ldisc, *proc_tty_driver;
28
29 /*
30  * This is the handler for /proc/tty/drivers
31  */
32 static void show_tty_range(struct seq_file *m, struct tty_driver *p,
33         dev_t from, int num)
34 {
35         seq_printf(m, "%-20s ", p->driver_name ? p->driver_name : "unknown");
36         seq_printf(m, "/dev/%-8s ", p->name);
37         if (p->num > 1) {
38                 char    range[20];
39                 sprintf(range, "%d-%d", MINOR(from),
40                         MINOR(from) + num - 1);
41                 seq_printf(m, "%3d %7s ", MAJOR(from), range);
42         } else {
43                 seq_printf(m, "%3d %7d ", MAJOR(from), MINOR(from));
44         }
45         switch (p->type) {
46         case TTY_DRIVER_TYPE_SYSTEM:
47                 seq_printf(m, "system");
48                 if (p->subtype == SYSTEM_TYPE_TTY)
49                         seq_printf(m, ":/dev/tty");
50                 else if (p->subtype == SYSTEM_TYPE_SYSCONS)
51                         seq_printf(m, ":console");
52                 else if (p->subtype == SYSTEM_TYPE_CONSOLE)
53                         seq_printf(m, ":vtmaster");
54                 break;
55         case TTY_DRIVER_TYPE_CONSOLE:
56                 seq_printf(m, "console");
57                 break;
58         case TTY_DRIVER_TYPE_SERIAL:
59                 seq_printf(m, "serial");
60                 break;
61         case TTY_DRIVER_TYPE_PTY:
62                 if (p->subtype == PTY_TYPE_MASTER)
63                         seq_printf(m, "pty:master");
64                 else if (p->subtype == PTY_TYPE_SLAVE)
65                         seq_printf(m, "pty:slave");
66                 else
67                         seq_printf(m, "pty");
68                 break;
69         default:
70                 seq_printf(m, "type:%d.%d", p->type, p->subtype);
71         }
72         seq_putc(m, '\n');
73 }
74
75 static int show_tty_driver(struct seq_file *m, void *v)
76 {
77         struct tty_driver *p = v;
78         dev_t from = MKDEV(p->major, p->minor_start);
79         dev_t to = from + p->num;
80
81         if (&p->tty_drivers == tty_drivers.next) {
82                 /* pseudo-drivers first */
83                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/tty", "tty");
84                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 0);
85                 seq_printf(m, "system:/dev/tty\n");
86                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/console", "console");
87                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 1);
88                 seq_printf(m, "system:console\n");
89 #ifdef CONFIG_UNIX98_PTYS
90                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/ptmx", "ptmx");
91                 seq_printf(m, "%3d %7d ", TTYAUX_MAJOR, 2);
92                 seq_printf(m, "system\n");
93 #endif
94 #ifdef CONFIG_VT
95                 seq_printf(m, "%-20s /dev/%-8s ", "/dev/vc/0", "vc/0");
96                 seq_printf(m, "%3d %7d ", TTY_MAJOR, 0);
97                 seq_printf(m, "system:vtmaster\n");
98 #endif
99         }
100
101         while (MAJOR(from) < MAJOR(to)) {
102                 dev_t next = MKDEV(MAJOR(from)+1, 0);
103                 show_tty_range(m, p, from, next - from);
104                 from = next;
105         }
106         if (from != to)
107                 show_tty_range(m, p, from, to - from);
108         return 0;
109 }
110
111 /* iterator */
112 static void *t_start(struct seq_file *m, loff_t *pos)
113 {
114         struct list_head *p;
115         loff_t l = *pos;
116         list_for_each(p, &tty_drivers)
117                 if (!l--)
118                         return list_entry(p, struct tty_driver, tty_drivers);
119         return NULL;
120 }
121
122 static void *t_next(struct seq_file *m, void *v, loff_t *pos)
123 {
124         struct list_head *p = ((struct tty_driver *)v)->tty_drivers.next;
125         (*pos)++;
126         return p==&tty_drivers ? NULL :
127                         list_entry(p, struct tty_driver, tty_drivers);
128 }
129
130 static void t_stop(struct seq_file *m, void *v)
131 {
132 }
133
134 static struct seq_operations tty_drivers_op = {
135         .start  = t_start,
136         .next   = t_next,
137         .stop   = t_stop,
138         .show   = show_tty_driver
139 };
140
141 static int tty_drivers_open(struct inode *inode, struct file *file)
142 {
143         return seq_open(file, &tty_drivers_op);
144 }
145
146 static struct file_operations proc_tty_drivers_operations = {
147         .open           = tty_drivers_open,
148         .read           = seq_read,
149         .llseek         = seq_lseek,
150         .release        = seq_release,
151 };
152
153 /*
154  * This is the handler for /proc/tty/ldiscs
155  */
156 static int tty_ldiscs_read_proc(char *page, char **start, off_t off,
157                                 int count, int *eof, void *data)
158 {
159         int     i;
160         int     len = 0;
161         off_t   begin = 0;
162
163         for (i=0; i < NR_LDISCS; i++) {
164                 if (!(ldiscs[i].flags & LDISC_FLAG_DEFINED))
165                         continue;
166                 len += sprintf(page+len, "%-10s %2d\n",
167                                ldiscs[i].name ? ldiscs[i].name : "???", i);
168                 if (len+begin > off+count)
169                         break;
170                 if (len+begin < off) {
171                         begin += len;
172                         len = 0;
173                 }
174         }
175         if (i >= NR_LDISCS)
176                 *eof = 1;
177         if (off >= len+begin)
178                 return 0;
179         *start = page + (off-begin);
180         return ((count < begin+len-off) ? count : begin+len-off);
181 }
182
183 /*
184  * This function is called by tty_register_driver() to handle
185  * registering the driver's /proc handler into /proc/tty/driver/<foo>
186  */
187 void proc_tty_register_driver(struct tty_driver *driver)
188 {
189         struct proc_dir_entry *ent;
190                 
191         if ((!driver->read_proc && !driver->write_proc) ||
192             !driver->driver_name ||
193             driver->proc_entry)
194                 return;
195
196         ent = create_proc_entry(driver->driver_name, 0, proc_tty_driver);
197         if (!ent)
198                 return;
199         ent->read_proc = driver->read_proc;
200         ent->write_proc = driver->write_proc;
201         ent->owner = driver->owner;
202         ent->data = driver;
203
204         driver->proc_entry = ent;
205 }
206
207 /*
208  * This function is called by tty_unregister_driver()
209  */
210 void proc_tty_unregister_driver(struct tty_driver *driver)
211 {
212         struct proc_dir_entry *ent;
213
214         ent = driver->proc_entry;
215         if (!ent)
216                 return;
217                 
218         remove_proc_entry(driver->driver_name, proc_tty_driver);
219         
220         driver->proc_entry = 0;
221 }
222
223 /*
224  * Called by proc_root_init() to initialize the /proc/tty subtree
225  */
226 void __init proc_tty_init(void)
227 {
228         struct proc_dir_entry *entry;
229         if (!proc_mkdir("tty", 0))
230                 return;
231         proc_tty_ldisc = proc_mkdir("tty/ldisc", 0);
232         /*
233          * /proc/tty/driver/serial reveals the exact character counts for
234          * serial links which is just too easy to abuse for inferring
235          * password lengths and inter-keystroke timings during password
236          * entry.
237          */
238         proc_tty_driver = proc_mkdir_mode("tty/driver", S_IRUSR | S_IXUSR, 0);
239
240         create_proc_read_entry("tty/ldiscs", 0, 0, tty_ldiscs_read_proc,NULL);
241         entry = create_proc_entry("tty/drivers", 0, NULL);
242         if (entry)
243                 entry->proc_fops = &proc_tty_drivers_operations;
244 }