n_tty.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640
  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 write_chan 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 <asm/uaccess.h>
  50. #include <asm/system.h>
  51. /* number of characters left in xmit buffer before select has we have room */
  52. #define WAKEUP_CHARS 256
  53. /*
  54. * This defines the low- and high-watermarks for throttling and
  55. * unthrottling the TTY driver. These watermarks are used for
  56. * controlling the space in the read buffer.
  57. */
  58. #define TTY_THRESHOLD_THROTTLE 128 /* now based on remaining room */
  59. #define TTY_THRESHOLD_UNTHROTTLE 128
  60. static inline unsigned char *alloc_buf(void)
  61. {
  62. gfp_t prio = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
  63. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  64. return kmalloc(N_TTY_BUF_SIZE, prio);
  65. else
  66. return (unsigned char *)__get_free_page(prio);
  67. }
  68. static inline void free_buf(unsigned char *buf)
  69. {
  70. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  71. kfree(buf);
  72. else
  73. free_page((unsigned long) buf);
  74. }
  75. static inline int tty_put_user(struct tty_struct *tty, unsigned char x,
  76. unsigned char __user *ptr)
  77. {
  78. tty_audit_add_data(tty, &x, 1);
  79. return put_user(x, ptr);
  80. }
  81. /**
  82. * n_tty_set__room - receive space
  83. * @tty: terminal
  84. *
  85. * Called by the driver to find out how much data it is
  86. * permitted to feed to the line discipline without any being lost
  87. * and thus to manage flow control. Not serialized. Answers for the
  88. * "instant".
  89. */
  90. static void n_tty_set_room(struct tty_struct *tty)
  91. {
  92. int left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
  93. /*
  94. * If we are doing input canonicalization, and there are no
  95. * pending newlines, let characters through without limit, so
  96. * that erase characters will be handled. Other excess
  97. * characters will be beeped.
  98. */
  99. if (left <= 0)
  100. left = tty->icanon && !tty->canon_data;
  101. tty->receive_room = left;
  102. }
  103. static void put_tty_queue_nolock(unsigned char c, struct tty_struct *tty)
  104. {
  105. if (tty->read_cnt < N_TTY_BUF_SIZE) {
  106. tty->read_buf[tty->read_head] = c;
  107. tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  108. tty->read_cnt++;
  109. }
  110. }
  111. static void put_tty_queue(unsigned char c, struct tty_struct *tty)
  112. {
  113. unsigned long flags;
  114. /*
  115. * The problem of stomping on the buffers ends here.
  116. * Why didn't anyone see this one coming? --AJK
  117. */
  118. spin_lock_irqsave(&tty->read_lock, flags);
  119. put_tty_queue_nolock(c, tty);
  120. spin_unlock_irqrestore(&tty->read_lock, flags);
  121. }
  122. /**
  123. * check_unthrottle - allow new receive data
  124. * @tty; tty device
  125. *
  126. * Check whether to call the driver.unthrottle function.
  127. * We test the TTY_THROTTLED bit first so that it always
  128. * indicates the current state. The decision about whether
  129. * it is worth allowing more input has been taken by the caller.
  130. * Can sleep, may be called under the atomic_read_lock mutex but
  131. * this is not guaranteed.
  132. */
  133. static void check_unthrottle(struct tty_struct *tty)
  134. {
  135. if (tty->count)
  136. tty_unthrottle(tty);
  137. }
  138. /**
  139. * reset_buffer_flags - reset buffer state
  140. * @tty: terminal to reset
  141. *
  142. * Reset the read buffer counters, clear the flags,
  143. * and make sure the driver is unthrottled. Called
  144. * from n_tty_open() and n_tty_flush_buffer().
  145. */
  146. static void reset_buffer_flags(struct tty_struct *tty)
  147. {
  148. unsigned long flags;
  149. spin_lock_irqsave(&tty->read_lock, flags);
  150. tty->read_head = tty->read_tail = tty->read_cnt = 0;
  151. spin_unlock_irqrestore(&tty->read_lock, flags);
  152. tty->canon_head = tty->canon_data = tty->erasing = 0;
  153. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  154. n_tty_set_room(tty);
  155. check_unthrottle(tty);
  156. }
  157. /**
  158. * n_tty_flush_buffer - clean input queue
  159. * @tty: terminal device
  160. *
  161. * Flush the input buffer. Called when the line discipline is
  162. * being closed, when the tty layer wants the buffer flushed (eg
  163. * at hangup) or when the N_TTY line discipline internally has to
  164. * clean the pending queue (for example some signals).
  165. *
  166. * Locking: ctrl_lock
  167. */
  168. static void n_tty_flush_buffer(struct tty_struct *tty)
  169. {
  170. unsigned long flags;
  171. /* clear everything and unthrottle the driver */
  172. reset_buffer_flags(tty);
  173. if (!tty->link)
  174. return;
  175. spin_lock_irqsave(&tty->ctrl_lock, flags);
  176. if (tty->link->packet) {
  177. tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  178. wake_up_interruptible(&tty->link->read_wait);
  179. }
  180. spin_unlock_irqrestore(&tty->ctrl_lock, flags);
  181. }
  182. /**
  183. * n_tty_chars_in_buffer - report available bytes
  184. * @tty: tty device
  185. *
  186. * Report the number of characters buffered to be delivered to user
  187. * at this instant in time.
  188. */
  189. static ssize_t n_tty_chars_in_buffer(struct tty_struct *tty)
  190. {
  191. unsigned long flags;
  192. ssize_t n = 0;
  193. spin_lock_irqsave(&tty->read_lock, flags);
  194. if (!tty->icanon) {
  195. n = tty->read_cnt;
  196. } else if (tty->canon_data) {
  197. n = (tty->canon_head > tty->read_tail) ?
  198. tty->canon_head - tty->read_tail :
  199. tty->canon_head + (N_TTY_BUF_SIZE - tty->read_tail);
  200. }
  201. spin_unlock_irqrestore(&tty->read_lock, flags);
  202. return n;
  203. }
  204. /**
  205. * is_utf8_continuation - utf8 multibyte check
  206. * @c: byte to check
  207. *
  208. * Returns true if the utf8 character 'c' is a multibyte continuation
  209. * character. We use this to correctly compute the on screen size
  210. * of the character when printing
  211. */
  212. static inline int is_utf8_continuation(unsigned char c)
  213. {
  214. return (c & 0xc0) == 0x80;
  215. }
  216. /**
  217. * is_continuation - multibyte check
  218. * @c: byte to check
  219. *
  220. * Returns true if the utf8 character 'c' is a multibyte continuation
  221. * character and the terminal is in unicode mode.
  222. */
  223. static inline int is_continuation(unsigned char c, struct tty_struct *tty)
  224. {
  225. return I_IUTF8(tty) && is_utf8_continuation(c);
  226. }
  227. /**
  228. * opost - output post processor
  229. * @c: character (or partial unicode symbol)
  230. * @tty: terminal device
  231. *
  232. * Perform OPOST processing. Returns -1 when the output device is
  233. * full and the character must be retried. Note that Linux currently
  234. * ignores TABDLY, CRDLY, VTDLY, FFDLY and NLDLY. They simply aren't
  235. * relevant in the world today. If you ever need them, add them here.
  236. *
  237. * Called from both the receive and transmit sides and can be called
  238. * re-entrantly. Relies on lock_kernel() for tty->column state.
  239. */
  240. static int opost(unsigned char c, struct tty_struct *tty)
  241. {
  242. int space, spaces;
  243. space = tty_write_room(tty);
  244. if (!space)
  245. return -1;
  246. lock_kernel();
  247. if (O_OPOST(tty)) {
  248. switch (c) {
  249. case '\n':
  250. if (O_ONLRET(tty))
  251. tty->column = 0;
  252. if (O_ONLCR(tty)) {
  253. if (space < 2) {
  254. unlock_kernel();
  255. return -1;
  256. }
  257. tty_put_char(tty, '\r');
  258. tty->column = 0;
  259. }
  260. tty->canon_column = tty->column;
  261. break;
  262. case '\r':
  263. if (O_ONOCR(tty) && tty->column == 0) {
  264. unlock_kernel();
  265. return 0;
  266. }
  267. if (O_OCRNL(tty)) {
  268. c = '\n';
  269. if (O_ONLRET(tty))
  270. tty->canon_column = tty->column = 0;
  271. break;
  272. }
  273. tty->canon_column = tty->column = 0;
  274. break;
  275. case '\t':
  276. spaces = 8 - (tty->column & 7);
  277. if (O_TABDLY(tty) == XTABS) {
  278. if (space < spaces) {
  279. unlock_kernel();
  280. return -1;
  281. }
  282. tty->column += spaces;
  283. tty->ops->write(tty, " ", spaces);
  284. unlock_kernel();
  285. return 0;
  286. }
  287. tty->column += spaces;
  288. break;
  289. case '\b':
  290. if (tty->column > 0)
  291. tty->column--;
  292. break;
  293. default:
  294. if (O_OLCUC(tty))
  295. c = toupper(c);
  296. if (!iscntrl(c) && !is_continuation(c, tty))
  297. tty->column++;
  298. break;
  299. }
  300. }
  301. tty_put_char(tty, c);
  302. unlock_kernel();
  303. return 0;
  304. }
  305. /**
  306. * opost_block - block postprocess
  307. * @tty: terminal device
  308. * @inbuf: user buffer
  309. * @nr: number of bytes
  310. *
  311. * This path is used to speed up block console writes, among other
  312. * things when processing blocks of output data. It handles only
  313. * the simple cases normally found and helps to generate blocks of
  314. * symbols for the console driver and thus improve performance.
  315. *
  316. * Called from write_chan under the tty layer write lock. Relies
  317. * on lock_kernel for the tty->column state.
  318. */
  319. static ssize_t opost_block(struct tty_struct *tty,
  320. const unsigned char *buf, unsigned int nr)
  321. {
  322. int space;
  323. int i;
  324. const unsigned char *cp;
  325. space = tty_write_room(tty);
  326. if (!space)
  327. return 0;
  328. if (nr > space)
  329. nr = space;
  330. lock_kernel();
  331. for (i = 0, cp = buf; i < nr; i++, cp++) {
  332. switch (*cp) {
  333. case '\n':
  334. if (O_ONLRET(tty))
  335. tty->column = 0;
  336. if (O_ONLCR(tty))
  337. goto break_out;
  338. tty->canon_column = tty->column;
  339. break;
  340. case '\r':
  341. if (O_ONOCR(tty) && tty->column == 0)
  342. goto break_out;
  343. if (O_OCRNL(tty))
  344. goto break_out;
  345. tty->canon_column = tty->column = 0;
  346. break;
  347. case '\t':
  348. goto break_out;
  349. case '\b':
  350. if (tty->column > 0)
  351. tty->column--;
  352. break;
  353. default:
  354. if (O_OLCUC(tty))
  355. goto break_out;
  356. if (!iscntrl(*cp))
  357. tty->column++;
  358. break;
  359. }
  360. }
  361. break_out:
  362. if (tty->ops->flush_chars)
  363. tty->ops->flush_chars(tty);
  364. i = tty->ops->write(tty, buf, i);
  365. unlock_kernel();
  366. return i;
  367. }
  368. /**
  369. * echo_char - echo characters
  370. * @c: unicode byte to echo
  371. * @tty: terminal device
  372. *
  373. * Echo user input back onto the screen. This must be called only when
  374. * L_ECHO(tty) is true. Called from the driver receive_buf path.
  375. */
  376. static void echo_char(unsigned char c, struct tty_struct *tty)
  377. {
  378. if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
  379. tty_put_char(tty, '^');
  380. tty_put_char(tty, c ^ 0100);
  381. tty->column += 2;
  382. } else
  383. opost(c, tty);
  384. }
  385. static inline void finish_erasing(struct tty_struct *tty)
  386. {
  387. if (tty->erasing) {
  388. tty_put_char(tty, '/');
  389. tty->column++;
  390. tty->erasing = 0;
  391. }
  392. }
  393. /**
  394. * eraser - handle erase function
  395. * @c: character input
  396. * @tty: terminal device
  397. *
  398. * Perform erase and necessary output when an erase character is
  399. * present in the stream from the driver layer. Handles the complexities
  400. * of UTF-8 multibyte symbols.
  401. */
  402. static void eraser(unsigned char c, struct tty_struct *tty)
  403. {
  404. enum { ERASE, WERASE, KILL } kill_type;
  405. int head, seen_alnums, cnt;
  406. unsigned long flags;
  407. if (tty->read_head == tty->canon_head) {
  408. /* opost('\a', tty); */ /* what do you think? */
  409. return;
  410. }
  411. if (c == ERASE_CHAR(tty))
  412. kill_type = ERASE;
  413. else if (c == WERASE_CHAR(tty))
  414. kill_type = WERASE;
  415. else {
  416. if (!L_ECHO(tty)) {
  417. spin_lock_irqsave(&tty->read_lock, flags);
  418. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  419. (N_TTY_BUF_SIZE - 1));
  420. tty->read_head = tty->canon_head;
  421. spin_unlock_irqrestore(&tty->read_lock, flags);
  422. return;
  423. }
  424. if (!L_ECHOK(tty) || !L_ECHOKE(tty) || !L_ECHOE(tty)) {
  425. spin_lock_irqsave(&tty->read_lock, flags);
  426. tty->read_cnt -= ((tty->read_head - tty->canon_head) &
  427. (N_TTY_BUF_SIZE - 1));
  428. tty->read_head = tty->canon_head;
  429. spin_unlock_irqrestore(&tty->read_lock, flags);
  430. finish_erasing(tty);
  431. echo_char(KILL_CHAR(tty), tty);
  432. /* Add a newline if ECHOK is on and ECHOKE is off. */
  433. if (L_ECHOK(tty))
  434. opost('\n', tty);
  435. return;
  436. }
  437. kill_type = KILL;
  438. }
  439. seen_alnums = 0;
  440. while (tty->read_head != tty->canon_head) {
  441. head = tty->read_head;
  442. /* erase a single possibly multibyte character */
  443. do {
  444. head = (head - 1) & (N_TTY_BUF_SIZE-1);
  445. c = tty->read_buf[head];
  446. } while (is_continuation(c, tty) && head != tty->canon_head);
  447. /* do not partially erase */
  448. if (is_continuation(c, tty))
  449. break;
  450. if (kill_type == WERASE) {
  451. /* Equivalent to BSD's ALTWERASE. */
  452. if (isalnum(c) || c == '_')
  453. seen_alnums++;
  454. else if (seen_alnums)
  455. break;
  456. }
  457. cnt = (tty->read_head - head) & (N_TTY_BUF_SIZE-1);
  458. spin_lock_irqsave(&tty->read_lock, flags);
  459. tty->read_head = head;
  460. tty->read_cnt -= cnt;
  461. spin_unlock_irqrestore(&tty->read_lock, flags);
  462. if (L_ECHO(tty)) {
  463. if (L_ECHOPRT(tty)) {
  464. if (!tty->erasing) {
  465. tty_put_char(tty, '\\');
  466. tty->column++;
  467. tty->erasing = 1;
  468. }
  469. /* if cnt > 1, output a multi-byte character */
  470. echo_char(c, tty);
  471. while (--cnt > 0) {
  472. head = (head+1) & (N_TTY_BUF_SIZE-1);
  473. tty_put_char(tty, tty->read_buf[head]);
  474. }
  475. } else if (kill_type == ERASE && !L_ECHOE(tty)) {
  476. echo_char(ERASE_CHAR(tty), tty);
  477. } else if (c == '\t') {
  478. unsigned int col = tty->canon_column;
  479. unsigned long tail = tty->canon_head;
  480. /* Find the column of the last char. */
  481. while (tail != tty->read_head) {
  482. c = tty->read_buf[tail];
  483. if (c == '\t')
  484. col = (col | 7) + 1;
  485. else if (iscntrl(c)) {
  486. if (L_ECHOCTL(tty))
  487. col += 2;
  488. } else if (!is_continuation(c, tty))
  489. col++;
  490. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  491. }
  492. /* should never happen */
  493. if (tty->column > 0x80000000)
  494. tty->column = 0;
  495. /* Now backup to that column. */
  496. while (tty->column > col) {
  497. /* Can't use opost here. */
  498. tty_put_char(tty, '\b');
  499. if (tty->column > 0)
  500. tty->column--;
  501. }
  502. } else {
  503. if (iscntrl(c) && L_ECHOCTL(tty)) {
  504. tty_put_char(tty, '\b');
  505. tty_put_char(tty, ' ');
  506. tty_put_char(tty, '\b');
  507. if (tty->column > 0)
  508. tty->column--;
  509. }
  510. if (!iscntrl(c) || L_ECHOCTL(tty)) {
  511. tty_put_char(tty, '\b');
  512. tty_put_char(tty, ' ');
  513. tty_put_char(tty, '\b');
  514. if (tty->column > 0)
  515. tty->column--;
  516. }
  517. }
  518. }
  519. if (kill_type == ERASE)
  520. break;
  521. }
  522. if (tty->read_head == tty->canon_head)
  523. finish_erasing(tty);
  524. }
  525. /**
  526. * isig - handle the ISIG optio
  527. * @sig: signal
  528. * @tty: terminal
  529. * @flush: force flush
  530. *
  531. * Called when a signal is being sent due to terminal input. This
  532. * may caus terminal flushing to take place according to the termios
  533. * settings and character used. Called from the driver receive_buf
  534. * path so serialized.
  535. */
  536. static inline void isig(int sig, struct tty_struct *tty, int flush)
  537. {
  538. if (tty->pgrp)
  539. kill_pgrp(tty->pgrp, sig, 1);
  540. if (flush || !L_NOFLSH(tty)) {
  541. n_tty_flush_buffer(tty);
  542. tty_driver_flush_buffer(tty);
  543. }
  544. }
  545. /**
  546. * n_tty_receive_break - handle break
  547. * @tty: terminal
  548. *
  549. * An RS232 break event has been hit in the incoming bitstream. This
  550. * can cause a variety of events depending upon the termios settings.
  551. *
  552. * Called from the receive_buf path so single threaded.
  553. */
  554. static inline void n_tty_receive_break(struct tty_struct *tty)
  555. {
  556. if (I_IGNBRK(tty))
  557. return;
  558. if (I_BRKINT(tty)) {
  559. isig(SIGINT, tty, 1);
  560. return;
  561. }
  562. if (I_PARMRK(tty)) {
  563. put_tty_queue('\377', tty);
  564. put_tty_queue('\0', tty);
  565. }
  566. put_tty_queue('\0', tty);
  567. wake_up_interruptible(&tty->read_wait);
  568. }
  569. /**
  570. * n_tty_receive_overrun - handle overrun reporting
  571. * @tty: terminal
  572. *
  573. * Data arrived faster than we could process it. While the tty
  574. * driver has flagged this the bits that were missed are gone
  575. * forever.
  576. *
  577. * Called from the receive_buf path so single threaded. Does not
  578. * need locking as num_overrun and overrun_time are function
  579. * private.
  580. */
  581. static inline void n_tty_receive_overrun(struct tty_struct *tty)
  582. {
  583. char buf[64];
  584. tty->num_overrun++;
  585. if (time_before(tty->overrun_time, jiffies - HZ) ||
  586. time_after(tty->overrun_time, jiffies)) {
  587. printk(KERN_WARNING "%s: %d input overrun(s)\n",
  588. tty_name(tty, buf),
  589. tty->num_overrun);
  590. tty->overrun_time = jiffies;
  591. tty->num_overrun = 0;
  592. }
  593. }
  594. /**
  595. * n_tty_receive_parity_error - error notifier
  596. * @tty: terminal device
  597. * @c: character
  598. *
  599. * Process a parity error and queue the right data to indicate
  600. * the error case if necessary. Locking as per n_tty_receive_buf.
  601. */
  602. static inline void n_tty_receive_parity_error(struct tty_struct *tty,
  603. unsigned char c)
  604. {
  605. if (I_IGNPAR(tty))
  606. return;
  607. if (I_PARMRK(tty)) {
  608. put_tty_queue('\377', tty);
  609. put_tty_queue('\0', tty);
  610. put_tty_queue(c, tty);
  611. } else if (I_INPCK(tty))
  612. put_tty_queue('\0', tty);
  613. else
  614. put_tty_queue(c, tty);
  615. wake_up_interruptible(&tty->read_wait);
  616. }
  617. /**
  618. * n_tty_receive_char - perform processing
  619. * @tty: terminal device
  620. * @c: character
  621. *
  622. * Process an individual character of input received from the driver.
  623. * This is serialized with respect to itself by the rules for the
  624. * driver above.
  625. */
  626. static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
  627. {
  628. unsigned long flags;
  629. if (tty->raw) {
  630. put_tty_queue(c, tty);
  631. return;
  632. }
  633. if (I_ISTRIP(tty))
  634. c &= 0x7f;
  635. if (I_IUCLC(tty) && L_IEXTEN(tty))
  636. c=tolower(c);
  637. if (tty->stopped && !tty->flow_stopped && I_IXON(tty) &&
  638. ((I_IXANY(tty) && c != START_CHAR(tty) && c != STOP_CHAR(tty)) ||
  639. c == INTR_CHAR(tty) || c == QUIT_CHAR(tty) || c == SUSP_CHAR(tty)))
  640. start_tty(tty);
  641. if (tty->closing) {
  642. if (I_IXON(tty)) {
  643. if (c == START_CHAR(tty))
  644. start_tty(tty);
  645. else if (c == STOP_CHAR(tty))
  646. stop_tty(tty);
  647. }
  648. return;
  649. }
  650. /*
  651. * If the previous character was LNEXT, or we know that this
  652. * character is not one of the characters that we'll have to
  653. * handle specially, do shortcut processing to speed things
  654. * up.
  655. */
  656. if (!test_bit(c, tty->process_char_map) || tty->lnext) {
  657. finish_erasing(tty);
  658. tty->lnext = 0;
  659. if (L_ECHO(tty)) {
  660. if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
  661. tty_put_char(tty, '\a'); /* beep if no space */
  662. return;
  663. }
  664. /* Record the column of first canon char. */
  665. if (tty->canon_head == tty->read_head)
  666. tty->canon_column = tty->column;
  667. echo_char(c, tty);
  668. }
  669. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  670. put_tty_queue(c, tty);
  671. put_tty_queue(c, tty);
  672. return;
  673. }
  674. if (I_IXON(tty)) {
  675. if (c == START_CHAR(tty)) {
  676. start_tty(tty);
  677. return;
  678. }
  679. if (c == STOP_CHAR(tty)) {
  680. stop_tty(tty);
  681. return;
  682. }
  683. }
  684. if (L_ISIG(tty)) {
  685. int signal;
  686. signal = SIGINT;
  687. if (c == INTR_CHAR(tty))
  688. goto send_signal;
  689. signal = SIGQUIT;
  690. if (c == QUIT_CHAR(tty))
  691. goto send_signal;
  692. signal = SIGTSTP;
  693. if (c == SUSP_CHAR(tty)) {
  694. send_signal:
  695. /*
  696. * Echo character, and then send the signal.
  697. * Note that we do not use isig() here because we want
  698. * the order to be:
  699. * 1) flush, 2) echo, 3) signal
  700. */
  701. if (!L_NOFLSH(tty)) {
  702. n_tty_flush_buffer(tty);
  703. tty_driver_flush_buffer(tty);
  704. }
  705. if (L_ECHO(tty))
  706. echo_char(c, tty);
  707. if (tty->pgrp)
  708. kill_pgrp(tty->pgrp, signal, 1);
  709. return;
  710. }
  711. }
  712. if (c == '\r') {
  713. if (I_IGNCR(tty))
  714. return;
  715. if (I_ICRNL(tty))
  716. c = '\n';
  717. } else if (c == '\n' && I_INLCR(tty))
  718. c = '\r';
  719. if (tty->icanon) {
  720. if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
  721. (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
  722. eraser(c, tty);
  723. return;
  724. }
  725. if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
  726. tty->lnext = 1;
  727. if (L_ECHO(tty)) {
  728. finish_erasing(tty);
  729. if (L_ECHOCTL(tty)) {
  730. tty_put_char(tty, '^');
  731. tty_put_char(tty, '\b');
  732. }
  733. }
  734. return;
  735. }
  736. if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
  737. L_IEXTEN(tty)) {
  738. unsigned long tail = tty->canon_head;
  739. finish_erasing(tty);
  740. echo_char(c, tty);
  741. opost('\n', tty);
  742. while (tail != tty->read_head) {
  743. echo_char(tty->read_buf[tail], tty);
  744. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  745. }
  746. return;
  747. }
  748. if (c == '\n') {
  749. if (L_ECHO(tty) || L_ECHONL(tty)) {
  750. if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
  751. tty_put_char(tty, '\a');
  752. opost('\n', tty);
  753. }
  754. goto handle_newline;
  755. }
  756. if (c == EOF_CHAR(tty)) {
  757. if (tty->canon_head != tty->read_head)
  758. set_bit(TTY_PUSH, &tty->flags);
  759. c = __DISABLED_CHAR;
  760. goto handle_newline;
  761. }
  762. if ((c == EOL_CHAR(tty)) ||
  763. (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
  764. /*
  765. * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
  766. */
  767. if (L_ECHO(tty)) {
  768. if (tty->read_cnt >= N_TTY_BUF_SIZE-1)
  769. tty_put_char(tty, '\a');
  770. /* Record the column of first canon char. */
  771. if (tty->canon_head == tty->read_head)
  772. tty->canon_column = tty->column;
  773. echo_char(c, tty);
  774. }
  775. /*
  776. * XXX does PARMRK doubling happen for
  777. * EOL_CHAR and EOL2_CHAR?
  778. */
  779. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  780. put_tty_queue(c, tty);
  781. handle_newline:
  782. spin_lock_irqsave(&tty->read_lock, flags);
  783. set_bit(tty->read_head, tty->read_flags);
  784. put_tty_queue_nolock(c, tty);
  785. tty->canon_head = tty->read_head;
  786. tty->canon_data++;
  787. spin_unlock_irqrestore(&tty->read_lock, flags);
  788. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  789. if (waitqueue_active(&tty->read_wait))
  790. wake_up_interruptible(&tty->read_wait);
  791. return;
  792. }
  793. }
  794. finish_erasing(tty);
  795. if (L_ECHO(tty)) {
  796. if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
  797. tty_put_char(tty, '\a'); /* beep if no space */
  798. return;
  799. }
  800. if (c == '\n')
  801. opost('\n', tty);
  802. else {
  803. /* Record the column of first canon char. */
  804. if (tty->canon_head == tty->read_head)
  805. tty->canon_column = tty->column;
  806. echo_char(c, tty);
  807. }
  808. }
  809. if (I_PARMRK(tty) && c == (unsigned char) '\377')
  810. put_tty_queue(c, tty);
  811. put_tty_queue(c, tty);
  812. }
  813. /**
  814. * n_tty_write_wakeup - asynchronous I/O notifier
  815. * @tty: tty device
  816. *
  817. * Required for the ptys, serial driver etc. since processes
  818. * that attach themselves to the master and rely on ASYNC
  819. * IO must be woken up
  820. */
  821. static void n_tty_write_wakeup(struct tty_struct *tty)
  822. {
  823. if (tty->fasync) {
  824. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  825. kill_fasync(&tty->fasync, SIGIO, POLL_OUT);
  826. }
  827. }
  828. /**
  829. * n_tty_receive_buf - data receive
  830. * @tty: terminal device
  831. * @cp: buffer
  832. * @fp: flag buffer
  833. * @count: characters
  834. *
  835. * Called by the terminal driver when a block of characters has
  836. * been received. This function must be called from soft contexts
  837. * not from interrupt context. The driver is responsible for making
  838. * calls one at a time and in order (or using flush_to_ldisc)
  839. */
  840. static void n_tty_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  841. char *fp, int count)
  842. {
  843. const unsigned char *p;
  844. char *f, flags = TTY_NORMAL;
  845. int i;
  846. char buf[64];
  847. unsigned long cpuflags;
  848. if (!tty->read_buf)
  849. return;
  850. if (tty->real_raw) {
  851. spin_lock_irqsave(&tty->read_lock, cpuflags);
  852. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  853. N_TTY_BUF_SIZE - tty->read_head);
  854. i = min(count, i);
  855. memcpy(tty->read_buf + tty->read_head, cp, i);
  856. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  857. tty->read_cnt += i;
  858. cp += i;
  859. count -= i;
  860. i = min(N_TTY_BUF_SIZE - tty->read_cnt,
  861. N_TTY_BUF_SIZE - tty->read_head);
  862. i = min(count, i);
  863. memcpy(tty->read_buf + tty->read_head, cp, i);
  864. tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
  865. tty->read_cnt += i;
  866. spin_unlock_irqrestore(&tty->read_lock, cpuflags);
  867. } else {
  868. for (i = count, p = cp, f = fp; i; i--, p++) {
  869. if (f)
  870. flags = *f++;
  871. switch (flags) {
  872. case TTY_NORMAL:
  873. n_tty_receive_char(tty, *p);
  874. break;
  875. case TTY_BREAK:
  876. n_tty_receive_break(tty);
  877. break;
  878. case TTY_PARITY:
  879. case TTY_FRAME:
  880. n_tty_receive_parity_error(tty, *p);
  881. break;
  882. case TTY_OVERRUN:
  883. n_tty_receive_overrun(tty);
  884. break;
  885. default:
  886. printk(KERN_ERR "%s: unknown flag %d\n",
  887. tty_name(tty, buf), flags);
  888. break;
  889. }
  890. }
  891. if (tty->ops->flush_chars)
  892. tty->ops->flush_chars(tty);
  893. }
  894. n_tty_set_room(tty);
  895. if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
  896. kill_fasync(&tty->fasync, SIGIO, POLL_IN);
  897. if (waitqueue_active(&tty->read_wait))
  898. wake_up_interruptible(&tty->read_wait);
  899. }
  900. /*
  901. * Check the remaining room for the input canonicalization
  902. * mode. We don't want to throttle the driver if we're in
  903. * canonical mode and don't have a newline yet!
  904. */
  905. if (tty->receive_room < TTY_THRESHOLD_THROTTLE)
  906. tty_throttle(tty);
  907. }
  908. int is_ignored(int sig)
  909. {
  910. return (sigismember(&current->blocked, sig) ||
  911. current->sighand->action[sig-1].sa.sa_handler == SIG_IGN);
  912. }
  913. /**
  914. * n_tty_set_termios - termios data changed
  915. * @tty: terminal
  916. * @old: previous data
  917. *
  918. * Called by the tty layer when the user changes termios flags so
  919. * that the line discipline can plan ahead. This function cannot sleep
  920. * and is protected from re-entry by the tty layer. The user is
  921. * guaranteed that this function will not be re-entered or in progress
  922. * when the ldisc is closed.
  923. */
  924. static void n_tty_set_termios(struct tty_struct *tty, struct ktermios *old)
  925. {
  926. int canon_change = 1;
  927. BUG_ON(!tty);
  928. if (old)
  929. canon_change = (old->c_lflag ^ tty->termios->c_lflag) & ICANON;
  930. if (canon_change) {
  931. memset(&tty->read_flags, 0, sizeof tty->read_flags);
  932. tty->canon_head = tty->read_tail;
  933. tty->canon_data = 0;
  934. tty->erasing = 0;
  935. }
  936. if (canon_change && !L_ICANON(tty) && tty->read_cnt)
  937. wake_up_interruptible(&tty->read_wait);
  938. tty->icanon = (L_ICANON(tty) != 0);
  939. if (test_bit(TTY_HW_COOK_IN, &tty->flags)) {
  940. tty->raw = 1;
  941. tty->real_raw = 1;
  942. n_tty_set_room(tty);
  943. return;
  944. }
  945. if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
  946. I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
  947. I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
  948. I_PARMRK(tty)) {
  949. memset(tty->process_char_map, 0, 256/8);
  950. if (I_IGNCR(tty) || I_ICRNL(tty))
  951. set_bit('\r', tty->process_char_map);
  952. if (I_INLCR(tty))
  953. set_bit('\n', tty->process_char_map);
  954. if (L_ICANON(tty)) {
  955. set_bit(ERASE_CHAR(tty), tty->process_char_map);
  956. set_bit(KILL_CHAR(tty), tty->process_char_map);
  957. set_bit(EOF_CHAR(tty), tty->process_char_map);
  958. set_bit('\n', tty->process_char_map);
  959. set_bit(EOL_CHAR(tty), tty->process_char_map);
  960. if (L_IEXTEN(tty)) {
  961. set_bit(WERASE_CHAR(tty),
  962. tty->process_char_map);
  963. set_bit(LNEXT_CHAR(tty),
  964. tty->process_char_map);
  965. set_bit(EOL2_CHAR(tty),
  966. tty->process_char_map);
  967. if (L_ECHO(tty))
  968. set_bit(REPRINT_CHAR(tty),
  969. tty->process_char_map);
  970. }
  971. }
  972. if (I_IXON(tty)) {
  973. set_bit(START_CHAR(tty), tty->process_char_map);
  974. set_bit(STOP_CHAR(tty), tty->process_char_map);
  975. }
  976. if (L_ISIG(tty)) {
  977. set_bit(INTR_CHAR(tty), tty->process_char_map);
  978. set_bit(QUIT_CHAR(tty), tty->process_char_map);
  979. set_bit(SUSP_CHAR(tty), tty->process_char_map);
  980. }
  981. clear_bit(__DISABLED_CHAR, tty->process_char_map);
  982. tty->raw = 0;
  983. tty->real_raw = 0;
  984. } else {
  985. tty->raw = 1;
  986. if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
  987. (I_IGNPAR(tty) || !I_INPCK(tty)) &&
  988. (tty->driver->flags & TTY_DRIVER_REAL_RAW))
  989. tty->real_raw = 1;
  990. else
  991. tty->real_raw = 0;
  992. }
  993. n_tty_set_room(tty);
  994. /* The termios change make the tty ready for I/O */
  995. wake_up_interruptible(&tty->write_wait);
  996. wake_up_interruptible(&tty->read_wait);
  997. }
  998. /**
  999. * n_tty_close - close the ldisc for this tty
  1000. * @tty: device
  1001. *
  1002. * Called from the terminal layer when this line discipline is
  1003. * being shut down, either because of a close or becsuse of a
  1004. * discipline change. The function will not be called while other
  1005. * ldisc methods are in progress.
  1006. */
  1007. static void n_tty_close(struct tty_struct *tty)
  1008. {
  1009. n_tty_flush_buffer(tty);
  1010. if (tty->read_buf) {
  1011. free_buf(tty->read_buf);
  1012. tty->read_buf = NULL;
  1013. }
  1014. }
  1015. /**
  1016. * n_tty_open - open an ldisc
  1017. * @tty: terminal to open
  1018. *
  1019. * Called when this line discipline is being attached to the
  1020. * terminal device. Can sleep. Called serialized so that no
  1021. * other events will occur in parallel. No further open will occur
  1022. * until a close.
  1023. */
  1024. static int n_tty_open(struct tty_struct *tty)
  1025. {
  1026. if (!tty)
  1027. return -EINVAL;
  1028. /* This one is ugly. Currently a malloc failure here can panic */
  1029. if (!tty->read_buf) {
  1030. tty->read_buf = alloc_buf();
  1031. if (!tty->read_buf)
  1032. return -ENOMEM;
  1033. }
  1034. memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
  1035. reset_buffer_flags(tty);
  1036. tty->column = 0;
  1037. n_tty_set_termios(tty, NULL);
  1038. tty->minimum_to_wake = 1;
  1039. tty->closing = 0;
  1040. return 0;
  1041. }
  1042. static inline int input_available_p(struct tty_struct *tty, int amt)
  1043. {
  1044. if (tty->icanon) {
  1045. if (tty->canon_data)
  1046. return 1;
  1047. } else if (tty->read_cnt >= (amt ? amt : 1))
  1048. return 1;
  1049. return 0;
  1050. }
  1051. /**
  1052. * copy_from_read_buf - copy read data directly
  1053. * @tty: terminal device
  1054. * @b: user data
  1055. * @nr: size of data
  1056. *
  1057. * Helper function to speed up read_chan. It is only called when
  1058. * ICANON is off; it copies characters straight from the tty queue to
  1059. * user space directly. It can be profitably called twice; once to
  1060. * drain the space from the tail pointer to the (physical) end of the
  1061. * buffer, and once to drain the space from the (physical) beginning of
  1062. * the buffer to head pointer.
  1063. *
  1064. * Called under the tty->atomic_read_lock sem
  1065. *
  1066. */
  1067. static int copy_from_read_buf(struct tty_struct *tty,
  1068. unsigned char __user **b,
  1069. size_t *nr)
  1070. {
  1071. int retval;
  1072. size_t n;
  1073. unsigned long flags;
  1074. retval = 0;
  1075. spin_lock_irqsave(&tty->read_lock, flags);
  1076. n = min(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail);
  1077. n = min(*nr, n);
  1078. spin_unlock_irqrestore(&tty->read_lock, flags);
  1079. if (n) {
  1080. retval = copy_to_user(*b, &tty->read_buf[tty->read_tail], n);
  1081. n -= retval;
  1082. tty_audit_add_data(tty, &tty->read_buf[tty->read_tail], n);
  1083. spin_lock_irqsave(&tty->read_lock, flags);
  1084. tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
  1085. tty->read_cnt -= n;
  1086. spin_unlock_irqrestore(&tty->read_lock, flags);
  1087. *b += n;
  1088. *nr -= n;
  1089. }
  1090. return retval;
  1091. }
  1092. extern ssize_t redirected_tty_write(struct file *, const char __user *,
  1093. size_t, loff_t *);
  1094. /**
  1095. * job_control - check job control
  1096. * @tty: tty
  1097. * @file: file handle
  1098. *
  1099. * Perform job control management checks on this file/tty descriptor
  1100. * and if appropriate send any needed signals and return a negative
  1101. * error code if action should be taken.
  1102. *
  1103. * FIXME:
  1104. * Locking: None - redirected write test is safe, testing
  1105. * current->signal should possibly lock current->sighand
  1106. * pgrp locking ?
  1107. */
  1108. static int job_control(struct tty_struct *tty, struct file *file)
  1109. {
  1110. /* Job control check -- must be done at start and after
  1111. every sleep (POSIX.1 7.1.1.4). */
  1112. /* NOTE: not yet done after every sleep pending a thorough
  1113. check of the logic of this change. -- jlc */
  1114. /* don't stop on /dev/console */
  1115. if (file->f_op->write != redirected_tty_write &&
  1116. current->signal->tty == tty) {
  1117. if (!tty->pgrp)
  1118. printk(KERN_ERR "read_chan: no tty->pgrp!\n");
  1119. else if (task_pgrp(current) != tty->pgrp) {
  1120. if (is_ignored(SIGTTIN) ||
  1121. is_current_pgrp_orphaned())
  1122. return -EIO;
  1123. kill_pgrp(task_pgrp(current), SIGTTIN, 1);
  1124. set_thread_flag(TIF_SIGPENDING);
  1125. return -ERESTARTSYS;
  1126. }
  1127. }
  1128. return 0;
  1129. }
  1130. /**
  1131. * read_chan - read function for tty
  1132. * @tty: tty device
  1133. * @file: file object
  1134. * @buf: userspace buffer pointer
  1135. * @nr: size of I/O
  1136. *
  1137. * Perform reads for the line discipline. We are guaranteed that the
  1138. * line discipline will not be closed under us but we may get multiple
  1139. * parallel readers and must handle this ourselves. We may also get
  1140. * a hangup. Always called in user context, may sleep.
  1141. *
  1142. * This code must be sure never to sleep through a hangup.
  1143. */
  1144. static ssize_t read_chan(struct tty_struct *tty, struct file *file,
  1145. unsigned char __user *buf, size_t nr)
  1146. {
  1147. unsigned char __user *b = buf;
  1148. DECLARE_WAITQUEUE(wait, current);
  1149. int c;
  1150. int minimum, time;
  1151. ssize_t retval = 0;
  1152. ssize_t size;
  1153. long timeout;
  1154. unsigned long flags;
  1155. int packet;
  1156. do_it_again:
  1157. if (!tty->read_buf) {
  1158. printk(KERN_ERR "n_tty_read_chan: read_buf == NULL?!?\n");
  1159. return -EIO;
  1160. }
  1161. c = job_control(tty, file);
  1162. if (c < 0)
  1163. return c;
  1164. minimum = time = 0;
  1165. timeout = MAX_SCHEDULE_TIMEOUT;
  1166. if (!tty->icanon) {
  1167. time = (HZ / 10) * TIME_CHAR(tty);
  1168. minimum = MIN_CHAR(tty);
  1169. if (minimum) {
  1170. if (time)
  1171. tty->minimum_to_wake = 1;
  1172. else if (!waitqueue_active(&tty->read_wait) ||
  1173. (tty->minimum_to_wake > minimum))
  1174. tty->minimum_to_wake = minimum;
  1175. } else {
  1176. timeout = 0;
  1177. if (time) {
  1178. timeout = time;
  1179. time = 0;
  1180. }
  1181. tty->minimum_to_wake = minimum = 1;
  1182. }
  1183. }
  1184. /*
  1185. * Internal serialization of reads.
  1186. */
  1187. if (file->f_flags & O_NONBLOCK) {
  1188. if (!mutex_trylock(&tty->atomic_read_lock))
  1189. return -EAGAIN;
  1190. } else {
  1191. if (mutex_lock_interruptible(&tty->atomic_read_lock))
  1192. return -ERESTARTSYS;
  1193. }
  1194. packet = tty->packet;
  1195. add_wait_queue(&tty->read_wait, &wait);
  1196. while (nr) {
  1197. /* First test for status change. */
  1198. if (packet && tty->link->ctrl_status) {
  1199. unsigned char cs;
  1200. if (b != buf)
  1201. break;
  1202. spin_lock_irqsave(&tty->link->ctrl_lock, flags);
  1203. cs = tty->link->ctrl_status;
  1204. tty->link->ctrl_status = 0;
  1205. spin_unlock_irqrestore(&tty->link->ctrl_lock, flags);
  1206. if (tty_put_user(tty, cs, b++)) {
  1207. retval = -EFAULT;
  1208. b--;
  1209. break;
  1210. }
  1211. nr--;
  1212. break;
  1213. }
  1214. /* This statement must be first before checking for input
  1215. so that any interrupt will set the state back to
  1216. TASK_RUNNING. */
  1217. set_current_state(TASK_INTERRUPTIBLE);
  1218. if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
  1219. ((minimum - (b - buf)) >= 1))
  1220. tty->minimum_to_wake = (minimum - (b - buf));
  1221. if (!input_available_p(tty, 0)) {
  1222. if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
  1223. retval = -EIO;
  1224. break;
  1225. }
  1226. if (tty_hung_up_p(file))
  1227. break;
  1228. if (!timeout)
  1229. break;
  1230. if (file->f_flags & O_NONBLOCK) {
  1231. retval = -EAGAIN;
  1232. break;
  1233. }
  1234. if (signal_pending(current)) {
  1235. retval = -ERESTARTSYS;
  1236. break;
  1237. }
  1238. /* FIXME: does n_tty_set_room need locking ? */
  1239. n_tty_set_room(tty);
  1240. timeout = schedule_timeout(timeout);
  1241. continue;
  1242. }
  1243. __set_current_state(TASK_RUNNING);
  1244. /* Deal with packet mode. */
  1245. if (packet && b == buf) {
  1246. if (tty_put_user(tty, TIOCPKT_DATA, b++)) {
  1247. retval = -EFAULT;
  1248. b--;
  1249. break;
  1250. }
  1251. nr--;
  1252. }
  1253. if (tty->icanon) {
  1254. /* N.B. avoid overrun if nr == 0 */
  1255. while (nr && tty->read_cnt) {
  1256. int eol;
  1257. eol = test_and_clear_bit(tty->read_tail,
  1258. tty->read_flags);
  1259. c = tty->read_buf[tty->read_tail];
  1260. spin_lock_irqsave(&tty->read_lock, flags);
  1261. tty->read_tail = ((tty->read_tail+1) &
  1262. (N_TTY_BUF_SIZE-1));
  1263. tty->read_cnt--;
  1264. if (eol) {
  1265. /* this test should be redundant:
  1266. * we shouldn't be reading data if
  1267. * canon_data is 0
  1268. */
  1269. if (--tty->canon_data < 0)
  1270. tty->canon_data = 0;
  1271. }
  1272. spin_unlock_irqrestore(&tty->read_lock, flags);
  1273. if (!eol || (c != __DISABLED_CHAR)) {
  1274. if (tty_put_user(tty, c, b++)) {
  1275. retval = -EFAULT;
  1276. b--;
  1277. break;
  1278. }
  1279. nr--;
  1280. }
  1281. if (eol) {
  1282. tty_audit_push(tty);
  1283. break;
  1284. }
  1285. }
  1286. if (retval)
  1287. break;
  1288. } else {
  1289. int uncopied;
  1290. /* The copy function takes the read lock and handles
  1291. locking internally for this case */
  1292. uncopied = copy_from_read_buf(tty, &b, &nr);
  1293. uncopied += copy_from_read_buf(tty, &b, &nr);
  1294. if (uncopied) {
  1295. retval = -EFAULT;
  1296. break;
  1297. }
  1298. }
  1299. /* If there is enough space in the read buffer now, let the
  1300. * low-level driver know. We use n_tty_chars_in_buffer() to
  1301. * check the buffer, as it now knows about canonical mode.
  1302. * Otherwise, if the driver is throttled and the line is
  1303. * longer than TTY_THRESHOLD_UNTHROTTLE in canonical mode,
  1304. * we won't get any more characters.
  1305. */
  1306. if (n_tty_chars_in_buffer(tty) <= TTY_THRESHOLD_UNTHROTTLE) {
  1307. n_tty_set_room(tty);
  1308. check_unthrottle(tty);
  1309. }
  1310. if (b - buf >= minimum)
  1311. break;
  1312. if (time)
  1313. timeout = time;
  1314. }
  1315. mutex_unlock(&tty->atomic_read_lock);
  1316. remove_wait_queue(&tty->read_wait, &wait);
  1317. if (!waitqueue_active(&tty->read_wait))
  1318. tty->minimum_to_wake = minimum;
  1319. __set_current_state(TASK_RUNNING);
  1320. size = b - buf;
  1321. if (size) {
  1322. retval = size;
  1323. if (nr)
  1324. clear_bit(TTY_PUSH, &tty->flags);
  1325. } else if (test_and_clear_bit(TTY_PUSH, &tty->flags))
  1326. goto do_it_again;
  1327. n_tty_set_room(tty);
  1328. return retval;
  1329. }
  1330. /**
  1331. * write_chan - write function for tty
  1332. * @tty: tty device
  1333. * @file: file object
  1334. * @buf: userspace buffer pointer
  1335. * @nr: size of I/O
  1336. *
  1337. * Write function of the terminal device. This is serialized with
  1338. * respect to other write callers but not to termios changes, reads
  1339. * and other such events. We must be careful with N_TTY as the receive
  1340. * code will echo characters, thus calling driver write methods.
  1341. *
  1342. * This code must be sure never to sleep through a hangup.
  1343. */
  1344. static ssize_t write_chan(struct tty_struct *tty, struct file *file,
  1345. const unsigned char *buf, size_t nr)
  1346. {
  1347. const unsigned char *b = buf;
  1348. DECLARE_WAITQUEUE(wait, current);
  1349. int c;
  1350. ssize_t retval = 0;
  1351. /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
  1352. if (L_TOSTOP(tty) && file->f_op->write != redirected_tty_write) {
  1353. retval = tty_check_change(tty);
  1354. if (retval)
  1355. return retval;
  1356. }
  1357. add_wait_queue(&tty->write_wait, &wait);
  1358. while (1) {
  1359. set_current_state(TASK_INTERRUPTIBLE);
  1360. if (signal_pending(current)) {
  1361. retval = -ERESTARTSYS;
  1362. break;
  1363. }
  1364. if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
  1365. retval = -EIO;
  1366. break;
  1367. }
  1368. if (O_OPOST(tty) && !(test_bit(TTY_HW_COOK_OUT, &tty->flags))) {
  1369. while (nr > 0) {
  1370. ssize_t num = opost_block(tty, b, nr);
  1371. if (num < 0) {
  1372. if (num == -EAGAIN)
  1373. break;
  1374. retval = num;
  1375. goto break_out;
  1376. }
  1377. b += num;
  1378. nr -= num;
  1379. if (nr == 0)
  1380. break;
  1381. c = *b;
  1382. if (opost(c, tty) < 0)
  1383. break;
  1384. b++; nr--;
  1385. }
  1386. if (tty->ops->flush_chars)
  1387. tty->ops->flush_chars(tty);
  1388. } else {
  1389. while (nr > 0) {
  1390. c = tty->ops->write(tty, b, nr);
  1391. if (c < 0) {
  1392. retval = c;
  1393. goto break_out;
  1394. }
  1395. if (!c)
  1396. break;
  1397. b += c;
  1398. nr -= c;
  1399. }
  1400. }
  1401. if (!nr)
  1402. break;
  1403. if (file->f_flags & O_NONBLOCK) {
  1404. retval = -EAGAIN;
  1405. break;
  1406. }
  1407. schedule();
  1408. }
  1409. break_out:
  1410. __set_current_state(TASK_RUNNING);
  1411. remove_wait_queue(&tty->write_wait, &wait);
  1412. return (b - buf) ? b - buf : retval;
  1413. }
  1414. /**
  1415. * normal_poll - poll method for N_TTY
  1416. * @tty: terminal device
  1417. * @file: file accessing it
  1418. * @wait: poll table
  1419. *
  1420. * Called when the line discipline is asked to poll() for data or
  1421. * for special events. This code is not serialized with respect to
  1422. * other events save open/close.
  1423. *
  1424. * This code must be sure never to sleep through a hangup.
  1425. * Called without the kernel lock held - fine
  1426. */
  1427. static unsigned int normal_poll(struct tty_struct *tty, struct file *file,
  1428. poll_table *wait)
  1429. {
  1430. unsigned int mask = 0;
  1431. poll_wait(file, &tty->read_wait, wait);
  1432. poll_wait(file, &tty->write_wait, wait);
  1433. if (input_available_p(tty, TIME_CHAR(tty) ? 0 : MIN_CHAR(tty)))
  1434. mask |= POLLIN | POLLRDNORM;
  1435. if (tty->packet && tty->link->ctrl_status)
  1436. mask |= POLLPRI | POLLIN | POLLRDNORM;
  1437. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  1438. mask |= POLLHUP;
  1439. if (tty_hung_up_p(file))
  1440. mask |= POLLHUP;
  1441. if (!(mask & (POLLHUP | POLLIN | POLLRDNORM))) {
  1442. if (MIN_CHAR(tty) && !TIME_CHAR(tty))
  1443. tty->minimum_to_wake = MIN_CHAR(tty);
  1444. else
  1445. tty->minimum_to_wake = 1;
  1446. }
  1447. if (tty->ops->write && !tty_is_writelocked(tty) &&
  1448. tty_chars_in_buffer(tty) < WAKEUP_CHARS &&
  1449. tty_write_room(tty) > 0)
  1450. mask |= POLLOUT | POLLWRNORM;
  1451. return mask;
  1452. }
  1453. static unsigned long inq_canon(struct tty_struct *tty)
  1454. {
  1455. int nr, head, tail;
  1456. if (!tty->canon_data || !tty->read_buf)
  1457. return 0;
  1458. head = tty->canon_head;
  1459. tail = tty->read_tail;
  1460. nr = (head - tail) & (N_TTY_BUF_SIZE-1);
  1461. /* Skip EOF-chars.. */
  1462. while (head != tail) {
  1463. if (test_bit(tail, tty->read_flags) &&
  1464. tty->read_buf[tail] == __DISABLED_CHAR)
  1465. nr--;
  1466. tail = (tail+1) & (N_TTY_BUF_SIZE-1);
  1467. }
  1468. return nr;
  1469. }
  1470. static int n_tty_ioctl(struct tty_struct *tty, struct file *file,
  1471. unsigned int cmd, unsigned long arg)
  1472. {
  1473. int retval;
  1474. switch (cmd) {
  1475. case TIOCOUTQ:
  1476. return put_user(tty_chars_in_buffer(tty), (int __user *) arg);
  1477. case TIOCINQ:
  1478. retval = tty->read_cnt;
  1479. if (L_ICANON(tty))
  1480. retval = inq_canon(tty);
  1481. return put_user(retval, (unsigned int __user *) arg);
  1482. default:
  1483. return n_tty_ioctl_helper(tty, file, cmd, arg);
  1484. }
  1485. }
  1486. struct tty_ldisc_ops tty_ldisc_N_TTY = {
  1487. .magic = TTY_LDISC_MAGIC,
  1488. .name = "n_tty",
  1489. .open = n_tty_open,
  1490. .close = n_tty_close,
  1491. .flush_buffer = n_tty_flush_buffer,
  1492. .chars_in_buffer = n_tty_chars_in_buffer,
  1493. .read = read_chan,
  1494. .write = write_chan,
  1495. .ioctl = n_tty_ioctl,
  1496. .set_termios = n_tty_set_termios,
  1497. .poll = normal_poll,
  1498. .receive_buf = n_tty_receive_buf,
  1499. .write_wakeup = n_tty_write_wakeup
  1500. };