n_tty.c 57 KB

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