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. #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. int old_left;
  91. /*
  92. * If we are doing input canonicalization, and there are no
  93. * pending newlines, let characters through without limit, so
  94. * that erase characters will be handled. Other excess
  95. * characters will be beeped.
  96. */
  97. if (left <= 0)
  98. left = tty->icanon && !tty->canon_data;
  99. old_left = tty->receive_room;
  100. tty->receive_room = left;
  101. /* Did this open up the receive buffer? We may need to flip */
  102. if (left && !old_left)
  103. schedule_work(&tty->buf.work);
  104. }
  105. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  106. {
  107. if (tty->read_cnt < N_TTY_BUF_SIZE) {
  108. tty->read_buf[tty->read_head] = c;
  109. tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  110. tty->read_cnt++;
  111. }
  112. }
  113. /**
  114. * put_tty_queue - add character to tty
  115. * @c: character
  116. * @tty: tty device
  117. *
  118. * Add a character to the tty read_buf queue. This is done under the
  119. * read_lock to serialize character addition and also to protect us
  120. * against parallel reads or flushes
  121. */
  122. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  123. {
  124. unsigned long flags;
  125. /*
  126. * The problem of stomping on the buffers ends here.
  127. * Why didn't anyone see this one coming? --AJK
  128. */
  129. spin_lock_irqsave(&tty->read_lock, flags);
  130. put_tty_queue_nolock(c, tty);
  131. spin_unlock_irqrestore(&tty->read_lock, flags);
  132. }
  133. /**
  134. * check_unthrottle - allow new receive data
  135. * @tty; tty device
  136. *
  137. * Check whether to call the driver unthrottle functions
  138. *
  139. * Can sleep, may be called under the atomic_read_lock mutex but
  140. * this is not guaranteed.
  141. */
  142. static void check_unthrottle(struct tty_struct *tty)
  143. {
  144. if (tty->count)
  145. tty_unthrottle(tty);
  146. }
  147. /**
  148. * reset_buffer_flags - reset buffer state
  149. * @tty: terminal to reset
  150. *
  151. * Reset the read buffer counters, clear the flags,
  152. * and make sure the driver is unthrottled. Called
  153. * from n_tty_open() and n_tty_flush_buffer().
  154. *
  155. * Locking: tty_read_lock for read fields.
  156. */
  157. static void reset_buffer_flags(struct tty_struct *tty)
  158. {
  159. unsigned long flags;
  160. spin_lock_irqsave(&tty->read_lock, flags);
  161. tty->read_head = tty->read_tail = tty->read_cnt = 0;
  162. spin_unlock_irqrestore(&tty->read_lock, flags);
  163. mutex_lock(&tty->echo_lock);
  164. tty->echo_pos = tty->echo_cnt = tty->echo_overrun = 0;
  165. mutex_unlock(&tty->echo_lock);
  166. tty->canon_head = tty->canon_data = tty->erasing = 0;
  167. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  168. n_tty_set_room(tty);
  169. }
  170. /**
  171. * n_tty_flush_buffer - clean input queue
  172. * @tty: terminal device
  173. *
  174. * Flush the input buffer. Called when the line discipline is
  175. * being closed, when the tty layer wants the buffer flushed (eg
  176. * at hangup) or when the N_TTY line discipline internally has to
  177. * clean the pending queue (for example some signals).
  178. *
  179. * Locking: ctrl_lock, read_lock.
  180. */
  181. static void n_tty_flush_buffer(struct tty_struct *tty)
  182. {
  183. unsigned long flags;
  184. /* clear everything and unthrottle the driver */
  185. reset_buffer_flags(tty);
  186. if (!tty->link)
  187. return;
  188. spin_lock_irqsave(&tty->ctrl_lock, flags);
  189. if (tty->link->packet) {
  190. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  191. wake_up_interruptible(&tty->link->read_wait);
  192. }
  193. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  194. }
  195. /**
  196. * n_tty_chars_in_buffer - report available bytes
  197. * @tty: tty device
  198. *
  199. * Report the number of characters buffered to be delivered to user
  200. * at this instant in time.
  201. *
  202. * Locking: read_lock
  203. */
  204. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  205. {
  206. unsigned long flags;
  207. ssize_t n = 0;
  208. spin_lock_irqsave(&tty->read_lock, flags);
  209. if (!tty->icanon) {
  210. n = tty->read_cnt;
  211. } else if (tty->canon_data) {
  212. n = (tty->canon_head > tty->read_tail) ?
  213. tty->canon_head - tty->read_tail :
  214. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  215. }
  216. spin_unlock_irqrestore(&tty->read_lock, flags);
  217. return n;
  218. }
  219. /**
  220. * is_utf8_continuation - utf8 multibyte check
  221. * @c: byte to check
  222. *
  223. * Returns true if the utf8 character 'c' is a multibyte continuation
  224. * character. We use this to correctly compute the on screen size
  225. * of the character when printing
  226. */
  227. static inline int is_utf8_continuation(unsigned char c)
  228. {
  229. return (c & 0xc0) == 0x80;
  230. }
  231. /**
  232. * is_continuation - multibyte check
  233. * @c: byte to check
  234. *
  235. * Returns true if the utf8 character 'c' is a multibyte continuation
  236. * character and the terminal is in unicode mode.
  237. */
  238. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  239. {
  240. return I_IUTF8(tty) && is_utf8_continuation(c);
  241. }
  242. /**
  243. * do_output_char - output one character
  244. * @c: character (or partial unicode symbol)
  245. * @tty: terminal device
  246. * @space: space available in tty driver write buffer
  247. *
  248. * This is a helper function that handles one output character
  249. * (including special characters like TAB, CR, LF, etc.),
  250. * doing OPOST processing and putting the results in the
  251. * tty driver's write buffer.
  252. *
  253. * Note that Linux currently ignores TABDLY, CRDLY, VTDLY, FFDLY
  254. * and NLDLY. They simply aren't relevant in the world today.
  255. * If you ever need them, add them here.
  256. *
  257. * Returns the number of bytes of buffer space used or -1 if
  258. * no space left.
  259. *
  260. * Locking: should be called under the output_lock to protect
  261. * the column state and space left in the buffer
  262. */
  263. static int do_output_char(unsigned char c, struct tty_struct *tty, int space)
  264. {
  265. int spaces;
  266. if (!space)
  267. return -1;
  268. switch (c) {
  269. case '\n':
  270. if (O_ONLRET(tty))
  271. tty->column = 0;
  272. if (O_ONLCR(tty)) {
  273. if (space < 2)
  274. return -1;
  275. tty->canon_column = tty->column = 0;
  276. tty->ops->write(tty, "\r\n", 2);
  277. return 2;
  278. }
  279. tty->canon_column = tty->column;
  280. break;
  281. case '\r':
  282. if (O_ONOCR(tty) && tty->column == 0)
  283. return 0;
  284. if (O_OCRNL(tty)) {
  285. c = '\n';
  286. if (O_ONLRET(tty))
  287. tty->canon_column = tty->column = 0;
  288. break;
  289. }
  290. tty->canon_column = tty->column = 0;
  291. break;
  292. case '\t':
  293. spaces = 8 - (tty->column & 7);
  294. if (O_TABDLY(tty) == XTABS) {
  295. if (space < spaces)
  296. return -1;
  297. tty->column += spaces;
  298. tty->ops->write(tty, " ", spaces);
  299. return spaces;
  300. }
  301. tty->column += spaces;
  302. break;
  303. case '\b':
  304. if (tty->column > 0)
  305. tty->column--;
  306. break;
  307. default:
  308. if (!iscntrl(c)) {
  309. if (O_OLCUC(tty))
  310. c = toupper(c);
  311. if (!is_continuation(c, tty))
  312. tty->column++;
  313. }
  314. break;
  315. }
  316. tty_put_char(tty, c);
  317. return 1;
  318. }
  319. /**
  320. * process_output - output post processor
  321. * @c: character (or partial unicode symbol)
  322. * @tty: terminal device
  323. *
  324. * Output one character with OPOST processing.
  325. * Returns -1 when the output device is full and the character
  326. * must be retried.
  327. *
  328. * Locking: output_lock to protect column state and space left
  329. * (also, this is called from n_tty_write under the
  330. * tty layer write lock)
  331. */
  332. static int process_output(unsigned char c, struct tty_struct *tty)
  333. {
  334. int space, retval;
  335. mutex_lock(&tty->output_lock);
  336. space = tty_write_room(tty);
  337. retval = do_output_char(c, tty, space);
  338. mutex_unlock(&tty->output_lock);
  339. if (retval < 0)
  340. return -1;
  341. else
  342. return 0;
  343. }
  344. /**
  345. * process_output_block - block post processor
  346. * @tty: terminal device
  347. * @buf: character buffer
  348. * @nr: number of bytes to output
  349. *
  350. * Output a block of characters with OPOST processing.
  351. * Returns the number of characters output.
  352. *
  353. * This path is used to speed up block console writes, among other
  354. * things when processing blocks of output data. It handles only
  355. * the simple cases normally found and helps to generate blocks of
  356. * symbols for the console driver and thus improve performance.
  357. *
  358. * Locking: output_lock to protect column state and space left
  359. * (also, this is called from n_tty_write under the
  360. * tty layer write lock)
  361. */
  362. static ssize_t process_output_block(struct tty_struct *tty,
  363. const unsigned char *buf, unsigned int nr)
  364. {
  365. int space;
  366. int i;
  367. const unsigned char *cp;
  368. mutex_lock(&tty->output_lock);
  369. space = tty_write_room(tty);
  370. if (!space) {
  371. mutex_unlock(&tty->output_lock);
  372. return 0;
  373. }
  374. if (nr > space)
  375. nr = space;
  376. for (i = 0, cp = buf; i < nr; i++, cp++) {
  377. unsigned char c = *cp;
  378. switch (c) {
  379. case '\n':
  380. if (O_ONLRET(tty))
  381. tty->column = 0;
  382. if (O_ONLCR(tty))
  383. goto break_out;
  384. tty->canon_column = tty->column;
  385. break;
  386. case '\r':
  387. if (O_ONOCR(tty) && tty->column == 0)
  388. goto break_out;
  389. if (O_OCRNL(tty))
  390. goto break_out;
  391. tty->canon_column = tty->column = 0;
  392. break;
  393. case '\t':
  394. goto break_out;
  395. case '\b':
  396. if (tty->column > 0)
  397. tty->column--;
  398. break;
  399. default:
  400. if (!iscntrl(c)) {
  401. if (O_OLCUC(tty))
  402. goto break_out;
  403. if (!is_continuation(c, tty))
  404. tty->column++;
  405. }
  406. break;
  407. }
  408. }
  409. break_out:
  410. i = tty->ops->write(tty, buf, i);
  411. mutex_unlock(&tty->output_lock);
  412. return i;
  413. }
  414. /**
  415. * process_echoes - write pending echo characters
  416. * @tty: terminal device
  417. *
  418. * Write previously buffered echo (and other ldisc-generated)
  419. * characters to the tty.
  420. *
  421. * Characters generated by the ldisc (including echoes) need to
  422. * be buffered because the driver's write buffer can fill during
  423. * heavy program output. Echoing straight to the driver will
  424. * often fail under these conditions, causing lost characters and
  425. * resulting mismatches of ldisc state information.
  426. *
  427. * Since the ldisc state must represent the characters actually sent
  428. * to the driver at the time of the write, operations like certain
  429. * changes in column state are also saved in the buffer and executed
  430. * here.
  431. *
  432. * A circular fifo buffer is used so that the most recent characters
  433. * are prioritized. Also, when control characters are echoed with a
  434. * prefixed "^", the pair is treated atomically and thus not separated.
  435. *
  436. * Locking: output_lock to protect column state and space left,
  437. * echo_lock to protect the echo buffer
  438. */
  439. static void process_echoes(struct tty_struct *tty)
  440. {
  441. int space, nr;
  442. unsigned char c;
  443. unsigned char *cp, *buf_end;
  444. if (!tty->echo_cnt)
  445. return;
  446. mutex_lock(&tty->output_lock);
  447. mutex_lock(&tty->echo_lock);
  448. space = tty_write_room(tty);
  449. buf_end = tty->echo_buf + N_TTY_BUF_SIZE;
  450. cp = tty->echo_buf + tty->echo_pos;
  451. nr = tty->echo_cnt;
  452. while (nr > 0) {
  453. c = *cp;
  454. if (c == ECHO_OP_START) {
  455. unsigned char op;
  456. unsigned char *opp;
  457. int no_space_left = 0;
  458. /*
  459. * If the buffer byte is the start of a multi-byte
  460. * operation, get the next byte, which is either the
  461. * op code or a control character value.
  462. */
  463. opp = cp + 1;
  464. if (opp == buf_end)
  465. opp -= N_TTY_BUF_SIZE;
  466. op = *opp;
  467. switch (op) {
  468. unsigned int num_chars, num_bs;
  469. case ECHO_OP_ERASE_TAB:
  470. if (++opp == buf_end)
  471. opp -= N_TTY_BUF_SIZE;
  472. num_chars = *opp;
  473. /*
  474. * Determine how many columns to go back
  475. * in order to erase the tab.
  476. * This depends on the number of columns
  477. * used by other characters within the tab
  478. * area. If this (modulo 8) count is from
  479. * the start of input rather than from a
  480. * previous tab, we offset by canon column.
  481. * Otherwise, tab spacing is normal.
  482. */
  483. if (!(num_chars & 0x80))
  484. num_chars += tty->canon_column;
  485. num_bs = 8 - (num_chars & 7);
  486. if (num_bs > space) {
  487. no_space_left = 1;
  488. break;
  489. }
  490. space -= num_bs;
  491. while (num_bs--) {
  492. tty_put_char(tty, '\b');
  493. if (tty->column > 0)
  494. tty->column--;
  495. }
  496. cp += 3;
  497. nr -= 3;
  498. break;
  499. case ECHO_OP_SET_CANON_COL:
  500. tty->canon_column = tty->column;
  501. cp += 2;
  502. nr -= 2;
  503. break;
  504. case ECHO_OP_MOVE_BACK_COL:
  505. if (tty->column > 0)
  506. tty->column--;
  507. cp += 2;
  508. nr -= 2;
  509. break;
  510. case ECHO_OP_START:
  511. /* This is an escaped echo op start code */
  512. if (!space) {
  513. no_space_left = 1;
  514. break;
  515. }
  516. tty_put_char(tty, ECHO_OP_START);
  517. tty->column++;
  518. space--;
  519. cp += 2;
  520. nr -= 2;
  521. break;
  522. default:
  523. /*
  524. * If the op is not a special byte code,
  525. * it is a ctrl char tagged to be echoed
  526. * as "^X" (where X is the letter
  527. * representing the control char).
  528. * Note that we must ensure there is
  529. * enough space for the whole ctrl pair.
  530. *
  531. */
  532. if (space < 2) {
  533. no_space_left = 1;
  534. break;
  535. }
  536. tty_put_char(tty, '^');
  537. tty_put_char(tty, op ^ 0100);
  538. tty->column += 2;
  539. space -= 2;
  540. cp += 2;
  541. nr -= 2;
  542. }
  543. if (no_space_left)
  544. break;
  545. } else {
  546. if (O_OPOST(tty) &&
  547. !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  548. int retval = do_output_char(c, tty, space);
  549. if (retval < 0)
  550. break;
  551. space -= retval;
  552. } else {
  553. if (!space)
  554. break;
  555. tty_put_char(tty, c);
  556. space -= 1;
  557. }
  558. cp += 1;
  559. nr -= 1;
  560. }
  561. /* When end of circular buffer reached, wrap around */
  562. if (cp >= buf_end)
  563. cp -= N_TTY_BUF_SIZE;
  564. }
  565. if (nr == 0) {
  566. tty->echo_pos = 0;
  567. tty->echo_cnt = 0;
  568. tty->echo_overrun = 0;
  569. } else {
  570. int num_processed = tty->echo_cnt - nr;
  571. tty->echo_pos += num_processed;
  572. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  573. tty->echo_cnt = nr;
  574. if (num_processed > 0)
  575. tty->echo_overrun = 0;
  576. }
  577. mutex_unlock(&tty->echo_lock);
  578. mutex_unlock(&tty->output_lock);
  579. if (tty->ops->flush_chars)
  580. tty->ops->flush_chars(tty);
  581. }
  582. /**
  583. * add_echo_byte - add a byte to the echo buffer
  584. * @c: unicode byte to echo
  585. * @tty: terminal device
  586. *
  587. * Add a character or operation byte to the echo buffer.
  588. *
  589. * Should be called under the echo lock to protect the echo buffer.
  590. */
  591. static void add_echo_byte(unsigned char c, struct tty_struct *tty)
  592. {
  593. int new_byte_pos;
  594. if (tty->echo_cnt == N_TTY_BUF_SIZE) {
  595. /* Circular buffer is already at capacity */
  596. new_byte_pos = tty->echo_pos;
  597. /*
  598. * Since the buffer start position needs to be advanced,
  599. * be sure to step by a whole operation byte group.
  600. */
  601. if (tty->echo_buf[tty->echo_pos] == ECHO_OP_START) {
  602. if (tty->echo_buf[(tty->echo_pos + 1) &
  603. (N_TTY_BUF_SIZE - 1)] ==
  604. ECHO_OP_ERASE_TAB) {
  605. tty->echo_pos += 3;
  606. tty->echo_cnt -= 2;
  607. } else {
  608. tty->echo_pos += 2;
  609. tty->echo_cnt -= 1;
  610. }
  611. } else {
  612. tty->echo_pos++;
  613. }
  614. tty->echo_pos &= N_TTY_BUF_SIZE - 1;
  615. tty->echo_overrun = 1;
  616. } else {
  617. new_byte_pos = tty->echo_pos + tty->echo_cnt;
  618. new_byte_pos &= N_TTY_BUF_SIZE - 1;
  619. tty->echo_cnt++;
  620. }
  621. tty->echo_buf[new_byte_pos] = c;
  622. }
  623. /**
  624. * echo_move_back_col - add operation to move back a column
  625. * @tty: terminal device
  626. *
  627. * Add an operation to the echo buffer to move back one column.
  628. *
  629. * Locking: echo_lock to protect the echo buffer
  630. */
  631. static void echo_move_back_col(struct tty_struct *tty)
  632. {
  633. mutex_lock(&tty->echo_lock);
  634. add_echo_byte(ECHO_OP_START, tty);
  635. add_echo_byte(ECHO_OP_MOVE_BACK_COL, tty);
  636. mutex_unlock(&tty->echo_lock);
  637. }
  638. /**
  639. * echo_set_canon_col - add operation to set the canon column
  640. * @tty: terminal device
  641. *
  642. * Add an operation to the echo buffer to set the canon column
  643. * to the current column.
  644. *
  645. * Locking: echo_lock to protect the echo buffer
  646. */
  647. static void echo_set_canon_col(struct tty_struct *tty)
  648. {
  649. mutex_lock(&tty->echo_lock);
  650. add_echo_byte(ECHO_OP_START, tty);
  651. add_echo_byte(ECHO_OP_SET_CANON_COL, tty);
  652. mutex_unlock(&tty->echo_lock);
  653. }
  654. /**
  655. * echo_erase_tab - add operation to erase a tab
  656. * @num_chars: number of character columns already used
  657. * @after_tab: true if num_chars starts after a previous tab
  658. * @tty: terminal device
  659. *
  660. * Add an operation to the echo buffer to erase a tab.
  661. *
  662. * Called by the eraser function, which knows how many character
  663. * columns have been used since either a previous tab or the start
  664. * of input. This information will be used later, along with
  665. * canon column (if applicable), to go back the correct number
  666. * of columns.
  667. *
  668. * Locking: echo_lock to protect the echo buffer
  669. */
  670. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  671. struct tty_struct *tty)
  672. {
  673. mutex_lock(&tty->echo_lock);
  674. add_echo_byte(ECHO_OP_START, tty);
  675. add_echo_byte(ECHO_OP_ERASE_TAB, tty);
  676. /* We only need to know this modulo 8 (tab spacing) */
  677. num_chars &= 7;
  678. /* Set the high bit as a flag if num_chars is after a previous tab */
  679. if (after_tab)
  680. num_chars |= 0x80;
  681. add_echo_byte(num_chars, tty);
  682. mutex_unlock(&tty->echo_lock);
  683. }
  684. /**
  685. * echo_char_raw - echo a character raw
  686. * @c: unicode byte to echo
  687. * @tty: terminal device
  688. *
  689. * Echo user input back onto the screen. This must be called only when
  690. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  691. *
  692. * This variant does not treat control characters specially.
  693. *
  694. * Locking: echo_lock to protect the echo buffer
  695. */
  696. static void echo_char_raw(unsigned char c, struct tty_struct *tty)
  697. {
  698. mutex_lock(&tty->echo_lock);
  699. if (c == ECHO_OP_START) {
  700. add_echo_byte(ECHO_OP_START, tty);
  701. add_echo_byte(ECHO_OP_START, tty);
  702. } else {
  703. add_echo_byte(c, tty);
  704. }
  705. mutex_unlock(&tty->echo_lock);
  706. }
  707. /**
  708. * echo_char - echo a character
  709. * @c: unicode byte to echo
  710. * @tty: terminal device
  711. *
  712. * Echo user input back onto the screen. This must be called only when
  713. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  714. *
  715. * This variant tags control characters to be echoed as "^X"
  716. * (where X is the letter representing the control char).
  717. *
  718. * Locking: echo_lock to protect the echo buffer
  719. */
  720. static void echo_char(unsigned char c, struct tty_struct *tty)
  721. {
  722. mutex_lock(&tty->echo_lock);
  723. if (c == ECHO_OP_START) {
  724. add_echo_byte(ECHO_OP_START, tty);
  725. add_echo_byte(ECHO_OP_START, tty);
  726. } else {
  727. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  728. add_echo_byte(ECHO_OP_START, tty);
  729. add_echo_byte(c, tty);
  730. }
  731. mutex_unlock(&tty->echo_lock);
  732. }
  733. /**
  734. * finish_erasing - complete erase
  735. * @tty: tty doing the erase
  736. */
  737. static inline void finish_erasing(struct tty_struct *tty)
  738. {
  739. if (tty->erasing) {
  740. echo_char_raw('/', tty);
  741. tty->erasing = 0;
  742. }
  743. }
  744. /**
  745. * eraser - handle erase function
  746. * @c: character input
  747. * @tty: terminal device
  748. *
  749. * Perform erase and necessary output when an erase character is
  750. * present in the stream from the driver layer. Handles the complexities
  751. * of UTF-8 multibyte symbols.
  752. *
  753. * Locking: read_lock for tty buffers
  754. */
  755. static void eraser(unsigned char c, struct tty_struct *tty)
  756. {
  757. enum { ERASE, WERASE, KILL } kill_type;
  758. int head, seen_alnums, cnt;
  759. unsigned long flags;
  760. /* FIXME: locking needed ? */
  761. if (tty->read_head == tty->canon_head) {
  762. /* process_output('\a', tty); */ /* what do you think? */
  763. return;
  764. }
  765. if (c == ERASE_CHAR(tty))
  766. kill_type = ERASE;
  767. else if (c == WERASE_CHAR(tty))
  768. kill_type = WERASE;
  769. else {
  770. if (!L_ECHO(tty)) {
  771. spin_lock_irqsave(&tty->read_lock, flags);
  772. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  773. (N_TTY_BUF_SIZE - 1));
  774. tty->read_head = tty->canon_head;
  775. spin_unlock_irqrestore(&tty->read_lock, flags);
  776. return;
  777. }
  778. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  779. spin_lock_irqsave(&tty->read_lock, flags);
  780. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  781. (N_TTY_BUF_SIZE - 1));
  782. tty->read_head = tty->canon_head;
  783. spin_unlock_irqrestore(&tty->read_lock, flags);
  784. finish_erasing(tty);
  785. echo_char(KILL_CHAR(tty), tty);
  786. /* Add a newline if ECHOK is on and ECHOKE is off. */
  787. if (L_ECHOK(tty))
  788. echo_char_raw('\n', tty);
  789. return;
  790. }
  791. kill_type = KILL;
  792. }
  793. seen_alnums = 0;
  794. /* FIXME: Locking ?? */
  795. while (tty->read_head != tty->canon_head) {
  796. head = tty->read_head;
  797. /* erase a single possibly multibyte character */
  798. do {
  799. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  800. c = tty->read_buf[head];
  801. } while (is_continuation(c, tty) && head != tty->canon_head);
  802. /* do not partially erase */
  803. if (is_continuation(c, tty))
  804. break;
  805. if (kill_type == WERASE) {
  806. /* Equivalent to BSD's ALTWERASE. */
  807. if (isalnum(c) || c == '_')
  808. seen_alnums++;
  809. else if (seen_alnums)
  810. break;
  811. }
  812. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  813. spin_lock_irqsave(&tty->read_lock, flags);
  814. tty->read_head = head;
  815. tty->read_cnt -= cnt;
  816. spin_unlock_irqrestore(&tty->read_lock, flags);
  817. if (L_ECHO(tty)) {
  818. if (L_ECHOPRT(tty)) {
  819. if (!tty->erasing) {
  820. echo_char_raw('\\', tty);
  821. tty->erasing = 1;
  822. }
  823. /* if cnt > 1, output a multi-byte character */
  824. echo_char(c, tty);
  825. while (--cnt > 0) {
  826. head = (head+1) & (N_TTY_BUF_SIZE-1);
  827. echo_char_raw(tty->read_buf[head], tty);
  828. echo_move_back_col(tty);
  829. }
  830. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  831. echo_char(ERASE_CHAR(tty), tty);
  832. } else if (c == '\t') {
  833. unsigned int num_chars = 0;
  834. int after_tab = 0;
  835. unsigned long tail = tty->read_head;
  836. /*
  837. * Count the columns used for characters
  838. * since the start of input or after a
  839. * previous tab.
  840. * This info is used to go back the correct
  841. * number of columns.
  842. */
  843. while (tail != tty->canon_head) {
  844. tail = (tail-1) & (N_TTY_BUF_SIZE-1);
  845. c = tty->read_buf[tail];
  846. if (c == '\t') {
  847. after_tab = 1;
  848. break;
  849. } else if (iscntrl(c)) {
  850. if (L_ECHOCTL(tty))
  851. num_chars += 2;
  852. } else if (!is_continuation(c, tty)) {
  853. num_chars++;
  854. }
  855. }
  856. echo_erase_tab(num_chars, after_tab, tty);
  857. } else {
  858. if (iscntrl(c) && L_ECHOCTL(tty)) {
  859. echo_char_raw('\b', tty);
  860. echo_char_raw(' ', tty);
  861. echo_char_raw('\b', tty);
  862. }
  863. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  864. echo_char_raw('\b', tty);
  865. echo_char_raw(' ', tty);
  866. echo_char_raw('\b', tty);
  867. }
  868. }
  869. }
  870. if (kill_type == ERASE)
  871. break;
  872. }
  873. if (tty->read_head == tty->canon_head && L_ECHO(tty))
  874. finish_erasing(tty);
  875. }
  876. /**
  877. * isig - handle the ISIG optio
  878. * @sig: signal
  879. * @tty: terminal
  880. * @flush: force flush
  881. *
  882. * Called when a signal is being sent due to terminal input. This
  883. * may caus terminal flushing to take place according to the termios
  884. * settings and character used. Called from the driver receive_buf
  885. * path so serialized.
  886. *
  887. * Locking: ctrl_lock, read_lock (both via flush buffer)
  888. */
  889. static inline void isig(int sig, struct tty_struct *tty, int flush)
  890. {
  891. if (tty->pgrp)
  892. kill_pgrp(tty->pgrp, sig, 1);
  893. if (flush || !L_NOFLSH(tty)) {
  894. n_tty_flush_buffer(tty);
  895. tty_driver_flush_buffer(tty);
  896. }
  897. }
  898. /**
  899. * n_tty_receive_break - handle break
  900. * @tty: terminal
  901. *
  902. * An RS232 break event has been hit in the incoming bitstream. This
  903. * can cause a variety of events depending upon the termios settings.
  904. *
  905. * Called from the receive_buf path so single threaded.
  906. */
  907. static inline void n_tty_receive_break(struct tty_struct *tty)
  908. {
  909. if (I_IGNBRK(tty))
  910. return;
  911. if (I_BRKINT(tty)) {
  912. isig(SIGINT, tty, 1);
  913. return;
  914. }
  915. if (I_PARMRK(tty)) {
  916. put_tty_queue('\377', tty);
  917. put_tty_queue('\0', tty);
  918. }
  919. put_tty_queue('\0', tty);
  920. wake_up_interruptible(&tty->read_wait);
  921. }
  922. /**
  923. * n_tty_receive_overrun - handle overrun reporting
  924. * @tty: terminal
  925. *
  926. * Data arrived faster than we could process it. While the tty
  927. * driver has flagged this the bits that were missed are gone
  928. * forever.
  929. *
  930. * Called from the receive_buf path so single threaded. Does not
  931. * need locking as num_overrun and overrun_time are function
  932. * private.
  933. */
  934. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  935. {
  936. char buf[64];
  937. tty->num_overrun++;
  938. if (time_before(tty->overrun_time, jiffies - HZ) ||
  939. time_after(tty->overrun_time, jiffies)) {
  940. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  941. tty_name(tty, buf),
  942. tty->num_overrun);
  943. tty->overrun_time = jiffies;
  944. tty->num_overrun = 0;
  945. }
  946. }
  947. /**
  948. * n_tty_receive_parity_error - error notifier
  949. * @tty: terminal device
  950. * @c: character
  951. *
  952. * Process a parity error and queue the right data to indicate
  953. * the error case if necessary. Locking as per n_tty_receive_buf.
  954. */
  955. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  956. unsigned char c)
  957. {
  958. if (I_IGNPAR(tty))
  959. return;
  960. if (I_PARMRK(tty)) {
  961. put_tty_queue('\377', tty);
  962. put_tty_queue('\0', tty);
  963. put_tty_queue(c, tty);
  964. } else if (I_INPCK(tty))
  965. put_tty_queue('\0', tty);
  966. else
  967. put_tty_queue(c, tty);
  968. wake_up_interruptible(&tty->read_wait);
  969. }
  970. /**
  971. * n_tty_receive_char - perform processing
  972. * @tty: terminal device
  973. * @c: character
  974. *
  975. * Process an individual character of input received from the driver.
  976. * This is serialized with respect to itself by the rules for the
  977. * driver above.
  978. */
  979. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  980. {
  981. unsigned long flags;
  982. int parmrk;
  983. if (tty->raw) {
  984. put_tty_queue(c, tty);
  985. return;
  986. }
  987. if (I_ISTRIP(tty))
  988. c &= 0x7f;
  989. if (I_IUCLC(tty) && L_IEXTEN(tty))
  990. c = tolower(c);
  991. if (L_EXTPROC(tty)) {
  992. put_tty_queue(c, tty);
  993. return;
  994. }
  995. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  996. I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty) &&
  997. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) && c != SUSP_CHAR(tty)) {
  998. start_tty(tty);
  999. process_echoes(tty);
  1000. }
  1001. if (tty->closing) {
  1002. if (I_IXON(tty)) {
  1003. if (c == START_CHAR(tty)) {
  1004. start_tty(tty);
  1005. process_echoes(tty);
  1006. } else if (c == STOP_CHAR(tty))
  1007. stop_tty(tty);
  1008. }
  1009. return;
  1010. }
  1011. /*
  1012. * If the previous character was LNEXT, or we know that this
  1013. * character is not one of the characters that we'll have to
  1014. * handle specially, do shortcut processing to speed things
  1015. * up.
  1016. */
  1017. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  1018. tty->lnext = 0;
  1019. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1020. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1021. /* beep if no space */
  1022. if (L_ECHO(tty))
  1023. process_output('\a', tty);
  1024. return;
  1025. }
  1026. if (L_ECHO(tty)) {
  1027. finish_erasing(tty);
  1028. /* Record the column of first canon char. */
  1029. if (tty->canon_head == tty->read_head)
  1030. echo_set_canon_col(tty);
  1031. echo_char(c, tty);
  1032. process_echoes(tty);
  1033. }
  1034. if (parmrk)
  1035. put_tty_queue(c, tty);
  1036. put_tty_queue(c, tty);
  1037. return;
  1038. }
  1039. if (I_IXON(tty)) {
  1040. if (c == START_CHAR(tty)) {
  1041. start_tty(tty);
  1042. process_echoes(tty);
  1043. return;
  1044. }
  1045. if (c == STOP_CHAR(tty)) {
  1046. stop_tty(tty);
  1047. return;
  1048. }
  1049. }
  1050. if (L_ISIG(tty)) {
  1051. int signal;
  1052. signal = SIGINT;
  1053. if (c == INTR_CHAR(tty))
  1054. goto send_signal;
  1055. signal = SIGQUIT;
  1056. if (c == QUIT_CHAR(tty))
  1057. goto send_signal;
  1058. signal = SIGTSTP;
  1059. if (c == SUSP_CHAR(tty)) {
  1060. send_signal:
  1061. /*
  1062. * Note that we do not use isig() here because we want
  1063. * the order to be:
  1064. * 1) flush, 2) echo, 3) signal
  1065. */
  1066. if (!L_NOFLSH(tty)) {
  1067. n_tty_flush_buffer(tty);
  1068. tty_driver_flush_buffer(tty);
  1069. }
  1070. if (I_IXON(tty))
  1071. start_tty(tty);
  1072. if (L_ECHO(tty)) {
  1073. echo_char(c, tty);
  1074. process_echoes(tty);
  1075. }
  1076. if (tty->pgrp)
  1077. kill_pgrp(tty->pgrp, signal, 1);
  1078. return;
  1079. }
  1080. }
  1081. if (c == '\r') {
  1082. if (I_IGNCR(tty))
  1083. return;
  1084. if (I_ICRNL(tty))
  1085. c = '\n';
  1086. } else if (c == '\n' && I_INLCR(tty))
  1087. c = '\r';
  1088. if (tty->icanon) {
  1089. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1090. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1091. eraser(c, tty);
  1092. process_echoes(tty);
  1093. return;
  1094. }
  1095. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1096. tty->lnext = 1;
  1097. if (L_ECHO(tty)) {
  1098. finish_erasing(tty);
  1099. if (L_ECHOCTL(tty)) {
  1100. echo_char_raw('^', tty);
  1101. echo_char_raw('\b', tty);
  1102. process_echoes(tty);
  1103. }
  1104. }
  1105. return;
  1106. }
  1107. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  1108. L_IEXTEN(tty)) {
  1109. unsigned long tail = tty->canon_head;
  1110. finish_erasing(tty);
  1111. echo_char(c, tty);
  1112. echo_char_raw('\n', tty);
  1113. while (tail != tty->read_head) {
  1114. echo_char(tty->read_buf[tail], tty);
  1115. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1116. }
  1117. process_echoes(tty);
  1118. return;
  1119. }
  1120. if (c == '\n') {
  1121. if (tty->read_cnt >= N_TTY_BUF_SIZE) {
  1122. if (L_ECHO(tty))
  1123. process_output('\a', tty);
  1124. return;
  1125. }
  1126. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1127. echo_char_raw('\n', tty);
  1128. process_echoes(tty);
  1129. }
  1130. goto handle_newline;
  1131. }
  1132. if (c == EOF_CHAR(tty)) {
  1133. if (tty->read_cnt >= N_TTY_BUF_SIZE)
  1134. return;
  1135. if (tty->canon_head != tty->read_head)
  1136. set_bit(TTY_PUSH, &tty->flags);
  1137. c = __DISABLED_CHAR;
  1138. goto handle_newline;
  1139. }
  1140. if ((c == EOL_CHAR(tty)) ||
  1141. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1142. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty))
  1143. ? 1 : 0;
  1144. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk)) {
  1145. if (L_ECHO(tty))
  1146. process_output('\a', tty);
  1147. return;
  1148. }
  1149. /*
  1150. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1151. */
  1152. if (L_ECHO(tty)) {
  1153. /* Record the column of first canon char. */
  1154. if (tty->canon_head == tty->read_head)
  1155. echo_set_canon_col(tty);
  1156. echo_char(c, tty);
  1157. process_echoes(tty);
  1158. }
  1159. /*
  1160. * XXX does PARMRK doubling happen for
  1161. * EOL_CHAR and EOL2_CHAR?
  1162. */
  1163. if (parmrk)
  1164. put_tty_queue(c, tty);
  1165. handle_newline:
  1166. spin_lock_irqsave(&tty->read_lock, flags);
  1167. set_bit(tty->read_head, tty->read_flags);
  1168. put_tty_queue_nolock(c, tty);
  1169. tty->canon_head = tty->read_head;
  1170. tty->canon_data++;
  1171. spin_unlock_irqrestore(&tty->read_lock, flags);
  1172. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1173. if (waitqueue_active(&tty->read_wait))
  1174. wake_up_interruptible(&tty->read_wait);
  1175. return;
  1176. }
  1177. }
  1178. parmrk = (c == (unsigned char) '\377' && I_PARMRK(tty)) ? 1 : 0;
  1179. if (tty->read_cnt >= (N_TTY_BUF_SIZE - parmrk - 1)) {
  1180. /* beep if no space */
  1181. if (L_ECHO(tty))
  1182. process_output('\a', tty);
  1183. return;
  1184. }
  1185. if (L_ECHO(tty)) {
  1186. finish_erasing(tty);
  1187. if (c == '\n')
  1188. echo_char_raw('\n', tty);
  1189. else {
  1190. /* Record the column of first canon char. */
  1191. if (tty->canon_head == tty->read_head)
  1192. echo_set_canon_col(tty);
  1193. echo_char(c, tty);
  1194. }
  1195. process_echoes(tty);
  1196. }
  1197. if (parmrk)
  1198. put_tty_queue(c, tty);
  1199. put_tty_queue(c, tty);
  1200. }
  1201. /**
  1202. * n_tty_write_wakeup - asynchronous I/O notifier
  1203. * @tty: tty device
  1204. *
  1205. * Required for the ptys, serial driver etc. since processes
  1206. * that attach themselves to the master and rely on ASYNC
  1207. * IO must be woken up
  1208. */
  1209. static void n_tty_write_wakeup(struct tty_struct *tty)
  1210. {
  1211. if (tty->fasync && test_and_clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags))
  1212. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  1213. }
  1214. /**
  1215. * n_tty_receive_buf - data receive
  1216. * @tty: terminal device
  1217. * @cp: buffer
  1218. * @fp: flag buffer
  1219. * @count: characters
  1220. *
  1221. * Called by the terminal driver when a block of characters has
  1222. * been received. This function must be called from soft contexts
  1223. * not from interrupt context. The driver is responsible for making
  1224. * calls one at a time and in order (or using flush_to_ldisc)
  1225. */
  1226. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1227. char *fp, int count)
  1228. {
  1229. const unsigned char *p;
  1230. char *f, flags = TTY_NORMAL;
  1231. int i;
  1232. char buf[64];
  1233. unsigned long cpuflags;
  1234. if (!tty->read_buf)
  1235. return;
  1236. if (tty->real_raw) {
  1237. spin_lock_irqsave(&tty->read_lock, cpuflags);
  1238. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1239. N_TTY_BUF_SIZE - tty->read_head);
  1240. i = min(count, i);
  1241. memcpy(tty->read_buf + tty->read_head, cp, i);
  1242. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1243. tty->read_cnt += i;
  1244. cp += i;
  1245. count -= i;
  1246. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  1247. N_TTY_BUF_SIZE - tty->read_head);
  1248. i = min(count, i);
  1249. memcpy(tty->read_buf + tty->read_head, cp, i);
  1250. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  1251. tty->read_cnt += i;
  1252. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  1253. } else {
  1254. for (i = count, p = cp, f = fp; i; i--, p++) {
  1255. if (f)
  1256. flags = *f++;
  1257. switch (flags) {
  1258. case TTY_NORMAL:
  1259. n_tty_receive_char(tty, *p);
  1260. break;
  1261. case TTY_BREAK:
  1262. n_tty_receive_break(tty);
  1263. break;
  1264. case TTY_PARITY:
  1265. case TTY_FRAME:
  1266. n_tty_receive_parity_error(tty, *p);
  1267. break;
  1268. case TTY_OVERRUN:
  1269. n_tty_receive_overrun(tty);
  1270. break;
  1271. default:
  1272. printk(KERN_ERR "%s: unknown flag %d\n",
  1273. tty_name(tty, buf), flags);
  1274. break;
  1275. }
  1276. }
  1277. if (tty->ops->flush_chars)
  1278. tty->ops->flush_chars(tty);
  1279. }
  1280. n_tty_set_room(tty);
  1281. if ((!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) ||
  1282. L_EXTPROC(tty)) {
  1283. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1284. if (waitqueue_active(&tty->read_wait))
  1285. wake_up_interruptible(&tty->read_wait);
  1286. }
  1287. /*
  1288. * Check the remaining room for the input canonicalization
  1289. * mode. We don't want to throttle the driver if we're in
  1290. * canonical mode and don't have a newline yet!
  1291. */
  1292. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  1293. tty_throttle(tty);
  1294. }
  1295. int is_ignored(int sig)
  1296. {
  1297. return (sigismember(&current->blocked, sig) ||
  1298. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  1299. }
  1300. /**
  1301. * n_tty_set_termios - termios data changed
  1302. * @tty: terminal
  1303. * @old: previous data
  1304. *
  1305. * Called by the tty layer when the user changes termios flags so
  1306. * that the line discipline can plan ahead. This function cannot sleep
  1307. * and is protected from re-entry by the tty layer. The user is
  1308. * guaranteed that this function will not be re-entered or in progress
  1309. * when the ldisc is closed.
  1310. *
  1311. * Locking: Caller holds tty->termios_mutex
  1312. */
  1313. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1314. {
  1315. int canon_change = 1;
  1316. BUG_ON(!tty);
  1317. if (old)
  1318. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  1319. if (canon_change) {
  1320. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  1321. tty->canon_head = tty->read_tail;
  1322. tty->canon_data = 0;
  1323. tty->erasing = 0;
  1324. }
  1325. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  1326. wake_up_interruptible(&tty->read_wait);
  1327. tty->icanon = (L_ICANON(tty) != 0);
  1328. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  1329. tty->raw = 1;
  1330. tty->real_raw = 1;
  1331. n_tty_set_room(tty);
  1332. return;
  1333. }
  1334. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1335. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1336. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1337. I_PARMRK(tty)) {
  1338. memset(tty->process_char_map, 0, 256/8);
  1339. if (I_IGNCR(tty) || I_ICRNL(tty))
  1340. set_bit('\r', tty->process_char_map);
  1341. if (I_INLCR(tty))
  1342. set_bit('\n', tty->process_char_map);
  1343. if (L_ICANON(tty)) {
  1344. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  1345. set_bit(KILL_CHAR(tty), tty->process_char_map);
  1346. set_bit(EOF_CHAR(tty), tty->process_char_map);
  1347. set_bit('\n', tty->process_char_map);
  1348. set_bit(EOL_CHAR(tty), tty->process_char_map);
  1349. if (L_IEXTEN(tty)) {
  1350. set_bit(WERASE_CHAR(tty),
  1351. tty->process_char_map);
  1352. set_bit(LNEXT_CHAR(tty),
  1353. tty->process_char_map);
  1354. set_bit(EOL2_CHAR(tty),
  1355. tty->process_char_map);
  1356. if (L_ECHO(tty))
  1357. set_bit(REPRINT_CHAR(tty),
  1358. tty->process_char_map);
  1359. }
  1360. }
  1361. if (I_IXON(tty)) {
  1362. set_bit(START_CHAR(tty), tty->process_char_map);
  1363. set_bit(STOP_CHAR(tty), tty->process_char_map);
  1364. }
  1365. if (L_ISIG(tty)) {
  1366. set_bit(INTR_CHAR(tty), tty->process_char_map);
  1367. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  1368. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  1369. }
  1370. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  1371. tty->raw = 0;
  1372. tty->real_raw = 0;
  1373. } else {
  1374. tty->raw = 1;
  1375. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1376. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1377. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1378. tty->real_raw = 1;
  1379. else
  1380. tty->real_raw = 0;
  1381. }
  1382. n_tty_set_room(tty);
  1383. /* The termios change make the tty ready for I/O */
  1384. wake_up_interruptible(&tty->write_wait);
  1385. wake_up_interruptible(&tty->read_wait);
  1386. }
  1387. /**
  1388. * n_tty_close - close the ldisc for this tty
  1389. * @tty: device
  1390. *
  1391. * Called from the terminal layer when this line discipline is
  1392. * being shut down, either because of a close or becsuse of a
  1393. * discipline change. The function will not be called while other
  1394. * ldisc methods are in progress.
  1395. */
  1396. static void n_tty_close(struct tty_struct *tty)
  1397. {
  1398. n_tty_flush_buffer(tty);
  1399. if (tty->read_buf) {
  1400. kfree(tty->read_buf);
  1401. tty->read_buf = NULL;
  1402. }
  1403. if (tty->echo_buf) {
  1404. kfree(tty->echo_buf);
  1405. tty->echo_buf = NULL;
  1406. }
  1407. }
  1408. /**
  1409. * n_tty_open - open an ldisc
  1410. * @tty: terminal to open
  1411. *
  1412. * Called when this line discipline is being attached to the
  1413. * terminal device. Can sleep. Called serialized so that no
  1414. * other events will occur in parallel. No further open will occur
  1415. * until a close.
  1416. */
  1417. static int n_tty_open(struct tty_struct *tty)
  1418. {
  1419. if (!tty)
  1420. return -EINVAL;
  1421. /* These are ugly. Currently a malloc failure here can panic */
  1422. if (!tty->read_buf) {
  1423. tty->read_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1424. if (!tty->read_buf)
  1425. return -ENOMEM;
  1426. }
  1427. if (!tty->echo_buf) {
  1428. tty->echo_buf = kzalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  1429. if (!tty->echo_buf)
  1430. return -ENOMEM;
  1431. }
  1432. reset_buffer_flags(tty);
  1433. tty_unthrottle(tty);
  1434. tty->column = 0;
  1435. n_tty_set_termios(tty, NULL);
  1436. tty->minimum_to_wake = 1;
  1437. tty->closing = 0;
  1438. return 0;
  1439. }
  1440. static inline int input_available_p(struct tty_struct *tty, int amt)
  1441. {
  1442. tty_flush_to_ldisc(tty);
  1443. if (tty->icanon && !L_EXTPROC(tty)) {
  1444. if (tty->canon_data)
  1445. return 1;
  1446. } else if (tty->read_cnt >= (amt ? amt : 1))
  1447. return 1;
  1448. return 0;
  1449. }
  1450. /**
  1451. * copy_from_read_buf - copy read data directly
  1452. * @tty: terminal device
  1453. * @b: user data
  1454. * @nr: size of data
  1455. *
  1456. * Helper function to speed up n_tty_read. It is only called when
  1457. * ICANON is off; it copies characters straight from the tty queue to
  1458. * user space directly. It can be profitably called twice; once to
  1459. * drain the space from the tail pointer to the (physical) end of the
  1460. * buffer, and once to drain the space from the (physical) beginning of
  1461. * the buffer to head pointer.
  1462. *
  1463. * Called under the tty->atomic_read_lock sem
  1464. *
  1465. */
  1466. static int copy_from_read_buf(struct tty_struct *tty,
  1467. unsigned char __user **b,
  1468. size_t *nr)
  1469. {
  1470. int retval;
  1471. size_t n;
  1472. unsigned long flags;
  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. tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1482. spin_lock_irqsave(&tty->read_lock, flags);
  1483. tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1484. tty->read_cnt -= n;
  1485. /* Turn single EOF into zero-length read */
  1486. if (L_EXTPROC(tty) && tty->icanon && n == 1) {
  1487. if (!tty->read_cnt && (*b)[n-1] == EOF_CHAR(tty))
  1488. n--;
  1489. }
  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);