n_tty.c 41 KB

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