n_tty.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. /*
  2. * n_tty.c --- implements the N_TTY line discipline.
  3. *
  4. * This code used to be in tty_io.c, but things are getting hairy
  5. * enough that it made sense to split things off. (The N_TTY
  6. * processing has changed so much that it's hardly recognizable,
  7. * anyway...)
  8. *
  9. * Note that the open routine for N_TTY is guaranteed never to return
  10. * an error. This is because Linux will fall back to setting a line
  11. * to N_TTY if it can not switch to any other line discipline.
  12. *
  13. * Written by Theodore Ts'o, Copyright 1994.
  14. *
  15. * This file also contains code originally written by Linus Torvalds,
  16. * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  17. *
  18. * This file may be redistributed under the terms of the GNU General Public
  19. * License.
  20. *
  21. * Reduced memory usage for older ARM systems - Russell King.
  22. *
  23. * 2000/01/20 Fixed SMP locking on put_tty_queue using bits of
  24. * the patch by Andrew J. Kroll <ag784@freenet.buffalo.edu>
  25. * who actually finally proved there really was a race.
  26. *
  27. * 2002/03/18 Implemented n_tty_wakeup to send SIGIO POLL_OUTs to
  28. * waiting writing processes-Sapan Bhatia <sapan@corewars.org>.
  29. * Also fixed a bug in BLOCKING mode where n_tty_write returns
  30. * EAGAIN
  31. */
  32. #include <linux/types.h>
  33. #include <linux/major.h>
  34. #include <linux/errno.h>
  35. #include <linux/signal.h>
  36. #include <linux/fcntl.h>
  37. #include <linux/sched.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/tty.h>
  40. #include <linux/timer.h>
  41. #include <linux/ctype.h>
  42. #include <linux/mm.h>
  43. #include <linux/string.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/bitops.h>
  47. #include <linux/audit.h>
  48. #include <linux/file.h>
  49. #include <linux/uaccess.h>
  50. #include <asm/system.h>
  51. /* number of characters left in xmit buffer before select has we have room */
  52. #define WAKEUP_CHARS 256
  53. /*
  54. * This defines the low- and high-watermarks for throttling and
  55. * unthrottling the TTY driver. These watermarks are used for
  56. * controlling the space in the read buffer.
  57. */
  58. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  59. #define TTY_THRESHOLD_UNTHROTTLE 128
  60. /*
  61. * Special byte codes used in the echo buffer to represent operations
  62. * or special handling of characters. Bytes in the echo buffer that
  63. * are not part of such special blocks are treated as normal character
  64. * codes.
  65. */
  66. #define ECHO_OP_START 0xff
  67. #define ECHO_OP_MOVE_BACK_COL 0x80
  68. #define ECHO_OP_SET_CANON_COL 0x81
  69. #define ECHO_OP_ERASE_TAB 0x82
  70. static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
  71. unsigned char __user *ptr)
  72. {
  73. tty_audit_add_data(tty, &x, 1);
  74. return put_user(x, ptr);
  75. }
  76. /**
  77. * n_tty_set__room - receive space
  78. * @tty: terminal
  79. *
  80. * Called by the driver to find out how much data it is
  81. * permitted to feed to the line discipline without any being lost
  82. * and thus to manage flow control. Not serialized. Answers for the
  83. * "instant".
  84. */
  85. static void n_tty_set_room(struct tty_struct *tty)
  86. {
  87. /* tty->read_cnt is not read locked ? */
  88. int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  89. /*
  90. * If we are doing input canonicalization, and there are no
  91. * pending newlines, let characters through without limit, so
  92. * that erase characters will be handled. Other excess
  93. * characters will be beeped.
  94. */
  95. if (left <= 0)
  96. left = tty->icanon && !tty->canon_data;
  97. tty->receive_room = left;
  98. }
  99. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  100. {
  101. if (tty->read_cnt < N_TTY_BUF_SIZE) {
  102. tty->read_buf[tty->read_head] = c;
  103. tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  104. tty->read_cnt++;
  105. }
  106. }
  107. /**
  108. * put_tty_queue - add character to tty
  109. * @c: character
  110. * @tty: tty device
  111. *
  112. * Add a character to the tty read_buf queue. This is done under the
  113. * read_lock to serialize character addition and also to protect us
  114. * against parallel reads or flushes
  115. */
  116. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  117. {
  118. unsigned long flags;
  119. /*
  120. * The problem of stomping on the buffers ends here.
  121. * Why didn't anyone see this one coming? --AJK
  122. */
  123. spin_lock_irqsave(&tty->read_lock, flags);
  124. put_tty_queue_nolock(c, tty);
  125. spin_unlock_irqrestore(&tty->read_lock, flags);
  126. }
  127. /**
  128. * check_unthrottle - allow new receive data
  129. * @tty; tty device
  130. *
  131. * Check whether to call the driver unthrottle functions
  132. *
  133. * Can sleep, may be called under the atomic_read_lock mutex but
  134. * this is not guaranteed.
  135. */
  136. static void check_unthrottle(struct tty_struct *tty)
  137. {
  138. if (tty->count)
  139. tty_unthrottle(tty);
  140. }
  141. /**
  142. * reset_buffer_flags - reset buffer state
  143. * @tty: terminal to reset
  144. *
  145. * Reset the read buffer counters, clear the flags,
  146. * and make sure the driver is unthrottled. Called
  147. * from n_tty_open() and n_tty_flush_buffer().
  148. *
  149. * Locking: tty_read_lock for read fields.
  150. */
  151. static void reset_buffer_flags(struct tty_struct *tty)
  152. {
  153. unsigned long flags;
  154. spin_lock_irqsave(&tty->read_lock, flags);
  155. tty->read_head = tty->read_tail = tty->read_cnt = 0;
  156. spin_unlock_irqrestore(&tty->read_lock, flags);
  157. mutex_lock(&tty->echo_lock);
  158. tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
  159. mutex_unlock(&tty->echo_lock);
  160. tty->canon_head = tty->canon_data = tty->erasing = 0;
  161. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  162. n_tty_set_room(tty);
  163. check_unthrottle(tty);
  164. }
  165. /**
  166. * n_tty_flush_buffer - clean input queue
  167. * @tty: terminal device
  168. *
  169. * Flush the input buffer. Called when the line discipline is
  170. * being closed, when the tty layer wants the buffer flushed (eg
  171. * at hangup) or when the N_TTY line discipline internally has to
  172. * clean the pending queue (for example some signals).
  173. *
  174. * Locking: ctrl_lock, read_lock.
  175. */
  176. static void n_tty_flush_buffer(struct tty_struct *tty)
  177. {
  178. unsigned long flags;
  179. /* clear everything and unthrottle the driver */
  180. reset_buffer_flags(tty);
  181. if (!tty->link)
  182. return;
  183. spin_lock_irqsave(&tty->ctrl_lock, flags);
  184. if (tty->link->packet) {
  185. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  186. wake_up_interruptible(&tty->link->read_wait);
  187. }
  188. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  189. }
  190. /**
  191. * n_tty_chars_in_buffer - report available bytes
  192. * @tty: tty device
  193. *
  194. * Report the number of characters buffered to be delivered to user
  195. * at this instant in time.
  196. *
  197. * Locking: read_lock
  198. */
  199. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  200. {
  201. unsigned long flags;
  202. ssize_t n = 0;
  203. spin_lock_irqsave(&tty->read_lock, flags);
  204. if (!tty->icanon) {
  205. n = tty->read_cnt;
  206. } else if (tty->canon_data) {
  207. n = (tty->canon_head > tty->read_tail) ?
  208. tty->canon_head - tty->read_tail :
  209. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  210. }
  211. spin_unlock_irqrestore(&tty->read_lock, flags);
  212. return n;
  213. }
  214. /**
  215. * is_utf8_continuation - utf8 multibyte check
  216. * @c: byte to check
  217. *
  218. * Returns true if the utf8 character 'c' is a multibyte continuation
  219. * character. We use this to correctly compute the on screen size
  220. * of the character when printing
  221. */
  222. static inline int is_utf8_continuation(unsigned char c)
  223. {
  224. return (c & 0xc0) == 0x80;
  225. }
  226. /**
  227. * is_continuation - multibyte check
  228. * @c: byte to check
  229. *
  230. * Returns true if the utf8 character 'c' is a multibyte continuation
  231. * character and the terminal is in unicode mode.
  232. */
  233. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  234. {
  235. return I_IUTF8(tty) && is_utf8_continuation(c);
  236. }
  237. /**
  238. * do_output_char - output one character
  239. * @c: character (or partial unicode symbol)
  240. * @tty: terminal device
  241. * @space: space available in tty driver write buffer
  242. *
  243. * This is a helper function that handles one output character
  244. * (including special characters like TAB, CR, LF, etc.),
  245. * putting the results in the tty driver's write buffer.
  246. *
  247. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  248. * and NLDLY. They simply aren't relevant in the world today.
  249. * If you ever need them, add them here.
  250. *
  251. * Returns the number of bytes of buffer space used or -1 if
  252. * no space left.
  253. *
  254. * Locking: should be called under the output_lock to protect
  255. * the column state and space left in the buffer
  256. */
  257. static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
  258. {
  259. int spaces;
  260. if (!space)
  261. return -1;
  262. switch (c) {
  263. case '\n':
  264. if (O_ONLRET(tty))
  265. tty->column = 0;
  266. if (O_ONLCR(tty)) {
  267. if (space < 2)
  268. return -1;
  269. tty->canon_column = tty->column = 0;
  270. tty_put_char(tty, '\r');
  271. tty_put_char(tty, c);
  272. return 2;
  273. }
  274. tty->canon_column = tty->column;
  275. break;
  276. case '\r':
  277. if (O_ONOCR(tty) && tty->column == 0)
  278. return 0;
  279. if (O_OCRNL(tty)) {
  280. c = '\n';
  281. if (O_ONLRET(tty))
  282. tty->canon_column = tty->column = 0;
  283. break;
  284. }
  285. tty->canon_column = tty->column = 0;
  286. break;
  287. case '\t':
  288. spaces = 8 - (tty->column & 7);
  289. if (O_TABDLY(tty) == XTABS) {
  290. if (space < spaces)
  291. return -1;
  292. tty->column += spaces;
  293. tty->ops->write(tty, " ", spaces);
  294. return spaces;
  295. }
  296. tty->column += spaces;
  297. break;
  298. case '\b':
  299. if (tty->column > 0)
  300. tty->column--;
  301. break;
  302. default:
  303. if (!iscntrl(c)) {
  304. if (O_OLCUC(tty))
  305. c = toupper(c);
  306. if (!is_continuation(c, tty))
  307. tty->column++;
  308. }
  309. break;
  310. }
  311. tty_put_char(tty, c);
  312. return 1;
  313. }
  314. /**
  315. * process_output - output post processor
  316. * @c: character (or partial unicode symbol)
  317. * @tty: terminal device
  318. *
  319. * Perform OPOST processing. Returns -1 when the output device is
  320. * full and the character must be retried.
  321. *
  322. * Locking: output_lock to protect column state and space left
  323. * (also, this is called from n_tty_write under the
  324. * tty layer write lock)
  325. */
  326. static int process_output(unsigned char c, struct tty_struct *tty)
  327. {
  328. int space, retval;
  329. mutex_lock(&tty->output_lock);
  330. space = tty_write_room(tty);
  331. retval = do_output_char(c, tty, space);
  332. mutex_unlock(&tty->output_lock);
  333. if (retval < 0)
  334. return -1;
  335. else
  336. return 0;
  337. }
  338. /**
  339. * process_output_block - block post processor
  340. * @tty: terminal device
  341. * @inbuf: user buffer
  342. * @nr: number of bytes
  343. *
  344. * This path is used to speed up block console writes, among other
  345. * things when processing blocks of output data. It handles only
  346. * the simple cases normally found and helps to generate blocks of
  347. * symbols for the console driver and thus improve performance.
  348. *
  349. * Locking: output_lock to protect column state and space left
  350. * (also, this is called from n_tty_write under the
  351. * tty layer write lock)
  352. */
  353. static ssize_t process_output_block(struct tty_struct *tty,
  354. const unsigned char *buf, unsigned int nr)
  355. {
  356. int space;
  357. int i;
  358. const unsigned char *cp;
  359. mutex_lock(&tty->output_lock);
  360. space = tty_write_room(tty);
  361. if (!space) {
  362. mutex_unlock(&tty->output_lock);
  363. return 0;
  364. }
  365. if (nr > space)
  366. nr = space;
  367. for (i = 0, cp = buf; i < nr; i++, cp++) {
  368. unsigned char c = *cp;
  369. switch (c) {
  370. case '\n':
  371. if (O_ONLRET(tty))
  372. tty->column = 0;
  373. if (O_ONLCR(tty))
  374. goto break_out;
  375. tty->canon_column = tty->column;
  376. break;
  377. case '\r':
  378. if (O_ONOCR(tty) && tty->column == 0)
  379. goto break_out;
  380. if (O_OCRNL(tty))
  381. goto break_out;
  382. tty->canon_column = tty->column = 0;
  383. break;
  384. case '\t':
  385. goto break_out;
  386. case '\b':
  387. if (tty->column > 0)
  388. tty->column--;
  389. break;
  390. default:
  391. if (!iscntrl(c)) {
  392. if (O_OLCUC(tty))
  393. goto break_out;
  394. if (!is_continuation(c, tty))
  395. tty->column++;
  396. }
  397. break;
  398. }
  399. }
  400. break_out:
  401. i = tty->ops->write(tty, buf, i);
  402. mutex_unlock(&tty->output_lock);
  403. return i;
  404. }
  405. /**
  406. * process_echoes - write pending echo characters
  407. * @tty: terminal device
  408. *
  409. * Write previously buffered echo (and other ldisc-generated)
  410. * characters to the tty.
  411. *
  412. * Characters generated by the ldisc (including echoes) need to
  413. * be buffered because the driver's write buffer can fill during
  414. * heavy program output. Echoing straight to the driver will
  415. * often fail under these conditions, causing lost characters and
  416. * resulting mismatches of ldisc state information.
  417. *
  418. * Since the ldisc state must represent the characters actually sent
  419. * to the driver at the time of the write, operations like certain
  420. * changes in column state are also saved in the buffer and executed
  421. * here.
  422. *
  423. * A circular fifo buffer is used so that the most recent characters
  424. * are prioritized. Also, when control characters are echoed with a
  425. * prefixed "^", the pair is treated atomically and thus not separated.
  426. *
  427. * Locking: output_lock to protect column state and space left,
  428. * echo_lock to protect the echo buffer
  429. */
  430. static void process_echoes(struct tty_struct *tty)
  431. {
  432. int space, nr;
  433. unsigned char c;
  434. unsigned char *cp, *buf_end;
  435. if (!tty->echo_cnt)
  436. return;
  437. mutex_lock(&tty->output_lock);
  438. mutex_lock(&tty->echo_lock);
  439. space = tty_write_room(tty);
  440. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  441. cp = tty->echo_buf + tty->echo_pos;
  442. nr = tty->echo_cnt;
  443. while (nr > 0) {
  444. c = *cp;
  445. if (c == ECHO_OP_START) {
  446. unsigned char op;
  447. unsigned char *opp;
  448. int no_space_left = 0;
  449. /*
  450. * If the buffer byte is the start of a multi-byte
  451. * operation, get the next byte, which is either the
  452. * op code or a control character value.
  453. */
  454. opp = cp + 1;
  455. if (opp == buf_end)
  456. opp -= N_TTY_BUF_SIZE;
  457. op = *opp;
  458. switch (op) {
  459. unsigned int num_chars, num_bs;
  460. case ECHO_OP_ERASE_TAB:
  461. if (++opp == buf_end)
  462. opp -= N_TTY_BUF_SIZE;
  463. num_chars = *opp;
  464. /*
  465. * Determine how many columns to go back
  466. * in order to erase the tab.
  467. * This depends on the number of columns
  468. * used by other characters within the tab
  469. * area. If this (modulo 8) count is from
  470. * the start of input rather than from a
  471. * previous tab, we offset by canon column.
  472. * Otherwise, tab spacing is normal.
  473. */
  474. if (!(num_chars & 0x80))
  475. num_chars += tty->canon_column;
  476. num_bs = 8 - (num_chars & 7);
  477. if (num_bs > space) {
  478. no_space_left = 1;
  479. break;
  480. }
  481. space -= num_bs;
  482. while (num_bs--) {
  483. tty_put_char(tty, '\b');
  484. if (tty->column > 0)
  485. tty->column--;
  486. }
  487. cp += 3;
  488. nr -= 3;
  489. break;
  490. case ECHO_OP_SET_CANON_COL:
  491. tty->canon_column = tty->column;
  492. cp += 2;
  493. nr -= 2;
  494. break;
  495. case ECHO_OP_MOVE_BACK_COL:
  496. if (tty->column > 0)
  497. tty->column--;
  498. cp += 2;
  499. nr -= 2;
  500. break;
  501. case ECHO_OP_START:
  502. /* This is an escaped echo op start code */
  503. if (!space) {
  504. no_space_left = 1;
  505. break;
  506. }
  507. tty_put_char(tty, ECHO_OP_START);
  508. tty->column++;
  509. space--;
  510. cp += 2;
  511. nr -= 2;
  512. break;
  513. default:
  514. if (iscntrl(op)) {
  515. if (L_ECHOCTL(tty)) {
  516. /*
  517. * Ensure there is enough space
  518. * for the whole ctrl pair.
  519. */
  520. if (space < 2) {
  521. no_space_left = 1;
  522. break;
  523. }
  524. tty_put_char(tty, '^');
  525. tty_put_char(tty, op ^ 0100);
  526. tty->column += 2;
  527. space -= 2;
  528. } else {
  529. if (!space) {
  530. no_space_left = 1;
  531. break;
  532. }
  533. tty_put_char(tty, op);
  534. space--;
  535. }
  536. }
  537. /*
  538. * If above falls through, this was an
  539. * undefined op.
  540. */
  541. cp += 2;
  542. nr -= 2;
  543. }
  544. if (no_space_left)
  545. break;
  546. } else {
  547. int retval;
  548. retval = do_output_char(c, tty, space);
  549. if (retval < 0)
  550. break;
  551. space -= retval;
  552. cp += 1;
  553. nr -= 1;
  554. }
  555. /* When end of circular buffer reached, wrap around */
  556. if (cp >= buf_end)
  557. cp -= N_TTY_BUF_SIZE;
  558. }
  559. if (nr == 0) {
  560. tty->echo_pos = 0;
  561. tty->echo_cnt = 0;
  562. tty->echo_overrun = 0;
  563. } else {
  564. int num_processed = tty->echo_cnt - nr;
  565. tty->echo_pos += num_processed;
  566. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  567. tty->echo_cnt = nr;
  568. if (num_processed > 0)
  569. tty->echo_overrun = 0;
  570. }
  571. mutex_unlock(&tty->echo_lock);
  572. mutex_unlock(&tty->output_lock);
  573. if (tty->ops->flush_chars)
  574. tty->ops->flush_chars(tty);
  575. }
  576. /**
  577. * add_echo_byte - add a byte to the echo buffer
  578. * @c: unicode byte to echo
  579. * @tty: terminal device
  580. *
  581. * Add a character or operation byte to the echo buffer.
  582. *
  583. * Should be called under the echo lock to protect the echo buffer.
  584. */
  585. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  586. {
  587. int new_byte_pos;
  588. if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  589. /* Circular buffer is already at capacity */
  590. new_byte_pos = tty->echo_pos;
  591. /*
  592. * Since the buffer start position needs to be advanced,
  593. * be sure to step by a whole operation byte group.
  594. */
  595. if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  596. if (tty->echo_buf[(tty->echo_pos + 1) &
  597. (N_TTY_BUF_SIZE - 1)] ==
  598. ECHO_OP_ERASE_TAB) {
  599. tty->echo_pos += 3;
  600. tty->echo_cnt -= 2;
  601. } else {
  602. tty->echo_pos += 2;
  603. tty->echo_cnt -= 1;
  604. }
  605. } else {
  606. tty->echo_pos++;
  607. }
  608. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  609. tty->echo_overrun = 1;
  610. } else {
  611. new_byte_pos = tty->echo_pos + tty->echo_cnt;
  612. new_byte_pos &= N_TTY_BUF_SIZE - 1;
  613. tty->echo_cnt++;
  614. }
  615. tty->echo_buf[new_byte_pos] = c;
  616. }
  617. /**
  618. * echo_move_back_col - add operation to move back a column
  619. * @tty: terminal device
  620. *
  621. * Add an operation to the echo buffer to move back one column.
  622. *
  623. * Locking: echo_lock to protect the echo buffer
  624. */
  625. static void echo_move_back_col(struct tty_struct *tty)
  626. {
  627. mutex_lock(&tty->echo_lock);
  628. add_echo_byte(ECHO_OP_START, tty);
  629. add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  630. mutex_unlock(&tty->echo_lock);
  631. }
  632. /**
  633. * echo_set_canon_col - add operation to set the canon column
  634. * @tty: terminal device
  635. *
  636. * Add an operation to the echo buffer to set the canon column
  637. * to the current column.
  638. *
  639. * Locking: echo_lock to protect the echo buffer
  640. */
  641. static void echo_set_canon_col(struct tty_struct *tty)
  642. {
  643. mutex_lock(&tty->echo_lock);
  644. add_echo_byte(ECHO_OP_START, tty);
  645. add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  646. mutex_unlock(&tty->echo_lock);
  647. }
  648. /**
  649. * echo_erase_tab - add operation to erase a tab
  650. * @num_chars: number of character columns already used
  651. * @after_tab: true if num_chars starts after a previous tab
  652. * @tty: terminal device
  653. *
  654. * Add an operation to the echo buffer to erase a tab.
  655. *
  656. * Called by the eraser function, which knows how many character
  657. * columns have been used since either a previous tab or the start
  658. * of input. This information will be used later, along with
  659. * canon column (if applicable), to go back the correct number
  660. * of columns.
  661. *
  662. * Locking: echo_lock to protect the echo buffer
  663. */
  664. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  665. struct tty_struct *tty)
  666. {
  667. mutex_lock(&tty->echo_lock);
  668. add_echo_byte(ECHO_OP_START, tty);
  669. add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  670. /* We only need to know this modulo 8 (tab spacing) */
  671. num_chars &= 7;
  672. /* Set the high bit as a flag if num_chars is after a previous tab */
  673. if (after_tab)
  674. num_chars |= 0x80;
  675. add_echo_byte(num_chars, tty);
  676. mutex_unlock(&tty->echo_lock);
  677. }
  678. /**
  679. * echo_char_raw - echo a character raw
  680. * @c: unicode byte to echo
  681. * @tty: terminal device
  682. *
  683. * Echo user input back onto the screen. This must be called only when
  684. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  685. *
  686. * This variant does not treat control characters specially.
  687. *
  688. * Locking: echo_lock to protect the echo buffer
  689. */
  690. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  691. {
  692. mutex_lock(&tty->echo_lock);
  693. if (c == ECHO_OP_START) {
  694. add_echo_byte(ECHO_OP_START, tty);
  695. add_echo_byte(ECHO_OP_START, tty);
  696. } else {
  697. add_echo_byte(c, tty);
  698. }
  699. mutex_unlock(&tty->echo_lock);
  700. }
  701. /**
  702. * echo_char - echo a character
  703. * @c: unicode byte to echo
  704. * @tty: terminal device
  705. *
  706. * Echo user input back onto the screen. This must be called only when
  707. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  708. *
  709. * This variant tags control characters to be possibly echoed as
  710. * as "^X" (where X is the letter representing the control char).
  711. *
  712. * Locking: echo_lock to protect the echo buffer
  713. */
  714. static void echo_char(unsigned char c, struct tty_struct *tty)
  715. {
  716. mutex_lock(&tty->echo_lock);
  717. if (c == ECHO_OP_START) {
  718. add_echo_byte(ECHO_OP_START, tty);
  719. add_echo_byte(ECHO_OP_START, tty);
  720. } else {
  721. if (iscntrl(c) && c != '\t')
  722. add_echo_byte(ECHO_OP_START, tty);
  723. add_echo_byte(c, tty);
  724. }
  725. mutex_unlock(&tty->echo_lock);
  726. }
  727. /**
  728. * finish_erasing - complete erase
  729. * @tty: tty doing the erase
  730. */
  731. static inline void finish_erasing(struct tty_struct *tty)
  732. {
  733. if (tty->erasing) {
  734. echo_char_raw('/', tty);
  735. tty->erasing = 0;
  736. }
  737. }
  738. /**
  739. * eraser - handle erase function
  740. * @c: character input
  741. * @tty: terminal device
  742. *
  743. * Perform erase and necessary output when an erase character is
  744. * present in the stream from the driver layer. Handles the complexities
  745. * of UTF-8 multibyte symbols.
  746. *
  747. * Locking: read_lock for tty buffers
  748. */
  749. static void eraser(unsigned char c, struct tty_struct *tty)
  750. {
  751. enum { ERASE, WERASE, KILL } kill_type;
  752. int head, seen_alnums, cnt;
  753. unsigned long flags;
  754. /* FIXME: locking needed ? */
  755. if (tty->read_head == tty->canon_head) {
  756. /* process_output('\a', tty); */ /* what do you think? */
  757. return;
  758. }
  759. if (c == ERASE_CHAR(tty))
  760. kill_type = ERASE;
  761. else if (c == WERASE_CHAR(tty))
  762. kill_type = WERASE;
  763. else {
  764. if (!L_ECHO(tty)) {
  765. spin_lock_irqsave(&tty->read_lock, flags);
  766. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  767. (N_TTY_BUF_SIZE - 1));
  768. tty->read_head = tty->canon_head;
  769. spin_unlock_irqrestore(&tty->read_lock, flags);
  770. return;
  771. }
  772. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  773. spin_lock_irqsave(&tty->read_lock, flags);
  774. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  775. (N_TTY_BUF_SIZE - 1));
  776. tty->read_head = tty->canon_head;
  777. spin_unlock_irqrestore(&tty->read_lock, flags);
  778. finish_erasing(tty);
  779. echo_char(KILL_CHAR(tty), tty);
  780. /* Add a newline if ECHOK is on and ECHOKE is off. */
  781. if (L_ECHOK(tty))
  782. echo_char_raw('\n', tty);
  783. return;
  784. }
  785. kill_type = KILL;
  786. }
  787. seen_alnums = 0;
  788. /* FIXME: Locking ?? */
  789. while (tty->read_head != tty->canon_head) {
  790. head = tty->read_head;
  791. /* erase a single possibly multibyte character */
  792. do {
  793. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  794. c = tty->read_buf[head];
  795. } while (is_continuation(c, tty) && head != tty->canon_head);
  796. /* do not partially erase */
  797. if (is_continuation(c, tty))
  798. break;
  799. if (kill_type == WERASE) {
  800. /* Equivalent to BSD's ALTWERASE. */
  801. if (isalnum(c) || c == '_')
  802. seen_alnums++;
  803. else if (seen_alnums)
  804. break;
  805. }
  806. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  807. spin_lock_irqsave(&tty->read_lock, flags);
  808. tty->read_head = head;
  809. tty->read_cnt -= cnt;
  810. spin_unlock_irqrestore(&tty->read_lock, flags);
  811. if (L_ECHO(tty)) {
  812. if (L_ECHOPRT(tty)) {
  813. if (!tty->erasing) {
  814. echo_char_raw('\\', tty);
  815. tty->erasing = 1;
  816. }
  817. /* if cnt > 1, output a multi-byte character */
  818. echo_char(c, tty);
  819. while (--cnt > 0) {
  820. head = (head+1) & (N_TTY_BUF_SIZE-1);
  821. echo_char_raw(tty->read_buf[head], tty);
  822. echo_move_back_col(tty);
  823. }
  824. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  825. echo_char(ERASE_CHAR(tty), tty);
  826. } else if (c == '\t') {
  827. unsigned int num_chars = 0;
  828. int after_tab = 0;
  829. unsigned long tail = tty->read_head;
  830. /*
  831. * Count the columns used for characters
  832. * since the start of input or after a
  833. * previous tab.
  834. * This info is used to go back the correct
  835. * number of columns.
  836. */
  837. while (tail != tty->canon_head) {
  838. tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  839. c = tty->read_buf[tail];
  840. if (c == '\t') {
  841. after_tab = 1;
  842. break;
  843. } else if (iscntrl(c)) {
  844. if (L_ECHOCTL(tty))
  845. num_chars += 2;
  846. } else if (!is_continuation(c, tty)) {
  847. num_chars++;
  848. }
  849. }
  850. echo_erase_tab(num_chars, after_tab, tty);
  851. } else {
  852. if (iscntrl(c) && L_ECHOCTL(tty)) {
  853. echo_char_raw('\b', tty);
  854. echo_char_raw(' ', tty);
  855. echo_char_raw('\b', tty);
  856. }
  857. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  858. echo_char_raw('\b', tty);
  859. echo_char_raw(' ', tty);
  860. echo_char_raw('\b', tty);
  861. }
  862. }
  863. }
  864. if (kill_type == ERASE)
  865. break;
  866. }
  867. if (tty->read_head == tty->canon_head && L_ECHO(tty))
  868. finish_erasing(tty);
  869. }
  870. /**
  871. * isig - handle the ISIG optio
  872. * @sig: signal
  873. * @tty: terminal
  874. * @flush: force flush
  875. *
  876. * Called when a signal is being sent due to terminal input. This
  877. * may caus terminal flushing to take place according to the termios
  878. * settings and character used. Called from the driver receive_buf
  879. * path so serialized.
  880. *
  881. * Locking: ctrl_lock, read_lock (both via flush buffer)
  882. */
  883. static inline void isig(int sig, struct tty_struct *tty, int flush)
  884. {
  885. if (tty->pgrp)
  886. kill_pgrp(tty->pgrp, sig, 1);
  887. if (flush || !L_NOFLSH(tty)) {
  888. n_tty_flush_buffer(tty);
  889. tty_driver_flush_buffer(tty);
  890. }
  891. }
  892. /**
  893. * n_tty_receive_break - handle break
  894. * @tty: terminal
  895. *
  896. * An RS232 break event has been hit in the incoming bitstream. This
  897. * can cause a variety of events depending upon the termios settings.
  898. *
  899. * Called from the receive_buf path so single threaded.
  900. */
  901. static inline void n_tty_receive_break(struct tty_struct *tty)
  902. {
  903. if (I_IGNBRK(tty))
  904. return;
  905. if (I_BRKINT(tty)) {
  906. isig(SIGINT, tty, 1);
  907. return;
  908. }
  909. if (I_PARMRK(tty)) {
  910. put_tty_queue('\377', tty);
  911. put_tty_queue('\0', tty);
  912. }
  913. put_tty_queue('\0', tty);
  914. wake_up_interruptible(&tty->read_wait);
  915. }
  916. /**
  917. * n_tty_receive_overrun - handle overrun reporting
  918. * @tty: terminal
  919. *
  920. * Data arrived faster than we could process it. While the tty
  921. * driver has flagged this the bits that were missed are gone
  922. * forever.
  923. *
  924. * Called from the receive_buf path so single threaded. Does not
  925. * need locking as num_overrun and overrun_time are function
  926. * private.
  927. */
  928. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  929. {
  930. char buf[64];
  931. tty->num_overrun++;
  932. if (time_before(tty->overrun_time, jiffies - HZ) ||
  933. time_after(tty->overrun_time, jiffies)) {
  934. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  935. tty_name(tty, buf),
  936. tty->num_overrun);
  937. tty->overrun_time = jiffies;
  938. tty->num_overrun = 0;
  939. }
  940. }
  941. /**
  942. * n_tty_receive_parity_error - error notifier
  943. * @tty: terminal device
  944. * @c: character
  945. *
  946. * Process a parity error and queue the right data to indicate
  947. * the error case if necessary. Locking as per n_tty_receive_buf.
  948. */
  949. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  950. unsigned char c)
  951. {
  952. if (I_IGNPAR(tty))
  953. return;
  954. if (I_PARMRK(tty)) {
  955. put_tty_queue('\377', tty);
  956. put_tty_queue('\0', tty);
  957. put_tty_queue(c, tty);
  958. } else if (I_INPCK(tty))
  959. put_tty_queue('\0', tty);
  960. else
  961. put_tty_queue(c, tty);
  962. wake_up_interruptible(&tty->read_wait);
  963. }
  964. /**
  965. * n_tty_receive_char - perform processing
  966. * @tty: terminal device
  967. * @c: character
  968. *
  969. * Process an individual character of input received from the driver.
  970. * This is serialized with respect to itself by the rules for the
  971. * driver above.
  972. */
  973. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  974. {
  975. unsigned long flags;
  976. int parmrk;
  977. if (tty->raw) {
  978. put_tty_queue(c, tty);
  979. return;
  980. }
  981. if (I_ISTRIP(tty))
  982. c &= 0x7f;
  983. if (I_IUCLC(tty) && L_IEXTEN(tty))
  984. c = tolower(c);
  985. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  986. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  987. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  988. start_tty(tty);
  989. process_echoes(tty);
  990. }
  991. if (tty->closing) {
  992. if (I_IXON(tty)) {
  993. if (c == START_CHAR(tty)) {
  994. start_tty(tty);
  995. process_echoes(tty);
  996. } else if (c == STOP_CHAR(tty))
  997. stop_tty(tty);
  998. }
  999. return;
  1000. }
  1001. /*
  1002. * If the previous character was LNEXT, or we know that this
  1003. * character is not one of the characters that we'll have to
  1004. * handle specially, do shortcut processing to speed things
  1005. * up.
  1006. */
  1007. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1008. tty->lnext = 0;
  1009. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1010. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1011. /* beep if no space */
  1012. if (L_ECHO(tty))
  1013. process_output('\a', tty);
  1014. return;
  1015. }
  1016. if (L_ECHO(tty)) {
  1017. finish_erasing(tty);
  1018. /* Record the column of first canon char. */
  1019. if (tty->canon_head == tty->read_head)
  1020. echo_set_canon_col(tty);
  1021. echo_char(c, tty);
  1022. process_echoes(tty);
  1023. }
  1024. if (parmrk)
  1025. put_tty_queue(c, tty);
  1026. put_tty_queue(c, tty);
  1027. return;
  1028. }
  1029. if (I_IXON(tty)) {
  1030. if (c == START_CHAR(tty)) {
  1031. start_tty(tty);
  1032. process_echoes(tty);
  1033. return;
  1034. }
  1035. if (c == STOP_CHAR(tty)) {
  1036. stop_tty(tty);
  1037. return;
  1038. }
  1039. }
  1040. if (L_ISIG(tty)) {
  1041. int signal;
  1042. signal = SIGINT;
  1043. if (c == INTR_CHAR(tty))
  1044. goto send_signal;
  1045. signal = SIGQUIT;
  1046. if (c == QUIT_CHAR(tty))
  1047. goto send_signal;
  1048. signal = SIGTSTP;
  1049. if (c == SUSP_CHAR(tty)) {
  1050. send_signal:
  1051. /*
  1052. * Note that we do not use isig() here because we want
  1053. * the order to be:
  1054. * 1) flush, 2) echo, 3) signal
  1055. */
  1056. if (!L_NOFLSH(tty)) {
  1057. n_tty_flush_buffer(tty);
  1058. tty_driver_flush_buffer(tty);
  1059. }
  1060. if (I_IXON(tty))
  1061. start_tty(tty);
  1062. if (L_ECHO(tty)) {
  1063. echo_char(c, tty);
  1064. process_echoes(tty);
  1065. }
  1066. if (tty->pgrp)
  1067. kill_pgrp(tty->pgrp, signal, 1);
  1068. return;
  1069. }
  1070. }
  1071. if (c == '\r') {
  1072. if (I_IGNCR(tty))
  1073. return;
  1074. if (I_ICRNL(tty))
  1075. c = '\n';
  1076. } else if (c == '\n' && I_INLCR(tty))
  1077. c = '\r';
  1078. if (tty->icanon) {
  1079. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1080. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1081. eraser(c, tty);
  1082. process_echoes(tty);
  1083. return;
  1084. }
  1085. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1086. tty->lnext = 1;
  1087. if (L_ECHO(tty)) {
  1088. finish_erasing(tty);
  1089. if (L_ECHOCTL(tty)) {
  1090. echo_char_raw('^', tty);
  1091. echo_char_raw('\b', tty);
  1092. process_echoes(tty);
  1093. }
  1094. }
  1095. return;
  1096. }
  1097. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1098. L_IEXTEN(tty)) {
  1099. unsigned long tail = tty->canon_head;
  1100. finish_erasing(tty);
  1101. echo_char(c, tty);
  1102. echo_char_raw('\n', tty);
  1103. while (tail != tty->read_head) {
  1104. echo_char(tty->read_buf[tail], tty);
  1105. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1106. }
  1107. process_echoes(tty);
  1108. return;
  1109. }
  1110. if (c == '\n') {
  1111. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1112. if (L_ECHO(tty))
  1113. process_output('\a', tty);
  1114. return;
  1115. }
  1116. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1117. echo_char_raw('\n', tty);
  1118. process_echoes(tty);
  1119. }
  1120. goto handle_newline;
  1121. }
  1122. if (c == EOF_CHAR(tty)) {
  1123. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1124. return;
  1125. if (tty->canon_head != tty->read_head)
  1126. set_bit(TTY_PUSH, &tty->flags);
  1127. c = __DISABLED_CHAR;
  1128. goto handle_newline;
  1129. }
  1130. if ((c == EOL_CHAR(tty)) ||
  1131. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1132. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1133. ? 1 : 0;
  1134. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1135. if (L_ECHO(tty))
  1136. process_output('\a', tty);
  1137. return;
  1138. }
  1139. /*
  1140. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1141. */
  1142. if (L_ECHO(tty)) {
  1143. /* Record the column of first canon char. */
  1144. if (tty->canon_head == tty->read_head)
  1145. echo_set_canon_col(tty);
  1146. echo_char(c, tty);
  1147. process_echoes(tty);
  1148. }
  1149. /*
  1150. * XXX does PARMRK doubling happen for
  1151. * EOL_CHAR and EOL2_CHAR?
  1152. */
  1153. if (parmrk)
  1154. put_tty_queue(c, tty);
  1155. handle_newline:
  1156. spin_lock_irqsave(&tty->read_lock, flags);
  1157. set_bit(tty->read_head, tty->read_flags);
  1158. put_tty_queue_nolock(c, tty);
  1159. tty->canon_head = tty->read_head;
  1160. tty->canon_data++;
  1161. spin_unlock_irqrestore(&tty->read_lock, flags);
  1162. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1163. if (waitqueue_active(&tty->read_wait))
  1164. wake_up_interruptible(&tty->read_wait);
  1165. return;
  1166. }
  1167. }
  1168. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1169. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1170. /* beep if no space */
  1171. if (L_ECHO(tty))
  1172. process_output('\a', tty);
  1173. return;
  1174. }
  1175. if (L_ECHO(tty)) {
  1176. finish_erasing(tty);
  1177. if (c == '\n')
  1178. echo_char_raw('\n', tty);
  1179. else {
  1180. /* Record the column of first canon char. */
  1181. if (tty->canon_head == tty->read_head)
  1182. echo_set_canon_col(tty);
  1183. echo_char(c, tty);
  1184. }
  1185. process_echoes(tty);
  1186. }
  1187. if (parmrk)
  1188. put_tty_queue(c, tty);
  1189. put_tty_queue(c, tty);
  1190. }
  1191. /**
  1192. * n_tty_write_wakeup - asynchronous I/O notifier
  1193. * @tty: tty device
  1194. *
  1195. * Required for the ptys, serial driver etc. since processes
  1196. * that attach themselves to the master and rely on ASYNC
  1197. * IO must be woken up
  1198. */
  1199. static void n_tty_write_wakeup(struct tty_struct *tty)
  1200. {
  1201. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1202. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1203. }
  1204. /**
  1205. * n_tty_receive_buf - data receive
  1206. * @tty: terminal device
  1207. * @cp: buffer
  1208. * @fp: flag buffer
  1209. * @count: characters
  1210. *
  1211. * Called by the terminal driver when a block of characters has
  1212. * been received. This function must be called from soft contexts
  1213. * not from interrupt context. The driver is responsible for making
  1214. * calls one at a time and in order (or using flush_to_ldisc)
  1215. */
  1216. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1217. char *fp, int count)
  1218. {
  1219. const unsigned char *p;
  1220. char *f, flags = TTY_NORMAL;
  1221. int i;
  1222. char buf[64];
  1223. unsigned long cpuflags;
  1224. if (!tty->read_buf)
  1225. return;
  1226. if (tty->real_raw) {
  1227. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1228. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1229. N_TTY_BUF_SIZE - tty->read_head);
  1230. i = min(count, i);
  1231. memcpy(tty->read_buf + tty->read_head, cp, i);
  1232. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1233. tty->read_cnt += i;
  1234. cp += i;
  1235. count -= i;
  1236. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1237. N_TTY_BUF_SIZE - tty->read_head);
  1238. i = min(count, i);
  1239. memcpy(tty->read_buf + tty->read_head, cp, i);
  1240. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1241. tty->read_cnt += i;
  1242. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1243. } else {
  1244. for (i = count, p = cp, f = fp; i; i--, p++) {
  1245. if (f)
  1246. flags = *f++;
  1247. switch (flags) {
  1248. case TTY_NORMAL:
  1249. n_tty_receive_char(tty, *p);
  1250. break;
  1251. case TTY_BREAK:
  1252. n_tty_receive_break(tty);
  1253. break;
  1254. case TTY_PARITY:
  1255. case TTY_FRAME:
  1256. n_tty_receive_parity_error(tty, *p);
  1257. break;
  1258. case TTY_OVERRUN:
  1259. n_tty_receive_overrun(tty);
  1260. break;
  1261. default:
  1262. printk(KERN_ERR "%s: unknown flag %d\n",
  1263. tty_name(tty, buf), flags);
  1264. break;
  1265. }
  1266. }
  1267. if (tty->ops->flush_chars)
  1268. tty->ops->flush_chars(tty);
  1269. }
  1270. n_tty_set_room(tty);
  1271. if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  1272. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1273. if (waitqueue_active(&tty->read_wait))
  1274. wake_up_interruptible(&tty->read_wait);
  1275. }
  1276. /*
  1277. * Check the remaining room for the input canonicalization
  1278. * mode. We don't want to throttle the driver if we're in
  1279. * canonical mode and don't have a newline yet!
  1280. */
  1281. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1282. tty_throttle(tty);
  1283. }
  1284. int is_ignored(int sig)
  1285. {
  1286. return (sigismember(&current->blocked, sig) ||
  1287. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1288. }
  1289. /**
  1290. * n_tty_set_termios - termios data changed
  1291. * @tty: terminal
  1292. * @old: previous data
  1293. *
  1294. * Called by the tty layer when the user changes termios flags so
  1295. * that the line discipline can plan ahead. This function cannot sleep
  1296. * and is protected from re-entry by the tty layer. The user is
  1297. * guaranteed that this function will not be re-entered or in progress
  1298. * when the ldisc is closed.
  1299. *
  1300. * Locking: Caller holds tty->termios_mutex
  1301. */
  1302. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1303. {
  1304. int canon_change = 1;
  1305. BUG_ON(!tty);
  1306. if (old)
  1307. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1308. if (canon_change) {
  1309. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1310. tty->canon_head = tty->read_tail;
  1311. tty->canon_data = 0;
  1312. tty->erasing = 0;
  1313. }
  1314. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1315. wake_up_interruptible(&tty->read_wait);
  1316. tty->icanon = (L_ICANON(tty) != 0);
  1317. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1318. tty->raw = 1;
  1319. tty->real_raw = 1;
  1320. n_tty_set_room(tty);
  1321. return;
  1322. }
  1323. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1324. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1325. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1326. I_PARMRK(tty)) {
  1327. memset(tty->process_char_map, 0, 256/8);
  1328. if (I_IGNCR(tty) || I_ICRNL(tty))
  1329. set_bit('\r', tty->process_char_map);
  1330. if (I_INLCR(tty))
  1331. set_bit('\n', tty->process_char_map);
  1332. if (L_ICANON(tty)) {
  1333. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1334. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1335. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1336. set_bit('\n', tty->process_char_map);
  1337. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1338. if (L_IEXTEN(tty)) {
  1339. set_bit(WERASE_CHAR(tty),
  1340. tty->process_char_map);
  1341. set_bit(LNEXT_CHAR(tty),
  1342. tty->process_char_map);
  1343. set_bit(EOL2_CHAR(tty),
  1344. tty->process_char_map);
  1345. if (L_ECHO(tty))
  1346. set_bit(REPRINT_CHAR(tty),
  1347. tty->process_char_map);
  1348. }
  1349. }
  1350. if (I_IXON(tty)) {
  1351. set_bit(START_CHAR(tty), tty->process_char_map);
  1352. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1353. }
  1354. if (L_ISIG(tty)) {
  1355. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1356. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1357. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1358. }
  1359. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1360. tty->raw = 0;
  1361. tty->real_raw = 0;
  1362. } else {
  1363. tty->raw = 1;
  1364. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1365. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1366. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1367. tty->real_raw = 1;
  1368. else
  1369. tty->real_raw = 0;
  1370. }
  1371. n_tty_set_room(tty);
  1372. /* The termios change make the tty ready for I/O */
  1373. wake_up_interruptible(&tty->write_wait);
  1374. wake_up_interruptible(&tty->read_wait);
  1375. }
  1376. /**
  1377. * n_tty_close - close the ldisc for this tty
  1378. * @tty: device
  1379. *
  1380. * Called from the terminal layer when this line discipline is
  1381. * being shut down, either because of a close or becsuse of a
  1382. * discipline change. The function will not be called while other
  1383. * ldisc methods are in progress.
  1384. */
  1385. static void n_tty_close(struct tty_struct *tty)
  1386. {
  1387. n_tty_flush_buffer(tty);
  1388. if (tty->read_buf) {
  1389. kfree(tty->read_buf);
  1390. tty->read_buf = NULL;
  1391. }
  1392. if (tty->echo_buf) {
  1393. kfree(tty->echo_buf);
  1394. tty->echo_buf = NULL;
  1395. }
  1396. }
  1397. /**
  1398. * n_tty_open - open an ldisc
  1399. * @tty: terminal to open
  1400. *
  1401. * Called when this line discipline is being attached to the
  1402. * terminal device. Can sleep. Called serialized so that no
  1403. * other events will occur in parallel. No further open will occur
  1404. * until a close.
  1405. */
  1406. static int n_tty_open(struct tty_struct *tty)
  1407. {
  1408. if (!tty)
  1409. return -EINVAL;
  1410. /* These are ugly. Currently a malloc failure here can panic */
  1411. if (!tty->read_buf) {
  1412. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1413. if (!tty->read_buf)
  1414. return -ENOMEM;
  1415. }
  1416. if (!tty->echo_buf) {
  1417. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1418. if (!tty->echo_buf)
  1419. return -ENOMEM;
  1420. }
  1421. reset_buffer_flags(tty);
  1422. tty->column = 0;
  1423. n_tty_set_termios(tty, NULL);
  1424. tty->minimum_to_wake = 1;
  1425. tty->closing = 0;
  1426. return 0;
  1427. }
  1428. static inline int input_available_p(struct tty_struct *tty, int amt)
  1429. {
  1430. tty_flush_to_ldisc(tty);
  1431. if (tty->icanon) {
  1432. if (tty->canon_data)
  1433. return 1;
  1434. } else if (tty->read_cnt >= (amt ? amt : 1))
  1435. return 1;
  1436. return 0;
  1437. }
  1438. /**
  1439. * copy_from_read_buf - copy read data directly
  1440. * @tty: terminal device
  1441. * @b: user data
  1442. * @nr: size of data
  1443. *
  1444. * Helper function to speed up n_tty_read. It is only called when
  1445. * ICANON is off; it copies characters straight from the tty queue to
  1446. * user space directly. It can be profitably called twice; once to
  1447. * drain the space from the tail pointer to the (physical) end of the
  1448. * buffer, and once to drain the space from the (physical) beginning of
  1449. * the buffer to head pointer.
  1450. *
  1451. * Called under the tty->atomic_read_lock sem
  1452. *
  1453. */
  1454. static int copy_from_read_buf(struct tty_struct *tty,
  1455. unsigned char __user **b,
  1456. size_t *nr)
  1457. {
  1458. int retval;
  1459. size_t n;
  1460. unsigned long flags;
  1461. retval = 0;
  1462. spin_lock_irqsave(&tty->read_lock, flags);
  1463. n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1464. n = min(*nr, n);
  1465. spin_unlock_irqrestore(&tty->read_lock, flags);
  1466. if (n) {
  1467. retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1468. n -= retval;
  1469. tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1470. spin_lock_irqsave(&tty->read_lock, flags);
  1471. tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1472. tty->read_cnt -= n;
  1473. spin_unlock_irqrestore(&tty->read_lock, flags);
  1474. *b += n;
  1475. *nr -= n;
  1476. }
  1477. return retval;
  1478. }
  1479. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1480. size_t, loff_t *);
  1481. /**
  1482. * job_control - check job control
  1483. * @tty: tty
  1484. * @file: file handle
  1485. *
  1486. * Perform job control management checks on this file/tty descriptor
  1487. * and if appropriate send any needed signals and return a negative
  1488. * error code if action should be taken.
  1489. *
  1490. * FIXME:
  1491. * Locking: None - redirected write test is safe, testing
  1492. * current->signal should possibly lock current->sighand
  1493. * pgrp locking ?
  1494. */
  1495. static int job_control(struct tty_struct *tty, struct file *file)
  1496. {
  1497. /* Job control check -- must be done at start and after
  1498. every sleep (POSIX.1 7.1.1.4). */
  1499. /* NOTE: not yet done after every sleep pending a thorough
  1500. check of the logic of this change. -- jlc */
  1501. /* don't stop on /dev/console */
  1502. if (file->f_op->write != redirected_tty_write &&
  1503. current->signal->tty == tty) {
  1504. if (!tty->pgrp)
  1505. printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
  1506. else if (task_pgrp(current) != tty->pgrp) {
  1507. if (is_ignored(SIGTTIN) ||
  1508. is_current_pgrp_orphaned())
  1509. return -EIO;
  1510. kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1511. set_thread_flag(TIF_SIGPENDING);
  1512. return -ERESTARTSYS;
  1513. }
  1514. }
  1515. return 0;
  1516. }
  1517. /**
  1518. * n_tty_read - read function for tty
  1519. * @tty: tty device
  1520. * @file: file object
  1521. * @buf: userspace buffer pointer
  1522. * @nr: size of I/O
  1523. *
  1524. * Perform reads for the line discipline. We are guaranteed that the
  1525. * line discipline will not be closed under us but we may get multiple
  1526. * parallel readers and must handle this ourselves. We may also get
  1527. * a hangup. Always called in user context, may sleep.
  1528. *
  1529. * This code must be sure never to sleep through a hangup.
  1530. */
  1531. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1532. unsigned char __user *buf, size_t nr)
  1533. {
  1534. unsigned char __user *b = buf;
  1535. DECLARE_WAITQUEUE(wait, current);
  1536. int c;
  1537. int minimum, time;
  1538. ssize_t retval = 0;
  1539. ssize_t size;
  1540. long timeout;
  1541. unsigned long flags;
  1542. int packet;
  1543. do_it_again:
  1544. BUG_ON(!tty->read_buf);
  1545. c = job_control(tty, file);
  1546. if (c < 0)
  1547. return c;
  1548. minimum = time = 0;
  1549. timeout = MAX_SCHEDULE_TIMEOUT;
  1550. if (!tty->icanon) {
  1551. time = (HZ / 10) * TIME_CHAR(tty);
  1552. minimum = MIN_CHAR(tty);
  1553. if (minimum) {
  1554. if (time)
  1555. tty->minimum_to_wake = 1;
  1556. else if (!waitqueue_active(&tty->read_wait) ||
  1557. (tty->minimum_to_wake > minimum))
  1558. tty->minimum_to_wake = minimum;
  1559. } else {
  1560. timeout = 0;
  1561. if (time) {
  1562. timeout = time;
  1563. time = 0;
  1564. }
  1565. tty->minimum_to_wake = minimum = 1;
  1566. }
  1567. }
  1568. /*
  1569. * Internal serialization of reads.
  1570. */
  1571. if (file->f_flags & O_NONBLOCK) {
  1572. if (!mutex_trylock(&tty->atomic_read_lock))
  1573. return -EAGAIN;
  1574. } else {
  1575. if (mutex_lock_interruptible(&tty->atomic_read_lock))
  1576. return -ERESTARTSYS;
  1577. }
  1578. packet = tty->packet;
  1579. add_wait_queue(&tty->read_wait, &wait);
  1580. while (nr) {
  1581. /* First test for status change. */
  1582. if (packet && tty->link->ctrl_status) {
  1583. unsigned char cs;
  1584. if (b != buf)
  1585. break;
  1586. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  1587. cs = tty->link->ctrl_status;
  1588. tty->link->ctrl_status = 0;
  1589. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  1590. if (tty_put_user(tty, cs, b++)) {
  1591. retval = -EFAULT;
  1592. b--;
  1593. break;
  1594. }
  1595. nr--;
  1596. break;
  1597. }
  1598. /* This statement must be first before checking for input
  1599. so that any interrupt will set the state back to
  1600. TASK_RUNNING. */
  1601. set_current_state(TASK_INTERRUPTIBLE);
  1602. if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  1603. ((minimum - (b - buf)) >= 1))
  1604. tty->minimum_to_wake = (minimum - (b - buf));
  1605. if (!input_available_p(tty, 0)) {
  1606. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1607. retval = -EIO;
  1608. break;
  1609. }
  1610. if (tty_hung_up_p(file))
  1611. break;
  1612. if (!timeout)
  1613. break;
  1614. if (file->f_flags & O_NONBLOCK) {
  1615. retval = -EAGAIN;
  1616. break;
  1617. }
  1618. if (signal_pending(current)) {
  1619. retval = -ERESTARTSYS;
  1620. break;
  1621. }
  1622. /* FIXME: does n_tty_set_room need locking ? */
  1623. n_tty_set_room(tty);
  1624. timeout = schedule_timeout(timeout);
  1625. continue;
  1626. }
  1627. __set_current_state(TASK_RUNNING);
  1628. /* Deal with packet mode. */
  1629. if (packet && b == buf) {
  1630. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  1631. retval = -EFAULT;
  1632. b--;
  1633. break;
  1634. }
  1635. nr--;
  1636. }
  1637. if (tty->icanon) {
  1638. /* N.B. avoid overrun if nr == 0 */
  1639. while (nr && tty->read_cnt) {
  1640. int eol;
  1641. eol = test_and_clear_bit(tty->read_tail,
  1642. tty->read_flags);
  1643. c = tty->read_buf[tty->read_tail];
  1644. spin_lock_irqsave(&tty->read_lock, flags);
  1645. tty->read_tail = ((tty->read_tail+1) &
  1646. (N_TTY_BUF_SIZE-1));
  1647. tty->read_cnt--;
  1648. if (eol) {
  1649. /* this test should be redundant:
  1650. * we shouldn't be reading data if
  1651. * canon_data is 0
  1652. */
  1653. if (--tty->canon_data < 0)
  1654. tty->canon_data = 0;
  1655. }
  1656. spin_unlock_irqrestore(&tty->read_lock, flags);
  1657. if (!eol || (c != __DISABLED_CHAR)) {
  1658. if (tty_put_user(tty, c, b++)) {
  1659. retval = -EFAULT;
  1660. b--;
  1661. break;
  1662. }
  1663. nr--;
  1664. }
  1665. if (eol) {
  1666. tty_audit_push(tty);
  1667. break;
  1668. }
  1669. }
  1670. if (retval)
  1671. break;
  1672. } else {
  1673. int uncopied;
  1674. /* The copy function takes the read lock and handles
  1675. locking internally for this case */
  1676. uncopied = copy_from_read_buf(tty, &b, &nr);
  1677. uncopied += copy_from_read_buf(tty, &b, &nr);
  1678. if (uncopied) {
  1679. retval = -EFAULT;
  1680. break;
  1681. }
  1682. }
  1683. /* If there is enough space in the read buffer now, let the
  1684. * low-level driver know. We use n_tty_chars_in_buffer() to
  1685. * check the buffer, as it now knows about canonical mode.
  1686. * Otherwise, if the driver is throttled and the line is
  1687. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  1688. * we won't get any more characters.
  1689. */
  1690. if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
  1691. n_tty_set_room(tty);
  1692. check_unthrottle(tty);
  1693. }
  1694. if (b - buf >= minimum)
  1695. break;
  1696. if (time)
  1697. timeout = time;
  1698. }
  1699. mutex_unlock(&tty->atomic_read_lock);
  1700. remove_wait_queue(&tty->read_wait, &wait);
  1701. if (!waitqueue_active(&tty->read_wait))
  1702. tty->minimum_to_wake = minimum;
  1703. __set_current_state(TASK_RUNNING);
  1704. size = b - buf;
  1705. if (size) {
  1706. retval = size;
  1707. if (nr)
  1708. clear_bit(TTY_PUSH, &tty->flags);
  1709. } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
  1710. goto do_it_again;
  1711. n_tty_set_room(tty);
  1712. return retval;
  1713. }
  1714. /**
  1715. * n_tty_write - write function for tty
  1716. * @tty: tty device
  1717. * @file: file object
  1718. * @buf: userspace buffer pointer
  1719. * @nr: size of I/O
  1720. *
  1721. * Write function of the terminal device. This is serialized with
  1722. * respect to other write callers but not to termios changes, reads
  1723. * and other such events. Since the receive code will echo characters,
  1724. * thus calling driver write methods, the output_lock is used in
  1725. * the output processing functions called here as well as in the
  1726. * echo processing function to protect the column state and space
  1727. * left in the buffer.
  1728. *
  1729. * This code must be sure never to sleep through a hangup.
  1730. *
  1731. * Locking: output_lock to protect column state and space left
  1732. * (note that the process_output*() functions take this
  1733. * lock themselves)
  1734. */
  1735. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  1736. const unsigned char *buf, size_t nr)
  1737. {
  1738. const unsigned char *b = buf;
  1739. DECLARE_WAITQUEUE(wait, current);
  1740. int c;
  1741. ssize_t retval = 0;
  1742. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  1743. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  1744. retval = tty_check_change(tty);
  1745. if (retval)
  1746. return retval;
  1747. }
  1748. /* Write out any echoed characters that are still pending */
  1749. process_echoes(tty);
  1750. add_wait_queue(&tty->write_wait, &wait);
  1751. while (1) {
  1752. set_current_state(TASK_INTERRUPTIBLE);
  1753. if (signal_pending(current)) {
  1754. retval = -ERESTARTSYS;
  1755. break;
  1756. }
  1757. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  1758. retval = -EIO;
  1759. break;
  1760. }
  1761. if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  1762. while (nr > 0) {
  1763. ssize_t num = process_output_block(tty, b, nr);
  1764. if (num < 0) {
  1765. if (num == -EAGAIN)
  1766. break;
  1767. retval = num;
  1768. goto break_out;
  1769. }
  1770. b += num;
  1771. nr -= num;
  1772. if (nr == 0)
  1773. break;
  1774. c = *b;
  1775. if (process_output(c, tty) < 0)
  1776. break;
  1777. b++; nr--;
  1778. }
  1779. if (tty->ops->flush_chars)
  1780. tty->ops->flush_chars(tty);
  1781. } else {
  1782. while (nr > 0) {
  1783. c = tty->ops->write(tty, b, nr);
  1784. if (c < 0) {
  1785. retval = c;
  1786. goto break_out;
  1787. }
  1788. if (!c)
  1789. break;
  1790. b += c;
  1791. nr -= c;
  1792. }
  1793. }
  1794. if (!nr)
  1795. break;
  1796. if (file->f_flags & O_NONBLOCK) {
  1797. retval = -EAGAIN;
  1798. break;
  1799. }
  1800. schedule();
  1801. }
  1802. break_out:
  1803. __set_current_state(TASK_RUNNING);
  1804. remove_wait_queue(&tty->write_wait, &wait);
  1805. if (b - buf != nr && tty->fasync)
  1806. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  1807. return (b - buf) ? b - buf : retval;
  1808. }
  1809. /**
  1810. * n_tty_poll - poll method for N_TTY
  1811. * @tty: terminal device
  1812. * @file: file accessing it
  1813. * @wait: poll table
  1814. *
  1815. * Called when the line discipline is asked to poll() for data or
  1816. * for special events. This code is not serialized with respect to
  1817. * other events save open/close.
  1818. *
  1819. * This code must be sure never to sleep through a hangup.
  1820. * Called without the kernel lock held - fine
  1821. */
  1822. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  1823. poll_table *wait)
  1824. {
  1825. unsigned int mask = 0;
  1826. poll_wait(file, &tty->read_wait, wait);
  1827. poll_wait(file, &tty->write_wait, wait);
  1828. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1829. mask |= POLLIN | POLLRDNORM;
  1830. if (tty->packet && tty->link->ctrl_status)
  1831. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1832. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1833. mask |= POLLHUP;
  1834. if (tty_hung_up_p(file))
  1835. mask |= POLLHUP;
  1836. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1837. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1838. tty->minimum_to_wake = MIN_CHAR(tty);
  1839. else
  1840. tty->minimum_to_wake = 1;
  1841. }
  1842. if (tty->ops->write && !tty_is_writelocked(tty) &&
  1843. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  1844. tty_write_room(tty) > 0)
  1845. mask |= POLLOUT | POLLWRNORM;
  1846. return mask;
  1847. }
  1848. static unsigned long inq_canon(struct tty_struct *tty)
  1849. {
  1850. int nr, head, tail;
  1851. if (!tty->canon_data)
  1852. return 0;
  1853. head = tty->canon_head;
  1854. tail = tty->read_tail;
  1855. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  1856. /* Skip EOF-chars.. */
  1857. while (head != tail) {
  1858. if (test_bit(tail, tty->read_flags) &&
  1859. tty->read_buf[tail] == __DISABLED_CHAR)
  1860. nr--;
  1861. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1862. }
  1863. return nr;
  1864. }
  1865. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  1866. unsigned int cmd, unsigned long arg)
  1867. {
  1868. int retval;
  1869. switch (cmd) {
  1870. case TIOCOUTQ:
  1871. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  1872. case TIOCINQ:
  1873. /* FIXME: Locking */
  1874. retval = tty->read_cnt;
  1875. if (L_ICANON(tty))
  1876. retval = inq_canon(tty);
  1877. return put_user(retval, (unsigned int __user *) arg);
  1878. default:
  1879. return n_tty_ioctl_helper(tty, file, cmd, arg);
  1880. }
  1881. }
  1882. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  1883. .magic = TTY_LDISC_MAGIC,
  1884. .name = "n_tty",
  1885. .open = n_tty_open,
  1886. .close = n_tty_close,
  1887. .flush_buffer = n_tty_flush_buffer,
  1888. .chars_in_buffer = n_tty_chars_in_buffer,
  1889. .read = n_tty_read,
  1890. .write = n_tty_write,
  1891. .ioctl = n_tty_ioctl,
  1892. .set_termios = n_tty_set_termios,
  1893. .poll = n_tty_poll,
  1894. .receive_buf = n_tty_receive_buf,
  1895. .write_wakeup = n_tty_write_wakeup
  1896. };