initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / drivers / char / tty_io.c
blob123cedf62ccef7741576cfe9e8b542e212a09916
1 /*
2 * linux/drivers/char/tty_io.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
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.
11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
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.
19 * Also restructured routines so that there is more of a separation
20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
21 * the low-level tty routines (serial.c, pty.c, console.c). This
22 * makes for cleaner and more compact code. -TYT, 9/17/92
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).
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
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.
36 * Rewrote canonical mode and added more termios flags.
37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
39 * Reorganized FASYNC support so mouse code can share it.
40 * -- ctm@ardi.com, 9Sep95
42 * New TIOCLINUX variants added.
43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
45 * Restrict vt switching via ioctl()
46 * -- grif@cs.ucr.edu, 5-Dec-95
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
52 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
53 * -- Bill Hawes <whawes@star.net>, June 97
55 * Added devfs support.
56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
58 * Added support for a Unix98-style ptmx device.
59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
61 * Reduced memory usage for older ARM systems
62 * -- Russell King <rmk@arm.linux.org.uk>
64 * Move do_SAK() into process context. Less stack use in devfs functions.
65 * alloc_tty_struct() always uses kmalloc()
66 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
69 #include <linux/types.h>
70 #include <linux/major.h>
71 #include <linux/errno.h>
72 #include <linux/signal.h>
73 #include <linux/fcntl.h>
74 #include <linux/sched.h>
75 #include <linux/interrupt.h>
76 #include <linux/tty.h>
77 #include <linux/tty_driver.h>
78 #include <linux/tty_flip.h>
79 #include <linux/devpts_fs.h>
80 #include <linux/file.h>
81 #include <linux/fdtable.h>
82 #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>
95 #include <linux/wait.h>
96 #include <linux/bitops.h>
97 #include <linux/delay.h>
98 #include <linux/seq_file.h>
99 #include <linux/serial.h>
101 #include <linux/uaccess.h>
102 #include <asm/system.h>
104 #include <linux/kbd_kern.h>
105 #include <linux/vt_kern.h>
106 #include <linux/selection.h>
108 #include <linux/kmod.h>
109 #include <linux/nsproxy.h>
111 #undef TTY_DEBUG_HANGUP
113 #define TTY_PARANOIA_CHECK 1
114 #define CHECK_TTY_COUNT 1
116 struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
117 .c_iflag = ICRNL | IXON,
118 .c_oflag = OPOST | ONLCR,
119 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
120 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
121 ECHOCTL | ECHOKE | IEXTEN,
122 .c_cc = INIT_C_CC,
123 .c_ispeed = 38400,
124 .c_ospeed = 38400
127 EXPORT_SYMBOL(tty_std_termios);
129 /* This list gets poked at by procfs and various bits of boot up code. This
130 could do with some rationalisation such as pulling the tty proc function
131 into this file */
133 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
135 /* Mutex to protect creating and releasing a tty. This is shared with
136 vt.c for deeply disgusting hack reasons */
137 DEFINE_MUTEX(tty_mutex);
138 EXPORT_SYMBOL(tty_mutex);
140 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
141 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
142 ssize_t redirected_tty_write(struct file *, const char __user *,
143 size_t, loff_t *);
144 static unsigned int tty_poll(struct file *, poll_table *);
145 static int tty_open(struct inode *, struct file *);
146 static int tty_release(struct inode *, struct file *);
147 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
148 #ifdef CONFIG_COMPAT
149 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
150 unsigned long arg);
151 #else
152 #define tty_compat_ioctl NULL
153 #endif
154 static int tty_fasync(int fd, struct file *filp, int on);
155 static void release_tty(struct tty_struct *tty, int idx);
156 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
157 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty);
160 * alloc_tty_struct - allocate a tty object
162 * Return a new empty tty structure. The data fields have not
163 * been initialized in any way but has been zeroed
165 * Locking: none
168 struct tty_struct *alloc_tty_struct(void)
170 return kzalloc(sizeof(struct tty_struct), GFP_KERNEL);
174 * free_tty_struct - free a disused tty
175 * @tty: tty struct to free
177 * Free the write buffers, tty queue and tty memory itself.
179 * Locking: none. Must be called after tty is definitely unused
182 void free_tty_struct(struct tty_struct *tty)
184 kfree(tty->write_buf);
185 tty_buffer_free_all(tty);
186 kfree(tty);
189 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
192 * tty_name - return tty naming
193 * @tty: tty structure
194 * @buf: buffer for output
196 * Convert a tty structure into a name. The name reflects the kernel
197 * naming policy and if udev is in use may not reflect user space
199 * Locking: none
202 char *tty_name(struct tty_struct *tty, char *buf)
204 if (!tty) /* Hmm. NULL pointer. That's fun. */
205 strcpy(buf, "NULL tty");
206 else
207 strcpy(buf, tty->name);
208 return buf;
211 EXPORT_SYMBOL(tty_name);
213 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
214 const char *routine)
216 #ifdef TTY_PARANOIA_CHECK
217 if (!tty) {
218 printk(KERN_WARNING
219 "null TTY for (%d:%d) in %s\n",
220 imajor(inode), iminor(inode), routine);
221 return 1;
223 if (tty->magic != TTY_MAGIC) {
224 printk(KERN_WARNING
225 "bad magic number for tty struct (%d:%d) in %s\n",
226 imajor(inode), iminor(inode), routine);
227 return 1;
229 #endif
230 return 0;
233 static int check_tty_count(struct tty_struct *tty, const char *routine)
235 #ifdef CHECK_TTY_COUNT
236 struct list_head *p;
237 int count = 0;
239 file_list_lock();
240 list_for_each(p, &tty->tty_files) {
241 count++;
243 file_list_unlock();
244 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
245 tty->driver->subtype == PTY_TYPE_SLAVE &&
246 tty->link && tty->link->count)
247 count++;
248 if (tty->count != count) {
249 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
250 "!= #fd's(%d) in %s\n",
251 tty->name, tty->count, count, routine);
252 return count;
254 #endif
255 return 0;
259 * get_tty_driver - find device of a tty
260 * @dev_t: device identifier
261 * @index: returns the index of the tty
263 * This routine returns a tty driver structure, given a device number
264 * and also passes back the index number.
266 * Locking: caller must hold tty_mutex
269 static struct tty_driver *get_tty_driver(dev_t device, int *index)
271 struct tty_driver *p;
273 list_for_each_entry(p, &tty_drivers, tty_drivers) {
274 dev_t base = MKDEV(p->major, p->minor_start);
275 if (device < base || device >= base + p->num)
276 continue;
277 *index = device - base;
278 return tty_driver_kref_get(p);
280 return NULL;
283 #ifdef CONFIG_CONSOLE_POLL
286 * tty_find_polling_driver - find device of a polled tty
287 * @name: name string to match
288 * @line: pointer to resulting tty line nr
290 * This routine returns a tty driver structure, given a name
291 * and the condition that the tty driver is capable of polled
292 * operation.
294 struct tty_driver *tty_find_polling_driver(char *name, int *line)
296 struct tty_driver *p, *res = NULL;
297 int tty_line = 0;
298 int len;
299 char *str, *stp;
301 for (str = name; *str; str++)
302 if ((*str >= '0' && *str <= '9') || *str == ',')
303 break;
304 if (!*str)
305 return NULL;
307 len = str - name;
308 tty_line = simple_strtoul(str, &str, 10);
310 mutex_lock(&tty_mutex);
311 /* Search through the tty devices to look for a match */
312 list_for_each_entry(p, &tty_drivers, tty_drivers) {
313 if (strncmp(name, p->name, len) != 0)
314 continue;
315 stp = str;
316 if (*stp == ',')
317 stp++;
318 if (*stp == '\0')
319 stp = NULL;
321 if (tty_line >= 0 && tty_line <= p->num && p->ops &&
322 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
323 res = tty_driver_kref_get(p);
324 *line = tty_line;
325 break;
328 mutex_unlock(&tty_mutex);
330 return res;
332 EXPORT_SYMBOL_GPL(tty_find_polling_driver);
333 #endif
336 * tty_check_change - check for POSIX terminal changes
337 * @tty: tty to check
339 * If we try to write to, or set the state of, a terminal and we're
340 * not in the foreground, send a SIGTTOU. If the signal is blocked or
341 * ignored, go ahead and perform the operation. (POSIX 7.2)
343 * Locking: ctrl_lock
346 int tty_check_change(struct tty_struct *tty)
348 unsigned long flags;
349 int ret = 0;
351 if (current->signal->tty != tty)
352 return 0;
354 spin_lock_irqsave(&tty->ctrl_lock, flags);
356 if (!tty->pgrp) {
357 printk(KERN_WARNING "tty_check_change: tty->pgrp == NULL!\n");
358 goto out_unlock;
360 if (task_pgrp(current) == tty->pgrp)
361 goto out_unlock;
362 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
363 if (is_ignored(SIGTTOU))
364 goto out;
365 if (is_current_pgrp_orphaned()) {
366 ret = -EIO;
367 goto out;
369 kill_pgrp(task_pgrp(current), SIGTTOU, 1);
370 set_thread_flag(TIF_SIGPENDING);
371 ret = -ERESTARTSYS;
372 out:
373 return ret;
374 out_unlock:
375 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
376 return ret;
379 EXPORT_SYMBOL(tty_check_change);
381 static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
382 size_t count, loff_t *ppos)
384 return 0;
387 static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
388 size_t count, loff_t *ppos)
390 return -EIO;
393 /* No kernel lock held - none needed ;) */
394 static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
396 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
399 static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
400 unsigned long arg)
402 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
405 static long hung_up_tty_compat_ioctl(struct file *file,
406 unsigned int cmd, unsigned long arg)
408 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
411 static const struct file_operations tty_fops = {
412 .llseek = no_llseek,
413 .read = tty_read,
414 .write = tty_write,
415 .poll = tty_poll,
416 .unlocked_ioctl = tty_ioctl,
417 .compat_ioctl = tty_compat_ioctl,
418 .open = tty_open,
419 .release = tty_release,
420 .fasync = tty_fasync,
423 static const struct file_operations console_fops = {
424 .llseek = no_llseek,
425 .read = tty_read,
426 .write = redirected_tty_write,
427 .poll = tty_poll,
428 .unlocked_ioctl = tty_ioctl,
429 .compat_ioctl = tty_compat_ioctl,
430 .open = tty_open,
431 .release = tty_release,
432 .fasync = tty_fasync,
435 static const struct file_operations hung_up_tty_fops = {
436 .llseek = no_llseek,
437 .read = hung_up_tty_read,
438 .write = hung_up_tty_write,
439 .poll = hung_up_tty_poll,
440 .unlocked_ioctl = hung_up_tty_ioctl,
441 .compat_ioctl = hung_up_tty_compat_ioctl,
442 .release = tty_release,
445 static DEFINE_SPINLOCK(redirect_lock);
446 static struct file *redirect;
449 * tty_wakeup - request more data
450 * @tty: terminal
452 * Internal and external helper for wakeups of tty. This function
453 * informs the line discipline if present that the driver is ready
454 * to receive more output data.
457 void tty_wakeup(struct tty_struct *tty)
459 struct tty_ldisc *ld;
461 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
462 ld = tty_ldisc_ref(tty);
463 if (ld) {
464 if (ld->ops->write_wakeup)
465 ld->ops->write_wakeup(tty);
466 tty_ldisc_deref(ld);
469 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
472 EXPORT_SYMBOL_GPL(tty_wakeup);
475 * do_tty_hangup - actual handler for hangup events
476 * @work: tty device
478 * This can be called by the "eventd" kernel thread. That is process
479 * synchronous but doesn't hold any locks, so we need to make sure we
480 * have the appropriate locks for what we're doing.
482 * The hangup event clears any pending redirections onto the hung up
483 * device. It ensures future writes will error and it does the needed
484 * line discipline hangup and signal delivery. The tty object itself
485 * remains intact.
487 * Locking:
488 * BKL
489 * redirect lock for undoing redirection
490 * file list lock for manipulating list of ttys
491 * tty_ldisc_lock from called functions
492 * termios_mutex resetting termios data
493 * tasklist_lock to walk task list for hangup event
494 * ->siglock to protect ->signal/->sighand
496 static void do_tty_hangup(struct work_struct *work)
498 struct tty_struct *tty =
499 container_of(work, struct tty_struct, hangup_work);
500 struct file *cons_filp = NULL;
501 struct file *filp, *f = NULL;
502 struct task_struct *p;
503 int closecount = 0, n;
504 unsigned long flags;
505 int refs = 0;
507 if (!tty)
508 return;
510 /* inuse_filps is protected by the single kernel lock */
511 lock_kernel();
513 spin_lock(&redirect_lock);
514 if (redirect && redirect->private_data == tty) {
515 f = redirect;
516 redirect = NULL;
518 spin_unlock(&redirect_lock);
520 check_tty_count(tty, "do_tty_hangup");
521 file_list_lock();
522 /* This breaks for file handles being sent over AF_UNIX sockets ? */
523 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
524 if (filp->f_op->write == redirected_tty_write)
525 cons_filp = filp;
526 if (filp->f_op->write != tty_write)
527 continue;
528 closecount++;
529 tty_fasync(-1, filp, 0); /* can't block */
530 filp->f_op = &hung_up_tty_fops;
532 file_list_unlock();
534 tty_ldisc_hangup(tty);
536 read_lock(&tasklist_lock);
537 if (tty->session) {
538 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
539 spin_lock_irq(&p->sighand->siglock);
540 if (p->signal->tty == tty) {
541 p->signal->tty = NULL;
542 /* We defer the dereferences outside fo
543 the tasklist lock */
544 refs++;
546 if (!p->signal->leader) {
547 spin_unlock_irq(&p->sighand->siglock);
548 continue;
550 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
551 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
552 put_pid(p->signal->tty_old_pgrp); /* A noop */
553 spin_lock_irqsave(&tty->ctrl_lock, flags);
554 if (tty->pgrp)
555 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
556 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
557 spin_unlock_irq(&p->sighand->siglock);
558 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
560 read_unlock(&tasklist_lock);
562 spin_lock_irqsave(&tty->ctrl_lock, flags);
563 clear_bit(TTY_THROTTLED, &tty->flags);
564 clear_bit(TTY_PUSH, &tty->flags);
565 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
566 put_pid(tty->session);
567 put_pid(tty->pgrp);
568 tty->session = NULL;
569 tty->pgrp = NULL;
570 tty->ctrl_status = 0;
571 set_bit(TTY_HUPPED, &tty->flags);
572 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
574 /* Account for the p->signal references we killed */
575 while (refs--)
576 tty_kref_put(tty);
579 * If one of the devices matches a console pointer, we
580 * cannot just call hangup() because that will cause
581 * tty->count and state->count to go out of sync.
582 * So we just call close() the right number of times.
584 if (cons_filp) {
585 if (tty->ops->close)
586 for (n = 0; n < closecount; n++)
587 tty->ops->close(tty, cons_filp);
588 } else if (tty->ops->hangup)
589 (tty->ops->hangup)(tty);
591 * We don't want to have driver/ldisc interactions beyond
592 * the ones we did here. The driver layer expects no
593 * calls after ->hangup() from the ldisc side. However we
594 * can't yet guarantee all that.
596 set_bit(TTY_HUPPED, &tty->flags);
597 tty_ldisc_enable(tty);
598 unlock_kernel();
599 if (f)
600 fput(f);
604 * tty_hangup - trigger a hangup event
605 * @tty: tty to hangup
607 * A carrier loss (virtual or otherwise) has occurred on this like
608 * schedule a hangup sequence to run after this event.
611 void tty_hangup(struct tty_struct *tty)
613 #ifdef TTY_DEBUG_HANGUP
614 char buf[64];
615 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
616 #endif
617 schedule_work(&tty->hangup_work);
620 EXPORT_SYMBOL(tty_hangup);
623 * tty_vhangup - process vhangup
624 * @tty: tty to hangup
626 * The user has asked via system call for the terminal to be hung up.
627 * We do this synchronously so that when the syscall returns the process
628 * is complete. That guarantee is necessary for security reasons.
631 void tty_vhangup(struct tty_struct *tty)
633 #ifdef TTY_DEBUG_HANGUP
634 char buf[64];
636 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
637 #endif
638 do_tty_hangup(&tty->hangup_work);
641 EXPORT_SYMBOL(tty_vhangup);
644 * tty_vhangup_self - process vhangup for own ctty
646 * Perform a vhangup on the current controlling tty
649 void tty_vhangup_self(void)
651 struct tty_struct *tty;
653 tty = get_current_tty();
654 if (tty) {
655 tty_vhangup(tty);
656 tty_kref_put(tty);
661 * tty_hung_up_p - was tty hung up
662 * @filp: file pointer of tty
664 * Return true if the tty has been subject to a vhangup or a carrier
665 * loss
668 int tty_hung_up_p(struct file *filp)
670 return (filp->f_op == &hung_up_tty_fops);
673 EXPORT_SYMBOL(tty_hung_up_p);
675 static void session_clear_tty(struct pid *session)
677 struct task_struct *p;
678 do_each_pid_task(session, PIDTYPE_SID, p) {
679 proc_clear_tty(p);
680 } while_each_pid_task(session, PIDTYPE_SID, p);
684 * disassociate_ctty - disconnect controlling tty
685 * @on_exit: true if exiting so need to "hang up" the session
687 * This function is typically called only by the session leader, when
688 * it wants to disassociate itself from its controlling tty.
690 * It performs the following functions:
691 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
692 * (2) Clears the tty from being controlling the session
693 * (3) Clears the controlling tty for all processes in the
694 * session group.
696 * The argument on_exit is set to 1 if called when a process is
697 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
699 * Locking:
700 * BKL is taken for hysterical raisins
701 * tty_mutex is taken to protect tty
702 * ->siglock is taken to protect ->signal/->sighand
703 * tasklist_lock is taken to walk process list for sessions
704 * ->siglock is taken to protect ->signal/->sighand
707 void disassociate_ctty(int on_exit)
709 struct tty_struct *tty;
710 struct pid *tty_pgrp = NULL;
713 tty = get_current_tty();
714 if (tty) {
715 tty_pgrp = get_pid(tty->pgrp);
716 lock_kernel();
717 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
718 tty_vhangup(tty);
719 unlock_kernel();
720 tty_kref_put(tty);
721 } else if (on_exit) {
722 struct pid *old_pgrp;
723 spin_lock_irq(&current->sighand->siglock);
724 old_pgrp = current->signal->tty_old_pgrp;
725 current->signal->tty_old_pgrp = NULL;
726 spin_unlock_irq(&current->sighand->siglock);
727 if (old_pgrp) {
728 kill_pgrp(old_pgrp, SIGHUP, on_exit);
729 kill_pgrp(old_pgrp, SIGCONT, on_exit);
730 put_pid(old_pgrp);
732 return;
734 if (tty_pgrp) {
735 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
736 if (!on_exit)
737 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
738 put_pid(tty_pgrp);
741 spin_lock_irq(&current->sighand->siglock);
742 put_pid(current->signal->tty_old_pgrp);
743 current->signal->tty_old_pgrp = NULL;
744 spin_unlock_irq(&current->sighand->siglock);
746 tty = get_current_tty();
747 if (tty) {
748 unsigned long flags;
749 spin_lock_irqsave(&tty->ctrl_lock, flags);
750 put_pid(tty->session);
751 put_pid(tty->pgrp);
752 tty->session = NULL;
753 tty->pgrp = NULL;
754 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
755 tty_kref_put(tty);
756 } else {
757 #ifdef TTY_DEBUG_HANGUP
758 printk(KERN_DEBUG "error attempted to write to tty [0x%p]"
759 " = NULL", tty);
760 #endif
763 /* Now clear signal->tty under the lock */
764 read_lock(&tasklist_lock);
765 session_clear_tty(task_session(current));
766 read_unlock(&tasklist_lock);
771 * no_tty - Ensure the current process does not have a controlling tty
773 void no_tty(void)
775 struct task_struct *tsk = current;
776 lock_kernel();
777 if (tsk->signal->leader)
778 disassociate_ctty(0);
779 unlock_kernel();
780 proc_clear_tty(tsk);
785 * stop_tty - propagate flow control
786 * @tty: tty to stop
788 * Perform flow control to the driver. For PTY/TTY pairs we
789 * must also propagate the TIOCKPKT status. May be called
790 * on an already stopped device and will not re-call the driver
791 * method.
793 * This functionality is used by both the line disciplines for
794 * halting incoming flow and by the driver. It may therefore be
795 * called from any context, may be under the tty atomic_write_lock
796 * but not always.
798 * Locking:
799 * Uses the tty control lock internally
802 void stop_tty(struct tty_struct *tty)
804 unsigned long flags;
805 spin_lock_irqsave(&tty->ctrl_lock, flags);
806 if (tty->stopped) {
807 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
808 return;
810 tty->stopped = 1;
811 if (tty->link && tty->link->packet) {
812 tty->ctrl_status &= ~TIOCPKT_START;
813 tty->ctrl_status |= TIOCPKT_STOP;
814 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
816 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
817 if (tty->ops->stop)
818 (tty->ops->stop)(tty);
821 EXPORT_SYMBOL(stop_tty);
824 * start_tty - propagate flow control
825 * @tty: tty to start
827 * Start a tty that has been stopped if at all possible. Perform
828 * any necessary wakeups and propagate the TIOCPKT status. If this
829 * is the tty was previous stopped and is being started then the
830 * driver start method is invoked and the line discipline woken.
832 * Locking:
833 * ctrl_lock
836 void start_tty(struct tty_struct *tty)
838 unsigned long flags;
839 spin_lock_irqsave(&tty->ctrl_lock, flags);
840 if (!tty->stopped || tty->flow_stopped) {
841 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
842 return;
844 tty->stopped = 0;
845 if (tty->link && tty->link->packet) {
846 tty->ctrl_status &= ~TIOCPKT_STOP;
847 tty->ctrl_status |= TIOCPKT_START;
848 wake_up_interruptible_poll(&tty->link->read_wait, POLLIN);
850 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
851 if (tty->ops->start)
852 (tty->ops->start)(tty);
853 /* If we have a running line discipline it may need kicking */
854 tty_wakeup(tty);
857 EXPORT_SYMBOL(start_tty);
860 * tty_read - read method for tty device files
861 * @file: pointer to tty file
862 * @buf: user buffer
863 * @count: size of user buffer
864 * @ppos: unused
866 * Perform the read system call function on this terminal device. Checks
867 * for hung up devices before calling the line discipline method.
869 * Locking:
870 * Locks the line discipline internally while needed. Multiple
871 * read calls may be outstanding in parallel.
874 static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
875 loff_t *ppos)
877 int i;
878 struct tty_struct *tty;
879 struct inode *inode;
880 struct tty_ldisc *ld;
882 tty = (struct tty_struct *)file->private_data;
883 inode = file->f_path.dentry->d_inode;
884 if (tty_paranoia_check(tty, inode, "tty_read"))
885 return -EIO;
886 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
887 return -EIO;
889 /* We want to wait for the line discipline to sort out in this
890 situation */
891 ld = tty_ldisc_ref_wait(tty);
892 if (ld->ops->read)
893 i = (ld->ops->read)(tty, file, buf, count);
894 else
895 i = -EIO;
896 tty_ldisc_deref(ld);
897 if (i > 0)
898 inode->i_atime = current_fs_time(inode->i_sb);
899 return i;
902 void tty_write_unlock(struct tty_struct *tty)
904 mutex_unlock(&tty->atomic_write_lock);
905 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
908 int tty_write_lock(struct tty_struct *tty, int ndelay)
910 if (!mutex_trylock(&tty->atomic_write_lock)) {
911 if (ndelay)
912 return -EAGAIN;
913 if (mutex_lock_interruptible(&tty->atomic_write_lock))
914 return -ERESTARTSYS;
916 return 0;
920 * Split writes up in sane blocksizes to avoid
921 * denial-of-service type attacks
923 static inline ssize_t do_tty_write(
924 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
925 struct tty_struct *tty,
926 struct file *file,
927 const char __user *buf,
928 size_t count)
930 ssize_t ret, written = 0;
931 unsigned int chunk;
933 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
934 if (ret < 0)
935 return ret;
938 * We chunk up writes into a temporary buffer. This
939 * simplifies low-level drivers immensely, since they
940 * don't have locking issues and user mode accesses.
942 * But if TTY_NO_WRITE_SPLIT is set, we should use a
943 * big chunk-size..
945 * The default chunk-size is 2kB, because the NTTY
946 * layer has problems with bigger chunks. It will
947 * claim to be able to handle more characters than
948 * it actually does.
950 * FIXME: This can probably go away now except that 64K chunks
951 * are too likely to fail unless switched to vmalloc...
953 chunk = 2048;
954 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
955 chunk = 65536;
956 if (count < chunk)
957 chunk = count;
959 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
960 if (tty->write_cnt < chunk) {
961 unsigned char *buf_chunk;
963 if (chunk < 1024)
964 chunk = 1024;
966 buf_chunk = kmalloc(chunk, GFP_KERNEL);
967 if (!buf_chunk) {
968 ret = -ENOMEM;
969 goto out;
971 kfree(tty->write_buf);
972 tty->write_cnt = chunk;
973 tty->write_buf = buf_chunk;
976 /* Do the write .. */
977 for (;;) {
978 size_t size = count;
979 if (size > chunk)
980 size = chunk;
981 ret = -EFAULT;
982 if (copy_from_user(tty->write_buf, buf, size))
983 break;
984 ret = write(tty, file, tty->write_buf, size);
985 if (ret <= 0)
986 break;
987 written += ret;
988 buf += ret;
989 count -= ret;
990 if (!count)
991 break;
992 ret = -ERESTARTSYS;
993 if (signal_pending(current))
994 break;
995 cond_resched();
997 if (written) {
998 struct inode *inode = file->f_path.dentry->d_inode;
999 inode->i_mtime = current_fs_time(inode->i_sb);
1000 ret = written;
1002 out:
1003 tty_write_unlock(tty);
1004 return ret;
1008 * tty_write_message - write a message to a certain tty, not just the console.
1009 * @tty: the destination tty_struct
1010 * @msg: the message to write
1012 * This is used for messages that need to be redirected to a specific tty.
1013 * We don't put it into the syslog queue right now maybe in the future if
1014 * really needed.
1016 * We must still hold the BKL and test the CLOSING flag for the moment.
1019 void tty_write_message(struct tty_struct *tty, char *msg)
1021 lock_kernel();
1022 if (tty) {
1023 mutex_lock(&tty->atomic_write_lock);
1024 if (tty->ops->write && !test_bit(TTY_CLOSING, &tty->flags))
1025 tty->ops->write(tty, msg, strlen(msg));
1026 tty_write_unlock(tty);
1028 unlock_kernel();
1029 return;
1034 * tty_write - write method for tty device file
1035 * @file: tty file pointer
1036 * @buf: user data to write
1037 * @count: bytes to write
1038 * @ppos: unused
1040 * Write data to a tty device via the line discipline.
1042 * Locking:
1043 * Locks the line discipline as required
1044 * Writes to the tty driver are serialized by the atomic_write_lock
1045 * and are then processed in chunks to the device. The line discipline
1046 * write method will not be invoked in parallel for each device.
1049 static ssize_t tty_write(struct file *file, const char __user *buf,
1050 size_t count, loff_t *ppos)
1052 struct tty_struct *tty;
1053 struct inode *inode = file->f_path.dentry->d_inode;
1054 ssize_t ret;
1055 struct tty_ldisc *ld;
1057 tty = (struct tty_struct *)file->private_data;
1058 if (tty_paranoia_check(tty, inode, "tty_write"))
1059 return -EIO;
1060 if (!tty || !tty->ops->write ||
1061 (test_bit(TTY_IO_ERROR, &tty->flags)))
1062 return -EIO;
1063 /* Short term debug to catch buggy drivers */
1064 if (tty->ops->write_room == NULL)
1065 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1066 tty->driver->name);
1067 ld = tty_ldisc_ref_wait(tty);
1068 if (!ld->ops->write)
1069 ret = -EIO;
1070 else
1071 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
1072 tty_ldisc_deref(ld);
1073 return ret;
1076 ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1077 size_t count, loff_t *ppos)
1079 struct file *p = NULL;
1081 spin_lock(&redirect_lock);
1082 if (redirect) {
1083 get_file(redirect);
1084 p = redirect;
1086 spin_unlock(&redirect_lock);
1088 if (p) {
1089 ssize_t res;
1090 res = vfs_write(p, buf, count, &p->f_pos);
1091 fput(p);
1092 return res;
1094 return tty_write(file, buf, count, ppos);
1097 static char ptychar[] = "pqrstuvwxyzabcde";
1100 * pty_line_name - generate name for a pty
1101 * @driver: the tty driver in use
1102 * @index: the minor number
1103 * @p: output buffer of at least 6 bytes
1105 * Generate a name from a driver reference and write it to the output
1106 * buffer.
1108 * Locking: None
1110 static void pty_line_name(struct tty_driver *driver, int index, char *p)
1112 int i = index + driver->name_base;
1113 /* ->name is initialized to "ttyp", but "tty" is expected */
1114 sprintf(p, "%s%c%x",
1115 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1116 ptychar[i >> 4 & 0xf], i & 0xf);
1120 * tty_line_name - generate name for a tty
1121 * @driver: the tty driver in use
1122 * @index: the minor number
1123 * @p: output buffer of at least 7 bytes
1125 * Generate a name from a driver reference and write it to the output
1126 * buffer.
1128 * Locking: None
1130 static void tty_line_name(struct tty_driver *driver, int index, char *p)
1132 sprintf(p, "%s%d", driver->name, index + driver->name_base);
1136 * tty_driver_lookup_tty() - find an existing tty, if any
1137 * @driver: the driver for the tty
1138 * @idx: the minor number
1140 * Return the tty, if found or ERR_PTR() otherwise.
1142 * Locking: tty_mutex must be held. If tty is found, the mutex must
1143 * be held until the 'fast-open' is also done. Will change once we
1144 * have refcounting in the driver and per driver locking
1146 static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
1147 struct inode *inode, int idx)
1149 struct tty_struct *tty;
1151 if (driver->ops->lookup)
1152 return driver->ops->lookup(driver, inode, idx);
1154 tty = driver->ttys[idx];
1155 return tty;
1159 * tty_init_termios - helper for termios setup
1160 * @tty: the tty to set up
1162 * Initialise the termios structures for this tty. Thus runs under
1163 * the tty_mutex currently so we can be relaxed about ordering.
1166 int tty_init_termios(struct tty_struct *tty)
1168 struct ktermios *tp;
1169 int idx = tty->index;
1171 tp = tty->driver->termios[idx];
1172 if (tp == NULL) {
1173 tp = kzalloc(sizeof(struct ktermios[2]), GFP_KERNEL);
1174 if (tp == NULL)
1175 return -ENOMEM;
1176 memcpy(tp, &tty->driver->init_termios,
1177 sizeof(struct ktermios));
1178 tty->driver->termios[idx] = tp;
1180 tty->termios = tp;
1181 tty->termios_locked = tp + 1;
1183 /* Compatibility until drivers always set this */
1184 tty->termios->c_ispeed = tty_termios_input_baud_rate(tty->termios);
1185 tty->termios->c_ospeed = tty_termios_baud_rate(tty->termios);
1186 return 0;
1188 EXPORT_SYMBOL_GPL(tty_init_termios);
1191 * tty_driver_install_tty() - install a tty entry in the driver
1192 * @driver: the driver for the tty
1193 * @tty: the tty
1195 * Install a tty object into the driver tables. The tty->index field
1196 * will be set by the time this is called. This method is responsible
1197 * for ensuring any need additional structures are allocated and
1198 * configured.
1200 * Locking: tty_mutex for now
1202 static int tty_driver_install_tty(struct tty_driver *driver,
1203 struct tty_struct *tty)
1205 int idx = tty->index;
1207 if (driver->ops->install)
1208 return driver->ops->install(driver, tty);
1210 if (tty_init_termios(tty) == 0) {
1211 tty_driver_kref_get(driver);
1212 tty->count++;
1213 driver->ttys[idx] = tty;
1214 return 0;
1216 return -ENOMEM;
1220 * tty_driver_remove_tty() - remove a tty from the driver tables
1221 * @driver: the driver for the tty
1222 * @idx: the minor number
1224 * Remvoe a tty object from the driver tables. The tty->index field
1225 * will be set by the time this is called.
1227 * Locking: tty_mutex for now
1229 static void tty_driver_remove_tty(struct tty_driver *driver,
1230 struct tty_struct *tty)
1232 if (driver->ops->remove)
1233 driver->ops->remove(driver, tty);
1234 else
1235 driver->ttys[tty->index] = NULL;
1239 * tty_reopen() - fast re-open of an open tty
1240 * @tty - the tty to open
1242 * Return 0 on success, -errno on error.
1244 * Locking: tty_mutex must be held from the time the tty was found
1245 * till this open completes.
1247 static int tty_reopen(struct tty_struct *tty)
1249 struct tty_driver *driver = tty->driver;
1251 if (test_bit(TTY_CLOSING, &tty->flags))
1252 return -EIO;
1254 if (driver->type == TTY_DRIVER_TYPE_PTY &&
1255 driver->subtype == PTY_TYPE_MASTER) {
1257 * special case for PTY masters: only one open permitted,
1258 * and the slave side open count is incremented as well.
1260 if (tty->count)
1261 return -EIO;
1263 tty->link->count++;
1265 tty->count++;
1266 tty->driver = driver; /* N.B. why do this every time?? */
1268 mutex_lock(&tty->ldisc_mutex);
1269 WARN_ON(!test_bit(TTY_LDISC, &tty->flags));
1270 mutex_unlock(&tty->ldisc_mutex);
1272 return 0;
1276 * tty_init_dev - initialise a tty device
1277 * @driver: tty driver we are opening a device on
1278 * @idx: device index
1279 * @ret_tty: returned tty structure
1280 * @first_ok: ok to open a new device (used by ptmx)
1282 * Prepare a tty device. This may not be a "new" clean device but
1283 * could also be an active device. The pty drivers require special
1284 * handling because of this.
1286 * Locking:
1287 * The function is called under the tty_mutex, which
1288 * protects us from the tty struct or driver itself going away.
1290 * On exit the tty device has the line discipline attached and
1291 * a reference count of 1. If a pair was created for pty/tty use
1292 * and the other was a pty master then it too has a reference count of 1.
1294 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
1295 * failed open. The new code protects the open with a mutex, so it's
1296 * really quite straightforward. The mutex locking can probably be
1297 * relaxed for the (most common) case of reopening a tty.
1300 struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx,
1301 int first_ok)
1303 struct tty_struct *tty;
1304 int retval;
1306 /* Check if pty master is being opened multiple times */
1307 if (driver->subtype == PTY_TYPE_MASTER &&
1308 (driver->flags & TTY_DRIVER_DEVPTS_MEM) && !first_ok)
1309 return ERR_PTR(-EIO);
1312 * First time open is complex, especially for PTY devices.
1313 * This code guarantees that either everything succeeds and the
1314 * TTY is ready for operation, or else the table slots are vacated
1315 * and the allocated memory released. (Except that the termios
1316 * and locked termios may be retained.)
1319 if (!try_module_get(driver->owner))
1320 return ERR_PTR(-ENODEV);
1322 tty = alloc_tty_struct();
1323 if (!tty)
1324 goto fail_no_mem;
1325 initialize_tty_struct(tty, driver, idx);
1327 retval = tty_driver_install_tty(driver, tty);
1328 if (retval < 0) {
1329 free_tty_struct(tty);
1330 module_put(driver->owner);
1331 return ERR_PTR(retval);
1335 * Structures all installed ... call the ldisc open routines.
1336 * If we fail here just call release_tty to clean up. No need
1337 * to decrement the use counts, as release_tty doesn't care.
1340 retval = tty_ldisc_setup(tty, tty->link);
1341 if (retval)
1342 goto release_mem_out;
1343 return tty;
1345 fail_no_mem:
1346 module_put(driver->owner);
1347 return ERR_PTR(-ENOMEM);
1349 /* call the tty release_tty routine to clean out this slot */
1350 release_mem_out:
1351 if (printk_ratelimit())
1352 printk(KERN_INFO "tty_init_dev: ldisc open failed, "
1353 "clearing slot %d\n", idx);
1354 release_tty(tty, idx);
1355 return ERR_PTR(retval);
1358 void tty_free_termios(struct tty_struct *tty)
1360 struct ktermios *tp;
1361 int idx = tty->index;
1362 /* Kill this flag and push into drivers for locking etc */
1363 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
1364 /* FIXME: Locking on ->termios array */
1365 tp = tty->termios;
1366 tty->driver->termios[idx] = NULL;
1367 kfree(tp);
1370 EXPORT_SYMBOL(tty_free_termios);
1372 void tty_shutdown(struct tty_struct *tty)
1374 tty_driver_remove_tty(tty->driver, tty);
1375 tty_free_termios(tty);
1377 EXPORT_SYMBOL(tty_shutdown);
1380 * release_one_tty - release tty structure memory
1381 * @kref: kref of tty we are obliterating
1383 * Releases memory associated with a tty structure, and clears out the
1384 * driver table slots. This function is called when a device is no longer
1385 * in use. It also gets called when setup of a device fails.
1387 * Locking:
1388 * tty_mutex - sometimes only
1389 * takes the file list lock internally when working on the list
1390 * of ttys that the driver keeps.
1392 * This method gets called from a work queue so that the driver private
1393 * cleanup ops can sleep (needed for USB at least)
1395 static void release_one_tty(struct work_struct *work)
1397 struct tty_struct *tty =
1398 container_of(work, struct tty_struct, hangup_work);
1399 struct tty_driver *driver = tty->driver;
1401 if (tty->ops->cleanup)
1402 tty->ops->cleanup(tty);
1404 tty->magic = 0;
1405 tty_driver_kref_put(driver);
1406 module_put(driver->owner);
1408 file_list_lock();
1409 list_del_init(&tty->tty_files);
1410 file_list_unlock();
1412 put_pid(tty->pgrp);
1413 put_pid(tty->session);
1414 free_tty_struct(tty);
1417 static void queue_release_one_tty(struct kref *kref)
1419 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
1421 if (tty->ops->shutdown)
1422 tty->ops->shutdown(tty);
1423 else
1424 tty_shutdown(tty);
1426 /* The hangup queue is now free so we can reuse it rather than
1427 waste a chunk of memory for each port */
1428 INIT_WORK(&tty->hangup_work, release_one_tty);
1429 schedule_work(&tty->hangup_work);
1433 * tty_kref_put - release a tty kref
1434 * @tty: tty device
1436 * Release a reference to a tty device and if need be let the kref
1437 * layer destruct the object for us
1440 void tty_kref_put(struct tty_struct *tty)
1442 if (tty)
1443 kref_put(&tty->kref, queue_release_one_tty);
1445 EXPORT_SYMBOL(tty_kref_put);
1448 * release_tty - release tty structure memory
1450 * Release both @tty and a possible linked partner (think pty pair),
1451 * and decrement the refcount of the backing module.
1453 * Locking:
1454 * tty_mutex - sometimes only
1455 * takes the file list lock internally when working on the list
1456 * of ttys that the driver keeps.
1457 * FIXME: should we require tty_mutex is held here ??
1460 static void release_tty(struct tty_struct *tty, int idx)
1462 /* This should always be true but check for the moment */
1463 WARN_ON(tty->index != idx);
1465 if (tty->link)
1466 tty_kref_put(tty->link);
1467 tty_kref_put(tty);
1471 * Even releasing the tty structures is a tricky business.. We have
1472 * to be very careful that the structures are all released at the
1473 * same time, as interrupts might otherwise get the wrong pointers.
1475 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1476 * lead to double frees or releasing memory still in use.
1478 void tty_release_dev(struct file *filp)
1480 struct tty_struct *tty, *o_tty;
1481 int pty_master, tty_closing, o_tty_closing, do_sleep;
1482 int devpts;
1483 int idx;
1484 char buf[64];
1485 struct inode *inode;
1487 inode = filp->f_path.dentry->d_inode;
1488 tty = (struct tty_struct *)filp->private_data;
1489 if (tty_paranoia_check(tty, inode, "tty_release_dev"))
1490 return;
1492 check_tty_count(tty, "tty_release_dev");
1494 tty_fasync(-1, filp, 0);
1496 idx = tty->index;
1497 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1498 tty->driver->subtype == PTY_TYPE_MASTER);
1499 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
1500 o_tty = tty->link;
1502 #ifdef TTY_PARANOIA_CHECK
1503 if (idx < 0 || idx >= tty->driver->num) {
1504 printk(KERN_DEBUG "tty_release_dev: bad idx when trying to "
1505 "free (%s)\n", tty->name);
1506 return;
1508 if (!devpts) {
1509 if (tty != tty->driver->ttys[idx]) {
1510 printk(KERN_DEBUG "tty_release_dev: driver.table[%d] not tty "
1511 "for (%s)\n", idx, tty->name);
1512 return;
1514 if (tty->termios != tty->driver->termios[idx]) {
1515 printk(KERN_DEBUG "tty_release_dev: driver.termios[%d] not termios "
1516 "for (%s)\n",
1517 idx, tty->name);
1518 return;
1521 #endif
1523 #ifdef TTY_DEBUG_HANGUP
1524 printk(KERN_DEBUG "tty_release_dev of %s (tty count=%d)...",
1525 tty_name(tty, buf), tty->count);
1526 #endif
1528 #ifdef TTY_PARANOIA_CHECK
1529 if (tty->driver->other &&
1530 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
1531 if (o_tty != tty->driver->other->ttys[idx]) {
1532 printk(KERN_DEBUG "tty_release_dev: other->table[%d] "
1533 "not o_tty for (%s)\n",
1534 idx, tty->name);
1535 return;
1537 if (o_tty->termios != tty->driver->other->termios[idx]) {
1538 printk(KERN_DEBUG "tty_release_dev: other->termios[%d] "
1539 "not o_termios for (%s)\n",
1540 idx, tty->name);
1541 return;
1543 if (o_tty->link != tty) {
1544 printk(KERN_DEBUG "tty_release_dev: bad pty pointers\n");
1545 return;
1548 #endif
1549 if (tty->ops->close)
1550 tty->ops->close(tty, filp);
1553 * Sanity check: if tty->count is going to zero, there shouldn't be
1554 * any waiters on tty->read_wait or tty->write_wait. We test the
1555 * wait queues and kick everyone out _before_ actually starting to
1556 * close. This ensures that we won't block while releasing the tty
1557 * structure.
1559 * The test for the o_tty closing is necessary, since the master and
1560 * slave sides may close in any order. If the slave side closes out
1561 * first, its count will be one, since the master side holds an open.
1562 * Thus this test wouldn't be triggered at the time the slave closes,
1563 * so we do it now.
1565 * Note that it's possible for the tty to be opened again while we're
1566 * flushing out waiters. By recalculating the closing flags before
1567 * each iteration we avoid any problems.
1569 while (1) {
1570 /* Guard against races with tty->count changes elsewhere and
1571 opens on /dev/tty */
1573 mutex_lock(&tty_mutex);
1574 tty_closing = tty->count <= 1;
1575 o_tty_closing = o_tty &&
1576 (o_tty->count <= (pty_master ? 1 : 0));
1577 do_sleep = 0;
1579 if (tty_closing) {
1580 if (waitqueue_active(&tty->read_wait)) {
1581 wake_up_poll(&tty->read_wait, POLLIN);
1582 do_sleep++;
1584 if (waitqueue_active(&tty->write_wait)) {
1585 wake_up_poll(&tty->write_wait, POLLOUT);
1586 do_sleep++;
1589 if (o_tty_closing) {
1590 if (waitqueue_active(&o_tty->read_wait)) {
1591 wake_up_poll(&o_tty->read_wait, POLLIN);
1592 do_sleep++;
1594 if (waitqueue_active(&o_tty->write_wait)) {
1595 wake_up_poll(&o_tty->write_wait, POLLOUT);
1596 do_sleep++;
1599 if (!do_sleep)
1600 break;
1602 printk(KERN_WARNING "tty_release_dev: %s: read/write wait queue "
1603 "active!\n", tty_name(tty, buf));
1604 mutex_unlock(&tty_mutex);
1605 schedule();
1609 * The closing flags are now consistent with the open counts on
1610 * both sides, and we've completed the last operation that could
1611 * block, so it's safe to proceed with closing.
1613 if (pty_master) {
1614 if (--o_tty->count < 0) {
1615 printk(KERN_WARNING "tty_release_dev: bad pty slave count "
1616 "(%d) for %s\n",
1617 o_tty->count, tty_name(o_tty, buf));
1618 o_tty->count = 0;
1621 if (--tty->count < 0) {
1622 printk(KERN_WARNING "tty_release_dev: bad tty->count (%d) for %s\n",
1623 tty->count, tty_name(tty, buf));
1624 tty->count = 0;
1628 * We've decremented tty->count, so we need to remove this file
1629 * descriptor off the tty->tty_files list; this serves two
1630 * purposes:
1631 * - check_tty_count sees the correct number of file descriptors
1632 * associated with this tty.
1633 * - do_tty_hangup no longer sees this file descriptor as
1634 * something that needs to be handled for hangups.
1636 file_kill(filp);
1637 filp->private_data = NULL;
1640 * Perform some housekeeping before deciding whether to return.
1642 * Set the TTY_CLOSING flag if this was the last open. In the
1643 * case of a pty we may have to wait around for the other side
1644 * to close, and TTY_CLOSING makes sure we can't be reopened.
1646 if (tty_closing)
1647 set_bit(TTY_CLOSING, &tty->flags);
1648 if (o_tty_closing)
1649 set_bit(TTY_CLOSING, &o_tty->flags);
1652 * If _either_ side is closing, make sure there aren't any
1653 * processes that still think tty or o_tty is their controlling
1654 * tty.
1656 if (tty_closing || o_tty_closing) {
1657 read_lock(&tasklist_lock);
1658 session_clear_tty(tty->session);
1659 if (o_tty)
1660 session_clear_tty(o_tty->session);
1661 read_unlock(&tasklist_lock);
1664 mutex_unlock(&tty_mutex);
1666 /* check whether both sides are closing ... */
1667 if (!tty_closing || (o_tty && !o_tty_closing))
1668 return;
1670 #ifdef TTY_DEBUG_HANGUP
1671 printk(KERN_DEBUG "freeing tty structure...");
1672 #endif
1674 * Ask the line discipline code to release its structures
1676 tty_ldisc_release(tty, o_tty);
1678 * The release_tty function takes care of the details of clearing
1679 * the slots and preserving the termios structure.
1681 release_tty(tty, idx);
1683 /* Make this pty number available for reallocation */
1684 if (devpts)
1685 devpts_kill_index(inode, idx);
1689 * __tty_open - open a tty device
1690 * @inode: inode of device file
1691 * @filp: file pointer to tty
1693 * tty_open and tty_release keep up the tty count that contains the
1694 * number of opens done on a tty. We cannot use the inode-count, as
1695 * different inodes might point to the same tty.
1697 * Open-counting is needed for pty masters, as well as for keeping
1698 * track of serial lines: DTR is dropped when the last close happens.
1699 * (This is not done solely through tty->count, now. - Ted 1/27/92)
1701 * The termios state of a pty is reset on first open so that
1702 * settings don't persist across reuse.
1704 * Locking: tty_mutex protects tty, get_tty_driver and tty_init_dev work.
1705 * tty->count should protect the rest.
1706 * ->siglock protects ->signal/->sighand
1709 static int __tty_open(struct inode *inode, struct file *filp)
1711 struct tty_struct *tty = NULL;
1712 int noctty, retval;
1713 struct tty_driver *driver;
1714 int index;
1715 dev_t device = inode->i_rdev;
1716 unsigned saved_flags = filp->f_flags;
1718 nonseekable_open(inode, filp);
1720 retry_open:
1721 noctty = filp->f_flags & O_NOCTTY;
1722 index = -1;
1723 retval = 0;
1725 mutex_lock(&tty_mutex);
1727 if (device == MKDEV(TTYAUX_MAJOR, 0)) {
1728 tty = get_current_tty();
1729 if (!tty) {
1730 mutex_unlock(&tty_mutex);
1731 return -ENXIO;
1733 driver = tty_driver_kref_get(tty->driver);
1734 index = tty->index;
1735 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1736 /* noctty = 1; */
1737 /* FIXME: Should we take a driver reference ? */
1738 tty_kref_put(tty);
1739 goto got_driver;
1741 #ifdef CONFIG_VT
1742 if (device == MKDEV(TTY_MAJOR, 0)) {
1743 extern struct tty_driver *console_driver;
1744 driver = tty_driver_kref_get(console_driver);
1745 index = fg_console;
1746 noctty = 1;
1747 goto got_driver;
1749 #endif
1750 if (device == MKDEV(TTYAUX_MAJOR, 1)) {
1751 struct tty_driver *console_driver = console_device(&index);
1752 if (console_driver) {
1753 driver = tty_driver_kref_get(console_driver);
1754 if (driver) {
1755 /* Don't let /dev/console block */
1756 filp->f_flags |= O_NONBLOCK;
1757 noctty = 1;
1758 goto got_driver;
1761 mutex_unlock(&tty_mutex);
1762 return -ENODEV;
1765 driver = get_tty_driver(device, &index);
1766 if (!driver) {
1767 mutex_unlock(&tty_mutex);
1768 return -ENODEV;
1770 got_driver:
1771 if (!tty) {
1772 /* check whether we're reopening an existing tty */
1773 tty = tty_driver_lookup_tty(driver, inode, index);
1775 if (IS_ERR(tty)) {
1776 mutex_unlock(&tty_mutex);
1777 return PTR_ERR(tty);
1781 if (tty) {
1782 retval = tty_reopen(tty);
1783 if (retval)
1784 tty = ERR_PTR(retval);
1785 } else
1786 tty = tty_init_dev(driver, index, 0);
1788 mutex_unlock(&tty_mutex);
1789 tty_driver_kref_put(driver);
1790 if (IS_ERR(tty))
1791 return PTR_ERR(tty);
1793 filp->private_data = tty;
1794 file_move(filp, &tty->tty_files);
1795 check_tty_count(tty, "tty_open");
1796 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1797 tty->driver->subtype == PTY_TYPE_MASTER)
1798 noctty = 1;
1799 #ifdef TTY_DEBUG_HANGUP
1800 printk(KERN_DEBUG "opening %s...", tty->name);
1801 #endif
1802 if (!retval) {
1803 if (tty->ops->open)
1804 retval = tty->ops->open(tty, filp);
1805 else
1806 retval = -ENODEV;
1808 filp->f_flags = saved_flags;
1810 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) &&
1811 !capable(CAP_SYS_ADMIN))
1812 retval = -EBUSY;
1814 if (retval) {
1815 #ifdef TTY_DEBUG_HANGUP
1816 printk(KERN_DEBUG "error %d in opening %s...", retval,
1817 tty->name);
1818 #endif
1819 tty_release_dev(filp);
1820 if (retval != -ERESTARTSYS)
1821 return retval;
1822 if (signal_pending(current))
1823 return retval;
1824 schedule();
1826 * Need to reset f_op in case a hangup happened.
1828 if (filp->f_op == &hung_up_tty_fops)
1829 filp->f_op = &tty_fops;
1830 goto retry_open;
1833 mutex_lock(&tty_mutex);
1834 spin_lock_irq(&current->sighand->siglock);
1835 if (!noctty &&
1836 current->signal->leader &&
1837 !current->signal->tty &&
1838 tty->session == NULL)
1839 __proc_set_tty(current, tty);
1840 spin_unlock_irq(&current->sighand->siglock);
1841 mutex_unlock(&tty_mutex);
1842 return 0;
1845 /* BKL pushdown: scary code avoidance wrapper */
1846 static int tty_open(struct inode *inode, struct file *filp)
1848 int ret;
1850 lock_kernel();
1851 ret = __tty_open(inode, filp);
1852 unlock_kernel();
1853 return ret;
1860 * tty_release - vfs callback for close
1861 * @inode: inode of tty
1862 * @filp: file pointer for handle to tty
1864 * Called the last time each file handle is closed that references
1865 * this tty. There may however be several such references.
1867 * Locking:
1868 * Takes bkl. See tty_release_dev
1871 static int tty_release(struct inode *inode, struct file *filp)
1873 lock_kernel();
1874 tty_release_dev(filp);
1875 unlock_kernel();
1876 return 0;
1880 * tty_poll - check tty status
1881 * @filp: file being polled
1882 * @wait: poll wait structures to update
1884 * Call the line discipline polling method to obtain the poll
1885 * status of the device.
1887 * Locking: locks called line discipline but ldisc poll method
1888 * may be re-entered freely by other callers.
1891 static unsigned int tty_poll(struct file *filp, poll_table *wait)
1893 struct tty_struct *tty;
1894 struct tty_ldisc *ld;
1895 int ret = 0;
1897 tty = (struct tty_struct *)filp->private_data;
1898 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_poll"))
1899 return 0;
1901 ld = tty_ldisc_ref_wait(tty);
1902 if (ld->ops->poll)
1903 ret = (ld->ops->poll)(tty, filp, wait);
1904 tty_ldisc_deref(ld);
1905 return ret;
1908 static int tty_fasync(int fd, struct file *filp, int on)
1910 struct tty_struct *tty;
1911 unsigned long flags;
1912 int retval = 0;
1914 lock_kernel();
1915 tty = (struct tty_struct *)filp->private_data;
1916 if (tty_paranoia_check(tty, filp->f_path.dentry->d_inode, "tty_fasync"))
1917 goto out;
1919 retval = fasync_helper(fd, filp, on, &tty->fasync);
1920 if (retval <= 0)
1921 goto out;
1923 if (on) {
1924 enum pid_type type;
1925 struct pid *pid;
1926 if (!waitqueue_active(&tty->read_wait))
1927 tty->minimum_to_wake = 1;
1928 spin_lock_irqsave(&tty->ctrl_lock, flags);
1929 if (tty->pgrp) {
1930 pid = tty->pgrp;
1931 type = PIDTYPE_PGID;
1932 } else {
1933 pid = task_pid(current);
1934 type = PIDTYPE_PID;
1936 get_pid(pid);
1937 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
1938 retval = __f_setown(filp, pid, type, 0);
1939 put_pid(pid);
1940 if (retval)
1941 goto out;
1942 } else {
1943 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
1944 tty->minimum_to_wake = N_TTY_BUF_SIZE;
1946 retval = 0;
1947 out:
1948 unlock_kernel();
1949 return retval;
1953 * tiocsti - fake input character
1954 * @tty: tty to fake input into
1955 * @p: pointer to character
1957 * Fake input to a tty device. Does the necessary locking and
1958 * input management.
1960 * FIXME: does not honour flow control ??
1962 * Locking:
1963 * Called functions take tty_ldisc_lock
1964 * current->signal->tty check is safe without locks
1966 * FIXME: may race normal receive processing
1969 static int tiocsti(struct tty_struct *tty, char __user *p)
1971 char ch, mbz = 0;
1972 struct tty_ldisc *ld;
1974 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
1975 return -EPERM;
1976 if (get_user(ch, p))
1977 return -EFAULT;
1978 tty_audit_tiocsti(tty, ch);
1979 ld = tty_ldisc_ref_wait(tty);
1980 ld->ops->receive_buf(tty, &ch, &mbz, 1);
1981 tty_ldisc_deref(ld);
1982 return 0;
1986 * tiocgwinsz - implement window query ioctl
1987 * @tty; tty
1988 * @arg: user buffer for result
1990 * Copies the kernel idea of the window size into the user buffer.
1992 * Locking: tty->termios_mutex is taken to ensure the winsize data
1993 * is consistent.
1996 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
1998 int err;
2000 mutex_lock(&tty->termios_mutex);
2001 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
2002 mutex_unlock(&tty->termios_mutex);
2004 return err ? -EFAULT: 0;
2008 * tty_do_resize - resize event
2009 * @tty: tty being resized
2010 * @rows: rows (character)
2011 * @cols: cols (character)
2013 * Update the termios variables and send the neccessary signals to
2014 * peform a terminal resize correctly
2017 int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
2019 struct pid *pgrp;
2020 unsigned long flags;
2022 /* Lock the tty */
2023 mutex_lock(&tty->termios_mutex);
2024 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
2025 goto done;
2026 /* Get the PID values and reference them so we can
2027 avoid holding the tty ctrl lock while sending signals */
2028 spin_lock_irqsave(&tty->ctrl_lock, flags);
2029 pgrp = get_pid(tty->pgrp);
2030 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2032 if (pgrp)
2033 kill_pgrp(pgrp, SIGWINCH, 1);
2034 put_pid(pgrp);
2036 tty->winsize = *ws;
2037 done:
2038 mutex_unlock(&tty->termios_mutex);
2039 return 0;
2043 * tiocswinsz - implement window size set ioctl
2044 * @tty; tty side of tty
2045 * @arg: user buffer for result
2047 * Copies the user idea of the window size to the kernel. Traditionally
2048 * this is just advisory information but for the Linux console it
2049 * actually has driver level meaning and triggers a VC resize.
2051 * Locking:
2052 * Driver dependant. The default do_resize method takes the
2053 * tty termios mutex and ctrl_lock. The console takes its own lock
2054 * then calls into the default method.
2057 static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
2059 struct winsize tmp_ws;
2060 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2061 return -EFAULT;
2063 if (tty->ops->resize)
2064 return tty->ops->resize(tty, &tmp_ws);
2065 else
2066 return tty_do_resize(tty, &tmp_ws);
2070 * tioccons - allow admin to move logical console
2071 * @file: the file to become console
2073 * Allow the adminstrator to move the redirected console device
2075 * Locking: uses redirect_lock to guard the redirect information
2078 static int tioccons(struct file *file)
2080 if (!capable(CAP_SYS_ADMIN))
2081 return -EPERM;
2082 if (file->f_op->write == redirected_tty_write) {
2083 struct file *f;
2084 spin_lock(&redirect_lock);
2085 f = redirect;
2086 redirect = NULL;
2087 spin_unlock(&redirect_lock);
2088 if (f)
2089 fput(f);
2090 return 0;
2092 spin_lock(&redirect_lock);
2093 if (redirect) {
2094 spin_unlock(&redirect_lock);
2095 return -EBUSY;
2097 get_file(file);
2098 redirect = file;
2099 spin_unlock(&redirect_lock);
2100 return 0;
2104 * fionbio - non blocking ioctl
2105 * @file: file to set blocking value
2106 * @p: user parameter
2108 * Historical tty interfaces had a blocking control ioctl before
2109 * the generic functionality existed. This piece of history is preserved
2110 * in the expected tty API of posix OS's.
2112 * Locking: none, the open file handle ensures it won't go away.
2115 static int fionbio(struct file *file, int __user *p)
2117 int nonblock;
2119 if (get_user(nonblock, p))
2120 return -EFAULT;
2122 spin_lock(&file->f_lock);
2123 if (nonblock)
2124 file->f_flags |= O_NONBLOCK;
2125 else
2126 file->f_flags &= ~O_NONBLOCK;
2127 spin_unlock(&file->f_lock);
2128 return 0;
2132 * tiocsctty - set controlling tty
2133 * @tty: tty structure
2134 * @arg: user argument
2136 * This ioctl is used to manage job control. It permits a session
2137 * leader to set this tty as the controlling tty for the session.
2139 * Locking:
2140 * Takes tty_mutex() to protect tty instance
2141 * Takes tasklist_lock internally to walk sessions
2142 * Takes ->siglock() when updating signal->tty
2145 static int tiocsctty(struct tty_struct *tty, int arg)
2147 int ret = 0;
2148 if (current->signal->leader && (task_session(current) == tty->session))
2149 return ret;
2151 mutex_lock(&tty_mutex);
2153 * The process must be a session leader and
2154 * not have a controlling tty already.
2156 if (!current->signal->leader || current->signal->tty) {
2157 ret = -EPERM;
2158 goto unlock;
2161 if (tty->session) {
2163 * This tty is already the controlling
2164 * tty for another session group!
2166 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
2168 * Steal it away
2170 read_lock(&tasklist_lock);
2171 session_clear_tty(tty->session);
2172 read_unlock(&tasklist_lock);
2173 } else {
2174 ret = -EPERM;
2175 goto unlock;
2178 proc_set_tty(current, tty);
2179 unlock:
2180 mutex_unlock(&tty_mutex);
2181 return ret;
2185 * tty_get_pgrp - return a ref counted pgrp pid
2186 * @tty: tty to read
2188 * Returns a refcounted instance of the pid struct for the process
2189 * group controlling the tty.
2192 struct pid *tty_get_pgrp(struct tty_struct *tty)
2194 unsigned long flags;
2195 struct pid *pgrp;
2197 spin_lock_irqsave(&tty->ctrl_lock, flags);
2198 pgrp = get_pid(tty->pgrp);
2199 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2201 return pgrp;
2203 EXPORT_SYMBOL_GPL(tty_get_pgrp);
2206 * tiocgpgrp - get process group
2207 * @tty: tty passed by user
2208 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2209 * @p: returned pid
2211 * Obtain the process group of the tty. If there is no process group
2212 * return an error.
2214 * Locking: none. Reference to current->signal->tty is safe.
2217 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2219 struct pid *pid;
2220 int ret;
2222 * (tty == real_tty) is a cheap way of
2223 * testing if the tty is NOT a master pty.
2225 if (tty == real_tty && current->signal->tty != real_tty)
2226 return -ENOTTY;
2227 pid = tty_get_pgrp(real_tty);
2228 ret = put_user(pid_vnr(pid), p);
2229 put_pid(pid);
2230 return ret;
2234 * tiocspgrp - attempt to set process group
2235 * @tty: tty passed by user
2236 * @real_tty: tty side device matching tty passed by user
2237 * @p: pid pointer
2239 * Set the process group of the tty to the session passed. Only
2240 * permitted where the tty session is our session.
2242 * Locking: RCU, ctrl lock
2245 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2247 struct pid *pgrp;
2248 pid_t pgrp_nr;
2249 int retval = tty_check_change(real_tty);
2250 unsigned long flags;
2252 if (retval == -EIO)
2253 return -ENOTTY;
2254 if (retval)
2255 return retval;
2256 if (!current->signal->tty ||
2257 (current->signal->tty != real_tty) ||
2258 (real_tty->session != task_session(current)))
2259 return -ENOTTY;
2260 if (get_user(pgrp_nr, p))
2261 return -EFAULT;
2262 if (pgrp_nr < 0)
2263 return -EINVAL;
2264 rcu_read_lock();
2265 pgrp = find_vpid(pgrp_nr);
2266 retval = -ESRCH;
2267 if (!pgrp)
2268 goto out_unlock;
2269 retval = -EPERM;
2270 if (session_of_pgrp(pgrp) != task_session(current))
2271 goto out_unlock;
2272 retval = 0;
2273 spin_lock_irqsave(&tty->ctrl_lock, flags);
2274 put_pid(real_tty->pgrp);
2275 real_tty->pgrp = get_pid(pgrp);
2276 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2277 out_unlock:
2278 rcu_read_unlock();
2279 return retval;
2283 * tiocgsid - get session id
2284 * @tty: tty passed by user
2285 * @real_tty: tty side of the tty pased by the user if a pty else the tty
2286 * @p: pointer to returned session id
2288 * Obtain the session id of the tty. If there is no session
2289 * return an error.
2291 * Locking: none. Reference to current->signal->tty is safe.
2294 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2297 * (tty == real_tty) is a cheap way of
2298 * testing if the tty is NOT a master pty.
2300 if (tty == real_tty && current->signal->tty != real_tty)
2301 return -ENOTTY;
2302 if (!real_tty->session)
2303 return -ENOTTY;
2304 return put_user(pid_vnr(real_tty->session), p);
2308 * tiocsetd - set line discipline
2309 * @tty: tty device
2310 * @p: pointer to user data
2312 * Set the line discipline according to user request.
2314 * Locking: see tty_set_ldisc, this function is just a helper
2317 static int tiocsetd(struct tty_struct *tty, int __user *p)
2319 int ldisc;
2320 int ret;
2322 if (get_user(ldisc, p))
2323 return -EFAULT;
2325 lock_kernel();
2326 ret = tty_set_ldisc(tty, ldisc);
2327 unlock_kernel();
2329 return ret;
2333 * send_break - performed time break
2334 * @tty: device to break on
2335 * @duration: timeout in mS
2337 * Perform a timed break on hardware that lacks its own driver level
2338 * timed break functionality.
2340 * Locking:
2341 * atomic_write_lock serializes
2345 static int send_break(struct tty_struct *tty, unsigned int duration)
2347 int retval;
2349 if (tty->ops->break_ctl == NULL)
2350 return 0;
2352 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2353 retval = tty->ops->break_ctl(tty, duration);
2354 else {
2355 /* Do the work ourselves */
2356 if (tty_write_lock(tty, 0) < 0)
2357 return -EINTR;
2358 retval = tty->ops->break_ctl(tty, -1);
2359 if (retval)
2360 goto out;
2361 if (!signal_pending(current))
2362 msleep_interruptible(duration);
2363 retval = tty->ops->break_ctl(tty, 0);
2364 out:
2365 tty_write_unlock(tty);
2366 if (signal_pending(current))
2367 retval = -EINTR;
2369 return retval;
2373 * tty_tiocmget - get modem status
2374 * @tty: tty device
2375 * @file: user file pointer
2376 * @p: pointer to result
2378 * Obtain the modem status bits from the tty driver if the feature
2379 * is supported. Return -EINVAL if it is not available.
2381 * Locking: none (up to the driver)
2384 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
2386 int retval = -EINVAL;
2388 if (tty->ops->tiocmget) {
2389 retval = tty->ops->tiocmget(tty, file);
2391 if (retval >= 0)
2392 retval = put_user(retval, p);
2394 return retval;
2398 * tty_tiocmset - set modem status
2399 * @tty: tty device
2400 * @file: user file pointer
2401 * @cmd: command - clear bits, set bits or set all
2402 * @p: pointer to desired bits
2404 * Set the modem status bits from the tty driver if the feature
2405 * is supported. Return -EINVAL if it is not available.
2407 * Locking: none (up to the driver)
2410 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
2411 unsigned __user *p)
2413 int retval;
2414 unsigned int set, clear, val;
2416 if (tty->ops->tiocmset == NULL)
2417 return -EINVAL;
2419 retval = get_user(val, p);
2420 if (retval)
2421 return retval;
2422 set = clear = 0;
2423 switch (cmd) {
2424 case TIOCMBIS:
2425 set = val;
2426 break;
2427 case TIOCMBIC:
2428 clear = val;
2429 break;
2430 case TIOCMSET:
2431 set = val;
2432 clear = ~val;
2433 break;
2435 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2436 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2437 return tty->ops->tiocmset(tty, file, set, clear);
2440 static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2442 int retval = -EINVAL;
2443 struct serial_icounter_struct icount;
2444 memset(&icount, 0, sizeof(icount));
2445 if (tty->ops->get_icount)
2446 retval = tty->ops->get_icount(tty, &icount);
2447 if (retval != 0)
2448 return retval;
2449 if (copy_to_user(arg, &icount, sizeof(icount)))
2450 return -EFAULT;
2451 return 0;
2454 struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
2456 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2457 tty->driver->subtype == PTY_TYPE_MASTER)
2458 tty = tty->link;
2459 return tty;
2461 EXPORT_SYMBOL(tty_pair_get_tty);
2463 struct tty_struct *tty_pair_get_pty(struct tty_struct *tty)
2465 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2466 tty->driver->subtype == PTY_TYPE_MASTER)
2467 return tty;
2468 return tty->link;
2470 EXPORT_SYMBOL(tty_pair_get_pty);
2473 * Split this up, as gcc can choke on it otherwise..
2475 long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2477 struct tty_struct *tty, *real_tty;
2478 void __user *p = (void __user *)arg;
2479 int retval;
2480 struct tty_ldisc *ld;
2481 struct inode *inode = file->f_dentry->d_inode;
2483 tty = (struct tty_struct *)file->private_data;
2484 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2485 return -EINVAL;
2487 real_tty = tty_pair_get_tty(tty);
2490 * Factor out some common prep work
2492 switch (cmd) {
2493 case TIOCSETD:
2494 case TIOCSBRK:
2495 case TIOCCBRK:
2496 case TCSBRK:
2497 case TCSBRKP:
2498 retval = tty_check_change(tty);
2499 if (retval)
2500 return retval;
2501 if (cmd != TIOCCBRK) {
2502 tty_wait_until_sent(tty, 0);
2503 if (signal_pending(current))
2504 return -EINTR;
2506 break;
2510 * Now do the stuff.
2512 switch (cmd) {
2513 case TIOCSTI:
2514 return tiocsti(tty, p);
2515 case TIOCGWINSZ:
2516 return tiocgwinsz(real_tty, p);
2517 case TIOCSWINSZ:
2518 return tiocswinsz(real_tty, p);
2519 case TIOCCONS:
2520 return real_tty != tty ? -EINVAL : tioccons(file);
2521 case FIONBIO:
2522 return fionbio(file, p);
2523 case TIOCEXCL:
2524 set_bit(TTY_EXCLUSIVE, &tty->flags);
2525 return 0;
2526 case TIOCNXCL:
2527 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2528 return 0;
2529 case TIOCNOTTY:
2530 if (current->signal->tty != tty)
2531 return -ENOTTY;
2532 no_tty();
2533 return 0;
2534 case TIOCSCTTY:
2535 return tiocsctty(tty, arg);
2536 case TIOCGPGRP:
2537 return tiocgpgrp(tty, real_tty, p);
2538 case TIOCSPGRP:
2539 return tiocspgrp(tty, real_tty, p);
2540 case TIOCGSID:
2541 return tiocgsid(tty, real_tty, p);
2542 case TIOCGETD:
2543 return put_user(tty->ldisc->ops->num, (int __user *)p);
2544 case TIOCSETD:
2545 return tiocsetd(tty, p);
2547 * Break handling
2549 case TIOCSBRK: /* Turn break on, unconditionally */
2550 if (tty->ops->break_ctl)
2551 return tty->ops->break_ctl(tty, -1);
2552 return 0;
2553 case TIOCCBRK: /* Turn break off, unconditionally */
2554 if (tty->ops->break_ctl)
2555 return tty->ops->break_ctl(tty, 0);
2556 return 0;
2557 case TCSBRK: /* SVID version: non-zero arg --> no break */
2558 /* non-zero arg means wait for all output data
2559 * to be sent (performed above) but don't send break.
2560 * This is used by the tcdrain() termios function.
2562 if (!arg)
2563 return send_break(tty, 250);
2564 return 0;
2565 case TCSBRKP: /* support for POSIX tcsendbreak() */
2566 return send_break(tty, arg ? arg*100 : 250);
2568 case TIOCMGET:
2569 return tty_tiocmget(tty, file, p);
2570 case TIOCMSET:
2571 case TIOCMBIC:
2572 case TIOCMBIS:
2573 return tty_tiocmset(tty, file, cmd, p);
2574 case TIOCGICOUNT:
2575 retval = tty_tiocgicount(tty, p);
2576 /* For the moment allow fall through to the old method */
2577 if (retval != -EINVAL)
2578 return retval;
2579 break;
2580 case TCFLSH:
2581 switch (arg) {
2582 case TCIFLUSH:
2583 case TCIOFLUSH:
2584 /* flush tty buffer and allow ldisc to process ioctl */
2585 tty_buffer_flush(tty);
2586 break;
2588 break;
2590 if (tty->ops->ioctl) {
2591 retval = (tty->ops->ioctl)(tty, file, cmd, arg);
2592 if (retval != -ENOIOCTLCMD)
2593 return retval;
2595 ld = tty_ldisc_ref_wait(tty);
2596 retval = -EINVAL;
2597 if (ld->ops->ioctl) {
2598 retval = ld->ops->ioctl(tty, file, cmd, arg);
2599 if (retval == -ENOIOCTLCMD)
2600 retval = -EINVAL;
2602 tty_ldisc_deref(ld);
2603 return retval;
2606 #ifdef CONFIG_COMPAT
2607 static long tty_compat_ioctl(struct file *file, unsigned int cmd,
2608 unsigned long arg)
2610 struct inode *inode = file->f_dentry->d_inode;
2611 struct tty_struct *tty = file->private_data;
2612 struct tty_ldisc *ld;
2613 int retval = -ENOIOCTLCMD;
2615 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
2616 return -EINVAL;
2618 if (tty->ops->compat_ioctl) {
2619 retval = (tty->ops->compat_ioctl)(tty, file, cmd, arg);
2620 if (retval != -ENOIOCTLCMD)
2621 return retval;
2624 ld = tty_ldisc_ref_wait(tty);
2625 if (ld->ops->compat_ioctl)
2626 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
2627 tty_ldisc_deref(ld);
2629 return retval;
2631 #endif
2634 * This implements the "Secure Attention Key" --- the idea is to
2635 * prevent trojan horses by killing all processes associated with this
2636 * tty when the user hits the "Secure Attention Key". Required for
2637 * super-paranoid applications --- see the Orange Book for more details.
2639 * This code could be nicer; ideally it should send a HUP, wait a few
2640 * seconds, then send a INT, and then a KILL signal. But you then
2641 * have to coordinate with the init process, since all processes associated
2642 * with the current tty must be dead before the new getty is allowed
2643 * to spawn.
2645 * Now, if it would be correct ;-/ The current code has a nasty hole -
2646 * it doesn't catch files in flight. We may send the descriptor to ourselves
2647 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
2649 * Nasty bug: do_SAK is being called in interrupt context. This can
2650 * deadlock. We punt it up to process context. AKPM - 16Mar2001
2652 void __do_SAK(struct tty_struct *tty)
2654 #ifdef TTY_SOFT_SAK
2655 tty_hangup(tty);
2656 #else
2657 struct task_struct *g, *p;
2658 struct pid *session;
2659 int i;
2660 struct file *filp;
2661 struct fdtable *fdt;
2663 if (!tty)
2664 return;
2665 session = tty->session;
2667 tty_ldisc_flush(tty);
2669 tty_driver_flush_buffer(tty);
2671 read_lock(&tasklist_lock);
2672 /* Kill the entire session */
2673 do_each_pid_task(session, PIDTYPE_SID, p) {
2674 printk(KERN_NOTICE "SAK: killed process %d"
2675 " (%s): task_session(p)==tty->session\n",
2676 task_pid_nr(p), p->comm);
2677 send_sig(SIGKILL, p, 1);
2678 } while_each_pid_task(session, PIDTYPE_SID, p);
2679 /* Now kill any processes that happen to have the
2680 * tty open.
2682 do_each_thread(g, p) {
2683 if (p->signal->tty == tty) {
2684 printk(KERN_NOTICE "SAK: killed process %d"
2685 " (%s): task_session(p)==tty->session\n",
2686 task_pid_nr(p), p->comm);
2687 send_sig(SIGKILL, p, 1);
2688 continue;
2690 task_lock(p);
2691 if (p->files) {
2693 * We don't take a ref to the file, so we must
2694 * hold ->file_lock instead.
2696 spin_lock(&p->files->file_lock);
2697 fdt = files_fdtable(p->files);
2698 for (i = 0; i < fdt->max_fds; i++) {
2699 filp = fcheck_files(p->files, i);
2700 if (!filp)
2701 continue;
2702 if (filp->f_op->read == tty_read &&
2703 filp->private_data == tty) {
2704 printk(KERN_NOTICE "SAK: killed process %d"
2705 " (%s): fd#%d opened to the tty\n",
2706 task_pid_nr(p), p->comm, i);
2707 force_sig(SIGKILL, p);
2708 break;
2711 spin_unlock(&p->files->file_lock);
2713 task_unlock(p);
2714 } while_each_thread(g, p);
2715 read_unlock(&tasklist_lock);
2716 #endif
2719 static void do_SAK_work(struct work_struct *work)
2721 struct tty_struct *tty =
2722 container_of(work, struct tty_struct, SAK_work);
2723 __do_SAK(tty);
2727 * The tq handling here is a little racy - tty->SAK_work may already be queued.
2728 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
2729 * the values which we write to it will be identical to the values which it
2730 * already has. --akpm
2732 void do_SAK(struct tty_struct *tty)
2734 if (!tty)
2735 return;
2736 schedule_work(&tty->SAK_work);
2739 EXPORT_SYMBOL(do_SAK);
2742 * initialize_tty_struct
2743 * @tty: tty to initialize
2745 * This subroutine initializes a tty structure that has been newly
2746 * allocated.
2748 * Locking: none - tty in question must not be exposed at this point
2751 void initialize_tty_struct(struct tty_struct *tty,
2752 struct tty_driver *driver, int idx)
2754 memset(tty, 0, sizeof(struct tty_struct));
2755 kref_init(&tty->kref);
2756 tty->magic = TTY_MAGIC;
2757 tty_ldisc_init(tty);
2758 tty->session = NULL;
2759 tty->pgrp = NULL;
2760 tty->overrun_time = jiffies;
2761 tty->buf.head = tty->buf.tail = NULL;
2762 tty_buffer_init(tty);
2763 mutex_init(&tty->termios_mutex);
2764 mutex_init(&tty->ldisc_mutex);
2765 init_waitqueue_head(&tty->write_wait);
2766 init_waitqueue_head(&tty->read_wait);
2767 INIT_WORK(&tty->hangup_work, do_tty_hangup);
2768 mutex_init(&tty->atomic_read_lock);
2769 mutex_init(&tty->atomic_write_lock);
2770 mutex_init(&tty->output_lock);
2771 mutex_init(&tty->echo_lock);
2772 spin_lock_init(&tty->read_lock);
2773 spin_lock_init(&tty->ctrl_lock);
2774 INIT_LIST_HEAD(&tty->tty_files);
2775 INIT_WORK(&tty->SAK_work, do_SAK_work);
2777 tty->driver = driver;
2778 tty->ops = driver->ops;
2779 tty->index = idx;
2780 tty_line_name(driver, idx, tty->name);
2784 * tty_put_char - write one character to a tty
2785 * @tty: tty
2786 * @ch: character
2788 * Write one byte to the tty using the provided put_char method
2789 * if present. Returns the number of characters successfully output.
2791 * Note: the specific put_char operation in the driver layer may go
2792 * away soon. Don't call it directly, use this method
2795 int tty_put_char(struct tty_struct *tty, unsigned char ch)
2797 if (tty->ops->put_char)
2798 return tty->ops->put_char(tty, ch);
2799 return tty->ops->write(tty, &ch, 1);
2801 EXPORT_SYMBOL_GPL(tty_put_char);
2803 struct class *tty_class;
2806 * tty_register_device - register a tty device
2807 * @driver: the tty driver that describes the tty device
2808 * @index: the index in the tty driver for this tty device
2809 * @device: a struct device that is associated with this tty device.
2810 * This field is optional, if there is no known struct device
2811 * for this tty device it can be set to NULL safely.
2813 * Returns a pointer to the struct device for this tty device
2814 * (or ERR_PTR(-EFOO) on error).
2816 * This call is required to be made to register an individual tty device
2817 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
2818 * that bit is not set, this function should not be called by a tty
2819 * driver.
2821 * Locking: ??
2824 struct device *tty_register_device(struct tty_driver *driver, unsigned index,
2825 struct device *device)
2827 char name[64];
2828 dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
2830 if (index >= driver->num) {
2831 printk(KERN_ERR "Attempt to register invalid tty line number "
2832 " (%d).\n", index);
2833 return ERR_PTR(-EINVAL);
2836 if (driver->type == TTY_DRIVER_TYPE_PTY)
2837 pty_line_name(driver, index, name);
2838 else
2839 tty_line_name(driver, index, name);
2841 return device_create(tty_class, device, dev, NULL, name);
2843 EXPORT_SYMBOL(tty_register_device);
2846 * tty_unregister_device - unregister a tty device
2847 * @driver: the tty driver that describes the tty device
2848 * @index: the index in the tty driver for this tty device
2850 * If a tty device is registered with a call to tty_register_device() then
2851 * this function must be called when the tty device is gone.
2853 * Locking: ??
2856 void tty_unregister_device(struct tty_driver *driver, unsigned index)
2858 device_destroy(tty_class,
2859 MKDEV(driver->major, driver->minor_start) + index);
2861 EXPORT_SYMBOL(tty_unregister_device);
2863 struct tty_driver *alloc_tty_driver(int lines)
2865 struct tty_driver *driver;
2867 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
2868 if (driver) {
2869 kref_init(&driver->kref);
2870 driver->magic = TTY_DRIVER_MAGIC;
2871 driver->num = lines;
2872 /* later we'll move allocation of tables here */
2874 return driver;
2876 EXPORT_SYMBOL(alloc_tty_driver);
2878 static void destruct_tty_driver(struct kref *kref)
2880 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
2881 int i;
2882 struct ktermios *tp;
2883 void *p;
2885 if (driver->flags & TTY_DRIVER_INSTALLED) {
2887 * Free the termios and termios_locked structures because
2888 * we don't want to get memory leaks when modular tty
2889 * drivers are removed from the kernel.
2891 for (i = 0; i < driver->num; i++) {
2892 tp = driver->termios[i];
2893 if (tp) {
2894 driver->termios[i] = NULL;
2895 kfree(tp);
2897 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
2898 tty_unregister_device(driver, i);
2900 p = driver->ttys;
2901 proc_tty_unregister_driver(driver);
2902 driver->ttys = NULL;
2903 driver->termios = NULL;
2904 kfree(p);
2905 cdev_del(&driver->cdev);
2907 kfree(driver);
2910 void tty_driver_kref_put(struct tty_driver *driver)
2912 kref_put(&driver->kref, destruct_tty_driver);
2914 EXPORT_SYMBOL(tty_driver_kref_put);
2916 void tty_set_operations(struct tty_driver *driver,
2917 const struct tty_operations *op)
2919 driver->ops = op;
2921 EXPORT_SYMBOL(tty_set_operations);
2923 void put_tty_driver(struct tty_driver *d)
2925 tty_driver_kref_put(d);
2927 EXPORT_SYMBOL(put_tty_driver);
2930 * Called by a tty driver to register itself.
2932 int tty_register_driver(struct tty_driver *driver)
2934 int error;
2935 int i;
2936 dev_t dev;
2937 void **p = NULL;
2939 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM) && driver->num) {
2940 p = kzalloc(driver->num * 2 * sizeof(void *), GFP_KERNEL);
2941 if (!p)
2942 return -ENOMEM;
2945 if (!driver->major) {
2946 error = alloc_chrdev_region(&dev, driver->minor_start,
2947 driver->num, driver->name);
2948 if (!error) {
2949 driver->major = MAJOR(dev);
2950 driver->minor_start = MINOR(dev);
2952 } else {
2953 dev = MKDEV(driver->major, driver->minor_start);
2954 error = register_chrdev_region(dev, driver->num, driver->name);
2956 if (error < 0) {
2957 kfree(p);
2958 return error;
2961 if (p) {
2962 driver->ttys = (struct tty_struct **)p;
2963 driver->termios = (struct ktermios **)(p + driver->num);
2964 } else {
2965 driver->ttys = NULL;
2966 driver->termios = NULL;
2969 cdev_init(&driver->cdev, &tty_fops);
2970 driver->cdev.owner = driver->owner;
2971 error = cdev_add(&driver->cdev, dev, driver->num);
2972 if (error) {
2973 unregister_chrdev_region(dev, driver->num);
2974 driver->ttys = NULL;
2975 driver->termios = NULL;
2976 kfree(p);
2977 return error;
2980 mutex_lock(&tty_mutex);
2981 list_add(&driver->tty_drivers, &tty_drivers);
2982 mutex_unlock(&tty_mutex);
2984 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
2985 for (i = 0; i < driver->num; i++)
2986 tty_register_device(driver, i, NULL);
2988 proc_tty_register_driver(driver);
2989 driver->flags |= TTY_DRIVER_INSTALLED;
2990 return 0;
2993 EXPORT_SYMBOL(tty_register_driver);
2996 * Called by a tty driver to unregister itself.
2998 int tty_unregister_driver(struct tty_driver *driver)
3000 #if 0
3001 /* FIXME */
3002 if (driver->refcount)
3003 return -EBUSY;
3004 #endif
3005 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3006 driver->num);
3007 mutex_lock(&tty_mutex);
3008 list_del(&driver->tty_drivers);
3009 mutex_unlock(&tty_mutex);
3010 return 0;
3013 EXPORT_SYMBOL(tty_unregister_driver);
3015 dev_t tty_devnum(struct tty_struct *tty)
3017 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3019 EXPORT_SYMBOL(tty_devnum);
3021 void proc_clear_tty(struct task_struct *p)
3023 unsigned long flags;
3024 struct tty_struct *tty;
3025 spin_lock_irqsave(&p->sighand->siglock, flags);
3026 tty = p->signal->tty;
3027 p->signal->tty = NULL;
3028 spin_unlock_irqrestore(&p->sighand->siglock, flags);
3029 tty_kref_put(tty);
3032 /* Called under the sighand lock */
3034 static void __proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3036 if (tty) {
3037 unsigned long flags;
3038 /* We should not have a session or pgrp to put here but.... */
3039 spin_lock_irqsave(&tty->ctrl_lock, flags);
3040 put_pid(tty->session);
3041 put_pid(tty->pgrp);
3042 tty->pgrp = get_pid(task_pgrp(tsk));
3043 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
3044 tty->session = get_pid(task_session(tsk));
3045 if (tsk->signal->tty) {
3046 printk(KERN_DEBUG "tty not NULL!!\n");
3047 tty_kref_put(tsk->signal->tty);
3050 put_pid(tsk->signal->tty_old_pgrp);
3051 tsk->signal->tty = tty_kref_get(tty);
3052 tsk->signal->tty_old_pgrp = NULL;
3055 static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
3057 spin_lock_irq(&tsk->sighand->siglock);
3058 __proc_set_tty(tsk, tty);
3059 spin_unlock_irq(&tsk->sighand->siglock);
3062 struct tty_struct *get_current_tty(void)
3064 struct tty_struct *tty;
3065 unsigned long flags;
3067 spin_lock_irqsave(&current->sighand->siglock, flags);
3068 tty = tty_kref_get(current->signal->tty);
3069 spin_unlock_irqrestore(&current->sighand->siglock, flags);
3070 return tty;
3072 EXPORT_SYMBOL_GPL(get_current_tty);
3074 void tty_default_fops(struct file_operations *fops)
3076 *fops = tty_fops;
3080 * Initialize the console device. This is called *early*, so
3081 * we can't necessarily depend on lots of kernel help here.
3082 * Just do some early initializations, and do the complex setup
3083 * later.
3085 void __init console_init(void)
3087 initcall_t *call;
3089 /* Setup the default TTY line discipline. */
3090 tty_ldisc_begin();
3093 * set up the console device so that later boot sequences can
3094 * inform about problems etc..
3096 call = __con_initcall_start;
3097 while (call < __con_initcall_end) {
3098 (*call)();
3099 call++;
3103 static char *tty_devnode(struct device *dev, mode_t *mode)
3105 if (!mode)
3106 return NULL;
3107 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3108 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3109 *mode = 0666;
3110 return NULL;
3113 static int __init tty_class_init(void)
3115 tty_class = class_create(THIS_MODULE, "tty");
3116 if (IS_ERR(tty_class))
3117 return PTR_ERR(tty_class);
3118 tty_class->devnode = tty_devnode;
3119 return 0;
3122 postcore_initcall(tty_class_init);
3124 /* 3/2004 jmc: why do these devices exist? */
3126 static struct cdev tty_cdev, console_cdev;
3129 * Ok, now we can initialize the rest of the tty devices and can count
3130 * on memory allocations, interrupts etc..
3132 static int __init tty_init(void)
3134 cdev_init(&tty_cdev, &tty_fops);
3135 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3136 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3137 panic("Couldn't register /dev/tty driver\n");
3138 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL,
3139 "tty");
3141 cdev_init(&console_cdev, &console_fops);
3142 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3143 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3144 panic("Couldn't register /dev/console driver\n");
3145 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 1), NULL,
3146 "console");
3148 #ifdef CONFIG_VT
3149 vty_init(&console_fops);
3150 #endif
3151 return 0;
3153 module_init(tty_init);