control.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Control socket for client/server test execution
  3. *
  4. * Copyright (C) 2017 Red Hat, Inc.
  5. *
  6. * Author: Stefan Hajnoczi <stefanha@redhat.com>
  7. */
  8. /* The client and server may need to coordinate to avoid race conditions like
  9. * the client attempting to connect to a socket that the server is not
  10. * listening on yet. The control socket offers a communications channel for
  11. * such coordination tasks.
  12. *
  13. * If the client calls control_expectln("LISTENING"), then it will block until
  14. * the server calls control_writeln("LISTENING"). This provides a simple
  15. * mechanism for coordinating between the client and the server.
  16. */
  17. #include <errno.h>
  18. #include <netdb.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include "timeout.h"
  26. #include "control.h"
  27. #include "util.h"
  28. static int control_fd = -1;
  29. /* Open the control socket, either in server or client mode */
  30. void control_init(const char *control_host,
  31. const char *control_port,
  32. bool server)
  33. {
  34. struct addrinfo hints = {
  35. .ai_socktype = SOCK_STREAM,
  36. };
  37. struct addrinfo *result = NULL;
  38. struct addrinfo *ai;
  39. int ret;
  40. ret = getaddrinfo(control_host, control_port, &hints, &result);
  41. if (ret != 0) {
  42. fprintf(stderr, "%s\n", gai_strerror(ret));
  43. exit(EXIT_FAILURE);
  44. }
  45. for (ai = result; ai; ai = ai->ai_next) {
  46. int fd;
  47. fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
  48. if (fd < 0)
  49. continue;
  50. if (!server) {
  51. if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0)
  52. goto next;
  53. control_fd = fd;
  54. printf("Control socket connected to %s:%s.\n",
  55. control_host, control_port);
  56. break;
  57. }
  58. setsockopt_int_check(fd, SOL_SOCKET, SO_REUSEADDR, 1,
  59. "setsockopt SO_REUSEADDR");
  60. if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
  61. goto next;
  62. if (listen(fd, 1) < 0)
  63. goto next;
  64. printf("Control socket listening on %s:%s\n",
  65. control_host, control_port);
  66. fflush(stdout);
  67. control_fd = accept(fd, NULL, 0);
  68. close(fd);
  69. if (control_fd < 0) {
  70. perror("accept");
  71. exit(EXIT_FAILURE);
  72. }
  73. printf("Control socket connection accepted...\n");
  74. break;
  75. next:
  76. close(fd);
  77. }
  78. if (control_fd < 0) {
  79. fprintf(stderr, "Control socket initialization failed. Invalid address %s:%s?\n",
  80. control_host, control_port);
  81. exit(EXIT_FAILURE);
  82. }
  83. freeaddrinfo(result);
  84. }
  85. /* Free resources */
  86. void control_cleanup(void)
  87. {
  88. close(control_fd);
  89. control_fd = -1;
  90. }
  91. /* Write a line to the control socket */
  92. void control_writeln(const char *str)
  93. {
  94. ssize_t len = strlen(str);
  95. ssize_t ret;
  96. timeout_begin(TIMEOUT);
  97. do {
  98. ret = send(control_fd, str, len, MSG_MORE);
  99. timeout_check("send");
  100. } while (ret < 0 && errno == EINTR);
  101. if (ret != len) {
  102. perror("send");
  103. exit(EXIT_FAILURE);
  104. }
  105. do {
  106. ret = send(control_fd, "\n", 1, 0);
  107. timeout_check("send");
  108. } while (ret < 0 && errno == EINTR);
  109. if (ret != 1) {
  110. perror("send");
  111. exit(EXIT_FAILURE);
  112. }
  113. timeout_end();
  114. }
  115. void control_writeulong(unsigned long value)
  116. {
  117. char str[32];
  118. if (snprintf(str, sizeof(str), "%lu", value) >= sizeof(str)) {
  119. perror("snprintf");
  120. exit(EXIT_FAILURE);
  121. }
  122. control_writeln(str);
  123. }
  124. unsigned long control_readulong(void)
  125. {
  126. unsigned long value;
  127. char *str;
  128. str = control_readln();
  129. if (!str)
  130. exit(EXIT_FAILURE);
  131. value = strtoul(str, NULL, 10);
  132. free(str);
  133. return value;
  134. }
  135. /* Return the next line from the control socket (without the trailing newline).
  136. *
  137. * The program terminates if a timeout occurs.
  138. *
  139. * The caller must free() the returned string.
  140. */
  141. char *control_readln(void)
  142. {
  143. char *buf = NULL;
  144. size_t idx = 0;
  145. size_t buflen = 0;
  146. timeout_begin(TIMEOUT);
  147. for (;;) {
  148. ssize_t ret;
  149. if (idx >= buflen) {
  150. char *new_buf;
  151. new_buf = realloc(buf, buflen + 80);
  152. if (!new_buf) {
  153. perror("realloc");
  154. exit(EXIT_FAILURE);
  155. }
  156. buf = new_buf;
  157. buflen += 80;
  158. }
  159. do {
  160. ret = recv(control_fd, &buf[idx], 1, 0);
  161. timeout_check("recv");
  162. } while (ret < 0 && errno == EINTR);
  163. if (ret == 0) {
  164. fprintf(stderr, "unexpected EOF on control socket\n");
  165. exit(EXIT_FAILURE);
  166. }
  167. if (ret != 1) {
  168. perror("recv");
  169. exit(EXIT_FAILURE);
  170. }
  171. if (buf[idx] == '\n') {
  172. buf[idx] = '\0';
  173. break;
  174. }
  175. idx++;
  176. }
  177. timeout_end();
  178. return buf;
  179. }
  180. /* Wait until a given line is received or a timeout occurs */
  181. void control_expectln(const char *str)
  182. {
  183. char *line;
  184. line = control_readln();
  185. control_cmpln(line, str, true);
  186. free(line);
  187. }
  188. bool control_cmpln(char *line, const char *str, bool fail)
  189. {
  190. if (strcmp(str, line) == 0)
  191. return true;
  192. if (fail) {
  193. fprintf(stderr, "expected \"%s\" on control socket, got \"%s\"\n",
  194. str, line);
  195. exit(EXIT_FAILURE);
  196. }
  197. return false;
  198. }