n_tty.c 61 KB

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