n_tty.c 60 KB

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