n_tty.c 39 KB

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