n_tty.c 61 KB

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