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