n_tty.c 52 KB

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