blob: ef8ee34670c322ba3a76c2375687fb6a5016eab8 [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,
126 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
129EXPORT_SYMBOL(tty_std_termios);
130
131/* This list gets poked at by procfs and various bits of boot up code. This
132 could do with some rationalisation such as pulling the tty proc function
133 into this file */
Alan Cox37bdfb02008-02-08 04:18:47 -0800134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135LIST_HEAD(tty_drivers); /* linked list of tty drivers */
136
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800137/* Mutex to protect creating and releasing a tty. This is shared with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 vt.c for deeply disgusting hack reasons */
Ingo Molnar70522e12006-03-23 03:00:31 -0800139DEFINE_MUTEX(tty_mutex);
Alan Coxde2a84f2006-09-29 02:00:57 -0700140EXPORT_SYMBOL(tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Nick Pigginee2ffa02010-08-18 04:37:35 +1000142/* Spinlock to protect the tty->tty_files list */
143DEFINE_SPINLOCK(tty_files_lock);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
146static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
Alan Cox37bdfb02008-02-08 04:18:47 -0800147ssize_t redirected_tty_write(struct file *, const char __user *,
148 size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static unsigned int tty_poll(struct file *, poll_table *);
150static int tty_open(struct inode *, struct file *);
Alan Cox04f378b2008-04-30 00:53:29 -0700151long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700152#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -0800153static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700154 unsigned long arg);
155#else
156#define tty_compat_ioctl NULL
157#endif
Arnd Bergmannec79d602010-06-01 22:53:01 +0200158static int __tty_fasync(int fd, struct file *filp, int on);
Alan Cox37bdfb02008-02-08 04:18:47 -0800159static int tty_fasync(int fd, struct file *filp, int on);
Christoph Hellwigd5698c22007-02-10 01:46:46 -0800160static void release_tty(struct tty_struct *tty, int idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Alan Coxaf9b8972006-08-27 01:24:01 -0700162/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700163 * free_tty_struct - free a disused tty
164 * @tty: tty struct to free
165 *
166 * Free the write buffers, tty queue and tty memory itself.
167 *
168 * Locking: none. Must be called after tty is definitely unused
169 */
170
Alan Coxbf970ee2008-10-13 10:42:39 +0100171void free_tty_struct(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Dan Carpenterdc6802a2012-07-24 12:52:04 +0100173 if (!tty)
174 return;
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) {
271 printk(KERN_WARNING
272 "null TTY for (%d:%d) in %s\n",
273 imajor(inode), iminor(inode), routine);
274 return 1;
275 }
276 if (tty->magic != TTY_MAGIC) {
277 printk(KERN_WARNING
278 "bad magic number for tty struct (%d:%d) in %s\n",
279 imajor(inode), iminor(inode), routine);
280 return 1;
281 }
282#endif
283 return 0;
284}
285
Peter Hurleydeb287e2014-11-05 12:12:55 -0500286/* Caller must hold tty_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static int check_tty_count(struct tty_struct *tty, const char *routine)
288{
289#ifdef CHECK_TTY_COUNT
290 struct list_head *p;
291 int count = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -0800292
Nick Pigginee2ffa02010-08-18 04:37:35 +1000293 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_for_each(p, &tty->tty_files) {
295 count++;
296 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000297 spin_unlock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
299 tty->driver->subtype == PTY_TYPE_SLAVE &&
300 tty->link && tty->link->count)
301 count++;
302 if (tty->count != count) {
303 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
304 "!= #fd's(%d) in %s\n",
305 tty->name, tty->count, count, routine);
306 return count;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#endif
309 return 0;
310}
311
Alan Coxaf9b8972006-08-27 01:24:01 -0700312/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700313 * get_tty_driver - find device of a tty
314 * @dev_t: device identifier
315 * @index: returns the index of the tty
316 *
317 * This routine returns a tty driver structure, given a device number
318 * and also passes back the index number.
319 *
320 * Locking: caller must hold tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static struct tty_driver *get_tty_driver(dev_t device, int *index)
324{
325 struct tty_driver *p;
326
327 list_for_each_entry(p, &tty_drivers, tty_drivers) {
328 dev_t base = MKDEV(p->major, p->minor_start);
329 if (device < base || device >= base + p->num)
330 continue;
331 *index = device - base;
Alan Cox7d7b93c2008-10-13 10:42:09 +0100332 return tty_driver_kref_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334 return NULL;
335}
336
Jason Wesself2d937f2008-04-17 20:05:37 +0200337#ifdef CONFIG_CONSOLE_POLL
338
339/**
340 * tty_find_polling_driver - find device of a polled tty
341 * @name: name string to match
342 * @line: pointer to resulting tty line nr
343 *
344 * This routine returns a tty driver structure, given a name
345 * and the condition that the tty driver is capable of polled
346 * operation.
347 */
348struct tty_driver *tty_find_polling_driver(char *name, int *line)
349{
350 struct tty_driver *p, *res = NULL;
351 int tty_line = 0;
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500352 int len;
Alan Cox5f0878a2009-06-11 12:46:41 +0100353 char *str, *stp;
Jason Wesself2d937f2008-04-17 20:05:37 +0200354
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500355 for (str = name; *str; str++)
356 if ((*str >= '0' && *str <= '9') || *str == ',')
357 break;
358 if (!*str)
359 return NULL;
360
361 len = str - name;
362 tty_line = simple_strtoul(str, &str, 10);
363
Jason Wesself2d937f2008-04-17 20:05:37 +0200364 mutex_lock(&tty_mutex);
365 /* Search through the tty devices to look for a match */
366 list_for_each_entry(p, &tty_drivers, tty_drivers) {
Jason Wessel0dca0fd2008-09-26 10:36:42 -0500367 if (strncmp(name, p->name, len) != 0)
368 continue;
Alan Cox5f0878a2009-06-11 12:46:41 +0100369 stp = str;
370 if (*stp == ',')
371 stp++;
372 if (*stp == '\0')
373 stp = NULL;
Jason Wesself2d937f2008-04-17 20:05:37 +0200374
Nathael Pajani6eb68d62010-09-02 16:06:16 +0200375 if (tty_line >= 0 && tty_line < p->num && p->ops &&
Alan Cox5f0878a2009-06-11 12:46:41 +0100376 p->ops->poll_init && !p->ops->poll_init(p, tty_line, stp)) {
Alan Cox7d7b93c2008-10-13 10:42:09 +0100377 res = tty_driver_kref_get(p);
Jason Wesself2d937f2008-04-17 20:05:37 +0200378 *line = tty_line;
379 break;
380 }
381 }
382 mutex_unlock(&tty_mutex);
383
384 return res;
385}
386EXPORT_SYMBOL_GPL(tty_find_polling_driver);
387#endif
388
Alan Coxaf9b8972006-08-27 01:24:01 -0700389/**
390 * tty_check_change - check for POSIX terminal changes
391 * @tty: tty to check
392 *
393 * If we try to write to, or set the state of, a terminal and we're
394 * not in the foreground, send a SIGTTOU. If the signal is blocked or
395 * ignored, go ahead and perform the operation. (POSIX 7.2)
396 *
Alan Cox978e5952008-04-30 00:53:59 -0700397 * Locking: ctrl_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700399
Peter Hurley2812d9e2015-10-10 20:28:42 -0400400int __tty_check_change(struct tty_struct *tty, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Alan Cox47f86832008-04-30 00:53:30 -0700402 unsigned long flags;
Peter Hurley2812d9e2015-10-10 20:28:42 -0400403 struct pid *pgrp, *tty_pgrp;
Alan Cox47f86832008-04-30 00:53:30 -0700404 int ret = 0;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (current->signal->tty != tty)
407 return 0;
Alan Cox47f86832008-04-30 00:53:30 -0700408
Patrick Donnelly67196932015-07-12 18:51:52 -0400409 rcu_read_lock();
410 pgrp = task_pgrp(current);
411
Alan Cox47f86832008-04-30 00:53:30 -0700412 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurley2812d9e2015-10-10 20:28:42 -0400413 tty_pgrp = tty->pgrp;
Andrew Morton9ffee4c2008-05-14 16:05:58 -0700414 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Patrick Donnelly67196932015-07-12 18:51:52 -0400415
Peter Hurley2812d9e2015-10-10 20:28:42 -0400416 if (tty_pgrp && pgrp != tty->pgrp) {
417 if (is_ignored(sig)) {
418 if (sig == SIGTTIN)
419 ret = -EIO;
420 } else if (is_current_pgrp_orphaned())
421 ret = -EIO;
422 else {
423 kill_pgrp(pgrp, sig, 1);
424 set_thread_flag(TIF_SIGPENDING);
425 ret = -ERESTARTSYS;
426 }
Alan Cox47f86832008-04-30 00:53:30 -0700427 }
Patrick Donnelly67196932015-07-12 18:51:52 -0400428 rcu_read_unlock();
Peter Hurley2812d9e2015-10-10 20:28:42 -0400429
430 if (!tty_pgrp) {
431 pr_warn("%s: tty_check_change: sig=%d, tty->pgrp == NULL!\n",
432 tty_name(tty), sig);
433 }
434
Alan Cox47f86832008-04-30 00:53:30 -0700435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Peter Hurley2812d9e2015-10-10 20:28:42 -0400438int tty_check_change(struct tty_struct *tty)
439{
440 return __tty_check_change(tty, SIGTTOU);
441}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442EXPORT_SYMBOL(tty_check_change);
443
Alan Cox37bdfb02008-02-08 04:18:47 -0800444static ssize_t hung_up_tty_read(struct file *file, char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 size_t count, loff_t *ppos)
446{
447 return 0;
448}
449
Alan Cox37bdfb02008-02-08 04:18:47 -0800450static ssize_t hung_up_tty_write(struct file *file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 size_t count, loff_t *ppos)
452{
453 return -EIO;
454}
455
456/* No kernel lock held - none needed ;) */
Alan Cox37bdfb02008-02-08 04:18:47 -0800457static unsigned int hung_up_tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
460}
461
Alan Cox04f378b2008-04-30 00:53:29 -0700462static long hung_up_tty_ioctl(struct file *file, unsigned int cmd,
463 unsigned long arg)
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700464{
465 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
466}
467
Alan Cox37bdfb02008-02-08 04:18:47 -0800468static long hung_up_tty_compat_ioctl(struct file *file,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700469 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
472}
473
Arjan van de Ven62322d22006-07-03 00:24:21 -0700474static const struct file_operations tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 .llseek = no_llseek,
476 .read = tty_read,
477 .write = tty_write,
478 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700479 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700480 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .open = tty_open,
482 .release = tty_release,
483 .fasync = tty_fasync,
484};
485
Arjan van de Ven62322d22006-07-03 00:24:21 -0700486static const struct file_operations console_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 .llseek = no_llseek,
488 .read = tty_read,
489 .write = redirected_tty_write,
490 .poll = tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700491 .unlocked_ioctl = tty_ioctl,
Paul Fulghume10cc1d2007-05-10 22:22:50 -0700492 .compat_ioctl = tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 .open = tty_open,
494 .release = tty_release,
495 .fasync = tty_fasync,
496};
497
Arjan van de Ven62322d22006-07-03 00:24:21 -0700498static const struct file_operations hung_up_tty_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 .llseek = no_llseek,
500 .read = hung_up_tty_read,
501 .write = hung_up_tty_write,
502 .poll = hung_up_tty_poll,
Alan Cox04f378b2008-04-30 00:53:29 -0700503 .unlocked_ioctl = hung_up_tty_ioctl,
Paul Fulghum38ad2ed2007-06-16 10:15:55 -0700504 .compat_ioctl = hung_up_tty_compat_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 .release = tty_release,
506};
507
508static DEFINE_SPINLOCK(redirect_lock);
509static struct file *redirect;
510
Peter Hurley11d9bef2014-10-16 14:59:42 -0400511
512void proc_clear_tty(struct task_struct *p)
513{
514 unsigned long flags;
515 struct tty_struct *tty;
516 spin_lock_irqsave(&p->sighand->siglock, flags);
517 tty = p->signal->tty;
518 p->signal->tty = NULL;
519 spin_unlock_irqrestore(&p->sighand->siglock, flags);
520 tty_kref_put(tty);
521}
522
Peter Hurley2c411c12014-10-16 14:59:47 -0400523/**
524 * proc_set_tty - set the controlling terminal
525 *
526 * Only callable by the session leader and only if it does not already have
527 * a controlling terminal.
528 *
Peter Hurleye218eb32014-10-16 14:59:49 -0400529 * Caller must hold: tty_lock()
530 * a readlock on tasklist_lock
Peter Hurley2c411c12014-10-16 14:59:47 -0400531 * sighand lock
532 */
Peter Hurleybce65f12014-10-16 14:59:43 -0400533static void __proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400534{
Peter Hurleyae28fa72014-10-16 14:59:46 -0400535 unsigned long flags;
536
Peter Hurleyae28fa72014-10-16 14:59:46 -0400537 spin_lock_irqsave(&tty->ctrl_lock, flags);
Peter Hurleya3618582014-10-16 14:59:50 -0400538 /*
539 * The session and fg pgrp references will be non-NULL if
540 * tiocsctty() is stealing the controlling tty
541 */
Peter Hurleyae28fa72014-10-16 14:59:46 -0400542 put_pid(tty->session);
543 put_pid(tty->pgrp);
544 tty->pgrp = get_pid(task_pgrp(current));
545 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
546 tty->session = get_pid(task_session(current));
547 if (current->signal->tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -0400548 tty_debug(tty, "current tty %s not NULL!!\n",
549 current->signal->tty->name);
Peter Hurleyae28fa72014-10-16 14:59:46 -0400550 tty_kref_put(current->signal->tty);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400551 }
Peter Hurleybce65f12014-10-16 14:59:43 -0400552 put_pid(current->signal->tty_old_pgrp);
553 current->signal->tty = tty_kref_get(tty);
554 current->signal->tty_old_pgrp = NULL;
Peter Hurley11d9bef2014-10-16 14:59:42 -0400555}
556
Peter Hurleybce65f12014-10-16 14:59:43 -0400557static void proc_set_tty(struct tty_struct *tty)
Peter Hurley11d9bef2014-10-16 14:59:42 -0400558{
Peter Hurleybce65f12014-10-16 14:59:43 -0400559 spin_lock_irq(&current->sighand->siglock);
560 __proc_set_tty(tty);
561 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley11d9bef2014-10-16 14:59:42 -0400562}
563
564struct tty_struct *get_current_tty(void)
565{
566 struct tty_struct *tty;
567 unsigned long flags;
568
569 spin_lock_irqsave(&current->sighand->siglock, flags);
570 tty = tty_kref_get(current->signal->tty);
571 spin_unlock_irqrestore(&current->sighand->siglock, flags);
572 return tty;
573}
574EXPORT_SYMBOL_GPL(get_current_tty);
575
576static void session_clear_tty(struct pid *session)
577{
578 struct task_struct *p;
579 do_each_pid_task(session, PIDTYPE_SID, p) {
580 proc_clear_tty(p);
581 } while_each_pid_task(session, PIDTYPE_SID, p);
582}
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**
585 * tty_wakeup - request more data
586 * @tty: terminal
587 *
588 * Internal and external helper for wakeups of tty. This function
589 * informs the line discipline if present that the driver is ready
590 * to receive more output data.
591 */
Alan Cox37bdfb02008-02-08 04:18:47 -0800592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593void tty_wakeup(struct tty_struct *tty)
594{
595 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -0800596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
598 ld = tty_ldisc_ref(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800599 if (ld) {
Alan Coxa352def2008-07-16 21:53:12 +0100600 if (ld->ops->write_wakeup)
601 ld->ops->write_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 tty_ldisc_deref(ld);
603 }
604 }
Davide Libenzi4b194492009-03-31 15:24:24 -0700605 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608EXPORT_SYMBOL_GPL(tty_wakeup);
609
610/**
Peter Hurleyea648a42013-03-06 07:20:53 -0500611 * tty_signal_session_leader - sends SIGHUP to session leader
Peter Hurleyf91e2592013-03-06 07:20:56 -0500612 * @tty controlling tty
613 * @exit_session if non-zero, signal all foreground group processes
Peter Hurleyea648a42013-03-06 07:20:53 -0500614 *
Peter Hurleyf91e2592013-03-06 07:20:56 -0500615 * Send SIGHUP and SIGCONT to the session leader and its process group.
616 * Optionally, signal all processes in the foreground process group.
Peter Hurleyea648a42013-03-06 07:20:53 -0500617 *
618 * Returns the number of processes in the session with this tty
619 * as their controlling terminal. This value is used to drop
620 * tty references for those processes.
621 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500622static int tty_signal_session_leader(struct tty_struct *tty, int exit_session)
Peter Hurleyea648a42013-03-06 07:20:53 -0500623{
624 struct task_struct *p;
Peter Hurleyea648a42013-03-06 07:20:53 -0500625 int refs = 0;
Peter Hurleyf91e2592013-03-06 07:20:56 -0500626 struct pid *tty_pgrp = NULL;
Peter Hurleyea648a42013-03-06 07:20:53 -0500627
628 read_lock(&tasklist_lock);
629 if (tty->session) {
630 do_each_pid_task(tty->session, PIDTYPE_SID, p) {
631 spin_lock_irq(&p->sighand->siglock);
632 if (p->signal->tty == tty) {
633 p->signal->tty = NULL;
634 /* We defer the dereferences outside fo
635 the tasklist lock */
636 refs++;
637 }
638 if (!p->signal->leader) {
639 spin_unlock_irq(&p->sighand->siglock);
640 continue;
641 }
642 __group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
643 __group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
644 put_pid(p->signal->tty_old_pgrp); /* A noop */
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500645 spin_lock(&tty->ctrl_lock);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500646 tty_pgrp = get_pid(tty->pgrp);
Peter Hurleyea648a42013-03-06 07:20:53 -0500647 if (tty->pgrp)
648 p->signal->tty_old_pgrp = get_pid(tty->pgrp);
Peter Hurleybc30c3b2013-03-06 07:20:55 -0500649 spin_unlock(&tty->ctrl_lock);
Peter Hurleyea648a42013-03-06 07:20:53 -0500650 spin_unlock_irq(&p->sighand->siglock);
651 } while_each_pid_task(tty->session, PIDTYPE_SID, p);
652 }
653 read_unlock(&tasklist_lock);
654
Peter Hurleyf91e2592013-03-06 07:20:56 -0500655 if (tty_pgrp) {
656 if (exit_session)
657 kill_pgrp(tty_pgrp, SIGHUP, exit_session);
658 put_pid(tty_pgrp);
659 }
660
Peter Hurleyea648a42013-03-06 07:20:53 -0500661 return refs;
662}
663
664/**
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200665 * __tty_hangup - actual handler for hangup events
David Howells65f27f32006-11-22 14:55:48 +0000666 * @work: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -0700667 *
Kevin Cernekeeef4f5272012-12-26 20:43:41 -0800668 * This can be called by a "kworker" kernel thread. That is process
Alan Coxaf9b8972006-08-27 01:24:01 -0700669 * synchronous but doesn't hold any locks, so we need to make sure we
670 * have the appropriate locks for what we're doing.
671 *
672 * The hangup event clears any pending redirections onto the hung up
673 * device. It ensures future writes will error and it does the needed
674 * line discipline hangup and signal delivery. The tty object itself
675 * remains intact.
676 *
677 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200678 * BTM
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800679 * redirect lock for undoing redirection
680 * file list lock for manipulating list of ttys
Peter Hurley137084b2013-06-15 07:04:46 -0400681 * tty_ldiscs_lock from called functions
Peter Hurley6a1c0682013-06-15 09:14:23 -0400682 * termios_rwsem resetting termios data
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800683 * tasklist_lock to walk task list for hangup event
684 * ->siglock to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Peter Hurleyf91e2592013-03-06 07:20:56 -0500686static void __tty_hangup(struct tty_struct *tty, int exit_session)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Alan Cox37bdfb02008-02-08 04:18:47 -0800688 struct file *cons_filp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct file *filp, *f = NULL;
Nick Piggind996b622010-08-18 04:37:36 +1000690 struct tty_file_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 int closecount = 0, n;
Peter Hurleyea648a42013-03-06 07:20:53 -0500692 int refs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (!tty)
695 return;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 spin_lock(&redirect_lock);
Nick Piggind996b622010-08-18 04:37:36 +1000699 if (redirect && file_tty(redirect) == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 f = redirect;
701 redirect = NULL;
702 }
703 spin_unlock(&redirect_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800704
Alan Cox89c8d912012-08-08 16:30:13 +0100705 tty_lock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200706
Peter Hurleycb50e522013-07-31 14:05:45 -0400707 if (test_bit(TTY_HUPPED, &tty->flags)) {
708 tty_unlock(tty);
709 return;
710 }
711
Arnd Bergmannec79d602010-06-01 22:53:01 +0200712 /* inuse_filps is protected by the single tty lock,
713 this really needs to change if we want to flush the
714 workqueue with the lock held */
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200715 check_tty_count(tty, "tty_hangup");
Alan Cox36ba7822009-11-30 13:18:51 +0000716
Nick Pigginee2ffa02010-08-18 04:37:35 +1000717 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* This breaks for file handles being sent over AF_UNIX sockets ? */
Nick Piggind996b622010-08-18 04:37:36 +1000719 list_for_each_entry(priv, &tty->tty_files, list) {
720 filp = priv->file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (filp->f_op->write == redirected_tty_write)
722 cons_filp = filp;
723 if (filp->f_op->write != tty_write)
724 continue;
725 closecount++;
Arnd Bergmannec79d602010-06-01 22:53:01 +0200726 __tty_fasync(-1, filp, 0); /* can't block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 filp->f_op = &hung_up_tty_fops;
728 }
Nick Pigginee2ffa02010-08-18 04:37:35 +1000729 spin_unlock(&tty_files_lock);
Alan Cox37bdfb02008-02-08 04:18:47 -0800730
Peter Hurley25fdf242013-03-06 07:20:57 -0500731 refs = tty_signal_session_leader(tty, exit_session);
732 /* Account for the p->signal references we killed */
733 while (refs--)
734 tty_kref_put(tty);
735
Alan Coxc65c9bc2009-06-11 12:50:12 +0100736 tty_ldisc_hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800737
Peter Hurley20cc2252013-03-06 07:20:54 -0500738 spin_lock_irq(&tty->ctrl_lock);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100739 clear_bit(TTY_THROTTLED, &tty->flags);
Alan Coxc65c9bc2009-06-11 12:50:12 +0100740 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Eric W. Biedermand9c1e9a2007-03-18 12:45:44 -0600741 put_pid(tty->session);
742 put_pid(tty->pgrp);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800743 tty->session = NULL;
744 tty->pgrp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 tty->ctrl_status = 0;
Peter Hurley20cc2252013-03-06 07:20:54 -0500746 spin_unlock_irq(&tty->ctrl_lock);
Alan Cox9c9f4de2008-10-13 10:37:26 +0100747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
Alan Cox37bdfb02008-02-08 04:18:47 -0800749 * If one of the devices matches a console pointer, we
750 * cannot just call hangup() because that will cause
751 * tty->count and state->count to go out of sync.
752 * So we just call close() the right number of times.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 */
754 if (cons_filp) {
Alan Coxf34d7a52008-04-30 00:54:13 -0700755 if (tty->ops->close)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 for (n = 0; n < closecount; n++)
Alan Coxf34d7a52008-04-30 00:54:13 -0700757 tty->ops->close(tty, cons_filp);
758 } else if (tty->ops->hangup)
Peter Hurley7c6d3402014-06-16 09:17:08 -0400759 tty->ops->hangup(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -0800760 /*
761 * We don't want to have driver/ldisc interactions beyond
762 * the ones we did here. The driver layer expects no
763 * calls after ->hangup() from the ldisc side. However we
764 * can't yet guarantee all that.
765 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 set_bit(TTY_HUPPED, &tty->flags);
Alan Cox89c8d912012-08-08 16:30:13 +0100767 tty_unlock(tty);
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (f)
770 fput(f);
771}
772
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200773static void do_tty_hangup(struct work_struct *work)
774{
775 struct tty_struct *tty =
776 container_of(work, struct tty_struct, hangup_work);
777
Peter Hurleyf91e2592013-03-06 07:20:56 -0500778 __tty_hangup(tty, 0);
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200779}
780
Alan Coxaf9b8972006-08-27 01:24:01 -0700781/**
782 * tty_hangup - trigger a hangup event
783 * @tty: tty to hangup
784 *
785 * A carrier loss (virtual or otherwise) has occurred on this like
786 * schedule a hangup sequence to run after this event.
787 */
788
Alan Cox37bdfb02008-02-08 04:18:47 -0800789void tty_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Peter Hurleyaccff792015-07-12 22:49:09 -0400791 tty_debug_hangup(tty, "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 schedule_work(&tty->hangup_work);
793}
794
795EXPORT_SYMBOL(tty_hangup);
796
Alan Coxaf9b8972006-08-27 01:24:01 -0700797/**
798 * tty_vhangup - process vhangup
799 * @tty: tty to hangup
800 *
801 * The user has asked via system call for the terminal to be hung up.
802 * We do this synchronously so that when the syscall returns the process
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200803 * is complete. That guarantee is necessary for security reasons.
Alan Coxaf9b8972006-08-27 01:24:01 -0700804 */
805
Alan Cox37bdfb02008-02-08 04:18:47 -0800806void tty_vhangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Peter Hurleyaccff792015-07-12 22:49:09 -0400808 tty_debug_hangup(tty, "\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500809 __tty_hangup(tty, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
Alan Cox37bdfb02008-02-08 04:18:47 -0800811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812EXPORT_SYMBOL(tty_vhangup);
813
Arnd Bergmann11dbf202010-06-18 14:58:07 +0200814
Alan Coxaf9b8972006-08-27 01:24:01 -0700815/**
Alan Cox2cb59982008-10-13 10:40:30 +0100816 * tty_vhangup_self - process vhangup for own ctty
817 *
818 * Perform a vhangup on the current controlling tty
819 */
820
821void tty_vhangup_self(void)
822{
823 struct tty_struct *tty;
824
Alan Cox2cb59982008-10-13 10:40:30 +0100825 tty = get_current_tty();
826 if (tty) {
827 tty_vhangup(tty);
828 tty_kref_put(tty);
829 }
Alan Cox2cb59982008-10-13 10:40:30 +0100830}
831
832/**
Peter Hurleyf91e2592013-03-06 07:20:56 -0500833 * tty_vhangup_session - hangup session leader exit
834 * @tty: tty to hangup
835 *
836 * The session leader is exiting and hanging up its controlling terminal.
837 * Every process in the foreground process group is signalled SIGHUP.
838 *
839 * We do this synchronously so that when the syscall returns the process
840 * is complete. That guarantee is necessary for security reasons.
841 */
842
Changlong Xie44a459f2013-03-20 08:42:04 +0800843static void tty_vhangup_session(struct tty_struct *tty)
Peter Hurleyf91e2592013-03-06 07:20:56 -0500844{
Peter Hurleyaccff792015-07-12 22:49:09 -0400845 tty_debug_hangup(tty, "\n");
Peter Hurleyf91e2592013-03-06 07:20:56 -0500846 __tty_hangup(tty, 1);
847}
848
849/**
Alan Coxaf9b8972006-08-27 01:24:01 -0700850 * tty_hung_up_p - was tty hung up
851 * @filp: file pointer of tty
852 *
853 * Return true if the tty has been subject to a vhangup or a carrier
854 * loss
855 */
856
Alan Cox37bdfb02008-02-08 04:18:47 -0800857int tty_hung_up_p(struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 return (filp->f_op == &hung_up_tty_fops);
860}
861
862EXPORT_SYMBOL(tty_hung_up_p);
863
Alan Coxaf9b8972006-08-27 01:24:01 -0700864/**
865 * disassociate_ctty - disconnect controlling tty
866 * @on_exit: true if exiting so need to "hang up" the session
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700868 * This function is typically called only by the session leader, when
869 * it wants to disassociate itself from its controlling tty.
870 *
871 * It performs the following functions:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
873 * (2) Clears the tty from being controlling the session
874 * (3) Clears the controlling tty for all processes in the
875 * session group.
876 *
Alan Coxaf9b8972006-08-27 01:24:01 -0700877 * The argument on_exit is set to 1 if called when a process is
878 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
879 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800880 * Locking:
Arnd Bergmannec79d602010-06-01 22:53:01 +0200881 * BTM is taken for hysterical raisins, and held when
882 * called from no_tty().
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800883 * tty_mutex is taken to protect tty
884 * ->siglock is taken to protect ->signal/->sighand
885 * tasklist_lock is taken to walk process list for sessions
886 * ->siglock is taken to protect ->signal/->sighand
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Alan Coxaf9b8972006-08-27 01:24:01 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889void disassociate_ctty(int on_exit)
890{
891 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Alan Cox5ec93d12009-11-30 13:18:45 +0000893 if (!current->signal->leader)
894 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800896 tty = get_current_tty();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (tty) {
Peter Hurleyf91e2592013-03-06 07:20:56 -0500898 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY) {
899 tty_vhangup_session(tty);
900 } else {
901 struct pid *tty_pgrp = tty_get_pgrp(tty);
902 if (tty_pgrp) {
903 kill_pgrp(tty_pgrp, SIGHUP, on_exit);
Oleg Nesterov03e12612013-09-15 17:50:26 +0200904 if (!on_exit)
905 kill_pgrp(tty_pgrp, SIGCONT, on_exit);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500906 put_pid(tty_pgrp);
907 }
Arnd Bergmannddcd9fb2010-06-01 22:53:08 +0200908 }
Alan Cox452a00d2008-10-13 10:39:13 +0100909 tty_kref_put(tty);
Peter Hurleyf91e2592013-03-06 07:20:56 -0500910
Eric W. Biederman680a9672007-02-12 00:52:52 -0800911 } else if (on_exit) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800912 struct pid *old_pgrp;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800913 spin_lock_irq(&current->sighand->siglock);
914 old_pgrp = current->signal->tty_old_pgrp;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800915 current->signal->tty_old_pgrp = NULL;
Eric W. Biederman680a9672007-02-12 00:52:52 -0800916 spin_unlock_irq(&current->sighand->siglock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800917 if (old_pgrp) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800918 kill_pgrp(old_pgrp, SIGHUP, on_exit);
919 kill_pgrp(old_pgrp, SIGCONT, on_exit);
920 put_pid(old_pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800925 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman2a65f1d2007-05-08 00:26:53 -0700926 put_pid(current->signal->tty_old_pgrp);
Randy Dunlap23cac8d2007-02-20 13:58:05 -0800927 current->signal->tty_old_pgrp = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800928
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800929 tty = tty_kref_get(current->signal->tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -0800930 if (tty) {
Alan Cox47f86832008-04-30 00:53:30 -0700931 unsigned long flags;
932 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800933 put_pid(tty->session);
934 put_pid(tty->pgrp);
935 tty->session = NULL;
936 tty->pgrp = NULL;
Alan Cox47f86832008-04-30 00:53:30 -0700937 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Alan Cox452a00d2008-10-13 10:39:13 +0100938 tty_kref_put(tty);
Peter Hurleyaccff792015-07-12 22:49:09 -0400939 } else
940 tty_debug_hangup(tty, "no current tty\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Chen Tingjiec70dbb12014-04-15 11:52:51 +0800942 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* Now clear signal->tty under the lock */
944 read_lock(&tasklist_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800945 session_clear_tty(task_session(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700949/**
950 *
951 * no_tty - Ensure the current process does not have a controlling tty
952 */
953void no_tty(void)
954{
Alan Cox3af502b2012-05-03 22:21:53 +0100955 /* FIXME: Review locking here. The tty_lock never covered any race
956 between a new association and proc_clear_tty but possible we need
957 to protect against this anyway */
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700958 struct task_struct *tsk = current;
Alan Cox5ec93d12009-11-30 13:18:45 +0000959 disassociate_ctty(0);
Eric W. Biederman98a27ba2007-05-08 00:26:56 -0700960 proc_clear_tty(tsk);
961}
962
Alan Coxaf9b8972006-08-27 01:24:01 -0700963
964/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +0200965 * stop_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -0700966 * @tty: tty to stop
967 *
Peter Hurley01adc802014-09-10 15:06:32 -0400968 * Perform flow control to the driver. May be called
Alan Coxaf9b8972006-08-27 01:24:01 -0700969 * on an already stopped device and will not re-call the driver
970 * method.
971 *
972 * This functionality is used by both the line disciplines for
973 * halting incoming flow and by the driver. It may therefore be
974 * called from any context, may be under the tty atomic_write_lock
975 * but not always.
976 *
977 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400978 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -0700979 */
980
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400981void __stop_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400983 if (tty->stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 return;
985 tty->stopped = 1;
Alan Coxf34d7a52008-04-30 00:54:13 -0700986 if (tty->ops->stop)
Peter Hurleyc961bfb2014-11-05 12:26:25 -0500987 tty->ops->stop(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
Peter Hurleyf9e053d2014-09-10 15:06:31 -0400990void stop_tty(struct tty_struct *tty)
991{
992 unsigned long flags;
993
994 spin_lock_irqsave(&tty->flow_lock, flags);
995 __stop_tty(tty);
996 spin_unlock_irqrestore(&tty->flow_lock, flags);
997}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998EXPORT_SYMBOL(stop_tty);
999
Alan Coxaf9b8972006-08-27 01:24:01 -07001000/**
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +02001001 * start_tty - propagate flow control
Alan Coxaf9b8972006-08-27 01:24:01 -07001002 * @tty: tty to start
1003 *
Peter Hurley01adc802014-09-10 15:06:32 -04001004 * Start a tty that has been stopped if at all possible. If this
1005 * tty was previous stopped and is now being started, the driver
1006 * start method is invoked and the line discipline woken.
Alan Coxaf9b8972006-08-27 01:24:01 -07001007 *
1008 * Locking:
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001009 * flow_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07001010 */
1011
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001012void __start_tty(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001014 if (!tty->stopped || tty->flow_stopped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return;
1016 tty->stopped = 0;
Alan Coxf34d7a52008-04-30 00:54:13 -07001017 if (tty->ops->start)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001018 tty->ops->start(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Peter Hurleyf9e053d2014-09-10 15:06:31 -04001022void start_tty(struct tty_struct *tty)
1023{
1024 unsigned long flags;
1025
1026 spin_lock_irqsave(&tty->flow_lock, flags);
1027 __start_tty(tty);
1028 spin_unlock_irqrestore(&tty->flow_lock, flags);
1029}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030EXPORT_SYMBOL(start_tty);
1031
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001032static void tty_update_time(struct timespec *time)
1033{
Jiri Slabyf0bf0bd2015-02-27 18:40:31 +01001034 unsigned long sec = get_seconds();
Greg Kroah-Hartmanfbf47632015-03-26 23:10:27 +01001035
1036 /*
1037 * We only care if the two values differ in anything other than the
1038 * lower three bits (i.e every 8 seconds). If so, then we can update
1039 * the time of the tty device, otherwise it could be construded as a
1040 * security leak to let userspace know the exact timing of the tty.
1041 */
1042 if ((sec ^ time->tv_sec) & ~7)
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001043 time->tv_sec = sec;
1044}
1045
Alan Coxaf9b8972006-08-27 01:24:01 -07001046/**
1047 * tty_read - read method for tty device files
1048 * @file: pointer to tty file
1049 * @buf: user buffer
1050 * @count: size of user buffer
1051 * @ppos: unused
1052 *
1053 * Perform the read system call function on this terminal device. Checks
1054 * for hung up devices before calling the line discipline method.
1055 *
1056 * Locking:
Alan Cox47f86832008-04-30 00:53:30 -07001057 * Locks the line discipline internally while needed. Multiple
1058 * read calls may be outstanding in parallel.
Alan Coxaf9b8972006-08-27 01:24:01 -07001059 */
1060
Alan Cox37bdfb02008-02-08 04:18:47 -08001061static ssize_t tty_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 loff_t *ppos)
1063{
1064 int i;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001065 struct inode *inode = file_inode(file);
Nick Piggind996b622010-08-18 04:37:36 +10001066 struct tty_struct *tty = file_tty(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct tty_ldisc *ld;
1068
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001069 if (tty_paranoia_check(tty, inode, "tty_read"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return -EIO;
1071 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
1072 return -EIO;
1073
1074 /* We want to wait for the line discipline to sort out in this
1075 situation */
1076 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001077 if (ld->ops->read)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05001078 i = ld->ops->read(tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 else
1080 i = -EIO;
1081 tty_ldisc_deref(ld);
Jiri Slabyb0de59b2013-02-15 15:25:05 +01001082
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001083 if (i > 0)
1084 tty_update_time(&inode->i_atime);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return i;
1087}
1088
Peter Hurley136d5252014-09-10 15:06:34 -04001089static void tty_write_unlock(struct tty_struct *tty)
Alan Cox9c1729d2007-07-15 23:39:43 -07001090{
1091 mutex_unlock(&tty->atomic_write_lock);
Davide Libenzi4b194492009-03-31 15:24:24 -07001092 wake_up_interruptible_poll(&tty->write_wait, POLLOUT);
Alan Cox9c1729d2007-07-15 23:39:43 -07001093}
1094
Peter Hurley136d5252014-09-10 15:06:34 -04001095static int tty_write_lock(struct tty_struct *tty, int ndelay)
Alan Cox9c1729d2007-07-15 23:39:43 -07001096{
1097 if (!mutex_trylock(&tty->atomic_write_lock)) {
1098 if (ndelay)
1099 return -EAGAIN;
1100 if (mutex_lock_interruptible(&tty->atomic_write_lock))
1101 return -ERESTARTSYS;
1102 }
1103 return 0;
1104}
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106/*
1107 * Split writes up in sane blocksizes to avoid
1108 * denial-of-service type attacks
1109 */
1110static inline ssize_t do_tty_write(
1111 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
1112 struct tty_struct *tty,
1113 struct file *file,
1114 const char __user *buf,
1115 size_t count)
1116{
Alan Cox9c1729d2007-07-15 23:39:43 -07001117 ssize_t ret, written = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 unsigned int chunk;
Alan Cox37bdfb02008-02-08 04:18:47 -08001119
Alan Cox9c1729d2007-07-15 23:39:43 -07001120 ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
1121 if (ret < 0)
1122 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /*
1125 * We chunk up writes into a temporary buffer. This
1126 * simplifies low-level drivers immensely, since they
1127 * don't have locking issues and user mode accesses.
1128 *
1129 * But if TTY_NO_WRITE_SPLIT is set, we should use a
1130 * big chunk-size..
1131 *
1132 * The default chunk-size is 2kB, because the NTTY
1133 * layer has problems with bigger chunks. It will
1134 * claim to be able to handle more characters than
1135 * it actually does.
Alan Coxaf9b8972006-08-27 01:24:01 -07001136 *
1137 * FIXME: This can probably go away now except that 64K chunks
1138 * are too likely to fail unless switched to vmalloc...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
1140 chunk = 2048;
1141 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
1142 chunk = 65536;
1143 if (count < chunk)
1144 chunk = count;
1145
Ingo Molnar70522e12006-03-23 03:00:31 -08001146 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (tty->write_cnt < chunk) {
Jason Wessel402fda92008-10-13 10:45:36 +01001148 unsigned char *buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (chunk < 1024)
1151 chunk = 1024;
1152
Jason Wessel402fda92008-10-13 10:45:36 +01001153 buf_chunk = kmalloc(chunk, GFP_KERNEL);
1154 if (!buf_chunk) {
Alan Cox9c1729d2007-07-15 23:39:43 -07001155 ret = -ENOMEM;
1156 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 kfree(tty->write_buf);
1159 tty->write_cnt = chunk;
Jason Wessel402fda92008-10-13 10:45:36 +01001160 tty->write_buf = buf_chunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
1163 /* Do the write .. */
1164 for (;;) {
1165 size_t size = count;
1166 if (size > chunk)
1167 size = chunk;
1168 ret = -EFAULT;
1169 if (copy_from_user(tty->write_buf, buf, size))
1170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ret = write(tty, file, tty->write_buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (ret <= 0)
1173 break;
1174 written += ret;
1175 buf += ret;
1176 count -= ret;
1177 if (!count)
1178 break;
1179 ret = -ERESTARTSYS;
1180 if (signal_pending(current))
1181 break;
1182 cond_resched();
1183 }
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001184 if (written) {
1185 tty_update_time(&file_inode(file)->i_mtime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 ret = written;
Jiri Slaby37b7f3c2013-04-26 13:48:53 +02001187 }
Alan Cox9c1729d2007-07-15 23:39:43 -07001188out:
1189 tty_write_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return ret;
1191}
1192
Alan Cox95f9bfc2008-10-13 10:39:23 +01001193/**
1194 * tty_write_message - write a message to a certain tty, not just the console.
1195 * @tty: the destination tty_struct
1196 * @msg: the message to write
1197 *
1198 * This is used for messages that need to be redirected to a specific tty.
1199 * We don't put it into the syslog queue right now maybe in the future if
1200 * really needed.
1201 *
Arnd Bergmannec79d602010-06-01 22:53:01 +02001202 * We must still hold the BTM and test the CLOSING flag for the moment.
Alan Cox95f9bfc2008-10-13 10:39:23 +01001203 */
1204
1205void tty_write_message(struct tty_struct *tty, char *msg)
1206{
Alan Cox95f9bfc2008-10-13 10:39:23 +01001207 if (tty) {
1208 mutex_lock(&tty->atomic_write_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01001209 tty_lock(tty);
Peter Hurley4b41b952015-10-10 20:28:44 -04001210 if (tty->ops->write && tty->count > 0)
Alan Cox95f9bfc2008-10-13 10:39:23 +01001211 tty->ops->write(tty, msg, strlen(msg));
Peter Hurley4b41b952015-10-10 20:28:44 -04001212 tty_unlock(tty);
Alan Cox95f9bfc2008-10-13 10:39:23 +01001213 tty_write_unlock(tty);
1214 }
Alan Cox95f9bfc2008-10-13 10:39:23 +01001215 return;
1216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Alan Coxaf9b8972006-08-27 01:24:01 -07001219/**
1220 * tty_write - write method for tty device file
1221 * @file: tty file pointer
1222 * @buf: user data to write
1223 * @count: bytes to write
1224 * @ppos: unused
1225 *
1226 * Write data to a tty device via the line discipline.
1227 *
1228 * Locking:
1229 * Locks the line discipline as required
1230 * Writes to the tty driver are serialized by the atomic_write_lock
1231 * and are then processed in chunks to the device. The line discipline
Joe Petersona88a69c2009-01-02 13:40:53 +00001232 * write method will not be invoked in parallel for each device.
Alan Coxaf9b8972006-08-27 01:24:01 -07001233 */
1234
Alan Cox37bdfb02008-02-08 04:18:47 -08001235static ssize_t tty_write(struct file *file, const char __user *buf,
1236 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
Nick Piggind996b622010-08-18 04:37:36 +10001238 struct tty_struct *tty = file_tty(file);
1239 struct tty_ldisc *ld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ssize_t ret;
Alan Cox37bdfb02008-02-08 04:18:47 -08001241
Al Viro6131ffa2013-02-27 16:59:05 -05001242 if (tty_paranoia_check(tty, file_inode(file), "tty_write"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001244 if (!tty || !tty->ops->write ||
Alan Cox37bdfb02008-02-08 04:18:47 -08001245 (test_bit(TTY_IO_ERROR, &tty->flags)))
1246 return -EIO;
Alan Coxf34d7a52008-04-30 00:54:13 -07001247 /* Short term debug to catch buggy drivers */
1248 if (tty->ops->write_room == NULL)
1249 printk(KERN_ERR "tty driver %s lacks a write_room method.\n",
1250 tty->driver->name);
Alan Cox37bdfb02008-02-08 04:18:47 -08001251 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01001252 if (!ld->ops->write)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ret = -EIO;
1254 else
Alan Coxa352def2008-07-16 21:53:12 +01001255 ret = do_tty_write(ld->ops->write, tty, file, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 tty_ldisc_deref(ld);
1257 return ret;
1258}
1259
Alan Cox37bdfb02008-02-08 04:18:47 -08001260ssize_t redirected_tty_write(struct file *file, const char __user *buf,
1261 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct file *p = NULL;
1264
1265 spin_lock(&redirect_lock);
Al Virocb0942b2012-08-27 14:48:26 -04001266 if (redirect)
1267 p = get_file(redirect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 spin_unlock(&redirect_lock);
1269
1270 if (p) {
1271 ssize_t res;
1272 res = vfs_write(p, buf, count, &p->f_pos);
1273 fput(p);
1274 return res;
1275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return tty_write(file, buf, count, ppos);
1277}
1278
Peter Hurley136d5252014-09-10 15:06:34 -04001279/**
1280 * tty_send_xchar - send priority character
1281 *
1282 * Send a high priority character to the tty even if stopped
1283 *
1284 * Locking: none for xchar method, write ordering for write method.
1285 */
1286
1287int tty_send_xchar(struct tty_struct *tty, char ch)
1288{
1289 int was_stopped = tty->stopped;
1290
1291 if (tty->ops->send_xchar) {
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001292 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001293 tty->ops->send_xchar(tty, ch);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001294 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001295 return 0;
1296 }
1297
1298 if (tty_write_lock(tty, 0) < 0)
1299 return -ERESTARTSYS;
1300
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001301 down_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001302 if (was_stopped)
1303 start_tty(tty);
1304 tty->ops->write(tty, &ch, 1);
1305 if (was_stopped)
1306 stop_tty(tty);
Peter Hurleyee0c1a62015-11-11 08:03:54 -05001307 up_read(&tty->termios_rwsem);
Peter Hurley136d5252014-09-10 15:06:34 -04001308 tty_write_unlock(tty);
1309 return 0;
1310}
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312static char ptychar[] = "pqrstuvwxyzabcde";
1313
Alan Coxaf9b8972006-08-27 01:24:01 -07001314/**
1315 * pty_line_name - generate name for a pty
1316 * @driver: the tty driver in use
1317 * @index: the minor number
1318 * @p: output buffer of at least 6 bytes
1319 *
1320 * Generate a name from a driver reference and write it to the output
1321 * buffer.
1322 *
1323 * Locking: None
1324 */
1325static void pty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 int i = index + driver->name_base;
1328 /* ->name is initialized to "ttyp", but "tty" is expected */
1329 sprintf(p, "%s%c%x",
Alan Cox37bdfb02008-02-08 04:18:47 -08001330 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
1331 ptychar[i >> 4 & 0xf], i & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Alan Coxaf9b8972006-08-27 01:24:01 -07001334/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001335 * tty_line_name - generate name for a tty
Alan Coxaf9b8972006-08-27 01:24:01 -07001336 * @driver: the tty driver in use
1337 * @index: the minor number
1338 * @p: output buffer of at least 7 bytes
1339 *
1340 * Generate a name from a driver reference and write it to the output
Greg Kroah-Hartman5c0a2452014-02-22 14:31:04 -08001341 * buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07001342 *
1343 * Locking: None
1344 */
Hannes Reinecke723abd82014-02-27 12:30:51 +01001345static ssize_t tty_line_name(struct tty_driver *driver, int index, char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Jiri Slaby0019b402012-08-08 22:26:43 +02001347 if (driver->flags & TTY_DRIVER_UNNUMBERED_NODE)
Hannes Reinecke723abd82014-02-27 12:30:51 +01001348 return sprintf(p, "%s", driver->name);
Jiri Slaby0019b402012-08-08 22:26:43 +02001349 else
Hannes Reinecke723abd82014-02-27 12:30:51 +01001350 return sprintf(p, "%s%d", driver->name,
1351 index + driver->name_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
Alan Cox99f1fe12008-10-13 10:42:00 +01001354/**
1355 * tty_driver_lookup_tty() - find an existing tty, if any
1356 * @driver: the driver for the tty
1357 * @idx: the minor number
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001358 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001359 * Return the tty, if found. If not found, return NULL or ERR_PTR() if the
1360 * driver lookup() method returns an error.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001361 *
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001362 * Locking: tty_mutex must be held. If the tty is found, bump the tty kref.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001363 */
Jason Wessela47d5452009-01-02 13:43:04 +00001364static struct tty_struct *tty_driver_lookup_tty(struct tty_driver *driver,
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01001365 struct inode *inode, int idx)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001366{
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001367 struct tty_struct *tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001368
Peter Hurleyaa3cb812014-11-05 12:12:51 -05001369 if (driver->ops->lookup)
1370 tty = driver->ops->lookup(driver, inode, idx);
1371 else
1372 tty = driver->ttys[idx];
1373
1374 if (!IS_ERR(tty))
1375 tty_kref_get(tty);
1376 return tty;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001377}
1378
Alan Cox99f1fe12008-10-13 10:42:00 +01001379/**
Alan Coxbf970ee2008-10-13 10:42:39 +01001380 * tty_init_termios - helper for termios setup
1381 * @tty: the tty to set up
1382 *
1383 * Initialise the termios structures for this tty. Thus runs under
1384 * the tty_mutex currently so we can be relaxed about ordering.
1385 */
1386
1387int tty_init_termios(struct tty_struct *tty)
1388{
Alan Coxfe6e29f2008-10-13 10:44:08 +01001389 struct ktermios *tp;
Alan Coxbf970ee2008-10-13 10:42:39 +01001390 int idx = tty->index;
1391
Alan Cox36b3c072012-07-17 17:06:57 +01001392 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1393 tty->termios = tty->driver->init_termios;
1394 else {
1395 /* Check for lazy saved data */
1396 tp = tty->driver->termios[idx];
1397 if (tp != NULL)
1398 tty->termios = *tp;
1399 else
1400 tty->termios = tty->driver->init_termios;
Alan Coxbf970ee2008-10-13 10:42:39 +01001401 }
Alan Coxbf970ee2008-10-13 10:42:39 +01001402 /* Compatibility until drivers always set this */
Alan Coxadc8d742012-07-14 15:31:47 +01001403 tty->termios.c_ispeed = tty_termios_input_baud_rate(&tty->termios);
1404 tty->termios.c_ospeed = tty_termios_baud_rate(&tty->termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001405 return 0;
1406}
Alan Coxfe1ae7f2009-09-19 13:13:33 -07001407EXPORT_SYMBOL_GPL(tty_init_termios);
Alan Coxbf970ee2008-10-13 10:42:39 +01001408
Jiri Slaby66d450e2012-01-30 21:14:28 +01001409int tty_standard_install(struct tty_driver *driver, struct tty_struct *tty)
1410{
1411 int ret = tty_init_termios(tty);
1412 if (ret)
1413 return ret;
1414
1415 tty_driver_kref_get(driver);
1416 tty->count++;
1417 driver->ttys[tty->index] = tty;
1418 return 0;
1419}
1420EXPORT_SYMBOL_GPL(tty_standard_install);
1421
Alan Coxbf970ee2008-10-13 10:42:39 +01001422/**
Alan Cox8b0a88d2008-10-13 10:42:19 +01001423 * tty_driver_install_tty() - install a tty entry in the driver
1424 * @driver: the driver for the tty
1425 * @tty: the tty
1426 *
1427 * Install a tty object into the driver tables. The tty->index field
Alan Coxbf970ee2008-10-13 10:42:39 +01001428 * will be set by the time this is called. This method is responsible
1429 * for ensuring any need additional structures are allocated and
1430 * configured.
Alan Cox8b0a88d2008-10-13 10:42:19 +01001431 *
1432 * Locking: tty_mutex for now
1433 */
1434static int tty_driver_install_tty(struct tty_driver *driver,
1435 struct tty_struct *tty)
1436{
Jiri Slaby66d450e2012-01-30 21:14:28 +01001437 return driver->ops->install ? driver->ops->install(driver, tty) :
1438 tty_standard_install(driver, tty);
Alan Cox8b0a88d2008-10-13 10:42:19 +01001439}
1440
1441/**
1442 * tty_driver_remove_tty() - remove a tty from the driver tables
1443 * @driver: the driver for the tty
1444 * @idx: the minor number
1445 *
1446 * Remvoe a tty object from the driver tables. The tty->index field
1447 * will be set by the time this is called.
1448 *
1449 * Locking: tty_mutex for now
1450 */
Jiri Slaby24d406a2011-08-10 14:59:28 +02001451void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *tty)
Alan Cox8b0a88d2008-10-13 10:42:19 +01001452{
1453 if (driver->ops->remove)
1454 driver->ops->remove(driver, tty);
1455 else
1456 driver->ttys[tty->index] = NULL;
1457}
1458
1459/*
1460 * tty_reopen() - fast re-open of an open tty
1461 * @tty - the tty to open
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001462 *
Alan Cox99f1fe12008-10-13 10:42:00 +01001463 * Return 0 on success, -errno on error.
Peter Hurley5d93e742014-11-05 12:12:47 -05001464 * Re-opens on master ptys are not allowed and return -EIO.
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001465 *
Peter Hurley5d93e742014-11-05 12:12:47 -05001466 * Locking: Caller must hold tty_lock
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001467 */
Alan Cox99f1fe12008-10-13 10:42:00 +01001468static int tty_reopen(struct tty_struct *tty)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001469{
1470 struct tty_driver *driver = tty->driver;
1471
Peter Hurley216030e2014-11-05 12:12:48 -05001472 if (!tty->count)
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001473 return -EIO;
1474
1475 if (driver->type == TTY_DRIVER_TYPE_PTY &&
Peter Hurley5d93e742014-11-05 12:12:47 -05001476 driver->subtype == PTY_TYPE_MASTER)
1477 return -EIO;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001478
Peter Hurley86f2c002014-12-30 10:39:25 -05001479 if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
1480 return -EBUSY;
1481
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001482 tty->count++;
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001483
Peter Hurley36697522013-06-15 07:04:48 -04001484 WARN_ON(!tty->ldisc);
Sukadev Bhattiprolu23499702008-10-13 10:41:51 +01001485
1486 return 0;
1487}
1488
Alan Coxaf9b8972006-08-27 01:24:01 -07001489/**
Alan Coxd81ed102008-10-13 10:41:42 +01001490 * tty_init_dev - initialise a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07001491 * @driver: tty driver we are opening a device on
1492 * @idx: device index
Alan Cox15582d32008-10-13 10:41:03 +01001493 * @ret_tty: returned tty structure
Alan Coxaf9b8972006-08-27 01:24:01 -07001494 *
1495 * Prepare a tty device. This may not be a "new" clean device but
1496 * could also be an active device. The pty drivers require special
1497 * handling because of this.
1498 *
1499 * Locking:
1500 * The function is called under the tty_mutex, which
1501 * protects us from the tty struct or driver itself going away.
1502 *
1503 * On exit the tty device has the line discipline attached and
1504 * a reference count of 1. If a pair was created for pty/tty use
1505 * and the other was a pty master then it too has a reference count of 1.
1506 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
Ingo Molnar70522e12006-03-23 03:00:31 -08001508 * failed open. The new code protects the open with a mutex, so it's
1509 * really quite straightforward. The mutex locking can probably be
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * relaxed for the (most common) case of reopening a tty.
1511 */
Alan Coxaf9b8972006-08-27 01:24:01 -07001512
Konstantin Khlebnikov593a27c2012-01-05 13:04:21 +04001513struct tty_struct *tty_init_dev(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Alan Coxbf970ee2008-10-13 10:42:39 +01001515 struct tty_struct *tty;
Alan Cox73ec06f2008-10-13 10:42:29 +01001516 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 /*
1519 * First time open is complex, especially for PTY devices.
1520 * This code guarantees that either everything succeeds and the
1521 * TTY is ready for operation, or else the table slots are vacated
Alan Cox37bdfb02008-02-08 04:18:47 -08001522 * and the allocated memory released. (Except that the termios
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * and locked termios may be retained.)
1524 */
1525
Alan Cox73ec06f2008-10-13 10:42:29 +01001526 if (!try_module_get(driver->owner))
1527 return ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02001529 tty = alloc_tty_struct(driver, idx);
Jiri Slabyd5543502011-03-23 10:48:32 +01001530 if (!tty) {
1531 retval = -ENOMEM;
1532 goto err_module_put;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Alan Cox89c8d912012-08-08 16:30:13 +01001535 tty_lock(tty);
Alan Cox73ec06f2008-10-13 10:42:29 +01001536 retval = tty_driver_install_tty(driver, tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001537 if (retval < 0)
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001538 goto err_deinit_tty;
Alan Cox8b0a88d2008-10-13 10:42:19 +01001539
Jiri Slaby04831dc2012-06-04 13:35:36 +02001540 if (!tty->port)
1541 tty->port = driver->ports[idx];
1542
Jiri Slaby5d4121c2012-08-17 14:27:52 +02001543 WARN_RATELIMIT(!tty->port,
1544 "%s: %s driver does not set tty->port. This will crash the kernel later. Fix the driver!\n",
1545 __func__, tty->driver->name);
1546
Jiri Slaby967fab62012-10-18 22:26:46 +02001547 tty->port->itty = tty;
1548
Alan Cox37bdfb02008-02-08 04:18:47 -08001549 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * Structures all installed ... call the ldisc open routines.
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001551 * If we fail here just call release_tty to clean up. No need
1552 * to decrement the use counts, as release_tty doesn't care.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 */
Alan Coxbf970ee2008-10-13 10:42:39 +01001554 retval = tty_ldisc_setup(tty, tty->link);
Alan Cox01e1abb2008-07-22 11:16:55 +01001555 if (retval)
Jiri Slabyd5543502011-03-23 10:48:32 +01001556 goto err_release_tty;
Alan Cox89c8d912012-08-08 16:30:13 +01001557 /* Return the tty locked so that it cannot vanish under the caller */
Alan Cox73ec06f2008-10-13 10:42:29 +01001558 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001560err_deinit_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001561 tty_unlock(tty);
Jiri Slabya9dccdd2011-03-23 10:48:36 +01001562 deinitialize_tty_struct(tty);
Jiri Slabyd5543502011-03-23 10:48:32 +01001563 free_tty_struct(tty);
1564err_module_put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 module_put(driver->owner);
Jiri Slabyd5543502011-03-23 10:48:32 +01001566 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001568 /* call the tty release_tty routine to clean out this slot */
Jiri Slabyd5543502011-03-23 10:48:32 +01001569err_release_tty:
Alan Cox89c8d912012-08-08 16:30:13 +01001570 tty_unlock(tty);
Manuel Zerpies5a3c6b22011-06-16 14:07:22 +02001571 printk_ratelimited(KERN_INFO "tty_init_dev: ldisc open failed, "
Akinobu Mita40509142006-09-29 02:01:27 -07001572 "clearing slot %d\n", idx);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001573 release_tty(tty, idx);
Alan Cox73ec06f2008-10-13 10:42:29 +01001574 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Alan Coxfeebed62008-10-13 10:41:30 +01001577void tty_free_termios(struct tty_struct *tty)
1578{
1579 struct ktermios *tp;
1580 int idx = tty->index;
Alan Cox36b3c072012-07-17 17:06:57 +01001581
1582 /* If the port is going to reset then it has no termios to save */
1583 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
1584 return;
1585
1586 /* Stash the termios data */
1587 tp = tty->driver->termios[idx];
1588 if (tp == NULL) {
1589 tp = kmalloc(sizeof(struct ktermios), GFP_KERNEL);
Peter Hurley25080652015-11-08 13:01:11 -05001590 if (tp == NULL)
Alan Cox36b3c072012-07-17 17:06:57 +01001591 return;
Dan Carpenter4ac5d702012-07-24 12:51:52 +01001592 tty->driver->termios[idx] = tp;
Alan Coxfeebed62008-10-13 10:41:30 +01001593 }
Alan Cox36b3c072012-07-17 17:06:57 +01001594 *tp = tty->termios;
Alan Coxfeebed62008-10-13 10:41:30 +01001595}
1596EXPORT_SYMBOL(tty_free_termios);
1597
Peter Hurleya2965b72013-03-11 16:44:35 -04001598/**
Peter Hurley949aa642014-11-05 12:12:57 -05001599 * tty_flush_works - flush all works of a tty/pty pair
1600 * @tty: tty device to flush works for (or either end of a pty pair)
Peter Hurleya2965b72013-03-11 16:44:35 -04001601 *
Peter Hurley949aa642014-11-05 12:12:57 -05001602 * Sync flush all works belonging to @tty (and the 'other' tty).
Peter Hurleya2965b72013-03-11 16:44:35 -04001603 */
1604static void tty_flush_works(struct tty_struct *tty)
1605{
1606 flush_work(&tty->SAK_work);
1607 flush_work(&tty->hangup_work);
Peter Hurley949aa642014-11-05 12:12:57 -05001608 if (tty->link) {
1609 flush_work(&tty->link->SAK_work);
1610 flush_work(&tty->link->hangup_work);
1611 }
Peter Hurleya2965b72013-03-11 16:44:35 -04001612}
Alan Coxfeebed62008-10-13 10:41:30 +01001613
Alan Coxaf9b8972006-08-27 01:24:01 -07001614/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001615 * release_one_tty - release tty structure memory
Alan Cox9c9f4de2008-10-13 10:37:26 +01001616 * @kref: kref of tty we are obliterating
Alan Coxaf9b8972006-08-27 01:24:01 -07001617 *
1618 * Releases memory associated with a tty structure, and clears out the
1619 * driver table slots. This function is called when a device is no longer
1620 * in use. It also gets called when setup of a device fails.
1621 *
1622 * Locking:
Alan Coxaf9b8972006-08-27 01:24:01 -07001623 * takes the file list lock internally when working on the list
1624 * of ttys that the driver keeps.
Alan Coxb50989d2009-09-19 13:13:22 -07001625 *
1626 * This method gets called from a work queue so that the driver private
Dave Youngf278a2f2009-09-27 16:00:42 +00001627 * cleanup ops can sleep (needed for USB at least)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
Alan Coxb50989d2009-09-19 13:13:22 -07001629static void release_one_tty(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Alan Coxb50989d2009-09-19 13:13:22 -07001631 struct tty_struct *tty =
1632 container_of(work, struct tty_struct, hangup_work);
Alan Cox6f967f72008-10-13 10:37:36 +01001633 struct tty_driver *driver = tty->driver;
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001634 struct module *owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dave Youngf278a2f2009-09-27 16:00:42 +00001636 if (tty->ops->cleanup)
1637 tty->ops->cleanup(tty);
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 tty->magic = 0;
Alan Cox7d7b93c2008-10-13 10:42:09 +01001640 tty_driver_kref_put(driver);
Cyrill Gorcunovb216df52014-08-08 00:26:15 +04001641 module_put(owner);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001642
Nick Pigginee2ffa02010-08-18 04:37:35 +10001643 spin_lock(&tty_files_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 list_del_init(&tty->tty_files);
Nick Pigginee2ffa02010-08-18 04:37:35 +10001645 spin_unlock(&tty_files_lock);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001646
Oleg Nesterov6da8d862010-04-02 18:05:12 +02001647 put_pid(tty->pgrp);
1648 put_pid(tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 free_tty_struct(tty);
1650}
1651
Alan Coxb50989d2009-09-19 13:13:22 -07001652static void queue_release_one_tty(struct kref *kref)
1653{
1654 struct tty_struct *tty = container_of(kref, struct tty_struct, kref);
Dave Youngf278a2f2009-09-27 16:00:42 +00001655
Alan Coxb50989d2009-09-19 13:13:22 -07001656 /* The hangup queue is now free so we can reuse it rather than
1657 waste a chunk of memory for each port */
1658 INIT_WORK(&tty->hangup_work, release_one_tty);
1659 schedule_work(&tty->hangup_work);
1660}
1661
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001662/**
Alan Cox9c9f4de2008-10-13 10:37:26 +01001663 * tty_kref_put - release a tty kref
1664 * @tty: tty device
1665 *
1666 * Release a reference to a tty device and if need be let the kref
1667 * layer destruct the object for us
1668 */
1669
1670void tty_kref_put(struct tty_struct *tty)
1671{
1672 if (tty)
Alan Coxb50989d2009-09-19 13:13:22 -07001673 kref_put(&tty->kref, queue_release_one_tty);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001674}
1675EXPORT_SYMBOL(tty_kref_put);
1676
1677/**
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001678 * release_tty - release tty structure memory
1679 *
1680 * Release both @tty and a possible linked partner (think pty pair),
1681 * and decrement the refcount of the backing module.
1682 *
1683 * Locking:
Alan Coxd1552552012-07-27 18:02:54 +01001684 * tty_mutex
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001685 * takes the file list lock internally when working on the list
1686 * of ttys that the driver keeps.
Alan Cox9c9f4de2008-10-13 10:37:26 +01001687 *
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001688 */
1689static void release_tty(struct tty_struct *tty, int idx)
1690{
Alan Cox9c9f4de2008-10-13 10:37:26 +01001691 /* This should always be true but check for the moment */
1692 WARN_ON(tty->index != idx);
Alan Coxd1552552012-07-27 18:02:54 +01001693 WARN_ON(!mutex_is_locked(&tty_mutex));
Alan Cox36b3c072012-07-17 17:06:57 +01001694 if (tty->ops->shutdown)
1695 tty->ops->shutdown(tty);
1696 tty_free_termios(tty);
1697 tty_driver_remove_tty(tty->driver, tty);
Jiri Slaby967fab62012-10-18 22:26:46 +02001698 tty->port->itty = NULL;
Peter Hurley64e377d2013-06-15 09:01:00 -04001699 if (tty->link)
1700 tty->link->port->itty = NULL;
Peter Hurleye1760582015-10-17 16:36:23 -04001701 tty_buffer_cancel_work(tty->port);
Alan Cox36b3c072012-07-17 17:06:57 +01001702
Markus Elfringa211b1a2014-11-21 13:42:29 +01001703 tty_kref_put(tty->link);
Alan Cox9c9f4de2008-10-13 10:37:26 +01001704 tty_kref_put(tty);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001705}
1706
Alan Coxeeb89d92009-11-30 13:18:29 +00001707/**
Jiri Slaby955787ca2011-11-11 10:47:23 +01001708 * tty_release_checks - check a tty before real release
1709 * @tty: tty to check
1710 * @o_tty: link of @tty (if any)
1711 * @idx: index of the tty
1712 *
1713 * Performs some paranoid checking before true release of the @tty.
1714 * This is a no-op unless TTY_PARANOIA_CHECK is defined.
1715 */
Peter Hurley359b9fb2014-11-05 12:12:59 -05001716static int tty_release_checks(struct tty_struct *tty, int idx)
Jiri Slaby955787ca2011-11-11 10:47:23 +01001717{
1718#ifdef TTY_PARANOIA_CHECK
1719 if (idx < 0 || idx >= tty->driver->num) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001720 tty_debug(tty, "bad idx %d\n", idx);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001721 return -1;
1722 }
1723
1724 /* not much to check for devpts */
1725 if (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)
1726 return 0;
1727
1728 if (tty != tty->driver->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001729 tty_debug(tty, "bad driver table[%d] = %p\n",
1730 idx, tty->driver->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001731 return -1;
1732 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001733 if (tty->driver->other) {
Peter Hurley359b9fb2014-11-05 12:12:59 -05001734 struct tty_struct *o_tty = tty->link;
1735
Jiri Slaby955787ca2011-11-11 10:47:23 +01001736 if (o_tty != tty->driver->other->ttys[idx]) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001737 tty_debug(tty, "bad other table[%d] = %p\n",
1738 idx, tty->driver->other->ttys[idx]);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001739 return -1;
1740 }
Jiri Slaby955787ca2011-11-11 10:47:23 +01001741 if (o_tty->link != tty) {
Peter Hurleye2dfa3d2015-07-12 22:49:08 -04001742 tty_debug(tty, "bad link = %p\n", o_tty->link);
Jiri Slaby955787ca2011-11-11 10:47:23 +01001743 return -1;
1744 }
1745 }
1746#endif
1747 return 0;
1748}
1749
1750/**
Alan Coxeeb89d92009-11-30 13:18:29 +00001751 * tty_release - vfs callback for close
1752 * @inode: inode of tty
1753 * @filp: file pointer for handle to tty
1754 *
1755 * Called the last time each file handle is closed that references
1756 * this tty. There may however be several such references.
1757 *
1758 * Locking:
1759 * Takes bkl. See tty_release_dev
1760 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 * Even releasing the tty structures is a tricky business.. We have
1762 * to be very careful that the structures are all released at the
1763 * same time, as interrupts might otherwise get the wrong pointers.
1764 *
1765 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
1766 * lead to double frees or releasing memory still in use.
1767 */
Alan Coxeeb89d92009-11-30 13:18:29 +00001768
1769int tty_release(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Nick Piggind996b622010-08-18 04:37:36 +10001771 struct tty_struct *tty = file_tty(filp);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001772 struct tty_struct *o_tty = NULL;
1773 int do_sleep, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 int idx;
Peter Hurley37b16452014-10-16 13:51:30 -04001775 long timeout = 0;
Peter Hurley494c1ea2014-10-16 13:54:36 -04001776 int once = 1;
Alan Cox37bdfb02008-02-08 04:18:47 -08001777
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001778 if (tty_paranoia_check(tty, inode, __func__))
Alan Coxeeb89d92009-11-30 13:18:29 +00001779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Alan Cox89c8d912012-08-08 16:30:13 +01001781 tty_lock(tty);
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001782 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Arnd Bergmannec79d602010-06-01 22:53:01 +02001784 __tty_fasync(-1, filp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 idx = tty->index;
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001787 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
1788 tty->driver->subtype == PTY_TYPE_MASTER)
1789 o_tty = tty->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Peter Hurley359b9fb2014-11-05 12:12:59 -05001791 if (tty_release_checks(tty, idx)) {
Alan Cox89c8d912012-08-08 16:30:13 +01001792 tty_unlock(tty);
Alan Coxeeb89d92009-11-30 13:18:29 +00001793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Peter Hurleyaccff792015-07-12 22:49:09 -04001796 tty_debug_hangup(tty, "(tty count=%d)...\n", tty->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Alan Coxf34d7a52008-04-30 00:54:13 -07001798 if (tty->ops->close)
1799 tty->ops->close(tty, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Peter Hurley2aff5e22014-11-05 12:13:01 -05001801 /* If tty is pty master, lock the slave pty (stable lock order) */
1802 tty_lock_slave(o_tty);
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /*
1805 * Sanity check: if tty->count is going to zero, there shouldn't be
1806 * any waiters on tty->read_wait or tty->write_wait. We test the
1807 * wait queues and kick everyone out _before_ actually starting to
1808 * close. This ensures that we won't block while releasing the tty
1809 * structure.
1810 *
1811 * The test for the o_tty closing is necessary, since the master and
1812 * slave sides may close in any order. If the slave side closes out
1813 * first, its count will be one, since the master side holds an open.
Peter Hurley324c1652014-11-05 12:12:56 -05001814 * Thus this test wouldn't be triggered at the time the slave closed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * so we do it now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
1817 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 do_sleep = 0;
1819
Peter Hurley324c1652014-11-05 12:12:56 -05001820 if (tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (waitqueue_active(&tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001822 wake_up_poll(&tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 do_sleep++;
1824 }
1825 if (waitqueue_active(&tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001826 wake_up_poll(&tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 do_sleep++;
1828 }
1829 }
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001830 if (o_tty && o_tty->count <= 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (waitqueue_active(&o_tty->read_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001832 wake_up_poll(&o_tty->read_wait, POLLIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 do_sleep++;
1834 }
1835 if (waitqueue_active(&o_tty->write_wait)) {
Davide Libenzi4b194492009-03-31 15:24:24 -07001836 wake_up_poll(&o_tty->write_wait, POLLOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 do_sleep++;
1838 }
1839 }
1840 if (!do_sleep)
1841 break;
1842
Peter Hurley494c1ea2014-10-16 13:54:36 -04001843 if (once) {
1844 once = 0;
1845 printk(KERN_WARNING "%s: %s: read/write wait queue active!\n",
Rasmus Villemoes429b4742015-03-31 15:55:59 +02001846 __func__, tty_name(tty));
Peter Hurley494c1ea2014-10-16 13:54:36 -04001847 }
Peter Hurley37b16452014-10-16 13:51:30 -04001848 schedule_timeout_killable(timeout);
1849 if (timeout < 120 * HZ)
1850 timeout = 2 * timeout + 1;
1851 else
1852 timeout = MAX_SCHEDULE_TIMEOUT;
Alan Cox37bdfb02008-02-08 04:18:47 -08001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001855 if (o_tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (--o_tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001857 printk(KERN_WARNING "%s: bad pty slave count (%d) for %s\n",
Rasmus Villemoes429b4742015-03-31 15:55:59 +02001858 __func__, o_tty->count, tty_name(o_tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 o_tty->count = 0;
1860 }
1861 }
1862 if (--tty->count < 0) {
Jiri Slaby9de44bd2011-11-09 21:33:24 +01001863 printk(KERN_WARNING "%s: bad tty->count (%d) for %s\n",
Rasmus Villemoes429b4742015-03-31 15:55:59 +02001864 __func__, tty->count, tty_name(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 tty->count = 0;
1866 }
Alan Cox37bdfb02008-02-08 04:18:47 -08001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * We've decremented tty->count, so we need to remove this file
1870 * descriptor off the tty->tty_files list; this serves two
1871 * purposes:
1872 * - check_tty_count sees the correct number of file descriptors
1873 * associated with this tty.
1874 * - do_tty_hangup no longer sees this file descriptor as
1875 * something that needs to be handled for hangups.
1876 */
Nick Piggind996b622010-08-18 04:37:36 +10001877 tty_del_file(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 /*
1880 * Perform some housekeeping before deciding whether to return.
1881 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 * If _either_ side is closing, make sure there aren't any
1883 * processes that still think tty or o_tty is their controlling
1884 * tty.
1885 */
Peter Hurley324c1652014-11-05 12:12:56 -05001886 if (!tty->count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001888 session_clear_tty(tty->session);
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001889 if (o_tty)
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001890 session_clear_tty(o_tty->session);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 read_unlock(&tasklist_lock);
1892 }
1893
Peter Hurley324c1652014-11-05 12:12:56 -05001894 /* check whether both sides are closing ... */
Peter Hurley7ffb6da2014-11-05 12:13:00 -05001895 final = !tty->count && !(o_tty && o_tty->count);
Peter Hurley324c1652014-11-05 12:12:56 -05001896
Peter Hurley2aff5e22014-11-05 12:13:01 -05001897 tty_unlock_slave(o_tty);
1898 tty_unlock(tty);
1899
Peter Hurley04980702014-11-05 12:12:52 -05001900 /* At this point, the tty->count == 0 should ensure a dead tty
Alan Coxd1552552012-07-27 18:02:54 +01001901 cannot be re-opened by a racing opener */
Paul Fulghumda965822006-02-14 13:53:00 -08001902
Peter Hurley324c1652014-11-05 12:12:56 -05001903 if (!final)
Alan Coxeeb89d92009-11-30 13:18:29 +00001904 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08001905
Peter Hurleyaccff792015-07-12 22:49:09 -04001906 tty_debug_hangup(tty, "final close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /*
Alan Cox01e1abb2008-07-22 11:16:55 +01001908 * Ask the line discipline code to release its structures
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Peter Hurley62462ae2014-11-05 12:12:58 -05001910 tty_ldisc_release(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001911
1912 /* Wait for pending work before tty destruction commmences */
1913 tty_flush_works(tty);
Peter Hurleya2965b72013-03-11 16:44:35 -04001914
Peter Hurleyaccff792015-07-12 22:49:09 -04001915 tty_debug_hangup(tty, "freeing structure...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001917 * The release_tty function takes care of the details of clearing
Alan Cox89c8d912012-08-08 16:30:13 +01001918 * the slots and preserving the termios structure. The tty_unlock_pair
1919 * should be safe as we keep a kref while the tty is locked (so the
1920 * unlock never unlocks a freed tty).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 */
Alan Coxd1552552012-07-27 18:02:54 +01001922 mutex_lock(&tty_mutex);
Christoph Hellwigd5698c22007-02-10 01:46:46 -08001923 release_tty(tty, idx);
Alan Coxd1552552012-07-27 18:02:54 +01001924 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Alan Coxeeb89d92009-11-30 13:18:29 +00001926 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Alan Coxaf9b8972006-08-27 01:24:01 -07001929/**
Peter Hurley52494ee2014-11-05 12:12:50 -05001930 * tty_open_current_tty - get locked tty of current task
Jiri Slabyb82154a2011-11-09 21:33:19 +01001931 * @device: device number
1932 * @filp: file pointer to tty
Peter Hurley52494ee2014-11-05 12:12:50 -05001933 * @return: locked tty of the current task iff @device is /dev/tty
1934 *
1935 * Performs a re-open of the current task's controlling tty.
Jiri Slabyb82154a2011-11-09 21:33:19 +01001936 *
1937 * We cannot return driver and index like for the other nodes because
1938 * devpts will not work then. It expects inodes to be from devpts FS.
1939 */
1940static struct tty_struct *tty_open_current_tty(dev_t device, struct file *filp)
1941{
1942 struct tty_struct *tty;
Peter Hurley52494ee2014-11-05 12:12:50 -05001943 int retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01001944
1945 if (device != MKDEV(TTYAUX_MAJOR, 0))
1946 return NULL;
1947
1948 tty = get_current_tty();
1949 if (!tty)
1950 return ERR_PTR(-ENXIO);
1951
1952 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
1953 /* noctty = 1; */
Peter Hurley52494ee2014-11-05 12:12:50 -05001954 tty_lock(tty);
1955 tty_kref_put(tty); /* safe to drop the kref now */
1956
1957 retval = tty_reopen(tty);
1958 if (retval < 0) {
1959 tty_unlock(tty);
1960 tty = ERR_PTR(retval);
1961 }
Jiri Slabyb82154a2011-11-09 21:33:19 +01001962 return tty;
1963}
1964
1965/**
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001966 * tty_lookup_driver - lookup a tty driver for a given device file
1967 * @device: device number
1968 * @filp: file pointer to tty
1969 * @noctty: set if the device should not become a controlling tty
1970 * @index: index for the device in the @return driver
1971 * @return: driver for this inode (with increased refcount)
1972 *
1973 * If @return is not erroneous, the caller is responsible to decrement the
1974 * refcount by tty_driver_kref_put.
1975 *
1976 * Locking: tty_mutex protects get_tty_driver
1977 */
1978static struct tty_driver *tty_lookup_driver(dev_t device, struct file *filp,
1979 int *noctty, int *index)
1980{
1981 struct tty_driver *driver;
1982
Jiri Slaby2cd00502011-11-09 21:33:22 +01001983 switch (device) {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001984#ifdef CONFIG_VT
Jiri Slaby2cd00502011-11-09 21:33:22 +01001985 case MKDEV(TTY_MAJOR, 0): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001986 extern struct tty_driver *console_driver;
1987 driver = tty_driver_kref_get(console_driver);
1988 *index = fg_console;
1989 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01001990 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001991 }
1992#endif
Jiri Slaby2cd00502011-11-09 21:33:22 +01001993 case MKDEV(TTYAUX_MAJOR, 1): {
Jiri Slaby5b5e7042011-11-09 21:33:20 +01001994 struct tty_driver *console_driver = console_device(index);
1995 if (console_driver) {
1996 driver = tty_driver_kref_get(console_driver);
1997 if (driver) {
1998 /* Don't let /dev/console block */
1999 filp->f_flags |= O_NONBLOCK;
2000 *noctty = 1;
Jiri Slaby2cd00502011-11-09 21:33:22 +01002001 break;
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002002 }
2003 }
2004 return ERR_PTR(-ENODEV);
2005 }
Jiri Slaby2cd00502011-11-09 21:33:22 +01002006 default:
2007 driver = get_tty_driver(device, index);
2008 if (!driver)
2009 return ERR_PTR(-ENODEV);
2010 break;
2011 }
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002012 return driver;
2013}
2014
2015/**
Alan Coxeeb89d92009-11-30 13:18:29 +00002016 * tty_open - open a tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002017 * @inode: inode of device file
2018 * @filp: file pointer to tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002020 * tty_open and tty_release keep up the tty count that contains the
2021 * number of opens done on a tty. We cannot use the inode-count, as
2022 * different inodes might point to the same tty.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002024 * Open-counting is needed for pty masters, as well as for keeping
2025 * track of serial lines: DTR is dropped when the last close happens.
2026 * (This is not done solely through tty->count, now. - Ted 1/27/92)
2027 *
2028 * The termios state of a pty is reset on first open so that
2029 * settings don't persist across reuse.
2030 *
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002031 * Locking: tty_mutex protects tty, tty_lookup_driver and tty_init_dev.
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002032 * tty->count should protect the rest.
2033 * ->siglock protects ->signal/->sighand
Alan Cox89c8d912012-08-08 16:30:13 +01002034 *
2035 * Note: the tty_unlock/lock cases without a ref are only safe due to
2036 * tty_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
Alan Coxaf9b8972006-08-27 01:24:01 -07002038
Alan Coxeeb89d92009-11-30 13:18:29 +00002039static int tty_open(struct inode *inode, struct file *filp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Jiri Slabyb82154a2011-11-09 21:33:19 +01002041 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 int noctty, retval;
Jiri Slabyb82154a2011-11-09 21:33:19 +01002043 struct tty_driver *driver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int index;
2045 dev_t device = inode->i_rdev;
Andrew Morton846c1512009-04-02 16:56:36 -07002046 unsigned saved_flags = filp->f_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 nonseekable_open(inode, filp);
Alan Cox37bdfb02008-02-08 04:18:47 -08002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050retry_open:
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002051 retval = tty_alloc_file(filp);
2052 if (retval)
2053 return -ENOMEM;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 noctty = filp->f_flags & O_NOCTTY;
2056 index = -1;
2057 retval = 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002058
Jiri Slabyb82154a2011-11-09 21:33:19 +01002059 tty = tty_open_current_tty(device, filp);
Peter Hurley52494ee2014-11-05 12:12:50 -05002060 if (!tty) {
2061 mutex_lock(&tty_mutex);
Jiri Slaby5b5e7042011-11-09 21:33:20 +01002062 driver = tty_lookup_driver(device, filp, &noctty, &index);
2063 if (IS_ERR(driver)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002064 retval = PTR_ERR(driver);
2065 goto err_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002068 /* check whether we're reopening an existing tty */
Sukadev Bhattiprolu15f1a632008-10-13 10:42:59 +01002069 tty = tty_driver_lookup_tty(driver, inode, index);
Eric Paris808ffa32009-01-27 11:50:37 +00002070 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002071 retval = PTR_ERR(tty);
2072 goto err_unlock;
Eric Paris808ffa32009-01-27 11:50:37 +00002073 }
Peter Hurley52494ee2014-11-05 12:12:50 -05002074
2075 if (tty) {
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002076 mutex_unlock(&tty_mutex);
Peter Hurley52494ee2014-11-05 12:12:50 -05002077 tty_lock(tty);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002078 /* safe to drop the kref from tty_driver_lookup_tty() */
2079 tty_kref_put(tty);
Peter Hurley52494ee2014-11-05 12:12:50 -05002080 retval = tty_reopen(tty);
2081 if (retval < 0) {
2082 tty_unlock(tty);
2083 tty = ERR_PTR(retval);
2084 }
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002085 } else { /* Returns with the tty_lock held for now */
Peter Hurley52494ee2014-11-05 12:12:50 -05002086 tty = tty_init_dev(driver, index);
Peter Hurleyaa3cb812014-11-05 12:12:51 -05002087 mutex_unlock(&tty_mutex);
2088 }
Peter Hurley52494ee2014-11-05 12:12:50 -05002089
Peter Hurley52494ee2014-11-05 12:12:50 -05002090 tty_driver_kref_put(driver);
Sukadev Bhattiprolu4a2b5fd2008-10-13 10:42:49 +01002091 }
2092
Alan Coxeeb89d92009-11-30 13:18:29 +00002093 if (IS_ERR(tty)) {
Jiri Slabyba5db442011-11-09 21:33:21 +01002094 retval = PTR_ERR(tty);
2095 goto err_file;
Alan Coxeeb89d92009-11-30 13:18:29 +00002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Jiri Slabyfa90e1c2011-10-12 11:32:43 +02002098 tty_add_file(tty, filp);
Nick Piggind996b622010-08-18 04:37:36 +10002099
Jiri Slaby9de44bd2011-11-09 21:33:24 +01002100 check_tty_count(tty, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2102 tty->driver->subtype == PTY_TYPE_MASTER)
2103 noctty = 1;
Peter Hurleyaccff792015-07-12 22:49:09 -04002104
2105 tty_debug_hangup(tty, "(tty count=%d)\n", tty->count);
2106
Herton Ronaldo Krzesinski909bc772011-03-31 15:35:31 -03002107 if (tty->ops->open)
2108 retval = tty->ops->open(tty, filp);
2109 else
2110 retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 filp->f_flags = saved_flags;
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (retval) {
Peter Hurleyaccff792015-07-12 22:49:09 -04002114 tty_debug_hangup(tty, "error %d, releasing...\n", retval);
2115
Alan Cox89c8d912012-08-08 16:30:13 +01002116 tty_unlock(tty); /* need to call tty_release without BTM */
Alan Coxeeb89d92009-11-30 13:18:29 +00002117 tty_release(inode, filp);
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002118 if (retval != -ERESTARTSYS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002120
2121 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return retval;
Arnd Bergmann64ba3dc2010-06-01 22:53:02 +02002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 schedule();
2125 /*
2126 * Need to reset f_op in case a hangup happened.
2127 */
Peter Hurley12569372014-11-05 12:26:24 -05002128 if (tty_hung_up_p(filp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 filp->f_op = &tty_fops;
2130 goto retry_open;
2131 }
Peter Hurleyd4855e12013-11-19 08:46:27 -05002132 clear_bit(TTY_HUPPED, &tty->flags);
Alan Coxeeb89d92009-11-30 13:18:29 +00002133
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002134
Peter Hurley2c411c12014-10-16 14:59:47 -04002135 read_lock(&tasklist_lock);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002136 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (!noctty &&
2138 current->signal->leader &&
2139 !current->signal->tty &&
Jann Horn0c5562712015-10-04 19:29:12 +02002140 tty->session == NULL) {
2141 /*
2142 * Don't let a process that only has write access to the tty
2143 * obtain the privileges associated with having a tty as
2144 * controlling terminal (being able to reopen it with full
2145 * access through /dev/tty, being able to perform pushback).
2146 * Many distributions set the group of all ttys to "tty" and
2147 * grant write-only access to all terminals for setgid tty
2148 * binaries, which should not imply full privileges on all ttys.
2149 *
2150 * This could theoretically break old code that performs open()
2151 * on a write-only file descriptor. In that case, it might be
2152 * necessary to also permit this if
2153 * inode_permission(inode, MAY_READ) == 0.
2154 */
2155 if (filp->f_mode & FMODE_READ)
2156 __proc_set_tty(tty);
2157 }
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002158 spin_unlock_irq(&current->sighand->siglock);
Peter Hurley2c411c12014-10-16 14:59:47 -04002159 read_unlock(&tasklist_lock);
Alan Cox89c8d912012-08-08 16:30:13 +01002160 tty_unlock(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return 0;
Jiri Slabyba5db442011-11-09 21:33:21 +01002162err_unlock:
Jiri Slabyba5db442011-11-09 21:33:21 +01002163 mutex_unlock(&tty_mutex);
2164 /* after locks to avoid deadlock */
2165 if (!IS_ERR_OR_NULL(driver))
2166 tty_driver_kref_put(driver);
2167err_file:
2168 tty_free_file(filp);
2169 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Jonathan Corbet39d95b92008-05-16 09:10:50 -06002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Alan Coxaf9b8972006-08-27 01:24:01 -07002174/**
2175 * tty_poll - check tty status
2176 * @filp: file being polled
2177 * @wait: poll wait structures to update
2178 *
2179 * Call the line discipline polling method to obtain the poll
2180 * status of the device.
2181 *
2182 * Locking: locks called line discipline but ldisc poll method
2183 * may be re-entered freely by other callers.
2184 */
2185
Alan Cox37bdfb02008-02-08 04:18:47 -08002186static unsigned int tty_poll(struct file *filp, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Nick Piggind996b622010-08-18 04:37:36 +10002188 struct tty_struct *tty = file_tty(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct tty_ldisc *ld;
2190 int ret = 0;
2191
Al Viro6131ffa2013-02-27 16:59:05 -05002192 if (tty_paranoia_check(tty, file_inode(filp), "tty_poll"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002196 if (ld->ops->poll)
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002197 ret = ld->ops->poll(tty, filp, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 tty_ldisc_deref(ld);
2199 return ret;
2200}
2201
Arnd Bergmannec79d602010-06-01 22:53:01 +02002202static int __tty_fasync(int fd, struct file *filp, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Nick Piggind996b622010-08-18 04:37:36 +10002204 struct tty_struct *tty = file_tty(filp);
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002205 struct tty_ldisc *ldisc;
Alan Cox47f86832008-04-30 00:53:30 -07002206 unsigned long flags;
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002207 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Al Viro6131ffa2013-02-27 16:59:05 -05002209 if (tty_paranoia_check(tty, file_inode(filp), "tty_fasync"))
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002210 goto out;
Alan Cox37bdfb02008-02-08 04:18:47 -08002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 retval = fasync_helper(fd, filp, on, &tty->fasync);
2213 if (retval <= 0)
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002216 ldisc = tty_ldisc_ref(tty);
2217 if (ldisc) {
2218 if (ldisc->ops->fasync)
2219 ldisc->ops->fasync(tty, on);
2220 tty_ldisc_deref(ldisc);
2221 }
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (on) {
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002224 enum pid_type type;
2225 struct pid *pid;
Peter Hurleyf6c8dbe2013-06-15 07:28:28 -04002226
Alan Cox47f86832008-04-30 00:53:30 -07002227 spin_lock_irqsave(&tty->ctrl_lock, flags);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002228 if (tty->pgrp) {
2229 pid = tty->pgrp;
2230 type = PIDTYPE_PGID;
2231 } else {
2232 pid = task_pid(current);
2233 type = PIDTYPE_PID;
2234 }
Linus Torvalds80e1e822010-02-07 10:11:23 -08002235 get_pid(pid);
Greg Kroah-Hartman70362512009-12-17 07:07:19 -08002236 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002237 __f_setown(filp, pid, type, 0);
Linus Torvalds80e1e822010-02-07 10:11:23 -08002238 put_pid(pid);
Jeff Laytone0b93ed2014-08-22 11:27:32 -04002239 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002241out:
Arnd Bergmannec79d602010-06-01 22:53:01 +02002242 return retval;
2243}
2244
2245static int tty_fasync(int fd, struct file *filp, int on)
2246{
Alan Cox89c8d912012-08-08 16:30:13 +01002247 struct tty_struct *tty = file_tty(filp);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002248 int retval;
Alan Cox89c8d912012-08-08 16:30:13 +01002249
2250 tty_lock(tty);
Arnd Bergmannec79d602010-06-01 22:53:01 +02002251 retval = __tty_fasync(fd, filp, on);
Alan Cox89c8d912012-08-08 16:30:13 +01002252 tty_unlock(tty);
2253
Jonathan Corbet5d1e3232008-06-19 16:04:53 -06002254 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
Alan Coxaf9b8972006-08-27 01:24:01 -07002257/**
2258 * tiocsti - fake input character
2259 * @tty: tty to fake input into
2260 * @p: pointer to character
2261 *
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +02002262 * Fake input to a tty device. Does the necessary locking and
Alan Coxaf9b8972006-08-27 01:24:01 -07002263 * input management.
2264 *
2265 * FIXME: does not honour flow control ??
2266 *
2267 * Locking:
Peter Hurley137084b2013-06-15 07:04:46 -04002268 * Called functions take tty_ldiscs_lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002269 * current->signal->tty check is safe without locks
Alan Cox28298232006-09-29 02:00:58 -07002270 *
2271 * FIXME: may race normal receive processing
Alan Coxaf9b8972006-08-27 01:24:01 -07002272 */
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274static int tiocsti(struct tty_struct *tty, char __user *p)
2275{
2276 char ch, mbz = 0;
2277 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
2280 return -EPERM;
2281 if (get_user(ch, p))
2282 return -EFAULT;
Al Viro1e641742008-12-09 09:23:33 +00002283 tty_audit_tiocsti(tty, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002285 ld->ops->receive_buf(tty, &ch, &mbz, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 tty_ldisc_deref(ld);
2287 return 0;
2288}
2289
Alan Coxaf9b8972006-08-27 01:24:01 -07002290/**
2291 * tiocgwinsz - implement window query ioctl
2292 * @tty; tty
2293 * @arg: user buffer for result
2294 *
Alan Cox808a0d32006-09-29 02:00:40 -07002295 * Copies the kernel idea of the window size into the user buffer.
Alan Coxaf9b8972006-08-27 01:24:01 -07002296 *
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002297 * Locking: tty->winsize_mutex is taken to ensure the winsize data
Alan Cox808a0d32006-09-29 02:00:40 -07002298 * is consistent.
Alan Coxaf9b8972006-08-27 01:24:01 -07002299 */
2300
Alan Cox37bdfb02008-02-08 04:18:47 -08002301static int tiocgwinsz(struct tty_struct *tty, struct winsize __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
Alan Cox808a0d32006-09-29 02:00:40 -07002303 int err;
2304
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002305 mutex_lock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002306 err = copy_to_user(arg, &tty->winsize, sizeof(*arg));
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002307 mutex_unlock(&tty->winsize_mutex);
Alan Cox808a0d32006-09-29 02:00:40 -07002308
2309 return err ? -EFAULT: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
Alan Coxaf9b8972006-08-27 01:24:01 -07002312/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002313 * tty_do_resize - resize event
2314 * @tty: tty being resized
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002315 * @rows: rows (character)
2316 * @cols: cols (character)
Alan Coxaf9b8972006-08-27 01:24:01 -07002317 *
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08002318 * Update the termios variables and send the necessary signals to
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002319 * peform a terminal resize correctly
Alan Coxaf9b8972006-08-27 01:24:01 -07002320 */
2321
Alan Coxfc6f6232009-01-02 13:43:17 +00002322int tty_do_resize(struct tty_struct *tty, struct winsize *ws)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Alan Coxfc6f6232009-01-02 13:43:17 +00002324 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Alan Coxfc6f6232009-01-02 13:43:17 +00002326 /* Lock the tty */
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002327 mutex_lock(&tty->winsize_mutex);
Alan Coxfc6f6232009-01-02 13:43:17 +00002328 if (!memcmp(ws, &tty->winsize, sizeof(*ws)))
Alan Coxca9bda02006-09-29 02:00:03 -07002329 goto done;
Alan Cox47f86832008-04-30 00:53:30 -07002330
Peter Hurley5b239542014-10-16 14:59:45 -04002331 /* Signal the foreground process group */
2332 pgrp = tty_get_pgrp(tty);
Alan Cox47f86832008-04-30 00:53:30 -07002333 if (pgrp)
2334 kill_pgrp(pgrp, SIGWINCH, 1);
Alan Cox47f86832008-04-30 00:53:30 -07002335 put_pid(pgrp);
Alan Cox47f86832008-04-30 00:53:30 -07002336
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002337 tty->winsize = *ws;
Alan Coxca9bda02006-09-29 02:00:03 -07002338done:
Peter Hurleydee4a0b2013-07-24 16:43:51 -04002339 mutex_unlock(&tty->winsize_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return 0;
2341}
Martin Schwidefsky4d334fd2013-01-04 14:55:13 +01002342EXPORT_SYMBOL(tty_do_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Alan Coxaf9b8972006-08-27 01:24:01 -07002344/**
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002345 * tiocswinsz - implement window size set ioctl
Alan Coxfc6f6232009-01-02 13:43:17 +00002346 * @tty; tty side of tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002347 * @arg: user buffer for result
2348 *
2349 * Copies the user idea of the window size to the kernel. Traditionally
2350 * this is just advisory information but for the Linux console it
2351 * actually has driver level meaning and triggers a VC resize.
2352 *
2353 * Locking:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002354 * Driver dependent. The default do_resize method takes the
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002355 * tty termios mutex and ctrl_lock. The console takes its own lock
2356 * then calls into the default method.
2357 */
2358
Alan Coxfc6f6232009-01-02 13:43:17 +00002359static int tiocswinsz(struct tty_struct *tty, struct winsize __user *arg)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002360{
2361 struct winsize tmp_ws;
2362 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
2363 return -EFAULT;
2364
2365 if (tty->ops->resize)
Alan Coxfc6f6232009-01-02 13:43:17 +00002366 return tty->ops->resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002367 else
Alan Coxfc6f6232009-01-02 13:43:17 +00002368 return tty_do_resize(tty, &tmp_ws);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002369}
2370
2371/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002372 * tioccons - allow admin to move logical console
2373 * @file: the file to become console
2374 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002375 * Allow the administrator to move the redirected console device
Alan Coxaf9b8972006-08-27 01:24:01 -07002376 *
2377 * Locking: uses redirect_lock to guard the redirect information
2378 */
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380static int tioccons(struct file *file)
2381{
2382 if (!capable(CAP_SYS_ADMIN))
2383 return -EPERM;
2384 if (file->f_op->write == redirected_tty_write) {
2385 struct file *f;
2386 spin_lock(&redirect_lock);
2387 f = redirect;
2388 redirect = NULL;
2389 spin_unlock(&redirect_lock);
2390 if (f)
2391 fput(f);
2392 return 0;
2393 }
2394 spin_lock(&redirect_lock);
2395 if (redirect) {
2396 spin_unlock(&redirect_lock);
2397 return -EBUSY;
2398 }
Al Virocb0942b2012-08-27 14:48:26 -04002399 redirect = get_file(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 spin_unlock(&redirect_lock);
2401 return 0;
2402}
2403
Alan Coxaf9b8972006-08-27 01:24:01 -07002404/**
2405 * fionbio - non blocking ioctl
2406 * @file: file to set blocking value
2407 * @p: user parameter
2408 *
2409 * Historical tty interfaces had a blocking control ioctl before
2410 * the generic functionality existed. This piece of history is preserved
2411 * in the expected tty API of posix OS's.
2412 *
Alan Cox6146b9a2009-09-19 13:13:19 -07002413 * Locking: none, the open file handle ensures it won't go away.
Alan Coxaf9b8972006-08-27 01:24:01 -07002414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416static int fionbio(struct file *file, int __user *p)
2417{
2418 int nonblock;
2419
2420 if (get_user(nonblock, p))
2421 return -EFAULT;
2422
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002423 spin_lock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (nonblock)
2425 file->f_flags |= O_NONBLOCK;
2426 else
2427 file->f_flags &= ~O_NONBLOCK;
Jonathan Corbetdb1dd4d2009-02-06 15:25:24 -07002428 spin_unlock(&file->f_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Alan Coxaf9b8972006-08-27 01:24:01 -07002432/**
2433 * tiocsctty - set controlling tty
2434 * @tty: tty structure
2435 * @arg: user argument
2436 *
2437 * This ioctl is used to manage job control. It permits a session
2438 * leader to set this tty as the controlling tty for the session.
2439 *
2440 * Locking:
Peter Hurleye218eb32014-10-16 14:59:49 -04002441 * Takes tty_lock() to serialize proc_set_tty() for this tty
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002442 * Takes tasklist_lock internally to walk sessions
2443 * Takes ->siglock() when updating signal->tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002444 */
2445
Jann Horn0c5562712015-10-04 19:29:12 +02002446static int tiocsctty(struct tty_struct *tty, struct file *file, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002448 int ret = 0;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002449
Peter Hurleye218eb32014-10-16 14:59:49 -04002450 tty_lock(tty);
Peter Hurley2c411c12014-10-16 14:59:47 -04002451 read_lock(&tasklist_lock);
2452
2453 if (current->signal->leader && (task_session(current) == tty->session))
2454 goto unlock;
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /*
2457 * The process must be a session leader and
2458 * not have a controlling tty already.
2459 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002460 if (!current->signal->leader || current->signal->tty) {
2461 ret = -EPERM;
2462 goto unlock;
2463 }
2464
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002465 if (tty->session) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
2467 * This tty is already the controlling
2468 * tty for another session group!
2469 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002470 if (arg == 1 && capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /*
2472 * Steal it away
2473 */
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002474 session_clear_tty(tty->session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002475 } else {
2476 ret = -EPERM;
2477 goto unlock;
2478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Jann Horn0c5562712015-10-04 19:29:12 +02002480
2481 /* See the comment in tty_open(). */
2482 if ((file->f_mode & FMODE_READ) == 0 && !capable(CAP_SYS_ADMIN)) {
2483 ret = -EPERM;
2484 goto unlock;
2485 }
2486
Peter Hurleybce65f12014-10-16 14:59:43 -04002487 proc_set_tty(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002488unlock:
Peter Hurley2c411c12014-10-16 14:59:47 -04002489 read_unlock(&tasklist_lock);
Peter Hurleye218eb32014-10-16 14:59:49 -04002490 tty_unlock(tty);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002491 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
Alan Coxaf9b8972006-08-27 01:24:01 -07002494/**
Alan Cox5d0fdf12008-04-30 00:53:31 -07002495 * tty_get_pgrp - return a ref counted pgrp pid
2496 * @tty: tty to read
2497 *
2498 * Returns a refcounted instance of the pid struct for the process
2499 * group controlling the tty.
2500 */
2501
2502struct pid *tty_get_pgrp(struct tty_struct *tty)
2503{
2504 unsigned long flags;
2505 struct pid *pgrp;
2506
2507 spin_lock_irqsave(&tty->ctrl_lock, flags);
2508 pgrp = get_pid(tty->pgrp);
2509 spin_unlock_irqrestore(&tty->ctrl_lock, flags);
2510
2511 return pgrp;
2512}
2513EXPORT_SYMBOL_GPL(tty_get_pgrp);
2514
Peter Hurleye1c22962014-10-16 14:59:48 -04002515/*
2516 * This checks not only the pgrp, but falls back on the pid if no
2517 * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
2518 * without this...
2519 *
2520 * The caller must hold rcu lock or the tasklist lock.
2521 */
2522static struct pid *session_of_pgrp(struct pid *pgrp)
2523{
2524 struct task_struct *p;
2525 struct pid *sid = NULL;
2526
2527 p = pid_task(pgrp, PIDTYPE_PGID);
2528 if (p == NULL)
2529 p = pid_task(pgrp, PIDTYPE_PID);
2530 if (p != NULL)
2531 sid = task_session(p);
2532
2533 return sid;
2534}
2535
Alan Cox5d0fdf12008-04-30 00:53:31 -07002536/**
Alan Coxaf9b8972006-08-27 01:24:01 -07002537 * tiocgpgrp - get process group
2538 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002539 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002540 * @p: returned pid
2541 *
2542 * Obtain the process group of the tty. If there is no process group
2543 * return an error.
2544 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002545 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002546 */
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2549{
Alan Cox5d0fdf12008-04-30 00:53:31 -07002550 struct pid *pid;
2551 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /*
2553 * (tty == real_tty) is a cheap way of
2554 * testing if the tty is NOT a master pty.
2555 */
2556 if (tty == real_tty && current->signal->tty != real_tty)
2557 return -ENOTTY;
Alan Cox5d0fdf12008-04-30 00:53:31 -07002558 pid = tty_get_pgrp(real_tty);
2559 ret = put_user(pid_vnr(pid), p);
2560 put_pid(pid);
2561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Alan Coxaf9b8972006-08-27 01:24:01 -07002564/**
2565 * tiocspgrp - attempt to set process group
2566 * @tty: tty passed by user
2567 * @real_tty: tty side device matching tty passed by user
2568 * @p: pid pointer
2569 *
2570 * Set the process group of the tty to the session passed. Only
2571 * permitted where the tty session is our session.
2572 *
Alan Cox47f86832008-04-30 00:53:30 -07002573 * Locking: RCU, ctrl lock
Alan Coxaf9b8972006-08-27 01:24:01 -07002574 */
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2577{
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002578 struct pid *pgrp;
2579 pid_t pgrp_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 int retval = tty_check_change(real_tty);
2581
2582 if (retval == -EIO)
2583 return -ENOTTY;
2584 if (retval)
2585 return retval;
2586 if (!current->signal->tty ||
2587 (current->signal->tty != real_tty) ||
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002588 (real_tty->session != task_session(current)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return -ENOTTY;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002590 if (get_user(pgrp_nr, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 return -EFAULT;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002592 if (pgrp_nr < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 return -EINVAL;
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002594 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002595 pgrp = find_vpid(pgrp_nr);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002596 retval = -ESRCH;
2597 if (!pgrp)
2598 goto out_unlock;
2599 retval = -EPERM;
2600 if (session_of_pgrp(pgrp) != task_session(current))
2601 goto out_unlock;
2602 retval = 0;
Peter Hurley1e86b5b2015-10-10 20:28:43 -04002603 spin_lock_irq(&tty->ctrl_lock);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002604 put_pid(real_tty->pgrp);
2605 real_tty->pgrp = get_pid(pgrp);
Peter Hurley1e86b5b2015-10-10 20:28:43 -04002606 spin_unlock_irq(&tty->ctrl_lock);
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -08002607out_unlock:
2608 rcu_read_unlock();
2609 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Alan Coxaf9b8972006-08-27 01:24:01 -07002612/**
2613 * tiocgsid - get session id
2614 * @tty: tty passed by user
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002615 * @real_tty: tty side of the tty passed by the user if a pty else the tty
Alan Coxaf9b8972006-08-27 01:24:01 -07002616 * @p: pointer to returned session id
2617 *
2618 * Obtain the session id of the tty. If there is no session
2619 * return an error.
2620 *
Peter Zijlstra24ec8392006-12-08 02:36:04 -08002621 * Locking: none. Reference to current->signal->tty is safe.
Alan Coxaf9b8972006-08-27 01:24:01 -07002622 */
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
2625{
2626 /*
2627 * (tty == real_tty) is a cheap way of
2628 * testing if the tty is NOT a master pty.
2629 */
2630 if (tty == real_tty && current->signal->tty != real_tty)
2631 return -ENOTTY;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08002632 if (!real_tty->session)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return -ENOTTY;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002634 return put_user(pid_vnr(real_tty->session), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635}
2636
Alan Coxaf9b8972006-08-27 01:24:01 -07002637/**
2638 * tiocsetd - set line discipline
2639 * @tty: tty device
2640 * @p: pointer to user data
2641 *
2642 * Set the line discipline according to user request.
2643 *
2644 * Locking: see tty_set_ldisc, this function is just a helper
2645 */
2646
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647static int tiocsetd(struct tty_struct *tty, int __user *p)
2648{
2649 int ldisc;
Alan Cox04f378b2008-04-30 00:53:29 -07002650 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 if (get_user(ldisc, p))
2653 return -EFAULT;
Alan Cox04f378b2008-04-30 00:53:29 -07002654
Alan Cox04f378b2008-04-30 00:53:29 -07002655 ret = tty_set_ldisc(tty, ldisc);
Alan Cox04f378b2008-04-30 00:53:29 -07002656
2657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
Alan Coxaf9b8972006-08-27 01:24:01 -07002660/**
2661 * send_break - performed time break
2662 * @tty: device to break on
2663 * @duration: timeout in mS
2664 *
2665 * Perform a timed break on hardware that lacks its own driver level
2666 * timed break functionality.
2667 *
2668 * Locking:
Alan Cox28298232006-09-29 02:00:58 -07002669 * atomic_write_lock serializes
Alan Coxaf9b8972006-08-27 01:24:01 -07002670 *
Alan Coxaf9b8972006-08-27 01:24:01 -07002671 */
2672
Domen Puncerb20f3ae2005-06-25 14:58:42 -07002673static int send_break(struct tty_struct *tty, unsigned int duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Alan Cox9e989662008-07-22 11:18:03 +01002675 int retval;
2676
2677 if (tty->ops->break_ctl == NULL)
2678 return 0;
2679
2680 if (tty->driver->flags & TTY_DRIVER_HARDWARE_BREAK)
2681 retval = tty->ops->break_ctl(tty, duration);
2682 else {
2683 /* Do the work ourselves */
2684 if (tty_write_lock(tty, 0) < 0)
2685 return -EINTR;
2686 retval = tty->ops->break_ctl(tty, -1);
2687 if (retval)
2688 goto out;
2689 if (!signal_pending(current))
2690 msleep_interruptible(duration);
2691 retval = tty->ops->break_ctl(tty, 0);
2692out:
2693 tty_write_unlock(tty);
2694 if (signal_pending(current))
2695 retval = -EINTR;
2696 }
2697 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698}
2699
Alan Coxaf9b8972006-08-27 01:24:01 -07002700/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002701 * tty_tiocmget - get modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002702 * @tty: tty device
2703 * @file: user file pointer
2704 * @p: pointer to result
2705 *
2706 * Obtain the modem status bits from the tty driver if the feature
2707 * is supported. Return -EINVAL if it is not available.
2708 *
2709 * Locking: none (up to the driver)
2710 */
2711
Alan Cox60b33c12011-02-14 16:26:14 +00002712static int tty_tiocmget(struct tty_struct *tty, int __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 int retval = -EINVAL;
2715
Alan Coxf34d7a52008-04-30 00:54:13 -07002716 if (tty->ops->tiocmget) {
Alan Cox60b33c12011-02-14 16:26:14 +00002717 retval = tty->ops->tiocmget(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 if (retval >= 0)
2720 retval = put_user(retval, p);
2721 }
2722 return retval;
2723}
2724
Alan Coxaf9b8972006-08-27 01:24:01 -07002725/**
Alan Coxf34d7a52008-04-30 00:54:13 -07002726 * tty_tiocmset - set modem status
Alan Coxaf9b8972006-08-27 01:24:01 -07002727 * @tty: tty device
Alan Coxaf9b8972006-08-27 01:24:01 -07002728 * @cmd: command - clear bits, set bits or set all
2729 * @p: pointer to desired bits
2730 *
2731 * Set the modem status bits from the tty driver if the feature
2732 * is supported. Return -EINVAL if it is not available.
2733 *
2734 * Locking: none (up to the driver)
2735 */
2736
Alan Cox20b9d172011-02-14 16:26:50 +00002737static int tty_tiocmset(struct tty_struct *tty, unsigned int cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 unsigned __user *p)
2739{
Alan Coxae677512008-07-16 21:56:54 +01002740 int retval;
2741 unsigned int set, clear, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Alan Coxae677512008-07-16 21:56:54 +01002743 if (tty->ops->tiocmset == NULL)
2744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Alan Coxae677512008-07-16 21:56:54 +01002746 retval = get_user(val, p);
2747 if (retval)
2748 return retval;
2749 set = clear = 0;
2750 switch (cmd) {
2751 case TIOCMBIS:
2752 set = val;
2753 break;
2754 case TIOCMBIC:
2755 clear = val;
2756 break;
2757 case TIOCMSET:
2758 set = val;
2759 clear = ~val;
2760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
Alan Coxae677512008-07-16 21:56:54 +01002762 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
2763 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
Alan Cox20b9d172011-02-14 16:26:50 +00002764 return tty->ops->tiocmset(tty, set, clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
Alan Coxd281da72010-09-16 18:21:24 +01002767static int tty_tiocgicount(struct tty_struct *tty, void __user *arg)
2768{
2769 int retval = -EINVAL;
2770 struct serial_icounter_struct icount;
2771 memset(&icount, 0, sizeof(icount));
2772 if (tty->ops->get_icount)
2773 retval = tty->ops->get_icount(tty, &icount);
2774 if (retval != 0)
2775 return retval;
2776 if (copy_to_user(arg, &icount, sizeof(icount)))
2777 return -EFAULT;
2778 return 0;
2779}
2780
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002781static void tty_warn_deprecated_flags(struct serial_struct __user *ss)
2782{
2783 static DEFINE_RATELIMIT_STATE(depr_flags,
2784 DEFAULT_RATELIMIT_INTERVAL,
2785 DEFAULT_RATELIMIT_BURST);
2786 char comm[TASK_COMM_LEN];
2787 int flags;
2788
2789 if (get_user(flags, &ss->flags))
2790 return;
2791
2792 flags &= ASYNC_DEPRECATED;
2793
2794 if (flags && __ratelimit(&depr_flags))
2795 pr_warning("%s: '%s' is using deprecated serial flags (with no effect): %.8x\n",
2796 __func__, get_task_comm(comm, current), flags);
2797}
2798
Peter Hurley8f166e02014-10-16 14:59:41 -04002799/*
2800 * if pty, return the slave side (real_tty)
2801 * otherwise, return self
2802 */
2803static struct tty_struct *tty_pair_get_tty(struct tty_struct *tty)
Alan Coxe8b70e72009-06-11 12:48:02 +01002804{
2805 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
2806 tty->driver->subtype == PTY_TYPE_MASTER)
2807 tty = tty->link;
2808 return tty;
2809}
Alan Coxe8b70e72009-06-11 12:48:02 +01002810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
2812 * Split this up, as gcc can choke on it otherwise..
2813 */
Alan Cox04f378b2008-04-30 00:53:29 -07002814long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Nick Piggind996b622010-08-18 04:37:36 +10002816 struct tty_struct *tty = file_tty(file);
2817 struct tty_struct *real_tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 void __user *p = (void __user *)arg;
2819 int retval;
2820 struct tty_ldisc *ld;
Alan Cox37bdfb02008-02-08 04:18:47 -08002821
Al Viro6131ffa2013-02-27 16:59:05 -05002822 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return -EINVAL;
2824
Alan Coxe8b70e72009-06-11 12:48:02 +01002825 real_tty = tty_pair_get_tty(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 /*
2828 * Factor out some common prep work
2829 */
2830 switch (cmd) {
2831 case TIOCSETD:
2832 case TIOCSBRK:
2833 case TIOCCBRK:
2834 case TCSBRK:
Alan Cox37bdfb02008-02-08 04:18:47 -08002835 case TCSBRKP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 retval = tty_check_change(tty);
2837 if (retval)
2838 return retval;
2839 if (cmd != TIOCCBRK) {
2840 tty_wait_until_sent(tty, 0);
2841 if (signal_pending(current))
2842 return -EINTR;
2843 }
2844 break;
2845 }
2846
Alan Cox9e989662008-07-22 11:18:03 +01002847 /*
2848 * Now do the stuff.
2849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 switch (cmd) {
Alan Cox37bdfb02008-02-08 04:18:47 -08002851 case TIOCSTI:
2852 return tiocsti(tty, p);
2853 case TIOCGWINSZ:
Alan Cox8f520022008-10-13 10:38:46 +01002854 return tiocgwinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002855 case TIOCSWINSZ:
Alan Coxfc6f6232009-01-02 13:43:17 +00002856 return tiocswinsz(real_tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002857 case TIOCCONS:
2858 return real_tty != tty ? -EINVAL : tioccons(file);
2859 case FIONBIO:
2860 return fionbio(file, p);
2861 case TIOCEXCL:
2862 set_bit(TTY_EXCLUSIVE, &tty->flags);
2863 return 0;
2864 case TIOCNXCL:
2865 clear_bit(TTY_EXCLUSIVE, &tty->flags);
2866 return 0;
Cyrill Gorcunov84fd7bd2012-10-24 23:43:22 +04002867 case TIOCGEXCL:
2868 {
2869 int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2870 return put_user(excl, (int __user *)p);
2871 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002872 case TIOCNOTTY:
2873 if (current->signal->tty != tty)
2874 return -ENOTTY;
2875 no_tty();
2876 return 0;
2877 case TIOCSCTTY:
Jann Horn0c5562712015-10-04 19:29:12 +02002878 return tiocsctty(tty, file, arg);
Alan Cox37bdfb02008-02-08 04:18:47 -08002879 case TIOCGPGRP:
2880 return tiocgpgrp(tty, real_tty, p);
2881 case TIOCSPGRP:
2882 return tiocspgrp(tty, real_tty, p);
2883 case TIOCGSID:
2884 return tiocgsid(tty, real_tty, p);
2885 case TIOCGETD:
Alan Coxc65c9bc2009-06-11 12:50:12 +01002886 return put_user(tty->ldisc->ops->num, (int __user *)p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002887 case TIOCSETD:
2888 return tiocsetd(tty, p);
Kay Sievers3c95c982011-02-17 18:39:28 +01002889 case TIOCVHANGUP:
2890 if (!capable(CAP_SYS_ADMIN))
2891 return -EPERM;
2892 tty_vhangup(tty);
2893 return 0;
Werner Finkb7b8de02010-12-03 12:48:23 +01002894 case TIOCGDEV:
2895 {
2896 unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2897 return put_user(ret, (unsigned int __user *)p);
2898 }
Alan Cox37bdfb02008-02-08 04:18:47 -08002899 /*
2900 * Break handling
2901 */
2902 case TIOCSBRK: /* Turn break on, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002903 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002904 return tty->ops->break_ctl(tty, -1);
Alan Cox37bdfb02008-02-08 04:18:47 -08002905 return 0;
Alan Cox37bdfb02008-02-08 04:18:47 -08002906 case TIOCCBRK: /* Turn break off, unconditionally */
Alan Coxf34d7a52008-04-30 00:54:13 -07002907 if (tty->ops->break_ctl)
Alan Cox9e989662008-07-22 11:18:03 +01002908 return tty->ops->break_ctl(tty, 0);
Alan Cox37bdfb02008-02-08 04:18:47 -08002909 return 0;
2910 case TCSBRK: /* SVID version: non-zero arg --> no break */
2911 /* non-zero arg means wait for all output data
2912 * to be sent (performed above) but don't send break.
2913 * This is used by the tcdrain() termios function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
Alan Cox37bdfb02008-02-08 04:18:47 -08002915 if (!arg)
2916 return send_break(tty, 250);
2917 return 0;
2918 case TCSBRKP: /* support for POSIX tcsendbreak() */
2919 return send_break(tty, arg ? arg*100 : 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Alan Cox37bdfb02008-02-08 04:18:47 -08002921 case TIOCMGET:
Alan Cox60b33c12011-02-14 16:26:14 +00002922 return tty_tiocmget(tty, p);
Alan Cox37bdfb02008-02-08 04:18:47 -08002923 case TIOCMSET:
2924 case TIOCMBIC:
2925 case TIOCMBIS:
Alan Cox20b9d172011-02-14 16:26:50 +00002926 return tty_tiocmset(tty, cmd, p);
Alan Coxd281da72010-09-16 18:21:24 +01002927 case TIOCGICOUNT:
2928 retval = tty_tiocgicount(tty, p);
2929 /* For the moment allow fall through to the old method */
2930 if (retval != -EINVAL)
2931 return retval;
2932 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002933 case TCFLSH:
2934 switch (arg) {
2935 case TCIFLUSH:
2936 case TCIOFLUSH:
2937 /* flush tty buffer and allow ldisc to process ioctl */
Peter Hurley86c80a82014-11-05 12:13:09 -05002938 tty_buffer_flush(tty, NULL);
Paul Fulghumc5c34d42007-05-12 10:36:55 -07002939 break;
Alan Cox37bdfb02008-02-08 04:18:47 -08002940 }
2941 break;
Jiri Slaby8a8ae622014-11-06 16:56:33 +01002942 case TIOCSSERIAL:
2943 tty_warn_deprecated_flags(p);
2944 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 }
Alan Coxf34d7a52008-04-30 00:54:13 -07002946 if (tty->ops->ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002947 retval = tty->ops->ioctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (retval != -ENOIOCTLCMD)
2949 return retval;
2950 }
2951 ld = tty_ldisc_ref_wait(tty);
2952 retval = -EINVAL;
Alan Coxa352def2008-07-16 21:53:12 +01002953 if (ld->ops->ioctl) {
2954 retval = ld->ops->ioctl(tty, file, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (retval == -ENOIOCTLCMD)
Wanlong Gaobbb63c52012-08-27 15:23:12 +08002956 retval = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 tty_ldisc_deref(ld);
2959 return retval;
2960}
2961
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002962#ifdef CONFIG_COMPAT
Alan Cox37bdfb02008-02-08 04:18:47 -08002963static long tty_compat_ioctl(struct file *file, unsigned int cmd,
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002964 unsigned long arg)
2965{
Nick Piggind996b622010-08-18 04:37:36 +10002966 struct tty_struct *tty = file_tty(file);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002967 struct tty_ldisc *ld;
2968 int retval = -ENOIOCTLCMD;
2969
Al Viro6131ffa2013-02-27 16:59:05 -05002970 if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002971 return -EINVAL;
2972
Alan Coxf34d7a52008-04-30 00:54:13 -07002973 if (tty->ops->compat_ioctl) {
Peter Hurleyc961bfb2014-11-05 12:26:25 -05002974 retval = tty->ops->compat_ioctl(tty, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002975 if (retval != -ENOIOCTLCMD)
2976 return retval;
2977 }
2978
2979 ld = tty_ldisc_ref_wait(tty);
Alan Coxa352def2008-07-16 21:53:12 +01002980 if (ld->ops->compat_ioctl)
2981 retval = ld->ops->compat_ioctl(tty, file, cmd, arg);
Thomas Meyer8193c422011-10-05 23:13:13 +02002982 else
2983 retval = n_tty_compat_ioctl_helper(tty, file, cmd, arg);
Paul Fulghume10cc1d2007-05-10 22:22:50 -07002984 tty_ldisc_deref(ld);
2985
2986 return retval;
2987}
2988#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Al Viroc3c073f2012-08-21 22:32:06 -04002990static int this_tty(const void *t, struct file *file, unsigned fd)
2991{
2992 if (likely(file->f_op->read != tty_read))
2993 return 0;
2994 return file_tty(file) != t ? 0 : fd + 1;
2995}
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997/*
2998 * This implements the "Secure Attention Key" --- the idea is to
2999 * prevent trojan horses by killing all processes associated with this
3000 * tty when the user hits the "Secure Attention Key". Required for
3001 * super-paranoid applications --- see the Orange Book for more details.
Alan Cox37bdfb02008-02-08 04:18:47 -08003002 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * This code could be nicer; ideally it should send a HUP, wait a few
3004 * seconds, then send a INT, and then a KILL signal. But you then
3005 * have to coordinate with the init process, since all processes associated
3006 * with the current tty must be dead before the new getty is allowed
3007 * to spawn.
3008 *
3009 * Now, if it would be correct ;-/ The current code has a nasty hole -
3010 * it doesn't catch files in flight. We may send the descriptor to ourselves
3011 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
3012 *
3013 * Nasty bug: do_SAK is being called in interrupt context. This can
3014 * deadlock. We punt it up to process context. AKPM - 16Mar2001
3015 */
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003016void __do_SAK(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
3018#ifdef TTY_SOFT_SAK
3019 tty_hangup(tty);
3020#else
Eric W. Biederman652486f2006-03-28 16:11:02 -08003021 struct task_struct *g, *p;
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003022 struct pid *session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 int i;
Alan Cox37bdfb02008-02-08 04:18:47 -08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 if (!tty)
3026 return;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003027 session = tty->session;
Alan Cox37bdfb02008-02-08 04:18:47 -08003028
Dan Carpenterb3f13de2006-12-13 00:35:09 -08003029 tty_ldisc_flush(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Alan Coxf34d7a52008-04-30 00:54:13 -07003031 tty_driver_flush_buffer(tty);
Alan Cox37bdfb02008-02-08 04:18:47 -08003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 read_lock(&tasklist_lock);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003034 /* Kill the entire session */
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003035 do_each_pid_task(session, PIDTYPE_SID, p) {
Eric W. Biederman652486f2006-03-28 16:11:02 -08003036 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07003037 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003038 task_pid_nr(p), p->comm);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003039 send_sig(SIGKILL, p, 1);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003040 } while_each_pid_task(session, PIDTYPE_SID, p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003041 /* Now kill any processes that happen to have the
3042 * tty open.
3043 */
3044 do_each_thread(g, p) {
3045 if (p->signal->tty == tty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 printk(KERN_NOTICE "SAK: killed process %d"
Oleg Nesterov1b0f7ff2009-04-02 16:58:39 -07003047 " (%s): task_session(p)==tty->session\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07003048 task_pid_nr(p), p->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 send_sig(SIGKILL, p, 1);
3050 continue;
3051 }
3052 task_lock(p);
Al Viroc3c073f2012-08-21 22:32:06 -04003053 i = iterate_fd(p->files, 0, this_tty, tty);
3054 if (i != 0) {
3055 printk(KERN_NOTICE "SAK: killed process %d"
3056 " (%s): fd#%d opened to the tty\n",
3057 task_pid_nr(p), p->comm, i - 1);
3058 force_sig(SIGKILL, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060 task_unlock(p);
Eric W. Biederman652486f2006-03-28 16:11:02 -08003061 } while_each_thread(g, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 read_unlock(&tasklist_lock);
3063#endif
3064}
3065
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08003066static void do_SAK_work(struct work_struct *work)
3067{
3068 struct tty_struct *tty =
3069 container_of(work, struct tty_struct, SAK_work);
3070 __do_SAK(tty);
3071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073/*
3074 * The tq handling here is a little racy - tty->SAK_work may already be queued.
3075 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
3076 * the values which we write to it will be identical to the values which it
3077 * already has. --akpm
3078 */
3079void do_SAK(struct tty_struct *tty)
3080{
3081 if (!tty)
3082 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 schedule_work(&tty->SAK_work);
3084}
3085
3086EXPORT_SYMBOL(do_SAK);
3087
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003088static int dev_match_devt(struct device *dev, const void *data)
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003089{
Greg Kroah-Hartman6e9430a2013-02-06 15:59:18 -08003090 const dev_t *devt = data;
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003091 return dev->devt == *devt;
3092}
3093
3094/* Must put_device() after it's unused! */
3095static struct device *tty_get_device(struct tty_struct *tty)
3096{
3097 dev_t devt = tty_devnum(tty);
3098 return class_find_device(tty_class, NULL, &devt, dev_match_devt);
3099}
3100
3101
Alan Coxaf9b8972006-08-27 01:24:01 -07003102/**
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003103 * alloc_tty_struct
Alan Coxaf9b8972006-08-27 01:24:01 -07003104 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003105 * This subroutine allocates and initializes a tty structure.
Alan Coxaf9b8972006-08-27 01:24:01 -07003106 *
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003107 * Locking: none - tty in question is not exposed at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003109
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003110struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003112 struct tty_struct *tty;
3113
3114 tty = kzalloc(sizeof(*tty), GFP_KERNEL);
3115 if (!tty)
3116 return NULL;
3117
Alan Cox9c9f4de2008-10-13 10:37:26 +01003118 kref_init(&tty->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 tty->magic = TTY_MAGIC;
Alan Cox01e1abb2008-07-22 11:16:55 +01003120 tty_ldisc_init(tty);
Eric W. Biedermanab521dc2007-02-12 00:53:00 -08003121 tty->session = NULL;
3122 tty->pgrp = NULL;
Alan Cox89c8d912012-08-08 16:30:13 +01003123 mutex_init(&tty->legacy_mutex);
Peter Hurleyd8c1f922013-06-15 09:14:31 -04003124 mutex_init(&tty->throttle_mutex);
Peter Hurley6a1c0682013-06-15 09:14:23 -04003125 init_rwsem(&tty->termios_rwsem);
Peter Hurleydee4a0b2013-07-24 16:43:51 -04003126 mutex_init(&tty->winsize_mutex);
Peter Hurley36697522013-06-15 07:04:48 -04003127 init_ldsem(&tty->ldisc_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 init_waitqueue_head(&tty->write_wait);
3129 init_waitqueue_head(&tty->read_wait);
David Howells65f27f32006-11-22 14:55:48 +00003130 INIT_WORK(&tty->hangup_work, do_tty_hangup);
Ingo Molnar70522e12006-03-23 03:00:31 -08003131 mutex_init(&tty->atomic_write_lock);
Alan Cox04f378b2008-04-30 00:53:29 -07003132 spin_lock_init(&tty->ctrl_lock);
Peter Hurleyf9e053d2014-09-10 15:06:31 -04003133 spin_lock_init(&tty->flow_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 INIT_LIST_HEAD(&tty->tty_files);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003135 INIT_WORK(&tty->SAK_work, do_SAK_work);
Alan Coxbf970ee2008-10-13 10:42:39 +01003136
3137 tty->driver = driver;
3138 tty->ops = driver->ops;
3139 tty->index = idx;
3140 tty_line_name(driver, idx, tty->name);
Dmitry Eremin-Solenikov30004ac2010-08-09 18:22:49 +04003141 tty->dev = tty_get_device(tty);
Rasmus Villemoes2c964a22014-07-10 21:01:22 +02003142
3143 return tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Alan Coxf34d7a52008-04-30 00:54:13 -07003146/**
Jiri Slaby67166712011-03-23 10:48:35 +01003147 * deinitialize_tty_struct
3148 * @tty: tty to deinitialize
3149 *
3150 * This subroutine deinitializes a tty structure that has been newly
3151 * allocated but tty_release cannot be called on that yet.
3152 *
3153 * Locking: none - tty in question must not be exposed at this point
3154 */
3155void deinitialize_tty_struct(struct tty_struct *tty)
3156{
3157 tty_ldisc_deinit(tty);
3158}
3159
3160/**
Alan Coxf34d7a52008-04-30 00:54:13 -07003161 * tty_put_char - write one character to a tty
3162 * @tty: tty
3163 * @ch: character
3164 *
3165 * Write one byte to the tty using the provided put_char method
3166 * if present. Returns the number of characters successfully output.
3167 *
3168 * Note: the specific put_char operation in the driver layer may go
3169 * away soon. Don't call it directly, use this method
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003171
Alan Coxf34d7a52008-04-30 00:54:13 -07003172int tty_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
Alan Coxf34d7a52008-04-30 00:54:13 -07003174 if (tty->ops->put_char)
3175 return tty->ops->put_char(tty, ch);
3176 return tty->ops->write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177}
Alan Coxf34d7a52008-04-30 00:54:13 -07003178EXPORT_SYMBOL_GPL(tty_put_char);
3179
Alan Coxd81ed102008-10-13 10:41:42 +01003180struct class *tty_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003182static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
3183 unsigned int index, unsigned int count)
3184{
Leon Yuc1a752b2015-09-07 13:08:37 +00003185 int err;
3186
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003187 /* init here, since reused cdevs cause crashes */
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003188 driver->cdevs[index] = cdev_alloc();
3189 if (!driver->cdevs[index])
3190 return -ENOMEM;
Leon Yuc1a752b2015-09-07 13:08:37 +00003191 driver->cdevs[index]->ops = &tty_fops;
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003192 driver->cdevs[index]->owner = driver->owner;
Leon Yuc1a752b2015-09-07 13:08:37 +00003193 err = cdev_add(driver->cdevs[index], dev, count);
3194 if (err)
3195 kobject_put(&driver->cdevs[index]->kobj);
3196 return err;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003200 * tty_register_device - register a tty device
3201 * @driver: the tty driver that describes the tty device
3202 * @index: the index in the tty driver for this tty device
3203 * @device: a struct device that is associated with this tty device.
3204 * This field is optional, if there is no known struct device
3205 * for this tty device it can be set to NULL safely.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 *
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003207 * Returns a pointer to the struct device for this tty device
3208 * (or ERR_PTR(-EFOO) on error).
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003209 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003210 * This call is required to be made to register an individual tty device
3211 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3212 * that bit is not set, this function should not be called by a tty
3213 * driver.
3214 *
3215 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003217
Greg Kroah-Hartman01107d32006-08-07 22:19:37 -07003218struct device *tty_register_device(struct tty_driver *driver, unsigned index,
3219 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003221 return tty_register_device_attr(driver, index, device, NULL, NULL);
3222}
3223EXPORT_SYMBOL(tty_register_device);
3224
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003225static void tty_device_create_release(struct device *dev)
3226{
3227 pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
3228 kfree(dev);
3229}
3230
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003231/**
3232 * tty_register_device_attr - register a tty device
3233 * @driver: the tty driver that describes the tty device
3234 * @index: the index in the tty driver for this tty device
3235 * @device: a struct device that is associated with this tty device.
3236 * This field is optional, if there is no known struct device
3237 * for this tty device it can be set to NULL safely.
3238 * @drvdata: Driver data to be set to device.
3239 * @attr_grp: Attribute group to be set on device.
3240 *
3241 * Returns a pointer to the struct device for this tty device
3242 * (or ERR_PTR(-EFOO) on error).
3243 *
3244 * This call is required to be made to register an individual tty device
3245 * if the tty driver's flags have the TTY_DRIVER_DYNAMIC_DEV bit set. If
3246 * that bit is not set, this function should not be called by a tty
3247 * driver.
3248 *
3249 * Locking: ??
3250 */
3251struct device *tty_register_device_attr(struct tty_driver *driver,
3252 unsigned index, struct device *device,
3253 void *drvdata,
3254 const struct attribute_group **attr_grp)
3255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 char name[64];
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003257 dev_t devt = MKDEV(driver->major, driver->minor_start) + index;
3258 struct device *dev = NULL;
3259 int retval = -ENODEV;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003260 bool cdev = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 if (index >= driver->num) {
3263 printk(KERN_ERR "Attempt to register invalid tty line number "
3264 " (%d).\n", index);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003265 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (driver->type == TTY_DRIVER_TYPE_PTY)
3269 pty_line_name(driver, index, name);
3270 else
3271 tty_line_name(driver, index, name);
Hansjoerg Lipp1cdcb6b2006-04-22 18:36:53 +02003272
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003273 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003274 retval = tty_cdev_add(driver, devt, index, 1);
3275 if (retval)
3276 goto error;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003277 cdev = true;
3278 }
3279
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003280 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3281 if (!dev) {
3282 retval = -ENOMEM;
3283 goto error;
3284 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003285
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003286 dev->devt = devt;
3287 dev->class = tty_class;
3288 dev->parent = device;
Tomas Hlavacekb1b79912012-09-06 23:17:47 +02003289 dev->release = tty_device_create_release;
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003290 dev_set_name(dev, "%s", name);
3291 dev->groups = attr_grp;
3292 dev_set_drvdata(dev, drvdata);
3293
3294 retval = device_register(dev);
3295 if (retval)
3296 goto error;
3297
3298 return dev;
3299
3300error:
3301 put_device(dev);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003302 if (cdev) {
3303 cdev_del(driver->cdevs[index]);
3304 driver->cdevs[index] = NULL;
3305 }
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003306 return ERR_PTR(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
Tomas Hlavacek6915c0e2012-09-06 03:17:18 +02003308EXPORT_SYMBOL_GPL(tty_register_device_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310/**
Alan Coxaf9b8972006-08-27 01:24:01 -07003311 * tty_unregister_device - unregister a tty device
3312 * @driver: the tty driver that describes the tty device
3313 * @index: the index in the tty driver for this tty device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 *
Alan Coxaf9b8972006-08-27 01:24:01 -07003315 * If a tty device is registered with a call to tty_register_device() then
3316 * this function must be called when the tty device is gone.
3317 *
3318 * Locking: ??
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 */
Alan Coxaf9b8972006-08-27 01:24:01 -07003320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321void tty_unregister_device(struct tty_driver *driver, unsigned index)
3322{
Alan Cox37bdfb02008-02-08 04:18:47 -08003323 device_destroy(tty_class,
3324 MKDEV(driver->major, driver->minor_start) + index);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003325 if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3326 cdev_del(driver->cdevs[index]);
3327 driver->cdevs[index] = NULL;
3328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330EXPORT_SYMBOL(tty_unregister_device);
3331
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003332/**
3333 * __tty_alloc_driver -- allocate tty driver
3334 * @lines: count of lines this driver can handle at most
3335 * @owner: module which is repsonsible for this driver
3336 * @flags: some of TTY_DRIVER_* flags, will be set in driver->flags
3337 *
3338 * This should not be called directly, some of the provided macros should be
3339 * used instead. Use IS_ERR and friends on @retval.
3340 */
3341struct tty_driver *__tty_alloc_driver(unsigned int lines, struct module *owner,
3342 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
3344 struct tty_driver *driver;
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003345 unsigned int cdevs = 1;
Jiri Slaby16a02082012-08-08 22:26:42 +02003346 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
Jiri Slaby0019b402012-08-08 22:26:43 +02003348 if (!lines || (flags & TTY_DRIVER_UNNUMBERED_NODE && lines > 1))
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003349 return ERR_PTR(-EINVAL);
3350
Jean Delvare506eb992007-07-15 23:40:14 -07003351 driver = kzalloc(sizeof(struct tty_driver), GFP_KERNEL);
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003352 if (!driver)
3353 return ERR_PTR(-ENOMEM);
3354
3355 kref_init(&driver->kref);
3356 driver->magic = TTY_DRIVER_MAGIC;
3357 driver->num = lines;
3358 driver->owner = owner;
3359 driver->flags = flags;
Jiri Slaby16a02082012-08-08 22:26:42 +02003360
3361 if (!(flags & TTY_DRIVER_DEVPTS_MEM)) {
3362 driver->ttys = kcalloc(lines, sizeof(*driver->ttys),
3363 GFP_KERNEL);
3364 driver->termios = kcalloc(lines, sizeof(*driver->termios),
3365 GFP_KERNEL);
3366 if (!driver->ttys || !driver->termios) {
3367 err = -ENOMEM;
3368 goto err_free_all;
3369 }
3370 }
3371
3372 if (!(flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
3373 driver->ports = kcalloc(lines, sizeof(*driver->ports),
3374 GFP_KERNEL);
3375 if (!driver->ports) {
3376 err = -ENOMEM;
3377 goto err_free_all;
3378 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003379 cdevs = lines;
3380 }
3381
3382 driver->cdevs = kcalloc(cdevs, sizeof(*driver->cdevs), GFP_KERNEL);
3383 if (!driver->cdevs) {
3384 err = -ENOMEM;
3385 goto err_free_all;
Jiri Slaby16a02082012-08-08 22:26:42 +02003386 }
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 return driver;
Jiri Slaby16a02082012-08-08 22:26:42 +02003389err_free_all:
3390 kfree(driver->ports);
3391 kfree(driver->ttys);
3392 kfree(driver->termios);
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003393 kfree(driver->cdevs);
Jiri Slaby16a02082012-08-08 22:26:42 +02003394 kfree(driver);
3395 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396}
Jiri Slaby7f0bc6a2012-08-07 21:47:42 +02003397EXPORT_SYMBOL(__tty_alloc_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Alan Cox7d7b93c2008-10-13 10:42:09 +01003399static void destruct_tty_driver(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003401 struct tty_driver *driver = container_of(kref, struct tty_driver, kref);
3402 int i;
3403 struct ktermios *tp;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003404
3405 if (driver->flags & TTY_DRIVER_INSTALLED) {
3406 /*
3407 * Free the termios and termios_locked structures because
3408 * we don't want to get memory leaks when modular tty
3409 * drivers are removed from the kernel.
3410 */
3411 for (i = 0; i < driver->num; i++) {
3412 tp = driver->termios[i];
3413 if (tp) {
3414 driver->termios[i] = NULL;
3415 kfree(tp);
3416 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003417 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV))
3418 tty_unregister_device(driver, i);
3419 }
Alan Cox7d7b93c2008-10-13 10:42:09 +01003420 proc_tty_unregister_driver(driver);
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003421 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)
Richard Wattsa3a10ce2015-05-19 16:06:53 +01003422 cdev_del(driver->cdevs[0]);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003423 }
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003424 kfree(driver->cdevs);
Jiri Slaby04831dc2012-06-04 13:35:36 +02003425 kfree(driver->ports);
Jiri Slaby16a02082012-08-08 22:26:42 +02003426 kfree(driver->termios);
3427 kfree(driver->ttys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 kfree(driver);
3429}
3430
Alan Cox7d7b93c2008-10-13 10:42:09 +01003431void tty_driver_kref_put(struct tty_driver *driver)
3432{
3433 kref_put(&driver->kref, destruct_tty_driver);
3434}
3435EXPORT_SYMBOL(tty_driver_kref_put);
3436
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003437void tty_set_operations(struct tty_driver *driver,
3438 const struct tty_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Alan Coxf34d7a52008-04-30 00:54:13 -07003440 driver->ops = op;
3441};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442EXPORT_SYMBOL(tty_set_operations);
3443
Alan Cox7d7b93c2008-10-13 10:42:09 +01003444void put_tty_driver(struct tty_driver *d)
3445{
3446 tty_driver_kref_put(d);
3447}
3448EXPORT_SYMBOL(put_tty_driver);
3449
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450/*
3451 * Called by a tty driver to register itself.
3452 */
3453int tty_register_driver(struct tty_driver *driver)
3454{
3455 int error;
Alan Cox37bdfb02008-02-08 04:18:47 -08003456 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 dev_t dev;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003458 struct device *d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 if (!driver->major) {
Alan Cox37bdfb02008-02-08 04:18:47 -08003461 error = alloc_chrdev_region(&dev, driver->minor_start,
3462 driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (!error) {
3464 driver->major = MAJOR(dev);
3465 driver->minor_start = MINOR(dev);
3466 }
3467 } else {
3468 dev = MKDEV(driver->major, driver->minor_start);
Geert Uytterhoevene5717c42007-02-20 15:45:21 +01003469 error = register_chrdev_region(dev, driver->num, driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 }
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003471 if (error < 0)
Jiri Slaby16a02082012-08-08 22:26:42 +02003472 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473
Jiri Slaby7e73eca2012-08-08 22:26:44 +02003474 if (driver->flags & TTY_DRIVER_DYNAMIC_ALLOC) {
3475 error = tty_cdev_add(driver, dev, 0, driver->num);
3476 if (error)
3477 goto err_unreg_char;
3478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003480 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 list_add(&driver->tty_drivers, &tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003482 mutex_unlock(&tty_mutex);
Alan Cox37bdfb02008-02-08 04:18:47 -08003483
3484 if (!(driver->flags & TTY_DRIVER_DYNAMIC_DEV)) {
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003485 for (i = 0; i < driver->num; i++) {
3486 d = tty_register_device(driver, i, NULL);
3487 if (IS_ERR(d)) {
3488 error = PTR_ERR(d);
Jiri Slaby16a02082012-08-08 22:26:42 +02003489 goto err_unreg_devs;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003490 }
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
3493 proc_tty_register_driver(driver);
Alan Cox7d7b93c2008-10-13 10:42:09 +01003494 driver->flags |= TTY_DRIVER_INSTALLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return 0;
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003496
Jiri Slaby16a02082012-08-08 22:26:42 +02003497err_unreg_devs:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003498 for (i--; i >= 0; i--)
3499 tty_unregister_device(driver, i);
3500
3501 mutex_lock(&tty_mutex);
3502 list_del(&driver->tty_drivers);
3503 mutex_unlock(&tty_mutex);
3504
Jiri Slaby9bb8a3d2012-06-04 13:35:35 +02003505err_unreg_char:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003506 unregister_chrdev_region(dev, driver->num);
Jiri Slaby16a02082012-08-08 22:26:42 +02003507err:
Vasiliy Kulikovb670bde2010-09-05 22:32:22 +04003508 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510EXPORT_SYMBOL(tty_register_driver);
3511
3512/*
3513 * Called by a tty driver to unregister itself.
3514 */
3515int tty_unregister_driver(struct tty_driver *driver)
3516{
Alan Cox7d7b93c2008-10-13 10:42:09 +01003517#if 0
3518 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (driver->refcount)
3520 return -EBUSY;
Alan Cox7d7b93c2008-10-13 10:42:09 +01003521#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 unregister_chrdev_region(MKDEV(driver->major, driver->minor_start),
3523 driver->num);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003524 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 list_del(&driver->tty_drivers);
Alexey Dobriyanca509f62007-05-08 00:27:12 -07003526 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 return 0;
3528}
Alan Cox7d7b93c2008-10-13 10:42:09 +01003529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530EXPORT_SYMBOL(tty_unregister_driver);
3531
Peter Zijlstra24ec8392006-12-08 02:36:04 -08003532dev_t tty_devnum(struct tty_struct *tty)
3533{
3534 return MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index;
3535}
3536EXPORT_SYMBOL(tty_devnum);
3537
Alan Coxd81ed102008-10-13 10:41:42 +01003538void tty_default_fops(struct file_operations *fops)
3539{
3540 *fops = tty_fops;
3541}
3542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543/*
3544 * Initialize the console device. This is called *early*, so
3545 * we can't necessarily depend on lots of kernel help here.
3546 * Just do some early initializations, and do the complex setup
3547 * later.
3548 */
3549void __init console_init(void)
3550{
3551 initcall_t *call;
3552
3553 /* Setup the default TTY line discipline. */
Alan Cox01e1abb2008-07-22 11:16:55 +01003554 tty_ldisc_begin();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 /*
Alan Cox37bdfb02008-02-08 04:18:47 -08003557 * set up the console device so that later boot sequences can
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 * inform about problems etc..
3559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 call = __con_initcall_start;
3561 while (call < __con_initcall_end) {
3562 (*call)();
3563 call++;
3564 }
3565}
3566
Al Viro2c9ede52011-07-23 20:24:48 -04003567static char *tty_devnode(struct device *dev, umode_t *mode)
Kay Sieverse454cea2009-09-18 23:01:12 +02003568{
3569 if (!mode)
3570 return NULL;
3571 if (dev->devt == MKDEV(TTYAUX_MAJOR, 0) ||
3572 dev->devt == MKDEV(TTYAUX_MAJOR, 2))
3573 *mode = 0666;
3574 return NULL;
3575}
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577static int __init tty_class_init(void)
3578{
gregkh@suse.de7fe845d2005-03-15 14:23:15 -08003579 tty_class = class_create(THIS_MODULE, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 if (IS_ERR(tty_class))
3581 return PTR_ERR(tty_class);
Kay Sieverse454cea2009-09-18 23:01:12 +02003582 tty_class->devnode = tty_devnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 return 0;
3584}
3585
3586postcore_initcall(tty_class_init);
3587
3588/* 3/2004 jmc: why do these devices exist? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589static struct cdev tty_cdev, console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Kay Sieversfbc92a32010-12-01 18:51:05 +01003591static ssize_t show_cons_active(struct device *dev,
3592 struct device_attribute *attr, char *buf)
3593{
3594 struct console *cs[16];
3595 int i = 0;
3596 struct console *c;
3597 ssize_t count = 0;
3598
Torben Hohnac751ef2011-01-25 15:07:35 -08003599 console_lock();
Kay Sieversa2a6a822011-01-09 16:39:14 +01003600 for_each_console(c) {
Kay Sieversfbc92a32010-12-01 18:51:05 +01003601 if (!c->device)
3602 continue;
3603 if (!c->write)
3604 continue;
3605 if ((c->flags & CON_ENABLED) == 0)
3606 continue;
3607 cs[i++] = c;
3608 if (i >= ARRAY_SIZE(cs))
3609 break;
3610 }
Hannes Reinecke723abd82014-02-27 12:30:51 +01003611 while (i--) {
3612 int index = cs[i]->index;
3613 struct tty_driver *drv = cs[i]->device(cs[i], &index);
3614
3615 /* don't resolve tty0 as some programs depend on it */
3616 if (drv && (cs[i]->index > 0 || drv->major != TTY_MAJOR))
3617 count += tty_line_name(drv, index, buf + count);
3618 else
3619 count += sprintf(buf + count, "%s%d",
3620 cs[i]->name, cs[i]->index);
3621
3622 count += sprintf(buf + count, "%c", i ? ' ':'\n');
3623 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003624 console_unlock();
Kay Sieversfbc92a32010-12-01 18:51:05 +01003625
3626 return count;
3627}
3628static DEVICE_ATTR(active, S_IRUGO, show_cons_active, NULL);
3629
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003630static struct attribute *cons_dev_attrs[] = {
3631 &dev_attr_active.attr,
3632 NULL
3633};
3634
3635ATTRIBUTE_GROUPS(cons_dev);
3636
Kay Sieversfbc92a32010-12-01 18:51:05 +01003637static struct device *consdev;
3638
3639void console_sysfs_notify(void)
3640{
3641 if (consdev)
3642 sysfs_notify(&consdev->kobj, NULL, "active");
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645/*
3646 * Ok, now we can initialize the rest of the tty devices and can count
3647 * on memory allocations, interrupts etc..
3648 */
David Howells31d1d482010-08-06 16:34:43 +01003649int __init tty_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 cdev_init(&tty_cdev, &tty_fops);
3652 if (cdev_add(&tty_cdev, MKDEV(TTYAUX_MAJOR, 0), 1) ||
3653 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 0), 1, "/dev/tty") < 0)
3654 panic("Couldn't register /dev/tty driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003655 device_create(tty_class, NULL, MKDEV(TTYAUX_MAJOR, 0), NULL, "tty");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 cdev_init(&console_cdev, &console_fops);
3658 if (cdev_add(&console_cdev, MKDEV(TTYAUX_MAJOR, 1), 1) ||
3659 register_chrdev_region(MKDEV(TTYAUX_MAJOR, 1), 1, "/dev/console") < 0)
3660 panic("Couldn't register /dev/console driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003661 consdev = device_create_with_groups(tty_class, NULL,
3662 MKDEV(TTYAUX_MAJOR, 1), NULL,
3663 cons_dev_groups, "console");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003664 if (IS_ERR(consdev))
3665 consdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667#ifdef CONFIG_VT
Alan Coxd81ed102008-10-13 10:41:42 +01003668 vty_init(&console_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669#endif
3670 return 0;
3671}
David Howells31d1d482010-08-06 16:34:43 +01003672