n_tty.c 53 KB

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