tst-affinity-pid.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Test for sched_getaffinity and sched_setaffinity, PID version.
  2. Copyright (C) 2015-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* Function definitions for the benefit of tst-skeleton-affinity.c.
  16. This variant forks a child process which then invokes
  17. sched_getaffinity and sched_setaffinity on the parent PID. */
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <sched.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/wait.h>
  25. #include <unistd.h>
  26. static int
  27. write_fully (int fd, const void *buffer, size_t length)
  28. {
  29. const void *end = buffer + length;
  30. while (buffer < end)
  31. {
  32. ssize_t bytes_written = TEMP_FAILURE_RETRY
  33. (write (fd, buffer, end - buffer));
  34. if (bytes_written < 0)
  35. return -1;
  36. if (bytes_written == 0)
  37. {
  38. errno = ENOSPC;
  39. return -1;
  40. }
  41. buffer += bytes_written;
  42. }
  43. return 0;
  44. }
  45. static ssize_t
  46. read_fully (int fd, void *buffer, size_t length)
  47. {
  48. const void *start = buffer;
  49. const void *end = buffer + length;
  50. while (buffer < end)
  51. {
  52. ssize_t bytes_read = TEMP_FAILURE_RETRY
  53. (read (fd, buffer, end - buffer));
  54. if (bytes_read < 0)
  55. return -1;
  56. if (bytes_read == 0)
  57. return buffer - start;
  58. buffer += bytes_read;
  59. }
  60. return length;
  61. }
  62. static int
  63. process_child_response (int *pipes, pid_t child,
  64. cpu_set_t *set, size_t size)
  65. {
  66. close (pipes[1]);
  67. int value_from_child;
  68. ssize_t bytes_read = read_fully
  69. (pipes[0], &value_from_child, sizeof (value_from_child));
  70. if (bytes_read < 0)
  71. {
  72. printf ("error: read from child: %m\n");
  73. exit (1);
  74. }
  75. if (bytes_read != sizeof (value_from_child))
  76. {
  77. printf ("error: not enough bytes from child: %zd\n", bytes_read);
  78. exit (1);
  79. }
  80. if (value_from_child == 0)
  81. {
  82. bytes_read = read_fully (pipes[0], set, size);
  83. if (bytes_read < 0)
  84. {
  85. printf ("error: read: %m\n");
  86. exit (1);
  87. }
  88. if (bytes_read != size)
  89. {
  90. printf ("error: not enough bytes from child: %zd\n", bytes_read);
  91. exit (1);
  92. }
  93. }
  94. int status;
  95. if (waitpid (child, &status, 0) < 0)
  96. {
  97. printf ("error: waitpid: %m\n");
  98. exit (1);
  99. }
  100. if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
  101. {
  102. printf ("error: invalid status from : %m\n");
  103. exit (1);
  104. }
  105. close (pipes[0]);
  106. if (value_from_child != 0)
  107. {
  108. errno = value_from_child;
  109. return -1;
  110. }
  111. return 0;
  112. }
  113. static int
  114. getaffinity (size_t size, cpu_set_t *set)
  115. {
  116. int pipes[2];
  117. if (pipe (pipes) < 0)
  118. {
  119. printf ("error: pipe: %m\n");
  120. exit (1);
  121. }
  122. int ret = fork ();
  123. if (ret < 0)
  124. {
  125. printf ("error: fork: %m\n");
  126. exit (1);
  127. }
  128. if (ret == 0)
  129. {
  130. /* Child. */
  131. int ret = sched_getaffinity (getppid (), size, set);
  132. if (ret < 0)
  133. ret = errno;
  134. if (write_fully (pipes[1], &ret, sizeof (ret)) < 0
  135. || write_fully (pipes[1], set, size) < 0
  136. || (ret == 0 && write_fully (pipes[1], set, size) < 0))
  137. {
  138. printf ("error: write: %m\n");
  139. _exit (1);
  140. }
  141. _exit (0);
  142. }
  143. /* Parent. */
  144. return process_child_response (pipes, ret, set, size);
  145. }
  146. static int
  147. setaffinity (size_t size, const cpu_set_t *set)
  148. {
  149. int pipes[2];
  150. if (pipe (pipes) < 0)
  151. {
  152. printf ("error: pipe: %m\n");
  153. exit (1);
  154. }
  155. int ret = fork ();
  156. if (ret < 0)
  157. {
  158. printf ("error: fork: %m\n");
  159. exit (1);
  160. }
  161. if (ret == 0)
  162. {
  163. /* Child. */
  164. int ret = sched_setaffinity (getppid (), size, set);
  165. if (write_fully (pipes[1], &ret, sizeof (ret)) < 0)
  166. {
  167. printf ("error: write: %m\n");
  168. _exit (1);
  169. }
  170. _exit (0);
  171. }
  172. /* Parent. There is no affinity mask to read from the child, so the
  173. size is 0. */
  174. return process_child_response (pipes, ret, NULL, 0);
  175. }
  176. struct conf;
  177. static bool early_test (struct conf *unused)
  178. {
  179. return true;
  180. }
  181. #include "tst-skeleton-affinity.c"