blob: 3c90ad44b585c7e1ba998c70bf504a99684dd59c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
7 * or rs-channels. It also implements echoing, cooked mode etc.
8 *
9 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
10 *
11 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
12 * tty_struct and tty_queue structures. Previously there was an array
13 * of 256 tty_struct's which was statically allocated, and the
14 * tty_queue structures were allocated at boot time. Both are now
15 * dynamically allocated only when the tty is open.
16 *
17 * Also restructured routines so that there is more of a separation
18 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
19 * the low-level tty routines (serial.c, pty.c, console.c). This
Alan Cox37bdfb02008-02-08 04:18:47 -080020 * makes for cleaner and more compact code. -TYT, 9/17/92
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
22 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
23 * which can be dynamically activated and de-activated by the line
24 * discipline handling modules (like SLIP).
25 *
26 * NOTE: pay no attention to the line discipline code (yet); its
27 * interface is still subject to change in this version...
28 * -- TYT, 1/31/92
29 *
30 * Added functionality to the OPOST tty handling. No delays, but all
31 * other bits should be there.
32 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
33 *
34 * Rewrote canonical mode and added more termios flags.
35 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
36 *
37 * Reorganized FASYNC support so mouse code can share it.
38 * -- ctm@ardi.com, 9Sep95
39 *
40 * New TIOCLINUX variants added.
41 * -- mj@k332.feld.cvut.cz, 19-Nov-95
Alan Cox37bdfb02008-02-08 04:18:47 -080042 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * Restrict vt switching via ioctl()
44 * -- grif@cs.ucr.edu, 5-Dec-95
45 *
46 * Move console and virtual terminal code to more appropriate files,
47 * implement CONFIG_VT and generalize console device interface.
48 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
49 *
Alan Coxd81ed102008-10-13 10:41:42 +010050 * Rewrote tty_init_dev and tty_release_dev to eliminate races.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * -- Bill Hawes <whawes@star.net>, June 97
52 *
53 * Added devfs support.
54 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
55 *
56 * Added support for a Unix98-style ptmx device.
57 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
58 *
59 * Reduced memory usage for older ARM systems
60 * -- Russell King <rmk@arm.linux.org.uk>
61 *
62 * Move do_SAK() into process context. Less stack use in devfs functions.
Alan Cox37bdfb02008-02-08 04:18:47 -080063 * alloc_tty_struct() always uses kmalloc()
64 * -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/types.h>
68#include <linux/major.h>
69#include <linux/errno.h>
70#include <linux/signal.h>
71#include <linux/fcntl.h>
72#include <linux/sched.h>
73#include <linux/interrupt.h>
74#include <linux/tty.h>
75#include <linux/tty_driver.h>
76#include <linux/tty_flip.h>
77#include <linux/devpts_fs.h>
78#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040079#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/console.h>
81#include <linux/timer.h>
82#include <linux/ctype.h>
83#include <linux/kd.h>
84#include <linux/mm.h>
85#include <linux/string.h>
86#include <linux/slab.h>
87#include <linux/poll.h>
88#include <linux/proc_fs.h>
89#include <linux/init.h>
90#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/wait.h>
93#include <linux/bitops.h>
Domen Puncerb20f3ae2005-06-25 14:58:42 -070094#include <linux/delay.h>
Alan Coxa352def2008-07-16 21:53:12 +010095#include <linux/seq_file.h>
Alan Coxd281da72010-09-16 18:21:24 +010096#include <linux/serial.h>
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +020097#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Alan Coxa352def2008-07-16 21:53:12 +010099#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#include <linux/kbd_kern.h>
102#include <linux/vt_kern.h>
103#include <linux/selection.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105#include <linux/kmod.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700106#include <linux/nsproxy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108#undef TTY_DEBUG_HANGUP
Peter Hurleyaccff792015-07-12 22:49:09 -0400109#ifdef TTY_DEBUG_HANGUP
110# define tty_debug_hangup(tty, f, args...) tty_debug(tty, f, ##args)
111#else
112# define tty_debug_hangup(tty, f, args...) do { } while (0)
113#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115#define TTY_PARANOIA_CHECK 1
116#define CHECK_TTY_COUNT 1
117
Alan Coxedc6afc2006-12-08 02:38:44 -0800118struct ktermios tty_std_termios = { /* for the benefit of tty drivers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .c_iflag = ICRNL | IXON,
120 .c_oflag = OPOST | ONLCR,
121 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
122 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
123 ECHOCTL | ECHOKE | IEXTEN,
Alan Coxedc6afc2006-12-08 02:38:44 -0800124 .c_cc = INIT_C_CC,
125 .c_ispeed = 38400,
Peter Hurley133b1302016-01-10 22:41:07 -0800126 .c_ospeed = 38400,
127 /* .c_line = N_TTY, */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130EXPORT_SYMBOL(tty_std_termios);
131
132/* This list gets poked at by procfs and various bits of boot up code. This
133 could do with some rationalisation such as pulling the tty proc function
134 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136LIST_HEAD(tty_drivers); /* linked list of tty drivers */
137
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800138/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800140DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700141EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Nick Pigginee2ffa02010-08-18 04:37:35 +1000143/* Spinlock to protect the tty->tty_files list */
144DEFINE_SPINLOCK(tty_files_lock);
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
147static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800148ssize_t redirected_tty_write(struct file *, const char __user *,
149 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static unsigned int tty_poll(struct file *, poll_table *);
151static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700152long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700153#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800154static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700155 unsigned long arg);
156#else
157#define tty_compat_ioctl NULL
158#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200159static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800160static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800161static void release_tty(struct tty_struct *tty, int idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Alan Coxaf9b8972006-08-27 01:24:01 -0700163/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700164 * free_tty_struct - free a disused tty
165 * @tty: tty struct to free
166 *
167 * Free the write buffers, tty queue and tty memory itself.
168 *
169 * Locking: none. Must be called after tty is definitely unused
170 */
171
Peter Hurleya3123fd2016-01-09 21:13:48 -0800172static void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Peter Hurleyc8b710b2016-01-09 21:13:46 -0800174 tty_ldisc_deinit(tty);
Markus Elfringa211b1a2014-11-21 13:42:29 +0100175 put_device(tty->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 kfree(tty->write_buf);
Alan Cox89c8d912012-08-08 16:30:13 +0100177 tty->magic = 0xDEADDEAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 kfree(tty);
179}
180
Nick Piggind996b622010-08-18 04:37:36 +1000181static inline struct tty_struct *file_tty(struct file *file)
182{
183 return ((struct tty_file_private *)file->private_data)->tty;
184}
185
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200186int tty_alloc_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000187{
188 struct tty_file_private *priv;
189
Pekka Enbergf573bd12010-08-24 07:48:34 +0300190 priv = kmalloc(sizeof(*priv), GFP_KERNEL);
191 if (!priv)
192 return -ENOMEM;
Nick Piggind996b622010-08-18 04:37:36 +1000193
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200194 file->private_data = priv;
195
196 return 0;
197}
198
199/* Associate a new file with the tty structure */
200void tty_add_file(struct tty_struct *tty, struct file *file)
201{
202 struct tty_file_private *priv = file->private_data;
203
Nick Piggind996b622010-08-18 04:37:36 +1000204 priv->tty = tty;
205 priv->file = file;
Nick Piggind996b622010-08-18 04:37:36 +1000206
207 spin_lock(&tty_files_lock);
208 list_add(&priv->list, &tty->tty_files);
209 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200210}
Pekka Enbergf573bd12010-08-24 07:48:34 +0300211
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200212/**
213 * tty_free_file - free file->private_data
214 *
215 * This shall be used only for fail path handling when tty_add_file was not
216 * called yet.
217 */
218void tty_free_file(struct file *file)
219{
220 struct tty_file_private *priv = file->private_data;
221
222 file->private_data = NULL;
223 kfree(priv);
Nick Piggind996b622010-08-18 04:37:36 +1000224}
225
226/* Delete file from its tty */
Josh Triplett2520e272012-11-18 21:27:47 -0800227static void tty_del_file(struct file *file)
Nick Piggind996b622010-08-18 04:37:36 +1000228{
229 struct tty_file_private *priv = file->private_data;
230
231 spin_lock(&tty_files_lock);
232 list_del(&priv->list);
233 spin_unlock(&tty_files_lock);
Jiri Slabyfa90e1c2011-10-12 11:32:43 +0200234 tty_free_file(file);
Nick Piggind996b622010-08-18 04:37:36 +1000235}
236
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
239
Alan Coxaf9b8972006-08-27 01:24:01 -0700240/**
241 * tty_name - return tty naming
242 * @tty: tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -0700243 *
244 * Convert a tty structure into a name. The name reflects the kernel
245 * naming policy and if udev is in use may not reflect user space
246 *
247 * Locking: none
248 */
249
Rasmus Villemoes429b4742015-03-31 15:55:59 +0200250const char *tty_name(const struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 if (!tty) /* Hmm. NULL pointer. That's fun. */
Rasmus Villemoes917162c2015-03-31 15:55:58 +0200253 return "NULL tty";
254 return tty->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
257EXPORT_SYMBOL(tty_name);
258
Peter Hurley0a083ed2015-11-08 13:01:12 -0500259const char *tty_driver_name(const struct tty_struct *tty)
260{
261 if (!tty || !tty->driver)
262 return "";
263 return tty->driver->name;
264}
265
Peter Hurley82b8f882015-11-08 13:01:09 -0500266static int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 const char *routine)
268{
269#ifdef TTY_PARANOIA_CHECK
270 if (!tty) {
Peter Hurley89222e62015-11-08 13:01:18 -0500271 pr_warn("(%d:%d): %s: NULL tty\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 imajor(inode), iminor(inode), routine);
273 return 1;
274 }
275 if (tty->magic != TTY_MAGIC) {
Peter Hurley89222e62015-11-08 13:01:18 -0500276 pr_warn("(%d:%d): %s: bad magic number\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 imajor(inode), iminor(inode), routine);
278 return 1;
279 }
280#endif
281 return 0;
282}
283
Peter Hurleydeb287e2014-11-05 12:12:55 -0500284/* Caller must hold tty_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int check_tty_count(struct tty_struct *tty, const char *routine)
286{
287#ifdef CHECK_TTY_COUNT
288 struct list_head *p;
289 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800290
Nick Pigginee2ffa02010-08-18 04:37:35 +1000291 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 list_for_each(p, &tty->tty_files) {
293 count++;
294 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000295 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
297 tty->driver->subtype == PTY_TYPE_SLAVE &&
298 tty->link && tty->link->count)
299 count++;
300 if (tty->count != count) {
Peter Hurley339f36b2015-11-08 13:01:13 -0500301 tty_warn(tty, "%s: tty->count(%d) != #fd's(%d)\n",
302 routine, tty->count, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
306 return 0;
307}
308
Alan Coxaf9b8972006-08-27 01:24:01 -0700309/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700310 * get_tty_driver - find device of a tty
311 * @dev_t: device identifier
312 * @index: returns the index of the tty
313 *
314 * This routine returns a tty driver structure, given a device number
315 * and also passes back the index number.
316 *
317 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320static struct tty_driver *get_tty_driver(dev_t device, int *index)
321{
322 struct tty_driver *p;
323
324 list_for_each_entry(p, &tty_drivers, tty_drivers) {
325 dev_t base = MKDEV(p->major, p->minor_start);
326 if (device < base || device >= base + p->num)
327 continue;
328 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100329 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 return NULL;
332}
333
Jason Wesself2d937f2008-04-17 20:05:37 +0200334#ifdef CONFIG_CONSOLE_POLL
335
336/**
337 * tty_find_polling_driver - find device of a polled tty
338 * @name: name string to match
339 * @line: pointer to resulting tty line nr
340 *
341 * This routine returns a tty driver structure, given a name
342 * and the condition that the tty driver is capable of polled
343 * operation.
344 */
345struct tty_driver *tty_find_polling_driver(char *name, int *line)
346{
347 struct tty_driver *p, *res = NULL;
348 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500349 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100350 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200351
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500352 for (str = name; *str; str++)
353 if ((*str >= '0' && *str <= '9') || *str == ',')
354 break;
355 if (!*str)
356 return NULL;
357
358 len = str - name;
359 tty_line = simple_strtoul(str, &str, 10);
360
Jason Wesself2d937f2008-04-17 20:05:37 +0200361 mutex_lock(&tty_mutex);
362 /* Search through the tty devices to look for a match */
363 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500364 if (strncmp(name, p->name, len) != 0)
365 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100366 stp = str;
367 if (*stp == ',')
368 stp++;
369 if (*stp == '\0')
370 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200371
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200372 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100373 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100374 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200375 *line = tty_line;
376 break;
377 }
378 }
379 mutex_unlock(&tty_mutex);
380
381 return res;
382}
383EXPORT_SYMBOL_GPL(tty_find_polling_driver);
384#endif
385
Alan Coxaf9b8972006-08-27 01:24:01 -0700386/**
387 * tty_check_change - check for POSIX terminal changes
388 * @tty: tty to check
389 *
390 * If we try to write to, or set the state of, a terminal and we're
391 * not in the foreground, send a SIGTTOU. If the signal is blocked or
392 * ignored, go ahead and perform the operation. (POSIX 7.2)
393 *
Alan Cox978e5952008-04-30 00:53:59 -0700394 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700396
Peter Hurley2812d9e2015-10-10 20:28:42 -0400397int __tty_check_change(struct tty_struct *tty, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Alan Cox47f86832008-04-30 00:53:30 -0700399 unsigned long flags;
Peter Hurley2812d9e2015-10-10 20:28:42 -0400400 struct pid *pgrp, *tty_pgrp;
Alan Cox47f86832008-04-30 00:53:30 -0700401 int ret = 0;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 if (current->signal->tty != tty)
404 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700405
Patrick Donnelly67196932015-07-12 18:51:52 -0400406 rcu_read_lock();
407 pgrp = task_pgrp(current);
408
Alan Cox47f86832008-04-30 00:53:30 -0700409 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurley2812d9e2015-10-10 20:28:42 -0400410 tty_pgrp = tty->pgrp;
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700411 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Patrick Donnelly67196932015-07-12 18:51:52 -0400412
Peter Hurley2812d9e2015-10-10 20:28:42 -0400413 if (tty_pgrp && pgrp != tty->pgrp) {
414 if (is_ignored(sig)) {
415 if (sig == SIGTTIN)
416 ret = -EIO;
417 } else if (is_current_pgrp_orphaned())
418 ret = -EIO;
419 else {
420 kill_pgrp(pgrp, sig, 1);
421 set_thread_flag(TIF_SIGPENDING);
422 ret = -ERESTARTSYS;
423 }
Alan Cox47f86832008-04-30 00:53:30 -0700424 }
Patrick Donnelly67196932015-07-12 18:51:52 -0400425 rcu_read_unlock();
Peter Hurley2812d9e2015-10-10 20:28:42 -0400426
Peter Hurley339f36b2015-11-08 13:01:13 -0500427 if (!tty_pgrp)
428 tty_warn(tty, "sig=%d, tty->pgrp == NULL!\n", sig);
Peter Hurley2812d9e2015-10-10 20:28:42 -0400429
Alan Cox47f86832008-04-30 00:53:30 -0700430 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Peter Hurley2812d9e2015-10-10 20:28:42 -0400433int tty_check_change(struct tty_struct *tty)
434{
435 return __tty_check_change(tty, SIGTTOU);
436}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437EXPORT_SYMBOL(tty_check_change);
438
Alan Cox37bdfb02008-02-08 04:18:47 -0800439static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 size_t count, loff_t *ppos)
441{
442 return 0;
443}
444
Alan Cox37bdfb02008-02-08 04:18:47 -0800445static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 size_t count, loff_t *ppos)
447{
448 return -EIO;
449}
450
451/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800452static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
455}
456
Alan Cox04f378b2008-04-30 00:53:29 -0700457static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
458 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700459{
460 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
461}
462
Alan Cox37bdfb02008-02-08 04:18:47 -0800463static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700464 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
467}
468
Arjan van de Ven62322d22006-07-03 00:24:21 -0700469static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .llseek = no_llseek,
471 .read = tty_read,
472 .write = tty_write,
473 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700474 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700475 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .open = tty_open,
477 .release = tty_release,
478 .fasync = tty_fasync,
479};
480
Arjan van de Ven62322d22006-07-03 00:24:21 -0700481static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .llseek = no_llseek,
483 .read = tty_read,
484 .write = redirected_tty_write,
485 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700486 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700487 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 .open = tty_open,
489 .release = tty_release,
490 .fasync = tty_fasync,
491};
492
Arjan van de Ven62322d22006-07-03 00:24:21 -0700493static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 .llseek = no_llseek,
495 .read = hung_up_tty_read,
496 .write = hung_up_tty_write,
497 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700498 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700499 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 .release = tty_release,
501};
502
503static DEFINE_SPINLOCK(redirect_lock);
504static struct file *redirect;
505
Peter Hurley11d9bef2014-10-16 14:59:42 -0400506
507void proc_clear_tty(struct task_struct *p)
508{
509 unsigned long flags;
510 struct tty_struct *tty;
511 spin_lock_irqsave(&p->sighand->siglock, flags);
512 tty = p->signal->tty;
513 p->signal->tty = NULL;
514 spin_unlock_irqrestore(&p->sighand->siglock, flags);
515 tty_kref_put(tty);
516}
517
Peter Hurley2c411c12014-10-16 14:59:47 -0400518/**
519 * proc_set_tty - set the controlling terminal
520 *
521 * Only callable by the session leader and only if it does not already have
522 * a controlling terminal.
523 *
Peter Hurleye218eb32014-10-16 14:59:49 -0400524 * Caller must hold: tty_lock()
525 * a readlock on tasklist_lock
Peter Hurley2c411c12014-10-16 14:59:47 -0400526 * sighand lock
527 */
Peter Hurleybce65f12014-10-16 14:59:43 -0400528static void __proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400529{
Peter Hurleyae28fa72014-10-16 14:59:46 -0400530 unsigned long flags;
531
Peter Hurleyae28fa72014-10-16 14:59:46 -0400532 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya3618582014-10-16 14:59:50 -0400533 /*
534 * The session and fg pgrp references will be non-NULL if
535 * tiocsctty() is stealing the controlling tty
536 */
Peter Hurleyae28fa72014-10-16 14:59:46 -0400537 put_pid(tty->session);
538 put_pid(tty->pgrp);
539 tty->pgrp = get_pid(task_pgrp(current));
540 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
541 tty->session = get_pid(task_session(current));
542 if (current->signal->tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -0400543 tty_debug(tty, "current tty %s not NULL!!\n",
544 current->signal->tty->name);
Peter Hurleyae28fa72014-10-16 14:59:46 -0400545 tty_kref_put(current->signal->tty);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400546 }
Peter Hurleybce65f12014-10-16 14:59:43 -0400547 put_pid(current->signal->tty_old_pgrp);
548 current->signal->tty = tty_kref_get(tty);
549 current->signal->tty_old_pgrp = NULL;
Peter Hurley11d9bef2014-10-16 14:59:42 -0400550}
551
Peter Hurleybce65f12014-10-16 14:59:43 -0400552static void proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400553{
Peter Hurleybce65f12014-10-16 14:59:43 -0400554 spin_lock_irq(&current->sighand->siglock);
555 __proc_set_tty(tty);
556 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400557}
558
559struct tty_struct *get_current_tty(void)
560{
561 struct tty_struct *tty;
562 unsigned long flags;
563
564 spin_lock_irqsave(&current->sighand->siglock, flags);
565 tty = tty_kref_get(current->signal->tty);
566 spin_unlock_irqrestore(&current->sighand->siglock, flags);
567 return tty;
568}
569EXPORT_SYMBOL_GPL(get_current_tty);
570
571static void session_clear_tty(struct pid *session)
572{
573 struct task_struct *p;
574 do_each_pid_task(session, PIDTYPE_SID, p) {
575 proc_clear_tty(p);
576 } while_each_pid_task(session, PIDTYPE_SID, p);
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/**
580 * tty_wakeup - request more data
581 * @tty: terminal
582 *
583 * Internal and external helper for wakeups of tty. This function
584 * informs the line discipline if present that the driver is ready
585 * to receive more output data.
586 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588void tty_wakeup(struct tty_struct *tty)
589{
590 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
593 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800594 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100595 if (ld->ops->write_wakeup)
596 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 tty_ldisc_deref(ld);
598 }
599 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700600 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603EXPORT_SYMBOL_GPL(tty_wakeup);
604
605/**
Peter Hurleyea648a42013-03-06 07:20:53 -0500606 * tty_signal_session_leader - sends SIGHUP to session leader
Peter Hurleyf91e2592013-03-06 07:20:56 -0500607 * @tty controlling tty
608 * @exit_session if non-zero, signal all foreground group processes
Peter Hurleyea648a42013-03-06 07:20:53 -0500609 *
Peter Hurleyf91e2592013-03-06 07:20:56 -0500610 * Send SIGHUP and SIGCONT to the session leader and its process group.
611 * Optionally, signal all processes in the foreground process group.
Peter Hurleyea648a42013-03-06 07:20:53 -0500612 *
613 * Returns the number of processes in the session with this tty
614 * as their controlling terminal. This value is used to drop
615 * tty references for those processes.
616 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500617static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
Peter Hurleyea648a42013-03-06 07:20:53 -0500618{
619 struct task_struct *p;
Peter Hurleyea648a42013-03-06 07:20:53 -0500620 int refs = 0;
Peter Hurleyf91e2592013-03-06 07:20:56 -0500621 struct pid *tty_pgrp = NULL;
Peter Hurleyea648a42013-03-06 07:20:53 -0500622
623 read_lock(&tasklist_lock);
624 if (tty->session) {
625 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
626 spin_lock_irq(&p->sighand->siglock);
627 if (p->signal->tty == tty) {
628 p->signal->tty = NULL;
629 /* We defer the dereferences outside fo
630 the tasklist lock */
631 refs++;
632 }
633 if (!p->signal->leader) {
634 spin_unlock_irq(&p->sighand->siglock);
635 continue;
636 }
637 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
638 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
639 put_pid(p->signal->tty_old_pgrp); /* A noop */
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500640 spin_lock(&tty->ctrl_lock);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500641 tty_pgrp = get_pid(tty->pgrp);
Peter Hurleyea648a42013-03-06 07:20:53 -0500642 if (tty->pgrp)
643 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500644 spin_unlock(&tty->ctrl_lock);
Peter Hurleyea648a42013-03-06 07:20:53 -0500645 spin_unlock_irq(&p->sighand->siglock);
646 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
647 }
648 read_unlock(&tasklist_lock);
649
Peter Hurleyf91e2592013-03-06 07:20:56 -0500650 if (tty_pgrp) {
651 if (exit_session)
652 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
653 put_pid(tty_pgrp);
654 }
655
Peter Hurleyea648a42013-03-06 07:20:53 -0500656 return refs;
657}
658
659/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200660 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000661 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700662 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800663 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700664 * synchronous but doesn't hold any locks, so we need to make sure we
665 * have the appropriate locks for what we're doing.
666 *
667 * The hangup event clears any pending redirections onto the hung up
668 * device. It ensures future writes will error and it does the needed
669 * line discipline hangup and signal delivery. The tty object itself
670 * remains intact.
671 *
672 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200673 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800674 * redirect lock for undoing redirection
675 * file list lock for manipulating list of ttys
Peter Hurley137084b2013-06-15 07:04:46 -0400676 * tty_ldiscs_lock from called functions
Peter Hurley6a1c0682013-06-15 09:14:23 -0400677 * termios_rwsem resetting termios data
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800678 * tasklist_lock to walk task list for hangup event
679 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500681static void __tty_hangup(struct tty_struct *tty, int exit_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Alan Cox37bdfb02008-02-08 04:18:47 -0800683 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000685 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500687 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!tty)
690 return;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000694 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 f = redirect;
696 redirect = NULL;
697 }
698 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800699
Alan Cox89c8d912012-08-08 16:30:13 +0100700 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200701
Peter Hurleycb50e522013-07-31 14:05:45 -0400702 if (test_bit(TTY_HUPPED, &tty->flags)) {
703 tty_unlock(tty);
704 return;
705 }
706
Arnd Bergmannec79d602010-06-01 22:53:01 +0200707 /* inuse_filps is protected by the single tty lock,
708 this really needs to change if we want to flush the
709 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200710 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000711
Nick Pigginee2ffa02010-08-18 04:37:35 +1000712 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000714 list_for_each_entry(priv, &tty->tty_files, list) {
715 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (filp->f_op->write == redirected_tty_write)
717 cons_filp = filp;
718 if (filp->f_op->write != tty_write)
719 continue;
720 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200721 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 filp->f_op = &hung_up_tty_fops;
723 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000724 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800725
Peter Hurley25fdf242013-03-06 07:20:57 -0500726 refs = tty_signal_session_leader(tty, exit_session);
727 /* Account for the p->signal references we killed */
728 while (refs--)
729 tty_kref_put(tty);
730
Peter Hurley892d1fa2016-01-10 22:41:06 -0800731 tty_ldisc_hangup(tty, cons_filp != NULL);
Alan Cox37bdfb02008-02-08 04:18:47 -0800732
Peter Hurley20cc2252013-03-06 07:20:54 -0500733 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100734 clear_bit(TTY_THROTTLED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100735 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600736 put_pid(tty->session);
737 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800738 tty->session = NULL;
739 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500741 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800744 * If one of the devices matches a console pointer, we
745 * cannot just call hangup() because that will cause
746 * tty->count and state->count to go out of sync.
747 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700750 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700752 tty->ops->close(tty, cons_filp);
753 } else if (tty->ops->hangup)
Peter Hurley7c6d3402014-06-16 09:17:08 -0400754 tty->ops->hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800755 /*
Peter Hurley892d1fa2016-01-10 22:41:06 -0800756 * We don't want to have driver/ldisc interactions beyond the ones
757 * we did here. The driver layer expects no calls after ->hangup()
758 * from the ldisc side, which is now guaranteed.
Alan Cox37bdfb02008-02-08 04:18:47 -0800759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100761 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (f)
764 fput(f);
765}
766
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200767static void do_tty_hangup(struct work_struct *work)
768{
769 struct tty_struct *tty =
770 container_of(work, struct tty_struct, hangup_work);
771
Peter Hurleyf91e2592013-03-06 07:20:56 -0500772 __tty_hangup(tty, 0);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200773}
774
Alan Coxaf9b8972006-08-27 01:24:01 -0700775/**
776 * tty_hangup - trigger a hangup event
777 * @tty: tty to hangup
778 *
779 * A carrier loss (virtual or otherwise) has occurred on this like
780 * schedule a hangup sequence to run after this event.
781 */
782
Alan Cox37bdfb02008-02-08 04:18:47 -0800783void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500785 tty_debug_hangup(tty, "hangup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 schedule_work(&tty->hangup_work);
787}
788
789EXPORT_SYMBOL(tty_hangup);
790
Alan Coxaf9b8972006-08-27 01:24:01 -0700791/**
792 * tty_vhangup - process vhangup
793 * @tty: tty to hangup
794 *
795 * The user has asked via system call for the terminal to be hung up.
796 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200797 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700798 */
799
Alan Cox37bdfb02008-02-08 04:18:47 -0800800void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500802 tty_debug_hangup(tty, "vhangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500803 __tty_hangup(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
Alan Cox37bdfb02008-02-08 04:18:47 -0800805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806EXPORT_SYMBOL(tty_vhangup);
807
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200808
Alan Coxaf9b8972006-08-27 01:24:01 -0700809/**
Alan Cox2cb59982008-10-13 10:40:30 +0100810 * tty_vhangup_self - process vhangup for own ctty
811 *
812 * Perform a vhangup on the current controlling tty
813 */
814
815void tty_vhangup_self(void)
816{
817 struct tty_struct *tty;
818
Alan Cox2cb59982008-10-13 10:40:30 +0100819 tty = get_current_tty();
820 if (tty) {
821 tty_vhangup(tty);
822 tty_kref_put(tty);
823 }
Alan Cox2cb59982008-10-13 10:40:30 +0100824}
825
826/**
Peter Hurleyf91e2592013-03-06 07:20:56 -0500827 * tty_vhangup_session - hangup session leader exit
828 * @tty: tty to hangup
829 *
830 * The session leader is exiting and hanging up its controlling terminal.
831 * Every process in the foreground process group is signalled SIGHUP.
832 *
833 * We do this synchronously so that when the syscall returns the process
834 * is complete. That guarantee is necessary for security reasons.
835 */
836
Changlong Xie44a459f2013-03-20 08:42:04 +0800837static void tty_vhangup_session(struct tty_struct *tty)
Peter Hurleyf91e2592013-03-06 07:20:56 -0500838{
Peter Hurleyd435cef2015-11-08 13:01:19 -0500839 tty_debug_hangup(tty, "session hangup\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500840 __tty_hangup(tty, 1);
841}
842
843/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700844 * tty_hung_up_p - was tty hung up
845 * @filp: file pointer of tty
846 *
847 * Return true if the tty has been subject to a vhangup or a carrier
848 * loss
849 */
850
Alan Cox37bdfb02008-02-08 04:18:47 -0800851int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 return (filp->f_op == &hung_up_tty_fops);
854}
855
856EXPORT_SYMBOL(tty_hung_up_p);
857
Alan Coxaf9b8972006-08-27 01:24:01 -0700858/**
859 * disassociate_ctty - disconnect controlling tty
860 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700862 * This function is typically called only by the session leader, when
863 * it wants to disassociate itself from its controlling tty.
864 *
865 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
867 * (2) Clears the tty from being controlling the session
868 * (3) Clears the controlling tty for all processes in the
869 * session group.
870 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700871 * The argument on_exit is set to 1 if called when a process is
872 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
873 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800874 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200875 * BTM is taken for hysterical raisins, and held when
876 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800877 * tty_mutex is taken to protect tty
878 * ->siglock is taken to protect ->signal/->sighand
879 * tasklist_lock is taken to walk process list for sessions
880 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883void disassociate_ctty(int on_exit)
884{
885 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Alan Cox5ec93d12009-11-30 13:18:45 +0000887 if (!current->signal->leader)
888 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800890 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (tty) {
Peter Hurleyf91e2592013-03-06 07:20:56 -0500892 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
893 tty_vhangup_session(tty);
894 } else {
895 struct pid *tty_pgrp = tty_get_pgrp(tty);
896 if (tty_pgrp) {
897 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Oleg Nesterov03e12612013-09-15 17:50:26 +0200898 if (!on_exit)
899 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500900 put_pid(tty_pgrp);
901 }
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200902 }
Alan Cox452a00d2008-10-13 10:39:13 +0100903 tty_kref_put(tty);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500904
Eric W. Biederman680a9672007-02-12 00:52:52 -0800905 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800906 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800907 spin_lock_irq(&current->sighand->siglock);
908 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800909 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800910 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800911 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800912 kill_pgrp(old_pgrp, SIGHUP, on_exit);
913 kill_pgrp(old_pgrp, SIGCONT, on_exit);
914 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return;
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800919 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700920 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800921 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800922
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800923 tty = tty_kref_get(current->signal->tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800924 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700925 unsigned long flags;
926 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800927 put_pid(tty->session);
928 put_pid(tty->pgrp);
929 tty->session = NULL;
930 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700931 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100932 tty_kref_put(tty);
Peter Hurleyaccff792015-07-12 22:49:09 -0400933 } else
934 tty_debug_hangup(tty, "no current tty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800936 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* Now clear signal->tty under the lock */
938 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800939 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941}
942
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700943/**
944 *
945 * no_tty - Ensure the current process does not have a controlling tty
946 */
947void no_tty(void)
948{
Alan Cox3af502b2012-05-03 22:21:53 +0100949 /* FIXME: Review locking here. The tty_lock never covered any race
950 between a new association and proc_clear_tty but possible we need
951 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700952 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000953 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700954 proc_clear_tty(tsk);
955}
956
Alan Coxaf9b8972006-08-27 01:24:01 -0700957
958/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200959 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700960 * @tty: tty to stop
961 *
Peter Hurley01adc802014-09-10 15:06:32 -0400962 * Perform flow control to the driver. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700963 * on an already stopped device and will not re-call the driver
964 * method.
965 *
966 * This functionality is used by both the line disciplines for
967 * halting incoming flow and by the driver. It may therefore be
968 * called from any context, may be under the tty atomic_write_lock
969 * but not always.
970 *
971 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400972 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700973 */
974
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400975void __stop_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400977 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return;
979 tty->stopped = 1;
Alan Coxf34d7a52008-04-30 00:54:13 -0700980 if (tty->ops->stop)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500981 tty->ops->stop(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400984void stop_tty(struct tty_struct *tty)
985{
986 unsigned long flags;
987
988 spin_lock_irqsave(&tty->flow_lock, flags);
989 __stop_tty(tty);
990 spin_unlock_irqrestore(&tty->flow_lock, flags);
991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992EXPORT_SYMBOL(stop_tty);
993
Alan Coxaf9b8972006-08-27 01:24:01 -0700994/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200995 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700996 * @tty: tty to start
997 *
Peter Hurley01adc802014-09-10 15:06:32 -0400998 * Start a tty that has been stopped if at all possible. If this
999 * tty was previous stopped and is now being started, the driver
1000 * start method is invoked and the line discipline woken.
Alan Coxaf9b8972006-08-27 01:24:01 -07001001 *
1002 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001003 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001004 */
1005
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001006void __start_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001008 if (!tty->stopped || tty->flow_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return;
1010 tty->stopped = 0;
Alan Coxf34d7a52008-04-30 00:54:13 -07001011 if (tty->ops->start)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001012 tty->ops->start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001016void start_tty(struct tty_struct *tty)
1017{
1018 unsigned long flags;
1019
1020 spin_lock_irqsave(&tty->flow_lock, flags);
1021 __start_tty(tty);
1022 spin_unlock_irqrestore(&tty->flow_lock, flags);
1023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024EXPORT_SYMBOL(start_tty);
1025
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001026static void tty_update_time(struct timespec *time)
1027{
Jiri Slabyf0bf0bd2015-02-27 18:40:31 +01001028 unsigned long sec = get_seconds();
Greg Kroah-Hartmanfbf47632015-03-26 23:10:27 +01001029
1030 /*
1031 * We only care if the two values differ in anything other than the
1032 * lower three bits (i.e every 8 seconds). If so, then we can update
1033 * the time of the tty device, otherwise it could be construded as a
1034 * security leak to let userspace know the exact timing of the tty.
1035 */
1036 if ((sec ^ time->tv_sec) & ~7)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001037 time->tv_sec = sec;
1038}
1039
Alan Coxaf9b8972006-08-27 01:24:01 -07001040/**
1041 * tty_read - read method for tty device files
1042 * @file: pointer to tty file
1043 * @buf: user buffer
1044 * @count: size of user buffer
1045 * @ppos: unused
1046 *
1047 * Perform the read system call function on this terminal device. Checks
1048 * for hung up devices before calling the line discipline method.
1049 *
1050 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -07001051 * Locks the line discipline internally while needed. Multiple
1052 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -07001053 */
1054
Alan Cox37bdfb02008-02-08 04:18:47 -08001055static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 loff_t *ppos)
1057{
1058 int i;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001059 struct inode *inode = file_inode(file);
Nick Piggind996b622010-08-18 04:37:36 +10001060 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct tty_ldisc *ld;
1062
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001063 if (tty_paranoia_check(tty, inode, "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 return -EIO;
1065 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1066 return -EIO;
1067
1068 /* We want to wait for the line discipline to sort out in this
1069 situation */
1070 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08001071 if (!ld)
1072 return hung_up_tty_read(file, buf, count, ppos);
Alan Coxa352def2008-07-16 21:53:12 +01001073 if (ld->ops->read)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001074 i = ld->ops->read(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 else
1076 i = -EIO;
1077 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001078
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001079 if (i > 0)
1080 tty_update_time(&inode->i_atime);
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return i;
1083}
1084
Peter Hurley136d5252014-09-10 15:06:34 -04001085static void tty_write_unlock(struct tty_struct *tty)
Alan Cox9c1729d2007-07-15 23:39:43 -07001086{
1087 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001088 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001089}
1090
Peter Hurley136d5252014-09-10 15:06:34 -04001091static int tty_write_lock(struct tty_struct *tty, int ndelay)
Alan Cox9c1729d2007-07-15 23:39:43 -07001092{
1093 if (!mutex_trylock(&tty->atomic_write_lock)) {
1094 if (ndelay)
1095 return -EAGAIN;
1096 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1097 return -ERESTARTSYS;
1098 }
1099 return 0;
1100}
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102/*
1103 * Split writes up in sane blocksizes to avoid
1104 * denial-of-service type attacks
1105 */
1106static inline ssize_t do_tty_write(
1107 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1108 struct tty_struct *tty,
1109 struct file *file,
1110 const char __user *buf,
1111 size_t count)
1112{
Alan Cox9c1729d2007-07-15 23:39:43 -07001113 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001115
Alan Cox9c1729d2007-07-15 23:39:43 -07001116 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1117 if (ret < 0)
1118 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /*
1121 * We chunk up writes into a temporary buffer. This
1122 * simplifies low-level drivers immensely, since they
1123 * don't have locking issues and user mode accesses.
1124 *
1125 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1126 * big chunk-size..
1127 *
1128 * The default chunk-size is 2kB, because the NTTY
1129 * layer has problems with bigger chunks. It will
1130 * claim to be able to handle more characters than
1131 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001132 *
1133 * FIXME: This can probably go away now except that 64K chunks
1134 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
1136 chunk = 2048;
1137 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1138 chunk = 65536;
1139 if (count < chunk)
1140 chunk = count;
1141
Ingo Molnar70522e12006-03-23 03:00:31 -08001142 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001144 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (chunk < 1024)
1147 chunk = 1024;
1148
Jason Wessel402fda92008-10-13 10:45:36 +01001149 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1150 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001151 ret = -ENOMEM;
1152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154 kfree(tty->write_buf);
1155 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001156 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 /* Do the write .. */
1160 for (;;) {
1161 size_t size = count;
1162 if (size > chunk)
1163 size = chunk;
1164 ret = -EFAULT;
1165 if (copy_from_user(tty->write_buf, buf, size))
1166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (ret <= 0)
1169 break;
1170 written += ret;
1171 buf += ret;
1172 count -= ret;
1173 if (!count)
1174 break;
1175 ret = -ERESTARTSYS;
1176 if (signal_pending(current))
1177 break;
1178 cond_resched();
1179 }
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001180 if (written) {
1181 tty_update_time(&file_inode(file)->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 ret = written;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001183 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001184out:
1185 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return ret;
1187}
1188
Alan Cox95f9bfc2008-10-13 10:39:23 +01001189/**
1190 * tty_write_message - write a message to a certain tty, not just the console.
1191 * @tty: the destination tty_struct
1192 * @msg: the message to write
1193 *
1194 * This is used for messages that need to be redirected to a specific tty.
1195 * We don't put it into the syslog queue right now maybe in the future if
1196 * really needed.
1197 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001198 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001199 */
1200
1201void tty_write_message(struct tty_struct *tty, char *msg)
1202{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001203 if (tty) {
1204 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001205 tty_lock(tty);
Peter Hurley4b41b952015-10-10 20:28:44 -04001206 if (tty->ops->write && tty->count > 0)
Alan Cox95f9bfc2008-10-13 10:39:23 +01001207 tty->ops->write(tty, msg, strlen(msg));
Peter Hurley4b41b952015-10-10 20:28:44 -04001208 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001209 tty_write_unlock(tty);
1210 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001211 return;
1212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Alan Coxaf9b8972006-08-27 01:24:01 -07001215/**
1216 * tty_write - write method for tty device file
1217 * @file: tty file pointer
1218 * @buf: user data to write
1219 * @count: bytes to write
1220 * @ppos: unused
1221 *
1222 * Write data to a tty device via the line discipline.
1223 *
1224 * Locking:
1225 * Locks the line discipline as required
1226 * Writes to the tty driver are serialized by the atomic_write_lock
1227 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001228 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001229 */
1230
Alan Cox37bdfb02008-02-08 04:18:47 -08001231static ssize_t tty_write(struct file *file, const char __user *buf,
1232 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
Nick Piggind996b622010-08-18 04:37:36 +10001234 struct tty_struct *tty = file_tty(file);
1235 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001237
Al Viro6131ffa2013-02-27 16:59:05 -05001238 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001240 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001241 (test_bit(TTY_IO_ERROR, &tty->flags)))
1242 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001243 /* Short term debug to catch buggy drivers */
1244 if (tty->ops->write_room == NULL)
Peter Hurley339f36b2015-11-08 13:01:13 -05001245 tty_err(tty, "missing write_room method\n");
Alan Cox37bdfb02008-02-08 04:18:47 -08001246 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08001247 if (!ld)
1248 return hung_up_tty_write(file, buf, count, ppos);
Alan Coxa352def2008-07-16 21:53:12 +01001249 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ret = -EIO;
1251 else
Alan Coxa352def2008-07-16 21:53:12 +01001252 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 tty_ldisc_deref(ld);
1254 return ret;
1255}
1256
Alan Cox37bdfb02008-02-08 04:18:47 -08001257ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1258 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct file *p = NULL;
1261
1262 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001263 if (redirect)
1264 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 spin_unlock(&redirect_lock);
1266
1267 if (p) {
1268 ssize_t res;
1269 res = vfs_write(p, buf, count, &p->f_pos);
1270 fput(p);
1271 return res;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return tty_write(file, buf, count, ppos);
1274}
1275
Peter Hurley136d5252014-09-10 15:06:34 -04001276/**
1277 * tty_send_xchar - send priority character
1278 *
1279 * Send a high priority character to the tty even if stopped
1280 *
1281 * Locking: none for xchar method, write ordering for write method.
1282 */
1283
1284int tty_send_xchar(struct tty_struct *tty, char ch)
1285{
1286 int was_stopped = tty->stopped;
1287
1288 if (tty->ops->send_xchar) {
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001289 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001290 tty->ops->send_xchar(tty, ch);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001291 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001292 return 0;
1293 }
1294
1295 if (tty_write_lock(tty, 0) < 0)
1296 return -ERESTARTSYS;
1297
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001298 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001299 if (was_stopped)
1300 start_tty(tty);
1301 tty->ops->write(tty, &ch, 1);
1302 if (was_stopped)
1303 stop_tty(tty);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001304 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001305 tty_write_unlock(tty);
1306 return 0;
1307}
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309static char ptychar[] = "pqrstuvwxyzabcde";
1310
Alan Coxaf9b8972006-08-27 01:24:01 -07001311/**
1312 * pty_line_name - generate name for a pty
1313 * @driver: the tty driver in use
1314 * @index: the minor number
1315 * @p: output buffer of at least 6 bytes
1316 *
1317 * Generate a name from a driver reference and write it to the output
1318 * buffer.
1319 *
1320 * Locking: None
1321 */
1322static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 int i = index + driver->name_base;
1325 /* ->name is initialized to "ttyp", but "tty" is expected */
1326 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001327 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1328 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Alan Coxaf9b8972006-08-27 01:24:01 -07001331/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001332 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001333 * @driver: the tty driver in use
1334 * @index: the minor number
1335 * @p: output buffer of at least 7 bytes
1336 *
1337 * Generate a name from a driver reference and write it to the output
Greg Kroah-Hartman5c0a2452014-02-22 14:31:04 -08001338 * buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07001339 *
1340 * Locking: None
1341 */
Hannes Reinecke723abd82014-02-27 12:30:51 +01001342static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Jiri Slaby0019b402012-08-08 22:26:43 +02001344 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
Hannes Reinecke723abd82014-02-27 12:30:51 +01001345 return sprintf(p, "%s", driver->name);
Jiri Slaby0019b402012-08-08 22:26:43 +02001346 else
Hannes Reinecke723abd82014-02-27 12:30:51 +01001347 return sprintf(p, "%s%d", driver->name,
1348 index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Alan Cox99f1fe12008-10-13 10:42:00 +01001351/**
1352 * tty_driver_lookup_tty() - find an existing tty, if any
1353 * @driver: the driver for the tty
1354 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001355 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001356 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1357 * driver lookup() method returns an error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001358 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001359 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001360 */
Jason Wessela47d5452009-01-02 13:43:04 +00001361static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001362 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001363{
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001364 struct tty_struct *tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001365
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001366 if (driver->ops->lookup)
1367 tty = driver->ops->lookup(driver, inode, idx);
1368 else
1369 tty = driver->ttys[idx];
1370
1371 if (!IS_ERR(tty))
1372 tty_kref_get(tty);
1373 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001374}
1375
Alan Cox99f1fe12008-10-13 10:42:00 +01001376/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001377 * tty_init_termios - helper for termios setup
1378 * @tty: the tty to set up
1379 *
1380 * Initialise the termios structures for this tty. Thus runs under
1381 * the tty_mutex currently so we can be relaxed about ordering.
1382 */
1383
Peter Hurleya3123fd2016-01-09 21:13:48 -08001384void tty_init_termios(struct tty_struct *tty)
Alan Coxbf970ee2008-10-13 10:42:39 +01001385{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001386 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001387 int idx = tty->index;
1388
Alan Cox36b3c072012-07-17 17:06:57 +01001389 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1390 tty->termios = tty->driver->init_termios;
1391 else {
1392 /* Check for lazy saved data */
1393 tp = tty->driver->termios[idx];
Peter Hurleyece53402016-01-10 22:40:57 -08001394 if (tp != NULL) {
Alan Cox36b3c072012-07-17 17:06:57 +01001395 tty->termios = *tp;
Peter Hurleyece53402016-01-10 22:40:57 -08001396 tty->termios.c_line = tty->driver->init_termios.c_line;
1397 } else
Alan Cox36b3c072012-07-17 17:06:57 +01001398 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001399 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001400 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001401 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1402 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001403}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001404EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001405
Jiri Slaby66d450e2012-01-30 21:14:28 +01001406int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1407{
Peter Hurleya3123fd2016-01-09 21:13:48 -08001408 tty_init_termios(tty);
Jiri Slaby66d450e2012-01-30 21:14:28 +01001409 tty_driver_kref_get(driver);
1410 tty->count++;
1411 driver->ttys[tty->index] = tty;
1412 return 0;
1413}
1414EXPORT_SYMBOL_GPL(tty_standard_install);
1415
Alan Coxbf970ee2008-10-13 10:42:39 +01001416/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001417 * tty_driver_install_tty() - install a tty entry in the driver
1418 * @driver: the driver for the tty
1419 * @tty: the tty
1420 *
1421 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001422 * will be set by the time this is called. This method is responsible
1423 * for ensuring any need additional structures are allocated and
1424 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001425 *
1426 * Locking: tty_mutex for now
1427 */
1428static int tty_driver_install_tty(struct tty_driver *driver,
1429 struct tty_struct *tty)
1430{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001431 return driver->ops->install ? driver->ops->install(driver, tty) :
1432 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001433}
1434
1435/**
1436 * tty_driver_remove_tty() - remove a tty from the driver tables
1437 * @driver: the driver for the tty
1438 * @idx: the minor number
1439 *
1440 * Remvoe a tty object from the driver tables. The tty->index field
1441 * will be set by the time this is called.
1442 *
1443 * Locking: tty_mutex for now
1444 */
Peter Hurley05de87ed2016-01-09 21:13:49 -08001445static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001446{
1447 if (driver->ops->remove)
1448 driver->ops->remove(driver, tty);
1449 else
1450 driver->ttys[tty->index] = NULL;
1451}
1452
1453/*
1454 * tty_reopen() - fast re-open of an open tty
1455 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001456 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001457 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001458 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001459 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001460 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001461 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001462static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001463{
1464 struct tty_driver *driver = tty->driver;
1465
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001466 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001467 driver->subtype == PTY_TYPE_MASTER)
1468 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001469
Peter Hurley7f22f6c2016-01-09 21:13:45 -08001470 if (!tty->count)
1471 return -EAGAIN;
1472
Peter Hurley86f2c002014-12-30 10:39:25 -05001473 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1474 return -EBUSY;
1475
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001476 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001477
Peter Hurley892d1fa2016-01-10 22:41:06 -08001478 if (!tty->ldisc)
1479 return tty_ldisc_reinit(tty, tty->termios.c_line);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001480
1481 return 0;
1482}
1483
Alan Coxaf9b8972006-08-27 01:24:01 -07001484/**
Alan Coxd81ed102008-10-13 10:41:42 +01001485 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001486 * @driver: tty driver we are opening a device on
1487 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001488 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001489 *
1490 * Prepare a tty device. This may not be a "new" clean device but
1491 * could also be an active device. The pty drivers require special
1492 * handling because of this.
1493 *
1494 * Locking:
1495 * The function is called under the tty_mutex, which
1496 * protects us from the tty struct or driver itself going away.
1497 *
1498 * On exit the tty device has the line discipline attached and
1499 * a reference count of 1. If a pair was created for pty/tty use
1500 * and the other was a pty master then it too has a reference count of 1.
1501 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001503 * failed open. The new code protects the open with a mutex, so it's
1504 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * relaxed for the (most common) case of reopening a tty.
1506 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001507
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001508struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
Alan Coxbf970ee2008-10-13 10:42:39 +01001510 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001511 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 /*
1514 * First time open is complex, especially for PTY devices.
1515 * This code guarantees that either everything succeeds and the
1516 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001517 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * and locked termios may be retained.)
1519 */
1520
Alan Cox73ec06f2008-10-13 10:42:29 +01001521 if (!try_module_get(driver->owner))
1522 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001524 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001525 if (!tty) {
1526 retval = -ENOMEM;
1527 goto err_module_put;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Alan Cox89c8d912012-08-08 16:30:13 +01001530 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001531 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001532 if (retval < 0)
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001533 goto err_free_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001534
Jiri Slaby04831dc2012-06-04 13:35:36 +02001535 if (!tty->port)
1536 tty->port = driver->ports[idx];
1537
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001538 WARN_RATELIMIT(!tty->port,
1539 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1540 __func__, tty->driver->name);
1541
Jiri Slaby967fab62012-10-18 22:26:46 +02001542 tty->port->itty = tty;
1543
Alan Cox37bdfb02008-02-08 04:18:47 -08001544 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001546 * If we fail here just call release_tty to clean up. No need
1547 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001549 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001550 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001551 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001552 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001553 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Peter Hurleyc8b710b2016-01-09 21:13:46 -08001555err_free_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001556 tty_unlock(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001557 free_tty_struct(tty);
1558err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001560 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001562 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001563err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001564 tty_unlock(tty);
Peter Hurley339f36b2015-11-08 13:01:13 -05001565 tty_info_ratelimited(tty, "ldisc open failed (%d), clearing slot %d\n",
1566 retval, idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001567 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001568 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Peter Hurleya3123fd2016-01-09 21:13:48 -08001571static void tty_free_termios(struct tty_struct *tty)
Alan Coxfeebed62008-10-13 10:41:30 +01001572{
1573 struct ktermios *tp;
1574 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001575
1576 /* If the port is going to reset then it has no termios to save */
1577 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1578 return;
1579
1580 /* Stash the termios data */
1581 tp = tty->driver->termios[idx];
1582 if (tp == NULL) {
1583 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Peter Hurley25080652015-11-08 13:01:11 -05001584 if (tp == NULL)
Alan Cox36b3c072012-07-17 17:06:57 +01001585 return;
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001586 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001587 }
Alan Cox36b3c072012-07-17 17:06:57 +01001588 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001589}
Alan Coxfeebed62008-10-13 10:41:30 +01001590
Peter Hurleya2965b72013-03-11 16:44:35 -04001591/**
Peter Hurley949aa642014-11-05 12:12:57 -05001592 * tty_flush_works - flush all works of a tty/pty pair
1593 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001594 *
Peter Hurley949aa642014-11-05 12:12:57 -05001595 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001596 */
1597static void tty_flush_works(struct tty_struct *tty)
1598{
1599 flush_work(&tty->SAK_work);
1600 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001601 if (tty->link) {
1602 flush_work(&tty->link->SAK_work);
1603 flush_work(&tty->link->hangup_work);
1604 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001605}
Alan Coxfeebed62008-10-13 10:41:30 +01001606
Alan Coxaf9b8972006-08-27 01:24:01 -07001607/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001608 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001609 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001610 *
1611 * Releases memory associated with a tty structure, and clears out the
1612 * driver table slots. This function is called when a device is no longer
1613 * in use. It also gets called when setup of a device fails.
1614 *
1615 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001616 * takes the file list lock internally when working on the list
1617 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001618 *
1619 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001620 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
Alan Coxb50989d2009-09-19 13:13:22 -07001622static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Alan Coxb50989d2009-09-19 13:13:22 -07001624 struct tty_struct *tty =
1625 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001626 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001627 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Dave Youngf278a2f2009-09-27 16:00:42 +00001629 if (tty->ops->cleanup)
1630 tty->ops->cleanup(tty);
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001633 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001634 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001635
Nick Pigginee2ffa02010-08-18 04:37:35 +10001636 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001638 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001639
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001640 put_pid(tty->pgrp);
1641 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 free_tty_struct(tty);
1643}
1644
Alan Coxb50989d2009-09-19 13:13:22 -07001645static void queue_release_one_tty(struct kref *kref)
1646{
1647 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001648
Alan Coxb50989d2009-09-19 13:13:22 -07001649 /* The hangup queue is now free so we can reuse it rather than
1650 waste a chunk of memory for each port */
1651 INIT_WORK(&tty->hangup_work, release_one_tty);
1652 schedule_work(&tty->hangup_work);
1653}
1654
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001655/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001656 * tty_kref_put - release a tty kref
1657 * @tty: tty device
1658 *
1659 * Release a reference to a tty device and if need be let the kref
1660 * layer destruct the object for us
1661 */
1662
1663void tty_kref_put(struct tty_struct *tty)
1664{
1665 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001666 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001667}
1668EXPORT_SYMBOL(tty_kref_put);
1669
1670/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001671 * release_tty - release tty structure memory
1672 *
1673 * Release both @tty and a possible linked partner (think pty pair),
1674 * and decrement the refcount of the backing module.
1675 *
1676 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001677 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001678 * takes the file list lock internally when working on the list
1679 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001680 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001681 */
1682static void release_tty(struct tty_struct *tty, int idx)
1683{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001684 /* This should always be true but check for the moment */
1685 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001686 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001687 if (tty->ops->shutdown)
1688 tty->ops->shutdown(tty);
1689 tty_free_termios(tty);
1690 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001691 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001692 if (tty->link)
1693 tty->link->port->itty = NULL;
Peter Hurleye1760582015-10-17 16:36:23 -04001694 tty_buffer_cancel_work(tty->port);
Alan Cox36b3c072012-07-17 17:06:57 +01001695
Markus Elfringa211b1a2014-11-21 13:42:29 +01001696 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001697 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001698}
1699
Alan Coxeeb89d92009-11-30 13:18:29 +00001700/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001701 * tty_release_checks - check a tty before real release
1702 * @tty: tty to check
1703 * @o_tty: link of @tty (if any)
1704 * @idx: index of the tty
1705 *
1706 * Performs some paranoid checking before true release of the @tty.
1707 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1708 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001709static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001710{
1711#ifdef TTY_PARANOIA_CHECK
1712 if (idx < 0 || idx >= tty->driver->num) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001713 tty_debug(tty, "bad idx %d\n", idx);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001714 return -1;
1715 }
1716
1717 /* not much to check for devpts */
1718 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1719 return 0;
1720
1721 if (tty != tty->driver->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001722 tty_debug(tty, "bad driver table[%d] = %p\n",
1723 idx, tty->driver->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001724 return -1;
1725 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001726 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001727 struct tty_struct *o_tty = tty->link;
1728
Jiri Slaby955787ca2011-11-11 10:47:23 +01001729 if (o_tty != tty->driver->other->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001730 tty_debug(tty, "bad other table[%d] = %p\n",
1731 idx, tty->driver->other->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001732 return -1;
1733 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001734 if (o_tty->link != tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001735 tty_debug(tty, "bad link = %p\n", o_tty->link);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001736 return -1;
1737 }
1738 }
1739#endif
1740 return 0;
1741}
1742
1743/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001744 * tty_release - vfs callback for close
1745 * @inode: inode of tty
1746 * @filp: file pointer for handle to tty
1747 *
1748 * Called the last time each file handle is closed that references
1749 * this tty. There may however be several such references.
1750 *
1751 * Locking:
1752 * Takes bkl. See tty_release_dev
1753 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 * Even releasing the tty structures is a tricky business.. We have
1755 * to be very careful that the structures are all released at the
1756 * same time, as interrupts might otherwise get the wrong pointers.
1757 *
1758 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1759 * lead to double frees or releasing memory still in use.
1760 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001761
1762int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Nick Piggind996b622010-08-18 04:37:36 +10001764 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001765 struct tty_struct *o_tty = NULL;
1766 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 int idx;
Peter Hurley37b16452014-10-16 13:51:30 -04001768 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001769 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001770
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001771 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Alan Cox89c8d912012-08-08 16:30:13 +01001774 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001775 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Arnd Bergmannec79d602010-06-01 22:53:01 +02001777 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001780 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1781 tty->driver->subtype == PTY_TYPE_MASTER)
1782 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Peter Hurley359b9fb2014-11-05 12:12:59 -05001784 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001785 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Peter Hurleyd435cef2015-11-08 13:01:19 -05001789 tty_debug_hangup(tty, "releasing (count=%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Alan Coxf34d7a52008-04-30 00:54:13 -07001791 if (tty->ops->close)
1792 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Peter Hurley2aff5e22014-11-05 12:13:01 -05001794 /* If tty is pty master, lock the slave pty (stable lock order) */
1795 tty_lock_slave(o_tty);
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * Sanity check: if tty->count is going to zero, there shouldn't be
1799 * any waiters on tty->read_wait or tty->write_wait. We test the
1800 * wait queues and kick everyone out _before_ actually starting to
1801 * close. This ensures that we won't block while releasing the tty
1802 * structure.
1803 *
1804 * The test for the o_tty closing is necessary, since the master and
1805 * slave sides may close in any order. If the slave side closes out
1806 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001807 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
1810 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 do_sleep = 0;
1812
Peter Hurley324c1652014-11-05 12:12:56 -05001813 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001815 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 do_sleep++;
1817 }
1818 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001819 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 do_sleep++;
1821 }
1822 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001823 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001825 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 do_sleep++;
1827 }
1828 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001829 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 do_sleep++;
1831 }
1832 }
1833 if (!do_sleep)
1834 break;
1835
Peter Hurley494c1ea2014-10-16 13:54:36 -04001836 if (once) {
1837 once = 0;
Peter Hurley339f36b2015-11-08 13:01:13 -05001838 tty_warn(tty, "read/write wait queue active!\n");
Peter Hurley494c1ea2014-10-16 13:54:36 -04001839 }
Peter Hurley37b16452014-10-16 13:51:30 -04001840 schedule_timeout_killable(timeout);
1841 if (timeout < 120 * HZ)
1842 timeout = 2 * timeout + 1;
1843 else
1844 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001847 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (--o_tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001849 tty_warn(tty, "bad slave count (%d)\n", o_tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 o_tty->count = 0;
1851 }
1852 }
1853 if (--tty->count < 0) {
Peter Hurley339f36b2015-11-08 13:01:13 -05001854 tty_warn(tty, "bad tty->count (%d)\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 tty->count = 0;
1856 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /*
1859 * We've decremented tty->count, so we need to remove this file
1860 * descriptor off the tty->tty_files list; this serves two
1861 * purposes:
1862 * - check_tty_count sees the correct number of file descriptors
1863 * associated with this tty.
1864 * - do_tty_hangup no longer sees this file descriptor as
1865 * something that needs to be handled for hangups.
1866 */
Nick Piggind996b622010-08-18 04:37:36 +10001867 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * Perform some housekeeping before deciding whether to return.
1871 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * If _either_ side is closing, make sure there aren't any
1873 * processes that still think tty or o_tty is their controlling
1874 * tty.
1875 */
Peter Hurley324c1652014-11-05 12:12:56 -05001876 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001878 session_clear_tty(tty->session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001879 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001880 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 read_unlock(&tasklist_lock);
1882 }
1883
Peter Hurley324c1652014-11-05 12:12:56 -05001884 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001885 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001886
Peter Hurley2aff5e22014-11-05 12:13:01 -05001887 tty_unlock_slave(o_tty);
1888 tty_unlock(tty);
1889
Peter Hurley04980702014-11-05 12:12:52 -05001890 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001891 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001892
Peter Hurley324c1652014-11-05 12:12:56 -05001893 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001894 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001895
Peter Hurleyaccff792015-07-12 22:49:09 -04001896 tty_debug_hangup(tty, "final close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001898 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Peter Hurley62462ae2014-11-05 12:12:58 -05001900 tty_ldisc_release(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001901
1902 /* Wait for pending work before tty destruction commmences */
1903 tty_flush_works(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001904
Peter Hurleyd435cef2015-11-08 13:01:19 -05001905 tty_debug_hangup(tty, "freeing structure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001907 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001908 * the slots and preserving the termios structure. The tty_unlock_pair
1909 * should be safe as we keep a kref while the tty is locked (so the
1910 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 */
Alan Coxd1552552012-07-27 18:02:54 +01001912 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001913 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001914 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Alan Coxeeb89d92009-11-30 13:18:29 +00001916 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917}
1918
Alan Coxaf9b8972006-08-27 01:24:01 -07001919/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001920 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001921 * @device: device number
1922 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001923 * @return: locked tty of the current task iff @device is /dev/tty
1924 *
1925 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001926 *
1927 * We cannot return driver and index like for the other nodes because
1928 * devpts will not work then. It expects inodes to be from devpts FS.
1929 */
1930static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1931{
1932 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001933 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001934
1935 if (device != MKDEV(TTYAUX_MAJOR, 0))
1936 return NULL;
1937
1938 tty = get_current_tty();
1939 if (!tty)
1940 return ERR_PTR(-ENXIO);
1941
1942 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1943 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001944 tty_lock(tty);
1945 tty_kref_put(tty); /* safe to drop the kref now */
1946
1947 retval = tty_reopen(tty);
1948 if (retval < 0) {
1949 tty_unlock(tty);
1950 tty = ERR_PTR(retval);
1951 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001952 return tty;
1953}
1954
1955/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001956 * tty_lookup_driver - lookup a tty driver for a given device file
1957 * @device: device number
1958 * @filp: file pointer to tty
1959 * @noctty: set if the device should not become a controlling tty
1960 * @index: index for the device in the @return driver
1961 * @return: driver for this inode (with increased refcount)
1962 *
1963 * If @return is not erroneous, the caller is responsible to decrement the
1964 * refcount by tty_driver_kref_put.
1965 *
1966 * Locking: tty_mutex protects get_tty_driver
1967 */
1968static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
Peter Hurley11e1d4a2016-01-09 21:13:52 -08001969 int *index)
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001970{
1971 struct tty_driver *driver;
1972
Jiri Slaby2cd00502011-11-09 21:33:22 +01001973 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001974#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001975 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001976 extern struct tty_driver *console_driver;
1977 driver = tty_driver_kref_get(console_driver);
1978 *index = fg_console;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001979 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001980 }
1981#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001982 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001983 struct tty_driver *console_driver = console_device(index);
1984 if (console_driver) {
1985 driver = tty_driver_kref_get(console_driver);
1986 if (driver) {
1987 /* Don't let /dev/console block */
1988 filp->f_flags |= O_NONBLOCK;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001989 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001990 }
1991 }
1992 return ERR_PTR(-ENODEV);
1993 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01001994 default:
1995 driver = get_tty_driver(device, index);
1996 if (!driver)
1997 return ERR_PTR(-ENODEV);
1998 break;
1999 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002000 return driver;
2001}
2002
2003/**
Peter Hurleyd6203d02016-01-09 21:13:53 -08002004 * tty_open_by_driver - open a tty device
2005 * @device: dev_t of device to open
2006 * @inode: inode of device file
2007 * @filp: file pointer to tty
2008 *
2009 * Performs the driver lookup, checks for a reopen, or otherwise
2010 * performs the first-time tty initialization.
2011 *
2012 * Returns the locked initialized or re-opened &tty_struct
2013 *
2014 * Claims the global tty_mutex to serialize:
2015 * - concurrent first-time tty initialization
2016 * - concurrent tty driver removal w/ lookup
2017 * - concurrent tty removal from driver table
2018 */
2019static struct tty_struct *tty_open_by_driver(dev_t device, struct inode *inode,
2020 struct file *filp)
2021{
2022 struct tty_struct *tty;
2023 struct tty_driver *driver = NULL;
2024 int index = -1;
2025 int retval;
2026
2027 mutex_lock(&tty_mutex);
2028 driver = tty_lookup_driver(device, filp, &index);
2029 if (IS_ERR(driver)) {
2030 mutex_unlock(&tty_mutex);
2031 return ERR_CAST(driver);
2032 }
2033
2034 /* check whether we're reopening an existing tty */
2035 tty = tty_driver_lookup_tty(driver, inode, index);
2036 if (IS_ERR(tty)) {
2037 mutex_unlock(&tty_mutex);
2038 goto out;
2039 }
2040
2041 if (tty) {
2042 mutex_unlock(&tty_mutex);
2043 retval = tty_lock_interruptible(tty);
2044 if (retval) {
2045 if (retval == -EINTR)
2046 retval = -ERESTARTSYS;
2047 tty = ERR_PTR(retval);
2048 goto out;
2049 }
2050 /* safe to drop the kref from tty_driver_lookup_tty() */
2051 tty_kref_put(tty);
2052 retval = tty_reopen(tty);
2053 if (retval < 0) {
2054 tty_unlock(tty);
2055 tty = ERR_PTR(retval);
2056 }
2057 } else { /* Returns with the tty_lock held for now */
2058 tty = tty_init_dev(driver, index);
2059 mutex_unlock(&tty_mutex);
2060 }
2061out:
2062 tty_driver_kref_put(driver);
2063 return tty;
2064}
2065
2066/**
Alan Coxeeb89d92009-11-30 13:18:29 +00002067 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002068 * @inode: inode of device file
2069 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002071 * tty_open and tty_release keep up the tty count that contains the
2072 * number of opens done on a tty. We cannot use the inode-count, as
2073 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002075 * Open-counting is needed for pty masters, as well as for keeping
2076 * track of serial lines: DTR is dropped when the last close happens.
2077 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2078 *
2079 * The termios state of a pty is reset on first open so that
2080 * settings don't persist across reuse.
2081 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002082 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002083 * tty->count should protect the rest.
2084 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01002085 *
2086 * Note: the tty_unlock/lock cases without a ref are only safe due to
2087 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002089
Alan Coxeeb89d92009-11-30 13:18:29 +00002090static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
Jiri Slabyb82154a2011-11-09 21:33:19 +01002092 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 int noctty, retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07002095 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002100 retval = tty_alloc_file(filp);
2101 if (retval)
2102 return -ENOMEM;
2103
Jiri Slabyb82154a2011-11-09 21:33:19 +01002104 tty = tty_open_current_tty(device, filp);
Peter Hurleyd6203d02016-01-09 21:13:53 -08002105 if (!tty)
2106 tty = tty_open_by_driver(device, inode, filp);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002107
Alan Coxeeb89d92009-11-30 13:18:29 +00002108 if (IS_ERR(tty)) {
Peter Hurleyd6203d02016-01-09 21:13:53 -08002109 tty_free_file(filp);
Jiri Slabyba5db442011-11-09 21:33:21 +01002110 retval = PTR_ERR(tty);
Peter Hurley7f22f6c2016-01-09 21:13:45 -08002111 if (retval != -EAGAIN || signal_pending(current))
Peter Hurleyd6203d02016-01-09 21:13:53 -08002112 return retval;
Peter Hurley7f22f6c2016-01-09 21:13:45 -08002113 schedule();
2114 goto retry_open;
Alan Coxeeb89d92009-11-30 13:18:29 +00002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002117 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10002118
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002119 check_tty_count(tty, __func__);
Peter Hurleyd435cef2015-11-08 13:01:19 -05002120 tty_debug_hangup(tty, "opening (count=%d)\n", tty->count);
Peter Hurleyaccff792015-07-12 22:49:09 -04002121
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03002122 if (tty->ops->open)
2123 retval = tty->ops->open(tty, filp);
2124 else
2125 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 filp->f_flags = saved_flags;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (retval) {
Peter Hurleyd435cef2015-11-08 13:01:19 -05002129 tty_debug_hangup(tty, "open error %d, releasing\n", retval);
Peter Hurleyaccff792015-07-12 22:49:09 -04002130
Alan Cox89c8d912012-08-08 16:30:13 +01002131 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002132 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002133 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002135
2136 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 schedule();
2140 /*
2141 * Need to reset f_op in case a hangup happened.
2142 */
Peter Hurley12569372014-11-05 12:26:24 -05002143 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 filp->f_op = &tty_fops;
2145 goto retry_open;
2146 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05002147 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00002148
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002149
Peter Hurley2c411c12014-10-16 14:59:47 -04002150 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002151 spin_lock_irq(&current->sighand->siglock);
Peter Hurley11e1d4a2016-01-09 21:13:52 -08002152 noctty = (filp->f_flags & O_NOCTTY) ||
2153 device == MKDEV(TTY_MAJOR, 0) ||
2154 device == MKDEV(TTYAUX_MAJOR, 1) ||
2155 (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2156 tty->driver->subtype == PTY_TYPE_MASTER);
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (!noctty &&
2159 current->signal->leader &&
2160 !current->signal->tty &&
Jann Horn0c5562712015-10-04 19:29:12 +02002161 tty->session == NULL) {
2162 /*
2163 * Don't let a process that only has write access to the tty
2164 * obtain the privileges associated with having a tty as
2165 * controlling terminal (being able to reopen it with full
2166 * access through /dev/tty, being able to perform pushback).
2167 * Many distributions set the group of all ttys to "tty" and
2168 * grant write-only access to all terminals for setgid tty
2169 * binaries, which should not imply full privileges on all ttys.
2170 *
2171 * This could theoretically break old code that performs open()
2172 * on a write-only file descriptor. In that case, it might be
2173 * necessary to also permit this if
2174 * inode_permission(inode, MAY_READ) == 0.
2175 */
2176 if (filp->f_mode & FMODE_READ)
2177 __proc_set_tty(tty);
2178 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002179 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley2c411c12014-10-16 14:59:47 -04002180 read_unlock(&tasklist_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01002181 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return 0;
2183}
2184
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Alan Coxaf9b8972006-08-27 01:24:01 -07002187/**
2188 * tty_poll - check tty status
2189 * @filp: file being polled
2190 * @wait: poll wait structures to update
2191 *
2192 * Call the line discipline polling method to obtain the poll
2193 * status of the device.
2194 *
2195 * Locking: locks called line discipline but ldisc poll method
2196 * may be re-entered freely by other callers.
2197 */
2198
Alan Cox37bdfb02008-02-08 04:18:47 -08002199static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Nick Piggind996b622010-08-18 04:37:36 +10002201 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct tty_ldisc *ld;
2203 int ret = 0;
2204
Al Viro6131ffa2013-02-27 16:59:05 -05002205 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002209 if (!ld)
2210 return hung_up_tty_poll(filp, wait);
Alan Coxa352def2008-07-16 21:53:12 +01002211 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002212 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 tty_ldisc_deref(ld);
2214 return ret;
2215}
2216
Arnd Bergmannec79d602010-06-01 22:53:01 +02002217static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
Nick Piggind996b622010-08-18 04:37:36 +10002219 struct tty_struct *tty = file_tty(filp);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002220 struct tty_ldisc *ldisc;
Alan Cox47f86832008-04-30 00:53:30 -07002221 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002222 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Al Viro6131ffa2013-02-27 16:59:05 -05002224 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002225 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 retval = fasync_helper(fd, filp, on, &tty->fasync);
2228 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002231 ldisc = tty_ldisc_ref(tty);
2232 if (ldisc) {
2233 if (ldisc->ops->fasync)
2234 ldisc->ops->fasync(tty, on);
2235 tty_ldisc_deref(ldisc);
2236 }
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002239 enum pid_type type;
2240 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002241
Alan Cox47f86832008-04-30 00:53:30 -07002242 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002243 if (tty->pgrp) {
2244 pid = tty->pgrp;
2245 type = PIDTYPE_PGID;
2246 } else {
2247 pid = task_pid(current);
2248 type = PIDTYPE_PID;
2249 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002250 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002251 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002252 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002253 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002254 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002256out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002257 return retval;
2258}
2259
2260static int tty_fasync(int fd, struct file *filp, int on)
2261{
Alan Cox89c8d912012-08-08 16:30:13 +01002262 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002263 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002264
2265 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002266 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002267 tty_unlock(tty);
2268
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002269 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
Alan Coxaf9b8972006-08-27 01:24:01 -07002272/**
2273 * tiocsti - fake input character
2274 * @tty: tty to fake input into
2275 * @p: pointer to character
2276 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002277 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002278 * input management.
2279 *
2280 * FIXME: does not honour flow control ??
2281 *
2282 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04002283 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002284 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002285 *
2286 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002287 */
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289static int tiocsti(struct tty_struct *tty, char __user *p)
2290{
2291 char ch, mbz = 0;
2292 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2295 return -EPERM;
2296 if (get_user(ch, p))
2297 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002298 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002300 if (!ld)
2301 return -EIO;
Alan Coxa352def2008-07-16 21:53:12 +01002302 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 tty_ldisc_deref(ld);
2304 return 0;
2305}
2306
Alan Coxaf9b8972006-08-27 01:24:01 -07002307/**
2308 * tiocgwinsz - implement window query ioctl
2309 * @tty; tty
2310 * @arg: user buffer for result
2311 *
Alan Cox808a0d32006-09-29 02:00:40 -07002312 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002313 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002314 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002315 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002316 */
2317
Alan Cox37bdfb02008-02-08 04:18:47 -08002318static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Alan Cox808a0d32006-09-29 02:00:40 -07002320 int err;
2321
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002322 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002323 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002324 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002325
2326 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327}
2328
Alan Coxaf9b8972006-08-27 01:24:01 -07002329/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002330 * tty_do_resize - resize event
2331 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002332 * @rows: rows (character)
2333 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002334 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002335 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002336 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002337 */
2338
Alan Coxfc6f6232009-01-02 13:43:17 +00002339int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
Alan Coxfc6f6232009-01-02 13:43:17 +00002341 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Alan Coxfc6f6232009-01-02 13:43:17 +00002343 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002344 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002345 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002346 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002347
Peter Hurley5b239542014-10-16 14:59:45 -04002348 /* Signal the foreground process group */
2349 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002350 if (pgrp)
2351 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002352 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002353
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002354 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002355done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002356 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return 0;
2358}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002359EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Alan Coxaf9b8972006-08-27 01:24:01 -07002361/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002362 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002363 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002364 * @arg: user buffer for result
2365 *
2366 * Copies the user idea of the window size to the kernel. Traditionally
2367 * this is just advisory information but for the Linux console it
2368 * actually has driver level meaning and triggers a VC resize.
2369 *
2370 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002371 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002372 * tty termios mutex and ctrl_lock. The console takes its own lock
2373 * then calls into the default method.
2374 */
2375
Alan Coxfc6f6232009-01-02 13:43:17 +00002376static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002377{
2378 struct winsize tmp_ws;
2379 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2380 return -EFAULT;
2381
2382 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002383 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002384 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002385 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002386}
2387
2388/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002389 * tioccons - allow admin to move logical console
2390 * @file: the file to become console
2391 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002392 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002393 *
2394 * Locking: uses redirect_lock to guard the redirect information
2395 */
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397static int tioccons(struct file *file)
2398{
2399 if (!capable(CAP_SYS_ADMIN))
2400 return -EPERM;
2401 if (file->f_op->write == redirected_tty_write) {
2402 struct file *f;
2403 spin_lock(&redirect_lock);
2404 f = redirect;
2405 redirect = NULL;
2406 spin_unlock(&redirect_lock);
2407 if (f)
2408 fput(f);
2409 return 0;
2410 }
2411 spin_lock(&redirect_lock);
2412 if (redirect) {
2413 spin_unlock(&redirect_lock);
2414 return -EBUSY;
2415 }
Al Virocb0942b2012-08-27 14:48:26 -04002416 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 spin_unlock(&redirect_lock);
2418 return 0;
2419}
2420
Alan Coxaf9b8972006-08-27 01:24:01 -07002421/**
2422 * fionbio - non blocking ioctl
2423 * @file: file to set blocking value
2424 * @p: user parameter
2425 *
2426 * Historical tty interfaces had a blocking control ioctl before
2427 * the generic functionality existed. This piece of history is preserved
2428 * in the expected tty API of posix OS's.
2429 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002430 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002431 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433static int fionbio(struct file *file, int __user *p)
2434{
2435 int nonblock;
2436
2437 if (get_user(nonblock, p))
2438 return -EFAULT;
2439
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002440 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (nonblock)
2442 file->f_flags |= O_NONBLOCK;
2443 else
2444 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002445 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 return 0;
2447}
2448
Alan Coxaf9b8972006-08-27 01:24:01 -07002449/**
2450 * tiocsctty - set controlling tty
2451 * @tty: tty structure
2452 * @arg: user argument
2453 *
2454 * This ioctl is used to manage job control. It permits a session
2455 * leader to set this tty as the controlling tty for the session.
2456 *
2457 * Locking:
Peter Hurleye218eb32014-10-16 14:59:49 -04002458 * Takes tty_lock() to serialize proc_set_tty() for this tty
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002459 * Takes tasklist_lock internally to walk sessions
2460 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002461 */
2462
Jann Horn0c5562712015-10-04 19:29:12 +02002463static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002465 int ret = 0;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002466
Peter Hurleye218eb32014-10-16 14:59:49 -04002467 tty_lock(tty);
Peter Hurley2c411c12014-10-16 14:59:47 -04002468 read_lock(&tasklist_lock);
2469
2470 if (current->signal->leader && (task_session(current) == tty->session))
2471 goto unlock;
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /*
2474 * The process must be a session leader and
2475 * not have a controlling tty already.
2476 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002477 if (!current->signal->leader || current->signal->tty) {
2478 ret = -EPERM;
2479 goto unlock;
2480 }
2481
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002482 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /*
2484 * This tty is already the controlling
2485 * tty for another session group!
2486 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002487 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /*
2489 * Steal it away
2490 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002491 session_clear_tty(tty->session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002492 } else {
2493 ret = -EPERM;
2494 goto unlock;
2495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
Jann Horn0c5562712015-10-04 19:29:12 +02002497
2498 /* See the comment in tty_open(). */
2499 if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
2500 ret = -EPERM;
2501 goto unlock;
2502 }
2503
Peter Hurleybce65f12014-10-16 14:59:43 -04002504 proc_set_tty(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002505unlock:
Peter Hurley2c411c12014-10-16 14:59:47 -04002506 read_unlock(&tasklist_lock);
Peter Hurleye218eb32014-10-16 14:59:49 -04002507 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
Alan Coxaf9b8972006-08-27 01:24:01 -07002511/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002512 * tty_get_pgrp - return a ref counted pgrp pid
2513 * @tty: tty to read
2514 *
2515 * Returns a refcounted instance of the pid struct for the process
2516 * group controlling the tty.
2517 */
2518
2519struct pid *tty_get_pgrp(struct tty_struct *tty)
2520{
2521 unsigned long flags;
2522 struct pid *pgrp;
2523
2524 spin_lock_irqsave(&tty->ctrl_lock, flags);
2525 pgrp = get_pid(tty->pgrp);
2526 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2527
2528 return pgrp;
2529}
2530EXPORT_SYMBOL_GPL(tty_get_pgrp);
2531
Peter Hurleye1c22962014-10-16 14:59:48 -04002532/*
2533 * This checks not only the pgrp, but falls back on the pid if no
2534 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2535 * without this...
2536 *
2537 * The caller must hold rcu lock or the tasklist lock.
2538 */
2539static struct pid *session_of_pgrp(struct pid *pgrp)
2540{
2541 struct task_struct *p;
2542 struct pid *sid = NULL;
2543
2544 p = pid_task(pgrp, PIDTYPE_PGID);
2545 if (p == NULL)
2546 p = pid_task(pgrp, PIDTYPE_PID);
2547 if (p != NULL)
2548 sid = task_session(p);
2549
2550 return sid;
2551}
2552
Alan Cox5d0fdf12008-04-30 00:53:31 -07002553/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002554 * tiocgpgrp - get process group
2555 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002556 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002557 * @p: returned pid
2558 *
2559 * Obtain the process group of the tty. If there is no process group
2560 * return an error.
2561 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002562 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002563 */
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2566{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002567 struct pid *pid;
2568 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /*
2570 * (tty == real_tty) is a cheap way of
2571 * testing if the tty is NOT a master pty.
2572 */
2573 if (tty == real_tty && current->signal->tty != real_tty)
2574 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002575 pid = tty_get_pgrp(real_tty);
2576 ret = put_user(pid_vnr(pid), p);
2577 put_pid(pid);
2578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Alan Coxaf9b8972006-08-27 01:24:01 -07002581/**
2582 * tiocspgrp - attempt to set process group
2583 * @tty: tty passed by user
2584 * @real_tty: tty side device matching tty passed by user
2585 * @p: pid pointer
2586 *
2587 * Set the process group of the tty to the session passed. Only
2588 * permitted where the tty session is our session.
2589 *
Alan Cox47f86832008-04-30 00:53:30 -07002590 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002591 */
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2594{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002595 struct pid *pgrp;
2596 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 int retval = tty_check_change(real_tty);
2598
2599 if (retval == -EIO)
2600 return -ENOTTY;
2601 if (retval)
2602 return retval;
2603 if (!current->signal->tty ||
2604 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002605 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002607 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002609 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002611 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002612 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002613 retval = -ESRCH;
2614 if (!pgrp)
2615 goto out_unlock;
2616 retval = -EPERM;
2617 if (session_of_pgrp(pgrp) != task_session(current))
2618 goto out_unlock;
2619 retval = 0;
Peter Hurley1e86b5b2015-10-10 20:28:43 -04002620 spin_lock_irq(&tty->ctrl_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002621 put_pid(real_tty->pgrp);
2622 real_tty->pgrp = get_pid(pgrp);
Peter Hurley1e86b5b2015-10-10 20:28:43 -04002623 spin_unlock_irq(&tty->ctrl_lock);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002624out_unlock:
2625 rcu_read_unlock();
2626 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Alan Coxaf9b8972006-08-27 01:24:01 -07002629/**
2630 * tiocgsid - get session id
2631 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002632 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002633 * @p: pointer to returned session id
2634 *
2635 * Obtain the session id of the tty. If there is no session
2636 * return an error.
2637 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002638 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002639 */
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2642{
2643 /*
2644 * (tty == real_tty) is a cheap way of
2645 * testing if the tty is NOT a master pty.
2646 */
2647 if (tty == real_tty && current->signal->tty != real_tty)
2648 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002649 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002651 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652}
2653
Alan Coxaf9b8972006-08-27 01:24:01 -07002654/**
2655 * tiocsetd - set line discipline
2656 * @tty: tty device
2657 * @p: pointer to user data
2658 *
2659 * Set the line discipline according to user request.
2660 *
2661 * Locking: see tty_set_ldisc, this function is just a helper
2662 */
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664static int tiocsetd(struct tty_struct *tty, int __user *p)
2665{
Peter Hurleyc12da962016-01-10 22:41:04 -08002666 int disc;
Alan Cox04f378b2008-04-30 00:53:29 -07002667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Peter Hurleyc12da962016-01-10 22:41:04 -08002669 if (get_user(disc, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002671
Peter Hurleyc12da962016-01-10 22:41:04 -08002672 ret = tty_set_ldisc(tty, disc);
Alan Cox04f378b2008-04-30 00:53:29 -07002673
2674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
Alan Coxaf9b8972006-08-27 01:24:01 -07002677/**
Peter Hurley5c17c862016-01-10 22:40:55 -08002678 * tiocgetd - get line discipline
2679 * @tty: tty device
2680 * @p: pointer to user data
2681 *
2682 * Retrieves the line discipline id directly from the ldisc.
2683 *
2684 * Locking: waits for ldisc reference (in case the line discipline
2685 * is changing or the tty is being hungup)
2686 */
2687
2688static int tiocgetd(struct tty_struct *tty, int __user *p)
2689{
2690 struct tty_ldisc *ld;
2691 int ret;
2692
2693 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002694 if (!ld)
2695 return -EIO;
Peter Hurley5c17c862016-01-10 22:40:55 -08002696 ret = put_user(ld->ops->num, p);
2697 tty_ldisc_deref(ld);
2698 return ret;
2699}
2700
2701/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002702 * send_break - performed time break
2703 * @tty: device to break on
2704 * @duration: timeout in mS
2705 *
2706 * Perform a timed break on hardware that lacks its own driver level
2707 * timed break functionality.
2708 *
2709 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002710 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002711 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002712 */
2713
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002714static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Alan Cox9e989662008-07-22 11:18:03 +01002716 int retval;
2717
2718 if (tty->ops->break_ctl == NULL)
2719 return 0;
2720
2721 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2722 retval = tty->ops->break_ctl(tty, duration);
2723 else {
2724 /* Do the work ourselves */
2725 if (tty_write_lock(tty, 0) < 0)
2726 return -EINTR;
2727 retval = tty->ops->break_ctl(tty, -1);
2728 if (retval)
2729 goto out;
2730 if (!signal_pending(current))
2731 msleep_interruptible(duration);
2732 retval = tty->ops->break_ctl(tty, 0);
2733out:
2734 tty_write_unlock(tty);
2735 if (signal_pending(current))
2736 retval = -EINTR;
2737 }
2738 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Alan Coxaf9b8972006-08-27 01:24:01 -07002741/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002742 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002743 * @tty: tty device
2744 * @file: user file pointer
2745 * @p: pointer to result
2746 *
2747 * Obtain the modem status bits from the tty driver if the feature
2748 * is supported. Return -EINVAL if it is not available.
2749 *
2750 * Locking: none (up to the driver)
2751 */
2752
Alan Cox60b33c12011-02-14 16:26:14 +00002753static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 int retval = -EINVAL;
2756
Alan Coxf34d7a52008-04-30 00:54:13 -07002757 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002758 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 if (retval >= 0)
2761 retval = put_user(retval, p);
2762 }
2763 return retval;
2764}
2765
Alan Coxaf9b8972006-08-27 01:24:01 -07002766/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002767 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002768 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002769 * @cmd: command - clear bits, set bits or set all
2770 * @p: pointer to desired bits
2771 *
2772 * Set the modem status bits from the tty driver if the feature
2773 * is supported. Return -EINVAL if it is not available.
2774 *
2775 * Locking: none (up to the driver)
2776 */
2777
Alan Cox20b9d172011-02-14 16:26:50 +00002778static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 unsigned __user *p)
2780{
Alan Coxae677512008-07-16 21:56:54 +01002781 int retval;
2782 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Alan Coxae677512008-07-16 21:56:54 +01002784 if (tty->ops->tiocmset == NULL)
2785 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Alan Coxae677512008-07-16 21:56:54 +01002787 retval = get_user(val, p);
2788 if (retval)
2789 return retval;
2790 set = clear = 0;
2791 switch (cmd) {
2792 case TIOCMBIS:
2793 set = val;
2794 break;
2795 case TIOCMBIC:
2796 clear = val;
2797 break;
2798 case TIOCMSET:
2799 set = val;
2800 clear = ~val;
2801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
Alan Coxae677512008-07-16 21:56:54 +01002803 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2804 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002805 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806}
2807
Alan Coxd281da72010-09-16 18:21:24 +01002808static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2809{
2810 int retval = -EINVAL;
2811 struct serial_icounter_struct icount;
2812 memset(&icount, 0, sizeof(icount));
2813 if (tty->ops->get_icount)
2814 retval = tty->ops->get_icount(tty, &icount);
2815 if (retval != 0)
2816 return retval;
2817 if (copy_to_user(arg, &icount, sizeof(icount)))
2818 return -EFAULT;
2819 return 0;
2820}
2821
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002822static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2823{
2824 static DEFINE_RATELIMIT_STATE(depr_flags,
2825 DEFAULT_RATELIMIT_INTERVAL,
2826 DEFAULT_RATELIMIT_BURST);
2827 char comm[TASK_COMM_LEN];
2828 int flags;
2829
2830 if (get_user(flags, &ss->flags))
2831 return;
2832
2833 flags &= ASYNC_DEPRECATED;
2834
2835 if (flags && __ratelimit(&depr_flags))
2836 pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2837 __func__, get_task_comm(comm, current), flags);
2838}
2839
Peter Hurley8f166e02014-10-16 14:59:41 -04002840/*
2841 * if pty, return the slave side (real_tty)
2842 * otherwise, return self
2843 */
2844static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002845{
2846 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2847 tty->driver->subtype == PTY_TYPE_MASTER)
2848 tty = tty->link;
2849 return tty;
2850}
Alan Coxe8b70e72009-06-11 12:48:02 +01002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852/*
2853 * Split this up, as gcc can choke on it otherwise..
2854 */
Alan Cox04f378b2008-04-30 00:53:29 -07002855long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856{
Nick Piggind996b622010-08-18 04:37:36 +10002857 struct tty_struct *tty = file_tty(file);
2858 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 void __user *p = (void __user *)arg;
2860 int retval;
2861 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002862
Al Viro6131ffa2013-02-27 16:59:05 -05002863 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return -EINVAL;
2865
Alan Coxe8b70e72009-06-11 12:48:02 +01002866 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 /*
2869 * Factor out some common prep work
2870 */
2871 switch (cmd) {
2872 case TIOCSETD:
2873 case TIOCSBRK:
2874 case TIOCCBRK:
2875 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002876 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 retval = tty_check_change(tty);
2878 if (retval)
2879 return retval;
2880 if (cmd != TIOCCBRK) {
2881 tty_wait_until_sent(tty, 0);
2882 if (signal_pending(current))
2883 return -EINTR;
2884 }
2885 break;
2886 }
2887
Alan Cox9e989662008-07-22 11:18:03 +01002888 /*
2889 * Now do the stuff.
2890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002892 case TIOCSTI:
2893 return tiocsti(tty, p);
2894 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002895 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002896 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002897 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002898 case TIOCCONS:
2899 return real_tty != tty ? -EINVAL : tioccons(file);
2900 case FIONBIO:
2901 return fionbio(file, p);
2902 case TIOCEXCL:
2903 set_bit(TTY_EXCLUSIVE, &tty->flags);
2904 return 0;
2905 case TIOCNXCL:
2906 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2907 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002908 case TIOCGEXCL:
2909 {
2910 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2911 return put_user(excl, (int __user *)p);
2912 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002913 case TIOCNOTTY:
2914 if (current->signal->tty != tty)
2915 return -ENOTTY;
2916 no_tty();
2917 return 0;
2918 case TIOCSCTTY:
Peter Hurleyd1d3a0f2015-11-08 09:06:05 -05002919 return tiocsctty(real_tty, file, arg);
Alan Cox37bdfb02008-02-08 04:18:47 -08002920 case TIOCGPGRP:
2921 return tiocgpgrp(tty, real_tty, p);
2922 case TIOCSPGRP:
2923 return tiocspgrp(tty, real_tty, p);
2924 case TIOCGSID:
2925 return tiocgsid(tty, real_tty, p);
2926 case TIOCGETD:
Peter Hurley5c17c862016-01-10 22:40:55 -08002927 return tiocgetd(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002928 case TIOCSETD:
2929 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002930 case TIOCVHANGUP:
2931 if (!capable(CAP_SYS_ADMIN))
2932 return -EPERM;
2933 tty_vhangup(tty);
2934 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002935 case TIOCGDEV:
2936 {
2937 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2938 return put_user(ret, (unsigned int __user *)p);
2939 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002940 /*
2941 * Break handling
2942 */
2943 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002944 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002945 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002946 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002947 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002948 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002949 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002950 return 0;
2951 case TCSBRK: /* SVID version: non-zero arg --> no break */
2952 /* non-zero arg means wait for all output data
2953 * to be sent (performed above) but don't send break.
2954 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002956 if (!arg)
2957 return send_break(tty, 250);
2958 return 0;
2959 case TCSBRKP: /* support for POSIX tcsendbreak() */
2960 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Alan Cox37bdfb02008-02-08 04:18:47 -08002962 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002963 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002964 case TIOCMSET:
2965 case TIOCMBIC:
2966 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002967 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002968 case TIOCGICOUNT:
2969 retval = tty_tiocgicount(tty, p);
2970 /* For the moment allow fall through to the old method */
2971 if (retval != -EINVAL)
2972 return retval;
2973 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002974 case TCFLSH:
2975 switch (arg) {
2976 case TCIFLUSH:
2977 case TCIOFLUSH:
2978 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002979 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002980 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002981 }
2982 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002983 case TIOCSSERIAL:
2984 tty_warn_deprecated_flags(p);
2985 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002987 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002988 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (retval != -ENOIOCTLCMD)
2990 return retval;
2991 }
2992 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08002993 if (!ld)
2994 return hung_up_tty_ioctl(file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002996 if (ld->ops->ioctl) {
2997 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002999 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 tty_ldisc_deref(ld);
3002 return retval;
3003}
3004
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003005#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08003006static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003007 unsigned long arg)
3008{
Nick Piggind996b622010-08-18 04:37:36 +10003009 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003010 struct tty_ldisc *ld;
3011 int retval = -ENOIOCTLCMD;
3012
Al Viro6131ffa2013-02-27 16:59:05 -05003013 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003014 return -EINVAL;
3015
Alan Coxf34d7a52008-04-30 00:54:13 -07003016 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05003017 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003018 if (retval != -ENOIOCTLCMD)
3019 return retval;
3020 }
3021
3022 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -08003023 if (!ld)
3024 return hung_up_tty_compat_ioctl(file, cmd, arg);
Alan Coxa352def2008-07-16 21:53:12 +01003025 if (ld->ops->compat_ioctl)
3026 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02003027 else
3028 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07003029 tty_ldisc_deref(ld);
3030
3031 return retval;
3032}
3033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Al Viroc3c073f2012-08-21 22:32:06 -04003035static int this_tty(const void *t, struct file *file, unsigned fd)
3036{
3037 if (likely(file->f_op->read != tty_read))
3038 return 0;
3039 return file_tty(file) != t ? 0 : fd + 1;
3040}
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042/*
3043 * This implements the "Secure Attention Key" --- the idea is to
3044 * prevent trojan horses by killing all processes associated with this
3045 * tty when the user hits the "Secure Attention Key". Required for
3046 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08003047 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 * This code could be nicer; ideally it should send a HUP, wait a few
3049 * seconds, then send a INT, and then a KILL signal. But you then
3050 * have to coordinate with the init process, since all processes associated
3051 * with the current tty must be dead before the new getty is allowed
3052 * to spawn.
3053 *
3054 * Now, if it would be correct ;-/ The current code has a nasty hole -
3055 * it doesn't catch files in flight. We may send the descriptor to ourselves
3056 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3057 *
3058 * Nasty bug: do_SAK is being called in interrupt context. This can
3059 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3060 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003061void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063#ifdef TTY_SOFT_SAK
3064 tty_hangup(tty);
3065#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003066 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003067 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!tty)
3071 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003072 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08003073
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003074 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Alan Coxf34d7a52008-04-30 00:54:13 -07003076 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003079 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003080 do_each_pid_task(session, PIDTYPE_SID, p) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003081 tty_notice(tty, "SAK: killed process %d (%s): by session\n",
3082 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003083 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003084 } while_each_pid_task(session, PIDTYPE_SID, p);
Peter Hurley9b42bb72015-11-08 13:01:14 -05003085
3086 /* Now kill any processes that happen to have the tty open */
Eric W. Biederman652486f2006-03-28 16:11:02 -08003087 do_each_thread(g, p) {
3088 if (p->signal->tty == tty) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003089 tty_notice(tty, "SAK: killed process %d (%s): by controlling tty\n",
3090 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 send_sig(SIGKILL, p, 1);
3092 continue;
3093 }
3094 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04003095 i = iterate_fd(p->files, 0, this_tty, tty);
3096 if (i != 0) {
Peter Hurley9b42bb72015-11-08 13:01:14 -05003097 tty_notice(tty, "SAK: killed process %d (%s): by fd#%d\n",
3098 task_pid_nr(p), p->comm, i - 1);
Al Viroc3c073f2012-08-21 22:32:06 -04003099 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003102 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 read_unlock(&tasklist_lock);
3104#endif
3105}
3106
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003107static void do_SAK_work(struct work_struct *work)
3108{
3109 struct tty_struct *tty =
3110 container_of(work, struct tty_struct, SAK_work);
3111 __do_SAK(tty);
3112}
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114/*
3115 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3116 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3117 * the values which we write to it will be identical to the values which it
3118 * already has. --akpm
3119 */
3120void do_SAK(struct tty_struct *tty)
3121{
3122 if (!tty)
3123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 schedule_work(&tty->SAK_work);
3125}
3126
3127EXPORT_SYMBOL(do_SAK);
3128
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003129static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003130{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003131 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003132 return dev->devt == *devt;
3133}
3134
3135/* Must put_device() after it's unused! */
3136static struct device *tty_get_device(struct tty_struct *tty)
3137{
3138 dev_t devt = tty_devnum(tty);
3139 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
3140}
3141
3142
Alan Coxaf9b8972006-08-27 01:24:01 -07003143/**
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003144 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07003145 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003146 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07003147 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003148 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003150
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003151struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003153 struct tty_struct *tty;
3154
3155 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3156 if (!tty)
3157 return NULL;
3158
Alan Cox9c9f4de2008-10-13 10:37:26 +01003159 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01003161 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003162 tty->session = NULL;
3163 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01003164 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04003165 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04003166 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04003167 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04003168 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 init_waitqueue_head(&tty->write_wait);
3170 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003171 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003172 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07003173 spin_lock_init(&tty->ctrl_lock);
Peter Hurleyf9e053d2014-09-10 15:06:31 -04003174 spin_lock_init(&tty->flow_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003176 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01003177
3178 tty->driver = driver;
3179 tty->ops = driver->ops;
3180 tty->index = idx;
3181 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003182 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003183
3184 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185}
3186
Alan Coxf34d7a52008-04-30 00:54:13 -07003187/**
3188 * tty_put_char - write one character to a tty
3189 * @tty: tty
3190 * @ch: character
3191 *
3192 * Write one byte to the tty using the provided put_char method
3193 * if present. Returns the number of characters successfully output.
3194 *
3195 * Note: the specific put_char operation in the driver layer may go
3196 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003198
Alan Coxf34d7a52008-04-30 00:54:13 -07003199int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Alan Coxf34d7a52008-04-30 00:54:13 -07003201 if (tty->ops->put_char)
3202 return tty->ops->put_char(tty, ch);
3203 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
Alan Coxf34d7a52008-04-30 00:54:13 -07003205EXPORT_SYMBOL_GPL(tty_put_char);
3206
Alan Coxd81ed102008-10-13 10:41:42 +01003207struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003209static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3210 unsigned int index, unsigned int count)
3211{
Leon Yuc1a752b2015-09-07 13:08:37 +00003212 int err;
3213
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003214 /* init here, since reused cdevs cause crashes */
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003215 driver->cdevs[index] = cdev_alloc();
3216 if (!driver->cdevs[index])
3217 return -ENOMEM;
Leon Yuc1a752b2015-09-07 13:08:37 +00003218 driver->cdevs[index]->ops = &tty_fops;
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003219 driver->cdevs[index]->owner = driver->owner;
Leon Yuc1a752b2015-09-07 13:08:37 +00003220 err = cdev_add(driver->cdevs[index], dev, count);
3221 if (err)
3222 kobject_put(&driver->cdevs[index]->kobj);
3223 return err;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003224}
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003227 * tty_register_device - register a tty device
3228 * @driver: the tty driver that describes the tty device
3229 * @index: the index in the tty driver for this tty device
3230 * @device: a struct device that is associated with this tty device.
3231 * This field is optional, if there is no known struct device
3232 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003234 * Returns a pointer to the struct device for this tty device
3235 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003236 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003237 * This call is required to be made to register an individual tty device
3238 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3239 * that bit is not set, this function should not be called by a tty
3240 * driver.
3241 *
3242 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003244
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003245struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3246 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003248 return tty_register_device_attr(driver, index, device, NULL, NULL);
3249}
3250EXPORT_SYMBOL(tty_register_device);
3251
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003252static void tty_device_create_release(struct device *dev)
3253{
Peter Hurley83db1df2015-11-08 13:01:21 -05003254 dev_dbg(dev, "releasing...\n");
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003255 kfree(dev);
3256}
3257
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003258/**
3259 * tty_register_device_attr - register a tty device
3260 * @driver: the tty driver that describes the tty device
3261 * @index: the index in the tty driver for this tty device
3262 * @device: a struct device that is associated with this tty device.
3263 * This field is optional, if there is no known struct device
3264 * for this tty device it can be set to NULL safely.
3265 * @drvdata: Driver data to be set to device.
3266 * @attr_grp: Attribute group to be set on device.
3267 *
3268 * Returns a pointer to the struct device for this tty device
3269 * (or ERR_PTR(-EFOO) on error).
3270 *
3271 * This call is required to be made to register an individual tty device
3272 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3273 * that bit is not set, this function should not be called by a tty
3274 * driver.
3275 *
3276 * Locking: ??
3277 */
3278struct device *tty_register_device_attr(struct tty_driver *driver,
3279 unsigned index, struct device *device,
3280 void *drvdata,
3281 const struct attribute_group **attr_grp)
3282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003284 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3285 struct device *dev = NULL;
3286 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003287 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 if (index >= driver->num) {
Peter Hurley656fb862015-11-08 13:01:15 -05003290 pr_err("%s: Attempt to register invalid tty line number (%d)\n",
3291 driver->name, index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003292 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
3294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (driver->type == TTY_DRIVER_TYPE_PTY)
3296 pty_line_name(driver, index, name);
3297 else
3298 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003299
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003300 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003301 retval = tty_cdev_add(driver, devt, index, 1);
3302 if (retval)
3303 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003304 cdev = true;
3305 }
3306
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003307 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3308 if (!dev) {
3309 retval = -ENOMEM;
3310 goto error;
3311 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003312
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003313 dev->devt = devt;
3314 dev->class = tty_class;
3315 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003316 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003317 dev_set_name(dev, "%s", name);
3318 dev->groups = attr_grp;
3319 dev_set_drvdata(dev, drvdata);
3320
3321 retval = device_register(dev);
3322 if (retval)
3323 goto error;
3324
3325 return dev;
3326
3327error:
3328 put_device(dev);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003329 if (cdev) {
3330 cdev_del(driver->cdevs[index]);
3331 driver->cdevs[index] = NULL;
3332 }
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003333 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003335EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003338 * tty_unregister_device - unregister a tty device
3339 * @driver: the tty driver that describes the tty device
3340 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003342 * If a tty device is registered with a call to tty_register_device() then
3343 * this function must be called when the tty device is gone.
3344 *
3345 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348void tty_unregister_device(struct tty_driver *driver, unsigned index)
3349{
Alan Cox37bdfb02008-02-08 04:18:47 -08003350 device_destroy(tty_class,
3351 MKDEV(driver->major, driver->minor_start) + index);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003352 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3353 cdev_del(driver->cdevs[index]);
3354 driver->cdevs[index] = NULL;
3355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357EXPORT_SYMBOL(tty_unregister_device);
3358
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003359/**
3360 * __tty_alloc_driver -- allocate tty driver
3361 * @lines: count of lines this driver can handle at most
3362 * @owner: module which is repsonsible for this driver
3363 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3364 *
3365 * This should not be called directly, some of the provided macros should be
3366 * used instead. Use IS_ERR and friends on @retval.
3367 */
3368struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3369 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
3371 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003372 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Jiri Slaby0019b402012-08-08 22:26:43 +02003375 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003376 return ERR_PTR(-EINVAL);
3377
Jean Delvare506eb992007-07-15 23:40:14 -07003378 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003379 if (!driver)
3380 return ERR_PTR(-ENOMEM);
3381
3382 kref_init(&driver->kref);
3383 driver->magic = TTY_DRIVER_MAGIC;
3384 driver->num = lines;
3385 driver->owner = owner;
3386 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003387
3388 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3389 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3390 GFP_KERNEL);
3391 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3392 GFP_KERNEL);
3393 if (!driver->ttys || !driver->termios) {
3394 err = -ENOMEM;
3395 goto err_free_all;
3396 }
3397 }
3398
3399 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3400 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3401 GFP_KERNEL);
3402 if (!driver->ports) {
3403 err = -ENOMEM;
3404 goto err_free_all;
3405 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003406 cdevs = lines;
3407 }
3408
3409 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3410 if (!driver->cdevs) {
3411 err = -ENOMEM;
3412 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003413 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003416err_free_all:
3417 kfree(driver->ports);
3418 kfree(driver->ttys);
3419 kfree(driver->termios);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003420 kfree(driver->cdevs);
Jiri Slaby16a02082012-08-08 22:26:42 +02003421 kfree(driver);
3422 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003424EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
Alan Cox7d7b93c2008-10-13 10:42:09 +01003426static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003428 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3429 int i;
3430 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003431
3432 if (driver->flags & TTY_DRIVER_INSTALLED) {
3433 /*
3434 * Free the termios and termios_locked structures because
3435 * we don't want to get memory leaks when modular tty
3436 * drivers are removed from the kernel.
3437 */
3438 for (i = 0; i < driver->num; i++) {
3439 tp = driver->termios[i];
3440 if (tp) {
3441 driver->termios[i] = NULL;
3442 kfree(tp);
3443 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003444 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3445 tty_unregister_device(driver, i);
3446 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003447 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003448 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003449 cdev_del(driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003450 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003451 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003452 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003453 kfree(driver->termios);
3454 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 kfree(driver);
3456}
3457
Alan Cox7d7b93c2008-10-13 10:42:09 +01003458void tty_driver_kref_put(struct tty_driver *driver)
3459{
3460 kref_put(&driver->kref, destruct_tty_driver);
3461}
3462EXPORT_SYMBOL(tty_driver_kref_put);
3463
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003464void tty_set_operations(struct tty_driver *driver,
3465 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Alan Coxf34d7a52008-04-30 00:54:13 -07003467 driver->ops = op;
3468};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469EXPORT_SYMBOL(tty_set_operations);
3470
Alan Cox7d7b93c2008-10-13 10:42:09 +01003471void put_tty_driver(struct tty_driver *d)
3472{
3473 tty_driver_kref_put(d);
3474}
3475EXPORT_SYMBOL(put_tty_driver);
3476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477/*
3478 * Called by a tty driver to register itself.
3479 */
3480int tty_register_driver(struct tty_driver *driver)
3481{
3482 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003483 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003485 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003488 error = alloc_chrdev_region(&dev, driver->minor_start,
3489 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (!error) {
3491 driver->major = MAJOR(dev);
3492 driver->minor_start = MINOR(dev);
3493 }
3494 } else {
3495 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003496 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003498 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003499 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003501 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3502 error = tty_cdev_add(driver, dev, 0, driver->num);
3503 if (error)
3504 goto err_unreg_char;
3505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003507 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003509 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003510
3511 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003512 for (i = 0; i < driver->num; i++) {
3513 d = tty_register_device(driver, i, NULL);
3514 if (IS_ERR(d)) {
3515 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003516 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003517 }
3518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 }
3520 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003521 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003523
Jiri Slaby16a02082012-08-08 22:26:42 +02003524err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003525 for (i--; i >= 0; i--)
3526 tty_unregister_device(driver, i);
3527
3528 mutex_lock(&tty_mutex);
3529 list_del(&driver->tty_drivers);
3530 mutex_unlock(&tty_mutex);
3531
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003532err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003533 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003534err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003535 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537EXPORT_SYMBOL(tty_register_driver);
3538
3539/*
3540 * Called by a tty driver to unregister itself.
3541 */
3542int tty_unregister_driver(struct tty_driver *driver)
3543{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003544#if 0
3545 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if (driver->refcount)
3547 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003548#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3550 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003551 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003553 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 return 0;
3555}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557EXPORT_SYMBOL(tty_unregister_driver);
3558
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003559dev_t tty_devnum(struct tty_struct *tty)
3560{
3561 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3562}
3563EXPORT_SYMBOL(tty_devnum);
3564
Alan Coxd81ed102008-10-13 10:41:42 +01003565void tty_default_fops(struct file_operations *fops)
3566{
3567 *fops = tty_fops;
3568}
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570/*
3571 * Initialize the console device. This is called *early*, so
3572 * we can't necessarily depend on lots of kernel help here.
3573 * Just do some early initializations, and do the complex setup
3574 * later.
3575 */
3576void __init console_init(void)
3577{
3578 initcall_t *call;
3579
3580 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003581 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003584 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 * inform about problems etc..
3586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 call = __con_initcall_start;
3588 while (call < __con_initcall_end) {
3589 (*call)();
3590 call++;
3591 }
3592}
3593
Al Viro2c9ede52011-07-23 20:24:48 -04003594static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003595{
3596 if (!mode)
3597 return NULL;
3598 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3599 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3600 *mode = 0666;
3601 return NULL;
3602}
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604static int __init tty_class_init(void)
3605{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003606 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (IS_ERR(tty_class))
3608 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003609 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 return 0;
3611}
3612
3613postcore_initcall(tty_class_init);
3614
3615/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Kay Sieversfbc92a32010-12-01 18:51:05 +01003618static ssize_t show_cons_active(struct device *dev,
3619 struct device_attribute *attr, char *buf)
3620{
3621 struct console *cs[16];
3622 int i = 0;
3623 struct console *c;
3624 ssize_t count = 0;
3625
Torben Hohnac751ef2011-01-25 15:07:35 -08003626 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003627 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003628 if (!c->device)
3629 continue;
3630 if (!c->write)
3631 continue;
3632 if ((c->flags & CON_ENABLED) == 0)
3633 continue;
3634 cs[i++] = c;
3635 if (i >= ARRAY_SIZE(cs))
3636 break;
3637 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003638 while (i--) {
3639 int index = cs[i]->index;
3640 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3641
3642 /* don't resolve tty0 as some programs depend on it */
3643 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3644 count += tty_line_name(drv, index, buf + count);
3645 else
3646 count += sprintf(buf + count, "%s%d",
3647 cs[i]->name, cs[i]->index);
3648
3649 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3650 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003651 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003652
3653 return count;
3654}
3655static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3656
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003657static struct attribute *cons_dev_attrs[] = {
3658 &dev_attr_active.attr,
3659 NULL
3660};
3661
3662ATTRIBUTE_GROUPS(cons_dev);
3663
Kay Sieversfbc92a32010-12-01 18:51:05 +01003664static struct device *consdev;
3665
3666void console_sysfs_notify(void)
3667{
3668 if (consdev)
3669 sysfs_notify(&consdev->kobj, NULL, "active");
3670}
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672/*
3673 * Ok, now we can initialize the rest of the tty devices and can count
3674 * on memory allocations, interrupts etc..
3675 */
David Howells31d1d482010-08-06 16:34:43 +01003676int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
3678 cdev_init(&tty_cdev, &tty_fops);
3679 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3680 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3681 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003682 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 cdev_init(&console_cdev, &console_fops);
3685 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3686 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3687 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003688 consdev = device_create_with_groups(tty_class, NULL,
3689 MKDEV(TTYAUX_MAJOR, 1), NULL,
3690 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003691 if (IS_ERR(consdev))
3692 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003695 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696#endif
3697 return 0;
3698}
David Howells31d1d482010-08-06 16:34:43 +01003699