// SPDX-License-Identifier: GPL-2.0-only /* Control socket for client/server test execution * * Copyright (C) 2017 Red Hat, Inc. * * Author: Stefan Hajnoczi <stefanha@redhat.com>
*/
/* The client and server may need to coordinate to avoid race conditions like * the client attempting to connect to a socket that the server is not * listening on yet. The control socket offers a communications channel for * such coordination tasks. * * If the client calls control_expectln("LISTENING"), then it will block until * the server calls control_writeln("LISTENING"). This provides a simple * mechanism for coordinating between the client and the server.
*/
/* Open the control socket, either in server or client mode */ void control_init(constchar *control_host, constchar *control_port, bool server)
{ struct addrinfo hints = {
.ai_socktype = SOCK_STREAM,
}; struct addrinfo *result = NULL; struct addrinfo *ai; int ret;
ret = getaddrinfo(control_host, control_port, &hints, &result); if (ret != 0) {
fprintf(stderr, "%s\n", gai_strerror(ret)); exit(EXIT_FAILURE);
}
for (ai = result; ai; ai = ai->ai_next) { int fd;
fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd < 0) continue;
if (!server) { if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) goto next;
control_fd = fd;
printf("Control socket connected to %s:%s.\n",
control_host, control_port); break;
}
/* Return the next line from the control socket (without the trailing newline). * * The program terminates if a timeout occurs. * * The caller must free() the returned string.
*/ char *control_readln(void)
{ char *buf = NULL;
size_t idx = 0;
size_t buflen = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.