n_tty.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  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 (I_ISTRIP(tty))
  635. c &= 0x7f;
  636. if (I_IUCLC(tty) && L_IEXTEN(tty))
  637. c=tolower(c);
  638. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  639. ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) ||
  640. c == INTR_CHAR(tty) || c == QUIT_CHAR(tty)))
  641. start_tty(tty);
  642. if (tty->closing) {
  643. if (I_IXON(tty)) {
  644. if (c == START_CHAR(tty))
  645. start_tty(tty);
  646. else if (c == STOP_CHAR(tty))
  647. stop_tty(tty);
  648. }
  649. return;
  650. }
  651. /*
  652. * If the previous character was LNEXT, or we know that this
  653. * character is not one of the characters that we'll have to
  654. * handle specially, do shortcut processing to speed things
  655. * up.
  656. */
  657. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  658. finish_erasing(tty);
  659. tty->lnext = 0;
  660. if (L_ECHO(tty)) {
  661. if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
  662. put_char('\a', tty); /* beep if no space */
  663. return;
  664. }
  665. /* Record the column of first canon char. */
  666. if (tty->canon_head == tty->read_head)
  667. tty->canon_column = tty->column;
  668. echo_char(c, tty);
  669. }
  670. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  671. put_tty_queue(c, tty);
  672. put_tty_queue(c, tty);
  673. return;
  674. }
  675. if (c == '\r') {
  676. if (I_IGNCR(tty))
  677. return;
  678. if (I_ICRNL(tty))
  679. c = '\n';
  680. } else if (c == '\n' && I_INLCR(tty))
  681. c = '\r';
  682. if (I_IXON(tty)) {
  683. if (c == START_CHAR(tty)) {
  684. start_tty(tty);
  685. return;
  686. }
  687. if (c == STOP_CHAR(tty)) {
  688. stop_tty(tty);
  689. return;
  690. }
  691. }
  692. if (L_ISIG(tty)) {
  693. int signal;
  694. signal = SIGINT;
  695. if (c == INTR_CHAR(tty))
  696. goto send_signal;
  697. signal = SIGQUIT;
  698. if (c == QUIT_CHAR(tty))
  699. goto send_signal;
  700. signal = SIGTSTP;
  701. if (c == SUSP_CHAR(tty)) {
  702. send_signal:
  703. /*
  704. * Echo character, and then send the signal.
  705. * Note that we do not use isig() here because we want
  706. * the order to be:
  707. * 1) flush, 2) echo, 3) signal
  708. */
  709. if (!L_NOFLSH(tty)) {
  710. n_tty_flush_buffer(tty);
  711. if (tty->driver->flush_buffer)
  712. tty->driver->flush_buffer(tty);
  713. }
  714. if (L_ECHO(tty))
  715. echo_char(c, tty);
  716. if (tty->pgrp)
  717. kill_pgrp(tty->pgrp, signal, 1);
  718. return;
  719. }
  720. }
  721. if (tty->icanon) {
  722. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  723. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  724. eraser(c, tty);
  725. return;
  726. }
  727. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  728. tty->lnext = 1;
  729. if (L_ECHO(tty)) {
  730. finish_erasing(tty);
  731. if (L_ECHOCTL(tty)) {
  732. put_char('^', tty);
  733. put_char('\b', tty);
  734. }
  735. }
  736. return;
  737. }
  738. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  739. L_IEXTEN(tty)) {
  740. unsigned long tail = tty->canon_head;
  741. finish_erasing(tty);
  742. echo_char(c, tty);
  743. opost('\n', tty);
  744. while (tail != tty->read_head) {
  745. echo_char(tty->read_buf[tail], tty);
  746. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  747. }
  748. return;
  749. }
  750. if (c == '\n') {
  751. if (L_ECHO(tty) || L_ECHONL(tty)) {
  752. if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
  753. put_char('\a', tty);
  754. opost('\n', tty);
  755. }
  756. goto handle_newline;
  757. }
  758. if (c == EOF_CHAR(tty)) {
  759. if (tty->canon_head != tty->read_head)
  760. set_bit(TTY_PUSH, &tty->flags);
  761. c = __DISABLED_CHAR;
  762. goto handle_newline;
  763. }
  764. if ((c == EOL_CHAR(tty)) ||
  765. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  766. /*
  767. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  768. */
  769. if (L_ECHO(tty)) {
  770. if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
  771. put_char('\a', tty);
  772. /* Record the column of first canon char. */
  773. if (tty->canon_head == tty->read_head)
  774. tty->canon_column = tty->column;
  775. echo_char(c, tty);
  776. }
  777. /*
  778. * XXX does PARMRK doubling happen for
  779. * EOL_CHAR and EOL2_CHAR?
  780. */
  781. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  782. put_tty_queue(c, tty);
  783. handle_newline:
  784. spin_lock_irqsave(&tty->read_lock, flags);
  785. set_bit(tty->read_head, tty->read_flags);
  786. put_tty_queue_nolock(c, tty);
  787. tty->canon_head = tty->read_head;
  788. tty->canon_data++;
  789. spin_unlock_irqrestore(&tty->read_lock, flags);
  790. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  791. if (waitqueue_active(&tty->read_wait))
  792. wake_up_interruptible(&tty->read_wait);
  793. return;
  794. }
  795. }
  796. finish_erasing(tty);
  797. if (L_ECHO(tty)) {
  798. if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
  799. put_char('\a', tty); /* beep if no space */
  800. return;
  801. }
  802. if (c == '\n')
  803. opost('\n', tty);
  804. else {
  805. /* Record the column of first canon char. */
  806. if (tty->canon_head == tty->read_head)
  807. tty->canon_column = tty->column;
  808. echo_char(c, tty);
  809. }
  810. }
  811. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  812. put_tty_queue(c, tty);
  813. put_tty_queue(c, tty);
  814. }
  815. /**
  816. * n_tty_write_wakeup - asynchronous I/O notifier
  817. * @tty: tty device
  818. *
  819. * Required for the ptys, serial driver etc. since processes
  820. * that attach themselves to the master and rely on ASYNC
  821. * IO must be woken up
  822. */
  823. static void n_tty_write_wakeup(struct tty_struct *tty)
  824. {
  825. if (tty->fasync)
  826. {
  827. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  828. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  829. }
  830. return;
  831. }
  832. /**
  833. * n_tty_receive_buf - data receive
  834. * @tty: terminal device
  835. * @cp: buffer
  836. * @fp: flag buffer
  837. * @count: characters
  838. *
  839. * Called by the terminal driver when a block of characters has
  840. * been received. This function must be called from soft contexts
  841. * not from interrupt context. The driver is responsible for making
  842. * calls one at a time and in order (or using flush_to_ldisc)
  843. */
  844. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  845. char *fp, int count)
  846. {
  847. const unsigned char *p;
  848. char *f, flags = TTY_NORMAL;
  849. int i;
  850. char buf[64];
  851. unsigned long cpuflags;
  852. if (!tty->read_buf)
  853. return;
  854. if (tty->real_raw) {
  855. spin_lock_irqsave(&tty->read_lock, cpuflags);
  856. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  857. N_TTY_BUF_SIZE - tty->read_head);
  858. i = min(count, i);
  859. memcpy(tty->read_buf + tty->read_head, cp, i);
  860. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  861. tty->read_cnt += i;
  862. cp += i;
  863. count -= i;
  864. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  865. N_TTY_BUF_SIZE - tty->read_head);
  866. i = min(count, i);
  867. memcpy(tty->read_buf + tty->read_head, cp, i);
  868. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  869. tty->read_cnt += i;
  870. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  871. } else {
  872. for (i=count, p = cp, f = fp; i; i--, p++) {
  873. if (f)
  874. flags = *f++;
  875. switch (flags) {
  876. case TTY_NORMAL:
  877. n_tty_receive_char(tty, *p);
  878. break;
  879. case TTY_BREAK:
  880. n_tty_receive_break(tty);
  881. break;
  882. case TTY_PARITY:
  883. case TTY_FRAME:
  884. n_tty_receive_parity_error(tty, *p);
  885. break;
  886. case TTY_OVERRUN:
  887. n_tty_receive_overrun(tty);
  888. break;
  889. default:
  890. printk("%s: unknown flag %d\n",
  891. tty_name(tty, buf), flags);
  892. break;
  893. }
  894. }
  895. if (tty->driver->flush_chars)
  896. tty->driver->flush_chars(tty);
  897. }
  898. n_tty_set_room(tty);
  899. if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  900. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  901. if (waitqueue_active(&tty->read_wait))
  902. wake_up_interruptible(&tty->read_wait);
  903. }
  904. /*
  905. * Check the remaining room for the input canonicalization
  906. * mode. We don't want to throttle the driver if we're in
  907. * canonical mode and don't have a newline yet!
  908. */
  909. if (tty->receive_room < TTY_THRESHOLD_THROTTLE) {
  910. /* check TTY_THROTTLED first so it indicates our state */
  911. if (!test_and_set_bit(TTY_THROTTLED, &tty->flags) &&
  912. tty->driver->throttle)
  913. tty->driver->throttle(tty);
  914. }
  915. }
  916. int is_ignored(int sig)
  917. {
  918. return (sigismember(&current->blocked, sig) ||
  919. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  920. }
  921. /**
  922. * n_tty_set_termios - termios data changed
  923. * @tty: terminal
  924. * @old: previous data
  925. *
  926. * Called by the tty layer when the user changes termios flags so
  927. * that the line discipline can plan ahead. This function cannot sleep
  928. * and is protected from re-entry by the tty layer. The user is
  929. * guaranteed that this function will not be re-entered or in progress
  930. * when the ldisc is closed.
  931. */
  932. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios * old)
  933. {
  934. if (!tty)
  935. return;
  936. tty->icanon = (L_ICANON(tty) != 0);
  937. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  938. tty->raw = 1;
  939. tty->real_raw = 1;
  940. n_tty_set_room(tty);
  941. return;
  942. }
  943. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  944. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  945. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  946. I_PARMRK(tty)) {
  947. memset(tty->process_char_map, 0, 256/8);
  948. if (I_IGNCR(tty) || I_ICRNL(tty))
  949. set_bit('\r', tty->process_char_map);
  950. if (I_INLCR(tty))
  951. set_bit('\n', tty->process_char_map);
  952. if (L_ICANON(tty)) {
  953. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  954. set_bit(KILL_CHAR(tty), tty->process_char_map);
  955. set_bit(EOF_CHAR(tty), tty->process_char_map);
  956. set_bit('\n', tty->process_char_map);
  957. set_bit(EOL_CHAR(tty), tty->process_char_map);
  958. if (L_IEXTEN(tty)) {
  959. set_bit(WERASE_CHAR(tty),
  960. tty->process_char_map);
  961. set_bit(LNEXT_CHAR(tty),
  962. tty->process_char_map);
  963. set_bit(EOL2_CHAR(tty),
  964. tty->process_char_map);
  965. if (L_ECHO(tty))
  966. set_bit(REPRINT_CHAR(tty),
  967. tty->process_char_map);
  968. }
  969. }
  970. if (I_IXON(tty)) {
  971. set_bit(START_CHAR(tty), tty->process_char_map);
  972. set_bit(STOP_CHAR(tty), tty->process_char_map);
  973. }
  974. if (L_ISIG(tty)) {
  975. set_bit(INTR_CHAR(tty), tty->process_char_map);
  976. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  977. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  978. }
  979. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  980. tty->raw = 0;
  981. tty->real_raw = 0;
  982. } else {
  983. tty->raw = 1;
  984. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  985. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  986. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  987. tty->real_raw = 1;
  988. else
  989. tty->real_raw = 0;
  990. }
  991. n_tty_set_room(tty);
  992. }
  993. /**
  994. * n_tty_close - close the ldisc for this tty
  995. * @tty: device
  996. *
  997. * Called from the terminal layer when this line discipline is
  998. * being shut down, either because of a close or becsuse of a
  999. * discipline change. The function will not be called while other
  1000. * ldisc methods are in progress.
  1001. */
  1002. static void n_tty_close(struct tty_struct *tty)
  1003. {
  1004. n_tty_flush_buffer(tty);
  1005. if (tty->read_buf) {
  1006. free_buf(tty->read_buf);
  1007. tty->read_buf = NULL;
  1008. }
  1009. }
  1010. /**
  1011. * n_tty_open - open an ldisc
  1012. * @tty: terminal to open
  1013. *
  1014. * Called when this line discipline is being attached to the
  1015. * terminal device. Can sleep. Called serialized so that no
  1016. * other events will occur in parallel. No further open will occur
  1017. * until a close.
  1018. */
  1019. static int n_tty_open(struct tty_struct *tty)
  1020. {
  1021. if (!tty)
  1022. return -EINVAL;
  1023. /* This one is ugly. Currently a malloc failure here can panic */
  1024. if (!tty->read_buf) {
  1025. tty->read_buf = alloc_buf();
  1026. if (!tty->read_buf)
  1027. return -ENOMEM;
  1028. }
  1029. memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
  1030. reset_buffer_flags(tty);
  1031. tty->column = 0;
  1032. n_tty_set_termios(tty, NULL);
  1033. tty->minimum_to_wake = 1;
  1034. tty->closing = 0;
  1035. return 0;
  1036. }
  1037. static inline int input_available_p(struct tty_struct *tty, int amt)
  1038. {
  1039. if (tty->icanon) {
  1040. if (tty->canon_data)
  1041. return 1;
  1042. } else if (tty->read_cnt >= (amt ? amt : 1))
  1043. return 1;
  1044. return 0;
  1045. }
  1046. /**
  1047. * copy_from_read_buf - copy read data directly
  1048. * @tty: terminal device
  1049. * @b: user data
  1050. * @nr: size of data
  1051. *
  1052. * Helper function to speed up read_chan. It is only called when
  1053. * ICANON is off; it copies characters straight from the tty queue to
  1054. * user space directly. It can be profitably called twice; once to
  1055. * drain the space from the tail pointer to the (physical) end of the
  1056. * buffer, and once to drain the space from the (physical) beginning of
  1057. * the buffer to head pointer.
  1058. *
  1059. * Called under the tty->atomic_read_lock sem
  1060. *
  1061. */
  1062. static int copy_from_read_buf(struct tty_struct *tty,
  1063. unsigned char __user **b,
  1064. size_t *nr)
  1065. {
  1066. int retval;
  1067. size_t n;
  1068. unsigned long flags;
  1069. retval = 0;
  1070. spin_lock_irqsave(&tty->read_lock, flags);
  1071. n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1072. n = min(*nr, n);
  1073. spin_unlock_irqrestore(&tty->read_lock, flags);
  1074. if (n) {
  1075. retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1076. n -= retval;
  1077. tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1078. spin_lock_irqsave(&tty->read_lock, flags);
  1079. tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1080. tty->read_cnt -= n;
  1081. spin_unlock_irqrestore(&tty->read_lock, flags);
  1082. *b += n;
  1083. *nr -= n;
  1084. }
  1085. return retval;
  1086. }
  1087. extern ssize_t redirected_tty_write(struct file *,const char *,size_t,loff_t *);
  1088. /**
  1089. * job_control - check job control
  1090. * @tty: tty
  1091. * @file: file handle
  1092. *
  1093. * Perform job control management checks on this file/tty descriptor
  1094. * and if appropriate send any needed signals and return a negative
  1095. * error code if action should be taken.
  1096. */
  1097. static int job_control(struct tty_struct *tty, struct file *file)
  1098. {
  1099. /* Job control check -- must be done at start and after
  1100. every sleep (POSIX.1 7.1.1.4). */
  1101. /* NOTE: not yet done after every sleep pending a thorough
  1102. check of the logic of this change. -- jlc */
  1103. /* don't stop on /dev/console */
  1104. if (file->f_op->write != redirected_tty_write &&
  1105. current->signal->tty == tty) {
  1106. if (!tty->pgrp)
  1107. printk("read_chan: no tty->pgrp!\n");
  1108. else if (task_pgrp(current) != tty->pgrp) {
  1109. if (is_ignored(SIGTTIN) ||
  1110. is_current_pgrp_orphaned())
  1111. return -EIO;
  1112. kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1113. set_thread_flag(TIF_SIGPENDING);
  1114. return -ERESTARTSYS;
  1115. }
  1116. }
  1117. return 0;
  1118. }
  1119. /**
  1120. * read_chan - read function for tty
  1121. * @tty: tty device
  1122. * @file: file object
  1123. * @buf: userspace buffer pointer
  1124. * @nr: size of I/O
  1125. *
  1126. * Perform reads for the line discipline. We are guaranteed that the
  1127. * line discipline will not be closed under us but we may get multiple
  1128. * parallel readers and must handle this ourselves. We may also get
  1129. * a hangup. Always called in user context, may sleep.
  1130. *
  1131. * This code must be sure never to sleep through a hangup.
  1132. */
  1133. static ssize_t read_chan(struct tty_struct *tty, struct file *file,
  1134. unsigned char __user *buf, size_t nr)
  1135. {
  1136. unsigned char __user *b = buf;
  1137. DECLARE_WAITQUEUE(wait, current);
  1138. int c;
  1139. int minimum, time;
  1140. ssize_t retval = 0;
  1141. ssize_t size;
  1142. long timeout;
  1143. unsigned long flags;
  1144. do_it_again:
  1145. if (!tty->read_buf) {
  1146. printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
  1147. return -EIO;
  1148. }
  1149. c = job_control(tty, file);
  1150. if(c < 0)
  1151. return c;
  1152. minimum = time = 0;
  1153. timeout = MAX_SCHEDULE_TIMEOUT;
  1154. if (!tty->icanon) {
  1155. time = (HZ / 10) * TIME_CHAR(tty);
  1156. minimum = MIN_CHAR(tty);
  1157. if (minimum) {
  1158. if (time)
  1159. tty->minimum_to_wake = 1;
  1160. else if (!waitqueue_active(&tty->read_wait) ||
  1161. (tty->minimum_to_wake > minimum))
  1162. tty->minimum_to_wake = minimum;
  1163. } else {
  1164. timeout = 0;
  1165. if (time) {
  1166. timeout = time;
  1167. time = 0;
  1168. }
  1169. tty->minimum_to_wake = minimum = 1;
  1170. }
  1171. }
  1172. /*
  1173. * Internal serialization of reads.
  1174. */
  1175. if (file->f_flags & O_NONBLOCK) {
  1176. if (!mutex_trylock(&tty->atomic_read_lock))
  1177. return -EAGAIN;
  1178. }
  1179. else {
  1180. if (mutex_lock_interruptible(&tty->atomic_read_lock))
  1181. return -ERESTARTSYS;
  1182. }
  1183. add_wait_queue(&tty->read_wait, &wait);
  1184. while (nr) {
  1185. /* First test for status change. */
  1186. if (tty->packet && tty->link->ctrl_status) {
  1187. unsigned char cs;
  1188. if (b != buf)
  1189. break;
  1190. cs = tty->link->ctrl_status;
  1191. tty->link->ctrl_status = 0;
  1192. if (tty_put_user(tty, cs, b++)) {
  1193. retval = -EFAULT;
  1194. b--;
  1195. break;
  1196. }
  1197. nr--;
  1198. break;
  1199. }
  1200. /* This statement must be first before checking for input
  1201. so that any interrupt will set the state back to
  1202. TASK_RUNNING. */
  1203. set_current_state(TASK_INTERRUPTIBLE);
  1204. if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  1205. ((minimum - (b - buf)) >= 1))
  1206. tty->minimum_to_wake = (minimum - (b - buf));
  1207. if (!input_available_p(tty, 0)) {
  1208. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1209. retval = -EIO;
  1210. break;
  1211. }
  1212. if (tty_hung_up_p(file))
  1213. break;
  1214. if (!timeout)
  1215. break;
  1216. if (file->f_flags & O_NONBLOCK) {
  1217. retval = -EAGAIN;
  1218. break;
  1219. }
  1220. if (signal_pending(current)) {
  1221. retval = -ERESTARTSYS;
  1222. break;
  1223. }
  1224. n_tty_set_room(tty);
  1225. timeout = schedule_timeout(timeout);
  1226. continue;
  1227. }
  1228. __set_current_state(TASK_RUNNING);
  1229. /* Deal with packet mode. */
  1230. if (tty->packet && b == buf) {
  1231. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  1232. retval = -EFAULT;
  1233. b--;
  1234. break;
  1235. }
  1236. nr--;
  1237. }
  1238. if (tty->icanon) {
  1239. /* N.B. avoid overrun if nr == 0 */
  1240. while (nr && tty->read_cnt) {
  1241. int eol;
  1242. eol = test_and_clear_bit(tty->read_tail,
  1243. tty->read_flags);
  1244. c = tty->read_buf[tty->read_tail];
  1245. spin_lock_irqsave(&tty->read_lock, flags);
  1246. tty->read_tail = ((tty->read_tail+1) &
  1247. (N_TTY_BUF_SIZE-1));
  1248. tty->read_cnt--;
  1249. if (eol) {
  1250. /* this test should be redundant:
  1251. * we shouldn't be reading data if
  1252. * canon_data is 0
  1253. */
  1254. if (--tty->canon_data < 0)
  1255. tty->canon_data = 0;
  1256. }
  1257. spin_unlock_irqrestore(&tty->read_lock, flags);
  1258. if (!eol || (c != __DISABLED_CHAR)) {
  1259. if (tty_put_user(tty, c, b++)) {
  1260. retval = -EFAULT;
  1261. b--;
  1262. break;
  1263. }
  1264. nr--;
  1265. }
  1266. if (eol) {
  1267. tty_audit_push(tty);
  1268. break;
  1269. }
  1270. }
  1271. if (retval)
  1272. break;
  1273. } else {
  1274. int uncopied;
  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->driver->flush_chars)
  1370. tty->driver->flush_chars(tty);
  1371. } else {
  1372. while (nr > 0) {
  1373. c = tty->driver->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. * FIXME: if someone changes the VMIN or discipline settings for the
  1411. * terminal while another process is in poll() the poll does not
  1412. * recompute the new limits. Possibly set_termios should issue
  1413. * a read wakeup to fix this bug.
  1414. */
  1415. static unsigned int normal_poll(struct tty_struct * tty, struct file * file, poll_table *wait)
  1416. {
  1417. unsigned int mask = 0;
  1418. poll_wait(file, &tty->read_wait, wait);
  1419. poll_wait(file, &tty->write_wait, wait);
  1420. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1421. mask |= POLLIN | POLLRDNORM;
  1422. if (tty->packet && tty->link->ctrl_status)
  1423. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1424. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1425. mask |= POLLHUP;
  1426. if (tty_hung_up_p(file))
  1427. mask |= POLLHUP;
  1428. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1429. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1430. tty->minimum_to_wake = MIN_CHAR(tty);
  1431. else
  1432. tty->minimum_to_wake = 1;
  1433. }
  1434. if (!tty_is_writelocked(tty) &&
  1435. tty->driver->chars_in_buffer(tty) < WAKEUP_CHARS &&
  1436. tty->driver->write_room(tty) > 0)
  1437. mask |= POLLOUT | POLLWRNORM;
  1438. return mask;
  1439. }
  1440. struct tty_ldisc tty_ldisc_N_TTY = {
  1441. .magic = TTY_LDISC_MAGIC,
  1442. .name = "n_tty",
  1443. .open = n_tty_open,
  1444. .close = n_tty_close,
  1445. .flush_buffer = n_tty_flush_buffer,
  1446. .chars_in_buffer = n_tty_chars_in_buffer,
  1447. .read = read_chan,
  1448. .write = write_chan,
  1449. .ioctl = n_tty_ioctl,
  1450. .set_termios = n_tty_set_termios,
  1451. .poll = normal_poll,
  1452. .receive_buf = n_tty_receive_buf,
  1453. .write_wakeup = n_tty_write_wakeup
  1454. };