line.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/irqreturn.h"
  6. #include "linux/kd.h"
  7. #include "chan_kern.h"
  8. #include "irq_kern.h"
  9. #include "irq_user.h"
  10. #include "os.h"
  11. #define LINE_BUFSIZE 4096
  12. static irqreturn_t line_interrupt(int irq, void *data)
  13. {
  14. struct chan *chan = data;
  15. struct line *line = chan->line;
  16. struct tty_struct *tty = line->tty;
  17. if (line)
  18. chan_interrupt(&line->chan_list, &line->task, tty, irq);
  19. return IRQ_HANDLED;
  20. }
  21. static void line_timer_cb(struct work_struct *work)
  22. {
  23. struct line *line = container_of(work, struct line, task.work);
  24. if (!line->throttled)
  25. chan_interrupt(&line->chan_list, &line->task, line->tty,
  26. line->driver->read_irq);
  27. }
  28. /*
  29. * Returns the free space inside the ring buffer of this line.
  30. *
  31. * Should be called while holding line->lock (this does not modify data).
  32. */
  33. static int write_room(struct line *line)
  34. {
  35. int n;
  36. if (line->buffer == NULL)
  37. return LINE_BUFSIZE - 1;
  38. /* This is for the case where the buffer is wrapped! */
  39. n = line->head - line->tail;
  40. if (n <= 0)
  41. n = LINE_BUFSIZE + n; /* The other case */
  42. return n - 1;
  43. }
  44. int line_write_room(struct tty_struct *tty)
  45. {
  46. struct line *line = tty->driver_data;
  47. unsigned long flags;
  48. int room;
  49. if (tty->stopped)
  50. return 0;
  51. spin_lock_irqsave(&line->lock, flags);
  52. room = write_room(line);
  53. spin_unlock_irqrestore(&line->lock, flags);
  54. /*XXX: Warning to remove */
  55. if (0 == room)
  56. printk(KERN_DEBUG "%s: %s: no room left in buffer\n",
  57. __FUNCTION__,tty->name);
  58. return room;
  59. }
  60. int line_chars_in_buffer(struct tty_struct *tty)
  61. {
  62. struct line *line = tty->driver_data;
  63. unsigned long flags;
  64. int ret;
  65. spin_lock_irqsave(&line->lock, flags);
  66. /*write_room subtracts 1 for the needed NULL, so we readd it.*/
  67. ret = LINE_BUFSIZE - (write_room(line) + 1);
  68. spin_unlock_irqrestore(&line->lock, flags);
  69. return ret;
  70. }
  71. /*
  72. * This copies the content of buf into the circular buffer associated with
  73. * this line.
  74. * The return value is the number of characters actually copied, i.e. the ones
  75. * for which there was space: this function is not supposed to ever flush out
  76. * the circular buffer.
  77. *
  78. * Must be called while holding line->lock!
  79. */
  80. static int buffer_data(struct line *line, const char *buf, int len)
  81. {
  82. int end, room;
  83. if (line->buffer == NULL) {
  84. line->buffer = kmalloc(LINE_BUFSIZE, GFP_ATOMIC);
  85. if (line->buffer == NULL) {
  86. printk(KERN_ERR "buffer_data - atomic allocation "
  87. "failed\n");
  88. return 0;
  89. }
  90. line->head = line->buffer;
  91. line->tail = line->buffer;
  92. }
  93. room = write_room(line);
  94. len = (len > room) ? room : len;
  95. end = line->buffer + LINE_BUFSIZE - line->tail;
  96. if (len < end) {
  97. memcpy(line->tail, buf, len);
  98. line->tail += len;
  99. }
  100. else {
  101. /* The circular buffer is wrapping */
  102. memcpy(line->tail, buf, end);
  103. buf += end;
  104. memcpy(line->buffer, buf, len - end);
  105. line->tail = line->buffer + len - end;
  106. }
  107. return len;
  108. }
  109. /*
  110. * Flushes the ring buffer to the output channels. That is, write_chan is
  111. * called, passing it line->head as buffer, and an appropriate count.
  112. *
  113. * On exit, returns 1 when the buffer is empty,
  114. * 0 when the buffer is not empty on exit,
  115. * and -errno when an error occurred.
  116. *
  117. * Must be called while holding line->lock!*/
  118. static int flush_buffer(struct line *line)
  119. {
  120. int n, count;
  121. if ((line->buffer == NULL) || (line->head == line->tail))
  122. return 1;
  123. if (line->tail < line->head) {
  124. /* line->buffer + LINE_BUFSIZE is the end of the buffer! */
  125. count = line->buffer + LINE_BUFSIZE - line->head;
  126. n = write_chan(&line->chan_list, line->head, count,
  127. line->driver->write_irq);
  128. if (n < 0)
  129. return n;
  130. if (n == count) {
  131. /*
  132. * We have flushed from ->head to buffer end, now we
  133. * must flush only from the beginning to ->tail.
  134. */
  135. line->head = line->buffer;
  136. } else {
  137. line->head += n;
  138. return 0;
  139. }
  140. }
  141. count = line->tail - line->head;
  142. n = write_chan(&line->chan_list, line->head, count,
  143. line->driver->write_irq);
  144. if (n < 0)
  145. return n;
  146. line->head += n;
  147. return line->head == line->tail;
  148. }
  149. void line_flush_buffer(struct tty_struct *tty)
  150. {
  151. struct line *line = tty->driver_data;
  152. unsigned long flags;
  153. int err;
  154. /*XXX: copied from line_write, verify if it is correct!*/
  155. if (tty->stopped)
  156. return;
  157. spin_lock_irqsave(&line->lock, flags);
  158. err = flush_buffer(line);
  159. spin_unlock_irqrestore(&line->lock, flags);
  160. }
  161. /*
  162. * We map both ->flush_chars and ->put_char (which go in pair) onto
  163. * ->flush_buffer and ->write. Hope it's not that bad.
  164. */
  165. void line_flush_chars(struct tty_struct *tty)
  166. {
  167. line_flush_buffer(tty);
  168. }
  169. void line_put_char(struct tty_struct *tty, unsigned char ch)
  170. {
  171. line_write(tty, &ch, sizeof(ch));
  172. }
  173. int line_write(struct tty_struct *tty, const unsigned char *buf, int len)
  174. {
  175. struct line *line = tty->driver_data;
  176. unsigned long flags;
  177. int n, ret = 0;
  178. if (tty->stopped)
  179. return 0;
  180. spin_lock_irqsave(&line->lock, flags);
  181. if (line->head != line->tail)
  182. ret = buffer_data(line, buf, len);
  183. else {
  184. n = write_chan(&line->chan_list, buf, len,
  185. line->driver->write_irq);
  186. if (n < 0) {
  187. ret = n;
  188. goto out_up;
  189. }
  190. len -= n;
  191. ret += n;
  192. if (len > 0)
  193. ret += buffer_data(line, buf + n, len);
  194. }
  195. out_up:
  196. spin_unlock_irqrestore(&line->lock, flags);
  197. return ret;
  198. }
  199. void line_set_termios(struct tty_struct *tty, struct ktermios * old)
  200. {
  201. /* nothing */
  202. }
  203. static const struct {
  204. int cmd;
  205. char *level;
  206. char *name;
  207. } tty_ioctls[] = {
  208. /* don't print these, they flood the log ... */
  209. { TCGETS, NULL, "TCGETS" },
  210. { TCSETS, NULL, "TCSETS" },
  211. { TCSETSW, NULL, "TCSETSW" },
  212. { TCFLSH, NULL, "TCFLSH" },
  213. { TCSBRK, NULL, "TCSBRK" },
  214. /* general tty stuff */
  215. { TCSETSF, KERN_DEBUG, "TCSETSF" },
  216. { TCGETA, KERN_DEBUG, "TCGETA" },
  217. { TIOCMGET, KERN_DEBUG, "TIOCMGET" },
  218. { TCSBRKP, KERN_DEBUG, "TCSBRKP" },
  219. { TIOCMSET, KERN_DEBUG, "TIOCMSET" },
  220. /* linux-specific ones */
  221. { TIOCLINUX, KERN_INFO, "TIOCLINUX" },
  222. { KDGKBMODE, KERN_INFO, "KDGKBMODE" },
  223. { KDGKBTYPE, KERN_INFO, "KDGKBTYPE" },
  224. { KDSIGACCEPT, KERN_INFO, "KDSIGACCEPT" },
  225. };
  226. int line_ioctl(struct tty_struct *tty, struct file * file,
  227. unsigned int cmd, unsigned long arg)
  228. {
  229. int ret;
  230. int i;
  231. ret = 0;
  232. switch(cmd) {
  233. #ifdef TIOCGETP
  234. case TIOCGETP:
  235. case TIOCSETP:
  236. case TIOCSETN:
  237. #endif
  238. #ifdef TIOCGETC
  239. case TIOCGETC:
  240. case TIOCSETC:
  241. #endif
  242. #ifdef TIOCGLTC
  243. case TIOCGLTC:
  244. case TIOCSLTC:
  245. #endif
  246. case TCGETS:
  247. case TCSETSF:
  248. case TCSETSW:
  249. case TCSETS:
  250. case TCGETA:
  251. case TCSETAF:
  252. case TCSETAW:
  253. case TCSETA:
  254. case TCXONC:
  255. case TCFLSH:
  256. case TIOCOUTQ:
  257. case TIOCINQ:
  258. case TIOCGLCKTRMIOS:
  259. case TIOCSLCKTRMIOS:
  260. case TIOCPKT:
  261. case TIOCGSOFTCAR:
  262. case TIOCSSOFTCAR:
  263. return -ENOIOCTLCMD;
  264. #if 0
  265. case TCwhatever:
  266. /* do something */
  267. break;
  268. #endif
  269. default:
  270. for (i = 0; i < ARRAY_SIZE(tty_ioctls); i++)
  271. if (cmd == tty_ioctls[i].cmd)
  272. break;
  273. if (i == ARRAY_SIZE(tty_ioctls)) {
  274. printk(KERN_ERR "%s: %s: unknown ioctl: 0x%x\n",
  275. __FUNCTION__, tty->name, cmd);
  276. }
  277. ret = -ENOIOCTLCMD;
  278. break;
  279. }
  280. return ret;
  281. }
  282. void line_throttle(struct tty_struct *tty)
  283. {
  284. struct line *line = tty->driver_data;
  285. deactivate_chan(&line->chan_list, line->driver->read_irq);
  286. line->throttled = 1;
  287. }
  288. void line_unthrottle(struct tty_struct *tty)
  289. {
  290. struct line *line = tty->driver_data;
  291. line->throttled = 0;
  292. chan_interrupt(&line->chan_list, &line->task, tty,
  293. line->driver->read_irq);
  294. /*
  295. * Maybe there is enough stuff pending that calling the interrupt
  296. * throttles us again. In this case, line->throttled will be 1
  297. * again and we shouldn't turn the interrupt back on.
  298. */
  299. if (!line->throttled)
  300. reactivate_chan(&line->chan_list, line->driver->read_irq);
  301. }
  302. static irqreturn_t line_write_interrupt(int irq, void *data)
  303. {
  304. struct chan *chan = data;
  305. struct line *line = chan->line;
  306. struct tty_struct *tty = line->tty;
  307. int err;
  308. /*
  309. * Interrupts are disabled here because we registered the interrupt with
  310. * IRQF_DISABLED (see line_setup_irq).
  311. */
  312. spin_lock(&line->lock);
  313. err = flush_buffer(line);
  314. if (err == 0) {
  315. return IRQ_NONE;
  316. } else if (err < 0) {
  317. line->head = line->buffer;
  318. line->tail = line->buffer;
  319. }
  320. spin_unlock(&line->lock);
  321. if (tty == NULL)
  322. return IRQ_NONE;
  323. if (test_bit(TTY_DO_WRITE_WAKEUP, &tty->flags) &&
  324. (tty->ldisc.write_wakeup != NULL))
  325. (tty->ldisc.write_wakeup)(tty);
  326. /*
  327. * BLOCKING mode
  328. * In blocking mode, everything sleeps on tty->write_wait.
  329. * Sleeping in the console driver would break non-blocking
  330. * writes.
  331. */
  332. if (waitqueue_active(&tty->write_wait))
  333. wake_up_interruptible(&tty->write_wait);
  334. return IRQ_HANDLED;
  335. }
  336. int line_setup_irq(int fd, int input, int output, struct line *line, void *data)
  337. {
  338. const struct line_driver *driver = line->driver;
  339. int err = 0, flags = IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM;
  340. if (input)
  341. err = um_request_irq(driver->read_irq, fd, IRQ_READ,
  342. line_interrupt, flags,
  343. driver->read_irq_name, data);
  344. if (err)
  345. return err;
  346. if (output)
  347. err = um_request_irq(driver->write_irq, fd, IRQ_WRITE,
  348. line_write_interrupt, flags,
  349. driver->write_irq_name, data);
  350. line->have_irq = 1;
  351. return err;
  352. }
  353. /*
  354. * Normally, a driver like this can rely mostly on the tty layer
  355. * locking, particularly when it comes to the driver structure.
  356. * However, in this case, mconsole requests can come in "from the
  357. * side", and race with opens and closes.
  358. *
  359. * mconsole config requests will want to be sure the device isn't in
  360. * use, and get_config, open, and close will want a stable
  361. * configuration. The checking and modification of the configuration
  362. * is done under a spinlock. Checking whether the device is in use is
  363. * line->tty->count > 1, also under the spinlock.
  364. *
  365. * tty->count serves to decide whether the device should be enabled or
  366. * disabled on the host. If it's equal to 1, then we are doing the
  367. * first open or last close. Otherwise, open and close just return.
  368. */
  369. int line_open(struct line *lines, struct tty_struct *tty)
  370. {
  371. struct line *line = &lines[tty->index];
  372. int err = -ENODEV;
  373. spin_lock(&line->count_lock);
  374. if (!line->valid)
  375. goto out_unlock;
  376. err = 0;
  377. if (tty->count > 1)
  378. goto out_unlock;
  379. spin_unlock(&line->count_lock);
  380. tty->driver_data = line;
  381. line->tty = tty;
  382. err = enable_chan(line);
  383. if (err)
  384. return err;
  385. INIT_DELAYED_WORK(&line->task, line_timer_cb);
  386. if (!line->sigio) {
  387. chan_enable_winch(&line->chan_list, tty);
  388. line->sigio = 1;
  389. }
  390. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  391. &tty->winsize.ws_col);
  392. return err;
  393. out_unlock:
  394. spin_unlock(&line->count_lock);
  395. return err;
  396. }
  397. static void unregister_winch(struct tty_struct *tty);
  398. void line_close(struct tty_struct *tty, struct file * filp)
  399. {
  400. struct line *line = tty->driver_data;
  401. /*
  402. * If line_open fails (and tty->driver_data is never set),
  403. * tty_open will call line_close. So just return in this case.
  404. */
  405. if (line == NULL)
  406. return;
  407. /* We ignore the error anyway! */
  408. flush_buffer(line);
  409. spin_lock(&line->count_lock);
  410. if (!line->valid)
  411. goto out_unlock;
  412. if (tty->count > 1)
  413. goto out_unlock;
  414. spin_unlock(&line->count_lock);
  415. line->tty = NULL;
  416. tty->driver_data = NULL;
  417. if (line->sigio) {
  418. unregister_winch(tty);
  419. line->sigio = 0;
  420. }
  421. return;
  422. out_unlock:
  423. spin_unlock(&line->count_lock);
  424. }
  425. void close_lines(struct line *lines, int nlines)
  426. {
  427. int i;
  428. for(i = 0; i < nlines; i++)
  429. close_chan(&lines[i].chan_list, 0);
  430. }
  431. static int setup_one_line(struct line *lines, int n, char *init, int init_prio,
  432. char **error_out)
  433. {
  434. struct line *line = &lines[n];
  435. int err = -EINVAL;
  436. spin_lock(&line->count_lock);
  437. if (line->tty != NULL) {
  438. *error_out = "Device is already open";
  439. goto out;
  440. }
  441. if (line->init_pri <= init_prio) {
  442. line->init_pri = init_prio;
  443. if (!strcmp(init, "none"))
  444. line->valid = 0;
  445. else {
  446. line->init_str = init;
  447. line->valid = 1;
  448. }
  449. }
  450. err = 0;
  451. out:
  452. spin_unlock(&line->count_lock);
  453. return err;
  454. }
  455. /*
  456. * Common setup code for both startup command line and mconsole initialization.
  457. * @lines contains the array (of size @num) to modify;
  458. * @init is the setup string;
  459. * @error_out is an error string in the case of failure;
  460. */
  461. int line_setup(struct line *lines, unsigned int num, char *init,
  462. char **error_out)
  463. {
  464. int i, n, err;
  465. char *end;
  466. if (*init == '=') {
  467. /*
  468. * We said con=/ssl= instead of con#=, so we are configuring all
  469. * consoles at once.
  470. */
  471. n = -1;
  472. }
  473. else {
  474. n = simple_strtoul(init, &end, 0);
  475. if (*end != '=') {
  476. *error_out = "Couldn't parse device number";
  477. return -EINVAL;
  478. }
  479. init = end;
  480. }
  481. init++;
  482. if (n >= (signed int) num) {
  483. *error_out = "Device number out of range";
  484. return -EINVAL;
  485. }
  486. else if (n >= 0) {
  487. err = setup_one_line(lines, n, init, INIT_ONE, error_out);
  488. if (err)
  489. return err;
  490. }
  491. else {
  492. for(i = 0; i < num; i++) {
  493. err = setup_one_line(lines, i, init, INIT_ALL,
  494. error_out);
  495. if (err)
  496. return err;
  497. }
  498. }
  499. return n == -1 ? num : n;
  500. }
  501. int line_config(struct line *lines, unsigned int num, char *str,
  502. const struct chan_opts *opts, char **error_out)
  503. {
  504. struct line *line;
  505. char *new;
  506. int n;
  507. if (*str == '=') {
  508. *error_out = "Can't configure all devices from mconsole";
  509. return -EINVAL;
  510. }
  511. new = kstrdup(str, GFP_KERNEL);
  512. if (new == NULL) {
  513. *error_out = "Failed to allocate memory";
  514. return -ENOMEM;
  515. }
  516. n = line_setup(lines, num, new, error_out);
  517. if (n < 0)
  518. return n;
  519. line = &lines[n];
  520. return parse_chan_pair(line->init_str, line, n, opts, error_out);
  521. }
  522. int line_get_config(char *name, struct line *lines, unsigned int num, char *str,
  523. int size, char **error_out)
  524. {
  525. struct line *line;
  526. char *end;
  527. int dev, n = 0;
  528. dev = simple_strtoul(name, &end, 0);
  529. if ((*end != '\0') || (end == name)) {
  530. *error_out = "line_get_config failed to parse device number";
  531. return 0;
  532. }
  533. if ((dev < 0) || (dev >= num)) {
  534. *error_out = "device number out of range";
  535. return 0;
  536. }
  537. line = &lines[dev];
  538. spin_lock(&line->count_lock);
  539. if (!line->valid)
  540. CONFIG_CHUNK(str, size, n, "none", 1);
  541. else if (line->tty == NULL)
  542. CONFIG_CHUNK(str, size, n, line->init_str, 1);
  543. else n = chan_config_string(&line->chan_list, str, size, error_out);
  544. spin_unlock(&line->count_lock);
  545. return n;
  546. }
  547. int line_id(char **str, int *start_out, int *end_out)
  548. {
  549. char *end;
  550. int n;
  551. n = simple_strtoul(*str, &end, 0);
  552. if ((*end != '\0') || (end == *str))
  553. return -1;
  554. *str = end;
  555. *start_out = n;
  556. *end_out = n;
  557. return n;
  558. }
  559. int line_remove(struct line *lines, unsigned int num, int n, char **error_out)
  560. {
  561. int err;
  562. char config[sizeof("conxxxx=none\0")];
  563. sprintf(config, "%d=none", n);
  564. err = line_setup(lines, num, config, error_out);
  565. if (err >= 0)
  566. err = 0;
  567. return err;
  568. }
  569. struct tty_driver *register_lines(struct line_driver *line_driver,
  570. const struct tty_operations *ops,
  571. struct line *lines, int nlines)
  572. {
  573. int i;
  574. struct tty_driver *driver = alloc_tty_driver(nlines);
  575. if (!driver)
  576. return NULL;
  577. driver->driver_name = line_driver->name;
  578. driver->name = line_driver->device_name;
  579. driver->major = line_driver->major;
  580. driver->minor_start = line_driver->minor_start;
  581. driver->type = line_driver->type;
  582. driver->subtype = line_driver->subtype;
  583. driver->flags = TTY_DRIVER_REAL_RAW;
  584. driver->init_termios = tty_std_termios;
  585. tty_set_operations(driver, ops);
  586. if (tty_register_driver(driver)) {
  587. printk(KERN_ERR "register_lines : can't register %s driver\n",
  588. line_driver->name);
  589. put_tty_driver(driver);
  590. return NULL;
  591. }
  592. for(i = 0; i < nlines; i++) {
  593. if (!lines[i].valid)
  594. tty_unregister_device(driver, i);
  595. }
  596. mconsole_register_dev(&line_driver->mc);
  597. return driver;
  598. }
  599. static DEFINE_SPINLOCK(winch_handler_lock);
  600. static LIST_HEAD(winch_handlers);
  601. void lines_init(struct line *lines, int nlines, struct chan_opts *opts)
  602. {
  603. struct line *line;
  604. char *error;
  605. int i;
  606. for(i = 0; i < nlines; i++) {
  607. line = &lines[i];
  608. INIT_LIST_HEAD(&line->chan_list);
  609. if (line->init_str == NULL)
  610. continue;
  611. line->init_str = kstrdup(line->init_str, GFP_KERNEL);
  612. if (line->init_str == NULL)
  613. printk(KERN_ERR "lines_init - kstrdup returned NULL\n");
  614. if (parse_chan_pair(line->init_str, line, i, opts, &error)) {
  615. printk(KERN_ERR "parse_chan_pair failed for "
  616. "device %d : %s\n", i, error);
  617. line->valid = 0;
  618. }
  619. }
  620. }
  621. struct winch {
  622. struct list_head list;
  623. int fd;
  624. int tty_fd;
  625. int pid;
  626. struct tty_struct *tty;
  627. unsigned long stack;
  628. };
  629. static void free_winch(struct winch *winch, int free_irq_ok)
  630. {
  631. list_del(&winch->list);
  632. if (winch->pid != -1)
  633. os_kill_process(winch->pid, 1);
  634. if (winch->fd != -1)
  635. os_close_file(winch->fd);
  636. if (winch->stack != 0)
  637. free_stack(winch->stack, 0);
  638. if (free_irq_ok)
  639. free_irq(WINCH_IRQ, winch);
  640. kfree(winch);
  641. }
  642. static irqreturn_t winch_interrupt(int irq, void *data)
  643. {
  644. struct winch *winch = data;
  645. struct tty_struct *tty;
  646. struct line *line;
  647. int err;
  648. char c;
  649. if (winch->fd != -1) {
  650. err = generic_read(winch->fd, &c, NULL);
  651. if (err < 0) {
  652. if (err != -EAGAIN) {
  653. printk(KERN_ERR "winch_interrupt : "
  654. "read failed, errno = %d\n", -err);
  655. printk(KERN_ERR "fd %d is losing SIGWINCH "
  656. "support\n", winch->tty_fd);
  657. free_winch(winch, 0);
  658. return IRQ_HANDLED;
  659. }
  660. goto out;
  661. }
  662. }
  663. tty = winch->tty;
  664. if (tty != NULL) {
  665. line = tty->driver_data;
  666. chan_window_size(&line->chan_list, &tty->winsize.ws_row,
  667. &tty->winsize.ws_col);
  668. kill_pgrp(tty->pgrp, SIGWINCH, 1);
  669. }
  670. out:
  671. if (winch->fd != -1)
  672. reactivate_fd(winch->fd, WINCH_IRQ);
  673. return IRQ_HANDLED;
  674. }
  675. void register_winch_irq(int fd, int tty_fd, int pid, struct tty_struct *tty,
  676. unsigned long stack)
  677. {
  678. struct winch *winch;
  679. winch = kmalloc(sizeof(*winch), GFP_KERNEL);
  680. if (winch == NULL) {
  681. printk(KERN_ERR "register_winch_irq - kmalloc failed\n");
  682. goto cleanup;
  683. }
  684. *winch = ((struct winch) { .list = LIST_HEAD_INIT(winch->list),
  685. .fd = fd,
  686. .tty_fd = tty_fd,
  687. .pid = pid,
  688. .tty = tty,
  689. .stack = stack });
  690. if (um_request_irq(WINCH_IRQ, fd, IRQ_READ, winch_interrupt,
  691. IRQF_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  692. "winch", winch) < 0) {
  693. printk(KERN_ERR "register_winch_irq - failed to register "
  694. "IRQ\n");
  695. goto out_free;
  696. }
  697. spin_lock(&winch_handler_lock);
  698. list_add(&winch->list, &winch_handlers);
  699. spin_unlock(&winch_handler_lock);
  700. return;
  701. out_free:
  702. kfree(winch);
  703. cleanup:
  704. os_kill_process(pid, 1);
  705. os_close_file(fd);
  706. if (stack != 0)
  707. free_stack(stack, 0);
  708. }
  709. static void unregister_winch(struct tty_struct *tty)
  710. {
  711. struct list_head *ele;
  712. struct winch *winch;
  713. spin_lock(&winch_handler_lock);
  714. list_for_each(ele, &winch_handlers) {
  715. winch = list_entry(ele, struct winch, list);
  716. if (winch->tty == tty) {
  717. free_winch(winch, 1);
  718. break;
  719. }
  720. }
  721. spin_unlock(&winch_handler_lock);
  722. }
  723. static void winch_cleanup(void)
  724. {
  725. struct list_head *ele, *next;
  726. struct winch *winch;
  727. spin_lock(&winch_handler_lock);
  728. list_for_each_safe(ele, next, &winch_handlers) {
  729. winch = list_entry(ele, struct winch, list);
  730. free_winch(winch, 1);
  731. }
  732. spin_unlock(&winch_handler_lock);
  733. }
  734. __uml_exitcall(winch_cleanup);
  735. char *add_xterm_umid(char *base)
  736. {
  737. char *umid, *title;
  738. int len;
  739. umid = get_umid();
  740. if (*umid == '\0')
  741. return base;
  742. len = strlen(base) + strlen(" ()") + strlen(umid) + 1;
  743. title = kmalloc(len, GFP_KERNEL);
  744. if (title == NULL) {
  745. printk(KERN_ERR "Failed to allocate buffer for xterm title\n");
  746. return base;
  747. }
  748. snprintf(title, len, "%s (%s)", base, umid);
  749. return title;
  750. }