child.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Helper functions to sync execution between parent and child processes.
  4. *
  5. * Copyright 2018, Thiago Jung Bauermann, IBM Corporation.
  6. */
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include <semaphore.h>
  10. /*
  11. * Information in a shared memory location for synchronization between child and
  12. * parent.
  13. */
  14. struct child_sync {
  15. /* The parent waits on this semaphore. */
  16. sem_t sem_parent;
  17. /* If true, the child should give up as well. */
  18. bool parent_gave_up;
  19. /* The child waits on this semaphore. */
  20. sem_t sem_child;
  21. /* If true, the parent should give up as well. */
  22. bool child_gave_up;
  23. };
  24. #define CHILD_FAIL_IF(x, sync) \
  25. do { \
  26. if (x) { \
  27. fprintf(stderr, \
  28. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  29. (sync)->child_gave_up = true; \
  30. prod_parent(sync); \
  31. return 1; \
  32. } \
  33. } while (0)
  34. #define PARENT_FAIL_IF(x, sync) \
  35. do { \
  36. if (x) { \
  37. fprintf(stderr, \
  38. "[FAIL] Test FAILED on line %d\n", __LINE__); \
  39. (sync)->parent_gave_up = true; \
  40. prod_child(sync); \
  41. return 1; \
  42. } \
  43. } while (0)
  44. #define PARENT_SKIP_IF_UNSUPPORTED(x, sync) \
  45. do { \
  46. if ((x) == -1 && (errno == ENODEV || errno == EINVAL)) { \
  47. (sync)->parent_gave_up = true; \
  48. prod_child(sync); \
  49. SKIP_IF(1); \
  50. } \
  51. } while (0)
  52. int init_child_sync(struct child_sync *sync)
  53. {
  54. int ret;
  55. ret = sem_init(&sync->sem_parent, 1, 0);
  56. if (ret) {
  57. perror("Semaphore initialization failed");
  58. return 1;
  59. }
  60. ret = sem_init(&sync->sem_child, 1, 0);
  61. if (ret) {
  62. perror("Semaphore initialization failed");
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. void destroy_child_sync(struct child_sync *sync)
  68. {
  69. sem_destroy(&sync->sem_parent);
  70. sem_destroy(&sync->sem_child);
  71. }
  72. int wait_child(struct child_sync *sync)
  73. {
  74. int ret;
  75. /* Wait until the child prods us. */
  76. ret = sem_wait(&sync->sem_parent);
  77. if (ret) {
  78. perror("Error waiting for child");
  79. return 1;
  80. }
  81. return sync->child_gave_up;
  82. }
  83. int prod_child(struct child_sync *sync)
  84. {
  85. int ret;
  86. /* Unblock the child now. */
  87. ret = sem_post(&sync->sem_child);
  88. if (ret) {
  89. perror("Error prodding child");
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. int wait_parent(struct child_sync *sync)
  95. {
  96. int ret;
  97. /* Wait until the parent prods us. */
  98. ret = sem_wait(&sync->sem_child);
  99. if (ret) {
  100. perror("Error waiting for parent");
  101. return 1;
  102. }
  103. return sync->parent_gave_up;
  104. }
  105. int prod_parent(struct child_sync *sync)
  106. {
  107. int ret;
  108. /* Unblock the parent now. */
  109. ret = sem_post(&sync->sem_parent);
  110. if (ret) {
  111. perror("Error prodding parent");
  112. return 1;
  113. }
  114. return 0;
  115. }