eventfd.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <errno.h>
  2. #include <poll.h>
  3. #include <pthread.h>
  4. #include <signal.h>
  5. #include <stdarg.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/eventfd.h>
  11. #include <sys/time.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <unistd.h>
  15. #define MAX_EFDS 3
  16. #define CLOSE_EFD_AND_EXIT(efd) \
  17. do { \
  18. close(efd); \
  19. return 1; \
  20. } while (0)
  21. #define EXIT_IF_ERROR(efd, bytes, prefix) \
  22. do { \
  23. if ((bytes) != sizeof(uint64_t)) { \
  24. perror(prefix); \
  25. printf("error at line-%d\n", __LINE__); \
  26. CLOSE_EFD_AND_EXIT(efd); \
  27. } \
  28. } while (0)
  29. int efds[MAX_EFDS] = {0};
  30. void* write_eventfd_thread(void* arg) {
  31. uint64_t count = 10;
  32. int* efds = (int*)arg;
  33. if (!arg) {
  34. printf("arg is NULL\n");
  35. return NULL;
  36. }
  37. printf("%s:got here\n", __func__);
  38. for (int i = 0; i < MAX_EFDS; i++) {
  39. printf("%s: efd = %d\n", __func__, efds[i]);
  40. }
  41. for (int i = 0; i < MAX_EFDS; i++) {
  42. sleep(1);
  43. write(efds[i], &count, sizeof(count));
  44. count += 1;
  45. }
  46. return NULL;
  47. }
  48. /* This function used to test polling on a group of eventfd descriptors.
  49. * To support regression testing, positive value returned for error case. */
  50. int eventfd_using_poll() {
  51. int ret = 0;
  52. struct pollfd pollfds[MAX_EFDS];
  53. pthread_t tid = 0;
  54. uint64_t count = 0;
  55. int poll_ret = 0;
  56. int nread_events = 0;
  57. for (int i = 0; i < MAX_EFDS; i++) {
  58. efds[i] = eventfd(0, 0);
  59. if (efds[i] < 0) {
  60. perror("eventfd failed");
  61. return 1;
  62. }
  63. printf("efd = %d\n", efds[i]);
  64. pollfds[i].fd = efds[i];
  65. pollfds[i].events = POLLIN;
  66. }
  67. ret = pthread_create(&tid, NULL, write_eventfd_thread, efds);
  68. if (ret != 0) {
  69. perror("error in thread creation\n");
  70. ret = 1;
  71. goto out;
  72. }
  73. while (1) {
  74. poll_ret = poll(pollfds, MAX_EFDS, 5000);
  75. if (poll_ret == 0) {
  76. printf("Poll timed out. Exiting.\n");
  77. break;
  78. }
  79. if (poll_ret < 0) {
  80. perror("error from poll");
  81. ret = 1;
  82. break;
  83. }
  84. for (int i = 0; i < MAX_EFDS; i++) {
  85. if (pollfds[i].revents & POLLIN) {
  86. pollfds[i].revents = 0;
  87. errno = 0;
  88. read(pollfds[i].fd, &count, sizeof(count));
  89. printf("fd set=%d\n", pollfds[i].fd);
  90. printf("efd = %d, count: %lu, errno=%d\n", pollfds[i].fd, count, errno);
  91. nread_events++;
  92. }
  93. }
  94. }
  95. if (nread_events == MAX_EFDS)
  96. printf("%s completed successfully\n", __func__);
  97. else
  98. printf("%s: nread_events=%d, MAX_EFDS=%d\n", __func__, nread_events, MAX_EFDS);
  99. pthread_join(tid, NULL);
  100. out:
  101. for (int i = 0; i < MAX_EFDS; i++) {
  102. close(efds[i]);
  103. }
  104. return ret;
  105. }
  106. /* This function used to test various flags supported while creating eventfd
  107. * descriptors.
  108. * To support regression testing, positive value returned for error case. */
  109. int eventfd_using_various_flags() {
  110. uint64_t count = 0;
  111. int efd = 0;
  112. ssize_t bytes = 0;
  113. int eventfd_flags[] = {0, EFD_SEMAPHORE, EFD_NONBLOCK, EFD_CLOEXEC};
  114. for (int i = 0; i < sizeof(eventfd_flags) / sizeof(int); i++) {
  115. printf("iteration #-%d, flags=%d\n", i, eventfd_flags[i]);
  116. efd = eventfd(0, eventfd_flags[i]);
  117. if (efd < 0) {
  118. perror("eventfd failed");
  119. printf("eventfd error for iteration #-%d, flags-%d\n", i, eventfd_flags[i]);
  120. return 1;
  121. }
  122. count = 5;
  123. bytes = write(efd, &count, sizeof(count));
  124. EXIT_IF_ERROR(efd, bytes, "write");
  125. bytes = write(efd, &count, sizeof(count));
  126. EXIT_IF_ERROR(efd, bytes, "write");
  127. count = 0;
  128. errno = 0;
  129. if (eventfd_flags[i] & EFD_SEMAPHORE) {
  130. uint64_t prev_count = 0;
  131. bytes = read(efd, &prev_count, sizeof(prev_count));
  132. EXIT_IF_ERROR(efd, bytes, "read");
  133. bytes = read(efd, &count, sizeof(count));
  134. EXIT_IF_ERROR(efd, bytes, "read");
  135. if (prev_count != 1 || count != 1) {
  136. printf("flag->EFD_SEMAPHORE, error, prev_count=%lu, new count=%lu\n", prev_count,
  137. count);
  138. close(efd);
  139. return 1;
  140. }
  141. continue;
  142. }
  143. count = 0;
  144. errno = 0;
  145. bytes = read(efd, &count, sizeof(count));
  146. EXIT_IF_ERROR(efd, bytes, "read");
  147. if (count != 10) {
  148. printf("%d: efd = %d, count: %lu, errno=%d\n", __LINE__, efd, count, errno);
  149. CLOSE_EFD_AND_EXIT(efd);
  150. }
  151. /* calling the second read would block if flags doesn't have EFD_NONBLOCK */
  152. if (eventfd_flags[i] & EFD_NONBLOCK) {
  153. count = 0;
  154. errno = 0;
  155. read(efd, &count, sizeof(count));
  156. printf("%d: efd = %d, count: %lu, errno=%d\n", __LINE__, efd, count, errno);
  157. }
  158. close(efd);
  159. }
  160. printf("%s completed successfully\n", __func__);
  161. return 0;
  162. }
  163. int eventfd_using_fork() {
  164. int status = 0;
  165. int efd = 0;
  166. uint64_t count = 0;
  167. efd = eventfd(0, EFD_NONBLOCK);
  168. if (efd < 0) {
  169. perror("eventfd failed");
  170. return 1;
  171. }
  172. pid_t pid = fork();
  173. if (pid == 0) {
  174. // child process
  175. count = 5;
  176. write(efd, &count, sizeof(count));
  177. exit(0);
  178. } else if (pid > 0) {
  179. // parent process
  180. waitpid(pid, &status, 0);
  181. if (WIFSIGNALED(status)) {
  182. perror("child was terminated by signal");
  183. CLOSE_EFD_AND_EXIT(efd);
  184. }
  185. count = 0;
  186. read(efd, &count, sizeof(count));
  187. if (count != 5) {
  188. printf("parent-pid=%d, efd = %d, count: %lu, errno=%d\n", getpid(), efd, count, errno);
  189. CLOSE_EFD_AND_EXIT(efd);
  190. }
  191. } else {
  192. perror("fork error");
  193. CLOSE_EFD_AND_EXIT(efd);
  194. }
  195. close(efd);
  196. printf("%s completed successfully\n", __func__);
  197. return 0;
  198. }
  199. int main(int argc, char* argv[]) {
  200. int ret = 0;
  201. ret = eventfd_using_poll();
  202. ret += eventfd_using_various_flags();
  203. ret += eventfd_using_fork();
  204. return ret;
  205. }