n_tty.c 58 KB

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