n_tty.c 58 KB

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