n_tty.c 51 KB

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