blob: 1d298c2cf9301c0a1ac2026577463e6a3a6b1813 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Alan Cox37bdfb02008-02-08 04:18:47 -080022 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 *
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
Alan Cox37bdfb02008-02-08 04:18:47 -080044 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * 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.
Alan Cox37bdfb02008-02-08 04:18:47 -080065 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/types.h>
70#include <linux/major.h>
71#include <linux/errno.h>
72#include <linux/signal.h>
73#include <linux/fcntl.h>
74#include <linux/sched.h>
75#include <linux/interrupt.h>
76#include <linux/tty.h>
77#include <linux/tty_driver.h>
78#include <linux/tty_flip.h>
79#include <linux/devpts_fs.h>
80#include <linux/file.h>
81#include <linux/console.h>
82#include <linux/timer.h>
83#include <linux/ctype.h>
84#include <linux/kd.h>
85#include <linux/mm.h>
86#include <linux/string.h>
87#include <linux/slab.h>
88#include <linux/poll.h>
89#include <linux/proc_fs.h>
90#include <linux/init.h>
91#include <linux/module.h>
92#include <linux/smp_lock.h>
93#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <linux/wait.h>
95#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070096#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700106#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#undef TTY_DEBUG_HANGUP
109
110#define TTY_PARANOIA_CHECK 1
111#define CHECK_TTY_COUNT 1
112
Alan Coxedc6afc2006-12-08 02:38:44 -0800113struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .c_iflag = ICRNL | IXON,
115 .c_oflag = OPOST | ONLCR,
116 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
117 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
118 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800119 .c_cc = INIT_C_CC,
120 .c_ispeed = 38400,
121 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122};
123
124EXPORT_SYMBOL(tty_std_termios);
125
126/* This list gets poked at by procfs and various bits of boot up code. This
127 could do with some rationalisation such as pulling the tty proc function
128 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130LIST_HEAD(tty_drivers); /* linked list of tty drivers */
131
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800132/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800134DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700135EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137#ifdef CONFIG_UNIX98_PTYS
138extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static int ptmx_open(struct inode *, struct file *);
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void initialize_tty_struct(struct tty_struct *tty);
143
144static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
145static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800146ssize_t redirected_tty_write(struct file *, const char __user *,
147 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static unsigned int tty_poll(struct file *, poll_table *);
149static int tty_open(struct inode *, struct file *);
150static int tty_release(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700151long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700152#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800153static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700154 unsigned long arg);
155#else
156#define tty_compat_ioctl NULL
157#endif
Alan Cox37bdfb02008-02-08 04:18:47 -0800158static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800159static void release_tty(struct tty_struct *tty, int idx);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700160static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700161static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Alan Coxaf9b8972006-08-27 01:24:01 -0700163/**
164 * alloc_tty_struct - allocate a tty object
165 *
166 * Return a new empty tty structure. The data fields have not
167 * been initialized in any way but has been zeroed
168 *
169 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static struct tty_struct *alloc_tty_struct(void)
173{
Alan Cox1266b1e2006-09-29 02:00:40 -0700174 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Alan Cox33f0f882006-01-09 20:54:13 -0800177static void tty_buffer_free_all(struct tty_struct *);
178
Alan Coxaf9b8972006-08-27 01:24:01 -0700179/**
180 * free_tty_struct - free a disused tty
181 * @tty: tty struct to free
182 *
183 * Free the write buffers, tty queue and tty memory itself.
184 *
185 * Locking: none. Must be called after tty is definitely unused
186 */
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static inline void free_tty_struct(struct tty_struct *tty)
189{
190 kfree(tty->write_buf);
Alan Cox33f0f882006-01-09 20:54:13 -0800191 tty_buffer_free_all(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kfree(tty);
193}
194
195#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
196
Alan Coxaf9b8972006-08-27 01:24:01 -0700197/**
198 * tty_name - return tty naming
199 * @tty: tty structure
200 * @buf: buffer for output
201 *
202 * Convert a tty structure into a name. The name reflects the kernel
203 * naming policy and if udev is in use may not reflect user space
204 *
205 * Locking: none
206 */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208char *tty_name(struct tty_struct *tty, char *buf)
209{
210 if (!tty) /* Hmm. NULL pointer. That's fun. */
211 strcpy(buf, "NULL tty");
212 else
213 strcpy(buf, tty->name);
214 return buf;
215}
216
217EXPORT_SYMBOL(tty_name);
218
Andrew Mortond769a662005-05-05 16:15:50 -0700219int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 const char *routine)
221{
222#ifdef TTY_PARANOIA_CHECK
223 if (!tty) {
224 printk(KERN_WARNING
225 "null TTY for (%d:%d) in %s\n",
226 imajor(inode), iminor(inode), routine);
227 return 1;
228 }
229 if (tty->magic != TTY_MAGIC) {
230 printk(KERN_WARNING
231 "bad magic number for tty struct (%d:%d) in %s\n",
232 imajor(inode), iminor(inode), routine);
233 return 1;
234 }
235#endif
236 return 0;
237}
238
239static int check_tty_count(struct tty_struct *tty, const char *routine)
240{
241#ifdef CHECK_TTY_COUNT
242 struct list_head *p;
243 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 file_list_lock();
246 list_for_each(p, &tty->tty_files) {
247 count++;
248 }
249 file_list_unlock();
250 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
251 tty->driver->subtype == PTY_TYPE_SLAVE &&
252 tty->link && tty->link->count)
253 count++;
254 if (tty->count != count) {
255 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
256 "!= #fd's(%d) in %s\n",
257 tty->name, tty->count, count, routine);
258 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260#endif
261 return 0;
262}
263
264/*
Alan Cox33f0f882006-01-09 20:54:13 -0800265 * Tty buffer allocation management
266 */
267
Alan Cox01da5fd2006-08-27 01:24:02 -0700268/**
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
Alan Cox33f0f882006-01-09 20:54:13 -0800278static void tty_buffer_free_all(struct tty_struct *tty)
279{
280 struct tty_buffer *thead;
Alan Cox37bdfb02008-02-08 04:18:47 -0800281 while ((thead = tty->buf.head) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800282 tty->buf.head = thead->next;
283 kfree(thead);
284 }
Alan Cox37bdfb02008-02-08 04:18:47 -0800285 while ((thead = tty->buf.free) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800286 tty->buf.free = thead->next;
287 kfree(thead);
288 }
289 tty->buf.tail = NULL;
Alan Cox01da5fd2006-08-27 01:24:02 -0700290 tty->buf.memory_used = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800291}
292
Alan Cox01da5fd2006-08-27 01:24:02 -0700293/**
294 * tty_buffer_init - prepare a tty buffer structure
295 * @tty: tty to initialise
296 *
297 * Set up the initial state of the buffer management for a tty device.
298 * Must be called before the other tty buffer functions are used.
299 *
300 * Locking: none
301 */
302
Alan Cox33f0f882006-01-09 20:54:13 -0800303static void tty_buffer_init(struct tty_struct *tty)
304{
Paul Fulghum808249c2006-02-03 03:04:41 -0800305 spin_lock_init(&tty->buf.lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800306 tty->buf.head = NULL;
307 tty->buf.tail = NULL;
308 tty->buf.free = NULL;
Alan Cox01da5fd2006-08-27 01:24:02 -0700309 tty->buf.memory_used = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800310}
311
Alan Cox01da5fd2006-08-27 01:24:02 -0700312/**
313 * tty_buffer_alloc - allocate a tty buffer
314 * @tty: tty device
315 * @size: desired size (characters)
316 *
317 * Allocate a new tty buffer to hold the desired number of characters.
318 * Return NULL if out of memory or the allocation would exceed the
319 * per device queue
320 *
321 * Locking: Caller must hold tty->buf.lock
322 */
323
324static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800325{
Alan Cox01da5fd2006-08-27 01:24:02 -0700326 struct tty_buffer *p;
327
328 if (tty->buf.memory_used + size > 65536)
329 return NULL;
330 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
Alan Cox37bdfb02008-02-08 04:18:47 -0800331 if (p == NULL)
Alan Cox33f0f882006-01-09 20:54:13 -0800332 return NULL;
333 p->used = 0;
334 p->size = size;
335 p->next = NULL;
Paul Fulghum8977d922006-02-10 01:51:14 -0800336 p->commit = 0;
337 p->read = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800338 p->char_buf_ptr = (char *)(p->data);
339 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
Alan Cox01da5fd2006-08-27 01:24:02 -0700340 tty->buf.memory_used += size;
Alan Cox33f0f882006-01-09 20:54:13 -0800341 return p;
342}
343
Alan Cox01da5fd2006-08-27 01:24:02 -0700344/**
345 * tty_buffer_free - free a tty buffer
346 * @tty: tty owning the buffer
347 * @b: the buffer to free
348 *
349 * Free a tty buffer, or add it to the free list according to our
350 * internal strategy
351 *
352 * Locking: Caller must hold tty->buf.lock
353 */
Alan Cox33f0f882006-01-09 20:54:13 -0800354
355static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
356{
357 /* Dumb strategy for now - should keep some stats */
Alan Cox01da5fd2006-08-27 01:24:02 -0700358 tty->buf.memory_used -= b->size;
359 WARN_ON(tty->buf.memory_used < 0);
360
Alan Cox37bdfb02008-02-08 04:18:47 -0800361 if (b->size >= 512)
Alan Cox33f0f882006-01-09 20:54:13 -0800362 kfree(b);
363 else {
364 b->next = tty->buf.free;
365 tty->buf.free = b;
366 }
367}
368
Alan Cox01da5fd2006-08-27 01:24:02 -0700369/**
Alan Cox42fd5522007-08-10 13:01:05 -0700370 * __tty_buffer_flush - flush full tty buffers
371 * @tty: tty to flush
372 *
373 * flush all the buffers containing receive data. Caller must
374 * hold the buffer lock and must have ensured no parallel flush to
375 * ldisc is running.
376 *
377 * Locking: Caller must hold tty->buf.lock
378 */
379
380static void __tty_buffer_flush(struct tty_struct *tty)
381{
382 struct tty_buffer *thead;
383
Alan Cox37bdfb02008-02-08 04:18:47 -0800384 while ((thead = tty->buf.head) != NULL) {
Alan Cox42fd5522007-08-10 13:01:05 -0700385 tty->buf.head = thead->next;
386 tty_buffer_free(tty, thead);
387 }
388 tty->buf.tail = NULL;
389}
390
391/**
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700392 * tty_buffer_flush - flush full tty buffers
393 * @tty: tty to flush
394 *
Alan Cox42fd5522007-08-10 13:01:05 -0700395 * flush all the buffers containing receive data. If the buffer is
396 * being processed by flush_to_ldisc then we defer the processing
397 * to that function
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700398 *
399 * Locking: none
400 */
401
402static void tty_buffer_flush(struct tty_struct *tty)
403{
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700404 unsigned long flags;
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700405 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox42fd5522007-08-10 13:01:05 -0700406
407 /* If the data is being pushed to the tty layer then we can't
408 process it here. Instead set a flag and the flush_to_ldisc
409 path will process the flush request before it exits */
410 if (test_bit(TTY_FLUSHING, &tty->flags)) {
411 set_bit(TTY_FLUSHPENDING, &tty->flags);
412 spin_unlock_irqrestore(&tty->buf.lock, flags);
413 wait_event(tty->read_wait,
414 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
415 return;
416 } else
417 __tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700418 spin_unlock_irqrestore(&tty->buf.lock, flags);
419}
420
421/**
Alan Cox01da5fd2006-08-27 01:24:02 -0700422 * tty_buffer_find - find a free tty buffer
423 * @tty: tty owning the buffer
424 * @size: characters wanted
425 *
426 * Locate an existing suitable tty buffer or if we are lacking one then
427 * allocate a new one. We round our buffers off in 256 character chunks
428 * to get better allocation behaviour.
429 *
430 * Locking: Caller must hold tty->buf.lock
431 */
432
Alan Cox33f0f882006-01-09 20:54:13 -0800433static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
434{
435 struct tty_buffer **tbh = &tty->buf.free;
Alan Cox37bdfb02008-02-08 04:18:47 -0800436 while ((*tbh) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800437 struct tty_buffer *t = *tbh;
Alan Cox37bdfb02008-02-08 04:18:47 -0800438 if (t->size >= size) {
Alan Cox33f0f882006-01-09 20:54:13 -0800439 *tbh = t->next;
440 t->next = NULL;
441 t->used = 0;
Paul Fulghum8977d922006-02-10 01:51:14 -0800442 t->commit = 0;
443 t->read = 0;
Alan Cox01da5fd2006-08-27 01:24:02 -0700444 tty->buf.memory_used += t->size;
Alan Cox33f0f882006-01-09 20:54:13 -0800445 return t;
446 }
447 tbh = &((*tbh)->next);
448 }
449 /* Round the buffer size out */
Alan Cox37bdfb02008-02-08 04:18:47 -0800450 size = (size + 0xFF) & ~0xFF;
Alan Cox01da5fd2006-08-27 01:24:02 -0700451 return tty_buffer_alloc(tty, size);
Alan Cox33f0f882006-01-09 20:54:13 -0800452 /* Should possibly check if this fails for the largest buffer we
453 have queued and recycle that ? */
454}
455
Alan Cox01da5fd2006-08-27 01:24:02 -0700456/**
457 * tty_buffer_request_room - grow tty buffer if needed
458 * @tty: tty structure
459 * @size: size desired
460 *
461 * Make at least size bytes of linear space available for the tty
462 * buffer. If we fail return the size we managed to find.
463 *
464 * Locking: Takes tty->buf.lock
465 */
Alan Cox33f0f882006-01-09 20:54:13 -0800466int tty_buffer_request_room(struct tty_struct *tty, size_t size)
467{
Paul Fulghum808249c2006-02-03 03:04:41 -0800468 struct tty_buffer *b, *n;
469 int left;
470 unsigned long flags;
471
472 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800473
474 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
475 remove this conditional if its worth it. This would be invisible
476 to the callers */
Paul Fulghum33b37a32006-06-28 04:26:49 -0700477 if ((b = tty->buf.tail) != NULL)
Alan Cox33f0f882006-01-09 20:54:13 -0800478 left = b->size - b->used;
Paul Fulghum33b37a32006-06-28 04:26:49 -0700479 else
Paul Fulghum808249c2006-02-03 03:04:41 -0800480 left = 0;
481
482 if (left < size) {
483 /* This is the slow path - looking for new buffers to use */
484 if ((n = tty_buffer_find(tty, size)) != NULL) {
485 if (b != NULL) {
486 b->next = n;
Paul Fulghum8977d922006-02-10 01:51:14 -0800487 b->commit = b->used;
Paul Fulghum808249c2006-02-03 03:04:41 -0800488 } else
489 tty->buf.head = n;
490 tty->buf.tail = n;
Paul Fulghum808249c2006-02-03 03:04:41 -0800491 } else
492 size = left;
493 }
494
495 spin_unlock_irqrestore(&tty->buf.lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800496 return size;
497}
Alan Cox33f0f882006-01-09 20:54:13 -0800498EXPORT_SYMBOL_GPL(tty_buffer_request_room);
499
Alan Coxaf9b8972006-08-27 01:24:01 -0700500/**
501 * tty_insert_flip_string - Add characters to the tty buffer
502 * @tty: tty structure
503 * @chars: characters
504 * @size: size
505 *
506 * Queue a series of bytes to the tty buffering. All the characters
507 * passed are marked as without error. Returns the number added.
508 *
509 * Locking: Called functions may take tty->buf.lock
510 */
511
Andrew Mortone1a25092006-04-10 22:54:05 -0700512int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
513 size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800514{
515 int copied = 0;
516 do {
517 int space = tty_buffer_request_room(tty, size - copied);
518 struct tty_buffer *tb = tty->buf.tail;
519 /* If there is no space then tb may be NULL */
Alan Cox37bdfb02008-02-08 04:18:47 -0800520 if (unlikely(space == 0))
Alan Cox33f0f882006-01-09 20:54:13 -0800521 break;
522 memcpy(tb->char_buf_ptr + tb->used, chars, space);
523 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
524 tb->used += space;
525 copied += space;
526 chars += space;
Alexey Dobriyan527063b2006-09-29 01:59:50 -0700527 /* There is a small chance that we need to split the data over
528 several buffers. If this is the case we must loop */
529 } while (unlikely(size > copied));
Alan Cox33f0f882006-01-09 20:54:13 -0800530 return copied;
531}
Andrew Mortonee37df72006-03-31 02:30:35 -0800532EXPORT_SYMBOL(tty_insert_flip_string);
Alan Cox33f0f882006-01-09 20:54:13 -0800533
Alan Coxaf9b8972006-08-27 01:24:01 -0700534/**
535 * tty_insert_flip_string_flags - Add characters to the tty buffer
536 * @tty: tty structure
537 * @chars: characters
538 * @flags: flag bytes
539 * @size: size
540 *
541 * Queue a series of bytes to the tty buffering. For each character
542 * the flags array indicates the status of the character. Returns the
543 * number added.
544 *
545 * Locking: Called functions may take tty->buf.lock
546 */
547
Andrew Mortone1a25092006-04-10 22:54:05 -0700548int tty_insert_flip_string_flags(struct tty_struct *tty,
549 const unsigned char *chars, const char *flags, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800550{
551 int copied = 0;
552 do {
553 int space = tty_buffer_request_room(tty, size - copied);
554 struct tty_buffer *tb = tty->buf.tail;
555 /* If there is no space then tb may be NULL */
Alan Cox37bdfb02008-02-08 04:18:47 -0800556 if (unlikely(space == 0))
Alan Cox33f0f882006-01-09 20:54:13 -0800557 break;
558 memcpy(tb->char_buf_ptr + tb->used, chars, space);
559 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
560 tb->used += space;
561 copied += space;
562 chars += space;
563 flags += space;
Alexey Dobriyan527063b2006-09-29 01:59:50 -0700564 /* There is a small chance that we need to split the data over
565 several buffers. If this is the case we must loop */
566 } while (unlikely(size > copied));
Alan Cox33f0f882006-01-09 20:54:13 -0800567 return copied;
568}
Tobias Powalowskiff4547f2006-05-22 22:35:28 -0700569EXPORT_SYMBOL(tty_insert_flip_string_flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800570
Alan Coxaf9b8972006-08-27 01:24:01 -0700571/**
572 * tty_schedule_flip - push characters to ldisc
573 * @tty: tty to push from
574 *
575 * Takes any pending buffers and transfers their ownership to the
576 * ldisc side of the queue. It then schedules those characters for
577 * processing by the line discipline.
578 *
579 * Locking: Takes tty->buf.lock
580 */
581
Andrew Mortone1a25092006-04-10 22:54:05 -0700582void tty_schedule_flip(struct tty_struct *tty)
583{
584 unsigned long flags;
585 spin_lock_irqsave(&tty->buf.lock, flags);
Paul Fulghum33b37a32006-06-28 04:26:49 -0700586 if (tty->buf.tail != NULL)
Andrew Mortone1a25092006-04-10 22:54:05 -0700587 tty->buf.tail->commit = tty->buf.tail->used;
Andrew Mortone1a25092006-04-10 22:54:05 -0700588 spin_unlock_irqrestore(&tty->buf.lock, flags);
589 schedule_delayed_work(&tty->buf.work, 1);
590}
591EXPORT_SYMBOL(tty_schedule_flip);
Alan Cox33f0f882006-01-09 20:54:13 -0800592
Alan Coxaf9b8972006-08-27 01:24:01 -0700593/**
594 * tty_prepare_flip_string - make room for characters
595 * @tty: tty
596 * @chars: return pointer for character write area
597 * @size: desired size
598 *
Alan Cox33f0f882006-01-09 20:54:13 -0800599 * Prepare a block of space in the buffer for data. Returns the length
600 * available and buffer pointer to the space which is now allocated and
601 * accounted for as ready for normal characters. This is used for drivers
602 * that need their own block copy routines into the buffer. There is no
603 * guarantee the buffer is a DMA target!
Alan Coxaf9b8972006-08-27 01:24:01 -0700604 *
605 * Locking: May call functions taking tty->buf.lock
Alan Cox33f0f882006-01-09 20:54:13 -0800606 */
607
Alan Cox37bdfb02008-02-08 04:18:47 -0800608int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
609 size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800610{
611 int space = tty_buffer_request_room(tty, size);
Paul Fulghum808249c2006-02-03 03:04:41 -0800612 if (likely(space)) {
613 struct tty_buffer *tb = tty->buf.tail;
614 *chars = tb->char_buf_ptr + tb->used;
615 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
616 tb->used += space;
617 }
Alan Cox33f0f882006-01-09 20:54:13 -0800618 return space;
619}
620
621EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
622
Alan Coxaf9b8972006-08-27 01:24:01 -0700623/**
624 * tty_prepare_flip_string_flags - make room for characters
625 * @tty: tty
626 * @chars: return pointer for character write area
627 * @flags: return pointer for status flag write area
628 * @size: desired size
629 *
Alan Cox33f0f882006-01-09 20:54:13 -0800630 * Prepare a block of space in the buffer for data. Returns the length
631 * available and buffer pointer to the space which is now allocated and
632 * accounted for as ready for characters. This is used for drivers
633 * that need their own block copy routines into the buffer. There is no
634 * guarantee the buffer is a DMA target!
Alan Coxaf9b8972006-08-27 01:24:01 -0700635 *
636 * Locking: May call functions taking tty->buf.lock
Alan Cox33f0f882006-01-09 20:54:13 -0800637 */
638
Alan Cox37bdfb02008-02-08 04:18:47 -0800639int tty_prepare_flip_string_flags(struct tty_struct *tty,
640 unsigned char **chars, char **flags, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800641{
642 int space = tty_buffer_request_room(tty, size);
Paul Fulghum808249c2006-02-03 03:04:41 -0800643 if (likely(space)) {
644 struct tty_buffer *tb = tty->buf.tail;
645 *chars = tb->char_buf_ptr + tb->used;
646 *flags = tb->flag_buf_ptr + tb->used;
647 tb->used += space;
648 }
Alan Cox33f0f882006-01-09 20:54:13 -0800649 return space;
650}
651
652EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
653
654
655
Alan Coxaf9b8972006-08-27 01:24:01 -0700656/**
657 * tty_set_termios_ldisc - set ldisc field
658 * @tty: tty structure
659 * @num: line discipline number
660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 * This is probably overkill for real world processors but
Alan Cox37bdfb02008-02-08 04:18:47 -0800662 * they are not on hot paths so a little discipline won't do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * any harm.
Alan Coxaf9b8972006-08-27 01:24:01 -0700664 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800665 * Locking: takes termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
669{
Arjan van de Ven5785c952006-09-29 02:00:43 -0700670 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 tty->termios->c_line = num;
Arjan van de Ven5785c952006-09-29 02:00:43 -0700672 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
676 * This guards the refcounted line discipline lists. The lock
677 * must be taken with irqs off because there are hangup path
678 * callers who will do ldisc lookups and cannot sleep.
679 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static DEFINE_SPINLOCK(tty_ldisc_lock);
682static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
Alan Cox37bdfb02008-02-08 04:18:47 -0800683/* Line disc dispatch table */
684static struct tty_ldisc tty_ldiscs[NR_LDISCS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Alan Coxaf9b8972006-08-27 01:24:01 -0700686/**
687 * tty_register_ldisc - install a line discipline
688 * @disc: ldisc number
689 * @new_ldisc: pointer to the ldisc object
690 *
691 * Installs a new line discipline into the kernel. The discipline
692 * is set up as unreferenced and then made available to the kernel
693 * from this point onwards.
694 *
695 * Locking:
696 * takes tty_ldisc_lock to guard against ldisc races
697 */
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
700{
701 unsigned long flags;
702 int ret = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if (disc < N_TTY || disc >= NR_LDISCS)
705 return -EINVAL;
Alan Cox37bdfb02008-02-08 04:18:47 -0800706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alexey Dobriyanbfb07592005-06-23 00:10:32 -0700708 tty_ldiscs[disc] = *new_ldisc;
709 tty_ldiscs[disc].num = disc;
710 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
711 tty_ldiscs[disc].refcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -0800713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return ret;
715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716EXPORT_SYMBOL(tty_register_ldisc);
717
Alan Coxaf9b8972006-08-27 01:24:01 -0700718/**
719 * tty_unregister_ldisc - unload a line discipline
720 * @disc: ldisc number
721 * @new_ldisc: pointer to the ldisc object
722 *
723 * Remove a line discipline from the kernel providing it is not
724 * currently in use.
725 *
726 * Locking:
727 * takes tty_ldisc_lock to guard against ldisc races
728 */
729
Alexey Dobriyanbfb07592005-06-23 00:10:32 -0700730int tty_unregister_ldisc(int disc)
731{
732 unsigned long flags;
733 int ret = 0;
734
735 if (disc < N_TTY || disc >= NR_LDISCS)
736 return -EINVAL;
737
738 spin_lock_irqsave(&tty_ldisc_lock, flags);
739 if (tty_ldiscs[disc].refcount)
740 ret = -EBUSY;
741 else
742 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
743 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
744
745 return ret;
746}
747EXPORT_SYMBOL(tty_unregister_ldisc);
748
Alan Coxaf9b8972006-08-27 01:24:01 -0700749/**
750 * tty_ldisc_get - take a reference to an ldisc
751 * @disc: ldisc number
752 *
753 * Takes a reference to a line discipline. Deals with refcounts and
754 * module locking counts. Returns NULL if the discipline is not available.
755 * Returns a pointer to the discipline and bumps the ref count if it is
756 * available
757 *
758 * Locking:
759 * takes tty_ldisc_lock to guard against ldisc races
760 */
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762struct tty_ldisc *tty_ldisc_get(int disc)
763{
764 unsigned long flags;
765 struct tty_ldisc *ld;
766
767 if (disc < N_TTY || disc >= NR_LDISCS)
768 return NULL;
Alan Cox37bdfb02008-02-08 04:18:47 -0800769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 spin_lock_irqsave(&tty_ldisc_lock, flags);
771
772 ld = &tty_ldiscs[disc];
773 /* Check the entry is defined */
Alan Cox37bdfb02008-02-08 04:18:47 -0800774 if (ld->flags & LDISC_FLAG_DEFINED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /* If the module is being unloaded we can't use it */
776 if (!try_module_get(ld->owner))
Alan Cox37bdfb02008-02-08 04:18:47 -0800777 ld = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 else /* lock it */
779 ld->refcount++;
Alan Cox37bdfb02008-02-08 04:18:47 -0800780 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ld = NULL;
782 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
783 return ld;
784}
785
786EXPORT_SYMBOL_GPL(tty_ldisc_get);
787
Alan Coxaf9b8972006-08-27 01:24:01 -0700788/**
789 * tty_ldisc_put - drop ldisc reference
790 * @disc: ldisc number
791 *
792 * Drop a reference to a line discipline. Manage refcounts and
793 * module usage counts
794 *
795 * Locking:
796 * takes tty_ldisc_lock to guard against ldisc races
797 */
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799void tty_ldisc_put(int disc)
800{
801 struct tty_ldisc *ld;
802 unsigned long flags;
Alan Cox37bdfb02008-02-08 04:18:47 -0800803
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200804 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
Alan Cox37bdfb02008-02-08 04:18:47 -0800805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_lock_irqsave(&tty_ldisc_lock, flags);
807 ld = &tty_ldiscs[disc];
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200808 BUG_ON(ld->refcount == 0);
809 ld->refcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 module_put(ld->owner);
811 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
812}
Alan Cox37bdfb02008-02-08 04:18:47 -0800813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814EXPORT_SYMBOL_GPL(tty_ldisc_put);
815
Alan Coxaf9b8972006-08-27 01:24:01 -0700816/**
817 * tty_ldisc_assign - set ldisc on a tty
818 * @tty: tty to assign
819 * @ld: line discipline
820 *
821 * Install an instance of a line discipline into a tty structure. The
822 * ldisc must have a reference count above zero to ensure it remains/
823 * The tty instance refcount starts at zero.
824 *
825 * Locking:
826 * Caller must hold references
827 */
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
830{
831 tty->ldisc = *ld;
832 tty->ldisc.refcount = 0;
833}
834
835/**
836 * tty_ldisc_try - internal helper
837 * @tty: the tty
838 *
839 * Make a single attempt to grab and bump the refcount on
840 * the tty ldisc. Return 0 on failure or 1 on success. This is
841 * used to implement both the waiting and non waiting versions
842 * of tty_ldisc_ref
Alan Coxaf9b8972006-08-27 01:24:01 -0700843 *
844 * Locking: takes tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
846
847static int tty_ldisc_try(struct tty_struct *tty)
848{
849 unsigned long flags;
850 struct tty_ldisc *ld;
851 int ret = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_lock_irqsave(&tty_ldisc_lock, flags);
854 ld = &tty->ldisc;
Alan Cox37bdfb02008-02-08 04:18:47 -0800855 if (test_bit(TTY_LDISC, &tty->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ld->refcount++;
857 ret = 1;
858 }
859 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
860 return ret;
861}
862
863/**
864 * tty_ldisc_ref_wait - wait for the tty ldisc
865 * @tty: tty device
866 *
Alan Cox37bdfb02008-02-08 04:18:47 -0800867 * Dereference the line discipline for the terminal and take a
868 * reference to it. If the line discipline is in flux then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 * wait patiently until it changes.
870 *
871 * Note: Must not be called from an IRQ/timer context. The caller
872 * must also be careful not to hold other locks that will deadlock
873 * against a discipline change, such as an existing ldisc reference
874 * (which we check for)
Alan Coxaf9b8972006-08-27 01:24:01 -0700875 *
876 * Locking: call functions take tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
880{
881 /* wait_event is a macro */
882 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
Alan Cox37bdfb02008-02-08 04:18:47 -0800883 if (tty->ldisc.refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 printk(KERN_ERR "tty_ldisc_ref_wait\n");
885 return &tty->ldisc;
886}
887
888EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
889
890/**
891 * tty_ldisc_ref - get the tty ldisc
892 * @tty: tty device
893 *
Alan Cox37bdfb02008-02-08 04:18:47 -0800894 * Dereference the line discipline for the terminal and take a
895 * reference to it. If the line discipline is in flux then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * return NULL. Can be called from IRQ and timer functions.
Alan Coxaf9b8972006-08-27 01:24:01 -0700897 *
898 * Locking: called functions take tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
902{
Alan Cox37bdfb02008-02-08 04:18:47 -0800903 if (tty_ldisc_try(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return &tty->ldisc;
905 return NULL;
906}
907
908EXPORT_SYMBOL_GPL(tty_ldisc_ref);
909
910/**
911 * tty_ldisc_deref - free a tty ldisc reference
912 * @ld: reference to free up
913 *
914 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
915 * be called in IRQ context.
Alan Coxaf9b8972006-08-27 01:24:01 -0700916 *
917 * Locking: takes tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920void tty_ldisc_deref(struct tty_ldisc *ld)
921{
922 unsigned long flags;
923
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200924 BUG_ON(ld == NULL);
Alan Cox37bdfb02008-02-08 04:18:47 -0800925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -0800927 if (ld->refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
929 else
930 ld->refcount--;
Alan Cox37bdfb02008-02-08 04:18:47 -0800931 if (ld->refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 wake_up(&tty_ldisc_wait);
933 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
934}
935
936EXPORT_SYMBOL_GPL(tty_ldisc_deref);
937
938/**
939 * tty_ldisc_enable - allow ldisc use
940 * @tty: terminal to activate ldisc on
941 *
942 * Set the TTY_LDISC flag when the line discipline can be called
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200943 * again. Do necessary wakeups for existing sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 *
945 * Note: nobody should set this bit except via this function. Clearing
946 * directly is allowed.
947 */
948
949static void tty_ldisc_enable(struct tty_struct *tty)
950{
951 set_bit(TTY_LDISC, &tty->flags);
952 wake_up(&tty_ldisc_wait);
953}
Alan Cox37bdfb02008-02-08 04:18:47 -0800954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/**
956 * tty_set_ldisc - set line discipline
957 * @tty: the terminal to set
958 * @ldisc: the line discipline
959 *
960 * Set the discipline of a tty line. Must be called from a process
961 * context.
Alan Coxaf9b8972006-08-27 01:24:01 -0700962 *
963 * Locking: takes tty_ldisc_lock.
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800964 * called functions take termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
968{
Jason Baronff55fe22005-09-09 13:01:57 -0700969 int retval = 0;
970 struct tty_ldisc o_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 char buf[64];
972 int work;
973 unsigned long flags;
974 struct tty_ldisc *ld;
Jason Baronff55fe22005-09-09 13:01:57 -0700975 struct tty_struct *o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
978 return -EINVAL;
979
980restart:
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ld = tty_ldisc_get(ldisc);
983 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
984 /* Cyrus Durgin <cider@speakeasy.org> */
985 if (ld == NULL) {
986 request_module("tty-ldisc-%d", ldisc);
987 ld = tty_ldisc_get(ldisc);
988 }
989 if (ld == NULL)
990 return -EINVAL;
991
Alan Cox33f0f882006-01-09 20:54:13 -0800992 /*
Alan Cox33f0f882006-01-09 20:54:13 -0800993 * Problem: What do we do if this blocks ?
994 */
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 tty_wait_until_sent(tty, 0);
997
Jason Baronff55fe22005-09-09 13:01:57 -0700998 if (tty->ldisc.num == ldisc) {
999 tty_ldisc_put(ldisc);
1000 return 0;
1001 }
1002
Paul Fulghumae030e42007-05-09 02:33:38 -07001003 /*
1004 * No more input please, we are switching. The new ldisc
1005 * will update this value in the ldisc open function
1006 */
1007
1008 tty->receive_room = 0;
1009
Jason Baronff55fe22005-09-09 13:01:57 -07001010 o_ldisc = tty->ldisc;
1011 o_tty = tty->link;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /*
1014 * Make sure we don't change while someone holds a
1015 * reference to the line discipline. The TTY_LDISC bit
1016 * prevents anyone taking a reference once it is clear.
1017 * We need the lock to avoid racing reference takers.
1018 */
Jason Baronff55fe22005-09-09 13:01:57 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 spin_lock_irqsave(&tty_ldisc_lock, flags);
Jason Baronff55fe22005-09-09 13:01:57 -07001021 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
Alan Cox37bdfb02008-02-08 04:18:47 -08001022 if (tty->ldisc.refcount) {
Jason Baronff55fe22005-09-09 13:01:57 -07001023 /* Free the new ldisc we grabbed. Must drop the lock
1024 first. */
1025 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1026 tty_ldisc_put(ldisc);
1027 /*
1028 * There are several reasons we may be busy, including
1029 * random momentary I/O traffic. We must therefore
1030 * retry. We could distinguish between blocking ops
Alan Cox37bdfb02008-02-08 04:18:47 -08001031 * and retries if we made tty_ldisc_wait() smarter.
1032 * That is up for discussion.
Jason Baronff55fe22005-09-09 13:01:57 -07001033 */
1034 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
1035 return -ERESTARTSYS;
1036 goto restart;
1037 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001038 if (o_tty && o_tty->ldisc.refcount) {
Jason Baronff55fe22005-09-09 13:01:57 -07001039 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1040 tty_ldisc_put(ldisc);
1041 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
1042 return -ERESTARTSYS;
1043 goto restart;
1044 }
1045 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001046 /*
1047 * If the TTY_LDISC bit is set, then we are racing against
1048 * another ldisc change
1049 */
Jason Baronff55fe22005-09-09 13:01:57 -07001050 if (!test_bit(TTY_LDISC, &tty->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1052 tty_ldisc_put(ldisc);
Jason Baronff55fe22005-09-09 13:01:57 -07001053 ld = tty_ldisc_ref_wait(tty);
1054 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 goto restart;
1056 }
Jason Baronff55fe22005-09-09 13:01:57 -07001057
1058 clear_bit(TTY_LDISC, &tty->flags);
Paul Fulghum817d6d32006-06-28 04:26:47 -07001059 if (o_tty)
Jason Baronff55fe22005-09-09 13:01:57 -07001060 clear_bit(TTY_LDISC, &o_tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Jason Baronff55fe22005-09-09 13:01:57 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /*
1064 * From this point on we know nobody has an ldisc
1065 * usage reference, nor can they obtain one until
1066 * we say so later on.
1067 */
Jason Baronff55fe22005-09-09 13:01:57 -07001068
Alan Cox33f0f882006-01-09 20:54:13 -08001069 work = cancel_delayed_work(&tty->buf.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
Alan Cox33f0f882006-01-09 20:54:13 -08001071 * Wait for ->hangup_work and ->buf.work handlers to terminate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 flush_scheduled_work();
1074 /* Shutdown the current discipline. */
1075 if (tty->ldisc.close)
1076 (tty->ldisc.close)(tty);
1077
1078 /* Now set up the new line discipline. */
1079 tty_ldisc_assign(tty, ld);
1080 tty_set_termios_ldisc(tty, ldisc);
1081 if (tty->ldisc.open)
1082 retval = (tty->ldisc.open)(tty);
1083 if (retval < 0) {
1084 tty_ldisc_put(ldisc);
1085 /* There is an outstanding reference here so this is safe */
1086 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
1087 tty_set_termios_ldisc(tty, tty->ldisc.num);
1088 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
1089 tty_ldisc_put(o_ldisc.num);
1090 /* This driver is always present */
1091 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
1092 tty_set_termios_ldisc(tty, N_TTY);
1093 if (tty->ldisc.open) {
1094 int r = tty->ldisc.open(tty);
1095
1096 if (r < 0)
1097 panic("Couldn't open N_TTY ldisc for "
1098 "%s --- error %d.",
1099 tty_name(tty, buf), r);
1100 }
1101 }
1102 }
1103 /* At this point we hold a reference to the new ldisc and a
1104 a reference to the old ldisc. If we ended up flipping back
1105 to the existing ldisc we have two references to it */
Alan Cox37bdfb02008-02-08 04:18:47 -08001106
Alan Coxf34d7a52008-04-30 00:54:13 -07001107 if (tty->ldisc.num != o_ldisc.num && tty->ops->set_ldisc)
1108 tty->ops->set_ldisc(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 tty_ldisc_put(o_ldisc.num);
Alan Cox37bdfb02008-02-08 04:18:47 -08001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /*
1113 * Allow ldisc referencing to occur as soon as the driver
1114 * ldisc callback completes.
1115 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 tty_ldisc_enable(tty);
Jason Baronff55fe22005-09-09 13:01:57 -07001118 if (o_tty)
1119 tty_ldisc_enable(o_tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /* Restart it in case no characters kick it off. Safe if
1122 already running */
Jason Baronff55fe22005-09-09 13:01:57 -07001123 if (work)
Alan Cox33f0f882006-01-09 20:54:13 -08001124 schedule_delayed_work(&tty->buf.work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return retval;
1126}
1127
Alan Coxaf9b8972006-08-27 01:24:01 -07001128/**
1129 * get_tty_driver - find device of a tty
1130 * @dev_t: device identifier
1131 * @index: returns the index of the tty
1132 *
1133 * This routine returns a tty driver structure, given a device number
1134 * and also passes back the index number.
1135 *
1136 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139static struct tty_driver *get_tty_driver(dev_t device, int *index)
1140{
1141 struct tty_driver *p;
1142
1143 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1144 dev_t base = MKDEV(p->major, p->minor_start);
1145 if (device < base || device >= base + p->num)
1146 continue;
1147 *index = device - base;
1148 return p;
1149 }
1150 return NULL;
1151}
1152
Jason Wesself2d937f2008-04-17 20:05:37 +02001153#ifdef CONFIG_CONSOLE_POLL
1154
1155/**
1156 * tty_find_polling_driver - find device of a polled tty
1157 * @name: name string to match
1158 * @line: pointer to resulting tty line nr
1159 *
1160 * This routine returns a tty driver structure, given a name
1161 * and the condition that the tty driver is capable of polled
1162 * operation.
1163 */
1164struct tty_driver *tty_find_polling_driver(char *name, int *line)
1165{
1166 struct tty_driver *p, *res = NULL;
1167 int tty_line = 0;
1168 char *str;
1169
1170 mutex_lock(&tty_mutex);
1171 /* Search through the tty devices to look for a match */
1172 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1173 str = name + strlen(p->name);
1174 tty_line = simple_strtoul(str, &str, 10);
1175 if (*str == ',')
1176 str++;
1177 if (*str == '\0')
Harvey Harrison8da56302008-04-28 14:13:20 -07001178 str = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +02001179
Alan Coxf34d7a52008-04-30 00:54:13 -07001180 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
1181 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
Jason Wesself2d937f2008-04-17 20:05:37 +02001182 res = p;
1183 *line = tty_line;
1184 break;
1185 }
1186 }
1187 mutex_unlock(&tty_mutex);
1188
1189 return res;
1190}
1191EXPORT_SYMBOL_GPL(tty_find_polling_driver);
1192#endif
1193
Alan Coxaf9b8972006-08-27 01:24:01 -07001194/**
1195 * tty_check_change - check for POSIX terminal changes
1196 * @tty: tty to check
1197 *
1198 * If we try to write to, or set the state of, a terminal and we're
1199 * not in the foreground, send a SIGTTOU. If the signal is blocked or
1200 * ignored, go ahead and perform the operation. (POSIX 7.2)
1201 *
Alan Cox978e5952008-04-30 00:53:59 -07001202 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001204
Alan Cox37bdfb02008-02-08 04:18:47 -08001205int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Alan Cox47f86832008-04-30 00:53:30 -07001207 unsigned long flags;
1208 int ret = 0;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (current->signal->tty != tty)
1211 return 0;
Alan Cox47f86832008-04-30 00:53:30 -07001212
1213 spin_lock_irqsave(&tty->ctrl_lock, flags);
1214
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001215 if (!tty->pgrp) {
1216 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Alan Cox47f86832008-04-30 00:53:30 -07001217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001219 if (task_pgrp(current) == tty->pgrp)
Alan Cox47f86832008-04-30 00:53:30 -07001220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -07001222 goto out;
1223 if (is_current_pgrp_orphaned()) {
1224 ret = -EIO;
1225 goto out;
1226 }
Oleg Nesterov040b6362007-06-01 00:46:53 -07001227 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
1228 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -07001229 ret = -ERESTARTSYS;
1230out:
1231 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235EXPORT_SYMBOL(tty_check_change);
1236
Alan Cox37bdfb02008-02-08 04:18:47 -08001237static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 size_t count, loff_t *ppos)
1239{
1240 return 0;
1241}
1242
Alan Cox37bdfb02008-02-08 04:18:47 -08001243static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 size_t count, loff_t *ppos)
1245{
1246 return -EIO;
1247}
1248
1249/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -08001250static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
1252 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
1253}
1254
Alan Cox04f378b2008-04-30 00:53:29 -07001255static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
1256 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001257{
1258 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1259}
1260
Alan Cox37bdfb02008-02-08 04:18:47 -08001261static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001262 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1265}
1266
Arjan van de Ven62322d22006-07-03 00:24:21 -07001267static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 .llseek = no_llseek,
1269 .read = tty_read,
1270 .write = tty_write,
1271 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001272 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001273 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 .open = tty_open,
1275 .release = tty_release,
1276 .fasync = tty_fasync,
1277};
1278
1279#ifdef CONFIG_UNIX98_PTYS
Arjan van de Ven62322d22006-07-03 00:24:21 -07001280static const struct file_operations ptmx_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 .llseek = no_llseek,
1282 .read = tty_read,
1283 .write = tty_write,
1284 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001285 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001286 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 .open = ptmx_open,
1288 .release = tty_release,
1289 .fasync = tty_fasync,
1290};
1291#endif
1292
Arjan van de Ven62322d22006-07-03 00:24:21 -07001293static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .llseek = no_llseek,
1295 .read = tty_read,
1296 .write = redirected_tty_write,
1297 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001298 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001299 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 .open = tty_open,
1301 .release = tty_release,
1302 .fasync = tty_fasync,
1303};
1304
Arjan van de Ven62322d22006-07-03 00:24:21 -07001305static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 .llseek = no_llseek,
1307 .read = hung_up_tty_read,
1308 .write = hung_up_tty_write,
1309 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001310 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001311 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 .release = tty_release,
1313};
1314
1315static DEFINE_SPINLOCK(redirect_lock);
1316static struct file *redirect;
1317
1318/**
1319 * tty_wakeup - request more data
1320 * @tty: terminal
1321 *
1322 * Internal and external helper for wakeups of tty. This function
1323 * informs the line discipline if present that the driver is ready
1324 * to receive more output data.
1325 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327void tty_wakeup(struct tty_struct *tty)
1328{
1329 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
1332 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001333 if (ld) {
1334 if (ld->write_wakeup)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ld->write_wakeup(tty);
1336 tty_ldisc_deref(ld);
1337 }
1338 }
1339 wake_up_interruptible(&tty->write_wait);
1340}
1341
1342EXPORT_SYMBOL_GPL(tty_wakeup);
1343
1344/**
1345 * tty_ldisc_flush - flush line discipline queue
1346 * @tty: tty
1347 *
1348 * Flush the line discipline queue (if any) for this tty. If there
1349 * is no line discipline active this is a no-op.
1350 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352void tty_ldisc_flush(struct tty_struct *tty)
1353{
1354 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001355 if (ld) {
1356 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ld->flush_buffer(tty);
1358 tty_ldisc_deref(ld);
1359 }
Paul Fulghumc5c34d42007-05-12 10:36:55 -07001360 tty_buffer_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363EXPORT_SYMBOL_GPL(tty_ldisc_flush);
Alan Coxedc6afc2006-12-08 02:38:44 -08001364
1365/**
1366 * tty_reset_termios - reset terminal state
1367 * @tty: tty to reset
1368 *
1369 * Restore a terminal to the driver default state
1370 */
1371
1372static void tty_reset_termios(struct tty_struct *tty)
1373{
1374 mutex_lock(&tty->termios_mutex);
1375 *tty->termios = tty->driver->init_termios;
1376 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1377 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1378 mutex_unlock(&tty->termios_mutex);
1379}
Alan Cox37bdfb02008-02-08 04:18:47 -08001380
Alan Coxaf9b8972006-08-27 01:24:01 -07001381/**
1382 * do_tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +00001383 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001384 *
1385 * This can be called by the "eventd" kernel thread. That is process
1386 * synchronous but doesn't hold any locks, so we need to make sure we
1387 * have the appropriate locks for what we're doing.
1388 *
1389 * The hangup event clears any pending redirections onto the hung up
1390 * device. It ensures future writes will error and it does the needed
1391 * line discipline hangup and signal delivery. The tty object itself
1392 * remains intact.
1393 *
1394 * Locking:
1395 * BKL
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001396 * redirect lock for undoing redirection
1397 * file list lock for manipulating list of ttys
1398 * tty_ldisc_lock from called functions
1399 * termios_mutex resetting termios data
1400 * tasklist_lock to walk task list for hangup event
1401 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
David Howells65f27f32006-11-22 14:55:48 +00001403static void do_tty_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
David Howells65f27f32006-11-22 14:55:48 +00001405 struct tty_struct *tty =
1406 container_of(work, struct tty_struct, hangup_work);
Alan Cox37bdfb02008-02-08 04:18:47 -08001407 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct file *filp, *f = NULL;
1409 struct task_struct *p;
1410 struct tty_ldisc *ld;
1411 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -07001412 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (!tty)
1415 return;
1416
1417 /* inuse_filps is protected by the single kernel lock */
1418 lock_kernel();
1419
1420 spin_lock(&redirect_lock);
1421 if (redirect && redirect->private_data == tty) {
1422 f = redirect;
1423 redirect = NULL;
1424 }
1425 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -08001426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 check_tty_count(tty, "do_tty_hangup");
1428 file_list_lock();
1429 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Eric Dumazet2f512012005-10-30 15:02:16 -08001430 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (filp->f_op->write == redirected_tty_write)
1432 cons_filp = filp;
1433 if (filp->f_op->write != tty_write)
1434 continue;
1435 closecount++;
1436 tty_fasync(-1, filp, 0); /* can't block */
1437 filp->f_op = &hung_up_tty_fops;
1438 }
1439 file_list_unlock();
Alan Cox37bdfb02008-02-08 04:18:47 -08001440 /*
1441 * FIXME! What are the locking issues here? This may me overdoing
1442 * things... This question is especially important now that we've
1443 * removed the irqlock.
1444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001446 if (ld != NULL) {
1447 /* We may have no line discipline at this point */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (ld->flush_buffer)
1449 ld->flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001450 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
1452 ld->write_wakeup)
1453 ld->write_wakeup(tty);
1454 if (ld->hangup)
1455 ld->hangup(tty);
1456 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001457 /*
1458 * FIXME: Once we trust the LDISC code better we can wait here for
1459 * ldisc completion and fix the driver call race
1460 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 wake_up_interruptible(&tty->write_wait);
1462 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /*
1464 * Shutdown the current line discipline, and reset it to
1465 * N_TTY.
1466 */
1467 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Coxedc6afc2006-12-08 02:38:44 -08001468 tty_reset_termios(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* Defer ldisc switch */
1470 /* tty_deferred_ldisc_switch(N_TTY);
Alan Cox37bdfb02008-02-08 04:18:47 -08001471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 This should get done automatically when the port closes and
1473 tty_release is called */
Alan Cox37bdfb02008-02-08 04:18:47 -08001474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001476 if (tty->session) {
1477 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001478 spin_lock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (p->signal->tty == tty)
1480 p->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001481 if (!p->signal->leader) {
1482 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 continue;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001484 }
1485 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1486 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001487 put_pid(p->signal->tty_old_pgrp); /* A noop */
Alan Cox47f86832008-04-30 00:53:30 -07001488 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001489 if (tty->pgrp)
1490 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07001491 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001492 spin_unlock_irq(&p->sighand->siglock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001493 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 read_unlock(&tasklist_lock);
1496
Alan Cox47f86832008-04-30 00:53:30 -07001497 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 tty->flags = 0;
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06001499 put_pid(tty->session);
1500 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001501 tty->session = NULL;
1502 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 tty->ctrl_status = 0;
Alan Cox47f86832008-04-30 00:53:30 -07001504 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001507 * If one of the devices matches a console pointer, we
1508 * cannot just call hangup() because that will cause
1509 * tty->count and state->count to go out of sync.
1510 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001513 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -07001515 tty->ops->close(tty, cons_filp);
1516 } else if (tty->ops->hangup)
1517 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001518 /*
1519 * We don't want to have driver/ldisc interactions beyond
1520 * the ones we did here. The driver layer expects no
1521 * calls after ->hangup() from the ldisc side. However we
1522 * can't yet guarantee all that.
1523 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 set_bit(TTY_HUPPED, &tty->flags);
1525 if (ld) {
1526 tty_ldisc_enable(tty);
1527 tty_ldisc_deref(ld);
1528 }
1529 unlock_kernel();
1530 if (f)
1531 fput(f);
1532}
1533
Alan Coxaf9b8972006-08-27 01:24:01 -07001534/**
1535 * tty_hangup - trigger a hangup event
1536 * @tty: tty to hangup
1537 *
1538 * A carrier loss (virtual or otherwise) has occurred on this like
1539 * schedule a hangup sequence to run after this event.
1540 */
1541
Alan Cox37bdfb02008-02-08 04:18:47 -08001542void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544#ifdef TTY_DEBUG_HANGUP
1545 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1547#endif
1548 schedule_work(&tty->hangup_work);
1549}
1550
1551EXPORT_SYMBOL(tty_hangup);
1552
Alan Coxaf9b8972006-08-27 01:24:01 -07001553/**
1554 * tty_vhangup - process vhangup
1555 * @tty: tty to hangup
1556 *
1557 * The user has asked via system call for the terminal to be hung up.
1558 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001559 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -07001560 */
1561
Alan Cox37bdfb02008-02-08 04:18:47 -08001562void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564#ifdef TTY_DEBUG_HANGUP
1565 char buf[64];
1566
1567 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1568#endif
David Howells65f27f32006-11-22 14:55:48 +00001569 do_tty_hangup(&tty->hangup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
Alan Cox37bdfb02008-02-08 04:18:47 -08001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572EXPORT_SYMBOL(tty_vhangup);
1573
Alan Coxaf9b8972006-08-27 01:24:01 -07001574/**
1575 * tty_hung_up_p - was tty hung up
1576 * @filp: file pointer of tty
1577 *
1578 * Return true if the tty has been subject to a vhangup or a carrier
1579 * loss
1580 */
1581
Alan Cox37bdfb02008-02-08 04:18:47 -08001582int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 return (filp->f_op == &hung_up_tty_fops);
1585}
1586
1587EXPORT_SYMBOL(tty_hung_up_p);
1588
Miloslav Trmac522ed772007-07-15 23:40:56 -07001589/**
Alan Cox37bdfb02008-02-08 04:18:47 -08001590 * is_tty - checker whether file is a TTY
1591 * @filp: file handle that may be a tty
1592 *
1593 * Check if the file handle is a tty handle.
Miloslav Trmac522ed772007-07-15 23:40:56 -07001594 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001595
Miloslav Trmac522ed772007-07-15 23:40:56 -07001596int is_tty(struct file *filp)
1597{
1598 return filp->f_op->read == tty_read
1599 || filp->f_op->read == hung_up_tty_read;
1600}
1601
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001602static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001603{
1604 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001605 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001606 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001607 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001608}
1609
Alan Coxaf9b8972006-08-27 01:24:01 -07001610/**
1611 * disassociate_ctty - disconnect controlling tty
1612 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001614 * This function is typically called only by the session leader, when
1615 * it wants to disassociate itself from its controlling tty.
1616 *
1617 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1619 * (2) Clears the tty from being controlling the session
1620 * (3) Clears the controlling tty for all processes in the
1621 * session group.
1622 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001623 * The argument on_exit is set to 1 if called when a process is
1624 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1625 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001626 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001627 * BKL is taken for hysterical raisins
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001628 * tty_mutex is taken to protect tty
1629 * ->siglock is taken to protect ->signal/->sighand
1630 * tasklist_lock is taken to walk process list for sessions
1631 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634void disassociate_ctty(int on_exit)
1635{
1636 struct tty_struct *tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001637 struct pid *tty_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Ingo Molnar70522e12006-03-23 03:00:31 -08001640 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001641 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001643 tty_pgrp = get_pid(tty->pgrp);
Ingo Molnar70522e12006-03-23 03:00:31 -08001644 mutex_unlock(&tty_mutex);
Alan Cox04f378b2008-04-30 00:53:29 -07001645 lock_kernel();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001646 /* XXX: here we race, there is nothing protecting tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1648 tty_vhangup(tty);
Alan Cox04f378b2008-04-30 00:53:29 -07001649 unlock_kernel();
Eric W. Biederman680a9672007-02-12 00:52:52 -08001650 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001651 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -08001652 spin_lock_irq(&current->sighand->siglock);
1653 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001654 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -08001655 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001656 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001657 kill_pgrp(old_pgrp, SIGHUP, on_exit);
1658 kill_pgrp(old_pgrp, SIGCONT, on_exit);
1659 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001661 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return;
1663 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001664 if (tty_pgrp) {
1665 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (!on_exit)
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001667 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
1668 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001671 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07001672 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -08001673 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001674 spin_unlock_irq(&current->sighand->siglock);
1675
1676 mutex_lock(&tty_mutex);
1677 /* It is possible that do_tty_hangup has free'd this tty */
1678 tty = get_current_tty();
1679 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07001680 unsigned long flags;
1681 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001682 put_pid(tty->session);
1683 put_pid(tty->pgrp);
1684 tty->session = NULL;
1685 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -07001686 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001687 } else {
1688#ifdef TTY_DEBUG_HANGUP
1689 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1690 " = NULL", tty);
1691#endif
1692 }
1693 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 /* Now clear signal->tty under the lock */
1696 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001697 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001701/**
1702 *
1703 * no_tty - Ensure the current process does not have a controlling tty
1704 */
1705void no_tty(void)
1706{
1707 struct task_struct *tsk = current;
Alan Cox04f378b2008-04-30 00:53:29 -07001708 lock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001709 if (tsk->signal->leader)
1710 disassociate_ctty(0);
Alan Cox04f378b2008-04-30 00:53:29 -07001711 unlock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001712 proc_clear_tty(tsk);
1713}
1714
Alan Coxaf9b8972006-08-27 01:24:01 -07001715
1716/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001717 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -07001718 * @tty: tty to stop
1719 *
1720 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001721 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -07001722 * on an already stopped device and will not re-call the driver
1723 * method.
1724 *
1725 * This functionality is used by both the line disciplines for
1726 * halting incoming flow and by the driver. It may therefore be
1727 * called from any context, may be under the tty atomic_write_lock
1728 * but not always.
1729 *
1730 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -07001731 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -07001732 */
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734void stop_tty(struct tty_struct *tty)
1735{
Alan Cox04f378b2008-04-30 00:53:29 -07001736 unsigned long flags;
1737 spin_lock_irqsave(&tty->ctrl_lock, flags);
1738 if (tty->stopped) {
1739 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return;
Alan Cox04f378b2008-04-30 00:53:29 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 tty->stopped = 1;
1743 if (tty->link && tty->link->packet) {
1744 tty->ctrl_status &= ~TIOCPKT_START;
1745 tty->ctrl_status |= TIOCPKT_STOP;
1746 wake_up_interruptible(&tty->link->read_wait);
1747 }
Alan Cox04f378b2008-04-30 00:53:29 -07001748 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -07001749 if (tty->ops->stop)
1750 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
1753EXPORT_SYMBOL(stop_tty);
1754
Alan Coxaf9b8972006-08-27 01:24:01 -07001755/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001756 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -07001757 * @tty: tty to start
1758 *
1759 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001760 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -07001761 * is the tty was previous stopped and is being started then the
1762 * driver start method is invoked and the line discipline woken.
1763 *
1764 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -07001765 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001766 */
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768void start_tty(struct tty_struct *tty)
1769{
Alan Cox04f378b2008-04-30 00:53:29 -07001770 unsigned long flags;
1771 spin_lock_irqsave(&tty->ctrl_lock, flags);
1772 if (!tty->stopped || tty->flow_stopped) {
1773 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return;
Alan Cox04f378b2008-04-30 00:53:29 -07001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 tty->stopped = 0;
1777 if (tty->link && tty->link->packet) {
1778 tty->ctrl_status &= ~TIOCPKT_STOP;
1779 tty->ctrl_status |= TIOCPKT_START;
1780 wake_up_interruptible(&tty->link->read_wait);
1781 }
Alan Cox04f378b2008-04-30 00:53:29 -07001782 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -07001783 if (tty->ops->start)
1784 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* If we have a running line discipline it may need kicking */
1786 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789EXPORT_SYMBOL(start_tty);
1790
Alan Coxaf9b8972006-08-27 01:24:01 -07001791/**
1792 * tty_read - read method for tty device files
1793 * @file: pointer to tty file
1794 * @buf: user buffer
1795 * @count: size of user buffer
1796 * @ppos: unused
1797 *
1798 * Perform the read system call function on this terminal device. Checks
1799 * for hung up devices before calling the line discipline method.
1800 *
1801 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -07001802 * Locks the line discipline internally while needed. Multiple
1803 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -07001804 */
1805
Alan Cox37bdfb02008-02-08 04:18:47 -08001806static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 loff_t *ppos)
1808{
1809 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08001810 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct inode *inode;
1812 struct tty_ldisc *ld;
1813
1814 tty = (struct tty_struct *)file->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001815 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (tty_paranoia_check(tty, inode, "tty_read"))
1817 return -EIO;
1818 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1819 return -EIO;
1820
1821 /* We want to wait for the line discipline to sort out in this
1822 situation */
1823 ld = tty_ldisc_ref_wait(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (ld->read)
Alan Cox37bdfb02008-02-08 04:18:47 -08001825 i = (ld->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 else
1827 i = -EIO;
1828 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (i > 0)
1830 inode->i_atime = current_fs_time(inode->i_sb);
1831 return i;
1832}
1833
Alan Cox9c1729d2007-07-15 23:39:43 -07001834void tty_write_unlock(struct tty_struct *tty)
1835{
1836 mutex_unlock(&tty->atomic_write_lock);
1837 wake_up_interruptible(&tty->write_wait);
1838}
1839
1840int tty_write_lock(struct tty_struct *tty, int ndelay)
1841{
1842 if (!mutex_trylock(&tty->atomic_write_lock)) {
1843 if (ndelay)
1844 return -EAGAIN;
1845 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1846 return -ERESTARTSYS;
1847 }
1848 return 0;
1849}
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/*
1852 * Split writes up in sane blocksizes to avoid
1853 * denial-of-service type attacks
1854 */
1855static inline ssize_t do_tty_write(
1856 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1857 struct tty_struct *tty,
1858 struct file *file,
1859 const char __user *buf,
1860 size_t count)
1861{
Alan Cox9c1729d2007-07-15 23:39:43 -07001862 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001864
Alan Cox9c1729d2007-07-15 23:39:43 -07001865 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1866 if (ret < 0)
1867 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * We chunk up writes into a temporary buffer. This
1871 * simplifies low-level drivers immensely, since they
1872 * don't have locking issues and user mode accesses.
1873 *
1874 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1875 * big chunk-size..
1876 *
1877 * The default chunk-size is 2kB, because the NTTY
1878 * layer has problems with bigger chunks. It will
1879 * claim to be able to handle more characters than
1880 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001881 *
1882 * FIXME: This can probably go away now except that 64K chunks
1883 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
1885 chunk = 2048;
1886 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1887 chunk = 65536;
1888 if (count < chunk)
1889 chunk = count;
1890
Ingo Molnar70522e12006-03-23 03:00:31 -08001891 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (tty->write_cnt < chunk) {
1893 unsigned char *buf;
1894
1895 if (chunk < 1024)
1896 chunk = 1024;
1897
1898 buf = kmalloc(chunk, GFP_KERNEL);
1899 if (!buf) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001900 ret = -ENOMEM;
1901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 kfree(tty->write_buf);
1904 tty->write_cnt = chunk;
1905 tty->write_buf = buf;
1906 }
1907
1908 /* Do the write .. */
1909 for (;;) {
1910 size_t size = count;
1911 if (size > chunk)
1912 size = chunk;
1913 ret = -EFAULT;
1914 if (copy_from_user(tty->write_buf, buf, size))
1915 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (ret <= 0)
1918 break;
1919 written += ret;
1920 buf += ret;
1921 count -= ret;
1922 if (!count)
1923 break;
1924 ret = -ERESTARTSYS;
1925 if (signal_pending(current))
1926 break;
1927 cond_resched();
1928 }
1929 if (written) {
Josef Sipeka7113a92006-12-08 02:36:55 -08001930 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 inode->i_mtime = current_fs_time(inode->i_sb);
1932 ret = written;
1933 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001934out:
1935 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return ret;
1937}
1938
1939
Alan Coxaf9b8972006-08-27 01:24:01 -07001940/**
1941 * tty_write - write method for tty device file
1942 * @file: tty file pointer
1943 * @buf: user data to write
1944 * @count: bytes to write
1945 * @ppos: unused
1946 *
1947 * Write data to a tty device via the line discipline.
1948 *
1949 * Locking:
1950 * Locks the line discipline as required
1951 * Writes to the tty driver are serialized by the atomic_write_lock
1952 * and are then processed in chunks to the device. The line discipline
1953 * write method will not be involked in parallel for each device
1954 * The line discipline write method is called under the big
1955 * kernel lock for historical reasons. New code should not rely on this.
1956 */
1957
Alan Cox37bdfb02008-02-08 04:18:47 -08001958static ssize_t tty_write(struct file *file, const char __user *buf,
1959 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
Alan Cox37bdfb02008-02-08 04:18:47 -08001961 struct tty_struct *tty;
Josef Sipeka7113a92006-12-08 02:36:55 -08001962 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ssize_t ret;
1964 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 tty = (struct tty_struct *)file->private_data;
1967 if (tty_paranoia_check(tty, inode, "tty_write"))
1968 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001969 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001970 (test_bit(TTY_IO_ERROR, &tty->flags)))
1971 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001972 /* Short term debug to catch buggy drivers */
1973 if (tty->ops->write_room == NULL)
1974 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1975 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001976 ld = tty_ldisc_ref_wait(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!ld->write)
1978 ret = -EIO;
1979 else
1980 ret = do_tty_write(ld->write, tty, file, buf, count);
1981 tty_ldisc_deref(ld);
1982 return ret;
1983}
1984
Alan Cox37bdfb02008-02-08 04:18:47 -08001985ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1986 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 struct file *p = NULL;
1989
1990 spin_lock(&redirect_lock);
1991 if (redirect) {
1992 get_file(redirect);
1993 p = redirect;
1994 }
1995 spin_unlock(&redirect_lock);
1996
1997 if (p) {
1998 ssize_t res;
1999 res = vfs_write(p, buf, count, &p->f_pos);
2000 fput(p);
2001 return res;
2002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return tty_write(file, buf, count, ppos);
2004}
2005
2006static char ptychar[] = "pqrstuvwxyzabcde";
2007
Alan Coxaf9b8972006-08-27 01:24:01 -07002008/**
2009 * pty_line_name - generate name for a pty
2010 * @driver: the tty driver in use
2011 * @index: the minor number
2012 * @p: output buffer of at least 6 bytes
2013 *
2014 * Generate a name from a driver reference and write it to the output
2015 * buffer.
2016 *
2017 * Locking: None
2018 */
2019static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 int i = index + driver->name_base;
2022 /* ->name is initialized to "ttyp", but "tty" is expected */
2023 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08002024 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
2025 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Alan Coxaf9b8972006-08-27 01:24:01 -07002028/**
2029 * pty_line_name - generate name for a tty
2030 * @driver: the tty driver in use
2031 * @index: the minor number
2032 * @p: output buffer of at least 7 bytes
2033 *
2034 * Generate a name from a driver reference and write it to the output
2035 * buffer.
2036 *
2037 * Locking: None
2038 */
2039static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 sprintf(p, "%s%d", driver->name, index + driver->name_base);
2042}
2043
Alan Coxaf9b8972006-08-27 01:24:01 -07002044/**
2045 * init_dev - initialise a tty device
2046 * @driver: tty driver we are opening a device on
2047 * @idx: device index
2048 * @tty: returned tty structure
2049 *
2050 * Prepare a tty device. This may not be a "new" clean device but
2051 * could also be an active device. The pty drivers require special
2052 * handling because of this.
2053 *
2054 * Locking:
2055 * The function is called under the tty_mutex, which
2056 * protects us from the tty struct or driver itself going away.
2057 *
2058 * On exit the tty device has the line discipline attached and
2059 * a reference count of 1. If a pair was created for pty/tty use
2060 * and the other was a pty master then it too has a reference count of 1.
2061 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08002063 * failed open. The new code protects the open with a mutex, so it's
2064 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 * relaxed for the (most common) case of reopening a tty.
2066 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068static int init_dev(struct tty_driver *driver, int idx,
2069 struct tty_struct **ret_tty)
2070{
2071 struct tty_struct *tty, *o_tty;
Alan Coxedc6afc2006-12-08 02:38:44 -08002072 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
2073 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
Alan Coxaf9b8972006-08-27 01:24:01 -07002074 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /* check whether we're reopening an existing tty */
2077 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2078 tty = devpts_get_tty(idx);
Aristeu Sergio Rozanski Filho5a39e8c2007-02-28 20:13:53 -08002079 /*
2080 * If we don't have a tty here on a slave open, it's because
2081 * the master already started the close process and there's
2082 * no relation between devpts file and tty anymore.
2083 */
2084 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
2085 retval = -EIO;
2086 goto end_init;
2087 }
2088 /*
2089 * It's safe from now on because init_dev() is called with
2090 * tty_mutex held and release_dev() won't change tty->count
2091 * or tty->flags without having to grab tty_mutex
2092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (tty && driver->subtype == PTY_TYPE_MASTER)
2094 tty = tty->link;
2095 } else {
2096 tty = driver->ttys[idx];
2097 }
2098 if (tty) goto fast_track;
2099
2100 /*
2101 * First time open is complex, especially for PTY devices.
2102 * This code guarantees that either everything succeeds and the
2103 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08002104 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 * and locked termios may be retained.)
2106 */
2107
2108 if (!try_module_get(driver->owner)) {
2109 retval = -ENODEV;
2110 goto end_init;
2111 }
2112
2113 o_tty = NULL;
2114 tp = o_tp = NULL;
2115 ltp = o_ltp = NULL;
2116
2117 tty = alloc_tty_struct();
Alan Cox37bdfb02008-02-08 04:18:47 -08002118 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 goto fail_no_mem;
2120 initialize_tty_struct(tty);
2121 tty->driver = driver;
Alan Coxf34d7a52008-04-30 00:54:13 -07002122 tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 tty->index = idx;
2124 tty_line_name(driver, idx, tty->name);
2125
2126 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2127 tp_loc = &tty->termios;
2128 ltp_loc = &tty->termios_locked;
2129 } else {
2130 tp_loc = &driver->termios[idx];
2131 ltp_loc = &driver->termios_locked[idx];
2132 }
2133
2134 if (!*tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02002135 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (!tp)
2137 goto free_mem_out;
2138 *tp = driver->init_termios;
2139 }
2140
2141 if (!*ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07002142 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!ltp)
2144 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146
2147 if (driver->type == TTY_DRIVER_TYPE_PTY) {
2148 o_tty = alloc_tty_struct();
2149 if (!o_tty)
2150 goto free_mem_out;
2151 initialize_tty_struct(o_tty);
2152 o_tty->driver = driver->other;
Alan Coxf34d7a52008-04-30 00:54:13 -07002153 o_tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 o_tty->index = idx;
2155 tty_line_name(driver->other, idx, o_tty->name);
2156
2157 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2158 o_tp_loc = &o_tty->termios;
2159 o_ltp_loc = &o_tty->termios_locked;
2160 } else {
2161 o_tp_loc = &driver->other->termios[idx];
2162 o_ltp_loc = &driver->other->termios_locked[idx];
2163 }
2164
2165 if (!*o_tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02002166 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (!o_tp)
2168 goto free_mem_out;
2169 *o_tp = driver->other->init_termios;
2170 }
2171
2172 if (!*o_ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07002173 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!o_ltp)
2175 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
2178 /*
2179 * Everything allocated ... set up the o_tty structure.
2180 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002181 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 driver->other->ttys[idx] = o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!*o_tp_loc)
2184 *o_tp_loc = o_tp;
2185 if (!*o_ltp_loc)
2186 *o_ltp_loc = o_ltp;
2187 o_tty->termios = *o_tp_loc;
2188 o_tty->termios_locked = *o_ltp_loc;
2189 driver->other->refcount++;
2190 if (driver->subtype == PTY_TYPE_MASTER)
2191 o_tty->count++;
2192
2193 /* Establish the links in both directions */
2194 tty->link = o_tty;
2195 o_tty->link = tty;
2196 }
2197
Alan Cox37bdfb02008-02-08 04:18:47 -08002198 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 * All structures have been allocated, so now we install them.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002200 * Failures after this point use release_tty to clean up, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 * there's no need to null out the local pointers.
2202 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002203 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 driver->ttys[idx] = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (!*tp_loc)
2207 *tp_loc = tp;
2208 if (!*ltp_loc)
2209 *ltp_loc = ltp;
2210 tty->termios = *tp_loc;
2211 tty->termios_locked = *ltp_loc;
Alan Coxedc6afc2006-12-08 02:38:44 -08002212 /* Compatibility until drivers always set this */
2213 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
2214 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 driver->refcount++;
2216 tty->count++;
2217
Alan Cox37bdfb02008-02-08 04:18:47 -08002218 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002220 * If we fail here just call release_tty to clean up. No need
2221 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
2223
2224 if (tty->ldisc.open) {
2225 retval = (tty->ldisc.open)(tty);
2226 if (retval)
2227 goto release_mem_out;
2228 }
2229 if (o_tty && o_tty->ldisc.open) {
2230 retval = (o_tty->ldisc.open)(o_tty);
2231 if (retval) {
2232 if (tty->ldisc.close)
2233 (tty->ldisc.close)(tty);
2234 goto release_mem_out;
2235 }
2236 tty_ldisc_enable(o_tty);
2237 }
2238 tty_ldisc_enable(tty);
2239 goto success;
2240
2241 /*
2242 * This fast open can be used if the tty is already open.
2243 * No memory is allocated, and the only failures are from
2244 * attempting to open a closing tty or attempting multiple
2245 * opens on a pty master.
2246 */
2247fast_track:
2248 if (test_bit(TTY_CLOSING, &tty->flags)) {
2249 retval = -EIO;
2250 goto end_init;
2251 }
2252 if (driver->type == TTY_DRIVER_TYPE_PTY &&
2253 driver->subtype == PTY_TYPE_MASTER) {
2254 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08002255 * special case for PTY masters: only one open permitted,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * and the slave side open count is incremented as well.
2257 */
2258 if (tty->count) {
2259 retval = -EIO;
2260 goto end_init;
2261 }
2262 tty->link->count++;
2263 }
2264 tty->count++;
2265 tty->driver = driver; /* N.B. why do this every time?? */
2266
2267 /* FIXME */
Alan Cox37bdfb02008-02-08 04:18:47 -08002268 if (!test_bit(TTY_LDISC, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 printk(KERN_ERR "init_dev but no ldisc\n");
2270success:
2271 *ret_tty = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08002272
Ingo Molnar70522e12006-03-23 03:00:31 -08002273 /* All paths come through here to release the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274end_init:
2275 return retval;
2276
2277 /* Release locally allocated memory ... nothing placed in slots */
2278free_mem_out:
Jesper Juhl735d5662005-11-07 01:01:29 -08002279 kfree(o_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (o_tty)
2281 free_tty_struct(o_tty);
Jesper Juhl735d5662005-11-07 01:01:29 -08002282 kfree(ltp);
2283 kfree(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 free_tty_struct(tty);
2285
2286fail_no_mem:
2287 module_put(driver->owner);
2288 retval = -ENOMEM;
2289 goto end_init;
2290
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002291 /* call the tty release_tty routine to clean out this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292release_mem_out:
Akinobu Mita40509142006-09-29 02:01:27 -07002293 if (printk_ratelimit())
2294 printk(KERN_INFO "init_dev: ldisc open failed, "
2295 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002296 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto end_init;
2298}
2299
Alan Coxaf9b8972006-08-27 01:24:01 -07002300/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002301 * release_one_tty - release tty structure memory
Alan Coxaf9b8972006-08-27 01:24:01 -07002302 *
2303 * Releases memory associated with a tty structure, and clears out the
2304 * driver table slots. This function is called when a device is no longer
2305 * in use. It also gets called when setup of a device fails.
2306 *
2307 * Locking:
2308 * tty_mutex - sometimes only
2309 * takes the file list lock internally when working on the list
2310 * of ttys that the driver keeps.
2311 * FIXME: should we require tty_mutex is held here ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002313static void release_one_tty(struct tty_struct *tty, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002316 struct ktermios *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 if (!devpts)
2319 tty->driver->ttys[idx] = NULL;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2322 tp = tty->termios;
2323 if (!devpts)
2324 tty->driver->termios[idx] = NULL;
2325 kfree(tp);
2326
2327 tp = tty->termios_locked;
2328 if (!devpts)
2329 tty->driver->termios_locked[idx] = NULL;
2330 kfree(tp);
2331 }
2332
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 tty->magic = 0;
2335 tty->driver->refcount--;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 file_list_lock();
2338 list_del_init(&tty->tty_files);
2339 file_list_unlock();
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 free_tty_struct(tty);
2342}
2343
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002344/**
2345 * release_tty - release tty structure memory
2346 *
2347 * Release both @tty and a possible linked partner (think pty pair),
2348 * and decrement the refcount of the backing module.
2349 *
2350 * Locking:
2351 * tty_mutex - sometimes only
2352 * takes the file list lock internally when working on the list
2353 * of ttys that the driver keeps.
2354 * FIXME: should we require tty_mutex is held here ??
2355 */
2356static void release_tty(struct tty_struct *tty, int idx)
2357{
2358 struct tty_driver *driver = tty->driver;
2359
2360 if (tty->link)
2361 release_one_tty(tty->link, idx);
2362 release_one_tty(tty, idx);
2363 module_put(driver->owner);
2364}
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366/*
2367 * Even releasing the tty structures is a tricky business.. We have
2368 * to be very careful that the structures are all released at the
2369 * same time, as interrupts might otherwise get the wrong pointers.
2370 *
2371 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
2372 * lead to double frees or releasing memory still in use.
2373 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002374static void release_dev(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 struct tty_struct *tty, *o_tty;
2377 int pty_master, tty_closing, o_tty_closing, do_sleep;
Paul Fulghum14a62832006-04-10 22:54:19 -07002378 int devpts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 int idx;
2380 char buf[64];
2381 unsigned long flags;
Alan Cox37bdfb02008-02-08 04:18:47 -08002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 tty = (struct tty_struct *)filp->private_data;
Alan Cox37bdfb02008-02-08 04:18:47 -08002384 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
2385 "release_dev"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return;
2387
2388 check_tty_count(tty, "release_dev");
2389
2390 tty_fasync(-1, filp, 0);
2391
2392 idx = tty->index;
2393 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2394 tty->driver->subtype == PTY_TYPE_MASTER);
2395 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 o_tty = tty->link;
2397
2398#ifdef TTY_PARANOIA_CHECK
2399 if (idx < 0 || idx >= tty->driver->num) {
2400 printk(KERN_DEBUG "release_dev: bad idx when trying to "
2401 "free (%s)\n", tty->name);
2402 return;
2403 }
2404 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2405 if (tty != tty->driver->ttys[idx]) {
2406 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
2407 "for (%s)\n", idx, tty->name);
2408 return;
2409 }
2410 if (tty->termios != tty->driver->termios[idx]) {
2411 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
2412 "for (%s)\n",
2413 idx, tty->name);
2414 return;
2415 }
2416 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
2417 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
2418 "termios_locked for (%s)\n",
2419 idx, tty->name);
2420 return;
2421 }
2422 }
2423#endif
2424
2425#ifdef TTY_DEBUG_HANGUP
2426 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
2427 tty_name(tty, buf), tty->count);
2428#endif
2429
2430#ifdef TTY_PARANOIA_CHECK
2431 if (tty->driver->other &&
2432 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2433 if (o_tty != tty->driver->other->ttys[idx]) {
2434 printk(KERN_DEBUG "release_dev: other->table[%d] "
2435 "not o_tty for (%s)\n",
2436 idx, tty->name);
2437 return;
2438 }
2439 if (o_tty->termios != tty->driver->other->termios[idx]) {
2440 printk(KERN_DEBUG "release_dev: other->termios[%d] "
2441 "not o_termios for (%s)\n",
2442 idx, tty->name);
2443 return;
2444 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002445 if (o_tty->termios_locked !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 tty->driver->other->termios_locked[idx]) {
2447 printk(KERN_DEBUG "release_dev: other->termios_locked["
2448 "%d] not o_termios_locked for (%s)\n",
2449 idx, tty->name);
2450 return;
2451 }
2452 if (o_tty->link != tty) {
2453 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2454 return;
2455 }
2456 }
2457#endif
Alan Coxf34d7a52008-04-30 00:54:13 -07002458 if (tty->ops->close)
2459 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 /*
2462 * Sanity check: if tty->count is going to zero, there shouldn't be
2463 * any waiters on tty->read_wait or tty->write_wait. We test the
2464 * wait queues and kick everyone out _before_ actually starting to
2465 * close. This ensures that we won't block while releasing the tty
2466 * structure.
2467 *
2468 * The test for the o_tty closing is necessary, since the master and
2469 * slave sides may close in any order. If the slave side closes out
2470 * first, its count will be one, since the master side holds an open.
2471 * Thus this test wouldn't be triggered at the time the slave closes,
2472 * so we do it now.
2473 *
2474 * Note that it's possible for the tty to be opened again while we're
2475 * flushing out waiters. By recalculating the closing flags before
2476 * each iteration we avoid any problems.
2477 */
2478 while (1) {
2479 /* Guard against races with tty->count changes elsewhere and
2480 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08002481
Ingo Molnar70522e12006-03-23 03:00:31 -08002482 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 tty_closing = tty->count <= 1;
2484 o_tty_closing = o_tty &&
2485 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 do_sleep = 0;
2487
2488 if (tty_closing) {
2489 if (waitqueue_active(&tty->read_wait)) {
2490 wake_up(&tty->read_wait);
2491 do_sleep++;
2492 }
2493 if (waitqueue_active(&tty->write_wait)) {
2494 wake_up(&tty->write_wait);
2495 do_sleep++;
2496 }
2497 }
2498 if (o_tty_closing) {
2499 if (waitqueue_active(&o_tty->read_wait)) {
2500 wake_up(&o_tty->read_wait);
2501 do_sleep++;
2502 }
2503 if (waitqueue_active(&o_tty->write_wait)) {
2504 wake_up(&o_tty->write_wait);
2505 do_sleep++;
2506 }
2507 }
2508 if (!do_sleep)
2509 break;
2510
2511 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2512 "active!\n", tty_name(tty, buf));
Ingo Molnar70522e12006-03-23 03:00:31 -08002513 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08002515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08002518 * The closing flags are now consistent with the open counts on
2519 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 * block, so it's safe to proceed with closing.
2521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (pty_master) {
2523 if (--o_tty->count < 0) {
2524 printk(KERN_WARNING "release_dev: bad pty slave count "
2525 "(%d) for %s\n",
2526 o_tty->count, tty_name(o_tty, buf));
2527 o_tty->count = 0;
2528 }
2529 }
2530 if (--tty->count < 0) {
2531 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2532 tty->count, tty_name(tty, buf));
2533 tty->count = 0;
2534 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * We've decremented tty->count, so we need to remove this file
2538 * descriptor off the tty->tty_files list; this serves two
2539 * purposes:
2540 * - check_tty_count sees the correct number of file descriptors
2541 * associated with this tty.
2542 * - do_tty_hangup no longer sees this file descriptor as
2543 * something that needs to be handled for hangups.
2544 */
2545 file_kill(filp);
2546 filp->private_data = NULL;
2547
2548 /*
2549 * Perform some housekeeping before deciding whether to return.
2550 *
2551 * Set the TTY_CLOSING flag if this was the last open. In the
2552 * case of a pty we may have to wait around for the other side
2553 * to close, and TTY_CLOSING makes sure we can't be reopened.
2554 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002555 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08002557 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 set_bit(TTY_CLOSING, &o_tty->flags);
2559
2560 /*
2561 * If _either_ side is closing, make sure there aren't any
2562 * processes that still think tty or o_tty is their controlling
2563 * tty.
2564 */
2565 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002567 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002569 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 read_unlock(&tasklist_lock);
2571 }
2572
Ingo Molnar70522e12006-03-23 03:00:31 -08002573 mutex_unlock(&tty_mutex);
Paul Fulghumda965822006-02-14 13:53:00 -08002574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /* check whether both sides are closing ... */
2576 if (!tty_closing || (o_tty && !o_tty_closing))
2577 return;
Alan Cox37bdfb02008-02-08 04:18:47 -08002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579#ifdef TTY_DEBUG_HANGUP
2580 printk(KERN_DEBUG "freeing tty structure...");
2581#endif
2582 /*
2583 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
2584 * kill any delayed work. As this is the final close it does not
2585 * race with the set_ldisc code path.
2586 */
2587 clear_bit(TTY_LDISC, &tty->flags);
Alan Cox33f0f882006-01-09 20:54:13 -08002588 cancel_delayed_work(&tty->buf.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 /*
Alan Cox33f0f882006-01-09 20:54:13 -08002591 * Wait for ->hangup_work and ->buf.work handlers to terminate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 flush_scheduled_work();
Alan Cox37bdfb02008-02-08 04:18:47 -08002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /*
2597 * Wait for any short term users (we know they are just driver
2598 * side waiters as the file is closing so user count on the file
2599 * side is zero.
2600 */
2601 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08002602 while (tty->ldisc.refcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2604 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
2605 spin_lock_irqsave(&tty_ldisc_lock, flags);
2606 }
2607 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2608 /*
2609 * Shutdown the current line discipline, and reset it to N_TTY.
2610 * N.B. why reset ldisc when we're releasing the memory??
2611 *
2612 * FIXME: this MUST get fixed for the new reflocking
2613 */
2614 if (tty->ldisc.close)
2615 (tty->ldisc.close)(tty);
2616 tty_ldisc_put(tty->ldisc.num);
Alan Cox37bdfb02008-02-08 04:18:47 -08002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /*
2619 * Switch the line discipline back
2620 */
2621 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
Alan Cox37bdfb02008-02-08 04:18:47 -08002622 tty_set_termios_ldisc(tty, N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (o_tty) {
2624 /* FIXME: could o_tty be in setldisc here ? */
2625 clear_bit(TTY_LDISC, &o_tty->flags);
2626 if (o_tty->ldisc.close)
2627 (o_tty->ldisc.close)(o_tty);
2628 tty_ldisc_put(o_tty->ldisc.num);
2629 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
Alan Cox37bdfb02008-02-08 04:18:47 -08002630 tty_set_termios_ldisc(o_tty, N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
2632 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002633 * The release_tty function takes care of the details of clearing
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 * the slots and preserving the termios structure.
2635 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002636 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /* Make this pty number available for reallocation */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002639 if (devpts)
2640 devpts_kill_index(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Alan Coxaf9b8972006-08-27 01:24:01 -07002643/**
2644 * tty_open - open a tty device
2645 * @inode: inode of device file
2646 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002648 * tty_open and tty_release keep up the tty count that contains the
2649 * number of opens done on a tty. We cannot use the inode-count, as
2650 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002652 * Open-counting is needed for pty masters, as well as for keeping
2653 * track of serial lines: DTR is dropped when the last close happens.
2654 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2655 *
2656 * The termios state of a pty is reset on first open so that
2657 * settings don't persist across reuse.
2658 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002659 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2660 * tty->count should protect the rest.
2661 * ->siglock protects ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002663
Alan Cox37bdfb02008-02-08 04:18:47 -08002664static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct tty_struct *tty;
2667 int noctty, retval;
2668 struct tty_driver *driver;
2669 int index;
2670 dev_t device = inode->i_rdev;
2671 unsigned short saved_flags = filp->f_flags;
2672
2673 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675retry_open:
2676 noctty = filp->f_flags & O_NOCTTY;
2677 index = -1;
2678 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002679
Ingo Molnar70522e12006-03-23 03:00:31 -08002680 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
Alan Cox37bdfb02008-02-08 04:18:47 -08002682 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002683 tty = get_current_tty();
2684 if (!tty) {
Ingo Molnar70522e12006-03-23 03:00:31 -08002685 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return -ENXIO;
2687 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002688 driver = tty->driver;
2689 index = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2691 /* noctty = 1; */
2692 goto got_driver;
2693 }
2694#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08002695 if (device == MKDEV(TTY_MAJOR, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 extern struct tty_driver *console_driver;
2697 driver = console_driver;
2698 index = fg_console;
2699 noctty = 1;
2700 goto got_driver;
2701 }
2702#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08002703 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 driver = console_device(&index);
2705 if (driver) {
2706 /* Don't let /dev/console block */
2707 filp->f_flags |= O_NONBLOCK;
2708 noctty = 1;
2709 goto got_driver;
2710 }
Ingo Molnar70522e12006-03-23 03:00:31 -08002711 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 return -ENODEV;
2713 }
2714
2715 driver = get_tty_driver(device, &index);
2716 if (!driver) {
Ingo Molnar70522e12006-03-23 03:00:31 -08002717 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return -ENODEV;
2719 }
2720got_driver:
2721 retval = init_dev(driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08002722 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (retval)
2724 return retval;
2725
2726 filp->private_data = tty;
2727 file_move(filp, &tty->tty_files);
2728 check_tty_count(tty, "tty_open");
2729 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2730 tty->driver->subtype == PTY_TYPE_MASTER)
2731 noctty = 1;
2732#ifdef TTY_DEBUG_HANGUP
2733 printk(KERN_DEBUG "opening %s...", tty->name);
2734#endif
2735 if (!retval) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002736 if (tty->ops->open)
2737 retval = tty->ops->open(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 else
2739 retval = -ENODEV;
2740 }
2741 filp->f_flags = saved_flags;
2742
Alan Cox37bdfb02008-02-08 04:18:47 -08002743 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
2744 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 retval = -EBUSY;
2746
2747 if (retval) {
2748#ifdef TTY_DEBUG_HANGUP
2749 printk(KERN_DEBUG "error %d in opening %s...", retval,
2750 tty->name);
2751#endif
2752 release_dev(filp);
2753 if (retval != -ERESTARTSYS)
2754 return retval;
2755 if (signal_pending(current))
2756 return retval;
2757 schedule();
2758 /*
2759 * Need to reset f_op in case a hangup happened.
2760 */
2761 if (filp->f_op == &hung_up_tty_fops)
2762 filp->f_op = &tty_fops;
2763 goto retry_open;
2764 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002765
2766 mutex_lock(&tty_mutex);
2767 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (!noctty &&
2769 current->signal->leader &&
2770 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002771 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07002772 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002773 spin_unlock_irq(&current->sighand->siglock);
2774 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return 0;
2776}
2777
2778#ifdef CONFIG_UNIX98_PTYS
Alan Coxaf9b8972006-08-27 01:24:01 -07002779/**
2780 * ptmx_open - open a unix 98 pty master
2781 * @inode: inode of device file
2782 * @filp: file pointer to tty
2783 *
2784 * Allocate a unix98 pty master device from the ptmx driver.
2785 *
2786 * Locking: tty_mutex protects theinit_dev work. tty->count should
Alan Cox37bdfb02008-02-08 04:18:47 -08002787 * protect the rest.
Alan Coxaf9b8972006-08-27 01:24:01 -07002788 * allocated_ptys_lock handles the list of free pty numbers
2789 */
2790
Alan Cox37bdfb02008-02-08 04:18:47 -08002791static int ptmx_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
2793 struct tty_struct *tty;
2794 int retval;
2795 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797 nonseekable_open(inode, filp);
2798
2799 /* find a device that is not in use. */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002800 index = devpts_new_index();
2801 if (index < 0)
2802 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Ingo Molnar70522e12006-03-23 03:00:31 -08002804 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 retval = init_dev(ptm_driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08002806 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (retval)
2809 goto out;
2810
2811 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2812 filp->private_data = tty;
2813 file_move(filp, &tty->tty_files);
2814
Sukadev Bhattiprolu4f8f9d62008-04-30 00:54:20 -07002815 retval = devpts_pty_new(tty->link);
2816 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 goto out1;
2818
Hiroshi Shimamoto86a96532008-04-30 00:54:20 -07002819 check_tty_count(tty, "ptmx_open");
Alan Coxf34d7a52008-04-30 00:54:13 -07002820 retval = ptm_driver->ops->open(tty, filp);
Miloslav Trmac41126222008-04-18 13:30:14 -07002821 if (!retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return 0;
2823out1:
2824 release_dev(filp);
Paul Fulghum9453a5a2006-04-10 22:54:18 -07002825 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826out:
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002827 devpts_kill_index(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return retval;
2829}
2830#endif
2831
Alan Coxaf9b8972006-08-27 01:24:01 -07002832/**
2833 * tty_release - vfs callback for close
2834 * @inode: inode of tty
2835 * @filp: file pointer for handle to tty
2836 *
2837 * Called the last time each file handle is closed that references
2838 * this tty. There may however be several such references.
2839 *
2840 * Locking:
2841 * Takes bkl. See release_dev
2842 */
2843
Alan Cox37bdfb02008-02-08 04:18:47 -08002844static int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
2846 lock_kernel();
2847 release_dev(filp);
2848 unlock_kernel();
2849 return 0;
2850}
2851
Alan Coxaf9b8972006-08-27 01:24:01 -07002852/**
2853 * tty_poll - check tty status
2854 * @filp: file being polled
2855 * @wait: poll wait structures to update
2856 *
2857 * Call the line discipline polling method to obtain the poll
2858 * status of the device.
2859 *
2860 * Locking: locks called line discipline but ldisc poll method
2861 * may be re-entered freely by other callers.
2862 */
2863
Alan Cox37bdfb02008-02-08 04:18:47 -08002864static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865{
Alan Cox37bdfb02008-02-08 04:18:47 -08002866 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct tty_ldisc *ld;
2868 int ret = 0;
2869
2870 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08002871 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 ld = tty_ldisc_ref_wait(tty);
2875 if (ld->poll)
2876 ret = (ld->poll)(tty, filp, wait);
2877 tty_ldisc_deref(ld);
2878 return ret;
2879}
2880
Alan Cox37bdfb02008-02-08 04:18:47 -08002881static int tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Alan Cox37bdfb02008-02-08 04:18:47 -08002883 struct tty_struct *tty;
Alan Cox47f86832008-04-30 00:53:30 -07002884 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int retval;
2886
2887 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08002888 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 retval = fasync_helper(fd, filp, on, &tty->fasync);
2892 if (retval <= 0)
2893 return retval;
2894
2895 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002896 enum pid_type type;
2897 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (!waitqueue_active(&tty->read_wait))
2899 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07002900 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002901 if (tty->pgrp) {
2902 pid = tty->pgrp;
2903 type = PIDTYPE_PGID;
2904 } else {
2905 pid = task_pid(current);
2906 type = PIDTYPE_PID;
2907 }
Alan Cox47f86832008-04-30 00:53:30 -07002908 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002909 retval = __f_setown(filp, pid, type, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (retval)
2911 return retval;
2912 } else {
2913 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2914 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2915 }
2916 return 0;
2917}
2918
Alan Coxaf9b8972006-08-27 01:24:01 -07002919/**
2920 * tiocsti - fake input character
2921 * @tty: tty to fake input into
2922 * @p: pointer to character
2923 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002924 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002925 * input management.
2926 *
2927 * FIXME: does not honour flow control ??
2928 *
2929 * Locking:
2930 * Called functions take tty_ldisc_lock
2931 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002932 *
2933 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002934 */
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936static int tiocsti(struct tty_struct *tty, char __user *p)
2937{
2938 char ch, mbz = 0;
2939 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2942 return -EPERM;
2943 if (get_user(ch, p))
2944 return -EFAULT;
2945 ld = tty_ldisc_ref_wait(tty);
2946 ld->receive_buf(tty, &ch, &mbz, 1);
2947 tty_ldisc_deref(ld);
2948 return 0;
2949}
2950
Alan Coxaf9b8972006-08-27 01:24:01 -07002951/**
2952 * tiocgwinsz - implement window query ioctl
2953 * @tty; tty
2954 * @arg: user buffer for result
2955 *
Alan Cox808a0d32006-09-29 02:00:40 -07002956 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002957 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002958 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002959 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002960 */
2961
Alan Cox37bdfb02008-02-08 04:18:47 -08002962static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
Alan Cox808a0d32006-09-29 02:00:40 -07002964 int err;
2965
Arjan van de Ven5785c952006-09-29 02:00:43 -07002966 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002967 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002968 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002969
2970 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
Alan Coxaf9b8972006-08-27 01:24:01 -07002973/**
2974 * tiocswinsz - implement window size set ioctl
2975 * @tty; tty
2976 * @arg: user buffer for result
2977 *
2978 * Copies the user idea of the window size to the kernel. Traditionally
2979 * this is just advisory information but for the Linux console it
2980 * actually has driver level meaning and triggers a VC resize.
2981 *
2982 * Locking:
Alan Coxca9bda02006-09-29 02:00:03 -07002983 * Called function use the console_sem is used to ensure we do
2984 * not try and resize the console twice at once.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002985 * The tty->termios_mutex is used to ensure we don't double
2986 * resize and get confused. Lock order - tty->termios_mutex before
Alan Coxca9bda02006-09-29 02:00:03 -07002987 * console sem
Alan Coxaf9b8972006-08-27 01:24:01 -07002988 */
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
Alan Cox37bdfb02008-02-08 04:18:47 -08002991 struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct winsize tmp_ws;
Alan Cox47f86832008-04-30 00:53:30 -07002994 struct pid *pgrp, *rpgrp;
2995 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2998 return -EFAULT;
Alan Coxca9bda02006-09-29 02:00:03 -07002999
Arjan van de Ven5785c952006-09-29 02:00:43 -07003000 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
Alan Coxca9bda02006-09-29 02:00:03 -07003002 goto done;
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004#ifdef CONFIG_VT
3005 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
Arjan van de Ven5785c952006-09-29 02:00:43 -07003006 if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
3007 tmp_ws.ws_row)) {
3008 mutex_unlock(&tty->termios_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003009 return -ENXIO;
Alan Coxca9bda02006-09-29 02:00:03 -07003010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 }
3012#endif
Alan Cox47f86832008-04-30 00:53:30 -07003013 /* Get the PID values and reference them so we can
3014 avoid holding the tty ctrl lock while sending signals */
3015 spin_lock_irqsave(&tty->ctrl_lock, flags);
3016 pgrp = get_pid(tty->pgrp);
3017 rpgrp = get_pid(real_tty->pgrp);
3018 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3019
3020 if (pgrp)
3021 kill_pgrp(pgrp, SIGWINCH, 1);
3022 if (rpgrp != pgrp && rpgrp)
3023 kill_pgrp(rpgrp, SIGWINCH, 1);
3024
3025 put_pid(pgrp);
3026 put_pid(rpgrp);
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 tty->winsize = tmp_ws;
3029 real_tty->winsize = tmp_ws;
Alan Coxca9bda02006-09-29 02:00:03 -07003030done:
Arjan van de Ven5785c952006-09-29 02:00:43 -07003031 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 0;
3033}
3034
Alan Coxaf9b8972006-08-27 01:24:01 -07003035/**
3036 * tioccons - allow admin to move logical console
3037 * @file: the file to become console
3038 *
3039 * Allow the adminstrator to move the redirected console device
3040 *
3041 * Locking: uses redirect_lock to guard the redirect information
3042 */
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044static int tioccons(struct file *file)
3045{
3046 if (!capable(CAP_SYS_ADMIN))
3047 return -EPERM;
3048 if (file->f_op->write == redirected_tty_write) {
3049 struct file *f;
3050 spin_lock(&redirect_lock);
3051 f = redirect;
3052 redirect = NULL;
3053 spin_unlock(&redirect_lock);
3054 if (f)
3055 fput(f);
3056 return 0;
3057 }
3058 spin_lock(&redirect_lock);
3059 if (redirect) {
3060 spin_unlock(&redirect_lock);
3061 return -EBUSY;
3062 }
3063 get_file(file);
3064 redirect = file;
3065 spin_unlock(&redirect_lock);
3066 return 0;
3067}
3068
Alan Coxaf9b8972006-08-27 01:24:01 -07003069/**
3070 * fionbio - non blocking ioctl
3071 * @file: file to set blocking value
3072 * @p: user parameter
3073 *
3074 * Historical tty interfaces had a blocking control ioctl before
3075 * the generic functionality existed. This piece of history is preserved
3076 * in the expected tty API of posix OS's.
3077 *
3078 * Locking: none, the open fle handle ensures it won't go away.
3079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081static int fionbio(struct file *file, int __user *p)
3082{
3083 int nonblock;
3084
3085 if (get_user(nonblock, p))
3086 return -EFAULT;
3087
Alan Cox04f378b2008-04-30 00:53:29 -07003088 /* file->f_flags is still BKL protected in the fs layer - vomit */
3089 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (nonblock)
3091 file->f_flags |= O_NONBLOCK;
3092 else
3093 file->f_flags &= ~O_NONBLOCK;
Alan Cox04f378b2008-04-30 00:53:29 -07003094 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return 0;
3096}
3097
Alan Coxaf9b8972006-08-27 01:24:01 -07003098/**
3099 * tiocsctty - set controlling tty
3100 * @tty: tty structure
3101 * @arg: user argument
3102 *
3103 * This ioctl is used to manage job control. It permits a session
3104 * leader to set this tty as the controlling tty for the session.
3105 *
3106 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07003107 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003108 * Takes tasklist_lock internally to walk sessions
3109 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07003110 */
3111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112static int tiocsctty(struct tty_struct *tty, int arg)
3113{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003114 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003115 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003116 return ret;
3117
3118 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /*
3120 * The process must be a session leader and
3121 * not have a controlling tty already.
3122 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003123 if (!current->signal->leader || current->signal->tty) {
3124 ret = -EPERM;
3125 goto unlock;
3126 }
3127
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003128 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /*
3130 * This tty is already the controlling
3131 * tty for another session group!
3132 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003133 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 /*
3135 * Steal it away
3136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003138 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003140 } else {
3141 ret = -EPERM;
3142 goto unlock;
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003145 proc_set_tty(current, tty);
3146unlock:
Alan Cox28298232006-09-29 02:00:58 -07003147 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Alan Coxaf9b8972006-08-27 01:24:01 -07003151/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07003152 * tty_get_pgrp - return a ref counted pgrp pid
3153 * @tty: tty to read
3154 *
3155 * Returns a refcounted instance of the pid struct for the process
3156 * group controlling the tty.
3157 */
3158
3159struct pid *tty_get_pgrp(struct tty_struct *tty)
3160{
3161 unsigned long flags;
3162 struct pid *pgrp;
3163
3164 spin_lock_irqsave(&tty->ctrl_lock, flags);
3165 pgrp = get_pid(tty->pgrp);
3166 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3167
3168 return pgrp;
3169}
3170EXPORT_SYMBOL_GPL(tty_get_pgrp);
3171
3172/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003173 * tiocgpgrp - get process group
3174 * @tty: tty passed by user
3175 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3176 * @p: returned pid
3177 *
3178 * Obtain the process group of the tty. If there is no process group
3179 * return an error.
3180 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003181 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07003182 */
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3185{
Alan Cox5d0fdf12008-04-30 00:53:31 -07003186 struct pid *pid;
3187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * (tty == real_tty) is a cheap way of
3190 * testing if the tty is NOT a master pty.
3191 */
3192 if (tty == real_tty && current->signal->tty != real_tty)
3193 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07003194 pid = tty_get_pgrp(real_tty);
3195 ret = put_user(pid_vnr(pid), p);
3196 put_pid(pid);
3197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Alan Coxaf9b8972006-08-27 01:24:01 -07003200/**
3201 * tiocspgrp - attempt to set process group
3202 * @tty: tty passed by user
3203 * @real_tty: tty side device matching tty passed by user
3204 * @p: pid pointer
3205 *
3206 * Set the process group of the tty to the session passed. Only
3207 * permitted where the tty session is our session.
3208 *
Alan Cox47f86832008-04-30 00:53:30 -07003209 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07003210 */
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3213{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003214 struct pid *pgrp;
3215 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07003217 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 if (retval == -EIO)
3220 return -ENOTTY;
3221 if (retval)
3222 return retval;
3223 if (!current->signal->tty ||
3224 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003225 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003227 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003229 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003231 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003232 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003233 retval = -ESRCH;
3234 if (!pgrp)
3235 goto out_unlock;
3236 retval = -EPERM;
3237 if (session_of_pgrp(pgrp) != task_session(current))
3238 goto out_unlock;
3239 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07003240 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003241 put_pid(real_tty->pgrp);
3242 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07003243 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003244out_unlock:
3245 rcu_read_unlock();
3246 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Alan Coxaf9b8972006-08-27 01:24:01 -07003249/**
3250 * tiocgsid - get session id
3251 * @tty: tty passed by user
3252 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3253 * @p: pointer to returned session id
3254 *
3255 * Obtain the session id of the tty. If there is no session
3256 * return an error.
3257 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003258 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07003259 */
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3262{
3263 /*
3264 * (tty == real_tty) is a cheap way of
3265 * testing if the tty is NOT a master pty.
3266 */
3267 if (tty == real_tty && current->signal->tty != real_tty)
3268 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003269 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003271 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Alan Coxaf9b8972006-08-27 01:24:01 -07003274/**
3275 * tiocsetd - set line discipline
3276 * @tty: tty device
3277 * @p: pointer to user data
3278 *
3279 * Set the line discipline according to user request.
3280 *
3281 * Locking: see tty_set_ldisc, this function is just a helper
3282 */
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284static int tiocsetd(struct tty_struct *tty, int __user *p)
3285{
3286 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07003287 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 if (get_user(ldisc, p))
3290 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07003291
3292 lock_kernel();
3293 ret = tty_set_ldisc(tty, ldisc);
3294 unlock_kernel();
3295
3296 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Alan Coxaf9b8972006-08-27 01:24:01 -07003299/**
3300 * send_break - performed time break
3301 * @tty: device to break on
3302 * @duration: timeout in mS
3303 *
3304 * Perform a timed break on hardware that lacks its own driver level
3305 * timed break functionality.
3306 *
3307 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07003308 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07003309 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003310 */
3311
Domen Puncerb20f3ae2005-06-25 14:58:42 -07003312static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Alan Cox9c1729d2007-07-15 23:39:43 -07003314 if (tty_write_lock(tty, 0) < 0)
Alan Coxf34d7a52008-04-30 00:54:13 -07003315 return -EINTR;
3316 tty->ops->break_ctl(tty, -1);
Alan Cox9c1729d2007-07-15 23:39:43 -07003317 if (!signal_pending(current))
Domen Puncerb20f3ae2005-06-25 14:58:42 -07003318 msleep_interruptible(duration);
Alan Coxf34d7a52008-04-30 00:54:13 -07003319 tty->ops->break_ctl(tty, 0);
Alan Cox9c1729d2007-07-15 23:39:43 -07003320 tty_write_unlock(tty);
Alan Cox04f378b2008-04-30 00:53:29 -07003321 if (!signal_pending(current))
Alan Coxf34d7a52008-04-30 00:54:13 -07003322 return -EINTR;
3323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Alan Coxaf9b8972006-08-27 01:24:01 -07003326/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003327 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07003328 * @tty: tty device
3329 * @file: user file pointer
3330 * @p: pointer to result
3331 *
3332 * Obtain the modem status bits from the tty driver if the feature
3333 * is supported. Return -EINVAL if it is not available.
3334 *
3335 * Locking: none (up to the driver)
3336 */
3337
3338static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 int retval = -EINVAL;
3341
Alan Coxf34d7a52008-04-30 00:54:13 -07003342 if (tty->ops->tiocmget) {
3343 retval = tty->ops->tiocmget(tty, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
3345 if (retval >= 0)
3346 retval = put_user(retval, p);
3347 }
3348 return retval;
3349}
3350
Alan Coxaf9b8972006-08-27 01:24:01 -07003351/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003352 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07003353 * @tty: tty device
3354 * @file: user file pointer
3355 * @cmd: command - clear bits, set bits or set all
3356 * @p: pointer to desired bits
3357 *
3358 * Set the modem status bits from the tty driver if the feature
3359 * is supported. Return -EINVAL if it is not available.
3360 *
3361 * Locking: none (up to the driver)
3362 */
3363
3364static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 unsigned __user *p)
3366{
3367 int retval = -EINVAL;
3368
Alan Coxf34d7a52008-04-30 00:54:13 -07003369 if (tty->ops->tiocmset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 unsigned int set, clear, val;
3371
3372 retval = get_user(val, p);
3373 if (retval)
3374 return retval;
3375
3376 set = clear = 0;
3377 switch (cmd) {
3378 case TIOCMBIS:
3379 set = val;
3380 break;
3381 case TIOCMBIC:
3382 clear = val;
3383 break;
3384 case TIOCMSET:
3385 set = val;
3386 clear = ~val;
3387 break;
3388 }
3389
3390 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3391 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3392
Alan Coxf34d7a52008-04-30 00:54:13 -07003393 retval = tty->ops->tiocmset(tty, file, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 }
3395 return retval;
3396}
3397
3398/*
3399 * Split this up, as gcc can choke on it otherwise..
3400 */
Alan Cox04f378b2008-04-30 00:53:29 -07003401long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
3403 struct tty_struct *tty, *real_tty;
3404 void __user *p = (void __user *)arg;
3405 int retval;
3406 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -07003407 struct inode *inode = file->f_dentry->d_inode;
Alan Cox37bdfb02008-02-08 04:18:47 -08003408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 tty = (struct tty_struct *)file->private_data;
3410 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3411 return -EINVAL;
3412
3413 real_tty = tty;
3414 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3415 tty->driver->subtype == PTY_TYPE_MASTER)
3416 real_tty = tty->link;
3417
3418 /*
3419 * Break handling by driver
3420 */
Alan Cox04f378b2008-04-30 00:53:29 -07003421
3422 retval = -EINVAL;
3423
Alan Coxf34d7a52008-04-30 00:54:13 -07003424 if (!tty->ops->break_ctl) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003425 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 case TIOCSBRK:
3427 case TIOCCBRK:
Alan Coxf34d7a52008-04-30 00:54:13 -07003428 if (tty->ops->ioctl)
3429 retval = tty->ops->ioctl(tty, file, cmd, arg);
3430 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3431 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
Alan Cox04f378b2008-04-30 00:53:29 -07003432 return retval;
Alan Cox37bdfb02008-02-08 04:18:47 -08003433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 /* These two ioctl's always return success; even if */
3435 /* the driver doesn't support them. */
3436 case TCSBRK:
3437 case TCSBRKP:
Alan Coxf34d7a52008-04-30 00:54:13 -07003438 if (!tty->ops->ioctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return 0;
Alan Coxf34d7a52008-04-30 00:54:13 -07003440 retval = tty->ops->ioctl(tty, file, cmd, arg);
3441 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3442 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (retval == -ENOIOCTLCMD)
3444 retval = 0;
3445 return retval;
3446 }
3447 }
3448
3449 /*
3450 * Factor out some common prep work
3451 */
3452 switch (cmd) {
3453 case TIOCSETD:
3454 case TIOCSBRK:
3455 case TIOCCBRK:
3456 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08003457 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 retval = tty_check_change(tty);
3459 if (retval)
3460 return retval;
3461 if (cmd != TIOCCBRK) {
3462 tty_wait_until_sent(tty, 0);
3463 if (signal_pending(current))
3464 return -EINTR;
3465 }
3466 break;
3467 }
3468
3469 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003470 case TIOCSTI:
3471 return tiocsti(tty, p);
3472 case TIOCGWINSZ:
3473 return tiocgwinsz(tty, p);
3474 case TIOCSWINSZ:
3475 return tiocswinsz(tty, real_tty, p);
3476 case TIOCCONS:
3477 return real_tty != tty ? -EINVAL : tioccons(file);
3478 case FIONBIO:
3479 return fionbio(file, p);
3480 case TIOCEXCL:
3481 set_bit(TTY_EXCLUSIVE, &tty->flags);
3482 return 0;
3483 case TIOCNXCL:
3484 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3485 return 0;
3486 case TIOCNOTTY:
3487 if (current->signal->tty != tty)
3488 return -ENOTTY;
3489 no_tty();
3490 return 0;
3491 case TIOCSCTTY:
3492 return tiocsctty(tty, arg);
3493 case TIOCGPGRP:
3494 return tiocgpgrp(tty, real_tty, p);
3495 case TIOCSPGRP:
3496 return tiocspgrp(tty, real_tty, p);
3497 case TIOCGSID:
3498 return tiocgsid(tty, real_tty, p);
3499 case TIOCGETD:
Alan Cox37bdfb02008-02-08 04:18:47 -08003500 return put_user(tty->ldisc.num, (int __user *)p);
3501 case TIOCSETD:
3502 return tiocsetd(tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08003504 case TIOCLINUX:
3505 return tioclinux(tty, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08003507 /*
3508 * Break handling
3509 */
3510 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07003511 if (tty->ops->break_ctl)
3512 tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08003513 return 0;
3514
3515 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07003516 if (tty->ops->break_ctl)
3517 tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08003518 return 0;
3519 case TCSBRK: /* SVID version: non-zero arg --> no break */
3520 /* non-zero arg means wait for all output data
3521 * to be sent (performed above) but don't send break.
3522 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003524 if (!arg)
3525 return send_break(tty, 250);
3526 return 0;
3527 case TCSBRKP: /* support for POSIX tcsendbreak() */
3528 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Alan Cox37bdfb02008-02-08 04:18:47 -08003530 case TIOCMGET:
3531 return tty_tiocmget(tty, file, p);
3532 case TIOCMSET:
3533 case TIOCMBIC:
3534 case TIOCMBIS:
3535 return tty_tiocmset(tty, file, cmd, p);
3536 case TCFLSH:
3537 switch (arg) {
3538 case TCIFLUSH:
3539 case TCIOFLUSH:
3540 /* flush tty buffer and allow ldisc to process ioctl */
3541 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07003542 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08003543 }
3544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 }
Alan Coxf34d7a52008-04-30 00:54:13 -07003546 if (tty->ops->ioctl) {
3547 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 if (retval != -ENOIOCTLCMD)
3549 return retval;
3550 }
3551 ld = tty_ldisc_ref_wait(tty);
3552 retval = -EINVAL;
3553 if (ld->ioctl) {
3554 retval = ld->ioctl(tty, file, cmd, arg);
3555 if (retval == -ENOIOCTLCMD)
3556 retval = -EINVAL;
3557 }
3558 tty_ldisc_deref(ld);
3559 return retval;
3560}
3561
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003562#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08003563static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003564 unsigned long arg)
3565{
3566 struct inode *inode = file->f_dentry->d_inode;
3567 struct tty_struct *tty = file->private_data;
3568 struct tty_ldisc *ld;
3569 int retval = -ENOIOCTLCMD;
3570
3571 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3572 return -EINVAL;
3573
Alan Coxf34d7a52008-04-30 00:54:13 -07003574 if (tty->ops->compat_ioctl) {
3575 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003576 if (retval != -ENOIOCTLCMD)
3577 return retval;
3578 }
3579
3580 ld = tty_ldisc_ref_wait(tty);
3581 if (ld->compat_ioctl)
3582 retval = ld->compat_ioctl(tty, file, cmd, arg);
3583 tty_ldisc_deref(ld);
3584
3585 return retval;
3586}
3587#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
3589/*
3590 * This implements the "Secure Attention Key" --- the idea is to
3591 * prevent trojan horses by killing all processes associated with this
3592 * tty when the user hits the "Secure Attention Key". Required for
3593 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08003594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 * This code could be nicer; ideally it should send a HUP, wait a few
3596 * seconds, then send a INT, and then a KILL signal. But you then
3597 * have to coordinate with the init process, since all processes associated
3598 * with the current tty must be dead before the new getty is allowed
3599 * to spawn.
3600 *
3601 * Now, if it would be correct ;-/ The current code has a nasty hole -
3602 * it doesn't catch files in flight. We may send the descriptor to ourselves
3603 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3604 *
3605 * Nasty bug: do_SAK is being called in interrupt context. This can
3606 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3607 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003608void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
3610#ifdef TTY_SOFT_SAK
3611 tty_hangup(tty);
3612#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003613 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003614 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 int i;
3616 struct file *filp;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07003617 struct fdtable *fdt;
Alan Cox37bdfb02008-02-08 04:18:47 -08003618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 if (!tty)
3620 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003621 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08003622
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003623 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Alan Coxf34d7a52008-04-30 00:54:13 -07003625 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003628 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003629 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08003630 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07003631 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003632 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003633 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003634 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003635 /* Now kill any processes that happen to have the
3636 * tty open.
3637 */
3638 do_each_thread(g, p) {
3639 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07003641 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003642 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 send_sig(SIGKILL, p, 1);
3644 continue;
3645 }
3646 task_lock(p);
3647 if (p->files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07003648 /*
3649 * We don't take a ref to the file, so we must
3650 * hold ->file_lock instead.
3651 */
3652 spin_lock(&p->files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07003653 fdt = files_fdtable(p->files);
Alan Cox37bdfb02008-02-08 04:18:47 -08003654 for (i = 0; i < fdt->max_fds; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 filp = fcheck_files(p->files, i);
3656 if (!filp)
3657 continue;
3658 if (filp->f_op->read == tty_read &&
3659 filp->private_data == tty) {
3660 printk(KERN_NOTICE "SAK: killed process %d"
3661 " (%s): fd#%d opened to the tty\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003662 task_pid_nr(p), p->comm, i);
Eric W. Biederman20ac9432006-04-13 04:49:07 -06003663 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 break;
3665 }
3666 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07003667 spin_unlock(&p->files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003670 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 read_unlock(&tasklist_lock);
3672#endif
3673}
3674
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003675static void do_SAK_work(struct work_struct *work)
3676{
3677 struct tty_struct *tty =
3678 container_of(work, struct tty_struct, SAK_work);
3679 __do_SAK(tty);
3680}
3681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682/*
3683 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3684 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3685 * the values which we write to it will be identical to the values which it
3686 * already has. --akpm
3687 */
3688void do_SAK(struct tty_struct *tty)
3689{
3690 if (!tty)
3691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 schedule_work(&tty->SAK_work);
3693}
3694
3695EXPORT_SYMBOL(do_SAK);
3696
Alan Coxaf9b8972006-08-27 01:24:01 -07003697/**
3698 * flush_to_ldisc
David Howells65f27f32006-11-22 14:55:48 +00003699 * @work: tty structure passed from work queue.
Alan Coxaf9b8972006-08-27 01:24:01 -07003700 *
3701 * This routine is called out of the software interrupt to flush data
3702 * from the buffer chain to the line discipline.
3703 *
3704 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3705 * while invoking the line discipline receive_buf method. The
3706 * receive_buf method is single threaded for each tty instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003708
David Howells65f27f32006-11-22 14:55:48 +00003709static void flush_to_ldisc(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
David Howells65f27f32006-11-22 14:55:48 +00003711 struct tty_struct *tty =
3712 container_of(work, struct tty_struct, buf.work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 unsigned long flags;
3714 struct tty_ldisc *disc;
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003715 struct tty_buffer *tbuf, *head;
Paul Fulghum8977d922006-02-10 01:51:14 -08003716 char *char_buf;
3717 unsigned char *flag_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 disc = tty_ldisc_ref(tty);
3720 if (disc == NULL) /* !TTY_LDISC */
3721 return;
3722
Paul Fulghum808249c2006-02-03 03:04:41 -08003723 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08003724 /* So we know a flush is running */
3725 set_bit(TTY_FLUSHING, &tty->flags);
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003726 head = tty->buf.head;
3727 if (head != NULL) {
3728 tty->buf.head = NULL;
3729 for (;;) {
3730 int count = head->commit - head->read;
3731 if (!count) {
3732 if (head->next == NULL)
3733 break;
3734 tbuf = head;
3735 head = head->next;
3736 tty_buffer_free(tty, tbuf);
3737 continue;
3738 }
Alan Cox42fd5522007-08-10 13:01:05 -07003739 /* Ldisc or user is trying to flush the buffers
3740 we are feeding to the ldisc, stop feeding the
3741 line discipline as we want to empty the queue */
3742 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
3743 break;
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003744 if (!tty->receive_room) {
3745 schedule_delayed_work(&tty->buf.work, 1);
3746 break;
3747 }
3748 if (count > tty->receive_room)
3749 count = tty->receive_room;
3750 char_buf = head->char_buf_ptr + head->read;
3751 flag_buf = head->flag_buf_ptr + head->read;
3752 head->read += count;
Paul Fulghum8977d922006-02-10 01:51:14 -08003753 spin_unlock_irqrestore(&tty->buf.lock, flags);
3754 disc->receive_buf(tty, char_buf, flag_buf, count);
3755 spin_lock_irqsave(&tty->buf.lock, flags);
3756 }
Alan Cox42fd5522007-08-10 13:01:05 -07003757 /* Restore the queue head */
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003758 tty->buf.head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
Alan Cox42fd5522007-08-10 13:01:05 -07003760 /* We may have a deferred request to flush the input buffer,
3761 if so pull the chain under the lock and empty the queue */
3762 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
3763 __tty_buffer_flush(tty);
3764 clear_bit(TTY_FLUSHPENDING, &tty->flags);
3765 wake_up(&tty->read_wait);
3766 }
3767 clear_bit(TTY_FLUSHING, &tty->flags);
Paul Fulghum808249c2006-02-03 03:04:41 -08003768 spin_unlock_irqrestore(&tty->buf.lock, flags);
Paul Fulghum817d6d32006-06-28 04:26:47 -07003769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 tty_ldisc_deref(disc);
3771}
3772
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773/**
3774 * tty_flip_buffer_push - terminal
3775 * @tty: tty to push
3776 *
3777 * Queue a push of the terminal flip buffers to the line discipline. This
3778 * function must not be called from IRQ context if tty->low_latency is set.
3779 *
3780 * In the event of the queue being busy for flipping the work will be
3781 * held off and retried later.
Alan Coxaf9b8972006-08-27 01:24:01 -07003782 *
3783 * Locking: tty buffer lock. Driver locks in low latency mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 */
3785
3786void tty_flip_buffer_push(struct tty_struct *tty)
3787{
Paul Fulghum808249c2006-02-03 03:04:41 -08003788 unsigned long flags;
3789 spin_lock_irqsave(&tty->buf.lock, flags);
Paul Fulghum33b37a32006-06-28 04:26:49 -07003790 if (tty->buf.tail != NULL)
Paul Fulghum8977d922006-02-10 01:51:14 -08003791 tty->buf.tail->commit = tty->buf.tail->used;
Paul Fulghum808249c2006-02-03 03:04:41 -08003792 spin_unlock_irqrestore(&tty->buf.lock, flags);
3793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 if (tty->low_latency)
David Howells65f27f32006-11-22 14:55:48 +00003795 flush_to_ldisc(&tty->buf.work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 else
Alan Cox33f0f882006-01-09 20:54:13 -08003797 schedule_delayed_work(&tty->buf.work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
3800EXPORT_SYMBOL(tty_flip_buffer_push);
3801
Alan Cox33f0f882006-01-09 20:54:13 -08003802
Alan Coxaf9b8972006-08-27 01:24:01 -07003803/**
3804 * initialize_tty_struct
3805 * @tty: tty to initialize
3806 *
3807 * This subroutine initializes a tty structure that has been newly
3808 * allocated.
3809 *
3810 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813static void initialize_tty_struct(struct tty_struct *tty)
3814{
3815 memset(tty, 0, sizeof(struct tty_struct));
3816 tty->magic = TTY_MAGIC;
3817 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003818 tty->session = NULL;
3819 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 tty->overrun_time = jiffies;
Alan Cox33f0f882006-01-09 20:54:13 -08003821 tty->buf.head = tty->buf.tail = NULL;
3822 tty_buffer_init(tty);
David Howells65f27f32006-11-22 14:55:48 +00003823 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
Arjan van de Ven5785c952006-09-29 02:00:43 -07003824 mutex_init(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 init_waitqueue_head(&tty->write_wait);
3826 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003827 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003828 mutex_init(&tty->atomic_read_lock);
3829 mutex_init(&tty->atomic_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 spin_lock_init(&tty->read_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07003831 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003833 INIT_WORK(&tty->SAK_work, do_SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
Alan Coxf34d7a52008-04-30 00:54:13 -07003836/**
3837 * tty_put_char - write one character to a tty
3838 * @tty: tty
3839 * @ch: character
3840 *
3841 * Write one byte to the tty using the provided put_char method
3842 * if present. Returns the number of characters successfully output.
3843 *
3844 * Note: the specific put_char operation in the driver layer may go
3845 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003847
Alan Coxf34d7a52008-04-30 00:54:13 -07003848int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849{
Alan Coxf34d7a52008-04-30 00:54:13 -07003850 if (tty->ops->put_char)
3851 return tty->ops->put_char(tty, ch);
3852 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853}
3854
Alan Coxf34d7a52008-04-30 00:54:13 -07003855EXPORT_SYMBOL_GPL(tty_put_char);
3856
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003857static struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
3859/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003860 * tty_register_device - register a tty device
3861 * @driver: the tty driver that describes the tty device
3862 * @index: the index in the tty driver for this tty device
3863 * @device: a struct device that is associated with this tty device.
3864 * This field is optional, if there is no known struct device
3865 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003867 * Returns a pointer to the struct device for this tty device
3868 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003869 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003870 * This call is required to be made to register an individual tty device
3871 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3872 * that bit is not set, this function should not be called by a tty
3873 * driver.
3874 *
3875 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003877
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003878struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3879 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880{
3881 char name[64];
3882 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
3883
3884 if (index >= driver->num) {
3885 printk(KERN_ERR "Attempt to register invalid tty line number "
3886 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003887 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 }
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 if (driver->type == TTY_DRIVER_TYPE_PTY)
3891 pty_line_name(driver, index, name);
3892 else
3893 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003894
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003895 return device_create(tty_class, device, dev, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003899 * tty_unregister_device - unregister a tty device
3900 * @driver: the tty driver that describes the tty device
3901 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003903 * If a tty device is registered with a call to tty_register_device() then
3904 * this function must be called when the tty device is gone.
3905 *
3906 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909void tty_unregister_device(struct tty_driver *driver, unsigned index)
3910{
Alan Cox37bdfb02008-02-08 04:18:47 -08003911 device_destroy(tty_class,
3912 MKDEV(driver->major, driver->minor_start) + index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913}
3914
3915EXPORT_SYMBOL(tty_register_device);
3916EXPORT_SYMBOL(tty_unregister_device);
3917
3918struct tty_driver *alloc_tty_driver(int lines)
3919{
3920 struct tty_driver *driver;
3921
Jean Delvare506eb992007-07-15 23:40:14 -07003922 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 if (driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 driver->magic = TTY_DRIVER_MAGIC;
3925 driver->num = lines;
3926 /* later we'll move allocation of tables here */
3927 }
3928 return driver;
3929}
3930
3931void put_tty_driver(struct tty_driver *driver)
3932{
3933 kfree(driver);
3934}
3935
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003936void tty_set_operations(struct tty_driver *driver,
3937 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
Alan Coxf34d7a52008-04-30 00:54:13 -07003939 driver->ops = op;
3940};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942EXPORT_SYMBOL(alloc_tty_driver);
3943EXPORT_SYMBOL(put_tty_driver);
3944EXPORT_SYMBOL(tty_set_operations);
3945
3946/*
3947 * Called by a tty driver to register itself.
3948 */
3949int tty_register_driver(struct tty_driver *driver)
3950{
3951 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003952 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 dev_t dev;
3954 void **p = NULL;
3955
3956 if (driver->flags & TTY_DRIVER_INSTALLED)
3957 return 0;
3958
Andy Whitcroft543691a62007-05-06 14:49:33 -07003959 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
3960 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 if (!p)
3962 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964
3965 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003966 error = alloc_chrdev_region(&dev, driver->minor_start,
3967 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (!error) {
3969 driver->major = MAJOR(dev);
3970 driver->minor_start = MINOR(dev);
3971 }
3972 } else {
3973 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003974 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976 if (error < 0) {
3977 kfree(p);
3978 return error;
3979 }
3980
3981 if (p) {
3982 driver->ttys = (struct tty_struct **)p;
Alan Coxedc6afc2006-12-08 02:38:44 -08003983 driver->termios = (struct ktermios **)(p + driver->num);
Alan Cox37bdfb02008-02-08 04:18:47 -08003984 driver->termios_locked = (struct ktermios **)
3985 (p + driver->num * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 } else {
3987 driver->ttys = NULL;
3988 driver->termios = NULL;
3989 driver->termios_locked = NULL;
3990 }
3991
3992 cdev_init(&driver->cdev, &tty_fops);
3993 driver->cdev.owner = driver->owner;
3994 error = cdev_add(&driver->cdev, dev, driver->num);
3995 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 unregister_chrdev_region(dev, driver->num);
3997 driver->ttys = NULL;
3998 driver->termios = driver->termios_locked = NULL;
3999 kfree(p);
4000 return error;
4001 }
4002
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004003 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004005 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08004006
4007 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
4008 for (i = 0; i < driver->num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 tty_register_device(driver, i, NULL);
4010 }
4011 proc_tty_register_driver(driver);
4012 return 0;
4013}
4014
4015EXPORT_SYMBOL(tty_register_driver);
4016
4017/*
4018 * Called by a tty driver to unregister itself.
4019 */
4020int tty_unregister_driver(struct tty_driver *driver)
4021{
4022 int i;
Alan Coxedc6afc2006-12-08 02:38:44 -08004023 struct ktermios *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 void *p;
4025
4026 if (driver->refcount)
4027 return -EBUSY;
4028
4029 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
4030 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004031 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004033 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 /*
4036 * Free the termios and termios_locked structures because
4037 * we don't want to get memory leaks when modular tty
4038 * drivers are removed from the kernel.
4039 */
4040 for (i = 0; i < driver->num; i++) {
4041 tp = driver->termios[i];
4042 if (tp) {
4043 driver->termios[i] = NULL;
4044 kfree(tp);
4045 }
4046 tp = driver->termios_locked[i];
4047 if (tp) {
4048 driver->termios_locked[i] = NULL;
4049 kfree(tp);
4050 }
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07004051 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 tty_unregister_device(driver, i);
4053 }
4054 p = driver->ttys;
4055 proc_tty_unregister_driver(driver);
4056 driver->ttys = NULL;
4057 driver->termios = driver->termios_locked = NULL;
4058 kfree(p);
4059 cdev_del(&driver->cdev);
4060 return 0;
4061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062EXPORT_SYMBOL(tty_unregister_driver);
4063
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004064dev_t tty_devnum(struct tty_struct *tty)
4065{
4066 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
4067}
4068EXPORT_SYMBOL(tty_devnum);
4069
4070void proc_clear_tty(struct task_struct *p)
4071{
4072 spin_lock_irq(&p->sighand->siglock);
4073 p->signal->tty = NULL;
4074 spin_unlock_irq(&p->sighand->siglock);
4075}
David S. Miller7cac4ce2007-05-11 14:30:57 -07004076EXPORT_SYMBOL(proc_clear_tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004077
Alan Cox47f86832008-04-30 00:53:30 -07004078/* Called under the sighand lock */
4079
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004080static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004081{
4082 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07004083 unsigned long flags;
4084 /* We should not have a session or pgrp to put here but.... */
4085 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06004086 put_pid(tty->session);
4087 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08004088 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07004089 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
4090 tty->session = get_pid(task_session(tsk));
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004091 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004092 put_pid(tsk->signal->tty_old_pgrp);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004093 tsk->signal->tty = tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08004094 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004095}
4096
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07004097static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004098{
4099 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004100 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004101 spin_unlock_irq(&tsk->sighand->siglock);
4102}
4103
4104struct tty_struct *get_current_tty(void)
4105{
4106 struct tty_struct *tty;
4107 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
4108 tty = current->signal->tty;
4109 /*
4110 * session->tty can be changed/cleared from under us, make sure we
4111 * issue the load. The obtained pointer, when not NULL, is valid as
4112 * long as we hold tty_mutex.
4113 */
4114 barrier();
4115 return tty;
4116}
Heiko Carstensa311f742006-12-13 00:33:41 -08004117EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119/*
4120 * Initialize the console device. This is called *early*, so
4121 * we can't necessarily depend on lots of kernel help here.
4122 * Just do some early initializations, and do the complex setup
4123 * later.
4124 */
4125void __init console_init(void)
4126{
4127 initcall_t *call;
4128
4129 /* Setup the default TTY line discipline. */
4130 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
4131
4132 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08004133 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 * inform about problems etc..
4135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 call = __con_initcall_start;
4137 while (call < __con_initcall_end) {
4138 (*call)();
4139 call++;
4140 }
4141}
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143static int __init tty_class_init(void)
4144{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08004145 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 if (IS_ERR(tty_class))
4147 return PTR_ERR(tty_class);
4148 return 0;
4149}
4150
4151postcore_initcall(tty_class_init);
4152
4153/* 3/2004 jmc: why do these devices exist? */
4154
4155static struct cdev tty_cdev, console_cdev;
4156#ifdef CONFIG_UNIX98_PTYS
4157static struct cdev ptmx_cdev;
4158#endif
4159#ifdef CONFIG_VT
4160static struct cdev vc0_cdev;
4161#endif
4162
4163/*
4164 * Ok, now we can initialize the rest of the tty devices and can count
4165 * on memory allocations, interrupts etc..
4166 */
4167static int __init tty_init(void)
4168{
4169 cdev_init(&tty_cdev, &tty_fops);
4170 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
4171 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
4172 panic("Couldn't register /dev/tty driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004173 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 cdev_init(&console_cdev, &console_fops);
4176 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
4177 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
4178 panic("Couldn't register /dev/console driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004179 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181#ifdef CONFIG_UNIX98_PTYS
4182 cdev_init(&ptmx_cdev, &ptmx_fops);
4183 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
4184 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
4185 panic("Couldn't register /dev/ptmx driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004186 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187#endif
4188
4189#ifdef CONFIG_VT
4190 cdev_init(&vc0_cdev, &console_fops);
4191 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
4192 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
4193 panic("Couldn't register /dev/tty0 driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004194 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 vty_init();
4197#endif
4198 return 0;
4199}
4200module_init(tty_init);