compatfd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-common.h"
  16. #include "compatfd.h"
  17. #include <sys/syscall.h>
  18. #include <pthread.h>
  19. struct sigfd_compat_info
  20. {
  21. sigset_t mask;
  22. int fd;
  23. };
  24. static void *sigwait_compat(void *opaque)
  25. {
  26. struct sigfd_compat_info *info = opaque;
  27. sigset_t all;
  28. sigfillset(&all);
  29. pthread_sigmask(SIG_BLOCK, &all, NULL);
  30. while (1) {
  31. int sig;
  32. int err;
  33. err = sigwait(&info->mask, &sig);
  34. if (err != 0) {
  35. if (errno == EINTR) {
  36. continue;
  37. } else {
  38. return NULL;
  39. }
  40. } else {
  41. struct qemu_signalfd_siginfo buffer;
  42. size_t offset = 0;
  43. memset(&buffer, 0, sizeof(buffer));
  44. buffer.ssi_signo = sig;
  45. while (offset < sizeof(buffer)) {
  46. ssize_t len;
  47. len = write(info->fd, (char *)&buffer + offset,
  48. sizeof(buffer) - offset);
  49. if (len == -1 && errno == EINTR)
  50. continue;
  51. if (len <= 0) {
  52. return NULL;
  53. }
  54. offset += len;
  55. }
  56. }
  57. }
  58. }
  59. static int qemu_signalfd_compat(const sigset_t *mask)
  60. {
  61. pthread_attr_t attr;
  62. pthread_t tid;
  63. struct sigfd_compat_info *info;
  64. int fds[2];
  65. info = malloc(sizeof(*info));
  66. if (info == NULL) {
  67. errno = ENOMEM;
  68. return -1;
  69. }
  70. if (pipe(fds) == -1) {
  71. free(info);
  72. return -1;
  73. }
  74. qemu_set_cloexec(fds[0]);
  75. qemu_set_cloexec(fds[1]);
  76. memcpy(&info->mask, mask, sizeof(*mask));
  77. info->fd = fds[1];
  78. pthread_attr_init(&attr);
  79. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  80. pthread_create(&tid, &attr, sigwait_compat, info);
  81. pthread_attr_destroy(&attr);
  82. return fds[0];
  83. }
  84. int qemu_signalfd(const sigset_t *mask)
  85. {
  86. #if defined(CONFIG_SIGNALFD)
  87. int ret;
  88. ret = syscall(SYS_signalfd, -1, mask, _NSIG / 8);
  89. if (ret != -1) {
  90. qemu_set_cloexec(ret);
  91. return ret;
  92. }
  93. #endif
  94. return qemu_signalfd_compat(mask);
  95. }
  96. bool qemu_signalfd_available(void)
  97. {
  98. #ifdef CONFIG_SIGNALFD
  99. sigset_t mask;
  100. int fd;
  101. bool ok;
  102. sigemptyset(&mask);
  103. errno = 0;
  104. fd = syscall(SYS_signalfd, -1, &mask, _NSIG / 8);
  105. ok = (errno != ENOSYS);
  106. if (fd >= 0) {
  107. close(fd);
  108. }
  109. return ok;
  110. #else
  111. return false;
  112. #endif
  113. }