n_tty.c 58 KB

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