n_tty.c 57 KB

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