n_tty.c 61 KB

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