n_tty.c 58 KB

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