n_tty.c 38 KB

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