blob: f40298e9873a0f274a8563181efa394d0e04bf50 [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 Coxaf9b8972006-08-27 01:24:01 -0700179/**
180 * free_tty_struct - free a disused tty
181 * @tty: tty struct to free
182 *
183 * Free the write buffers, tty queue and tty memory itself.
184 *
185 * Locking: none. Must be called after tty is definitely unused
186 */
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static inline void free_tty_struct(struct tty_struct *tty)
189{
190 kfree(tty->write_buf);
Alan Cox33f0f882006-01-09 20:54:13 -0800191 tty_buffer_free_all(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 kfree(tty);
193}
194
195#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
196
Alan Coxaf9b8972006-08-27 01:24:01 -0700197/**
198 * tty_name - return tty naming
199 * @tty: tty structure
200 * @buf: buffer for output
201 *
202 * Convert a tty structure into a name. The name reflects the kernel
203 * naming policy and if udev is in use may not reflect user space
204 *
205 * Locking: none
206 */
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208char *tty_name(struct tty_struct *tty, char *buf)
209{
210 if (!tty) /* Hmm. NULL pointer. That's fun. */
211 strcpy(buf, "NULL tty");
212 else
213 strcpy(buf, tty->name);
214 return buf;
215}
216
217EXPORT_SYMBOL(tty_name);
218
Andrew Mortond769a662005-05-05 16:15:50 -0700219int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 const char *routine)
221{
222#ifdef TTY_PARANOIA_CHECK
223 if (!tty) {
224 printk(KERN_WARNING
225 "null TTY for (%d:%d) in %s\n",
226 imajor(inode), iminor(inode), routine);
227 return 1;
228 }
229 if (tty->magic != TTY_MAGIC) {
230 printk(KERN_WARNING
231 "bad magic number for tty struct (%d:%d) in %s\n",
232 imajor(inode), iminor(inode), routine);
233 return 1;
234 }
235#endif
236 return 0;
237}
238
239static int check_tty_count(struct tty_struct *tty, const char *routine)
240{
241#ifdef CHECK_TTY_COUNT
242 struct list_head *p;
243 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 file_list_lock();
246 list_for_each(p, &tty->tty_files) {
247 count++;
248 }
249 file_list_unlock();
250 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
251 tty->driver->subtype == PTY_TYPE_SLAVE &&
252 tty->link && tty->link->count)
253 count++;
254 if (tty->count != count) {
255 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
256 "!= #fd's(%d) in %s\n",
257 tty->name, tty->count, count, routine);
258 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260#endif
261 return 0;
262}
263
Alan Coxaf9b8972006-08-27 01:24:01 -0700264/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700265 * get_tty_driver - find device of a tty
266 * @dev_t: device identifier
267 * @index: returns the index of the tty
268 *
269 * This routine returns a tty driver structure, given a device number
270 * and also passes back the index number.
271 *
272 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static struct tty_driver *get_tty_driver(dev_t device, int *index)
276{
277 struct tty_driver *p;
278
279 list_for_each_entry(p, &tty_drivers, tty_drivers) {
280 dev_t base = MKDEV(p->major, p->minor_start);
281 if (device < base || device >= base + p->num)
282 continue;
283 *index = device - base;
284 return p;
285 }
286 return NULL;
287}
288
Jason Wesself2d937f2008-04-17 20:05:37 +0200289#ifdef CONFIG_CONSOLE_POLL
290
291/**
292 * tty_find_polling_driver - find device of a polled tty
293 * @name: name string to match
294 * @line: pointer to resulting tty line nr
295 *
296 * This routine returns a tty driver structure, given a name
297 * and the condition that the tty driver is capable of polled
298 * operation.
299 */
300struct tty_driver *tty_find_polling_driver(char *name, int *line)
301{
302 struct tty_driver *p, *res = NULL;
303 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500304 int len;
Jason Wesself2d937f2008-04-17 20:05:37 +0200305 char *str;
306
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500307 for (str = name; *str; str++)
308 if ((*str >= '0' && *str <= '9') || *str == ',')
309 break;
310 if (!*str)
311 return NULL;
312
313 len = str - name;
314 tty_line = simple_strtoul(str, &str, 10);
315
Jason Wesself2d937f2008-04-17 20:05:37 +0200316 mutex_lock(&tty_mutex);
317 /* Search through the tty devices to look for a match */
318 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500319 if (strncmp(name, p->name, len) != 0)
320 continue;
Jason Wesself2d937f2008-04-17 20:05:37 +0200321 if (*str == ',')
322 str++;
323 if (*str == '\0')
Harvey Harrison8da56302008-04-28 14:13:20 -0700324 str = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200325
Alan Coxf34d7a52008-04-30 00:54:13 -0700326 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
327 p->ops->poll_init && !p->ops->poll_init(p, tty_line, str)) {
Jason Wesself2d937f2008-04-17 20:05:37 +0200328 res = p;
329 *line = tty_line;
330 break;
331 }
332 }
333 mutex_unlock(&tty_mutex);
334
335 return res;
336}
337EXPORT_SYMBOL_GPL(tty_find_polling_driver);
338#endif
339
Alan Coxaf9b8972006-08-27 01:24:01 -0700340/**
341 * tty_check_change - check for POSIX terminal changes
342 * @tty: tty to check
343 *
344 * If we try to write to, or set the state of, a terminal and we're
345 * not in the foreground, send a SIGTTOU. If the signal is blocked or
346 * ignored, go ahead and perform the operation. (POSIX 7.2)
347 *
Alan Cox978e5952008-04-30 00:53:59 -0700348 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700350
Alan Cox37bdfb02008-02-08 04:18:47 -0800351int tty_check_change(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Alan Cox47f86832008-04-30 00:53:30 -0700353 unsigned long flags;
354 int ret = 0;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (current->signal->tty != tty)
357 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700358
359 spin_lock_irqsave(&tty->ctrl_lock, flags);
360
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800361 if (!tty->pgrp) {
362 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700363 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800365 if (task_pgrp(current) == tty->pgrp)
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700366 goto out_unlock;
367 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (is_ignored(SIGTTOU))
Alan Cox47f86832008-04-30 00:53:30 -0700369 goto out;
370 if (is_current_pgrp_orphaned()) {
371 ret = -EIO;
372 goto out;
373 }
Oleg Nesterov040b6362007-06-01 00:46:53 -0700374 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
375 set_thread_flag(TIF_SIGPENDING);
Alan Cox47f86832008-04-30 00:53:30 -0700376 ret = -ERESTARTSYS;
377out:
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700378 return ret;
379out_unlock:
Alan Cox47f86832008-04-30 00:53:30 -0700380 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
381 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384EXPORT_SYMBOL(tty_check_change);
385
Alan Cox37bdfb02008-02-08 04:18:47 -0800386static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 size_t count, loff_t *ppos)
388{
389 return 0;
390}
391
Alan Cox37bdfb02008-02-08 04:18:47 -0800392static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 size_t count, loff_t *ppos)
394{
395 return -EIO;
396}
397
398/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800399static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
402}
403
Alan Cox04f378b2008-04-30 00:53:29 -0700404static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
405 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700406{
407 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
408}
409
Alan Cox37bdfb02008-02-08 04:18:47 -0800410static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700411 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
414}
415
Arjan van de Ven62322d22006-07-03 00:24:21 -0700416static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .llseek = no_llseek,
418 .read = tty_read,
419 .write = tty_write,
420 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700421 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700422 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .open = tty_open,
424 .release = tty_release,
425 .fasync = tty_fasync,
426};
427
428#ifdef CONFIG_UNIX98_PTYS
Arjan van de Ven62322d22006-07-03 00:24:21 -0700429static const struct file_operations ptmx_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 .llseek = no_llseek,
431 .read = tty_read,
432 .write = tty_write,
433 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700434 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700435 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .open = ptmx_open,
437 .release = tty_release,
438 .fasync = tty_fasync,
439};
440#endif
441
Arjan van de Ven62322d22006-07-03 00:24:21 -0700442static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .llseek = no_llseek,
444 .read = tty_read,
445 .write = redirected_tty_write,
446 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700447 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700448 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .open = tty_open,
450 .release = tty_release,
451 .fasync = tty_fasync,
452};
453
Arjan van de Ven62322d22006-07-03 00:24:21 -0700454static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .llseek = no_llseek,
456 .read = hung_up_tty_read,
457 .write = hung_up_tty_write,
458 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700459 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700460 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 .release = tty_release,
462};
463
464static DEFINE_SPINLOCK(redirect_lock);
465static struct file *redirect;
466
467/**
468 * tty_wakeup - request more data
469 * @tty: terminal
470 *
471 * Internal and external helper for wakeups of tty. This function
472 * informs the line discipline if present that the driver is ready
473 * to receive more output data.
474 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476void tty_wakeup(struct tty_struct *tty)
477{
478 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
481 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800482 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100483 if (ld->ops->write_wakeup)
484 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 tty_ldisc_deref(ld);
486 }
487 }
488 wake_up_interruptible(&tty->write_wait);
489}
490
491EXPORT_SYMBOL_GPL(tty_wakeup);
492
493/**
494 * tty_ldisc_flush - flush line discipline queue
495 * @tty: tty
496 *
497 * Flush the line discipline queue (if any) for this tty. If there
498 * is no line discipline active this is a no-op.
499 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501void tty_ldisc_flush(struct tty_struct *tty)
502{
503 struct tty_ldisc *ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800504 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100505 if (ld->ops->flush_buffer)
506 ld->ops->flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 tty_ldisc_deref(ld);
508 }
Paul Fulghumc5c34d42007-05-12 10:36:55 -0700509 tty_buffer_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512EXPORT_SYMBOL_GPL(tty_ldisc_flush);
Alan Coxedc6afc2006-12-08 02:38:44 -0800513
514/**
515 * tty_reset_termios - reset terminal state
516 * @tty: tty to reset
517 *
518 * Restore a terminal to the driver default state
519 */
520
521static void tty_reset_termios(struct tty_struct *tty)
522{
523 mutex_lock(&tty->termios_mutex);
524 *tty->termios = tty->driver->init_termios;
525 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
526 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
527 mutex_unlock(&tty->termios_mutex);
528}
Alan Cox37bdfb02008-02-08 04:18:47 -0800529
Alan Coxaf9b8972006-08-27 01:24:01 -0700530/**
531 * do_tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000532 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700533 *
Alan Cox1bad8792008-07-22 23:38:04 +0100534 * This can be called by the "eventd" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700535 * synchronous but doesn't hold any locks, so we need to make sure we
536 * have the appropriate locks for what we're doing.
537 *
538 * The hangup event clears any pending redirections onto the hung up
539 * device. It ensures future writes will error and it does the needed
540 * line discipline hangup and signal delivery. The tty object itself
541 * remains intact.
542 *
543 * Locking:
544 * BKL
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800545 * redirect lock for undoing redirection
546 * file list lock for manipulating list of ttys
547 * tty_ldisc_lock from called functions
548 * termios_mutex resetting termios data
549 * tasklist_lock to walk task list for hangup event
550 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
David Howells65f27f32006-11-22 14:55:48 +0000552static void do_tty_hangup(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
David Howells65f27f32006-11-22 14:55:48 +0000554 struct tty_struct *tty =
555 container_of(work, struct tty_struct, hangup_work);
Alan Cox37bdfb02008-02-08 04:18:47 -0800556 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct file *filp, *f = NULL;
558 struct task_struct *p;
559 struct tty_ldisc *ld;
560 int closecount = 0, n;
Alan Cox47f86832008-04-30 00:53:30 -0700561 unsigned long flags;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100562 int refs = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 if (!tty)
565 return;
566
567 /* inuse_filps is protected by the single kernel lock */
568 lock_kernel();
569
570 spin_lock(&redirect_lock);
571 if (redirect && redirect->private_data == tty) {
572 f = redirect;
573 redirect = NULL;
574 }
575 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 check_tty_count(tty, "do_tty_hangup");
578 file_list_lock();
579 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Eric Dumazet2f512012005-10-30 15:02:16 -0800580 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (filp->f_op->write == redirected_tty_write)
582 cons_filp = filp;
583 if (filp->f_op->write != tty_write)
584 continue;
585 closecount++;
586 tty_fasync(-1, filp, 0); /* can't block */
587 filp->f_op = &hung_up_tty_fops;
588 }
589 file_list_unlock();
Alan Cox37bdfb02008-02-08 04:18:47 -0800590 /*
591 * FIXME! What are the locking issues here? This may me overdoing
592 * things... This question is especially important now that we've
593 * removed the irqlock.
594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800596 if (ld != NULL) {
597 /* We may have no line discipline at this point */
Alan Coxa352def2008-07-16 21:53:12 +0100598 if (ld->ops->flush_buffer)
599 ld->ops->flush_buffer(tty);
Alan Coxf34d7a52008-04-30 00:54:13 -0700600 tty_driver_flush_buffer(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
Alan Coxa352def2008-07-16 21:53:12 +0100602 ld->ops->write_wakeup)
603 ld->ops->write_wakeup(tty);
604 if (ld->ops->hangup)
605 ld->ops->hangup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Alan Cox37bdfb02008-02-08 04:18:47 -0800607 /*
608 * FIXME: Once we trust the LDISC code better we can wait here for
609 * ldisc completion and fix the driver call race
610 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 wake_up_interruptible(&tty->write_wait);
612 wake_up_interruptible(&tty->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
614 * Shutdown the current line discipline, and reset it to
615 * N_TTY.
616 */
617 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
Alan Coxedc6afc2006-12-08 02:38:44 -0800618 tty_reset_termios(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 /* Defer ldisc switch */
620 /* tty_deferred_ldisc_switch(N_TTY);
Alan Cox37bdfb02008-02-08 04:18:47 -0800621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 This should get done automatically when the port closes and
623 tty_release is called */
Alan Cox37bdfb02008-02-08 04:18:47 -0800624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800626 if (tty->session) {
627 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800628 spin_lock_irq(&p->sighand->siglock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100629 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 p->signal->tty = NULL;
Alan Cox9c9f4de2008-10-13 10:37:26 +0100631 /* We defer the dereferences outside fo
632 the tasklist lock */
633 refs++;
634 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800635 if (!p->signal->leader) {
636 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 continue;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800638 }
639 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
640 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800641 put_pid(p->signal->tty_old_pgrp); /* A noop */
Alan Cox47f86832008-04-30 00:53:30 -0700642 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800643 if (tty->pgrp)
644 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Alan Cox47f86832008-04-30 00:53:30 -0700645 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800646 spin_unlock_irq(&p->sighand->siglock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800647 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 read_unlock(&tasklist_lock);
650
Alan Cox47f86832008-04-30 00:53:30 -0700651 spin_lock_irqsave(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 tty->flags = 0;
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600653 put_pid(tty->session);
654 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800655 tty->session = NULL;
656 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tty->ctrl_status = 0;
Alan Cox47f86832008-04-30 00:53:30 -0700658 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
659
Alan Cox9c9f4de2008-10-13 10:37:26 +0100660 /* Account for the p->signal references we killed */
661 while (refs--)
662 tty_kref_put(tty);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800665 * If one of the devices matches a console pointer, we
666 * cannot just call hangup() because that will cause
667 * tty->count and state->count to go out of sync.
668 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 */
670 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700671 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700673 tty->ops->close(tty, cons_filp);
674 } else if (tty->ops->hangup)
675 (tty->ops->hangup)(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800676 /*
677 * We don't want to have driver/ldisc interactions beyond
678 * the ones we did here. The driver layer expects no
679 * calls after ->hangup() from the ldisc side. However we
680 * can't yet guarantee all that.
681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 set_bit(TTY_HUPPED, &tty->flags);
683 if (ld) {
684 tty_ldisc_enable(tty);
685 tty_ldisc_deref(ld);
686 }
687 unlock_kernel();
688 if (f)
689 fput(f);
690}
691
Alan Coxaf9b8972006-08-27 01:24:01 -0700692/**
693 * tty_hangup - trigger a hangup event
694 * @tty: tty to hangup
695 *
696 * A carrier loss (virtual or otherwise) has occurred on this like
697 * schedule a hangup sequence to run after this event.
698 */
699
Alan Cox37bdfb02008-02-08 04:18:47 -0800700void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702#ifdef TTY_DEBUG_HANGUP
703 char buf[64];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
705#endif
706 schedule_work(&tty->hangup_work);
707}
708
709EXPORT_SYMBOL(tty_hangup);
710
Alan Coxaf9b8972006-08-27 01:24:01 -0700711/**
712 * tty_vhangup - process vhangup
713 * @tty: tty to hangup
714 *
715 * The user has asked via system call for the terminal to be hung up.
716 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200717 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700718 */
719
Alan Cox37bdfb02008-02-08 04:18:47 -0800720void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722#ifdef TTY_DEBUG_HANGUP
723 char buf[64];
724
725 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
726#endif
David Howells65f27f32006-11-22 14:55:48 +0000727 do_tty_hangup(&tty->hangup_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
Alan Cox37bdfb02008-02-08 04:18:47 -0800729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730EXPORT_SYMBOL(tty_vhangup);
731
Alan Coxaf9b8972006-08-27 01:24:01 -0700732/**
Alan Cox2cb59982008-10-13 10:40:30 +0100733 * tty_vhangup_self - process vhangup for own ctty
734 *
735 * Perform a vhangup on the current controlling tty
736 */
737
738void tty_vhangup_self(void)
739{
740 struct tty_struct *tty;
741
Alan Cox2cb59982008-10-13 10:40:30 +0100742 tty = get_current_tty();
743 if (tty) {
744 tty_vhangup(tty);
745 tty_kref_put(tty);
746 }
Alan Cox2cb59982008-10-13 10:40:30 +0100747}
748
749/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700750 * tty_hung_up_p - was tty hung up
751 * @filp: file pointer of tty
752 *
753 * Return true if the tty has been subject to a vhangup or a carrier
754 * loss
755 */
756
Alan Cox37bdfb02008-02-08 04:18:47 -0800757int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 return (filp->f_op == &hung_up_tty_fops);
760}
761
762EXPORT_SYMBOL(tty_hung_up_p);
763
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800764static void session_clear_tty(struct pid *session)
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800765{
766 struct task_struct *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800767 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800768 proc_clear_tty(p);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800769 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800770}
771
Alan Coxaf9b8972006-08-27 01:24:01 -0700772/**
773 * disassociate_ctty - disconnect controlling tty
774 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700776 * This function is typically called only by the session leader, when
777 * it wants to disassociate itself from its controlling tty.
778 *
779 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
781 * (2) Clears the tty from being controlling the session
782 * (3) Clears the controlling tty for all processes in the
783 * session group.
784 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700785 * The argument on_exit is set to 1 if called when a process is
786 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
787 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800788 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -0700789 * BKL is taken for hysterical raisins
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800790 * tty_mutex is taken to protect tty
791 * ->siglock is taken to protect ->signal/->sighand
792 * tasklist_lock is taken to walk process list for sessions
793 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796void disassociate_ctty(int on_exit)
797{
798 struct tty_struct *tty;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800799 struct pid *tty_pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800802 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (tty) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800804 tty_pgrp = get_pid(tty->pgrp);
Alan Cox452a00d2008-10-13 10:39:13 +0100805 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
807 tty_vhangup(tty);
Alan Cox04f378b2008-04-30 00:53:29 -0700808 unlock_kernel();
Alan Cox452a00d2008-10-13 10:39:13 +0100809 tty_kref_put(tty);
Eric W. Biederman680a9672007-02-12 00:52:52 -0800810 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800811 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800812 spin_lock_irq(&current->sighand->siglock);
813 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800814 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800815 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800816 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800817 kill_pgrp(old_pgrp, SIGHUP, on_exit);
818 kill_pgrp(old_pgrp, SIGCONT, on_exit);
819 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return;
822 }
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800823 if (tty_pgrp) {
824 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!on_exit)
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800826 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
827 put_pid(tty_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800830 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700831 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800832 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800833 spin_unlock_irq(&current->sighand->siglock);
834
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800835 tty = get_current_tty();
836 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700837 unsigned long flags;
838 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800839 put_pid(tty->session);
840 put_pid(tty->pgrp);
841 tty->session = NULL;
842 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700843 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100844 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800845 } else {
846#ifdef TTY_DEBUG_HANGUP
847 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
848 " = NULL", tty);
849#endif
850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Now clear signal->tty under the lock */
853 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800854 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700858/**
859 *
860 * no_tty - Ensure the current process does not have a controlling tty
861 */
862void no_tty(void)
863{
864 struct task_struct *tsk = current;
Alan Cox04f378b2008-04-30 00:53:29 -0700865 lock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700866 if (tsk->signal->leader)
867 disassociate_ctty(0);
Alan Cox04f378b2008-04-30 00:53:29 -0700868 unlock_kernel();
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700869 proc_clear_tty(tsk);
870}
871
Alan Coxaf9b8972006-08-27 01:24:01 -0700872
873/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200874 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700875 * @tty: tty to stop
876 *
877 * Perform flow control to the driver. For PTY/TTY pairs we
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200878 * must also propagate the TIOCKPKT status. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700879 * on an already stopped device and will not re-call the driver
880 * method.
881 *
882 * This functionality is used by both the line disciplines for
883 * halting incoming flow and by the driver. It may therefore be
884 * called from any context, may be under the tty atomic_write_lock
885 * but not always.
886 *
887 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700888 * Uses the tty control lock internally
Alan Coxaf9b8972006-08-27 01:24:01 -0700889 */
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891void stop_tty(struct tty_struct *tty)
892{
Alan Cox04f378b2008-04-30 00:53:29 -0700893 unsigned long flags;
894 spin_lock_irqsave(&tty->ctrl_lock, flags);
895 if (tty->stopped) {
896 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 tty->stopped = 1;
900 if (tty->link && tty->link->packet) {
901 tty->ctrl_status &= ~TIOCPKT_START;
902 tty->ctrl_status |= TIOCPKT_STOP;
903 wake_up_interruptible(&tty->link->read_wait);
904 }
Alan Cox04f378b2008-04-30 00:53:29 -0700905 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700906 if (tty->ops->stop)
907 (tty->ops->stop)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910EXPORT_SYMBOL(stop_tty);
911
Alan Coxaf9b8972006-08-27 01:24:01 -0700912/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200913 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700914 * @tty: tty to start
915 *
916 * Start a tty that has been stopped if at all possible. Perform
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200917 * any necessary wakeups and propagate the TIOCPKT status. If this
Alan Coxaf9b8972006-08-27 01:24:01 -0700918 * is the tty was previous stopped and is being started then the
919 * driver start method is invoked and the line discipline woken.
920 *
921 * Locking:
Alan Cox04f378b2008-04-30 00:53:29 -0700922 * ctrl_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700923 */
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925void start_tty(struct tty_struct *tty)
926{
Alan Cox04f378b2008-04-30 00:53:29 -0700927 unsigned long flags;
928 spin_lock_irqsave(&tty->ctrl_lock, flags);
929 if (!tty->stopped || tty->flow_stopped) {
930 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return;
Alan Cox04f378b2008-04-30 00:53:29 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 tty->stopped = 0;
934 if (tty->link && tty->link->packet) {
935 tty->ctrl_status &= ~TIOCPKT_STOP;
936 tty->ctrl_status |= TIOCPKT_START;
937 wake_up_interruptible(&tty->link->read_wait);
938 }
Alan Cox04f378b2008-04-30 00:53:29 -0700939 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700940 if (tty->ops->start)
941 (tty->ops->start)(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* If we have a running line discipline it may need kicking */
943 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946EXPORT_SYMBOL(start_tty);
947
Alan Coxaf9b8972006-08-27 01:24:01 -0700948/**
949 * tty_read - read method for tty device files
950 * @file: pointer to tty file
951 * @buf: user buffer
952 * @count: size of user buffer
953 * @ppos: unused
954 *
955 * Perform the read system call function on this terminal device. Checks
956 * for hung up devices before calling the line discipline method.
957 *
958 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -0700959 * Locks the line discipline internally while needed. Multiple
960 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -0700961 */
962
Alan Cox37bdfb02008-02-08 04:18:47 -0800963static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 loff_t *ppos)
965{
966 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -0800967 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct inode *inode;
969 struct tty_ldisc *ld;
970
971 tty = (struct tty_struct *)file->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -0800972 inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (tty_paranoia_check(tty, inode, "tty_read"))
974 return -EIO;
975 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
976 return -EIO;
977
978 /* We want to wait for the line discipline to sort out in this
979 situation */
980 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +0100981 if (ld->ops->read)
982 i = (ld->ops->read)(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 else
984 i = -EIO;
985 tty_ldisc_deref(ld);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (i > 0)
987 inode->i_atime = current_fs_time(inode->i_sb);
988 return i;
989}
990
Alan Cox9c1729d2007-07-15 23:39:43 -0700991void tty_write_unlock(struct tty_struct *tty)
992{
993 mutex_unlock(&tty->atomic_write_lock);
994 wake_up_interruptible(&tty->write_wait);
995}
996
997int tty_write_lock(struct tty_struct *tty, int ndelay)
998{
999 if (!mutex_trylock(&tty->atomic_write_lock)) {
1000 if (ndelay)
1001 return -EAGAIN;
1002 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1003 return -ERESTARTSYS;
1004 }
1005 return 0;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * Split writes up in sane blocksizes to avoid
1010 * denial-of-service type attacks
1011 */
1012static inline ssize_t do_tty_write(
1013 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1014 struct tty_struct *tty,
1015 struct file *file,
1016 const char __user *buf,
1017 size_t count)
1018{
Alan Cox9c1729d2007-07-15 23:39:43 -07001019 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001021
Alan Cox9c1729d2007-07-15 23:39:43 -07001022 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1023 if (ret < 0)
1024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /*
1027 * We chunk up writes into a temporary buffer. This
1028 * simplifies low-level drivers immensely, since they
1029 * don't have locking issues and user mode accesses.
1030 *
1031 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1032 * big chunk-size..
1033 *
1034 * The default chunk-size is 2kB, because the NTTY
1035 * layer has problems with bigger chunks. It will
1036 * claim to be able to handle more characters than
1037 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001038 *
1039 * FIXME: This can probably go away now except that 64K chunks
1040 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042 chunk = 2048;
1043 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1044 chunk = 65536;
1045 if (count < chunk)
1046 chunk = count;
1047
Ingo Molnar70522e12006-03-23 03:00:31 -08001048 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 if (tty->write_cnt < chunk) {
1050 unsigned char *buf;
1051
1052 if (chunk < 1024)
1053 chunk = 1024;
1054
1055 buf = kmalloc(chunk, GFP_KERNEL);
1056 if (!buf) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001057 ret = -ENOMEM;
1058 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 kfree(tty->write_buf);
1061 tty->write_cnt = chunk;
1062 tty->write_buf = buf;
1063 }
1064
1065 /* Do the write .. */
1066 for (;;) {
1067 size_t size = count;
1068 if (size > chunk)
1069 size = chunk;
1070 ret = -EFAULT;
1071 if (copy_from_user(tty->write_buf, buf, size))
1072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (ret <= 0)
1075 break;
1076 written += ret;
1077 buf += ret;
1078 count -= ret;
1079 if (!count)
1080 break;
1081 ret = -ERESTARTSYS;
1082 if (signal_pending(current))
1083 break;
1084 cond_resched();
1085 }
1086 if (written) {
Josef Sipeka7113a92006-12-08 02:36:55 -08001087 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 inode->i_mtime = current_fs_time(inode->i_sb);
1089 ret = written;
1090 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001091out:
1092 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return ret;
1094}
1095
Alan Cox95f9bfc2008-10-13 10:39:23 +01001096/**
1097 * tty_write_message - write a message to a certain tty, not just the console.
1098 * @tty: the destination tty_struct
1099 * @msg: the message to write
1100 *
1101 * This is used for messages that need to be redirected to a specific tty.
1102 * We don't put it into the syslog queue right now maybe in the future if
1103 * really needed.
1104 *
1105 * We must still hold the BKL and test the CLOSING flag for the moment.
1106 */
1107
1108void tty_write_message(struct tty_struct *tty, char *msg)
1109{
1110 lock_kernel();
1111 if (tty) {
1112 mutex_lock(&tty->atomic_write_lock);
1113 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags))
1114 tty->ops->write(tty, msg, strlen(msg));
1115 tty_write_unlock(tty);
1116 }
1117 unlock_kernel();
1118 return;
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Alan Coxaf9b8972006-08-27 01:24:01 -07001122/**
1123 * tty_write - write method for tty device file
1124 * @file: tty file pointer
1125 * @buf: user data to write
1126 * @count: bytes to write
1127 * @ppos: unused
1128 *
1129 * Write data to a tty device via the line discipline.
1130 *
1131 * Locking:
1132 * Locks the line discipline as required
1133 * Writes to the tty driver are serialized by the atomic_write_lock
1134 * and are then processed in chunks to the device. The line discipline
1135 * write method will not be involked in parallel for each device
1136 * The line discipline write method is called under the big
1137 * kernel lock for historical reasons. New code should not rely on this.
1138 */
1139
Alan Cox37bdfb02008-02-08 04:18:47 -08001140static ssize_t tty_write(struct file *file, const char __user *buf,
1141 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Alan Cox37bdfb02008-02-08 04:18:47 -08001143 struct tty_struct *tty;
Josef Sipeka7113a92006-12-08 02:36:55 -08001144 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ssize_t ret;
1146 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 tty = (struct tty_struct *)file->private_data;
1149 if (tty_paranoia_check(tty, inode, "tty_write"))
1150 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001151 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001152 (test_bit(TTY_IO_ERROR, &tty->flags)))
1153 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001154 /* Short term debug to catch buggy drivers */
1155 if (tty->ops->write_room == NULL)
1156 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1157 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001158 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001159 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ret = -EIO;
1161 else
Alan Coxa352def2008-07-16 21:53:12 +01001162 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 tty_ldisc_deref(ld);
1164 return ret;
1165}
1166
Alan Cox37bdfb02008-02-08 04:18:47 -08001167ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1168 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
1170 struct file *p = NULL;
1171
1172 spin_lock(&redirect_lock);
1173 if (redirect) {
1174 get_file(redirect);
1175 p = redirect;
1176 }
1177 spin_unlock(&redirect_lock);
1178
1179 if (p) {
1180 ssize_t res;
1181 res = vfs_write(p, buf, count, &p->f_pos);
1182 fput(p);
1183 return res;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return tty_write(file, buf, count, ppos);
1186}
1187
1188static char ptychar[] = "pqrstuvwxyzabcde";
1189
Alan Coxaf9b8972006-08-27 01:24:01 -07001190/**
1191 * pty_line_name - generate name for a pty
1192 * @driver: the tty driver in use
1193 * @index: the minor number
1194 * @p: output buffer of at least 6 bytes
1195 *
1196 * Generate a name from a driver reference and write it to the output
1197 * buffer.
1198 *
1199 * Locking: None
1200 */
1201static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
1203 int i = index + driver->name_base;
1204 /* ->name is initialized to "ttyp", but "tty" is expected */
1205 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001206 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1207 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208}
1209
Alan Coxaf9b8972006-08-27 01:24:01 -07001210/**
1211 * pty_line_name - generate name for a tty
1212 * @driver: the tty driver in use
1213 * @index: the minor number
1214 * @p: output buffer of at least 7 bytes
1215 *
1216 * Generate a name from a driver reference and write it to the output
1217 * buffer.
1218 *
1219 * Locking: None
1220 */
1221static void tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1224}
1225
Alan Coxaf9b8972006-08-27 01:24:01 -07001226/**
1227 * init_dev - initialise a tty device
1228 * @driver: tty driver we are opening a device on
1229 * @idx: device index
1230 * @tty: returned tty structure
1231 *
1232 * Prepare a tty device. This may not be a "new" clean device but
1233 * could also be an active device. The pty drivers require special
1234 * handling because of this.
1235 *
1236 * Locking:
1237 * The function is called under the tty_mutex, which
1238 * protects us from the tty struct or driver itself going away.
1239 *
1240 * On exit the tty device has the line discipline attached and
1241 * a reference count of 1. If a pair was created for pty/tty use
1242 * and the other was a pty master then it too has a reference count of 1.
1243 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001245 * failed open. The new code protects the open with a mutex, so it's
1246 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * relaxed for the (most common) case of reopening a tty.
1248 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250static int init_dev(struct tty_driver *driver, int idx,
1251 struct tty_struct **ret_tty)
1252{
1253 struct tty_struct *tty, *o_tty;
Alan Coxedc6afc2006-12-08 02:38:44 -08001254 struct ktermios *tp, **tp_loc, *o_tp, **o_tp_loc;
1255 struct ktermios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
Alan Coxaf9b8972006-08-27 01:24:01 -07001256 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* check whether we're reopening an existing tty */
1259 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1260 tty = devpts_get_tty(idx);
Aristeu Sergio Rozanski Filho5a39e8c2007-02-28 20:13:53 -08001261 /*
1262 * If we don't have a tty here on a slave open, it's because
1263 * the master already started the close process and there's
1264 * no relation between devpts file and tty anymore.
1265 */
1266 if (!tty && driver->subtype == PTY_TYPE_SLAVE) {
1267 retval = -EIO;
1268 goto end_init;
1269 }
1270 /*
1271 * It's safe from now on because init_dev() is called with
1272 * tty_mutex held and release_dev() won't change tty->count
1273 * or tty->flags without having to grab tty_mutex
1274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (tty && driver->subtype == PTY_TYPE_MASTER)
1276 tty = tty->link;
1277 } else {
1278 tty = driver->ttys[idx];
1279 }
1280 if (tty) goto fast_track;
1281
1282 /*
1283 * First time open is complex, especially for PTY devices.
1284 * This code guarantees that either everything succeeds and the
1285 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001286 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * and locked termios may be retained.)
1288 */
1289
1290 if (!try_module_get(driver->owner)) {
1291 retval = -ENODEV;
1292 goto end_init;
1293 }
1294
1295 o_tty = NULL;
1296 tp = o_tp = NULL;
1297 ltp = o_ltp = NULL;
1298
1299 tty = alloc_tty_struct();
Alan Cox37bdfb02008-02-08 04:18:47 -08001300 if (!tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto fail_no_mem;
1302 initialize_tty_struct(tty);
1303 tty->driver = driver;
Alan Coxf34d7a52008-04-30 00:54:13 -07001304 tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 tty->index = idx;
1306 tty_line_name(driver, idx, tty->name);
1307
1308 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1309 tp_loc = &tty->termios;
1310 ltp_loc = &tty->termios_locked;
1311 } else {
1312 tp_loc = &driver->termios[idx];
1313 ltp_loc = &driver->termios_locked[idx];
1314 }
1315
1316 if (!*tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02001317 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!tp)
1319 goto free_mem_out;
1320 *tp = driver->init_termios;
1321 }
1322
1323 if (!*ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07001324 ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (!ltp)
1326 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
1329 if (driver->type == TTY_DRIVER_TYPE_PTY) {
1330 o_tty = alloc_tty_struct();
1331 if (!o_tty)
1332 goto free_mem_out;
Alan Cox6f967f72008-10-13 10:37:36 +01001333 if (!try_module_get(driver->other->owner)) {
1334 /* This cannot in fact currently happen */
1335 free_tty_struct(o_tty);
1336 o_tty = NULL;
1337 goto free_mem_out;
1338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 initialize_tty_struct(o_tty);
1340 o_tty->driver = driver->other;
Alan Coxf34d7a52008-04-30 00:54:13 -07001341 o_tty->ops = driver->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 o_tty->index = idx;
1343 tty_line_name(driver->other, idx, o_tty->name);
1344
1345 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
1346 o_tp_loc = &o_tty->termios;
1347 o_ltp_loc = &o_tty->termios_locked;
1348 } else {
1349 o_tp_loc = &driver->other->termios[idx];
1350 o_ltp_loc = &driver->other->termios_locked[idx];
1351 }
1352
1353 if (!*o_tp_loc) {
Jesper Juhlabcb1ff32007-08-24 02:28:42 +02001354 o_tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (!o_tp)
1356 goto free_mem_out;
1357 *o_tp = driver->other->init_termios;
1358 }
1359
1360 if (!*o_ltp_loc) {
Jean Delvare506eb992007-07-15 23:40:14 -07001361 o_ltp = kzalloc(sizeof(struct ktermios), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (!o_ltp)
1363 goto free_mem_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 /*
1367 * Everything allocated ... set up the o_tty structure.
1368 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001369 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 driver->other->ttys[idx] = o_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (!*o_tp_loc)
1372 *o_tp_loc = o_tp;
1373 if (!*o_ltp_loc)
1374 *o_ltp_loc = o_ltp;
1375 o_tty->termios = *o_tp_loc;
1376 o_tty->termios_locked = *o_ltp_loc;
1377 driver->other->refcount++;
1378 if (driver->subtype == PTY_TYPE_MASTER)
1379 o_tty->count++;
1380
1381 /* Establish the links in both directions */
1382 tty->link = o_tty;
1383 o_tty->link = tty;
1384 }
1385
Alan Cox37bdfb02008-02-08 04:18:47 -08001386 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * All structures have been allocated, so now we install them.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001388 * Failures after this point use release_tty to clean up, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 * there's no need to null out the local pointers.
1390 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001391 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 driver->ttys[idx] = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08001393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 if (!*tp_loc)
1395 *tp_loc = tp;
1396 if (!*ltp_loc)
1397 *ltp_loc = ltp;
1398 tty->termios = *tp_loc;
1399 tty->termios_locked = *ltp_loc;
Alan Coxedc6afc2006-12-08 02:38:44 -08001400 /* Compatibility until drivers always set this */
1401 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1402 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 driver->refcount++;
1404 tty->count++;
1405
Alan Cox37bdfb02008-02-08 04:18:47 -08001406 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001408 * If we fail here just call release_tty to clean up. No need
1409 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411
Alan Cox01e1abb2008-07-22 11:16:55 +01001412 retval = tty_ldisc_setup(tty, o_tty);
1413
1414 if (retval)
1415 goto release_mem_out;
1416 goto success;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /*
1419 * This fast open can be used if the tty is already open.
1420 * No memory is allocated, and the only failures are from
1421 * attempting to open a closing tty or attempting multiple
1422 * opens on a pty master.
1423 */
1424fast_track:
1425 if (test_bit(TTY_CLOSING, &tty->flags)) {
1426 retval = -EIO;
1427 goto end_init;
1428 }
1429 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1430 driver->subtype == PTY_TYPE_MASTER) {
1431 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001432 * special case for PTY masters: only one open permitted,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * and the slave side open count is incremented as well.
1434 */
1435 if (tty->count) {
1436 retval = -EIO;
1437 goto end_init;
1438 }
1439 tty->link->count++;
1440 }
1441 tty->count++;
1442 tty->driver = driver; /* N.B. why do this every time?? */
1443
1444 /* FIXME */
Alan Cox37bdfb02008-02-08 04:18:47 -08001445 if (!test_bit(TTY_LDISC, &tty->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 printk(KERN_ERR "init_dev but no ldisc\n");
1447success:
1448 *ret_tty = tty;
Alan Cox37bdfb02008-02-08 04:18:47 -08001449
Ingo Molnar70522e12006-03-23 03:00:31 -08001450 /* All paths come through here to release the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451end_init:
1452 return retval;
1453
1454 /* Release locally allocated memory ... nothing placed in slots */
1455free_mem_out:
Jesper Juhl735d5662005-11-07 01:01:29 -08001456 kfree(o_tp);
Alan Cox6f967f72008-10-13 10:37:36 +01001457 if (o_tty) {
1458 module_put(o_tty->driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 free_tty_struct(o_tty);
Alan Cox6f967f72008-10-13 10:37:36 +01001460 }
Jesper Juhl735d5662005-11-07 01:01:29 -08001461 kfree(ltp);
1462 kfree(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 free_tty_struct(tty);
1464
1465fail_no_mem:
1466 module_put(driver->owner);
1467 retval = -ENOMEM;
1468 goto end_init;
1469
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001470 /* call the tty release_tty routine to clean out this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471release_mem_out:
Akinobu Mita40509142006-09-29 02:01:27 -07001472 if (printk_ratelimit())
1473 printk(KERN_INFO "init_dev: ldisc open failed, "
1474 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001475 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 goto end_init;
1477}
1478
Alan Coxaf9b8972006-08-27 01:24:01 -07001479/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001480 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001481 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001482 *
1483 * Releases memory associated with a tty structure, and clears out the
1484 * driver table slots. This function is called when a device is no longer
1485 * in use. It also gets called when setup of a device fails.
1486 *
1487 * Locking:
1488 * tty_mutex - sometimes only
1489 * takes the file list lock internally when working on the list
1490 * of ttys that the driver keeps.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 */
Alan Cox9c9f4de2008-10-13 10:37:26 +01001492static void release_one_tty(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001494 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Alan Cox6f967f72008-10-13 10:37:36 +01001495 struct tty_driver *driver = tty->driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001497 struct ktermios *tp;
Alan Cox9c9f4de2008-10-13 10:37:26 +01001498 int idx = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (!devpts)
1501 tty->driver->ttys[idx] = NULL;
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
Alan Cox9c9f4de2008-10-13 10:37:26 +01001504 /* FIXME: Locking on ->termios array */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 tp = tty->termios;
1506 if (!devpts)
1507 tty->driver->termios[idx] = NULL;
1508 kfree(tp);
1509
1510 tp = tty->termios_locked;
1511 if (!devpts)
1512 tty->driver->termios_locked[idx] = NULL;
1513 kfree(tp);
1514 }
1515
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 tty->magic = 0;
Alan Cox9c9f4de2008-10-13 10:37:26 +01001518 /* FIXME: locking on tty->driver->refcount */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 tty->driver->refcount--;
Alan Cox6f967f72008-10-13 10:37:36 +01001520 module_put(driver->owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 file_list_lock();
1523 list_del_init(&tty->tty_files);
1524 file_list_unlock();
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 free_tty_struct(tty);
1527}
1528
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001529/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001530 * tty_kref_put - release a tty kref
1531 * @tty: tty device
1532 *
1533 * Release a reference to a tty device and if need be let the kref
1534 * layer destruct the object for us
1535 */
1536
1537void tty_kref_put(struct tty_struct *tty)
1538{
1539 if (tty)
1540 kref_put(&tty->kref, release_one_tty);
1541}
1542EXPORT_SYMBOL(tty_kref_put);
1543
1544/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001545 * release_tty - release tty structure memory
1546 *
1547 * Release both @tty and a possible linked partner (think pty pair),
1548 * and decrement the refcount of the backing module.
1549 *
1550 * Locking:
1551 * tty_mutex - sometimes only
1552 * takes the file list lock internally when working on the list
1553 * of ttys that the driver keeps.
1554 * FIXME: should we require tty_mutex is held here ??
Alan Cox9c9f4de2008-10-13 10:37:26 +01001555 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001556 */
1557static void release_tty(struct tty_struct *tty, int idx)
1558{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001559 /* This should always be true but check for the moment */
1560 WARN_ON(tty->index != idx);
1561
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001562 if (tty->link)
Alan Cox9c9f4de2008-10-13 10:37:26 +01001563 tty_kref_put(tty->link);
1564 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567/*
1568 * Even releasing the tty structures is a tricky business.. We have
1569 * to be very careful that the structures are all released at the
1570 * same time, as interrupts might otherwise get the wrong pointers.
1571 *
1572 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1573 * lead to double frees or releasing memory still in use.
1574 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001575static void release_dev(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
1577 struct tty_struct *tty, *o_tty;
1578 int pty_master, tty_closing, o_tty_closing, do_sleep;
Paul Fulghum14a62832006-04-10 22:54:19 -07001579 int devpts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int idx;
1581 char buf[64];
Alan Cox37bdfb02008-02-08 04:18:47 -08001582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 tty = (struct tty_struct *)filp->private_data;
Alan Cox37bdfb02008-02-08 04:18:47 -08001584 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode,
1585 "release_dev"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 return;
1587
1588 check_tty_count(tty, "release_dev");
1589
1590 tty_fasync(-1, filp, 0);
1591
1592 idx = tty->index;
1593 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1594 tty->driver->subtype == PTY_TYPE_MASTER);
1595 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 o_tty = tty->link;
1597
1598#ifdef TTY_PARANOIA_CHECK
1599 if (idx < 0 || idx >= tty->driver->num) {
1600 printk(KERN_DEBUG "release_dev: bad idx when trying to "
1601 "free (%s)\n", tty->name);
1602 return;
1603 }
1604 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1605 if (tty != tty->driver->ttys[idx]) {
1606 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
1607 "for (%s)\n", idx, tty->name);
1608 return;
1609 }
1610 if (tty->termios != tty->driver->termios[idx]) {
1611 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
1612 "for (%s)\n",
1613 idx, tty->name);
1614 return;
1615 }
1616 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
1617 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
1618 "termios_locked for (%s)\n",
1619 idx, tty->name);
1620 return;
1621 }
1622 }
1623#endif
1624
1625#ifdef TTY_DEBUG_HANGUP
1626 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
1627 tty_name(tty, buf), tty->count);
1628#endif
1629
1630#ifdef TTY_PARANOIA_CHECK
1631 if (tty->driver->other &&
1632 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1633 if (o_tty != tty->driver->other->ttys[idx]) {
1634 printk(KERN_DEBUG "release_dev: other->table[%d] "
1635 "not o_tty for (%s)\n",
1636 idx, tty->name);
1637 return;
1638 }
1639 if (o_tty->termios != tty->driver->other->termios[idx]) {
1640 printk(KERN_DEBUG "release_dev: other->termios[%d] "
1641 "not o_termios for (%s)\n",
1642 idx, tty->name);
1643 return;
1644 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001645 if (o_tty->termios_locked !=
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 tty->driver->other->termios_locked[idx]) {
1647 printk(KERN_DEBUG "release_dev: other->termios_locked["
1648 "%d] not o_termios_locked for (%s)\n",
1649 idx, tty->name);
1650 return;
1651 }
1652 if (o_tty->link != tty) {
1653 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
1654 return;
1655 }
1656 }
1657#endif
Alan Coxf34d7a52008-04-30 00:54:13 -07001658 if (tty->ops->close)
1659 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /*
1662 * Sanity check: if tty->count is going to zero, there shouldn't be
1663 * any waiters on tty->read_wait or tty->write_wait. We test the
1664 * wait queues and kick everyone out _before_ actually starting to
1665 * close. This ensures that we won't block while releasing the tty
1666 * structure.
1667 *
1668 * The test for the o_tty closing is necessary, since the master and
1669 * slave sides may close in any order. If the slave side closes out
1670 * first, its count will be one, since the master side holds an open.
1671 * Thus this test wouldn't be triggered at the time the slave closes,
1672 * so we do it now.
1673 *
1674 * Note that it's possible for the tty to be opened again while we're
1675 * flushing out waiters. By recalculating the closing flags before
1676 * each iteration we avoid any problems.
1677 */
1678 while (1) {
1679 /* Guard against races with tty->count changes elsewhere and
1680 opens on /dev/tty */
Alan Cox37bdfb02008-02-08 04:18:47 -08001681
Ingo Molnar70522e12006-03-23 03:00:31 -08001682 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 tty_closing = tty->count <= 1;
1684 o_tty_closing = o_tty &&
1685 (o_tty->count <= (pty_master ? 1 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 do_sleep = 0;
1687
1688 if (tty_closing) {
1689 if (waitqueue_active(&tty->read_wait)) {
1690 wake_up(&tty->read_wait);
1691 do_sleep++;
1692 }
1693 if (waitqueue_active(&tty->write_wait)) {
1694 wake_up(&tty->write_wait);
1695 do_sleep++;
1696 }
1697 }
1698 if (o_tty_closing) {
1699 if (waitqueue_active(&o_tty->read_wait)) {
1700 wake_up(&o_tty->read_wait);
1701 do_sleep++;
1702 }
1703 if (waitqueue_active(&o_tty->write_wait)) {
1704 wake_up(&o_tty->write_wait);
1705 do_sleep++;
1706 }
1707 }
1708 if (!do_sleep)
1709 break;
1710
1711 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
1712 "active!\n", tty_name(tty, buf));
Ingo Molnar70522e12006-03-23 03:00:31 -08001713 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 schedule();
Alan Cox37bdfb02008-02-08 04:18:47 -08001715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08001718 * The closing flags are now consistent with the open counts on
1719 * both sides, and we've completed the last operation that could
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * block, so it's safe to proceed with closing.
1721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (pty_master) {
1723 if (--o_tty->count < 0) {
1724 printk(KERN_WARNING "release_dev: bad pty slave count "
1725 "(%d) for %s\n",
1726 o_tty->count, tty_name(o_tty, buf));
1727 o_tty->count = 0;
1728 }
1729 }
1730 if (--tty->count < 0) {
1731 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
1732 tty->count, tty_name(tty, buf));
1733 tty->count = 0;
1734 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
1737 * We've decremented tty->count, so we need to remove this file
1738 * descriptor off the tty->tty_files list; this serves two
1739 * purposes:
1740 * - check_tty_count sees the correct number of file descriptors
1741 * associated with this tty.
1742 * - do_tty_hangup no longer sees this file descriptor as
1743 * something that needs to be handled for hangups.
1744 */
1745 file_kill(filp);
1746 filp->private_data = NULL;
1747
1748 /*
1749 * Perform some housekeeping before deciding whether to return.
1750 *
1751 * Set the TTY_CLOSING flag if this was the last open. In the
1752 * case of a pty we may have to wait around for the other side
1753 * to close, and TTY_CLOSING makes sure we can't be reopened.
1754 */
Alan Cox37bdfb02008-02-08 04:18:47 -08001755 if (tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 set_bit(TTY_CLOSING, &tty->flags);
Alan Cox37bdfb02008-02-08 04:18:47 -08001757 if (o_tty_closing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 set_bit(TTY_CLOSING, &o_tty->flags);
1759
1760 /*
1761 * If _either_ side is closing, make sure there aren't any
1762 * processes that still think tty or o_tty is their controlling
1763 * tty.
1764 */
1765 if (tty_closing || o_tty_closing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001767 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001769 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 read_unlock(&tasklist_lock);
1771 }
1772
Ingo Molnar70522e12006-03-23 03:00:31 -08001773 mutex_unlock(&tty_mutex);
Paul Fulghumda965822006-02-14 13:53:00 -08001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* check whether both sides are closing ... */
1776 if (!tty_closing || (o_tty && !o_tty_closing))
1777 return;
Alan Cox37bdfb02008-02-08 04:18:47 -08001778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779#ifdef TTY_DEBUG_HANGUP
1780 printk(KERN_DEBUG "freeing tty structure...");
1781#endif
1782 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001783 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Alan Cox01e1abb2008-07-22 11:16:55 +01001785 tty_ldisc_release(tty, o_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001787 * The release_tty function takes care of the details of clearing
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * the slots and preserving the termios structure.
1789 */
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001790 release_tty(tty, idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* Make this pty number available for reallocation */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07001793 if (devpts)
1794 devpts_kill_index(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
Alan Coxaf9b8972006-08-27 01:24:01 -07001797/**
1798 * tty_open - open a tty device
1799 * @inode: inode of device file
1800 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001802 * tty_open and tty_release keep up the tty count that contains the
1803 * number of opens done on a tty. We cannot use the inode-count, as
1804 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *
Alan Coxaf9b8972006-08-27 01:24:01 -07001806 * Open-counting is needed for pty masters, as well as for keeping
1807 * track of serial lines: DTR is dropped when the last close happens.
1808 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1809 *
1810 * The termios state of a pty is reset on first open so that
1811 * settings don't persist across reuse.
1812 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001813 * Locking: tty_mutex protects tty, get_tty_driver and init_dev work.
1814 * tty->count should protect the rest.
1815 * ->siglock protects ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001817
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001818static int __tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 struct tty_struct *tty;
1821 int noctty, retval;
1822 struct tty_driver *driver;
1823 int index;
1824 dev_t device = inode->i_rdev;
1825 unsigned short saved_flags = filp->f_flags;
1826
1827 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829retry_open:
1830 noctty = filp->f_flags & O_NOCTTY;
1831 index = -1;
1832 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001833
Ingo Molnar70522e12006-03-23 03:00:31 -08001834 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Alan Cox37bdfb02008-02-08 04:18:47 -08001836 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001837 tty = get_current_tty();
1838 if (!tty) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001839 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return -ENXIO;
1841 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001842 driver = tty->driver;
1843 index = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1845 /* noctty = 1; */
Alan Cox452a00d2008-10-13 10:39:13 +01001846 /* FIXME: Should we take a driver reference ? */
1847 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto got_driver;
1849 }
1850#ifdef CONFIG_VT
Alan Cox37bdfb02008-02-08 04:18:47 -08001851 if (device == MKDEV(TTY_MAJOR, 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 extern struct tty_driver *console_driver;
1853 driver = console_driver;
1854 index = fg_console;
1855 noctty = 1;
1856 goto got_driver;
1857 }
1858#endif
Alan Cox37bdfb02008-02-08 04:18:47 -08001859 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 driver = console_device(&index);
1861 if (driver) {
1862 /* Don't let /dev/console block */
1863 filp->f_flags |= O_NONBLOCK;
1864 noctty = 1;
1865 goto got_driver;
1866 }
Ingo Molnar70522e12006-03-23 03:00:31 -08001867 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return -ENODEV;
1869 }
1870
1871 driver = get_tty_driver(device, &index);
1872 if (!driver) {
Ingo Molnar70522e12006-03-23 03:00:31 -08001873 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return -ENODEV;
1875 }
1876got_driver:
1877 retval = init_dev(driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08001878 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (retval)
1880 return retval;
1881
1882 filp->private_data = tty;
1883 file_move(filp, &tty->tty_files);
1884 check_tty_count(tty, "tty_open");
1885 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1886 tty->driver->subtype == PTY_TYPE_MASTER)
1887 noctty = 1;
1888#ifdef TTY_DEBUG_HANGUP
1889 printk(KERN_DEBUG "opening %s...", tty->name);
1890#endif
1891 if (!retval) {
Alan Coxf34d7a52008-04-30 00:54:13 -07001892 if (tty->ops->open)
1893 retval = tty->ops->open(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 else
1895 retval = -ENODEV;
1896 }
1897 filp->f_flags = saved_flags;
1898
Alan Cox37bdfb02008-02-08 04:18:47 -08001899 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1900 !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 retval = -EBUSY;
1902
1903 if (retval) {
1904#ifdef TTY_DEBUG_HANGUP
1905 printk(KERN_DEBUG "error %d in opening %s...", retval,
1906 tty->name);
1907#endif
1908 release_dev(filp);
1909 if (retval != -ERESTARTSYS)
1910 return retval;
1911 if (signal_pending(current))
1912 return retval;
1913 schedule();
1914 /*
1915 * Need to reset f_op in case a hangup happened.
1916 */
1917 if (filp->f_op == &hung_up_tty_fops)
1918 filp->f_op = &tty_fops;
1919 goto retry_open;
1920 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001921
1922 mutex_lock(&tty_mutex);
1923 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (!noctty &&
1925 current->signal->leader &&
1926 !current->signal->tty &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08001927 tty->session == NULL)
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07001928 __proc_set_tty(current, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001929 spin_unlock_irq(&current->sighand->siglock);
1930 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return 0;
1932}
1933
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001934/* BKL pushdown: scary code avoidance wrapper */
1935static int tty_open(struct inode *inode, struct file *filp)
1936{
1937 int ret;
1938
1939 lock_kernel();
1940 ret = __tty_open(inode, filp);
1941 unlock_kernel();
1942 return ret;
1943}
1944
1945
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947#ifdef CONFIG_UNIX98_PTYS
Alan Coxaf9b8972006-08-27 01:24:01 -07001948/**
1949 * ptmx_open - open a unix 98 pty master
1950 * @inode: inode of device file
1951 * @filp: file pointer to tty
1952 *
1953 * Allocate a unix98 pty master device from the ptmx driver.
1954 *
1955 * Locking: tty_mutex protects theinit_dev work. tty->count should
Alan Cox37bdfb02008-02-08 04:18:47 -08001956 * protect the rest.
Alan Coxaf9b8972006-08-27 01:24:01 -07001957 * allocated_ptys_lock handles the list of free pty numbers
1958 */
1959
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001960static int __ptmx_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 struct tty_struct *tty;
1963 int retval;
1964 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 nonseekable_open(inode, filp);
1967
1968 /* find a device that is not in use. */
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07001969 index = devpts_new_index();
1970 if (index < 0)
1971 return index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Ingo Molnar70522e12006-03-23 03:00:31 -08001973 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 retval = init_dev(ptm_driver, index, &tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08001975 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08001976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (retval)
1978 goto out;
1979
1980 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
1981 filp->private_data = tty;
1982 file_move(filp, &tty->tty_files);
1983
Sukadev Bhattiprolu4f8f9d62008-04-30 00:54:20 -07001984 retval = devpts_pty_new(tty->link);
1985 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 goto out1;
1987
Hiroshi Shimamoto86a96532008-04-30 00:54:20 -07001988 check_tty_count(tty, "ptmx_open");
Alan Coxf34d7a52008-04-30 00:54:13 -07001989 retval = ptm_driver->ops->open(tty, filp);
Miloslav Trmac41126222008-04-18 13:30:14 -07001990 if (!retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return 0;
1992out1:
1993 release_dev(filp);
Paul Fulghum9453a5a2006-04-10 22:54:18 -07001994 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995out:
Sukadev Bhattiprolu718a9162008-04-30 00:54:21 -07001996 devpts_kill_index(index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return retval;
1998}
Jonathan Corbet39d95b92008-05-16 09:10:50 -06001999
2000static int ptmx_open(struct inode *inode, struct file *filp)
2001{
2002 int ret;
2003
2004 lock_kernel();
2005 ret = __ptmx_open(inode, filp);
2006 unlock_kernel();
2007 return ret;
2008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009#endif
2010
Alan Coxaf9b8972006-08-27 01:24:01 -07002011/**
2012 * tty_release - vfs callback for close
2013 * @inode: inode of tty
2014 * @filp: file pointer for handle to tty
2015 *
2016 * Called the last time each file handle is closed that references
2017 * this tty. There may however be several such references.
2018 *
2019 * Locking:
2020 * Takes bkl. See release_dev
2021 */
2022
Alan Cox37bdfb02008-02-08 04:18:47 -08002023static int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
2025 lock_kernel();
2026 release_dev(filp);
2027 unlock_kernel();
2028 return 0;
2029}
2030
Alan Coxaf9b8972006-08-27 01:24:01 -07002031/**
2032 * tty_poll - check tty status
2033 * @filp: file being polled
2034 * @wait: poll wait structures to update
2035 *
2036 * Call the line discipline polling method to obtain the poll
2037 * status of the device.
2038 *
2039 * Locking: locks called line discipline but ldisc poll method
2040 * may be re-entered freely by other callers.
2041 */
2042
Alan Cox37bdfb02008-02-08 04:18:47 -08002043static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Alan Cox37bdfb02008-02-08 04:18:47 -08002045 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 struct tty_ldisc *ld;
2047 int ret = 0;
2048
2049 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08002050 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002054 if (ld->ops->poll)
2055 ret = (ld->ops->poll)(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 tty_ldisc_deref(ld);
2057 return ret;
2058}
2059
Alan Cox37bdfb02008-02-08 04:18:47 -08002060static int tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061{
Alan Cox37bdfb02008-02-08 04:18:47 -08002062 struct tty_struct *tty;
Alan Cox47f86832008-04-30 00:53:30 -07002063 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002064 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002066 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 tty = (struct tty_struct *)filp->private_data;
Josef Sipeka7113a92006-12-08 02:36:55 -08002068 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002069 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 retval = fasync_helper(fd, filp, on, &tty->fasync);
2072 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002076 enum pid_type type;
2077 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (!waitqueue_active(&tty->read_wait))
2079 tty->minimum_to_wake = 1;
Alan Cox47f86832008-04-30 00:53:30 -07002080 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002081 if (tty->pgrp) {
2082 pid = tty->pgrp;
2083 type = PIDTYPE_PGID;
2084 } else {
2085 pid = task_pid(current);
2086 type = PIDTYPE_PID;
2087 }
Alan Cox47f86832008-04-30 00:53:30 -07002088 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002089 retval = __f_setown(filp, pid, type, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (retval)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002091 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 } else {
2093 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
2094 tty->minimum_to_wake = N_TTY_BUF_SIZE;
2095 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002096 retval = 0;
2097out:
2098 unlock_kernel();
2099 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Alan Coxaf9b8972006-08-27 01:24:01 -07002102/**
2103 * tiocsti - fake input character
2104 * @tty: tty to fake input into
2105 * @p: pointer to character
2106 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002107 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002108 * input management.
2109 *
2110 * FIXME: does not honour flow control ??
2111 *
2112 * Locking:
2113 * Called functions take tty_ldisc_lock
2114 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002115 *
2116 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002117 */
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119static int tiocsti(struct tty_struct *tty, char __user *p)
2120{
2121 char ch, mbz = 0;
2122 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2125 return -EPERM;
2126 if (get_user(ch, p))
2127 return -EFAULT;
2128 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002129 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 tty_ldisc_deref(ld);
2131 return 0;
2132}
2133
Alan Coxaf9b8972006-08-27 01:24:01 -07002134/**
2135 * tiocgwinsz - implement window query ioctl
2136 * @tty; tty
2137 * @arg: user buffer for result
2138 *
Alan Cox808a0d32006-09-29 02:00:40 -07002139 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002140 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002141 * Locking: tty->termios_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002142 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002143 */
2144
Alan Cox37bdfb02008-02-08 04:18:47 -08002145static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Alan Cox808a0d32006-09-29 02:00:40 -07002147 int err;
2148
Arjan van de Ven5785c952006-09-29 02:00:43 -07002149 mutex_lock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002150 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Arjan van de Ven5785c952006-09-29 02:00:43 -07002151 mutex_unlock(&tty->termios_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002152
2153 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Alan Coxaf9b8972006-08-27 01:24:01 -07002156/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002157 * tty_do_resize - resize event
2158 * @tty: tty being resized
Alan Coxa152db72008-08-26 19:55:58 +01002159 * @real_tty: real tty (not the same as tty if using a pty/tty pair)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002160 * @rows: rows (character)
2161 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002162 *
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002163 * Update the termios variables and send the neccessary signals to
2164 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002165 */
2166
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002167int tty_do_resize(struct tty_struct *tty, struct tty_struct *real_tty,
2168 struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Alan Cox47f86832008-04-30 00:53:30 -07002170 struct pid *pgrp, *rpgrp;
2171 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Alan Coxa152db72008-08-26 19:55:58 +01002173 /* For a PTY we need to lock the tty side */
2174 mutex_lock(&real_tty->termios_mutex);
Alan Coxf4d2a6c2008-10-13 10:39:01 +01002175 if (!memcmp(ws, &real_tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002176 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002177 /* Get the PID values and reference them so we can
2178 avoid holding the tty ctrl lock while sending signals */
2179 spin_lock_irqsave(&tty->ctrl_lock, flags);
2180 pgrp = get_pid(tty->pgrp);
2181 rpgrp = get_pid(real_tty->pgrp);
2182 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2183
2184 if (pgrp)
2185 kill_pgrp(pgrp, SIGWINCH, 1);
2186 if (rpgrp != pgrp && rpgrp)
2187 kill_pgrp(rpgrp, SIGWINCH, 1);
2188
2189 put_pid(pgrp);
2190 put_pid(rpgrp);
2191
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002192 tty->winsize = *ws;
2193 real_tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002194done:
Alan Coxa152db72008-08-26 19:55:58 +01002195 mutex_unlock(&real_tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return 0;
2197}
2198
Alan Coxaf9b8972006-08-27 01:24:01 -07002199/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002200 * tiocswinsz - implement window size set ioctl
2201 * @tty; tty
2202 * @arg: user buffer for result
2203 *
2204 * Copies the user idea of the window size to the kernel. Traditionally
2205 * this is just advisory information but for the Linux console it
2206 * actually has driver level meaning and triggers a VC resize.
2207 *
2208 * Locking:
2209 * Driver dependant. The default do_resize method takes the
2210 * tty termios mutex and ctrl_lock. The console takes its own lock
2211 * then calls into the default method.
2212 */
2213
2214static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
2215 struct winsize __user *arg)
2216{
2217 struct winsize tmp_ws;
2218 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2219 return -EFAULT;
2220
2221 if (tty->ops->resize)
2222 return tty->ops->resize(tty, real_tty, &tmp_ws);
2223 else
2224 return tty_do_resize(tty, real_tty, &tmp_ws);
2225}
2226
2227/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002228 * tioccons - allow admin to move logical console
2229 * @file: the file to become console
2230 *
2231 * Allow the adminstrator to move the redirected console device
2232 *
2233 * Locking: uses redirect_lock to guard the redirect information
2234 */
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236static int tioccons(struct file *file)
2237{
2238 if (!capable(CAP_SYS_ADMIN))
2239 return -EPERM;
2240 if (file->f_op->write == redirected_tty_write) {
2241 struct file *f;
2242 spin_lock(&redirect_lock);
2243 f = redirect;
2244 redirect = NULL;
2245 spin_unlock(&redirect_lock);
2246 if (f)
2247 fput(f);
2248 return 0;
2249 }
2250 spin_lock(&redirect_lock);
2251 if (redirect) {
2252 spin_unlock(&redirect_lock);
2253 return -EBUSY;
2254 }
2255 get_file(file);
2256 redirect = file;
2257 spin_unlock(&redirect_lock);
2258 return 0;
2259}
2260
Alan Coxaf9b8972006-08-27 01:24:01 -07002261/**
2262 * fionbio - non blocking ioctl
2263 * @file: file to set blocking value
2264 * @p: user parameter
2265 *
2266 * Historical tty interfaces had a blocking control ioctl before
2267 * the generic functionality existed. This piece of history is preserved
2268 * in the expected tty API of posix OS's.
2269 *
2270 * Locking: none, the open fle handle ensures it won't go away.
2271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273static int fionbio(struct file *file, int __user *p)
2274{
2275 int nonblock;
2276
2277 if (get_user(nonblock, p))
2278 return -EFAULT;
2279
Alan Cox04f378b2008-04-30 00:53:29 -07002280 /* file->f_flags is still BKL protected in the fs layer - vomit */
2281 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (nonblock)
2283 file->f_flags |= O_NONBLOCK;
2284 else
2285 file->f_flags &= ~O_NONBLOCK;
Alan Cox04f378b2008-04-30 00:53:29 -07002286 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return 0;
2288}
2289
Alan Coxaf9b8972006-08-27 01:24:01 -07002290/**
2291 * tiocsctty - set controlling tty
2292 * @tty: tty structure
2293 * @arg: user argument
2294 *
2295 * This ioctl is used to manage job control. It permits a session
2296 * leader to set this tty as the controlling tty for the session.
2297 *
2298 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002299 * Takes tty_mutex() to protect tty instance
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002300 * Takes tasklist_lock internally to walk sessions
2301 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002302 */
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static int tiocsctty(struct tty_struct *tty, int arg)
2305{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002306 int ret = 0;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002307 if (current->signal->leader && (task_session(current) == tty->session))
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002308 return ret;
2309
2310 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /*
2312 * The process must be a session leader and
2313 * not have a controlling tty already.
2314 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002315 if (!current->signal->leader || current->signal->tty) {
2316 ret = -EPERM;
2317 goto unlock;
2318 }
2319
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002320 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /*
2322 * This tty is already the controlling
2323 * tty for another session group!
2324 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002325 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /*
2327 * Steal it away
2328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002330 session_clear_tty(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 read_unlock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002332 } else {
2333 ret = -EPERM;
2334 goto unlock;
2335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002337 proc_set_tty(current, tty);
2338unlock:
Alan Cox28298232006-09-29 02:00:58 -07002339 mutex_unlock(&tty_mutex);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Alan Coxaf9b8972006-08-27 01:24:01 -07002343/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002344 * tty_get_pgrp - return a ref counted pgrp pid
2345 * @tty: tty to read
2346 *
2347 * Returns a refcounted instance of the pid struct for the process
2348 * group controlling the tty.
2349 */
2350
2351struct pid *tty_get_pgrp(struct tty_struct *tty)
2352{
2353 unsigned long flags;
2354 struct pid *pgrp;
2355
2356 spin_lock_irqsave(&tty->ctrl_lock, flags);
2357 pgrp = get_pid(tty->pgrp);
2358 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2359
2360 return pgrp;
2361}
2362EXPORT_SYMBOL_GPL(tty_get_pgrp);
2363
2364/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002365 * tiocgpgrp - get process group
2366 * @tty: tty passed by user
2367 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2368 * @p: returned pid
2369 *
2370 * Obtain the process group of the tty. If there is no process group
2371 * return an error.
2372 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002373 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002374 */
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2377{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002378 struct pid *pid;
2379 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 /*
2381 * (tty == real_tty) is a cheap way of
2382 * testing if the tty is NOT a master pty.
2383 */
2384 if (tty == real_tty && current->signal->tty != real_tty)
2385 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002386 pid = tty_get_pgrp(real_tty);
2387 ret = put_user(pid_vnr(pid), p);
2388 put_pid(pid);
2389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390}
2391
Alan Coxaf9b8972006-08-27 01:24:01 -07002392/**
2393 * tiocspgrp - attempt to set process group
2394 * @tty: tty passed by user
2395 * @real_tty: tty side device matching tty passed by user
2396 * @p: pid pointer
2397 *
2398 * Set the process group of the tty to the session passed. Only
2399 * permitted where the tty session is our session.
2400 *
Alan Cox47f86832008-04-30 00:53:30 -07002401 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002402 */
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2405{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002406 struct pid *pgrp;
2407 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 int retval = tty_check_change(real_tty);
Alan Cox47f86832008-04-30 00:53:30 -07002409 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 if (retval == -EIO)
2412 return -ENOTTY;
2413 if (retval)
2414 return retval;
2415 if (!current->signal->tty ||
2416 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002417 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002419 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002421 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002423 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002424 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002425 retval = -ESRCH;
2426 if (!pgrp)
2427 goto out_unlock;
2428 retval = -EPERM;
2429 if (session_of_pgrp(pgrp) != task_session(current))
2430 goto out_unlock;
2431 retval = 0;
Alan Cox47f86832008-04-30 00:53:30 -07002432 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002433 put_pid(real_tty->pgrp);
2434 real_tty->pgrp = get_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002435 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002436out_unlock:
2437 rcu_read_unlock();
2438 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Alan Coxaf9b8972006-08-27 01:24:01 -07002441/**
2442 * tiocgsid - get session id
2443 * @tty: tty passed by user
2444 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2445 * @p: pointer to returned session id
2446 *
2447 * Obtain the session id of the tty. If there is no session
2448 * return an error.
2449 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002450 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002451 */
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2454{
2455 /*
2456 * (tty == real_tty) is a cheap way of
2457 * testing if the tty is NOT a master pty.
2458 */
2459 if (tty == real_tty && current->signal->tty != real_tty)
2460 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002461 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002463 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Alan Coxaf9b8972006-08-27 01:24:01 -07002466/**
2467 * tiocsetd - set line discipline
2468 * @tty: tty device
2469 * @p: pointer to user data
2470 *
2471 * Set the line discipline according to user request.
2472 *
2473 * Locking: see tty_set_ldisc, this function is just a helper
2474 */
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476static int tiocsetd(struct tty_struct *tty, int __user *p)
2477{
2478 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002479 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 if (get_user(ldisc, p))
2482 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002483
2484 lock_kernel();
2485 ret = tty_set_ldisc(tty, ldisc);
2486 unlock_kernel();
2487
2488 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Alan Coxaf9b8972006-08-27 01:24:01 -07002491/**
2492 * send_break - performed time break
2493 * @tty: device to break on
2494 * @duration: timeout in mS
2495 *
2496 * Perform a timed break on hardware that lacks its own driver level
2497 * timed break functionality.
2498 *
2499 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002500 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002501 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002502 */
2503
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002504static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Alan Cox9e989662008-07-22 11:18:03 +01002506 int retval;
2507
2508 if (tty->ops->break_ctl == NULL)
2509 return 0;
2510
2511 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2512 retval = tty->ops->break_ctl(tty, duration);
2513 else {
2514 /* Do the work ourselves */
2515 if (tty_write_lock(tty, 0) < 0)
2516 return -EINTR;
2517 retval = tty->ops->break_ctl(tty, -1);
2518 if (retval)
2519 goto out;
2520 if (!signal_pending(current))
2521 msleep_interruptible(duration);
2522 retval = tty->ops->break_ctl(tty, 0);
2523out:
2524 tty_write_unlock(tty);
2525 if (signal_pending(current))
2526 retval = -EINTR;
2527 }
2528 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Alan Coxaf9b8972006-08-27 01:24:01 -07002531/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002532 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002533 * @tty: tty device
2534 * @file: user file pointer
2535 * @p: pointer to result
2536 *
2537 * Obtain the modem status bits from the tty driver if the feature
2538 * is supported. Return -EINVAL if it is not available.
2539 *
2540 * Locking: none (up to the driver)
2541 */
2542
2543static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 int retval = -EINVAL;
2546
Alan Coxf34d7a52008-04-30 00:54:13 -07002547 if (tty->ops->tiocmget) {
2548 retval = tty->ops->tiocmget(tty, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 if (retval >= 0)
2551 retval = put_user(retval, p);
2552 }
2553 return retval;
2554}
2555
Alan Coxaf9b8972006-08-27 01:24:01 -07002556/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002557 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002558 * @tty: tty device
2559 * @file: user file pointer
2560 * @cmd: command - clear bits, set bits or set all
2561 * @p: pointer to desired bits
2562 *
2563 * Set the modem status bits from the tty driver if the feature
2564 * is supported. Return -EINVAL if it is not available.
2565 *
2566 * Locking: none (up to the driver)
2567 */
2568
2569static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 unsigned __user *p)
2571{
Alan Coxae677512008-07-16 21:56:54 +01002572 int retval;
2573 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Alan Coxae677512008-07-16 21:56:54 +01002575 if (tty->ops->tiocmset == NULL)
2576 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Alan Coxae677512008-07-16 21:56:54 +01002578 retval = get_user(val, p);
2579 if (retval)
2580 return retval;
2581 set = clear = 0;
2582 switch (cmd) {
2583 case TIOCMBIS:
2584 set = val;
2585 break;
2586 case TIOCMBIC:
2587 clear = val;
2588 break;
2589 case TIOCMSET:
2590 set = val;
2591 clear = ~val;
2592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Alan Coxae677512008-07-16 21:56:54 +01002594 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2595 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2596 return tty->ops->tiocmset(tty, file, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
2598
2599/*
2600 * Split this up, as gcc can choke on it otherwise..
2601 */
Alan Cox04f378b2008-04-30 00:53:29 -07002602long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 struct tty_struct *tty, *real_tty;
2605 void __user *p = (void __user *)arg;
2606 int retval;
2607 struct tty_ldisc *ld;
Alan Cox04f378b2008-04-30 00:53:29 -07002608 struct inode *inode = file->f_dentry->d_inode;
Alan Cox37bdfb02008-02-08 04:18:47 -08002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 tty = (struct tty_struct *)file->private_data;
2611 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2612 return -EINVAL;
2613
2614 real_tty = tty;
2615 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2616 tty->driver->subtype == PTY_TYPE_MASTER)
2617 real_tty = tty->link;
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 /*
2621 * Factor out some common prep work
2622 */
2623 switch (cmd) {
2624 case TIOCSETD:
2625 case TIOCSBRK:
2626 case TIOCCBRK:
2627 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002628 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 retval = tty_check_change(tty);
2630 if (retval)
2631 return retval;
2632 if (cmd != TIOCCBRK) {
2633 tty_wait_until_sent(tty, 0);
2634 if (signal_pending(current))
2635 return -EINTR;
2636 }
2637 break;
2638 }
2639
Alan Cox9e989662008-07-22 11:18:03 +01002640 /*
2641 * Now do the stuff.
2642 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002644 case TIOCSTI:
2645 return tiocsti(tty, p);
2646 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002647 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002648 case TIOCSWINSZ:
2649 return tiocswinsz(tty, real_tty, p);
2650 case TIOCCONS:
2651 return real_tty != tty ? -EINVAL : tioccons(file);
2652 case FIONBIO:
2653 return fionbio(file, p);
2654 case TIOCEXCL:
2655 set_bit(TTY_EXCLUSIVE, &tty->flags);
2656 return 0;
2657 case TIOCNXCL:
2658 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2659 return 0;
2660 case TIOCNOTTY:
2661 if (current->signal->tty != tty)
2662 return -ENOTTY;
2663 no_tty();
2664 return 0;
2665 case TIOCSCTTY:
2666 return tiocsctty(tty, arg);
2667 case TIOCGPGRP:
2668 return tiocgpgrp(tty, real_tty, p);
2669 case TIOCSPGRP:
2670 return tiocspgrp(tty, real_tty, p);
2671 case TIOCGSID:
2672 return tiocgsid(tty, real_tty, p);
2673 case TIOCGETD:
Alan Coxa352def2008-07-16 21:53:12 +01002674 return put_user(tty->ldisc.ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002675 case TIOCSETD:
2676 return tiocsetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002677 /*
2678 * Break handling
2679 */
2680 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002681 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002682 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002683 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002684 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002685 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002686 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002687 return 0;
2688 case TCSBRK: /* SVID version: non-zero arg --> no break */
2689 /* non-zero arg means wait for all output data
2690 * to be sent (performed above) but don't send break.
2691 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002693 if (!arg)
2694 return send_break(tty, 250);
2695 return 0;
2696 case TCSBRKP: /* support for POSIX tcsendbreak() */
2697 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Alan Cox37bdfb02008-02-08 04:18:47 -08002699 case TIOCMGET:
2700 return tty_tiocmget(tty, file, p);
2701 case TIOCMSET:
2702 case TIOCMBIC:
2703 case TIOCMBIS:
2704 return tty_tiocmset(tty, file, cmd, p);
2705 case TCFLSH:
2706 switch (arg) {
2707 case TCIFLUSH:
2708 case TCIOFLUSH:
2709 /* flush tty buffer and allow ldisc to process ioctl */
2710 tty_buffer_flush(tty);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002711 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002712 }
2713 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002715 if (tty->ops->ioctl) {
2716 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (retval != -ENOIOCTLCMD)
2718 return retval;
2719 }
2720 ld = tty_ldisc_ref_wait(tty);
2721 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002722 if (ld->ops->ioctl) {
2723 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (retval == -ENOIOCTLCMD)
2725 retval = -EINVAL;
2726 }
2727 tty_ldisc_deref(ld);
2728 return retval;
2729}
2730
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002731#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002732static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002733 unsigned long arg)
2734{
2735 struct inode *inode = file->f_dentry->d_inode;
2736 struct tty_struct *tty = file->private_data;
2737 struct tty_ldisc *ld;
2738 int retval = -ENOIOCTLCMD;
2739
2740 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2741 return -EINVAL;
2742
Alan Coxf34d7a52008-04-30 00:54:13 -07002743 if (tty->ops->compat_ioctl) {
2744 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002745 if (retval != -ENOIOCTLCMD)
2746 return retval;
2747 }
2748
2749 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002750 if (ld->ops->compat_ioctl)
2751 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002752 tty_ldisc_deref(ld);
2753
2754 return retval;
2755}
2756#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758/*
2759 * This implements the "Secure Attention Key" --- the idea is to
2760 * prevent trojan horses by killing all processes associated with this
2761 * tty when the user hits the "Secure Attention Key". Required for
2762 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08002763 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 * This code could be nicer; ideally it should send a HUP, wait a few
2765 * seconds, then send a INT, and then a KILL signal. But you then
2766 * have to coordinate with the init process, since all processes associated
2767 * with the current tty must be dead before the new getty is allowed
2768 * to spawn.
2769 *
2770 * Now, if it would be correct ;-/ The current code has a nasty hole -
2771 * it doesn't catch files in flight. We may send the descriptor to ourselves
2772 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2773 *
2774 * Nasty bug: do_SAK is being called in interrupt context. This can
2775 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2776 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002777void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778{
2779#ifdef TTY_SOFT_SAK
2780 tty_hangup(tty);
2781#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08002782 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002783 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 int i;
2785 struct file *filp;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002786 struct fdtable *fdt;
Alan Cox37bdfb02008-02-08 04:18:47 -08002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (!tty)
2789 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002790 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08002791
Dan Carpenterb3f13de2006-12-13 00:35:09 -08002792 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Alan Coxf34d7a52008-04-30 00:54:13 -07002794 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08002795
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002797 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002798 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08002799 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07002800 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002801 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002802 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002803 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002804 /* Now kill any processes that happen to have the
2805 * tty open.
2806 */
2807 do_each_thread(g, p) {
2808 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 printk(KERN_NOTICE "SAK: killed process %d"
Pavel Emelianova47afb02007-10-18 23:39:46 -07002810 " (%s): task_session_nr(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002811 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 send_sig(SIGKILL, p, 1);
2813 continue;
2814 }
2815 task_lock(p);
2816 if (p->files) {
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002817 /*
2818 * We don't take a ref to the file, so we must
2819 * hold ->file_lock instead.
2820 */
2821 spin_lock(&p->files->file_lock);
Dipankar Sarmabadf1662005-09-09 13:04:10 -07002822 fdt = files_fdtable(p->files);
Alan Cox37bdfb02008-02-08 04:18:47 -08002823 for (i = 0; i < fdt->max_fds; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 filp = fcheck_files(p->files, i);
2825 if (!filp)
2826 continue;
2827 if (filp->f_op->read == tty_read &&
2828 filp->private_data == tty) {
2829 printk(KERN_NOTICE "SAK: killed process %d"
2830 " (%s): fd#%d opened to the tty\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07002831 task_pid_nr(p), p->comm, i);
Eric W. Biederman20ac9432006-04-13 04:49:07 -06002832 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 break;
2834 }
2835 }
Dipankar Sarmaca99c1d2006-04-18 22:21:46 -07002836 spin_unlock(&p->files->file_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08002839 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 read_unlock(&tasklist_lock);
2841#endif
2842}
2843
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08002844static void do_SAK_work(struct work_struct *work)
2845{
2846 struct tty_struct *tty =
2847 container_of(work, struct tty_struct, SAK_work);
2848 __do_SAK(tty);
2849}
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851/*
2852 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2853 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2854 * the values which we write to it will be identical to the values which it
2855 * already has. --akpm
2856 */
2857void do_SAK(struct tty_struct *tty)
2858{
2859 if (!tty)
2860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 schedule_work(&tty->SAK_work);
2862}
2863
2864EXPORT_SYMBOL(do_SAK);
2865
Alan Coxaf9b8972006-08-27 01:24:01 -07002866/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002867 * initialize_tty_struct
2868 * @tty: tty to initialize
2869 *
2870 * This subroutine initializes a tty structure that has been newly
2871 * allocated.
2872 *
2873 * Locking: none - tty in question must not be exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876static void initialize_tty_struct(struct tty_struct *tty)
2877{
2878 memset(tty, 0, sizeof(struct tty_struct));
Alan Cox9c9f4de2008-10-13 10:37:26 +01002879 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01002881 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002882 tty->session = NULL;
2883 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 tty->overrun_time = jiffies;
Alan Cox33f0f882006-01-09 20:54:13 -08002885 tty->buf.head = tty->buf.tail = NULL;
2886 tty_buffer_init(tty);
Arjan van de Ven5785c952006-09-29 02:00:43 -07002887 mutex_init(&tty->termios_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 init_waitqueue_head(&tty->write_wait);
2889 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00002890 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08002891 mutex_init(&tty->atomic_read_lock);
2892 mutex_init(&tty->atomic_write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 spin_lock_init(&tty->read_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07002894 spin_lock_init(&tty->ctrl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002896 INIT_WORK(&tty->SAK_work, do_SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
Alan Coxf34d7a52008-04-30 00:54:13 -07002899/**
2900 * tty_put_char - write one character to a tty
2901 * @tty: tty
2902 * @ch: character
2903 *
2904 * Write one byte to the tty using the provided put_char method
2905 * if present. Returns the number of characters successfully output.
2906 *
2907 * Note: the specific put_char operation in the driver layer may go
2908 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002910
Alan Coxf34d7a52008-04-30 00:54:13 -07002911int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
Alan Coxf34d7a52008-04-30 00:54:13 -07002913 if (tty->ops->put_char)
2914 return tty->ops->put_char(tty, ch);
2915 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Alan Coxf34d7a52008-04-30 00:54:13 -07002918EXPORT_SYMBOL_GPL(tty_put_char);
2919
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08002920static struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
2922/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002923 * tty_register_device - register a tty device
2924 * @driver: the tty driver that describes the tty device
2925 * @index: the index in the tty driver for this tty device
2926 * @device: a struct device that is associated with this tty device.
2927 * This field is optional, if there is no known struct device
2928 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002930 * Returns a pointer to the struct device for this tty device
2931 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002932 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002933 * This call is required to be made to register an individual tty device
2934 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2935 * that bit is not set, this function should not be called by a tty
2936 * driver.
2937 *
2938 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002940
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07002941struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2942 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 char name[64];
2945 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2946
2947 if (index >= driver->num) {
2948 printk(KERN_ERR "Attempt to register invalid tty line number "
2949 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002950 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 }
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (driver->type == TTY_DRIVER_TYPE_PTY)
2954 pty_line_name(driver, index, name);
2955 else
2956 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02002957
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07002958 return device_create_drvdata(tty_class, device, dev, NULL, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
2961/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002962 * tty_unregister_device - unregister a tty device
2963 * @driver: the tty driver that describes the tty device
2964 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002966 * If a tty device is registered with a call to tty_register_device() then
2967 * this function must be called when the tty device is gone.
2968 *
2969 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972void tty_unregister_device(struct tty_driver *driver, unsigned index)
2973{
Alan Cox37bdfb02008-02-08 04:18:47 -08002974 device_destroy(tty_class,
2975 MKDEV(driver->major, driver->minor_start) + index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978EXPORT_SYMBOL(tty_register_device);
2979EXPORT_SYMBOL(tty_unregister_device);
2980
2981struct tty_driver *alloc_tty_driver(int lines)
2982{
2983 struct tty_driver *driver;
2984
Jean Delvare506eb992007-07-15 23:40:14 -07002985 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (driver) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 driver->magic = TTY_DRIVER_MAGIC;
2988 driver->num = lines;
2989 /* later we'll move allocation of tables here */
2990 }
2991 return driver;
2992}
2993
2994void put_tty_driver(struct tty_driver *driver)
2995{
2996 kfree(driver);
2997}
2998
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002999void tty_set_operations(struct tty_driver *driver,
3000 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001{
Alan Coxf34d7a52008-04-30 00:54:13 -07003002 driver->ops = op;
3003};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005EXPORT_SYMBOL(alloc_tty_driver);
3006EXPORT_SYMBOL(put_tty_driver);
3007EXPORT_SYMBOL(tty_set_operations);
3008
3009/*
3010 * Called by a tty driver to register itself.
3011 */
3012int tty_register_driver(struct tty_driver *driver)
3013{
3014 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003015 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 dev_t dev;
3017 void **p = NULL;
3018
3019 if (driver->flags & TTY_DRIVER_INSTALLED)
3020 return 0;
3021
Andy Whitcroft543691a62007-05-06 14:49:33 -07003022 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
3023 p = kzalloc(driver->num * 3 * sizeof(void *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (!p)
3025 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
3027
3028 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003029 error = alloc_chrdev_region(&dev, driver->minor_start,
3030 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (!error) {
3032 driver->major = MAJOR(dev);
3033 driver->minor_start = MINOR(dev);
3034 }
3035 } else {
3036 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003037 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 if (error < 0) {
3040 kfree(p);
3041 return error;
3042 }
3043
3044 if (p) {
3045 driver->ttys = (struct tty_struct **)p;
Alan Coxedc6afc2006-12-08 02:38:44 -08003046 driver->termios = (struct ktermios **)(p + driver->num);
Alan Cox37bdfb02008-02-08 04:18:47 -08003047 driver->termios_locked = (struct ktermios **)
3048 (p + driver->num * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 } else {
3050 driver->ttys = NULL;
3051 driver->termios = NULL;
3052 driver->termios_locked = NULL;
3053 }
3054
3055 cdev_init(&driver->cdev, &tty_fops);
3056 driver->cdev.owner = driver->owner;
3057 error = cdev_add(&driver->cdev, dev, driver->num);
3058 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 unregister_chrdev_region(dev, driver->num);
3060 driver->ttys = NULL;
3061 driver->termios = driver->termios_locked = NULL;
3062 kfree(p);
3063 return error;
3064 }
3065
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003066 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003068 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003069
3070 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
3071 for (i = 0; i < driver->num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 tty_register_device(driver, i, NULL);
3073 }
3074 proc_tty_register_driver(driver);
3075 return 0;
3076}
3077
3078EXPORT_SYMBOL(tty_register_driver);
3079
3080/*
3081 * Called by a tty driver to unregister itself.
3082 */
3083int tty_unregister_driver(struct tty_driver *driver)
3084{
3085 int i;
Alan Coxedc6afc2006-12-08 02:38:44 -08003086 struct ktermios *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 void *p;
3088
3089 if (driver->refcount)
3090 return -EBUSY;
3091
3092 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3093 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003094 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003096 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 /*
3099 * Free the termios and termios_locked structures because
3100 * we don't want to get memory leaks when modular tty
3101 * drivers are removed from the kernel.
3102 */
3103 for (i = 0; i < driver->num; i++) {
3104 tp = driver->termios[i];
3105 if (tp) {
3106 driver->termios[i] = NULL;
3107 kfree(tp);
3108 }
3109 tp = driver->termios_locked[i];
3110 if (tp) {
3111 driver->termios_locked[i] = NULL;
3112 kfree(tp);
3113 }
Greg Kroah-Hartman331b8312005-06-20 21:15:16 -07003114 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 tty_unregister_device(driver, i);
3116 }
3117 p = driver->ttys;
3118 proc_tty_unregister_driver(driver);
3119 driver->ttys = NULL;
3120 driver->termios = driver->termios_locked = NULL;
3121 kfree(p);
3122 cdev_del(&driver->cdev);
3123 return 0;
3124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125EXPORT_SYMBOL(tty_unregister_driver);
3126
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003127dev_t tty_devnum(struct tty_struct *tty)
3128{
3129 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3130}
3131EXPORT_SYMBOL(tty_devnum);
3132
3133void proc_clear_tty(struct task_struct *p)
3134{
Alan Cox9c9f4de2008-10-13 10:37:26 +01003135 struct tty_struct *tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003136 spin_lock_irq(&p->sighand->siglock);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003137 tty = p->signal->tty;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003138 p->signal->tty = NULL;
3139 spin_unlock_irq(&p->sighand->siglock);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003140 tty_kref_put(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003141}
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003142
Alan Cox47f86832008-04-30 00:53:30 -07003143/* Called under the sighand lock */
3144
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003145static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003146{
3147 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -07003148 unsigned long flags;
3149 /* We should not have a session or pgrp to put here but.... */
3150 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -06003151 put_pid(tty->session);
3152 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003153 tty->pgrp = get_pid(task_pgrp(tsk));
Alan Cox47f86832008-04-30 00:53:30 -07003154 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3155 tty->session = get_pid(task_session(tsk));
Alan Cox9c9f4de2008-10-13 10:37:26 +01003156 if (tsk->signal->tty) {
3157 printk(KERN_DEBUG "tty not NULL!!\n");
3158 tty_kref_put(tsk->signal->tty);
3159 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003160 }
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003161 put_pid(tsk->signal->tty_old_pgrp);
Alan Cox9c9f4de2008-10-13 10:37:26 +01003162 tsk->signal->tty = tty_kref_get(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003163 tsk->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003164}
3165
Eric W. Biederman98a27ba2007-05-08 00:26:56 -07003166static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003167{
3168 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -07003169 __proc_set_tty(tsk, tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003170 spin_unlock_irq(&tsk->sighand->siglock);
3171}
3172
3173struct tty_struct *get_current_tty(void)
3174{
3175 struct tty_struct *tty;
Alan Cox934e6eb2008-10-13 10:40:43 +01003176 unsigned long flags;
3177
3178 spin_lock_irqsave(&current->sighand->siglock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +01003179 tty = tty_kref_get(current->signal->tty);
Alan Cox934e6eb2008-10-13 10:40:43 +01003180 spin_unlock_irqrestore(&current->sighand->siglock, flags);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003181 return tty;
3182}
Heiko Carstensa311f742006-12-13 00:33:41 -08003183EXPORT_SYMBOL_GPL(get_current_tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185/*
3186 * Initialize the console device. This is called *early*, so
3187 * we can't necessarily depend on lots of kernel help here.
3188 * Just do some early initializations, and do the complex setup
3189 * later.
3190 */
3191void __init console_init(void)
3192{
3193 initcall_t *call;
3194
3195 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003196 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003199 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 * inform about problems etc..
3201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 call = __con_initcall_start;
3203 while (call < __con_initcall_end) {
3204 (*call)();
3205 call++;
3206 }
3207}
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209static int __init tty_class_init(void)
3210{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003211 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 if (IS_ERR(tty_class))
3213 return PTR_ERR(tty_class);
3214 return 0;
3215}
3216
3217postcore_initcall(tty_class_init);
3218
3219/* 3/2004 jmc: why do these devices exist? */
3220
3221static struct cdev tty_cdev, console_cdev;
3222#ifdef CONFIG_UNIX98_PTYS
3223static struct cdev ptmx_cdev;
3224#endif
3225#ifdef CONFIG_VT
3226static struct cdev vc0_cdev;
3227#endif
3228
3229/*
3230 * Ok, now we can initialize the rest of the tty devices and can count
3231 * on memory allocations, interrupts etc..
3232 */
3233static int __init tty_init(void)
3234{
3235 cdev_init(&tty_cdev, &tty_fops);
3236 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3237 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3238 panic("Couldn't register /dev/tty driver\n");
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003239 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3240 "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
3242 cdev_init(&console_cdev, &console_fops);
3243 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3244 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3245 panic("Couldn't register /dev/console driver\n");
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003246 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3247 "console");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249#ifdef CONFIG_UNIX98_PTYS
3250 cdev_init(&ptmx_cdev, &ptmx_fops);
3251 if (cdev_add(&ptmx_cdev, MKDEV(TTYAUX_MAJOR, 2), 1) ||
3252 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 2), 1, "/dev/ptmx") < 0)
3253 panic("Couldn't register /dev/ptmx driver\n");
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003254 device_create_drvdata(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 2), NULL, "ptmx");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255#endif
3256
3257#ifdef CONFIG_VT
3258 cdev_init(&vc0_cdev, &console_fops);
3259 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3260 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3261 panic("Couldn't register /dev/tty0 driver\n");
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003262 device_create_drvdata(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 vty_init();
3265#endif
3266 return 0;
3267}
3268module_init(tty_init);