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