n_tty.c 61 KB

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