control.c 4.3 KB

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