n_tty.c 53 KB

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