n_tty.c 62 KB

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