n_tty.c 52 KB

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