n_tty.c 52 KB

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