line.c 19 KB

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