n_tty.c 63 KB

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