qed: Fix static checker warning
[linux/fpc-iii.git] / tools / testing / vsock / control.c
blob90fd47f0e4227d58391b576538960b9f3f1b85b4
1 /* Control socket for client/server test execution
3 * Copyright (C) 2017 Red Hat, Inc.
5 * Author: Stefan Hajnoczi <stefanha@redhat.com>
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.
13 /* The client and server may need to coordinate to avoid race conditions like
14 * the client attempting to connect to a socket that the server is not
15 * listening on yet. The control socket offers a communications channel for
16 * such coordination tasks.
18 * If the client calls control_expectln("LISTENING"), then it will block until
19 * the server calls control_writeln("LISTENING"). This provides a simple
20 * mechanism for coordinating between the client and the server.
23 #include <errno.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include "timeout.h"
33 #include "control.h"
35 static int control_fd = -1;
37 /* Open the control socket, either in server or client mode */
38 void control_init(const char *control_host,
39 const char *control_port,
40 bool server)
42 struct addrinfo hints = {
43 .ai_socktype = SOCK_STREAM,
45 struct addrinfo *result = NULL;
46 struct addrinfo *ai;
47 int ret;
49 ret = getaddrinfo(control_host, control_port, &hints, &result);
50 if (ret != 0) {
51 fprintf(stderr, "%s\n", gai_strerror(ret));
52 exit(EXIT_FAILURE);
55 for (ai = result; ai; ai = ai->ai_next) {
56 int fd;
57 int val = 1;
59 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
60 if (fd < 0)
61 continue;
63 if (!server) {
64 if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0)
65 goto next;
66 control_fd = fd;
67 printf("Control socket connected to %s:%s.\n",
68 control_host, control_port);
69 break;
72 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
73 &val, sizeof(val)) < 0) {
74 perror("setsockopt");
75 exit(EXIT_FAILURE);
78 if (bind(fd, ai->ai_addr, ai->ai_addrlen) < 0)
79 goto next;
80 if (listen(fd, 1) < 0)
81 goto next;
83 printf("Control socket listening on %s:%s\n",
84 control_host, control_port);
85 fflush(stdout);
87 control_fd = accept(fd, NULL, 0);
88 close(fd);
90 if (control_fd < 0) {
91 perror("accept");
92 exit(EXIT_FAILURE);
94 printf("Control socket connection accepted...\n");
95 break;
97 next:
98 close(fd);
101 if (control_fd < 0) {
102 fprintf(stderr, "Control socket initialization failed. Invalid address %s:%s?\n",
103 control_host, control_port);
104 exit(EXIT_FAILURE);
107 freeaddrinfo(result);
110 /* Free resources */
111 void control_cleanup(void)
113 close(control_fd);
114 control_fd = -1;
117 /* Write a line to the control socket */
118 void control_writeln(const char *str)
120 ssize_t len = strlen(str);
121 ssize_t ret;
123 timeout_begin(TIMEOUT);
125 do {
126 ret = send(control_fd, str, len, MSG_MORE);
127 timeout_check("send");
128 } while (ret < 0 && errno == EINTR);
130 if (ret != len) {
131 perror("send");
132 exit(EXIT_FAILURE);
135 do {
136 ret = send(control_fd, "\n", 1, 0);
137 timeout_check("send");
138 } while (ret < 0 && errno == EINTR);
140 if (ret != 1) {
141 perror("send");
142 exit(EXIT_FAILURE);
145 timeout_end();
148 /* Return the next line from the control socket (without the trailing newline).
150 * The program terminates if a timeout occurs.
152 * The caller must free() the returned string.
154 char *control_readln(void)
156 char *buf = NULL;
157 size_t idx = 0;
158 size_t buflen = 0;
160 timeout_begin(TIMEOUT);
162 for (;;) {
163 ssize_t ret;
165 if (idx >= buflen) {
166 char *new_buf;
168 new_buf = realloc(buf, buflen + 80);
169 if (!new_buf) {
170 perror("realloc");
171 exit(EXIT_FAILURE);
174 buf = new_buf;
175 buflen += 80;
178 do {
179 ret = recv(control_fd, &buf[idx], 1, 0);
180 timeout_check("recv");
181 } while (ret < 0 && errno == EINTR);
183 if (ret == 0) {
184 fprintf(stderr, "unexpected EOF on control socket\n");
185 exit(EXIT_FAILURE);
188 if (ret != 1) {
189 perror("recv");
190 exit(EXIT_FAILURE);
193 if (buf[idx] == '\n') {
194 buf[idx] = '\0';
195 break;
198 idx++;
201 timeout_end();
203 return buf;
206 /* Wait until a given line is received or a timeout occurs */
207 void control_expectln(const char *str)
209 char *line;
211 line = control_readln();
212 if (strcmp(str, line) != 0) {
213 fprintf(stderr, "expected \"%s\" on control socket, got \"%s\"\n",
214 str, line);
215 exit(EXIT_FAILURE);
218 free(line);