Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * n_tty.c --- implements the N_TTY line discipline. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 3 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * This code used to be in tty_io.c, but things are getting hairy |
| 5 | * enough that it made sense to split things off. (The N_TTY |
| 6 | * processing has changed so much that it's hardly recognizable, |
| 7 | * anyway...) |
| 8 | * |
| 9 | * Note that the open routine for N_TTY is guaranteed never to return |
| 10 | * an error. This is because Linux will fall back to setting a line |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 11 | * to N_TTY if it can not switch to any other line discipline. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
| 13 | * Written by Theodore Ts'o, Copyright 1994. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 14 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | * This file also contains code originally written by Linus Torvalds, |
| 16 | * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 17 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | * This file may be redistributed under the terms of the GNU General Public |
| 19 | * License. |
| 20 | * |
| 21 | * Reduced memory usage for older ARM systems - Russell King. |
| 22 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 23 | * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu> |
| 25 | * who actually finally proved there really was a race. |
| 26 | * |
| 27 | * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to |
| 28 | * waiting writing processes-Sapan Bhatia <sapan@corewars.org>. |
| 29 | * Also fixed a bug in BLOCKING mode where write_chan returns |
| 30 | * EAGAIN |
| 31 | */ |
| 32 | |
| 33 | #include <linux/types.h> |
| 34 | #include <linux/major.h> |
| 35 | #include <linux/errno.h> |
| 36 | #include <linux/signal.h> |
| 37 | #include <linux/fcntl.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/tty.h> |
| 41 | #include <linux/timer.h> |
| 42 | #include <linux/ctype.h> |
| 43 | #include <linux/mm.h> |
| 44 | #include <linux/string.h> |
| 45 | #include <linux/slab.h> |
| 46 | #include <linux/poll.h> |
| 47 | #include <linux/bitops.h> |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 48 | #include <linux/audit.h> |
| 49 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | #include <asm/uaccess.h> |
| 52 | #include <asm/system.h> |
| 53 | |
| 54 | /* number of characters left in xmit buffer before select has we have room */ |
| 55 | #define WAKEUP_CHARS 256 |
| 56 | |
| 57 | /* |
| 58 | * This defines the low- and high-watermarks for throttling and |
| 59 | * unthrottling the TTY driver. These watermarks are used for |
| 60 | * controlling the space in the read buffer. |
| 61 | */ |
| 62 | #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */ |
| 63 | #define TTY_THRESHOLD_UNTHROTTLE 128 |
| 64 | |
| 65 | static inline unsigned char *alloc_buf(void) |
| 66 | { |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 67 | gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | if (PAGE_SIZE != N_TTY_BUF_SIZE) |
| 70 | return kmalloc(N_TTY_BUF_SIZE, prio); |
| 71 | else |
| 72 | return (unsigned char *)__get_free_page(prio); |
| 73 | } |
| 74 | |
| 75 | static inline void free_buf(unsigned char *buf) |
| 76 | { |
| 77 | if (PAGE_SIZE != N_TTY_BUF_SIZE) |
| 78 | kfree(buf); |
| 79 | else |
| 80 | free_page((unsigned long) buf); |
| 81 | } |
| 82 | |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 83 | static inline int tty_put_user(struct tty_struct *tty, unsigned char x, |
| 84 | unsigned char __user *ptr) |
| 85 | { |
| 86 | tty_audit_add_data(tty, &x, 1); |
| 87 | return put_user(x, ptr); |
| 88 | } |
| 89 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 90 | /** |
| 91 | * n_tty_set__room - receive space |
| 92 | * @tty: terminal |
| 93 | * |
| 94 | * Called by the driver to find out how much data it is |
| 95 | * permitted to feed to the line discipline without any being lost |
| 96 | * and thus to manage flow control. Not serialized. Answers for the |
| 97 | * "instant". |
| 98 | */ |
| 99 | |
| 100 | static void n_tty_set_room(struct tty_struct *tty) |
| 101 | { |
| 102 | int left = N_TTY_BUF_SIZE - tty->read_cnt - 1; |
| 103 | |
| 104 | /* |
| 105 | * If we are doing input canonicalization, and there are no |
| 106 | * pending newlines, let characters through without limit, so |
| 107 | * that erase characters will be handled. Other excess |
| 108 | * characters will be beeped. |
| 109 | */ |
| 110 | if (left <= 0) |
| 111 | left = tty->icanon && !tty->canon_data; |
| 112 | tty->receive_room = left; |
| 113 | } |
| 114 | |
| 115 | static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
| 117 | if (tty->read_cnt < N_TTY_BUF_SIZE) { |
| 118 | tty->read_buf[tty->read_head] = c; |
| 119 | tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1); |
| 120 | tty->read_cnt++; |
| 121 | } |
| 122 | } |
| 123 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 124 | static void put_tty_queue(unsigned char c, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | unsigned long flags; |
| 127 | /* |
| 128 | * The problem of stomping on the buffers ends here. |
| 129 | * Why didn't anyone see this one coming? --AJK |
| 130 | */ |
| 131 | spin_lock_irqsave(&tty->read_lock, flags); |
| 132 | put_tty_queue_nolock(c, tty); |
| 133 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * check_unthrottle - allow new receive data |
| 138 | * @tty; tty device |
| 139 | * |
| 140 | * Check whether to call the driver.unthrottle function. |
| 141 | * We test the TTY_THROTTLED bit first so that it always |
| 142 | * indicates the current state. The decision about whether |
| 143 | * it is worth allowing more input has been taken by the caller. |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 144 | * Can sleep, may be called under the atomic_read_lock mutex but |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * this is not guaranteed. |
| 146 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 147 | |
| 148 | static void check_unthrottle(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 150 | if (tty->count) |
| 151 | tty_unthrottle(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * reset_buffer_flags - reset buffer state |
| 156 | * @tty: terminal to reset |
| 157 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 158 | * Reset the read buffer counters, clear the flags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | * and make sure the driver is unthrottled. Called |
| 160 | * from n_tty_open() and n_tty_flush_buffer(). |
| 161 | */ |
| 162 | static void reset_buffer_flags(struct tty_struct *tty) |
| 163 | { |
| 164 | unsigned long flags; |
| 165 | |
| 166 | spin_lock_irqsave(&tty->read_lock, flags); |
| 167 | tty->read_head = tty->read_tail = tty->read_cnt = 0; |
| 168 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 169 | tty->canon_head = tty->canon_data = tty->erasing = 0; |
| 170 | memset(&tty->read_flags, 0, sizeof tty->read_flags); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 171 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | check_unthrottle(tty); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * n_tty_flush_buffer - clean input queue |
| 177 | * @tty: terminal device |
| 178 | * |
| 179 | * Flush the input buffer. Called when the line discipline is |
| 180 | * being closed, when the tty layer wants the buffer flushed (eg |
| 181 | * at hangup) or when the N_TTY line discipline internally has to |
| 182 | * clean the pending queue (for example some signals). |
| 183 | * |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 184 | * Locking: ctrl_lock |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 186 | |
| 187 | static void n_tty_flush_buffer(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 189 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* clear everything and unthrottle the driver */ |
| 191 | reset_buffer_flags(tty); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!tty->link) |
| 194 | return; |
| 195 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 196 | spin_lock_irqsave(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | if (tty->link->packet) { |
| 198 | tty->ctrl_status |= TIOCPKT_FLUSHREAD; |
| 199 | wake_up_interruptible(&tty->link->read_wait); |
| 200 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 201 | spin_unlock_irqrestore(&tty->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * n_tty_chars_in_buffer - report available bytes |
| 206 | * @tty: tty device |
| 207 | * |
| 208 | * Report the number of characters buffered to be delivered to user |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 209 | * at this instant in time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty) |
| 213 | { |
| 214 | unsigned long flags; |
| 215 | ssize_t n = 0; |
| 216 | |
| 217 | spin_lock_irqsave(&tty->read_lock, flags); |
| 218 | if (!tty->icanon) { |
| 219 | n = tty->read_cnt; |
| 220 | } else if (tty->canon_data) { |
| 221 | n = (tty->canon_head > tty->read_tail) ? |
| 222 | tty->canon_head - tty->read_tail : |
| 223 | tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail); |
| 224 | } |
| 225 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 226 | return n; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * is_utf8_continuation - utf8 multibyte check |
| 231 | * @c: byte to check |
| 232 | * |
| 233 | * Returns true if the utf8 character 'c' is a multibyte continuation |
| 234 | * character. We use this to correctly compute the on screen size |
| 235 | * of the character when printing |
| 236 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | static inline int is_utf8_continuation(unsigned char c) |
| 239 | { |
| 240 | return (c & 0xc0) == 0x80; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * is_continuation - multibyte check |
| 245 | * @c: byte to check |
| 246 | * |
| 247 | * Returns true if the utf8 character 'c' is a multibyte continuation |
| 248 | * character and the terminal is in unicode mode. |
| 249 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | static inline int is_continuation(unsigned char c, struct tty_struct *tty) |
| 252 | { |
| 253 | return I_IUTF8(tty) && is_utf8_continuation(c); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * opost - output post processor |
| 258 | * @c: character (or partial unicode symbol) |
| 259 | * @tty: terminal device |
| 260 | * |
| 261 | * Perform OPOST processing. Returns -1 when the output device is |
| 262 | * full and the character must be retried. Note that Linux currently |
| 263 | * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't |
| 264 | * relevant in the world today. If you ever need them, add them here. |
| 265 | * |
| 266 | * Called from both the receive and transmit sides and can be called |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 267 | * re-entrantly. Relies on lock_kernel() for tty->column state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | static int opost(unsigned char c, struct tty_struct *tty) |
| 271 | { |
| 272 | int space, spaces; |
| 273 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 274 | space = tty_write_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | if (!space) |
| 276 | return -1; |
| 277 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 278 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | if (O_OPOST(tty)) { |
| 280 | switch (c) { |
| 281 | case '\n': |
| 282 | if (O_ONLRET(tty)) |
| 283 | tty->column = 0; |
| 284 | if (O_ONLCR(tty)) { |
| 285 | if (space < 2) |
| 286 | return -1; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 287 | tty_put_char(tty, '\r'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | tty->column = 0; |
| 289 | } |
| 290 | tty->canon_column = tty->column; |
| 291 | break; |
| 292 | case '\r': |
| 293 | if (O_ONOCR(tty) && tty->column == 0) |
| 294 | return 0; |
| 295 | if (O_OCRNL(tty)) { |
| 296 | c = '\n'; |
| 297 | if (O_ONLRET(tty)) |
| 298 | tty->canon_column = tty->column = 0; |
| 299 | break; |
| 300 | } |
| 301 | tty->canon_column = tty->column = 0; |
| 302 | break; |
| 303 | case '\t': |
| 304 | spaces = 8 - (tty->column & 7); |
| 305 | if (O_TABDLY(tty) == XTABS) { |
| 306 | if (space < spaces) |
| 307 | return -1; |
| 308 | tty->column += spaces; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 309 | tty->ops->write(tty, " ", spaces); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | tty->column += spaces; |
| 313 | break; |
| 314 | case '\b': |
| 315 | if (tty->column > 0) |
| 316 | tty->column--; |
| 317 | break; |
| 318 | default: |
| 319 | if (O_OLCUC(tty)) |
| 320 | c = toupper(c); |
| 321 | if (!iscntrl(c) && !is_continuation(c, tty)) |
| 322 | tty->column++; |
| 323 | break; |
| 324 | } |
| 325 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 326 | tty_put_char(tty, c); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 327 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * opost_block - block postprocess |
| 333 | * @tty: terminal device |
| 334 | * @inbuf: user buffer |
| 335 | * @nr: number of bytes |
| 336 | * |
| 337 | * This path is used to speed up block console writes, among other |
| 338 | * things when processing blocks of output data. It handles only |
| 339 | * the simple cases normally found and helps to generate blocks of |
| 340 | * symbols for the console driver and thus improve performance. |
| 341 | * |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 342 | * Called from write_chan under the tty layer write lock. Relies |
| 343 | * on lock_kernel for the tty->column state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 345 | |
| 346 | static ssize_t opost_block(struct tty_struct *tty, |
| 347 | const unsigned char *buf, unsigned int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
| 349 | int space; |
| 350 | int i; |
| 351 | const unsigned char *cp; |
| 352 | |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 353 | space = tty_write_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | if (!space) |
| 355 | return 0; |
| 356 | if (nr > space) |
| 357 | nr = space; |
| 358 | |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 359 | lock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | for (i = 0, cp = buf; i < nr; i++, cp++) { |
| 361 | switch (*cp) { |
| 362 | case '\n': |
| 363 | if (O_ONLRET(tty)) |
| 364 | tty->column = 0; |
| 365 | if (O_ONLCR(tty)) |
| 366 | goto break_out; |
| 367 | tty->canon_column = tty->column; |
| 368 | break; |
| 369 | case '\r': |
| 370 | if (O_ONOCR(tty) && tty->column == 0) |
| 371 | goto break_out; |
| 372 | if (O_OCRNL(tty)) |
| 373 | goto break_out; |
| 374 | tty->canon_column = tty->column = 0; |
| 375 | break; |
| 376 | case '\t': |
| 377 | goto break_out; |
| 378 | case '\b': |
| 379 | if (tty->column > 0) |
| 380 | tty->column--; |
| 381 | break; |
| 382 | default: |
| 383 | if (O_OLCUC(tty)) |
| 384 | goto break_out; |
| 385 | if (!iscntrl(*cp)) |
| 386 | tty->column++; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | break_out: |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 391 | if (tty->ops->flush_chars) |
| 392 | tty->ops->flush_chars(tty); |
| 393 | i = tty->ops->write(tty, buf, i); |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 394 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return i; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | * echo_char - echo characters |
| 401 | * @c: unicode byte to echo |
| 402 | * @tty: terminal device |
| 403 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 404 | * Echo user input back onto the screen. This must be called only when |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | * L_ECHO(tty) is true. Called from the driver receive_buf path. |
| 406 | */ |
| 407 | |
| 408 | static void echo_char(unsigned char c, struct tty_struct *tty) |
| 409 | { |
| 410 | if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 411 | tty_put_char(tty, '^'); |
| 412 | tty_put_char(tty, c ^ 0100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | tty->column += 2; |
| 414 | } else |
| 415 | opost(c, tty); |
| 416 | } |
| 417 | |
| 418 | static inline void finish_erasing(struct tty_struct *tty) |
| 419 | { |
| 420 | if (tty->erasing) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 421 | tty_put_char(tty, '/'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | tty->column++; |
| 423 | tty->erasing = 0; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * eraser - handle erase function |
| 429 | * @c: character input |
| 430 | * @tty: terminal device |
| 431 | * |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 432 | * Perform erase and necessary output when an erase character is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | * present in the stream from the driver layer. Handles the complexities |
| 434 | * of UTF-8 multibyte symbols. |
| 435 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | static void eraser(unsigned char c, struct tty_struct *tty) |
| 438 | { |
| 439 | enum { ERASE, WERASE, KILL } kill_type; |
| 440 | int head, seen_alnums, cnt; |
| 441 | unsigned long flags; |
| 442 | |
| 443 | if (tty->read_head == tty->canon_head) { |
| 444 | /* opost('\a', tty); */ /* what do you think? */ |
| 445 | return; |
| 446 | } |
| 447 | if (c == ERASE_CHAR(tty)) |
| 448 | kill_type = ERASE; |
| 449 | else if (c == WERASE_CHAR(tty)) |
| 450 | kill_type = WERASE; |
| 451 | else { |
| 452 | if (!L_ECHO(tty)) { |
| 453 | spin_lock_irqsave(&tty->read_lock, flags); |
| 454 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & |
| 455 | (N_TTY_BUF_SIZE - 1)); |
| 456 | tty->read_head = tty->canon_head; |
| 457 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 458 | return; |
| 459 | } |
| 460 | if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) { |
| 461 | spin_lock_irqsave(&tty->read_lock, flags); |
| 462 | tty->read_cnt -= ((tty->read_head - tty->canon_head) & |
| 463 | (N_TTY_BUF_SIZE - 1)); |
| 464 | tty->read_head = tty->canon_head; |
| 465 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 466 | finish_erasing(tty); |
| 467 | echo_char(KILL_CHAR(tty), tty); |
| 468 | /* Add a newline if ECHOK is on and ECHOKE is off. */ |
| 469 | if (L_ECHOK(tty)) |
| 470 | opost('\n', tty); |
| 471 | return; |
| 472 | } |
| 473 | kill_type = KILL; |
| 474 | } |
| 475 | |
| 476 | seen_alnums = 0; |
| 477 | while (tty->read_head != tty->canon_head) { |
| 478 | head = tty->read_head; |
| 479 | |
| 480 | /* erase a single possibly multibyte character */ |
| 481 | do { |
| 482 | head = (head - 1) & (N_TTY_BUF_SIZE-1); |
| 483 | c = tty->read_buf[head]; |
| 484 | } while (is_continuation(c, tty) && head != tty->canon_head); |
| 485 | |
| 486 | /* do not partially erase */ |
| 487 | if (is_continuation(c, tty)) |
| 488 | break; |
| 489 | |
| 490 | if (kill_type == WERASE) { |
| 491 | /* Equivalent to BSD's ALTWERASE. */ |
| 492 | if (isalnum(c) || c == '_') |
| 493 | seen_alnums++; |
| 494 | else if (seen_alnums) |
| 495 | break; |
| 496 | } |
| 497 | cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1); |
| 498 | spin_lock_irqsave(&tty->read_lock, flags); |
| 499 | tty->read_head = head; |
| 500 | tty->read_cnt -= cnt; |
| 501 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 502 | if (L_ECHO(tty)) { |
| 503 | if (L_ECHOPRT(tty)) { |
| 504 | if (!tty->erasing) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 505 | tty_put_char(tty, '\\'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | tty->column++; |
| 507 | tty->erasing = 1; |
| 508 | } |
| 509 | /* if cnt > 1, output a multi-byte character */ |
| 510 | echo_char(c, tty); |
| 511 | while (--cnt > 0) { |
| 512 | head = (head+1) & (N_TTY_BUF_SIZE-1); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 513 | tty_put_char(tty, tty->read_buf[head]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | } else if (kill_type == ERASE && !L_ECHOE(tty)) { |
| 516 | echo_char(ERASE_CHAR(tty), tty); |
| 517 | } else if (c == '\t') { |
| 518 | unsigned int col = tty->canon_column; |
| 519 | unsigned long tail = tty->canon_head; |
| 520 | |
| 521 | /* Find the column of the last char. */ |
| 522 | while (tail != tty->read_head) { |
| 523 | c = tty->read_buf[tail]; |
| 524 | if (c == '\t') |
| 525 | col = (col | 7) + 1; |
| 526 | else if (iscntrl(c)) { |
| 527 | if (L_ECHOCTL(tty)) |
| 528 | col += 2; |
| 529 | } else if (!is_continuation(c, tty)) |
| 530 | col++; |
| 531 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); |
| 532 | } |
| 533 | |
| 534 | /* should never happen */ |
| 535 | if (tty->column > 0x80000000) |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 536 | tty->column = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | /* Now backup to that column. */ |
| 539 | while (tty->column > col) { |
| 540 | /* Can't use opost here. */ |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 541 | tty_put_char(tty, '\b'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (tty->column > 0) |
| 543 | tty->column--; |
| 544 | } |
| 545 | } else { |
| 546 | if (iscntrl(c) && L_ECHOCTL(tty)) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 547 | tty_put_char(tty, '\b'); |
| 548 | tty_put_char(tty, ' '); |
| 549 | tty_put_char(tty, '\b'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | if (tty->column > 0) |
| 551 | tty->column--; |
| 552 | } |
| 553 | if (!iscntrl(c) || L_ECHOCTL(tty)) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 554 | tty_put_char(tty, '\b'); |
| 555 | tty_put_char(tty, ' '); |
| 556 | tty_put_char(tty, '\b'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | if (tty->column > 0) |
| 558 | tty->column--; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | if (kill_type == ERASE) |
| 563 | break; |
| 564 | } |
| 565 | if (tty->read_head == tty->canon_head) |
| 566 | finish_erasing(tty); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * isig - handle the ISIG optio |
| 571 | * @sig: signal |
| 572 | * @tty: terminal |
| 573 | * @flush: force flush |
| 574 | * |
| 575 | * Called when a signal is being sent due to terminal input. This |
| 576 | * may caus terminal flushing to take place according to the termios |
| 577 | * settings and character used. Called from the driver receive_buf |
| 578 | * path so serialized. |
| 579 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | static inline void isig(int sig, struct tty_struct *tty, int flush) |
| 582 | { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 583 | if (tty->pgrp) |
| 584 | kill_pgrp(tty->pgrp, sig, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | if (flush || !L_NOFLSH(tty)) { |
| 586 | n_tty_flush_buffer(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 587 | tty_driver_flush_buffer(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * n_tty_receive_break - handle break |
| 593 | * @tty: terminal |
| 594 | * |
| 595 | * An RS232 break event has been hit in the incoming bitstream. This |
| 596 | * can cause a variety of events depending upon the termios settings. |
| 597 | * |
| 598 | * Called from the receive_buf path so single threaded. |
| 599 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | static inline void n_tty_receive_break(struct tty_struct *tty) |
| 602 | { |
| 603 | if (I_IGNBRK(tty)) |
| 604 | return; |
| 605 | if (I_BRKINT(tty)) { |
| 606 | isig(SIGINT, tty, 1); |
| 607 | return; |
| 608 | } |
| 609 | if (I_PARMRK(tty)) { |
| 610 | put_tty_queue('\377', tty); |
| 611 | put_tty_queue('\0', tty); |
| 612 | } |
| 613 | put_tty_queue('\0', tty); |
| 614 | wake_up_interruptible(&tty->read_wait); |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * n_tty_receive_overrun - handle overrun reporting |
| 619 | * @tty: terminal |
| 620 | * |
| 621 | * Data arrived faster than we could process it. While the tty |
| 622 | * driver has flagged this the bits that were missed are gone |
| 623 | * forever. |
| 624 | * |
| 625 | * Called from the receive_buf path so single threaded. Does not |
| 626 | * need locking as num_overrun and overrun_time are function |
| 627 | * private. |
| 628 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | static inline void n_tty_receive_overrun(struct tty_struct *tty) |
| 631 | { |
| 632 | char buf[64]; |
| 633 | |
| 634 | tty->num_overrun++; |
| 635 | if (time_before(tty->overrun_time, jiffies - HZ) || |
| 636 | time_after(tty->overrun_time, jiffies)) { |
| 637 | printk(KERN_WARNING "%s: %d input overrun(s)\n", |
| 638 | tty_name(tty, buf), |
| 639 | tty->num_overrun); |
| 640 | tty->overrun_time = jiffies; |
| 641 | tty->num_overrun = 0; |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * n_tty_receive_parity_error - error notifier |
| 647 | * @tty: terminal device |
| 648 | * @c: character |
| 649 | * |
| 650 | * Process a parity error and queue the right data to indicate |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 651 | * the error case if necessary. Locking as per n_tty_receive_buf. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | */ |
| 653 | static inline void n_tty_receive_parity_error(struct tty_struct *tty, |
| 654 | unsigned char c) |
| 655 | { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 656 | if (I_IGNPAR(tty)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | if (I_PARMRK(tty)) { |
| 659 | put_tty_queue('\377', tty); |
| 660 | put_tty_queue('\0', tty); |
| 661 | put_tty_queue(c, tty); |
| 662 | } else if (I_INPCK(tty)) |
| 663 | put_tty_queue('\0', tty); |
| 664 | else |
| 665 | put_tty_queue(c, tty); |
| 666 | wake_up_interruptible(&tty->read_wait); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * n_tty_receive_char - perform processing |
| 671 | * @tty: terminal device |
| 672 | * @c: character |
| 673 | * |
| 674 | * Process an individual character of input received from the driver. |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 675 | * This is serialized with respect to itself by the rules for the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | * driver above. |
| 677 | */ |
| 678 | |
| 679 | static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c) |
| 680 | { |
| 681 | unsigned long flags; |
| 682 | |
| 683 | if (tty->raw) { |
| 684 | put_tty_queue(c, tty); |
| 685 | return; |
| 686 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | if (I_ISTRIP(tty)) |
| 689 | c &= 0x7f; |
| 690 | if (I_IUCLC(tty) && L_IEXTEN(tty)) |
| 691 | c=tolower(c); |
| 692 | |
Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 693 | if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && |
| 694 | ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) || |
Joe Peterson | 575537b | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 695 | c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty))) |
Joe Peterson | 54d2a37 | 2008-02-06 01:37:59 -0800 | [diff] [blame] | 696 | start_tty(tty); |
| 697 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | if (tty->closing) { |
| 699 | if (I_IXON(tty)) { |
| 700 | if (c == START_CHAR(tty)) |
| 701 | start_tty(tty); |
| 702 | else if (c == STOP_CHAR(tty)) |
| 703 | stop_tty(tty); |
| 704 | } |
| 705 | return; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * If the previous character was LNEXT, or we know that this |
| 710 | * character is not one of the characters that we'll have to |
| 711 | * handle specially, do shortcut processing to speed things |
| 712 | * up. |
| 713 | */ |
| 714 | if (!test_bit(c, tty->process_char_map) || tty->lnext) { |
| 715 | finish_erasing(tty); |
| 716 | tty->lnext = 0; |
| 717 | if (L_ECHO(tty)) { |
| 718 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 719 | tty_put_char(tty, '\a'); /* beep if no space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | /* Record the column of first canon char. */ |
| 723 | if (tty->canon_head == tty->read_head) |
| 724 | tty->canon_column = tty->column; |
| 725 | echo_char(c, tty); |
| 726 | } |
| 727 | if (I_PARMRK(tty) && c == (unsigned char) '\377') |
| 728 | put_tty_queue(c, tty); |
| 729 | put_tty_queue(c, tty); |
| 730 | return; |
| 731 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | if (I_IXON(tty)) { |
| 734 | if (c == START_CHAR(tty)) { |
| 735 | start_tty(tty); |
| 736 | return; |
| 737 | } |
| 738 | if (c == STOP_CHAR(tty)) { |
| 739 | stop_tty(tty); |
| 740 | return; |
| 741 | } |
| 742 | } |
Joe Peterson | 575537b | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | if (L_ISIG(tty)) { |
| 745 | int signal; |
| 746 | signal = SIGINT; |
| 747 | if (c == INTR_CHAR(tty)) |
| 748 | goto send_signal; |
| 749 | signal = SIGQUIT; |
| 750 | if (c == QUIT_CHAR(tty)) |
| 751 | goto send_signal; |
| 752 | signal = SIGTSTP; |
| 753 | if (c == SUSP_CHAR(tty)) { |
| 754 | send_signal: |
Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 755 | /* |
| 756 | * Echo character, and then send the signal. |
| 757 | * Note that we do not use isig() here because we want |
| 758 | * the order to be: |
| 759 | * 1) flush, 2) echo, 3) signal |
| 760 | */ |
| 761 | if (!L_NOFLSH(tty)) { |
| 762 | n_tty_flush_buffer(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 763 | tty_driver_flush_buffer(tty); |
Joe Peterson | ec5b115 | 2008-02-06 01:37:38 -0800 | [diff] [blame] | 764 | } |
| 765 | if (L_ECHO(tty)) |
| 766 | echo_char(c, tty); |
| 767 | if (tty->pgrp) |
| 768 | kill_pgrp(tty->pgrp, signal, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | return; |
| 770 | } |
| 771 | } |
Joe Peterson | 575537b | 2008-04-30 00:53:30 -0700 | [diff] [blame] | 772 | |
| 773 | if (c == '\r') { |
| 774 | if (I_IGNCR(tty)) |
| 775 | return; |
| 776 | if (I_ICRNL(tty)) |
| 777 | c = '\n'; |
| 778 | } else if (c == '\n' && I_INLCR(tty)) |
| 779 | c = '\r'; |
| 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | if (tty->icanon) { |
| 782 | if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) || |
| 783 | (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) { |
| 784 | eraser(c, tty); |
| 785 | return; |
| 786 | } |
| 787 | if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) { |
| 788 | tty->lnext = 1; |
| 789 | if (L_ECHO(tty)) { |
| 790 | finish_erasing(tty); |
| 791 | if (L_ECHOCTL(tty)) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 792 | tty_put_char(tty, '^'); |
| 793 | tty_put_char(tty, '\b'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | return; |
| 797 | } |
| 798 | if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && |
| 799 | L_IEXTEN(tty)) { |
| 800 | unsigned long tail = tty->canon_head; |
| 801 | |
| 802 | finish_erasing(tty); |
| 803 | echo_char(c, tty); |
| 804 | opost('\n', tty); |
| 805 | while (tail != tty->read_head) { |
| 806 | echo_char(tty->read_buf[tail], tty); |
| 807 | tail = (tail+1) & (N_TTY_BUF_SIZE-1); |
| 808 | } |
| 809 | return; |
| 810 | } |
| 811 | if (c == '\n') { |
| 812 | if (L_ECHO(tty) || L_ECHONL(tty)) { |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 813 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 814 | tty_put_char(tty, '\a'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | opost('\n', tty); |
| 816 | } |
| 817 | goto handle_newline; |
| 818 | } |
| 819 | if (c == EOF_CHAR(tty)) { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 820 | if (tty->canon_head != tty->read_head) |
| 821 | set_bit(TTY_PUSH, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | c = __DISABLED_CHAR; |
| 823 | goto handle_newline; |
| 824 | } |
| 825 | if ((c == EOL_CHAR(tty)) || |
| 826 | (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) { |
| 827 | /* |
| 828 | * XXX are EOL_CHAR and EOL2_CHAR echoed?!? |
| 829 | */ |
| 830 | if (L_ECHO(tty)) { |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 831 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 832 | tty_put_char(tty, '\a'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | /* Record the column of first canon char. */ |
| 834 | if (tty->canon_head == tty->read_head) |
| 835 | tty->canon_column = tty->column; |
| 836 | echo_char(c, tty); |
| 837 | } |
| 838 | /* |
| 839 | * XXX does PARMRK doubling happen for |
| 840 | * EOL_CHAR and EOL2_CHAR? |
| 841 | */ |
| 842 | if (I_PARMRK(tty) && c == (unsigned char) '\377') |
| 843 | put_tty_queue(c, tty); |
| 844 | |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 845 | handle_newline: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | spin_lock_irqsave(&tty->read_lock, flags); |
| 847 | set_bit(tty->read_head, tty->read_flags); |
| 848 | put_tty_queue_nolock(c, tty); |
| 849 | tty->canon_head = tty->read_head; |
| 850 | tty->canon_data++; |
| 851 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 852 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
| 853 | if (waitqueue_active(&tty->read_wait)) |
| 854 | wake_up_interruptible(&tty->read_wait); |
| 855 | return; |
| 856 | } |
| 857 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | finish_erasing(tty); |
| 860 | if (L_ECHO(tty)) { |
| 861 | if (tty->read_cnt >= N_TTY_BUF_SIZE-1) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 862 | tty_put_char(tty, '\a'); /* beep if no space */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | if (c == '\n') |
| 866 | opost('\n', tty); |
| 867 | else { |
| 868 | /* Record the column of first canon char. */ |
| 869 | if (tty->canon_head == tty->read_head) |
| 870 | tty->canon_column = tty->column; |
| 871 | echo_char(c, tty); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | if (I_PARMRK(tty) && c == (unsigned char) '\377') |
| 876 | put_tty_queue(c, tty); |
| 877 | |
| 878 | put_tty_queue(c, tty); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 879 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | |
| 882 | /** |
| 883 | * n_tty_write_wakeup - asynchronous I/O notifier |
| 884 | * @tty: tty device |
| 885 | * |
| 886 | * Required for the ptys, serial driver etc. since processes |
| 887 | * that attach themselves to the master and rely on ASYNC |
| 888 | * IO must be woken up |
| 889 | */ |
| 890 | |
| 891 | static void n_tty_write_wakeup(struct tty_struct *tty) |
| 892 | { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 893 | if (tty->fasync) { |
| 894 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | kill_fasync(&tty->fasync, SIGIO, POLL_OUT); |
| 896 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /** |
| 900 | * n_tty_receive_buf - data receive |
| 901 | * @tty: terminal device |
| 902 | * @cp: buffer |
| 903 | * @fp: flag buffer |
| 904 | * @count: characters |
| 905 | * |
| 906 | * Called by the terminal driver when a block of characters has |
| 907 | * been received. This function must be called from soft contexts |
| 908 | * not from interrupt context. The driver is responsible for making |
| 909 | * calls one at a time and in order (or using flush_to_ldisc) |
| 910 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp, |
| 913 | char *fp, int count) |
| 914 | { |
| 915 | const unsigned char *p; |
| 916 | char *f, flags = TTY_NORMAL; |
| 917 | int i; |
| 918 | char buf[64]; |
| 919 | unsigned long cpuflags; |
| 920 | |
| 921 | if (!tty->read_buf) |
| 922 | return; |
| 923 | |
| 924 | if (tty->real_raw) { |
| 925 | spin_lock_irqsave(&tty->read_lock, cpuflags); |
| 926 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, |
| 927 | N_TTY_BUF_SIZE - tty->read_head); |
| 928 | i = min(count, i); |
| 929 | memcpy(tty->read_buf + tty->read_head, cp, i); |
| 930 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); |
| 931 | tty->read_cnt += i; |
| 932 | cp += i; |
| 933 | count -= i; |
| 934 | |
| 935 | i = min(N_TTY_BUF_SIZE - tty->read_cnt, |
| 936 | N_TTY_BUF_SIZE - tty->read_head); |
| 937 | i = min(count, i); |
| 938 | memcpy(tty->read_buf + tty->read_head, cp, i); |
| 939 | tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1); |
| 940 | tty->read_cnt += i; |
| 941 | spin_unlock_irqrestore(&tty->read_lock, cpuflags); |
| 942 | } else { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 943 | for (i = count, p = cp, f = fp; i; i--, p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | if (f) |
| 945 | flags = *f++; |
| 946 | switch (flags) { |
| 947 | case TTY_NORMAL: |
| 948 | n_tty_receive_char(tty, *p); |
| 949 | break; |
| 950 | case TTY_BREAK: |
| 951 | n_tty_receive_break(tty); |
| 952 | break; |
| 953 | case TTY_PARITY: |
| 954 | case TTY_FRAME: |
| 955 | n_tty_receive_parity_error(tty, *p); |
| 956 | break; |
| 957 | case TTY_OVERRUN: |
| 958 | n_tty_receive_overrun(tty); |
| 959 | break; |
| 960 | default: |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 961 | printk(KERN_ERR "%s: unknown flag %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | tty_name(tty, buf), flags); |
| 963 | break; |
| 964 | } |
| 965 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 966 | if (tty->ops->flush_chars) |
| 967 | tty->ops->flush_chars(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 970 | n_tty_set_room(tty); |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) { |
| 973 | kill_fasync(&tty->fasync, SIGIO, POLL_IN); |
| 974 | if (waitqueue_active(&tty->read_wait)) |
| 975 | wake_up_interruptible(&tty->read_wait); |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Check the remaining room for the input canonicalization |
| 980 | * mode. We don't want to throttle the driver if we're in |
| 981 | * canonical mode and don't have a newline yet! |
| 982 | */ |
Alan Cox | 39c2e60 | 2008-04-30 00:54:18 -0700 | [diff] [blame] | 983 | if (tty->receive_room < TTY_THRESHOLD_THROTTLE) |
| 984 | tty_throttle(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | int is_ignored(int sig) |
| 988 | { |
| 989 | return (sigismember(¤t->blocked, sig) || |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 990 | current->sighand->action[sig-1].sa.sa_handler == SIG_IGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** |
| 994 | * n_tty_set_termios - termios data changed |
| 995 | * @tty: terminal |
| 996 | * @old: previous data |
| 997 | * |
| 998 | * Called by the tty layer when the user changes termios flags so |
| 999 | * that the line discipline can plan ahead. This function cannot sleep |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1000 | * and is protected from re-entry by the tty layer. The user is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | * guaranteed that this function will not be re-entered or in progress |
| 1002 | * when the ldisc is closed. |
| 1003 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1004 | |
| 1005 | static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | { |
| 1007 | if (!tty) |
| 1008 | return; |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | tty->icanon = (L_ICANON(tty) != 0); |
| 1011 | if (test_bit(TTY_HW_COOK_IN, &tty->flags)) { |
| 1012 | tty->raw = 1; |
| 1013 | tty->real_raw = 1; |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1014 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | return; |
| 1016 | } |
| 1017 | if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) || |
| 1018 | I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) || |
| 1019 | I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) || |
| 1020 | I_PARMRK(tty)) { |
| 1021 | memset(tty->process_char_map, 0, 256/8); |
| 1022 | |
| 1023 | if (I_IGNCR(tty) || I_ICRNL(tty)) |
| 1024 | set_bit('\r', tty->process_char_map); |
| 1025 | if (I_INLCR(tty)) |
| 1026 | set_bit('\n', tty->process_char_map); |
| 1027 | |
| 1028 | if (L_ICANON(tty)) { |
| 1029 | set_bit(ERASE_CHAR(tty), tty->process_char_map); |
| 1030 | set_bit(KILL_CHAR(tty), tty->process_char_map); |
| 1031 | set_bit(EOF_CHAR(tty), tty->process_char_map); |
| 1032 | set_bit('\n', tty->process_char_map); |
| 1033 | set_bit(EOL_CHAR(tty), tty->process_char_map); |
| 1034 | if (L_IEXTEN(tty)) { |
| 1035 | set_bit(WERASE_CHAR(tty), |
| 1036 | tty->process_char_map); |
| 1037 | set_bit(LNEXT_CHAR(tty), |
| 1038 | tty->process_char_map); |
| 1039 | set_bit(EOL2_CHAR(tty), |
| 1040 | tty->process_char_map); |
| 1041 | if (L_ECHO(tty)) |
| 1042 | set_bit(REPRINT_CHAR(tty), |
| 1043 | tty->process_char_map); |
| 1044 | } |
| 1045 | } |
| 1046 | if (I_IXON(tty)) { |
| 1047 | set_bit(START_CHAR(tty), tty->process_char_map); |
| 1048 | set_bit(STOP_CHAR(tty), tty->process_char_map); |
| 1049 | } |
| 1050 | if (L_ISIG(tty)) { |
| 1051 | set_bit(INTR_CHAR(tty), tty->process_char_map); |
| 1052 | set_bit(QUIT_CHAR(tty), tty->process_char_map); |
| 1053 | set_bit(SUSP_CHAR(tty), tty->process_char_map); |
| 1054 | } |
| 1055 | clear_bit(__DISABLED_CHAR, tty->process_char_map); |
| 1056 | tty->raw = 0; |
| 1057 | tty->real_raw = 0; |
| 1058 | } else { |
| 1059 | tty->raw = 1; |
| 1060 | if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) && |
| 1061 | (I_IGNPAR(tty) || !I_INPCK(tty)) && |
| 1062 | (tty->driver->flags & TTY_DRIVER_REAL_RAW)) |
| 1063 | tty->real_raw = 1; |
| 1064 | else |
| 1065 | tty->real_raw = 0; |
| 1066 | } |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1067 | n_tty_set_room(tty); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1068 | /* The termios change make the tty ready for I/O */ |
| 1069 | wake_up_interruptible(&tty->write_wait); |
| 1070 | wake_up_interruptible(&tty->read_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * n_tty_close - close the ldisc for this tty |
| 1075 | * @tty: device |
| 1076 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1077 | * Called from the terminal layer when this line discipline is |
| 1078 | * being shut down, either because of a close or becsuse of a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | * discipline change. The function will not be called while other |
| 1080 | * ldisc methods are in progress. |
| 1081 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | static void n_tty_close(struct tty_struct *tty) |
| 1084 | { |
| 1085 | n_tty_flush_buffer(tty); |
| 1086 | if (tty->read_buf) { |
| 1087 | free_buf(tty->read_buf); |
| 1088 | tty->read_buf = NULL; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | /** |
| 1093 | * n_tty_open - open an ldisc |
| 1094 | * @tty: terminal to open |
| 1095 | * |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1096 | * Called when this line discipline is being attached to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | * terminal device. Can sleep. Called serialized so that no |
| 1098 | * other events will occur in parallel. No further open will occur |
| 1099 | * until a close. |
| 1100 | */ |
| 1101 | |
| 1102 | static int n_tty_open(struct tty_struct *tty) |
| 1103 | { |
| 1104 | if (!tty) |
| 1105 | return -EINVAL; |
| 1106 | |
| 1107 | /* This one is ugly. Currently a malloc failure here can panic */ |
| 1108 | if (!tty->read_buf) { |
| 1109 | tty->read_buf = alloc_buf(); |
| 1110 | if (!tty->read_buf) |
| 1111 | return -ENOMEM; |
| 1112 | } |
| 1113 | memset(tty->read_buf, 0, N_TTY_BUF_SIZE); |
| 1114 | reset_buffer_flags(tty); |
| 1115 | tty->column = 0; |
| 1116 | n_tty_set_termios(tty, NULL); |
| 1117 | tty->minimum_to_wake = 1; |
| 1118 | tty->closing = 0; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
| 1122 | static inline int input_available_p(struct tty_struct *tty, int amt) |
| 1123 | { |
| 1124 | if (tty->icanon) { |
| 1125 | if (tty->canon_data) |
| 1126 | return 1; |
| 1127 | } else if (tty->read_cnt >= (amt ? amt : 1)) |
| 1128 | return 1; |
| 1129 | |
| 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * copy_from_read_buf - copy read data directly |
| 1135 | * @tty: terminal device |
| 1136 | * @b: user data |
| 1137 | * @nr: size of data |
| 1138 | * |
| 1139 | * Helper function to speed up read_chan. It is only called when |
| 1140 | * ICANON is off; it copies characters straight from the tty queue to |
| 1141 | * user space directly. It can be profitably called twice; once to |
| 1142 | * drain the space from the tail pointer to the (physical) end of the |
| 1143 | * buffer, and once to drain the space from the (physical) beginning of |
| 1144 | * the buffer to head pointer. |
| 1145 | * |
Paul Fulghum | 817d6d3 | 2006-06-28 04:26:47 -0700 | [diff] [blame] | 1146 | * Called under the tty->atomic_read_lock sem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | * |
| 1148 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1149 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1150 | static int copy_from_read_buf(struct tty_struct *tty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | unsigned char __user **b, |
| 1152 | size_t *nr) |
| 1153 | |
| 1154 | { |
| 1155 | int retval; |
| 1156 | size_t n; |
| 1157 | unsigned long flags; |
| 1158 | |
| 1159 | retval = 0; |
| 1160 | spin_lock_irqsave(&tty->read_lock, flags); |
| 1161 | n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail); |
| 1162 | n = min(*nr, n); |
| 1163 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 1164 | if (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n); |
| 1166 | n -= retval; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1167 | tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | spin_lock_irqsave(&tty->read_lock, flags); |
| 1169 | tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1); |
| 1170 | tty->read_cnt -= n; |
| 1171 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 1172 | *b += n; |
| 1173 | *nr -= n; |
| 1174 | } |
| 1175 | return retval; |
| 1176 | } |
| 1177 | |
Al Viro | cc4191d | 2008-03-29 03:08:48 +0000 | [diff] [blame] | 1178 | extern ssize_t redirected_tty_write(struct file *, const char __user *, |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1179 | size_t, loff_t *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
| 1181 | /** |
| 1182 | * job_control - check job control |
| 1183 | * @tty: tty |
| 1184 | * @file: file handle |
| 1185 | * |
| 1186 | * Perform job control management checks on this file/tty descriptor |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1187 | * and if appropriate send any needed signals and return a negative |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | * error code if action should be taken. |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1189 | * |
| 1190 | * FIXME: |
| 1191 | * Locking: None - redirected write test is safe, testing |
| 1192 | * current->signal should possibly lock current->sighand |
| 1193 | * pgrp locking ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | static int job_control(struct tty_struct *tty, struct file *file) |
| 1197 | { |
| 1198 | /* Job control check -- must be done at start and after |
| 1199 | every sleep (POSIX.1 7.1.1.4). */ |
| 1200 | /* NOTE: not yet done after every sleep pending a thorough |
| 1201 | check of the logic of this change. -- jlc */ |
| 1202 | /* don't stop on /dev/console */ |
| 1203 | if (file->f_op->write != redirected_tty_write && |
| 1204 | current->signal->tty == tty) { |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1205 | if (!tty->pgrp) |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1206 | printk(KERN_ERR "read_chan: no tty->pgrp!\n"); |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1207 | else if (task_pgrp(current) != tty->pgrp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | if (is_ignored(SIGTTIN) || |
Eric W. Biederman | 3e7cd6c | 2007-02-12 00:52:58 -0800 | [diff] [blame] | 1209 | is_current_pgrp_orphaned()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | return -EIO; |
Eric W. Biederman | ab521dc | 2007-02-12 00:53:00 -0800 | [diff] [blame] | 1211 | kill_pgrp(task_pgrp(current), SIGTTIN, 1); |
Oleg Nesterov | 040b636 | 2007-06-01 00:46:53 -0700 | [diff] [blame] | 1212 | set_thread_flag(TIF_SIGPENDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | return -ERESTARTSYS; |
| 1214 | } |
| 1215 | } |
| 1216 | return 0; |
| 1217 | } |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | /** |
| 1221 | * read_chan - read function for tty |
| 1222 | * @tty: tty device |
| 1223 | * @file: file object |
| 1224 | * @buf: userspace buffer pointer |
| 1225 | * @nr: size of I/O |
| 1226 | * |
| 1227 | * Perform reads for the line discipline. We are guaranteed that the |
| 1228 | * line discipline will not be closed under us but we may get multiple |
| 1229 | * parallel readers and must handle this ourselves. We may also get |
| 1230 | * a hangup. Always called in user context, may sleep. |
| 1231 | * |
| 1232 | * This code must be sure never to sleep through a hangup. |
| 1233 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | static ssize_t read_chan(struct tty_struct *tty, struct file *file, |
| 1236 | unsigned char __user *buf, size_t nr) |
| 1237 | { |
| 1238 | unsigned char __user *b = buf; |
| 1239 | DECLARE_WAITQUEUE(wait, current); |
| 1240 | int c; |
| 1241 | int minimum, time; |
| 1242 | ssize_t retval = 0; |
| 1243 | ssize_t size; |
| 1244 | long timeout; |
| 1245 | unsigned long flags; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1246 | int packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
| 1248 | do_it_again: |
| 1249 | |
| 1250 | if (!tty->read_buf) { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1251 | printk(KERN_ERR "n_tty_read_chan: read_buf == NULL?!?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | return -EIO; |
| 1253 | } |
| 1254 | |
| 1255 | c = job_control(tty, file); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1256 | if (c < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | return c; |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | minimum = time = 0; |
| 1260 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 1261 | if (!tty->icanon) { |
| 1262 | time = (HZ / 10) * TIME_CHAR(tty); |
| 1263 | minimum = MIN_CHAR(tty); |
| 1264 | if (minimum) { |
| 1265 | if (time) |
| 1266 | tty->minimum_to_wake = 1; |
| 1267 | else if (!waitqueue_active(&tty->read_wait) || |
| 1268 | (tty->minimum_to_wake > minimum)) |
| 1269 | tty->minimum_to_wake = minimum; |
| 1270 | } else { |
| 1271 | timeout = 0; |
| 1272 | if (time) { |
| 1273 | timeout = time; |
| 1274 | time = 0; |
| 1275 | } |
| 1276 | tty->minimum_to_wake = minimum = 1; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | /* |
| 1281 | * Internal serialization of reads. |
| 1282 | */ |
| 1283 | if (file->f_flags & O_NONBLOCK) { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1284 | if (!mutex_trylock(&tty->atomic_read_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | return -EAGAIN; |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1286 | } else { |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1287 | if (mutex_lock_interruptible(&tty->atomic_read_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | return -ERESTARTSYS; |
| 1289 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1290 | packet = tty->packet; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | |
| 1292 | add_wait_queue(&tty->read_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | while (nr) { |
| 1294 | /* First test for status change. */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1295 | if (packet && tty->link->ctrl_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | unsigned char cs; |
| 1297 | if (b != buf) |
| 1298 | break; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1299 | spin_lock_irqsave(&tty->link->ctrl_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | cs = tty->link->ctrl_status; |
| 1301 | tty->link->ctrl_status = 0; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1302 | spin_unlock_irqrestore(&tty->link->ctrl_lock, flags); |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1303 | if (tty_put_user(tty, cs, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | retval = -EFAULT; |
| 1305 | b--; |
| 1306 | break; |
| 1307 | } |
| 1308 | nr--; |
| 1309 | break; |
| 1310 | } |
| 1311 | /* This statement must be first before checking for input |
| 1312 | so that any interrupt will set the state back to |
| 1313 | TASK_RUNNING. */ |
| 1314 | set_current_state(TASK_INTERRUPTIBLE); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | if (((minimum - (b - buf)) < tty->minimum_to_wake) && |
| 1317 | ((minimum - (b - buf)) >= 1)) |
| 1318 | tty->minimum_to_wake = (minimum - (b - buf)); |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | if (!input_available_p(tty, 0)) { |
| 1321 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { |
| 1322 | retval = -EIO; |
| 1323 | break; |
| 1324 | } |
| 1325 | if (tty_hung_up_p(file)) |
| 1326 | break; |
| 1327 | if (!timeout) |
| 1328 | break; |
| 1329 | if (file->f_flags & O_NONBLOCK) { |
| 1330 | retval = -EAGAIN; |
| 1331 | break; |
| 1332 | } |
| 1333 | if (signal_pending(current)) { |
| 1334 | retval = -ERESTARTSYS; |
| 1335 | break; |
| 1336 | } |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1337 | /* FIXME: does n_tty_set_room need locking ? */ |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1338 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | timeout = schedule_timeout(timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | continue; |
| 1341 | } |
| 1342 | __set_current_state(TASK_RUNNING); |
| 1343 | |
| 1344 | /* Deal with packet mode. */ |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1345 | if (packet && b == buf) { |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1346 | if (tty_put_user(tty, TIOCPKT_DATA, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | retval = -EFAULT; |
| 1348 | b--; |
| 1349 | break; |
| 1350 | } |
| 1351 | nr--; |
| 1352 | } |
| 1353 | |
| 1354 | if (tty->icanon) { |
| 1355 | /* N.B. avoid overrun if nr == 0 */ |
| 1356 | while (nr && tty->read_cnt) { |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1357 | int eol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | |
| 1359 | eol = test_and_clear_bit(tty->read_tail, |
| 1360 | tty->read_flags); |
| 1361 | c = tty->read_buf[tty->read_tail]; |
| 1362 | spin_lock_irqsave(&tty->read_lock, flags); |
| 1363 | tty->read_tail = ((tty->read_tail+1) & |
| 1364 | (N_TTY_BUF_SIZE-1)); |
| 1365 | tty->read_cnt--; |
| 1366 | if (eol) { |
| 1367 | /* this test should be redundant: |
| 1368 | * we shouldn't be reading data if |
| 1369 | * canon_data is 0 |
| 1370 | */ |
| 1371 | if (--tty->canon_data < 0) |
| 1372 | tty->canon_data = 0; |
| 1373 | } |
| 1374 | spin_unlock_irqrestore(&tty->read_lock, flags); |
| 1375 | |
| 1376 | if (!eol || (c != __DISABLED_CHAR)) { |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1377 | if (tty_put_user(tty, c, b++)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | retval = -EFAULT; |
| 1379 | b--; |
| 1380 | break; |
| 1381 | } |
| 1382 | nr--; |
| 1383 | } |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1384 | if (eol) { |
| 1385 | tty_audit_push(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | break; |
Miloslav Trmac | 522ed77 | 2007-07-15 23:40:56 -0700 | [diff] [blame] | 1387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | if (retval) |
| 1390 | break; |
| 1391 | } else { |
| 1392 | int uncopied; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 1393 | /* The copy function takes the read lock and handles |
| 1394 | locking internally for this case */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | uncopied = copy_from_read_buf(tty, &b, &nr); |
| 1396 | uncopied += copy_from_read_buf(tty, &b, &nr); |
| 1397 | if (uncopied) { |
| 1398 | retval = -EFAULT; |
| 1399 | break; |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | /* If there is enough space in the read buffer now, let the |
| 1404 | * low-level driver know. We use n_tty_chars_in_buffer() to |
| 1405 | * check the buffer, as it now knows about canonical mode. |
| 1406 | * Otherwise, if the driver is throttled and the line is |
| 1407 | * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode, |
| 1408 | * we won't get any more characters. |
| 1409 | */ |
Paul Mackerras | 289a1e9 | 2006-06-12 12:16:26 +1000 | [diff] [blame] | 1410 | if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) { |
| 1411 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | check_unthrottle(tty); |
Paul Mackerras | 289a1e9 | 2006-06-12 12:16:26 +1000 | [diff] [blame] | 1413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | |
| 1415 | if (b - buf >= minimum) |
| 1416 | break; |
| 1417 | if (time) |
| 1418 | timeout = time; |
| 1419 | } |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 1420 | mutex_unlock(&tty->atomic_read_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | remove_wait_queue(&tty->read_wait, &wait); |
| 1422 | |
| 1423 | if (!waitqueue_active(&tty->read_wait)) |
| 1424 | tty->minimum_to_wake = minimum; |
| 1425 | |
| 1426 | __set_current_state(TASK_RUNNING); |
| 1427 | size = b - buf; |
| 1428 | if (size) { |
| 1429 | retval = size; |
| 1430 | if (nr) |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1431 | clear_bit(TTY_PUSH, &tty->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | } else if (test_and_clear_bit(TTY_PUSH, &tty->flags)) |
| 1433 | goto do_it_again; |
| 1434 | |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 1435 | n_tty_set_room(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | return retval; |
| 1437 | } |
| 1438 | |
| 1439 | /** |
| 1440 | * write_chan - write function for tty |
| 1441 | * @tty: tty device |
| 1442 | * @file: file object |
| 1443 | * @buf: userspace buffer pointer |
| 1444 | * @nr: size of I/O |
| 1445 | * |
| 1446 | * Write function of the terminal device. This is serialized with |
| 1447 | * respect to other write callers but not to termios changes, reads |
| 1448 | * and other such events. We must be careful with N_TTY as the receive |
| 1449 | * code will echo characters, thus calling driver write methods. |
| 1450 | * |
| 1451 | * This code must be sure never to sleep through a hangup. |
| 1452 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1453 | |
| 1454 | static ssize_t write_chan(struct tty_struct *tty, struct file *file, |
| 1455 | const unsigned char *buf, size_t nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | { |
| 1457 | const unsigned char *b = buf; |
| 1458 | DECLARE_WAITQUEUE(wait, current); |
| 1459 | int c; |
| 1460 | ssize_t retval = 0; |
| 1461 | |
| 1462 | /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */ |
| 1463 | if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) { |
| 1464 | retval = tty_check_change(tty); |
| 1465 | if (retval) |
| 1466 | return retval; |
| 1467 | } |
| 1468 | |
| 1469 | add_wait_queue(&tty->write_wait, &wait); |
| 1470 | while (1) { |
| 1471 | set_current_state(TASK_INTERRUPTIBLE); |
| 1472 | if (signal_pending(current)) { |
| 1473 | retval = -ERESTARTSYS; |
| 1474 | break; |
| 1475 | } |
| 1476 | if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) { |
| 1477 | retval = -EIO; |
| 1478 | break; |
| 1479 | } |
| 1480 | if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) { |
| 1481 | while (nr > 0) { |
| 1482 | ssize_t num = opost_block(tty, b, nr); |
| 1483 | if (num < 0) { |
| 1484 | if (num == -EAGAIN) |
| 1485 | break; |
| 1486 | retval = num; |
| 1487 | goto break_out; |
| 1488 | } |
| 1489 | b += num; |
| 1490 | nr -= num; |
| 1491 | if (nr == 0) |
| 1492 | break; |
| 1493 | c = *b; |
| 1494 | if (opost(c, tty) < 0) |
| 1495 | break; |
| 1496 | b++; nr--; |
| 1497 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1498 | if (tty->ops->flush_chars) |
| 1499 | tty->ops->flush_chars(tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | } else { |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 1501 | while (nr > 0) { |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1502 | c = tty->ops->write(tty, b, nr); |
Roman Zippel | d6afe27 | 2005-07-07 17:56:55 -0700 | [diff] [blame] | 1503 | if (c < 0) { |
| 1504 | retval = c; |
| 1505 | goto break_out; |
| 1506 | } |
| 1507 | if (!c) |
| 1508 | break; |
| 1509 | b += c; |
| 1510 | nr -= c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
| 1513 | if (!nr) |
| 1514 | break; |
| 1515 | if (file->f_flags & O_NONBLOCK) { |
| 1516 | retval = -EAGAIN; |
| 1517 | break; |
| 1518 | } |
| 1519 | schedule(); |
| 1520 | } |
| 1521 | break_out: |
| 1522 | __set_current_state(TASK_RUNNING); |
| 1523 | remove_wait_queue(&tty->write_wait, &wait); |
| 1524 | return (b - buf) ? b - buf : retval; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * normal_poll - poll method for N_TTY |
| 1529 | * @tty: terminal device |
| 1530 | * @file: file accessing it |
| 1531 | * @wait: poll table |
| 1532 | * |
| 1533 | * Called when the line discipline is asked to poll() for data or |
| 1534 | * for special events. This code is not serialized with respect to |
| 1535 | * other events save open/close. |
| 1536 | * |
| 1537 | * This code must be sure never to sleep through a hangup. |
| 1538 | * Called without the kernel lock held - fine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | */ |
Alan Cox | 4edf182 | 2008-02-08 04:18:44 -0800 | [diff] [blame] | 1540 | |
| 1541 | static unsigned int normal_poll(struct tty_struct *tty, struct file *file, |
| 1542 | poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | { |
| 1544 | unsigned int mask = 0; |
| 1545 | |
| 1546 | poll_wait(file, &tty->read_wait, wait); |
| 1547 | poll_wait(file, &tty->write_wait, wait); |
| 1548 | if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty))) |
| 1549 | mask |= POLLIN | POLLRDNORM; |
| 1550 | if (tty->packet && tty->link->ctrl_status) |
| 1551 | mask |= POLLPRI | POLLIN | POLLRDNORM; |
| 1552 | if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
| 1553 | mask |= POLLHUP; |
| 1554 | if (tty_hung_up_p(file)) |
| 1555 | mask |= POLLHUP; |
| 1556 | if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) { |
| 1557 | if (MIN_CHAR(tty) && !TIME_CHAR(tty)) |
| 1558 | tty->minimum_to_wake = MIN_CHAR(tty); |
| 1559 | else |
| 1560 | tty->minimum_to_wake = 1; |
| 1561 | } |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 1562 | if (tty->ops->write && !tty_is_writelocked(tty) && |
| 1563 | tty_chars_in_buffer(tty) < WAKEUP_CHARS && |
| 1564 | tty_write_room(tty) > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | mask |= POLLOUT | POLLWRNORM; |
| 1566 | return mask; |
| 1567 | } |
| 1568 | |
| 1569 | struct tty_ldisc tty_ldisc_N_TTY = { |
Paul Fulghum | e10cc1d | 2007-05-10 22:22:50 -0700 | [diff] [blame] | 1570 | .magic = TTY_LDISC_MAGIC, |
| 1571 | .name = "n_tty", |
| 1572 | .open = n_tty_open, |
| 1573 | .close = n_tty_close, |
| 1574 | .flush_buffer = n_tty_flush_buffer, |
| 1575 | .chars_in_buffer = n_tty_chars_in_buffer, |
| 1576 | .read = read_chan, |
| 1577 | .write = write_chan, |
| 1578 | .ioctl = n_tty_ioctl, |
| 1579 | .set_termios = n_tty_set_termios, |
| 1580 | .poll = normal_poll, |
| 1581 | .receive_buf = n_tty_receive_buf, |
| 1582 | .write_wakeup = n_tty_write_wakeup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | }; |
| 1584 | |