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