n_tty.c 57 KB

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