n_tty.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  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. * Sets tty->receive_room to reflect the currently available space
  81. * in the input buffer, and re-schedules the flip buffer work if space
  82. * just became available.
  83. *
  84. * Locks: Concurrent update is protected with read_lock
  85. */
  86. static void n_tty_set_room(struct tty_struct *tty)
  87. {
  88. int left;
  89. int old_left;
  90. unsigned long flags;
  91. spin_lock_irqsave(&tty->read_lock, flags);
  92. if (I_PARMRK(tty)) {
  93. /* Multiply read_cnt by 3, since each byte might take up to
  94. * three times as many spaces when PARMRK is set (depending on
  95. * its flags, e.g. parity error). */
  96. left = N_TTY_BUF_SIZE - tty->read_cnt * 3 - 1;
  97. } else
  98. left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  99. /*
  100. * If we are doing input canonicalization, and there are no
  101. * pending newlines, let characters through without limit, so
  102. * that erase characters will be handled. Other excess
  103. * characters will be beeped.
  104. */
  105. if (left <= 0)
  106. left = tty->icanon && !tty->canon_data;
  107. old_left = tty->receive_room;
  108. tty->receive_room = left;
  109. spin_unlock_irqrestore(&tty->read_lock, flags);
  110. /* Did this open up the receive buffer? We may need to flip */
  111. if (left && !old_left)
  112. schedule_work(&tty->buf.work);
  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. }
  179. /**
  180. * n_tty_flush_buffer - clean input queue
  181. * @tty: terminal device
  182. *
  183. * Flush the input buffer. Called when the line discipline is
  184. * being closed, when the tty layer wants the buffer flushed (eg
  185. * at hangup) or when the N_TTY line discipline internally has to
  186. * clean the pending queue (for example some signals).
  187. *
  188. * Locking: ctrl_lock, read_lock.
  189. */
  190. static void n_tty_flush_buffer(struct tty_struct *tty)
  191. {
  192. unsigned long flags;
  193. /* clear everything and unthrottle the driver */
  194. reset_buffer_flags(tty);
  195. if (!tty->link)
  196. return;
  197. spin_lock_irqsave(&tty->ctrl_lock, flags);
  198. if (tty->link->packet) {
  199. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  200. wake_up_interruptible(&tty->link->read_wait);
  201. }
  202. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  203. }
  204. /**
  205. * n_tty_chars_in_buffer - report available bytes
  206. * @tty: tty device
  207. *
  208. * Report the number of characters buffered to be delivered to user
  209. * at this instant in time.
  210. *
  211. * Locking: read_lock
  212. */
  213. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  214. {
  215. unsigned long flags;
  216. ssize_t n = 0;
  217. spin_lock_irqsave(&tty->read_lock, flags);
  218. if (!tty->icanon) {
  219. n = tty->read_cnt;
  220. } else if (tty->canon_data) {
  221. n = (tty->canon_head > tty->read_tail) ?
  222. tty->canon_head - tty->read_tail :
  223. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  224. }
  225. spin_unlock_irqrestore(&tty->read_lock, flags);
  226. return n;
  227. }
  228. /**
  229. * is_utf8_continuation - utf8 multibyte check
  230. * @c: byte to check
  231. *
  232. * Returns true if the utf8 character 'c' is a multibyte continuation
  233. * character. We use this to correctly compute the on screen size
  234. * of the character when printing
  235. */
  236. static inline int is_utf8_continuation(unsigned char c)
  237. {
  238. return (c & 0xc0) == 0x80;
  239. }
  240. /**
  241. * is_continuation - multibyte check
  242. * @c: byte to check
  243. *
  244. * Returns true if the utf8 character 'c' is a multibyte continuation
  245. * character and the terminal is in unicode mode.
  246. */
  247. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  248. {
  249. return I_IUTF8(tty) && is_utf8_continuation(c);
  250. }
  251. /**
  252. * do_output_char - output one character
  253. * @c: character (or partial unicode symbol)
  254. * @tty: terminal device
  255. * @space: space available in tty driver write buffer
  256. *
  257. * This is a helper function that handles one output character
  258. * (including special characters like TAB, CR, LF, etc.),
  259. * doing OPOST processing and putting the results in the
  260. * 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->ops->write(tty, "\r\n", 2);
  286. return 2;
  287. }
  288. tty->canon_column = tty->column;
  289. break;
  290. case '\r':
  291. if (O_ONOCR(tty) && tty->column == 0)
  292. return 0;
  293. if (O_OCRNL(tty)) {
  294. c = '\n';
  295. if (O_ONLRET(tty))
  296. tty->canon_column = tty->column = 0;
  297. break;
  298. }
  299. tty->canon_column = tty->column = 0;
  300. break;
  301. case '\t':
  302. spaces = 8 - (tty->column & 7);
  303. if (O_TABDLY(tty) == XTABS) {
  304. if (space < spaces)
  305. return -1;
  306. tty->column += spaces;
  307. tty->ops->write(tty, " ", spaces);
  308. return spaces;
  309. }
  310. tty->column += spaces;
  311. break;
  312. case '\b':
  313. if (tty->column > 0)
  314. tty->column--;
  315. break;
  316. default:
  317. if (!iscntrl(c)) {
  318. if (O_OLCUC(tty))
  319. c = toupper(c);
  320. if (!is_continuation(c, tty))
  321. tty->column++;
  322. }
  323. break;
  324. }
  325. tty_put_char(tty, c);
  326. return 1;
  327. }
  328. /**
  329. * process_output - output post processor
  330. * @c: character (or partial unicode symbol)
  331. * @tty: terminal device
  332. *
  333. * Output one character with OPOST processing.
  334. * Returns -1 when the output device is full and the character
  335. * 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. * @buf: character buffer
  357. * @nr: number of bytes to output
  358. *
  359. * Output a block of characters with OPOST processing.
  360. * Returns the number of characters output.
  361. *
  362. * This path is used to speed up block console writes, among other
  363. * things when processing blocks of output data. It handles only
  364. * the simple cases normally found and helps to generate blocks of
  365. * symbols for the console driver and thus improve performance.
  366. *
  367. * Locking: output_lock to protect column state and space left
  368. * (also, this is called from n_tty_write under the
  369. * tty layer write lock)
  370. */
  371. static ssize_t process_output_block(struct tty_struct *tty,
  372. const unsigned char *buf, unsigned int nr)
  373. {
  374. int space;
  375. int i;
  376. const unsigned char *cp;
  377. mutex_lock(&tty->output_lock);
  378. space = tty_write_room(tty);
  379. if (!space) {
  380. mutex_unlock(&tty->output_lock);
  381. return 0;
  382. }
  383. if (nr > space)
  384. nr = space;
  385. for (i = 0, cp = buf; i < nr; i++, cp++) {
  386. unsigned char c = *cp;
  387. switch (c) {
  388. case '\n':
  389. if (O_ONLRET(tty))
  390. tty->column = 0;
  391. if (O_ONLCR(tty))
  392. goto break_out;
  393. tty->canon_column = tty->column;
  394. break;
  395. case '\r':
  396. if (O_ONOCR(tty) && tty->column == 0)
  397. goto break_out;
  398. if (O_OCRNL(tty))
  399. goto break_out;
  400. tty->canon_column = tty->column = 0;
  401. break;
  402. case '\t':
  403. goto break_out;
  404. case '\b':
  405. if (tty->column > 0)
  406. tty->column--;
  407. break;
  408. default:
  409. if (!iscntrl(c)) {
  410. if (O_OLCUC(tty))
  411. goto break_out;
  412. if (!is_continuation(c, tty))
  413. tty->column++;
  414. }
  415. break;
  416. }
  417. }
  418. break_out:
  419. i = tty->ops->write(tty, buf, i);
  420. mutex_unlock(&tty->output_lock);
  421. return i;
  422. }
  423. /**
  424. * process_echoes - write pending echo characters
  425. * @tty: terminal device
  426. *
  427. * Write previously buffered echo (and other ldisc-generated)
  428. * characters to the tty.
  429. *
  430. * Characters generated by the ldisc (including echoes) need to
  431. * be buffered because the driver's write buffer can fill during
  432. * heavy program output. Echoing straight to the driver will
  433. * often fail under these conditions, causing lost characters and
  434. * resulting mismatches of ldisc state information.
  435. *
  436. * Since the ldisc state must represent the characters actually sent
  437. * to the driver at the time of the write, operations like certain
  438. * changes in column state are also saved in the buffer and executed
  439. * here.
  440. *
  441. * A circular fifo buffer is used so that the most recent characters
  442. * are prioritized. Also, when control characters are echoed with a
  443. * prefixed "^", the pair is treated atomically and thus not separated.
  444. *
  445. * Locking: output_lock to protect column state and space left,
  446. * echo_lock to protect the echo buffer
  447. */
  448. static void process_echoes(struct tty_struct *tty)
  449. {
  450. int space, nr;
  451. unsigned char c;
  452. unsigned char *cp, *buf_end;
  453. if (!tty->echo_cnt)
  454. return;
  455. mutex_lock(&tty->output_lock);
  456. mutex_lock(&tty->echo_lock);
  457. space = tty_write_room(tty);
  458. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  459. cp = tty->echo_buf + tty->echo_pos;
  460. nr = tty->echo_cnt;
  461. while (nr > 0) {
  462. c = *cp;
  463. if (c == ECHO_OP_START) {
  464. unsigned char op;
  465. unsigned char *opp;
  466. int no_space_left = 0;
  467. /*
  468. * If the buffer byte is the start of a multi-byte
  469. * operation, get the next byte, which is either the
  470. * op code or a control character value.
  471. */
  472. opp = cp + 1;
  473. if (opp == buf_end)
  474. opp -= N_TTY_BUF_SIZE;
  475. op = *opp;
  476. switch (op) {
  477. unsigned int num_chars, num_bs;
  478. case ECHO_OP_ERASE_TAB:
  479. if (++opp == buf_end)
  480. opp -= N_TTY_BUF_SIZE;
  481. num_chars = *opp;
  482. /*
  483. * Determine how many columns to go back
  484. * in order to erase the tab.
  485. * This depends on the number of columns
  486. * used by other characters within the tab
  487. * area. If this (modulo 8) count is from
  488. * the start of input rather than from a
  489. * previous tab, we offset by canon column.
  490. * Otherwise, tab spacing is normal.
  491. */
  492. if (!(num_chars & 0x80))
  493. num_chars += tty->canon_column;
  494. num_bs = 8 - (num_chars & 7);
  495. if (num_bs > space) {
  496. no_space_left = 1;
  497. break;
  498. }
  499. space -= num_bs;
  500. while (num_bs--) {
  501. tty_put_char(tty, '\b');
  502. if (tty->column > 0)
  503. tty->column--;
  504. }
  505. cp += 3;
  506. nr -= 3;
  507. break;
  508. case ECHO_OP_SET_CANON_COL:
  509. tty->canon_column = tty->column;
  510. cp += 2;
  511. nr -= 2;
  512. break;
  513. case ECHO_OP_MOVE_BACK_COL:
  514. if (tty->column > 0)
  515. tty->column--;
  516. cp += 2;
  517. nr -= 2;
  518. break;
  519. case ECHO_OP_START:
  520. /* This is an escaped echo op start code */
  521. if (!space) {
  522. no_space_left = 1;
  523. break;
  524. }
  525. tty_put_char(tty, ECHO_OP_START);
  526. tty->column++;
  527. space--;
  528. cp += 2;
  529. nr -= 2;
  530. break;
  531. default:
  532. /*
  533. * If the op is not a special byte code,
  534. * it is a ctrl char tagged to be echoed
  535. * as "^X" (where X is the letter
  536. * representing the control char).
  537. * Note that we must ensure there is
  538. * enough space for the whole ctrl pair.
  539. *
  540. */
  541. if (space < 2) {
  542. no_space_left = 1;
  543. break;
  544. }
  545. tty_put_char(tty, '^');
  546. tty_put_char(tty, op ^ 0100);
  547. tty->column += 2;
  548. space -= 2;
  549. cp += 2;
  550. nr -= 2;
  551. }
  552. if (no_space_left)
  553. break;
  554. } else {
  555. if (O_OPOST(tty) &&
  556. !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  557. int retval = do_output_char(c, tty, space);
  558. if (retval < 0)
  559. break;
  560. space -= retval;
  561. } else {
  562. if (!space)
  563. break;
  564. tty_put_char(tty, c);
  565. space -= 1;
  566. }
  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 echoed as "^X"
  725. * (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 (L_ECHOCTL(tty) && 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 (L_EXTPROC(tty)) {
  1001. put_tty_queue(c, tty);
  1002. return;
  1003. }
  1004. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  1005. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  1006. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  1007. start_tty(tty);
  1008. process_echoes(tty);
  1009. }
  1010. if (tty->closing) {
  1011. if (I_IXON(tty)) {
  1012. if (c == START_CHAR(tty)) {
  1013. start_tty(tty);
  1014. process_echoes(tty);
  1015. } else if (c == STOP_CHAR(tty))
  1016. stop_tty(tty);
  1017. }
  1018. return;
  1019. }
  1020. /*
  1021. * If the previous character was LNEXT, or we know that this
  1022. * character is not one of the characters that we'll have to
  1023. * handle specially, do shortcut processing to speed things
  1024. * up.
  1025. */
  1026. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1027. tty->lnext = 0;
  1028. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1029. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1030. /* beep if no space */
  1031. if (L_ECHO(tty))
  1032. process_output('\a', tty);
  1033. return;
  1034. }
  1035. if (L_ECHO(tty)) {
  1036. finish_erasing(tty);
  1037. /* Record the column of first canon char. */
  1038. if (tty->canon_head == tty->read_head)
  1039. echo_set_canon_col(tty);
  1040. echo_char(c, tty);
  1041. process_echoes(tty);
  1042. }
  1043. if (parmrk)
  1044. put_tty_queue(c, tty);
  1045. put_tty_queue(c, tty);
  1046. return;
  1047. }
  1048. if (I_IXON(tty)) {
  1049. if (c == START_CHAR(tty)) {
  1050. start_tty(tty);
  1051. process_echoes(tty);
  1052. return;
  1053. }
  1054. if (c == STOP_CHAR(tty)) {
  1055. stop_tty(tty);
  1056. return;
  1057. }
  1058. }
  1059. if (L_ISIG(tty)) {
  1060. int signal;
  1061. signal = SIGINT;
  1062. if (c == INTR_CHAR(tty))
  1063. goto send_signal;
  1064. signal = SIGQUIT;
  1065. if (c == QUIT_CHAR(tty))
  1066. goto send_signal;
  1067. signal = SIGTSTP;
  1068. if (c == SUSP_CHAR(tty)) {
  1069. send_signal:
  1070. /*
  1071. * Note that we do not use isig() here because we want
  1072. * the order to be:
  1073. * 1) flush, 2) echo, 3) signal
  1074. */
  1075. if (!L_NOFLSH(tty)) {
  1076. n_tty_flush_buffer(tty);
  1077. tty_driver_flush_buffer(tty);
  1078. }
  1079. if (I_IXON(tty))
  1080. start_tty(tty);
  1081. if (L_ECHO(tty)) {
  1082. echo_char(c, tty);
  1083. process_echoes(tty);
  1084. }
  1085. if (tty->pgrp)
  1086. kill_pgrp(tty->pgrp, signal, 1);
  1087. return;
  1088. }
  1089. }
  1090. if (c == '\r') {
  1091. if (I_IGNCR(tty))
  1092. return;
  1093. if (I_ICRNL(tty))
  1094. c = '\n';
  1095. } else if (c == '\n' && I_INLCR(tty))
  1096. c = '\r';
  1097. if (tty->icanon) {
  1098. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1099. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1100. eraser(c, tty);
  1101. process_echoes(tty);
  1102. return;
  1103. }
  1104. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1105. tty->lnext = 1;
  1106. if (L_ECHO(tty)) {
  1107. finish_erasing(tty);
  1108. if (L_ECHOCTL(tty)) {
  1109. echo_char_raw('^', tty);
  1110. echo_char_raw('\b', tty);
  1111. process_echoes(tty);
  1112. }
  1113. }
  1114. return;
  1115. }
  1116. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1117. L_IEXTEN(tty)) {
  1118. unsigned long tail = tty->canon_head;
  1119. finish_erasing(tty);
  1120. echo_char(c, tty);
  1121. echo_char_raw('\n', tty);
  1122. while (tail != tty->read_head) {
  1123. echo_char(tty->read_buf[tail], tty);
  1124. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1125. }
  1126. process_echoes(tty);
  1127. return;
  1128. }
  1129. if (c == '\n') {
  1130. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1131. if (L_ECHO(tty))
  1132. process_output('\a', tty);
  1133. return;
  1134. }
  1135. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1136. echo_char_raw('\n', tty);
  1137. process_echoes(tty);
  1138. }
  1139. goto handle_newline;
  1140. }
  1141. if (c == EOF_CHAR(tty)) {
  1142. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1143. return;
  1144. if (tty->canon_head != tty->read_head)
  1145. set_bit(TTY_PUSH, &tty->flags);
  1146. c = __DISABLED_CHAR;
  1147. goto handle_newline;
  1148. }
  1149. if ((c == EOL_CHAR(tty)) ||
  1150. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1151. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1152. ? 1 : 0;
  1153. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1154. if (L_ECHO(tty))
  1155. process_output('\a', tty);
  1156. return;
  1157. }
  1158. /*
  1159. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1160. */
  1161. if (L_ECHO(tty)) {
  1162. /* Record the column of first canon char. */
  1163. if (tty->canon_head == tty->read_head)
  1164. echo_set_canon_col(tty);
  1165. echo_char(c, tty);
  1166. process_echoes(tty);
  1167. }
  1168. /*
  1169. * XXX does PARMRK doubling happen for
  1170. * EOL_CHAR and EOL2_CHAR?
  1171. */
  1172. if (parmrk)
  1173. put_tty_queue(c, tty);
  1174. handle_newline:
  1175. spin_lock_irqsave(&tty->read_lock, flags);
  1176. set_bit(tty->read_head, tty->read_flags);
  1177. put_tty_queue_nolock(c, tty);
  1178. tty->canon_head = tty->read_head;
  1179. tty->canon_data++;
  1180. spin_unlock_irqrestore(&tty->read_lock, flags);
  1181. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1182. wake_up_interruptible(&tty->read_wait);
  1183. return;
  1184. }
  1185. }
  1186. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1187. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1188. /* beep if no space */
  1189. if (L_ECHO(tty))
  1190. process_output('\a', tty);
  1191. return;
  1192. }
  1193. if (L_ECHO(tty)) {
  1194. finish_erasing(tty);
  1195. if (c == '\n')
  1196. echo_char_raw('\n', tty);
  1197. else {
  1198. /* Record the column of first canon char. */
  1199. if (tty->canon_head == tty->read_head)
  1200. echo_set_canon_col(tty);
  1201. echo_char(c, tty);
  1202. }
  1203. process_echoes(tty);
  1204. }
  1205. if (parmrk)
  1206. put_tty_queue(c, tty);
  1207. put_tty_queue(c, tty);
  1208. }
  1209. /**
  1210. * n_tty_write_wakeup - asynchronous I/O notifier
  1211. * @tty: tty device
  1212. *
  1213. * Required for the ptys, serial driver etc. since processes
  1214. * that attach themselves to the master and rely on ASYNC
  1215. * IO must be woken up
  1216. */
  1217. static void n_tty_write_wakeup(struct tty_struct *tty)
  1218. {
  1219. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1220. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1221. }
  1222. /**
  1223. * n_tty_receive_buf - data receive
  1224. * @tty: terminal device
  1225. * @cp: buffer
  1226. * @fp: flag buffer
  1227. * @count: characters
  1228. *
  1229. * Called by the terminal driver when a block of characters has
  1230. * been received. This function must be called from soft contexts
  1231. * not from interrupt context. The driver is responsible for making
  1232. * calls one at a time and in order (or using flush_to_ldisc)
  1233. */
  1234. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1235. char *fp, int count)
  1236. {
  1237. const unsigned char *p;
  1238. char *f, flags = TTY_NORMAL;
  1239. int i;
  1240. char buf[64];
  1241. unsigned long cpuflags;
  1242. if (!tty->read_buf)
  1243. return;
  1244. if (tty->real_raw) {
  1245. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1246. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1247. N_TTY_BUF_SIZE - tty->read_head);
  1248. i = min(count, i);
  1249. memcpy(tty->read_buf + tty->read_head, cp, i);
  1250. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1251. tty->read_cnt += i;
  1252. cp += i;
  1253. count -= i;
  1254. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1255. N_TTY_BUF_SIZE - tty->read_head);
  1256. i = min(count, i);
  1257. memcpy(tty->read_buf + tty->read_head, cp, i);
  1258. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1259. tty->read_cnt += i;
  1260. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1261. } else {
  1262. for (i = count, p = cp, f = fp; i; i--, p++) {
  1263. if (f)
  1264. flags = *f++;
  1265. switch (flags) {
  1266. case TTY_NORMAL:
  1267. n_tty_receive_char(tty, *p);
  1268. break;
  1269. case TTY_BREAK:
  1270. n_tty_receive_break(tty);
  1271. break;
  1272. case TTY_PARITY:
  1273. case TTY_FRAME:
  1274. n_tty_receive_parity_error(tty, *p);
  1275. break;
  1276. case TTY_OVERRUN:
  1277. n_tty_receive_overrun(tty);
  1278. break;
  1279. default:
  1280. printk(KERN_ERR "%s: unknown flag %d\n",
  1281. tty_name(tty, buf), flags);
  1282. break;
  1283. }
  1284. }
  1285. if (tty->ops->flush_chars)
  1286. tty->ops->flush_chars(tty);
  1287. }
  1288. n_tty_set_room(tty);
  1289. if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
  1290. L_EXTPROC(tty)) {
  1291. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1292. wake_up_interruptible(&tty->read_wait);
  1293. }
  1294. /*
  1295. * Check the remaining room for the input canonicalization
  1296. * mode. We don't want to throttle the driver if we're in
  1297. * canonical mode and don't have a newline yet!
  1298. */
  1299. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1300. tty_throttle(tty);
  1301. }
  1302. int is_ignored(int sig)
  1303. {
  1304. return (sigismember(&current->blocked, sig) ||
  1305. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1306. }
  1307. /**
  1308. * n_tty_set_termios - termios data changed
  1309. * @tty: terminal
  1310. * @old: previous data
  1311. *
  1312. * Called by the tty layer when the user changes termios flags so
  1313. * that the line discipline can plan ahead. This function cannot sleep
  1314. * and is protected from re-entry by the tty layer. The user is
  1315. * guaranteed that this function will not be re-entered or in progress
  1316. * when the ldisc is closed.
  1317. *
  1318. * Locking: Caller holds tty->termios_mutex
  1319. */
  1320. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1321. {
  1322. int canon_change = 1;
  1323. BUG_ON(!tty);
  1324. if (old)
  1325. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1326. if (canon_change) {
  1327. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1328. tty->canon_head = tty->read_tail;
  1329. tty->canon_data = 0;
  1330. tty->erasing = 0;
  1331. }
  1332. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1333. wake_up_interruptible(&tty->read_wait);
  1334. tty->icanon = (L_ICANON(tty) != 0);
  1335. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1336. tty->raw = 1;
  1337. tty->real_raw = 1;
  1338. n_tty_set_room(tty);
  1339. return;
  1340. }
  1341. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1342. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1343. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1344. I_PARMRK(tty)) {
  1345. memset(tty->process_char_map, 0, 256/8);
  1346. if (I_IGNCR(tty) || I_ICRNL(tty))
  1347. set_bit('\r', tty->process_char_map);
  1348. if (I_INLCR(tty))
  1349. set_bit('\n', tty->process_char_map);
  1350. if (L_ICANON(tty)) {
  1351. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1352. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1353. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1354. set_bit('\n', tty->process_char_map);
  1355. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1356. if (L_IEXTEN(tty)) {
  1357. set_bit(WERASE_CHAR(tty),
  1358. tty->process_char_map);
  1359. set_bit(LNEXT_CHAR(tty),
  1360. tty->process_char_map);
  1361. set_bit(EOL2_CHAR(tty),
  1362. tty->process_char_map);
  1363. if (L_ECHO(tty))
  1364. set_bit(REPRINT_CHAR(tty),
  1365. tty->process_char_map);
  1366. }
  1367. }
  1368. if (I_IXON(tty)) {
  1369. set_bit(START_CHAR(tty), tty->process_char_map);
  1370. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1371. }
  1372. if (L_ISIG(tty)) {
  1373. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1374. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1375. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1376. }
  1377. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1378. tty->raw = 0;
  1379. tty->real_raw = 0;
  1380. } else {
  1381. tty->raw = 1;
  1382. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1383. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1384. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1385. tty->real_raw = 1;
  1386. else
  1387. tty->real_raw = 0;
  1388. }
  1389. n_tty_set_room(tty);
  1390. /*
  1391. * Fix tty hang when I_IXON(tty) is cleared, but the tty
  1392. * been stopped by STOP_CHAR(tty) before it.
  1393. */
  1394. if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
  1395. start_tty(tty);
  1396. }
  1397. /* The termios change make the tty ready for I/O */
  1398. wake_up_interruptible(&tty->write_wait);
  1399. wake_up_interruptible(&tty->read_wait);
  1400. }
  1401. /**
  1402. * n_tty_close - close the ldisc for this tty
  1403. * @tty: device
  1404. *
  1405. * Called from the terminal layer when this line discipline is
  1406. * being shut down, either because of a close or becsuse of a
  1407. * discipline change. The function will not be called while other
  1408. * ldisc methods are in progress.
  1409. */
  1410. static void n_tty_close(struct tty_struct *tty)
  1411. {
  1412. n_tty_flush_buffer(tty);
  1413. if (tty->read_buf) {
  1414. kfree(tty->read_buf);
  1415. tty->read_buf = NULL;
  1416. }
  1417. if (tty->echo_buf) {
  1418. kfree(tty->echo_buf);
  1419. tty->echo_buf = NULL;
  1420. }
  1421. }
  1422. /**
  1423. * n_tty_open - open an ldisc
  1424. * @tty: terminal to open
  1425. *
  1426. * Called when this line discipline is being attached to the
  1427. * terminal device. Can sleep. Called serialized so that no
  1428. * other events will occur in parallel. No further open will occur
  1429. * until a close.
  1430. */
  1431. static int n_tty_open(struct tty_struct *tty)
  1432. {
  1433. if (!tty)
  1434. return -EINVAL;
  1435. /* These are ugly. Currently a malloc failure here can panic */
  1436. if (!tty->read_buf) {
  1437. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1438. if (!tty->read_buf)
  1439. return -ENOMEM;
  1440. }
  1441. if (!tty->echo_buf) {
  1442. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1443. if (!tty->echo_buf)
  1444. return -ENOMEM;
  1445. }
  1446. reset_buffer_flags(tty);
  1447. tty_unthrottle(tty);
  1448. tty->column = 0;
  1449. n_tty_set_termios(tty, NULL);
  1450. tty->minimum_to_wake = 1;
  1451. tty->closing = 0;
  1452. return 0;
  1453. }
  1454. static inline int input_available_p(struct tty_struct *tty, int amt)
  1455. {
  1456. tty_flush_to_ldisc(tty);
  1457. if (tty->icanon && !L_EXTPROC(tty)) {
  1458. if (tty->canon_data)
  1459. return 1;
  1460. } else if (tty->read_cnt >= (amt ? amt : 1))
  1461. return 1;
  1462. return 0;
  1463. }
  1464. /**
  1465. * copy_from_read_buf - copy read data directly
  1466. * @tty: terminal device
  1467. * @b: user data
  1468. * @nr: size of data
  1469. *
  1470. * Helper function to speed up n_tty_read. It is only called when
  1471. * ICANON is off; it copies characters straight from the tty queue to
  1472. * user space directly. It can be profitably called twice; once to
  1473. * drain the space from the tail pointer to the (physical) end of the
  1474. * buffer, and once to drain the space from the (physical) beginning of
  1475. * the buffer to head pointer.
  1476. *
  1477. * Called under the tty->atomic_read_lock sem
  1478. *
  1479. */
  1480. static int copy_from_read_buf(struct tty_struct *tty,
  1481. unsigned char __user **b,
  1482. size_t *nr)
  1483. {
  1484. int retval;
  1485. size_t n;
  1486. unsigned long flags;
  1487. retval = 0;
  1488. spin_lock_irqsave(&tty->read_lock, flags);
  1489. n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1490. n = min(*nr, n);
  1491. spin_unlock_irqrestore(&tty->read_lock, flags);
  1492. if (n) {
  1493. retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1494. n -= retval;
  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 && n == 1) {
  1501. if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
  1502. n--;
  1503. }
  1504. spin_unlock_irqrestore(&tty->read_lock, flags);
  1505. *b += n;
  1506. *nr -= n;
  1507. }
  1508. return retval;
  1509. }
  1510. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1511. size_t, loff_t *);
  1512. /**
  1513. * job_control - check job control
  1514. * @tty: tty
  1515. * @file: file handle
  1516. *
  1517. * Perform job control management checks on this file/tty descriptor
  1518. * and if appropriate send any needed signals and return a negative
  1519. * error code if action should be taken.
  1520. *
  1521. * FIXME:
  1522. * Locking: None - redirected write test is safe, testing
  1523. * current->signal should possibly lock current->sighand
  1524. * pgrp locking ?
  1525. */
  1526. static int job_control(struct tty_struct *tty, struct file *file)
  1527. {
  1528. /* Job control check -- must be done at start and after
  1529. every sleep (POSIX.1 7.1.1.4). */
  1530. /* NOTE: not yet done after every sleep pending a thorough
  1531. check of the logic of this change. -- jlc */
  1532. /* don't stop on /dev/console */
  1533. if (file->f_op->write != redirected_tty_write &&
  1534. current->signal->tty == tty) {
  1535. if (!tty->pgrp)
  1536. printk(KERN_ERR "n_tty_read: no tty->pgrp!\n");
  1537. else if (task_pgrp(current) != tty->pgrp) {
  1538. if (is_ignored(SIGTTIN) ||
  1539. is_current_pgrp_orphaned())
  1540. return -EIO;
  1541. kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1542. set_thread_flag(TIF_SIGPENDING);
  1543. return -ERESTARTSYS;
  1544. }
  1545. }
  1546. return 0;
  1547. }
  1548. /**
  1549. * n_tty_read - read function for tty
  1550. * @tty: tty device
  1551. * @file: file object
  1552. * @buf: userspace buffer pointer
  1553. * @nr: size of I/O
  1554. *
  1555. * Perform reads for the line discipline. We are guaranteed that the
  1556. * line discipline will not be closed under us but we may get multiple
  1557. * parallel readers and must handle this ourselves. We may also get
  1558. * a hangup. Always called in user context, may sleep.
  1559. *
  1560. * This code must be sure never to sleep through a hangup.
  1561. */
  1562. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1563. unsigned char __user *buf, size_t nr)
  1564. {
  1565. unsigned char __user *b = buf;
  1566. DECLARE_WAITQUEUE(wait, current);
  1567. int c;
  1568. int minimum, time;
  1569. ssize_t retval = 0;
  1570. ssize_t size;
  1571. long timeout;
  1572. unsigned long flags;
  1573. int packet;
  1574. do_it_again:
  1575. if (WARN_ON(!tty->read_buf))
  1576. return -EAGAIN;
  1577. c = job_control(tty, file);
  1578. if (c < 0)
  1579. return c;
  1580. minimum = time = 0;
  1581. timeout = MAX_SCHEDULE_TIMEOUT;
  1582. if (!tty->icanon) {
  1583. time = (HZ / 10) * TIME_CHAR(tty);
  1584. minimum = MIN_CHAR(tty);
  1585. if (minimum) {
  1586. if (time)
  1587. tty->minimum_to_wake = 1;
  1588. else if (!waitqueue_active(&tty->read_wait) ||
  1589. (tty->minimum_to_wake > minimum))
  1590. tty->minimum_to_wake = minimum;
  1591. } else {
  1592. timeout = 0;
  1593. if (time) {
  1594. timeout = time;
  1595. time = 0;
  1596. }
  1597. tty->minimum_to_wake = minimum = 1;
  1598. }
  1599. }
  1600. /*
  1601. * Internal serialization of reads.
  1602. */
  1603. if (file->f_flags & O_NONBLOCK) {
  1604. if (!mutex_trylock(&tty->atomic_read_lock))
  1605. return -EAGAIN;
  1606. } else {
  1607. if (mutex_lock_interruptible(&tty->atomic_read_lock))
  1608. return -ERESTARTSYS;
  1609. }
  1610. packet = tty->packet;
  1611. add_wait_queue(&tty->read_wait, &wait);
  1612. while (nr) {
  1613. /* First test for status change. */
  1614. if (packet && tty->link->ctrl_status) {
  1615. unsigned char cs;
  1616. if (b != buf)
  1617. break;
  1618. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  1619. cs = tty->link->ctrl_status;
  1620. tty->link->ctrl_status = 0;
  1621. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  1622. if (tty_put_user(tty, cs, b++)) {
  1623. retval = -EFAULT;
  1624. b--;
  1625. break;
  1626. }
  1627. nr--;
  1628. break;
  1629. }
  1630. /* This statement must be first before checking for input
  1631. so that any interrupt will set the state back to
  1632. TASK_RUNNING. */
  1633. set_current_state(TASK_INTERRUPTIBLE);
  1634. if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  1635. ((minimum - (b - buf)) >= 1))
  1636. tty->minimum_to_wake = (minimum - (b - buf));
  1637. if (!input_available_p(tty, 0)) {
  1638. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1639. retval = -EIO;
  1640. break;
  1641. }
  1642. if (tty_hung_up_p(file))
  1643. break;
  1644. if (!timeout)
  1645. break;
  1646. if (file->f_flags & O_NONBLOCK) {
  1647. retval = -EAGAIN;
  1648. break;
  1649. }
  1650. if (signal_pending(current)) {
  1651. retval = -ERESTARTSYS;
  1652. break;
  1653. }
  1654. n_tty_set_room(tty);
  1655. timeout = schedule_timeout(timeout);
  1656. BUG_ON(!tty->read_buf);
  1657. continue;
  1658. }
  1659. __set_current_state(TASK_RUNNING);
  1660. /* Deal with packet mode. */
  1661. if (packet && b == buf) {
  1662. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  1663. retval = -EFAULT;
  1664. b--;
  1665. break;
  1666. }
  1667. nr--;
  1668. }
  1669. if (tty->icanon && !L_EXTPROC(tty)) {
  1670. /* N.B. avoid overrun if nr == 0 */
  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. spin_lock_irqsave(&tty->read_lock, flags);
  1677. tty->read_tail = ((tty->read_tail+1) &
  1678. (N_TTY_BUF_SIZE-1));
  1679. tty->read_cnt--;
  1680. if (eol) {
  1681. /* this test should be redundant:
  1682. * we shouldn't be reading data if
  1683. * canon_data is 0
  1684. */
  1685. if (--tty->canon_data < 0)
  1686. tty->canon_data = 0;
  1687. }
  1688. spin_unlock_irqrestore(&tty->read_lock, flags);
  1689. if (!eol || (c != __DISABLED_CHAR)) {
  1690. if (tty_put_user(tty, c, b++)) {
  1691. retval = -EFAULT;
  1692. b--;
  1693. break;
  1694. }
  1695. nr--;
  1696. }
  1697. if (eol) {
  1698. tty_audit_push(tty);
  1699. break;
  1700. }
  1701. }
  1702. if (retval)
  1703. break;
  1704. } else {
  1705. int uncopied;
  1706. /* The copy function takes the read lock and handles
  1707. locking internally for this case */
  1708. uncopied = copy_from_read_buf(tty, &b, &nr);
  1709. uncopied += copy_from_read_buf(tty, &b, &nr);
  1710. if (uncopied) {
  1711. retval = -EFAULT;
  1712. break;
  1713. }
  1714. }
  1715. /* If there is enough space in the read buffer now, let the
  1716. * low-level driver know. We use n_tty_chars_in_buffer() to
  1717. * check the buffer, as it now knows about canonical mode.
  1718. * Otherwise, if the driver is throttled and the line is
  1719. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  1720. * we won't get any more characters.
  1721. */
  1722. if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
  1723. n_tty_set_room(tty);
  1724. check_unthrottle(tty);
  1725. }
  1726. if (b - buf >= minimum)
  1727. break;
  1728. if (time)
  1729. timeout = time;
  1730. }
  1731. mutex_unlock(&tty->atomic_read_lock);
  1732. remove_wait_queue(&tty->read_wait, &wait);
  1733. if (!waitqueue_active(&tty->read_wait))
  1734. tty->minimum_to_wake = minimum;
  1735. __set_current_state(TASK_RUNNING);
  1736. size = b - buf;
  1737. if (size) {
  1738. retval = size;
  1739. if (nr)
  1740. clear_bit(TTY_PUSH, &tty->flags);
  1741. } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
  1742. goto do_it_again;
  1743. n_tty_set_room(tty);
  1744. return retval;
  1745. }
  1746. /**
  1747. * n_tty_write - write function for tty
  1748. * @tty: tty device
  1749. * @file: file object
  1750. * @buf: userspace buffer pointer
  1751. * @nr: size of I/O
  1752. *
  1753. * Write function of the terminal device. This is serialized with
  1754. * respect to other write callers but not to termios changes, reads
  1755. * and other such events. Since the receive code will echo characters,
  1756. * thus calling driver write methods, the output_lock is used in
  1757. * the output processing functions called here as well as in the
  1758. * echo processing function to protect the column state and space
  1759. * left in the buffer.
  1760. *
  1761. * This code must be sure never to sleep through a hangup.
  1762. *
  1763. * Locking: output_lock to protect column state and space left
  1764. * (note that the process_output*() functions take this
  1765. * lock themselves)
  1766. */
  1767. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  1768. const unsigned char *buf, size_t nr)
  1769. {
  1770. const unsigned char *b = buf;
  1771. DECLARE_WAITQUEUE(wait, current);
  1772. int c;
  1773. ssize_t retval = 0;
  1774. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  1775. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  1776. retval = tty_check_change(tty);
  1777. if (retval)
  1778. return retval;
  1779. }
  1780. /* Write out any echoed characters that are still pending */
  1781. process_echoes(tty);
  1782. add_wait_queue(&tty->write_wait, &wait);
  1783. while (1) {
  1784. set_current_state(TASK_INTERRUPTIBLE);
  1785. if (signal_pending(current)) {
  1786. retval = -ERESTARTSYS;
  1787. break;
  1788. }
  1789. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  1790. retval = -EIO;
  1791. break;
  1792. }
  1793. if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  1794. while (nr > 0) {
  1795. ssize_t num = process_output_block(tty, b, nr);
  1796. if (num < 0) {
  1797. if (num == -EAGAIN)
  1798. break;
  1799. retval = num;
  1800. goto break_out;
  1801. }
  1802. b += num;
  1803. nr -= num;
  1804. if (nr == 0)
  1805. break;
  1806. c = *b;
  1807. if (process_output(c, tty) < 0)
  1808. break;
  1809. b++; nr--;
  1810. }
  1811. if (tty->ops->flush_chars)
  1812. tty->ops->flush_chars(tty);
  1813. } else {
  1814. while (nr > 0) {
  1815. mutex_lock(&tty->output_lock);
  1816. c = tty->ops->write(tty, b, nr);
  1817. mutex_unlock(&tty->output_lock);
  1818. if (c < 0) {
  1819. retval = c;
  1820. goto break_out;
  1821. }
  1822. if (!c)
  1823. break;
  1824. b += c;
  1825. nr -= c;
  1826. }
  1827. }
  1828. if (!nr)
  1829. break;
  1830. if (file->f_flags & O_NONBLOCK) {
  1831. retval = -EAGAIN;
  1832. break;
  1833. }
  1834. schedule();
  1835. }
  1836. break_out:
  1837. __set_current_state(TASK_RUNNING);
  1838. remove_wait_queue(&tty->write_wait, &wait);
  1839. if (b - buf != nr && tty->fasync)
  1840. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  1841. return (b - buf) ? b - buf : retval;
  1842. }
  1843. /**
  1844. * n_tty_poll - poll method for N_TTY
  1845. * @tty: terminal device
  1846. * @file: file accessing it
  1847. * @wait: poll table
  1848. *
  1849. * Called when the line discipline is asked to poll() for data or
  1850. * for special events. This code is not serialized with respect to
  1851. * other events save open/close.
  1852. *
  1853. * This code must be sure never to sleep through a hangup.
  1854. * Called without the kernel lock held - fine
  1855. */
  1856. static unsigned int n_tty_poll(struct tty_struct *tty, struct file *file,
  1857. poll_table *wait)
  1858. {
  1859. unsigned int mask = 0;
  1860. poll_wait(file, &tty->read_wait, wait);
  1861. poll_wait(file, &tty->write_wait, wait);
  1862. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1863. mask |= POLLIN | POLLRDNORM;
  1864. if (tty->packet && tty->link->ctrl_status)
  1865. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1866. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1867. mask |= POLLHUP;
  1868. if (tty_hung_up_p(file))
  1869. mask |= POLLHUP;
  1870. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1871. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1872. tty->minimum_to_wake = MIN_CHAR(tty);
  1873. else
  1874. tty->minimum_to_wake = 1;
  1875. }
  1876. if (tty->ops->write && !tty_is_writelocked(tty) &&
  1877. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  1878. tty_write_room(tty) > 0)
  1879. mask |= POLLOUT | POLLWRNORM;
  1880. return mask;
  1881. }
  1882. static unsigned long inq_canon(struct tty_struct *tty)
  1883. {
  1884. int nr, head, tail;
  1885. if (!tty->canon_data)
  1886. return 0;
  1887. head = tty->canon_head;
  1888. tail = tty->read_tail;
  1889. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  1890. /* Skip EOF-chars.. */
  1891. while (head != tail) {
  1892. if (test_bit(tail, tty->read_flags) &&
  1893. tty->read_buf[tail] == __DISABLED_CHAR)
  1894. nr--;
  1895. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1896. }
  1897. return nr;
  1898. }
  1899. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  1900. unsigned int cmd, unsigned long arg)
  1901. {
  1902. int retval;
  1903. switch (cmd) {
  1904. case TIOCOUTQ:
  1905. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  1906. case TIOCINQ:
  1907. /* FIXME: Locking */
  1908. retval = tty->read_cnt;
  1909. if (L_ICANON(tty))
  1910. retval = inq_canon(tty);
  1911. return put_user(retval, (unsigned int __user *) arg);
  1912. default:
  1913. return n_tty_ioctl_helper(tty, file, cmd, arg);
  1914. }
  1915. }
  1916. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  1917. .magic = TTY_LDISC_MAGIC,
  1918. .name = "n_tty",
  1919. .open = n_tty_open,
  1920. .close = n_tty_close,
  1921. .flush_buffer = n_tty_flush_buffer,
  1922. .chars_in_buffer = n_tty_chars_in_buffer,
  1923. .read = n_tty_read,
  1924. .write = n_tty_write,
  1925. .ioctl = n_tty_ioctl,
  1926. .set_termios = n_tty_set_termios,
  1927. .poll = n_tty_poll,
  1928. .receive_buf = n_tty_receive_buf,
  1929. .write_wakeup = n_tty_write_wakeup
  1930. };
  1931. /**
  1932. * n_tty_inherit_ops - inherit N_TTY methods
  1933. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  1934. *
  1935. * Used by a generic struct tty_ldisc_ops to easily inherit N_TTY
  1936. * methods.
  1937. */
  1938. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  1939. {
  1940. *ops = tty_ldisc_N_TTY;
  1941. ops->owner = NULL;
  1942. ops->refcount = ops->flags = 0;
  1943. }
  1944. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);