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