n_tty.c 57 KB

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