n_tty.c 39 KB

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