Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
2 | * Copyright (C) 1991, 1992 Linus Torvalds |
3 | */ | |
4 | ||
5 | /* | |
6 | * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles | |
7 | * or rs-channels. It also implements echoing, cooked mode etc. | |
8 | * | |
9 | * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. | |
10 | * | |
11 | * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the | |
12 | * tty_struct and tty_queue structures. Previously there was an array | |
13 | * of 256 tty_struct's which was statically allocated, and the | |
14 | * tty_queue structures were allocated at boot time. Both are now | |
15 | * dynamically allocated only when the tty is open. | |
16 | * | |
17 | * Also restructured routines so that there is more of a separation | |
18 | * between the high-level tty routines (tty_io.c and tty_ioctl.c) and | |
19 | * the low-level tty routines (serial.c, pty.c, console.c). This | |
37bdfb07 | 20 | * makes for cleaner and more compact code. -TYT, 9/17/92 |
1da177e4 LT |
21 | * |
22 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines | |
23 | * which can be dynamically activated and de-activated by the line | |
24 | * discipline handling modules (like SLIP). | |
25 | * | |
26 | * NOTE: pay no attention to the line discipline code (yet); its | |
27 | * interface is still subject to change in this version... | |
28 | * -- TYT, 1/31/92 | |
29 | * | |
30 | * Added functionality to the OPOST tty handling. No delays, but all | |
31 | * other bits should be there. | |
32 | * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. | |
33 | * | |
34 | * Rewrote canonical mode and added more termios flags. | |
35 | * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 | |
36 | * | |
37 | * Reorganized FASYNC support so mouse code can share it. | |
38 | * -- ctm@ardi.com, 9Sep95 | |
39 | * | |
40 | * New TIOCLINUX variants added. | |
41 | * -- mj@k332.feld.cvut.cz, 19-Nov-95 | |
37bdfb07 | 42 | * |
1da177e4 LT |
43 | * Restrict vt switching via ioctl() |
44 | * -- grif@cs.ucr.edu, 5-Dec-95 | |
45 | * | |
46 | * Move console and virtual terminal code to more appropriate files, | |
47 | * implement CONFIG_VT and generalize console device interface. | |
48 | * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 | |
49 | * | |
d81ed103 | 50 | * Rewrote tty_init_dev and tty_release_dev to eliminate races. |
1da177e4 LT |
51 | * -- Bill Hawes <whawes@star.net>, June 97 |
52 | * | |
53 | * Added devfs support. | |
54 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 | |
55 | * | |
56 | * Added support for a Unix98-style ptmx device. | |
57 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 | |
58 | * | |
59 | * Reduced memory usage for older ARM systems | |
60 | * -- Russell King <rmk@arm.linux.org.uk> | |
61 | * | |
62 | * Move do_SAK() into process context. Less stack use in devfs functions. | |
37bdfb07 AC |
63 | * alloc_tty_struct() always uses kmalloc() |
64 | * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 | |
1da177e4 LT |
65 | */ |
66 | ||
1da177e4 LT |
67 | #include <linux/types.h> |
68 | #include <linux/major.h> | |
69 | #include <linux/errno.h> | |
70 | #include <linux/signal.h> | |
71 | #include <linux/fcntl.h> | |
72 | #include <linux/sched.h> | |
73 | #include <linux/interrupt.h> | |
74 | #include <linux/tty.h> | |
75 | #include <linux/tty_driver.h> | |
76 | #include <linux/tty_flip.h> | |
77 | #include <linux/devpts_fs.h> | |
78 | #include <linux/file.h> | |
9f3acc31 | 79 | #include <linux/fdtable.h> |
1da177e4 LT |
80 | #include <linux/console.h> |
81 | #include <linux/timer.h> | |
82 | #include <linux/ctype.h> | |
83 | #include <linux/kd.h> | |
84 | #include <linux/mm.h> | |
85 | #include <linux/string.h> | |
86 | #include <linux/slab.h> | |
87 | #include <linux/poll.h> | |
88 | #include <linux/proc_fs.h> | |
89 | #include <linux/init.h> | |
90 | #include <linux/module.h> | |
1da177e4 | 91 | #include <linux/device.h> |
1da177e4 LT |
92 | #include <linux/wait.h> |
93 | #include <linux/bitops.h> | |
b20f3ae5 | 94 | #include <linux/delay.h> |
a352def2 | 95 | #include <linux/seq_file.h> |
d281da7f | 96 | #include <linux/serial.h> |
5a3c6b25 | 97 | #include <linux/ratelimit.h> |
1da177e4 | 98 | |
a352def2 | 99 | #include <linux/uaccess.h> |
1da177e4 LT |
100 | |
101 | #include <linux/kbd_kern.h> | |
102 | #include <linux/vt_kern.h> | |
103 | #include <linux/selection.h> | |
1da177e4 LT |
104 | |
105 | #include <linux/kmod.h> | |
b488893a | 106 | #include <linux/nsproxy.h> |
1da177e4 LT |
107 | |
108 | #undef TTY_DEBUG_HANGUP | |
109 | ||
110 | #define TTY_PARANOIA_CHECK 1 | |
111 | #define CHECK_TTY_COUNT 1 | |
112 | ||
edc6afc5 | 113 | struct ktermios tty_std_termios = { /* for the benefit of tty drivers */ |
1da177e4 LT |
114 | .c_iflag = ICRNL | IXON, |
115 | .c_oflag = OPOST | ONLCR, | |
116 | .c_cflag = B38400 | CS8 | CREAD | HUPCL, | |
117 | .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | | |
118 | ECHOCTL | ECHOKE | IEXTEN, | |
edc6afc5 AC |
119 | .c_cc = INIT_C_CC, |
120 | .c_ispeed = 38400, | |
121 | .c_ospeed = 38400 | |
1da177e4 LT |
122 | }; |
123 | ||
124 | EXPORT_SYMBOL(tty_std_termios); | |
125 | ||
126 | /* This list gets poked at by procfs and various bits of boot up code. This | |
127 | could do with some rationalisation such as pulling the tty proc function | |
128 | into this file */ | |
37bdfb07 | 129 | |
1da177e4 LT |
130 | LIST_HEAD(tty_drivers); /* linked list of tty drivers */ |
131 | ||
24ec839c | 132 | /* Mutex to protect creating and releasing a tty. This is shared with |
1da177e4 | 133 | vt.c for deeply disgusting hack reasons */ |
70522e12 | 134 | DEFINE_MUTEX(tty_mutex); |
de2a84f2 | 135 | EXPORT_SYMBOL(tty_mutex); |
1da177e4 | 136 | |
ee2ffa0d NP |
137 | /* Spinlock to protect the tty->tty_files list */ |
138 | DEFINE_SPINLOCK(tty_files_lock); | |
139 | ||
1da177e4 LT |
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 *); | |
37bdfb07 AC |
142 | ssize_t redirected_tty_write(struct file *, const char __user *, |
143 | size_t, loff_t *); | |
1da177e4 LT |
144 | static unsigned int tty_poll(struct file *, poll_table *); |
145 | static int tty_open(struct inode *, struct file *); | |
04f378b1 | 146 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg); |
e10cc1df | 147 | #ifdef CONFIG_COMPAT |
37bdfb07 | 148 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
e10cc1df PF |
149 | unsigned long arg); |
150 | #else | |
151 | #define tty_compat_ioctl NULL | |
152 | #endif | |
ec79d605 | 153 | static int __tty_fasync(int fd, struct file *filp, int on); |
37bdfb07 | 154 | static int tty_fasync(int fd, struct file *filp, int on); |
d5698c28 | 155 | static void release_tty(struct tty_struct *tty, int idx); |
2a65f1d9 | 156 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); |
98a27ba4 | 157 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty); |
1da177e4 | 158 | |
af9b897e AC |
159 | /** |
160 | * alloc_tty_struct - allocate a tty object | |
161 | * | |
162 | * Return a new empty tty structure. The data fields have not | |
163 | * been initialized in any way but has been zeroed | |
164 | * | |
165 | * Locking: none | |
af9b897e | 166 | */ |
1da177e4 | 167 | |
bf970ee4 | 168 | struct tty_struct *alloc_tty_struct(void) |
1da177e4 | 169 | { |
1266b1e1 | 170 | return kzalloc(sizeof(struct tty_struct), GFP_KERNEL); |
1da177e4 LT |
171 | } |
172 | ||
af9b897e AC |
173 | /** |
174 | * free_tty_struct - free a disused tty | |
175 | * @tty: tty struct to free | |
176 | * | |
177 | * Free the write buffers, tty queue and tty memory itself. | |
178 | * | |
179 | * Locking: none. Must be called after tty is definitely unused | |
180 | */ | |
181 | ||
bf970ee4 | 182 | void free_tty_struct(struct tty_struct *tty) |
1da177e4 | 183 | { |
dc6802a7 DC |
184 | if (!tty) |
185 | return; | |
30004ac9 DES |
186 | if (tty->dev) |
187 | put_device(tty->dev); | |
1da177e4 | 188 | kfree(tty->write_buf); |
89c8d91e | 189 | tty->magic = 0xDEADDEAD; |
1da177e4 LT |
190 | kfree(tty); |
191 | } | |
192 | ||
d996b62a NP |
193 | static inline struct tty_struct *file_tty(struct file *file) |
194 | { | |
195 | return ((struct tty_file_private *)file->private_data)->tty; | |
196 | } | |
197 | ||
fa90e1c9 | 198 | int tty_alloc_file(struct file *file) |
d996b62a NP |
199 | { |
200 | struct tty_file_private *priv; | |
201 | ||
f573bd17 PE |
202 | priv = kmalloc(sizeof(*priv), GFP_KERNEL); |
203 | if (!priv) | |
204 | return -ENOMEM; | |
d996b62a | 205 | |
fa90e1c9 JS |
206 | file->private_data = priv; |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | /* Associate a new file with the tty structure */ | |
212 | void tty_add_file(struct tty_struct *tty, struct file *file) | |
213 | { | |
214 | struct tty_file_private *priv = file->private_data; | |
215 | ||
d996b62a NP |
216 | priv->tty = tty; |
217 | priv->file = file; | |
d996b62a NP |
218 | |
219 | spin_lock(&tty_files_lock); | |
220 | list_add(&priv->list, &tty->tty_files); | |
221 | spin_unlock(&tty_files_lock); | |
fa90e1c9 | 222 | } |
f573bd17 | 223 | |
fa90e1c9 JS |
224 | /** |
225 | * tty_free_file - free file->private_data | |
226 | * | |
227 | * This shall be used only for fail path handling when tty_add_file was not | |
228 | * called yet. | |
229 | */ | |
230 | void tty_free_file(struct file *file) | |
231 | { | |
232 | struct tty_file_private *priv = file->private_data; | |
233 | ||
234 | file->private_data = NULL; | |
235 | kfree(priv); | |
d996b62a NP |
236 | } |
237 | ||
238 | /* Delete file from its tty */ | |
2520e274 | 239 | static void tty_del_file(struct file *file) |
d996b62a NP |
240 | { |
241 | struct tty_file_private *priv = file->private_data; | |
242 | ||
243 | spin_lock(&tty_files_lock); | |
244 | list_del(&priv->list); | |
245 | spin_unlock(&tty_files_lock); | |
fa90e1c9 | 246 | tty_free_file(file); |
d996b62a NP |
247 | } |
248 | ||
249 | ||
1da177e4 LT |
250 | #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) |
251 | ||
af9b897e AC |
252 | /** |
253 | * tty_name - return tty naming | |
254 | * @tty: tty structure | |
255 | * @buf: buffer for output | |
256 | * | |
257 | * Convert a tty structure into a name. The name reflects the kernel | |
258 | * naming policy and if udev is in use may not reflect user space | |
259 | * | |
260 | * Locking: none | |
261 | */ | |
262 | ||
1da177e4 LT |
263 | char *tty_name(struct tty_struct *tty, char *buf) |
264 | { | |
265 | if (!tty) /* Hmm. NULL pointer. That's fun. */ | |
266 | strcpy(buf, "NULL tty"); | |
267 | else | |
268 | strcpy(buf, tty->name); | |
269 | return buf; | |
270 | } | |
271 | ||
272 | EXPORT_SYMBOL(tty_name); | |
273 | ||
d769a669 | 274 | int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, |
1da177e4 LT |
275 | const char *routine) |
276 | { | |
277 | #ifdef TTY_PARANOIA_CHECK | |
278 | if (!tty) { | |
279 | printk(KERN_WARNING | |
280 | "null TTY for (%d:%d) in %s\n", | |
281 | imajor(inode), iminor(inode), routine); | |
282 | return 1; | |
283 | } | |
284 | if (tty->magic != TTY_MAGIC) { | |
285 | printk(KERN_WARNING | |
286 | "bad magic number for tty struct (%d:%d) in %s\n", | |
287 | imajor(inode), iminor(inode), routine); | |
288 | return 1; | |
289 | } | |
290 | #endif | |
291 | return 0; | |
292 | } | |
293 | ||
294 | static int check_tty_count(struct tty_struct *tty, const char *routine) | |
295 | { | |
296 | #ifdef CHECK_TTY_COUNT | |
297 | struct list_head *p; | |
298 | int count = 0; | |
37bdfb07 | 299 | |
ee2ffa0d | 300 | spin_lock(&tty_files_lock); |
1da177e4 LT |
301 | list_for_each(p, &tty->tty_files) { |
302 | count++; | |
303 | } | |
ee2ffa0d | 304 | spin_unlock(&tty_files_lock); |
1da177e4 LT |
305 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
306 | tty->driver->subtype == PTY_TYPE_SLAVE && | |
307 | tty->link && tty->link->count) | |
308 | count++; | |
309 | if (tty->count != count) { | |
310 | printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " | |
311 | "!= #fd's(%d) in %s\n", | |
312 | tty->name, tty->count, count, routine); | |
313 | return count; | |
24ec839c | 314 | } |
1da177e4 LT |
315 | #endif |
316 | return 0; | |
317 | } | |
318 | ||
af9b897e AC |
319 | /** |
320 | * get_tty_driver - find device of a tty | |
321 | * @dev_t: device identifier | |
322 | * @index: returns the index of the tty | |
323 | * | |
324 | * This routine returns a tty driver structure, given a device number | |
325 | * and also passes back the index number. | |
326 | * | |
327 | * Locking: caller must hold tty_mutex | |
1da177e4 | 328 | */ |
af9b897e | 329 | |
1da177e4 LT |
330 | static struct tty_driver *get_tty_driver(dev_t device, int *index) |
331 | { | |
332 | struct tty_driver *p; | |
333 | ||
334 | list_for_each_entry(p, &tty_drivers, tty_drivers) { | |
335 | dev_t base = MKDEV(p->major, p->minor_start); | |
336 | if (device < base || device >= base + p->num) | |
337 | continue; | |
338 | *index = device - base; | |
7d7b93c1 | 339 | return tty_driver_kref_get(p); |
1da177e4 LT |
340 | } |
341 | return NULL; | |
342 | } | |
343 | ||
f2d937f3 JW |
344 | #ifdef CONFIG_CONSOLE_POLL |
345 | ||
346 | /** | |
347 | * tty_find_polling_driver - find device of a polled tty | |
348 | * @name: name string to match | |
349 | * @line: pointer to resulting tty line nr | |
350 | * | |
351 | * This routine returns a tty driver structure, given a name | |
352 | * and the condition that the tty driver is capable of polled | |
353 | * operation. | |
354 | */ | |
355 | struct tty_driver *tty_find_polling_driver(char *name, int *line) | |
356 | { | |
357 | struct tty_driver *p, *res = NULL; | |
358 | int tty_line = 0; | |
0dca0fd2 | 359 | int len; |
5f0878ac | 360 | char *str, *stp; |
f2d937f3 | 361 | |
0dca0fd2 JW |
362 | for (str = name; *str; str++) |
363 | if ((*str >= '0' && *str <= '9') || *str == ',') | |
364 | break; | |
365 | if (!*str) | |
366 | return NULL; | |
367 | ||
368 | len = str - name; | |
369 | tty_line = simple_strtoul(str, &str, 10); | |
370 | ||
f2d937f3 JW |
371 | mutex_lock(&tty_mutex); |
372 | /* Search through the tty devices to look for a match */ | |
373 | list_for_each_entry(p, &tty_drivers, tty_drivers) { | |
0dca0fd2 JW |
374 | if (strncmp(name, p->name, len) != 0) |
375 | continue; | |
5f0878ac AC |
376 | stp = str; |
377 | if (*stp == ',') | |
378 | stp++; | |
379 | if (*stp == '\0') | |
380 | stp = NULL; | |
f2d937f3 | 381 | |
6eb68d6f | 382 | if (tty_line >= 0 && tty_line < p->num && p->ops && |
5f0878ac | 383 | p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) { |
7d7b93c1 | 384 | res = tty_driver_kref_get(p); |
f2d937f3 JW |
385 | *line = tty_line; |
386 | break; | |
387 | } | |
388 | } | |
389 | mutex_unlock(&tty_mutex); | |
390 | ||
391 | return res; | |
392 | } | |
393 | EXPORT_SYMBOL_GPL(tty_find_polling_driver); | |
394 | #endif | |
395 | ||
af9b897e AC |
396 | /** |
397 | * tty_check_change - check for POSIX terminal changes | |
398 | * @tty: tty to check | |
399 | * | |
400 | * If we try to write to, or set the state of, a terminal and we're | |
401 | * not in the foreground, send a SIGTTOU. If the signal is blocked or | |
402 | * ignored, go ahead and perform the operation. (POSIX 7.2) | |
403 | * | |
978e595f | 404 | * Locking: ctrl_lock |
1da177e4 | 405 | */ |
af9b897e | 406 | |
37bdfb07 | 407 | int tty_check_change(struct tty_struct *tty) |
1da177e4 | 408 | { |
47f86834 AC |
409 | unsigned long flags; |
410 | int ret = 0; | |
411 | ||
1da177e4 LT |
412 | if (current->signal->tty != tty) |
413 | return 0; | |
47f86834 AC |
414 | |
415 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
416 | ||
ab521dc0 EB |
417 | if (!tty->pgrp) { |
418 | printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n"); | |
9ffee4cb | 419 | goto out_unlock; |
1da177e4 | 420 | } |
ab521dc0 | 421 | if (task_pgrp(current) == tty->pgrp) |
9ffee4cb AM |
422 | goto out_unlock; |
423 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
1da177e4 | 424 | if (is_ignored(SIGTTOU)) |
47f86834 AC |
425 | goto out; |
426 | if (is_current_pgrp_orphaned()) { | |
427 | ret = -EIO; | |
428 | goto out; | |
429 | } | |
040b6362 ON |
430 | kill_pgrp(task_pgrp(current), SIGTTOU, 1); |
431 | set_thread_flag(TIF_SIGPENDING); | |
47f86834 AC |
432 | ret = -ERESTARTSYS; |
433 | out: | |
9ffee4cb AM |
434 | return ret; |
435 | out_unlock: | |
47f86834 AC |
436 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
437 | return ret; | |
1da177e4 LT |
438 | } |
439 | ||
440 | EXPORT_SYMBOL(tty_check_change); | |
441 | ||
37bdfb07 | 442 | static ssize_t hung_up_tty_read(struct file *file, char __user *buf, |
1da177e4 LT |
443 | size_t count, loff_t *ppos) |
444 | { | |
445 | return 0; | |
446 | } | |
447 | ||
37bdfb07 | 448 | static ssize_t hung_up_tty_write(struct file *file, const char __user *buf, |
1da177e4 LT |
449 | size_t count, loff_t *ppos) |
450 | { | |
451 | return -EIO; | |
452 | } | |
453 | ||
454 | /* No kernel lock held - none needed ;) */ | |
37bdfb07 | 455 | static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait) |
1da177e4 LT |
456 | { |
457 | return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; | |
458 | } | |
459 | ||
04f378b1 AC |
460 | static long hung_up_tty_ioctl(struct file *file, unsigned int cmd, |
461 | unsigned long arg) | |
38ad2ed0 PF |
462 | { |
463 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; | |
464 | } | |
465 | ||
37bdfb07 | 466 | static long hung_up_tty_compat_ioctl(struct file *file, |
38ad2ed0 | 467 | unsigned int cmd, unsigned long arg) |
1da177e4 LT |
468 | { |
469 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; | |
470 | } | |
471 | ||
62322d25 | 472 | static const struct file_operations tty_fops = { |
1da177e4 LT |
473 | .llseek = no_llseek, |
474 | .read = tty_read, | |
475 | .write = tty_write, | |
476 | .poll = tty_poll, | |
04f378b1 | 477 | .unlocked_ioctl = tty_ioctl, |
e10cc1df | 478 | .compat_ioctl = tty_compat_ioctl, |
1da177e4 LT |
479 | .open = tty_open, |
480 | .release = tty_release, | |
481 | .fasync = tty_fasync, | |
482 | }; | |
483 | ||
62322d25 | 484 | static const struct file_operations console_fops = { |
1da177e4 LT |
485 | .llseek = no_llseek, |
486 | .read = tty_read, | |
487 | .write = redirected_tty_write, | |
488 | .poll = tty_poll, | |
04f378b1 | 489 | .unlocked_ioctl = tty_ioctl, |
e10cc1df | 490 | .compat_ioctl = tty_compat_ioctl, |
1da177e4 LT |
491 | .open = tty_open, |
492 | .release = tty_release, | |
493 | .fasync = tty_fasync, | |
494 | }; | |
495 | ||
62322d25 | 496 | static const struct file_operations hung_up_tty_fops = { |
1da177e4 LT |
497 | .llseek = no_llseek, |
498 | .read = hung_up_tty_read, | |
499 | .write = hung_up_tty_write, | |
500 | .poll = hung_up_tty_poll, | |
04f378b1 | 501 | .unlocked_ioctl = hung_up_tty_ioctl, |
38ad2ed0 | 502 | .compat_ioctl = hung_up_tty_compat_ioctl, |
1da177e4 LT |
503 | .release = tty_release, |
504 | }; | |
505 | ||
506 | static DEFINE_SPINLOCK(redirect_lock); | |
507 | static struct file *redirect; | |
508 | ||
509 | /** | |
510 | * tty_wakeup - request more data | |
511 | * @tty: terminal | |
512 | * | |
513 | * Internal and external helper for wakeups of tty. This function | |
514 | * informs the line discipline if present that the driver is ready | |
515 | * to receive more output data. | |
516 | */ | |
37bdfb07 | 517 | |
1da177e4 LT |
518 | void tty_wakeup(struct tty_struct *tty) |
519 | { | |
520 | struct tty_ldisc *ld; | |
37bdfb07 | 521 | |
1da177e4 LT |
522 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { |
523 | ld = tty_ldisc_ref(tty); | |
37bdfb07 | 524 | if (ld) { |
a352def2 AC |
525 | if (ld->ops->write_wakeup) |
526 | ld->ops->write_wakeup(tty); | |
1da177e4 LT |
527 | tty_ldisc_deref(ld); |
528 | } | |
529 | } | |
4b19449d | 530 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); |
1da177e4 LT |
531 | } |
532 | ||
533 | EXPORT_SYMBOL_GPL(tty_wakeup); | |
534 | ||
ea648a47 PH |
535 | /** |
536 | * tty_signal_session_leader - sends SIGHUP to session leader | |
537 | * | |
538 | * Send SIGHUP and SIGCONT to the session leader and its | |
539 | * process group. | |
540 | * | |
541 | * Returns the number of processes in the session with this tty | |
542 | * as their controlling terminal. This value is used to drop | |
543 | * tty references for those processes. | |
544 | */ | |
545 | static int tty_signal_session_leader(struct tty_struct *tty) | |
546 | { | |
547 | struct task_struct *p; | |
548 | unsigned long flags; | |
549 | int refs = 0; | |
550 | ||
551 | read_lock(&tasklist_lock); | |
552 | if (tty->session) { | |
553 | do_each_pid_task(tty->session, PIDTYPE_SID, p) { | |
554 | spin_lock_irq(&p->sighand->siglock); | |
555 | if (p->signal->tty == tty) { | |
556 | p->signal->tty = NULL; | |
557 | /* We defer the dereferences outside fo | |
558 | the tasklist lock */ | |
559 | refs++; | |
560 | } | |
561 | if (!p->signal->leader) { | |
562 | spin_unlock_irq(&p->sighand->siglock); | |
563 | continue; | |
564 | } | |
565 | __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); | |
566 | __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); | |
567 | put_pid(p->signal->tty_old_pgrp); /* A noop */ | |
568 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
569 | if (tty->pgrp) | |
570 | p->signal->tty_old_pgrp = get_pid(tty->pgrp); | |
571 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
572 | spin_unlock_irq(&p->sighand->siglock); | |
573 | } while_each_pid_task(tty->session, PIDTYPE_SID, p); | |
574 | } | |
575 | read_unlock(&tasklist_lock); | |
576 | ||
577 | return refs; | |
578 | } | |
579 | ||
af9b897e | 580 | /** |
11dbf203 | 581 | * __tty_hangup - actual handler for hangup events |
65f27f38 | 582 | * @work: tty device |
af9b897e | 583 | * |
ef4f527c | 584 | * This can be called by a "kworker" kernel thread. That is process |
af9b897e AC |
585 | * synchronous but doesn't hold any locks, so we need to make sure we |
586 | * have the appropriate locks for what we're doing. | |
587 | * | |
588 | * The hangup event clears any pending redirections onto the hung up | |
589 | * device. It ensures future writes will error and it does the needed | |
590 | * line discipline hangup and signal delivery. The tty object itself | |
591 | * remains intact. | |
592 | * | |
593 | * Locking: | |
ec79d605 | 594 | * BTM |
24ec839c PZ |
595 | * redirect lock for undoing redirection |
596 | * file list lock for manipulating list of ttys | |
597 | * tty_ldisc_lock from called functions | |
598 | * termios_mutex resetting termios data | |
599 | * tasklist_lock to walk task list for hangup event | |
600 | * ->siglock to protect ->signal/->sighand | |
1da177e4 | 601 | */ |
2520e274 | 602 | static void __tty_hangup(struct tty_struct *tty) |
1da177e4 | 603 | { |
37bdfb07 | 604 | struct file *cons_filp = NULL; |
1da177e4 | 605 | struct file *filp, *f = NULL; |
d996b62a | 606 | struct tty_file_private *priv; |
1da177e4 | 607 | int closecount = 0, n; |
ea648a47 | 608 | int refs; |
1da177e4 LT |
609 | |
610 | if (!tty) | |
611 | return; | |
612 | ||
1da177e4 LT |
613 | |
614 | spin_lock(&redirect_lock); | |
d996b62a | 615 | if (redirect && file_tty(redirect) == tty) { |
1da177e4 LT |
616 | f = redirect; |
617 | redirect = NULL; | |
618 | } | |
619 | spin_unlock(&redirect_lock); | |
37bdfb07 | 620 | |
89c8d91e | 621 | tty_lock(tty); |
11dbf203 | 622 | |
acfa747b JS |
623 | /* some functions below drop BTM, so we need this bit */ |
624 | set_bit(TTY_HUPPING, &tty->flags); | |
625 | ||
ec79d605 AB |
626 | /* inuse_filps is protected by the single tty lock, |
627 | this really needs to change if we want to flush the | |
628 | workqueue with the lock held */ | |
11dbf203 | 629 | check_tty_count(tty, "tty_hangup"); |
36ba782e | 630 | |
ee2ffa0d | 631 | spin_lock(&tty_files_lock); |
1da177e4 | 632 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ |
d996b62a NP |
633 | list_for_each_entry(priv, &tty->tty_files, list) { |
634 | filp = priv->file; | |
1da177e4 LT |
635 | if (filp->f_op->write == redirected_tty_write) |
636 | cons_filp = filp; | |
637 | if (filp->f_op->write != tty_write) | |
638 | continue; | |
639 | closecount++; | |
ec79d605 | 640 | __tty_fasync(-1, filp, 0); /* can't block */ |
1da177e4 LT |
641 | filp->f_op = &hung_up_tty_fops; |
642 | } | |
ee2ffa0d | 643 | spin_unlock(&tty_files_lock); |
37bdfb07 | 644 | |
acfa747b JS |
645 | /* |
646 | * it drops BTM and thus races with reopen | |
647 | * we protect the race by TTY_HUPPING | |
648 | */ | |
c65c9bc3 | 649 | tty_ldisc_hangup(tty); |
37bdfb07 | 650 | |
ea648a47 PH |
651 | refs = tty_signal_session_leader(tty); |
652 | /* Account for the p->signal references we killed */ | |
653 | while (refs--) | |
654 | tty_kref_put(tty); | |
1da177e4 | 655 | |
20cc225b | 656 | spin_lock_irq(&tty->ctrl_lock); |
c65c9bc3 AC |
657 | clear_bit(TTY_THROTTLED, &tty->flags); |
658 | clear_bit(TTY_PUSH, &tty->flags); | |
659 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); | |
d9c1e9a8 EB |
660 | put_pid(tty->session); |
661 | put_pid(tty->pgrp); | |
ab521dc0 EB |
662 | tty->session = NULL; |
663 | tty->pgrp = NULL; | |
1da177e4 | 664 | tty->ctrl_status = 0; |
20cc225b | 665 | spin_unlock_irq(&tty->ctrl_lock); |
9c9f4ded | 666 | |
1da177e4 | 667 | /* |
37bdfb07 AC |
668 | * If one of the devices matches a console pointer, we |
669 | * cannot just call hangup() because that will cause | |
670 | * tty->count and state->count to go out of sync. | |
671 | * So we just call close() the right number of times. | |
1da177e4 LT |
672 | */ |
673 | if (cons_filp) { | |
f34d7a5b | 674 | if (tty->ops->close) |
1da177e4 | 675 | for (n = 0; n < closecount; n++) |
f34d7a5b AC |
676 | tty->ops->close(tty, cons_filp); |
677 | } else if (tty->ops->hangup) | |
678 | (tty->ops->hangup)(tty); | |
37bdfb07 AC |
679 | /* |
680 | * We don't want to have driver/ldisc interactions beyond | |
681 | * the ones we did here. The driver layer expects no | |
682 | * calls after ->hangup() from the ldisc side. However we | |
683 | * can't yet guarantee all that. | |
684 | */ | |
1da177e4 | 685 | set_bit(TTY_HUPPED, &tty->flags); |
acfa747b | 686 | clear_bit(TTY_HUPPING, &tty->flags); |
c65c9bc3 | 687 | tty_ldisc_enable(tty); |
11dbf203 | 688 | |
89c8d91e | 689 | tty_unlock(tty); |
11dbf203 | 690 | |
1da177e4 LT |
691 | if (f) |
692 | fput(f); | |
693 | } | |
694 | ||
ddcd9fb6 AB |
695 | static void do_tty_hangup(struct work_struct *work) |
696 | { | |
697 | struct tty_struct *tty = | |
698 | container_of(work, struct tty_struct, hangup_work); | |
699 | ||
11dbf203 | 700 | __tty_hangup(tty); |
ddcd9fb6 AB |
701 | } |
702 | ||
af9b897e AC |
703 | /** |
704 | * tty_hangup - trigger a hangup event | |
705 | * @tty: tty to hangup | |
706 | * | |
707 | * A carrier loss (virtual or otherwise) has occurred on this like | |
708 | * schedule a hangup sequence to run after this event. | |
709 | */ | |
710 | ||
37bdfb07 | 711 | void tty_hangup(struct tty_struct *tty) |
1da177e4 LT |
712 | { |
713 | #ifdef TTY_DEBUG_HANGUP | |
714 | char buf[64]; | |
1da177e4 LT |
715 | printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); |
716 | #endif | |
717 | schedule_work(&tty->hangup_work); | |
718 | } | |
719 | ||
720 | EXPORT_SYMBOL(tty_hangup); | |
721 | ||
af9b897e AC |
722 | /** |
723 | * tty_vhangup - process vhangup | |
724 | * @tty: tty to hangup | |
725 | * | |
726 | * The user has asked via system call for the terminal to be hung up. | |
727 | * We do this synchronously so that when the syscall returns the process | |
3a4fa0a2 | 728 | * is complete. That guarantee is necessary for security reasons. |
af9b897e AC |
729 | */ |
730 | ||
37bdfb07 | 731 | void tty_vhangup(struct tty_struct *tty) |
1da177e4 LT |
732 | { |
733 | #ifdef TTY_DEBUG_HANGUP | |
734 | char buf[64]; | |
735 | ||
736 | printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); | |
737 | #endif | |
11dbf203 | 738 | __tty_hangup(tty); |
1da177e4 | 739 | } |
37bdfb07 | 740 | |
1da177e4 LT |
741 | EXPORT_SYMBOL(tty_vhangup); |
742 | ||
11dbf203 | 743 | |
2cb5998b AC |
744 | /** |
745 | * tty_vhangup_self - process vhangup for own ctty | |
746 | * | |
747 | * Perform a vhangup on the current controlling tty | |
748 | */ | |
749 | ||
750 | void tty_vhangup_self(void) | |
751 | { | |
752 | struct tty_struct *tty; | |
753 | ||
2cb5998b AC |
754 | tty = get_current_tty(); |
755 | if (tty) { | |
756 | tty_vhangup(tty); | |
757 | tty_kref_put(tty); | |
758 | } | |
2cb5998b AC |
759 | } |
760 | ||
af9b897e AC |
761 | /** |
762 | * tty_hung_up_p - was tty hung up | |
763 | * @filp: file pointer of tty | |
764 | * | |
765 | * Return true if the tty has been subject to a vhangup or a carrier | |
766 | * loss | |
767 | */ | |
768 | ||
37bdfb07 | 769 | int tty_hung_up_p(struct file *filp) |
1da177e4 LT |
770 | { |
771 | return (filp->f_op == &hung_up_tty_fops); | |
772 | } | |
773 | ||
774 | EXPORT_SYMBOL(tty_hung_up_p); | |
775 | ||
ab521dc0 | 776 | static void session_clear_tty(struct pid *session) |
24ec839c PZ |
777 | { |
778 | struct task_struct *p; | |
ab521dc0 | 779 | do_each_pid_task(session, PIDTYPE_SID, p) { |
24ec839c | 780 | proc_clear_tty(p); |
ab521dc0 | 781 | } while_each_pid_task(session, PIDTYPE_SID, p); |
24ec839c PZ |
782 | } |
783 | ||
af9b897e AC |
784 | /** |
785 | * disassociate_ctty - disconnect controlling tty | |
786 | * @on_exit: true if exiting so need to "hang up" the session | |
1da177e4 | 787 | * |
af9b897e AC |
788 | * This function is typically called only by the session leader, when |
789 | * it wants to disassociate itself from its controlling tty. | |
790 | * | |
791 | * It performs the following functions: | |
1da177e4 LT |
792 | * (1) Sends a SIGHUP and SIGCONT to the foreground process group |
793 | * (2) Clears the tty from being controlling the session | |
794 | * (3) Clears the controlling tty for all processes in the | |
795 | * session group. | |
796 | * | |
af9b897e AC |
797 | * The argument on_exit is set to 1 if called when a process is |
798 | * exiting; it is 0 if called by the ioctl TIOCNOTTY. | |
799 | * | |
24ec839c | 800 | * Locking: |
ec79d605 AB |
801 | * BTM is taken for hysterical raisins, and held when |
802 | * called from no_tty(). | |
24ec839c PZ |
803 | * tty_mutex is taken to protect tty |
804 | * ->siglock is taken to protect ->signal/->sighand | |
805 | * tasklist_lock is taken to walk process list for sessions | |
806 | * ->siglock is taken to protect ->signal/->sighand | |
1da177e4 | 807 | */ |
af9b897e | 808 | |
1da177e4 LT |
809 | void disassociate_ctty(int on_exit) |
810 | { | |
811 | struct tty_struct *tty; | |
1da177e4 | 812 | |
5ec93d11 AC |
813 | if (!current->signal->leader) |
814 | return; | |
1da177e4 | 815 | |
24ec839c | 816 | tty = get_current_tty(); |
1da177e4 | 817 | if (tty) { |
1411dc4a | 818 | struct pid *tty_pgrp = get_pid(tty->pgrp); |
ddcd9fb6 | 819 | if (on_exit) { |
ddcd9fb6 | 820 | if (tty->driver->type != TTY_DRIVER_TYPE_PTY) |
11dbf203 | 821 | tty_vhangup(tty); |
ddcd9fb6 | 822 | } |
452a00d2 | 823 | tty_kref_put(tty); |
1411dc4a JS |
824 | if (tty_pgrp) { |
825 | kill_pgrp(tty_pgrp, SIGHUP, on_exit); | |
826 | if (!on_exit) | |
827 | kill_pgrp(tty_pgrp, SIGCONT, on_exit); | |
828 | put_pid(tty_pgrp); | |
829 | } | |
680a9671 | 830 | } else if (on_exit) { |
ab521dc0 | 831 | struct pid *old_pgrp; |
680a9671 EB |
832 | spin_lock_irq(¤t->sighand->siglock); |
833 | old_pgrp = current->signal->tty_old_pgrp; | |
ab521dc0 | 834 | current->signal->tty_old_pgrp = NULL; |
680a9671 | 835 | spin_unlock_irq(¤t->sighand->siglock); |
24ec839c | 836 | if (old_pgrp) { |
ab521dc0 EB |
837 | kill_pgrp(old_pgrp, SIGHUP, on_exit); |
838 | kill_pgrp(old_pgrp, SIGCONT, on_exit); | |
839 | put_pid(old_pgrp); | |
1da177e4 | 840 | } |
1da177e4 LT |
841 | return; |
842 | } | |
1da177e4 | 843 | |
24ec839c | 844 | spin_lock_irq(¤t->sighand->siglock); |
2a65f1d9 | 845 | put_pid(current->signal->tty_old_pgrp); |
23cac8de | 846 | current->signal->tty_old_pgrp = NULL; |
24ec839c PZ |
847 | spin_unlock_irq(¤t->sighand->siglock); |
848 | ||
24ec839c PZ |
849 | tty = get_current_tty(); |
850 | if (tty) { | |
47f86834 AC |
851 | unsigned long flags; |
852 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
ab521dc0 EB |
853 | put_pid(tty->session); |
854 | put_pid(tty->pgrp); | |
855 | tty->session = NULL; | |
856 | tty->pgrp = NULL; | |
47f86834 | 857 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
452a00d2 | 858 | tty_kref_put(tty); |
24ec839c PZ |
859 | } else { |
860 | #ifdef TTY_DEBUG_HANGUP | |
861 | printk(KERN_DEBUG "error attempted to write to tty [0x%p]" | |
862 | " = NULL", tty); | |
863 | #endif | |
864 | } | |
1da177e4 LT |
865 | |
866 | /* Now clear signal->tty under the lock */ | |
867 | read_lock(&tasklist_lock); | |
ab521dc0 | 868 | session_clear_tty(task_session(current)); |
1da177e4 | 869 | read_unlock(&tasklist_lock); |
1da177e4 LT |
870 | } |
871 | ||
98a27ba4 EB |
872 | /** |
873 | * | |
874 | * no_tty - Ensure the current process does not have a controlling tty | |
875 | */ | |
876 | void no_tty(void) | |
877 | { | |
3af502b9 AC |
878 | /* FIXME: Review locking here. The tty_lock never covered any race |
879 | between a new association and proc_clear_tty but possible we need | |
880 | to protect against this anyway */ | |
98a27ba4 | 881 | struct task_struct *tsk = current; |
5ec93d11 | 882 | disassociate_ctty(0); |
98a27ba4 EB |
883 | proc_clear_tty(tsk); |
884 | } | |
885 | ||
af9b897e AC |
886 | |
887 | /** | |
beb7dd86 | 888 | * stop_tty - propagate flow control |
af9b897e AC |
889 | * @tty: tty to stop |
890 | * | |
891 | * Perform flow control to the driver. For PTY/TTY pairs we | |
beb7dd86 | 892 | * must also propagate the TIOCKPKT status. May be called |
af9b897e AC |
893 | * on an already stopped device and will not re-call the driver |
894 | * method. | |
895 | * | |
896 | * This functionality is used by both the line disciplines for | |
897 | * halting incoming flow and by the driver. It may therefore be | |
898 | * called from any context, may be under the tty atomic_write_lock | |
899 | * but not always. | |
900 | * | |
901 | * Locking: | |
04f378b1 | 902 | * Uses the tty control lock internally |
af9b897e AC |
903 | */ |
904 | ||
1da177e4 LT |
905 | void stop_tty(struct tty_struct *tty) |
906 | { | |
04f378b1 AC |
907 | unsigned long flags; |
908 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
909 | if (tty->stopped) { | |
910 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
1da177e4 | 911 | return; |
04f378b1 | 912 | } |
1da177e4 LT |
913 | tty->stopped = 1; |
914 | if (tty->link && tty->link->packet) { | |
915 | tty->ctrl_status &= ~TIOCPKT_START; | |
916 | tty->ctrl_status |= TIOCPKT_STOP; | |
4b19449d | 917 | wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); |
1da177e4 | 918 | } |
04f378b1 | 919 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
f34d7a5b AC |
920 | if (tty->ops->stop) |
921 | (tty->ops->stop)(tty); | |
1da177e4 LT |
922 | } |
923 | ||
924 | EXPORT_SYMBOL(stop_tty); | |
925 | ||
af9b897e | 926 | /** |
beb7dd86 | 927 | * start_tty - propagate flow control |
af9b897e AC |
928 | * @tty: tty to start |
929 | * | |
930 | * Start a tty that has been stopped if at all possible. Perform | |
3a4fa0a2 | 931 | * any necessary wakeups and propagate the TIOCPKT status. If this |
af9b897e AC |
932 | * is the tty was previous stopped and is being started then the |
933 | * driver start method is invoked and the line discipline woken. | |
934 | * | |
935 | * Locking: | |
04f378b1 | 936 | * ctrl_lock |
af9b897e AC |
937 | */ |
938 | ||
1da177e4 LT |
939 | void start_tty(struct tty_struct *tty) |
940 | { | |
04f378b1 AC |
941 | unsigned long flags; |
942 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
943 | if (!tty->stopped || tty->flow_stopped) { | |
944 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
1da177e4 | 945 | return; |
04f378b1 | 946 | } |
1da177e4 LT |
947 | tty->stopped = 0; |
948 | if (tty->link && tty->link->packet) { | |
949 | tty->ctrl_status &= ~TIOCPKT_STOP; | |
950 | tty->ctrl_status |= TIOCPKT_START; | |
4b19449d | 951 | wake_up_interruptible_poll(&tty->link->read_wait, POLLIN); |
1da177e4 | 952 | } |
04f378b1 | 953 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
f34d7a5b AC |
954 | if (tty->ops->start) |
955 | (tty->ops->start)(tty); | |
1da177e4 LT |
956 | /* If we have a running line discipline it may need kicking */ |
957 | tty_wakeup(tty); | |
1da177e4 LT |
958 | } |
959 | ||
960 | EXPORT_SYMBOL(start_tty); | |
961 | ||
af9b897e AC |
962 | /** |
963 | * tty_read - read method for tty device files | |
964 | * @file: pointer to tty file | |
965 | * @buf: user buffer | |
966 | * @count: size of user buffer | |
967 | * @ppos: unused | |
968 | * | |
969 | * Perform the read system call function on this terminal device. Checks | |
970 | * for hung up devices before calling the line discipline method. | |
971 | * | |
972 | * Locking: | |
47f86834 AC |
973 | * Locks the line discipline internally while needed. Multiple |
974 | * read calls may be outstanding in parallel. | |
af9b897e AC |
975 | */ |
976 | ||
37bdfb07 | 977 | static ssize_t tty_read(struct file *file, char __user *buf, size_t count, |
1da177e4 LT |
978 | loff_t *ppos) |
979 | { | |
980 | int i; | |
d996b62a | 981 | struct tty_struct *tty = file_tty(file); |
1da177e4 LT |
982 | struct tty_ldisc *ld; |
983 | ||
6131ffaa | 984 | if (tty_paranoia_check(tty, file_inode(file), "tty_read")) |
1da177e4 LT |
985 | return -EIO; |
986 | if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) | |
987 | return -EIO; | |
988 | ||
989 | /* We want to wait for the line discipline to sort out in this | |
990 | situation */ | |
991 | ld = tty_ldisc_ref_wait(tty); | |
a352def2 AC |
992 | if (ld->ops->read) |
993 | i = (ld->ops->read)(tty, file, buf, count); | |
1da177e4 LT |
994 | else |
995 | i = -EIO; | |
996 | tty_ldisc_deref(ld); | |
b0de59b5 | 997 | |
1da177e4 LT |
998 | return i; |
999 | } | |
1000 | ||
9c1729db | 1001 | void tty_write_unlock(struct tty_struct *tty) |
83c67571 | 1002 | __releases(&tty->atomic_write_lock) |
9c1729db AC |
1003 | { |
1004 | mutex_unlock(&tty->atomic_write_lock); | |
4b19449d | 1005 | wake_up_interruptible_poll(&tty->write_wait, POLLOUT); |
9c1729db AC |
1006 | } |
1007 | ||
1008 | int tty_write_lock(struct tty_struct *tty, int ndelay) | |
83c67571 | 1009 | __acquires(&tty->atomic_write_lock) |
9c1729db AC |
1010 | { |
1011 | if (!mutex_trylock(&tty->atomic_write_lock)) { | |
1012 | if (ndelay) | |
1013 | return -EAGAIN; | |
1014 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) | |
1015 | return -ERESTARTSYS; | |
1016 | } | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1da177e4 LT |
1020 | /* |
1021 | * Split writes up in sane blocksizes to avoid | |
1022 | * denial-of-service type attacks | |
1023 | */ | |
1024 | static inline ssize_t do_tty_write( | |
1025 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), | |
1026 | struct tty_struct *tty, | |
1027 | struct file *file, | |
1028 | const char __user *buf, | |
1029 | size_t count) | |
1030 | { | |
9c1729db | 1031 | ssize_t ret, written = 0; |
1da177e4 | 1032 | unsigned int chunk; |
37bdfb07 | 1033 | |
9c1729db AC |
1034 | ret = tty_write_lock(tty, file->f_flags & O_NDELAY); |
1035 | if (ret < 0) | |
1036 | return ret; | |
1da177e4 LT |
1037 | |
1038 | /* | |
1039 | * We chunk up writes into a temporary buffer. This | |
1040 | * simplifies low-level drivers immensely, since they | |
1041 | * don't have locking issues and user mode accesses. | |
1042 | * | |
1043 | * But if TTY_NO_WRITE_SPLIT is set, we should use a | |
1044 | * big chunk-size.. | |
1045 | * | |
1046 | * The default chunk-size is 2kB, because the NTTY | |
1047 | * layer has problems with bigger chunks. It will | |
1048 | * claim to be able to handle more characters than | |
1049 | * it actually does. | |
af9b897e AC |
1050 | * |
1051 | * FIXME: This can probably go away now except that 64K chunks | |
1052 | * are too likely to fail unless switched to vmalloc... | |
1da177e4 LT |
1053 | */ |
1054 | chunk = 2048; | |
1055 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) | |
1056 | chunk = 65536; | |
1057 | if (count < chunk) | |
1058 | chunk = count; | |
1059 | ||
70522e12 | 1060 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ |
1da177e4 | 1061 | if (tty->write_cnt < chunk) { |
402fda92 | 1062 | unsigned char *buf_chunk; |
1da177e4 LT |
1063 | |
1064 | if (chunk < 1024) | |
1065 | chunk = 1024; | |
1066 | ||
402fda92 JW |
1067 | buf_chunk = kmalloc(chunk, GFP_KERNEL); |
1068 | if (!buf_chunk) { | |
9c1729db AC |
1069 | ret = -ENOMEM; |
1070 | goto out; | |
1da177e4 LT |
1071 | } |
1072 | kfree(tty->write_buf); | |
1073 | tty->write_cnt = chunk; | |
402fda92 | 1074 | tty->write_buf = buf_chunk; |
1da177e4 LT |
1075 | } |
1076 | ||
1077 | /* Do the write .. */ | |
1078 | for (;;) { | |
1079 | size_t size = count; | |
1080 | if (size > chunk) | |
1081 | size = chunk; | |
1082 | ret = -EFAULT; | |
1083 | if (copy_from_user(tty->write_buf, buf, size)) | |
1084 | break; | |
1da177e4 | 1085 | ret = write(tty, file, tty->write_buf, size); |
1da177e4 LT |
1086 | if (ret <= 0) |
1087 | break; | |
1088 | written += ret; | |
1089 | buf += ret; | |
1090 | count -= ret; | |
1091 | if (!count) | |
1092 | break; | |
1093 | ret = -ERESTARTSYS; | |
1094 | if (signal_pending(current)) | |
1095 | break; | |
1096 | cond_resched(); | |
1097 | } | |
b0de59b5 | 1098 | if (written) |
1da177e4 | 1099 | ret = written; |
9c1729db AC |
1100 | out: |
1101 | tty_write_unlock(tty); | |
1da177e4 LT |
1102 | return ret; |
1103 | } | |
1104 | ||
95f9bfc6 AC |
1105 | /** |
1106 | * tty_write_message - write a message to a certain tty, not just the console. | |
1107 | * @tty: the destination tty_struct | |
1108 | * @msg: the message to write | |
1109 | * | |
1110 | * This is used for messages that need to be redirected to a specific tty. | |
1111 | * We don't put it into the syslog queue right now maybe in the future if | |
1112 | * really needed. | |
1113 | * | |
ec79d605 | 1114 | * We must still hold the BTM and test the CLOSING flag for the moment. |
95f9bfc6 AC |
1115 | */ |
1116 | ||
1117 | void tty_write_message(struct tty_struct *tty, char *msg) | |
1118 | { | |
95f9bfc6 AC |
1119 | if (tty) { |
1120 | mutex_lock(&tty->atomic_write_lock); | |
89c8d91e | 1121 | tty_lock(tty); |
eeb89d91 | 1122 | if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags)) { |
89c8d91e | 1123 | tty_unlock(tty); |
95f9bfc6 | 1124 | tty->ops->write(tty, msg, strlen(msg)); |
eeb89d91 | 1125 | } else |
89c8d91e | 1126 | tty_unlock(tty); |
95f9bfc6 AC |
1127 | tty_write_unlock(tty); |
1128 | } | |
95f9bfc6 AC |
1129 | return; |
1130 | } | |
1131 | ||
1da177e4 | 1132 | |
af9b897e AC |
1133 | /** |
1134 | * tty_write - write method for tty device file | |
1135 | * @file: tty file pointer | |
1136 | * @buf: user data to write | |
1137 | * @count: bytes to write | |
1138 | * @ppos: unused | |
1139 | * | |
1140 | * Write data to a tty device via the line discipline. | |
1141 | * | |
1142 | * Locking: | |
1143 | * Locks the line discipline as required | |
1144 | * Writes to the tty driver are serialized by the atomic_write_lock | |
1145 | * and are then processed in chunks to the device. The line discipline | |
a88a69c9 | 1146 | * write method will not be invoked in parallel for each device. |
af9b897e AC |
1147 | */ |
1148 | ||
37bdfb07 AC |
1149 | static ssize_t tty_write(struct file *file, const char __user *buf, |
1150 | size_t count, loff_t *ppos) | |
1da177e4 | 1151 | { |
d996b62a NP |
1152 | struct tty_struct *tty = file_tty(file); |
1153 | struct tty_ldisc *ld; | |
1da177e4 | 1154 | ssize_t ret; |
37bdfb07 | 1155 | |
6131ffaa | 1156 | if (tty_paranoia_check(tty, file_inode(file), "tty_write")) |
1da177e4 | 1157 | return -EIO; |
f34d7a5b | 1158 | if (!tty || !tty->ops->write || |
37bdfb07 AC |
1159 | (test_bit(TTY_IO_ERROR, &tty->flags))) |
1160 | return -EIO; | |
f34d7a5b AC |
1161 | /* Short term debug to catch buggy drivers */ |
1162 | if (tty->ops->write_room == NULL) | |
1163 | printk(KERN_ERR "tty driver %s lacks a write_room method.\n", | |
1164 | tty->driver->name); | |
37bdfb07 | 1165 | ld = tty_ldisc_ref_wait(tty); |
a352def2 | 1166 | if (!ld->ops->write) |
1da177e4 LT |
1167 | ret = -EIO; |
1168 | else | |
a352def2 | 1169 | ret = do_tty_write(ld->ops->write, tty, file, buf, count); |
1da177e4 LT |
1170 | tty_ldisc_deref(ld); |
1171 | return ret; | |
1172 | } | |
1173 | ||
37bdfb07 AC |
1174 | ssize_t redirected_tty_write(struct file *file, const char __user *buf, |
1175 | size_t count, loff_t *ppos) | |
1da177e4 LT |
1176 | { |
1177 | struct file *p = NULL; | |
1178 | ||
1179 | spin_lock(&redirect_lock); | |
cb0942b8 AV |
1180 | if (redirect) |
1181 | p = get_file(redirect); | |
1da177e4 LT |
1182 | spin_unlock(&redirect_lock); |
1183 | ||
1184 | if (p) { | |
1185 | ssize_t res; | |
1186 | res = vfs_write(p, buf, count, &p->f_pos); | |
1187 | fput(p); | |
1188 | return res; | |
1189 | } | |
1da177e4 LT |
1190 | return tty_write(file, buf, count, ppos); |
1191 | } | |
1192 | ||
1193 | static char ptychar[] = "pqrstuvwxyzabcde"; | |
1194 | ||
af9b897e AC |
1195 | /** |
1196 | * pty_line_name - generate name for a pty | |
1197 | * @driver: the tty driver in use | |
1198 | * @index: the minor number | |
1199 | * @p: output buffer of at least 6 bytes | |
1200 | * | |
1201 | * Generate a name from a driver reference and write it to the output | |
1202 | * buffer. | |
1203 | * | |
1204 | * Locking: None | |
1205 | */ | |
1206 | static void pty_line_name(struct tty_driver *driver, int index, char *p) | |
1da177e4 LT |
1207 | { |
1208 | int i = index + driver->name_base; | |
1209 | /* ->name is initialized to "ttyp", but "tty" is expected */ | |
1210 | sprintf(p, "%s%c%x", | |
37bdfb07 AC |
1211 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, |
1212 | ptychar[i >> 4 & 0xf], i & 0xf); | |
1da177e4 LT |
1213 | } |
1214 | ||
af9b897e | 1215 | /** |
8b0a88d5 | 1216 | * tty_line_name - generate name for a tty |
af9b897e AC |
1217 | * @driver: the tty driver in use |
1218 | * @index: the minor number | |
1219 | * @p: output buffer of at least 7 bytes | |
1220 | * | |
1221 | * Generate a name from a driver reference and write it to the output | |
1222 | * buffer. | |
1223 | * | |
1224 | * Locking: None | |
1225 | */ | |
1226 | static void tty_line_name(struct tty_driver *driver, int index, char *p) | |
1da177e4 | 1227 | { |
0019b408 JS |
1228 | if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE) |
1229 | strcpy(p, driver->name); | |
1230 | else | |
1231 | sprintf(p, "%s%d", driver->name, index + driver->name_base); | |
1da177e4 LT |
1232 | } |
1233 | ||
99f1fe18 AC |
1234 | /** |
1235 | * tty_driver_lookup_tty() - find an existing tty, if any | |
1236 | * @driver: the driver for the tty | |
1237 | * @idx: the minor number | |
23499705 | 1238 | * |
99f1fe18 | 1239 | * Return the tty, if found or ERR_PTR() otherwise. |
23499705 | 1240 | * |
99f1fe18 AC |
1241 | * Locking: tty_mutex must be held. If tty is found, the mutex must |
1242 | * be held until the 'fast-open' is also done. Will change once we | |
1243 | * have refcounting in the driver and per driver locking | |
23499705 | 1244 | */ |
a47d545f | 1245 | static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver, |
15f1a633 | 1246 | struct inode *inode, int idx) |
23499705 | 1247 | { |
99f1fe18 | 1248 | if (driver->ops->lookup) |
15f1a633 | 1249 | return driver->ops->lookup(driver, inode, idx); |
23499705 | 1250 | |
d4834267 | 1251 | return driver->ttys[idx]; |
23499705 SB |
1252 | } |
1253 | ||
bf970ee4 AC |
1254 | /** |
1255 | * tty_init_termios - helper for termios setup | |
1256 | * @tty: the tty to set up | |
1257 | * | |
1258 | * Initialise the termios structures for this tty. Thus runs under | |
1259 | * the tty_mutex currently so we can be relaxed about ordering. | |
1260 | */ | |
1261 | ||
1262 | int tty_init_termios(struct tty_struct *tty) | |
1263 | { | |
fe6e29fd | 1264 | struct ktermios *tp; |
bf970ee4 AC |
1265 | int idx = tty->index; |
1266 | ||
36b3c070 AC |
1267 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) |
1268 | tty->termios = tty->driver->init_termios; | |
1269 | else { | |
1270 | /* Check for lazy saved data */ | |
1271 | tp = tty->driver->termios[idx]; | |
1272 | if (tp != NULL) | |
1273 | tty->termios = *tp; | |
1274 | else | |
1275 | tty->termios = tty->driver->init_termios; | |
bf970ee4 | 1276 | } |
bf970ee4 | 1277 | /* Compatibility until drivers always set this */ |
adc8d746 AC |
1278 | tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios); |
1279 | tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios); | |
bf970ee4 AC |
1280 | return 0; |
1281 | } | |
fe1ae7fd | 1282 | EXPORT_SYMBOL_GPL(tty_init_termios); |
bf970ee4 | 1283 | |
66d450e8 JS |
1284 | int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty) |
1285 | { | |
1286 | int ret = tty_init_termios(tty); | |
1287 | if (ret) | |
1288 | return ret; | |
1289 | ||
1290 | tty_driver_kref_get(driver); | |
1291 | tty->count++; | |
1292 | driver->ttys[tty->index] = tty; | |
1293 | return 0; | |
1294 | } | |
1295 | EXPORT_SYMBOL_GPL(tty_standard_install); | |
1296 | ||
99f1fe18 | 1297 | /** |
8b0a88d5 AC |
1298 | * tty_driver_install_tty() - install a tty entry in the driver |
1299 | * @driver: the driver for the tty | |
1300 | * @tty: the tty | |
1301 | * | |
1302 | * Install a tty object into the driver tables. The tty->index field | |
bf970ee4 AC |
1303 | * will be set by the time this is called. This method is responsible |
1304 | * for ensuring any need additional structures are allocated and | |
1305 | * configured. | |
8b0a88d5 AC |
1306 | * |
1307 | * Locking: tty_mutex for now | |
1308 | */ | |
1309 | static int tty_driver_install_tty(struct tty_driver *driver, | |
1310 | struct tty_struct *tty) | |
1311 | { | |
66d450e8 JS |
1312 | return driver->ops->install ? driver->ops->install(driver, tty) : |
1313 | tty_standard_install(driver, tty); | |
8b0a88d5 AC |
1314 | } |
1315 | ||
1316 | /** | |
1317 | * tty_driver_remove_tty() - remove a tty from the driver tables | |
1318 | * @driver: the driver for the tty | |
1319 | * @idx: the minor number | |
1320 | * | |
1321 | * Remvoe a tty object from the driver tables. The tty->index field | |
1322 | * will be set by the time this is called. | |
1323 | * | |
1324 | * Locking: tty_mutex for now | |
1325 | */ | |
24d406a6 | 1326 | void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty) |
8b0a88d5 AC |
1327 | { |
1328 | if (driver->ops->remove) | |
1329 | driver->ops->remove(driver, tty); | |
1330 | else | |
1331 | driver->ttys[tty->index] = NULL; | |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * tty_reopen() - fast re-open of an open tty | |
1336 | * @tty - the tty to open | |
23499705 | 1337 | * |
99f1fe18 | 1338 | * Return 0 on success, -errno on error. |
23499705 | 1339 | * |
99f1fe18 AC |
1340 | * Locking: tty_mutex must be held from the time the tty was found |
1341 | * till this open completes. | |
23499705 | 1342 | */ |
99f1fe18 | 1343 | static int tty_reopen(struct tty_struct *tty) |
23499705 SB |
1344 | { |
1345 | struct tty_driver *driver = tty->driver; | |
1346 | ||
e2efafbf | 1347 | if (test_bit(TTY_CLOSING, &tty->flags) || |
acfa747b | 1348 | test_bit(TTY_HUPPING, &tty->flags) || |
e2efafbf | 1349 | test_bit(TTY_LDISC_CHANGING, &tty->flags)) |
23499705 SB |
1350 | return -EIO; |
1351 | ||
1352 | if (driver->type == TTY_DRIVER_TYPE_PTY && | |
1353 | driver->subtype == PTY_TYPE_MASTER) { | |
1354 | /* | |
1355 | * special case for PTY masters: only one open permitted, | |
1356 | * and the slave side open count is incremented as well. | |
1357 | */ | |
1358 | if (tty->count) | |
1359 | return -EIO; | |
1360 | ||
1361 | tty->link->count++; | |
1362 | } | |
1363 | tty->count++; | |
23499705 | 1364 | |
1aa4bed8 | 1365 | mutex_lock(&tty->ldisc_mutex); |
99f1fe18 | 1366 | WARN_ON(!test_bit(TTY_LDISC, &tty->flags)); |
1aa4bed8 | 1367 | mutex_unlock(&tty->ldisc_mutex); |
23499705 SB |
1368 | |
1369 | return 0; | |
1370 | } | |
1371 | ||
af9b897e | 1372 | /** |
d81ed103 | 1373 | * tty_init_dev - initialise a tty device |
af9b897e AC |
1374 | * @driver: tty driver we are opening a device on |
1375 | * @idx: device index | |
15582d36 | 1376 | * @ret_tty: returned tty structure |
af9b897e AC |
1377 | * |
1378 | * Prepare a tty device. This may not be a "new" clean device but | |
1379 | * could also be an active device. The pty drivers require special | |
1380 | * handling because of this. | |
1381 | * | |
1382 | * Locking: | |
1383 | * The function is called under the tty_mutex, which | |
1384 | * protects us from the tty struct or driver itself going away. | |
1385 | * | |
1386 | * On exit the tty device has the line discipline attached and | |
1387 | * a reference count of 1. If a pair was created for pty/tty use | |
1388 | * and the other was a pty master then it too has a reference count of 1. | |
1389 | * | |
1da177e4 | 1390 | * WSH 06/09/97: Rewritten to remove races and properly clean up after a |
70522e12 IM |
1391 | * failed open. The new code protects the open with a mutex, so it's |
1392 | * really quite straightforward. The mutex locking can probably be | |
1da177e4 LT |
1393 | * relaxed for the (most common) case of reopening a tty. |
1394 | */ | |
af9b897e | 1395 | |
593a27c4 | 1396 | struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx) |
1da177e4 | 1397 | { |
bf970ee4 | 1398 | struct tty_struct *tty; |
73ec06fc | 1399 | int retval; |
1da177e4 | 1400 | |
1da177e4 LT |
1401 | /* |
1402 | * First time open is complex, especially for PTY devices. | |
1403 | * This code guarantees that either everything succeeds and the | |
1404 | * TTY is ready for operation, or else the table slots are vacated | |
37bdfb07 | 1405 | * and the allocated memory released. (Except that the termios |
1da177e4 LT |
1406 | * and locked termios may be retained.) |
1407 | */ | |
1408 | ||
73ec06fc AC |
1409 | if (!try_module_get(driver->owner)) |
1410 | return ERR_PTR(-ENODEV); | |
1da177e4 | 1411 | |
1da177e4 | 1412 | tty = alloc_tty_struct(); |
d5543503 JS |
1413 | if (!tty) { |
1414 | retval = -ENOMEM; | |
1415 | goto err_module_put; | |
1416 | } | |
bf970ee4 | 1417 | initialize_tty_struct(tty, driver, idx); |
1da177e4 | 1418 | |
89c8d91e | 1419 | tty_lock(tty); |
73ec06fc | 1420 | retval = tty_driver_install_tty(driver, tty); |
d5543503 | 1421 | if (retval < 0) |
a9dccddb | 1422 | goto err_deinit_tty; |
8b0a88d5 | 1423 | |
04831dc1 JS |
1424 | if (!tty->port) |
1425 | tty->port = driver->ports[idx]; | |
1426 | ||
5d4121c0 JS |
1427 | WARN_RATELIMIT(!tty->port, |
1428 | "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n", | |
1429 | __func__, tty->driver->name); | |
1430 | ||
967fab69 JS |
1431 | tty->port->itty = tty; |
1432 | ||
37bdfb07 | 1433 | /* |
1da177e4 | 1434 | * Structures all installed ... call the ldisc open routines. |
d5698c28 CH |
1435 | * If we fail here just call release_tty to clean up. No need |
1436 | * to decrement the use counts, as release_tty doesn't care. | |
1da177e4 | 1437 | */ |
bf970ee4 | 1438 | retval = tty_ldisc_setup(tty, tty->link); |
01e1abb2 | 1439 | if (retval) |
d5543503 | 1440 | goto err_release_tty; |
89c8d91e | 1441 | /* Return the tty locked so that it cannot vanish under the caller */ |
73ec06fc | 1442 | return tty; |
1da177e4 | 1443 | |
a9dccddb | 1444 | err_deinit_tty: |
89c8d91e | 1445 | tty_unlock(tty); |
a9dccddb | 1446 | deinitialize_tty_struct(tty); |
d5543503 JS |
1447 | free_tty_struct(tty); |
1448 | err_module_put: | |
1da177e4 | 1449 | module_put(driver->owner); |
d5543503 | 1450 | return ERR_PTR(retval); |
1da177e4 | 1451 | |
d5698c28 | 1452 | /* call the tty release_tty routine to clean out this slot */ |
d5543503 | 1453 | err_release_tty: |
89c8d91e | 1454 | tty_unlock(tty); |
5a3c6b25 | 1455 | printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, " |
4050914f | 1456 | "clearing slot %d\n", idx); |
d5698c28 | 1457 | release_tty(tty, idx); |
73ec06fc | 1458 | return ERR_PTR(retval); |
1da177e4 LT |
1459 | } |
1460 | ||
feebed65 AC |
1461 | void tty_free_termios(struct tty_struct *tty) |
1462 | { | |
1463 | struct ktermios *tp; | |
1464 | int idx = tty->index; | |
36b3c070 AC |
1465 | |
1466 | /* If the port is going to reset then it has no termios to save */ | |
1467 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) | |
1468 | return; | |
1469 | ||
1470 | /* Stash the termios data */ | |
1471 | tp = tty->driver->termios[idx]; | |
1472 | if (tp == NULL) { | |
1473 | tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL); | |
1474 | if (tp == NULL) { | |
1475 | pr_warn("tty: no memory to save termios state.\n"); | |
1476 | return; | |
1477 | } | |
4ac5d705 | 1478 | tty->driver->termios[idx] = tp; |
feebed65 | 1479 | } |
36b3c070 | 1480 | *tp = tty->termios; |
feebed65 AC |
1481 | } |
1482 | EXPORT_SYMBOL(tty_free_termios); | |
1483 | ||
feebed65 | 1484 | |
af9b897e | 1485 | /** |
d5698c28 | 1486 | * release_one_tty - release tty structure memory |
9c9f4ded | 1487 | * @kref: kref of tty we are obliterating |
af9b897e AC |
1488 | * |
1489 | * Releases memory associated with a tty structure, and clears out the | |
1490 | * driver table slots. This function is called when a device is no longer | |
1491 | * in use. It also gets called when setup of a device fails. | |
1492 | * | |
1493 | * Locking: | |
af9b897e AC |
1494 | * takes the file list lock internally when working on the list |
1495 | * of ttys that the driver keeps. | |
b50989dc AC |
1496 | * |
1497 | * This method gets called from a work queue so that the driver private | |
f278a2f7 | 1498 | * cleanup ops can sleep (needed for USB at least) |
1da177e4 | 1499 | */ |
b50989dc | 1500 | static void release_one_tty(struct work_struct *work) |
1da177e4 | 1501 | { |
b50989dc AC |
1502 | struct tty_struct *tty = |
1503 | container_of(work, struct tty_struct, hangup_work); | |
6f967f78 | 1504 | struct tty_driver *driver = tty->driver; |
d5698c28 | 1505 | |
f278a2f7 DY |
1506 | if (tty->ops->cleanup) |
1507 | tty->ops->cleanup(tty); | |
1508 | ||
1da177e4 | 1509 | tty->magic = 0; |
7d7b93c1 | 1510 | tty_driver_kref_put(driver); |
6f967f78 | 1511 | module_put(driver->owner); |
d5698c28 | 1512 | |
ee2ffa0d | 1513 | spin_lock(&tty_files_lock); |
1da177e4 | 1514 | list_del_init(&tty->tty_files); |
ee2ffa0d | 1515 | spin_unlock(&tty_files_lock); |
d5698c28 | 1516 | |
6da8d866 ON |
1517 | put_pid(tty->pgrp); |
1518 | put_pid(tty->session); | |
1da177e4 LT |
1519 | free_tty_struct(tty); |
1520 | } | |
1521 | ||
b50989dc AC |
1522 | static void queue_release_one_tty(struct kref *kref) |
1523 | { | |
1524 | struct tty_struct *tty = container_of(kref, struct tty_struct, kref); | |
f278a2f7 | 1525 | |
b50989dc AC |
1526 | /* The hangup queue is now free so we can reuse it rather than |
1527 | waste a chunk of memory for each port */ | |
1528 | INIT_WORK(&tty->hangup_work, release_one_tty); | |
1529 | schedule_work(&tty->hangup_work); | |
1530 | } | |
1531 | ||
9c9f4ded AC |
1532 | /** |
1533 | * tty_kref_put - release a tty kref | |
1534 | * @tty: tty device | |
1535 | * | |
1536 | * Release a reference to a tty device and if need be let the kref | |
1537 | * layer destruct the object for us | |
1538 | */ | |
1539 | ||
1540 | void tty_kref_put(struct tty_struct *tty) | |
1541 | { | |
1542 | if (tty) | |
b50989dc | 1543 | kref_put(&tty->kref, queue_release_one_tty); |
9c9f4ded AC |
1544 | } |
1545 | EXPORT_SYMBOL(tty_kref_put); | |
1546 | ||
d5698c28 CH |
1547 | /** |
1548 | * release_tty - release tty structure memory | |
1549 | * | |
1550 | * Release both @tty and a possible linked partner (think pty pair), | |
1551 | * and decrement the refcount of the backing module. | |
1552 | * | |
1553 | * Locking: | |
d155255a | 1554 | * tty_mutex |
d5698c28 CH |
1555 | * takes the file list lock internally when working on the list |
1556 | * of ttys that the driver keeps. | |
9c9f4ded | 1557 | * |
d5698c28 CH |
1558 | */ |
1559 | static void release_tty(struct tty_struct *tty, int idx) | |
1560 | { | |
9c9f4ded AC |
1561 | /* This should always be true but check for the moment */ |
1562 | WARN_ON(tty->index != idx); | |
d155255a | 1563 | WARN_ON(!mutex_is_locked(&tty_mutex)); |
36b3c070 AC |
1564 | if (tty->ops->shutdown) |
1565 | tty->ops->shutdown(tty); | |
1566 | tty_free_termios(tty); | |
1567 | tty_driver_remove_tty(tty->driver, tty); | |
967fab69 | 1568 | tty->port->itty = NULL; |
36b3c070 | 1569 | |
d5698c28 | 1570 | if (tty->link) |
9c9f4ded AC |
1571 | tty_kref_put(tty->link); |
1572 | tty_kref_put(tty); | |
d5698c28 CH |
1573 | } |
1574 | ||
955787ca JS |
1575 | /** |
1576 | * tty_release_checks - check a tty before real release | |
1577 | * @tty: tty to check | |
1578 | * @o_tty: link of @tty (if any) | |
1579 | * @idx: index of the tty | |
1580 | * | |
1581 | * Performs some paranoid checking before true release of the @tty. | |
1582 | * This is a no-op unless TTY_PARANOIA_CHECK is defined. | |
1583 | */ | |
1584 | static int tty_release_checks(struct tty_struct *tty, struct tty_struct *o_tty, | |
1585 | int idx) | |
1586 | { | |
1587 | #ifdef TTY_PARANOIA_CHECK | |
1588 | if (idx < 0 || idx >= tty->driver->num) { | |
9de44bd6 JS |
1589 | printk(KERN_DEBUG "%s: bad idx when trying to free (%s)\n", |
1590 | __func__, tty->name); | |
955787ca JS |
1591 | return -1; |
1592 | } | |
1593 | ||
1594 | /* not much to check for devpts */ | |
1595 | if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) | |
1596 | return 0; | |
1597 | ||
1598 | if (tty != tty->driver->ttys[idx]) { | |
9de44bd6 JS |
1599 | printk(KERN_DEBUG "%s: driver.table[%d] not tty for (%s)\n", |
1600 | __func__, idx, tty->name); | |
955787ca JS |
1601 | return -1; |
1602 | } | |
955787ca JS |
1603 | if (tty->driver->other) { |
1604 | if (o_tty != tty->driver->other->ttys[idx]) { | |
9de44bd6 JS |
1605 | printk(KERN_DEBUG "%s: other->table[%d] not o_tty for (%s)\n", |
1606 | __func__, idx, tty->name); | |
955787ca JS |
1607 | return -1; |
1608 | } | |
955787ca | 1609 | if (o_tty->link != tty) { |
9de44bd6 | 1610 | printk(KERN_DEBUG "%s: bad pty pointers\n", __func__); |
955787ca JS |
1611 | return -1; |
1612 | } | |
1613 | } | |
1614 | #endif | |
1615 | return 0; | |
1616 | } | |
1617 | ||
eeb89d91 AC |
1618 | /** |
1619 | * tty_release - vfs callback for close | |
1620 | * @inode: inode of tty | |
1621 | * @filp: file pointer for handle to tty | |
1622 | * | |
1623 | * Called the last time each file handle is closed that references | |
1624 | * this tty. There may however be several such references. | |
1625 | * | |
1626 | * Locking: | |
1627 | * Takes bkl. See tty_release_dev | |
1628 | * | |
1da177e4 LT |
1629 | * Even releasing the tty structures is a tricky business.. We have |
1630 | * to be very careful that the structures are all released at the | |
1631 | * same time, as interrupts might otherwise get the wrong pointers. | |
1632 | * | |
1633 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could | |
1634 | * lead to double frees or releasing memory still in use. | |
1635 | */ | |
eeb89d91 AC |
1636 | |
1637 | int tty_release(struct inode *inode, struct file *filp) | |
1da177e4 | 1638 | { |
d996b62a NP |
1639 | struct tty_struct *tty = file_tty(filp); |
1640 | struct tty_struct *o_tty; | |
1da177e4 | 1641 | int pty_master, tty_closing, o_tty_closing, do_sleep; |
1da177e4 LT |
1642 | int idx; |
1643 | char buf[64]; | |
37bdfb07 | 1644 | |
9de44bd6 | 1645 | if (tty_paranoia_check(tty, inode, __func__)) |
eeb89d91 | 1646 | return 0; |
1da177e4 | 1647 | |
89c8d91e | 1648 | tty_lock(tty); |
9de44bd6 | 1649 | check_tty_count(tty, __func__); |
1da177e4 | 1650 | |
ec79d605 | 1651 | __tty_fasync(-1, filp, 0); |
1da177e4 LT |
1652 | |
1653 | idx = tty->index; | |
1654 | pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
1655 | tty->driver->subtype == PTY_TYPE_MASTER); | |
89c8d91e | 1656 | /* Review: parallel close */ |
1da177e4 LT |
1657 | o_tty = tty->link; |
1658 | ||
955787ca | 1659 | if (tty_release_checks(tty, o_tty, idx)) { |
89c8d91e | 1660 | tty_unlock(tty); |
eeb89d91 | 1661 | return 0; |
1da177e4 | 1662 | } |
1da177e4 LT |
1663 | |
1664 | #ifdef TTY_DEBUG_HANGUP | |
9de44bd6 JS |
1665 | printk(KERN_DEBUG "%s: %s (tty count=%d)...\n", __func__, |
1666 | tty_name(tty, buf), tty->count); | |
1da177e4 LT |
1667 | #endif |
1668 | ||
f34d7a5b AC |
1669 | if (tty->ops->close) |
1670 | tty->ops->close(tty, filp); | |
1da177e4 | 1671 | |
89c8d91e | 1672 | tty_unlock(tty); |
1da177e4 LT |
1673 | /* |
1674 | * Sanity check: if tty->count is going to zero, there shouldn't be | |
1675 | * any waiters on tty->read_wait or tty->write_wait. We test the | |
1676 | * wait queues and kick everyone out _before_ actually starting to | |
1677 | * close. This ensures that we won't block while releasing the tty | |
1678 | * structure. | |
1679 | * | |
1680 | * The test for the o_tty closing is necessary, since the master and | |
1681 | * slave sides may close in any order. If the slave side closes out | |
1682 | * first, its count will be one, since the master side holds an open. | |
1683 | * Thus this test wouldn't be triggered at the time the slave closes, | |
1684 | * so we do it now. | |
1685 | * | |
1686 | * Note that it's possible for the tty to be opened again while we're | |
1687 | * flushing out waiters. By recalculating the closing flags before | |
1688 | * each iteration we avoid any problems. | |
1689 | */ | |
1690 | while (1) { | |
1691 | /* Guard against races with tty->count changes elsewhere and | |
1692 | opens on /dev/tty */ | |
37bdfb07 | 1693 | |
70522e12 | 1694 | mutex_lock(&tty_mutex); |
89c8d91e | 1695 | tty_lock_pair(tty, o_tty); |
1da177e4 LT |
1696 | tty_closing = tty->count <= 1; |
1697 | o_tty_closing = o_tty && | |
1698 | (o_tty->count <= (pty_master ? 1 : 0)); | |
1da177e4 LT |
1699 | do_sleep = 0; |
1700 | ||
1701 | if (tty_closing) { | |
1702 | if (waitqueue_active(&tty->read_wait)) { | |
4b19449d | 1703 | wake_up_poll(&tty->read_wait, POLLIN); |
1da177e4 LT |
1704 | do_sleep++; |
1705 | } | |
1706 | if (waitqueue_active(&tty->write_wait)) { | |
4b19449d | 1707 | wake_up_poll(&tty->write_wait, POLLOUT); |
1da177e4 LT |
1708 | do_sleep++; |
1709 | } | |
1710 | } | |
1711 | if (o_tty_closing) { | |
1712 | if (waitqueue_active(&o_tty->read_wait)) { | |
4b19449d | 1713 | wake_up_poll(&o_tty->read_wait, POLLIN); |
1da177e4 LT |
1714 | do_sleep++; |
1715 | } | |
1716 | if (waitqueue_active(&o_tty->write_wait)) { | |
4b19449d | 1717 | wake_up_poll(&o_tty->write_wait, POLLOUT); |
1da177e4 LT |
1718 | do_sleep++; |
1719 | } | |
1720 | } | |
1721 | if (!do_sleep) | |
1722 | break; | |
1723 | ||
9de44bd6 JS |
1724 | printk(KERN_WARNING "%s: %s: read/write wait queue active!\n", |
1725 | __func__, tty_name(tty, buf)); | |
89c8d91e | 1726 | tty_unlock_pair(tty, o_tty); |
70522e12 | 1727 | mutex_unlock(&tty_mutex); |
1da177e4 | 1728 | schedule(); |
37bdfb07 | 1729 | } |
1da177e4 LT |
1730 | |
1731 | /* | |
37bdfb07 AC |
1732 | * The closing flags are now consistent with the open counts on |
1733 | * both sides, and we've completed the last operation that could | |
1da177e4 | 1734 | * block, so it's safe to proceed with closing. |
d155255a AC |
1735 | * |
1736 | * We must *not* drop the tty_mutex until we ensure that a further | |
1737 | * entry into tty_open can not pick up this tty. | |
1da177e4 | 1738 | */ |
1da177e4 LT |
1739 | if (pty_master) { |
1740 | if (--o_tty->count < 0) { | |
9de44bd6 JS |
1741 | printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n", |
1742 | __func__, o_tty->count, tty_name(o_tty, buf)); | |
1da177e4 LT |
1743 | o_tty->count = 0; |
1744 | } | |
1745 | } | |
1746 | if (--tty->count < 0) { | |
9de44bd6 JS |
1747 | printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n", |
1748 | __func__, tty->count, tty_name(tty, buf)); | |
1da177e4 LT |
1749 | tty->count = 0; |
1750 | } | |
37bdfb07 | 1751 | |
1da177e4 LT |
1752 | /* |
1753 | * We've decremented tty->count, so we need to remove this file | |
1754 | * descriptor off the tty->tty_files list; this serves two | |
1755 | * purposes: | |
1756 | * - check_tty_count sees the correct number of file descriptors | |
1757 | * associated with this tty. | |
1758 | * - do_tty_hangup no longer sees this file descriptor as | |
1759 | * something that needs to be handled for hangups. | |
1760 | */ | |
d996b62a | 1761 | tty_del_file(filp); |
1da177e4 LT |
1762 | |
1763 | /* | |
1764 | * Perform some housekeeping before deciding whether to return. | |
1765 | * | |
1766 | * Set the TTY_CLOSING flag if this was the last open. In the | |
1767 | * case of a pty we may have to wait around for the other side | |
1768 | * to close, and TTY_CLOSING makes sure we can't be reopened. | |
1769 | */ | |
37bdfb07 | 1770 | if (tty_closing) |
1da177e4 | 1771 | set_bit(TTY_CLOSING, &tty->flags); |
37bdfb07 | 1772 | if (o_tty_closing) |
1da177e4 LT |
1773 | set_bit(TTY_CLOSING, &o_tty->flags); |
1774 | ||
1775 | /* | |
1776 | * If _either_ side is closing, make sure there aren't any | |
1777 | * processes that still think tty or o_tty is their controlling | |
1778 | * tty. | |
1779 | */ | |
1780 | if (tty_closing || o_tty_closing) { | |
1da177e4 | 1781 | read_lock(&tasklist_lock); |
24ec839c | 1782 | session_clear_tty(tty->session); |
1da177e4 | 1783 | if (o_tty) |
24ec839c | 1784 | session_clear_tty(o_tty->session); |
1da177e4 LT |
1785 | read_unlock(&tasklist_lock); |
1786 | } | |
1787 | ||
70522e12 | 1788 | mutex_unlock(&tty_mutex); |
89c8d91e | 1789 | tty_unlock_pair(tty, o_tty); |
d155255a AC |
1790 | /* At this point the TTY_CLOSING flag should ensure a dead tty |
1791 | cannot be re-opened by a racing opener */ | |
da965822 | 1792 | |
1da177e4 | 1793 | /* check whether both sides are closing ... */ |
d155255a | 1794 | if (!tty_closing || (o_tty && !o_tty_closing)) |
eeb89d91 | 1795 | return 0; |
37bdfb07 | 1796 | |
1da177e4 | 1797 | #ifdef TTY_DEBUG_HANGUP |
9de44bd6 | 1798 | printk(KERN_DEBUG "%s: freeing tty structure...\n", __func__); |
1da177e4 LT |
1799 | #endif |
1800 | /* | |
01e1abb2 | 1801 | * Ask the line discipline code to release its structures |
1da177e4 | 1802 | */ |
01e1abb2 | 1803 | tty_ldisc_release(tty, o_tty); |
1da177e4 | 1804 | /* |
d5698c28 | 1805 | * The release_tty function takes care of the details of clearing |
89c8d91e AC |
1806 | * the slots and preserving the termios structure. The tty_unlock_pair |
1807 | * should be safe as we keep a kref while the tty is locked (so the | |
1808 | * unlock never unlocks a freed tty). | |
1da177e4 | 1809 | */ |
d155255a | 1810 | mutex_lock(&tty_mutex); |
d5698c28 | 1811 | release_tty(tty, idx); |
d155255a | 1812 | mutex_unlock(&tty_mutex); |
1da177e4 | 1813 | |
eeb89d91 | 1814 | return 0; |
1da177e4 LT |
1815 | } |
1816 | ||
b82154ac JS |
1817 | /** |
1818 | * tty_open_current_tty - get tty of current task for open | |
1819 | * @device: device number | |
1820 | * @filp: file pointer to tty | |
1821 | * @return: tty of the current task iff @device is /dev/tty | |
1822 | * | |
1823 | * We cannot return driver and index like for the other nodes because | |
1824 | * devpts will not work then. It expects inodes to be from devpts FS. | |
3af502b9 AC |
1825 | * |
1826 | * We need to move to returning a refcounted object from all the lookup | |
1827 | * paths including this one. | |
b82154ac JS |
1828 | */ |
1829 | static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp) | |
1830 | { | |
1831 | struct tty_struct *tty; | |
1832 | ||
1833 | if (device != MKDEV(TTYAUX_MAJOR, 0)) | |
1834 | return NULL; | |
1835 | ||
1836 | tty = get_current_tty(); | |
1837 | if (!tty) | |
1838 | return ERR_PTR(-ENXIO); | |
1839 | ||
1840 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ | |
1841 | /* noctty = 1; */ | |
1842 | tty_kref_put(tty); | |
1843 | /* FIXME: we put a reference and return a TTY! */ | |
3af502b9 | 1844 | /* This is only safe because the caller holds tty_mutex */ |
b82154ac JS |
1845 | return tty; |
1846 | } | |
1847 | ||
5b5e7040 JS |
1848 | /** |
1849 | * tty_lookup_driver - lookup a tty driver for a given device file | |
1850 | * @device: device number | |
1851 | * @filp: file pointer to tty | |
1852 | * @noctty: set if the device should not become a controlling tty | |
1853 | * @index: index for the device in the @return driver | |
1854 | * @return: driver for this inode (with increased refcount) | |
1855 | * | |
1856 | * If @return is not erroneous, the caller is responsible to decrement the | |
1857 | * refcount by tty_driver_kref_put. | |
1858 | * | |
1859 | * Locking: tty_mutex protects get_tty_driver | |
1860 | */ | |
1861 | static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp, | |
1862 | int *noctty, int *index) | |
1863 | { | |
1864 | struct tty_driver *driver; | |
1865 | ||
2cd0050c | 1866 | switch (device) { |
5b5e7040 | 1867 | #ifdef CONFIG_VT |
2cd0050c | 1868 | case MKDEV(TTY_MAJOR, 0): { |
5b5e7040 JS |
1869 | extern struct tty_driver *console_driver; |
1870 | driver = tty_driver_kref_get(console_driver); | |
1871 | *index = fg_console; | |
1872 | *noctty = 1; | |
2cd0050c | 1873 | break; |
5b5e7040 JS |
1874 | } |
1875 | #endif | |
2cd0050c | 1876 | case MKDEV(TTYAUX_MAJOR, 1): { |
5b5e7040 JS |
1877 | struct tty_driver *console_driver = console_device(index); |
1878 | if (console_driver) { | |
1879 | driver = tty_driver_kref_get(console_driver); | |
1880 | if (driver) { | |
1881 | /* Don't let /dev/console block */ | |
1882 | filp->f_flags |= O_NONBLOCK; | |
1883 | *noctty = 1; | |
2cd0050c | 1884 | break; |
5b5e7040 JS |
1885 | } |
1886 | } | |
1887 | return ERR_PTR(-ENODEV); | |
1888 | } | |
2cd0050c JS |
1889 | default: |
1890 | driver = get_tty_driver(device, index); | |
1891 | if (!driver) | |
1892 | return ERR_PTR(-ENODEV); | |
1893 | break; | |
1894 | } | |
5b5e7040 JS |
1895 | return driver; |
1896 | } | |
1897 | ||
af9b897e | 1898 | /** |
eeb89d91 | 1899 | * tty_open - open a tty device |
af9b897e AC |
1900 | * @inode: inode of device file |
1901 | * @filp: file pointer to tty | |
1da177e4 | 1902 | * |
af9b897e AC |
1903 | * tty_open and tty_release keep up the tty count that contains the |
1904 | * number of opens done on a tty. We cannot use the inode-count, as | |
1905 | * different inodes might point to the same tty. | |
1da177e4 | 1906 | * |
af9b897e AC |
1907 | * Open-counting is needed for pty masters, as well as for keeping |
1908 | * track of serial lines: DTR is dropped when the last close happens. | |
1909 | * (This is not done solely through tty->count, now. - Ted 1/27/92) | |
1910 | * | |
1911 | * The termios state of a pty is reset on first open so that | |
1912 | * settings don't persist across reuse. | |
1913 | * | |
5b5e7040 | 1914 | * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev. |
24ec839c PZ |
1915 | * tty->count should protect the rest. |
1916 | * ->siglock protects ->signal/->sighand | |
89c8d91e AC |
1917 | * |
1918 | * Note: the tty_unlock/lock cases without a ref are only safe due to | |
1919 | * tty_mutex | |
1da177e4 | 1920 | */ |
af9b897e | 1921 | |
eeb89d91 | 1922 | static int tty_open(struct inode *inode, struct file *filp) |
1da177e4 | 1923 | { |
b82154ac | 1924 | struct tty_struct *tty; |
1da177e4 | 1925 | int noctty, retval; |
b82154ac | 1926 | struct tty_driver *driver = NULL; |
1da177e4 LT |
1927 | int index; |
1928 | dev_t device = inode->i_rdev; | |
846c151a | 1929 | unsigned saved_flags = filp->f_flags; |
1da177e4 LT |
1930 | |
1931 | nonseekable_open(inode, filp); | |
37bdfb07 | 1932 | |
1da177e4 | 1933 | retry_open: |
fa90e1c9 JS |
1934 | retval = tty_alloc_file(filp); |
1935 | if (retval) | |
1936 | return -ENOMEM; | |
1937 | ||
1da177e4 LT |
1938 | noctty = filp->f_flags & O_NOCTTY; |
1939 | index = -1; | |
1940 | retval = 0; | |
37bdfb07 | 1941 | |
70522e12 | 1942 | mutex_lock(&tty_mutex); |
89c8d91e | 1943 | /* This is protected by the tty_mutex */ |
b82154ac JS |
1944 | tty = tty_open_current_tty(device, filp); |
1945 | if (IS_ERR(tty)) { | |
ba5db448 JS |
1946 | retval = PTR_ERR(tty); |
1947 | goto err_unlock; | |
5b5e7040 JS |
1948 | } else if (!tty) { |
1949 | driver = tty_lookup_driver(device, filp, &noctty, &index); | |
1950 | if (IS_ERR(driver)) { | |
ba5db448 JS |
1951 | retval = PTR_ERR(driver); |
1952 | goto err_unlock; | |
1da177e4 | 1953 | } |
1da177e4 | 1954 | |
4a2b5fdd | 1955 | /* check whether we're reopening an existing tty */ |
15f1a633 | 1956 | tty = tty_driver_lookup_tty(driver, inode, index); |
808ffa3d | 1957 | if (IS_ERR(tty)) { |
ba5db448 JS |
1958 | retval = PTR_ERR(tty); |
1959 | goto err_unlock; | |
808ffa3d | 1960 | } |
4a2b5fdd SB |
1961 | } |
1962 | ||
1963 | if (tty) { | |
89c8d91e | 1964 | tty_lock(tty); |
4a2b5fdd | 1965 | retval = tty_reopen(tty); |
89c8d91e AC |
1966 | if (retval < 0) { |
1967 | tty_unlock(tty); | |
4a2b5fdd | 1968 | tty = ERR_PTR(retval); |
89c8d91e AC |
1969 | } |
1970 | } else /* Returns with the tty_lock held for now */ | |
593a27c4 | 1971 | tty = tty_init_dev(driver, index); |
4a2b5fdd | 1972 | |
70522e12 | 1973 | mutex_unlock(&tty_mutex); |
b82154ac JS |
1974 | if (driver) |
1975 | tty_driver_kref_put(driver); | |
eeb89d91 | 1976 | if (IS_ERR(tty)) { |
ba5db448 JS |
1977 | retval = PTR_ERR(tty); |
1978 | goto err_file; | |
eeb89d91 | 1979 | } |
1da177e4 | 1980 | |
fa90e1c9 | 1981 | tty_add_file(tty, filp); |
d996b62a | 1982 | |
9de44bd6 | 1983 | check_tty_count(tty, __func__); |
1da177e4 LT |
1984 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && |
1985 | tty->driver->subtype == PTY_TYPE_MASTER) | |
1986 | noctty = 1; | |
1987 | #ifdef TTY_DEBUG_HANGUP | |
9de44bd6 | 1988 | printk(KERN_DEBUG "%s: opening %s...\n", __func__, tty->name); |
1da177e4 | 1989 | #endif |
909bc774 HRK |
1990 | if (tty->ops->open) |
1991 | retval = tty->ops->open(tty, filp); | |
1992 | else | |
1993 | retval = -ENODEV; | |
1da177e4 LT |
1994 | filp->f_flags = saved_flags; |
1995 | ||
37bdfb07 AC |
1996 | if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && |
1997 | !capable(CAP_SYS_ADMIN)) | |
1da177e4 LT |
1998 | retval = -EBUSY; |
1999 | ||
2000 | if (retval) { | |
2001 | #ifdef TTY_DEBUG_HANGUP | |
9de44bd6 JS |
2002 | printk(KERN_DEBUG "%s: error %d in opening %s...\n", __func__, |
2003 | retval, tty->name); | |
1da177e4 | 2004 | #endif |
89c8d91e | 2005 | tty_unlock(tty); /* need to call tty_release without BTM */ |
eeb89d91 | 2006 | tty_release(inode, filp); |
64ba3dc3 | 2007 | if (retval != -ERESTARTSYS) |
1da177e4 | 2008 | return retval; |
64ba3dc3 AB |
2009 | |
2010 | if (signal_pending(current)) | |
1da177e4 | 2011 | return retval; |
64ba3dc3 | 2012 | |
1da177e4 LT |
2013 | schedule(); |
2014 | /* | |
2015 | * Need to reset f_op in case a hangup happened. | |
2016 | */ | |
2017 | if (filp->f_op == &hung_up_tty_fops) | |
2018 | filp->f_op = &tty_fops; | |
2019 | goto retry_open; | |
2020 | } | |
89c8d91e | 2021 | tty_unlock(tty); |
eeb89d91 | 2022 | |
24ec839c PZ |
2023 | |
2024 | mutex_lock(&tty_mutex); | |
89c8d91e | 2025 | tty_lock(tty); |
24ec839c | 2026 | spin_lock_irq(¤t->sighand->siglock); |
1da177e4 LT |
2027 | if (!noctty && |
2028 | current->signal->leader && | |
2029 | !current->signal->tty && | |
ab521dc0 | 2030 | tty->session == NULL) |
2a65f1d9 | 2031 | __proc_set_tty(current, tty); |
24ec839c | 2032 | spin_unlock_irq(¤t->sighand->siglock); |
89c8d91e | 2033 | tty_unlock(tty); |
24ec839c | 2034 | mutex_unlock(&tty_mutex); |
1da177e4 | 2035 | return 0; |
ba5db448 | 2036 | err_unlock: |
ba5db448 JS |
2037 | mutex_unlock(&tty_mutex); |
2038 | /* after locks to avoid deadlock */ | |
2039 | if (!IS_ERR_OR_NULL(driver)) | |
2040 | tty_driver_kref_put(driver); | |
2041 | err_file: | |
2042 | tty_free_file(filp); | |
2043 | return retval; | |
1da177e4 LT |
2044 | } |
2045 | ||
39d95b9d JC |
2046 | |
2047 | ||
af9b897e AC |
2048 | /** |
2049 | * tty_poll - check tty status | |
2050 | * @filp: file being polled | |
2051 | * @wait: poll wait structures to update | |
2052 | * | |
2053 | * Call the line discipline polling method to obtain the poll | |
2054 | * status of the device. | |
2055 | * | |
2056 | * Locking: locks called line discipline but ldisc poll method | |
2057 | * may be re-entered freely by other callers. | |
2058 | */ | |
2059 | ||
37bdfb07 | 2060 | static unsigned int tty_poll(struct file *filp, poll_table *wait) |
1da177e4 | 2061 | { |
d996b62a | 2062 | struct tty_struct *tty = file_tty(filp); |
1da177e4 LT |
2063 | struct tty_ldisc *ld; |
2064 | int ret = 0; | |
2065 | ||
6131ffaa | 2066 | if (tty_paranoia_check(tty, file_inode(filp), "tty_poll")) |
1da177e4 | 2067 | return 0; |
37bdfb07 | 2068 | |
1da177e4 | 2069 | ld = tty_ldisc_ref_wait(tty); |
a352def2 AC |
2070 | if (ld->ops->poll) |
2071 | ret = (ld->ops->poll)(tty, filp, wait); | |
1da177e4 LT |
2072 | tty_ldisc_deref(ld); |
2073 | return ret; | |
2074 | } | |
2075 | ||
ec79d605 | 2076 | static int __tty_fasync(int fd, struct file *filp, int on) |
1da177e4 | 2077 | { |
d996b62a | 2078 | struct tty_struct *tty = file_tty(filp); |
47f86834 | 2079 | unsigned long flags; |
5d1e3230 | 2080 | int retval = 0; |
1da177e4 | 2081 | |
6131ffaa | 2082 | if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync")) |
5d1e3230 | 2083 | goto out; |
37bdfb07 | 2084 | |
1da177e4 LT |
2085 | retval = fasync_helper(fd, filp, on, &tty->fasync); |
2086 | if (retval <= 0) | |
5d1e3230 | 2087 | goto out; |
1da177e4 LT |
2088 | |
2089 | if (on) { | |
ab521dc0 EB |
2090 | enum pid_type type; |
2091 | struct pid *pid; | |
1da177e4 LT |
2092 | if (!waitqueue_active(&tty->read_wait)) |
2093 | tty->minimum_to_wake = 1; | |
47f86834 | 2094 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
ab521dc0 EB |
2095 | if (tty->pgrp) { |
2096 | pid = tty->pgrp; | |
2097 | type = PIDTYPE_PGID; | |
2098 | } else { | |
2099 | pid = task_pid(current); | |
2100 | type = PIDTYPE_PID; | |
2101 | } | |
80e1e823 | 2102 | get_pid(pid); |
70362511 | 2103 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
80e1e823 LT |
2104 | retval = __f_setown(filp, pid, type, 0); |
2105 | put_pid(pid); | |
1da177e4 | 2106 | if (retval) |
5d1e3230 | 2107 | goto out; |
1da177e4 LT |
2108 | } else { |
2109 | if (!tty->fasync && !waitqueue_active(&tty->read_wait)) | |
2110 | tty->minimum_to_wake = N_TTY_BUF_SIZE; | |
2111 | } | |
5d1e3230 JC |
2112 | retval = 0; |
2113 | out: | |
ec79d605 AB |
2114 | return retval; |
2115 | } | |
2116 | ||
2117 | static int tty_fasync(int fd, struct file *filp, int on) | |
2118 | { | |
89c8d91e | 2119 | struct tty_struct *tty = file_tty(filp); |
ec79d605 | 2120 | int retval; |
89c8d91e AC |
2121 | |
2122 | tty_lock(tty); | |
ec79d605 | 2123 | retval = __tty_fasync(fd, filp, on); |
89c8d91e AC |
2124 | tty_unlock(tty); |
2125 | ||
5d1e3230 | 2126 | return retval; |
1da177e4 LT |
2127 | } |
2128 | ||
af9b897e AC |
2129 | /** |
2130 | * tiocsti - fake input character | |
2131 | * @tty: tty to fake input into | |
2132 | * @p: pointer to character | |
2133 | * | |
3a4fa0a2 | 2134 | * Fake input to a tty device. Does the necessary locking and |
af9b897e AC |
2135 | * input management. |
2136 | * | |
2137 | * FIXME: does not honour flow control ?? | |
2138 | * | |
2139 | * Locking: | |
2140 | * Called functions take tty_ldisc_lock | |
2141 | * current->signal->tty check is safe without locks | |
28298232 AC |
2142 | * |
2143 | * FIXME: may race normal receive processing | |
af9b897e AC |
2144 | */ |
2145 | ||
1da177e4 LT |
2146 | static int tiocsti(struct tty_struct *tty, char __user *p) |
2147 | { | |
2148 | char ch, mbz = 0; | |
2149 | struct tty_ldisc *ld; | |
37bdfb07 | 2150 | |
1da177e4 LT |
2151 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) |
2152 | return -EPERM; | |
2153 | if (get_user(ch, p)) | |
2154 | return -EFAULT; | |
1e641743 | 2155 | tty_audit_tiocsti(tty, ch); |
1da177e4 | 2156 | ld = tty_ldisc_ref_wait(tty); |
a352def2 | 2157 | ld->ops->receive_buf(tty, &ch, &mbz, 1); |
1da177e4 LT |
2158 | tty_ldisc_deref(ld); |
2159 | return 0; | |
2160 | } | |
2161 | ||
af9b897e AC |
2162 | /** |
2163 | * tiocgwinsz - implement window query ioctl | |
2164 | * @tty; tty | |
2165 | * @arg: user buffer for result | |
2166 | * | |
808a0d38 | 2167 | * Copies the kernel idea of the window size into the user buffer. |
af9b897e | 2168 | * |
24ec839c | 2169 | * Locking: tty->termios_mutex is taken to ensure the winsize data |
808a0d38 | 2170 | * is consistent. |
af9b897e AC |
2171 | */ |
2172 | ||
37bdfb07 | 2173 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg) |
1da177e4 | 2174 | { |
808a0d38 AC |
2175 | int err; |
2176 | ||
5785c95b | 2177 | mutex_lock(&tty->termios_mutex); |
808a0d38 | 2178 | err = copy_to_user(arg, &tty->winsize, sizeof(*arg)); |
5785c95b | 2179 | mutex_unlock(&tty->termios_mutex); |
808a0d38 AC |
2180 | |
2181 | return err ? -EFAULT: 0; | |
1da177e4 LT |
2182 | } |
2183 | ||
af9b897e | 2184 | /** |
8c9a9dd0 AC |
2185 | * tty_do_resize - resize event |
2186 | * @tty: tty being resized | |
8c9a9dd0 AC |
2187 | * @rows: rows (character) |
2188 | * @cols: cols (character) | |
2189 | * | |
3ad2f3fb | 2190 | * Update the termios variables and send the necessary signals to |
8c9a9dd0 | 2191 | * peform a terminal resize correctly |
af9b897e AC |
2192 | */ |
2193 | ||
fc6f6238 | 2194 | int tty_do_resize(struct tty_struct *tty, struct winsize *ws) |
1da177e4 | 2195 | { |
fc6f6238 | 2196 | struct pid *pgrp; |
47f86834 | 2197 | unsigned long flags; |
1da177e4 | 2198 | |
fc6f6238 AC |
2199 | /* Lock the tty */ |
2200 | mutex_lock(&tty->termios_mutex); | |
2201 | if (!memcmp(ws, &tty->winsize, sizeof(*ws))) | |
ca9bda00 | 2202 | goto done; |
47f86834 AC |
2203 | /* Get the PID values and reference them so we can |
2204 | avoid holding the tty ctrl lock while sending signals */ | |
2205 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
2206 | pgrp = get_pid(tty->pgrp); | |
47f86834 AC |
2207 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
2208 | ||
2209 | if (pgrp) | |
2210 | kill_pgrp(pgrp, SIGWINCH, 1); | |
47f86834 | 2211 | put_pid(pgrp); |
47f86834 | 2212 | |
8c9a9dd0 | 2213 | tty->winsize = *ws; |
ca9bda00 | 2214 | done: |
fc6f6238 | 2215 | mutex_unlock(&tty->termios_mutex); |
1da177e4 LT |
2216 | return 0; |
2217 | } | |
4d334fd1 | 2218 | EXPORT_SYMBOL(tty_do_resize); |
1da177e4 | 2219 | |
8c9a9dd0 AC |
2220 | /** |
2221 | * tiocswinsz - implement window size set ioctl | |
fc6f6238 | 2222 | * @tty; tty side of tty |
8c9a9dd0 AC |
2223 | * @arg: user buffer for result |
2224 | * | |
2225 | * Copies the user idea of the window size to the kernel. Traditionally | |
2226 | * this is just advisory information but for the Linux console it | |
2227 | * actually has driver level meaning and triggers a VC resize. | |
2228 | * | |
2229 | * Locking: | |
25985edc | 2230 | * Driver dependent. The default do_resize method takes the |
8c9a9dd0 AC |
2231 | * tty termios mutex and ctrl_lock. The console takes its own lock |
2232 | * then calls into the default method. | |
2233 | */ | |
2234 | ||
fc6f6238 | 2235 | static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg) |
8c9a9dd0 AC |
2236 | { |
2237 | struct winsize tmp_ws; | |
2238 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) | |
2239 | return -EFAULT; | |
2240 | ||
2241 | if (tty->ops->resize) | |
fc6f6238 | 2242 | return tty->ops->resize(tty, &tmp_ws); |
8c9a9dd0 | 2243 | else |
fc6f6238 | 2244 | return tty_do_resize(tty, &tmp_ws); |
8c9a9dd0 AC |
2245 | } |
2246 | ||
af9b897e AC |
2247 | /** |
2248 | * tioccons - allow admin to move logical console | |
2249 | * @file: the file to become console | |
2250 | * | |
25985edc | 2251 | * Allow the administrator to move the redirected console device |
af9b897e AC |
2252 | * |
2253 | * Locking: uses redirect_lock to guard the redirect information | |
2254 | */ | |
2255 | ||
1da177e4 LT |
2256 | static int tioccons(struct file *file) |
2257 | { | |
2258 | if (!capable(CAP_SYS_ADMIN)) | |
2259 | return -EPERM; | |
2260 | if (file->f_op->write == redirected_tty_write) { | |
2261 | struct file *f; | |
2262 | spin_lock(&redirect_lock); | |
2263 | f = redirect; | |
2264 | redirect = NULL; | |
2265 | spin_unlock(&redirect_lock); | |
2266 | if (f) | |
2267 | fput(f); | |
2268 | return 0; | |
2269 | } | |
2270 | spin_lock(&redirect_lock); | |
2271 | if (redirect) { | |
2272 | spin_unlock(&redirect_lock); | |
2273 | return -EBUSY; | |
2274 | } | |
cb0942b8 | 2275 | redirect = get_file(file); |
1da177e4 LT |
2276 | spin_unlock(&redirect_lock); |
2277 | return 0; | |
2278 | } | |
2279 | ||
af9b897e AC |
2280 | /** |
2281 | * fionbio - non blocking ioctl | |
2282 | * @file: file to set blocking value | |
2283 | * @p: user parameter | |
2284 | * | |
2285 | * Historical tty interfaces had a blocking control ioctl before | |
2286 | * the generic functionality existed. This piece of history is preserved | |
2287 | * in the expected tty API of posix OS's. | |
2288 | * | |
6146b9af | 2289 | * Locking: none, the open file handle ensures it won't go away. |
af9b897e | 2290 | */ |
1da177e4 LT |
2291 | |
2292 | static int fionbio(struct file *file, int __user *p) | |
2293 | { | |
2294 | int nonblock; | |
2295 | ||
2296 | if (get_user(nonblock, p)) | |
2297 | return -EFAULT; | |
2298 | ||
db1dd4d3 | 2299 | spin_lock(&file->f_lock); |
1da177e4 LT |
2300 | if (nonblock) |
2301 | file->f_flags |= O_NONBLOCK; | |
2302 | else | |
2303 | file->f_flags &= ~O_NONBLOCK; | |
db1dd4d3 | 2304 | spin_unlock(&file->f_lock); |
1da177e4 LT |
2305 | return 0; |
2306 | } | |
2307 | ||
af9b897e AC |
2308 | /** |
2309 | * tiocsctty - set controlling tty | |
2310 | * @tty: tty structure | |
2311 | * @arg: user argument | |
2312 | * | |
2313 | * This ioctl is used to manage job control. It permits a session | |
2314 | * leader to set this tty as the controlling tty for the session. | |
2315 | * | |
2316 | * Locking: | |
28298232 | 2317 | * Takes tty_mutex() to protect tty instance |
24ec839c PZ |
2318 | * Takes tasklist_lock internally to walk sessions |
2319 | * Takes ->siglock() when updating signal->tty | |
af9b897e AC |
2320 | */ |
2321 | ||
1da177e4 LT |
2322 | static int tiocsctty(struct tty_struct *tty, int arg) |
2323 | { | |
24ec839c | 2324 | int ret = 0; |
ab521dc0 | 2325 | if (current->signal->leader && (task_session(current) == tty->session)) |
24ec839c PZ |
2326 | return ret; |
2327 | ||
2328 | mutex_lock(&tty_mutex); | |
1da177e4 LT |
2329 | /* |
2330 | * The process must be a session leader and | |
2331 | * not have a controlling tty already. | |
2332 | */ | |
24ec839c PZ |
2333 | if (!current->signal->leader || current->signal->tty) { |
2334 | ret = -EPERM; | |
2335 | goto unlock; | |
2336 | } | |
2337 | ||
ab521dc0 | 2338 | if (tty->session) { |
1da177e4 LT |
2339 | /* |
2340 | * This tty is already the controlling | |
2341 | * tty for another session group! | |
2342 | */ | |
37bdfb07 | 2343 | if (arg == 1 && capable(CAP_SYS_ADMIN)) { |
1da177e4 LT |
2344 | /* |
2345 | * Steal it away | |
2346 | */ | |
1da177e4 | 2347 | read_lock(&tasklist_lock); |
24ec839c | 2348 | session_clear_tty(tty->session); |
1da177e4 | 2349 | read_unlock(&tasklist_lock); |
24ec839c PZ |
2350 | } else { |
2351 | ret = -EPERM; | |
2352 | goto unlock; | |
2353 | } | |
1da177e4 | 2354 | } |
24ec839c PZ |
2355 | proc_set_tty(current, tty); |
2356 | unlock: | |
28298232 | 2357 | mutex_unlock(&tty_mutex); |
24ec839c | 2358 | return ret; |
1da177e4 LT |
2359 | } |
2360 | ||
5d0fdf1e AC |
2361 | /** |
2362 | * tty_get_pgrp - return a ref counted pgrp pid | |
2363 | * @tty: tty to read | |
2364 | * | |
2365 | * Returns a refcounted instance of the pid struct for the process | |
2366 | * group controlling the tty. | |
2367 | */ | |
2368 | ||
2369 | struct pid *tty_get_pgrp(struct tty_struct *tty) | |
2370 | { | |
2371 | unsigned long flags; | |
2372 | struct pid *pgrp; | |
2373 | ||
2374 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
2375 | pgrp = get_pid(tty->pgrp); | |
2376 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); | |
2377 | ||
2378 | return pgrp; | |
2379 | } | |
2380 | EXPORT_SYMBOL_GPL(tty_get_pgrp); | |
2381 | ||
af9b897e AC |
2382 | /** |
2383 | * tiocgpgrp - get process group | |
2384 | * @tty: tty passed by user | |
25985edc | 2385 | * @real_tty: tty side of the tty passed by the user if a pty else the tty |
af9b897e AC |
2386 | * @p: returned pid |
2387 | * | |
2388 | * Obtain the process group of the tty. If there is no process group | |
2389 | * return an error. | |
2390 | * | |
24ec839c | 2391 | * Locking: none. Reference to current->signal->tty is safe. |
af9b897e AC |
2392 | */ |
2393 | ||
1da177e4 LT |
2394 | static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2395 | { | |
5d0fdf1e AC |
2396 | struct pid *pid; |
2397 | int ret; | |
1da177e4 LT |
2398 | /* |
2399 | * (tty == real_tty) is a cheap way of | |
2400 | * testing if the tty is NOT a master pty. | |
2401 | */ | |
2402 | if (tty == real_tty && current->signal->tty != real_tty) | |
2403 | return -ENOTTY; | |
5d0fdf1e AC |
2404 | pid = tty_get_pgrp(real_tty); |
2405 | ret = put_user(pid_vnr(pid), p); | |
2406 | put_pid(pid); | |
2407 | return ret; | |
1da177e4 LT |
2408 | } |
2409 | ||
af9b897e AC |
2410 | /** |
2411 | * tiocspgrp - attempt to set process group | |
2412 | * @tty: tty passed by user | |
2413 | * @real_tty: tty side device matching tty passed by user | |
2414 | * @p: pid pointer | |
2415 | * | |
2416 | * Set the process group of the tty to the session passed. Only | |
2417 | * permitted where the tty session is our session. | |
2418 | * | |
47f86834 | 2419 | * Locking: RCU, ctrl lock |
af9b897e AC |
2420 | */ |
2421 | ||
1da177e4 LT |
2422 | static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2423 | { | |
04a2e6a5 EB |
2424 | struct pid *pgrp; |
2425 | pid_t pgrp_nr; | |
1da177e4 | 2426 | int retval = tty_check_change(real_tty); |
47f86834 | 2427 | unsigned long flags; |
1da177e4 LT |
2428 | |
2429 | if (retval == -EIO) | |
2430 | return -ENOTTY; | |
2431 | if (retval) | |
2432 | return retval; | |
2433 | if (!current->signal->tty || | |
2434 | (current->signal->tty != real_tty) || | |
ab521dc0 | 2435 | (real_tty->session != task_session(current))) |
1da177e4 | 2436 | return -ENOTTY; |
04a2e6a5 | 2437 | if (get_user(pgrp_nr, p)) |
1da177e4 | 2438 | return -EFAULT; |
04a2e6a5 | 2439 | if (pgrp_nr < 0) |
1da177e4 | 2440 | return -EINVAL; |
04a2e6a5 | 2441 | rcu_read_lock(); |
b488893a | 2442 | pgrp = find_vpid(pgrp_nr); |
04a2e6a5 EB |
2443 | retval = -ESRCH; |
2444 | if (!pgrp) | |
2445 | goto out_unlock; | |
2446 | retval = -EPERM; | |
2447 | if (session_of_pgrp(pgrp) != task_session(current)) | |
2448 | goto out_unlock; | |
2449 | retval = 0; | |
47f86834 | 2450 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
ab521dc0 EB |
2451 | put_pid(real_tty->pgrp); |
2452 | real_tty->pgrp = get_pid(pgrp); | |
47f86834 | 2453 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
04a2e6a5 EB |
2454 | out_unlock: |
2455 | rcu_read_unlock(); | |
2456 | return retval; | |
1da177e4 LT |
2457 | } |
2458 | ||
af9b897e AC |
2459 | /** |
2460 | * tiocgsid - get session id | |
2461 | * @tty: tty passed by user | |
25985edc | 2462 | * @real_tty: tty side of the tty passed by the user if a pty else the tty |
af9b897e AC |
2463 | * @p: pointer to returned session id |
2464 | * | |
2465 | * Obtain the session id of the tty. If there is no session | |
2466 | * return an error. | |
2467 | * | |
24ec839c | 2468 | * Locking: none. Reference to current->signal->tty is safe. |
af9b897e AC |
2469 | */ |
2470 | ||
1da177e4 LT |
2471 | static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2472 | { | |
2473 | /* | |
2474 | * (tty == real_tty) is a cheap way of | |
2475 | * testing if the tty is NOT a master pty. | |
2476 | */ | |
2477 | if (tty == real_tty && current->signal->tty != real_tty) | |
2478 | return -ENOTTY; | |
ab521dc0 | 2479 | if (!real_tty->session) |
1da177e4 | 2480 | return -ENOTTY; |
b488893a | 2481 | return put_user(pid_vnr(real_tty->session), p); |
1da177e4 LT |
2482 | } |
2483 | ||
af9b897e AC |
2484 | /** |
2485 | * tiocsetd - set line discipline | |
2486 | * @tty: tty device | |
2487 | * @p: pointer to user data | |
2488 | * | |
2489 | * Set the line discipline according to user request. | |
2490 | * | |
2491 | * Locking: see tty_set_ldisc, this function is just a helper | |
2492 | */ | |
2493 | ||
1da177e4 LT |
2494 | static int tiocsetd(struct tty_struct *tty, int __user *p) |
2495 | { | |
2496 | int ldisc; | |
04f378b1 | 2497 | int ret; |
1da177e4 LT |
2498 | |
2499 | if (get_user(ldisc, p)) | |
2500 | return -EFAULT; | |
04f378b1 | 2501 | |
04f378b1 | 2502 | ret = tty_set_ldisc(tty, ldisc); |
04f378b1 AC |
2503 | |
2504 | return ret; | |
1da177e4 LT |
2505 | } |
2506 | ||
af9b897e AC |
2507 | /** |
2508 | * send_break - performed time break | |
2509 | * @tty: device to break on | |
2510 | * @duration: timeout in mS | |
2511 | * | |
2512 | * Perform a timed break on hardware that lacks its own driver level | |
2513 | * timed break functionality. | |
2514 | * | |
2515 | * Locking: | |
28298232 | 2516 | * atomic_write_lock serializes |
af9b897e | 2517 | * |
af9b897e AC |
2518 | */ |
2519 | ||
b20f3ae5 | 2520 | static int send_break(struct tty_struct *tty, unsigned int duration) |
1da177e4 | 2521 | { |
9e98966c AC |
2522 | int retval; |
2523 | ||
2524 | if (tty->ops->break_ctl == NULL) | |
2525 | return 0; | |
2526 | ||
2527 | if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK) | |
2528 | retval = tty->ops->break_ctl(tty, duration); | |
2529 | else { | |
2530 | /* Do the work ourselves */ | |
2531 | if (tty_write_lock(tty, 0) < 0) | |
2532 | return -EINTR; | |
2533 | retval = tty->ops->break_ctl(tty, -1); | |
2534 | if (retval) | |
2535 | goto out; | |
2536 | if (!signal_pending(current)) | |
2537 | msleep_interruptible(duration); | |
2538 | retval = tty->ops->break_ctl(tty, 0); | |
2539 | out: | |
2540 | tty_write_unlock(tty); | |
2541 | if (signal_pending(current)) | |
2542 | retval = -EINTR; | |
2543 | } | |
2544 | return retval; | |
1da177e4 LT |
2545 | } |
2546 | ||
af9b897e | 2547 | /** |
f34d7a5b | 2548 | * tty_tiocmget - get modem status |
af9b897e AC |
2549 | * @tty: tty device |
2550 | * @file: user file pointer | |
2551 | * @p: pointer to result | |
2552 | * | |
2553 | * Obtain the modem status bits from the tty driver if the feature | |
2554 | * is supported. Return -EINVAL if it is not available. | |
2555 | * | |
2556 | * Locking: none (up to the driver) | |
2557 | */ | |
2558 | ||
60b33c13 | 2559 | static int tty_tiocmget(struct tty_struct *tty, int __user *p) |
1da177e4 LT |
2560 | { |
2561 | int retval = -EINVAL; | |
2562 | ||
f34d7a5b | 2563 | if (tty->ops->tiocmget) { |
60b33c13 | 2564 | retval = tty->ops->tiocmget(tty); |
1da177e4 LT |
2565 | |
2566 | if (retval >= 0) | |
2567 | retval = put_user(retval, p); | |
2568 | } | |
2569 | return retval; | |
2570 | } | |
2571 | ||
af9b897e | 2572 | /** |
f34d7a5b | 2573 | * tty_tiocmset - set modem status |
af9b897e | 2574 | * @tty: tty device |
af9b897e AC |
2575 | * @cmd: command - clear bits, set bits or set all |
2576 | * @p: pointer to desired bits | |
2577 | * | |
2578 | * Set the modem status bits from the tty driver if the feature | |
2579 | * is supported. Return -EINVAL if it is not available. | |
2580 | * | |
2581 | * Locking: none (up to the driver) | |
2582 | */ | |
2583 | ||
20b9d177 | 2584 | static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd, |
1da177e4 LT |
2585 | unsigned __user *p) |
2586 | { | |
ae677517 AC |
2587 | int retval; |
2588 | unsigned int set, clear, val; | |
1da177e4 | 2589 | |
ae677517 AC |
2590 | if (tty->ops->tiocmset == NULL) |
2591 | return -EINVAL; | |
1da177e4 | 2592 | |
ae677517 AC |
2593 | retval = get_user(val, p); |
2594 | if (retval) | |
2595 | return retval; | |
2596 | set = clear = 0; | |
2597 | switch (cmd) { | |
2598 | case TIOCMBIS: | |
2599 | set = val; | |
2600 | break; | |
2601 | case TIOCMBIC: | |
2602 | clear = val; | |
2603 | break; | |
2604 | case TIOCMSET: | |
2605 | set = val; | |
2606 | clear = ~val; | |
2607 | break; | |
2608 | } | |
2609 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | |
2610 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | |
20b9d177 | 2611 | return tty->ops->tiocmset(tty, set, clear); |
1da177e4 LT |
2612 | } |
2613 | ||
d281da7f AC |
2614 | static int tty_tiocgicount(struct tty_struct *tty, void __user *arg) |
2615 | { | |
2616 | int retval = -EINVAL; | |
2617 | struct serial_icounter_struct icount; | |
2618 | memset(&icount, 0, sizeof(icount)); | |
2619 | if (tty->ops->get_icount) | |
2620 | retval = tty->ops->get_icount(tty, &icount); | |
2621 | if (retval != 0) | |
2622 | return retval; | |
2623 | if (copy_to_user(arg, &icount, sizeof(icount))) | |
2624 | return -EFAULT; | |
2625 | return 0; | |
2626 | } | |
2627 | ||
e8b70e7d AC |
2628 | struct tty_struct *tty_pair_get_tty(struct tty_struct *tty) |
2629 | { | |
2630 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
2631 | tty->driver->subtype == PTY_TYPE_MASTER) | |
2632 | tty = tty->link; | |
2633 | return tty; | |
2634 | } | |
2635 | EXPORT_SYMBOL(tty_pair_get_tty); | |
2636 | ||
2637 | struct tty_struct *tty_pair_get_pty(struct tty_struct *tty) | |
2638 | { | |
2639 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
2640 | tty->driver->subtype == PTY_TYPE_MASTER) | |
2641 | return tty; | |
2642 | return tty->link; | |
2643 | } | |
2644 | EXPORT_SYMBOL(tty_pair_get_pty); | |
2645 | ||
1da177e4 LT |
2646 | /* |
2647 | * Split this up, as gcc can choke on it otherwise.. | |
2648 | */ | |
04f378b1 | 2649 | long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
1da177e4 | 2650 | { |
d996b62a NP |
2651 | struct tty_struct *tty = file_tty(file); |
2652 | struct tty_struct *real_tty; | |
1da177e4 LT |
2653 | void __user *p = (void __user *)arg; |
2654 | int retval; | |
2655 | struct tty_ldisc *ld; | |
37bdfb07 | 2656 | |
6131ffaa | 2657 | if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) |
1da177e4 LT |
2658 | return -EINVAL; |
2659 | ||
e8b70e7d | 2660 | real_tty = tty_pair_get_tty(tty); |
1da177e4 LT |
2661 | |
2662 | /* | |
2663 | * Factor out some common prep work | |
2664 | */ | |
2665 | switch (cmd) { | |
2666 | case TIOCSETD: | |
2667 | case TIOCSBRK: | |
2668 | case TIOCCBRK: | |
2669 | case TCSBRK: | |
37bdfb07 | 2670 | case TCSBRKP: |
1da177e4 LT |
2671 | retval = tty_check_change(tty); |
2672 | if (retval) | |
2673 | return retval; | |
2674 | if (cmd != TIOCCBRK) { | |
2675 | tty_wait_until_sent(tty, 0); | |
2676 | if (signal_pending(current)) | |
2677 | return -EINTR; | |
2678 | } | |
2679 | break; | |
2680 | } | |
2681 | ||
9e98966c AC |
2682 | /* |
2683 | * Now do the stuff. | |
2684 | */ | |
1da177e4 | 2685 | switch (cmd) { |
37bdfb07 AC |
2686 | case TIOCSTI: |
2687 | return tiocsti(tty, p); | |
2688 | case TIOCGWINSZ: | |
8f520021 | 2689 | return tiocgwinsz(real_tty, p); |
37bdfb07 | 2690 | case TIOCSWINSZ: |
fc6f6238 | 2691 | return tiocswinsz(real_tty, p); |
37bdfb07 AC |
2692 | case TIOCCONS: |
2693 | return real_tty != tty ? -EINVAL : tioccons(file); | |
2694 | case FIONBIO: | |
2695 | return fionbio(file, p); | |
2696 | case TIOCEXCL: | |
2697 | set_bit(TTY_EXCLUSIVE, &tty->flags); | |
2698 | return 0; | |
2699 | case TIOCNXCL: | |
2700 | clear_bit(TTY_EXCLUSIVE, &tty->flags); | |
2701 | return 0; | |
84fd7bdf CG |
2702 | case TIOCGEXCL: |
2703 | { | |
2704 | int excl = test_bit(TTY_EXCLUSIVE, &tty->flags); | |
2705 | return put_user(excl, (int __user *)p); | |
2706 | } | |
37bdfb07 AC |
2707 | case TIOCNOTTY: |
2708 | if (current->signal->tty != tty) | |
2709 | return -ENOTTY; | |
2710 | no_tty(); | |
2711 | return 0; | |
2712 | case TIOCSCTTY: | |
2713 | return tiocsctty(tty, arg); | |
2714 | case TIOCGPGRP: | |
2715 | return tiocgpgrp(tty, real_tty, p); | |
2716 | case TIOCSPGRP: | |
2717 | return tiocspgrp(tty, real_tty, p); | |
2718 | case TIOCGSID: | |
2719 | return tiocgsid(tty, real_tty, p); | |
2720 | case TIOCGETD: | |
c65c9bc3 | 2721 | return put_user(tty->ldisc->ops->num, (int __user *)p); |
37bdfb07 AC |
2722 | case TIOCSETD: |
2723 | return tiocsetd(tty, p); | |
3c95c985 KS |
2724 | case TIOCVHANGUP: |
2725 | if (!capable(CAP_SYS_ADMIN)) | |
2726 | return -EPERM; | |
2727 | tty_vhangup(tty); | |
2728 | return 0; | |
b7b8de08 WF |
2729 | case TIOCGDEV: |
2730 | { | |
2731 | unsigned int ret = new_encode_dev(tty_devnum(real_tty)); | |
2732 | return put_user(ret, (unsigned int __user *)p); | |
2733 | } | |
37bdfb07 AC |
2734 | /* |
2735 | * Break handling | |
2736 | */ | |
2737 | case TIOCSBRK: /* Turn break on, unconditionally */ | |
f34d7a5b | 2738 | if (tty->ops->break_ctl) |
9e98966c | 2739 | return tty->ops->break_ctl(tty, -1); |
37bdfb07 | 2740 | return 0; |
37bdfb07 | 2741 | case TIOCCBRK: /* Turn break off, unconditionally */ |
f34d7a5b | 2742 | if (tty->ops->break_ctl) |
9e98966c | 2743 | return tty->ops->break_ctl(tty, 0); |
37bdfb07 AC |
2744 | return 0; |
2745 | case TCSBRK: /* SVID version: non-zero arg --> no break */ | |
2746 | /* non-zero arg means wait for all output data | |
2747 | * to be sent (performed above) but don't send break. | |
2748 | * This is used by the tcdrain() termios function. | |
2749 | */ | |
2750 | if (!arg) | |
2751 | return send_break(tty, 250); | |
2752 | return 0; | |
2753 | case TCSBRKP: /* support for POSIX tcsendbreak() */ | |
2754 | return send_break(tty, arg ? arg*100 : 250); | |
2755 | ||
2756 | case TIOCMGET: | |
60b33c13 | 2757 | return tty_tiocmget(tty, p); |
37bdfb07 AC |
2758 | case TIOCMSET: |
2759 | case TIOCMBIC: | |
2760 | case TIOCMBIS: | |
20b9d177 | 2761 | return tty_tiocmset(tty, cmd, p); |
d281da7f AC |
2762 | case TIOCGICOUNT: |
2763 | retval = tty_tiocgicount(tty, p); | |
2764 | /* For the moment allow fall through to the old method */ | |
2765 | if (retval != -EINVAL) | |
2766 | return retval; | |
2767 | break; | |
37bdfb07 AC |
2768 | case TCFLSH: |
2769 | switch (arg) { | |
2770 | case TCIFLUSH: | |
2771 | case TCIOFLUSH: | |
2772 | /* flush tty buffer and allow ldisc to process ioctl */ | |
2773 | tty_buffer_flush(tty); | |
c5c34d48 | 2774 | break; |
37bdfb07 AC |
2775 | } |
2776 | break; | |
1da177e4 | 2777 | } |
f34d7a5b | 2778 | if (tty->ops->ioctl) { |
6caa76b7 | 2779 | retval = (tty->ops->ioctl)(tty, cmd, arg); |
1da177e4 LT |
2780 | if (retval != -ENOIOCTLCMD) |
2781 | return retval; | |
2782 | } | |
2783 | ld = tty_ldisc_ref_wait(tty); | |
2784 | retval = -EINVAL; | |
a352def2 AC |
2785 | if (ld->ops->ioctl) { |
2786 | retval = ld->ops->ioctl(tty, file, cmd, arg); | |
1da177e4 | 2787 | if (retval == -ENOIOCTLCMD) |
bbb63c51 | 2788 | retval = -ENOTTY; |
1da177e4 LT |
2789 | } |
2790 | tty_ldisc_deref(ld); | |
2791 | return retval; | |
2792 | } | |
2793 | ||
e10cc1df | 2794 | #ifdef CONFIG_COMPAT |
37bdfb07 | 2795 | static long tty_compat_ioctl(struct file *file, unsigned int cmd, |
e10cc1df PF |
2796 | unsigned long arg) |
2797 | { | |
d996b62a | 2798 | struct tty_struct *tty = file_tty(file); |
e10cc1df PF |
2799 | struct tty_ldisc *ld; |
2800 | int retval = -ENOIOCTLCMD; | |
2801 | ||
6131ffaa | 2802 | if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl")) |
e10cc1df PF |
2803 | return -EINVAL; |
2804 | ||
f34d7a5b | 2805 | if (tty->ops->compat_ioctl) { |
6caa76b7 | 2806 | retval = (tty->ops->compat_ioctl)(tty, cmd, arg); |
e10cc1df PF |
2807 | if (retval != -ENOIOCTLCMD) |
2808 | return retval; | |
2809 | } | |
2810 | ||
2811 | ld = tty_ldisc_ref_wait(tty); | |
a352def2 AC |
2812 | if (ld->ops->compat_ioctl) |
2813 | retval = ld->ops->compat_ioctl(tty, file, cmd, arg); | |
8193c429 TM |
2814 | else |
2815 | retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg); | |
e10cc1df PF |
2816 | tty_ldisc_deref(ld); |
2817 | ||
2818 | return retval; | |
2819 | } | |
2820 | #endif | |
1da177e4 | 2821 | |
c3c073f8 AV |
2822 | static int this_tty(const void *t, struct file *file, unsigned fd) |
2823 | { | |
2824 | if (likely(file->f_op->read != tty_read)) | |
2825 | return 0; | |
2826 | return file_tty(file) != t ? 0 : fd + 1; | |
2827 | } | |
2828 | ||
1da177e4 LT |
2829 | /* |
2830 | * This implements the "Secure Attention Key" --- the idea is to | |
2831 | * prevent trojan horses by killing all processes associated with this | |
2832 | * tty when the user hits the "Secure Attention Key". Required for | |
2833 | * super-paranoid applications --- see the Orange Book for more details. | |
37bdfb07 | 2834 | * |
1da177e4 LT |
2835 | * This code could be nicer; ideally it should send a HUP, wait a few |
2836 | * seconds, then send a INT, and then a KILL signal. But you then | |
2837 | * have to coordinate with the init process, since all processes associated | |
2838 | * with the current tty must be dead before the new getty is allowed | |
2839 | * to spawn. | |
2840 | * | |
2841 | * Now, if it would be correct ;-/ The current code has a nasty hole - | |
2842 | * it doesn't catch files in flight. We may send the descriptor to ourselves | |
2843 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. | |
2844 | * | |
2845 | * Nasty bug: do_SAK is being called in interrupt context. This can | |
2846 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 | |
2847 | */ | |
8b6312f4 | 2848 | void __do_SAK(struct tty_struct *tty) |
1da177e4 LT |
2849 | { |
2850 | #ifdef TTY_SOFT_SAK | |
2851 | tty_hangup(tty); | |
2852 | #else | |
652486fb | 2853 | struct task_struct *g, *p; |
ab521dc0 | 2854 | struct pid *session; |
1da177e4 | 2855 | int i; |
37bdfb07 | 2856 | |
1da177e4 LT |
2857 | if (!tty) |
2858 | return; | |
24ec839c | 2859 | session = tty->session; |
37bdfb07 | 2860 | |
b3f13deb | 2861 | tty_ldisc_flush(tty); |
1da177e4 | 2862 | |
f34d7a5b | 2863 | tty_driver_flush_buffer(tty); |
37bdfb07 | 2864 | |
1da177e4 | 2865 | read_lock(&tasklist_lock); |
652486fb | 2866 | /* Kill the entire session */ |
ab521dc0 | 2867 | do_each_pid_task(session, PIDTYPE_SID, p) { |
652486fb | 2868 | printk(KERN_NOTICE "SAK: killed process %d" |
1b0f7ffd | 2869 | " (%s): task_session(p)==tty->session\n", |
ba25f9dc | 2870 | task_pid_nr(p), p->comm); |
652486fb | 2871 | send_sig(SIGKILL, p, 1); |
ab521dc0 | 2872 | } while_each_pid_task(session, PIDTYPE_SID, p); |
652486fb EB |
2873 | /* Now kill any processes that happen to have the |
2874 | * tty open. | |
2875 | */ | |
2876 | do_each_thread(g, p) { | |
2877 | if (p->signal->tty == tty) { | |
1da177e4 | 2878 | printk(KERN_NOTICE "SAK: killed process %d" |
1b0f7ffd | 2879 | " (%s): task_session(p)==tty->session\n", |
ba25f9dc | 2880 | task_pid_nr(p), p->comm); |
1da177e4 LT |
2881 | send_sig(SIGKILL, p, 1); |
2882 | continue; | |
2883 | } | |
2884 | task_lock(p); | |
c3c073f8 AV |
2885 | i = iterate_fd(p->files, 0, this_tty, tty); |
2886 | if (i != 0) { | |
2887 | printk(KERN_NOTICE "SAK: killed process %d" | |
2888 | " (%s): fd#%d opened to the tty\n", | |
2889 | task_pid_nr(p), p->comm, i - 1); | |
2890 | force_sig(SIGKILL, p); | |
1da177e4 LT |
2891 | } |
2892 | task_unlock(p); | |
652486fb | 2893 | } while_each_thread(g, p); |
1da177e4 LT |
2894 | read_unlock(&tasklist_lock); |
2895 | #endif | |
2896 | } | |
2897 | ||
8b6312f4 EB |
2898 | static void do_SAK_work(struct work_struct *work) |
2899 | { | |
2900 | struct tty_struct *tty = | |
2901 | container_of(work, struct tty_struct, SAK_work); | |
2902 | __do_SAK(tty); | |
2903 | } | |
2904 | ||
1da177e4 LT |
2905 | /* |
2906 | * The tq handling here is a little racy - tty->SAK_work may already be queued. | |
2907 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, | |
2908 | * the values which we write to it will be identical to the values which it | |
2909 | * already has. --akpm | |
2910 | */ | |
2911 | void do_SAK(struct tty_struct *tty) | |
2912 | { | |
2913 | if (!tty) | |
2914 | return; | |
1da177e4 LT |
2915 | schedule_work(&tty->SAK_work); |
2916 | } | |
2917 | ||
2918 | EXPORT_SYMBOL(do_SAK); | |
2919 | ||
6e9430ac | 2920 | static int dev_match_devt(struct device *dev, const void *data) |
30004ac9 | 2921 | { |
6e9430ac | 2922 | const dev_t *devt = data; |
30004ac9 DES |
2923 | return dev->devt == *devt; |
2924 | } | |
2925 | ||
2926 | /* Must put_device() after it's unused! */ | |
2927 | static struct device *tty_get_device(struct tty_struct *tty) | |
2928 | { | |
2929 | dev_t devt = tty_devnum(tty); | |
2930 | return class_find_device(tty_class, NULL, &devt, dev_match_devt); | |
2931 | } | |
2932 | ||
2933 | ||
af9b897e AC |
2934 | /** |
2935 | * initialize_tty_struct | |
2936 | * @tty: tty to initialize | |
2937 | * | |
2938 | * This subroutine initializes a tty structure that has been newly | |
2939 | * allocated. | |
2940 | * | |
2941 | * Locking: none - tty in question must not be exposed at this point | |
1da177e4 | 2942 | */ |
af9b897e | 2943 | |
bf970ee4 AC |
2944 | void initialize_tty_struct(struct tty_struct *tty, |
2945 | struct tty_driver *driver, int idx) | |
1da177e4 LT |
2946 | { |
2947 | memset(tty, 0, sizeof(struct tty_struct)); | |
9c9f4ded | 2948 | kref_init(&tty->kref); |
1da177e4 | 2949 | tty->magic = TTY_MAGIC; |
01e1abb2 | 2950 | tty_ldisc_init(tty); |
ab521dc0 EB |
2951 | tty->session = NULL; |
2952 | tty->pgrp = NULL; | |
89c8d91e | 2953 | mutex_init(&tty->legacy_mutex); |
5785c95b | 2954 | mutex_init(&tty->termios_mutex); |
c65c9bc3 | 2955 | mutex_init(&tty->ldisc_mutex); |
1da177e4 LT |
2956 | init_waitqueue_head(&tty->write_wait); |
2957 | init_waitqueue_head(&tty->read_wait); | |
65f27f38 | 2958 | INIT_WORK(&tty->hangup_work, do_tty_hangup); |
70522e12 | 2959 | mutex_init(&tty->atomic_write_lock); |
04f378b1 | 2960 | spin_lock_init(&tty->ctrl_lock); |
1da177e4 | 2961 | INIT_LIST_HEAD(&tty->tty_files); |
7f1f86a0 | 2962 | INIT_WORK(&tty->SAK_work, do_SAK_work); |
bf970ee4 AC |
2963 | |
2964 | tty->driver = driver; | |
2965 | tty->ops = driver->ops; | |
2966 | tty->index = idx; | |
2967 | tty_line_name(driver, idx, tty->name); | |
30004ac9 | 2968 | tty->dev = tty_get_device(tty); |
1da177e4 LT |
2969 | } |
2970 | ||
6716671d JS |
2971 | /** |
2972 | * deinitialize_tty_struct | |
2973 | * @tty: tty to deinitialize | |
2974 | * | |
2975 | * This subroutine deinitializes a tty structure that has been newly | |
2976 | * allocated but tty_release cannot be called on that yet. | |
2977 | * | |
2978 | * Locking: none - tty in question must not be exposed at this point | |
2979 | */ | |
2980 | void deinitialize_tty_struct(struct tty_struct *tty) | |
2981 | { | |
2982 | tty_ldisc_deinit(tty); | |
2983 | } | |
2984 | ||
f34d7a5b AC |
2985 | /** |
2986 | * tty_put_char - write one character to a tty | |
2987 | * @tty: tty | |
2988 | * @ch: character | |
2989 | * | |
2990 | * Write one byte to the tty using the provided put_char method | |
2991 | * if present. Returns the number of characters successfully output. | |
2992 | * | |
2993 | * Note: the specific put_char operation in the driver layer may go | |
2994 | * away soon. Don't call it directly, use this method | |
1da177e4 | 2995 | */ |
af9b897e | 2996 | |
f34d7a5b | 2997 | int tty_put_char(struct tty_struct *tty, unsigned char ch) |
1da177e4 | 2998 | { |
f34d7a5b AC |
2999 | if (tty->ops->put_char) |
3000 | return tty->ops->put_char(tty, ch); | |
3001 | return tty->ops->write(tty, &ch, 1); | |
1da177e4 | 3002 | } |
f34d7a5b AC |
3003 | EXPORT_SYMBOL_GPL(tty_put_char); |
3004 | ||
d81ed103 | 3005 | struct class *tty_class; |
1da177e4 | 3006 | |
7e73eca6 JS |
3007 | static int tty_cdev_add(struct tty_driver *driver, dev_t dev, |
3008 | unsigned int index, unsigned int count) | |
3009 | { | |
3010 | /* init here, since reused cdevs cause crashes */ | |
3011 | cdev_init(&driver->cdevs[index], &tty_fops); | |
3012 | driver->cdevs[index].owner = driver->owner; | |
3013 | return cdev_add(&driver->cdevs[index], dev, count); | |
3014 | } | |
3015 | ||
1da177e4 | 3016 | /** |
af9b897e AC |
3017 | * tty_register_device - register a tty device |
3018 | * @driver: the tty driver that describes the tty device | |
3019 | * @index: the index in the tty driver for this tty device | |
3020 | * @device: a struct device that is associated with this tty device. | |
3021 | * This field is optional, if there is no known struct device | |
3022 | * for this tty device it can be set to NULL safely. | |
1da177e4 | 3023 | * |
01107d34 GKH |
3024 | * Returns a pointer to the struct device for this tty device |
3025 | * (or ERR_PTR(-EFOO) on error). | |
1cdcb6b4 | 3026 | * |
af9b897e AC |
3027 | * This call is required to be made to register an individual tty device |
3028 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If | |
3029 | * that bit is not set, this function should not be called by a tty | |
3030 | * driver. | |
3031 | * | |
3032 | * Locking: ?? | |
1da177e4 | 3033 | */ |
af9b897e | 3034 | |
01107d34 GKH |
3035 | struct device *tty_register_device(struct tty_driver *driver, unsigned index, |
3036 | struct device *device) | |
1da177e4 | 3037 | { |
6915c0e4 TH |
3038 | return tty_register_device_attr(driver, index, device, NULL, NULL); |
3039 | } | |
3040 | EXPORT_SYMBOL(tty_register_device); | |
3041 | ||
b1b79916 TH |
3042 | static void tty_device_create_release(struct device *dev) |
3043 | { | |
3044 | pr_debug("device: '%s': %s\n", dev_name(dev), __func__); | |
3045 | kfree(dev); | |
3046 | } | |
3047 | ||
6915c0e4 TH |
3048 | /** |
3049 | * tty_register_device_attr - register a tty device | |
3050 | * @driver: the tty driver that describes the tty device | |
3051 | * @index: the index in the tty driver for this tty device | |
3052 | * @device: a struct device that is associated with this tty device. | |
3053 | * This field is optional, if there is no known struct device | |
3054 | * for this tty device it can be set to NULL safely. | |
3055 | * @drvdata: Driver data to be set to device. | |
3056 | * @attr_grp: Attribute group to be set on device. | |
3057 | * | |
3058 | * Returns a pointer to the struct device for this tty device | |
3059 | * (or ERR_PTR(-EFOO) on error). | |
3060 | * | |
3061 | * This call is required to be made to register an individual tty device | |
3062 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If | |
3063 | * that bit is not set, this function should not be called by a tty | |
3064 | * driver. | |
3065 | * | |
3066 | * Locking: ?? | |
3067 | */ | |
3068 | struct device *tty_register_device_attr(struct tty_driver *driver, | |
3069 | unsigned index, struct device *device, | |
3070 | void *drvdata, | |
3071 | const struct attribute_group **attr_grp) | |
3072 | { | |
1da177e4 | 3073 | char name[64]; |
6915c0e4 TH |
3074 | dev_t devt = MKDEV(driver->major, driver->minor_start) + index; |
3075 | struct device *dev = NULL; | |
3076 | int retval = -ENODEV; | |
7e73eca6 | 3077 | bool cdev = false; |
1da177e4 LT |
3078 | |
3079 | if (index >= driver->num) { | |
3080 | printk(KERN_ERR "Attempt to register invalid tty line number " | |
3081 | " (%d).\n", index); | |
1cdcb6b4 | 3082 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
3083 | } |
3084 | ||
1da177e4 LT |
3085 | if (driver->type == TTY_DRIVER_TYPE_PTY) |
3086 | pty_line_name(driver, index, name); | |
3087 | else | |
3088 | tty_line_name(driver, index, name); | |
1cdcb6b4 | 3089 | |
7e73eca6 | 3090 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) { |
6915c0e4 TH |
3091 | retval = tty_cdev_add(driver, devt, index, 1); |
3092 | if (retval) | |
3093 | goto error; | |
7e73eca6 JS |
3094 | cdev = true; |
3095 | } | |
3096 | ||
6915c0e4 TH |
3097 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
3098 | if (!dev) { | |
3099 | retval = -ENOMEM; | |
3100 | goto error; | |
3101 | } | |
7e73eca6 | 3102 | |
6915c0e4 TH |
3103 | dev->devt = devt; |
3104 | dev->class = tty_class; | |
3105 | dev->parent = device; | |
b1b79916 | 3106 | dev->release = tty_device_create_release; |
6915c0e4 TH |
3107 | dev_set_name(dev, "%s", name); |
3108 | dev->groups = attr_grp; | |
3109 | dev_set_drvdata(dev, drvdata); | |
3110 | ||
3111 | retval = device_register(dev); | |
3112 | if (retval) | |
3113 | goto error; | |
3114 | ||
3115 | return dev; | |
3116 | ||
3117 | error: | |
3118 | put_device(dev); | |
3119 | if (cdev) | |
3120 | cdev_del(&driver->cdevs[index]); | |
3121 | return ERR_PTR(retval); | |
1da177e4 | 3122 | } |
6915c0e4 | 3123 | EXPORT_SYMBOL_GPL(tty_register_device_attr); |
1da177e4 LT |
3124 | |
3125 | /** | |
af9b897e AC |
3126 | * tty_unregister_device - unregister a tty device |
3127 | * @driver: the tty driver that describes the tty device | |
3128 | * @index: the index in the tty driver for this tty device | |
1da177e4 | 3129 | * |
af9b897e AC |
3130 | * If a tty device is registered with a call to tty_register_device() then |
3131 | * this function must be called when the tty device is gone. | |
3132 | * | |
3133 | * Locking: ?? | |
1da177e4 | 3134 | */ |
af9b897e | 3135 | |
1da177e4 LT |
3136 | void tty_unregister_device(struct tty_driver *driver, unsigned index) |
3137 | { | |
37bdfb07 AC |
3138 | device_destroy(tty_class, |
3139 | MKDEV(driver->major, driver->minor_start) + index); | |
7e73eca6 JS |
3140 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) |
3141 | cdev_del(&driver->cdevs[index]); | |
1da177e4 | 3142 | } |
1da177e4 LT |
3143 | EXPORT_SYMBOL(tty_unregister_device); |
3144 | ||
7f0bc6a6 JS |
3145 | /** |
3146 | * __tty_alloc_driver -- allocate tty driver | |
3147 | * @lines: count of lines this driver can handle at most | |
3148 | * @owner: module which is repsonsible for this driver | |
3149 | * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags | |
3150 | * | |
3151 | * This should not be called directly, some of the provided macros should be | |
3152 | * used instead. Use IS_ERR and friends on @retval. | |
3153 | */ | |
3154 | struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner, | |
3155 | unsigned long flags) | |
1da177e4 LT |
3156 | { |
3157 | struct tty_driver *driver; | |
7e73eca6 | 3158 | unsigned int cdevs = 1; |
16a02081 | 3159 | int err; |
1da177e4 | 3160 | |
0019b408 | 3161 | if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1)) |
7f0bc6a6 JS |
3162 | return ERR_PTR(-EINVAL); |
3163 | ||
506eb99a | 3164 | driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL); |
7f0bc6a6 JS |
3165 | if (!driver) |
3166 | return ERR_PTR(-ENOMEM); | |
3167 | ||
3168 | kref_init(&driver->kref); | |
3169 | driver->magic = TTY_DRIVER_MAGIC; | |
3170 | driver->num = lines; | |
3171 | driver->owner = owner; | |
3172 | driver->flags = flags; | |
16a02081 JS |
3173 | |
3174 | if (!(flags & TTY_DRIVER_DEVPTS_MEM)) { | |
3175 | driver->ttys = kcalloc(lines, sizeof(*driver->ttys), | |
3176 | GFP_KERNEL); | |
3177 | driver->termios = kcalloc(lines, sizeof(*driver->termios), | |
3178 | GFP_KERNEL); | |
3179 | if (!driver->ttys || !driver->termios) { | |
3180 | err = -ENOMEM; | |
3181 | goto err_free_all; | |
3182 | } | |
3183 | } | |
3184 | ||
3185 | if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) { | |
3186 | driver->ports = kcalloc(lines, sizeof(*driver->ports), | |
3187 | GFP_KERNEL); | |
3188 | if (!driver->ports) { | |
3189 | err = -ENOMEM; | |
3190 | goto err_free_all; | |
3191 | } | |
7e73eca6 JS |
3192 | cdevs = lines; |
3193 | } | |
3194 | ||
3195 | driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL); | |
3196 | if (!driver->cdevs) { | |
3197 | err = -ENOMEM; | |
3198 | goto err_free_all; | |
16a02081 | 3199 | } |
7f0bc6a6 | 3200 | |
1da177e4 | 3201 | return driver; |
16a02081 JS |
3202 | err_free_all: |
3203 | kfree(driver->ports); | |
3204 | kfree(driver->ttys); | |
3205 | kfree(driver->termios); | |
3206 | kfree(driver); | |
3207 | return ERR_PTR(err); | |
1da177e4 | 3208 | } |
7f0bc6a6 | 3209 | EXPORT_SYMBOL(__tty_alloc_driver); |
1da177e4 | 3210 | |
7d7b93c1 | 3211 | static void destruct_tty_driver(struct kref *kref) |
1da177e4 | 3212 | { |
7d7b93c1 AC |
3213 | struct tty_driver *driver = container_of(kref, struct tty_driver, kref); |
3214 | int i; | |
3215 | struct ktermios *tp; | |
7d7b93c1 AC |
3216 | |
3217 | if (driver->flags & TTY_DRIVER_INSTALLED) { | |
3218 | /* | |
3219 | * Free the termios and termios_locked structures because | |
3220 | * we don't want to get memory leaks when modular tty | |
3221 | * drivers are removed from the kernel. | |
3222 | */ | |
3223 | for (i = 0; i < driver->num; i++) { | |
3224 | tp = driver->termios[i]; | |
3225 | if (tp) { | |
3226 | driver->termios[i] = NULL; | |
3227 | kfree(tp); | |
3228 | } | |
7d7b93c1 AC |
3229 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) |
3230 | tty_unregister_device(driver, i); | |
3231 | } | |
7d7b93c1 | 3232 | proc_tty_unregister_driver(driver); |
7e73eca6 JS |
3233 | if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) |
3234 | cdev_del(&driver->cdevs[0]); | |
7d7b93c1 | 3235 | } |
7e73eca6 | 3236 | kfree(driver->cdevs); |
04831dc1 | 3237 | kfree(driver->ports); |
16a02081 JS |
3238 | kfree(driver->termios); |
3239 | kfree(driver->ttys); | |
1da177e4 LT |
3240 | kfree(driver); |
3241 | } | |
3242 | ||
7d7b93c1 AC |
3243 | void tty_driver_kref_put(struct tty_driver *driver) |
3244 | { | |
3245 | kref_put(&driver->kref, destruct_tty_driver); | |
3246 | } | |
3247 | EXPORT_SYMBOL(tty_driver_kref_put); | |
3248 | ||
b68e31d0 JD |
3249 | void tty_set_operations(struct tty_driver *driver, |
3250 | const struct tty_operations *op) | |
1da177e4 | 3251 | { |
f34d7a5b AC |
3252 | driver->ops = op; |
3253 | }; | |
7d7b93c1 | 3254 | EXPORT_SYMBOL(tty_set_operations); |
1da177e4 | 3255 | |
7d7b93c1 AC |
3256 | void put_tty_driver(struct tty_driver *d) |
3257 | { | |
3258 | tty_driver_kref_put(d); | |
3259 | } | |
1da177e4 | 3260 | EXPORT_SYMBOL(put_tty_driver); |
1da177e4 LT |
3261 | |
3262 | /* | |
3263 | * Called by a tty driver to register itself. | |
3264 | */ | |
3265 | int tty_register_driver(struct tty_driver *driver) | |
3266 | { | |
3267 | int error; | |
37bdfb07 | 3268 | int i; |
1da177e4 | 3269 | dev_t dev; |
b670bde0 | 3270 | struct device *d; |
1da177e4 | 3271 | |
1da177e4 | 3272 | if (!driver->major) { |
37bdfb07 AC |
3273 | error = alloc_chrdev_region(&dev, driver->minor_start, |
3274 | driver->num, driver->name); | |
1da177e4 LT |
3275 | if (!error) { |
3276 | driver->major = MAJOR(dev); | |
3277 | driver->minor_start = MINOR(dev); | |
3278 | } | |
3279 | } else { | |
3280 | dev = MKDEV(driver->major, driver->minor_start); | |
e5717c48 | 3281 | error = register_chrdev_region(dev, driver->num, driver->name); |
1da177e4 | 3282 | } |
9bb8a3d4 | 3283 | if (error < 0) |
16a02081 | 3284 | goto err; |
1da177e4 | 3285 | |
7e73eca6 JS |
3286 | if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) { |
3287 | error = tty_cdev_add(driver, dev, 0, driver->num); | |
3288 | if (error) | |
3289 | goto err_unreg_char; | |
3290 | } | |
1da177e4 | 3291 | |
ca509f69 | 3292 | mutex_lock(&tty_mutex); |
1da177e4 | 3293 | list_add(&driver->tty_drivers, &tty_drivers); |
ca509f69 | 3294 | mutex_unlock(&tty_mutex); |
37bdfb07 AC |
3295 | |
3296 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) { | |
b670bde0 VK |
3297 | for (i = 0; i < driver->num; i++) { |
3298 | d = tty_register_device(driver, i, NULL); | |
3299 | if (IS_ERR(d)) { | |
3300 | error = PTR_ERR(d); | |
16a02081 | 3301 | goto err_unreg_devs; |
b670bde0 VK |
3302 | } |
3303 | } | |
1da177e4 LT |
3304 | } |
3305 | proc_tty_register_driver(driver); | |
7d7b93c1 | 3306 | driver->flags |= TTY_DRIVER_INSTALLED; |
1da177e4 | 3307 | return 0; |
b670bde0 | 3308 | |
16a02081 | 3309 | err_unreg_devs: |
b670bde0 VK |
3310 | for (i--; i >= 0; i--) |
3311 | tty_unregister_device(driver, i); | |
3312 | ||
3313 | mutex_lock(&tty_mutex); | |
3314 | list_del(&driver->tty_drivers); | |
3315 | mutex_unlock(&tty_mutex); | |
3316 | ||
9bb8a3d4 | 3317 | err_unreg_char: |
b670bde0 | 3318 | unregister_chrdev_region(dev, driver->num); |
16a02081 | 3319 | err: |
b670bde0 | 3320 | return error; |
1da177e4 | 3321 | } |
1da177e4 LT |
3322 | EXPORT_SYMBOL(tty_register_driver); |
3323 | ||
3324 | /* | |
3325 | * Called by a tty driver to unregister itself. | |
3326 | */ | |
3327 | int tty_unregister_driver(struct tty_driver *driver) | |
3328 | { | |
7d7b93c1 AC |
3329 | #if 0 |
3330 | /* FIXME */ | |
1da177e4 LT |
3331 | if (driver->refcount) |
3332 | return -EBUSY; | |
7d7b93c1 | 3333 | #endif |
1da177e4 LT |
3334 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), |
3335 | driver->num); | |
ca509f69 | 3336 | mutex_lock(&tty_mutex); |
1da177e4 | 3337 | list_del(&driver->tty_drivers); |
ca509f69 | 3338 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
3339 | return 0; |
3340 | } | |
7d7b93c1 | 3341 | |
1da177e4 LT |
3342 | EXPORT_SYMBOL(tty_unregister_driver); |
3343 | ||
24ec839c PZ |
3344 | dev_t tty_devnum(struct tty_struct *tty) |
3345 | { | |
3346 | return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; | |
3347 | } | |
3348 | EXPORT_SYMBOL(tty_devnum); | |
3349 | ||
3350 | void proc_clear_tty(struct task_struct *p) | |
3351 | { | |
7c3b1dcf | 3352 | unsigned long flags; |
9c9f4ded | 3353 | struct tty_struct *tty; |
7c3b1dcf | 3354 | spin_lock_irqsave(&p->sighand->siglock, flags); |
9c9f4ded | 3355 | tty = p->signal->tty; |
24ec839c | 3356 | p->signal->tty = NULL; |
7c3b1dcf | 3357 | spin_unlock_irqrestore(&p->sighand->siglock, flags); |
9c9f4ded | 3358 | tty_kref_put(tty); |
24ec839c | 3359 | } |
24ec839c | 3360 | |
47f86834 AC |
3361 | /* Called under the sighand lock */ |
3362 | ||
2a65f1d9 | 3363 | static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) |
24ec839c PZ |
3364 | { |
3365 | if (tty) { | |
47f86834 AC |
3366 | unsigned long flags; |
3367 | /* We should not have a session or pgrp to put here but.... */ | |
3368 | spin_lock_irqsave(&tty->ctrl_lock, flags); | |
d9c1e9a8 EB |
3369 | put_pid(tty->session); |
3370 | put_pid(tty->pgrp); | |
ab521dc0 | 3371 | tty->pgrp = get_pid(task_pgrp(tsk)); |
47f86834 AC |
3372 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
3373 | tty->session = get_pid(task_session(tsk)); | |
9c9f4ded AC |
3374 | if (tsk->signal->tty) { |
3375 | printk(KERN_DEBUG "tty not NULL!!\n"); | |
3376 | tty_kref_put(tsk->signal->tty); | |
3377 | } | |
24ec839c | 3378 | } |
2a65f1d9 | 3379 | put_pid(tsk->signal->tty_old_pgrp); |
9c9f4ded | 3380 | tsk->signal->tty = tty_kref_get(tty); |
ab521dc0 | 3381 | tsk->signal->tty_old_pgrp = NULL; |
24ec839c PZ |
3382 | } |
3383 | ||
98a27ba4 | 3384 | static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty) |
24ec839c PZ |
3385 | { |
3386 | spin_lock_irq(&tsk->sighand->siglock); | |
2a65f1d9 | 3387 | __proc_set_tty(tsk, tty); |
24ec839c PZ |
3388 | spin_unlock_irq(&tsk->sighand->siglock); |
3389 | } | |
3390 | ||
3391 | struct tty_struct *get_current_tty(void) | |
3392 | { | |
3393 | struct tty_struct *tty; | |
934e6ebf AC |
3394 | unsigned long flags; |
3395 | ||
3396 | spin_lock_irqsave(¤t->sighand->siglock, flags); | |
452a00d2 | 3397 | tty = tty_kref_get(current->signal->tty); |
934e6ebf | 3398 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
24ec839c PZ |
3399 | return tty; |
3400 | } | |
a311f743 | 3401 | EXPORT_SYMBOL_GPL(get_current_tty); |
1da177e4 | 3402 | |
d81ed103 AC |
3403 | void tty_default_fops(struct file_operations *fops) |
3404 | { | |
3405 | *fops = tty_fops; | |
3406 | } | |
3407 | ||
1da177e4 LT |
3408 | /* |
3409 | * Initialize the console device. This is called *early*, so | |
3410 | * we can't necessarily depend on lots of kernel help here. | |
3411 | * Just do some early initializations, and do the complex setup | |
3412 | * later. | |
3413 | */ | |
3414 | void __init console_init(void) | |
3415 | { | |
3416 | initcall_t *call; | |
3417 | ||
3418 | /* Setup the default TTY line discipline. */ | |
01e1abb2 | 3419 | tty_ldisc_begin(); |
1da177e4 LT |
3420 | |
3421 | /* | |
37bdfb07 | 3422 | * set up the console device so that later boot sequences can |
1da177e4 LT |
3423 | * inform about problems etc.. |
3424 | */ | |
1da177e4 LT |
3425 | call = __con_initcall_start; |
3426 | while (call < __con_initcall_end) { | |
3427 | (*call)(); | |
3428 | call++; | |
3429 | } | |
3430 | } | |
3431 | ||
2c9ede55 | 3432 | static char *tty_devnode(struct device *dev, umode_t *mode) |
e454cea2 KS |
3433 | { |
3434 | if (!mode) | |
3435 | return NULL; | |
3436 | if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) || | |
3437 | dev->devt == MKDEV(TTYAUX_MAJOR, 2)) | |
3438 | *mode = 0666; | |
3439 | return NULL; | |
3440 | } | |
3441 | ||
1da177e4 LT |
3442 | static int __init tty_class_init(void) |
3443 | { | |
7fe845d1 | 3444 | tty_class = class_create(THIS_MODULE, "tty"); |
1da177e4 LT |
3445 | if (IS_ERR(tty_class)) |
3446 | return PTR_ERR(tty_class); | |
e454cea2 | 3447 | tty_class->devnode = tty_devnode; |
1da177e4 LT |
3448 | return 0; |
3449 | } | |
3450 | ||
3451 | postcore_initcall(tty_class_init); | |
3452 | ||
3453 | /* 3/2004 jmc: why do these devices exist? */ | |
1da177e4 | 3454 | static struct cdev tty_cdev, console_cdev; |
1da177e4 | 3455 | |
fbc92a34 KS |
3456 | static ssize_t show_cons_active(struct device *dev, |
3457 | struct device_attribute *attr, char *buf) | |
3458 | { | |
3459 | struct console *cs[16]; | |
3460 | int i = 0; | |
3461 | struct console *c; | |
3462 | ssize_t count = 0; | |
3463 | ||
ac751efa | 3464 | console_lock(); |
a2a6a822 | 3465 | for_each_console(c) { |
fbc92a34 KS |
3466 | if (!c->device) |
3467 | continue; | |
3468 | if (!c->write) | |
3469 | continue; | |
3470 | if ((c->flags & CON_ENABLED) == 0) | |
3471 | continue; | |
3472 | cs[i++] = c; | |
3473 | if (i >= ARRAY_SIZE(cs)) | |
3474 | break; | |
3475 | } | |
3476 | while (i--) | |
3477 | count += sprintf(buf + count, "%s%d%c", | |
3478 | cs[i]->name, cs[i]->index, i ? ' ':'\n'); | |
ac751efa | 3479 | console_unlock(); |
fbc92a34 KS |
3480 | |
3481 | return count; | |
3482 | } | |
3483 | static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL); | |
3484 | ||
3485 | static struct device *consdev; | |
3486 | ||
3487 | void console_sysfs_notify(void) | |
3488 | { | |
3489 | if (consdev) | |
3490 | sysfs_notify(&consdev->kobj, NULL, "active"); | |
3491 | } | |
3492 | ||
1da177e4 LT |
3493 | /* |
3494 | * Ok, now we can initialize the rest of the tty devices and can count | |
3495 | * on memory allocations, interrupts etc.. | |
3496 | */ | |
31d1d48e | 3497 | int __init tty_init(void) |
1da177e4 LT |
3498 | { |
3499 | cdev_init(&tty_cdev, &tty_fops); | |
3500 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || | |
3501 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) | |
3502 | panic("Couldn't register /dev/tty driver\n"); | |
fbc92a34 | 3503 | device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); |
1da177e4 LT |
3504 | |
3505 | cdev_init(&console_cdev, &console_fops); | |
3506 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || | |
3507 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) | |
3508 | panic("Couldn't register /dev/console driver\n"); | |
fbc92a34 | 3509 | consdev = device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, |
47aa5793 | 3510 | "console"); |
fbc92a34 KS |
3511 | if (IS_ERR(consdev)) |
3512 | consdev = NULL; | |
3513 | else | |
a2a6a822 | 3514 | WARN_ON(device_create_file(consdev, &dev_attr_active) < 0); |
1da177e4 | 3515 | |
1da177e4 | 3516 | #ifdef CONFIG_VT |
d81ed103 | 3517 | vty_init(&console_fops); |
1da177e4 LT |
3518 | #endif |
3519 | return 0; | |
3520 | } | |
31d1d48e | 3521 |