xterm_kern.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_DISABLED | IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  44. "xterm", data);
  45. if (err) {
  46. printk(KERN_ERR "xterm_fd : failed to get IRQ for xterm, "
  47. "err = %d\n", err);
  48. ret = err;
  49. goto out;
  50. }
  51. /* ... so here we wait for an xterm interrupt.
  52. *
  53. * XXX Note, if the xterm doesn't work for some reason (eg. DISPLAY
  54. * isn't set) this will hang... */
  55. wait_for_completion(&data->ready);
  56. free_irq(XTERM_IRQ, data);
  57. ret = data->new_fd;
  58. *pid_out = data->pid;
  59. out:
  60. kfree(data);
  61. return ret;
  62. }