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