n_tty.c 57 KB

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