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