blob: 54c4ada460ee157910452a46735a8b44b83847c9 [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>
Al Viro9f3acc32008-04-24 07:44:08 -040081#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/console.h>
83#include <linux/timer.h>
84#include <linux/ctype.h>
85#include <linux/kd.h>
86#include <linux/mm.h>
87#include <linux/string.h>
88#include <linux/slab.h>
89#include <linux/poll.h>
90#include <linux/proc_fs.h>
91#include <linux/init.h>
92#include <linux/module.h>
93#include <linux/smp_lock.h>
94#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/wait.h>
96#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070097#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010098#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Alan Coxa352def2008-07-16 21:53:12 +0100100#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include <asm/system.h>
102
103#include <linux/kbd_kern.h>
104#include <linux/vt_kern.h>
105#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700108#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#undef TTY_DEBUG_HANGUP
111
112#define TTY_PARANOIA_CHECK 1
113#define CHECK_TTY_COUNT 1
114
Alan Coxedc6afc2006-12-08 02:38:44 -0800115struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .c_iflag = ICRNL | IXON,
117 .c_oflag = OPOST | ONLCR,
118 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
119 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
120 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800121 .c_cc = INIT_C_CC,
122 .c_ispeed = 38400,
123 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126EXPORT_SYMBOL(tty_std_termios);
127
128/* This list gets poked at by procfs and various bits of boot up code. This
129 could do with some rationalisation such as pulling the tty proc function
130 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132LIST_HEAD(tty_drivers); /* linked list of tty drivers */
133
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800134/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800136DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700137EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#ifdef CONFIG_UNIX98_PTYS
140extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static int ptmx_open(struct inode *, struct file *);
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void initialize_tty_struct(struct tty_struct *tty);
145
146static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
147static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800148ssize_t redirected_tty_write(struct file *, const char __user *,
149 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static unsigned int tty_poll(struct file *, poll_table *);
151static int tty_open(struct inode *, struct file *);
152static int tty_release(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700153long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700154#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800155static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700156 unsigned long arg);
157#else
158#define tty_compat_ioctl NULL
159#endif
Alan Cox37bdfb02008-02-08 04:18:47 -0800160static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800161static void release_tty(struct tty_struct *tty, int idx);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700162static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700163static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Alan Coxaf9b8972006-08-27 01:24:01 -0700165/**
166 * alloc_tty_struct - allocate a tty object
167 *
168 * Return a new empty tty structure. The data fields have not
169 * been initialized in any way but has been zeroed
170 *
171 * Locking: none
Alan Coxaf9b8972006-08-27 01:24:01 -0700172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174static struct tty_struct *alloc_tty_struct(void)
175{
Alan Cox1266b1e2006-09-29 02:00:40 -0700176 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Alan Cox33f0f882006-01-09 20:54:13 -0800179static void tty_buffer_free_all(struct tty_struct *);
180
Alan Coxaf9b8972006-08-27 01:24:01 -0700181/**
182 * free_tty_struct - free a disused tty
183 * @tty: tty struct to free
184 *
185 * Free the write buffers, tty queue and tty memory itself.
186 *
187 * Locking: none. Must be called after tty is definitely unused
188 */
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static inline void free_tty_struct(struct tty_struct *tty)
191{
192 kfree(tty->write_buf);
Alan Cox33f0f882006-01-09 20:54:13 -0800193 tty_buffer_free_all(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kfree(tty);
195}
196
197#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
198
Alan Coxaf9b8972006-08-27 01:24:01 -0700199/**
200 * tty_name - return tty naming
201 * @tty: tty structure
202 * @buf: buffer for output
203 *
204 * Convert a tty structure into a name. The name reflects the kernel
205 * naming policy and if udev is in use may not reflect user space
206 *
207 * Locking: none
208 */
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210char *tty_name(struct tty_struct *tty, char *buf)
211{
212 if (!tty) /* Hmm. NULL pointer. That's fun. */
213 strcpy(buf, "NULL tty");
214 else
215 strcpy(buf, tty->name);
216 return buf;
217}
218
219EXPORT_SYMBOL(tty_name);
220
Andrew Mortond769a662005-05-05 16:15:50 -0700221int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 const char *routine)
223{
224#ifdef TTY_PARANOIA_CHECK
225 if (!tty) {
226 printk(KERN_WARNING
227 "null TTY for (%d:%d) in %s\n",
228 imajor(inode), iminor(inode), routine);
229 return 1;
230 }
231 if (tty->magic != TTY_MAGIC) {
232 printk(KERN_WARNING
233 "bad magic number for tty struct (%d:%d) in %s\n",
234 imajor(inode), iminor(inode), routine);
235 return 1;
236 }
237#endif
238 return 0;
239}
240
241static int check_tty_count(struct tty_struct *tty, const char *routine)
242{
243#ifdef CHECK_TTY_COUNT
244 struct list_head *p;
245 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 file_list_lock();
248 list_for_each(p, &tty->tty_files) {
249 count++;
250 }
251 file_list_unlock();
252 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
253 tty->driver->subtype == PTY_TYPE_SLAVE &&
254 tty->link && tty->link->count)
255 count++;
256 if (tty->count != count) {
257 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
258 "!= #fd's(%d) in %s\n",
259 tty->name, tty->count, count, routine);
260 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#endif
263 return 0;
264}
265
266/*
Alan Cox33f0f882006-01-09 20:54:13 -0800267 * Tty buffer allocation management
268 */
269
Alan Cox01da5fd2006-08-27 01:24:02 -0700270/**
271 * tty_buffer_free_all - free buffers used by a tty
272 * @tty: tty to free from
273 *
274 * Remove all the buffers pending on a tty whether queued with data
275 * or in the free ring. Must be called when the tty is no longer in use
276 *
277 * Locking: none
278 */
279
Alan Cox33f0f882006-01-09 20:54:13 -0800280static void tty_buffer_free_all(struct tty_struct *tty)
281{
282 struct tty_buffer *thead;
Alan Cox37bdfb02008-02-08 04:18:47 -0800283 while ((thead = tty->buf.head) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800284 tty->buf.head = thead->next;
285 kfree(thead);
286 }
Alan Cox37bdfb02008-02-08 04:18:47 -0800287 while ((thead = tty->buf.free) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800288 tty->buf.free = thead->next;
289 kfree(thead);
290 }
291 tty->buf.tail = NULL;
Alan Cox01da5fd2006-08-27 01:24:02 -0700292 tty->buf.memory_used = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800293}
294
Alan Cox01da5fd2006-08-27 01:24:02 -0700295/**
296 * tty_buffer_init - prepare a tty buffer structure
297 * @tty: tty to initialise
298 *
299 * Set up the initial state of the buffer management for a tty device.
300 * Must be called before the other tty buffer functions are used.
301 *
302 * Locking: none
303 */
304
Alan Cox33f0f882006-01-09 20:54:13 -0800305static void tty_buffer_init(struct tty_struct *tty)
306{
Paul Fulghum808249c2006-02-03 03:04:41 -0800307 spin_lock_init(&tty->buf.lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800308 tty->buf.head = NULL;
309 tty->buf.tail = NULL;
310 tty->buf.free = NULL;
Alan Cox01da5fd2006-08-27 01:24:02 -0700311 tty->buf.memory_used = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800312}
313
Alan Cox01da5fd2006-08-27 01:24:02 -0700314/**
315 * tty_buffer_alloc - allocate a tty buffer
316 * @tty: tty device
317 * @size: desired size (characters)
318 *
319 * Allocate a new tty buffer to hold the desired number of characters.
320 * Return NULL if out of memory or the allocation would exceed the
321 * per device queue
322 *
323 * Locking: Caller must hold tty->buf.lock
324 */
325
326static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800327{
Alan Cox01da5fd2006-08-27 01:24:02 -0700328 struct tty_buffer *p;
329
330 if (tty->buf.memory_used + size > 65536)
331 return NULL;
332 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
Alan Cox37bdfb02008-02-08 04:18:47 -0800333 if (p == NULL)
Alan Cox33f0f882006-01-09 20:54:13 -0800334 return NULL;
335 p->used = 0;
336 p->size = size;
337 p->next = NULL;
Paul Fulghum8977d922006-02-10 01:51:14 -0800338 p->commit = 0;
339 p->read = 0;
Alan Cox33f0f882006-01-09 20:54:13 -0800340 p->char_buf_ptr = (char *)(p->data);
341 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
Alan Cox01da5fd2006-08-27 01:24:02 -0700342 tty->buf.memory_used += size;
Alan Cox33f0f882006-01-09 20:54:13 -0800343 return p;
344}
345
Alan Cox01da5fd2006-08-27 01:24:02 -0700346/**
347 * tty_buffer_free - free a tty buffer
348 * @tty: tty owning the buffer
349 * @b: the buffer to free
350 *
351 * Free a tty buffer, or add it to the free list according to our
352 * internal strategy
353 *
354 * Locking: Caller must hold tty->buf.lock
355 */
Alan Cox33f0f882006-01-09 20:54:13 -0800356
357static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
358{
359 /* Dumb strategy for now - should keep some stats */
Alan Cox01da5fd2006-08-27 01:24:02 -0700360 tty->buf.memory_used -= b->size;
361 WARN_ON(tty->buf.memory_used < 0);
362
Alan Cox37bdfb02008-02-08 04:18:47 -0800363 if (b->size >= 512)
Alan Cox33f0f882006-01-09 20:54:13 -0800364 kfree(b);
365 else {
366 b->next = tty->buf.free;
367 tty->buf.free = b;
368 }
369}
370
Alan Cox01da5fd2006-08-27 01:24:02 -0700371/**
Alan Cox42fd5522007-08-10 13:01:05 -0700372 * __tty_buffer_flush - flush full tty buffers
373 * @tty: tty to flush
374 *
375 * flush all the buffers containing receive data. Caller must
376 * hold the buffer lock and must have ensured no parallel flush to
377 * ldisc is running.
378 *
379 * Locking: Caller must hold tty->buf.lock
380 */
381
382static void __tty_buffer_flush(struct tty_struct *tty)
383{
384 struct tty_buffer *thead;
385
Alan Cox37bdfb02008-02-08 04:18:47 -0800386 while ((thead = tty->buf.head) != NULL) {
Alan Cox42fd5522007-08-10 13:01:05 -0700387 tty->buf.head = thead->next;
388 tty_buffer_free(tty, thead);
389 }
390 tty->buf.tail = NULL;
391}
392
393/**
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700394 * tty_buffer_flush - flush full tty buffers
395 * @tty: tty to flush
396 *
Alan Cox42fd5522007-08-10 13:01:05 -0700397 * flush all the buffers containing receive data. If the buffer is
398 * being processed by flush_to_ldisc then we defer the processing
399 * to that function
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700400 *
401 * Locking: none
402 */
403
404static void tty_buffer_flush(struct tty_struct *tty)
405{
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700406 unsigned long flags;
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700407 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox42fd5522007-08-10 13:01:05 -0700408
409 /* If the data is being pushed to the tty layer then we can't
410 process it here. Instead set a flag and the flush_to_ldisc
411 path will process the flush request before it exits */
412 if (test_bit(TTY_FLUSHING, &tty->flags)) {
413 set_bit(TTY_FLUSHPENDING, &tty->flags);
414 spin_unlock_irqrestore(&tty->buf.lock, flags);
415 wait_event(tty->read_wait,
416 test_bit(TTY_FLUSHPENDING, &tty->flags) == 0);
417 return;
418 } else
419 __tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700420 spin_unlock_irqrestore(&tty->buf.lock, flags);
421}
422
423/**
Alan Cox01da5fd2006-08-27 01:24:02 -0700424 * tty_buffer_find - find a free tty buffer
425 * @tty: tty owning the buffer
426 * @size: characters wanted
427 *
428 * Locate an existing suitable tty buffer or if we are lacking one then
429 * allocate a new one. We round our buffers off in 256 character chunks
430 * to get better allocation behaviour.
431 *
432 * Locking: Caller must hold tty->buf.lock
433 */
434
Alan Cox33f0f882006-01-09 20:54:13 -0800435static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
436{
437 struct tty_buffer **tbh = &tty->buf.free;
Alan Cox37bdfb02008-02-08 04:18:47 -0800438 while ((*tbh) != NULL) {
Alan Cox33f0f882006-01-09 20:54:13 -0800439 struct tty_buffer *t = *tbh;
Alan Cox37bdfb02008-02-08 04:18:47 -0800440 if (t->size >= size) {
Alan Cox33f0f882006-01-09 20:54:13 -0800441 *tbh = t->next;
442 t->next = NULL;
443 t->used = 0;
Paul Fulghum8977d922006-02-10 01:51:14 -0800444 t->commit = 0;
445 t->read = 0;
Alan Cox01da5fd2006-08-27 01:24:02 -0700446 tty->buf.memory_used += t->size;
Alan Cox33f0f882006-01-09 20:54:13 -0800447 return t;
448 }
449 tbh = &((*tbh)->next);
450 }
451 /* Round the buffer size out */
Alan Cox37bdfb02008-02-08 04:18:47 -0800452 size = (size + 0xFF) & ~0xFF;
Alan Cox01da5fd2006-08-27 01:24:02 -0700453 return tty_buffer_alloc(tty, size);
Alan Cox33f0f882006-01-09 20:54:13 -0800454 /* Should possibly check if this fails for the largest buffer we
455 have queued and recycle that ? */
456}
457
Alan Cox01da5fd2006-08-27 01:24:02 -0700458/**
459 * tty_buffer_request_room - grow tty buffer if needed
460 * @tty: tty structure
461 * @size: size desired
462 *
463 * Make at least size bytes of linear space available for the tty
464 * buffer. If we fail return the size we managed to find.
465 *
466 * Locking: Takes tty->buf.lock
467 */
Alan Cox33f0f882006-01-09 20:54:13 -0800468int tty_buffer_request_room(struct tty_struct *tty, size_t size)
469{
Paul Fulghum808249c2006-02-03 03:04:41 -0800470 struct tty_buffer *b, *n;
471 int left;
472 unsigned long flags;
473
474 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800475
476 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
477 remove this conditional if its worth it. This would be invisible
478 to the callers */
Paul Fulghum33b37a32006-06-28 04:26:49 -0700479 if ((b = tty->buf.tail) != NULL)
Alan Cox33f0f882006-01-09 20:54:13 -0800480 left = b->size - b->used;
Paul Fulghum33b37a32006-06-28 04:26:49 -0700481 else
Paul Fulghum808249c2006-02-03 03:04:41 -0800482 left = 0;
483
484 if (left < size) {
485 /* This is the slow path - looking for new buffers to use */
486 if ((n = tty_buffer_find(tty, size)) != NULL) {
487 if (b != NULL) {
488 b->next = n;
Paul Fulghum8977d922006-02-10 01:51:14 -0800489 b->commit = b->used;
Paul Fulghum808249c2006-02-03 03:04:41 -0800490 } else
491 tty->buf.head = n;
492 tty->buf.tail = n;
Paul Fulghum808249c2006-02-03 03:04:41 -0800493 } else
494 size = left;
495 }
496
497 spin_unlock_irqrestore(&tty->buf.lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800498 return size;
499}
Alan Cox33f0f882006-01-09 20:54:13 -0800500EXPORT_SYMBOL_GPL(tty_buffer_request_room);
501
Alan Coxaf9b8972006-08-27 01:24:01 -0700502/**
503 * tty_insert_flip_string - Add characters to the tty buffer
504 * @tty: tty structure
505 * @chars: characters
506 * @size: size
507 *
508 * Queue a series of bytes to the tty buffering. All the characters
509 * passed are marked as without error. Returns the number added.
510 *
511 * Locking: Called functions may take tty->buf.lock
512 */
513
Andrew Mortone1a25092006-04-10 22:54:05 -0700514int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
515 size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800516{
517 int copied = 0;
518 do {
519 int space = tty_buffer_request_room(tty, size - copied);
520 struct tty_buffer *tb = tty->buf.tail;
521 /* If there is no space then tb may be NULL */
Alan Cox37bdfb02008-02-08 04:18:47 -0800522 if (unlikely(space == 0))
Alan Cox33f0f882006-01-09 20:54:13 -0800523 break;
524 memcpy(tb->char_buf_ptr + tb->used, chars, space);
525 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
526 tb->used += space;
527 copied += space;
528 chars += space;
Alexey Dobriyan527063b2006-09-29 01:59:50 -0700529 /* There is a small chance that we need to split the data over
530 several buffers. If this is the case we must loop */
531 } while (unlikely(size > copied));
Alan Cox33f0f882006-01-09 20:54:13 -0800532 return copied;
533}
Andrew Mortonee37df72006-03-31 02:30:35 -0800534EXPORT_SYMBOL(tty_insert_flip_string);
Alan Cox33f0f882006-01-09 20:54:13 -0800535
Alan Coxaf9b8972006-08-27 01:24:01 -0700536/**
537 * tty_insert_flip_string_flags - Add characters to the tty buffer
538 * @tty: tty structure
539 * @chars: characters
540 * @flags: flag bytes
541 * @size: size
542 *
543 * Queue a series of bytes to the tty buffering. For each character
544 * the flags array indicates the status of the character. Returns the
545 * number added.
546 *
547 * Locking: Called functions may take tty->buf.lock
548 */
549
Andrew Mortone1a25092006-04-10 22:54:05 -0700550int tty_insert_flip_string_flags(struct tty_struct *tty,
551 const unsigned char *chars, const char *flags, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800552{
553 int copied = 0;
554 do {
555 int space = tty_buffer_request_room(tty, size - copied);
556 struct tty_buffer *tb = tty->buf.tail;
557 /* If there is no space then tb may be NULL */
Alan Cox37bdfb02008-02-08 04:18:47 -0800558 if (unlikely(space == 0))
Alan Cox33f0f882006-01-09 20:54:13 -0800559 break;
560 memcpy(tb->char_buf_ptr + tb->used, chars, space);
561 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
562 tb->used += space;
563 copied += space;
564 chars += space;
565 flags += space;
Alexey Dobriyan527063b2006-09-29 01:59:50 -0700566 /* There is a small chance that we need to split the data over
567 several buffers. If this is the case we must loop */
568 } while (unlikely(size > copied));
Alan Cox33f0f882006-01-09 20:54:13 -0800569 return copied;
570}
Tobias Powalowskiff4547f2006-05-22 22:35:28 -0700571EXPORT_SYMBOL(tty_insert_flip_string_flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800572
Alan Coxaf9b8972006-08-27 01:24:01 -0700573/**
574 * tty_schedule_flip - push characters to ldisc
575 * @tty: tty to push from
576 *
577 * Takes any pending buffers and transfers their ownership to the
578 * ldisc side of the queue. It then schedules those characters for
579 * processing by the line discipline.
580 *
581 * Locking: Takes tty->buf.lock
582 */
583
Andrew Mortone1a25092006-04-10 22:54:05 -0700584void tty_schedule_flip(struct tty_struct *tty)
585{
586 unsigned long flags;
587 spin_lock_irqsave(&tty->buf.lock, flags);
Paul Fulghum33b37a32006-06-28 04:26:49 -0700588 if (tty->buf.tail != NULL)
Andrew Mortone1a25092006-04-10 22:54:05 -0700589 tty->buf.tail->commit = tty->buf.tail->used;
Andrew Mortone1a25092006-04-10 22:54:05 -0700590 spin_unlock_irqrestore(&tty->buf.lock, flags);
591 schedule_delayed_work(&tty->buf.work, 1);
592}
593EXPORT_SYMBOL(tty_schedule_flip);
Alan Cox33f0f882006-01-09 20:54:13 -0800594
Alan Coxaf9b8972006-08-27 01:24:01 -0700595/**
596 * tty_prepare_flip_string - make room for characters
597 * @tty: tty
598 * @chars: return pointer for character write area
599 * @size: desired size
600 *
Alan Cox33f0f882006-01-09 20:54:13 -0800601 * Prepare a block of space in the buffer for data. Returns the length
602 * available and buffer pointer to the space which is now allocated and
603 * accounted for as ready for normal characters. This is used for drivers
604 * that need their own block copy routines into the buffer. There is no
605 * guarantee the buffer is a DMA target!
Alan Coxaf9b8972006-08-27 01:24:01 -0700606 *
607 * Locking: May call functions taking tty->buf.lock
Alan Cox33f0f882006-01-09 20:54:13 -0800608 */
609
Alan Cox37bdfb02008-02-08 04:18:47 -0800610int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars,
611 size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800612{
613 int space = tty_buffer_request_room(tty, size);
Paul Fulghum808249c2006-02-03 03:04:41 -0800614 if (likely(space)) {
615 struct tty_buffer *tb = tty->buf.tail;
616 *chars = tb->char_buf_ptr + tb->used;
617 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
618 tb->used += space;
619 }
Alan Cox33f0f882006-01-09 20:54:13 -0800620 return space;
621}
622
623EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
624
Alan Coxaf9b8972006-08-27 01:24:01 -0700625/**
626 * tty_prepare_flip_string_flags - make room for characters
627 * @tty: tty
628 * @chars: return pointer for character write area
629 * @flags: return pointer for status flag write area
630 * @size: desired size
631 *
Alan Cox33f0f882006-01-09 20:54:13 -0800632 * Prepare a block of space in the buffer for data. Returns the length
633 * available and buffer pointer to the space which is now allocated and
634 * accounted for as ready for characters. This is used for drivers
635 * that need their own block copy routines into the buffer. There is no
636 * guarantee the buffer is a DMA target!
Alan Coxaf9b8972006-08-27 01:24:01 -0700637 *
638 * Locking: May call functions taking tty->buf.lock
Alan Cox33f0f882006-01-09 20:54:13 -0800639 */
640
Alan Cox37bdfb02008-02-08 04:18:47 -0800641int tty_prepare_flip_string_flags(struct tty_struct *tty,
642 unsigned char **chars, char **flags, size_t size)
Alan Cox33f0f882006-01-09 20:54:13 -0800643{
644 int space = tty_buffer_request_room(tty, size);
Paul Fulghum808249c2006-02-03 03:04:41 -0800645 if (likely(space)) {
646 struct tty_buffer *tb = tty->buf.tail;
647 *chars = tb->char_buf_ptr + tb->used;
648 *flags = tb->flag_buf_ptr + tb->used;
649 tb->used += space;
650 }
Alan Cox33f0f882006-01-09 20:54:13 -0800651 return space;
652}
653
654EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
655
656
657
Alan Coxaf9b8972006-08-27 01:24:01 -0700658/**
659 * tty_set_termios_ldisc - set ldisc field
660 * @tty: tty structure
661 * @num: line discipline number
662 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * This is probably overkill for real world processors but
Alan Cox37bdfb02008-02-08 04:18:47 -0800664 * they are not on hot paths so a little discipline won't do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * any harm.
Alan Coxaf9b8972006-08-27 01:24:01 -0700666 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800667 * Locking: takes termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
671{
Arjan van de Ven5785c952006-09-29 02:00:43 -0700672 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 tty->termios->c_line = num;
Arjan van de Ven5785c952006-09-29 02:00:43 -0700674 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677/*
678 * This guards the refcounted line discipline lists. The lock
679 * must be taken with irqs off because there are hangup path
680 * callers who will do ldisc lookups and cannot sleep.
681 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683static DEFINE_SPINLOCK(tty_ldisc_lock);
684static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
Alan Cox37bdfb02008-02-08 04:18:47 -0800685/* Line disc dispatch table */
Alan Coxa352def2008-07-16 21:53:12 +0100686static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Alan Coxaf9b8972006-08-27 01:24:01 -0700688/**
689 * tty_register_ldisc - install a line discipline
690 * @disc: ldisc number
691 * @new_ldisc: pointer to the ldisc object
692 *
693 * Installs a new line discipline into the kernel. The discipline
694 * is set up as unreferenced and then made available to the kernel
695 * from this point onwards.
696 *
697 * Locking:
698 * takes tty_ldisc_lock to guard against ldisc races
699 */
700
Alan Coxa352def2008-07-16 21:53:12 +0100701int tty_register_ldisc(int disc, struct tty_ldisc_ops *new_ldisc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
703 unsigned long flags;
704 int ret = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (disc < N_TTY || disc >= NR_LDISCS)
707 return -EINVAL;
Alan Cox37bdfb02008-02-08 04:18:47 -0800708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +0100710 tty_ldiscs[disc] = new_ldisc;
711 new_ldisc->num = disc;
712 new_ldisc->refcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -0800714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return ret;
716}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717EXPORT_SYMBOL(tty_register_ldisc);
718
Alan Coxaf9b8972006-08-27 01:24:01 -0700719/**
720 * tty_unregister_ldisc - unload a line discipline
721 * @disc: ldisc number
722 * @new_ldisc: pointer to the ldisc object
723 *
724 * Remove a line discipline from the kernel providing it is not
725 * currently in use.
726 *
727 * Locking:
728 * takes tty_ldisc_lock to guard against ldisc races
729 */
730
Alexey Dobriyanbfb07592005-06-23 00:10:32 -0700731int tty_unregister_ldisc(int disc)
732{
733 unsigned long flags;
734 int ret = 0;
735
736 if (disc < N_TTY || disc >= NR_LDISCS)
737 return -EINVAL;
738
739 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +0100740 if (tty_ldiscs[disc]->refcount)
Alexey Dobriyanbfb07592005-06-23 00:10:32 -0700741 ret = -EBUSY;
742 else
Alan Coxa352def2008-07-16 21:53:12 +0100743 tty_ldiscs[disc] = NULL;
Alexey Dobriyanbfb07592005-06-23 00:10:32 -0700744 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
745
746 return ret;
747}
748EXPORT_SYMBOL(tty_unregister_ldisc);
749
Alan Coxa352def2008-07-16 21:53:12 +0100750
751/**
752 * tty_ldisc_try_get - try and reference an ldisc
753 * @disc: ldisc number
754 * @ld: tty ldisc structure to complete
755 *
756 * Attempt to open and lock a line discipline into place. Return
757 * the line discipline refcounted and assigned in ld. On an error
758 * report the error code back
759 */
760
761static int tty_ldisc_try_get(int disc, struct tty_ldisc *ld)
762{
763 unsigned long flags;
764 struct tty_ldisc_ops *ldops;
765 int err = -EINVAL;
766
767 spin_lock_irqsave(&tty_ldisc_lock, flags);
768 ld->ops = NULL;
769 ldops = tty_ldiscs[disc];
770 /* Check the entry is defined */
771 if (ldops) {
772 /* If the module is being unloaded we can't use it */
773 if (!try_module_get(ldops->owner))
774 err = -EAGAIN;
775 else {
776 /* lock it */
777 ldops->refcount++;
778 ld->ops = ldops;
779 err = 0;
780 }
781 }
782 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
783 return err;
784}
785
Alan Coxaf9b8972006-08-27 01:24:01 -0700786/**
787 * tty_ldisc_get - take a reference to an ldisc
788 * @disc: ldisc number
Alan Coxa352def2008-07-16 21:53:12 +0100789 * @ld: tty line discipline structure to use
Alan Coxaf9b8972006-08-27 01:24:01 -0700790 *
791 * Takes a reference to a line discipline. Deals with refcounts and
792 * module locking counts. Returns NULL if the discipline is not available.
793 * Returns a pointer to the discipline and bumps the ref count if it is
794 * available
795 *
796 * Locking:
797 * takes tty_ldisc_lock to guard against ldisc races
798 */
799
Alan Coxa352def2008-07-16 21:53:12 +0100800static int tty_ldisc_get(int disc, struct tty_ldisc *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Alan Coxa352def2008-07-16 21:53:12 +0100802 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (disc < N_TTY || disc >= NR_LDISCS)
Alan Coxa352def2008-07-16 21:53:12 +0100805 return -EINVAL;
806 err = tty_ldisc_try_get(disc, ld);
807 if (err == -EAGAIN) {
808 request_module("tty-ldisc-%d", disc);
809 err = tty_ldisc_try_get(disc, ld);
810 }
811 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Alan Coxaf9b8972006-08-27 01:24:01 -0700814/**
815 * tty_ldisc_put - drop ldisc reference
816 * @disc: ldisc number
817 *
818 * Drop a reference to a line discipline. Manage refcounts and
819 * module usage counts
820 *
821 * Locking:
822 * takes tty_ldisc_lock to guard against ldisc races
823 */
824
Alan Coxa352def2008-07-16 21:53:12 +0100825static void tty_ldisc_put(struct tty_ldisc_ops *ld)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned long flags;
Alan Coxa352def2008-07-16 21:53:12 +0100828 int disc = ld->num;
Alan Cox37bdfb02008-02-08 04:18:47 -0800829
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200830 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
Alan Cox37bdfb02008-02-08 04:18:47 -0800831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +0100833 ld = tty_ldiscs[disc];
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200834 BUG_ON(ld->refcount == 0);
835 ld->refcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 module_put(ld->owner);
837 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
838}
Alan Cox37bdfb02008-02-08 04:18:47 -0800839
Alan Coxa352def2008-07-16 21:53:12 +0100840static void * tty_ldiscs_seq_start(struct seq_file *m, loff_t *pos)
841{
842 return (*pos < NR_LDISCS) ? pos : NULL;
843}
844
845static void * tty_ldiscs_seq_next(struct seq_file *m, void *v, loff_t *pos)
846{
847 (*pos)++;
848 return (*pos < NR_LDISCS) ? pos : NULL;
849}
850
851static void tty_ldiscs_seq_stop(struct seq_file *m, void *v)
852{
853}
854
855static int tty_ldiscs_seq_show(struct seq_file *m, void *v)
856{
857 int i = *(loff_t *)v;
858 struct tty_ldisc ld;
859
860 if (tty_ldisc_get(i, &ld) < 0)
861 return 0;
862 seq_printf(m, "%-10s %2d\n", ld.ops->name ? ld.ops->name : "???", i);
863 tty_ldisc_put(ld.ops);
864 return 0;
865}
866
867static const struct seq_operations tty_ldiscs_seq_ops = {
868 .start = tty_ldiscs_seq_start,
869 .next = tty_ldiscs_seq_next,
870 .stop = tty_ldiscs_seq_stop,
871 .show = tty_ldiscs_seq_show,
872};
873
874static int proc_tty_ldiscs_open(struct inode *inode, struct file *file)
875{
876 return seq_open(file, &tty_ldiscs_seq_ops);
877}
878
879const struct file_operations tty_ldiscs_proc_fops = {
880 .owner = THIS_MODULE,
881 .open = proc_tty_ldiscs_open,
882 .read = seq_read,
883 .llseek = seq_lseek,
884 .release = seq_release,
885};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Alan Coxaf9b8972006-08-27 01:24:01 -0700887/**
888 * tty_ldisc_assign - set ldisc on a tty
889 * @tty: tty to assign
890 * @ld: line discipline
891 *
892 * Install an instance of a line discipline into a tty structure. The
893 * ldisc must have a reference count above zero to ensure it remains/
894 * The tty instance refcount starts at zero.
895 *
896 * Locking:
897 * Caller must hold references
898 */
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
901{
Alan Coxa352def2008-07-16 21:53:12 +0100902 ld->refcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 tty->ldisc = *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
906/**
907 * tty_ldisc_try - internal helper
908 * @tty: the tty
909 *
910 * Make a single attempt to grab and bump the refcount on
911 * the tty ldisc. Return 0 on failure or 1 on success. This is
912 * used to implement both the waiting and non waiting versions
913 * of tty_ldisc_ref
Alan Coxaf9b8972006-08-27 01:24:01 -0700914 *
915 * Locking: takes tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
917
918static int tty_ldisc_try(struct tty_struct *tty)
919{
920 unsigned long flags;
921 struct tty_ldisc *ld;
922 int ret = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 spin_lock_irqsave(&tty_ldisc_lock, flags);
925 ld = &tty->ldisc;
Alan Cox37bdfb02008-02-08 04:18:47 -0800926 if (test_bit(TTY_LDISC, &tty->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 ld->refcount++;
928 ret = 1;
929 }
930 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
931 return ret;
932}
933
934/**
935 * tty_ldisc_ref_wait - wait for the tty ldisc
936 * @tty: tty device
937 *
Alan Cox37bdfb02008-02-08 04:18:47 -0800938 * Dereference the line discipline for the terminal and take a
939 * reference to it. If the line discipline is in flux then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * wait patiently until it changes.
941 *
942 * Note: Must not be called from an IRQ/timer context. The caller
943 * must also be careful not to hold other locks that will deadlock
944 * against a discipline change, such as an existing ldisc reference
945 * (which we check for)
Alan Coxaf9b8972006-08-27 01:24:01 -0700946 *
947 * Locking: call functions take tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
951{
952 /* wait_event is a macro */
953 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
Alan Cox37bdfb02008-02-08 04:18:47 -0800954 if (tty->ldisc.refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 printk(KERN_ERR "tty_ldisc_ref_wait\n");
956 return &tty->ldisc;
957}
958
959EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
960
961/**
962 * tty_ldisc_ref - get the tty ldisc
963 * @tty: tty device
964 *
Alan Cox37bdfb02008-02-08 04:18:47 -0800965 * Dereference the line discipline for the terminal and take a
966 * reference to it. If the line discipline is in flux then
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * return NULL. Can be called from IRQ and timer functions.
Alan Coxaf9b8972006-08-27 01:24:01 -0700968 *
969 * Locking: called functions take tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
973{
Alan Cox37bdfb02008-02-08 04:18:47 -0800974 if (tty_ldisc_try(tty))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return &tty->ldisc;
976 return NULL;
977}
978
979EXPORT_SYMBOL_GPL(tty_ldisc_ref);
980
981/**
982 * tty_ldisc_deref - free a tty ldisc reference
983 * @ld: reference to free up
984 *
985 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
986 * be called in IRQ context.
Alan Coxaf9b8972006-08-27 01:24:01 -0700987 *
988 * Locking: takes tty_ldisc_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991void tty_ldisc_deref(struct tty_ldisc *ld)
992{
993 unsigned long flags;
994
Eric Sesterhenn56ee4822006-03-26 18:17:21 +0200995 BUG_ON(ld == NULL);
Alan Cox37bdfb02008-02-08 04:18:47 -0800996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -0800998 if (ld->refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
1000 else
1001 ld->refcount--;
Alan Cox37bdfb02008-02-08 04:18:47 -08001002 if (ld->refcount == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 wake_up(&tty_ldisc_wait);
1004 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
1005}
1006
1007EXPORT_SYMBOL_GPL(tty_ldisc_deref);
1008
1009/**
1010 * tty_ldisc_enable - allow ldisc use
1011 * @tty: terminal to activate ldisc on
1012 *
1013 * Set the TTY_LDISC flag when the line discipline can be called
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001014 * again. Do necessary wakeups for existing sleepers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 *
1016 * Note: nobody should set this bit except via this function. Clearing
1017 * directly is allowed.
1018 */
1019
1020static void tty_ldisc_enable(struct tty_struct *tty)
1021{
1022 set_bit(TTY_LDISC, &tty->flags);
1023 wake_up(&tty_ldisc_wait);
1024}
Alan Cox37bdfb02008-02-08 04:18:47 -08001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/**
Alan Coxa352def2008-07-16 21:53:12 +01001027 * tty_ldisc_restore - helper for tty ldisc change
1028 * @tty: tty to recover
1029 * @old: previous ldisc
1030 *
1031 * Restore the previous line discipline or N_TTY when a line discipline
1032 * change fails due to an open error
1033 */
1034
1035static void tty_ldisc_restore(struct tty_struct *tty, struct tty_ldisc *old)
1036{
1037 char buf[64];
1038 struct tty_ldisc new_ldisc;
1039
1040 /* There is an outstanding reference here so this is safe */
1041 tty_ldisc_get(old->ops->num, old);
1042 tty_ldisc_assign(tty, old);
1043 tty_set_termios_ldisc(tty, old->ops->num);
1044 if (old->ops->open && (old->ops->open(tty) < 0)) {
1045 tty_ldisc_put(old->ops);
1046 /* This driver is always present */
1047 if (tty_ldisc_get(N_TTY, &new_ldisc) < 0)
1048 panic("n_tty: get");
1049 tty_ldisc_assign(tty, &new_ldisc);
1050 tty_set_termios_ldisc(tty, N_TTY);
1051 if (new_ldisc.ops->open) {
1052 int r = new_ldisc.ops->open(tty);
1053 if (r < 0)
1054 panic("Couldn't open N_TTY ldisc for "
1055 "%s --- error %d.",
1056 tty_name(tty, buf), r);
1057 }
1058 }
1059}
1060
1061/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 * tty_set_ldisc - set line discipline
1063 * @tty: the terminal to set
1064 * @ldisc: the line discipline
1065 *
1066 * Set the discipline of a tty line. Must be called from a process
1067 * context.
Alan Coxaf9b8972006-08-27 01:24:01 -07001068 *
1069 * Locking: takes tty_ldisc_lock.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001070 * called functions take termios_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
1074{
Alan Coxa352def2008-07-16 21:53:12 +01001075 int retval;
1076 struct tty_ldisc o_ldisc, new_ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int work;
1078 unsigned long flags;
Jason Baronff55fe22005-09-09 13:01:57 -07001079 struct tty_struct *o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081restart:
Alan Coxa352def2008-07-16 21:53:12 +01001082 /* This is a bit ugly for now but means we can break the 'ldisc
1083 is part of the tty struct' assumption later */
1084 retval = tty_ldisc_get(ldisc, &new_ldisc);
1085 if (retval)
1086 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Alan Cox33f0f882006-01-09 20:54:13 -08001088 /*
Alan Cox33f0f882006-01-09 20:54:13 -08001089 * Problem: What do we do if this blocks ?
1090 */
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 tty_wait_until_sent(tty, 0);
1093
Alan Coxa352def2008-07-16 21:53:12 +01001094 if (tty->ldisc.ops->num == ldisc) {
1095 tty_ldisc_put(new_ldisc.ops);
Jason Baronff55fe22005-09-09 13:01:57 -07001096 return 0;
1097 }
1098
Paul Fulghumae030e42007-05-09 02:33:38 -07001099 /*
1100 * No more input please, we are switching. The new ldisc
1101 * will update this value in the ldisc open function
1102 */
1103
1104 tty->receive_room = 0;
1105
Jason Baronff55fe22005-09-09 13:01:57 -07001106 o_ldisc = tty->ldisc;
1107 o_tty = tty->link;
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 /*
1110 * Make sure we don't change while someone holds a
1111 * reference to the line discipline. The TTY_LDISC bit
1112 * prevents anyone taking a reference once it is clear.
1113 * We need the lock to avoid racing reference takers.
1114 */
Jason Baronff55fe22005-09-09 13:01:57 -07001115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 spin_lock_irqsave(&tty_ldisc_lock, flags);
Jason Baronff55fe22005-09-09 13:01:57 -07001117 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
Alan Cox37bdfb02008-02-08 04:18:47 -08001118 if (tty->ldisc.refcount) {
Jason Baronff55fe22005-09-09 13:01:57 -07001119 /* Free the new ldisc we grabbed. Must drop the lock
1120 first. */
1121 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +01001122 tty_ldisc_put(o_ldisc.ops);
Jason Baronff55fe22005-09-09 13:01:57 -07001123 /*
1124 * There are several reasons we may be busy, including
1125 * random momentary I/O traffic. We must therefore
1126 * retry. We could distinguish between blocking ops
Alan Cox37bdfb02008-02-08 04:18:47 -08001127 * and retries if we made tty_ldisc_wait() smarter.
1128 * That is up for discussion.
Jason Baronff55fe22005-09-09 13:01:57 -07001129 */
1130 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
1131 return -ERESTARTSYS;
1132 goto restart;
1133 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001134 if (o_tty && o_tty->ldisc.refcount) {
Jason Baronff55fe22005-09-09 13:01:57 -07001135 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +01001136 tty_ldisc_put(o_tty->ldisc.ops);
Jason Baronff55fe22005-09-09 13:01:57 -07001137 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
1138 return -ERESTARTSYS;
1139 goto restart;
1140 }
1141 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001142 /*
1143 * If the TTY_LDISC bit is set, then we are racing against
1144 * another ldisc change
1145 */
Jason Baronff55fe22005-09-09 13:01:57 -07001146 if (!test_bit(TTY_LDISC, &tty->flags)) {
Alan Coxa352def2008-07-16 21:53:12 +01001147 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +01001149 tty_ldisc_put(new_ldisc.ops);
Jason Baronff55fe22005-09-09 13:01:57 -07001150 ld = tty_ldisc_ref_wait(tty);
1151 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 goto restart;
1153 }
Jason Baronff55fe22005-09-09 13:01:57 -07001154
1155 clear_bit(TTY_LDISC, &tty->flags);
Paul Fulghum817d6d32006-06-28 04:26:47 -07001156 if (o_tty)
Jason Baronff55fe22005-09-09 13:01:57 -07001157 clear_bit(TTY_LDISC, &o_tty->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +01001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * From this point on we know nobody has an ldisc
1162 * usage reference, nor can they obtain one until
1163 * we say so later on.
1164 */
Jason Baronff55fe22005-09-09 13:01:57 -07001165
Alan Cox33f0f882006-01-09 20:54:13 -08001166 work = cancel_delayed_work(&tty->buf.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
Alan Cox33f0f882006-01-09 20:54:13 -08001168 * Wait for ->hangup_work and ->buf.work handlers to terminate
Alan Coxa352def2008-07-16 21:53:12 +01001169 * MUST NOT hold locks here.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 flush_scheduled_work();
1172 /* Shutdown the current discipline. */
Alan Coxa352def2008-07-16 21:53:12 +01001173 if (o_ldisc.ops->close)
1174 (o_ldisc.ops->close)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* Now set up the new line discipline. */
Alan Coxa352def2008-07-16 21:53:12 +01001177 tty_ldisc_assign(tty, &new_ldisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 tty_set_termios_ldisc(tty, ldisc);
Alan Coxa352def2008-07-16 21:53:12 +01001179 if (new_ldisc.ops->open)
1180 retval = (new_ldisc.ops->open)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (retval < 0) {
Alan Coxa352def2008-07-16 21:53:12 +01001182 tty_ldisc_put(new_ldisc.ops);
1183 tty_ldisc_restore(tty, &o_ldisc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185 /* At this point we hold a reference to the new ldisc and a
1186 a reference to the old ldisc. If we ended up flipping back
1187 to the existing ldisc we have two references to it */
Alan Cox37bdfb02008-02-08 04:18:47 -08001188
Alan Coxa352def2008-07-16 21:53:12 +01001189 if (tty->ldisc.ops->num != o_ldisc.ops->num && tty->ops->set_ldisc)
Alan Coxf34d7a52008-04-30 00:54:13 -07001190 tty->ops->set_ldisc(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001191
Alan Coxa352def2008-07-16 21:53:12 +01001192 tty_ldisc_put(o_ldisc.ops);
Alan Cox37bdfb02008-02-08 04:18:47 -08001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Allow ldisc referencing to occur as soon as the driver
1196 * ldisc callback completes.
1197 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tty_ldisc_enable(tty);
Jason Baronff55fe22005-09-09 13:01:57 -07001200 if (o_tty)
1201 tty_ldisc_enable(o_tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /* Restart it in case no characters kick it off. Safe if
1204 already running */
Jason Baronff55fe22005-09-09 13:01:57 -07001205 if (work)
Alan Cox33f0f882006-01-09 20:54:13 -08001206 schedule_delayed_work(&tty->buf.work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return retval;
1208}
1209
Alan Coxaf9b8972006-08-27 01:24:01 -07001210/**
1211 * get_tty_driver - find device of a tty
1212 * @dev_t: device identifier
1213 * @index: returns the index of the tty
1214 *
1215 * This routine returns a tty driver structure, given a device number
1216 * and also passes back the index number.
1217 *
1218 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static struct tty_driver *get_tty_driver(dev_t device, int *index)
1222{
1223 struct tty_driver *p;
1224
1225 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1226 dev_t base = MKDEV(p->major, p->minor_start);
1227 if (device < base || device >= base + p->num)
1228 continue;
1229 *index = device - base;
1230 return p;
1231 }
1232 return NULL;
1233}
1234
Jason Wesself2d937f2008-04-17 20:05:37 +02001235#ifdef CONFIG_CONSOLE_POLL
1236
1237/**
1238 * tty_find_polling_driver - find device of a polled tty
1239 * @name: name string to match
1240 * @line: pointer to resulting tty line nr
1241 *
1242 * This routine returns a tty driver structure, given a name
1243 * and the condition that the tty driver is capable of polled
1244 * operation.
1245 */
1246struct tty_driver *tty_find_polling_driver(char *name, int *line)
1247{
1248 struct tty_driver *p, *res = NULL;
1249 int tty_line = 0;
1250 char *str;
1251
1252 mutex_lock(&tty_mutex);
1253 /* Search through the tty devices to look for a match */
1254 list_for_each_entry(p, &tty_drivers, tty_drivers) {
1255 str = name + strlen(p->name);
1256 tty_line = simple_strtoul(str, &str, 10);
1257 if (*str == ',')
1258 str++;
1259 if (*str == '\0')
Harvey Harrison8da56302008-04-28 14:13:20 -07001260 str = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +02001261
Alan Coxf34d7a52008-04-30 00:54:13 -07001262 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
1263 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
Jason Wesself2d937f2008-04-17 20:05:37 +02001264 res = p;
1265 *line = tty_line;
1266 break;
1267 }
1268 }
1269 mutex_unlock(&tty_mutex);
1270
1271 return res;
1272}
1273EXPORT_SYMBOL_GPL(tty_find_polling_driver);
1274#endif
1275
Alan Coxaf9b8972006-08-27 01:24:01 -07001276/**
1277 * tty_check_change - check for POSIX terminal changes
1278 * @tty: tty to check
1279 *
1280 * If we try to write to, or set the state of, a terminal and we're
1281 * not in the foreground, send a SIGTTOU. If the signal is blocked or
1282 * ignored, go ahead and perform the operation. (POSIX 7.2)
1283 *
Alan Cox978e5952008-04-30 00:53:59 -07001284 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001286
Alan Cox37bdfb02008-02-08 04:18:47 -08001287int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Alan Cox47f86832008-04-30 00:53:30 -07001289 unsigned long flags;
1290 int ret = 0;
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (current->signal->tty != tty)
1293 return 0;
Alan Cox47f86832008-04-30 00:53:30 -07001294
1295 spin_lock_irqsave(&tty->ctrl_lock, flags);
1296
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001297 if (!tty->pgrp) {
1298 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -07001299 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001301 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -07001302 goto out_unlock;
1303 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -07001305 goto out;
1306 if (is_current_pgrp_orphaned()) {
1307 ret = -EIO;
1308 goto out;
1309 }
Oleg Nesterov040b6362007-06-01 00:46:53 -07001310 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
1311 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -07001312 ret = -ERESTARTSYS;
1313out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -07001314 return ret;
1315out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -07001316 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320EXPORT_SYMBOL(tty_check_change);
1321
Alan Cox37bdfb02008-02-08 04:18:47 -08001322static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 size_t count, loff_t *ppos)
1324{
1325 return 0;
1326}
1327
Alan Cox37bdfb02008-02-08 04:18:47 -08001328static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 size_t count, loff_t *ppos)
1330{
1331 return -EIO;
1332}
1333
1334/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -08001335static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
1338}
1339
Alan Cox04f378b2008-04-30 00:53:29 -07001340static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
1341 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001342{
1343 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1344}
1345
Alan Cox37bdfb02008-02-08 04:18:47 -08001346static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001347 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
1350}
1351
Arjan van de Ven62322d22006-07-03 00:24:21 -07001352static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 .llseek = no_llseek,
1354 .read = tty_read,
1355 .write = tty_write,
1356 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001357 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001358 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 .open = tty_open,
1360 .release = tty_release,
1361 .fasync = tty_fasync,
1362};
1363
1364#ifdef CONFIG_UNIX98_PTYS
Arjan van de Ven62322d22006-07-03 00:24:21 -07001365static const struct file_operations ptmx_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 .llseek = no_llseek,
1367 .read = tty_read,
1368 .write = tty_write,
1369 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001370 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001371 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 .open = ptmx_open,
1373 .release = tty_release,
1374 .fasync = tty_fasync,
1375};
1376#endif
1377
Arjan van de Ven62322d22006-07-03 00:24:21 -07001378static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 .llseek = no_llseek,
1380 .read = tty_read,
1381 .write = redirected_tty_write,
1382 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001383 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07001384 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 .open = tty_open,
1386 .release = tty_release,
1387 .fasync = tty_fasync,
1388};
1389
Arjan van de Ven62322d22006-07-03 00:24:21 -07001390static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 .llseek = no_llseek,
1392 .read = hung_up_tty_read,
1393 .write = hung_up_tty_write,
1394 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -07001395 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -07001396 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 .release = tty_release,
1398};
1399
1400static DEFINE_SPINLOCK(redirect_lock);
1401static struct file *redirect;
1402
1403/**
1404 * tty_wakeup - request more data
1405 * @tty: terminal
1406 *
1407 * Internal and external helper for wakeups of tty. This function
1408 * informs the line discipline if present that the driver is ready
1409 * to receive more output data.
1410 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412void tty_wakeup(struct tty_struct *tty)
1413{
1414 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
1417 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001418 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +01001419 if (ld->ops->write_wakeup)
1420 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 tty_ldisc_deref(ld);
1422 }
1423 }
1424 wake_up_interruptible(&tty->write_wait);
1425}
1426
1427EXPORT_SYMBOL_GPL(tty_wakeup);
1428
1429/**
1430 * tty_ldisc_flush - flush line discipline queue
1431 * @tty: tty
1432 *
1433 * Flush the line discipline queue (if any) for this tty. If there
1434 * is no line discipline active this is a no-op.
1435 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437void tty_ldisc_flush(struct tty_struct *tty)
1438{
1439 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001440 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +01001441 if (ld->ops->flush_buffer)
1442 ld->ops->flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 tty_ldisc_deref(ld);
1444 }
Paul Fulghumc5c34d42007-05-12 10:36:55 -07001445 tty_buffer_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448EXPORT_SYMBOL_GPL(tty_ldisc_flush);
Alan Coxedc6afc2006-12-08 02:38:44 -08001449
1450/**
1451 * tty_reset_termios - reset terminal state
1452 * @tty: tty to reset
1453 *
1454 * Restore a terminal to the driver default state
1455 */
1456
1457static void tty_reset_termios(struct tty_struct *tty)
1458{
1459 mutex_lock(&tty->termios_mutex);
1460 *tty->termios = tty->driver->init_termios;
1461 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1462 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1463 mutex_unlock(&tty->termios_mutex);
1464}
Alan Cox37bdfb02008-02-08 04:18:47 -08001465
Alan Coxaf9b8972006-08-27 01:24:01 -07001466/**
1467 * do_tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +00001468 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001469 *
Alan Coxa352def2008-07-16 21:53:12 +01001470k * This can be called by the "eventd" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -07001471 * synchronous but doesn't hold any locks, so we need to make sure we
1472 * have the appropriate locks for what we're doing.
1473 *
1474 * The hangup event clears any pending redirections onto the hung up
1475 * device. It ensures future writes will error and it does the needed
1476 * line discipline hangup and signal delivery. The tty object itself
1477 * remains intact.
1478 *
1479 * Locking:
1480 * BKL
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001481 * redirect lock for undoing redirection
1482 * file list lock for manipulating list of ttys
1483 * tty_ldisc_lock from called functions
1484 * termios_mutex resetting termios data
1485 * tasklist_lock to walk task list for hangup event
1486 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 */
David Howells65f27f32006-11-22 14:55:48 +00001488static void do_tty_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
David Howells65f27f32006-11-22 14:55:48 +00001490 struct tty_struct *tty =
1491 container_of(work, struct tty_struct, hangup_work);
Alan Cox37bdfb02008-02-08 04:18:47 -08001492 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct file *filp, *f = NULL;
1494 struct task_struct *p;
1495 struct tty_ldisc *ld;
1496 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -07001497 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (!tty)
1500 return;
1501
1502 /* inuse_filps is protected by the single kernel lock */
1503 lock_kernel();
1504
1505 spin_lock(&redirect_lock);
1506 if (redirect && redirect->private_data == tty) {
1507 f = redirect;
1508 redirect = NULL;
1509 }
1510 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -08001511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 check_tty_count(tty, "do_tty_hangup");
1513 file_list_lock();
1514 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Eric Dumazet2f512012005-10-30 15:02:16 -08001515 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 if (filp->f_op->write == redirected_tty_write)
1517 cons_filp = filp;
1518 if (filp->f_op->write != tty_write)
1519 continue;
1520 closecount++;
1521 tty_fasync(-1, filp, 0); /* can't block */
1522 filp->f_op = &hung_up_tty_fops;
1523 }
1524 file_list_unlock();
Alan Cox37bdfb02008-02-08 04:18:47 -08001525 /*
1526 * FIXME! What are the locking issues here? This may me overdoing
1527 * things... This question is especially important now that we've
1528 * removed the irqlock.
1529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001531 if (ld != NULL) {
1532 /* We may have no line discipline at this point */
Alan Coxa352def2008-07-16 21:53:12 +01001533 if (ld->ops->flush_buffer)
1534 ld->ops->flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -07001535 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
Alan Coxa352def2008-07-16 21:53:12 +01001537 ld->ops->write_wakeup)
1538 ld->ops->write_wakeup(tty);
1539 if (ld->ops->hangup)
1540 ld->ops->hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001542 /*
1543 * FIXME: Once we trust the LDISC code better we can wait here for
1544 * ldisc completion and fix the driver call race
1545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 wake_up_interruptible(&tty->write_wait);
1547 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /*
1549 * Shutdown the current line discipline, and reset it to
1550 * N_TTY.
1551 */
1552 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Coxedc6afc2006-12-08 02:38:44 -08001553 tty_reset_termios(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* Defer ldisc switch */
1555 /* tty_deferred_ldisc_switch(N_TTY);
Alan Cox37bdfb02008-02-08 04:18:47 -08001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 This should get done automatically when the port closes and
1558 tty_release is called */
Alan Cox37bdfb02008-02-08 04:18:47 -08001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001561 if (tty->session) {
1562 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001563 spin_lock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (p->signal->tty == tty)
1565 p->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001566 if (!p->signal->leader) {
1567 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 continue;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001569 }
1570 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
1571 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001572 put_pid(p->signal->tty_old_pgrp); /* A noop */
Alan Cox47f86832008-04-30 00:53:30 -07001573 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001574 if (tty->pgrp)
1575 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07001576 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001577 spin_unlock_irq(&p->sighand->siglock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001578 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580 read_unlock(&tasklist_lock);
1581
Alan Cox47f86832008-04-30 00:53:30 -07001582 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 tty->flags = 0;
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06001584 put_pid(tty->session);
1585 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001586 tty->session = NULL;
1587 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 tty->ctrl_status = 0;
Alan Cox47f86832008-04-30 00:53:30 -07001589 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001592 * If one of the devices matches a console pointer, we
1593 * cannot just call hangup() because that will cause
1594 * tty->count and state->count to go out of sync.
1595 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
1597 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001598 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -07001600 tty->ops->close(tty, cons_filp);
1601 } else if (tty->ops->hangup)
1602 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08001603 /*
1604 * We don't want to have driver/ldisc interactions beyond
1605 * the ones we did here. The driver layer expects no
1606 * calls after ->hangup() from the ldisc side. However we
1607 * can't yet guarantee all that.
1608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 set_bit(TTY_HUPPED, &tty->flags);
1610 if (ld) {
1611 tty_ldisc_enable(tty);
1612 tty_ldisc_deref(ld);
1613 }
1614 unlock_kernel();
1615 if (f)
1616 fput(f);
1617}
1618
Alan Coxaf9b8972006-08-27 01:24:01 -07001619/**
1620 * tty_hangup - trigger a hangup event
1621 * @tty: tty to hangup
1622 *
1623 * A carrier loss (virtual or otherwise) has occurred on this like
1624 * schedule a hangup sequence to run after this event.
1625 */
1626
Alan Cox37bdfb02008-02-08 04:18:47 -08001627void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629#ifdef TTY_DEBUG_HANGUP
1630 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
1632#endif
1633 schedule_work(&tty->hangup_work);
1634}
1635
1636EXPORT_SYMBOL(tty_hangup);
1637
Alan Coxaf9b8972006-08-27 01:24:01 -07001638/**
1639 * tty_vhangup - process vhangup
1640 * @tty: tty to hangup
1641 *
1642 * The user has asked via system call for the terminal to be hung up.
1643 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001644 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -07001645 */
1646
Alan Cox37bdfb02008-02-08 04:18:47 -08001647void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649#ifdef TTY_DEBUG_HANGUP
1650 char buf[64];
1651
1652 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
1653#endif
David Howells65f27f32006-11-22 14:55:48 +00001654 do_tty_hangup(&tty->hangup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
Alan Cox37bdfb02008-02-08 04:18:47 -08001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657EXPORT_SYMBOL(tty_vhangup);
1658
Alan Coxaf9b8972006-08-27 01:24:01 -07001659/**
1660 * tty_hung_up_p - was tty hung up
1661 * @filp: file pointer of tty
1662 *
1663 * Return true if the tty has been subject to a vhangup or a carrier
1664 * loss
1665 */
1666
Alan Cox37bdfb02008-02-08 04:18:47 -08001667int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 return (filp->f_op == &hung_up_tty_fops);
1670}
1671
1672EXPORT_SYMBOL(tty_hung_up_p);
1673
Miloslav Trmac522ed772007-07-15 23:40:56 -07001674/**
Alan Cox37bdfb02008-02-08 04:18:47 -08001675 * is_tty - checker whether file is a TTY
1676 * @filp: file handle that may be a tty
1677 *
1678 * Check if the file handle is a tty handle.
Miloslav Trmac522ed772007-07-15 23:40:56 -07001679 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001680
Miloslav Trmac522ed772007-07-15 23:40:56 -07001681int is_tty(struct file *filp)
1682{
1683 return filp->f_op->read == tty_read
1684 || filp->f_op->read == hung_up_tty_read;
1685}
1686
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001687static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001688{
1689 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001690 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001691 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001692 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001693}
1694
Alan Coxaf9b8972006-08-27 01:24:01 -07001695/**
1696 * disassociate_ctty - disconnect controlling tty
1697 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001699 * This function is typically called only by the session leader, when
1700 * it wants to disassociate itself from its controlling tty.
1701 *
1702 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
1704 * (2) Clears the tty from being controlling the session
1705 * (3) Clears the controlling tty for all processes in the
1706 * session group.
1707 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001708 * The argument on_exit is set to 1 if called when a process is
1709 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
1710 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001711 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001712 * BKL is taken for hysterical raisins
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001713 * tty_mutex is taken to protect tty
1714 * ->siglock is taken to protect ->signal/->sighand
1715 * tasklist_lock is taken to walk process list for sessions
1716 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719void disassociate_ctty(int on_exit)
1720{
1721 struct tty_struct *tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001722 struct pid *tty_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Ingo Molnar70522e12006-03-23 03:00:31 -08001725 mutex_lock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001726 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001728 tty_pgrp = get_pid(tty->pgrp);
Ingo Molnar70522e12006-03-23 03:00:31 -08001729 mutex_unlock(&tty_mutex);
Alan Cox04f378b2008-04-30 00:53:29 -07001730 lock_kernel();
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001731 /* XXX: here we race, there is nothing protecting tty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
1733 tty_vhangup(tty);
Alan Cox04f378b2008-04-30 00:53:29 -07001734 unlock_kernel();
Eric W. Biederman680a9672007-02-12 00:52:52 -08001735 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001736 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -08001737 spin_lock_irq(&current->sighand->siglock);
1738 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001739 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -08001740 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001741 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001742 kill_pgrp(old_pgrp, SIGHUP, on_exit);
1743 kill_pgrp(old_pgrp, SIGCONT, on_exit);
1744 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001746 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return;
1748 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001749 if (tty_pgrp) {
1750 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (!on_exit)
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001752 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
1753 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001756 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07001757 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -08001758 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001759 spin_unlock_irq(&current->sighand->siglock);
1760
1761 mutex_lock(&tty_mutex);
1762 /* It is possible that do_tty_hangup has free'd this tty */
1763 tty = get_current_tty();
1764 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07001765 unsigned long flags;
1766 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001767 put_pid(tty->session);
1768 put_pid(tty->pgrp);
1769 tty->session = NULL;
1770 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -07001771 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001772 } else {
1773#ifdef TTY_DEBUG_HANGUP
1774 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
1775 " = NULL", tty);
1776#endif
1777 }
1778 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* Now clear signal->tty under the lock */
1781 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001782 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001786/**
1787 *
1788 * no_tty - Ensure the current process does not have a controlling tty
1789 */
1790void no_tty(void)
1791{
1792 struct task_struct *tsk = current;
Alan Cox04f378b2008-04-30 00:53:29 -07001793 lock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001794 if (tsk->signal->leader)
1795 disassociate_ctty(0);
Alan Cox04f378b2008-04-30 00:53:29 -07001796 unlock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07001797 proc_clear_tty(tsk);
1798}
1799
Alan Coxaf9b8972006-08-27 01:24:01 -07001800
1801/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001802 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -07001803 * @tty: tty to stop
1804 *
1805 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001806 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -07001807 * on an already stopped device and will not re-call the driver
1808 * method.
1809 *
1810 * This functionality is used by both the line disciplines for
1811 * halting incoming flow and by the driver. It may therefore be
1812 * called from any context, may be under the tty atomic_write_lock
1813 * but not always.
1814 *
1815 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -07001816 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -07001817 */
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819void stop_tty(struct tty_struct *tty)
1820{
Alan Cox04f378b2008-04-30 00:53:29 -07001821 unsigned long flags;
1822 spin_lock_irqsave(&tty->ctrl_lock, flags);
1823 if (tty->stopped) {
1824 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return;
Alan Cox04f378b2008-04-30 00:53:29 -07001826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 tty->stopped = 1;
1828 if (tty->link && tty->link->packet) {
1829 tty->ctrl_status &= ~TIOCPKT_START;
1830 tty->ctrl_status |= TIOCPKT_STOP;
1831 wake_up_interruptible(&tty->link->read_wait);
1832 }
Alan Cox04f378b2008-04-30 00:53:29 -07001833 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -07001834 if (tty->ops->stop)
1835 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838EXPORT_SYMBOL(stop_tty);
1839
Alan Coxaf9b8972006-08-27 01:24:01 -07001840/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001841 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -07001842 * @tty: tty to start
1843 *
1844 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02001845 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -07001846 * is the tty was previous stopped and is being started then the
1847 * driver start method is invoked and the line discipline woken.
1848 *
1849 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -07001850 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001851 */
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853void start_tty(struct tty_struct *tty)
1854{
Alan Cox04f378b2008-04-30 00:53:29 -07001855 unsigned long flags;
1856 spin_lock_irqsave(&tty->ctrl_lock, flags);
1857 if (!tty->stopped || tty->flow_stopped) {
1858 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return;
Alan Cox04f378b2008-04-30 00:53:29 -07001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 tty->stopped = 0;
1862 if (tty->link && tty->link->packet) {
1863 tty->ctrl_status &= ~TIOCPKT_STOP;
1864 tty->ctrl_status |= TIOCPKT_START;
1865 wake_up_interruptible(&tty->link->read_wait);
1866 }
Alan Cox04f378b2008-04-30 00:53:29 -07001867 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -07001868 if (tty->ops->start)
1869 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /* If we have a running line discipline it may need kicking */
1871 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874EXPORT_SYMBOL(start_tty);
1875
Alan Coxaf9b8972006-08-27 01:24:01 -07001876/**
1877 * tty_read - read method for tty device files
1878 * @file: pointer to tty file
1879 * @buf: user buffer
1880 * @count: size of user buffer
1881 * @ppos: unused
1882 *
1883 * Perform the read system call function on this terminal device. Checks
1884 * for hung up devices before calling the line discipline method.
1885 *
1886 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -07001887 * Locks the line discipline internally while needed. Multiple
1888 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -07001889 */
1890
Alan Cox37bdfb02008-02-08 04:18:47 -08001891static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 loff_t *ppos)
1893{
1894 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08001895 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct inode *inode;
1897 struct tty_ldisc *ld;
1898
1899 tty = (struct tty_struct *)file->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08001900 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (tty_paranoia_check(tty, inode, "tty_read"))
1902 return -EIO;
1903 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1904 return -EIO;
1905
1906 /* We want to wait for the line discipline to sort out in this
1907 situation */
1908 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001909 if (ld->ops->read)
1910 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 else
1912 i = -EIO;
1913 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (i > 0)
1915 inode->i_atime = current_fs_time(inode->i_sb);
1916 return i;
1917}
1918
Alan Cox9c1729d2007-07-15 23:39:43 -07001919void tty_write_unlock(struct tty_struct *tty)
1920{
1921 mutex_unlock(&tty->atomic_write_lock);
1922 wake_up_interruptible(&tty->write_wait);
1923}
1924
1925int tty_write_lock(struct tty_struct *tty, int ndelay)
1926{
1927 if (!mutex_trylock(&tty->atomic_write_lock)) {
1928 if (ndelay)
1929 return -EAGAIN;
1930 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1931 return -ERESTARTSYS;
1932 }
1933 return 0;
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/*
1937 * Split writes up in sane blocksizes to avoid
1938 * denial-of-service type attacks
1939 */
1940static inline ssize_t do_tty_write(
1941 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1942 struct tty_struct *tty,
1943 struct file *file,
1944 const char __user *buf,
1945 size_t count)
1946{
Alan Cox9c1729d2007-07-15 23:39:43 -07001947 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001949
Alan Cox9c1729d2007-07-15 23:39:43 -07001950 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1951 if (ret < 0)
1952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 /*
1955 * We chunk up writes into a temporary buffer. This
1956 * simplifies low-level drivers immensely, since they
1957 * don't have locking issues and user mode accesses.
1958 *
1959 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1960 * big chunk-size..
1961 *
1962 * The default chunk-size is 2kB, because the NTTY
1963 * layer has problems with bigger chunks. It will
1964 * claim to be able to handle more characters than
1965 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001966 *
1967 * FIXME: This can probably go away now except that 64K chunks
1968 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 */
1970 chunk = 2048;
1971 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1972 chunk = 65536;
1973 if (count < chunk)
1974 chunk = count;
1975
Ingo Molnar70522e12006-03-23 03:00:31 -08001976 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (tty->write_cnt < chunk) {
1978 unsigned char *buf;
1979
1980 if (chunk < 1024)
1981 chunk = 1024;
1982
1983 buf = kmalloc(chunk, GFP_KERNEL);
1984 if (!buf) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001985 ret = -ENOMEM;
1986 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988 kfree(tty->write_buf);
1989 tty->write_cnt = chunk;
1990 tty->write_buf = buf;
1991 }
1992
1993 /* Do the write .. */
1994 for (;;) {
1995 size_t size = count;
1996 if (size > chunk)
1997 size = chunk;
1998 ret = -EFAULT;
1999 if (copy_from_user(tty->write_buf, buf, size))
2000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (ret <= 0)
2003 break;
2004 written += ret;
2005 buf += ret;
2006 count -= ret;
2007 if (!count)
2008 break;
2009 ret = -ERESTARTSYS;
2010 if (signal_pending(current))
2011 break;
2012 cond_resched();
2013 }
2014 if (written) {
Josef Sipeka7113a92006-12-08 02:36:55 -08002015 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 inode->i_mtime = current_fs_time(inode->i_sb);
2017 ret = written;
2018 }
Alan Cox9c1729d2007-07-15 23:39:43 -07002019out:
2020 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return ret;
2022}
2023
2024
Alan Coxaf9b8972006-08-27 01:24:01 -07002025/**
2026 * tty_write - write method for tty device file
2027 * @file: tty file pointer
2028 * @buf: user data to write
2029 * @count: bytes to write
2030 * @ppos: unused
2031 *
2032 * Write data to a tty device via the line discipline.
2033 *
2034 * Locking:
2035 * Locks the line discipline as required
2036 * Writes to the tty driver are serialized by the atomic_write_lock
2037 * and are then processed in chunks to the device. The line discipline
2038 * write method will not be involked in parallel for each device
2039 * The line discipline write method is called under the big
2040 * kernel lock for historical reasons. New code should not rely on this.
2041 */
2042
Alan Cox37bdfb02008-02-08 04:18:47 -08002043static ssize_t tty_write(struct file *file, const char __user *buf,
2044 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Alan Cox37bdfb02008-02-08 04:18:47 -08002046 struct tty_struct *tty;
Josef Sipeka7113a92006-12-08 02:36:55 -08002047 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 ssize_t ret;
2049 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 tty = (struct tty_struct *)file->private_data;
2052 if (tty_paranoia_check(tty, inode, "tty_write"))
2053 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07002054 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08002055 (test_bit(TTY_IO_ERROR, &tty->flags)))
2056 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07002057 /* Short term debug to catch buggy drivers */
2058 if (tty->ops->write_room == NULL)
2059 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
2060 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08002061 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002062 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 ret = -EIO;
2064 else
Alan Coxa352def2008-07-16 21:53:12 +01002065 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 tty_ldisc_deref(ld);
2067 return ret;
2068}
2069
Alan Cox37bdfb02008-02-08 04:18:47 -08002070ssize_t redirected_tty_write(struct file *file, const char __user *buf,
2071 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct file *p = NULL;
2074
2075 spin_lock(&redirect_lock);
2076 if (redirect) {
2077 get_file(redirect);
2078 p = redirect;
2079 }
2080 spin_unlock(&redirect_lock);
2081
2082 if (p) {
2083 ssize_t res;
2084 res = vfs_write(p, buf, count, &p->f_pos);
2085 fput(p);
2086 return res;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return tty_write(file, buf, count, ppos);
2089}
2090
2091static char ptychar[] = "pqrstuvwxyzabcde";
2092
Alan Coxaf9b8972006-08-27 01:24:01 -07002093/**
2094 * pty_line_name - generate name for a pty
2095 * @driver: the tty driver in use
2096 * @index: the minor number
2097 * @p: output buffer of at least 6 bytes
2098 *
2099 * Generate a name from a driver reference and write it to the output
2100 * buffer.
2101 *
2102 * Locking: None
2103 */
2104static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 int i = index + driver->name_base;
2107 /* ->name is initialized to "ttyp", but "tty" is expected */
2108 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08002109 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
2110 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
Alan Coxaf9b8972006-08-27 01:24:01 -07002113/**
2114 * pty_line_name - generate name for a tty
2115 * @driver: the tty driver in use
2116 * @index: the minor number
2117 * @p: output buffer of at least 7 bytes
2118 *
2119 * Generate a name from a driver reference and write it to the output
2120 * buffer.
2121 *
2122 * Locking: None
2123 */
2124static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 sprintf(p, "%s%d", driver->name, index + driver->name_base);
2127}
2128
Alan Coxaf9b8972006-08-27 01:24:01 -07002129/**
2130 * init_dev - initialise a tty device
2131 * @driver: tty driver we are opening a device on
2132 * @idx: device index
2133 * @tty: returned tty structure
2134 *
2135 * Prepare a tty device. This may not be a "new" clean device but
2136 * could also be an active device. The pty drivers require special
2137 * handling because of this.
2138 *
2139 * Locking:
2140 * The function is called under the tty_mutex, which
2141 * protects us from the tty struct or driver itself going away.
2142 *
2143 * On exit the tty device has the line discipline attached and
2144 * a reference count of 1. If a pair was created for pty/tty use
2145 * and the other was a pty master then it too has a reference count of 1.
2146 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08002148 * failed open. The new code protects the open with a mutex, so it's
2149 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * relaxed for the (most common) case of reopening a tty.
2151 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153static int init_dev(struct tty_driver *driver, int idx,
2154 struct tty_struct **ret_tty)
2155{
2156 struct tty_struct *tty, *o_tty;
Alan Coxedc6afc2006-12-08 02:38:44 -08002157 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
2158 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
Alan Coxaf9b8972006-08-27 01:24:01 -07002159 int retval = 0;
Alan Coxa352def2008-07-16 21:53:12 +01002160 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* check whether we're reopening an existing tty */
2163 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2164 tty = devpts_get_tty(idx);
Aristeu Sergio Rozanski Filho5a39e8c2007-02-28 20:13:53 -08002165 /*
2166 * If we don't have a tty here on a slave open, it's because
2167 * the master already started the close process and there's
2168 * no relation between devpts file and tty anymore.
2169 */
2170 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
2171 retval = -EIO;
2172 goto end_init;
2173 }
2174 /*
2175 * It's safe from now on because init_dev() is called with
2176 * tty_mutex held and release_dev() won't change tty->count
2177 * or tty->flags without having to grab tty_mutex
2178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 if (tty && driver->subtype == PTY_TYPE_MASTER)
2180 tty = tty->link;
2181 } else {
2182 tty = driver->ttys[idx];
2183 }
2184 if (tty) goto fast_track;
2185
2186 /*
2187 * First time open is complex, especially for PTY devices.
2188 * This code guarantees that either everything succeeds and the
2189 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08002190 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * and locked termios may be retained.)
2192 */
2193
2194 if (!try_module_get(driver->owner)) {
2195 retval = -ENODEV;
2196 goto end_init;
2197 }
2198
2199 o_tty = NULL;
2200 tp = o_tp = NULL;
2201 ltp = o_ltp = NULL;
2202
2203 tty = alloc_tty_struct();
Alan Cox37bdfb02008-02-08 04:18:47 -08002204 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto fail_no_mem;
2206 initialize_tty_struct(tty);
2207 tty->driver = driver;
Alan Coxf34d7a52008-04-30 00:54:13 -07002208 tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 tty->index = idx;
2210 tty_line_name(driver, idx, tty->name);
2211
2212 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2213 tp_loc = &tty->termios;
2214 ltp_loc = &tty->termios_locked;
2215 } else {
2216 tp_loc = &driver->termios[idx];
2217 ltp_loc = &driver->termios_locked[idx];
2218 }
2219
2220 if (!*tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02002221 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (!tp)
2223 goto free_mem_out;
2224 *tp = driver->init_termios;
2225 }
2226
2227 if (!*ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07002228 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (!ltp)
2230 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232
2233 if (driver->type == TTY_DRIVER_TYPE_PTY) {
2234 o_tty = alloc_tty_struct();
2235 if (!o_tty)
2236 goto free_mem_out;
2237 initialize_tty_struct(o_tty);
2238 o_tty->driver = driver->other;
Alan Coxf34d7a52008-04-30 00:54:13 -07002239 o_tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 o_tty->index = idx;
2241 tty_line_name(driver->other, idx, o_tty->name);
2242
2243 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
2244 o_tp_loc = &o_tty->termios;
2245 o_ltp_loc = &o_tty->termios_locked;
2246 } else {
2247 o_tp_loc = &driver->other->termios[idx];
2248 o_ltp_loc = &driver->other->termios_locked[idx];
2249 }
2250
2251 if (!*o_tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02002252 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (!o_tp)
2254 goto free_mem_out;
2255 *o_tp = driver->other->init_termios;
2256 }
2257
2258 if (!*o_ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07002259 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (!o_ltp)
2261 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
2263
2264 /*
2265 * Everything allocated ... set up the o_tty structure.
2266 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002267 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 driver->other->ttys[idx] = o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (!*o_tp_loc)
2270 *o_tp_loc = o_tp;
2271 if (!*o_ltp_loc)
2272 *o_ltp_loc = o_ltp;
2273 o_tty->termios = *o_tp_loc;
2274 o_tty->termios_locked = *o_ltp_loc;
2275 driver->other->refcount++;
2276 if (driver->subtype == PTY_TYPE_MASTER)
2277 o_tty->count++;
2278
2279 /* Establish the links in both directions */
2280 tty->link = o_tty;
2281 o_tty->link = tty;
2282 }
2283
Alan Cox37bdfb02008-02-08 04:18:47 -08002284 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 * All structures have been allocated, so now we install them.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002286 * Failures after this point use release_tty to clean up, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 * there's no need to null out the local pointers.
2288 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002289 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 driver->ttys[idx] = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08002291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (!*tp_loc)
2293 *tp_loc = tp;
2294 if (!*ltp_loc)
2295 *ltp_loc = ltp;
2296 tty->termios = *tp_loc;
2297 tty->termios_locked = *ltp_loc;
Alan Coxedc6afc2006-12-08 02:38:44 -08002298 /* Compatibility until drivers always set this */
2299 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
2300 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 driver->refcount++;
2302 tty->count++;
2303
Alan Cox37bdfb02008-02-08 04:18:47 -08002304 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002306 * If we fail here just call release_tty to clean up. No need
2307 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
Alan Coxa352def2008-07-16 21:53:12 +01002309
2310 ld = &tty->ldisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Alan Coxa352def2008-07-16 21:53:12 +01002312 if (ld->ops->open) {
2313 retval = (ld->ops->open)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (retval)
2315 goto release_mem_out;
2316 }
Alan Coxa352def2008-07-16 21:53:12 +01002317 if (o_tty && o_tty->ldisc.ops->open) {
2318 retval = (o_tty->ldisc.ops->open)(o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (retval) {
Alan Coxa352def2008-07-16 21:53:12 +01002320 if (ld->ops->close)
2321 (ld->ops->close)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 goto release_mem_out;
2323 }
2324 tty_ldisc_enable(o_tty);
2325 }
2326 tty_ldisc_enable(tty);
2327 goto success;
2328
2329 /*
2330 * This fast open can be used if the tty is already open.
2331 * No memory is allocated, and the only failures are from
2332 * attempting to open a closing tty or attempting multiple
2333 * opens on a pty master.
2334 */
2335fast_track:
2336 if (test_bit(TTY_CLOSING, &tty->flags)) {
2337 retval = -EIO;
2338 goto end_init;
2339 }
2340 if (driver->type == TTY_DRIVER_TYPE_PTY &&
2341 driver->subtype == PTY_TYPE_MASTER) {
2342 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08002343 * special case for PTY masters: only one open permitted,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 * and the slave side open count is incremented as well.
2345 */
2346 if (tty->count) {
2347 retval = -EIO;
2348 goto end_init;
2349 }
2350 tty->link->count++;
2351 }
2352 tty->count++;
2353 tty->driver = driver; /* N.B. why do this every time?? */
2354
2355 /* FIXME */
Alan Cox37bdfb02008-02-08 04:18:47 -08002356 if (!test_bit(TTY_LDISC, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 printk(KERN_ERR "init_dev but no ldisc\n");
2358success:
2359 *ret_tty = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08002360
Ingo Molnar70522e12006-03-23 03:00:31 -08002361 /* All paths come through here to release the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362end_init:
2363 return retval;
2364
2365 /* Release locally allocated memory ... nothing placed in slots */
2366free_mem_out:
Jesper Juhl735d5662005-11-07 01:01:29 -08002367 kfree(o_tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (o_tty)
2369 free_tty_struct(o_tty);
Jesper Juhl735d5662005-11-07 01:01:29 -08002370 kfree(ltp);
2371 kfree(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 free_tty_struct(tty);
2373
2374fail_no_mem:
2375 module_put(driver->owner);
2376 retval = -ENOMEM;
2377 goto end_init;
2378
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002379 /* call the tty release_tty routine to clean out this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380release_mem_out:
Akinobu Mita40509142006-09-29 02:01:27 -07002381 if (printk_ratelimit())
2382 printk(KERN_INFO "init_dev: ldisc open failed, "
2383 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002384 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto end_init;
2386}
2387
Alan Coxaf9b8972006-08-27 01:24:01 -07002388/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002389 * release_one_tty - release tty structure memory
Alan Coxaf9b8972006-08-27 01:24:01 -07002390 *
2391 * Releases memory associated with a tty structure, and clears out the
2392 * driver table slots. This function is called when a device is no longer
2393 * in use. It also gets called when setup of a device fails.
2394 *
2395 * Locking:
2396 * tty_mutex - sometimes only
2397 * takes the file list lock internally when working on the list
2398 * of ttys that the driver keeps.
2399 * FIXME: should we require tty_mutex is held here ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002401static void release_one_tty(struct tty_struct *tty, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002404 struct ktermios *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 if (!devpts)
2407 tty->driver->ttys[idx] = NULL;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
2410 tp = tty->termios;
2411 if (!devpts)
2412 tty->driver->termios[idx] = NULL;
2413 kfree(tp);
2414
2415 tp = tty->termios_locked;
2416 if (!devpts)
2417 tty->driver->termios_locked[idx] = NULL;
2418 kfree(tp);
2419 }
2420
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 tty->magic = 0;
2423 tty->driver->refcount--;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 file_list_lock();
2426 list_del_init(&tty->tty_files);
2427 file_list_unlock();
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 free_tty_struct(tty);
2430}
2431
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002432/**
2433 * release_tty - release tty structure memory
2434 *
2435 * Release both @tty and a possible linked partner (think pty pair),
2436 * and decrement the refcount of the backing module.
2437 *
2438 * Locking:
2439 * tty_mutex - sometimes only
2440 * takes the file list lock internally when working on the list
2441 * of ttys that the driver keeps.
2442 * FIXME: should we require tty_mutex is held here ??
2443 */
2444static void release_tty(struct tty_struct *tty, int idx)
2445{
2446 struct tty_driver *driver = tty->driver;
2447
2448 if (tty->link)
2449 release_one_tty(tty->link, idx);
2450 release_one_tty(tty, idx);
2451 module_put(driver->owner);
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/*
2455 * Even releasing the tty structures is a tricky business.. We have
2456 * to be very careful that the structures are all released at the
2457 * same time, as interrupts might otherwise get the wrong pointers.
2458 *
2459 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
2460 * lead to double frees or releasing memory still in use.
2461 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002462static void release_dev(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct tty_struct *tty, *o_tty;
Alan Coxa352def2008-07-16 21:53:12 +01002465 struct tty_ldisc ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 int pty_master, tty_closing, o_tty_closing, do_sleep;
Paul Fulghum14a62832006-04-10 22:54:19 -07002467 int devpts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 int idx;
2469 char buf[64];
2470 unsigned long flags;
Alan Cox37bdfb02008-02-08 04:18:47 -08002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 tty = (struct tty_struct *)filp->private_data;
Alan Cox37bdfb02008-02-08 04:18:47 -08002473 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
2474 "release_dev"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 return;
2476
2477 check_tty_count(tty, "release_dev");
2478
2479 tty_fasync(-1, filp, 0);
2480
2481 idx = tty->index;
2482 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2483 tty->driver->subtype == PTY_TYPE_MASTER);
2484 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 o_tty = tty->link;
2486
2487#ifdef TTY_PARANOIA_CHECK
2488 if (idx < 0 || idx >= tty->driver->num) {
2489 printk(KERN_DEBUG "release_dev: bad idx when trying to "
2490 "free (%s)\n", tty->name);
2491 return;
2492 }
2493 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2494 if (tty != tty->driver->ttys[idx]) {
2495 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
2496 "for (%s)\n", idx, tty->name);
2497 return;
2498 }
2499 if (tty->termios != tty->driver->termios[idx]) {
2500 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
2501 "for (%s)\n",
2502 idx, tty->name);
2503 return;
2504 }
2505 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
2506 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
2507 "termios_locked for (%s)\n",
2508 idx, tty->name);
2509 return;
2510 }
2511 }
2512#endif
2513
2514#ifdef TTY_DEBUG_HANGUP
2515 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
2516 tty_name(tty, buf), tty->count);
2517#endif
2518
2519#ifdef TTY_PARANOIA_CHECK
2520 if (tty->driver->other &&
2521 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
2522 if (o_tty != tty->driver->other->ttys[idx]) {
2523 printk(KERN_DEBUG "release_dev: other->table[%d] "
2524 "not o_tty for (%s)\n",
2525 idx, tty->name);
2526 return;
2527 }
2528 if (o_tty->termios != tty->driver->other->termios[idx]) {
2529 printk(KERN_DEBUG "release_dev: other->termios[%d] "
2530 "not o_termios for (%s)\n",
2531 idx, tty->name);
2532 return;
2533 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002534 if (o_tty->termios_locked !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 tty->driver->other->termios_locked[idx]) {
2536 printk(KERN_DEBUG "release_dev: other->termios_locked["
2537 "%d] not o_termios_locked for (%s)\n",
2538 idx, tty->name);
2539 return;
2540 }
2541 if (o_tty->link != tty) {
2542 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
2543 return;
2544 }
2545 }
2546#endif
Alan Coxf34d7a52008-04-30 00:54:13 -07002547 if (tty->ops->close)
2548 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 /*
2551 * Sanity check: if tty->count is going to zero, there shouldn't be
2552 * any waiters on tty->read_wait or tty->write_wait. We test the
2553 * wait queues and kick everyone out _before_ actually starting to
2554 * close. This ensures that we won't block while releasing the tty
2555 * structure.
2556 *
2557 * The test for the o_tty closing is necessary, since the master and
2558 * slave sides may close in any order. If the slave side closes out
2559 * first, its count will be one, since the master side holds an open.
2560 * Thus this test wouldn't be triggered at the time the slave closes,
2561 * so we do it now.
2562 *
2563 * Note that it's possible for the tty to be opened again while we're
2564 * flushing out waiters. By recalculating the closing flags before
2565 * each iteration we avoid any problems.
2566 */
2567 while (1) {
2568 /* Guard against races with tty->count changes elsewhere and
2569 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08002570
Ingo Molnar70522e12006-03-23 03:00:31 -08002571 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 tty_closing = tty->count <= 1;
2573 o_tty_closing = o_tty &&
2574 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 do_sleep = 0;
2576
2577 if (tty_closing) {
2578 if (waitqueue_active(&tty->read_wait)) {
2579 wake_up(&tty->read_wait);
2580 do_sleep++;
2581 }
2582 if (waitqueue_active(&tty->write_wait)) {
2583 wake_up(&tty->write_wait);
2584 do_sleep++;
2585 }
2586 }
2587 if (o_tty_closing) {
2588 if (waitqueue_active(&o_tty->read_wait)) {
2589 wake_up(&o_tty->read_wait);
2590 do_sleep++;
2591 }
2592 if (waitqueue_active(&o_tty->write_wait)) {
2593 wake_up(&o_tty->write_wait);
2594 do_sleep++;
2595 }
2596 }
2597 if (!do_sleep)
2598 break;
2599
2600 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
2601 "active!\n", tty_name(tty, buf));
Ingo Molnar70522e12006-03-23 03:00:31 -08002602 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08002607 * The closing flags are now consistent with the open counts on
2608 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 * block, so it's safe to proceed with closing.
2610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (pty_master) {
2612 if (--o_tty->count < 0) {
2613 printk(KERN_WARNING "release_dev: bad pty slave count "
2614 "(%d) for %s\n",
2615 o_tty->count, tty_name(o_tty, buf));
2616 o_tty->count = 0;
2617 }
2618 }
2619 if (--tty->count < 0) {
2620 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
2621 tty->count, tty_name(tty, buf));
2622 tty->count = 0;
2623 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
2626 * We've decremented tty->count, so we need to remove this file
2627 * descriptor off the tty->tty_files list; this serves two
2628 * purposes:
2629 * - check_tty_count sees the correct number of file descriptors
2630 * associated with this tty.
2631 * - do_tty_hangup no longer sees this file descriptor as
2632 * something that needs to be handled for hangups.
2633 */
2634 file_kill(filp);
2635 filp->private_data = NULL;
2636
2637 /*
2638 * Perform some housekeeping before deciding whether to return.
2639 *
2640 * Set the TTY_CLOSING flag if this was the last open. In the
2641 * case of a pty we may have to wait around for the other side
2642 * to close, and TTY_CLOSING makes sure we can't be reopened.
2643 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002644 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08002646 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 set_bit(TTY_CLOSING, &o_tty->flags);
2648
2649 /*
2650 * If _either_ side is closing, make sure there aren't any
2651 * processes that still think tty or o_tty is their controlling
2652 * tty.
2653 */
2654 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002656 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002658 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 read_unlock(&tasklist_lock);
2660 }
2661
Ingo Molnar70522e12006-03-23 03:00:31 -08002662 mutex_unlock(&tty_mutex);
Paul Fulghumda965822006-02-14 13:53:00 -08002663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 /* check whether both sides are closing ... */
2665 if (!tty_closing || (o_tty && !o_tty_closing))
2666 return;
Alan Cox37bdfb02008-02-08 04:18:47 -08002667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668#ifdef TTY_DEBUG_HANGUP
2669 printk(KERN_DEBUG "freeing tty structure...");
2670#endif
2671 /*
2672 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
2673 * kill any delayed work. As this is the final close it does not
2674 * race with the set_ldisc code path.
2675 */
2676 clear_bit(TTY_LDISC, &tty->flags);
Alan Cox33f0f882006-01-09 20:54:13 -08002677 cancel_delayed_work(&tty->buf.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 /*
Alan Cox33f0f882006-01-09 20:54:13 -08002680 * Wait for ->hangup_work and ->buf.work handlers to terminate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 flush_scheduled_work();
Alan Cox37bdfb02008-02-08 04:18:47 -08002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /*
2686 * Wait for any short term users (we know they are just driver
2687 * side waiters as the file is closing so user count on the file
2688 * side is zero.
2689 */
2690 spin_lock_irqsave(&tty_ldisc_lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08002691 while (tty->ldisc.refcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2693 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
2694 spin_lock_irqsave(&tty_ldisc_lock, flags);
2695 }
2696 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
2697 /*
2698 * Shutdown the current line discipline, and reset it to N_TTY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 *
2700 * FIXME: this MUST get fixed for the new reflocking
2701 */
Alan Coxa352def2008-07-16 21:53:12 +01002702 if (tty->ldisc.ops->close)
2703 (tty->ldisc.ops->close)(tty);
2704 tty_ldisc_put(tty->ldisc.ops);
Alan Cox37bdfb02008-02-08 04:18:47 -08002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /*
2707 * Switch the line discipline back
2708 */
Alan Coxa352def2008-07-16 21:53:12 +01002709 WARN_ON(tty_ldisc_get(N_TTY, &ld));
2710 tty_ldisc_assign(tty, &ld);
Alan Cox37bdfb02008-02-08 04:18:47 -08002711 tty_set_termios_ldisc(tty, N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (o_tty) {
2713 /* FIXME: could o_tty be in setldisc here ? */
2714 clear_bit(TTY_LDISC, &o_tty->flags);
Alan Coxa352def2008-07-16 21:53:12 +01002715 if (o_tty->ldisc.ops->close)
2716 (o_tty->ldisc.ops->close)(o_tty);
2717 tty_ldisc_put(o_tty->ldisc.ops);
2718 WARN_ON(tty_ldisc_get(N_TTY, &ld));
2719 tty_ldisc_assign(o_tty, &ld);
Alan Cox37bdfb02008-02-08 04:18:47 -08002720 tty_set_termios_ldisc(o_tty, N_TTY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002723 * The release_tty function takes care of the details of clearing
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * the slots and preserving the termios structure.
2725 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08002726 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* Make this pty number available for reallocation */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002729 if (devpts)
2730 devpts_kill_index(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Alan Coxaf9b8972006-08-27 01:24:01 -07002733/**
2734 * tty_open - open a tty device
2735 * @inode: inode of device file
2736 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002738 * tty_open and tty_release keep up the tty count that contains the
2739 * number of opens done on a tty. We cannot use the inode-count, as
2740 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002742 * Open-counting is needed for pty masters, as well as for keeping
2743 * track of serial lines: DTR is dropped when the last close happens.
2744 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2745 *
2746 * The termios state of a pty is reset on first open so that
2747 * settings don't persist across reuse.
2748 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002749 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
2750 * tty->count should protect the rest.
2751 * ->siglock protects ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002753
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002754static int __tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 struct tty_struct *tty;
2757 int noctty, retval;
2758 struct tty_driver *driver;
2759 int index;
2760 dev_t device = inode->i_rdev;
2761 unsigned short saved_flags = filp->f_flags;
2762
2763 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765retry_open:
2766 noctty = filp->f_flags & O_NOCTTY;
2767 index = -1;
2768 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002769
Ingo Molnar70522e12006-03-23 03:00:31 -08002770 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Alan Cox37bdfb02008-02-08 04:18:47 -08002772 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002773 tty = get_current_tty();
2774 if (!tty) {
Ingo Molnar70522e12006-03-23 03:00:31 -08002775 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return -ENXIO;
2777 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002778 driver = tty->driver;
2779 index = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
2781 /* noctty = 1; */
2782 goto got_driver;
2783 }
2784#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08002785 if (device == MKDEV(TTY_MAJOR, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 extern struct tty_driver *console_driver;
2787 driver = console_driver;
2788 index = fg_console;
2789 noctty = 1;
2790 goto got_driver;
2791 }
2792#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08002793 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 driver = console_device(&index);
2795 if (driver) {
2796 /* Don't let /dev/console block */
2797 filp->f_flags |= O_NONBLOCK;
2798 noctty = 1;
2799 goto got_driver;
2800 }
Ingo Molnar70522e12006-03-23 03:00:31 -08002801 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return -ENODEV;
2803 }
2804
2805 driver = get_tty_driver(device, &index);
2806 if (!driver) {
Ingo Molnar70522e12006-03-23 03:00:31 -08002807 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return -ENODEV;
2809 }
2810got_driver:
2811 retval = init_dev(driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08002812 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (retval)
2814 return retval;
2815
2816 filp->private_data = tty;
2817 file_move(filp, &tty->tty_files);
2818 check_tty_count(tty, "tty_open");
2819 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2820 tty->driver->subtype == PTY_TYPE_MASTER)
2821 noctty = 1;
2822#ifdef TTY_DEBUG_HANGUP
2823 printk(KERN_DEBUG "opening %s...", tty->name);
2824#endif
2825 if (!retval) {
Alan Coxf34d7a52008-04-30 00:54:13 -07002826 if (tty->ops->open)
2827 retval = tty->ops->open(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 else
2829 retval = -ENODEV;
2830 }
2831 filp->f_flags = saved_flags;
2832
Alan Cox37bdfb02008-02-08 04:18:47 -08002833 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
2834 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 retval = -EBUSY;
2836
2837 if (retval) {
2838#ifdef TTY_DEBUG_HANGUP
2839 printk(KERN_DEBUG "error %d in opening %s...", retval,
2840 tty->name);
2841#endif
2842 release_dev(filp);
2843 if (retval != -ERESTARTSYS)
2844 return retval;
2845 if (signal_pending(current))
2846 return retval;
2847 schedule();
2848 /*
2849 * Need to reset f_op in case a hangup happened.
2850 */
2851 if (filp->f_op == &hung_up_tty_fops)
2852 filp->f_op = &tty_fops;
2853 goto retry_open;
2854 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002855
2856 mutex_lock(&tty_mutex);
2857 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 if (!noctty &&
2859 current->signal->leader &&
2860 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002861 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07002862 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002863 spin_unlock_irq(&current->sighand->siglock);
2864 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return 0;
2866}
2867
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002868/* BKL pushdown: scary code avoidance wrapper */
2869static int tty_open(struct inode *inode, struct file *filp)
2870{
2871 int ret;
2872
2873 lock_kernel();
2874 ret = __tty_open(inode, filp);
2875 unlock_kernel();
2876 return ret;
2877}
2878
2879
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881#ifdef CONFIG_UNIX98_PTYS
Alan Coxaf9b8972006-08-27 01:24:01 -07002882/**
2883 * ptmx_open - open a unix 98 pty master
2884 * @inode: inode of device file
2885 * @filp: file pointer to tty
2886 *
2887 * Allocate a unix98 pty master device from the ptmx driver.
2888 *
2889 * Locking: tty_mutex protects theinit_dev work. tty->count should
Alan Cox37bdfb02008-02-08 04:18:47 -08002890 * protect the rest.
Alan Coxaf9b8972006-08-27 01:24:01 -07002891 * allocated_ptys_lock handles the list of free pty numbers
2892 */
2893
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002894static int __ptmx_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
2896 struct tty_struct *tty;
2897 int retval;
2898 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 nonseekable_open(inode, filp);
2901
2902 /* find a device that is not in use. */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002903 index = devpts_new_index();
2904 if (index < 0)
2905 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Ingo Molnar70522e12006-03-23 03:00:31 -08002907 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 retval = init_dev(ptm_driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08002909 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08002910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (retval)
2912 goto out;
2913
2914 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
2915 filp->private_data = tty;
2916 file_move(filp, &tty->tty_files);
2917
Sukadev Bhattiprolu4f8f9d62008-04-30 00:54:20 -07002918 retval = devpts_pty_new(tty->link);
2919 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 goto out1;
2921
Hiroshi Shimamoto86a96532008-04-30 00:54:20 -07002922 check_tty_count(tty, "ptmx_open");
Alan Coxf34d7a52008-04-30 00:54:13 -07002923 retval = ptm_driver->ops->open(tty, filp);
Miloslav Trmac41126222008-04-18 13:30:14 -07002924 if (!retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
2926out1:
2927 release_dev(filp);
Paul Fulghum9453a5a2006-04-10 22:54:18 -07002928 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929out:
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07002930 devpts_kill_index(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return retval;
2932}
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002933
2934static int ptmx_open(struct inode *inode, struct file *filp)
2935{
2936 int ret;
2937
2938 lock_kernel();
2939 ret = __ptmx_open(inode, filp);
2940 unlock_kernel();
2941 return ret;
2942}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943#endif
2944
Alan Coxaf9b8972006-08-27 01:24:01 -07002945/**
2946 * tty_release - vfs callback for close
2947 * @inode: inode of tty
2948 * @filp: file pointer for handle to tty
2949 *
2950 * Called the last time each file handle is closed that references
2951 * this tty. There may however be several such references.
2952 *
2953 * Locking:
2954 * Takes bkl. See release_dev
2955 */
2956
Alan Cox37bdfb02008-02-08 04:18:47 -08002957static int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 lock_kernel();
2960 release_dev(filp);
2961 unlock_kernel();
2962 return 0;
2963}
2964
Alan Coxaf9b8972006-08-27 01:24:01 -07002965/**
2966 * tty_poll - check tty status
2967 * @filp: file being polled
2968 * @wait: poll wait structures to update
2969 *
2970 * Call the line discipline polling method to obtain the poll
2971 * status of the device.
2972 *
2973 * Locking: locks called line discipline but ldisc poll method
2974 * may be re-entered freely by other callers.
2975 */
2976
Alan Cox37bdfb02008-02-08 04:18:47 -08002977static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
Alan Cox37bdfb02008-02-08 04:18:47 -08002979 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct tty_ldisc *ld;
2981 int ret = 0;
2982
2983 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08002984 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002988 if (ld->ops->poll)
2989 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 tty_ldisc_deref(ld);
2991 return ret;
2992}
2993
Alan Cox37bdfb02008-02-08 04:18:47 -08002994static int tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Alan Cox37bdfb02008-02-08 04:18:47 -08002996 struct tty_struct *tty;
Alan Cox47f86832008-04-30 00:53:30 -07002997 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002998 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06003000 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08003002 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06003003 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08003004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 retval = fasync_helper(fd, filp, on, &tty->fasync);
3006 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06003007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003010 enum pid_type type;
3011 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (!waitqueue_active(&tty->read_wait))
3013 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07003014 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003015 if (tty->pgrp) {
3016 pid = tty->pgrp;
3017 type = PIDTYPE_PGID;
3018 } else {
3019 pid = task_pid(current);
3020 type = PIDTYPE_PID;
3021 }
Alan Cox47f86832008-04-30 00:53:30 -07003022 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003023 retval = __f_setown(filp, pid, type, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06003025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 } else {
3027 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
3028 tty->minimum_to_wake = N_TTY_BUF_SIZE;
3029 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06003030 retval = 0;
3031out:
3032 unlock_kernel();
3033 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034}
3035
Alan Coxaf9b8972006-08-27 01:24:01 -07003036/**
3037 * tiocsti - fake input character
3038 * @tty: tty to fake input into
3039 * @p: pointer to character
3040 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02003041 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07003042 * input management.
3043 *
3044 * FIXME: does not honour flow control ??
3045 *
3046 * Locking:
3047 * Called functions take tty_ldisc_lock
3048 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07003049 *
3050 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07003051 */
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053static int tiocsti(struct tty_struct *tty, char __user *p)
3054{
3055 char ch, mbz = 0;
3056 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
3059 return -EPERM;
3060 if (get_user(ch, p))
3061 return -EFAULT;
3062 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01003063 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 tty_ldisc_deref(ld);
3065 return 0;
3066}
3067
Alan Coxaf9b8972006-08-27 01:24:01 -07003068/**
3069 * tiocgwinsz - implement window query ioctl
3070 * @tty; tty
3071 * @arg: user buffer for result
3072 *
Alan Cox808a0d32006-09-29 02:00:40 -07003073 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07003074 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003075 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07003076 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07003077 */
3078
Alan Cox37bdfb02008-02-08 04:18:47 -08003079static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Alan Cox808a0d32006-09-29 02:00:40 -07003081 int err;
3082
Arjan van de Ven5785c952006-09-29 02:00:43 -07003083 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07003084 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07003085 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07003086
3087 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Alan Coxaf9b8972006-08-27 01:24:01 -07003090/**
3091 * tiocswinsz - implement window size set ioctl
3092 * @tty; tty
3093 * @arg: user buffer for result
3094 *
3095 * Copies the user idea of the window size to the kernel. Traditionally
3096 * this is just advisory information but for the Linux console it
3097 * actually has driver level meaning and triggers a VC resize.
3098 *
3099 * Locking:
Alan Coxca9bda02006-09-29 02:00:03 -07003100 * Called function use the console_sem is used to ensure we do
3101 * not try and resize the console twice at once.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003102 * The tty->termios_mutex is used to ensure we don't double
3103 * resize and get confused. Lock order - tty->termios_mutex before
Alan Coxca9bda02006-09-29 02:00:03 -07003104 * console sem
Alan Coxaf9b8972006-08-27 01:24:01 -07003105 */
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
Alan Cox37bdfb02008-02-08 04:18:47 -08003108 struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct winsize tmp_ws;
Alan Cox47f86832008-04-30 00:53:30 -07003111 struct pid *pgrp, *rpgrp;
3112 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
3115 return -EFAULT;
Alan Coxca9bda02006-09-29 02:00:03 -07003116
Arjan van de Ven5785c952006-09-29 02:00:43 -07003117 mutex_lock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
Alan Coxca9bda02006-09-29 02:00:03 -07003119 goto done;
3120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121#ifdef CONFIG_VT
3122 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
Arjan van de Ven5785c952006-09-29 02:00:43 -07003123 if (vc_lock_resize(tty->driver_data, tmp_ws.ws_col,
3124 tmp_ws.ws_row)) {
3125 mutex_unlock(&tty->termios_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003126 return -ENXIO;
Alan Coxca9bda02006-09-29 02:00:03 -07003127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129#endif
Alan Cox47f86832008-04-30 00:53:30 -07003130 /* Get the PID values and reference them so we can
3131 avoid holding the tty ctrl lock while sending signals */
3132 spin_lock_irqsave(&tty->ctrl_lock, flags);
3133 pgrp = get_pid(tty->pgrp);
3134 rpgrp = get_pid(real_tty->pgrp);
3135 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3136
3137 if (pgrp)
3138 kill_pgrp(pgrp, SIGWINCH, 1);
3139 if (rpgrp != pgrp && rpgrp)
3140 kill_pgrp(rpgrp, SIGWINCH, 1);
3141
3142 put_pid(pgrp);
3143 put_pid(rpgrp);
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 tty->winsize = tmp_ws;
3146 real_tty->winsize = tmp_ws;
Alan Coxca9bda02006-09-29 02:00:03 -07003147done:
Arjan van de Ven5785c952006-09-29 02:00:43 -07003148 mutex_unlock(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return 0;
3150}
3151
Alan Coxaf9b8972006-08-27 01:24:01 -07003152/**
3153 * tioccons - allow admin to move logical console
3154 * @file: the file to become console
3155 *
3156 * Allow the adminstrator to move the redirected console device
3157 *
3158 * Locking: uses redirect_lock to guard the redirect information
3159 */
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static int tioccons(struct file *file)
3162{
3163 if (!capable(CAP_SYS_ADMIN))
3164 return -EPERM;
3165 if (file->f_op->write == redirected_tty_write) {
3166 struct file *f;
3167 spin_lock(&redirect_lock);
3168 f = redirect;
3169 redirect = NULL;
3170 spin_unlock(&redirect_lock);
3171 if (f)
3172 fput(f);
3173 return 0;
3174 }
3175 spin_lock(&redirect_lock);
3176 if (redirect) {
3177 spin_unlock(&redirect_lock);
3178 return -EBUSY;
3179 }
3180 get_file(file);
3181 redirect = file;
3182 spin_unlock(&redirect_lock);
3183 return 0;
3184}
3185
Alan Coxaf9b8972006-08-27 01:24:01 -07003186/**
3187 * fionbio - non blocking ioctl
3188 * @file: file to set blocking value
3189 * @p: user parameter
3190 *
3191 * Historical tty interfaces had a blocking control ioctl before
3192 * the generic functionality existed. This piece of history is preserved
3193 * in the expected tty API of posix OS's.
3194 *
3195 * Locking: none, the open fle handle ensures it won't go away.
3196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198static int fionbio(struct file *file, int __user *p)
3199{
3200 int nonblock;
3201
3202 if (get_user(nonblock, p))
3203 return -EFAULT;
3204
Alan Cox04f378b2008-04-30 00:53:29 -07003205 /* file->f_flags is still BKL protected in the fs layer - vomit */
3206 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (nonblock)
3208 file->f_flags |= O_NONBLOCK;
3209 else
3210 file->f_flags &= ~O_NONBLOCK;
Alan Cox04f378b2008-04-30 00:53:29 -07003211 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return 0;
3213}
3214
Alan Coxaf9b8972006-08-27 01:24:01 -07003215/**
3216 * tiocsctty - set controlling tty
3217 * @tty: tty structure
3218 * @arg: user argument
3219 *
3220 * This ioctl is used to manage job control. It permits a session
3221 * leader to set this tty as the controlling tty for the session.
3222 *
3223 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07003224 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003225 * Takes tasklist_lock internally to walk sessions
3226 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07003227 */
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229static int tiocsctty(struct tty_struct *tty, int arg)
3230{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003231 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003232 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003233 return ret;
3234
3235 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /*
3237 * The process must be a session leader and
3238 * not have a controlling tty already.
3239 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003240 if (!current->signal->leader || current->signal->tty) {
3241 ret = -EPERM;
3242 goto unlock;
3243 }
3244
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003245 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 /*
3247 * This tty is already the controlling
3248 * tty for another session group!
3249 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003250 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 /*
3252 * Steal it away
3253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003255 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003257 } else {
3258 ret = -EPERM;
3259 goto unlock;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003262 proc_set_tty(current, tty);
3263unlock:
Alan Cox28298232006-09-29 02:00:58 -07003264 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
3267
Alan Coxaf9b8972006-08-27 01:24:01 -07003268/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07003269 * tty_get_pgrp - return a ref counted pgrp pid
3270 * @tty: tty to read
3271 *
3272 * Returns a refcounted instance of the pid struct for the process
3273 * group controlling the tty.
3274 */
3275
3276struct pid *tty_get_pgrp(struct tty_struct *tty)
3277{
3278 unsigned long flags;
3279 struct pid *pgrp;
3280
3281 spin_lock_irqsave(&tty->ctrl_lock, flags);
3282 pgrp = get_pid(tty->pgrp);
3283 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3284
3285 return pgrp;
3286}
3287EXPORT_SYMBOL_GPL(tty_get_pgrp);
3288
3289/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003290 * tiocgpgrp - get process group
3291 * @tty: tty passed by user
3292 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3293 * @p: returned pid
3294 *
3295 * Obtain the process group of the tty. If there is no process group
3296 * return an error.
3297 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003298 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07003299 */
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3302{
Alan Cox5d0fdf12008-04-30 00:53:31 -07003303 struct pid *pid;
3304 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 /*
3306 * (tty == real_tty) is a cheap way of
3307 * testing if the tty is NOT a master pty.
3308 */
3309 if (tty == real_tty && current->signal->tty != real_tty)
3310 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07003311 pid = tty_get_pgrp(real_tty);
3312 ret = put_user(pid_vnr(pid), p);
3313 put_pid(pid);
3314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Alan Coxaf9b8972006-08-27 01:24:01 -07003317/**
3318 * tiocspgrp - attempt to set process group
3319 * @tty: tty passed by user
3320 * @real_tty: tty side device matching tty passed by user
3321 * @p: pid pointer
3322 *
3323 * Set the process group of the tty to the session passed. Only
3324 * permitted where the tty session is our session.
3325 *
Alan Cox47f86832008-04-30 00:53:30 -07003326 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07003327 */
3328
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3330{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003331 struct pid *pgrp;
3332 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07003334 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 if (retval == -EIO)
3337 return -ENOTTY;
3338 if (retval)
3339 return retval;
3340 if (!current->signal->tty ||
3341 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003342 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003344 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003346 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003348 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003349 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003350 retval = -ESRCH;
3351 if (!pgrp)
3352 goto out_unlock;
3353 retval = -EPERM;
3354 if (session_of_pgrp(pgrp) != task_session(current))
3355 goto out_unlock;
3356 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07003357 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003358 put_pid(real_tty->pgrp);
3359 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07003360 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08003361out_unlock:
3362 rcu_read_unlock();
3363 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
Alan Coxaf9b8972006-08-27 01:24:01 -07003366/**
3367 * tiocgsid - get session id
3368 * @tty: tty passed by user
3369 * @real_tty: tty side of the tty pased by the user if a pty else the tty
3370 * @p: pointer to returned session id
3371 *
3372 * Obtain the session id of the tty. If there is no session
3373 * return an error.
3374 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003375 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07003376 */
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
3379{
3380 /*
3381 * (tty == real_tty) is a cheap way of
3382 * testing if the tty is NOT a master pty.
3383 */
3384 if (tty == real_tty && current->signal->tty != real_tty)
3385 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003386 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003388 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Alan Coxaf9b8972006-08-27 01:24:01 -07003391/**
3392 * tiocsetd - set line discipline
3393 * @tty: tty device
3394 * @p: pointer to user data
3395 *
3396 * Set the line discipline according to user request.
3397 *
3398 * Locking: see tty_set_ldisc, this function is just a helper
3399 */
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401static int tiocsetd(struct tty_struct *tty, int __user *p)
3402{
3403 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07003404 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
3406 if (get_user(ldisc, p))
3407 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07003408
3409 lock_kernel();
3410 ret = tty_set_ldisc(tty, ldisc);
3411 unlock_kernel();
3412
3413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Alan Coxaf9b8972006-08-27 01:24:01 -07003416/**
3417 * send_break - performed time break
3418 * @tty: device to break on
3419 * @duration: timeout in mS
3420 *
3421 * Perform a timed break on hardware that lacks its own driver level
3422 * timed break functionality.
3423 *
3424 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07003425 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07003426 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003427 */
3428
Domen Puncerb20f3ae2005-06-25 14:58:42 -07003429static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430{
Alan Cox9c1729d2007-07-15 23:39:43 -07003431 if (tty_write_lock(tty, 0) < 0)
Alan Coxf34d7a52008-04-30 00:54:13 -07003432 return -EINTR;
3433 tty->ops->break_ctl(tty, -1);
Alan Cox9c1729d2007-07-15 23:39:43 -07003434 if (!signal_pending(current))
Domen Puncerb20f3ae2005-06-25 14:58:42 -07003435 msleep_interruptible(duration);
Alan Coxf34d7a52008-04-30 00:54:13 -07003436 tty->ops->break_ctl(tty, 0);
Alan Cox9c1729d2007-07-15 23:39:43 -07003437 tty_write_unlock(tty);
Alan Cox3e2a0782008-06-30 17:40:08 +01003438 if (signal_pending(current))
Alan Coxf34d7a52008-04-30 00:54:13 -07003439 return -EINTR;
3440 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441}
3442
Alan Coxaf9b8972006-08-27 01:24:01 -07003443/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003444 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07003445 * @tty: tty device
3446 * @file: user file pointer
3447 * @p: pointer to result
3448 *
3449 * Obtain the modem status bits from the tty driver if the feature
3450 * is supported. Return -EINVAL if it is not available.
3451 *
3452 * Locking: none (up to the driver)
3453 */
3454
3455static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 int retval = -EINVAL;
3458
Alan Coxf34d7a52008-04-30 00:54:13 -07003459 if (tty->ops->tiocmget) {
3460 retval = tty->ops->tiocmget(tty, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 if (retval >= 0)
3463 retval = put_user(retval, p);
3464 }
3465 return retval;
3466}
3467
Alan Coxaf9b8972006-08-27 01:24:01 -07003468/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003469 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07003470 * @tty: tty device
3471 * @file: user file pointer
3472 * @cmd: command - clear bits, set bits or set all
3473 * @p: pointer to desired bits
3474 *
3475 * Set the modem status bits from the tty driver if the feature
3476 * is supported. Return -EINVAL if it is not available.
3477 *
3478 * Locking: none (up to the driver)
3479 */
3480
3481static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 unsigned __user *p)
3483{
3484 int retval = -EINVAL;
3485
Alan Coxf34d7a52008-04-30 00:54:13 -07003486 if (tty->ops->tiocmset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 unsigned int set, clear, val;
3488
3489 retval = get_user(val, p);
3490 if (retval)
3491 return retval;
3492
3493 set = clear = 0;
3494 switch (cmd) {
3495 case TIOCMBIS:
3496 set = val;
3497 break;
3498 case TIOCMBIC:
3499 clear = val;
3500 break;
3501 case TIOCMSET:
3502 set = val;
3503 clear = ~val;
3504 break;
3505 }
3506
3507 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3508 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
3509
Alan Coxf34d7a52008-04-30 00:54:13 -07003510 retval = tty->ops->tiocmset(tty, file, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
3512 return retval;
3513}
3514
3515/*
3516 * Split this up, as gcc can choke on it otherwise..
3517 */
Alan Cox04f378b2008-04-30 00:53:29 -07003518long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
3520 struct tty_struct *tty, *real_tty;
3521 void __user *p = (void __user *)arg;
3522 int retval;
3523 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -07003524 struct inode *inode = file->f_dentry->d_inode;
Alan Cox37bdfb02008-02-08 04:18:47 -08003525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 tty = (struct tty_struct *)file->private_data;
3527 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3528 return -EINVAL;
3529
3530 real_tty = tty;
3531 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
3532 tty->driver->subtype == PTY_TYPE_MASTER)
3533 real_tty = tty->link;
3534
3535 /*
3536 * Break handling by driver
3537 */
Alan Cox04f378b2008-04-30 00:53:29 -07003538
3539 retval = -EINVAL;
3540
Alan Coxf34d7a52008-04-30 00:54:13 -07003541 if (!tty->ops->break_ctl) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003542 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 case TIOCSBRK:
3544 case TIOCCBRK:
Alan Coxf34d7a52008-04-30 00:54:13 -07003545 if (tty->ops->ioctl)
3546 retval = tty->ops->ioctl(tty, file, cmd, arg);
3547 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3548 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
Alan Cox04f378b2008-04-30 00:53:29 -07003549 return retval;
Alan Cox37bdfb02008-02-08 04:18:47 -08003550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 /* These two ioctl's always return success; even if */
3552 /* the driver doesn't support them. */
3553 case TCSBRK:
3554 case TCSBRKP:
Alan Coxf34d7a52008-04-30 00:54:13 -07003555 if (!tty->ops->ioctl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return 0;
Alan Coxf34d7a52008-04-30 00:54:13 -07003557 retval = tty->ops->ioctl(tty, file, cmd, arg);
3558 if (retval != -EINVAL && retval != -ENOIOCTLCMD)
3559 printk(KERN_WARNING "tty: driver %s needs updating to use break_ctl\n", tty->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (retval == -ENOIOCTLCMD)
3561 retval = 0;
3562 return retval;
3563 }
3564 }
3565
3566 /*
3567 * Factor out some common prep work
3568 */
3569 switch (cmd) {
3570 case TIOCSETD:
3571 case TIOCSBRK:
3572 case TIOCCBRK:
3573 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08003574 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 retval = tty_check_change(tty);
3576 if (retval)
3577 return retval;
3578 if (cmd != TIOCCBRK) {
3579 tty_wait_until_sent(tty, 0);
3580 if (signal_pending(current))
3581 return -EINTR;
3582 }
3583 break;
3584 }
3585
3586 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003587 case TIOCSTI:
3588 return tiocsti(tty, p);
3589 case TIOCGWINSZ:
3590 return tiocgwinsz(tty, p);
3591 case TIOCSWINSZ:
3592 return tiocswinsz(tty, real_tty, p);
3593 case TIOCCONS:
3594 return real_tty != tty ? -EINVAL : tioccons(file);
3595 case FIONBIO:
3596 return fionbio(file, p);
3597 case TIOCEXCL:
3598 set_bit(TTY_EXCLUSIVE, &tty->flags);
3599 return 0;
3600 case TIOCNXCL:
3601 clear_bit(TTY_EXCLUSIVE, &tty->flags);
3602 return 0;
3603 case TIOCNOTTY:
3604 if (current->signal->tty != tty)
3605 return -ENOTTY;
3606 no_tty();
3607 return 0;
3608 case TIOCSCTTY:
3609 return tiocsctty(tty, arg);
3610 case TIOCGPGRP:
3611 return tiocgpgrp(tty, real_tty, p);
3612 case TIOCSPGRP:
3613 return tiocspgrp(tty, real_tty, p);
3614 case TIOCGSID:
3615 return tiocgsid(tty, real_tty, p);
3616 case TIOCGETD:
Alan Coxa352def2008-07-16 21:53:12 +01003617 return put_user(tty->ldisc.ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08003618 case TIOCSETD:
3619 return tiocsetd(tty, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08003621 case TIOCLINUX:
3622 return tioclinux(tty, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08003624 /*
3625 * Break handling
3626 */
3627 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07003628 if (tty->ops->break_ctl)
3629 tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08003630 return 0;
3631
3632 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07003633 if (tty->ops->break_ctl)
3634 tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08003635 return 0;
3636 case TCSBRK: /* SVID version: non-zero arg --> no break */
3637 /* non-zero arg means wait for all output data
3638 * to be sent (performed above) but don't send break.
3639 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003641 if (!arg)
3642 return send_break(tty, 250);
3643 return 0;
3644 case TCSBRKP: /* support for POSIX tcsendbreak() */
3645 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Alan Cox37bdfb02008-02-08 04:18:47 -08003647 case TIOCMGET:
3648 return tty_tiocmget(tty, file, p);
3649 case TIOCMSET:
3650 case TIOCMBIC:
3651 case TIOCMBIS:
3652 return tty_tiocmset(tty, file, cmd, p);
3653 case TCFLSH:
3654 switch (arg) {
3655 case TCIFLUSH:
3656 case TCIOFLUSH:
3657 /* flush tty buffer and allow ldisc to process ioctl */
3658 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07003659 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08003660 }
3661 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Alan Coxf34d7a52008-04-30 00:54:13 -07003663 if (tty->ops->ioctl) {
3664 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (retval != -ENOIOCTLCMD)
3666 return retval;
3667 }
3668 ld = tty_ldisc_ref_wait(tty);
3669 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01003670 if (ld->ops->ioctl) {
3671 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (retval == -ENOIOCTLCMD)
3673 retval = -EINVAL;
3674 }
3675 tty_ldisc_deref(ld);
3676 return retval;
3677}
3678
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003679#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08003680static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003681 unsigned long arg)
3682{
3683 struct inode *inode = file->f_dentry->d_inode;
3684 struct tty_struct *tty = file->private_data;
3685 struct tty_ldisc *ld;
3686 int retval = -ENOIOCTLCMD;
3687
3688 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
3689 return -EINVAL;
3690
Alan Coxf34d7a52008-04-30 00:54:13 -07003691 if (tty->ops->compat_ioctl) {
3692 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003693 if (retval != -ENOIOCTLCMD)
3694 return retval;
3695 }
3696
3697 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01003698 if (ld->ops->compat_ioctl)
3699 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003700 tty_ldisc_deref(ld);
3701
3702 return retval;
3703}
3704#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706/*
3707 * This implements the "Secure Attention Key" --- the idea is to
3708 * prevent trojan horses by killing all processes associated with this
3709 * tty when the user hits the "Secure Attention Key". Required for
3710 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08003711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 * This code could be nicer; ideally it should send a HUP, wait a few
3713 * seconds, then send a INT, and then a KILL signal. But you then
3714 * have to coordinate with the init process, since all processes associated
3715 * with the current tty must be dead before the new getty is allowed
3716 * to spawn.
3717 *
3718 * Now, if it would be correct ;-/ The current code has a nasty hole -
3719 * it doesn't catch files in flight. We may send the descriptor to ourselves
3720 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3721 *
3722 * Nasty bug: do_SAK is being called in interrupt context. This can
3723 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3724 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003725void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727#ifdef TTY_SOFT_SAK
3728 tty_hangup(tty);
3729#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003730 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003731 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 int i;
3733 struct file *filp;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07003734 struct fdtable *fdt;
Alan Cox37bdfb02008-02-08 04:18:47 -08003735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 if (!tty)
3737 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003738 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08003739
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003740 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Alan Coxf34d7a52008-04-30 00:54:13 -07003742 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003745 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003746 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08003747 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07003748 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003749 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003750 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003751 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003752 /* Now kill any processes that happen to have the
3753 * tty open.
3754 */
3755 do_each_thread(g, p) {
3756 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07003758 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003759 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 send_sig(SIGKILL, p, 1);
3761 continue;
3762 }
3763 task_lock(p);
3764 if (p->files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07003765 /*
3766 * We don't take a ref to the file, so we must
3767 * hold ->file_lock instead.
3768 */
3769 spin_lock(&p->files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07003770 fdt = files_fdtable(p->files);
Alan Cox37bdfb02008-02-08 04:18:47 -08003771 for (i = 0; i < fdt->max_fds; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 filp = fcheck_files(p->files, i);
3773 if (!filp)
3774 continue;
3775 if (filp->f_op->read == tty_read &&
3776 filp->private_data == tty) {
3777 printk(KERN_NOTICE "SAK: killed process %d"
3778 " (%s): fd#%d opened to the tty\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003779 task_pid_nr(p), p->comm, i);
Eric W. Biederman20ac9432006-04-13 04:49:07 -06003780 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 break;
3782 }
3783 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07003784 spin_unlock(&p->files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 }
3786 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003787 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 read_unlock(&tasklist_lock);
3789#endif
3790}
3791
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003792static void do_SAK_work(struct work_struct *work)
3793{
3794 struct tty_struct *tty =
3795 container_of(work, struct tty_struct, SAK_work);
3796 __do_SAK(tty);
3797}
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799/*
3800 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3801 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3802 * the values which we write to it will be identical to the values which it
3803 * already has. --akpm
3804 */
3805void do_SAK(struct tty_struct *tty)
3806{
3807 if (!tty)
3808 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 schedule_work(&tty->SAK_work);
3810}
3811
3812EXPORT_SYMBOL(do_SAK);
3813
Alan Coxaf9b8972006-08-27 01:24:01 -07003814/**
3815 * flush_to_ldisc
David Howells65f27f32006-11-22 14:55:48 +00003816 * @work: tty structure passed from work queue.
Alan Coxaf9b8972006-08-27 01:24:01 -07003817 *
3818 * This routine is called out of the software interrupt to flush data
3819 * from the buffer chain to the line discipline.
3820 *
3821 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
3822 * while invoking the line discipline receive_buf method. The
3823 * receive_buf method is single threaded for each tty instance.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 */
Alan Cox37bdfb02008-02-08 04:18:47 -08003825
David Howells65f27f32006-11-22 14:55:48 +00003826static void flush_to_ldisc(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
David Howells65f27f32006-11-22 14:55:48 +00003828 struct tty_struct *tty =
3829 container_of(work, struct tty_struct, buf.work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 unsigned long flags;
3831 struct tty_ldisc *disc;
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003832 struct tty_buffer *tbuf, *head;
Paul Fulghum8977d922006-02-10 01:51:14 -08003833 char *char_buf;
3834 unsigned char *flag_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
3836 disc = tty_ldisc_ref(tty);
3837 if (disc == NULL) /* !TTY_LDISC */
3838 return;
3839
Paul Fulghum808249c2006-02-03 03:04:41 -08003840 spin_lock_irqsave(&tty->buf.lock, flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08003841 /* So we know a flush is running */
3842 set_bit(TTY_FLUSHING, &tty->flags);
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003843 head = tty->buf.head;
3844 if (head != NULL) {
3845 tty->buf.head = NULL;
3846 for (;;) {
3847 int count = head->commit - head->read;
3848 if (!count) {
3849 if (head->next == NULL)
3850 break;
3851 tbuf = head;
3852 head = head->next;
3853 tty_buffer_free(tty, tbuf);
3854 continue;
3855 }
Alan Cox42fd5522007-08-10 13:01:05 -07003856 /* Ldisc or user is trying to flush the buffers
3857 we are feeding to the ldisc, stop feeding the
3858 line discipline as we want to empty the queue */
3859 if (test_bit(TTY_FLUSHPENDING, &tty->flags))
3860 break;
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003861 if (!tty->receive_room) {
3862 schedule_delayed_work(&tty->buf.work, 1);
3863 break;
3864 }
3865 if (count > tty->receive_room)
3866 count = tty->receive_room;
3867 char_buf = head->char_buf_ptr + head->read;
3868 flag_buf = head->flag_buf_ptr + head->read;
3869 head->read += count;
Paul Fulghum8977d922006-02-10 01:51:14 -08003870 spin_unlock_irqrestore(&tty->buf.lock, flags);
Alan Coxa352def2008-07-16 21:53:12 +01003871 disc->ops->receive_buf(tty, char_buf,
3872 flag_buf, count);
Paul Fulghum8977d922006-02-10 01:51:14 -08003873 spin_lock_irqsave(&tty->buf.lock, flags);
3874 }
Alan Cox42fd5522007-08-10 13:01:05 -07003875 /* Restore the queue head */
Paul Fulghum2c3bb202006-06-28 04:26:48 -07003876 tty->buf.head = head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
Alan Cox42fd5522007-08-10 13:01:05 -07003878 /* We may have a deferred request to flush the input buffer,
3879 if so pull the chain under the lock and empty the queue */
3880 if (test_bit(TTY_FLUSHPENDING, &tty->flags)) {
3881 __tty_buffer_flush(tty);
3882 clear_bit(TTY_FLUSHPENDING, &tty->flags);
3883 wake_up(&tty->read_wait);
3884 }
3885 clear_bit(TTY_FLUSHING, &tty->flags);
Paul Fulghum808249c2006-02-03 03:04:41 -08003886 spin_unlock_irqrestore(&tty->buf.lock, flags);
Paul Fulghum817d6d32006-06-28 04:26:47 -07003887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 tty_ldisc_deref(disc);
3889}
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891/**
3892 * tty_flip_buffer_push - terminal
3893 * @tty: tty to push
3894 *
3895 * Queue a push of the terminal flip buffers to the line discipline. This
3896 * function must not be called from IRQ context if tty->low_latency is set.
3897 *
3898 * In the event of the queue being busy for flipping the work will be
3899 * held off and retried later.
Alan Coxaf9b8972006-08-27 01:24:01 -07003900 *
3901 * Locking: tty buffer lock. Driver locks in low latency mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 */
3903
3904void tty_flip_buffer_push(struct tty_struct *tty)
3905{
Paul Fulghum808249c2006-02-03 03:04:41 -08003906 unsigned long flags;
3907 spin_lock_irqsave(&tty->buf.lock, flags);
Paul Fulghum33b37a32006-06-28 04:26:49 -07003908 if (tty->buf.tail != NULL)
Paul Fulghum8977d922006-02-10 01:51:14 -08003909 tty->buf.tail->commit = tty->buf.tail->used;
Paul Fulghum808249c2006-02-03 03:04:41 -08003910 spin_unlock_irqrestore(&tty->buf.lock, flags);
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 if (tty->low_latency)
David Howells65f27f32006-11-22 14:55:48 +00003913 flush_to_ldisc(&tty->buf.work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 else
Alan Cox33f0f882006-01-09 20:54:13 -08003915 schedule_delayed_work(&tty->buf.work, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
3918EXPORT_SYMBOL(tty_flip_buffer_push);
3919
Alan Cox33f0f882006-01-09 20:54:13 -08003920
Alan Coxaf9b8972006-08-27 01:24:01 -07003921/**
3922 * initialize_tty_struct
3923 * @tty: tty to initialize
3924 *
3925 * This subroutine initializes a tty structure that has been newly
3926 * allocated.
3927 *
3928 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931static void initialize_tty_struct(struct tty_struct *tty)
3932{
Alan Coxa352def2008-07-16 21:53:12 +01003933 struct tty_ldisc ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 memset(tty, 0, sizeof(struct tty_struct));
3935 tty->magic = TTY_MAGIC;
Alan Coxa352def2008-07-16 21:53:12 +01003936 if (tty_ldisc_get(N_TTY, &ld) < 0)
3937 panic("n_tty: init_tty");
3938 tty_ldisc_assign(tty, &ld);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003939 tty->session = NULL;
3940 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 tty->overrun_time = jiffies;
Alan Cox33f0f882006-01-09 20:54:13 -08003942 tty->buf.head = tty->buf.tail = NULL;
3943 tty_buffer_init(tty);
David Howells65f27f32006-11-22 14:55:48 +00003944 INIT_DELAYED_WORK(&tty->buf.work, flush_to_ldisc);
Arjan van de Ven5785c952006-09-29 02:00:43 -07003945 mutex_init(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 init_waitqueue_head(&tty->write_wait);
3947 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003948 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003949 mutex_init(&tty->atomic_read_lock);
3950 mutex_init(&tty->atomic_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 spin_lock_init(&tty->read_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07003952 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003954 INIT_WORK(&tty->SAK_work, do_SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
Alan Coxf34d7a52008-04-30 00:54:13 -07003957/**
3958 * tty_put_char - write one character to a tty
3959 * @tty: tty
3960 * @ch: character
3961 *
3962 * Write one byte to the tty using the provided put_char method
3963 * if present. Returns the number of characters successfully output.
3964 *
3965 * Note: the specific put_char operation in the driver layer may go
3966 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003968
Alan Coxf34d7a52008-04-30 00:54:13 -07003969int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
Alan Coxf34d7a52008-04-30 00:54:13 -07003971 if (tty->ops->put_char)
3972 return tty->ops->put_char(tty, ch);
3973 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
Alan Coxf34d7a52008-04-30 00:54:13 -07003976EXPORT_SYMBOL_GPL(tty_put_char);
3977
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003978static struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979
3980/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003981 * tty_register_device - register a tty device
3982 * @driver: the tty driver that describes the tty device
3983 * @index: the index in the tty driver for this tty device
3984 * @device: a struct device that is associated with this tty device.
3985 * This field is optional, if there is no known struct device
3986 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003988 * Returns a pointer to the struct device for this tty device
3989 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003990 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003991 * This call is required to be made to register an individual tty device
3992 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3993 * that bit is not set, this function should not be called by a tty
3994 * driver.
3995 *
3996 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003998
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003999struct device *tty_register_device(struct tty_driver *driver, unsigned index,
4000 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
4002 char name[64];
4003 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
4004
4005 if (index >= driver->num) {
4006 printk(KERN_ERR "Attempt to register invalid tty line number "
4007 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02004008 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 if (driver->type == TTY_DRIVER_TYPE_PTY)
4012 pty_line_name(driver, index, name);
4013 else
4014 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02004015
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004016 return device_create(tty_class, device, dev, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
4019/**
Alan Coxaf9b8972006-08-27 01:24:01 -07004020 * tty_unregister_device - unregister a tty device
4021 * @driver: the tty driver that describes the tty device
4022 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 *
Alan Coxaf9b8972006-08-27 01:24:01 -07004024 * If a tty device is registered with a call to tty_register_device() then
4025 * this function must be called when the tty device is gone.
4026 *
4027 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 */
Alan Coxaf9b8972006-08-27 01:24:01 -07004029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030void tty_unregister_device(struct tty_driver *driver, unsigned index)
4031{
Alan Cox37bdfb02008-02-08 04:18:47 -08004032 device_destroy(tty_class,
4033 MKDEV(driver->major, driver->minor_start) + index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
4036EXPORT_SYMBOL(tty_register_device);
4037EXPORT_SYMBOL(tty_unregister_device);
4038
4039struct tty_driver *alloc_tty_driver(int lines)
4040{
4041 struct tty_driver *driver;
4042
Jean Delvare506eb992007-07-15 23:40:14 -07004043 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 if (driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 driver->magic = TTY_DRIVER_MAGIC;
4046 driver->num = lines;
4047 /* later we'll move allocation of tables here */
4048 }
4049 return driver;
4050}
4051
4052void put_tty_driver(struct tty_driver *driver)
4053{
4054 kfree(driver);
4055}
4056
Jeff Dikeb68e31d2006-10-02 02:17:18 -07004057void tty_set_operations(struct tty_driver *driver,
4058 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Alan Coxf34d7a52008-04-30 00:54:13 -07004060 driver->ops = op;
4061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
4063EXPORT_SYMBOL(alloc_tty_driver);
4064EXPORT_SYMBOL(put_tty_driver);
4065EXPORT_SYMBOL(tty_set_operations);
4066
4067/*
4068 * Called by a tty driver to register itself.
4069 */
4070int tty_register_driver(struct tty_driver *driver)
4071{
4072 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08004073 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 dev_t dev;
4075 void **p = NULL;
4076
4077 if (driver->flags & TTY_DRIVER_INSTALLED)
4078 return 0;
4079
Andy Whitcroft543691a62007-05-06 14:49:33 -07004080 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
4081 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (!p)
4083 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
4085
4086 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08004087 error = alloc_chrdev_region(&dev, driver->minor_start,
4088 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (!error) {
4090 driver->major = MAJOR(dev);
4091 driver->minor_start = MINOR(dev);
4092 }
4093 } else {
4094 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01004095 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 }
4097 if (error < 0) {
4098 kfree(p);
4099 return error;
4100 }
4101
4102 if (p) {
4103 driver->ttys = (struct tty_struct **)p;
Alan Coxedc6afc2006-12-08 02:38:44 -08004104 driver->termios = (struct ktermios **)(p + driver->num);
Alan Cox37bdfb02008-02-08 04:18:47 -08004105 driver->termios_locked = (struct ktermios **)
4106 (p + driver->num * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 } else {
4108 driver->ttys = NULL;
4109 driver->termios = NULL;
4110 driver->termios_locked = NULL;
4111 }
4112
4113 cdev_init(&driver->cdev, &tty_fops);
4114 driver->cdev.owner = driver->owner;
4115 error = cdev_add(&driver->cdev, dev, driver->num);
4116 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 unregister_chrdev_region(dev, driver->num);
4118 driver->ttys = NULL;
4119 driver->termios = driver->termios_locked = NULL;
4120 kfree(p);
4121 return error;
4122 }
4123
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004124 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004126 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08004127
4128 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
4129 for (i = 0; i < driver->num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 tty_register_device(driver, i, NULL);
4131 }
4132 proc_tty_register_driver(driver);
4133 return 0;
4134}
4135
4136EXPORT_SYMBOL(tty_register_driver);
4137
4138/*
4139 * Called by a tty driver to unregister itself.
4140 */
4141int tty_unregister_driver(struct tty_driver *driver)
4142{
4143 int i;
Alan Coxedc6afc2006-12-08 02:38:44 -08004144 struct ktermios *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 void *p;
4146
4147 if (driver->refcount)
4148 return -EBUSY;
4149
4150 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
4151 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004152 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07004154 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
4156 /*
4157 * Free the termios and termios_locked structures because
4158 * we don't want to get memory leaks when modular tty
4159 * drivers are removed from the kernel.
4160 */
4161 for (i = 0; i < driver->num; i++) {
4162 tp = driver->termios[i];
4163 if (tp) {
4164 driver->termios[i] = NULL;
4165 kfree(tp);
4166 }
4167 tp = driver->termios_locked[i];
4168 if (tp) {
4169 driver->termios_locked[i] = NULL;
4170 kfree(tp);
4171 }
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07004172 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 tty_unregister_device(driver, i);
4174 }
4175 p = driver->ttys;
4176 proc_tty_unregister_driver(driver);
4177 driver->ttys = NULL;
4178 driver->termios = driver->termios_locked = NULL;
4179 kfree(p);
4180 cdev_del(&driver->cdev);
4181 return 0;
4182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183EXPORT_SYMBOL(tty_unregister_driver);
4184
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004185dev_t tty_devnum(struct tty_struct *tty)
4186{
4187 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
4188}
4189EXPORT_SYMBOL(tty_devnum);
4190
4191void proc_clear_tty(struct task_struct *p)
4192{
4193 spin_lock_irq(&p->sighand->siglock);
4194 p->signal->tty = NULL;
4195 spin_unlock_irq(&p->sighand->siglock);
4196}
David S. Miller7cac4ce2007-05-11 14:30:57 -07004197EXPORT_SYMBOL(proc_clear_tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004198
Alan Cox47f86832008-04-30 00:53:30 -07004199/* Called under the sighand lock */
4200
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004201static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004202{
4203 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07004204 unsigned long flags;
4205 /* We should not have a session or pgrp to put here but.... */
4206 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06004207 put_pid(tty->session);
4208 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08004209 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07004210 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
4211 tty->session = get_pid(task_session(tsk));
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004212 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004213 put_pid(tsk->signal->tty_old_pgrp);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004214 tsk->signal->tty = tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08004215 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004216}
4217
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07004218static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004219{
4220 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07004221 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08004222 spin_unlock_irq(&tsk->sighand->siglock);
4223}
4224
4225struct tty_struct *get_current_tty(void)
4226{
4227 struct tty_struct *tty;
4228 WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
4229 tty = current->signal->tty;
4230 /*
4231 * session->tty can be changed/cleared from under us, make sure we
4232 * issue the load. The obtained pointer, when not NULL, is valid as
4233 * long as we hold tty_mutex.
4234 */
4235 barrier();
4236 return tty;
4237}
Heiko Carstensa311f742006-12-13 00:33:41 -08004238EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240/*
4241 * Initialize the console device. This is called *early*, so
4242 * we can't necessarily depend on lots of kernel help here.
4243 * Just do some early initializations, and do the complex setup
4244 * later.
4245 */
4246void __init console_init(void)
4247{
4248 initcall_t *call;
4249
4250 /* Setup the default TTY line discipline. */
4251 (void) tty_register_ldisc(N_TTY, &tty_ldisc_N_TTY);
4252
4253 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08004254 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 * inform about problems etc..
4256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 call = __con_initcall_start;
4258 while (call < __con_initcall_end) {
4259 (*call)();
4260 call++;
4261 }
4262}
4263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264static int __init tty_class_init(void)
4265{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08004266 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (IS_ERR(tty_class))
4268 return PTR_ERR(tty_class);
4269 return 0;
4270}
4271
4272postcore_initcall(tty_class_init);
4273
4274/* 3/2004 jmc: why do these devices exist? */
4275
4276static struct cdev tty_cdev, console_cdev;
4277#ifdef CONFIG_UNIX98_PTYS
4278static struct cdev ptmx_cdev;
4279#endif
4280#ifdef CONFIG_VT
4281static struct cdev vc0_cdev;
4282#endif
4283
4284/*
4285 * Ok, now we can initialize the rest of the tty devices and can count
4286 * on memory allocations, interrupts etc..
4287 */
4288static int __init tty_init(void)
4289{
4290 cdev_init(&tty_cdev, &tty_fops);
4291 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
4292 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
4293 panic("Couldn't register /dev/tty driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004294 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 cdev_init(&console_cdev, &console_fops);
4297 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
4298 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
4299 panic("Couldn't register /dev/console driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004300 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
4302#ifdef CONFIG_UNIX98_PTYS
4303 cdev_init(&ptmx_cdev, &ptmx_fops);
4304 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
4305 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
4306 panic("Couldn't register /dev/ptmx driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004307 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308#endif
4309
4310#ifdef CONFIG_VT
4311 cdev_init(&vc0_cdev, &console_fops);
4312 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
4313 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
4314 panic("Couldn't register /dev/tty0 driver\n");
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07004315 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), "tty0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316
4317 vty_init();
4318#endif
4319 return 0;
4320}
4321module_init(tty_init);