n_tty.c 62 KB

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