n_tty.c 39 KB

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