n_tty.c 59 KB

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