n_tty.c 52 KB

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