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