compatfd.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * signalfd/eventfd compatibility
  3. *
  4. * Copyright IBM, Corp. 2008
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "qemu/thread.h"
  17. #if defined(CONFIG_SIGNALFD)
  18. #include <sys/signalfd.h>
  19. #endif
  20. struct sigfd_compat_info {
  21. sigset_t mask;
  22. int fd;
  23. };
  24. static void *sigwait_compat(void *opaque)
  25. {
  26. struct sigfd_compat_info *info = opaque;
  27. while (1) {
  28. int sig;
  29. int err;
  30. err = sigwait(&info->mask, &sig);
  31. if (err != 0) {
  32. if (errno == EINTR) {
  33. continue;
  34. } else {
  35. return NULL;
  36. }
  37. } else {
  38. struct qemu_signalfd_siginfo buffer;
  39. memset(&buffer, 0, sizeof(buffer));
  40. buffer.ssi_signo = sig;
  41. if (qemu_write_full(info->fd, &buffer, sizeof(buffer)) != sizeof(buffer)) {
  42. return NULL;
  43. }
  44. }
  45. }
  46. }
  47. static int qemu_signalfd_compat(const sigset_t *mask)
  48. {
  49. struct sigfd_compat_info *info;
  50. QemuThread thread;
  51. int fds[2];
  52. info = g_malloc(sizeof(*info));
  53. if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
  54. g_free(info);
  55. return -1;
  56. }
  57. memcpy(&info->mask, mask, sizeof(*mask));
  58. info->fd = fds[1];
  59. qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info,
  60. QEMU_THREAD_DETACHED);
  61. return fds[0];
  62. }
  63. int qemu_signalfd(const sigset_t *mask)
  64. {
  65. #if defined(CONFIG_SIGNALFD)
  66. int ret;
  67. ret = signalfd(-1, mask, SFD_CLOEXEC);
  68. if (ret != -1) {
  69. return ret;
  70. }
  71. #endif
  72. return qemu_signalfd_compat(mask);
  73. }