n_tty.c 61 KB

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