n_tty.c 62 KB

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