n_tty.c 39 KB

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