n_tty.c 62 KB

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