n_tty.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  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. #define MASK(x) ((x) & (N_TTY_BUF_SIZE - 1))
  111. static inline size_t read_cnt(struct n_tty_data *ldata)
  112. {
  113. return ldata->read_head - ldata->read_tail;
  114. }
  115. static inline unsigned char read_buf(struct n_tty_data *ldata, size_t i)
  116. {
  117. return ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
  118. }
  119. static inline unsigned char *read_buf_addr(struct n_tty_data *ldata, size_t i)
  120. {
  121. return &ldata->read_buf[i & (N_TTY_BUF_SIZE - 1)];
  122. }
  123. static inline unsigned char echo_buf(struct n_tty_data *ldata, size_t i)
  124. {
  125. smp_rmb(); /* Matches smp_wmb() in add_echo_byte(). */
  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->commit_head = 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 (MASK(ldata->echo_commit) != MASK(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. * Since add_echo_byte() is called without holding
  554. * output_lock, we might see only portion of multi-byte
  555. * operation.
  556. */
  557. if (MASK(ldata->echo_commit) == MASK(tail + 1))
  558. goto not_yet_stored;
  559. /*
  560. * If the buffer byte is the start of a multi-byte
  561. * operation, get the next byte, which is either the
  562. * op code or a control character value.
  563. */
  564. op = echo_buf(ldata, tail + 1);
  565. switch (op) {
  566. unsigned int num_chars, num_bs;
  567. case ECHO_OP_ERASE_TAB:
  568. if (MASK(ldata->echo_commit) == MASK(tail + 2))
  569. goto not_yet_stored;
  570. num_chars = echo_buf(ldata, tail + 2);
  571. /*
  572. * Determine how many columns to go back
  573. * in order to erase the tab.
  574. * This depends on the number of columns
  575. * used by other characters within the tab
  576. * area. If this (modulo 8) count is from
  577. * the start of input rather than from a
  578. * previous tab, we offset by canon column.
  579. * Otherwise, tab spacing is normal.
  580. */
  581. if (!(num_chars & 0x80))
  582. num_chars += ldata->canon_column;
  583. num_bs = 8 - (num_chars & 7);
  584. if (num_bs > space) {
  585. no_space_left = 1;
  586. break;
  587. }
  588. space -= num_bs;
  589. while (num_bs--) {
  590. tty_put_char(tty, '\b');
  591. if (ldata->column > 0)
  592. ldata->column--;
  593. }
  594. tail += 3;
  595. break;
  596. case ECHO_OP_SET_CANON_COL:
  597. ldata->canon_column = ldata->column;
  598. tail += 2;
  599. break;
  600. case ECHO_OP_MOVE_BACK_COL:
  601. if (ldata->column > 0)
  602. ldata->column--;
  603. tail += 2;
  604. break;
  605. case ECHO_OP_START:
  606. /* This is an escaped echo op start code */
  607. if (!space) {
  608. no_space_left = 1;
  609. break;
  610. }
  611. tty_put_char(tty, ECHO_OP_START);
  612. ldata->column++;
  613. space--;
  614. tail += 2;
  615. break;
  616. default:
  617. /*
  618. * If the op is not a special byte code,
  619. * it is a ctrl char tagged to be echoed
  620. * as "^X" (where X is the letter
  621. * representing the control char).
  622. * Note that we must ensure there is
  623. * enough space for the whole ctrl pair.
  624. *
  625. */
  626. if (space < 2) {
  627. no_space_left = 1;
  628. break;
  629. }
  630. tty_put_char(tty, '^');
  631. tty_put_char(tty, op ^ 0100);
  632. ldata->column += 2;
  633. space -= 2;
  634. tail += 2;
  635. }
  636. if (no_space_left)
  637. break;
  638. } else {
  639. if (O_OPOST(tty)) {
  640. int retval = do_output_char(c, tty, space);
  641. if (retval < 0)
  642. break;
  643. space -= retval;
  644. } else {
  645. if (!space)
  646. break;
  647. tty_put_char(tty, c);
  648. space -= 1;
  649. }
  650. tail += 1;
  651. }
  652. }
  653. /* If the echo buffer is nearly full (so that the possibility exists
  654. * of echo overrun before the next commit), then discard enough
  655. * data at the tail to prevent a subsequent overrun */
  656. while (ldata->echo_commit > tail &&
  657. ldata->echo_commit - tail >= ECHO_DISCARD_WATERMARK) {
  658. if (echo_buf(ldata, tail) == ECHO_OP_START) {
  659. if (echo_buf(ldata, tail + 1) == ECHO_OP_ERASE_TAB)
  660. tail += 3;
  661. else
  662. tail += 2;
  663. } else
  664. tail++;
  665. }
  666. not_yet_stored:
  667. ldata->echo_tail = tail;
  668. return old_space - space;
  669. }
  670. static void commit_echoes(struct tty_struct *tty)
  671. {
  672. struct n_tty_data *ldata = tty->disc_data;
  673. size_t nr, old, echoed;
  674. size_t head;
  675. mutex_lock(&ldata->output_lock);
  676. head = ldata->echo_head;
  677. ldata->echo_mark = head;
  678. old = ldata->echo_commit - ldata->echo_tail;
  679. /* Process committed echoes if the accumulated # of bytes
  680. * is over the threshold (and try again each time another
  681. * block is accumulated) */
  682. nr = head - ldata->echo_tail;
  683. if (nr < ECHO_COMMIT_WATERMARK ||
  684. (nr % ECHO_BLOCK > old % ECHO_BLOCK)) {
  685. mutex_unlock(&ldata->output_lock);
  686. return;
  687. }
  688. ldata->echo_commit = head;
  689. echoed = __process_echoes(tty);
  690. mutex_unlock(&ldata->output_lock);
  691. if (echoed && tty->ops->flush_chars)
  692. tty->ops->flush_chars(tty);
  693. }
  694. static void process_echoes(struct tty_struct *tty)
  695. {
  696. struct n_tty_data *ldata = tty->disc_data;
  697. size_t echoed;
  698. if (ldata->echo_mark == ldata->echo_tail)
  699. return;
  700. mutex_lock(&ldata->output_lock);
  701. ldata->echo_commit = ldata->echo_mark;
  702. echoed = __process_echoes(tty);
  703. mutex_unlock(&ldata->output_lock);
  704. if (echoed && tty->ops->flush_chars)
  705. tty->ops->flush_chars(tty);
  706. }
  707. /* NB: echo_mark and echo_head should be equivalent here */
  708. static void flush_echoes(struct tty_struct *tty)
  709. {
  710. struct n_tty_data *ldata = tty->disc_data;
  711. if ((!L_ECHO(tty) && !L_ECHONL(tty)) ||
  712. ldata->echo_commit == ldata->echo_head)
  713. return;
  714. mutex_lock(&ldata->output_lock);
  715. ldata->echo_commit = ldata->echo_head;
  716. __process_echoes(tty);
  717. mutex_unlock(&ldata->output_lock);
  718. }
  719. /**
  720. * add_echo_byte - add a byte to the echo buffer
  721. * @c: unicode byte to echo
  722. * @ldata: n_tty data
  723. *
  724. * Add a character or operation byte to the echo buffer.
  725. */
  726. static inline void add_echo_byte(unsigned char c, struct n_tty_data *ldata)
  727. {
  728. *echo_buf_addr(ldata, ldata->echo_head) = c;
  729. smp_wmb(); /* Matches smp_rmb() in echo_buf(). */
  730. ldata->echo_head++;
  731. }
  732. /**
  733. * echo_move_back_col - add operation to move back a column
  734. * @ldata: n_tty data
  735. *
  736. * Add an operation to the echo buffer to move back one column.
  737. */
  738. static void echo_move_back_col(struct n_tty_data *ldata)
  739. {
  740. add_echo_byte(ECHO_OP_START, ldata);
  741. add_echo_byte(ECHO_OP_MOVE_BACK_COL, ldata);
  742. }
  743. /**
  744. * echo_set_canon_col - add operation to set the canon column
  745. * @ldata: n_tty data
  746. *
  747. * Add an operation to the echo buffer to set the canon column
  748. * to the current column.
  749. */
  750. static void echo_set_canon_col(struct n_tty_data *ldata)
  751. {
  752. add_echo_byte(ECHO_OP_START, ldata);
  753. add_echo_byte(ECHO_OP_SET_CANON_COL, ldata);
  754. }
  755. /**
  756. * echo_erase_tab - add operation to erase a tab
  757. * @num_chars: number of character columns already used
  758. * @after_tab: true if num_chars starts after a previous tab
  759. * @ldata: n_tty data
  760. *
  761. * Add an operation to the echo buffer to erase a tab.
  762. *
  763. * Called by the eraser function, which knows how many character
  764. * columns have been used since either a previous tab or the start
  765. * of input. This information will be used later, along with
  766. * canon column (if applicable), to go back the correct number
  767. * of columns.
  768. */
  769. static void echo_erase_tab(unsigned int num_chars, int after_tab,
  770. struct n_tty_data *ldata)
  771. {
  772. add_echo_byte(ECHO_OP_START, ldata);
  773. add_echo_byte(ECHO_OP_ERASE_TAB, ldata);
  774. /* We only need to know this modulo 8 (tab spacing) */
  775. num_chars &= 7;
  776. /* Set the high bit as a flag if num_chars is after a previous tab */
  777. if (after_tab)
  778. num_chars |= 0x80;
  779. add_echo_byte(num_chars, ldata);
  780. }
  781. /**
  782. * echo_char_raw - echo a character raw
  783. * @c: unicode byte to echo
  784. * @tty: terminal device
  785. *
  786. * Echo user input back onto the screen. This must be called only when
  787. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  788. *
  789. * This variant does not treat control characters specially.
  790. */
  791. static void echo_char_raw(unsigned char c, struct n_tty_data *ldata)
  792. {
  793. if (c == ECHO_OP_START) {
  794. add_echo_byte(ECHO_OP_START, ldata);
  795. add_echo_byte(ECHO_OP_START, ldata);
  796. } else {
  797. add_echo_byte(c, ldata);
  798. }
  799. }
  800. /**
  801. * echo_char - echo a character
  802. * @c: unicode byte to echo
  803. * @tty: terminal device
  804. *
  805. * Echo user input back onto the screen. This must be called only when
  806. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  807. *
  808. * This variant tags control characters to be echoed as "^X"
  809. * (where X is the letter representing the control char).
  810. */
  811. static void echo_char(unsigned char c, struct tty_struct *tty)
  812. {
  813. struct n_tty_data *ldata = tty->disc_data;
  814. if (c == ECHO_OP_START) {
  815. add_echo_byte(ECHO_OP_START, ldata);
  816. add_echo_byte(ECHO_OP_START, ldata);
  817. } else {
  818. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t')
  819. add_echo_byte(ECHO_OP_START, ldata);
  820. add_echo_byte(c, ldata);
  821. }
  822. }
  823. /**
  824. * finish_erasing - complete erase
  825. * @ldata: n_tty data
  826. */
  827. static inline void finish_erasing(struct n_tty_data *ldata)
  828. {
  829. if (ldata->erasing) {
  830. echo_char_raw('/', ldata);
  831. ldata->erasing = 0;
  832. }
  833. }
  834. /**
  835. * eraser - handle erase function
  836. * @c: character input
  837. * @tty: terminal device
  838. *
  839. * Perform erase and necessary output when an erase character is
  840. * present in the stream from the driver layer. Handles the complexities
  841. * of UTF-8 multibyte symbols.
  842. *
  843. * n_tty_receive_buf()/producer path:
  844. * caller holds non-exclusive termios_rwsem
  845. */
  846. static void eraser(unsigned char c, struct tty_struct *tty)
  847. {
  848. struct n_tty_data *ldata = tty->disc_data;
  849. enum { ERASE, WERASE, KILL } kill_type;
  850. size_t head;
  851. size_t cnt;
  852. int seen_alnums;
  853. if (ldata->read_head == ldata->canon_head) {
  854. /* process_output('\a', tty); */ /* what do you think? */
  855. return;
  856. }
  857. if (c == ERASE_CHAR(tty))
  858. kill_type = ERASE;
  859. else if (c == WERASE_CHAR(tty))
  860. kill_type = WERASE;
  861. else {
  862. if (!L_ECHO(tty)) {
  863. ldata->read_head = ldata->canon_head;
  864. return;
  865. }
  866. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  867. ldata->read_head = ldata->canon_head;
  868. finish_erasing(ldata);
  869. echo_char(KILL_CHAR(tty), tty);
  870. /* Add a newline if ECHOK is on and ECHOKE is off. */
  871. if (L_ECHOK(tty))
  872. echo_char_raw('\n', ldata);
  873. return;
  874. }
  875. kill_type = KILL;
  876. }
  877. seen_alnums = 0;
  878. while (MASK(ldata->read_head) != MASK(ldata->canon_head)) {
  879. head = ldata->read_head;
  880. /* erase a single possibly multibyte character */
  881. do {
  882. head--;
  883. c = read_buf(ldata, head);
  884. } while (is_continuation(c, tty) &&
  885. MASK(head) != MASK(ldata->canon_head));
  886. /* do not partially erase */
  887. if (is_continuation(c, tty))
  888. break;
  889. if (kill_type == WERASE) {
  890. /* Equivalent to BSD's ALTWERASE. */
  891. if (isalnum(c) || c == '_')
  892. seen_alnums++;
  893. else if (seen_alnums)
  894. break;
  895. }
  896. cnt = ldata->read_head - head;
  897. ldata->read_head = head;
  898. if (L_ECHO(tty)) {
  899. if (L_ECHOPRT(tty)) {
  900. if (!ldata->erasing) {
  901. echo_char_raw('\\', ldata);
  902. ldata->erasing = 1;
  903. }
  904. /* if cnt > 1, output a multi-byte character */
  905. echo_char(c, tty);
  906. while (--cnt > 0) {
  907. head++;
  908. echo_char_raw(read_buf(ldata, head), ldata);
  909. echo_move_back_col(ldata);
  910. }
  911. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  912. echo_char(ERASE_CHAR(tty), tty);
  913. } else if (c == '\t') {
  914. unsigned int num_chars = 0;
  915. int after_tab = 0;
  916. size_t tail = ldata->read_head;
  917. /*
  918. * Count the columns used for characters
  919. * since the start of input or after a
  920. * previous tab.
  921. * This info is used to go back the correct
  922. * number of columns.
  923. */
  924. while (MASK(tail) != MASK(ldata->canon_head)) {
  925. tail--;
  926. c = read_buf(ldata, tail);
  927. if (c == '\t') {
  928. after_tab = 1;
  929. break;
  930. } else if (iscntrl(c)) {
  931. if (L_ECHOCTL(tty))
  932. num_chars += 2;
  933. } else if (!is_continuation(c, tty)) {
  934. num_chars++;
  935. }
  936. }
  937. echo_erase_tab(num_chars, after_tab, ldata);
  938. } else {
  939. if (iscntrl(c) && L_ECHOCTL(tty)) {
  940. echo_char_raw('\b', ldata);
  941. echo_char_raw(' ', ldata);
  942. echo_char_raw('\b', ldata);
  943. }
  944. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  945. echo_char_raw('\b', ldata);
  946. echo_char_raw(' ', ldata);
  947. echo_char_raw('\b', ldata);
  948. }
  949. }
  950. }
  951. if (kill_type == ERASE)
  952. break;
  953. }
  954. if (ldata->read_head == ldata->canon_head && L_ECHO(tty))
  955. finish_erasing(ldata);
  956. }
  957. /**
  958. * isig - handle the ISIG optio
  959. * @sig: signal
  960. * @tty: terminal
  961. *
  962. * Called when a signal is being sent due to terminal input.
  963. * Called from the driver receive_buf path so serialized.
  964. *
  965. * Performs input and output flush if !NOFLSH. In this context, the echo
  966. * buffer is 'output'. The signal is processed first to alert any current
  967. * readers or writers to discontinue and exit their i/o loops.
  968. *
  969. * Locking: ctrl_lock
  970. */
  971. static void __isig(int sig, struct tty_struct *tty)
  972. {
  973. struct pid *tty_pgrp = tty_get_pgrp(tty);
  974. if (tty_pgrp) {
  975. kill_pgrp(tty_pgrp, sig, 1);
  976. put_pid(tty_pgrp);
  977. }
  978. }
  979. static void isig(int sig, struct tty_struct *tty)
  980. {
  981. struct n_tty_data *ldata = tty->disc_data;
  982. if (L_NOFLSH(tty)) {
  983. /* signal only */
  984. __isig(sig, tty);
  985. } else { /* signal and flush */
  986. up_read(&tty->termios_rwsem);
  987. down_write(&tty->termios_rwsem);
  988. __isig(sig, tty);
  989. /* clear echo buffer */
  990. mutex_lock(&ldata->output_lock);
  991. ldata->echo_head = ldata->echo_tail = 0;
  992. ldata->echo_mark = ldata->echo_commit = 0;
  993. mutex_unlock(&ldata->output_lock);
  994. /* clear output buffer */
  995. tty_driver_flush_buffer(tty);
  996. /* clear input buffer */
  997. reset_buffer_flags(tty->disc_data);
  998. /* notify pty master of flush */
  999. if (tty->link)
  1000. n_tty_packet_mode_flush(tty);
  1001. up_write(&tty->termios_rwsem);
  1002. down_read(&tty->termios_rwsem);
  1003. }
  1004. }
  1005. /**
  1006. * n_tty_receive_break - handle break
  1007. * @tty: terminal
  1008. *
  1009. * An RS232 break event has been hit in the incoming bitstream. This
  1010. * can cause a variety of events depending upon the termios settings.
  1011. *
  1012. * n_tty_receive_buf()/producer path:
  1013. * caller holds non-exclusive termios_rwsem
  1014. *
  1015. * Note: may get exclusive termios_rwsem if flushing input buffer
  1016. */
  1017. static void n_tty_receive_break(struct tty_struct *tty)
  1018. {
  1019. struct n_tty_data *ldata = tty->disc_data;
  1020. if (I_IGNBRK(tty))
  1021. return;
  1022. if (I_BRKINT(tty)) {
  1023. isig(SIGINT, tty);
  1024. return;
  1025. }
  1026. if (I_PARMRK(tty)) {
  1027. put_tty_queue('\377', ldata);
  1028. put_tty_queue('\0', ldata);
  1029. }
  1030. put_tty_queue('\0', ldata);
  1031. }
  1032. /**
  1033. * n_tty_receive_overrun - handle overrun reporting
  1034. * @tty: terminal
  1035. *
  1036. * Data arrived faster than we could process it. While the tty
  1037. * driver has flagged this the bits that were missed are gone
  1038. * forever.
  1039. *
  1040. * Called from the receive_buf path so single threaded. Does not
  1041. * need locking as num_overrun and overrun_time are function
  1042. * private.
  1043. */
  1044. static void n_tty_receive_overrun(struct tty_struct *tty)
  1045. {
  1046. struct n_tty_data *ldata = tty->disc_data;
  1047. ldata->num_overrun++;
  1048. if (time_after(jiffies, ldata->overrun_time + HZ) ||
  1049. time_after(ldata->overrun_time, jiffies)) {
  1050. tty_warn(tty, "%d input overrun(s)\n", ldata->num_overrun);
  1051. ldata->overrun_time = jiffies;
  1052. ldata->num_overrun = 0;
  1053. }
  1054. }
  1055. /**
  1056. * n_tty_receive_parity_error - error notifier
  1057. * @tty: terminal device
  1058. * @c: character
  1059. *
  1060. * Process a parity error and queue the right data to indicate
  1061. * the error case if necessary.
  1062. *
  1063. * n_tty_receive_buf()/producer path:
  1064. * caller holds non-exclusive termios_rwsem
  1065. */
  1066. static void n_tty_receive_parity_error(struct tty_struct *tty, unsigned char c)
  1067. {
  1068. struct n_tty_data *ldata = tty->disc_data;
  1069. if (I_INPCK(tty)) {
  1070. if (I_IGNPAR(tty))
  1071. return;
  1072. if (I_PARMRK(tty)) {
  1073. put_tty_queue('\377', ldata);
  1074. put_tty_queue('\0', ldata);
  1075. put_tty_queue(c, ldata);
  1076. } else
  1077. put_tty_queue('\0', ldata);
  1078. } else
  1079. put_tty_queue(c, ldata);
  1080. }
  1081. static void
  1082. n_tty_receive_signal_char(struct tty_struct *tty, int signal, unsigned char c)
  1083. {
  1084. isig(signal, tty);
  1085. if (I_IXON(tty))
  1086. start_tty(tty);
  1087. if (L_ECHO(tty)) {
  1088. echo_char(c, tty);
  1089. commit_echoes(tty);
  1090. } else
  1091. process_echoes(tty);
  1092. return;
  1093. }
  1094. /**
  1095. * n_tty_receive_char - perform processing
  1096. * @tty: terminal device
  1097. * @c: character
  1098. *
  1099. * Process an individual character of input received from the driver.
  1100. * This is serialized with respect to itself by the rules for the
  1101. * driver above.
  1102. *
  1103. * n_tty_receive_buf()/producer path:
  1104. * caller holds non-exclusive termios_rwsem
  1105. * publishes canon_head if canonical mode is active
  1106. *
  1107. * Returns 1 if LNEXT was received, else returns 0
  1108. */
  1109. static int
  1110. n_tty_receive_char_special(struct tty_struct *tty, unsigned char c)
  1111. {
  1112. struct n_tty_data *ldata = tty->disc_data;
  1113. if (I_IXON(tty)) {
  1114. if (c == START_CHAR(tty)) {
  1115. start_tty(tty);
  1116. process_echoes(tty);
  1117. return 0;
  1118. }
  1119. if (c == STOP_CHAR(tty)) {
  1120. stop_tty(tty);
  1121. return 0;
  1122. }
  1123. }
  1124. if (L_ISIG(tty)) {
  1125. if (c == INTR_CHAR(tty)) {
  1126. n_tty_receive_signal_char(tty, SIGINT, c);
  1127. return 0;
  1128. } else if (c == QUIT_CHAR(tty)) {
  1129. n_tty_receive_signal_char(tty, SIGQUIT, c);
  1130. return 0;
  1131. } else if (c == SUSP_CHAR(tty)) {
  1132. n_tty_receive_signal_char(tty, SIGTSTP, c);
  1133. return 0;
  1134. }
  1135. }
  1136. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1137. start_tty(tty);
  1138. process_echoes(tty);
  1139. }
  1140. if (c == '\r') {
  1141. if (I_IGNCR(tty))
  1142. return 0;
  1143. if (I_ICRNL(tty))
  1144. c = '\n';
  1145. } else if (c == '\n' && I_INLCR(tty))
  1146. c = '\r';
  1147. if (ldata->icanon) {
  1148. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  1149. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  1150. eraser(c, tty);
  1151. commit_echoes(tty);
  1152. return 0;
  1153. }
  1154. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  1155. ldata->lnext = 1;
  1156. if (L_ECHO(tty)) {
  1157. finish_erasing(ldata);
  1158. if (L_ECHOCTL(tty)) {
  1159. echo_char_raw('^', ldata);
  1160. echo_char_raw('\b', ldata);
  1161. commit_echoes(tty);
  1162. }
  1163. }
  1164. return 1;
  1165. }
  1166. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) && L_IEXTEN(tty)) {
  1167. size_t tail = ldata->canon_head;
  1168. finish_erasing(ldata);
  1169. echo_char(c, tty);
  1170. echo_char_raw('\n', ldata);
  1171. while (MASK(tail) != MASK(ldata->read_head)) {
  1172. echo_char(read_buf(ldata, tail), tty);
  1173. tail++;
  1174. }
  1175. commit_echoes(tty);
  1176. return 0;
  1177. }
  1178. if (c == '\n') {
  1179. if (L_ECHO(tty) || L_ECHONL(tty)) {
  1180. echo_char_raw('\n', ldata);
  1181. commit_echoes(tty);
  1182. }
  1183. goto handle_newline;
  1184. }
  1185. if (c == EOF_CHAR(tty)) {
  1186. c = __DISABLED_CHAR;
  1187. goto handle_newline;
  1188. }
  1189. if ((c == EOL_CHAR(tty)) ||
  1190. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  1191. /*
  1192. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  1193. */
  1194. if (L_ECHO(tty)) {
  1195. /* Record the column of first canon char. */
  1196. if (ldata->canon_head == ldata->read_head)
  1197. echo_set_canon_col(ldata);
  1198. echo_char(c, tty);
  1199. commit_echoes(tty);
  1200. }
  1201. /*
  1202. * XXX does PARMRK doubling happen for
  1203. * EOL_CHAR and EOL2_CHAR?
  1204. */
  1205. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1206. put_tty_queue(c, ldata);
  1207. handle_newline:
  1208. set_bit(ldata->read_head & (N_TTY_BUF_SIZE - 1), ldata->read_flags);
  1209. put_tty_queue(c, ldata);
  1210. smp_store_release(&ldata->canon_head, ldata->read_head);
  1211. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1212. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
  1213. return 0;
  1214. }
  1215. }
  1216. if (L_ECHO(tty)) {
  1217. finish_erasing(ldata);
  1218. if (c == '\n')
  1219. echo_char_raw('\n', ldata);
  1220. else {
  1221. /* Record the column of first canon char. */
  1222. if (ldata->canon_head == ldata->read_head)
  1223. echo_set_canon_col(ldata);
  1224. echo_char(c, tty);
  1225. }
  1226. commit_echoes(tty);
  1227. }
  1228. /* PARMRK doubling check */
  1229. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1230. put_tty_queue(c, ldata);
  1231. put_tty_queue(c, ldata);
  1232. return 0;
  1233. }
  1234. static inline void
  1235. n_tty_receive_char_inline(struct tty_struct *tty, unsigned char c)
  1236. {
  1237. struct n_tty_data *ldata = tty->disc_data;
  1238. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1239. start_tty(tty);
  1240. process_echoes(tty);
  1241. }
  1242. if (L_ECHO(tty)) {
  1243. finish_erasing(ldata);
  1244. /* Record the column of first canon char. */
  1245. if (ldata->canon_head == ldata->read_head)
  1246. echo_set_canon_col(ldata);
  1247. echo_char(c, tty);
  1248. commit_echoes(tty);
  1249. }
  1250. /* PARMRK doubling check */
  1251. if (c == (unsigned char) '\377' && I_PARMRK(tty))
  1252. put_tty_queue(c, ldata);
  1253. put_tty_queue(c, ldata);
  1254. }
  1255. static void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  1256. {
  1257. n_tty_receive_char_inline(tty, c);
  1258. }
  1259. static inline void
  1260. n_tty_receive_char_fast(struct tty_struct *tty, unsigned char c)
  1261. {
  1262. struct n_tty_data *ldata = tty->disc_data;
  1263. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) && I_IXANY(tty)) {
  1264. start_tty(tty);
  1265. process_echoes(tty);
  1266. }
  1267. if (L_ECHO(tty)) {
  1268. finish_erasing(ldata);
  1269. /* Record the column of first canon char. */
  1270. if (ldata->canon_head == ldata->read_head)
  1271. echo_set_canon_col(ldata);
  1272. echo_char(c, tty);
  1273. commit_echoes(tty);
  1274. }
  1275. put_tty_queue(c, ldata);
  1276. }
  1277. static void n_tty_receive_char_closing(struct tty_struct *tty, unsigned char c)
  1278. {
  1279. if (I_ISTRIP(tty))
  1280. c &= 0x7f;
  1281. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1282. c = tolower(c);
  1283. if (I_IXON(tty)) {
  1284. if (c == STOP_CHAR(tty))
  1285. stop_tty(tty);
  1286. else if (c == START_CHAR(tty) ||
  1287. (tty->stopped && !tty->flow_stopped && I_IXANY(tty) &&
  1288. c != INTR_CHAR(tty) && c != QUIT_CHAR(tty) &&
  1289. c != SUSP_CHAR(tty))) {
  1290. start_tty(tty);
  1291. process_echoes(tty);
  1292. }
  1293. }
  1294. }
  1295. static void
  1296. n_tty_receive_char_flagged(struct tty_struct *tty, unsigned char c, char flag)
  1297. {
  1298. switch (flag) {
  1299. case TTY_BREAK:
  1300. n_tty_receive_break(tty);
  1301. break;
  1302. case TTY_PARITY:
  1303. case TTY_FRAME:
  1304. n_tty_receive_parity_error(tty, c);
  1305. break;
  1306. case TTY_OVERRUN:
  1307. n_tty_receive_overrun(tty);
  1308. break;
  1309. default:
  1310. tty_err(tty, "unknown flag %d\n", flag);
  1311. break;
  1312. }
  1313. }
  1314. static void
  1315. n_tty_receive_char_lnext(struct tty_struct *tty, unsigned char c, char flag)
  1316. {
  1317. struct n_tty_data *ldata = tty->disc_data;
  1318. ldata->lnext = 0;
  1319. if (likely(flag == TTY_NORMAL)) {
  1320. if (I_ISTRIP(tty))
  1321. c &= 0x7f;
  1322. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1323. c = tolower(c);
  1324. n_tty_receive_char(tty, c);
  1325. } else
  1326. n_tty_receive_char_flagged(tty, c, flag);
  1327. }
  1328. static void
  1329. n_tty_receive_buf_real_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. size_t n, head;
  1334. head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
  1335. n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
  1336. memcpy(read_buf_addr(ldata, head), cp, n);
  1337. ldata->read_head += n;
  1338. cp += n;
  1339. count -= n;
  1340. head = ldata->read_head & (N_TTY_BUF_SIZE - 1);
  1341. n = min_t(size_t, count, N_TTY_BUF_SIZE - head);
  1342. memcpy(read_buf_addr(ldata, head), cp, n);
  1343. ldata->read_head += n;
  1344. }
  1345. static void
  1346. n_tty_receive_buf_raw(struct tty_struct *tty, const unsigned char *cp,
  1347. char *fp, int count)
  1348. {
  1349. struct n_tty_data *ldata = tty->disc_data;
  1350. char flag = TTY_NORMAL;
  1351. while (count--) {
  1352. if (fp)
  1353. flag = *fp++;
  1354. if (likely(flag == TTY_NORMAL))
  1355. put_tty_queue(*cp++, ldata);
  1356. else
  1357. n_tty_receive_char_flagged(tty, *cp++, flag);
  1358. }
  1359. }
  1360. static void
  1361. n_tty_receive_buf_closing(struct tty_struct *tty, const unsigned char *cp,
  1362. char *fp, int count)
  1363. {
  1364. char flag = TTY_NORMAL;
  1365. while (count--) {
  1366. if (fp)
  1367. flag = *fp++;
  1368. if (likely(flag == TTY_NORMAL))
  1369. n_tty_receive_char_closing(tty, *cp++);
  1370. }
  1371. }
  1372. static void
  1373. n_tty_receive_buf_standard(struct tty_struct *tty, const unsigned char *cp,
  1374. char *fp, int count)
  1375. {
  1376. struct n_tty_data *ldata = tty->disc_data;
  1377. char flag = TTY_NORMAL;
  1378. while (count--) {
  1379. if (fp)
  1380. flag = *fp++;
  1381. if (likely(flag == TTY_NORMAL)) {
  1382. unsigned char c = *cp++;
  1383. if (I_ISTRIP(tty))
  1384. c &= 0x7f;
  1385. if (I_IUCLC(tty) && L_IEXTEN(tty))
  1386. c = tolower(c);
  1387. if (L_EXTPROC(tty)) {
  1388. put_tty_queue(c, ldata);
  1389. continue;
  1390. }
  1391. if (!test_bit(c, ldata->char_map))
  1392. n_tty_receive_char_inline(tty, c);
  1393. else if (n_tty_receive_char_special(tty, c) && count) {
  1394. if (fp)
  1395. flag = *fp++;
  1396. n_tty_receive_char_lnext(tty, *cp++, flag);
  1397. count--;
  1398. }
  1399. } else
  1400. n_tty_receive_char_flagged(tty, *cp++, flag);
  1401. }
  1402. }
  1403. static void
  1404. n_tty_receive_buf_fast(struct tty_struct *tty, const unsigned char *cp,
  1405. char *fp, int count)
  1406. {
  1407. struct n_tty_data *ldata = tty->disc_data;
  1408. char flag = TTY_NORMAL;
  1409. while (count--) {
  1410. if (fp)
  1411. flag = *fp++;
  1412. if (likely(flag == TTY_NORMAL)) {
  1413. unsigned char c = *cp++;
  1414. if (!test_bit(c, ldata->char_map))
  1415. n_tty_receive_char_fast(tty, c);
  1416. else if (n_tty_receive_char_special(tty, c) && count) {
  1417. if (fp)
  1418. flag = *fp++;
  1419. n_tty_receive_char_lnext(tty, *cp++, flag);
  1420. count--;
  1421. }
  1422. } else
  1423. n_tty_receive_char_flagged(tty, *cp++, flag);
  1424. }
  1425. }
  1426. static void __receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1427. char *fp, int count)
  1428. {
  1429. struct n_tty_data *ldata = tty->disc_data;
  1430. bool preops = I_ISTRIP(tty) || (I_IUCLC(tty) && L_IEXTEN(tty));
  1431. if (ldata->real_raw)
  1432. n_tty_receive_buf_real_raw(tty, cp, fp, count);
  1433. else if (ldata->raw || (L_EXTPROC(tty) && !preops))
  1434. n_tty_receive_buf_raw(tty, cp, fp, count);
  1435. else if (tty->closing && !L_EXTPROC(tty))
  1436. n_tty_receive_buf_closing(tty, cp, fp, count);
  1437. else {
  1438. if (ldata->lnext) {
  1439. char flag = TTY_NORMAL;
  1440. if (fp)
  1441. flag = *fp++;
  1442. n_tty_receive_char_lnext(tty, *cp++, flag);
  1443. count--;
  1444. }
  1445. if (!preops && !I_PARMRK(tty))
  1446. n_tty_receive_buf_fast(tty, cp, fp, count);
  1447. else
  1448. n_tty_receive_buf_standard(tty, cp, fp, count);
  1449. flush_echoes(tty);
  1450. if (tty->ops->flush_chars)
  1451. tty->ops->flush_chars(tty);
  1452. }
  1453. if (ldata->icanon && !L_EXTPROC(tty))
  1454. return;
  1455. /* publish read_head to consumer */
  1456. smp_store_release(&ldata->commit_head, ldata->read_head);
  1457. if (read_cnt(ldata)) {
  1458. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  1459. wake_up_interruptible_poll(&tty->read_wait, EPOLLIN);
  1460. }
  1461. }
  1462. /**
  1463. * n_tty_receive_buf_common - process input
  1464. * @tty: device to receive input
  1465. * @cp: input chars
  1466. * @fp: flags for each char (if NULL, all chars are TTY_NORMAL)
  1467. * @count: number of input chars in @cp
  1468. *
  1469. * Called by the terminal driver when a block of characters has
  1470. * been received. This function must be called from soft contexts
  1471. * not from interrupt context. The driver is responsible for making
  1472. * calls one at a time and in order (or using flush_to_ldisc)
  1473. *
  1474. * Returns the # of input chars from @cp which were processed.
  1475. *
  1476. * In canonical mode, the maximum line length is 4096 chars (including
  1477. * the line termination char); lines longer than 4096 chars are
  1478. * truncated. After 4095 chars, input data is still processed but
  1479. * not stored. Overflow processing ensures the tty can always
  1480. * receive more input until at least one line can be read.
  1481. *
  1482. * In non-canonical mode, the read buffer will only accept 4095 chars;
  1483. * this provides the necessary space for a newline char if the input
  1484. * mode is switched to canonical.
  1485. *
  1486. * Note it is possible for the read buffer to _contain_ 4096 chars
  1487. * in non-canonical mode: the read buffer could already contain the
  1488. * maximum canon line of 4096 chars when the mode is switched to
  1489. * non-canonical.
  1490. *
  1491. * n_tty_receive_buf()/producer path:
  1492. * claims non-exclusive termios_rwsem
  1493. * publishes commit_head or canon_head
  1494. */
  1495. static int
  1496. n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
  1497. char *fp, int count, int flow)
  1498. {
  1499. struct n_tty_data *ldata = tty->disc_data;
  1500. int room, n, rcvd = 0, overflow;
  1501. down_read(&tty->termios_rwsem);
  1502. while (1) {
  1503. /*
  1504. * When PARMRK is set, each input char may take up to 3 chars
  1505. * in the read buf; reduce the buffer space avail by 3x
  1506. *
  1507. * If we are doing input canonicalization, and there are no
  1508. * pending newlines, let characters through without limit, so
  1509. * that erase characters will be handled. Other excess
  1510. * characters will be beeped.
  1511. *
  1512. * paired with store in *_copy_from_read_buf() -- guarantees
  1513. * the consumer has loaded the data in read_buf up to the new
  1514. * read_tail (so this producer will not overwrite unread data)
  1515. */
  1516. size_t tail = smp_load_acquire(&ldata->read_tail);
  1517. room = N_TTY_BUF_SIZE - (ldata->read_head - tail);
  1518. if (I_PARMRK(tty))
  1519. room = (room + 2) / 3;
  1520. room--;
  1521. if (room <= 0) {
  1522. overflow = ldata->icanon && ldata->canon_head == tail;
  1523. if (overflow && room < 0)
  1524. ldata->read_head--;
  1525. room = overflow;
  1526. ldata->no_room = flow && !room;
  1527. } else
  1528. overflow = 0;
  1529. n = min(count, room);
  1530. if (!n)
  1531. break;
  1532. /* ignore parity errors if handling overflow */
  1533. if (!overflow || !fp || *fp != TTY_PARITY)
  1534. __receive_buf(tty, cp, fp, n);
  1535. cp += n;
  1536. if (fp)
  1537. fp += n;
  1538. count -= n;
  1539. rcvd += n;
  1540. }
  1541. tty->receive_room = room;
  1542. /* Unthrottle if handling overflow on pty */
  1543. if (tty->driver->type == TTY_DRIVER_TYPE_PTY) {
  1544. if (overflow) {
  1545. tty_set_flow_change(tty, TTY_UNTHROTTLE_SAFE);
  1546. tty_unthrottle_safe(tty);
  1547. __tty_set_flow_change(tty, 0);
  1548. }
  1549. } else
  1550. n_tty_check_throttle(tty);
  1551. up_read(&tty->termios_rwsem);
  1552. return rcvd;
  1553. }
  1554. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1555. char *fp, int count)
  1556. {
  1557. n_tty_receive_buf_common(tty, cp, fp, count, 0);
  1558. }
  1559. static int n_tty_receive_buf2(struct tty_struct *tty, const unsigned char *cp,
  1560. char *fp, int count)
  1561. {
  1562. return n_tty_receive_buf_common(tty, cp, fp, count, 1);
  1563. }
  1564. /**
  1565. * n_tty_set_termios - termios data changed
  1566. * @tty: terminal
  1567. * @old: previous data
  1568. *
  1569. * Called by the tty layer when the user changes termios flags so
  1570. * that the line discipline can plan ahead. This function cannot sleep
  1571. * and is protected from re-entry by the tty layer. The user is
  1572. * guaranteed that this function will not be re-entered or in progress
  1573. * when the ldisc is closed.
  1574. *
  1575. * Locking: Caller holds tty->termios_rwsem
  1576. */
  1577. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  1578. {
  1579. struct n_tty_data *ldata = tty->disc_data;
  1580. if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
  1581. bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
  1582. ldata->line_start = ldata->read_tail;
  1583. if (!L_ICANON(tty) || !read_cnt(ldata)) {
  1584. ldata->canon_head = ldata->read_tail;
  1585. ldata->push = 0;
  1586. } else {
  1587. set_bit((ldata->read_head - 1) & (N_TTY_BUF_SIZE - 1),
  1588. ldata->read_flags);
  1589. ldata->canon_head = ldata->read_head;
  1590. ldata->push = 1;
  1591. }
  1592. ldata->commit_head = ldata->read_head;
  1593. ldata->erasing = 0;
  1594. ldata->lnext = 0;
  1595. }
  1596. ldata->icanon = (L_ICANON(tty) != 0);
  1597. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  1598. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  1599. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  1600. I_PARMRK(tty)) {
  1601. bitmap_zero(ldata->char_map, 256);
  1602. if (I_IGNCR(tty) || I_ICRNL(tty))
  1603. set_bit('\r', ldata->char_map);
  1604. if (I_INLCR(tty))
  1605. set_bit('\n', ldata->char_map);
  1606. if (L_ICANON(tty)) {
  1607. set_bit(ERASE_CHAR(tty), ldata->char_map);
  1608. set_bit(KILL_CHAR(tty), ldata->char_map);
  1609. set_bit(EOF_CHAR(tty), ldata->char_map);
  1610. set_bit('\n', ldata->char_map);
  1611. set_bit(EOL_CHAR(tty), ldata->char_map);
  1612. if (L_IEXTEN(tty)) {
  1613. set_bit(WERASE_CHAR(tty), ldata->char_map);
  1614. set_bit(LNEXT_CHAR(tty), ldata->char_map);
  1615. set_bit(EOL2_CHAR(tty), ldata->char_map);
  1616. if (L_ECHO(tty))
  1617. set_bit(REPRINT_CHAR(tty),
  1618. ldata->char_map);
  1619. }
  1620. }
  1621. if (I_IXON(tty)) {
  1622. set_bit(START_CHAR(tty), ldata->char_map);
  1623. set_bit(STOP_CHAR(tty), ldata->char_map);
  1624. }
  1625. if (L_ISIG(tty)) {
  1626. set_bit(INTR_CHAR(tty), ldata->char_map);
  1627. set_bit(QUIT_CHAR(tty), ldata->char_map);
  1628. set_bit(SUSP_CHAR(tty), ldata->char_map);
  1629. }
  1630. clear_bit(__DISABLED_CHAR, ldata->char_map);
  1631. ldata->raw = 0;
  1632. ldata->real_raw = 0;
  1633. } else {
  1634. ldata->raw = 1;
  1635. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  1636. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  1637. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  1638. ldata->real_raw = 1;
  1639. else
  1640. ldata->real_raw = 0;
  1641. }
  1642. /*
  1643. * Fix tty hang when I_IXON(tty) is cleared, but the tty
  1644. * been stopped by STOP_CHAR(tty) before it.
  1645. */
  1646. if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow_stopped) {
  1647. start_tty(tty);
  1648. process_echoes(tty);
  1649. }
  1650. /* The termios change make the tty ready for I/O */
  1651. wake_up_interruptible(&tty->write_wait);
  1652. wake_up_interruptible(&tty->read_wait);
  1653. }
  1654. /**
  1655. * n_tty_close - close the ldisc for this tty
  1656. * @tty: device
  1657. *
  1658. * Called from the terminal layer when this line discipline is
  1659. * being shut down, either because of a close or becsuse of a
  1660. * discipline change. The function will not be called while other
  1661. * ldisc methods are in progress.
  1662. */
  1663. static void n_tty_close(struct tty_struct *tty)
  1664. {
  1665. struct n_tty_data *ldata = tty->disc_data;
  1666. if (tty->link)
  1667. n_tty_packet_mode_flush(tty);
  1668. vfree(ldata);
  1669. tty->disc_data = NULL;
  1670. }
  1671. /**
  1672. * n_tty_open - open an ldisc
  1673. * @tty: terminal to open
  1674. *
  1675. * Called when this line discipline is being attached to the
  1676. * terminal device. Can sleep. Called serialized so that no
  1677. * other events will occur in parallel. No further open will occur
  1678. * until a close.
  1679. */
  1680. static int n_tty_open(struct tty_struct *tty)
  1681. {
  1682. struct n_tty_data *ldata;
  1683. /* Currently a malloc failure here can panic */
  1684. ldata = vzalloc(sizeof(*ldata));
  1685. if (!ldata)
  1686. return -ENOMEM;
  1687. ldata->overrun_time = jiffies;
  1688. mutex_init(&ldata->atomic_read_lock);
  1689. mutex_init(&ldata->output_lock);
  1690. tty->disc_data = ldata;
  1691. tty->closing = 0;
  1692. /* indicate buffer work may resume */
  1693. clear_bit(TTY_LDISC_HALTED, &tty->flags);
  1694. n_tty_set_termios(tty, NULL);
  1695. tty_unthrottle(tty);
  1696. return 0;
  1697. }
  1698. static inline int input_available_p(struct tty_struct *tty, int poll)
  1699. {
  1700. struct n_tty_data *ldata = tty->disc_data;
  1701. int amt = poll && !TIME_CHAR(tty) && MIN_CHAR(tty) ? MIN_CHAR(tty) : 1;
  1702. if (ldata->icanon && !L_EXTPROC(tty))
  1703. return ldata->canon_head != ldata->read_tail;
  1704. else
  1705. return ldata->commit_head - ldata->read_tail >= amt;
  1706. }
  1707. /**
  1708. * copy_from_read_buf - copy read data directly
  1709. * @tty: terminal device
  1710. * @b: user data
  1711. * @nr: size of data
  1712. *
  1713. * Helper function to speed up n_tty_read. It is only called when
  1714. * ICANON is off; it copies characters straight from the tty queue to
  1715. * user space directly. It can be profitably called twice; once to
  1716. * drain the space from the tail pointer to the (physical) end of the
  1717. * buffer, and once to drain the space from the (physical) beginning of
  1718. * the buffer to head pointer.
  1719. *
  1720. * Called under the ldata->atomic_read_lock sem
  1721. *
  1722. * n_tty_read()/consumer path:
  1723. * caller holds non-exclusive termios_rwsem
  1724. * read_tail published
  1725. */
  1726. static int copy_from_read_buf(struct tty_struct *tty,
  1727. unsigned char __user **b,
  1728. size_t *nr)
  1729. {
  1730. struct n_tty_data *ldata = tty->disc_data;
  1731. int retval;
  1732. size_t n;
  1733. bool is_eof;
  1734. size_t head = smp_load_acquire(&ldata->commit_head);
  1735. size_t tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
  1736. retval = 0;
  1737. n = min(head - ldata->read_tail, N_TTY_BUF_SIZE - tail);
  1738. n = min(*nr, n);
  1739. if (n) {
  1740. const unsigned char *from = read_buf_addr(ldata, tail);
  1741. retval = copy_to_user(*b, from, n);
  1742. n -= retval;
  1743. is_eof = n == 1 && *from == EOF_CHAR(tty);
  1744. tty_audit_add_data(tty, from, n);
  1745. smp_store_release(&ldata->read_tail, ldata->read_tail + n);
  1746. /* Turn single EOF into zero-length read */
  1747. if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
  1748. (head == ldata->read_tail))
  1749. n = 0;
  1750. *b += n;
  1751. *nr -= n;
  1752. }
  1753. return retval;
  1754. }
  1755. /**
  1756. * canon_copy_from_read_buf - copy read data in canonical mode
  1757. * @tty: terminal device
  1758. * @b: user data
  1759. * @nr: size of data
  1760. *
  1761. * Helper function for n_tty_read. It is only called when ICANON is on;
  1762. * it copies one line of input up to and including the line-delimiting
  1763. * character into the user-space buffer.
  1764. *
  1765. * NB: When termios is changed from non-canonical to canonical mode and
  1766. * the read buffer contains data, n_tty_set_termios() simulates an EOF
  1767. * push (as if C-d were input) _without_ the DISABLED_CHAR in the buffer.
  1768. * This causes data already processed as input to be immediately available
  1769. * as input although a newline has not been received.
  1770. *
  1771. * Called under the atomic_read_lock mutex
  1772. *
  1773. * n_tty_read()/consumer path:
  1774. * caller holds non-exclusive termios_rwsem
  1775. * read_tail published
  1776. */
  1777. static int canon_copy_from_read_buf(struct tty_struct *tty,
  1778. unsigned char __user **b,
  1779. size_t *nr)
  1780. {
  1781. struct n_tty_data *ldata = tty->disc_data;
  1782. size_t n, size, more, c;
  1783. size_t eol;
  1784. size_t tail;
  1785. int ret, found = 0;
  1786. /* N.B. avoid overrun if nr == 0 */
  1787. if (!*nr)
  1788. return 0;
  1789. n = min(*nr + 1, smp_load_acquire(&ldata->canon_head) - ldata->read_tail);
  1790. tail = ldata->read_tail & (N_TTY_BUF_SIZE - 1);
  1791. size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
  1792. n_tty_trace("%s: nr:%zu tail:%zu n:%zu size:%zu\n",
  1793. __func__, *nr, tail, n, size);
  1794. eol = find_next_bit(ldata->read_flags, size, tail);
  1795. more = n - (size - tail);
  1796. if (eol == N_TTY_BUF_SIZE && more) {
  1797. /* scan wrapped without finding set bit */
  1798. eol = find_next_bit(ldata->read_flags, more, 0);
  1799. found = eol != more;
  1800. } else
  1801. found = eol != size;
  1802. n = eol - tail;
  1803. if (n > N_TTY_BUF_SIZE)
  1804. n += N_TTY_BUF_SIZE;
  1805. c = n + found;
  1806. if (!found || read_buf(ldata, eol) != __DISABLED_CHAR) {
  1807. c = min(*nr, c);
  1808. n = c;
  1809. }
  1810. n_tty_trace("%s: eol:%zu found:%d n:%zu c:%zu tail:%zu more:%zu\n",
  1811. __func__, eol, found, n, c, tail, more);
  1812. ret = tty_copy_to_user(tty, *b, tail, n);
  1813. if (ret)
  1814. return -EFAULT;
  1815. *b += n;
  1816. *nr -= n;
  1817. if (found)
  1818. clear_bit(eol, ldata->read_flags);
  1819. smp_store_release(&ldata->read_tail, ldata->read_tail + c);
  1820. if (found) {
  1821. if (!ldata->push)
  1822. ldata->line_start = ldata->read_tail;
  1823. else
  1824. ldata->push = 0;
  1825. tty_audit_push();
  1826. }
  1827. return 0;
  1828. }
  1829. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1830. size_t, loff_t *);
  1831. /**
  1832. * job_control - check job control
  1833. * @tty: tty
  1834. * @file: file handle
  1835. *
  1836. * Perform job control management checks on this file/tty descriptor
  1837. * and if appropriate send any needed signals and return a negative
  1838. * error code if action should be taken.
  1839. *
  1840. * Locking: redirected write test is safe
  1841. * current->signal->tty check is safe
  1842. * ctrl_lock to safely reference tty->pgrp
  1843. */
  1844. static int job_control(struct tty_struct *tty, struct file *file)
  1845. {
  1846. /* Job control check -- must be done at start and after
  1847. every sleep (POSIX.1 7.1.1.4). */
  1848. /* NOTE: not yet done after every sleep pending a thorough
  1849. check of the logic of this change. -- jlc */
  1850. /* don't stop on /dev/console */
  1851. if (file->f_op->write == redirected_tty_write)
  1852. return 0;
  1853. return __tty_check_change(tty, SIGTTIN);
  1854. }
  1855. /**
  1856. * n_tty_read - read function for tty
  1857. * @tty: tty device
  1858. * @file: file object
  1859. * @buf: userspace buffer pointer
  1860. * @nr: size of I/O
  1861. *
  1862. * Perform reads for the line discipline. We are guaranteed that the
  1863. * line discipline will not be closed under us but we may get multiple
  1864. * parallel readers and must handle this ourselves. We may also get
  1865. * a hangup. Always called in user context, may sleep.
  1866. *
  1867. * This code must be sure never to sleep through a hangup.
  1868. *
  1869. * n_tty_read()/consumer path:
  1870. * claims non-exclusive termios_rwsem
  1871. * publishes read_tail
  1872. */
  1873. static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
  1874. unsigned char __user *buf, size_t nr)
  1875. {
  1876. struct n_tty_data *ldata = tty->disc_data;
  1877. unsigned char __user *b = buf;
  1878. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1879. int c;
  1880. int minimum, time;
  1881. ssize_t retval = 0;
  1882. long timeout;
  1883. int packet;
  1884. size_t tail;
  1885. c = job_control(tty, file);
  1886. if (c < 0)
  1887. return c;
  1888. /*
  1889. * Internal serialization of reads.
  1890. */
  1891. if (file->f_flags & O_NONBLOCK) {
  1892. if (!mutex_trylock(&ldata->atomic_read_lock))
  1893. return -EAGAIN;
  1894. } else {
  1895. if (mutex_lock_interruptible(&ldata->atomic_read_lock))
  1896. return -ERESTARTSYS;
  1897. }
  1898. down_read(&tty->termios_rwsem);
  1899. minimum = time = 0;
  1900. timeout = MAX_SCHEDULE_TIMEOUT;
  1901. if (!ldata->icanon) {
  1902. minimum = MIN_CHAR(tty);
  1903. if (minimum) {
  1904. time = (HZ / 10) * TIME_CHAR(tty);
  1905. } else {
  1906. timeout = (HZ / 10) * TIME_CHAR(tty);
  1907. minimum = 1;
  1908. }
  1909. }
  1910. packet = tty->packet;
  1911. tail = ldata->read_tail;
  1912. add_wait_queue(&tty->read_wait, &wait);
  1913. while (nr) {
  1914. /* First test for status change. */
  1915. if (packet && tty->link->ctrl_status) {
  1916. unsigned char cs;
  1917. if (b != buf)
  1918. break;
  1919. spin_lock_irq(&tty->link->ctrl_lock);
  1920. cs = tty->link->ctrl_status;
  1921. tty->link->ctrl_status = 0;
  1922. spin_unlock_irq(&tty->link->ctrl_lock);
  1923. if (put_user(cs, b)) {
  1924. retval = -EFAULT;
  1925. break;
  1926. }
  1927. b++;
  1928. nr--;
  1929. break;
  1930. }
  1931. if (!input_available_p(tty, 0)) {
  1932. up_read(&tty->termios_rwsem);
  1933. tty_buffer_flush_work(tty->port);
  1934. down_read(&tty->termios_rwsem);
  1935. if (!input_available_p(tty, 0)) {
  1936. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1937. retval = -EIO;
  1938. break;
  1939. }
  1940. if (tty_hung_up_p(file))
  1941. break;
  1942. /*
  1943. * Abort readers for ttys which never actually
  1944. * get hung up. See __tty_hangup().
  1945. */
  1946. if (test_bit(TTY_HUPPING, &tty->flags))
  1947. break;
  1948. if (!timeout)
  1949. break;
  1950. if (file->f_flags & O_NONBLOCK) {
  1951. retval = -EAGAIN;
  1952. break;
  1953. }
  1954. if (signal_pending(current)) {
  1955. retval = -ERESTARTSYS;
  1956. break;
  1957. }
  1958. up_read(&tty->termios_rwsem);
  1959. timeout = wait_woken(&wait, TASK_INTERRUPTIBLE,
  1960. timeout);
  1961. down_read(&tty->termios_rwsem);
  1962. continue;
  1963. }
  1964. }
  1965. if (ldata->icanon && !L_EXTPROC(tty)) {
  1966. retval = canon_copy_from_read_buf(tty, &b, &nr);
  1967. if (retval)
  1968. break;
  1969. } else {
  1970. int uncopied;
  1971. /* Deal with packet mode. */
  1972. if (packet && b == buf) {
  1973. if (put_user(TIOCPKT_DATA, b)) {
  1974. retval = -EFAULT;
  1975. break;
  1976. }
  1977. b++;
  1978. nr--;
  1979. }
  1980. uncopied = copy_from_read_buf(tty, &b, &nr);
  1981. uncopied += copy_from_read_buf(tty, &b, &nr);
  1982. if (uncopied) {
  1983. retval = -EFAULT;
  1984. break;
  1985. }
  1986. }
  1987. n_tty_check_unthrottle(tty);
  1988. if (b - buf >= minimum)
  1989. break;
  1990. if (time)
  1991. timeout = time;
  1992. }
  1993. if (tail != ldata->read_tail)
  1994. n_tty_kick_worker(tty);
  1995. up_read(&tty->termios_rwsem);
  1996. remove_wait_queue(&tty->read_wait, &wait);
  1997. mutex_unlock(&ldata->atomic_read_lock);
  1998. if (b - buf)
  1999. retval = b - buf;
  2000. return retval;
  2001. }
  2002. /**
  2003. * n_tty_write - write function for tty
  2004. * @tty: tty device
  2005. * @file: file object
  2006. * @buf: userspace buffer pointer
  2007. * @nr: size of I/O
  2008. *
  2009. * Write function of the terminal device. This is serialized with
  2010. * respect to other write callers but not to termios changes, reads
  2011. * and other such events. Since the receive code will echo characters,
  2012. * thus calling driver write methods, the output_lock is used in
  2013. * the output processing functions called here as well as in the
  2014. * echo processing function to protect the column state and space
  2015. * left in the buffer.
  2016. *
  2017. * This code must be sure never to sleep through a hangup.
  2018. *
  2019. * Locking: output_lock to protect column state and space left
  2020. * (note that the process_output*() functions take this
  2021. * lock themselves)
  2022. */
  2023. static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
  2024. const unsigned char *buf, size_t nr)
  2025. {
  2026. const unsigned char *b = buf;
  2027. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  2028. int c;
  2029. ssize_t retval = 0;
  2030. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  2031. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  2032. retval = tty_check_change(tty);
  2033. if (retval)
  2034. return retval;
  2035. }
  2036. down_read(&tty->termios_rwsem);
  2037. /* Write out any echoed characters that are still pending */
  2038. process_echoes(tty);
  2039. add_wait_queue(&tty->write_wait, &wait);
  2040. while (1) {
  2041. if (signal_pending(current)) {
  2042. retval = -ERESTARTSYS;
  2043. break;
  2044. }
  2045. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  2046. retval = -EIO;
  2047. break;
  2048. }
  2049. if (O_OPOST(tty)) {
  2050. while (nr > 0) {
  2051. ssize_t num = process_output_block(tty, b, nr);
  2052. if (num < 0) {
  2053. if (num == -EAGAIN)
  2054. break;
  2055. retval = num;
  2056. goto break_out;
  2057. }
  2058. b += num;
  2059. nr -= num;
  2060. if (nr == 0)
  2061. break;
  2062. c = *b;
  2063. if (process_output(c, tty) < 0)
  2064. break;
  2065. b++; nr--;
  2066. }
  2067. if (tty->ops->flush_chars)
  2068. tty->ops->flush_chars(tty);
  2069. } else {
  2070. struct n_tty_data *ldata = tty->disc_data;
  2071. while (nr > 0) {
  2072. mutex_lock(&ldata->output_lock);
  2073. c = tty->ops->write(tty, b, nr);
  2074. mutex_unlock(&ldata->output_lock);
  2075. if (c < 0) {
  2076. retval = c;
  2077. goto break_out;
  2078. }
  2079. if (!c)
  2080. break;
  2081. b += c;
  2082. nr -= c;
  2083. }
  2084. }
  2085. if (!nr)
  2086. break;
  2087. if (file->f_flags & O_NONBLOCK) {
  2088. retval = -EAGAIN;
  2089. break;
  2090. }
  2091. up_read(&tty->termios_rwsem);
  2092. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2093. down_read(&tty->termios_rwsem);
  2094. }
  2095. break_out:
  2096. remove_wait_queue(&tty->write_wait, &wait);
  2097. if (nr && tty->fasync)
  2098. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2099. up_read(&tty->termios_rwsem);
  2100. return (b - buf) ? b - buf : retval;
  2101. }
  2102. /**
  2103. * n_tty_poll - poll method for N_TTY
  2104. * @tty: terminal device
  2105. * @file: file accessing it
  2106. * @wait: poll table
  2107. *
  2108. * Called when the line discipline is asked to poll() for data or
  2109. * for special events. This code is not serialized with respect to
  2110. * other events save open/close.
  2111. *
  2112. * This code must be sure never to sleep through a hangup.
  2113. * Called without the kernel lock held - fine
  2114. */
  2115. static __poll_t n_tty_poll(struct tty_struct *tty, struct file *file,
  2116. poll_table *wait)
  2117. {
  2118. __poll_t mask = 0;
  2119. poll_wait(file, &tty->read_wait, wait);
  2120. poll_wait(file, &tty->write_wait, wait);
  2121. if (input_available_p(tty, 1))
  2122. mask |= EPOLLIN | EPOLLRDNORM;
  2123. else {
  2124. tty_buffer_flush_work(tty->port);
  2125. if (input_available_p(tty, 1))
  2126. mask |= EPOLLIN | EPOLLRDNORM;
  2127. }
  2128. if (tty->packet && tty->link->ctrl_status)
  2129. mask |= EPOLLPRI | EPOLLIN | EPOLLRDNORM;
  2130. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  2131. mask |= EPOLLHUP;
  2132. if (tty_hung_up_p(file))
  2133. mask |= EPOLLHUP;
  2134. if (tty->ops->write && !tty_is_writelocked(tty) &&
  2135. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  2136. tty_write_room(tty) > 0)
  2137. mask |= EPOLLOUT | EPOLLWRNORM;
  2138. return mask;
  2139. }
  2140. static unsigned long inq_canon(struct n_tty_data *ldata)
  2141. {
  2142. size_t nr, head, tail;
  2143. if (ldata->canon_head == ldata->read_tail)
  2144. return 0;
  2145. head = ldata->canon_head;
  2146. tail = ldata->read_tail;
  2147. nr = head - tail;
  2148. /* Skip EOF-chars.. */
  2149. while (MASK(head) != MASK(tail)) {
  2150. if (test_bit(tail & (N_TTY_BUF_SIZE - 1), ldata->read_flags) &&
  2151. read_buf(ldata, tail) == __DISABLED_CHAR)
  2152. nr--;
  2153. tail++;
  2154. }
  2155. return nr;
  2156. }
  2157. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  2158. unsigned int cmd, unsigned long arg)
  2159. {
  2160. struct n_tty_data *ldata = tty->disc_data;
  2161. int retval;
  2162. switch (cmd) {
  2163. case TIOCOUTQ:
  2164. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  2165. case TIOCINQ:
  2166. down_write(&tty->termios_rwsem);
  2167. if (L_ICANON(tty) && !L_EXTPROC(tty))
  2168. retval = inq_canon(ldata);
  2169. else
  2170. retval = read_cnt(ldata);
  2171. up_write(&tty->termios_rwsem);
  2172. return put_user(retval, (unsigned int __user *) arg);
  2173. default:
  2174. return n_tty_ioctl_helper(tty, file, cmd, arg);
  2175. }
  2176. }
  2177. static struct tty_ldisc_ops n_tty_ops = {
  2178. .magic = TTY_LDISC_MAGIC,
  2179. .name = "n_tty",
  2180. .open = n_tty_open,
  2181. .close = n_tty_close,
  2182. .flush_buffer = n_tty_flush_buffer,
  2183. .read = n_tty_read,
  2184. .write = n_tty_write,
  2185. .ioctl = n_tty_ioctl,
  2186. .set_termios = n_tty_set_termios,
  2187. .poll = n_tty_poll,
  2188. .receive_buf = n_tty_receive_buf,
  2189. .write_wakeup = n_tty_write_wakeup,
  2190. .receive_buf2 = n_tty_receive_buf2,
  2191. };
  2192. /**
  2193. * n_tty_inherit_ops - inherit N_TTY methods
  2194. * @ops: struct tty_ldisc_ops where to save N_TTY methods
  2195. *
  2196. * Enables a 'subclass' line discipline to 'inherit' N_TTY methods.
  2197. */
  2198. void n_tty_inherit_ops(struct tty_ldisc_ops *ops)
  2199. {
  2200. *ops = n_tty_ops;
  2201. ops->owner = NULL;
  2202. ops->refcount = ops->flags = 0;
  2203. }
  2204. EXPORT_SYMBOL_GPL(n_tty_inherit_ops);
  2205. void __init n_tty_init(void)
  2206. {
  2207. tty_register_ldisc(N_TTY, &n_tty_ops);
  2208. }