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