n_tty.c 38 KB

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