n_tty.c 62 KB

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