ia64/linux-2.6.18-xen.hg

annotate drivers/char/tty_io.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 04 10:32:57 2009 +0100 (2009-06-04)
parents a533be77c572
children
rev   line source
ian@0 1 /*
ian@0 2 * linux/drivers/char/tty_io.c
ian@0 3 *
ian@0 4 * Copyright (C) 1991, 1992 Linus Torvalds
ian@0 5 */
ian@0 6
ian@0 7 /*
ian@0 8 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
ian@0 9 * or rs-channels. It also implements echoing, cooked mode etc.
ian@0 10 *
ian@0 11 * Kill-line thanks to John T Kohl, who also corrected VMIN = VTIME = 0.
ian@0 12 *
ian@0 13 * Modified by Theodore Ts'o, 9/14/92, to dynamically allocate the
ian@0 14 * tty_struct and tty_queue structures. Previously there was an array
ian@0 15 * of 256 tty_struct's which was statically allocated, and the
ian@0 16 * tty_queue structures were allocated at boot time. Both are now
ian@0 17 * dynamically allocated only when the tty is open.
ian@0 18 *
ian@0 19 * Also restructured routines so that there is more of a separation
ian@0 20 * between the high-level tty routines (tty_io.c and tty_ioctl.c) and
ian@0 21 * the low-level tty routines (serial.c, pty.c, console.c). This
ian@0 22 * makes for cleaner and more compact code. -TYT, 9/17/92
ian@0 23 *
ian@0 24 * Modified by Fred N. van Kempen, 01/29/93, to add line disciplines
ian@0 25 * which can be dynamically activated and de-activated by the line
ian@0 26 * discipline handling modules (like SLIP).
ian@0 27 *
ian@0 28 * NOTE: pay no attention to the line discipline code (yet); its
ian@0 29 * interface is still subject to change in this version...
ian@0 30 * -- TYT, 1/31/92
ian@0 31 *
ian@0 32 * Added functionality to the OPOST tty handling. No delays, but all
ian@0 33 * other bits should be there.
ian@0 34 * -- Nick Holloway <alfie@dcs.warwick.ac.uk>, 27th May 1993.
ian@0 35 *
ian@0 36 * Rewrote canonical mode and added more termios flags.
ian@0 37 * -- julian@uhunix.uhcc.hawaii.edu (J. Cowley), 13Jan94
ian@0 38 *
ian@0 39 * Reorganized FASYNC support so mouse code can share it.
ian@0 40 * -- ctm@ardi.com, 9Sep95
ian@0 41 *
ian@0 42 * New TIOCLINUX variants added.
ian@0 43 * -- mj@k332.feld.cvut.cz, 19-Nov-95
ian@0 44 *
ian@0 45 * Restrict vt switching via ioctl()
ian@0 46 * -- grif@cs.ucr.edu, 5-Dec-95
ian@0 47 *
ian@0 48 * Move console and virtual terminal code to more appropriate files,
ian@0 49 * implement CONFIG_VT and generalize console device interface.
ian@0 50 * -- Marko Kohtala <Marko.Kohtala@hut.fi>, March 97
ian@0 51 *
ian@0 52 * Rewrote init_dev and release_dev to eliminate races.
ian@0 53 * -- Bill Hawes <whawes@star.net>, June 97
ian@0 54 *
ian@0 55 * Added devfs support.
ian@0 56 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 13-Jan-1998
ian@0 57 *
ian@0 58 * Added support for a Unix98-style ptmx device.
ian@0 59 * -- C. Scott Ananian <cananian@alumni.princeton.edu>, 14-Jan-1998
ian@0 60 *
ian@0 61 * Reduced memory usage for older ARM systems
ian@0 62 * -- Russell King <rmk@arm.linux.org.uk>
ian@0 63 *
ian@0 64 * Move do_SAK() into process context. Less stack use in devfs functions.
ian@0 65 * alloc_tty_struct() always uses kmalloc() -- Andrew Morton <andrewm@uow.edu.eu> 17Mar01
ian@0 66 */
ian@0 67
ian@0 68 #include <linux/types.h>
ian@0 69 #include <linux/major.h>
ian@0 70 #include <linux/errno.h>
ian@0 71 #include <linux/signal.h>
ian@0 72 #include <linux/fcntl.h>
ian@0 73 #include <linux/sched.h>
ian@0 74 #include <linux/interrupt.h>
ian@0 75 #include <linux/tty.h>
ian@0 76 #include <linux/tty_driver.h>
ian@0 77 #include <linux/tty_flip.h>
ian@0 78 #include <linux/devpts_fs.h>
ian@0 79 #include <linux/file.h>
ian@0 80 #include <linux/console.h>
ian@0 81 #include <linux/timer.h>
ian@0 82 #include <linux/ctype.h>
ian@0 83 #include <linux/kd.h>
ian@0 84 #include <linux/mm.h>
ian@0 85 #include <linux/string.h>
ian@0 86 #include <linux/slab.h>
ian@0 87 #include <linux/poll.h>
ian@0 88 #include <linux/proc_fs.h>
ian@0 89 #include <linux/init.h>
ian@0 90 #include <linux/module.h>
ian@0 91 #include <linux/smp_lock.h>
ian@0 92 #include <linux/device.h>
ian@0 93 #include <linux/idr.h>
ian@0 94 #include <linux/wait.h>
ian@0 95 #include <linux/bitops.h>
ian@0 96 #include <linux/delay.h>
ian@0 97
ian@0 98 #include <asm/uaccess.h>
ian@0 99 #include <asm/system.h>
ian@0 100
ian@0 101 #include <linux/kbd_kern.h>
ian@0 102 #include <linux/vt_kern.h>
ian@0 103 #include <linux/selection.h>
ian@0 104
ian@0 105 #include <linux/kmod.h>
ian@0 106
ian@0 107 #undef TTY_DEBUG_HANGUP
ian@0 108
ian@0 109 #define TTY_PARANOIA_CHECK 1
ian@0 110 #define CHECK_TTY_COUNT 1
ian@0 111
ian@0 112 struct termios tty_std_termios = { /* for the benefit of tty drivers */
ian@0 113 .c_iflag = ICRNL | IXON,
ian@0 114 .c_oflag = OPOST | ONLCR,
ian@0 115 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
ian@0 116 .c_lflag = ISIG | ICANON | ECHO | ECHOE | ECHOK |
ian@0 117 ECHOCTL | ECHOKE | IEXTEN,
ian@0 118 .c_cc = INIT_C_CC
ian@0 119 };
ian@0 120
ian@0 121 EXPORT_SYMBOL(tty_std_termios);
ian@0 122
ian@0 123 /* This list gets poked at by procfs and various bits of boot up code. This
ian@0 124 could do with some rationalisation such as pulling the tty proc function
ian@0 125 into this file */
ian@0 126
ian@0 127 LIST_HEAD(tty_drivers); /* linked list of tty drivers */
ian@0 128
ian@0 129 /* Semaphore to protect creating and releasing a tty. This is shared with
ian@0 130 vt.c for deeply disgusting hack reasons */
ian@0 131 DEFINE_MUTEX(tty_mutex);
ian@0 132
ian@26 133 int console_use_vt = 1;
ian@26 134
ian@0 135 #ifdef CONFIG_UNIX98_PTYS
ian@0 136 extern struct tty_driver *ptm_driver; /* Unix98 pty masters; for /dev/ptmx */
ian@0 137 extern int pty_limit; /* Config limit on Unix98 ptys */
ian@0 138 static DEFINE_IDR(allocated_ptys);
ian@0 139 static DECLARE_MUTEX(allocated_ptys_lock);
ian@0 140 static int ptmx_open(struct inode *, struct file *);
ian@0 141 #endif
ian@0 142
ian@0 143 extern void disable_early_printk(void);
ian@0 144
ian@0 145 static void initialize_tty_struct(struct tty_struct *tty);
ian@0 146
ian@0 147 static ssize_t tty_read(struct file *, char __user *, size_t, loff_t *);
ian@0 148 static ssize_t tty_write(struct file *, const char __user *, size_t, loff_t *);
ian@0 149 ssize_t redirected_tty_write(struct file *, const char __user *, size_t, loff_t *);
ian@0 150 static unsigned int tty_poll(struct file *, poll_table *);
ian@0 151 static int tty_open(struct inode *, struct file *);
ian@0 152 static int tty_release(struct inode *, struct file *);
ian@0 153 int tty_ioctl(struct inode * inode, struct file * file,
ian@0 154 unsigned int cmd, unsigned long arg);
ian@0 155 static int tty_fasync(int fd, struct file * filp, int on);
ian@0 156 static void release_mem(struct tty_struct *tty, int idx);
ian@0 157
ian@0 158 /**
ian@0 159 * alloc_tty_struct - allocate a tty object
ian@0 160 *
ian@0 161 * Return a new empty tty structure. The data fields have not
ian@0 162 * been initialized in any way but has been zeroed
ian@0 163 *
ian@0 164 * Locking: none
ian@0 165 * FIXME: use kzalloc
ian@0 166 */
ian@0 167
ian@0 168 static struct tty_struct *alloc_tty_struct(void)
ian@0 169 {
ian@0 170 struct tty_struct *tty;
ian@0 171
ian@0 172 tty = kmalloc(sizeof(struct tty_struct), GFP_KERNEL);
ian@0 173 if (tty)
ian@0 174 memset(tty, 0, sizeof(struct tty_struct));
ian@0 175 return tty;
ian@0 176 }
ian@0 177
ian@0 178 static void tty_buffer_free_all(struct tty_struct *);
ian@0 179
ian@0 180 /**
ian@0 181 * free_tty_struct - free a disused tty
ian@0 182 * @tty: tty struct to free
ian@0 183 *
ian@0 184 * Free the write buffers, tty queue and tty memory itself.
ian@0 185 *
ian@0 186 * Locking: none. Must be called after tty is definitely unused
ian@0 187 */
ian@0 188
ian@0 189 static inline void free_tty_struct(struct tty_struct *tty)
ian@0 190 {
ian@0 191 kfree(tty->write_buf);
ian@0 192 tty_buffer_free_all(tty);
ian@0 193 kfree(tty);
ian@0 194 }
ian@0 195
ian@0 196 #define TTY_NUMBER(tty) ((tty)->index + (tty)->driver->name_base)
ian@0 197
ian@0 198 /**
ian@0 199 * tty_name - return tty naming
ian@0 200 * @tty: tty structure
ian@0 201 * @buf: buffer for output
ian@0 202 *
ian@0 203 * Convert a tty structure into a name. The name reflects the kernel
ian@0 204 * naming policy and if udev is in use may not reflect user space
ian@0 205 *
ian@0 206 * Locking: none
ian@0 207 */
ian@0 208
ian@0 209 char *tty_name(struct tty_struct *tty, char *buf)
ian@0 210 {
ian@0 211 if (!tty) /* Hmm. NULL pointer. That's fun. */
ian@0 212 strcpy(buf, "NULL tty");
ian@0 213 else
ian@0 214 strcpy(buf, tty->name);
ian@0 215 return buf;
ian@0 216 }
ian@0 217
ian@0 218 EXPORT_SYMBOL(tty_name);
ian@0 219
ian@0 220 int tty_paranoia_check(struct tty_struct *tty, struct inode *inode,
ian@0 221 const char *routine)
ian@0 222 {
ian@0 223 #ifdef TTY_PARANOIA_CHECK
ian@0 224 if (!tty) {
ian@0 225 printk(KERN_WARNING
ian@0 226 "null TTY for (%d:%d) in %s\n",
ian@0 227 imajor(inode), iminor(inode), routine);
ian@0 228 return 1;
ian@0 229 }
ian@0 230 if (tty->magic != TTY_MAGIC) {
ian@0 231 printk(KERN_WARNING
ian@0 232 "bad magic number for tty struct (%d:%d) in %s\n",
ian@0 233 imajor(inode), iminor(inode), routine);
ian@0 234 return 1;
ian@0 235 }
ian@0 236 #endif
ian@0 237 return 0;
ian@0 238 }
ian@0 239
ian@0 240 static int check_tty_count(struct tty_struct *tty, const char *routine)
ian@0 241 {
ian@0 242 #ifdef CHECK_TTY_COUNT
ian@0 243 struct list_head *p;
ian@0 244 int count = 0;
ian@0 245
ian@0 246 file_list_lock();
ian@0 247 list_for_each(p, &tty->tty_files) {
ian@0 248 count++;
ian@0 249 }
ian@0 250 file_list_unlock();
ian@0 251 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
ian@0 252 tty->driver->subtype == PTY_TYPE_SLAVE &&
ian@0 253 tty->link && tty->link->count)
ian@0 254 count++;
ian@0 255 if (tty->count != count) {
ian@0 256 printk(KERN_WARNING "Warning: dev (%s) tty->count(%d) "
ian@0 257 "!= #fd's(%d) in %s\n",
ian@0 258 tty->name, tty->count, count, routine);
ian@0 259 return count;
ian@0 260 }
ian@0 261 #endif
ian@0 262 return 0;
ian@0 263 }
ian@0 264
ian@0 265 /*
ian@0 266 * Tty buffer allocation management
ian@0 267 */
ian@0 268
ian@0 269
ian@0 270 /**
ian@0 271 * tty_buffer_free_all - free buffers used by a tty
ian@0 272 * @tty: tty to free from
ian@0 273 *
ian@0 274 * Remove all the buffers pending on a tty whether queued with data
ian@0 275 * or in the free ring. Must be called when the tty is no longer in use
ian@0 276 *
ian@0 277 * Locking: none
ian@0 278 */
ian@0 279
ian@0 280
ian@0 281 /**
ian@0 282 * tty_buffer_free_all - free buffers used by a tty
ian@0 283 * @tty: tty to free from
ian@0 284 *
ian@0 285 * Remove all the buffers pending on a tty whether queued with data
ian@0 286 * or in the free ring. Must be called when the tty is no longer in use
ian@0 287 *
ian@0 288 * Locking: none
ian@0 289 */
ian@0 290
ian@0 291 static void tty_buffer_free_all(struct tty_struct *tty)
ian@0 292 {
ian@0 293 struct tty_buffer *thead;
ian@0 294 while((thead = tty->buf.head) != NULL) {
ian@0 295 tty->buf.head = thead->next;
ian@0 296 kfree(thead);
ian@0 297 }
ian@0 298 while((thead = tty->buf.free) != NULL) {
ian@0 299 tty->buf.free = thead->next;
ian@0 300 kfree(thead);
ian@0 301 }
ian@0 302 tty->buf.tail = NULL;
ian@0 303 tty->buf.memory_used = 0;
ian@0 304 }
ian@0 305
ian@0 306 /**
ian@0 307 * tty_buffer_init - prepare a tty buffer structure
ian@0 308 * @tty: tty to initialise
ian@0 309 *
ian@0 310 * Set up the initial state of the buffer management for a tty device.
ian@0 311 * Must be called before the other tty buffer functions are used.
ian@0 312 *
ian@0 313 * Locking: none
ian@0 314 */
ian@0 315
ian@0 316 static void tty_buffer_init(struct tty_struct *tty)
ian@0 317 {
ian@0 318 spin_lock_init(&tty->buf.lock);
ian@0 319 tty->buf.head = NULL;
ian@0 320 tty->buf.tail = NULL;
ian@0 321 tty->buf.free = NULL;
ian@0 322 tty->buf.memory_used = 0;
ian@0 323 }
ian@0 324
ian@0 325 /**
ian@0 326 * tty_buffer_alloc - allocate a tty buffer
ian@0 327 * @tty: tty device
ian@0 328 * @size: desired size (characters)
ian@0 329 *
ian@0 330 * Allocate a new tty buffer to hold the desired number of characters.
ian@0 331 * Return NULL if out of memory or the allocation would exceed the
ian@0 332 * per device queue
ian@0 333 *
ian@0 334 * Locking: Caller must hold tty->buf.lock
ian@0 335 */
ian@0 336
ian@0 337 static struct tty_buffer *tty_buffer_alloc(struct tty_struct *tty, size_t size)
ian@0 338 {
ian@0 339 struct tty_buffer *p;
ian@0 340
ian@0 341 if (tty->buf.memory_used + size > 65536)
ian@0 342 return NULL;
ian@0 343 p = kmalloc(sizeof(struct tty_buffer) + 2 * size, GFP_ATOMIC);
ian@0 344 if(p == NULL)
ian@0 345 return NULL;
ian@0 346 p->used = 0;
ian@0 347 p->size = size;
ian@0 348 p->next = NULL;
ian@0 349 p->commit = 0;
ian@0 350 p->read = 0;
ian@0 351 p->char_buf_ptr = (char *)(p->data);
ian@0 352 p->flag_buf_ptr = (unsigned char *)p->char_buf_ptr + size;
ian@0 353 tty->buf.memory_used += size;
ian@0 354 return p;
ian@0 355 }
ian@0 356
ian@0 357 /**
ian@0 358 * tty_buffer_free - free a tty buffer
ian@0 359 * @tty: tty owning the buffer
ian@0 360 * @b: the buffer to free
ian@0 361 *
ian@0 362 * Free a tty buffer, or add it to the free list according to our
ian@0 363 * internal strategy
ian@0 364 *
ian@0 365 * Locking: Caller must hold tty->buf.lock
ian@0 366 */
ian@0 367
ian@0 368 static void tty_buffer_free(struct tty_struct *tty, struct tty_buffer *b)
ian@0 369 {
ian@0 370 /* Dumb strategy for now - should keep some stats */
ian@0 371 tty->buf.memory_used -= b->size;
ian@0 372 WARN_ON(tty->buf.memory_used < 0);
ian@0 373
ian@0 374 if(b->size >= 512)
ian@0 375 kfree(b);
ian@0 376 else {
ian@0 377 b->next = tty->buf.free;
ian@0 378 tty->buf.free = b;
ian@0 379 }
ian@0 380 }
ian@0 381
ian@0 382 /**
ian@0 383 * tty_buffer_find - find a free tty buffer
ian@0 384 * @tty: tty owning the buffer
ian@0 385 * @size: characters wanted
ian@0 386 *
ian@0 387 * Locate an existing suitable tty buffer or if we are lacking one then
ian@0 388 * allocate a new one. We round our buffers off in 256 character chunks
ian@0 389 * to get better allocation behaviour.
ian@0 390 *
ian@0 391 * Locking: Caller must hold tty->buf.lock
ian@0 392 */
ian@0 393
ian@0 394 static struct tty_buffer *tty_buffer_find(struct tty_struct *tty, size_t size)
ian@0 395 {
ian@0 396 struct tty_buffer **tbh = &tty->buf.free;
ian@0 397 while((*tbh) != NULL) {
ian@0 398 struct tty_buffer *t = *tbh;
ian@0 399 if(t->size >= size) {
ian@0 400 *tbh = t->next;
ian@0 401 t->next = NULL;
ian@0 402 t->used = 0;
ian@0 403 t->commit = 0;
ian@0 404 t->read = 0;
ian@0 405 tty->buf.memory_used += t->size;
ian@0 406 return t;
ian@0 407 }
ian@0 408 tbh = &((*tbh)->next);
ian@0 409 }
ian@0 410 /* Round the buffer size out */
ian@0 411 size = (size + 0xFF) & ~ 0xFF;
ian@0 412 return tty_buffer_alloc(tty, size);
ian@0 413 /* Should possibly check if this fails for the largest buffer we
ian@0 414 have queued and recycle that ? */
ian@0 415 }
ian@0 416
ian@0 417 /**
ian@0 418 * tty_buffer_request_room - grow tty buffer if needed
ian@0 419 * @tty: tty structure
ian@0 420 * @size: size desired
ian@0 421 *
ian@0 422 * Make at least size bytes of linear space available for the tty
ian@0 423 * buffer. If we fail return the size we managed to find.
ian@0 424 *
ian@0 425 * Locking: Takes tty->buf.lock
ian@0 426 */
ian@0 427 int tty_buffer_request_room(struct tty_struct *tty, size_t size)
ian@0 428 {
ian@0 429 struct tty_buffer *b, *n;
ian@0 430 int left;
ian@0 431 unsigned long flags;
ian@0 432
ian@0 433 spin_lock_irqsave(&tty->buf.lock, flags);
ian@0 434
ian@0 435 /* OPTIMISATION: We could keep a per tty "zero" sized buffer to
ian@0 436 remove this conditional if its worth it. This would be invisible
ian@0 437 to the callers */
ian@0 438 if ((b = tty->buf.tail) != NULL)
ian@0 439 left = b->size - b->used;
ian@0 440 else
ian@0 441 left = 0;
ian@0 442
ian@0 443 if (left < size) {
ian@0 444 /* This is the slow path - looking for new buffers to use */
ian@0 445 if ((n = tty_buffer_find(tty, size)) != NULL) {
ian@0 446 if (b != NULL) {
ian@0 447 b->next = n;
ian@0 448 b->commit = b->used;
ian@0 449 } else
ian@0 450 tty->buf.head = n;
ian@0 451 tty->buf.tail = n;
ian@0 452 } else
ian@0 453 size = left;
ian@0 454 }
ian@0 455
ian@0 456 spin_unlock_irqrestore(&tty->buf.lock, flags);
ian@0 457 return size;
ian@0 458 }
ian@0 459 EXPORT_SYMBOL_GPL(tty_buffer_request_room);
ian@0 460
ian@0 461 /**
ian@0 462 * tty_insert_flip_string - Add characters to the tty buffer
ian@0 463 * @tty: tty structure
ian@0 464 * @chars: characters
ian@0 465 * @size: size
ian@0 466 *
ian@0 467 * Queue a series of bytes to the tty buffering. All the characters
ian@0 468 * passed are marked as without error. Returns the number added.
ian@0 469 *
ian@0 470 * Locking: Called functions may take tty->buf.lock
ian@0 471 */
ian@0 472
ian@0 473 int tty_insert_flip_string(struct tty_struct *tty, const unsigned char *chars,
ian@0 474 size_t size)
ian@0 475 {
ian@0 476 int copied = 0;
ian@0 477 do {
ian@0 478 int space = tty_buffer_request_room(tty, size - copied);
ian@0 479 struct tty_buffer *tb = tty->buf.tail;
ian@0 480 /* If there is no space then tb may be NULL */
ian@0 481 if(unlikely(space == 0))
ian@0 482 break;
ian@0 483 memcpy(tb->char_buf_ptr + tb->used, chars, space);
ian@0 484 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
ian@0 485 tb->used += space;
ian@0 486 copied += space;
ian@0 487 chars += space;
ian@0 488 }
ian@0 489 /* There is a small chance that we need to split the data over
ian@0 490 several buffers. If this is the case we must loop */
ian@0 491 while (unlikely(size > copied));
ian@0 492 return copied;
ian@0 493 }
ian@0 494 EXPORT_SYMBOL(tty_insert_flip_string);
ian@0 495
ian@0 496 /**
ian@0 497 * tty_insert_flip_string_flags - Add characters to the tty buffer
ian@0 498 * @tty: tty structure
ian@0 499 * @chars: characters
ian@0 500 * @flags: flag bytes
ian@0 501 * @size: size
ian@0 502 *
ian@0 503 * Queue a series of bytes to the tty buffering. For each character
ian@0 504 * the flags array indicates the status of the character. Returns the
ian@0 505 * number added.
ian@0 506 *
ian@0 507 * Locking: Called functions may take tty->buf.lock
ian@0 508 */
ian@0 509
ian@0 510 int tty_insert_flip_string_flags(struct tty_struct *tty,
ian@0 511 const unsigned char *chars, const char *flags, size_t size)
ian@0 512 {
ian@0 513 int copied = 0;
ian@0 514 do {
ian@0 515 int space = tty_buffer_request_room(tty, size - copied);
ian@0 516 struct tty_buffer *tb = tty->buf.tail;
ian@0 517 /* If there is no space then tb may be NULL */
ian@0 518 if(unlikely(space == 0))
ian@0 519 break;
ian@0 520 memcpy(tb->char_buf_ptr + tb->used, chars, space);
ian@0 521 memcpy(tb->flag_buf_ptr + tb->used, flags, space);
ian@0 522 tb->used += space;
ian@0 523 copied += space;
ian@0 524 chars += space;
ian@0 525 flags += space;
ian@0 526 }
ian@0 527 /* There is a small chance that we need to split the data over
ian@0 528 several buffers. If this is the case we must loop */
ian@0 529 while (unlikely(size > copied));
ian@0 530 return copied;
ian@0 531 }
ian@0 532 EXPORT_SYMBOL(tty_insert_flip_string_flags);
ian@0 533
ian@0 534 /**
ian@0 535 * tty_schedule_flip - push characters to ldisc
ian@0 536 * @tty: tty to push from
ian@0 537 *
ian@0 538 * Takes any pending buffers and transfers their ownership to the
ian@0 539 * ldisc side of the queue. It then schedules those characters for
ian@0 540 * processing by the line discipline.
ian@0 541 *
ian@0 542 * Locking: Takes tty->buf.lock
ian@0 543 */
ian@0 544
ian@0 545 void tty_schedule_flip(struct tty_struct *tty)
ian@0 546 {
ian@0 547 unsigned long flags;
ian@0 548 spin_lock_irqsave(&tty->buf.lock, flags);
ian@0 549 if (tty->buf.tail != NULL)
ian@0 550 tty->buf.tail->commit = tty->buf.tail->used;
ian@0 551 spin_unlock_irqrestore(&tty->buf.lock, flags);
ian@0 552 schedule_delayed_work(&tty->buf.work, 1);
ian@0 553 }
ian@0 554 EXPORT_SYMBOL(tty_schedule_flip);
ian@0 555
ian@0 556 /**
ian@0 557 * tty_prepare_flip_string - make room for characters
ian@0 558 * @tty: tty
ian@0 559 * @chars: return pointer for character write area
ian@0 560 * @size: desired size
ian@0 561 *
ian@0 562 * Prepare a block of space in the buffer for data. Returns the length
ian@0 563 * available and buffer pointer to the space which is now allocated and
ian@0 564 * accounted for as ready for normal characters. This is used for drivers
ian@0 565 * that need their own block copy routines into the buffer. There is no
ian@0 566 * guarantee the buffer is a DMA target!
ian@0 567 *
ian@0 568 * Locking: May call functions taking tty->buf.lock
ian@0 569 */
ian@0 570
ian@0 571 int tty_prepare_flip_string(struct tty_struct *tty, unsigned char **chars, size_t size)
ian@0 572 {
ian@0 573 int space = tty_buffer_request_room(tty, size);
ian@0 574 if (likely(space)) {
ian@0 575 struct tty_buffer *tb = tty->buf.tail;
ian@0 576 *chars = tb->char_buf_ptr + tb->used;
ian@0 577 memset(tb->flag_buf_ptr + tb->used, TTY_NORMAL, space);
ian@0 578 tb->used += space;
ian@0 579 }
ian@0 580 return space;
ian@0 581 }
ian@0 582
ian@0 583 EXPORT_SYMBOL_GPL(tty_prepare_flip_string);
ian@0 584
ian@0 585 /**
ian@0 586 * tty_prepare_flip_string_flags - make room for characters
ian@0 587 * @tty: tty
ian@0 588 * @chars: return pointer for character write area
ian@0 589 * @flags: return pointer for status flag write area
ian@0 590 * @size: desired size
ian@0 591 *
ian@0 592 * Prepare a block of space in the buffer for data. Returns the length
ian@0 593 * available and buffer pointer to the space which is now allocated and
ian@0 594 * accounted for as ready for characters. This is used for drivers
ian@0 595 * that need their own block copy routines into the buffer. There is no
ian@0 596 * guarantee the buffer is a DMA target!
ian@0 597 *
ian@0 598 * Locking: May call functions taking tty->buf.lock
ian@0 599 */
ian@0 600
ian@0 601 int tty_prepare_flip_string_flags(struct tty_struct *tty, unsigned char **chars, char **flags, size_t size)
ian@0 602 {
ian@0 603 int space = tty_buffer_request_room(tty, size);
ian@0 604 if (likely(space)) {
ian@0 605 struct tty_buffer *tb = tty->buf.tail;
ian@0 606 *chars = tb->char_buf_ptr + tb->used;
ian@0 607 *flags = tb->flag_buf_ptr + tb->used;
ian@0 608 tb->used += space;
ian@0 609 }
ian@0 610 return space;
ian@0 611 }
ian@0 612
ian@0 613 EXPORT_SYMBOL_GPL(tty_prepare_flip_string_flags);
ian@0 614
ian@0 615
ian@0 616
ian@0 617 /**
ian@0 618 * tty_set_termios_ldisc - set ldisc field
ian@0 619 * @tty: tty structure
ian@0 620 * @num: line discipline number
ian@0 621 *
ian@0 622 * This is probably overkill for real world processors but
ian@0 623 * they are not on hot paths so a little discipline won't do
ian@0 624 * any harm.
ian@0 625 *
ian@0 626 * Locking: takes termios_sem
ian@0 627 */
ian@0 628
ian@0 629 static void tty_set_termios_ldisc(struct tty_struct *tty, int num)
ian@0 630 {
ian@0 631 down(&tty->termios_sem);
ian@0 632 tty->termios->c_line = num;
ian@0 633 up(&tty->termios_sem);
ian@0 634 }
ian@0 635
ian@0 636 /*
ian@0 637 * This guards the refcounted line discipline lists. The lock
ian@0 638 * must be taken with irqs off because there are hangup path
ian@0 639 * callers who will do ldisc lookups and cannot sleep.
ian@0 640 */
ian@0 641
ian@0 642 static DEFINE_SPINLOCK(tty_ldisc_lock);
ian@0 643 static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait);
ian@0 644 static struct tty_ldisc tty_ldiscs[NR_LDISCS]; /* line disc dispatch table */
ian@0 645
ian@0 646 /**
ian@0 647 * tty_register_ldisc - install a line discipline
ian@0 648 * @disc: ldisc number
ian@0 649 * @new_ldisc: pointer to the ldisc object
ian@0 650 *
ian@0 651 * Installs a new line discipline into the kernel. The discipline
ian@0 652 * is set up as unreferenced and then made available to the kernel
ian@0 653 * from this point onwards.
ian@0 654 *
ian@0 655 * Locking:
ian@0 656 * takes tty_ldisc_lock to guard against ldisc races
ian@0 657 */
ian@0 658
ian@0 659 int tty_register_ldisc(int disc, struct tty_ldisc *new_ldisc)
ian@0 660 {
ian@0 661 unsigned long flags;
ian@0 662 int ret = 0;
ian@0 663
ian@0 664 if (disc < N_TTY || disc >= NR_LDISCS)
ian@0 665 return -EINVAL;
ian@0 666
ian@0 667 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 668 tty_ldiscs[disc] = *new_ldisc;
ian@0 669 tty_ldiscs[disc].num = disc;
ian@0 670 tty_ldiscs[disc].flags |= LDISC_FLAG_DEFINED;
ian@0 671 tty_ldiscs[disc].refcount = 0;
ian@0 672 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 673
ian@0 674 return ret;
ian@0 675 }
ian@0 676 EXPORT_SYMBOL(tty_register_ldisc);
ian@0 677
ian@0 678 /**
ian@0 679 * tty_unregister_ldisc - unload a line discipline
ian@0 680 * @disc: ldisc number
ian@0 681 * @new_ldisc: pointer to the ldisc object
ian@0 682 *
ian@0 683 * Remove a line discipline from the kernel providing it is not
ian@0 684 * currently in use.
ian@0 685 *
ian@0 686 * Locking:
ian@0 687 * takes tty_ldisc_lock to guard against ldisc races
ian@0 688 */
ian@0 689
ian@0 690 int tty_unregister_ldisc(int disc)
ian@0 691 {
ian@0 692 unsigned long flags;
ian@0 693 int ret = 0;
ian@0 694
ian@0 695 if (disc < N_TTY || disc >= NR_LDISCS)
ian@0 696 return -EINVAL;
ian@0 697
ian@0 698 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 699 if (tty_ldiscs[disc].refcount)
ian@0 700 ret = -EBUSY;
ian@0 701 else
ian@0 702 tty_ldiscs[disc].flags &= ~LDISC_FLAG_DEFINED;
ian@0 703 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 704
ian@0 705 return ret;
ian@0 706 }
ian@0 707 EXPORT_SYMBOL(tty_unregister_ldisc);
ian@0 708
ian@0 709 /**
ian@0 710 * tty_ldisc_get - take a reference to an ldisc
ian@0 711 * @disc: ldisc number
ian@0 712 *
ian@0 713 * Takes a reference to a line discipline. Deals with refcounts and
ian@0 714 * module locking counts. Returns NULL if the discipline is not available.
ian@0 715 * Returns a pointer to the discipline and bumps the ref count if it is
ian@0 716 * available
ian@0 717 *
ian@0 718 * Locking:
ian@0 719 * takes tty_ldisc_lock to guard against ldisc races
ian@0 720 */
ian@0 721
ian@0 722 struct tty_ldisc *tty_ldisc_get(int disc)
ian@0 723 {
ian@0 724 unsigned long flags;
ian@0 725 struct tty_ldisc *ld;
ian@0 726
ian@0 727 if (disc < N_TTY || disc >= NR_LDISCS)
ian@0 728 return NULL;
ian@0 729
ian@0 730 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 731
ian@0 732 ld = &tty_ldiscs[disc];
ian@0 733 /* Check the entry is defined */
ian@0 734 if(ld->flags & LDISC_FLAG_DEFINED)
ian@0 735 {
ian@0 736 /* If the module is being unloaded we can't use it */
ian@0 737 if (!try_module_get(ld->owner))
ian@0 738 ld = NULL;
ian@0 739 else /* lock it */
ian@0 740 ld->refcount++;
ian@0 741 }
ian@0 742 else
ian@0 743 ld = NULL;
ian@0 744 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 745 return ld;
ian@0 746 }
ian@0 747
ian@0 748 EXPORT_SYMBOL_GPL(tty_ldisc_get);
ian@0 749
ian@0 750 /**
ian@0 751 * tty_ldisc_put - drop ldisc reference
ian@0 752 * @disc: ldisc number
ian@0 753 *
ian@0 754 * Drop a reference to a line discipline. Manage refcounts and
ian@0 755 * module usage counts
ian@0 756 *
ian@0 757 * Locking:
ian@0 758 * takes tty_ldisc_lock to guard against ldisc races
ian@0 759 */
ian@0 760
ian@0 761 void tty_ldisc_put(int disc)
ian@0 762 {
ian@0 763 struct tty_ldisc *ld;
ian@0 764 unsigned long flags;
ian@0 765
ian@0 766 BUG_ON(disc < N_TTY || disc >= NR_LDISCS);
ian@0 767
ian@0 768 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 769 ld = &tty_ldiscs[disc];
ian@0 770 BUG_ON(ld->refcount == 0);
ian@0 771 ld->refcount--;
ian@0 772 module_put(ld->owner);
ian@0 773 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 774 }
ian@0 775
ian@0 776 EXPORT_SYMBOL_GPL(tty_ldisc_put);
ian@0 777
ian@0 778 /**
ian@0 779 * tty_ldisc_assign - set ldisc on a tty
ian@0 780 * @tty: tty to assign
ian@0 781 * @ld: line discipline
ian@0 782 *
ian@0 783 * Install an instance of a line discipline into a tty structure. The
ian@0 784 * ldisc must have a reference count above zero to ensure it remains/
ian@0 785 * The tty instance refcount starts at zero.
ian@0 786 *
ian@0 787 * Locking:
ian@0 788 * Caller must hold references
ian@0 789 */
ian@0 790
ian@0 791 static void tty_ldisc_assign(struct tty_struct *tty, struct tty_ldisc *ld)
ian@0 792 {
ian@0 793 tty->ldisc = *ld;
ian@0 794 tty->ldisc.refcount = 0;
ian@0 795 }
ian@0 796
ian@0 797 /**
ian@0 798 * tty_ldisc_try - internal helper
ian@0 799 * @tty: the tty
ian@0 800 *
ian@0 801 * Make a single attempt to grab and bump the refcount on
ian@0 802 * the tty ldisc. Return 0 on failure or 1 on success. This is
ian@0 803 * used to implement both the waiting and non waiting versions
ian@0 804 * of tty_ldisc_ref
ian@0 805 *
ian@0 806 * Locking: takes tty_ldisc_lock
ian@0 807 */
ian@0 808
ian@0 809 static int tty_ldisc_try(struct tty_struct *tty)
ian@0 810 {
ian@0 811 unsigned long flags;
ian@0 812 struct tty_ldisc *ld;
ian@0 813 int ret = 0;
ian@0 814
ian@0 815 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 816 ld = &tty->ldisc;
ian@0 817 if(test_bit(TTY_LDISC, &tty->flags))
ian@0 818 {
ian@0 819 ld->refcount++;
ian@0 820 ret = 1;
ian@0 821 }
ian@0 822 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 823 return ret;
ian@0 824 }
ian@0 825
ian@0 826 /**
ian@0 827 * tty_ldisc_ref_wait - wait for the tty ldisc
ian@0 828 * @tty: tty device
ian@0 829 *
ian@0 830 * Dereference the line discipline for the terminal and take a
ian@0 831 * reference to it. If the line discipline is in flux then
ian@0 832 * wait patiently until it changes.
ian@0 833 *
ian@0 834 * Note: Must not be called from an IRQ/timer context. The caller
ian@0 835 * must also be careful not to hold other locks that will deadlock
ian@0 836 * against a discipline change, such as an existing ldisc reference
ian@0 837 * (which we check for)
ian@0 838 *
ian@0 839 * Locking: call functions take tty_ldisc_lock
ian@0 840 */
ian@0 841
ian@0 842 struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
ian@0 843 {
ian@0 844 /* wait_event is a macro */
ian@0 845 wait_event(tty_ldisc_wait, tty_ldisc_try(tty));
ian@0 846 if(tty->ldisc.refcount == 0)
ian@0 847 printk(KERN_ERR "tty_ldisc_ref_wait\n");
ian@0 848 return &tty->ldisc;
ian@0 849 }
ian@0 850
ian@0 851 EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
ian@0 852
ian@0 853 /**
ian@0 854 * tty_ldisc_ref - get the tty ldisc
ian@0 855 * @tty: tty device
ian@0 856 *
ian@0 857 * Dereference the line discipline for the terminal and take a
ian@0 858 * reference to it. If the line discipline is in flux then
ian@0 859 * return NULL. Can be called from IRQ and timer functions.
ian@0 860 *
ian@0 861 * Locking: called functions take tty_ldisc_lock
ian@0 862 */
ian@0 863
ian@0 864 struct tty_ldisc *tty_ldisc_ref(struct tty_struct *tty)
ian@0 865 {
ian@0 866 if(tty_ldisc_try(tty))
ian@0 867 return &tty->ldisc;
ian@0 868 return NULL;
ian@0 869 }
ian@0 870
ian@0 871 EXPORT_SYMBOL_GPL(tty_ldisc_ref);
ian@0 872
ian@0 873 /**
ian@0 874 * tty_ldisc_deref - free a tty ldisc reference
ian@0 875 * @ld: reference to free up
ian@0 876 *
ian@0 877 * Undoes the effect of tty_ldisc_ref or tty_ldisc_ref_wait. May
ian@0 878 * be called in IRQ context.
ian@0 879 *
ian@0 880 * Locking: takes tty_ldisc_lock
ian@0 881 */
ian@0 882
ian@0 883 void tty_ldisc_deref(struct tty_ldisc *ld)
ian@0 884 {
ian@0 885 unsigned long flags;
ian@0 886
ian@0 887 BUG_ON(ld == NULL);
ian@0 888
ian@0 889 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 890 if(ld->refcount == 0)
ian@0 891 printk(KERN_ERR "tty_ldisc_deref: no references.\n");
ian@0 892 else
ian@0 893 ld->refcount--;
ian@0 894 if(ld->refcount == 0)
ian@0 895 wake_up(&tty_ldisc_wait);
ian@0 896 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 897 }
ian@0 898
ian@0 899 EXPORT_SYMBOL_GPL(tty_ldisc_deref);
ian@0 900
ian@0 901 /**
ian@0 902 * tty_ldisc_enable - allow ldisc use
ian@0 903 * @tty: terminal to activate ldisc on
ian@0 904 *
ian@0 905 * Set the TTY_LDISC flag when the line discipline can be called
ian@0 906 * again. Do neccessary wakeups for existing sleepers.
ian@0 907 *
ian@0 908 * Note: nobody should set this bit except via this function. Clearing
ian@0 909 * directly is allowed.
ian@0 910 */
ian@0 911
ian@0 912 static void tty_ldisc_enable(struct tty_struct *tty)
ian@0 913 {
ian@0 914 set_bit(TTY_LDISC, &tty->flags);
ian@0 915 wake_up(&tty_ldisc_wait);
ian@0 916 }
ian@0 917
ian@0 918 /**
ian@0 919 * tty_set_ldisc - set line discipline
ian@0 920 * @tty: the terminal to set
ian@0 921 * @ldisc: the line discipline
ian@0 922 *
ian@0 923 * Set the discipline of a tty line. Must be called from a process
ian@0 924 * context.
ian@0 925 *
ian@0 926 * Locking: takes tty_ldisc_lock.
ian@0 927 * called functions take termios_sem
ian@0 928 */
ian@0 929
ian@0 930 static int tty_set_ldisc(struct tty_struct *tty, int ldisc)
ian@0 931 {
ian@0 932 int retval = 0;
ian@0 933 struct tty_ldisc o_ldisc;
ian@0 934 char buf[64];
ian@0 935 int work;
ian@0 936 unsigned long flags;
ian@0 937 struct tty_ldisc *ld;
ian@0 938 struct tty_struct *o_tty;
ian@0 939
ian@0 940 if ((ldisc < N_TTY) || (ldisc >= NR_LDISCS))
ian@0 941 return -EINVAL;
ian@0 942
ian@0 943 restart:
ian@0 944
ian@0 945 ld = tty_ldisc_get(ldisc);
ian@0 946 /* Eduardo Blanco <ejbs@cs.cs.com.uy> */
ian@0 947 /* Cyrus Durgin <cider@speakeasy.org> */
ian@0 948 if (ld == NULL) {
ian@0 949 request_module("tty-ldisc-%d", ldisc);
ian@0 950 ld = tty_ldisc_get(ldisc);
ian@0 951 }
ian@0 952 if (ld == NULL)
ian@0 953 return -EINVAL;
ian@0 954
ian@0 955 /*
ian@0 956 * No more input please, we are switching. The new ldisc
ian@0 957 * will update this value in the ldisc open function
ian@0 958 */
ian@0 959
ian@0 960 tty->receive_room = 0;
ian@0 961
ian@0 962 /*
ian@0 963 * Problem: What do we do if this blocks ?
ian@0 964 */
ian@0 965
ian@0 966 tty_wait_until_sent(tty, 0);
ian@0 967
ian@0 968 if (tty->ldisc.num == ldisc) {
ian@0 969 tty_ldisc_put(ldisc);
ian@0 970 return 0;
ian@0 971 }
ian@0 972
ian@0 973 o_ldisc = tty->ldisc;
ian@0 974 o_tty = tty->link;
ian@0 975
ian@0 976 /*
ian@0 977 * Make sure we don't change while someone holds a
ian@0 978 * reference to the line discipline. The TTY_LDISC bit
ian@0 979 * prevents anyone taking a reference once it is clear.
ian@0 980 * We need the lock to avoid racing reference takers.
ian@0 981 */
ian@0 982
ian@0 983 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 984 if (tty->ldisc.refcount || (o_tty && o_tty->ldisc.refcount)) {
ian@0 985 if(tty->ldisc.refcount) {
ian@0 986 /* Free the new ldisc we grabbed. Must drop the lock
ian@0 987 first. */
ian@0 988 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 989 tty_ldisc_put(ldisc);
ian@0 990 /*
ian@0 991 * There are several reasons we may be busy, including
ian@0 992 * random momentary I/O traffic. We must therefore
ian@0 993 * retry. We could distinguish between blocking ops
ian@0 994 * and retries if we made tty_ldisc_wait() smarter. That
ian@0 995 * is up for discussion.
ian@0 996 */
ian@0 997 if (wait_event_interruptible(tty_ldisc_wait, tty->ldisc.refcount == 0) < 0)
ian@0 998 return -ERESTARTSYS;
ian@0 999 goto restart;
ian@0 1000 }
ian@0 1001 if(o_tty && o_tty->ldisc.refcount) {
ian@0 1002 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 1003 tty_ldisc_put(ldisc);
ian@0 1004 if (wait_event_interruptible(tty_ldisc_wait, o_tty->ldisc.refcount == 0) < 0)
ian@0 1005 return -ERESTARTSYS;
ian@0 1006 goto restart;
ian@0 1007 }
ian@0 1008 }
ian@0 1009
ian@0 1010 /* if the TTY_LDISC bit is set, then we are racing against another ldisc change */
ian@0 1011
ian@0 1012 if (!test_bit(TTY_LDISC, &tty->flags)) {
ian@0 1013 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 1014 tty_ldisc_put(ldisc);
ian@0 1015 ld = tty_ldisc_ref_wait(tty);
ian@0 1016 tty_ldisc_deref(ld);
ian@0 1017 goto restart;
ian@0 1018 }
ian@0 1019
ian@0 1020 clear_bit(TTY_LDISC, &tty->flags);
ian@0 1021 if (o_tty)
ian@0 1022 clear_bit(TTY_LDISC, &o_tty->flags);
ian@0 1023 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 1024
ian@0 1025 /*
ian@0 1026 * From this point on we know nobody has an ldisc
ian@0 1027 * usage reference, nor can they obtain one until
ian@0 1028 * we say so later on.
ian@0 1029 */
ian@0 1030
ian@0 1031 work = cancel_delayed_work(&tty->buf.work);
ian@0 1032 /*
ian@0 1033 * Wait for ->hangup_work and ->buf.work handlers to terminate
ian@0 1034 */
ian@0 1035
ian@0 1036 flush_scheduled_work();
ian@0 1037 /* Shutdown the current discipline. */
ian@0 1038 if (tty->ldisc.close)
ian@0 1039 (tty->ldisc.close)(tty);
ian@0 1040
ian@0 1041 /* Now set up the new line discipline. */
ian@0 1042 tty_ldisc_assign(tty, ld);
ian@0 1043 tty_set_termios_ldisc(tty, ldisc);
ian@0 1044 if (tty->ldisc.open)
ian@0 1045 retval = (tty->ldisc.open)(tty);
ian@0 1046 if (retval < 0) {
ian@0 1047 tty_ldisc_put(ldisc);
ian@0 1048 /* There is an outstanding reference here so this is safe */
ian@0 1049 tty_ldisc_assign(tty, tty_ldisc_get(o_ldisc.num));
ian@0 1050 tty_set_termios_ldisc(tty, tty->ldisc.num);
ian@0 1051 if (tty->ldisc.open && (tty->ldisc.open(tty) < 0)) {
ian@0 1052 tty_ldisc_put(o_ldisc.num);
ian@0 1053 /* This driver is always present */
ian@0 1054 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
ian@0 1055 tty_set_termios_ldisc(tty, N_TTY);
ian@0 1056 if (tty->ldisc.open) {
ian@0 1057 int r = tty->ldisc.open(tty);
ian@0 1058
ian@0 1059 if (r < 0)
ian@0 1060 panic("Couldn't open N_TTY ldisc for "
ian@0 1061 "%s --- error %d.",
ian@0 1062 tty_name(tty, buf), r);
ian@0 1063 }
ian@0 1064 }
ian@0 1065 }
ian@0 1066 /* At this point we hold a reference to the new ldisc and a
ian@0 1067 a reference to the old ldisc. If we ended up flipping back
ian@0 1068 to the existing ldisc we have two references to it */
ian@0 1069
ian@0 1070 if (tty->ldisc.num != o_ldisc.num && tty->driver->set_ldisc)
ian@0 1071 tty->driver->set_ldisc(tty);
ian@0 1072
ian@0 1073 tty_ldisc_put(o_ldisc.num);
ian@0 1074
ian@0 1075 /*
ian@0 1076 * Allow ldisc referencing to occur as soon as the driver
ian@0 1077 * ldisc callback completes.
ian@0 1078 */
ian@0 1079
ian@0 1080 tty_ldisc_enable(tty);
ian@0 1081 if (o_tty)
ian@0 1082 tty_ldisc_enable(o_tty);
ian@0 1083
ian@0 1084 /* Restart it in case no characters kick it off. Safe if
ian@0 1085 already running */
ian@0 1086 if (work)
ian@0 1087 schedule_delayed_work(&tty->buf.work, 1);
ian@0 1088 return retval;
ian@0 1089 }
ian@0 1090
ian@0 1091 /**
ian@0 1092 * get_tty_driver - find device of a tty
ian@0 1093 * @dev_t: device identifier
ian@0 1094 * @index: returns the index of the tty
ian@0 1095 *
ian@0 1096 * This routine returns a tty driver structure, given a device number
ian@0 1097 * and also passes back the index number.
ian@0 1098 *
ian@0 1099 * Locking: caller must hold tty_mutex
ian@0 1100 */
ian@0 1101
ian@0 1102 static struct tty_driver *get_tty_driver(dev_t device, int *index)
ian@0 1103 {
ian@0 1104 struct tty_driver *p;
ian@0 1105
ian@0 1106 list_for_each_entry(p, &tty_drivers, tty_drivers) {
ian@0 1107 dev_t base = MKDEV(p->major, p->minor_start);
ian@0 1108 if (device < base || device >= base + p->num)
ian@0 1109 continue;
ian@0 1110 *index = device - base;
ian@0 1111 return p;
ian@0 1112 }
ian@0 1113 return NULL;
ian@0 1114 }
ian@0 1115
ian@0 1116 /**
ian@0 1117 * tty_check_change - check for POSIX terminal changes
ian@0 1118 * @tty: tty to check
ian@0 1119 *
ian@0 1120 * If we try to write to, or set the state of, a terminal and we're
ian@0 1121 * not in the foreground, send a SIGTTOU. If the signal is blocked or
ian@0 1122 * ignored, go ahead and perform the operation. (POSIX 7.2)
ian@0 1123 *
ian@0 1124 * Locking: none
ian@0 1125 */
ian@0 1126
ian@0 1127 int tty_check_change(struct tty_struct * tty)
ian@0 1128 {
ian@0 1129 if (current->signal->tty != tty)
ian@0 1130 return 0;
ian@0 1131 if (tty->pgrp <= 0) {
ian@0 1132 printk(KERN_WARNING "tty_check_change: tty->pgrp <= 0!\n");
ian@0 1133 return 0;
ian@0 1134 }
ian@0 1135 if (process_group(current) == tty->pgrp)
ian@0 1136 return 0;
ian@0 1137 if (is_ignored(SIGTTOU))
ian@0 1138 return 0;
ian@0 1139 if (is_orphaned_pgrp(process_group(current)))
ian@0 1140 return -EIO;
ian@0 1141 (void) kill_pg(process_group(current), SIGTTOU, 1);
ian@0 1142 return -ERESTARTSYS;
ian@0 1143 }
ian@0 1144
ian@0 1145 EXPORT_SYMBOL(tty_check_change);
ian@0 1146
ian@0 1147 static ssize_t hung_up_tty_read(struct file * file, char __user * buf,
ian@0 1148 size_t count, loff_t *ppos)
ian@0 1149 {
ian@0 1150 return 0;
ian@0 1151 }
ian@0 1152
ian@0 1153 static ssize_t hung_up_tty_write(struct file * file, const char __user * buf,
ian@0 1154 size_t count, loff_t *ppos)
ian@0 1155 {
ian@0 1156 return -EIO;
ian@0 1157 }
ian@0 1158
ian@0 1159 /* No kernel lock held - none needed ;) */
ian@0 1160 static unsigned int hung_up_tty_poll(struct file * filp, poll_table * wait)
ian@0 1161 {
ian@0 1162 return POLLIN | POLLOUT | POLLERR | POLLHUP | POLLRDNORM | POLLWRNORM;
ian@0 1163 }
ian@0 1164
ian@0 1165 static int hung_up_tty_ioctl(struct inode * inode, struct file * file,
ian@0 1166 unsigned int cmd, unsigned long arg)
ian@0 1167 {
ian@0 1168 return cmd == TIOCSPGRP ? -ENOTTY : -EIO;
ian@0 1169 }
ian@0 1170
ian@0 1171 static const struct file_operations tty_fops = {
ian@0 1172 .llseek = no_llseek,
ian@0 1173 .read = tty_read,
ian@0 1174 .write = tty_write,
ian@0 1175 .poll = tty_poll,
ian@0 1176 .ioctl = tty_ioctl,
ian@0 1177 .open = tty_open,
ian@0 1178 .release = tty_release,
ian@0 1179 .fasync = tty_fasync,
ian@0 1180 };
ian@0 1181
ian@0 1182 #ifdef CONFIG_UNIX98_PTYS
ian@0 1183 static const struct file_operations ptmx_fops = {
ian@0 1184 .llseek = no_llseek,
ian@0 1185 .read = tty_read,
ian@0 1186 .write = tty_write,
ian@0 1187 .poll = tty_poll,
ian@0 1188 .ioctl = tty_ioctl,
ian@0 1189 .open = ptmx_open,
ian@0 1190 .release = tty_release,
ian@0 1191 .fasync = tty_fasync,
ian@0 1192 };
ian@0 1193 #endif
ian@0 1194
ian@0 1195 static const struct file_operations console_fops = {
ian@0 1196 .llseek = no_llseek,
ian@0 1197 .read = tty_read,
ian@0 1198 .write = redirected_tty_write,
ian@0 1199 .poll = tty_poll,
ian@0 1200 .ioctl = tty_ioctl,
ian@0 1201 .open = tty_open,
ian@0 1202 .release = tty_release,
ian@0 1203 .fasync = tty_fasync,
ian@0 1204 };
ian@0 1205
ian@0 1206 static const struct file_operations hung_up_tty_fops = {
ian@0 1207 .llseek = no_llseek,
ian@0 1208 .read = hung_up_tty_read,
ian@0 1209 .write = hung_up_tty_write,
ian@0 1210 .poll = hung_up_tty_poll,
ian@0 1211 .ioctl = hung_up_tty_ioctl,
ian@0 1212 .release = tty_release,
ian@0 1213 };
ian@0 1214
ian@0 1215 static DEFINE_SPINLOCK(redirect_lock);
ian@0 1216 static struct file *redirect;
ian@0 1217
ian@0 1218 /**
ian@0 1219 * tty_wakeup - request more data
ian@0 1220 * @tty: terminal
ian@0 1221 *
ian@0 1222 * Internal and external helper for wakeups of tty. This function
ian@0 1223 * informs the line discipline if present that the driver is ready
ian@0 1224 * to receive more output data.
ian@0 1225 */
ian@0 1226
ian@0 1227 void tty_wakeup(struct tty_struct *tty)
ian@0 1228 {
ian@0 1229 struct tty_ldisc *ld;
ian@0 1230
ian@0 1231 if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) {
ian@0 1232 ld = tty_ldisc_ref(tty);
ian@0 1233 if(ld) {
ian@0 1234 if(ld->write_wakeup)
ian@0 1235 ld->write_wakeup(tty);
ian@0 1236 tty_ldisc_deref(ld);
ian@0 1237 }
ian@0 1238 }
ian@0 1239 wake_up_interruptible(&tty->write_wait);
ian@0 1240 }
ian@0 1241
ian@0 1242 EXPORT_SYMBOL_GPL(tty_wakeup);
ian@0 1243
ian@0 1244 /**
ian@0 1245 * tty_ldisc_flush - flush line discipline queue
ian@0 1246 * @tty: tty
ian@0 1247 *
ian@0 1248 * Flush the line discipline queue (if any) for this tty. If there
ian@0 1249 * is no line discipline active this is a no-op.
ian@0 1250 */
ian@0 1251
ian@0 1252 void tty_ldisc_flush(struct tty_struct *tty)
ian@0 1253 {
ian@0 1254 struct tty_ldisc *ld = tty_ldisc_ref(tty);
ian@0 1255 if(ld) {
ian@0 1256 if(ld->flush_buffer)
ian@0 1257 ld->flush_buffer(tty);
ian@0 1258 tty_ldisc_deref(ld);
ian@0 1259 }
ian@0 1260 }
ian@0 1261
ian@0 1262 EXPORT_SYMBOL_GPL(tty_ldisc_flush);
ian@0 1263
ian@0 1264 /**
ian@0 1265 * do_tty_hangup - actual handler for hangup events
ian@0 1266 * @data: tty device
ian@0 1267 *
ian@0 1268 * This can be called by the "eventd" kernel thread. That is process
ian@0 1269 * synchronous but doesn't hold any locks, so we need to make sure we
ian@0 1270 * have the appropriate locks for what we're doing.
ian@0 1271 *
ian@0 1272 * The hangup event clears any pending redirections onto the hung up
ian@0 1273 * device. It ensures future writes will error and it does the needed
ian@0 1274 * line discipline hangup and signal delivery. The tty object itself
ian@0 1275 * remains intact.
ian@0 1276 *
ian@0 1277 * Locking:
ian@0 1278 * BKL
ian@0 1279 * redirect lock for undoing redirection
ian@0 1280 * file list lock for manipulating list of ttys
ian@0 1281 * tty_ldisc_lock from called functions
ian@0 1282 * termios_sem resetting termios data
ian@0 1283 * tasklist_lock to walk task list for hangup event
ian@0 1284 *
ian@0 1285 */
ian@0 1286 static void do_tty_hangup(void *data)
ian@0 1287 {
ian@0 1288 struct tty_struct *tty = (struct tty_struct *) data;
ian@0 1289 struct file * cons_filp = NULL;
ian@0 1290 struct file *filp, *f = NULL;
ian@0 1291 struct task_struct *p;
ian@0 1292 struct tty_ldisc *ld;
ian@0 1293 int closecount = 0, n;
ian@0 1294
ian@0 1295 if (!tty)
ian@0 1296 return;
ian@0 1297
ian@0 1298 /* inuse_filps is protected by the single kernel lock */
ian@0 1299 lock_kernel();
ian@0 1300
ian@0 1301 spin_lock(&redirect_lock);
ian@0 1302 if (redirect && redirect->private_data == tty) {
ian@0 1303 f = redirect;
ian@0 1304 redirect = NULL;
ian@0 1305 }
ian@0 1306 spin_unlock(&redirect_lock);
ian@0 1307
ian@0 1308 check_tty_count(tty, "do_tty_hangup");
ian@0 1309 file_list_lock();
ian@0 1310 /* This breaks for file handles being sent over AF_UNIX sockets ? */
ian@0 1311 list_for_each_entry(filp, &tty->tty_files, f_u.fu_list) {
ian@0 1312 if (filp->f_op->write == redirected_tty_write)
ian@0 1313 cons_filp = filp;
ian@0 1314 if (filp->f_op->write != tty_write)
ian@0 1315 continue;
ian@0 1316 closecount++;
ian@0 1317 tty_fasync(-1, filp, 0); /* can't block */
ian@0 1318 filp->f_op = &hung_up_tty_fops;
ian@0 1319 }
ian@0 1320 file_list_unlock();
ian@0 1321
ian@0 1322 /* FIXME! What are the locking issues here? This may me overdoing things..
ian@0 1323 * this question is especially important now that we've removed the irqlock. */
ian@0 1324
ian@0 1325 ld = tty_ldisc_ref(tty);
ian@0 1326 if(ld != NULL) /* We may have no line discipline at this point */
ian@0 1327 {
ian@0 1328 if (ld->flush_buffer)
ian@0 1329 ld->flush_buffer(tty);
ian@0 1330 if (tty->driver->flush_buffer)
ian@0 1331 tty->driver->flush_buffer(tty);
ian@0 1332 if ((test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags)) &&
ian@0 1333 ld->write_wakeup)
ian@0 1334 ld->write_wakeup(tty);
ian@0 1335 if (ld->hangup)
ian@0 1336 ld->hangup(tty);
ian@0 1337 }
ian@0 1338
ian@0 1339 /* FIXME: Once we trust the LDISC code better we can wait here for
ian@0 1340 ldisc completion and fix the driver call race */
ian@0 1341
ian@0 1342 wake_up_interruptible(&tty->write_wait);
ian@0 1343 wake_up_interruptible(&tty->read_wait);
ian@0 1344
ian@0 1345 /*
ian@0 1346 * Shutdown the current line discipline, and reset it to
ian@0 1347 * N_TTY.
ian@0 1348 */
ian@0 1349 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS)
ian@0 1350 {
ian@0 1351 down(&tty->termios_sem);
ian@0 1352 *tty->termios = tty->driver->init_termios;
ian@0 1353 up(&tty->termios_sem);
ian@0 1354 }
ian@0 1355
ian@0 1356 /* Defer ldisc switch */
ian@0 1357 /* tty_deferred_ldisc_switch(N_TTY);
ian@0 1358
ian@0 1359 This should get done automatically when the port closes and
ian@0 1360 tty_release is called */
ian@0 1361
ian@0 1362 read_lock(&tasklist_lock);
ian@0 1363 if (tty->session > 0) {
ian@0 1364 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
ian@0 1365 if (p->signal->tty == tty)
ian@0 1366 p->signal->tty = NULL;
ian@0 1367 if (!p->signal->leader)
ian@0 1368 continue;
ian@0 1369 group_send_sig_info(SIGHUP, SEND_SIG_PRIV, p);
ian@0 1370 group_send_sig_info(SIGCONT, SEND_SIG_PRIV, p);
ian@0 1371 if (tty->pgrp > 0)
ian@0 1372 p->signal->tty_old_pgrp = tty->pgrp;
ian@0 1373 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
ian@0 1374 }
ian@0 1375 read_unlock(&tasklist_lock);
ian@0 1376
ian@0 1377 tty->flags = 0;
ian@0 1378 tty->session = 0;
ian@0 1379 tty->pgrp = -1;
ian@0 1380 tty->ctrl_status = 0;
ian@0 1381 /*
ian@0 1382 * If one of the devices matches a console pointer, we
ian@0 1383 * cannot just call hangup() because that will cause
ian@0 1384 * tty->count and state->count to go out of sync.
ian@0 1385 * So we just call close() the right number of times.
ian@0 1386 */
ian@0 1387 if (cons_filp) {
ian@0 1388 if (tty->driver->close)
ian@0 1389 for (n = 0; n < closecount; n++)
ian@0 1390 tty->driver->close(tty, cons_filp);
ian@0 1391 } else if (tty->driver->hangup)
ian@0 1392 (tty->driver->hangup)(tty);
ian@0 1393
ian@0 1394 /* We don't want to have driver/ldisc interactions beyond
ian@0 1395 the ones we did here. The driver layer expects no
ian@0 1396 calls after ->hangup() from the ldisc side. However we
ian@0 1397 can't yet guarantee all that */
ian@0 1398
ian@0 1399 set_bit(TTY_HUPPED, &tty->flags);
ian@0 1400 if (ld) {
ian@0 1401 tty_ldisc_enable(tty);
ian@0 1402 tty_ldisc_deref(ld);
ian@0 1403 }
ian@0 1404 unlock_kernel();
ian@0 1405 if (f)
ian@0 1406 fput(f);
ian@0 1407 }
ian@0 1408
ian@0 1409 /**
ian@0 1410 * tty_hangup - trigger a hangup event
ian@0 1411 * @tty: tty to hangup
ian@0 1412 *
ian@0 1413 * A carrier loss (virtual or otherwise) has occurred on this like
ian@0 1414 * schedule a hangup sequence to run after this event.
ian@0 1415 */
ian@0 1416
ian@0 1417 void tty_hangup(struct tty_struct * tty)
ian@0 1418 {
ian@0 1419 #ifdef TTY_DEBUG_HANGUP
ian@0 1420 char buf[64];
ian@0 1421
ian@0 1422 printk(KERN_DEBUG "%s hangup...\n", tty_name(tty, buf));
ian@0 1423 #endif
ian@0 1424 schedule_work(&tty->hangup_work);
ian@0 1425 }
ian@0 1426
ian@0 1427 EXPORT_SYMBOL(tty_hangup);
ian@0 1428
ian@0 1429 /**
ian@0 1430 * tty_vhangup - process vhangup
ian@0 1431 * @tty: tty to hangup
ian@0 1432 *
ian@0 1433 * The user has asked via system call for the terminal to be hung up.
ian@0 1434 * We do this synchronously so that when the syscall returns the process
ian@0 1435 * is complete. That guarantee is neccessary for security reasons.
ian@0 1436 */
ian@0 1437
ian@0 1438 void tty_vhangup(struct tty_struct * tty)
ian@0 1439 {
ian@0 1440 #ifdef TTY_DEBUG_HANGUP
ian@0 1441 char buf[64];
ian@0 1442
ian@0 1443 printk(KERN_DEBUG "%s vhangup...\n", tty_name(tty, buf));
ian@0 1444 #endif
ian@0 1445 do_tty_hangup((void *) tty);
ian@0 1446 }
ian@0 1447 EXPORT_SYMBOL(tty_vhangup);
ian@0 1448
ian@0 1449 /**
ian@0 1450 * tty_hung_up_p - was tty hung up
ian@0 1451 * @filp: file pointer of tty
ian@0 1452 *
ian@0 1453 * Return true if the tty has been subject to a vhangup or a carrier
ian@0 1454 * loss
ian@0 1455 */
ian@0 1456
ian@0 1457 int tty_hung_up_p(struct file * filp)
ian@0 1458 {
ian@0 1459 return (filp->f_op == &hung_up_tty_fops);
ian@0 1460 }
ian@0 1461
ian@0 1462 EXPORT_SYMBOL(tty_hung_up_p);
ian@0 1463
ian@0 1464 /**
ian@0 1465 * disassociate_ctty - disconnect controlling tty
ian@0 1466 * @on_exit: true if exiting so need to "hang up" the session
ian@0 1467 *
ian@0 1468 * This function is typically called only by the session leader, when
ian@0 1469 * it wants to disassociate itself from its controlling tty.
ian@0 1470 *
ian@0 1471 * It performs the following functions:
ian@0 1472 * (1) Sends a SIGHUP and SIGCONT to the foreground process group
ian@0 1473 * (2) Clears the tty from being controlling the session
ian@0 1474 * (3) Clears the controlling tty for all processes in the
ian@0 1475 * session group.
ian@0 1476 *
ian@0 1477 * The argument on_exit is set to 1 if called when a process is
ian@0 1478 * exiting; it is 0 if called by the ioctl TIOCNOTTY.
ian@0 1479 *
ian@0 1480 * Locking: tty_mutex is taken to protect current->signal->tty
ian@0 1481 * BKL is taken for hysterical raisins
ian@0 1482 * Tasklist lock is taken (under tty_mutex) to walk process
ian@0 1483 * lists for the session.
ian@0 1484 */
ian@0 1485
ian@0 1486 void disassociate_ctty(int on_exit)
ian@0 1487 {
ian@0 1488 struct tty_struct *tty;
ian@0 1489 struct task_struct *p;
ian@0 1490 int tty_pgrp = -1;
ian@0 1491
ian@0 1492 lock_kernel();
ian@0 1493
ian@0 1494 mutex_lock(&tty_mutex);
ian@0 1495 tty = current->signal->tty;
ian@0 1496 if (tty) {
ian@0 1497 tty_pgrp = tty->pgrp;
ian@0 1498 mutex_unlock(&tty_mutex);
ian@0 1499 if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
ian@0 1500 tty_vhangup(tty);
ian@0 1501 } else {
ian@0 1502 if (current->signal->tty_old_pgrp) {
ian@0 1503 kill_pg(current->signal->tty_old_pgrp, SIGHUP, on_exit);
ian@0 1504 kill_pg(current->signal->tty_old_pgrp, SIGCONT, on_exit);
ian@0 1505 }
ian@0 1506 mutex_unlock(&tty_mutex);
ian@0 1507 unlock_kernel();
ian@0 1508 return;
ian@0 1509 }
ian@0 1510 if (tty_pgrp > 0) {
ian@0 1511 kill_pg(tty_pgrp, SIGHUP, on_exit);
ian@0 1512 if (!on_exit)
ian@0 1513 kill_pg(tty_pgrp, SIGCONT, on_exit);
ian@0 1514 }
ian@0 1515
ian@0 1516 /* Must lock changes to tty_old_pgrp */
ian@0 1517 mutex_lock(&tty_mutex);
ian@0 1518 current->signal->tty_old_pgrp = 0;
ian@0 1519 tty->session = 0;
ian@0 1520 tty->pgrp = -1;
ian@0 1521
ian@0 1522 /* Now clear signal->tty under the lock */
ian@0 1523 read_lock(&tasklist_lock);
ian@0 1524 do_each_task_pid(current->signal->session, PIDTYPE_SID, p) {
ian@0 1525 p->signal->tty = NULL;
ian@0 1526 } while_each_task_pid(current->signal->session, PIDTYPE_SID, p);
ian@0 1527 read_unlock(&tasklist_lock);
ian@0 1528 mutex_unlock(&tty_mutex);
ian@0 1529 unlock_kernel();
ian@0 1530 }
ian@0 1531
ian@0 1532
ian@0 1533 /**
ian@0 1534 * stop_tty - propogate flow control
ian@0 1535 * @tty: tty to stop
ian@0 1536 *
ian@0 1537 * Perform flow control to the driver. For PTY/TTY pairs we
ian@0 1538 * must also propogate the TIOCKPKT status. May be called
ian@0 1539 * on an already stopped device and will not re-call the driver
ian@0 1540 * method.
ian@0 1541 *
ian@0 1542 * This functionality is used by both the line disciplines for
ian@0 1543 * halting incoming flow and by the driver. It may therefore be
ian@0 1544 * called from any context, may be under the tty atomic_write_lock
ian@0 1545 * but not always.
ian@0 1546 *
ian@0 1547 * Locking:
ian@0 1548 * Broken. Relies on BKL which is unsafe here.
ian@0 1549 */
ian@0 1550
ian@0 1551 void stop_tty(struct tty_struct *tty)
ian@0 1552 {
ian@0 1553 if (tty->stopped)
ian@0 1554 return;
ian@0 1555 tty->stopped = 1;
ian@0 1556 if (tty->link && tty->link->packet) {
ian@0 1557 tty->ctrl_status &= ~TIOCPKT_START;
ian@0 1558 tty->ctrl_status |= TIOCPKT_STOP;
ian@0 1559 wake_up_interruptible(&tty->link->read_wait);
ian@0 1560 }
ian@0 1561 if (tty->driver->stop)
ian@0 1562 (tty->driver->stop)(tty);
ian@0 1563 }
ian@0 1564
ian@0 1565 EXPORT_SYMBOL(stop_tty);
ian@0 1566
ian@0 1567 /**
ian@0 1568 * start_tty - propogate flow control
ian@0 1569 * @tty: tty to start
ian@0 1570 *
ian@0 1571 * Start a tty that has been stopped if at all possible. Perform
ian@0 1572 * any neccessary wakeups and propogate the TIOCPKT status. If this
ian@0 1573 * is the tty was previous stopped and is being started then the
ian@0 1574 * driver start method is invoked and the line discipline woken.
ian@0 1575 *
ian@0 1576 * Locking:
ian@0 1577 * Broken. Relies on BKL which is unsafe here.
ian@0 1578 */
ian@0 1579
ian@0 1580 void start_tty(struct tty_struct *tty)
ian@0 1581 {
ian@0 1582 if (!tty->stopped || tty->flow_stopped)
ian@0 1583 return;
ian@0 1584 tty->stopped = 0;
ian@0 1585 if (tty->link && tty->link->packet) {
ian@0 1586 tty->ctrl_status &= ~TIOCPKT_STOP;
ian@0 1587 tty->ctrl_status |= TIOCPKT_START;
ian@0 1588 wake_up_interruptible(&tty->link->read_wait);
ian@0 1589 }
ian@0 1590 if (tty->driver->start)
ian@0 1591 (tty->driver->start)(tty);
ian@0 1592
ian@0 1593 /* If we have a running line discipline it may need kicking */
ian@0 1594 tty_wakeup(tty);
ian@0 1595 wake_up_interruptible(&tty->write_wait);
ian@0 1596 }
ian@0 1597
ian@0 1598 EXPORT_SYMBOL(start_tty);
ian@0 1599
ian@0 1600 /**
ian@0 1601 * tty_read - read method for tty device files
ian@0 1602 * @file: pointer to tty file
ian@0 1603 * @buf: user buffer
ian@0 1604 * @count: size of user buffer
ian@0 1605 * @ppos: unused
ian@0 1606 *
ian@0 1607 * Perform the read system call function on this terminal device. Checks
ian@0 1608 * for hung up devices before calling the line discipline method.
ian@0 1609 *
ian@0 1610 * Locking:
ian@0 1611 * Locks the line discipline internally while needed
ian@0 1612 * For historical reasons the line discipline read method is
ian@0 1613 * invoked under the BKL. This will go away in time so do not rely on it
ian@0 1614 * in new code. Multiple read calls may be outstanding in parallel.
ian@0 1615 */
ian@0 1616
ian@0 1617 static ssize_t tty_read(struct file * file, char __user * buf, size_t count,
ian@0 1618 loff_t *ppos)
ian@0 1619 {
ian@0 1620 int i;
ian@0 1621 struct tty_struct * tty;
ian@0 1622 struct inode *inode;
ian@0 1623 struct tty_ldisc *ld;
ian@0 1624
ian@0 1625 tty = (struct tty_struct *)file->private_data;
ian@0 1626 inode = file->f_dentry->d_inode;
ian@0 1627 if (tty_paranoia_check(tty, inode, "tty_read"))
ian@0 1628 return -EIO;
ian@0 1629 if (!tty || (test_bit(TTY_IO_ERROR, &tty->flags)))
ian@0 1630 return -EIO;
ian@0 1631
ian@0 1632 /* We want to wait for the line discipline to sort out in this
ian@0 1633 situation */
ian@0 1634 ld = tty_ldisc_ref_wait(tty);
ian@0 1635 lock_kernel();
ian@0 1636 if (ld->read)
ian@0 1637 i = (ld->read)(tty,file,buf,count);
ian@0 1638 else
ian@0 1639 i = -EIO;
ian@0 1640 tty_ldisc_deref(ld);
ian@0 1641 unlock_kernel();
ian@0 1642 if (i > 0)
ian@0 1643 inode->i_atime = current_fs_time(inode->i_sb);
ian@0 1644 return i;
ian@0 1645 }
ian@0 1646
ian@0 1647 /*
ian@0 1648 * Split writes up in sane blocksizes to avoid
ian@0 1649 * denial-of-service type attacks
ian@0 1650 */
ian@0 1651 static inline ssize_t do_tty_write(
ian@0 1652 ssize_t (*write)(struct tty_struct *, struct file *, const unsigned char *, size_t),
ian@0 1653 struct tty_struct *tty,
ian@0 1654 struct file *file,
ian@0 1655 const char __user *buf,
ian@0 1656 size_t count)
ian@0 1657 {
ian@0 1658 ssize_t ret = 0, written = 0;
ian@0 1659 unsigned int chunk;
ian@0 1660
ian@0 1661 /* FIXME: O_NDELAY ... */
ian@0 1662 if (mutex_lock_interruptible(&tty->atomic_write_lock)) {
ian@0 1663 return -ERESTARTSYS;
ian@0 1664 }
ian@0 1665
ian@0 1666 /*
ian@0 1667 * We chunk up writes into a temporary buffer. This
ian@0 1668 * simplifies low-level drivers immensely, since they
ian@0 1669 * don't have locking issues and user mode accesses.
ian@0 1670 *
ian@0 1671 * But if TTY_NO_WRITE_SPLIT is set, we should use a
ian@0 1672 * big chunk-size..
ian@0 1673 *
ian@0 1674 * The default chunk-size is 2kB, because the NTTY
ian@0 1675 * layer has problems with bigger chunks. It will
ian@0 1676 * claim to be able to handle more characters than
ian@0 1677 * it actually does.
ian@0 1678 *
ian@0 1679 * FIXME: This can probably go away now except that 64K chunks
ian@0 1680 * are too likely to fail unless switched to vmalloc...
ian@0 1681 */
ian@0 1682 chunk = 2048;
ian@0 1683 if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
ian@0 1684 chunk = 65536;
ian@0 1685 if (count < chunk)
ian@0 1686 chunk = count;
ian@0 1687
ian@0 1688 /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
ian@0 1689 if (tty->write_cnt < chunk) {
ian@0 1690 unsigned char *buf;
ian@0 1691
ian@0 1692 if (chunk < 1024)
ian@0 1693 chunk = 1024;
ian@0 1694
ian@0 1695 buf = kmalloc(chunk, GFP_KERNEL);
ian@0 1696 if (!buf) {
ian@0 1697 mutex_unlock(&tty->atomic_write_lock);
ian@0 1698 return -ENOMEM;
ian@0 1699 }
ian@0 1700 kfree(tty->write_buf);
ian@0 1701 tty->write_cnt = chunk;
ian@0 1702 tty->write_buf = buf;
ian@0 1703 }
ian@0 1704
ian@0 1705 /* Do the write .. */
ian@0 1706 for (;;) {
ian@0 1707 size_t size = count;
ian@0 1708 if (size > chunk)
ian@0 1709 size = chunk;
ian@0 1710 ret = -EFAULT;
ian@0 1711 if (copy_from_user(tty->write_buf, buf, size))
ian@0 1712 break;
ian@0 1713 lock_kernel();
ian@0 1714 ret = write(tty, file, tty->write_buf, size);
ian@0 1715 unlock_kernel();
ian@0 1716 if (ret <= 0)
ian@0 1717 break;
ian@0 1718 written += ret;
ian@0 1719 buf += ret;
ian@0 1720 count -= ret;
ian@0 1721 if (!count)
ian@0 1722 break;
ian@0 1723 ret = -ERESTARTSYS;
ian@0 1724 if (signal_pending(current))
ian@0 1725 break;
ian@0 1726 cond_resched();
ian@0 1727 }
ian@0 1728 if (written) {
ian@0 1729 struct inode *inode = file->f_dentry->d_inode;
ian@0 1730 inode->i_mtime = current_fs_time(inode->i_sb);
ian@0 1731 ret = written;
ian@0 1732 }
ian@0 1733 mutex_unlock(&tty->atomic_write_lock);
ian@0 1734 return ret;
ian@0 1735 }
ian@0 1736
ian@0 1737
ian@0 1738 /**
ian@0 1739 * tty_write - write method for tty device file
ian@0 1740 * @file: tty file pointer
ian@0 1741 * @buf: user data to write
ian@0 1742 * @count: bytes to write
ian@0 1743 * @ppos: unused
ian@0 1744 *
ian@0 1745 * Write data to a tty device via the line discipline.
ian@0 1746 *
ian@0 1747 * Locking:
ian@0 1748 * Locks the line discipline as required
ian@0 1749 * Writes to the tty driver are serialized by the atomic_write_lock
ian@0 1750 * and are then processed in chunks to the device. The line discipline
ian@0 1751 * write method will not be involked in parallel for each device
ian@0 1752 * The line discipline write method is called under the big
ian@0 1753 * kernel lock for historical reasons. New code should not rely on this.
ian@0 1754 */
ian@0 1755
ian@0 1756 static ssize_t tty_write(struct file * file, const char __user * buf, size_t count,
ian@0 1757 loff_t *ppos)
ian@0 1758 {
ian@0 1759 struct tty_struct * tty;
ian@0 1760 struct inode *inode = file->f_dentry->d_inode;
ian@0 1761 ssize_t ret;
ian@0 1762 struct tty_ldisc *ld;
ian@0 1763
ian@0 1764 tty = (struct tty_struct *)file->private_data;
ian@0 1765 if (tty_paranoia_check(tty, inode, "tty_write"))
ian@0 1766 return -EIO;
ian@0 1767 if (!tty || !tty->driver->write || (test_bit(TTY_IO_ERROR, &tty->flags)))
ian@0 1768 return -EIO;
ian@0 1769
ian@0 1770 ld = tty_ldisc_ref_wait(tty);
ian@0 1771 if (!ld->write)
ian@0 1772 ret = -EIO;
ian@0 1773 else
ian@0 1774 ret = do_tty_write(ld->write, tty, file, buf, count);
ian@0 1775 tty_ldisc_deref(ld);
ian@0 1776 return ret;
ian@0 1777 }
ian@0 1778
ian@0 1779 ssize_t redirected_tty_write(struct file * file, const char __user * buf, size_t count,
ian@0 1780 loff_t *ppos)
ian@0 1781 {
ian@0 1782 struct file *p = NULL;
ian@0 1783
ian@0 1784 spin_lock(&redirect_lock);
ian@0 1785 if (redirect) {
ian@0 1786 get_file(redirect);
ian@0 1787 p = redirect;
ian@0 1788 }
ian@0 1789 spin_unlock(&redirect_lock);
ian@0 1790
ian@0 1791 if (p) {
ian@0 1792 ssize_t res;
ian@0 1793 res = vfs_write(p, buf, count, &p->f_pos);
ian@0 1794 fput(p);
ian@0 1795 return res;
ian@0 1796 }
ian@0 1797
ian@0 1798 return tty_write(file, buf, count, ppos);
ian@0 1799 }
ian@0 1800
ian@0 1801 static char ptychar[] = "pqrstuvwxyzabcde";
ian@0 1802
ian@0 1803 /**
ian@0 1804 * pty_line_name - generate name for a pty
ian@0 1805 * @driver: the tty driver in use
ian@0 1806 * @index: the minor number
ian@0 1807 * @p: output buffer of at least 6 bytes
ian@0 1808 *
ian@0 1809 * Generate a name from a driver reference and write it to the output
ian@0 1810 * buffer.
ian@0 1811 *
ian@0 1812 * Locking: None
ian@0 1813 */
ian@0 1814 static void pty_line_name(struct tty_driver *driver, int index, char *p)
ian@0 1815 {
ian@0 1816 int i = index + driver->name_base;
ian@0 1817 /* ->name is initialized to "ttyp", but "tty" is expected */
ian@0 1818 sprintf(p, "%s%c%x",
ian@0 1819 driver->subtype == PTY_TYPE_SLAVE ? "tty" : driver->name,
ian@0 1820 ptychar[i >> 4 & 0xf], i & 0xf);
ian@0 1821 }
ian@0 1822
ian@0 1823 /**
ian@0 1824 * pty_line_name - generate name for a tty
ian@0 1825 * @driver: the tty driver in use
ian@0 1826 * @index: the minor number
ian@0 1827 * @p: output buffer of at least 7 bytes
ian@0 1828 *
ian@0 1829 * Generate a name from a driver reference and write it to the output
ian@0 1830 * buffer.
ian@0 1831 *
ian@0 1832 * Locking: None
ian@0 1833 */
ian@0 1834 static void tty_line_name(struct tty_driver *driver, int index, char *p)
ian@0 1835 {
ian@0 1836 sprintf(p, "%s%d", driver->name, index + driver->name_base);
ian@0 1837 }
ian@0 1838
ian@0 1839 /**
ian@0 1840 * init_dev - initialise a tty device
ian@0 1841 * @driver: tty driver we are opening a device on
ian@0 1842 * @idx: device index
ian@0 1843 * @tty: returned tty structure
ian@0 1844 *
ian@0 1845 * Prepare a tty device. This may not be a "new" clean device but
ian@0 1846 * could also be an active device. The pty drivers require special
ian@0 1847 * handling because of this.
ian@0 1848 *
ian@0 1849 * Locking:
ian@0 1850 * The function is called under the tty_mutex, which
ian@0 1851 * protects us from the tty struct or driver itself going away.
ian@0 1852 *
ian@0 1853 * On exit the tty device has the line discipline attached and
ian@0 1854 * a reference count of 1. If a pair was created for pty/tty use
ian@0 1855 * and the other was a pty master then it too has a reference count of 1.
ian@0 1856 *
ian@0 1857 * WSH 06/09/97: Rewritten to remove races and properly clean up after a
ian@0 1858 * failed open. The new code protects the open with a mutex, so it's
ian@0 1859 * really quite straightforward. The mutex locking can probably be
ian@0 1860 * relaxed for the (most common) case of reopening a tty.
ian@0 1861 */
ian@0 1862
ian@0 1863 static int init_dev(struct tty_driver *driver, int idx,
ian@0 1864 struct tty_struct **ret_tty)
ian@0 1865 {
ian@0 1866 struct tty_struct *tty, *o_tty;
ian@0 1867 struct termios *tp, **tp_loc, *o_tp, **o_tp_loc;
ian@0 1868 struct termios *ltp, **ltp_loc, *o_ltp, **o_ltp_loc;
ian@0 1869 int retval = 0;
ian@0 1870
ian@0 1871 /* check whether we're reopening an existing tty */
ian@0 1872 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
ian@0 1873 tty = devpts_get_tty(idx);
ian@0 1874 if (tty && driver->subtype == PTY_TYPE_MASTER)
ian@0 1875 tty = tty->link;
ian@0 1876 } else {
ian@0 1877 tty = driver->ttys[idx];
ian@0 1878 }
ian@0 1879 if (tty) goto fast_track;
ian@0 1880
ian@0 1881 /*
ian@0 1882 * First time open is complex, especially for PTY devices.
ian@0 1883 * This code guarantees that either everything succeeds and the
ian@0 1884 * TTY is ready for operation, or else the table slots are vacated
ian@0 1885 * and the allocated memory released. (Except that the termios
ian@0 1886 * and locked termios may be retained.)
ian@0 1887 */
ian@0 1888
ian@0 1889 if (!try_module_get(driver->owner)) {
ian@0 1890 retval = -ENODEV;
ian@0 1891 goto end_init;
ian@0 1892 }
ian@0 1893
ian@0 1894 o_tty = NULL;
ian@0 1895 tp = o_tp = NULL;
ian@0 1896 ltp = o_ltp = NULL;
ian@0 1897
ian@0 1898 tty = alloc_tty_struct();
ian@0 1899 if(!tty)
ian@0 1900 goto fail_no_mem;
ian@0 1901 initialize_tty_struct(tty);
ian@0 1902 tty->driver = driver;
ian@0 1903 tty->index = idx;
ian@0 1904 tty_line_name(driver, idx, tty->name);
ian@0 1905
ian@0 1906 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
ian@0 1907 tp_loc = &tty->termios;
ian@0 1908 ltp_loc = &tty->termios_locked;
ian@0 1909 } else {
ian@0 1910 tp_loc = &driver->termios[idx];
ian@0 1911 ltp_loc = &driver->termios_locked[idx];
ian@0 1912 }
ian@0 1913
ian@0 1914 if (!*tp_loc) {
ian@0 1915 tp = (struct termios *) kmalloc(sizeof(struct termios),
ian@0 1916 GFP_KERNEL);
ian@0 1917 if (!tp)
ian@0 1918 goto free_mem_out;
ian@0 1919 *tp = driver->init_termios;
ian@0 1920 }
ian@0 1921
ian@0 1922 if (!*ltp_loc) {
ian@0 1923 ltp = (struct termios *) kmalloc(sizeof(struct termios),
ian@0 1924 GFP_KERNEL);
ian@0 1925 if (!ltp)
ian@0 1926 goto free_mem_out;
ian@0 1927 memset(ltp, 0, sizeof(struct termios));
ian@0 1928 }
ian@0 1929
ian@0 1930 if (driver->type == TTY_DRIVER_TYPE_PTY) {
ian@0 1931 o_tty = alloc_tty_struct();
ian@0 1932 if (!o_tty)
ian@0 1933 goto free_mem_out;
ian@0 1934 initialize_tty_struct(o_tty);
ian@0 1935 o_tty->driver = driver->other;
ian@0 1936 o_tty->index = idx;
ian@0 1937 tty_line_name(driver->other, idx, o_tty->name);
ian@0 1938
ian@0 1939 if (driver->flags & TTY_DRIVER_DEVPTS_MEM) {
ian@0 1940 o_tp_loc = &o_tty->termios;
ian@0 1941 o_ltp_loc = &o_tty->termios_locked;
ian@0 1942 } else {
ian@0 1943 o_tp_loc = &driver->other->termios[idx];
ian@0 1944 o_ltp_loc = &driver->other->termios_locked[idx];
ian@0 1945 }
ian@0 1946
ian@0 1947 if (!*o_tp_loc) {
ian@0 1948 o_tp = (struct termios *)
ian@0 1949 kmalloc(sizeof(struct termios), GFP_KERNEL);
ian@0 1950 if (!o_tp)
ian@0 1951 goto free_mem_out;
ian@0 1952 *o_tp = driver->other->init_termios;
ian@0 1953 }
ian@0 1954
ian@0 1955 if (!*o_ltp_loc) {
ian@0 1956 o_ltp = (struct termios *)
ian@0 1957 kmalloc(sizeof(struct termios), GFP_KERNEL);
ian@0 1958 if (!o_ltp)
ian@0 1959 goto free_mem_out;
ian@0 1960 memset(o_ltp, 0, sizeof(struct termios));
ian@0 1961 }
ian@0 1962
ian@0 1963 /*
ian@0 1964 * Everything allocated ... set up the o_tty structure.
ian@0 1965 */
ian@0 1966 if (!(driver->other->flags & TTY_DRIVER_DEVPTS_MEM)) {
ian@0 1967 driver->other->ttys[idx] = o_tty;
ian@0 1968 }
ian@0 1969 if (!*o_tp_loc)
ian@0 1970 *o_tp_loc = o_tp;
ian@0 1971 if (!*o_ltp_loc)
ian@0 1972 *o_ltp_loc = o_ltp;
ian@0 1973 o_tty->termios = *o_tp_loc;
ian@0 1974 o_tty->termios_locked = *o_ltp_loc;
ian@0 1975 driver->other->refcount++;
ian@0 1976 if (driver->subtype == PTY_TYPE_MASTER)
ian@0 1977 o_tty->count++;
ian@0 1978
ian@0 1979 /* Establish the links in both directions */
ian@0 1980 tty->link = o_tty;
ian@0 1981 o_tty->link = tty;
ian@0 1982 }
ian@0 1983
ian@0 1984 /*
ian@0 1985 * All structures have been allocated, so now we install them.
ian@0 1986 * Failures after this point use release_mem to clean up, so
ian@0 1987 * there's no need to null out the local pointers.
ian@0 1988 */
ian@0 1989 if (!(driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
ian@0 1990 driver->ttys[idx] = tty;
ian@0 1991 }
ian@0 1992
ian@0 1993 if (!*tp_loc)
ian@0 1994 *tp_loc = tp;
ian@0 1995 if (!*ltp_loc)
ian@0 1996 *ltp_loc = ltp;
ian@0 1997 tty->termios = *tp_loc;
ian@0 1998 tty->termios_locked = *ltp_loc;
ian@0 1999 driver->refcount++;
ian@0 2000 tty->count++;
ian@0 2001
ian@0 2002 /*
ian@0 2003 * Structures all installed ... call the ldisc open routines.
ian@0 2004 * If we fail here just call release_mem to clean up. No need
ian@0 2005 * to decrement the use counts, as release_mem doesn't care.
ian@0 2006 */
ian@0 2007
ian@0 2008 if (tty->ldisc.open) {
ian@0 2009 retval = (tty->ldisc.open)(tty);
ian@0 2010 if (retval)
ian@0 2011 goto release_mem_out;
ian@0 2012 }
ian@0 2013 if (o_tty && o_tty->ldisc.open) {
ian@0 2014 retval = (o_tty->ldisc.open)(o_tty);
ian@0 2015 if (retval) {
ian@0 2016 if (tty->ldisc.close)
ian@0 2017 (tty->ldisc.close)(tty);
ian@0 2018 goto release_mem_out;
ian@0 2019 }
ian@0 2020 tty_ldisc_enable(o_tty);
ian@0 2021 }
ian@0 2022 tty_ldisc_enable(tty);
ian@0 2023 goto success;
ian@0 2024
ian@0 2025 /*
ian@0 2026 * This fast open can be used if the tty is already open.
ian@0 2027 * No memory is allocated, and the only failures are from
ian@0 2028 * attempting to open a closing tty or attempting multiple
ian@0 2029 * opens on a pty master.
ian@0 2030 */
ian@0 2031 fast_track:
ian@0 2032 if (test_bit(TTY_CLOSING, &tty->flags)) {
ian@0 2033 retval = -EIO;
ian@0 2034 goto end_init;
ian@0 2035 }
ian@0 2036 if (driver->type == TTY_DRIVER_TYPE_PTY &&
ian@0 2037 driver->subtype == PTY_TYPE_MASTER) {
ian@0 2038 /*
ian@0 2039 * special case for PTY masters: only one open permitted,
ian@0 2040 * and the slave side open count is incremented as well.
ian@0 2041 */
ian@0 2042 if (tty->count) {
ian@0 2043 retval = -EIO;
ian@0 2044 goto end_init;
ian@0 2045 }
ian@0 2046 tty->link->count++;
ian@0 2047 }
ian@0 2048 tty->count++;
ian@0 2049 tty->driver = driver; /* N.B. why do this every time?? */
ian@0 2050
ian@0 2051 /* FIXME */
ian@0 2052 if(!test_bit(TTY_LDISC, &tty->flags))
ian@0 2053 printk(KERN_ERR "init_dev but no ldisc\n");
ian@0 2054 success:
ian@0 2055 *ret_tty = tty;
ian@0 2056
ian@0 2057 /* All paths come through here to release the mutex */
ian@0 2058 end_init:
ian@0 2059 return retval;
ian@0 2060
ian@0 2061 /* Release locally allocated memory ... nothing placed in slots */
ian@0 2062 free_mem_out:
ian@0 2063 kfree(o_tp);
ian@0 2064 if (o_tty)
ian@0 2065 free_tty_struct(o_tty);
ian@0 2066 kfree(ltp);
ian@0 2067 kfree(tp);
ian@0 2068 free_tty_struct(tty);
ian@0 2069
ian@0 2070 fail_no_mem:
ian@0 2071 module_put(driver->owner);
ian@0 2072 retval = -ENOMEM;
ian@0 2073 goto end_init;
ian@0 2074
ian@0 2075 /* call the tty release_mem routine to clean out this slot */
ian@0 2076 release_mem_out:
ian@0 2077 printk(KERN_INFO "init_dev: ldisc open failed, "
ian@0 2078 "clearing slot %d\n", idx);
ian@0 2079 release_mem(tty, idx);
ian@0 2080 goto end_init;
ian@0 2081 }
ian@0 2082
ian@0 2083 /**
ian@0 2084 * release_mem - release tty structure memory
ian@0 2085 *
ian@0 2086 * Releases memory associated with a tty structure, and clears out the
ian@0 2087 * driver table slots. This function is called when a device is no longer
ian@0 2088 * in use. It also gets called when setup of a device fails.
ian@0 2089 *
ian@0 2090 * Locking:
ian@0 2091 * tty_mutex - sometimes only
ian@0 2092 * takes the file list lock internally when working on the list
ian@0 2093 * of ttys that the driver keeps.
ian@0 2094 * FIXME: should we require tty_mutex is held here ??
ian@0 2095 */
ian@0 2096
ian@0 2097 static void release_mem(struct tty_struct *tty, int idx)
ian@0 2098 {
ian@0 2099 struct tty_struct *o_tty;
ian@0 2100 struct termios *tp;
ian@0 2101 int devpts = tty->driver->flags & TTY_DRIVER_DEVPTS_MEM;
ian@0 2102
ian@0 2103 if ((o_tty = tty->link) != NULL) {
ian@0 2104 if (!devpts)
ian@0 2105 o_tty->driver->ttys[idx] = NULL;
ian@0 2106 if (o_tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
ian@0 2107 tp = o_tty->termios;
ian@0 2108 if (!devpts)
ian@0 2109 o_tty->driver->termios[idx] = NULL;
ian@0 2110 kfree(tp);
ian@0 2111
ian@0 2112 tp = o_tty->termios_locked;
ian@0 2113 if (!devpts)
ian@0 2114 o_tty->driver->termios_locked[idx] = NULL;
ian@0 2115 kfree(tp);
ian@0 2116 }
ian@0 2117 o_tty->magic = 0;
ian@0 2118 o_tty->driver->refcount--;
ian@0 2119 file_list_lock();
ian@0 2120 list_del_init(&o_tty->tty_files);
ian@0 2121 file_list_unlock();
ian@0 2122 free_tty_struct(o_tty);
ian@0 2123 }
ian@0 2124
ian@0 2125 if (!devpts)
ian@0 2126 tty->driver->ttys[idx] = NULL;
ian@0 2127 if (tty->driver->flags & TTY_DRIVER_RESET_TERMIOS) {
ian@0 2128 tp = tty->termios;
ian@0 2129 if (!devpts)
ian@0 2130 tty->driver->termios[idx] = NULL;
ian@0 2131 kfree(tp);
ian@0 2132
ian@0 2133 tp = tty->termios_locked;
ian@0 2134 if (!devpts)
ian@0 2135 tty->driver->termios_locked[idx] = NULL;
ian@0 2136 kfree(tp);
ian@0 2137 }
ian@0 2138
ian@0 2139 tty->magic = 0;
ian@0 2140 tty->driver->refcount--;
ian@0 2141 file_list_lock();
ian@0 2142 list_del_init(&tty->tty_files);
ian@0 2143 file_list_unlock();
ian@0 2144 module_put(tty->driver->owner);
ian@0 2145 free_tty_struct(tty);
ian@0 2146 }
ian@0 2147
ian@0 2148 /*
ian@0 2149 * Even releasing the tty structures is a tricky business.. We have
ian@0 2150 * to be very careful that the structures are all released at the
ian@0 2151 * same time, as interrupts might otherwise get the wrong pointers.
ian@0 2152 *
ian@0 2153 * WSH 09/09/97: rewritten to avoid some nasty race conditions that could
ian@0 2154 * lead to double frees or releasing memory still in use.
ian@0 2155 */
ian@0 2156 static void release_dev(struct file * filp)
ian@0 2157 {
ian@0 2158 struct tty_struct *tty, *o_tty;
ian@0 2159 int pty_master, tty_closing, o_tty_closing, do_sleep;
ian@0 2160 int devpts;
ian@0 2161 int idx;
ian@0 2162 char buf[64];
ian@0 2163 unsigned long flags;
ian@0 2164
ian@0 2165 tty = (struct tty_struct *)filp->private_data;
ian@0 2166 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "release_dev"))
ian@0 2167 return;
ian@0 2168
ian@0 2169 check_tty_count(tty, "release_dev");
ian@0 2170
ian@0 2171 tty_fasync(-1, filp, 0);
ian@0 2172
ian@0 2173 idx = tty->index;
ian@0 2174 pty_master = (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
ian@0 2175 tty->driver->subtype == PTY_TYPE_MASTER);
ian@0 2176 devpts = (tty->driver->flags & TTY_DRIVER_DEVPTS_MEM) != 0;
ian@0 2177 o_tty = tty->link;
ian@0 2178
ian@0 2179 #ifdef TTY_PARANOIA_CHECK
ian@0 2180 if (idx < 0 || idx >= tty->driver->num) {
ian@0 2181 printk(KERN_DEBUG "release_dev: bad idx when trying to "
ian@0 2182 "free (%s)\n", tty->name);
ian@0 2183 return;
ian@0 2184 }
ian@0 2185 if (!(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
ian@0 2186 if (tty != tty->driver->ttys[idx]) {
ian@0 2187 printk(KERN_DEBUG "release_dev: driver.table[%d] not tty "
ian@0 2188 "for (%s)\n", idx, tty->name);
ian@0 2189 return;
ian@0 2190 }
ian@0 2191 if (tty->termios != tty->driver->termios[idx]) {
ian@0 2192 printk(KERN_DEBUG "release_dev: driver.termios[%d] not termios "
ian@0 2193 "for (%s)\n",
ian@0 2194 idx, tty->name);
ian@0 2195 return;
ian@0 2196 }
ian@0 2197 if (tty->termios_locked != tty->driver->termios_locked[idx]) {
ian@0 2198 printk(KERN_DEBUG "release_dev: driver.termios_locked[%d] not "
ian@0 2199 "termios_locked for (%s)\n",
ian@0 2200 idx, tty->name);
ian@0 2201 return;
ian@0 2202 }
ian@0 2203 }
ian@0 2204 #endif
ian@0 2205
ian@0 2206 #ifdef TTY_DEBUG_HANGUP
ian@0 2207 printk(KERN_DEBUG "release_dev of %s (tty count=%d)...",
ian@0 2208 tty_name(tty, buf), tty->count);
ian@0 2209 #endif
ian@0 2210
ian@0 2211 #ifdef TTY_PARANOIA_CHECK
ian@0 2212 if (tty->driver->other &&
ian@0 2213 !(tty->driver->flags & TTY_DRIVER_DEVPTS_MEM)) {
ian@0 2214 if (o_tty != tty->driver->other->ttys[idx]) {
ian@0 2215 printk(KERN_DEBUG "release_dev: other->table[%d] "
ian@0 2216 "not o_tty for (%s)\n",
ian@0 2217 idx, tty->name);
ian@0 2218 return;
ian@0 2219 }
ian@0 2220 if (o_tty->termios != tty->driver->other->termios[idx]) {
ian@0 2221 printk(KERN_DEBUG "release_dev: other->termios[%d] "
ian@0 2222 "not o_termios for (%s)\n",
ian@0 2223 idx, tty->name);
ian@0 2224 return;
ian@0 2225 }
ian@0 2226 if (o_tty->termios_locked !=
ian@0 2227 tty->driver->other->termios_locked[idx]) {
ian@0 2228 printk(KERN_DEBUG "release_dev: other->termios_locked["
ian@0 2229 "%d] not o_termios_locked for (%s)\n",
ian@0 2230 idx, tty->name);
ian@0 2231 return;
ian@0 2232 }
ian@0 2233 if (o_tty->link != tty) {
ian@0 2234 printk(KERN_DEBUG "release_dev: bad pty pointers\n");
ian@0 2235 return;
ian@0 2236 }
ian@0 2237 }
ian@0 2238 #endif
ian@0 2239 if (tty->driver->close)
ian@0 2240 tty->driver->close(tty, filp);
ian@0 2241
ian@0 2242 /*
ian@0 2243 * Sanity check: if tty->count is going to zero, there shouldn't be
ian@0 2244 * any waiters on tty->read_wait or tty->write_wait. We test the
ian@0 2245 * wait queues and kick everyone out _before_ actually starting to
ian@0 2246 * close. This ensures that we won't block while releasing the tty
ian@0 2247 * structure.
ian@0 2248 *
ian@0 2249 * The test for the o_tty closing is necessary, since the master and
ian@0 2250 * slave sides may close in any order. If the slave side closes out
ian@0 2251 * first, its count will be one, since the master side holds an open.
ian@0 2252 * Thus this test wouldn't be triggered at the time the slave closes,
ian@0 2253 * so we do it now.
ian@0 2254 *
ian@0 2255 * Note that it's possible for the tty to be opened again while we're
ian@0 2256 * flushing out waiters. By recalculating the closing flags before
ian@0 2257 * each iteration we avoid any problems.
ian@0 2258 */
ian@0 2259 while (1) {
ian@0 2260 /* Guard against races with tty->count changes elsewhere and
ian@0 2261 opens on /dev/tty */
ian@0 2262
ian@0 2263 mutex_lock(&tty_mutex);
ian@0 2264 tty_closing = tty->count <= 1;
ian@0 2265 o_tty_closing = o_tty &&
ian@0 2266 (o_tty->count <= (pty_master ? 1 : 0));
ian@0 2267 do_sleep = 0;
ian@0 2268
ian@0 2269 if (tty_closing) {
ian@0 2270 if (waitqueue_active(&tty->read_wait)) {
ian@0 2271 wake_up(&tty->read_wait);
ian@0 2272 do_sleep++;
ian@0 2273 }
ian@0 2274 if (waitqueue_active(&tty->write_wait)) {
ian@0 2275 wake_up(&tty->write_wait);
ian@0 2276 do_sleep++;
ian@0 2277 }
ian@0 2278 }
ian@0 2279 if (o_tty_closing) {
ian@0 2280 if (waitqueue_active(&o_tty->read_wait)) {
ian@0 2281 wake_up(&o_tty->read_wait);
ian@0 2282 do_sleep++;
ian@0 2283 }
ian@0 2284 if (waitqueue_active(&o_tty->write_wait)) {
ian@0 2285 wake_up(&o_tty->write_wait);
ian@0 2286 do_sleep++;
ian@0 2287 }
ian@0 2288 }
ian@0 2289 if (!do_sleep)
ian@0 2290 break;
ian@0 2291
ian@0 2292 printk(KERN_WARNING "release_dev: %s: read/write wait queue "
ian@0 2293 "active!\n", tty_name(tty, buf));
ian@0 2294 mutex_unlock(&tty_mutex);
ian@0 2295 schedule();
ian@0 2296 }
ian@0 2297
ian@0 2298 /*
ian@0 2299 * The closing flags are now consistent with the open counts on
ian@0 2300 * both sides, and we've completed the last operation that could
ian@0 2301 * block, so it's safe to proceed with closing.
ian@0 2302 */
ian@0 2303 if (pty_master) {
ian@0 2304 if (--o_tty->count < 0) {
ian@0 2305 printk(KERN_WARNING "release_dev: bad pty slave count "
ian@0 2306 "(%d) for %s\n",
ian@0 2307 o_tty->count, tty_name(o_tty, buf));
ian@0 2308 o_tty->count = 0;
ian@0 2309 }
ian@0 2310 }
ian@0 2311 if (--tty->count < 0) {
ian@0 2312 printk(KERN_WARNING "release_dev: bad tty->count (%d) for %s\n",
ian@0 2313 tty->count, tty_name(tty, buf));
ian@0 2314 tty->count = 0;
ian@0 2315 }
ian@0 2316
ian@0 2317 /*
ian@0 2318 * We've decremented tty->count, so we need to remove this file
ian@0 2319 * descriptor off the tty->tty_files list; this serves two
ian@0 2320 * purposes:
ian@0 2321 * - check_tty_count sees the correct number of file descriptors
ian@0 2322 * associated with this tty.
ian@0 2323 * - do_tty_hangup no longer sees this file descriptor as
ian@0 2324 * something that needs to be handled for hangups.
ian@0 2325 */
ian@0 2326 file_kill(filp);
ian@0 2327 filp->private_data = NULL;
ian@0 2328
ian@0 2329 /*
ian@0 2330 * Perform some housekeeping before deciding whether to return.
ian@0 2331 *
ian@0 2332 * Set the TTY_CLOSING flag if this was the last open. In the
ian@0 2333 * case of a pty we may have to wait around for the other side
ian@0 2334 * to close, and TTY_CLOSING makes sure we can't be reopened.
ian@0 2335 */
ian@0 2336 if(tty_closing)
ian@0 2337 set_bit(TTY_CLOSING, &tty->flags);
ian@0 2338 if(o_tty_closing)
ian@0 2339 set_bit(TTY_CLOSING, &o_tty->flags);
ian@0 2340
ian@0 2341 /*
ian@0 2342 * If _either_ side is closing, make sure there aren't any
ian@0 2343 * processes that still think tty or o_tty is their controlling
ian@0 2344 * tty.
ian@0 2345 */
ian@0 2346 if (tty_closing || o_tty_closing) {
ian@0 2347 struct task_struct *p;
ian@0 2348
ian@0 2349 read_lock(&tasklist_lock);
ian@0 2350 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
ian@0 2351 p->signal->tty = NULL;
ian@0 2352 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
ian@0 2353 if (o_tty)
ian@0 2354 do_each_task_pid(o_tty->session, PIDTYPE_SID, p) {
ian@0 2355 p->signal->tty = NULL;
ian@0 2356 } while_each_task_pid(o_tty->session, PIDTYPE_SID, p);
ian@0 2357 read_unlock(&tasklist_lock);
ian@0 2358 }
ian@0 2359
ian@0 2360 mutex_unlock(&tty_mutex);
ian@0 2361
ian@0 2362 /* check whether both sides are closing ... */
ian@0 2363 if (!tty_closing || (o_tty && !o_tty_closing))
ian@0 2364 return;
ian@0 2365
ian@0 2366 #ifdef TTY_DEBUG_HANGUP
ian@0 2367 printk(KERN_DEBUG "freeing tty structure...");
ian@0 2368 #endif
ian@0 2369 /*
ian@0 2370 * Prevent flush_to_ldisc() from rescheduling the work for later. Then
ian@0 2371 * kill any delayed work. As this is the final close it does not
ian@0 2372 * race with the set_ldisc code path.
ian@0 2373 */
ian@0 2374 clear_bit(TTY_LDISC, &tty->flags);
ian@0 2375 cancel_delayed_work(&tty->buf.work);
ian@0 2376
ian@0 2377 /*
ian@0 2378 * Wait for ->hangup_work and ->buf.work handlers to terminate
ian@0 2379 */
ian@0 2380
ian@0 2381 flush_scheduled_work();
ian@0 2382
ian@0 2383 /*
ian@0 2384 * Wait for any short term users (we know they are just driver
ian@0 2385 * side waiters as the file is closing so user count on the file
ian@0 2386 * side is zero.
ian@0 2387 */
ian@0 2388 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 2389 while(tty->ldisc.refcount)
ian@0 2390 {
ian@0 2391 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 2392 wait_event(tty_ldisc_wait, tty->ldisc.refcount == 0);
ian@0 2393 spin_lock_irqsave(&tty_ldisc_lock, flags);
ian@0 2394 }
ian@0 2395 spin_unlock_irqrestore(&tty_ldisc_lock, flags);
ian@0 2396 /*
ian@0 2397 * Shutdown the current line discipline, and reset it to N_TTY.
ian@0 2398 * N.B. why reset ldisc when we're releasing the memory??
ian@0 2399 *
ian@0 2400 * FIXME: this MUST get fixed for the new reflocking
ian@0 2401 */
ian@0 2402 if (tty->ldisc.close)
ian@0 2403 (tty->ldisc.close)(tty);
ian@0 2404 tty_ldisc_put(tty->ldisc.num);
ian@0 2405
ian@0 2406 /*
ian@0 2407 * Switch the line discipline back
ian@0 2408 */
ian@0 2409 tty_ldisc_assign(tty, tty_ldisc_get(N_TTY));
ian@0 2410 tty_set_termios_ldisc(tty,N_TTY);
ian@0 2411 if (o_tty) {
ian@0 2412 /* FIXME: could o_tty be in setldisc here ? */
ian@0 2413 clear_bit(TTY_LDISC, &o_tty->flags);
ian@0 2414 if (o_tty->ldisc.close)
ian@0 2415 (o_tty->ldisc.close)(o_tty);
ian@0 2416 tty_ldisc_put(o_tty->ldisc.num);
ian@0 2417 tty_ldisc_assign(o_tty, tty_ldisc_get(N_TTY));
ian@0 2418 tty_set_termios_ldisc(o_tty,N_TTY);
ian@0 2419 }
ian@0 2420 /*
ian@0 2421 * The release_mem function takes care of the details of clearing
ian@0 2422 * the slots and preserving the termios structure.
ian@0 2423 */
ian@0 2424 release_mem(tty, idx);
ian@0 2425
ian@0 2426 #ifdef CONFIG_UNIX98_PTYS
ian@0 2427 /* Make this pty number available for reallocation */
ian@0 2428 if (devpts) {
ian@0 2429 down(&allocated_ptys_lock);
ian@0 2430 idr_remove(&allocated_ptys, idx);
ian@0 2431 up(&allocated_ptys_lock);
ian@0 2432 }
ian@0 2433 #endif
ian@0 2434
ian@0 2435 }
ian@0 2436
ian@0 2437 /**
ian@0 2438 * tty_open - open a tty device
ian@0 2439 * @inode: inode of device file
ian@0 2440 * @filp: file pointer to tty
ian@0 2441 *
ian@0 2442 * tty_open and tty_release keep up the tty count that contains the
ian@0 2443 * number of opens done on a tty. We cannot use the inode-count, as
ian@0 2444 * different inodes might point to the same tty.
ian@0 2445 *
ian@0 2446 * Open-counting is needed for pty masters, as well as for keeping
ian@0 2447 * track of serial lines: DTR is dropped when the last close happens.
ian@0 2448 * (This is not done solely through tty->count, now. - Ted 1/27/92)
ian@0 2449 *
ian@0 2450 * The termios state of a pty is reset on first open so that
ian@0 2451 * settings don't persist across reuse.
ian@0 2452 *
ian@0 2453 * Locking: tty_mutex protects current->signal->tty, get_tty_driver and
ian@0 2454 * init_dev work. tty->count should protect the rest.
ian@0 2455 * task_lock is held to update task details for sessions
ian@0 2456 */
ian@0 2457
ian@0 2458 static int tty_open(struct inode * inode, struct file * filp)
ian@0 2459 {
ian@0 2460 struct tty_struct *tty;
ian@0 2461 int noctty, retval;
ian@0 2462 struct tty_driver *driver;
ian@0 2463 int index;
ian@0 2464 dev_t device = inode->i_rdev;
ian@0 2465 unsigned short saved_flags = filp->f_flags;
ian@0 2466
ian@0 2467 nonseekable_open(inode, filp);
ian@0 2468
ian@0 2469 retry_open:
ian@0 2470 noctty = filp->f_flags & O_NOCTTY;
ian@0 2471 index = -1;
ian@0 2472 retval = 0;
ian@0 2473
ian@0 2474 mutex_lock(&tty_mutex);
ian@0 2475
ian@0 2476 if (device == MKDEV(TTYAUX_MAJOR,0)) {
ian@0 2477 if (!current->signal->tty) {
ian@0 2478 mutex_unlock(&tty_mutex);
ian@0 2479 return -ENXIO;
ian@0 2480 }
ian@0 2481 driver = current->signal->tty->driver;
ian@0 2482 index = current->signal->tty->index;
ian@0 2483 filp->f_flags |= O_NONBLOCK; /* Don't let /dev/tty block */
ian@0 2484 /* noctty = 1; */
ian@0 2485 goto got_driver;
ian@0 2486 }
ian@0 2487 #ifdef CONFIG_VT
ian@26 2488 if (console_use_vt && (device == MKDEV(TTY_MAJOR,0))) {
ian@0 2489 extern struct tty_driver *console_driver;
ian@0 2490 driver = console_driver;
ian@0 2491 index = fg_console;
ian@0 2492 noctty = 1;
ian@0 2493 goto got_driver;
ian@0 2494 }
ian@0 2495 #endif
ian@0 2496 if (device == MKDEV(TTYAUX_MAJOR,1)) {
ian@0 2497 driver = console_device(&index);
ian@0 2498 if (driver) {
ian@0 2499 /* Don't let /dev/console block */
ian@0 2500 filp->f_flags |= O_NONBLOCK;
ian@0 2501 noctty = 1;
ian@0 2502 goto got_driver;
ian@0 2503 }
ian@0 2504 mutex_unlock(&tty_mutex);
ian@0 2505 return -ENODEV;
ian@0 2506 }
ian@0 2507
ian@0 2508 driver = get_tty_driver(device, &index);
ian@0 2509 if (!driver) {
ian@0 2510 mutex_unlock(&tty_mutex);
ian@0 2511 return -ENODEV;
ian@0 2512 }
ian@0 2513 got_driver:
ian@0 2514 retval = init_dev(driver, index, &tty);
ian@0 2515 mutex_unlock(&tty_mutex);
ian@0 2516 if (retval)
ian@0 2517 return retval;
ian@0 2518
ian@0 2519 filp->private_data = tty;
ian@0 2520 file_move(filp, &tty->tty_files);
ian@0 2521 check_tty_count(tty, "tty_open");
ian@0 2522 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
ian@0 2523 tty->driver->subtype == PTY_TYPE_MASTER)
ian@0 2524 noctty = 1;
ian@0 2525 #ifdef TTY_DEBUG_HANGUP
ian@0 2526 printk(KERN_DEBUG "opening %s...", tty->name);
ian@0 2527 #endif
ian@0 2528 if (!retval) {
ian@0 2529 if (tty->driver->open)
ian@0 2530 retval = tty->driver->open(tty, filp);
ian@0 2531 else
ian@0 2532 retval = -ENODEV;
ian@0 2533 }
ian@0 2534 filp->f_flags = saved_flags;
ian@0 2535
ian@0 2536 if (!retval && test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
ian@0 2537 retval = -EBUSY;
ian@0 2538
ian@0 2539 if (retval) {
ian@0 2540 #ifdef TTY_DEBUG_HANGUP
ian@0 2541 printk(KERN_DEBUG "error %d in opening %s...", retval,
ian@0 2542 tty->name);
ian@0 2543 #endif
ian@0 2544 release_dev(filp);
ian@0 2545 if (retval != -ERESTARTSYS)
ian@0 2546 return retval;
ian@0 2547 if (signal_pending(current))
ian@0 2548 return retval;
ian@0 2549 schedule();
ian@0 2550 /*
ian@0 2551 * Need to reset f_op in case a hangup happened.
ian@0 2552 */
ian@0 2553 if (filp->f_op == &hung_up_tty_fops)
ian@0 2554 filp->f_op = &tty_fops;
ian@0 2555 goto retry_open;
ian@0 2556 }
ian@0 2557 if (!noctty &&
ian@0 2558 current->signal->leader &&
ian@0 2559 !current->signal->tty &&
ian@0 2560 tty->session == 0) {
ian@0 2561 task_lock(current);
ian@0 2562 current->signal->tty = tty;
ian@0 2563 task_unlock(current);
ian@0 2564 current->signal->tty_old_pgrp = 0;
ian@0 2565 tty->session = current->signal->session;
ian@0 2566 tty->pgrp = process_group(current);
ian@0 2567 }
ian@0 2568 return 0;
ian@0 2569 }
ian@0 2570
ian@0 2571 #ifdef CONFIG_UNIX98_PTYS
ian@0 2572 /**
ian@0 2573 * ptmx_open - open a unix 98 pty master
ian@0 2574 * @inode: inode of device file
ian@0 2575 * @filp: file pointer to tty
ian@0 2576 *
ian@0 2577 * Allocate a unix98 pty master device from the ptmx driver.
ian@0 2578 *
ian@0 2579 * Locking: tty_mutex protects theinit_dev work. tty->count should
ian@0 2580 protect the rest.
ian@0 2581 * allocated_ptys_lock handles the list of free pty numbers
ian@0 2582 */
ian@0 2583
ian@0 2584 static int ptmx_open(struct inode * inode, struct file * filp)
ian@0 2585 {
ian@0 2586 struct tty_struct *tty;
ian@0 2587 int retval;
ian@0 2588 int index;
ian@0 2589 int idr_ret;
ian@0 2590
ian@0 2591 nonseekable_open(inode, filp);
ian@0 2592
ian@0 2593 /* find a device that is not in use. */
ian@0 2594 down(&allocated_ptys_lock);
ian@0 2595 if (!idr_pre_get(&allocated_ptys, GFP_KERNEL)) {
ian@0 2596 up(&allocated_ptys_lock);
ian@0 2597 return -ENOMEM;
ian@0 2598 }
ian@0 2599 idr_ret = idr_get_new(&allocated_ptys, NULL, &index);
ian@0 2600 if (idr_ret < 0) {
ian@0 2601 up(&allocated_ptys_lock);
ian@0 2602 if (idr_ret == -EAGAIN)
ian@0 2603 return -ENOMEM;
ian@0 2604 return -EIO;
ian@0 2605 }
ian@0 2606 if (index >= pty_limit) {
ian@0 2607 idr_remove(&allocated_ptys, index);
ian@0 2608 up(&allocated_ptys_lock);
ian@0 2609 return -EIO;
ian@0 2610 }
ian@0 2611 up(&allocated_ptys_lock);
ian@0 2612
ian@0 2613 mutex_lock(&tty_mutex);
ian@0 2614 retval = init_dev(ptm_driver, index, &tty);
ian@0 2615 mutex_unlock(&tty_mutex);
ian@0 2616
ian@0 2617 if (retval)
ian@0 2618 goto out;
ian@0 2619
ian@0 2620 set_bit(TTY_PTY_LOCK, &tty->flags); /* LOCK THE SLAVE */
ian@0 2621 filp->private_data = tty;
ian@0 2622 file_move(filp, &tty->tty_files);
ian@0 2623
ian@0 2624 retval = -ENOMEM;
ian@0 2625 if (devpts_pty_new(tty->link))
ian@0 2626 goto out1;
ian@0 2627
ian@0 2628 check_tty_count(tty, "tty_open");
ian@0 2629 retval = ptm_driver->open(tty, filp);
ian@0 2630 if (!retval)
ian@0 2631 return 0;
ian@0 2632 out1:
ian@0 2633 release_dev(filp);
ian@0 2634 return retval;
ian@0 2635 out:
ian@0 2636 down(&allocated_ptys_lock);
ian@0 2637 idr_remove(&allocated_ptys, index);
ian@0 2638 up(&allocated_ptys_lock);
ian@0 2639 return retval;
ian@0 2640 }
ian@0 2641 #endif
ian@0 2642
ian@0 2643 /**
ian@0 2644 * tty_release - vfs callback for close
ian@0 2645 * @inode: inode of tty
ian@0 2646 * @filp: file pointer for handle to tty
ian@0 2647 *
ian@0 2648 * Called the last time each file handle is closed that references
ian@0 2649 * this tty. There may however be several such references.
ian@0 2650 *
ian@0 2651 * Locking:
ian@0 2652 * Takes bkl. See release_dev
ian@0 2653 */
ian@0 2654
ian@0 2655 static int tty_release(struct inode * inode, struct file * filp)
ian@0 2656 {
ian@0 2657 lock_kernel();
ian@0 2658 release_dev(filp);
ian@0 2659 unlock_kernel();
ian@0 2660 return 0;
ian@0 2661 }
ian@0 2662
ian@0 2663 /**
ian@0 2664 * tty_poll - check tty status
ian@0 2665 * @filp: file being polled
ian@0 2666 * @wait: poll wait structures to update
ian@0 2667 *
ian@0 2668 * Call the line discipline polling method to obtain the poll
ian@0 2669 * status of the device.
ian@0 2670 *
ian@0 2671 * Locking: locks called line discipline but ldisc poll method
ian@0 2672 * may be re-entered freely by other callers.
ian@0 2673 */
ian@0 2674
ian@0 2675 static unsigned int tty_poll(struct file * filp, poll_table * wait)
ian@0 2676 {
ian@0 2677 struct tty_struct * tty;
ian@0 2678 struct tty_ldisc *ld;
ian@0 2679 int ret = 0;
ian@0 2680
ian@0 2681 tty = (struct tty_struct *)filp->private_data;
ian@0 2682 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_poll"))
ian@0 2683 return 0;
ian@0 2684
ian@0 2685 ld = tty_ldisc_ref_wait(tty);
ian@0 2686 if (ld->poll)
ian@0 2687 ret = (ld->poll)(tty, filp, wait);
ian@0 2688 tty_ldisc_deref(ld);
ian@0 2689 return ret;
ian@0 2690 }
ian@0 2691
ian@0 2692 static int tty_fasync(int fd, struct file * filp, int on)
ian@0 2693 {
ian@0 2694 struct tty_struct * tty;
ian@0 2695 int retval;
ian@0 2696
ian@0 2697 tty = (struct tty_struct *)filp->private_data;
ian@0 2698 if (tty_paranoia_check(tty, filp->f_dentry->d_inode, "tty_fasync"))
ian@0 2699 return 0;
ian@0 2700
ian@0 2701 retval = fasync_helper(fd, filp, on, &tty->fasync);
ian@0 2702 if (retval <= 0)
ian@0 2703 return retval;
ian@0 2704
ian@0 2705 if (on) {
ian@0 2706 if (!waitqueue_active(&tty->read_wait))
ian@0 2707 tty->minimum_to_wake = 1;
ian@0 2708 retval = f_setown(filp, (-tty->pgrp) ? : current->pid, 0);
ian@0 2709 if (retval)
ian@0 2710 return retval;
ian@0 2711 } else {
ian@0 2712 if (!tty->fasync && !waitqueue_active(&tty->read_wait))
ian@0 2713 tty->minimum_to_wake = N_TTY_BUF_SIZE;
ian@0 2714 }
ian@0 2715 return 0;
ian@0 2716 }
ian@0 2717
ian@0 2718 /**
ian@0 2719 * tiocsti - fake input character
ian@0 2720 * @tty: tty to fake input into
ian@0 2721 * @p: pointer to character
ian@0 2722 *
ian@0 2723 * Fake input to a tty device. Does the neccessary locking and
ian@0 2724 * input management.
ian@0 2725 *
ian@0 2726 * FIXME: does not honour flow control ??
ian@0 2727 *
ian@0 2728 * Locking:
ian@0 2729 * Called functions take tty_ldisc_lock
ian@0 2730 * current->signal->tty check is safe without locks
ian@0 2731 */
ian@0 2732
ian@0 2733 static int tiocsti(struct tty_struct *tty, char __user *p)
ian@0 2734 {
ian@0 2735 char ch, mbz = 0;
ian@0 2736 struct tty_ldisc *ld;
ian@0 2737
ian@0 2738 if ((current->signal->tty != tty) && !capable(CAP_SYS_ADMIN))
ian@0 2739 return -EPERM;
ian@0 2740 if (get_user(ch, p))
ian@0 2741 return -EFAULT;
ian@0 2742 ld = tty_ldisc_ref_wait(tty);
ian@0 2743 ld->receive_buf(tty, &ch, &mbz, 1);
ian@0 2744 tty_ldisc_deref(ld);
ian@0 2745 return 0;
ian@0 2746 }
ian@0 2747
ian@0 2748 /**
ian@0 2749 * tiocgwinsz - implement window query ioctl
ian@0 2750 * @tty; tty
ian@0 2751 * @arg: user buffer for result
ian@0 2752 *
ian@0 2753 * Copies the kernel idea of the window size into the user buffer. No
ian@0 2754 * locking is done.
ian@0 2755 *
ian@0 2756 * FIXME: Returning random values racing a window size set is wrong
ian@0 2757 * should lock here against that
ian@0 2758 */
ian@0 2759
ian@0 2760 static int tiocgwinsz(struct tty_struct *tty, struct winsize __user * arg)
ian@0 2761 {
ian@0 2762 if (copy_to_user(arg, &tty->winsize, sizeof(*arg)))
ian@0 2763 return -EFAULT;
ian@0 2764 return 0;
ian@0 2765 }
ian@0 2766
ian@0 2767 /**
ian@0 2768 * tiocswinsz - implement window size set ioctl
ian@0 2769 * @tty; tty
ian@0 2770 * @arg: user buffer for result
ian@0 2771 *
ian@0 2772 * Copies the user idea of the window size to the kernel. Traditionally
ian@0 2773 * this is just advisory information but for the Linux console it
ian@0 2774 * actually has driver level meaning and triggers a VC resize.
ian@0 2775 *
ian@0 2776 * Locking:
ian@0 2777 * The console_sem is used to ensure we do not try and resize
ian@0 2778 * the console twice at once.
ian@0 2779 * FIXME: Two racing size sets may leave the console and kernel
ian@0 2780 * parameters disagreeing. Is this exploitable ?
ian@0 2781 * FIXME: Random values racing a window size get is wrong
ian@0 2782 * should lock here against that
ian@0 2783 */
ian@0 2784
ian@0 2785 static int tiocswinsz(struct tty_struct *tty, struct tty_struct *real_tty,
ian@0 2786 struct winsize __user * arg)
ian@0 2787 {
ian@0 2788 struct winsize tmp_ws;
ian@0 2789
ian@0 2790 if (copy_from_user(&tmp_ws, arg, sizeof(*arg)))
ian@0 2791 return -EFAULT;
ian@0 2792 if (!memcmp(&tmp_ws, &tty->winsize, sizeof(*arg)))
ian@0 2793 return 0;
ian@0 2794 #ifdef CONFIG_VT
ian@0 2795 if (tty->driver->type == TTY_DRIVER_TYPE_CONSOLE) {
ian@0 2796 int rc;
ian@0 2797
ian@0 2798 acquire_console_sem();
ian@0 2799 rc = vc_resize(tty->driver_data, tmp_ws.ws_col, tmp_ws.ws_row);
ian@0 2800 release_console_sem();
ian@0 2801 if (rc)
ian@0 2802 return -ENXIO;
ian@0 2803 }
ian@0 2804 #endif
ian@0 2805 if (tty->pgrp > 0)
ian@0 2806 kill_pg(tty->pgrp, SIGWINCH, 1);
ian@0 2807 if ((real_tty->pgrp != tty->pgrp) && (real_tty->pgrp > 0))
ian@0 2808 kill_pg(real_tty->pgrp, SIGWINCH, 1);
ian@0 2809 tty->winsize = tmp_ws;
ian@0 2810 real_tty->winsize = tmp_ws;
ian@0 2811 return 0;
ian@0 2812 }
ian@0 2813
ian@0 2814 /**
ian@0 2815 * tioccons - allow admin to move logical console
ian@0 2816 * @file: the file to become console
ian@0 2817 *
ian@0 2818 * Allow the adminstrator to move the redirected console device
ian@0 2819 *
ian@0 2820 * Locking: uses redirect_lock to guard the redirect information
ian@0 2821 */
ian@0 2822
ian@0 2823 static int tioccons(struct file *file)
ian@0 2824 {
ian@0 2825 if (!capable(CAP_SYS_ADMIN))
ian@0 2826 return -EPERM;
ian@0 2827 if (file->f_op->write == redirected_tty_write) {
ian@0 2828 struct file *f;
ian@0 2829 spin_lock(&redirect_lock);
ian@0 2830 f = redirect;
ian@0 2831 redirect = NULL;
ian@0 2832 spin_unlock(&redirect_lock);
ian@0 2833 if (f)
ian@0 2834 fput(f);
ian@0 2835 return 0;
ian@0 2836 }
ian@0 2837 spin_lock(&redirect_lock);
ian@0 2838 if (redirect) {
ian@0 2839 spin_unlock(&redirect_lock);
ian@0 2840 return -EBUSY;
ian@0 2841 }
ian@0 2842 get_file(file);
ian@0 2843 redirect = file;
ian@0 2844 spin_unlock(&redirect_lock);
ian@0 2845 return 0;
ian@0 2846 }
ian@0 2847
ian@0 2848 /**
ian@0 2849 * fionbio - non blocking ioctl
ian@0 2850 * @file: file to set blocking value
ian@0 2851 * @p: user parameter
ian@0 2852 *
ian@0 2853 * Historical tty interfaces had a blocking control ioctl before
ian@0 2854 * the generic functionality existed. This piece of history is preserved
ian@0 2855 * in the expected tty API of posix OS's.
ian@0 2856 *
ian@0 2857 * Locking: none, the open fle handle ensures it won't go away.
ian@0 2858 */
ian@0 2859
ian@0 2860 static int fionbio(struct file *file, int __user *p)
ian@0 2861 {
ian@0 2862 int nonblock;
ian@0 2863
ian@0 2864 if (get_user(nonblock, p))
ian@0 2865 return -EFAULT;
ian@0 2866
ian@0 2867 if (nonblock)
ian@0 2868 file->f_flags |= O_NONBLOCK;
ian@0 2869 else
ian@0 2870 file->f_flags &= ~O_NONBLOCK;
ian@0 2871 return 0;
ian@0 2872 }
ian@0 2873
ian@0 2874 /**
ian@0 2875 * tiocsctty - set controlling tty
ian@0 2876 * @tty: tty structure
ian@0 2877 * @arg: user argument
ian@0 2878 *
ian@0 2879 * This ioctl is used to manage job control. It permits a session
ian@0 2880 * leader to set this tty as the controlling tty for the session.
ian@0 2881 *
ian@0 2882 * Locking:
ian@0 2883 * Takes tasklist lock internally to walk sessions
ian@0 2884 * Takes task_lock() when updating signal->tty
ian@0 2885 *
ian@0 2886 * FIXME: tty_mutex is needed to protect signal->tty references.
ian@0 2887 * FIXME: why task_lock on the signal->tty reference ??
ian@0 2888 *
ian@0 2889 */
ian@0 2890
ian@0 2891 static int tiocsctty(struct tty_struct *tty, int arg)
ian@0 2892 {
ian@0 2893 struct task_struct *p;
ian@0 2894
ian@0 2895 if (current->signal->leader &&
ian@0 2896 (current->signal->session == tty->session))
ian@0 2897 return 0;
ian@0 2898 /*
ian@0 2899 * The process must be a session leader and
ian@0 2900 * not have a controlling tty already.
ian@0 2901 */
ian@0 2902 if (!current->signal->leader || current->signal->tty)
ian@0 2903 return -EPERM;
ian@0 2904 if (tty->session > 0) {
ian@0 2905 /*
ian@0 2906 * This tty is already the controlling
ian@0 2907 * tty for another session group!
ian@0 2908 */
ian@0 2909 if ((arg == 1) && capable(CAP_SYS_ADMIN)) {
ian@0 2910 /*
ian@0 2911 * Steal it away
ian@0 2912 */
ian@0 2913
ian@0 2914 read_lock(&tasklist_lock);
ian@0 2915 do_each_task_pid(tty->session, PIDTYPE_SID, p) {
ian@0 2916 p->signal->tty = NULL;
ian@0 2917 } while_each_task_pid(tty->session, PIDTYPE_SID, p);
ian@0 2918 read_unlock(&tasklist_lock);
ian@0 2919 } else
ian@0 2920 return -EPERM;
ian@0 2921 }
ian@0 2922 task_lock(current);
ian@0 2923 current->signal->tty = tty;
ian@0 2924 task_unlock(current);
ian@0 2925 current->signal->tty_old_pgrp = 0;
ian@0 2926 tty->session = current->signal->session;
ian@0 2927 tty->pgrp = process_group(current);
ian@0 2928 return 0;
ian@0 2929 }
ian@0 2930
ian@0 2931 /**
ian@0 2932 * tiocgpgrp - get process group
ian@0 2933 * @tty: tty passed by user
ian@0 2934 * @real_tty: tty side of the tty pased by the user if a pty else the tty
ian@0 2935 * @p: returned pid
ian@0 2936 *
ian@0 2937 * Obtain the process group of the tty. If there is no process group
ian@0 2938 * return an error.
ian@0 2939 *
ian@0 2940 * Locking: none. Reference to ->signal->tty is safe.
ian@0 2941 */
ian@0 2942
ian@0 2943 static int tiocgpgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
ian@0 2944 {
ian@0 2945 /*
ian@0 2946 * (tty == real_tty) is a cheap way of
ian@0 2947 * testing if the tty is NOT a master pty.
ian@0 2948 */
ian@0 2949 if (tty == real_tty && current->signal->tty != real_tty)
ian@0 2950 return -ENOTTY;
ian@0 2951 return put_user(real_tty->pgrp, p);
ian@0 2952 }
ian@0 2953
ian@0 2954 /**
ian@0 2955 * tiocspgrp - attempt to set process group
ian@0 2956 * @tty: tty passed by user
ian@0 2957 * @real_tty: tty side device matching tty passed by user
ian@0 2958 * @p: pid pointer
ian@0 2959 *
ian@0 2960 * Set the process group of the tty to the session passed. Only
ian@0 2961 * permitted where the tty session is our session.
ian@0 2962 *
ian@0 2963 * Locking: None
ian@0 2964 *
ian@0 2965 * FIXME: current->signal->tty referencing is unsafe.
ian@0 2966 */
ian@0 2967
ian@0 2968 static int tiocspgrp(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
ian@0 2969 {
ian@0 2970 pid_t pgrp;
ian@0 2971 int retval = tty_check_change(real_tty);
ian@0 2972
ian@0 2973 if (retval == -EIO)
ian@0 2974 return -ENOTTY;
ian@0 2975 if (retval)
ian@0 2976 return retval;
ian@0 2977 if (!current->signal->tty ||
ian@0 2978 (current->signal->tty != real_tty) ||
ian@0 2979 (real_tty->session != current->signal->session))
ian@0 2980 return -ENOTTY;
ian@0 2981 if (get_user(pgrp, p))
ian@0 2982 return -EFAULT;
ian@0 2983 if (pgrp < 0)
ian@0 2984 return -EINVAL;
ian@0 2985 if (session_of_pgrp(pgrp) != current->signal->session)
ian@0 2986 return -EPERM;
ian@0 2987 real_tty->pgrp = pgrp;
ian@0 2988 return 0;
ian@0 2989 }
ian@0 2990
ian@0 2991 /**
ian@0 2992 * tiocgsid - get session id
ian@0 2993 * @tty: tty passed by user
ian@0 2994 * @real_tty: tty side of the tty pased by the user if a pty else the tty
ian@0 2995 * @p: pointer to returned session id
ian@0 2996 *
ian@0 2997 * Obtain the session id of the tty. If there is no session
ian@0 2998 * return an error.
ian@0 2999 *
ian@0 3000 * Locking: none. Reference to ->signal->tty is safe.
ian@0 3001 */
ian@0 3002
ian@0 3003 static int tiocgsid(struct tty_struct *tty, struct tty_struct *real_tty, pid_t __user *p)
ian@0 3004 {
ian@0 3005 /*
ian@0 3006 * (tty == real_tty) is a cheap way of
ian@0 3007 * testing if the tty is NOT a master pty.
ian@0 3008 */
ian@0 3009 if (tty == real_tty && current->signal->tty != real_tty)
ian@0 3010 return -ENOTTY;
ian@0 3011 if (real_tty->session <= 0)
ian@0 3012 return -ENOTTY;
ian@0 3013 return put_user(real_tty->session, p);
ian@0 3014 }
ian@0 3015
ian@0 3016 /**
ian@0 3017 * tiocsetd - set line discipline
ian@0 3018 * @tty: tty device
ian@0 3019 * @p: pointer to user data
ian@0 3020 *
ian@0 3021 * Set the line discipline according to user request.
ian@0 3022 *
ian@0 3023 * Locking: see tty_set_ldisc, this function is just a helper
ian@0 3024 */
ian@0 3025
ian@0 3026 static int tiocsetd(struct tty_struct *tty, int __user *p)
ian@0 3027 {
ian@0 3028 int ldisc;
ian@0 3029
ian@0 3030 if (get_user(ldisc, p))
ian@0 3031 return -EFAULT;
ian@0 3032 return tty_set_ldisc(tty, ldisc);
ian@0 3033 }
ian@0 3034
ian@0 3035 /**
ian@0 3036 * send_break - performed time break
ian@0 3037 * @tty: device to break on
ian@0 3038 * @duration: timeout in mS
ian@0 3039 *
ian@0 3040 * Perform a timed break on hardware that lacks its own driver level
ian@0 3041 * timed break functionality.
ian@0 3042 *
ian@0 3043 * Locking:
ian@0 3044 * None
ian@0 3045 *
ian@0 3046 * FIXME:
ian@0 3047 * What if two overlap
ian@0 3048 */
ian@0 3049
ian@0 3050 static int send_break(struct tty_struct *tty, unsigned int duration)
ian@0 3051 {
ian@0 3052 tty->driver->break_ctl(tty, -1);
ian@0 3053 if (!signal_pending(current)) {
ian@0 3054 msleep_interruptible(duration);
ian@0 3055 }
ian@0 3056 tty->driver->break_ctl(tty, 0);
ian@0 3057 if (signal_pending(current))
ian@0 3058 return -EINTR;
ian@0 3059 return 0;
ian@0 3060 }
ian@0 3061
ian@0 3062 /**
ian@0 3063 * tiocmget - get modem status
ian@0 3064 * @tty: tty device
ian@0 3065 * @file: user file pointer
ian@0 3066 * @p: pointer to result
ian@0 3067 *
ian@0 3068 * Obtain the modem status bits from the tty driver if the feature
ian@0 3069 * is supported. Return -EINVAL if it is not available.
ian@0 3070 *
ian@0 3071 * Locking: none (up to the driver)
ian@0 3072 */
ian@0 3073
ian@0 3074 static int tty_tiocmget(struct tty_struct *tty, struct file *file, int __user *p)
ian@0 3075 {
ian@0 3076 int retval = -EINVAL;
ian@0 3077
ian@0 3078 if (tty->driver->tiocmget) {
ian@0 3079 retval = tty->driver->tiocmget(tty, file);
ian@0 3080
ian@0 3081 if (retval >= 0)
ian@0 3082 retval = put_user(retval, p);
ian@0 3083 }
ian@0 3084 return retval;
ian@0 3085 }
ian@0 3086
ian@0 3087 /**
ian@0 3088 * tiocmset - set modem status
ian@0 3089 * @tty: tty device
ian@0 3090 * @file: user file pointer
ian@0 3091 * @cmd: command - clear bits, set bits or set all
ian@0 3092 * @p: pointer to desired bits
ian@0 3093 *
ian@0 3094 * Set the modem status bits from the tty driver if the feature
ian@0 3095 * is supported. Return -EINVAL if it is not available.
ian@0 3096 *
ian@0 3097 * Locking: none (up to the driver)
ian@0 3098 */
ian@0 3099
ian@0 3100 static int tty_tiocmset(struct tty_struct *tty, struct file *file, unsigned int cmd,
ian@0 3101 unsigned __user *p)
ian@0 3102 {
ian@0 3103 int retval = -EINVAL;
ian@0 3104
ian@0 3105 if (tty->driver->tiocmset) {
ian@0 3106 unsigned int set, clear, val;
ian@0 3107
ian@0 3108 retval = get_user(val, p);
ian@0 3109 if (retval)
ian@0 3110 return retval;
ian@0 3111
ian@0 3112 set = clear = 0;
ian@0 3113 switch (cmd) {
ian@0 3114 case TIOCMBIS:
ian@0 3115 set = val;
ian@0 3116 break;
ian@0 3117 case TIOCMBIC:
ian@0 3118 clear = val;
ian@0 3119 break;
ian@0 3120 case TIOCMSET:
ian@0 3121 set = val;
ian@0 3122 clear = ~val;
ian@0 3123 break;
ian@0 3124 }
ian@0 3125
ian@0 3126 set &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
ian@0 3127 clear &= TIOCM_DTR|TIOCM_RTS|TIOCM_OUT1|TIOCM_OUT2|TIOCM_LOOP;
ian@0 3128
ian@0 3129 retval = tty->driver->tiocmset(tty, file, set, clear);
ian@0 3130 }
ian@0 3131 return retval;
ian@0 3132 }
ian@0 3133
ian@0 3134 /*
ian@0 3135 * Split this up, as gcc can choke on it otherwise..
ian@0 3136 */
ian@0 3137 int tty_ioctl(struct inode * inode, struct file * file,
ian@0 3138 unsigned int cmd, unsigned long arg)
ian@0 3139 {
ian@0 3140 struct tty_struct *tty, *real_tty;
ian@0 3141 void __user *p = (void __user *)arg;
ian@0 3142 int retval;
ian@0 3143 struct tty_ldisc *ld;
ian@0 3144
ian@0 3145 tty = (struct tty_struct *)file->private_data;
ian@0 3146 if (tty_paranoia_check(tty, inode, "tty_ioctl"))
ian@0 3147 return -EINVAL;
ian@0 3148
ian@0 3149 real_tty = tty;
ian@0 3150 if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
ian@0 3151 tty->driver->subtype == PTY_TYPE_MASTER)
ian@0 3152 real_tty = tty->link;
ian@0 3153
ian@0 3154 /*
ian@0 3155 * Break handling by driver
ian@0 3156 */
ian@0 3157 if (!tty->driver->break_ctl) {
ian@0 3158 switch(cmd) {
ian@0 3159 case TIOCSBRK:
ian@0 3160 case TIOCCBRK:
ian@0 3161 if (tty->driver->ioctl)
ian@0 3162 return tty->driver->ioctl(tty, file, cmd, arg);
ian@0 3163 return -EINVAL;
ian@0 3164
ian@0 3165 /* These two ioctl's always return success; even if */
ian@0 3166 /* the driver doesn't support them. */
ian@0 3167 case TCSBRK:
ian@0 3168 case TCSBRKP:
ian@0 3169 if (!tty->driver->ioctl)
ian@0 3170 return 0;
ian@0 3171 retval = tty->driver->ioctl(tty, file, cmd, arg);
ian@0 3172 if (retval == -ENOIOCTLCMD)
ian@0 3173 retval = 0;
ian@0 3174 return retval;
ian@0 3175 }
ian@0 3176 }
ian@0 3177
ian@0 3178 /*
ian@0 3179 * Factor out some common prep work
ian@0 3180 */
ian@0 3181 switch (cmd) {
ian@0 3182 case TIOCSETD:
ian@0 3183 case TIOCSBRK:
ian@0 3184 case TIOCCBRK:
ian@0 3185 case TCSBRK:
ian@0 3186 case TCSBRKP:
ian@0 3187 retval = tty_check_change(tty);
ian@0 3188 if (retval)
ian@0 3189 return retval;
ian@0 3190 if (cmd != TIOCCBRK) {
ian@0 3191 tty_wait_until_sent(tty, 0);
ian@0 3192 if (signal_pending(current))
ian@0 3193 return -EINTR;
ian@0 3194 }
ian@0 3195 break;
ian@0 3196 }
ian@0 3197
ian@0 3198 switch (cmd) {
ian@0 3199 case TIOCSTI:
ian@0 3200 return tiocsti(tty, p);
ian@0 3201 case TIOCGWINSZ:
ian@0 3202 return tiocgwinsz(tty, p);
ian@0 3203 case TIOCSWINSZ:
ian@0 3204 return tiocswinsz(tty, real_tty, p);
ian@0 3205 case TIOCCONS:
ian@0 3206 return real_tty!=tty ? -EINVAL : tioccons(file);
ian@0 3207 case FIONBIO:
ian@0 3208 return fionbio(file, p);
ian@0 3209 case TIOCEXCL:
ian@0 3210 set_bit(TTY_EXCLUSIVE, &tty->flags);
ian@0 3211 return 0;
ian@0 3212 case TIOCNXCL:
ian@0 3213 clear_bit(TTY_EXCLUSIVE, &tty->flags);
ian@0 3214 return 0;
ian@0 3215 case TIOCNOTTY:
ian@0 3216 /* FIXME: taks lock or tty_mutex ? */
ian@0 3217 if (current->signal->tty != tty)
ian@0 3218 return -ENOTTY;
ian@0 3219 if (current->signal->leader)
ian@0 3220 disassociate_ctty(0);
ian@0 3221 task_lock(current);
ian@0 3222 current->signal->tty = NULL;
ian@0 3223 task_unlock(current);
ian@0 3224 return 0;
ian@0 3225 case TIOCSCTTY:
ian@0 3226 return tiocsctty(tty, arg);
ian@0 3227 case TIOCGPGRP:
ian@0 3228 return tiocgpgrp(tty, real_tty, p);
ian@0 3229 case TIOCSPGRP:
ian@0 3230 return tiocspgrp(tty, real_tty, p);
ian@0 3231 case TIOCGSID:
ian@0 3232 return tiocgsid(tty, real_tty, p);
ian@0 3233 case TIOCGETD:
ian@0 3234 /* FIXME: check this is ok */
ian@0 3235 return put_user(tty->ldisc.num, (int __user *)p);
ian@0 3236 case TIOCSETD:
ian@0 3237 return tiocsetd(tty, p);
ian@0 3238 #ifdef CONFIG_VT
ian@0 3239 case TIOCLINUX:
ian@0 3240 return tioclinux(tty, arg);
ian@0 3241 #endif
ian@0 3242 /*
ian@0 3243 * Break handling
ian@0 3244 */
ian@0 3245 case TIOCSBRK: /* Turn break on, unconditionally */
ian@0 3246 tty->driver->break_ctl(tty, -1);
ian@0 3247 return 0;
ian@0 3248
ian@0 3249 case TIOCCBRK: /* Turn break off, unconditionally */
ian@0 3250 tty->driver->break_ctl(tty, 0);
ian@0 3251 return 0;
ian@0 3252 case TCSBRK: /* SVID version: non-zero arg --> no break */
ian@0 3253 /* non-zero arg means wait for all output data
ian@0 3254 * to be sent (performed above) but don't send break.
ian@0 3255 * This is used by the tcdrain() termios function.
ian@0 3256 */
ian@0 3257 if (!arg)
ian@0 3258 return send_break(tty, 250);
ian@0 3259 return 0;
ian@0 3260 case TCSBRKP: /* support for POSIX tcsendbreak() */
ian@0 3261 return send_break(tty, arg ? arg*100 : 250);
ian@0 3262
ian@0 3263 case TIOCMGET:
ian@0 3264 return tty_tiocmget(tty, file, p);
ian@0 3265
ian@0 3266 case TIOCMSET:
ian@0 3267 case TIOCMBIC:
ian@0 3268 case TIOCMBIS:
ian@0 3269 return tty_tiocmset(tty, file, cmd, p);
ian@0 3270 }
ian@0 3271 if (tty->driver->ioctl) {
ian@0 3272 retval = (tty->driver->ioctl)(tty, file, cmd, arg);
ian@0 3273 if (retval != -ENOIOCTLCMD)
ian@0 3274 return retval;
ian@0 3275 }
ian@0 3276 ld = tty_ldisc_ref_wait(tty);
ian@0 3277 retval = -EINVAL;
ian@0 3278 if (ld->ioctl) {
ian@0 3279 retval = ld->ioctl(tty, file, cmd, arg);
ian@0 3280 if (retval == -ENOIOCTLCMD)
ian@0 3281 retval = -EINVAL;
ian@0 3282 }
ian@0 3283 tty_ldisc_deref(ld);
ian@0 3284 return retval;
ian@0 3285 }
ian@0 3286
ian@0 3287
ian@0 3288 /*
ian@0 3289 * This implements the "Secure Attention Key" --- the idea is to
ian@0 3290 * prevent trojan horses by killing all processes associated with this
ian@0 3291 * tty when the user hits the "Secure Attention Key". Required for
ian@0 3292 * super-paranoid applications --- see the Orange Book for more details.
ian@0 3293 *
ian@0 3294 * This code could be nicer; ideally it should send a HUP, wait a few
ian@0 3295 * seconds, then send a INT, and then a KILL signal. But you then
ian@0 3296 * have to coordinate with the init process, since all processes associated
ian@0 3297 * with the current tty must be dead before the new getty is allowed
ian@0 3298 * to spawn.
ian@0 3299 *
ian@0 3300 * Now, if it would be correct ;-/ The current code has a nasty hole -
ian@0 3301 * it doesn't catch files in flight. We may send the descriptor to ourselves
ian@0 3302 * via AF_UNIX socket, close it and later fetch from socket. FIXME.
ian@0 3303 *
ian@0 3304 * Nasty bug: do_SAK is being called in interrupt context. This can
ian@0 3305 * deadlock. We punt it up to process context. AKPM - 16Mar2001
ian@0 3306 */
ian@0 3307 static void __do_SAK(void *arg)
ian@0 3308 {
ian@0 3309 #ifdef TTY_SOFT_SAK
ian@0 3310 tty_hangup(tty);
ian@0 3311 #else
ian@0 3312 struct tty_struct *tty = arg;
ian@0 3313 struct task_struct *g, *p;
ian@0 3314 int session;
ian@0 3315 int i;
ian@0 3316 struct file *filp;
ian@0 3317 struct tty_ldisc *disc;
ian@0 3318 struct fdtable *fdt;
ian@0 3319
ian@0 3320 if (!tty)
ian@0 3321 return;
ian@0 3322 session = tty->session;
ian@0 3323
ian@0 3324 /* We don't want an ldisc switch during this */
ian@0 3325 disc = tty_ldisc_ref(tty);
ian@0 3326 if (disc && disc->flush_buffer)
ian@0 3327 disc->flush_buffer(tty);
ian@0 3328 tty_ldisc_deref(disc);
ian@0 3329
ian@0 3330 if (tty->driver->flush_buffer)
ian@0 3331 tty->driver->flush_buffer(tty);
ian@0 3332
ian@0 3333 read_lock(&tasklist_lock);
ian@0 3334 /* Kill the entire session */
ian@0 3335 do_each_task_pid(session, PIDTYPE_SID, p) {
ian@0 3336 printk(KERN_NOTICE "SAK: killed process %d"
ian@0 3337 " (%s): p->signal->session==tty->session\n",
ian@0 3338 p->pid, p->comm);
ian@0 3339 send_sig(SIGKILL, p, 1);
ian@0 3340 } while_each_task_pid(session, PIDTYPE_SID, p);
ian@0 3341 /* Now kill any processes that happen to have the
ian@0 3342 * tty open.
ian@0 3343 */
ian@0 3344 do_each_thread(g, p) {
ian@0 3345 if (p->signal->tty == tty) {
ian@0 3346 printk(KERN_NOTICE "SAK: killed process %d"
ian@0 3347 " (%s): p->signal->session==tty->session\n",
ian@0 3348 p->pid, p->comm);
ian@0 3349 send_sig(SIGKILL, p, 1);
ian@0 3350 continue;
ian@0 3351 }
ian@0 3352 task_lock(p);
ian@0 3353 if (p->files) {
ian@0 3354 /*
ian@0 3355 * We don't take a ref to the file, so we must
ian@0 3356 * hold ->file_lock instead.
ian@0 3357 */
ian@0 3358 spin_lock(&p->files->file_lock);
ian@0 3359 fdt = files_fdtable(p->files);
ian@0 3360 for (i=0; i < fdt->max_fds; i++) {
ian@0 3361 filp = fcheck_files(p->files, i);
ian@0 3362 if (!filp)
ian@0 3363 continue;
ian@0 3364 if (filp->f_op->read == tty_read &&
ian@0 3365 filp->private_data == tty) {
ian@0 3366 printk(KERN_NOTICE "SAK: killed process %d"
ian@0 3367 " (%s): fd#%d opened to the tty\n",
ian@0 3368 p->pid, p->comm, i);
ian@0 3369 force_sig(SIGKILL, p);
ian@0 3370 break;
ian@0 3371 }
ian@0 3372 }
ian@0 3373 spin_unlock(&p->files->file_lock);
ian@0 3374 }
ian@0 3375 task_unlock(p);
ian@0 3376 } while_each_thread(g, p);
ian@0 3377 read_unlock(&tasklist_lock);
ian@0 3378 #endif
ian@0 3379 }
ian@0 3380
ian@0 3381 /*
ian@0 3382 * The tq handling here is a little racy - tty->SAK_work may already be queued.
ian@0 3383 * Fortunately we don't need to worry, because if ->SAK_work is already queued,
ian@0 3384 * the values which we write to it will be identical to the values which it
ian@0 3385 * already has. --akpm
ian@0 3386 */
ian@0 3387 void do_SAK(struct tty_struct *tty)
ian@0 3388 {
ian@0 3389 if (!tty)
ian@0 3390 return;
ian@0 3391 PREPARE_WORK(&tty->SAK_work, __do_SAK, tty);
ian@0 3392 schedule_work(&tty->SAK_work);
ian@0 3393 }
ian@0 3394
ian@0 3395 EXPORT_SYMBOL(do_SAK);
ian@0 3396
ian@0 3397 /**
ian@0 3398 * flush_to_ldisc
ian@0 3399 * @private_: tty structure passed from work queue.
ian@0 3400 *
ian@0 3401 * This routine is called out of the software interrupt to flush data
ian@0 3402 * from the buffer chain to the line discipline.
ian@0 3403 *
ian@0 3404 * Locking: holds tty->buf.lock to guard buffer list. Drops the lock
ian@0 3405 * while invoking the line discipline receive_buf method. The
ian@0 3406 * receive_buf method is single threaded for each tty instance.
ian@0 3407 */
ian@0 3408
ian@0 3409 static void flush_to_ldisc(void *private_)
ian@0 3410 {
ian@0 3411 struct tty_struct *tty = (struct tty_struct *) private_;
ian@0 3412 unsigned long flags;
ian@0 3413 struct tty_ldisc *disc;
ian@0 3414 struct tty_buffer *tbuf, *head;
ian@0 3415 char *char_buf;
ian@0 3416 unsigned char *flag_buf;
ian@0 3417
ian@0 3418 disc = tty_ldisc_ref(tty);
ian@0 3419 if (disc == NULL) /* !TTY_LDISC */
ian@0 3420 return;
ian@0 3421
ian@0 3422 spin_lock_irqsave(&tty->buf.lock, flags);
ian@0 3423 head = tty->buf.head;
ian@0 3424 if (head != NULL) {
ian@0 3425 tty->buf.head = NULL;
ian@0 3426 for (;;) {
ian@0 3427 int count = head->commit - head->read;
ian@0 3428 if (!count) {