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