]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/char/tty_io.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
7 | /* | |
8 | * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles | |
9 | * or rs-channels. It also implements echoing, cooked mode etc. | |
10 | * | |
11 | * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0. | |
12 | * | |
13 | * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the | |
14 | * tty_struct and tty_queue structures. Previously there was an array | |
15 | * of 256 tty_struct's which was statically allocated, and the | |
16 | * tty_queue structures were allocated at boot time. Both are now | |
17 | * dynamically allocated only when the tty is open. | |
18 | * | |
19 | * Also restructured routines so that there is more of a separation | |
20 | * between the high-level tty routines (tty_io.c and tty_ioctl.c) and | |
21 | * the low-level tty routines (serial.c, pty.c, console.c). This | |
22 | * makes for cleaner and more compact code. -TYT, 9/17/92 | |
23 | * | |
24 | * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines | |
25 | * which can be dynamically activated and de-activated by the line | |
26 | * discipline handling modules (like SLIP). | |
27 | * | |
28 | * NOTE: pay no attention to the line discipline code (yet); its | |
29 | * interface is still subject to change in this version... | |
30 | * -- TYT, 1/31/92 | |
31 | * | |
32 | * Added functionality to the OPOST tty handling. No delays, but all | |
33 | * other bits should be there. | |
34 | * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993. | |
35 | * | |
36 | * Rewrote canonical mode and added more termios flags. | |
37 | * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94 | |
38 | * | |
39 | * Reorganized FASYNC support so mouse code can share it. | |
40 | * -- ctm@ardi.com, 9Sep95 | |
41 | * | |
42 | * New TIOCLINUX variants added. | |
43 | * -- mj@k332.feld.cvut.cz, 19-Nov-95 | |
44 | * | |
45 | * Restrict vt switching via ioctl() | |
46 | * -- grif@cs.ucr.edu, 5-Dec-95 | |
47 | * | |
48 | * Move console and virtual terminal code to more appropriate files, | |
49 | * implement CONFIG_VT and generalize console device interface. | |
50 | * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97 | |
51 | * | |
52 | * Rewrote init_dev and release_dev to eliminate races. | |
53 | * -- Bill Hawes <whawes@star.net>, June 97 | |
54 | * | |
55 | * Added devfs support. | |
56 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998 | |
57 | * | |
58 | * Added support for a Unix98-style ptmx device. | |
59 | * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998 | |
60 | * | |
61 | * Reduced memory usage for older ARM systems | |
62 | * -- Russell King <rmk@arm.linux.org.uk> | |
63 | * | |
64 | * Move do_SAK() into process context. Less stack use in devfs functions. | |
65 | * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01 | |
66 | */ | |
67 | ||
1da177e4 LT |
68 | #include <linux/types.h> |
69 | #include <linux/major.h> | |
70 | #include <linux/errno.h> | |
71 | #include <linux/signal.h> | |
72 | #include <linux/fcntl.h> | |
73 | #include <linux/sched.h> | |
74 | #include <linux/interrupt.h> | |
75 | #include <linux/tty.h> | |
76 | #include <linux/tty_driver.h> | |
77 | #include <linux/tty_flip.h> | |
78 | #include <linux/devpts_fs.h> | |
79 | #include <linux/file.h> | |
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> | |
91 | #include <linux/smp_lock.h> | |
92 | #include <linux/device.h> | |
93 | #include <linux/idr.h> | |
94 | #include <linux/wait.h> | |
95 | #include <linux/bitops.h> | |
b20f3ae5 | 96 | #include <linux/delay.h> |
1da177e4 LT |
97 | |
98 | #include <asm/uaccess.h> | |
99 | #include <asm/system.h> | |
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> | |
106 | ||
107 | #undef TTY_DEBUG_HANGUP | |
108 | ||
109 | #define TTY_PARANOIA_CHECK 1 | |
110 | #define CHECK_TTY_COUNT 1 | |
111 | ||
112 | struct termios tty_std_termios = { /* for the benefit of tty drivers */ | |
113 | .c_iflag = ICRNL | IXON, | |
114 | .c_oflag = OPOST | ONLCR, | |
115 | .c_cflag = B38400 | CS8 | CREAD | HUPCL, | |
116 | .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK | | |
117 | ECHOCTL | ECHOKE | IEXTEN, | |
118 | .c_cc = INIT_C_CC | |
119 | }; | |
120 | ||
121 | EXPORT_SYMBOL(tty_std_termios); | |
122 | ||
123 | /* This list gets poked at by procfs and various bits of boot up code. This | |
124 | could do with some rationalisation such as pulling the tty proc function | |
125 | into this file */ | |
126 | ||
127 | LIST_HEAD(tty_drivers); /* linked list of tty drivers */ | |
128 | ||
129 | /* Semaphore to protect creating and releasing a tty. This is shared with | |
130 | vt.c for deeply disgusting hack reasons */ | |
70522e12 | 131 | DEFINE_MUTEX(tty_mutex); |
1da177e4 LT |
132 | |
133 | #ifdef CONFIG_UNIX98_PTYS | |
134 | extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */ | |
135 | extern int pty_limit; /* Config limit on Unix98 ptys */ | |
136 | static DEFINE_IDR(allocated_ptys); | |
137 | static DECLARE_MUTEX(allocated_ptys_lock); | |
138 | static int ptmx_open(struct inode *, struct file *); | |
139 | #endif | |
140 | ||
141 | extern void disable_early_printk(void); | |
142 | ||
143 | static void initialize_tty_struct(struct tty_struct *tty); | |
144 | ||
145 | static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *); | |
146 | static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *); | |
147 | ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *); | |
148 | static unsigned int tty_poll(struct file *, poll_table *); | |
149 | static int tty_open(struct inode *, struct file *); | |
150 | static int tty_release(struct inode *, struct file *); | |
151 | int tty_ioctl(struct inode * inode, struct file * file, | |
152 | unsigned int cmd, unsigned long arg); | |
153 | static int tty_fasync(int fd, struct file * filp, int on); | |
1da177e4 LT |
154 | static void release_mem(struct tty_struct *tty, int idx); |
155 | ||
af9b897e AC |
156 | /** |
157 | * alloc_tty_struct - allocate a tty object | |
158 | * | |
159 | * Return a new empty tty structure. The data fields have not | |
160 | * been initialized in any way but has been zeroed | |
161 | * | |
162 | * Locking: none | |
163 | * FIXME: use kzalloc | |
164 | */ | |
1da177e4 LT |
165 | |
166 | static struct tty_struct *alloc_tty_struct(void) | |
167 | { | |
168 | struct tty_struct *tty; | |
169 | ||
170 | tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL); | |
171 | if (tty) | |
172 | memset(tty, 0, sizeof(struct tty_struct)); | |
173 | return tty; | |
174 | } | |
175 | ||
33f0f88f AC |
176 | static void tty_buffer_free_all(struct tty_struct *); |
177 | ||
af9b897e AC |
178 | /** |
179 | * free_tty_struct - free a disused tty | |
180 | * @tty: tty struct to free | |
181 | * | |
182 | * Free the write buffers, tty queue and tty memory itself. | |
183 | * | |
184 | * Locking: none. Must be called after tty is definitely unused | |
185 | */ | |
186 | ||
1da177e4 LT |
187 | static inline void free_tty_struct(struct tty_struct *tty) |
188 | { | |
189 | kfree(tty->write_buf); | |
33f0f88f | 190 | tty_buffer_free_all(tty); |
1da177e4 LT |
191 | kfree(tty); |
192 | } | |
193 | ||
194 | #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base) | |
195 | ||
af9b897e AC |
196 | /** |
197 | * tty_name - return tty naming | |
198 | * @tty: tty structure | |
199 | * @buf: buffer for output | |
200 | * | |
201 | * Convert a tty structure into a name. The name reflects the kernel | |
202 | * naming policy and if udev is in use may not reflect user space | |
203 | * | |
204 | * Locking: none | |
205 | */ | |
206 | ||
1da177e4 LT |
207 | char *tty_name(struct tty_struct *tty, char *buf) |
208 | { | |
209 | if (!tty) /* Hmm. NULL pointer. That's fun. */ | |
210 | strcpy(buf, "NULL tty"); | |
211 | else | |
212 | strcpy(buf, tty->name); | |
213 | return buf; | |
214 | } | |
215 | ||
216 | EXPORT_SYMBOL(tty_name); | |
217 | ||
d769a669 | 218 | int tty_paranoia_check(struct tty_struct *tty, struct inode *inode, |
1da177e4 LT |
219 | const char *routine) |
220 | { | |
221 | #ifdef TTY_PARANOIA_CHECK | |
222 | if (!tty) { | |
223 | printk(KERN_WARNING | |
224 | "null TTY for (%d:%d) in %s\n", | |
225 | imajor(inode), iminor(inode), routine); | |
226 | return 1; | |
227 | } | |
228 | if (tty->magic != TTY_MAGIC) { | |
229 | printk(KERN_WARNING | |
230 | "bad magic number for tty struct (%d:%d) in %s\n", | |
231 | imajor(inode), iminor(inode), routine); | |
232 | return 1; | |
233 | } | |
234 | #endif | |
235 | return 0; | |
236 | } | |
237 | ||
238 | static int check_tty_count(struct tty_struct *tty, const char *routine) | |
239 | { | |
240 | #ifdef CHECK_TTY_COUNT | |
241 | struct list_head *p; | |
242 | int count = 0; | |
243 | ||
244 | file_list_lock(); | |
245 | list_for_each(p, &tty->tty_files) { | |
246 | count++; | |
247 | } | |
248 | file_list_unlock(); | |
249 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
250 | tty->driver->subtype == PTY_TYPE_SLAVE && | |
251 | tty->link && tty->link->count) | |
252 | count++; | |
253 | if (tty->count != count) { | |
254 | printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) " | |
255 | "!= #fd's(%d) in %s\n", | |
256 | tty->name, tty->count, count, routine); | |
257 | return count; | |
258 | } | |
259 | #endif | |
260 | return 0; | |
261 | } | |
262 | ||
33f0f88f AC |
263 | /* |
264 | * Tty buffer allocation management | |
265 | */ | |
266 | ||
af9b897e AC |
267 | |
268 | /** | |
269 | * tty_buffer_free_all - free buffers used by a tty | |
270 | * @tty: tty to free from | |
271 | * | |
272 | * Remove all the buffers pending on a tty whether queued with data | |
273 | * or in the free ring. Must be called when the tty is no longer in use | |
274 | * | |
275 | * Locking: none | |
276 | */ | |
277 | ||
33f0f88f AC |
278 | static void tty_buffer_free_all(struct tty_struct *tty) |
279 | { | |
280 | struct tty_buffer *thead; | |
281 | while((thead = tty->buf.head) != NULL) { | |
282 | tty->buf.head = thead->next; | |
283 | kfree(thead); | |
284 | } | |
285 | while((thead = tty->buf.free) != NULL) { | |
286 | tty->buf.free = thead->next; | |
287 | kfree(thead); | |
288 | } | |
289 | tty->buf.tail = NULL; | |
290 | } | |
291 | ||
292 | static void tty_buffer_init(struct tty_struct *tty) | |
293 | { | |
808249ce | 294 | spin_lock_init(&tty->buf.lock); |
33f0f88f AC |
295 | tty->buf.head = NULL; |
296 | tty->buf.tail = NULL; | |
297 | tty->buf.free = NULL; | |
298 | } | |
299 | ||
300 | static struct tty_buffer *tty_buffer_alloc(size_t size) | |
301 | { | |
302 | struct tty_buffer *p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC); | |
303 | if(p == NULL) | |
304 | return NULL; | |
305 | p->used = 0; | |
306 | p->size = size; | |
307 | p->next = NULL; | |
8977d929 PF |
308 | p->commit = 0; |
309 | p->read = 0; | |
33f0f88f AC |
310 | p->char_buf_ptr = (char *)(p->data); |
311 | p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size; | |
312 | /* printk("Flip create %p\n", p); */ | |
313 | return p; | |
314 | } | |
315 | ||
316 | /* Must be called with the tty_read lock held. This needs to acquire strategy | |
317 | code to decide if we should kfree or relink a given expired buffer */ | |
318 | ||
319 | static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b) | |
320 | { | |
321 | /* Dumb strategy for now - should keep some stats */ | |
322 | /* printk("Flip dispose %p\n", b); */ | |
323 | if(b->size >= 512) | |
324 | kfree(b); | |
325 | else { | |
326 | b->next = tty->buf.free; | |
327 | tty->buf.free = b; | |
328 | } | |
329 | } | |
330 | ||
331 | static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size) | |
332 | { | |
333 | struct tty_buffer **tbh = &tty->buf.free; | |
334 | while((*tbh) != NULL) { | |
335 | struct tty_buffer *t = *tbh; | |
336 | if(t->size >= size) { | |
337 | *tbh = t->next; | |
338 | t->next = NULL; | |
339 | t->used = 0; | |
8977d929 PF |
340 | t->commit = 0; |
341 | t->read = 0; | |
33f0f88f | 342 | /* DEBUG ONLY */ |
6af6aab3 | 343 | /* memset(t->data, '*', size); */ |
33f0f88f AC |
344 | /* printk("Flip recycle %p\n", t); */ |
345 | return t; | |
346 | } | |
347 | tbh = &((*tbh)->next); | |
348 | } | |
349 | /* Round the buffer size out */ | |
350 | size = (size + 0xFF) & ~ 0xFF; | |
351 | return tty_buffer_alloc(size); | |
352 | /* Should possibly check if this fails for the largest buffer we | |
353 | have queued and recycle that ? */ | |
354 | } | |
355 | ||
356 | int tty_buffer_request_room(struct tty_struct *tty, size_t size) | |
357 | { | |
808249ce PF |
358 | struct tty_buffer *b, *n; |
359 | int left; | |
360 | unsigned long flags; | |
361 | ||
362 | spin_lock_irqsave(&tty->buf.lock, flags); | |
33f0f88f AC |
363 | |
364 | /* OPTIMISATION: We could keep a per tty "zero" sized buffer to | |
365 | remove this conditional if its worth it. This would be invisible | |
366 | to the callers */ | |
33b37a33 | 367 | if ((b = tty->buf.tail) != NULL) |
33f0f88f | 368 | left = b->size - b->used; |
33b37a33 | 369 | else |
808249ce PF |
370 | left = 0; |
371 | ||
372 | if (left < size) { | |
373 | /* This is the slow path - looking for new buffers to use */ | |
374 | if ((n = tty_buffer_find(tty, size)) != NULL) { | |
375 | if (b != NULL) { | |
376 | b->next = n; | |
8977d929 | 377 | b->commit = b->used; |
808249ce PF |
378 | } else |
379 | tty->buf.head = n; | |
380 | tty->buf.tail = n; | |
808249ce PF |
381 | } else |
382 | size = left; | |
383 | } | |
384 | ||
385 | spin_unlock_irqrestore(&tty->buf.lock, flags); | |
33f0f88f AC |
386 | return size; |
387 | } | |
33f0f88f AC |
388 | EXPORT_SYMBOL_GPL(tty_buffer_request_room); |
389 | ||
af9b897e AC |
390 | /** |
391 | * tty_insert_flip_string - Add characters to the tty buffer | |
392 | * @tty: tty structure | |
393 | * @chars: characters | |
394 | * @size: size | |
395 | * | |
396 | * Queue a series of bytes to the tty buffering. All the characters | |
397 | * passed are marked as without error. Returns the number added. | |
398 | * | |
399 | * Locking: Called functions may take tty->buf.lock | |
400 | */ | |
401 | ||
e1a25090 AM |
402 | int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars, |
403 | size_t size) | |
33f0f88f AC |
404 | { |
405 | int copied = 0; | |
406 | do { | |
407 | int space = tty_buffer_request_room(tty, size - copied); | |
408 | struct tty_buffer *tb = tty->buf.tail; | |
409 | /* If there is no space then tb may be NULL */ | |
410 | if(unlikely(space == 0)) | |
411 | break; | |
412 | memcpy(tb->char_buf_ptr + tb->used, chars, space); | |
413 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); | |
414 | tb->used += space; | |
415 | copied += space; | |
416 | chars += space; | |
33f0f88f AC |
417 | } |
418 | /* There is a small chance that we need to split the data over | |
419 | several buffers. If this is the case we must loop */ | |
420 | while (unlikely(size > copied)); | |
421 | return copied; | |
422 | } | |
ee37df78 | 423 | EXPORT_SYMBOL(tty_insert_flip_string); |
33f0f88f | 424 | |
af9b897e AC |
425 | /** |
426 | * tty_insert_flip_string_flags - Add characters to the tty buffer | |
427 | * @tty: tty structure | |
428 | * @chars: characters | |
429 | * @flags: flag bytes | |
430 | * @size: size | |
431 | * | |
432 | * Queue a series of bytes to the tty buffering. For each character | |
433 | * the flags array indicates the status of the character. Returns the | |
434 | * number added. | |
435 | * | |
436 | * Locking: Called functions may take tty->buf.lock | |
437 | */ | |
438 | ||
e1a25090 AM |
439 | int tty_insert_flip_string_flags(struct tty_struct *tty, |
440 | const unsigned char *chars, const char *flags, size_t size) | |
33f0f88f AC |
441 | { |
442 | int copied = 0; | |
443 | do { | |
444 | int space = tty_buffer_request_room(tty, size - copied); | |
445 | struct tty_buffer *tb = tty->buf.tail; | |
446 | /* If there is no space then tb may be NULL */ | |
447 | if(unlikely(space == 0)) | |
448 | break; | |
449 | memcpy(tb->char_buf_ptr + tb->used, chars, space); | |
450 | memcpy(tb->flag_buf_ptr + tb->used, flags, space); | |
451 | tb->used += space; | |
452 | copied += space; | |
453 | chars += space; | |
454 | flags += space; | |
455 | } | |
456 | /* There is a small chance that we need to split the data over | |
457 | several buffers. If this is the case we must loop */ | |
458 | while (unlikely(size > copied)); | |
459 | return copied; | |
460 | } | |
ff4547f4 | 461 | EXPORT_SYMBOL(tty_insert_flip_string_flags); |
33f0f88f | 462 | |
af9b897e AC |
463 | /** |
464 | * tty_schedule_flip - push characters to ldisc | |
465 | * @tty: tty to push from | |
466 | * | |
467 | * Takes any pending buffers and transfers their ownership to the | |
468 | * ldisc side of the queue. It then schedules those characters for | |
469 | * processing by the line discipline. | |
470 | * | |
471 | * Locking: Takes tty->buf.lock | |
472 | */ | |
473 | ||
e1a25090 AM |
474 | void tty_schedule_flip(struct tty_struct *tty) |
475 | { | |
476 | unsigned long flags; | |
477 | spin_lock_irqsave(&tty->buf.lock, flags); | |
33b37a33 | 478 | if (tty->buf.tail != NULL) |
e1a25090 | 479 | tty->buf.tail->commit = tty->buf.tail->used; |
e1a25090 AM |
480 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
481 | schedule_delayed_work(&tty->buf.work, 1); | |
482 | } | |
483 | EXPORT_SYMBOL(tty_schedule_flip); | |
33f0f88f | 484 | |
af9b897e AC |
485 | /** |
486 | * tty_prepare_flip_string - make room for characters | |
487 | * @tty: tty | |
488 | * @chars: return pointer for character write area | |
489 | * @size: desired size | |
490 | * | |
33f0f88f AC |
491 | * Prepare a block of space in the buffer for data. Returns the length |
492 | * available and buffer pointer to the space which is now allocated and | |
493 | * accounted for as ready for normal characters. This is used for drivers | |
494 | * that need their own block copy routines into the buffer. There is no | |
495 | * guarantee the buffer is a DMA target! | |
af9b897e AC |
496 | * |
497 | * Locking: May call functions taking tty->buf.lock | |
33f0f88f AC |
498 | */ |
499 | ||
500 | int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size) | |
501 | { | |
502 | int space = tty_buffer_request_room(tty, size); | |
808249ce PF |
503 | if (likely(space)) { |
504 | struct tty_buffer *tb = tty->buf.tail; | |
505 | *chars = tb->char_buf_ptr + tb->used; | |
506 | memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space); | |
507 | tb->used += space; | |
508 | } | |
33f0f88f AC |
509 | return space; |
510 | } | |
511 | ||
512 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string); | |
513 | ||
af9b897e AC |
514 | /** |
515 | * tty_prepare_flip_string_flags - make room for characters | |
516 | * @tty: tty | |
517 | * @chars: return pointer for character write area | |
518 | * @flags: return pointer for status flag write area | |
519 | * @size: desired size | |
520 | * | |
33f0f88f AC |
521 | * Prepare a block of space in the buffer for data. Returns the length |
522 | * available and buffer pointer to the space which is now allocated and | |
523 | * accounted for as ready for characters. This is used for drivers | |
524 | * that need their own block copy routines into the buffer. There is no | |
525 | * guarantee the buffer is a DMA target! | |
af9b897e AC |
526 | * |
527 | * Locking: May call functions taking tty->buf.lock | |
33f0f88f AC |
528 | */ |
529 | ||
530 | int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size) | |
531 | { | |
532 | int space = tty_buffer_request_room(tty, size); | |
808249ce PF |
533 | if (likely(space)) { |
534 | struct tty_buffer *tb = tty->buf.tail; | |
535 | *chars = tb->char_buf_ptr + tb->used; | |
536 | *flags = tb->flag_buf_ptr + tb->used; | |
537 | tb->used += space; | |
538 | } | |
33f0f88f AC |
539 | return space; |
540 | } | |
541 | ||
542 | EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags); | |
543 | ||
544 | ||
545 | ||
af9b897e AC |
546 | /** |
547 | * tty_set_termios_ldisc - set ldisc field | |
548 | * @tty: tty structure | |
549 | * @num: line discipline number | |
550 | * | |
1da177e4 LT |
551 | * This is probably overkill for real world processors but |
552 | * they are not on hot paths so a little discipline won't do | |
553 | * any harm. | |
af9b897e AC |
554 | * |
555 | * Locking: takes termios_sem | |
1da177e4 LT |
556 | */ |
557 | ||
558 | static void tty_set_termios_ldisc(struct tty_struct *tty, int num) | |
559 | { | |
560 | down(&tty->termios_sem); | |
561 | tty->termios->c_line = num; | |
562 | up(&tty->termios_sem); | |
563 | } | |
564 | ||
565 | /* | |
566 | * This guards the refcounted line discipline lists. The lock | |
567 | * must be taken with irqs off because there are hangup path | |
568 | * callers who will do ldisc lookups and cannot sleep. | |
569 | */ | |
570 | ||
571 | static DEFINE_SPINLOCK(tty_ldisc_lock); | |
572 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | |
bfb07599 | 573 | static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */ |
1da177e4 | 574 | |
af9b897e AC |
575 | /** |
576 | * tty_register_ldisc - install a line discipline | |
577 | * @disc: ldisc number | |
578 | * @new_ldisc: pointer to the ldisc object | |
579 | * | |
580 | * Installs a new line discipline into the kernel. The discipline | |
581 | * is set up as unreferenced and then made available to the kernel | |
582 | * from this point onwards. | |
583 | * | |
584 | * Locking: | |
585 | * takes tty_ldisc_lock to guard against ldisc races | |
586 | */ | |
587 | ||
1da177e4 LT |
588 | int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc) |
589 | { | |
590 | unsigned long flags; | |
591 | int ret = 0; | |
592 | ||
593 | if (disc < N_TTY || disc >= NR_LDISCS) | |
594 | return -EINVAL; | |
595 | ||
596 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
bfb07599 AD |
597 | tty_ldiscs[disc] = *new_ldisc; |
598 | tty_ldiscs[disc].num = disc; | |
599 | tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED; | |
600 | tty_ldiscs[disc].refcount = 0; | |
1da177e4 LT |
601 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
602 | ||
603 | return ret; | |
604 | } | |
1da177e4 LT |
605 | EXPORT_SYMBOL(tty_register_ldisc); |
606 | ||
af9b897e AC |
607 | /** |
608 | * tty_unregister_ldisc - unload a line discipline | |
609 | * @disc: ldisc number | |
610 | * @new_ldisc: pointer to the ldisc object | |
611 | * | |
612 | * Remove a line discipline from the kernel providing it is not | |
613 | * currently in use. | |
614 | * | |
615 | * Locking: | |
616 | * takes tty_ldisc_lock to guard against ldisc races | |
617 | */ | |
618 | ||
bfb07599 AD |
619 | int tty_unregister_ldisc(int disc) |
620 | { | |
621 | unsigned long flags; | |
622 | int ret = 0; | |
623 | ||
624 | if (disc < N_TTY || disc >= NR_LDISCS) | |
625 | return -EINVAL; | |
626 | ||
627 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
628 | if (tty_ldiscs[disc].refcount) | |
629 | ret = -EBUSY; | |
630 | else | |
631 | tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED; | |
632 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
633 | ||
634 | return ret; | |
635 | } | |
636 | EXPORT_SYMBOL(tty_unregister_ldisc); | |
637 | ||
af9b897e AC |
638 | /** |
639 | * tty_ldisc_get - take a reference to an ldisc | |
640 | * @disc: ldisc number | |
641 | * | |
642 | * Takes a reference to a line discipline. Deals with refcounts and | |
643 | * module locking counts. Returns NULL if the discipline is not available. | |
644 | * Returns a pointer to the discipline and bumps the ref count if it is | |
645 | * available | |
646 | * | |
647 | * Locking: | |
648 | * takes tty_ldisc_lock to guard against ldisc races | |
649 | */ | |
650 | ||
1da177e4 LT |
651 | struct tty_ldisc *tty_ldisc_get(int disc) |
652 | { | |
653 | unsigned long flags; | |
654 | struct tty_ldisc *ld; | |
655 | ||
656 | if (disc < N_TTY || disc >= NR_LDISCS) | |
657 | return NULL; | |
658 | ||
659 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
660 | ||
661 | ld = &tty_ldiscs[disc]; | |
662 | /* Check the entry is defined */ | |
663 | if(ld->flags & LDISC_FLAG_DEFINED) | |
664 | { | |
665 | /* If the module is being unloaded we can't use it */ | |
666 | if (!try_module_get(ld->owner)) | |
667 | ld = NULL; | |
668 | else /* lock it */ | |
669 | ld->refcount++; | |
670 | } | |
671 | else | |
672 | ld = NULL; | |
673 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
674 | return ld; | |
675 | } | |
676 | ||
677 | EXPORT_SYMBOL_GPL(tty_ldisc_get); | |
678 | ||
af9b897e AC |
679 | /** |
680 | * tty_ldisc_put - drop ldisc reference | |
681 | * @disc: ldisc number | |
682 | * | |
683 | * Drop a reference to a line discipline. Manage refcounts and | |
684 | * module usage counts | |
685 | * | |
686 | * Locking: | |
687 | * takes tty_ldisc_lock to guard against ldisc races | |
688 | */ | |
689 | ||
1da177e4 LT |
690 | void tty_ldisc_put(int disc) |
691 | { | |
692 | struct tty_ldisc *ld; | |
693 | unsigned long flags; | |
694 | ||
56ee4827 | 695 | BUG_ON(disc < N_TTY || disc >= NR_LDISCS); |
1da177e4 LT |
696 | |
697 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
698 | ld = &tty_ldiscs[disc]; | |
56ee4827 ES |
699 | BUG_ON(ld->refcount == 0); |
700 | ld->refcount--; | |
1da177e4 LT |
701 | module_put(ld->owner); |
702 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
703 | } | |
704 | ||
705 | EXPORT_SYMBOL_GPL(tty_ldisc_put); | |
706 | ||
af9b897e AC |
707 | /** |
708 | * tty_ldisc_assign - set ldisc on a tty | |
709 | * @tty: tty to assign | |
710 | * @ld: line discipline | |
711 | * | |
712 | * Install an instance of a line discipline into a tty structure. The | |
713 | * ldisc must have a reference count above zero to ensure it remains/ | |
714 | * The tty instance refcount starts at zero. | |
715 | * | |
716 | * Locking: | |
717 | * Caller must hold references | |
718 | */ | |
719 | ||
1da177e4 LT |
720 | static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld) |
721 | { | |
722 | tty->ldisc = *ld; | |
723 | tty->ldisc.refcount = 0; | |
724 | } | |
725 | ||
726 | /** | |
727 | * tty_ldisc_try - internal helper | |
728 | * @tty: the tty | |
729 | * | |
730 | * Make a single attempt to grab and bump the refcount on | |
731 | * the tty ldisc. Return 0 on failure or 1 on success. This is | |
732 | * used to implement both the waiting and non waiting versions | |
733 | * of tty_ldisc_ref | |
af9b897e AC |
734 | * |
735 | * Locking: takes tty_ldisc_lock | |
1da177e4 LT |
736 | */ |
737 | ||
738 | static int tty_ldisc_try(struct tty_struct *tty) | |
739 | { | |
740 | unsigned long flags; | |
741 | struct tty_ldisc *ld; | |
742 | int ret = 0; | |
743 | ||
744 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
745 | ld = &tty->ldisc; | |
746 | if(test_bit(TTY_LDISC, &tty->flags)) | |
747 | { | |
748 | ld->refcount++; | |
749 | ret = 1; | |
750 | } | |
751 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
752 | return ret; | |
753 | } | |
754 | ||
755 | /** | |
756 | * tty_ldisc_ref_wait - wait for the tty ldisc | |
757 | * @tty: tty device | |
758 | * | |
759 | * Dereference the line discipline for the terminal and take a | |
760 | * reference to it. If the line discipline is in flux then | |
761 | * wait patiently until it changes. | |
762 | * | |
763 | * Note: Must not be called from an IRQ/timer context. The caller | |
764 | * must also be careful not to hold other locks that will deadlock | |
765 | * against a discipline change, such as an existing ldisc reference | |
766 | * (which we check for) | |
af9b897e AC |
767 | * |
768 | * Locking: call functions take tty_ldisc_lock | |
1da177e4 LT |
769 | */ |
770 | ||
771 | struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty) | |
772 | { | |
773 | /* wait_event is a macro */ | |
774 | wait_event(tty_ldisc_wait, tty_ldisc_try(tty)); | |
775 | if(tty->ldisc.refcount == 0) | |
776 | printk(KERN_ERR "tty_ldisc_ref_wait\n"); | |
777 | return &tty->ldisc; | |
778 | } | |
779 | ||
780 | EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait); | |
781 | ||
782 | /** | |
783 | * tty_ldisc_ref - get the tty ldisc | |
784 | * @tty: tty device | |
785 | * | |
786 | * Dereference the line discipline for the terminal and take a | |
787 | * reference to it. If the line discipline is in flux then | |
788 | * return NULL. Can be called from IRQ and timer functions. | |
af9b897e AC |
789 | * |
790 | * Locking: called functions take tty_ldisc_lock | |
1da177e4 LT |
791 | */ |
792 | ||
793 | struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty) | |
794 | { | |
795 | if(tty_ldisc_try(tty)) | |
796 | return &tty->ldisc; | |
797 | return NULL; | |
798 | } | |
799 | ||
800 | EXPORT_SYMBOL_GPL(tty_ldisc_ref); | |
801 | ||
802 | /** | |
803 | * tty_ldisc_deref - free a tty ldisc reference | |
804 | * @ld: reference to free up | |
805 | * | |
806 | * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May | |
807 | * be called in IRQ context. | |
af9b897e AC |
808 | * |
809 | * Locking: takes tty_ldisc_lock | |
1da177e4 LT |
810 | */ |
811 | ||
812 | void tty_ldisc_deref(struct tty_ldisc *ld) | |
813 | { | |
814 | unsigned long flags; | |
815 | ||
56ee4827 | 816 | BUG_ON(ld == NULL); |
1da177e4 LT |
817 | |
818 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
819 | if(ld->refcount == 0) | |
820 | printk(KERN_ERR "tty_ldisc_deref: no references.\n"); | |
821 | else | |
822 | ld->refcount--; | |
823 | if(ld->refcount == 0) | |
824 | wake_up(&tty_ldisc_wait); | |
825 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
826 | } | |
827 | ||
828 | EXPORT_SYMBOL_GPL(tty_ldisc_deref); | |
829 | ||
830 | /** | |
831 | * tty_ldisc_enable - allow ldisc use | |
832 | * @tty: terminal to activate ldisc on | |
833 | * | |
834 | * Set the TTY_LDISC flag when the line discipline can be called | |
835 | * again. Do neccessary wakeups for existing sleepers. | |
836 | * | |
837 | * Note: nobody should set this bit except via this function. Clearing | |
838 | * directly is allowed. | |
839 | */ | |
840 | ||
841 | static void tty_ldisc_enable(struct tty_struct *tty) | |
842 | { | |
843 | set_bit(TTY_LDISC, &tty->flags); | |
844 | wake_up(&tty_ldisc_wait); | |
845 | } | |
846 | ||
847 | /** | |
848 | * tty_set_ldisc - set line discipline | |
849 | * @tty: the terminal to set | |
850 | * @ldisc: the line discipline | |
851 | * | |
852 | * Set the discipline of a tty line. Must be called from a process | |
853 | * context. | |
af9b897e AC |
854 | * |
855 | * Locking: takes tty_ldisc_lock. | |
856 | * called functions take termios_sem | |
1da177e4 LT |
857 | */ |
858 | ||
859 | static int tty_set_ldisc(struct tty_struct *tty, int ldisc) | |
860 | { | |
ff55fe20 JB |
861 | int retval = 0; |
862 | struct tty_ldisc o_ldisc; | |
1da177e4 LT |
863 | char buf[64]; |
864 | int work; | |
865 | unsigned long flags; | |
866 | struct tty_ldisc *ld; | |
ff55fe20 | 867 | struct tty_struct *o_tty; |
1da177e4 LT |
868 | |
869 | if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS)) | |
870 | return -EINVAL; | |
871 | ||
872 | restart: | |
873 | ||
1da177e4 LT |
874 | ld = tty_ldisc_get(ldisc); |
875 | /* Eduardo Blanco <ejbs@cs.cs.com.uy> */ | |
876 | /* Cyrus Durgin <cider@speakeasy.org> */ | |
877 | if (ld == NULL) { | |
878 | request_module("tty-ldisc-%d", ldisc); | |
879 | ld = tty_ldisc_get(ldisc); | |
880 | } | |
881 | if (ld == NULL) | |
882 | return -EINVAL; | |
883 | ||
33f0f88f AC |
884 | /* |
885 | * No more input please, we are switching. The new ldisc | |
886 | * will update this value in the ldisc open function | |
887 | */ | |
888 | ||
889 | tty->receive_room = 0; | |
890 | ||
891 | /* | |
892 | * Problem: What do we do if this blocks ? | |
893 | */ | |
894 | ||
1da177e4 LT |
895 | tty_wait_until_sent(tty, 0); |
896 | ||
ff55fe20 JB |
897 | if (tty->ldisc.num == ldisc) { |
898 | tty_ldisc_put(ldisc); | |
899 | return 0; | |
900 | } | |
901 | ||
902 | o_ldisc = tty->ldisc; | |
903 | o_tty = tty->link; | |
904 | ||
1da177e4 LT |
905 | /* |
906 | * Make sure we don't change while someone holds a | |
907 | * reference to the line discipline. The TTY_LDISC bit | |
908 | * prevents anyone taking a reference once it is clear. | |
909 | * We need the lock to avoid racing reference takers. | |
910 | */ | |
ff55fe20 | 911 | |
1da177e4 | 912 | spin_lock_irqsave(&tty_ldisc_lock, flags); |
ff55fe20 JB |
913 | if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) { |
914 | if(tty->ldisc.refcount) { | |
915 | /* Free the new ldisc we grabbed. Must drop the lock | |
916 | first. */ | |
917 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
918 | tty_ldisc_put(ldisc); | |
919 | /* | |
920 | * There are several reasons we may be busy, including | |
921 | * random momentary I/O traffic. We must therefore | |
922 | * retry. We could distinguish between blocking ops | |
923 | * and retries if we made tty_ldisc_wait() smarter. That | |
924 | * is up for discussion. | |
925 | */ | |
926 | if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0) | |
927 | return -ERESTARTSYS; | |
928 | goto restart; | |
929 | } | |
930 | if(o_tty && o_tty->ldisc.refcount) { | |
931 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
932 | tty_ldisc_put(ldisc); | |
933 | if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0) | |
934 | return -ERESTARTSYS; | |
935 | goto restart; | |
936 | } | |
937 | } | |
938 | ||
939 | /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */ | |
940 | ||
941 | if (!test_bit(TTY_LDISC, &tty->flags)) { | |
1da177e4 LT |
942 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
943 | tty_ldisc_put(ldisc); | |
ff55fe20 JB |
944 | ld = tty_ldisc_ref_wait(tty); |
945 | tty_ldisc_deref(ld); | |
1da177e4 LT |
946 | goto restart; |
947 | } | |
ff55fe20 JB |
948 | |
949 | clear_bit(TTY_LDISC, &tty->flags); | |
817d6d3b | 950 | if (o_tty) |
ff55fe20 | 951 | clear_bit(TTY_LDISC, &o_tty->flags); |
1da177e4 | 952 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); |
ff55fe20 | 953 | |
1da177e4 LT |
954 | /* |
955 | * From this point on we know nobody has an ldisc | |
956 | * usage reference, nor can they obtain one until | |
957 | * we say so later on. | |
958 | */ | |
ff55fe20 | 959 | |
33f0f88f | 960 | work = cancel_delayed_work(&tty->buf.work); |
1da177e4 | 961 | /* |
33f0f88f | 962 | * Wait for ->hangup_work and ->buf.work handlers to terminate |
1da177e4 LT |
963 | */ |
964 | ||
965 | flush_scheduled_work(); | |
966 | /* Shutdown the current discipline. */ | |
967 | if (tty->ldisc.close) | |
968 | (tty->ldisc.close)(tty); | |
969 | ||
970 | /* Now set up the new line discipline. */ | |
971 | tty_ldisc_assign(tty, ld); | |
972 | tty_set_termios_ldisc(tty, ldisc); | |
973 | if (tty->ldisc.open) | |
974 | retval = (tty->ldisc.open)(tty); | |
975 | if (retval < 0) { | |
976 | tty_ldisc_put(ldisc); | |
977 | /* There is an outstanding reference here so this is safe */ | |
978 | tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num)); | |
979 | tty_set_termios_ldisc(tty, tty->ldisc.num); | |
980 | if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) { | |
981 | tty_ldisc_put(o_ldisc.num); | |
982 | /* This driver is always present */ | |
983 | tty_ldisc_assign(tty, tty_ldisc_get(N_TTY)); | |
984 | tty_set_termios_ldisc(tty, N_TTY); | |
985 | if (tty->ldisc.open) { | |
986 | int r = tty->ldisc.open(tty); | |
987 | ||
988 | if (r < 0) | |
989 | panic("Couldn't open N_TTY ldisc for " | |
990 | "%s --- error %d.", | |
991 | tty_name(tty, buf), r); | |
992 | } | |
993 | } | |
994 | } | |
995 | /* At this point we hold a reference to the new ldisc and a | |
996 | a reference to the old ldisc. If we ended up flipping back | |
997 | to the existing ldisc we have two references to it */ | |
998 | ||
999 | if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc) | |
1000 | tty->driver->set_ldisc(tty); | |
1001 | ||
1002 | tty_ldisc_put(o_ldisc.num); | |
1003 | ||
1004 | /* | |
1005 | * Allow ldisc referencing to occur as soon as the driver | |
1006 | * ldisc callback completes. | |
1007 | */ | |
1008 | ||
1009 | tty_ldisc_enable(tty); | |
ff55fe20 JB |
1010 | if (o_tty) |
1011 | tty_ldisc_enable(o_tty); | |
1da177e4 LT |
1012 | |
1013 | /* Restart it in case no characters kick it off. Safe if | |
1014 | already running */ | |
ff55fe20 | 1015 | if (work) |
33f0f88f | 1016 | schedule_delayed_work(&tty->buf.work, 1); |
1da177e4 LT |
1017 | return retval; |
1018 | } | |
1019 | ||
af9b897e AC |
1020 | /** |
1021 | * get_tty_driver - find device of a tty | |
1022 | * @dev_t: device identifier | |
1023 | * @index: returns the index of the tty | |
1024 | * | |
1025 | * This routine returns a tty driver structure, given a device number | |
1026 | * and also passes back the index number. | |
1027 | * | |
1028 | * Locking: caller must hold tty_mutex | |
1da177e4 | 1029 | */ |
af9b897e | 1030 | |
1da177e4 LT |
1031 | static struct tty_driver *get_tty_driver(dev_t device, int *index) |
1032 | { | |
1033 | struct tty_driver *p; | |
1034 | ||
1035 | list_for_each_entry(p, &tty_drivers, tty_drivers) { | |
1036 | dev_t base = MKDEV(p->major, p->minor_start); | |
1037 | if (device < base || device >= base + p->num) | |
1038 | continue; | |
1039 | *index = device - base; | |
1040 | return p; | |
1041 | } | |
1042 | return NULL; | |
1043 | } | |
1044 | ||
af9b897e AC |
1045 | /** |
1046 | * tty_check_change - check for POSIX terminal changes | |
1047 | * @tty: tty to check | |
1048 | * | |
1049 | * If we try to write to, or set the state of, a terminal and we're | |
1050 | * not in the foreground, send a SIGTTOU. If the signal is blocked or | |
1051 | * ignored, go ahead and perform the operation. (POSIX 7.2) | |
1052 | * | |
1053 | * Locking: none | |
1da177e4 | 1054 | */ |
af9b897e | 1055 | |
1da177e4 LT |
1056 | int tty_check_change(struct tty_struct * tty) |
1057 | { | |
1058 | if (current->signal->tty != tty) | |
1059 | return 0; | |
1060 | if (tty->pgrp <= 0) { | |
1061 | printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n"); | |
1062 | return 0; | |
1063 | } | |
1064 | if (process_group(current) == tty->pgrp) | |
1065 | return 0; | |
1066 | if (is_ignored(SIGTTOU)) | |
1067 | return 0; | |
1068 | if (is_orphaned_pgrp(process_group(current))) | |
1069 | return -EIO; | |
1070 | (void) kill_pg(process_group(current), SIGTTOU, 1); | |
1071 | return -ERESTARTSYS; | |
1072 | } | |
1073 | ||
1074 | EXPORT_SYMBOL(tty_check_change); | |
1075 | ||
1076 | static ssize_t hung_up_tty_read(struct file * file, char __user * buf, | |
1077 | size_t count, loff_t *ppos) | |
1078 | { | |
1079 | return 0; | |
1080 | } | |
1081 | ||
1082 | static ssize_t hung_up_tty_write(struct file * file, const char __user * buf, | |
1083 | size_t count, loff_t *ppos) | |
1084 | { | |
1085 | return -EIO; | |
1086 | } | |
1087 | ||
1088 | /* No kernel lock held - none needed ;) */ | |
1089 | static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait) | |
1090 | { | |
1091 | return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM; | |
1092 | } | |
1093 | ||
1094 | static int hung_up_tty_ioctl(struct inode * inode, struct file * file, | |
1095 | unsigned int cmd, unsigned long arg) | |
1096 | { | |
1097 | return cmd == TIOCSPGRP ? -ENOTTY : -EIO; | |
1098 | } | |
1099 | ||
62322d25 | 1100 | static const struct file_operations tty_fops = { |
1da177e4 LT |
1101 | .llseek = no_llseek, |
1102 | .read = tty_read, | |
1103 | .write = tty_write, | |
1104 | .poll = tty_poll, | |
1105 | .ioctl = tty_ioctl, | |
1106 | .open = tty_open, | |
1107 | .release = tty_release, | |
1108 | .fasync = tty_fasync, | |
1109 | }; | |
1110 | ||
1111 | #ifdef CONFIG_UNIX98_PTYS | |
62322d25 | 1112 | static const struct file_operations ptmx_fops = { |
1da177e4 LT |
1113 | .llseek = no_llseek, |
1114 | .read = tty_read, | |
1115 | .write = tty_write, | |
1116 | .poll = tty_poll, | |
1117 | .ioctl = tty_ioctl, | |
1118 | .open = ptmx_open, | |
1119 | .release = tty_release, | |
1120 | .fasync = tty_fasync, | |
1121 | }; | |
1122 | #endif | |
1123 | ||
62322d25 | 1124 | static const struct file_operations console_fops = { |
1da177e4 LT |
1125 | .llseek = no_llseek, |
1126 | .read = tty_read, | |
1127 | .write = redirected_tty_write, | |
1128 | .poll = tty_poll, | |
1129 | .ioctl = tty_ioctl, | |
1130 | .open = tty_open, | |
1131 | .release = tty_release, | |
1132 | .fasync = tty_fasync, | |
1133 | }; | |
1134 | ||
62322d25 | 1135 | static const struct file_operations hung_up_tty_fops = { |
1da177e4 LT |
1136 | .llseek = no_llseek, |
1137 | .read = hung_up_tty_read, | |
1138 | .write = hung_up_tty_write, | |
1139 | .poll = hung_up_tty_poll, | |
1140 | .ioctl = hung_up_tty_ioctl, | |
1141 | .release = tty_release, | |
1142 | }; | |
1143 | ||
1144 | static DEFINE_SPINLOCK(redirect_lock); | |
1145 | static struct file *redirect; | |
1146 | ||
1147 | /** | |
1148 | * tty_wakeup - request more data | |
1149 | * @tty: terminal | |
1150 | * | |
1151 | * Internal and external helper for wakeups of tty. This function | |
1152 | * informs the line discipline if present that the driver is ready | |
1153 | * to receive more output data. | |
1154 | */ | |
1155 | ||
1156 | void tty_wakeup(struct tty_struct *tty) | |
1157 | { | |
1158 | struct tty_ldisc *ld; | |
1159 | ||
1160 | if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) { | |
1161 | ld = tty_ldisc_ref(tty); | |
1162 | if(ld) { | |
1163 | if(ld->write_wakeup) | |
1164 | ld->write_wakeup(tty); | |
1165 | tty_ldisc_deref(ld); | |
1166 | } | |
1167 | } | |
1168 | wake_up_interruptible(&tty->write_wait); | |
1169 | } | |
1170 | ||
1171 | EXPORT_SYMBOL_GPL(tty_wakeup); | |
1172 | ||
1173 | /** | |
1174 | * tty_ldisc_flush - flush line discipline queue | |
1175 | * @tty: tty | |
1176 | * | |
1177 | * Flush the line discipline queue (if any) for this tty. If there | |
1178 | * is no line discipline active this is a no-op. | |
1179 | */ | |
1180 | ||
1181 | void tty_ldisc_flush(struct tty_struct *tty) | |
1182 | { | |
1183 | struct tty_ldisc *ld = tty_ldisc_ref(tty); | |
1184 | if(ld) { | |
1185 | if(ld->flush_buffer) | |
1186 | ld->flush_buffer(tty); | |
1187 | tty_ldisc_deref(ld); | |
1188 | } | |
1189 | } | |
1190 | ||
1191 | EXPORT_SYMBOL_GPL(tty_ldisc_flush); | |
1192 | ||
af9b897e AC |
1193 | /** |
1194 | * do_tty_hangup - actual handler for hangup events | |
1195 | * @data: tty device | |
1196 | * | |
1197 | * This can be called by the "eventd" kernel thread. That is process | |
1198 | * synchronous but doesn't hold any locks, so we need to make sure we | |
1199 | * have the appropriate locks for what we're doing. | |
1200 | * | |
1201 | * The hangup event clears any pending redirections onto the hung up | |
1202 | * device. It ensures future writes will error and it does the needed | |
1203 | * line discipline hangup and signal delivery. The tty object itself | |
1204 | * remains intact. | |
1205 | * | |
1206 | * Locking: | |
1207 | * BKL | |
1208 | * redirect lock for undoing redirection | |
1209 | * file list lock for manipulating list of ttys | |
1210 | * tty_ldisc_lock from called functions | |
1211 | * termios_sem resetting termios data | |
1212 | * tasklist_lock to walk task list for hangup event | |
1213 | * | |
1da177e4 LT |
1214 | */ |
1215 | static void do_tty_hangup(void *data) | |
1216 | { | |
1217 | struct tty_struct *tty = (struct tty_struct *) data; | |
1218 | struct file * cons_filp = NULL; | |
1219 | struct file *filp, *f = NULL; | |
1220 | struct task_struct *p; | |
1221 | struct tty_ldisc *ld; | |
1222 | int closecount = 0, n; | |
1223 | ||
1224 | if (!tty) | |
1225 | return; | |
1226 | ||
1227 | /* inuse_filps is protected by the single kernel lock */ | |
1228 | lock_kernel(); | |
1229 | ||
1230 | spin_lock(&redirect_lock); | |
1231 | if (redirect && redirect->private_data == tty) { | |
1232 | f = redirect; | |
1233 | redirect = NULL; | |
1234 | } | |
1235 | spin_unlock(&redirect_lock); | |
1236 | ||
1237 | check_tty_count(tty, "do_tty_hangup"); | |
1238 | file_list_lock(); | |
1239 | /* This breaks for file handles being sent over AF_UNIX sockets ? */ | |
2f512016 | 1240 | list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) { |
1da177e4 LT |
1241 | if (filp->f_op->write == redirected_tty_write) |
1242 | cons_filp = filp; | |
1243 | if (filp->f_op->write != tty_write) | |
1244 | continue; | |
1245 | closecount++; | |
1246 | tty_fasync(-1, filp, 0); /* can't block */ | |
1247 | filp->f_op = &hung_up_tty_fops; | |
1248 | } | |
1249 | file_list_unlock(); | |
1250 | ||
1251 | /* FIXME! What are the locking issues here? This may me overdoing things.. | |
1252 | * this question is especially important now that we've removed the irqlock. */ | |
1253 | ||
1254 | ld = tty_ldisc_ref(tty); | |
1255 | if(ld != NULL) /* We may have no line discipline at this point */ | |
1256 | { | |
1257 | if (ld->flush_buffer) | |
1258 | ld->flush_buffer(tty); | |
1259 | if (tty->driver->flush_buffer) | |
1260 | tty->driver->flush_buffer(tty); | |
1261 | if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) && | |
1262 | ld->write_wakeup) | |
1263 | ld->write_wakeup(tty); | |
1264 | if (ld->hangup) | |
1265 | ld->hangup(tty); | |
1266 | } | |
1267 | ||
1268 | /* FIXME: Once we trust the LDISC code better we can wait here for | |
1269 | ldisc completion and fix the driver call race */ | |
1270 | ||
1271 | wake_up_interruptible(&tty->write_wait); | |
1272 | wake_up_interruptible(&tty->read_wait); | |
1273 | ||
1274 | /* | |
1275 | * Shutdown the current line discipline, and reset it to | |
1276 | * N_TTY. | |
1277 | */ | |
1278 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) | |
1279 | { | |
1280 | down(&tty->termios_sem); | |
1281 | *tty->termios = tty->driver->init_termios; | |
1282 | up(&tty->termios_sem); | |
1283 | } | |
1284 | ||
1285 | /* Defer ldisc switch */ | |
1286 | /* tty_deferred_ldisc_switch(N_TTY); | |
1287 | ||
1288 | This should get done automatically when the port closes and | |
1289 | tty_release is called */ | |
1290 | ||
1291 | read_lock(&tasklist_lock); | |
1292 | if (tty->session > 0) { | |
1293 | do_each_task_pid(tty->session, PIDTYPE_SID, p) { | |
1294 | if (p->signal->tty == tty) | |
1295 | p->signal->tty = NULL; | |
1296 | if (!p->signal->leader) | |
1297 | continue; | |
f96a795d EB |
1298 | group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p); |
1299 | group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p); | |
1da177e4 LT |
1300 | if (tty->pgrp > 0) |
1301 | p->signal->tty_old_pgrp = tty->pgrp; | |
1302 | } while_each_task_pid(tty->session, PIDTYPE_SID, p); | |
1303 | } | |
1304 | read_unlock(&tasklist_lock); | |
1305 | ||
1306 | tty->flags = 0; | |
1307 | tty->session = 0; | |
1308 | tty->pgrp = -1; | |
1309 | tty->ctrl_status = 0; | |
1310 | /* | |
1311 | * If one of the devices matches a console pointer, we | |
1312 | * cannot just call hangup() because that will cause | |
1313 | * tty->count and state->count to go out of sync. | |
1314 | * So we just call close() the right number of times. | |
1315 | */ | |
1316 | if (cons_filp) { | |
1317 | if (tty->driver->close) | |
1318 | for (n = 0; n < closecount; n++) | |
1319 | tty->driver->close(tty, cons_filp); | |
1320 | } else if (tty->driver->hangup) | |
1321 | (tty->driver->hangup)(tty); | |
1322 | ||
1323 | /* We don't want to have driver/ldisc interactions beyond | |
1324 | the ones we did here. The driver layer expects no | |
1325 | calls after ->hangup() from the ldisc side. However we | |
1326 | can't yet guarantee all that */ | |
1327 | ||
1328 | set_bit(TTY_HUPPED, &tty->flags); | |
1329 | if (ld) { | |
1330 | tty_ldisc_enable(tty); | |
1331 | tty_ldisc_deref(ld); | |
1332 | } | |
1333 | unlock_kernel(); | |
1334 | if (f) | |
1335 | fput(f); | |
1336 | } | |
1337 | ||
af9b897e AC |
1338 | /** |
1339 | * tty_hangup - trigger a hangup event | |
1340 | * @tty: tty to hangup | |
1341 | * | |
1342 | * A carrier loss (virtual or otherwise) has occurred on this like | |
1343 | * schedule a hangup sequence to run after this event. | |
1344 | */ | |
1345 | ||
1da177e4 LT |
1346 | void tty_hangup(struct tty_struct * tty) |
1347 | { | |
1348 | #ifdef TTY_DEBUG_HANGUP | |
1349 | char buf[64]; | |
1350 | ||
1351 | printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf)); | |
1352 | #endif | |
1353 | schedule_work(&tty->hangup_work); | |
1354 | } | |
1355 | ||
1356 | EXPORT_SYMBOL(tty_hangup); | |
1357 | ||
af9b897e AC |
1358 | /** |
1359 | * tty_vhangup - process vhangup | |
1360 | * @tty: tty to hangup | |
1361 | * | |
1362 | * The user has asked via system call for the terminal to be hung up. | |
1363 | * We do this synchronously so that when the syscall returns the process | |
1364 | * is complete. That guarantee is neccessary for security reasons. | |
1365 | */ | |
1366 | ||
1da177e4 LT |
1367 | void tty_vhangup(struct tty_struct * tty) |
1368 | { | |
1369 | #ifdef TTY_DEBUG_HANGUP | |
1370 | char buf[64]; | |
1371 | ||
1372 | printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf)); | |
1373 | #endif | |
1374 | do_tty_hangup((void *) tty); | |
1375 | } | |
1376 | EXPORT_SYMBOL(tty_vhangup); | |
1377 | ||
af9b897e AC |
1378 | /** |
1379 | * tty_hung_up_p - was tty hung up | |
1380 | * @filp: file pointer of tty | |
1381 | * | |
1382 | * Return true if the tty has been subject to a vhangup or a carrier | |
1383 | * loss | |
1384 | */ | |
1385 | ||
1da177e4 LT |
1386 | int tty_hung_up_p(struct file * filp) |
1387 | { | |
1388 | return (filp->f_op == &hung_up_tty_fops); | |
1389 | } | |
1390 | ||
1391 | EXPORT_SYMBOL(tty_hung_up_p); | |
1392 | ||
af9b897e AC |
1393 | /** |
1394 | * disassociate_ctty - disconnect controlling tty | |
1395 | * @on_exit: true if exiting so need to "hang up" the session | |
1da177e4 | 1396 | * |
af9b897e AC |
1397 | * This function is typically called only by the session leader, when |
1398 | * it wants to disassociate itself from its controlling tty. | |
1399 | * | |
1400 | * It performs the following functions: | |
1da177e4 LT |
1401 | * (1) Sends a SIGHUP and SIGCONT to the foreground process group |
1402 | * (2) Clears the tty from being controlling the session | |
1403 | * (3) Clears the controlling tty for all processes in the | |
1404 | * session group. | |
1405 | * | |
af9b897e AC |
1406 | * The argument on_exit is set to 1 if called when a process is |
1407 | * exiting; it is 0 if called by the ioctl TIOCNOTTY. | |
1408 | * | |
1409 | * Locking: tty_mutex is taken to protect current->signal->tty | |
1410 | * BKL is taken for hysterical raisins | |
1411 | * Tasklist lock is taken (under tty_mutex) to walk process | |
1412 | * lists for the session. | |
1da177e4 | 1413 | */ |
af9b897e | 1414 | |
1da177e4 LT |
1415 | void disassociate_ctty(int on_exit) |
1416 | { | |
1417 | struct tty_struct *tty; | |
1418 | struct task_struct *p; | |
1419 | int tty_pgrp = -1; | |
1420 | ||
1421 | lock_kernel(); | |
1422 | ||
70522e12 | 1423 | mutex_lock(&tty_mutex); |
1da177e4 LT |
1424 | tty = current->signal->tty; |
1425 | if (tty) { | |
1426 | tty_pgrp = tty->pgrp; | |
70522e12 | 1427 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
1428 | if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) |
1429 | tty_vhangup(tty); | |
1430 | } else { | |
1431 | if (current->signal->tty_old_pgrp) { | |
1432 | kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit); | |
1433 | kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit); | |
1434 | } | |
70522e12 | 1435 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
1436 | unlock_kernel(); |
1437 | return; | |
1438 | } | |
1439 | if (tty_pgrp > 0) { | |
1440 | kill_pg(tty_pgrp, SIGHUP, on_exit); | |
1441 | if (!on_exit) | |
1442 | kill_pg(tty_pgrp, SIGCONT, on_exit); | |
1443 | } | |
1444 | ||
1445 | /* Must lock changes to tty_old_pgrp */ | |
70522e12 | 1446 | mutex_lock(&tty_mutex); |
1da177e4 LT |
1447 | current->signal->tty_old_pgrp = 0; |
1448 | tty->session = 0; | |
1449 | tty->pgrp = -1; | |
1450 | ||
1451 | /* Now clear signal->tty under the lock */ | |
1452 | read_lock(&tasklist_lock); | |
1453 | do_each_task_pid(current->signal->session, PIDTYPE_SID, p) { | |
1454 | p->signal->tty = NULL; | |
1455 | } while_each_task_pid(current->signal->session, PIDTYPE_SID, p); | |
1456 | read_unlock(&tasklist_lock); | |
70522e12 | 1457 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
1458 | unlock_kernel(); |
1459 | } | |
1460 | ||
af9b897e AC |
1461 | |
1462 | /** | |
1463 | * stop_tty - propogate flow control | |
1464 | * @tty: tty to stop | |
1465 | * | |
1466 | * Perform flow control to the driver. For PTY/TTY pairs we | |
1467 | * must also propogate the TIOCKPKT status. May be called | |
1468 | * on an already stopped device and will not re-call the driver | |
1469 | * method. | |
1470 | * | |
1471 | * This functionality is used by both the line disciplines for | |
1472 | * halting incoming flow and by the driver. It may therefore be | |
1473 | * called from any context, may be under the tty atomic_write_lock | |
1474 | * but not always. | |
1475 | * | |
1476 | * Locking: | |
1477 | * Broken. Relies on BKL which is unsafe here. | |
1478 | */ | |
1479 | ||
1da177e4 LT |
1480 | void stop_tty(struct tty_struct *tty) |
1481 | { | |
1482 | if (tty->stopped) | |
1483 | return; | |
1484 | tty->stopped = 1; | |
1485 | if (tty->link && tty->link->packet) { | |
1486 | tty->ctrl_status &= ~TIOCPKT_START; | |
1487 | tty->ctrl_status |= TIOCPKT_STOP; | |
1488 | wake_up_interruptible(&tty->link->read_wait); | |
1489 | } | |
1490 | if (tty->driver->stop) | |
1491 | (tty->driver->stop)(tty); | |
1492 | } | |
1493 | ||
1494 | EXPORT_SYMBOL(stop_tty); | |
1495 | ||
af9b897e AC |
1496 | /** |
1497 | * start_tty - propogate flow control | |
1498 | * @tty: tty to start | |
1499 | * | |
1500 | * Start a tty that has been stopped if at all possible. Perform | |
1501 | * any neccessary wakeups and propogate the TIOCPKT status. If this | |
1502 | * is the tty was previous stopped and is being started then the | |
1503 | * driver start method is invoked and the line discipline woken. | |
1504 | * | |
1505 | * Locking: | |
1506 | * Broken. Relies on BKL which is unsafe here. | |
1507 | */ | |
1508 | ||
1da177e4 LT |
1509 | void start_tty(struct tty_struct *tty) |
1510 | { | |
1511 | if (!tty->stopped || tty->flow_stopped) | |
1512 | return; | |
1513 | tty->stopped = 0; | |
1514 | if (tty->link && tty->link->packet) { | |
1515 | tty->ctrl_status &= ~TIOCPKT_STOP; | |
1516 | tty->ctrl_status |= TIOCPKT_START; | |
1517 | wake_up_interruptible(&tty->link->read_wait); | |
1518 | } | |
1519 | if (tty->driver->start) | |
1520 | (tty->driver->start)(tty); | |
1521 | ||
1522 | /* If we have a running line discipline it may need kicking */ | |
1523 | tty_wakeup(tty); | |
1524 | wake_up_interruptible(&tty->write_wait); | |
1525 | } | |
1526 | ||
1527 | EXPORT_SYMBOL(start_tty); | |
1528 | ||
af9b897e AC |
1529 | /** |
1530 | * tty_read - read method for tty device files | |
1531 | * @file: pointer to tty file | |
1532 | * @buf: user buffer | |
1533 | * @count: size of user buffer | |
1534 | * @ppos: unused | |
1535 | * | |
1536 | * Perform the read system call function on this terminal device. Checks | |
1537 | * for hung up devices before calling the line discipline method. | |
1538 | * | |
1539 | * Locking: | |
1540 | * Locks the line discipline internally while needed | |
1541 | * For historical reasons the line discipline read method is | |
1542 | * invoked under the BKL. This will go away in time so do not rely on it | |
1543 | * in new code. Multiple read calls may be outstanding in parallel. | |
1544 | */ | |
1545 | ||
1da177e4 LT |
1546 | static ssize_t tty_read(struct file * file, char __user * buf, size_t count, |
1547 | loff_t *ppos) | |
1548 | { | |
1549 | int i; | |
1550 | struct tty_struct * tty; | |
1551 | struct inode *inode; | |
1552 | struct tty_ldisc *ld; | |
1553 | ||
1554 | tty = (struct tty_struct *)file->private_data; | |
1555 | inode = file->f_dentry->d_inode; | |
1556 | if (tty_paranoia_check(tty, inode, "tty_read")) | |
1557 | return -EIO; | |
1558 | if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags))) | |
1559 | return -EIO; | |
1560 | ||
1561 | /* We want to wait for the line discipline to sort out in this | |
1562 | situation */ | |
1563 | ld = tty_ldisc_ref_wait(tty); | |
1564 | lock_kernel(); | |
1565 | if (ld->read) | |
1566 | i = (ld->read)(tty,file,buf,count); | |
1567 | else | |
1568 | i = -EIO; | |
1569 | tty_ldisc_deref(ld); | |
1570 | unlock_kernel(); | |
1571 | if (i > 0) | |
1572 | inode->i_atime = current_fs_time(inode->i_sb); | |
1573 | return i; | |
1574 | } | |
1575 | ||
1576 | /* | |
1577 | * Split writes up in sane blocksizes to avoid | |
1578 | * denial-of-service type attacks | |
1579 | */ | |
1580 | static inline ssize_t do_tty_write( | |
1581 | ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t), | |
1582 | struct tty_struct *tty, | |
1583 | struct file *file, | |
1584 | const char __user *buf, | |
1585 | size_t count) | |
1586 | { | |
1587 | ssize_t ret = 0, written = 0; | |
1588 | unsigned int chunk; | |
1589 | ||
af9b897e | 1590 | /* FIXME: O_NDELAY ... */ |
70522e12 | 1591 | if (mutex_lock_interruptible(&tty->atomic_write_lock)) { |
1da177e4 LT |
1592 | return -ERESTARTSYS; |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * We chunk up writes into a temporary buffer. This | |
1597 | * simplifies low-level drivers immensely, since they | |
1598 | * don't have locking issues and user mode accesses. | |
1599 | * | |
1600 | * But if TTY_NO_WRITE_SPLIT is set, we should use a | |
1601 | * big chunk-size.. | |
1602 | * | |
1603 | * The default chunk-size is 2kB, because the NTTY | |
1604 | * layer has problems with bigger chunks. It will | |
1605 | * claim to be able to handle more characters than | |
1606 | * it actually does. | |
af9b897e AC |
1607 | * |
1608 | * FIXME: This can probably go away now except that 64K chunks | |
1609 | * are too likely to fail unless switched to vmalloc... | |
1da177e4 LT |
1610 | */ |
1611 | chunk = 2048; | |
1612 | if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags)) | |
1613 | chunk = 65536; | |
1614 | if (count < chunk) | |
1615 | chunk = count; | |
1616 | ||
70522e12 | 1617 | /* write_buf/write_cnt is protected by the atomic_write_lock mutex */ |
1da177e4 LT |
1618 | if (tty->write_cnt < chunk) { |
1619 | unsigned char *buf; | |
1620 | ||
1621 | if (chunk < 1024) | |
1622 | chunk = 1024; | |
1623 | ||
1624 | buf = kmalloc(chunk, GFP_KERNEL); | |
1625 | if (!buf) { | |
70522e12 | 1626 | mutex_unlock(&tty->atomic_write_lock); |
1da177e4 LT |
1627 | return -ENOMEM; |
1628 | } | |
1629 | kfree(tty->write_buf); | |
1630 | tty->write_cnt = chunk; | |
1631 | tty->write_buf = buf; | |
1632 | } | |
1633 | ||
1634 | /* Do the write .. */ | |
1635 | for (;;) { | |
1636 | size_t size = count; | |
1637 | if (size > chunk) | |
1638 | size = chunk; | |
1639 | ret = -EFAULT; | |
1640 | if (copy_from_user(tty->write_buf, buf, size)) | |
1641 | break; | |
1642 | lock_kernel(); | |
1643 | ret = write(tty, file, tty->write_buf, size); | |
1644 | unlock_kernel(); | |
1645 | if (ret <= 0) | |
1646 | break; | |
1647 | written += ret; | |
1648 | buf += ret; | |
1649 | count -= ret; | |
1650 | if (!count) | |
1651 | break; | |
1652 | ret = -ERESTARTSYS; | |
1653 | if (signal_pending(current)) | |
1654 | break; | |
1655 | cond_resched(); | |
1656 | } | |
1657 | if (written) { | |
1658 | struct inode *inode = file->f_dentry->d_inode; | |
1659 | inode->i_mtime = current_fs_time(inode->i_sb); | |
1660 | ret = written; | |
1661 | } | |
70522e12 | 1662 | mutex_unlock(&tty->atomic_write_lock); |
1da177e4 LT |
1663 | return ret; |
1664 | } | |
1665 | ||
1666 | ||
af9b897e AC |
1667 | /** |
1668 | * tty_write - write method for tty device file | |
1669 | * @file: tty file pointer | |
1670 | * @buf: user data to write | |
1671 | * @count: bytes to write | |
1672 | * @ppos: unused | |
1673 | * | |
1674 | * Write data to a tty device via the line discipline. | |
1675 | * | |
1676 | * Locking: | |
1677 | * Locks the line discipline as required | |
1678 | * Writes to the tty driver are serialized by the atomic_write_lock | |
1679 | * and are then processed in chunks to the device. The line discipline | |
1680 | * write method will not be involked in parallel for each device | |
1681 | * The line discipline write method is called under the big | |
1682 | * kernel lock for historical reasons. New code should not rely on this. | |
1683 | */ | |
1684 | ||
1da177e4 LT |
1685 | static ssize_t tty_write(struct file * file, const char __user * buf, size_t count, |
1686 | loff_t *ppos) | |
1687 | { | |
1688 | struct tty_struct * tty; | |
1689 | struct inode *inode = file->f_dentry->d_inode; | |
1690 | ssize_t ret; | |
1691 | struct tty_ldisc *ld; | |
1692 | ||
1693 | tty = (struct tty_struct *)file->private_data; | |
1694 | if (tty_paranoia_check(tty, inode, "tty_write")) | |
1695 | return -EIO; | |
1696 | if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags))) | |
1697 | return -EIO; | |
1698 | ||
1699 | ld = tty_ldisc_ref_wait(tty); | |
1700 | if (!ld->write) | |
1701 | ret = -EIO; | |
1702 | else | |
1703 | ret = do_tty_write(ld->write, tty, file, buf, count); | |
1704 | tty_ldisc_deref(ld); | |
1705 | return ret; | |
1706 | } | |
1707 | ||
1708 | ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count, | |
1709 | loff_t *ppos) | |
1710 | { | |
1711 | struct file *p = NULL; | |
1712 | ||
1713 | spin_lock(&redirect_lock); | |
1714 | if (redirect) { | |
1715 | get_file(redirect); | |
1716 | p = redirect; | |
1717 | } | |
1718 | spin_unlock(&redirect_lock); | |
1719 | ||
1720 | if (p) { | |
1721 | ssize_t res; | |
1722 | res = vfs_write(p, buf, count, &p->f_pos); | |
1723 | fput(p); | |
1724 | return res; | |
1725 | } | |
1726 | ||
1727 | return tty_write(file, buf, count, ppos); | |
1728 | } | |
1729 | ||
1730 | static char ptychar[] = "pqrstuvwxyzabcde"; | |
1731 | ||
af9b897e AC |
1732 | /** |
1733 | * pty_line_name - generate name for a pty | |
1734 | * @driver: the tty driver in use | |
1735 | * @index: the minor number | |
1736 | * @p: output buffer of at least 6 bytes | |
1737 | * | |
1738 | * Generate a name from a driver reference and write it to the output | |
1739 | * buffer. | |
1740 | * | |
1741 | * Locking: None | |
1742 | */ | |
1743 | static void pty_line_name(struct tty_driver *driver, int index, char *p) | |
1da177e4 LT |
1744 | { |
1745 | int i = index + driver->name_base; | |
1746 | /* ->name is initialized to "ttyp", but "tty" is expected */ | |
1747 | sprintf(p, "%s%c%x", | |
1748 | driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name, | |
1749 | ptychar[i >> 4 & 0xf], i & 0xf); | |
1750 | } | |
1751 | ||
af9b897e AC |
1752 | /** |
1753 | * pty_line_name - generate name for a tty | |
1754 | * @driver: the tty driver in use | |
1755 | * @index: the minor number | |
1756 | * @p: output buffer of at least 7 bytes | |
1757 | * | |
1758 | * Generate a name from a driver reference and write it to the output | |
1759 | * buffer. | |
1760 | * | |
1761 | * Locking: None | |
1762 | */ | |
1763 | static void tty_line_name(struct tty_driver *driver, int index, char *p) | |
1da177e4 LT |
1764 | { |
1765 | sprintf(p, "%s%d", driver->name, index + driver->name_base); | |
1766 | } | |
1767 | ||
af9b897e AC |
1768 | /** |
1769 | * init_dev - initialise a tty device | |
1770 | * @driver: tty driver we are opening a device on | |
1771 | * @idx: device index | |
1772 | * @tty: returned tty structure | |
1773 | * | |
1774 | * Prepare a tty device. This may not be a "new" clean device but | |
1775 | * could also be an active device. The pty drivers require special | |
1776 | * handling because of this. | |
1777 | * | |
1778 | * Locking: | |
1779 | * The function is called under the tty_mutex, which | |
1780 | * protects us from the tty struct or driver itself going away. | |
1781 | * | |
1782 | * On exit the tty device has the line discipline attached and | |
1783 | * a reference count of 1. If a pair was created for pty/tty use | |
1784 | * and the other was a pty master then it too has a reference count of 1. | |
1785 | * | |
1da177e4 | 1786 | * WSH 06/09/97: Rewritten to remove races and properly clean up after a |
70522e12 IM |
1787 | * failed open. The new code protects the open with a mutex, so it's |
1788 | * really quite straightforward. The mutex locking can probably be | |
1da177e4 LT |
1789 | * relaxed for the (most common) case of reopening a tty. |
1790 | */ | |
af9b897e | 1791 | |
1da177e4 LT |
1792 | static int init_dev(struct tty_driver *driver, int idx, |
1793 | struct tty_struct **ret_tty) | |
1794 | { | |
1795 | struct tty_struct *tty, *o_tty; | |
1796 | struct termios *tp, **tp_loc, *o_tp, **o_tp_loc; | |
1797 | struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc; | |
af9b897e | 1798 | int retval = 0; |
1da177e4 LT |
1799 | |
1800 | /* check whether we're reopening an existing tty */ | |
1801 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { | |
1802 | tty = devpts_get_tty(idx); | |
1803 | if (tty && driver->subtype == PTY_TYPE_MASTER) | |
1804 | tty = tty->link; | |
1805 | } else { | |
1806 | tty = driver->ttys[idx]; | |
1807 | } | |
1808 | if (tty) goto fast_track; | |
1809 | ||
1810 | /* | |
1811 | * First time open is complex, especially for PTY devices. | |
1812 | * This code guarantees that either everything succeeds and the | |
1813 | * TTY is ready for operation, or else the table slots are vacated | |
1814 | * and the allocated memory released. (Except that the termios | |
1815 | * and locked termios may be retained.) | |
1816 | */ | |
1817 | ||
1818 | if (!try_module_get(driver->owner)) { | |
1819 | retval = -ENODEV; | |
1820 | goto end_init; | |
1821 | } | |
1822 | ||
1823 | o_tty = NULL; | |
1824 | tp = o_tp = NULL; | |
1825 | ltp = o_ltp = NULL; | |
1826 | ||
1827 | tty = alloc_tty_struct(); | |
1828 | if(!tty) | |
1829 | goto fail_no_mem; | |
1830 | initialize_tty_struct(tty); | |
1831 | tty->driver = driver; | |
1832 | tty->index = idx; | |
1833 | tty_line_name(driver, idx, tty->name); | |
1834 | ||
1835 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { | |
1836 | tp_loc = &tty->termios; | |
1837 | ltp_loc = &tty->termios_locked; | |
1838 | } else { | |
1839 | tp_loc = &driver->termios[idx]; | |
1840 | ltp_loc = &driver->termios_locked[idx]; | |
1841 | } | |
1842 | ||
1843 | if (!*tp_loc) { | |
1844 | tp = (struct termios *) kmalloc(sizeof(struct termios), | |
1845 | GFP_KERNEL); | |
1846 | if (!tp) | |
1847 | goto free_mem_out; | |
1848 | *tp = driver->init_termios; | |
1849 | } | |
1850 | ||
1851 | if (!*ltp_loc) { | |
1852 | ltp = (struct termios *) kmalloc(sizeof(struct termios), | |
1853 | GFP_KERNEL); | |
1854 | if (!ltp) | |
1855 | goto free_mem_out; | |
1856 | memset(ltp, 0, sizeof(struct termios)); | |
1857 | } | |
1858 | ||
1859 | if (driver->type == TTY_DRIVER_TYPE_PTY) { | |
1860 | o_tty = alloc_tty_struct(); | |
1861 | if (!o_tty) | |
1862 | goto free_mem_out; | |
1863 | initialize_tty_struct(o_tty); | |
1864 | o_tty->driver = driver->other; | |
1865 | o_tty->index = idx; | |
1866 | tty_line_name(driver->other, idx, o_tty->name); | |
1867 | ||
1868 | if (driver->flags & TTY_DRIVER_DEVPTS_MEM) { | |
1869 | o_tp_loc = &o_tty->termios; | |
1870 | o_ltp_loc = &o_tty->termios_locked; | |
1871 | } else { | |
1872 | o_tp_loc = &driver->other->termios[idx]; | |
1873 | o_ltp_loc = &driver->other->termios_locked[idx]; | |
1874 | } | |
1875 | ||
1876 | if (!*o_tp_loc) { | |
1877 | o_tp = (struct termios *) | |
1878 | kmalloc(sizeof(struct termios), GFP_KERNEL); | |
1879 | if (!o_tp) | |
1880 | goto free_mem_out; | |
1881 | *o_tp = driver->other->init_termios; | |
1882 | } | |
1883 | ||
1884 | if (!*o_ltp_loc) { | |
1885 | o_ltp = (struct termios *) | |
1886 | kmalloc(sizeof(struct termios), GFP_KERNEL); | |
1887 | if (!o_ltp) | |
1888 | goto free_mem_out; | |
1889 | memset(o_ltp, 0, sizeof(struct termios)); | |
1890 | } | |
1891 | ||
1892 | /* | |
1893 | * Everything allocated ... set up the o_tty structure. | |
1894 | */ | |
1895 | if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) { | |
1896 | driver->other->ttys[idx] = o_tty; | |
1897 | } | |
1898 | if (!*o_tp_loc) | |
1899 | *o_tp_loc = o_tp; | |
1900 | if (!*o_ltp_loc) | |
1901 | *o_ltp_loc = o_ltp; | |
1902 | o_tty->termios = *o_tp_loc; | |
1903 | o_tty->termios_locked = *o_ltp_loc; | |
1904 | driver->other->refcount++; | |
1905 | if (driver->subtype == PTY_TYPE_MASTER) | |
1906 | o_tty->count++; | |
1907 | ||
1908 | /* Establish the links in both directions */ | |
1909 | tty->link = o_tty; | |
1910 | o_tty->link = tty; | |
1911 | } | |
1912 | ||
1913 | /* | |
1914 | * All structures have been allocated, so now we install them. | |
1915 | * Failures after this point use release_mem to clean up, so | |
1916 | * there's no need to null out the local pointers. | |
1917 | */ | |
1918 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) { | |
1919 | driver->ttys[idx] = tty; | |
1920 | } | |
1921 | ||
1922 | if (!*tp_loc) | |
1923 | *tp_loc = tp; | |
1924 | if (!*ltp_loc) | |
1925 | *ltp_loc = ltp; | |
1926 | tty->termios = *tp_loc; | |
1927 | tty->termios_locked = *ltp_loc; | |
1928 | driver->refcount++; | |
1929 | tty->count++; | |
1930 | ||
1931 | /* | |
1932 | * Structures all installed ... call the ldisc open routines. | |
1933 | * If we fail here just call release_mem to clean up. No need | |
1934 | * to decrement the use counts, as release_mem doesn't care. | |
1935 | */ | |
1936 | ||
1937 | if (tty->ldisc.open) { | |
1938 | retval = (tty->ldisc.open)(tty); | |
1939 | if (retval) | |
1940 | goto release_mem_out; | |
1941 | } | |
1942 | if (o_tty && o_tty->ldisc.open) { | |
1943 | retval = (o_tty->ldisc.open)(o_tty); | |
1944 | if (retval) { | |
1945 | if (tty->ldisc.close) | |
1946 | (tty->ldisc.close)(tty); | |
1947 | goto release_mem_out; | |
1948 | } | |
1949 | tty_ldisc_enable(o_tty); | |
1950 | } | |
1951 | tty_ldisc_enable(tty); | |
1952 | goto success; | |
1953 | ||
1954 | /* | |
1955 | * This fast open can be used if the tty is already open. | |
1956 | * No memory is allocated, and the only failures are from | |
1957 | * attempting to open a closing tty or attempting multiple | |
1958 | * opens on a pty master. | |
1959 | */ | |
1960 | fast_track: | |
1961 | if (test_bit(TTY_CLOSING, &tty->flags)) { | |
1962 | retval = -EIO; | |
1963 | goto end_init; | |
1964 | } | |
1965 | if (driver->type == TTY_DRIVER_TYPE_PTY && | |
1966 | driver->subtype == PTY_TYPE_MASTER) { | |
1967 | /* | |
1968 | * special case for PTY masters: only one open permitted, | |
1969 | * and the slave side open count is incremented as well. | |
1970 | */ | |
1971 | if (tty->count) { | |
1972 | retval = -EIO; | |
1973 | goto end_init; | |
1974 | } | |
1975 | tty->link->count++; | |
1976 | } | |
1977 | tty->count++; | |
1978 | tty->driver = driver; /* N.B. why do this every time?? */ | |
1979 | ||
1980 | /* FIXME */ | |
1981 | if(!test_bit(TTY_LDISC, &tty->flags)) | |
1982 | printk(KERN_ERR "init_dev but no ldisc\n"); | |
1983 | success: | |
1984 | *ret_tty = tty; | |
1985 | ||
70522e12 | 1986 | /* All paths come through here to release the mutex */ |
1da177e4 LT |
1987 | end_init: |
1988 | return retval; | |
1989 | ||
1990 | /* Release locally allocated memory ... nothing placed in slots */ | |
1991 | free_mem_out: | |
735d5661 | 1992 | kfree(o_tp); |
1da177e4 LT |
1993 | if (o_tty) |
1994 | free_tty_struct(o_tty); | |
735d5661 JJ |
1995 | kfree(ltp); |
1996 | kfree(tp); | |
1da177e4 LT |
1997 | free_tty_struct(tty); |
1998 | ||
1999 | fail_no_mem: | |
2000 | module_put(driver->owner); | |
2001 | retval = -ENOMEM; | |
2002 | goto end_init; | |
2003 | ||
2004 | /* call the tty release_mem routine to clean out this slot */ | |
2005 | release_mem_out: | |
2006 | printk(KERN_INFO "init_dev: ldisc open failed, " | |
2007 | "clearing slot %d\n", idx); | |
2008 | release_mem(tty, idx); | |
2009 | goto end_init; | |
2010 | } | |
2011 | ||
af9b897e AC |
2012 | /** |
2013 | * release_mem - release tty structure memory | |
2014 | * | |
2015 | * Releases memory associated with a tty structure, and clears out the | |
2016 | * driver table slots. This function is called when a device is no longer | |
2017 | * in use. It also gets called when setup of a device fails. | |
2018 | * | |
2019 | * Locking: | |
2020 | * tty_mutex - sometimes only | |
2021 | * takes the file list lock internally when working on the list | |
2022 | * of ttys that the driver keeps. | |
2023 | * FIXME: should we require tty_mutex is held here ?? | |
1da177e4 | 2024 | */ |
af9b897e | 2025 | |
1da177e4 LT |
2026 | static void release_mem(struct tty_struct *tty, int idx) |
2027 | { | |
2028 | struct tty_struct *o_tty; | |
2029 | struct termios *tp; | |
2030 | int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM; | |
2031 | ||
2032 | if ((o_tty = tty->link) != NULL) { | |
2033 | if (!devpts) | |
2034 | o_tty->driver->ttys[idx] = NULL; | |
2035 | if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { | |
2036 | tp = o_tty->termios; | |
2037 | if (!devpts) | |
2038 | o_tty->driver->termios[idx] = NULL; | |
2039 | kfree(tp); | |
2040 | ||
2041 | tp = o_tty->termios_locked; | |
2042 | if (!devpts) | |
2043 | o_tty->driver->termios_locked[idx] = NULL; | |
2044 | kfree(tp); | |
2045 | } | |
2046 | o_tty->magic = 0; | |
2047 | o_tty->driver->refcount--; | |
2048 | file_list_lock(); | |
2049 | list_del_init(&o_tty->tty_files); | |
2050 | file_list_unlock(); | |
2051 | free_tty_struct(o_tty); | |
2052 | } | |
2053 | ||
2054 | if (!devpts) | |
2055 | tty->driver->ttys[idx] = NULL; | |
2056 | if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) { | |
2057 | tp = tty->termios; | |
2058 | if (!devpts) | |
2059 | tty->driver->termios[idx] = NULL; | |
2060 | kfree(tp); | |
2061 | ||
2062 | tp = tty->termios_locked; | |
2063 | if (!devpts) | |
2064 | tty->driver->termios_locked[idx] = NULL; | |
2065 | kfree(tp); | |
2066 | } | |
2067 | ||
2068 | tty->magic = 0; | |
2069 | tty->driver->refcount--; | |
2070 | file_list_lock(); | |
2071 | list_del_init(&tty->tty_files); | |
2072 | file_list_unlock(); | |
2073 | module_put(tty->driver->owner); | |
2074 | free_tty_struct(tty); | |
2075 | } | |
2076 | ||
2077 | /* | |
2078 | * Even releasing the tty structures is a tricky business.. We have | |
2079 | * to be very careful that the structures are all released at the | |
2080 | * same time, as interrupts might otherwise get the wrong pointers. | |
2081 | * | |
2082 | * WSH 09/09/97: rewritten to avoid some nasty race conditions that could | |
2083 | * lead to double frees or releasing memory still in use. | |
2084 | */ | |
2085 | static void release_dev(struct file * filp) | |
2086 | { | |
2087 | struct tty_struct *tty, *o_tty; | |
2088 | int pty_master, tty_closing, o_tty_closing, do_sleep; | |
14a6283e | 2089 | int devpts; |
1da177e4 LT |
2090 | int idx; |
2091 | char buf[64]; | |
2092 | unsigned long flags; | |
2093 | ||
2094 | tty = (struct tty_struct *)filp->private_data; | |
2095 | if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev")) | |
2096 | return; | |
2097 | ||
2098 | check_tty_count(tty, "release_dev"); | |
2099 | ||
2100 | tty_fasync(-1, filp, 0); | |
2101 | ||
2102 | idx = tty->index; | |
2103 | pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
2104 | tty->driver->subtype == PTY_TYPE_MASTER); | |
2105 | devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0; | |
1da177e4 LT |
2106 | o_tty = tty->link; |
2107 | ||
2108 | #ifdef TTY_PARANOIA_CHECK | |
2109 | if (idx < 0 || idx >= tty->driver->num) { | |
2110 | printk(KERN_DEBUG "release_dev: bad idx when trying to " | |
2111 | "free (%s)\n", tty->name); | |
2112 | return; | |
2113 | } | |
2114 | if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { | |
2115 | if (tty != tty->driver->ttys[idx]) { | |
2116 | printk(KERN_DEBUG "release_dev: driver.table[%d] not tty " | |
2117 | "for (%s)\n", idx, tty->name); | |
2118 | return; | |
2119 | } | |
2120 | if (tty->termios != tty->driver->termios[idx]) { | |
2121 | printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios " | |
2122 | "for (%s)\n", | |
2123 | idx, tty->name); | |
2124 | return; | |
2125 | } | |
2126 | if (tty->termios_locked != tty->driver->termios_locked[idx]) { | |
2127 | printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not " | |
2128 | "termios_locked for (%s)\n", | |
2129 | idx, tty->name); | |
2130 | return; | |
2131 | } | |
2132 | } | |
2133 | #endif | |
2134 | ||
2135 | #ifdef TTY_DEBUG_HANGUP | |
2136 | printk(KERN_DEBUG "release_dev of %s (tty count=%d)...", | |
2137 | tty_name(tty, buf), tty->count); | |
2138 | #endif | |
2139 | ||
2140 | #ifdef TTY_PARANOIA_CHECK | |
2141 | if (tty->driver->other && | |
2142 | !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) { | |
2143 | if (o_tty != tty->driver->other->ttys[idx]) { | |
2144 | printk(KERN_DEBUG "release_dev: other->table[%d] " | |
2145 | "not o_tty for (%s)\n", | |
2146 | idx, tty->name); | |
2147 | return; | |
2148 | } | |
2149 | if (o_tty->termios != tty->driver->other->termios[idx]) { | |
2150 | printk(KERN_DEBUG "release_dev: other->termios[%d] " | |
2151 | "not o_termios for (%s)\n", | |
2152 | idx, tty->name); | |
2153 | return; | |
2154 | } | |
2155 | if (o_tty->termios_locked != | |
2156 | tty->driver->other->termios_locked[idx]) { | |
2157 | printk(KERN_DEBUG "release_dev: other->termios_locked[" | |
2158 | "%d] not o_termios_locked for (%s)\n", | |
2159 | idx, tty->name); | |
2160 | return; | |
2161 | } | |
2162 | if (o_tty->link != tty) { | |
2163 | printk(KERN_DEBUG "release_dev: bad pty pointers\n"); | |
2164 | return; | |
2165 | } | |
2166 | } | |
2167 | #endif | |
2168 | if (tty->driver->close) | |
2169 | tty->driver->close(tty, filp); | |
2170 | ||
2171 | /* | |
2172 | * Sanity check: if tty->count is going to zero, there shouldn't be | |
2173 | * any waiters on tty->read_wait or tty->write_wait. We test the | |
2174 | * wait queues and kick everyone out _before_ actually starting to | |
2175 | * close. This ensures that we won't block while releasing the tty | |
2176 | * structure. | |
2177 | * | |
2178 | * The test for the o_tty closing is necessary, since the master and | |
2179 | * slave sides may close in any order. If the slave side closes out | |
2180 | * first, its count will be one, since the master side holds an open. | |
2181 | * Thus this test wouldn't be triggered at the time the slave closes, | |
2182 | * so we do it now. | |
2183 | * | |
2184 | * Note that it's possible for the tty to be opened again while we're | |
2185 | * flushing out waiters. By recalculating the closing flags before | |
2186 | * each iteration we avoid any problems. | |
2187 | */ | |
2188 | while (1) { | |
2189 | /* Guard against races with tty->count changes elsewhere and | |
2190 | opens on /dev/tty */ | |
2191 | ||
70522e12 | 2192 | mutex_lock(&tty_mutex); |
1da177e4 LT |
2193 | tty_closing = tty->count <= 1; |
2194 | o_tty_closing = o_tty && | |
2195 | (o_tty->count <= (pty_master ? 1 : 0)); | |
1da177e4 LT |
2196 | do_sleep = 0; |
2197 | ||
2198 | if (tty_closing) { | |
2199 | if (waitqueue_active(&tty->read_wait)) { | |
2200 | wake_up(&tty->read_wait); | |
2201 | do_sleep++; | |
2202 | } | |
2203 | if (waitqueue_active(&tty->write_wait)) { | |
2204 | wake_up(&tty->write_wait); | |
2205 | do_sleep++; | |
2206 | } | |
2207 | } | |
2208 | if (o_tty_closing) { | |
2209 | if (waitqueue_active(&o_tty->read_wait)) { | |
2210 | wake_up(&o_tty->read_wait); | |
2211 | do_sleep++; | |
2212 | } | |
2213 | if (waitqueue_active(&o_tty->write_wait)) { | |
2214 | wake_up(&o_tty->write_wait); | |
2215 | do_sleep++; | |
2216 | } | |
2217 | } | |
2218 | if (!do_sleep) | |
2219 | break; | |
2220 | ||
2221 | printk(KERN_WARNING "release_dev: %s: read/write wait queue " | |
2222 | "active!\n", tty_name(tty, buf)); | |
70522e12 | 2223 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2224 | schedule(); |
2225 | } | |
2226 | ||
2227 | /* | |
2228 | * The closing flags are now consistent with the open counts on | |
2229 | * both sides, and we've completed the last operation that could | |
2230 | * block, so it's safe to proceed with closing. | |
2231 | */ | |
1da177e4 LT |
2232 | if (pty_master) { |
2233 | if (--o_tty->count < 0) { | |
2234 | printk(KERN_WARNING "release_dev: bad pty slave count " | |
2235 | "(%d) for %s\n", | |
2236 | o_tty->count, tty_name(o_tty, buf)); | |
2237 | o_tty->count = 0; | |
2238 | } | |
2239 | } | |
2240 | if (--tty->count < 0) { | |
2241 | printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n", | |
2242 | tty->count, tty_name(tty, buf)); | |
2243 | tty->count = 0; | |
2244 | } | |
1da177e4 LT |
2245 | |
2246 | /* | |
2247 | * We've decremented tty->count, so we need to remove this file | |
2248 | * descriptor off the tty->tty_files list; this serves two | |
2249 | * purposes: | |
2250 | * - check_tty_count sees the correct number of file descriptors | |
2251 | * associated with this tty. | |
2252 | * - do_tty_hangup no longer sees this file descriptor as | |
2253 | * something that needs to be handled for hangups. | |
2254 | */ | |
2255 | file_kill(filp); | |
2256 | filp->private_data = NULL; | |
2257 | ||
2258 | /* | |
2259 | * Perform some housekeeping before deciding whether to return. | |
2260 | * | |
2261 | * Set the TTY_CLOSING flag if this was the last open. In the | |
2262 | * case of a pty we may have to wait around for the other side | |
2263 | * to close, and TTY_CLOSING makes sure we can't be reopened. | |
2264 | */ | |
2265 | if(tty_closing) | |
2266 | set_bit(TTY_CLOSING, &tty->flags); | |
2267 | if(o_tty_closing) | |
2268 | set_bit(TTY_CLOSING, &o_tty->flags); | |
2269 | ||
2270 | /* | |
2271 | * If _either_ side is closing, make sure there aren't any | |
2272 | * processes that still think tty or o_tty is their controlling | |
2273 | * tty. | |
2274 | */ | |
2275 | if (tty_closing || o_tty_closing) { | |
2276 | struct task_struct *p; | |
2277 | ||
2278 | read_lock(&tasklist_lock); | |
2279 | do_each_task_pid(tty->session, PIDTYPE_SID, p) { | |
2280 | p->signal->tty = NULL; | |
2281 | } while_each_task_pid(tty->session, PIDTYPE_SID, p); | |
2282 | if (o_tty) | |
2283 | do_each_task_pid(o_tty->session, PIDTYPE_SID, p) { | |
2284 | p->signal->tty = NULL; | |
2285 | } while_each_task_pid(o_tty->session, PIDTYPE_SID, p); | |
2286 | read_unlock(&tasklist_lock); | |
2287 | } | |
2288 | ||
70522e12 | 2289 | mutex_unlock(&tty_mutex); |
da965822 | 2290 | |
1da177e4 LT |
2291 | /* check whether both sides are closing ... */ |
2292 | if (!tty_closing || (o_tty && !o_tty_closing)) | |
2293 | return; | |
2294 | ||
2295 | #ifdef TTY_DEBUG_HANGUP | |
2296 | printk(KERN_DEBUG "freeing tty structure..."); | |
2297 | #endif | |
2298 | /* | |
2299 | * Prevent flush_to_ldisc() from rescheduling the work for later. Then | |
2300 | * kill any delayed work. As this is the final close it does not | |
2301 | * race with the set_ldisc code path. | |
2302 | */ | |
2303 | clear_bit(TTY_LDISC, &tty->flags); | |
33f0f88f | 2304 | cancel_delayed_work(&tty->buf.work); |
1da177e4 LT |
2305 | |
2306 | /* | |
33f0f88f | 2307 | * Wait for ->hangup_work and ->buf.work handlers to terminate |
1da177e4 LT |
2308 | */ |
2309 | ||
2310 | flush_scheduled_work(); | |
2311 | ||
2312 | /* | |
2313 | * Wait for any short term users (we know they are just driver | |
2314 | * side waiters as the file is closing so user count on the file | |
2315 | * side is zero. | |
2316 | */ | |
2317 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
2318 | while(tty->ldisc.refcount) | |
2319 | { | |
2320 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
2321 | wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0); | |
2322 | spin_lock_irqsave(&tty_ldisc_lock, flags); | |
2323 | } | |
2324 | spin_unlock_irqrestore(&tty_ldisc_lock, flags); | |
2325 | /* | |
2326 | * Shutdown the current line discipline, and reset it to N_TTY. | |
2327 | * N.B. why reset ldisc when we're releasing the memory?? | |
2328 | * | |
2329 | * FIXME: this MUST get fixed for the new reflocking | |
2330 | */ | |
2331 | if (tty->ldisc.close) | |
2332 | (tty->ldisc.close)(tty); | |
2333 | tty_ldisc_put(tty->ldisc.num); | |
2334 | ||
2335 | /* | |
2336 | * Switch the line discipline back | |
2337 | */ | |
2338 | tty_ldisc_assign(tty, tty_ldisc_get(N_TTY)); | |
2339 | tty_set_termios_ldisc(tty,N_TTY); | |
2340 | if (o_tty) { | |
2341 | /* FIXME: could o_tty be in setldisc here ? */ | |
2342 | clear_bit(TTY_LDISC, &o_tty->flags); | |
2343 | if (o_tty->ldisc.close) | |
2344 | (o_tty->ldisc.close)(o_tty); | |
2345 | tty_ldisc_put(o_tty->ldisc.num); | |
2346 | tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY)); | |
2347 | tty_set_termios_ldisc(o_tty,N_TTY); | |
2348 | } | |
2349 | /* | |
2350 | * The release_mem function takes care of the details of clearing | |
2351 | * the slots and preserving the termios structure. | |
2352 | */ | |
2353 | release_mem(tty, idx); | |
2354 | ||
2355 | #ifdef CONFIG_UNIX98_PTYS | |
2356 | /* Make this pty number available for reallocation */ | |
2357 | if (devpts) { | |
2358 | down(&allocated_ptys_lock); | |
2359 | idr_remove(&allocated_ptys, idx); | |
2360 | up(&allocated_ptys_lock); | |
2361 | } | |
2362 | #endif | |
2363 | ||
2364 | } | |
2365 | ||
af9b897e AC |
2366 | /** |
2367 | * tty_open - open a tty device | |
2368 | * @inode: inode of device file | |
2369 | * @filp: file pointer to tty | |
1da177e4 | 2370 | * |
af9b897e AC |
2371 | * tty_open and tty_release keep up the tty count that contains the |
2372 | * number of opens done on a tty. We cannot use the inode-count, as | |
2373 | * different inodes might point to the same tty. | |
1da177e4 | 2374 | * |
af9b897e AC |
2375 | * Open-counting is needed for pty masters, as well as for keeping |
2376 | * track of serial lines: DTR is dropped when the last close happens. | |
2377 | * (This is not done solely through tty->count, now. - Ted 1/27/92) | |
2378 | * | |
2379 | * The termios state of a pty is reset on first open so that | |
2380 | * settings don't persist across reuse. | |
2381 | * | |
2382 | * Locking: tty_mutex protects current->signal->tty, get_tty_driver and | |
2383 | * init_dev work. tty->count should protect the rest. | |
2384 | * task_lock is held to update task details for sessions | |
1da177e4 | 2385 | */ |
af9b897e | 2386 | |
1da177e4 LT |
2387 | static int tty_open(struct inode * inode, struct file * filp) |
2388 | { | |
2389 | struct tty_struct *tty; | |
2390 | int noctty, retval; | |
2391 | struct tty_driver *driver; | |
2392 | int index; | |
2393 | dev_t device = inode->i_rdev; | |
2394 | unsigned short saved_flags = filp->f_flags; | |
2395 | ||
2396 | nonseekable_open(inode, filp); | |
2397 | ||
2398 | retry_open: | |
2399 | noctty = filp->f_flags & O_NOCTTY; | |
2400 | index = -1; | |
2401 | retval = 0; | |
2402 | ||
70522e12 | 2403 | mutex_lock(&tty_mutex); |
1da177e4 LT |
2404 | |
2405 | if (device == MKDEV(TTYAUX_MAJOR,0)) { | |
2406 | if (!current->signal->tty) { | |
70522e12 | 2407 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2408 | return -ENXIO; |
2409 | } | |
2410 | driver = current->signal->tty->driver; | |
2411 | index = current->signal->tty->index; | |
2412 | filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */ | |
2413 | /* noctty = 1; */ | |
2414 | goto got_driver; | |
2415 | } | |
2416 | #ifdef CONFIG_VT | |
2417 | if (device == MKDEV(TTY_MAJOR,0)) { | |
2418 | extern struct tty_driver *console_driver; | |
2419 | driver = console_driver; | |
2420 | index = fg_console; | |
2421 | noctty = 1; | |
2422 | goto got_driver; | |
2423 | } | |
2424 | #endif | |
2425 | if (device == MKDEV(TTYAUX_MAJOR,1)) { | |
2426 | driver = console_device(&index); | |
2427 | if (driver) { | |
2428 | /* Don't let /dev/console block */ | |
2429 | filp->f_flags |= O_NONBLOCK; | |
2430 | noctty = 1; | |
2431 | goto got_driver; | |
2432 | } | |
70522e12 | 2433 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2434 | return -ENODEV; |
2435 | } | |
2436 | ||
2437 | driver = get_tty_driver(device, &index); | |
2438 | if (!driver) { | |
70522e12 | 2439 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2440 | return -ENODEV; |
2441 | } | |
2442 | got_driver: | |
2443 | retval = init_dev(driver, index, &tty); | |
70522e12 | 2444 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2445 | if (retval) |
2446 | return retval; | |
2447 | ||
2448 | filp->private_data = tty; | |
2449 | file_move(filp, &tty->tty_files); | |
2450 | check_tty_count(tty, "tty_open"); | |
2451 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
2452 | tty->driver->subtype == PTY_TYPE_MASTER) | |
2453 | noctty = 1; | |
2454 | #ifdef TTY_DEBUG_HANGUP | |
2455 | printk(KERN_DEBUG "opening %s...", tty->name); | |
2456 | #endif | |
2457 | if (!retval) { | |
2458 | if (tty->driver->open) | |
2459 | retval = tty->driver->open(tty, filp); | |
2460 | else | |
2461 | retval = -ENODEV; | |
2462 | } | |
2463 | filp->f_flags = saved_flags; | |
2464 | ||
2465 | if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN)) | |
2466 | retval = -EBUSY; | |
2467 | ||
2468 | if (retval) { | |
2469 | #ifdef TTY_DEBUG_HANGUP | |
2470 | printk(KERN_DEBUG "error %d in opening %s...", retval, | |
2471 | tty->name); | |
2472 | #endif | |
2473 | release_dev(filp); | |
2474 | if (retval != -ERESTARTSYS) | |
2475 | return retval; | |
2476 | if (signal_pending(current)) | |
2477 | return retval; | |
2478 | schedule(); | |
2479 | /* | |
2480 | * Need to reset f_op in case a hangup happened. | |
2481 | */ | |
2482 | if (filp->f_op == &hung_up_tty_fops) | |
2483 | filp->f_op = &tty_fops; | |
2484 | goto retry_open; | |
2485 | } | |
2486 | if (!noctty && | |
2487 | current->signal->leader && | |
2488 | !current->signal->tty && | |
2489 | tty->session == 0) { | |
2490 | task_lock(current); | |
2491 | current->signal->tty = tty; | |
2492 | task_unlock(current); | |
2493 | current->signal->tty_old_pgrp = 0; | |
2494 | tty->session = current->signal->session; | |
2495 | tty->pgrp = process_group(current); | |
2496 | } | |
2497 | return 0; | |
2498 | } | |
2499 | ||
2500 | #ifdef CONFIG_UNIX98_PTYS | |
af9b897e AC |
2501 | /** |
2502 | * ptmx_open - open a unix 98 pty master | |
2503 | * @inode: inode of device file | |
2504 | * @filp: file pointer to tty | |
2505 | * | |
2506 | * Allocate a unix98 pty master device from the ptmx driver. | |
2507 | * | |
2508 | * Locking: tty_mutex protects theinit_dev work. tty->count should | |
2509 | protect the rest. | |
2510 | * allocated_ptys_lock handles the list of free pty numbers | |
2511 | */ | |
2512 | ||
1da177e4 LT |
2513 | static int ptmx_open(struct inode * inode, struct file * filp) |
2514 | { | |
2515 | struct tty_struct *tty; | |
2516 | int retval; | |
2517 | int index; | |
2518 | int idr_ret; | |
2519 | ||
2520 | nonseekable_open(inode, filp); | |
2521 | ||
2522 | /* find a device that is not in use. */ | |
2523 | down(&allocated_ptys_lock); | |
2524 | if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) { | |
2525 | up(&allocated_ptys_lock); | |
2526 | return -ENOMEM; | |
2527 | } | |
2528 | idr_ret = idr_get_new(&allocated_ptys, NULL, &index); | |
2529 | if (idr_ret < 0) { | |
2530 | up(&allocated_ptys_lock); | |
2531 | if (idr_ret == -EAGAIN) | |
2532 | return -ENOMEM; | |
2533 | return -EIO; | |
2534 | } | |
2535 | if (index >= pty_limit) { | |
2536 | idr_remove(&allocated_ptys, index); | |
2537 | up(&allocated_ptys_lock); | |
2538 | return -EIO; | |
2539 | } | |
2540 | up(&allocated_ptys_lock); | |
2541 | ||
70522e12 | 2542 | mutex_lock(&tty_mutex); |
1da177e4 | 2543 | retval = init_dev(ptm_driver, index, &tty); |
70522e12 | 2544 | mutex_unlock(&tty_mutex); |
1da177e4 LT |
2545 | |
2546 | if (retval) | |
2547 | goto out; | |
2548 | ||
2549 | set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */ | |
2550 | filp->private_data = tty; | |
2551 | file_move(filp, &tty->tty_files); | |
2552 | ||
2553 | retval = -ENOMEM; | |
2554 | if (devpts_pty_new(tty->link)) | |
2555 | goto out1; | |
2556 | ||
2557 | check_tty_count(tty, "tty_open"); | |
2558 | retval = ptm_driver->open(tty, filp); | |
2559 | if (!retval) | |
2560 | return 0; | |
2561 | out1: | |
2562 | release_dev(filp); | |
9453a5ad | 2563 | return retval; |
1da177e4 LT |
2564 | out: |
2565 | down(&allocated_ptys_lock); | |
2566 | idr_remove(&allocated_ptys, index); | |
2567 | up(&allocated_ptys_lock); | |
2568 | return retval; | |
2569 | } | |
2570 | #endif | |
2571 | ||
af9b897e AC |
2572 | /** |
2573 | * tty_release - vfs callback for close | |
2574 | * @inode: inode of tty | |
2575 | * @filp: file pointer for handle to tty | |
2576 | * | |
2577 | * Called the last time each file handle is closed that references | |
2578 | * this tty. There may however be several such references. | |
2579 | * | |
2580 | * Locking: | |
2581 | * Takes bkl. See release_dev | |
2582 | */ | |
2583 | ||
1da177e4 LT |
2584 | static int tty_release(struct inode * inode, struct file * filp) |
2585 | { | |
2586 | lock_kernel(); | |
2587 | release_dev(filp); | |
2588 | unlock_kernel(); | |
2589 | return 0; | |
2590 | } | |
2591 | ||
af9b897e AC |
2592 | /** |
2593 | * tty_poll - check tty status | |
2594 | * @filp: file being polled | |
2595 | * @wait: poll wait structures to update | |
2596 | * | |
2597 | * Call the line discipline polling method to obtain the poll | |
2598 | * status of the device. | |
2599 | * | |
2600 | * Locking: locks called line discipline but ldisc poll method | |
2601 | * may be re-entered freely by other callers. | |
2602 | */ | |
2603 | ||
1da177e4 LT |
2604 | static unsigned int tty_poll(struct file * filp, poll_table * wait) |
2605 | { | |
2606 | struct tty_struct * tty; | |
2607 | struct tty_ldisc *ld; | |
2608 | int ret = 0; | |
2609 | ||
2610 | tty = (struct tty_struct *)filp->private_data; | |
2611 | if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll")) | |
2612 | return 0; | |
2613 | ||
2614 | ld = tty_ldisc_ref_wait(tty); | |
2615 | if (ld->poll) | |
2616 | ret = (ld->poll)(tty, filp, wait); | |
2617 | tty_ldisc_deref(ld); | |
2618 | return ret; | |
2619 | } | |
2620 | ||
2621 | static int tty_fasync(int fd, struct file * filp, int on) | |
2622 | { | |
2623 | struct tty_struct * tty; | |
2624 | int retval; | |
2625 | ||
2626 | tty = (struct tty_struct *)filp->private_data; | |
2627 | if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync")) | |
2628 | return 0; | |
2629 | ||
2630 | retval = fasync_helper(fd, filp, on, &tty->fasync); | |
2631 | if (retval <= 0) | |
2632 | return retval; | |
2633 | ||
2634 | if (on) { | |
2635 | if (!waitqueue_active(&tty->read_wait)) | |
2636 | tty->minimum_to_wake = 1; | |
2637 | retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0); | |
2638 | if (retval) | |
2639 | return retval; | |
2640 | } else { | |
2641 | if (!tty->fasync && !waitqueue_active(&tty->read_wait)) | |
2642 | tty->minimum_to_wake = N_TTY_BUF_SIZE; | |
2643 | } | |
2644 | return 0; | |
2645 | } | |
2646 | ||
af9b897e AC |
2647 | /** |
2648 | * tiocsti - fake input character | |
2649 | * @tty: tty to fake input into | |
2650 | * @p: pointer to character | |
2651 | * | |
2652 | * Fake input to a tty device. Does the neccessary locking and | |
2653 | * input management. | |
2654 | * | |
2655 | * FIXME: does not honour flow control ?? | |
2656 | * | |
2657 | * Locking: | |
2658 | * Called functions take tty_ldisc_lock | |
2659 | * current->signal->tty check is safe without locks | |
2660 | */ | |
2661 | ||
1da177e4 LT |
2662 | static int tiocsti(struct tty_struct *tty, char __user *p) |
2663 | { | |
2664 | char ch, mbz = 0; | |
2665 | struct tty_ldisc *ld; | |
2666 | ||
2667 | if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN)) | |
2668 | return -EPERM; | |
2669 | if (get_user(ch, p)) | |
2670 | return -EFAULT; | |
2671 | ld = tty_ldisc_ref_wait(tty); | |
2672 | ld->receive_buf(tty, &ch, &mbz, 1); | |
2673 | tty_ldisc_deref(ld); | |
2674 | return 0; | |
2675 | } | |
2676 | ||
af9b897e AC |
2677 | /** |
2678 | * tiocgwinsz - implement window query ioctl | |
2679 | * @tty; tty | |
2680 | * @arg: user buffer for result | |
2681 | * | |
2682 | * Copies the kernel idea of the window size into the user buffer. No | |
2683 | * locking is done. | |
2684 | * | |
2685 | * FIXME: Returning random values racing a window size set is wrong | |
2686 | * should lock here against that | |
2687 | */ | |
2688 | ||
1da177e4 LT |
2689 | static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg) |
2690 | { | |
2691 | if (copy_to_user(arg, &tty->winsize, sizeof(*arg))) | |
2692 | return -EFAULT; | |
2693 | return 0; | |
2694 | } | |
2695 | ||
af9b897e AC |
2696 | /** |
2697 | * tiocswinsz - implement window size set ioctl | |
2698 | * @tty; tty | |
2699 | * @arg: user buffer for result | |
2700 | * | |
2701 | * Copies the user idea of the window size to the kernel. Traditionally | |
2702 | * this is just advisory information but for the Linux console it | |
2703 | * actually has driver level meaning and triggers a VC resize. | |
2704 | * | |
2705 | * Locking: | |
2706 | * The console_sem is used to ensure we do not try and resize | |
2707 | * the console twice at once. | |
2708 | * FIXME: Two racing size sets may leave the console and kernel | |
2709 | * parameters disagreeing. Is this exploitable ? | |
2710 | * FIXME: Random values racing a window size get is wrong | |
2711 | * should lock here against that | |
2712 | */ | |
2713 | ||
1da177e4 LT |
2714 | static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty, |
2715 | struct winsize __user * arg) | |
2716 | { | |
2717 | struct winsize tmp_ws; | |
2718 | ||
2719 | if (copy_from_user(&tmp_ws, arg, sizeof(*arg))) | |
2720 | return -EFAULT; | |
2721 | if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg))) | |
2722 | return 0; | |
2723 | #ifdef CONFIG_VT | |
2724 | if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) { | |
2725 | int rc; | |
2726 | ||
2727 | acquire_console_sem(); | |
2728 | rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row); | |
2729 | release_console_sem(); | |
2730 | if (rc) | |
2731 | return -ENXIO; | |
2732 | } | |
2733 | #endif | |
2734 | if (tty->pgrp > 0) | |
2735 | kill_pg(tty->pgrp, SIGWINCH, 1); | |
2736 | if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0)) | |
2737 | kill_pg(real_tty->pgrp, SIGWINCH, 1); | |
2738 | tty->winsize = tmp_ws; | |
2739 | real_tty->winsize = tmp_ws; | |
2740 | return 0; | |
2741 | } | |
2742 | ||
af9b897e AC |
2743 | /** |
2744 | * tioccons - allow admin to move logical console | |
2745 | * @file: the file to become console | |
2746 | * | |
2747 | * Allow the adminstrator to move the redirected console device | |
2748 | * | |
2749 | * Locking: uses redirect_lock to guard the redirect information | |
2750 | */ | |
2751 | ||
1da177e4 LT |
2752 | static int tioccons(struct file *file) |
2753 | { | |
2754 | if (!capable(CAP_SYS_ADMIN)) | |
2755 | return -EPERM; | |
2756 | if (file->f_op->write == redirected_tty_write) { | |
2757 | struct file *f; | |
2758 | spin_lock(&redirect_lock); | |
2759 | f = redirect; | |
2760 | redirect = NULL; | |
2761 | spin_unlock(&redirect_lock); | |
2762 | if (f) | |
2763 | fput(f); | |
2764 | return 0; | |
2765 | } | |
2766 | spin_lock(&redirect_lock); | |
2767 | if (redirect) { | |
2768 | spin_unlock(&redirect_lock); | |
2769 | return -EBUSY; | |
2770 | } | |
2771 | get_file(file); | |
2772 | redirect = file; | |
2773 | spin_unlock(&redirect_lock); | |
2774 | return 0; | |
2775 | } | |
2776 | ||
af9b897e AC |
2777 | /** |
2778 | * fionbio - non blocking ioctl | |
2779 | * @file: file to set blocking value | |
2780 | * @p: user parameter | |
2781 | * | |
2782 | * Historical tty interfaces had a blocking control ioctl before | |
2783 | * the generic functionality existed. This piece of history is preserved | |
2784 | * in the expected tty API of posix OS's. | |
2785 | * | |
2786 | * Locking: none, the open fle handle ensures it won't go away. | |
2787 | */ | |
1da177e4 LT |
2788 | |
2789 | static int fionbio(struct file *file, int __user *p) | |
2790 | { | |
2791 | int nonblock; | |
2792 | ||
2793 | if (get_user(nonblock, p)) | |
2794 | return -EFAULT; | |
2795 | ||
2796 | if (nonblock) | |
2797 | file->f_flags |= O_NONBLOCK; | |
2798 | else | |
2799 | file->f_flags &= ~O_NONBLOCK; | |
2800 | return 0; | |
2801 | } | |
2802 | ||
af9b897e AC |
2803 | /** |
2804 | * tiocsctty - set controlling tty | |
2805 | * @tty: tty structure | |
2806 | * @arg: user argument | |
2807 | * | |
2808 | * This ioctl is used to manage job control. It permits a session | |
2809 | * leader to set this tty as the controlling tty for the session. | |
2810 | * | |
2811 | * Locking: | |
2812 | * Takes tasklist lock internally to walk sessions | |
2813 | * Takes task_lock() when updating signal->tty | |
2814 | * | |
2815 | * FIXME: tty_mutex is needed to protect signal->tty references. | |
2816 | * FIXME: why task_lock on the signal->tty reference ?? | |
2817 | * | |
2818 | */ | |
2819 | ||
1da177e4 LT |
2820 | static int tiocsctty(struct tty_struct *tty, int arg) |
2821 | { | |
36c8b586 | 2822 | struct task_struct *p; |
1da177e4 LT |
2823 | |
2824 | if (current->signal->leader && | |
2825 | (current->signal->session == tty->session)) | |
2826 | return 0; | |
2827 | /* | |
2828 | * The process must be a session leader and | |
2829 | * not have a controlling tty already. | |
2830 | */ | |
2831 | if (!current->signal->leader || current->signal->tty) | |
2832 | return -EPERM; | |
2833 | if (tty->session > 0) { | |
2834 | /* | |
2835 | * This tty is already the controlling | |
2836 | * tty for another session group! | |
2837 | */ | |
2838 | if ((arg == 1) && capable(CAP_SYS_ADMIN)) { | |
2839 | /* | |
2840 | * Steal it away | |
2841 | */ | |
2842 | ||
2843 | read_lock(&tasklist_lock); | |
2844 | do_each_task_pid(tty->session, PIDTYPE_SID, p) { | |
2845 | p->signal->tty = NULL; | |
2846 | } while_each_task_pid(tty->session, PIDTYPE_SID, p); | |
2847 | read_unlock(&tasklist_lock); | |
2848 | } else | |
2849 | return -EPERM; | |
2850 | } | |
2851 | task_lock(current); | |
2852 | current->signal->tty = tty; | |
2853 | task_unlock(current); | |
2854 | current->signal->tty_old_pgrp = 0; | |
2855 | tty->session = current->signal->session; | |
2856 | tty->pgrp = process_group(current); | |
2857 | return 0; | |
2858 | } | |
2859 | ||
af9b897e AC |
2860 | /** |
2861 | * tiocgpgrp - get process group | |
2862 | * @tty: tty passed by user | |
2863 | * @real_tty: tty side of the tty pased by the user if a pty else the tty | |
2864 | * @p: returned pid | |
2865 | * | |
2866 | * Obtain the process group of the tty. If there is no process group | |
2867 | * return an error. | |
2868 | * | |
2869 | * Locking: none. Reference to ->signal->tty is safe. | |
2870 | */ | |
2871 | ||
1da177e4 LT |
2872 | static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2873 | { | |
2874 | /* | |
2875 | * (tty == real_tty) is a cheap way of | |
2876 | * testing if the tty is NOT a master pty. | |
2877 | */ | |
2878 | if (tty == real_tty && current->signal->tty != real_tty) | |
2879 | return -ENOTTY; | |
2880 | return put_user(real_tty->pgrp, p); | |
2881 | } | |
2882 | ||
af9b897e AC |
2883 | /** |
2884 | * tiocspgrp - attempt to set process group | |
2885 | * @tty: tty passed by user | |
2886 | * @real_tty: tty side device matching tty passed by user | |
2887 | * @p: pid pointer | |
2888 | * | |
2889 | * Set the process group of the tty to the session passed. Only | |
2890 | * permitted where the tty session is our session. | |
2891 | * | |
2892 | * Locking: None | |
2893 | * | |
2894 | * FIXME: current->signal->tty referencing is unsafe. | |
2895 | */ | |
2896 | ||
1da177e4 LT |
2897 | static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2898 | { | |
2899 | pid_t pgrp; | |
2900 | int retval = tty_check_change(real_tty); | |
2901 | ||
2902 | if (retval == -EIO) | |
2903 | return -ENOTTY; | |
2904 | if (retval) | |
2905 | return retval; | |
2906 | if (!current->signal->tty || | |
2907 | (current->signal->tty != real_tty) || | |
2908 | (real_tty->session != current->signal->session)) | |
2909 | return -ENOTTY; | |
2910 | if (get_user(pgrp, p)) | |
2911 | return -EFAULT; | |
2912 | if (pgrp < 0) | |
2913 | return -EINVAL; | |
2914 | if (session_of_pgrp(pgrp) != current->signal->session) | |
2915 | return -EPERM; | |
2916 | real_tty->pgrp = pgrp; | |
2917 | return 0; | |
2918 | } | |
2919 | ||
af9b897e AC |
2920 | /** |
2921 | * tiocgsid - get session id | |
2922 | * @tty: tty passed by user | |
2923 | * @real_tty: tty side of the tty pased by the user if a pty else the tty | |
2924 | * @p: pointer to returned session id | |
2925 | * | |
2926 | * Obtain the session id of the tty. If there is no session | |
2927 | * return an error. | |
2928 | * | |
2929 | * Locking: none. Reference to ->signal->tty is safe. | |
2930 | */ | |
2931 | ||
1da177e4 LT |
2932 | static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p) |
2933 | { | |
2934 | /* | |
2935 | * (tty == real_tty) is a cheap way of | |
2936 | * testing if the tty is NOT a master pty. | |
2937 | */ | |
2938 | if (tty == real_tty && current->signal->tty != real_tty) | |
2939 | return -ENOTTY; | |
2940 | if (real_tty->session <= 0) | |
2941 | return -ENOTTY; | |
2942 | return put_user(real_tty->session, p); | |
2943 | } | |
2944 | ||
af9b897e AC |
2945 | /** |
2946 | * tiocsetd - set line discipline | |
2947 | * @tty: tty device | |
2948 | * @p: pointer to user data | |
2949 | * | |
2950 | * Set the line discipline according to user request. | |
2951 | * | |
2952 | * Locking: see tty_set_ldisc, this function is just a helper | |
2953 | */ | |
2954 | ||
1da177e4 LT |
2955 | static int tiocsetd(struct tty_struct *tty, int __user *p) |
2956 | { | |
2957 | int ldisc; | |
2958 | ||
2959 | if (get_user(ldisc, p)) | |
2960 | return -EFAULT; | |
2961 | return tty_set_ldisc(tty, ldisc); | |
2962 | } | |
2963 | ||
af9b897e AC |
2964 | /** |
2965 | * send_break - performed time break | |
2966 | * @tty: device to break on | |
2967 | * @duration: timeout in mS | |
2968 | * | |
2969 | * Perform a timed break on hardware that lacks its own driver level | |
2970 | * timed break functionality. | |
2971 | * | |
2972 | * Locking: | |
2973 | * None | |
2974 | * | |
2975 | * FIXME: | |
2976 | * What if two overlap | |
2977 | */ | |
2978 | ||
b20f3ae5 | 2979 | static int send_break(struct tty_struct *tty, unsigned int duration) |
1da177e4 LT |
2980 | { |
2981 | tty->driver->break_ctl(tty, -1); | |
2982 | if (!signal_pending(current)) { | |
b20f3ae5 | 2983 | msleep_interruptible(duration); |
1da177e4 LT |
2984 | } |
2985 | tty->driver->break_ctl(tty, 0); | |
2986 | if (signal_pending(current)) | |
2987 | return -EINTR; | |
2988 | return 0; | |
2989 | } | |
2990 | ||
af9b897e AC |
2991 | /** |
2992 | * tiocmget - get modem status | |
2993 | * @tty: tty device | |
2994 | * @file: user file pointer | |
2995 | * @p: pointer to result | |
2996 | * | |
2997 | * Obtain the modem status bits from the tty driver if the feature | |
2998 | * is supported. Return -EINVAL if it is not available. | |
2999 | * | |
3000 | * Locking: none (up to the driver) | |
3001 | */ | |
3002 | ||
3003 | static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p) | |
1da177e4 LT |
3004 | { |
3005 | int retval = -EINVAL; | |
3006 | ||
3007 | if (tty->driver->tiocmget) { | |
3008 | retval = tty->driver->tiocmget(tty, file); | |
3009 | ||
3010 | if (retval >= 0) | |
3011 | retval = put_user(retval, p); | |
3012 | } | |
3013 | return retval; | |
3014 | } | |
3015 | ||
af9b897e AC |
3016 | /** |
3017 | * tiocmset - set modem status | |
3018 | * @tty: tty device | |
3019 | * @file: user file pointer | |
3020 | * @cmd: command - clear bits, set bits or set all | |
3021 | * @p: pointer to desired bits | |
3022 | * | |
3023 | * Set the modem status bits from the tty driver if the feature | |
3024 | * is supported. Return -EINVAL if it is not available. | |
3025 | * | |
3026 | * Locking: none (up to the driver) | |
3027 | */ | |
3028 | ||
3029 | static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd, | |
1da177e4 LT |
3030 | unsigned __user *p) |
3031 | { | |
3032 | int retval = -EINVAL; | |
3033 | ||
3034 | if (tty->driver->tiocmset) { | |
3035 | unsigned int set, clear, val; | |
3036 | ||
3037 | retval = get_user(val, p); | |
3038 | if (retval) | |
3039 | return retval; | |
3040 | ||
3041 | set = clear = 0; | |
3042 | switch (cmd) { | |
3043 | case TIOCMBIS: | |
3044 | set = val; | |
3045 | break; | |
3046 | case TIOCMBIC: | |
3047 | clear = val; | |
3048 | break; | |
3049 | case TIOCMSET: | |
3050 | set = val; | |
3051 | clear = ~val; | |
3052 | break; | |
3053 | } | |
3054 | ||
3055 | set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | |
3056 | clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP; | |
3057 | ||
3058 | retval = tty->driver->tiocmset(tty, file, set, clear); | |
3059 | } | |
3060 | return retval; | |
3061 | } | |
3062 | ||
3063 | /* | |
3064 | * Split this up, as gcc can choke on it otherwise.. | |
3065 | */ | |
3066 | int tty_ioctl(struct inode * inode, struct file * file, | |
3067 | unsigned int cmd, unsigned long arg) | |
3068 | { | |
3069 | struct tty_struct *tty, *real_tty; | |
3070 | void __user *p = (void __user *)arg; | |
3071 | int retval; | |
3072 | struct tty_ldisc *ld; | |
3073 | ||
3074 | tty = (struct tty_struct *)file->private_data; | |
3075 | if (tty_paranoia_check(tty, inode, "tty_ioctl")) | |
3076 | return -EINVAL; | |
3077 | ||
3078 | real_tty = tty; | |
3079 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY && | |
3080 | tty->driver->subtype == PTY_TYPE_MASTER) | |
3081 | real_tty = tty->link; | |
3082 | ||
3083 | /* | |
3084 | * Break handling by driver | |
3085 | */ | |
3086 | if (!tty->driver->break_ctl) { | |
3087 | switch(cmd) { | |
3088 | case TIOCSBRK: | |
3089 | case TIOCCBRK: | |
3090 | if (tty->driver->ioctl) | |
3091 | return tty->driver->ioctl(tty, file, cmd, arg); | |
3092 | return -EINVAL; | |
3093 | ||
3094 | /* These two ioctl's always return success; even if */ | |
3095 | /* the driver doesn't support them. */ | |
3096 | case TCSBRK: | |
3097 | case TCSBRKP: | |
3098 | if (!tty->driver->ioctl) | |
3099 | return 0; | |
3100 | retval = tty->driver->ioctl(tty, file, cmd, arg); | |
3101 | if (retval == -ENOIOCTLCMD) | |
3102 | retval = 0; | |
3103 | return retval; | |
3104 | } | |
3105 | } | |
3106 | ||
3107 | /* | |
3108 | * Factor out some common prep work | |
3109 | */ | |
3110 | switch (cmd) { | |
3111 | case TIOCSETD: | |
3112 | case TIOCSBRK: | |
3113 | case TIOCCBRK: | |
3114 | case TCSBRK: | |
3115 | case TCSBRKP: | |
3116 | retval = tty_check_change(tty); | |
3117 | if (retval) | |
3118 | return retval; | |
3119 | if (cmd != TIOCCBRK) { | |
3120 | tty_wait_until_sent(tty, 0); | |
3121 | if (signal_pending(current)) | |
3122 | return -EINTR; | |
3123 | } | |
3124 | break; | |
3125 | } | |
3126 | ||
3127 | switch (cmd) { | |
3128 | case TIOCSTI: | |
3129 | return tiocsti(tty, p); | |
3130 | case TIOCGWINSZ: | |
3131 | return tiocgwinsz(tty, p); | |
3132 | case TIOCSWINSZ: | |
3133 | return tiocswinsz(tty, real_tty, p); | |
3134 | case TIOCCONS: | |
3135 | return real_tty!=tty ? -EINVAL : tioccons(file); | |
3136 | case FIONBIO: | |
3137 | return fionbio(file, p); | |
3138 | case TIOCEXCL: | |
3139 | set_bit(TTY_EXCLUSIVE, &tty->flags); | |
3140 | return 0; | |
3141 | case TIOCNXCL: | |
3142 | clear_bit(TTY_EXCLUSIVE, &tty->flags); | |
3143 | return 0; | |
3144 | case TIOCNOTTY: | |
af9b897e | 3145 | /* FIXME: taks lock or tty_mutex ? */ |
1da177e4 LT |
3146 | if (current->signal->tty != tty) |
3147 | return -ENOTTY; | |
3148 | if (current->signal->leader) | |
3149 | disassociate_ctty(0); | |
3150 | task_lock(current); | |
3151 | current->signal->tty = NULL; | |
3152 | task_unlock(current); | |
3153 | return 0; | |
3154 | case TIOCSCTTY: | |
3155 | return tiocsctty(tty, arg); | |
3156 | case TIOCGPGRP: | |
3157 | return tiocgpgrp(tty, real_tty, p); | |
3158 | case TIOCSPGRP: | |
3159 | return tiocspgrp(tty, real_tty, p); | |
3160 | case TIOCGSID: | |
3161 | return tiocgsid(tty, real_tty, p); | |
3162 | case TIOCGETD: | |
3163 | /* FIXME: check this is ok */ | |
3164 | return put_user(tty->ldisc.num, (int __user *)p); | |
3165 | case TIOCSETD: | |
3166 | return tiocsetd(tty, p); | |
3167 | #ifdef CONFIG_VT | |
3168 | case TIOCLINUX: | |
3169 | return tioclinux(tty, arg); | |
3170 | #endif | |
3171 | /* | |
3172 | * Break handling | |
3173 | */ | |
3174 | case TIOCSBRK: /* Turn break on, unconditionally */ | |
3175 | tty->driver->break_ctl(tty, -1); | |
3176 | return 0; | |
3177 | ||
3178 | case TIOCCBRK: /* Turn break off, unconditionally */ | |
3179 | tty->driver->break_ctl(tty, 0); | |
3180 | return 0; | |
3181 | case TCSBRK: /* SVID version: non-zero arg --> no break */ | |
283fef59 PF |
3182 | /* non-zero arg means wait for all output data |
3183 | * to be sent (performed above) but don't send break. | |
3184 | * This is used by the tcdrain() termios function. | |
1da177e4 LT |
3185 | */ |
3186 | if (!arg) | |
b20f3ae5 | 3187 | return send_break(tty, 250); |
1da177e4 LT |
3188 | return 0; |
3189 | case TCSBRKP: /* support for POSIX tcsendbreak() */ | |
b20f3ae5 | 3190 | return send_break(tty, arg ? arg*100 : 250); |
1da177e4 LT |
3191 | |
3192 | case TIOCMGET: | |
3193 | return tty_tiocmget(tty, file, p); | |
3194 | ||
3195 | case TIOCMSET: | |
3196 | case TIOCMBIC: | |
3197 | case TIOCMBIS: | |
3198 | return tty_tiocmset(tty, file, cmd, p); | |
3199 | } | |
3200 | if (tty->driver->ioctl) { | |
3201 | retval = (tty->driver->ioctl)(tty, file, cmd, arg); | |
3202 | if (retval != -ENOIOCTLCMD) | |
3203 | return retval; | |
3204 | } | |
3205 | ld = tty_ldisc_ref_wait(tty); | |
3206 | retval = -EINVAL; | |
3207 | if (ld->ioctl) { | |
3208 | retval = ld->ioctl(tty, file, cmd, arg); | |
3209 | if (retval == -ENOIOCTLCMD) | |
3210 | retval = -EINVAL; | |
3211 | } | |
3212 | tty_ldisc_deref(ld); | |
3213 | return retval; | |
3214 | } | |
3215 | ||
3216 | ||
3217 | /* | |
3218 | * This implements the "Secure Attention Key" --- the idea is to | |
3219 | * prevent trojan horses by killing all processes associated with this | |
3220 | * tty when the user hits the "Secure Attention Key". Required for | |
3221 | * super-paranoid applications --- see the Orange Book for more details. | |
3222 | * | |
3223 | * This code could be nicer; ideally it should send a HUP, wait a few | |
3224 | * seconds, then send a INT, and then a KILL signal. But you then | |
3225 | * have to coordinate with the init process, since all processes associated | |
3226 | * with the current tty must be dead before the new getty is allowed | |
3227 | * to spawn. | |
3228 | * | |
3229 | * Now, if it would be correct ;-/ The current code has a nasty hole - | |
3230 | * it doesn't catch files in flight. We may send the descriptor to ourselves | |
3231 | * via AF_UNIX socket, close it and later fetch from socket. FIXME. | |
3232 | * | |
3233 | * Nasty bug: do_SAK is being called in interrupt context. This can | |
3234 | * deadlock. We punt it up to process context. AKPM - 16Mar2001 | |
3235 | */ | |
3236 | static void __do_SAK(void *arg) | |
3237 | { | |
3238 | #ifdef TTY_SOFT_SAK | |
3239 | tty_hangup(tty); | |
3240 | #else | |
3241 | struct tty_struct *tty = arg; | |
652486fb | 3242 | struct task_struct *g, *p; |
1da177e4 LT |
3243 | int session; |
3244 | int i; | |
3245 | struct file *filp; | |
3246 | struct tty_ldisc *disc; | |
badf1662 | 3247 | struct fdtable *fdt; |
1da177e4 LT |
3248 | |
3249 | if (!tty) | |
3250 | return; | |
3251 | session = tty->session; | |
3252 | ||
3253 | /* We don't want an ldisc switch during this */ | |
3254 | disc = tty_ldisc_ref(tty); | |
3255 | if (disc && disc->flush_buffer) | |
3256 | disc->flush_buffer(tty); | |
3257 | tty_ldisc_deref(disc); | |
3258 | ||
3259 | if (tty->driver->flush_buffer) | |
3260 | tty->driver->flush_buffer(tty); | |
3261 | ||
3262 | read_lock(&tasklist_lock); | |
652486fb | 3263 | /* Kill the entire session */ |
1da177e4 | 3264 | do_each_task_pid(session, PIDTYPE_SID, p) { |
652486fb EB |
3265 | printk(KERN_NOTICE "SAK: killed process %d" |
3266 | " (%s): p->signal->session==tty->session\n", | |
3267 | p->pid, p->comm); | |
3268 | send_sig(SIGKILL, p, 1); | |
3269 | } while_each_task_pid(session, PIDTYPE_SID, p); | |
3270 | /* Now kill any processes that happen to have the | |
3271 | * tty open. | |
3272 | */ | |
3273 | do_each_thread(g, p) { | |
3274 | if (p->signal->tty == tty) { | |
1da177e4 LT |
3275 | printk(KERN_NOTICE "SAK: killed process %d" |
3276 | " (%s): p->signal->session==tty->session\n", | |
3277 | p->pid, p->comm); | |
3278 | send_sig(SIGKILL, p, 1); | |
3279 | continue; | |
3280 | } | |
3281 | task_lock(p); | |
3282 | if (p->files) { | |
ca99c1da DS |
3283 | /* |
3284 | * We don't take a ref to the file, so we must | |
3285 | * hold ->file_lock instead. | |
3286 | */ | |
3287 | spin_lock(&p->files->file_lock); | |
badf1662 DS |
3288 | fdt = files_fdtable(p->files); |
3289 | for (i=0; i < fdt->max_fds; i++) { | |
1da177e4 LT |
3290 | filp = fcheck_files(p->files, i); |
3291 | if (!filp) | |
3292 | continue; | |
3293 | if (filp->f_op->read == tty_read && | |
3294 | filp->private_data == tty) { | |
3295 | printk(KERN_NOTICE "SAK: killed process %d" | |
3296 | " (%s): fd#%d opened to the tty\n", | |
3297 | p->pid, p->comm, i); | |
20ac9437 | 3298 | force_sig(SIGKILL, p); |
1da177e4 LT |
3299 | break; |
3300 | } | |
3301 | } | |
ca99c1da | 3302 | spin_unlock(&p->files->file_lock); |
1da177e4 LT |
3303 | } |
3304 | task_unlock(p); | |
652486fb | 3305 | } while_each_thread(g, p); |
1da177e4 LT |
3306 | read_unlock(&tasklist_lock); |
3307 | #endif | |
3308 | } | |
3309 | ||
3310 | /* | |
3311 | * The tq handling here is a little racy - tty->SAK_work may already be queued. | |
3312 | * Fortunately we don't need to worry, because if ->SAK_work is already queued, | |
3313 | * the values which we write to it will be identical to the values which it | |
3314 | * already has. --akpm | |
3315 | */ | |
3316 | void do_SAK(struct tty_struct *tty) | |
3317 | { | |
3318 | if (!tty) | |
3319 | return; | |
3320 | PREPARE_WORK(&tty->SAK_work, __do_SAK, tty); | |
3321 | schedule_work(&tty->SAK_work); | |
3322 | } | |
3323 | ||
3324 | EXPORT_SYMBOL(do_SAK); | |
3325 | ||
af9b897e AC |
3326 | /** |
3327 | * flush_to_ldisc | |
3328 | * @private_: tty structure passed from work queue. | |
3329 | * | |
3330 | * This routine is called out of the software interrupt to flush data | |
3331 | * from the buffer chain to the line discipline. | |
3332 | * | |
3333 | * Locking: holds tty->buf.lock to guard buffer list. Drops the lock | |
3334 | * while invoking the line discipline receive_buf method. The | |
3335 | * receive_buf method is single threaded for each tty instance. | |
1da177e4 LT |
3336 | */ |
3337 | ||
3338 | static void flush_to_ldisc(void *private_) | |
3339 | { | |
3340 | struct tty_struct *tty = (struct tty_struct *) private_; | |
1da177e4 LT |
3341 | unsigned long flags; |
3342 | struct tty_ldisc *disc; | |
2c3bb20f | 3343 | struct tty_buffer *tbuf, *head; |
8977d929 PF |
3344 | char *char_buf; |
3345 | unsigned char *flag_buf; | |
1da177e4 LT |
3346 | |
3347 | disc = tty_ldisc_ref(tty); | |
3348 | if (disc == NULL) /* !TTY_LDISC */ | |
3349 | return; | |
3350 | ||
808249ce | 3351 | spin_lock_irqsave(&tty->buf.lock, flags); |
2c3bb20f PF |
3352 | head = tty->buf.head; |
3353 | if (head != NULL) { | |
3354 | tty->buf.head = NULL; | |
3355 | for (;;) { | |
3356 | int count = head->commit - head->read; | |
3357 | if (!count) { | |
3358 | if (head->next == NULL) | |
3359 | break; | |
3360 | tbuf = head; | |
3361 | head = head->next; | |
3362 | tty_buffer_free(tty, tbuf); | |
3363 | continue; | |
3364 | } | |
3365 | if (!tty->receive_room) { | |
3366 | schedule_delayed_work(&tty->buf.work, 1); | |
3367 | break; | |
3368 | } | |
3369 | if (count > tty->receive_room) | |
3370 | count = tty->receive_room; | |
3371 | char_buf = head->char_buf_ptr + head->read; | |
3372 | flag_buf = head->flag_buf_ptr + head->read; | |
3373 | head->read += count; | |
8977d929 PF |
3374 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
3375 | disc->receive_buf(tty, char_buf, flag_buf, count); | |
3376 | spin_lock_irqsave(&tty->buf.lock, flags); | |
3377 | } | |
2c3bb20f | 3378 | tty->buf.head = head; |
33f0f88f | 3379 | } |
808249ce | 3380 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
817d6d3b | 3381 | |
1da177e4 LT |
3382 | tty_ldisc_deref(disc); |
3383 | } | |
3384 | ||
3385 | /* | |
3386 | * Routine which returns the baud rate of the tty | |
3387 | * | |
3388 | * Note that the baud_table needs to be kept in sync with the | |
3389 | * include/asm/termbits.h file. | |
3390 | */ | |
3391 | static int baud_table[] = { | |
3392 | 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, | |
3393 | 9600, 19200, 38400, 57600, 115200, 230400, 460800, | |
3394 | #ifdef __sparc__ | |
3395 | 76800, 153600, 307200, 614400, 921600 | |
3396 | #else | |
3397 | 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, | |
3398 | 2500000, 3000000, 3500000, 4000000 | |
3399 | #endif | |
3400 | }; | |
3401 | ||
3402 | static int n_baud_table = ARRAY_SIZE(baud_table); | |
3403 | ||
3404 | /** | |
3405 | * tty_termios_baud_rate | |
3406 | * @termios: termios structure | |
3407 | * | |
3408 | * Convert termios baud rate data into a speed. This should be called | |
3409 | * with the termios lock held if this termios is a terminal termios | |
3410 | * structure. May change the termios data. | |
af9b897e AC |
3411 | * |
3412 | * Locking: none | |
1da177e4 LT |
3413 | */ |
3414 | ||
3415 | int tty_termios_baud_rate(struct termios *termios) | |
3416 | { | |
3417 | unsigned int cbaud; | |
3418 | ||
3419 | cbaud = termios->c_cflag & CBAUD; | |
3420 | ||
3421 | if (cbaud & CBAUDEX) { | |
3422 | cbaud &= ~CBAUDEX; | |
3423 | ||
3424 | if (cbaud < 1 || cbaud + 15 > n_baud_table) | |
3425 | termios->c_cflag &= ~CBAUDEX; | |
3426 | else | |
3427 | cbaud += 15; | |
3428 | } | |
3429 | return baud_table[cbaud]; | |
3430 | } | |
3431 | ||
3432 | EXPORT_SYMBOL(tty_termios_baud_rate); | |
3433 | ||
3434 | /** | |
3435 | * tty_get_baud_rate - get tty bit rates | |
3436 | * @tty: tty to query | |
3437 | * | |
3438 | * Returns the baud rate as an integer for this terminal. The | |
3439 | * termios lock must be held by the caller and the terminal bit | |
3440 | * flags may be updated. | |
af9b897e AC |
3441 | * |
3442 | * Locking: none | |
1da177e4 LT |
3443 | */ |
3444 | ||
3445 | int tty_get_baud_rate(struct tty_struct *tty) | |
3446 | { | |
3447 | int baud = tty_termios_baud_rate(tty->termios); | |
3448 | ||
3449 | if (baud == 38400 && tty->alt_speed) { | |
3450 | if (!tty->warned) { | |
3451 | printk(KERN_WARNING "Use of setserial/setrocket to " | |
3452 | "set SPD_* flags is deprecated\n"); | |
3453 | tty->warned = 1; | |
3454 | } | |
3455 | baud = tty->alt_speed; | |
3456 | } | |
3457 | ||
3458 | return baud; | |
3459 | } | |
3460 | ||
3461 | EXPORT_SYMBOL(tty_get_baud_rate); | |
3462 | ||
3463 | /** | |
3464 | * tty_flip_buffer_push - terminal | |
3465 | * @tty: tty to push | |
3466 | * | |
3467 | * Queue a push of the terminal flip buffers to the line discipline. This | |
3468 | * function must not be called from IRQ context if tty->low_latency is set. | |
3469 | * | |
3470 | * In the event of the queue being busy for flipping the work will be | |
3471 | * held off and retried later. | |
af9b897e AC |
3472 | * |
3473 | * Locking: tty buffer lock. Driver locks in low latency mode. | |
1da177e4 LT |
3474 | */ |
3475 | ||
3476 | void tty_flip_buffer_push(struct tty_struct *tty) | |
3477 | { | |
808249ce PF |
3478 | unsigned long flags; |
3479 | spin_lock_irqsave(&tty->buf.lock, flags); | |
33b37a33 | 3480 | if (tty->buf.tail != NULL) |
8977d929 | 3481 | tty->buf.tail->commit = tty->buf.tail->used; |
808249ce PF |
3482 | spin_unlock_irqrestore(&tty->buf.lock, flags); |
3483 | ||
1da177e4 LT |
3484 | if (tty->low_latency) |
3485 | flush_to_ldisc((void *) tty); | |
3486 | else | |
33f0f88f | 3487 | schedule_delayed_work(&tty->buf.work, 1); |
1da177e4 LT |
3488 | } |
3489 | ||
3490 | EXPORT_SYMBOL(tty_flip_buffer_push); | |
3491 | ||
33f0f88f | 3492 | |
af9b897e AC |
3493 | /** |
3494 | * initialize_tty_struct | |
3495 | * @tty: tty to initialize | |
3496 | * | |
3497 | * This subroutine initializes a tty structure that has been newly | |
3498 | * allocated. | |
3499 | * | |
3500 | * Locking: none - tty in question must not be exposed at this point | |
1da177e4 | 3501 | */ |
af9b897e | 3502 | |
1da177e4 LT |
3503 | static void initialize_tty_struct(struct tty_struct *tty) |
3504 | { | |
3505 | memset(tty, 0, sizeof(struct tty_struct)); | |
3506 | tty->magic = TTY_MAGIC; | |
3507 | tty_ldisc_assign(tty, tty_ldisc_get(N_TTY)); | |
3508 | tty->pgrp = -1; | |
3509 | tty->overrun_time = jiffies; | |
33f0f88f AC |
3510 | tty->buf.head = tty->buf.tail = NULL; |
3511 | tty_buffer_init(tty); | |
3512 | INIT_WORK(&tty->buf.work, flush_to_ldisc, tty); | |
3513 | init_MUTEX(&tty->buf.pty_sem); | |
1da177e4 LT |
3514 | init_MUTEX(&tty->termios_sem); |
3515 | init_waitqueue_head(&tty->write_wait); | |
3516 | init_waitqueue_head(&tty->read_wait); | |
3517 | INIT_WORK(&tty->hangup_work, do_tty_hangup, tty); | |
70522e12 IM |
3518 | mutex_init(&tty->atomic_read_lock); |
3519 | mutex_init(&tty->atomic_write_lock); | |
1da177e4 LT |
3520 | spin_lock_init(&tty->read_lock); |
3521 | INIT_LIST_HEAD(&tty->tty_files); | |
3522 | INIT_WORK(&tty->SAK_work, NULL, NULL); | |
3523 | } | |
3524 | ||
3525 | /* | |
3526 | * The default put_char routine if the driver did not define one. | |
3527 | */ | |
af9b897e | 3528 | |
1da177e4 LT |
3529 | static void tty_default_put_char(struct tty_struct *tty, unsigned char ch) |
3530 | { | |
3531 | tty->driver->write(tty, &ch, 1); | |
3532 | } | |
3533 | ||
7fe845d1 | 3534 | static struct class *tty_class; |
1da177e4 LT |
3535 | |
3536 | /** | |
af9b897e AC |
3537 | * tty_register_device - register a tty device |
3538 | * @driver: the tty driver that describes the tty device | |
3539 | * @index: the index in the tty driver for this tty device | |
3540 | * @device: a struct device that is associated with this tty device. | |
3541 | * This field is optional, if there is no known struct device | |
3542 | * for this tty device it can be set to NULL safely. | |
1da177e4 | 3543 | * |
af9b897e | 3544 | * Returns a pointer to the class device (or ERR_PTR(-EFOO) on error). |
1cdcb6b4 | 3545 | * |
af9b897e AC |
3546 | * This call is required to be made to register an individual tty device |
3547 | * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If | |
3548 | * that bit is not set, this function should not be called by a tty | |
3549 | * driver. | |
3550 | * | |
3551 | * Locking: ?? | |
1da177e4 | 3552 | */ |
af9b897e | 3553 | |
1cdcb6b4 HL |
3554 | struct class_device *tty_register_device(struct tty_driver *driver, |
3555 | unsigned index, struct device *device) | |
1da177e4 LT |
3556 | { |
3557 | char name[64]; | |
3558 | dev_t dev = MKDEV(driver->major, driver->minor_start) + index; | |
3559 | ||
3560 | if (index >= driver->num) { | |
3561 | printk(KERN_ERR "Attempt to register invalid tty line number " | |
3562 | " (%d).\n", index); | |
1cdcb6b4 | 3563 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
3564 | } |
3565 | ||
1da177e4 LT |
3566 | if (driver->type == TTY_DRIVER_TYPE_PTY) |
3567 | pty_line_name(driver, index, name); | |
3568 | else | |
3569 | tty_line_name(driver, index, name); | |
1cdcb6b4 HL |
3570 | |
3571 | return class_device_create(tty_class, NULL, dev, device, "%s", name); | |
1da177e4 LT |
3572 | } |
3573 | ||
3574 | /** | |
af9b897e AC |
3575 | * tty_unregister_device - unregister a tty device |
3576 | * @driver: the tty driver that describes the tty device | |
3577 | * @index: the index in the tty driver for this tty device | |
1da177e4 | 3578 | * |
af9b897e AC |
3579 | * If a tty device is registered with a call to tty_register_device() then |
3580 | * this function must be called when the tty device is gone. | |
3581 | * | |
3582 | * Locking: ?? | |
1da177e4 | 3583 | */ |
af9b897e | 3584 | |
1da177e4 LT |
3585 | void tty_unregister_device(struct tty_driver *driver, unsigned index) |
3586 | { | |
7fe845d1 | 3587 | class_device_destroy(tty_class, MKDEV(driver->major, driver->minor_start) + index); |
1da177e4 LT |
3588 | } |
3589 | ||
3590 | EXPORT_SYMBOL(tty_register_device); | |
3591 | EXPORT_SYMBOL(tty_unregister_device); | |
3592 | ||
3593 | struct tty_driver *alloc_tty_driver(int lines) | |
3594 | { | |
3595 | struct tty_driver *driver; | |
3596 | ||
3597 | driver = kmalloc(sizeof(struct tty_driver), GFP_KERNEL); | |
3598 | if (driver) { | |
3599 | memset(driver, 0, sizeof(struct tty_driver)); | |
3600 | driver->magic = TTY_DRIVER_MAGIC; | |
3601 | driver->num = lines; | |
3602 | /* later we'll move allocation of tables here */ | |
3603 | } | |
3604 | return driver; | |
3605 | } | |
3606 | ||
3607 | void put_tty_driver(struct tty_driver *driver) | |
3608 | { | |
3609 | kfree(driver); | |
3610 | } | |
3611 | ||
3612 | void tty_set_operations(struct tty_driver *driver, struct tty_operations *op) | |
3613 | { | |
3614 | driver->open = op->open; | |
3615 | driver->close = op->close; | |
3616 | driver->write = op->write; | |
3617 | driver->put_char = op->put_char; | |
3618 | driver->flush_chars = op->flush_chars; | |
3619 | driver->write_room = op->write_room; | |
3620 | driver->chars_in_buffer = op->chars_in_buffer; | |
3621 | driver->ioctl = op->ioctl; | |
3622 | driver->set_termios = op->set_termios; | |
3623 | driver->throttle = op->throttle; | |
3624 | driver->unthrottle = op->unthrottle; | |
3625 | driver->stop = op->stop; | |
3626 | driver->start = op->start; | |
3627 | driver->hangup = op->hangup; | |
3628 | driver->break_ctl = op->break_ctl; | |
3629 | driver->flush_buffer = op->flush_buffer; | |
3630 | driver->set_ldisc = op->set_ldisc; | |
3631 | driver->wait_until_sent = op->wait_until_sent; | |
3632 | driver->send_xchar = op->send_xchar; | |
3633 | driver->read_proc = op->read_proc; | |
3634 | driver->write_proc = op->write_proc; | |
3635 | driver->tiocmget = op->tiocmget; | |
3636 | driver->tiocmset = op->tiocmset; | |
3637 | } | |
3638 | ||
3639 | ||
3640 | EXPORT_SYMBOL(alloc_tty_driver); | |
3641 | EXPORT_SYMBOL(put_tty_driver); | |
3642 | EXPORT_SYMBOL(tty_set_operations); | |
3643 | ||
3644 | /* | |
3645 | * Called by a tty driver to register itself. | |
3646 | */ | |
3647 | int tty_register_driver(struct tty_driver *driver) | |
3648 | { | |
3649 | int error; | |
3650 | int i; | |
3651 | dev_t dev; | |
3652 | void **p = NULL; | |
3653 | ||
3654 | if (driver->flags & TTY_DRIVER_INSTALLED) | |
3655 | return 0; | |
3656 | ||
3657 | if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) { | |
3658 | p = kmalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL); | |
3659 | if (!p) | |
3660 | return -ENOMEM; | |
3661 | memset(p, 0, driver->num * 3 * sizeof(void *)); | |
3662 | } | |
3663 | ||
3664 | if (!driver->major) { | |
3665 | error = alloc_chrdev_region(&dev, driver->minor_start, driver->num, | |
3666 | (char*)driver->name); | |
3667 | if (!error) { | |
3668 | driver->major = MAJOR(dev); | |
3669 | driver->minor_start = MINOR(dev); | |
3670 | } | |
3671 | } else { | |
3672 | dev = MKDEV(driver->major, driver->minor_start); | |
3673 | error = register_chrdev_region(dev, driver->num, | |
3674 | (char*)driver->name); | |
3675 | } | |
3676 | if (error < 0) { | |
3677 | kfree(p); | |
3678 | return error; | |
3679 | } | |
3680 | ||
3681 | if (p) { | |
3682 | driver->ttys = (struct tty_struct **)p; | |
3683 | driver->termios = (struct termios **)(p + driver->num); | |
3684 | driver->termios_locked = (struct termios **)(p + driver->num * 2); | |
3685 | } else { | |
3686 | driver->ttys = NULL; | |
3687 | driver->termios = NULL; | |
3688 | driver->termios_locked = NULL; | |
3689 | } | |
3690 | ||
3691 | cdev_init(&driver->cdev, &tty_fops); | |
3692 | driver->cdev.owner = driver->owner; | |
3693 | error = cdev_add(&driver->cdev, dev, driver->num); | |
3694 | if (error) { | |
1da177e4 LT |
3695 | unregister_chrdev_region(dev, driver->num); |
3696 | driver->ttys = NULL; | |
3697 | driver->termios = driver->termios_locked = NULL; | |
3698 | kfree(p); | |
3699 | return error; | |
3700 | } | |
3701 | ||
3702 | if (!driver->put_char) | |
3703 | driver->put_char = tty_default_put_char; | |
3704 | ||
3705 | list_add(&driver->tty_drivers, &tty_drivers); | |
3706 | ||
331b8319 | 3707 | if ( !(driver->flags & TTY_DRIVER_DYNAMIC_DEV) ) { |
1da177e4 LT |
3708 | for(i = 0; i < driver->num; i++) |
3709 | tty_register_device(driver, i, NULL); | |
3710 | } | |
3711 | proc_tty_register_driver(driver); | |
3712 | return 0; | |
3713 | } | |
3714 | ||
3715 | EXPORT_SYMBOL(tty_register_driver); | |
3716 | ||
3717 | /* | |
3718 | * Called by a tty driver to unregister itself. | |
3719 | */ | |
3720 | int tty_unregister_driver(struct tty_driver *driver) | |
3721 | { | |
3722 | int i; | |
3723 | struct termios *tp; | |
3724 | void *p; | |
3725 | ||
3726 | if (driver->refcount) | |
3727 | return -EBUSY; | |
3728 | ||
3729 | unregister_chrdev_region(MKDEV(driver->major, driver->minor_start), | |
3730 | driver->num); | |
3731 | ||
3732 | list_del(&driver->tty_drivers); | |
3733 | ||
3734 | /* | |
3735 | * Free the termios and termios_locked structures because | |
3736 | * we don't want to get memory leaks when modular tty | |
3737 | * drivers are removed from the kernel. | |
3738 | */ | |
3739 | for (i = 0; i < driver->num; i++) { | |
3740 | tp = driver->termios[i]; | |
3741 | if (tp) { | |
3742 | driver->termios[i] = NULL; | |
3743 | kfree(tp); | |
3744 | } | |
3745 | tp = driver->termios_locked[i]; | |
3746 | if (tp) { | |
3747 | driver->termios_locked[i] = NULL; | |
3748 | kfree(tp); | |
3749 | } | |
331b8319 | 3750 | if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) |
1da177e4 LT |
3751 | tty_unregister_device(driver, i); |
3752 | } | |
3753 | p = driver->ttys; | |
3754 | proc_tty_unregister_driver(driver); | |
3755 | driver->ttys = NULL; | |
3756 | driver->termios = driver->termios_locked = NULL; | |
3757 | kfree(p); | |
3758 | cdev_del(&driver->cdev); | |
3759 | return 0; | |
3760 | } | |
3761 | ||
3762 | EXPORT_SYMBOL(tty_unregister_driver); | |
3763 | ||
3764 | ||
3765 | /* | |
3766 | * Initialize the console device. This is called *early*, so | |
3767 | * we can't necessarily depend on lots of kernel help here. | |
3768 | * Just do some early initializations, and do the complex setup | |
3769 | * later. | |
3770 | */ | |
3771 | void __init console_init(void) | |
3772 | { | |
3773 | initcall_t *call; | |
3774 | ||
3775 | /* Setup the default TTY line discipline. */ | |
3776 | (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY); | |
3777 | ||
3778 | /* | |
3779 | * set up the console device so that later boot sequences can | |
3780 | * inform about problems etc.. | |
3781 | */ | |
3782 | #ifdef CONFIG_EARLY_PRINTK | |
3783 | disable_early_printk(); | |
1da177e4 LT |
3784 | #endif |
3785 | call = __con_initcall_start; | |
3786 | while (call < __con_initcall_end) { | |
3787 | (*call)(); | |
3788 | call++; | |
3789 | } | |
3790 | } | |
3791 | ||
3792 | #ifdef CONFIG_VT | |
3793 | extern int vty_init(void); | |
3794 | #endif | |
3795 | ||
3796 | static int __init tty_class_init(void) | |
3797 | { | |
7fe845d1 | 3798 | tty_class = class_create(THIS_MODULE, "tty"); |
1da177e4 LT |
3799 | if (IS_ERR(tty_class)) |
3800 | return PTR_ERR(tty_class); | |
3801 | return 0; | |
3802 | } | |
3803 | ||
3804 | postcore_initcall(tty_class_init); | |
3805 | ||
3806 | /* 3/2004 jmc: why do these devices exist? */ | |
3807 | ||
3808 | static struct cdev tty_cdev, console_cdev; | |
3809 | #ifdef CONFIG_UNIX98_PTYS | |
3810 | static struct cdev ptmx_cdev; | |
3811 | #endif | |
3812 | #ifdef CONFIG_VT | |
3813 | static struct cdev vc0_cdev; | |
3814 | #endif | |
3815 | ||
3816 | /* | |
3817 | * Ok, now we can initialize the rest of the tty devices and can count | |
3818 | * on memory allocations, interrupts etc.. | |
3819 | */ | |
3820 | static int __init tty_init(void) | |
3821 | { | |
3822 | cdev_init(&tty_cdev, &tty_fops); | |
3823 | if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) || | |
3824 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0) | |
3825 | panic("Couldn't register /dev/tty driver\n"); | |
53f46542 | 3826 | class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty"); |
1da177e4 LT |
3827 | |
3828 | cdev_init(&console_cdev, &console_fops); | |
3829 | if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) || | |
3830 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0) | |
3831 | panic("Couldn't register /dev/console driver\n"); | |
53f46542 | 3832 | class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL, "console"); |
1da177e4 LT |
3833 | |
3834 | #ifdef CONFIG_UNIX98_PTYS | |
3835 | cdev_init(&ptmx_cdev, &ptmx_fops); | |
3836 | if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) || | |
3837 | register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0) | |
3838 | panic("Couldn't register /dev/ptmx driver\n"); | |
53f46542 | 3839 | class_device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx"); |
1da177e4 LT |
3840 | #endif |
3841 | ||
3842 | #ifdef CONFIG_VT | |
3843 | cdev_init(&vc0_cdev, &console_fops); | |
3844 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || | |
3845 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) | |
3846 | panic("Couldn't register /dev/tty0 driver\n"); | |
53f46542 | 3847 | class_device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0"); |
1da177e4 LT |
3848 | |
3849 | vty_init(); | |
3850 | #endif | |
3851 | return 0; | |
3852 | } | |
3853 | module_init(tty_init); |