n_tty.c 62 KB

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