control.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <[email protected]>
  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. static int control_fd = -1;
  28. /* Open the control socket, either in server or client mode */
  29. void control_init(const char *control_host,
  30. const char *control_port,
  31. bool server)
  32. {
  33. struct addrinfo hints = {
  34. .ai_socktype = SOCK_STREAM,
  35. };
  36. struct addrinfo *result = NULL;
  37. struct addrinfo *ai;
  38. int ret;
  39. ret = getaddrinfo(control_host, control_port, &hints, &result);
  40. if (ret != 0) {
  41. fprintf(stderr, "%s\n", gai_strerror(ret));
  42. exit(EXIT_FAILURE);
  43. }
  44. for (ai = result; ai; ai = ai->ai_next) {
  45. int fd;
  46. int val = 1;
  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. if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
  59. &val, sizeof(val)) < 0) {
  60. perror("setsockopt");
  61. exit(EXIT_FAILURE);
  62. }
  63. if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
  64. goto next;
  65. if (listen(fd, 1) < 0)
  66. goto next;
  67. printf("Control socket listening on %s:%s\n",
  68. control_host, control_port);
  69. fflush(stdout);
  70. control_fd = accept(fd, NULL, 0);
  71. close(fd);
  72. if (control_fd < 0) {
  73. perror("accept");
  74. exit(EXIT_FAILURE);
  75. }
  76. printf("Control socket connection accepted...\n");
  77. break;
  78. next:
  79. close(fd);
  80. }
  81. if (control_fd < 0) {
  82. fprintf(stderr, "Control socket initialization failed. Invalid address %s:%s?\n",
  83. control_host, control_port);
  84. exit(EXIT_FAILURE);
  85. }
  86. freeaddrinfo(result);
  87. }
  88. /* Free resources */
  89. void control_cleanup(void)
  90. {
  91. close(control_fd);
  92. control_fd = -1;
  93. }
  94. /* Write a line to the control socket */
  95. void control_writeln(const char *str)
  96. {
  97. ssize_t len = strlen(str);
  98. ssize_t ret;
  99. timeout_begin(TIMEOUT);
  100. do {
  101. ret = send(control_fd, str, len, MSG_MORE);
  102. timeout_check("send");
  103. } while (ret < 0 && errno == EINTR);
  104. if (ret != len) {
  105. perror("send");
  106. exit(EXIT_FAILURE);
  107. }
  108. do {
  109. ret = send(control_fd, "\n", 1, 0);
  110. timeout_check("send");
  111. } while (ret < 0 && errno == EINTR);
  112. if (ret != 1) {
  113. perror("send");
  114. exit(EXIT_FAILURE);
  115. }
  116. timeout_end();
  117. }
  118. /* Return the next line from the control socket (without the trailing newline).
  119. *
  120. * The program terminates if a timeout occurs.
  121. *
  122. * The caller must free() the returned string.
  123. */
  124. char *control_readln(void)
  125. {
  126. char *buf = NULL;
  127. size_t idx = 0;
  128. size_t buflen = 0;
  129. timeout_begin(TIMEOUT);
  130. for (;;) {
  131. ssize_t ret;
  132. if (idx >= buflen) {
  133. char *new_buf;
  134. new_buf = realloc(buf, buflen + 80);
  135. if (!new_buf) {
  136. perror("realloc");
  137. exit(EXIT_FAILURE);
  138. }
  139. buf = new_buf;
  140. buflen += 80;
  141. }
  142. do {
  143. ret = recv(control_fd, &buf[idx], 1, 0);
  144. timeout_check("recv");
  145. } while (ret < 0 && errno == EINTR);
  146. if (ret == 0) {
  147. fprintf(stderr, "unexpected EOF on control socket\n");
  148. exit(EXIT_FAILURE);
  149. }
  150. if (ret != 1) {
  151. perror("recv");
  152. exit(EXIT_FAILURE);
  153. }
  154. if (buf[idx] == '\n') {
  155. buf[idx] = '\0';
  156. break;
  157. }
  158. idx++;
  159. }
  160. timeout_end();
  161. return buf;
  162. }
  163. /* Wait until a given line is received or a timeout occurs */
  164. void control_expectln(const char *str)
  165. {
  166. char *line;
  167. line = control_readln();
  168. control_cmpln(line, str, true);
  169. free(line);
  170. }
  171. bool control_cmpln(char *line, const char *str, bool fail)
  172. {
  173. if (strcmp(str, line) == 0)
  174. return true;
  175. if (fail) {
  176. fprintf(stderr, "expected \"%s\" on control socket, got \"%s\"\n",
  177. str, line);
  178. exit(EXIT_FAILURE);
  179. }
  180. return false;
  181. }