n_tty.c 61 KB

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