xterm_kern.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/completion.h>
  7. #include <linux/irqreturn.h>
  8. #include <asm/irq.h>
  9. #include <irq_kern.h>
  10. #include <os.h>
  11. struct xterm_wait {
  12. struct completion ready;
  13. int fd;
  14. int pid;
  15. int new_fd;
  16. };
  17. static irqreturn_t xterm_interrupt(int irq, void *data)
  18. {
  19. struct xterm_wait *xterm = data;
  20. int fd;
  21. fd = os_rcv_fd(xterm->fd, &xterm->pid);
  22. if (fd == -EAGAIN)
  23. return IRQ_NONE;
  24. xterm->new_fd = fd;
  25. complete(&xterm->ready);
  26. return IRQ_HANDLED;
  27. }
  28. int xterm_fd(int socket, int *pid_out)
  29. {
  30. struct xterm_wait *data;
  31. int err, ret;
  32. data = kmalloc(sizeof(*data), GFP_KERNEL);
  33. if (data == NULL) {
  34. printk(KERN_ERR "xterm_fd : failed to allocate xterm_wait\n");
  35. return -ENOMEM;
  36. }
  37. /* This is a locked semaphore... */
  38. *data = ((struct xterm_wait) { .fd = socket,
  39. .pid = -1,
  40. .new_fd = -1 });
  41. init_completion(&data->ready);
  42. err = um_request_irq(XTERM_IRQ, socket, IRQ_READ, xterm_interrupt,
  43. IRQF_SHARED, "xterm", data);
  44. if (err) {
  45. printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
  46. "err = %d\n", err);
  47. ret = err;
  48. goto out;
  49. }
  50. /* ... so here we wait for an xterm interrupt.
  51. *
  52. * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
  53. * isn't set) this will hang... */
  54. wait_for_completion(&data->ready);
  55. um_free_irq(XTERM_IRQ, data);
  56. ret = data->new_fd;
  57. *pid_out = data->pid;
  58. out:
  59. kfree(data);
  60. return ret;
  61. }