Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * vsock test utilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2017 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author: Stefan Hajnoczi <stefanha@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/epoll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "timeout.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "control.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "util.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /* Install signal handlers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) void init_signals(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct sigaction act = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		.sa_handler = sigalrm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	sigaction(SIGALRM, &act, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	signal(SIGPIPE, SIG_IGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /* Parse a CID in string representation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) unsigned int parse_cid(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	char *endptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned long n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	n = strtoul(str, &endptr, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	if (errno || *endptr != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		fprintf(stderr, "malformed CID \"%s\"\n", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* Wait for the remote to close the connection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) void vsock_wait_remote_close(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	struct epoll_event ev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	int epollfd, nfds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	epollfd = epoll_create1(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (epollfd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		perror("epoll_create1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ev.events = EPOLLRDHUP | EPOLLHUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ev.data.fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		perror("epoll_ctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	nfds = epoll_wait(epollfd, &ev, 1, TIMEOUT * 1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	if (nfds == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		perror("epoll_wait");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (nfds == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		fprintf(stderr, "epoll_wait timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	assert(nfds == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	assert(ev.events & (EPOLLRDHUP | EPOLLHUP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	assert(ev.data.fd == fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	close(epollfd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) /* Connect to <cid, port> and return the file descriptor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) int vsock_stream_connect(unsigned int cid, unsigned int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	} addr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		.svm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			.svm_family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 			.svm_port = port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			.svm_cid = cid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	control_expectln("LISTENING");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		ret = connect(fd, &addr.sa, sizeof(addr.svm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		timeout_check("connect");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	} while (ret < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		int old_errno = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		fd = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		errno = old_errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* Listen on <cid, port> and return the first incoming connection.  The remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * address is stored to clientaddrp.  clientaddrp may be NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int vsock_stream_accept(unsigned int cid, unsigned int port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			struct sockaddr_vm *clientaddrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	} addr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		.svm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			.svm_family = AF_VSOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			.svm_port = port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			.svm_cid = cid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		struct sockaddr sa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		struct sockaddr_vm svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	} clientaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	socklen_t clientaddr_len = sizeof(clientaddr.svm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int old_errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	fd = socket(AF_VSOCK, SOCK_STREAM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		perror("bind");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (listen(fd, 1) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		perror("listen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	control_writeln("LISTENING");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		client_fd = accept(fd, &clientaddr.sa, &clientaddr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		timeout_check("accept");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	} while (client_fd < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	old_errno = errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	errno = old_errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (client_fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (clientaddr_len != sizeof(clientaddr.svm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		fprintf(stderr, "unexpected addrlen from accept(2), %zu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 			(size_t)clientaddr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (clientaddr.sa.sa_family != AF_VSOCK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		fprintf(stderr, "expected AF_VSOCK from accept(2), got %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 			clientaddr.sa.sa_family);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	if (clientaddrp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		*clientaddrp = clientaddr.svm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return client_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Transmit one byte and check the return value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * expected_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *  <0 Negative errno (for testing errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  *   0 End-of-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  *   1 Success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) void send_byte(int fd, int expected_ret, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	const uint8_t byte = 'A';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ssize_t nwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		nwritten = send(fd, &byte, sizeof(byte), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		timeout_check("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	} while (nwritten < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (expected_ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (nwritten != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			fprintf(stderr, "bogus send(2) return value %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 				nwritten);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (errno != -expected_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (nwritten < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (nwritten == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		if (expected_ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		fprintf(stderr, "unexpected EOF while sending byte\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (nwritten != sizeof(byte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Receive one byte and check the return value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  * expected_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)  *  <0 Negative errno (for testing errors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)  *   0 End-of-file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)  *   1 Success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) void recv_byte(int fd, int expected_ret, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	uint8_t byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	ssize_t nread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	timeout_begin(TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		nread = recv(fd, &byte, sizeof(byte), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		timeout_check("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	} while (nread < 0 && errno == EINTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	timeout_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (expected_ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		if (nread != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			fprintf(stderr, "bogus recv(2) return value %zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				nread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		if (errno != -expected_ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	if (nread < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (nread == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (expected_ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		fprintf(stderr, "unexpected EOF while receiving byte\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (nread != sizeof(byte)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		fprintf(stderr, "bogus recv(2) return value %zd\n", nread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	if (byte != 'A') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		fprintf(stderr, "unexpected byte read %c\n", byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* Run test cases.  The program terminates if a failure occurs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) void run_tests(const struct test_case *test_cases,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	       const struct test_opts *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	for (i = 0; test_cases[i].name; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		void (*run)(const struct test_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		char *line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		printf("%d - %s...", i, test_cases[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		fflush(stdout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		/* Full barrier before executing the next test.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		 * ensures that client and server are executing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		 * same test case.  In particular, it means whoever is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		 * faster will not see the peer still executing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		 * last test.  This is important because port numbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 		 * can be used by multiple test cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		if (test_cases[i].skip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 			control_writeln("SKIP");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			control_writeln("NEXT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		line = control_readln();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		if (control_cmpln(line, "SKIP", false) || test_cases[i].skip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			printf("skipped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			free(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		control_cmpln(line, "NEXT", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		free(line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		if (opts->mode == TEST_MODE_CLIENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			run = test_cases[i].run_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			run = test_cases[i].run_server;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		if (run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			run(opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		printf("ok\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) void list_tests(const struct test_case *test_cases)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	printf("ID\tTest name\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	for (i = 0; test_cases[i].name; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		printf("%d\t%s\n", i, test_cases[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) void skip_test(struct test_case *test_cases, size_t test_cases_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	       const char *test_id_str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	unsigned long test_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	char *endptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	test_id = strtoul(test_id_str, &endptr, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	if (errno || *endptr != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		fprintf(stderr, "malformed test ID \"%s\"\n", test_id_str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	if (test_id >= test_cases_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		fprintf(stderr, "test ID (%lu) larger than the max allowed (%lu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			test_id, test_cases_len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		exit(EXIT_FAILURE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	test_cases[test_id].skip = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }