tools/testing/vsock/util.c

Source file repositories/reference/linux-study-clean/tools/testing/vsock/util.c

File Facts

System
Linux kernel
Corpus path
tools/testing/vsock/util.c
Extension
.c
Size
20195 bytes
Lines
955
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (ret < 0) {
			if (errno == EOPNOTSUPP || errno == ENOTTY)
				break;

			perror(name);
			exit(EXIT_FAILURE);
		}
		timeout_check(name);
	} while (actual != expected);
	timeout_end();

	return ret >= 0;
}

/* Wait until transport reports no data left to be sent.
 * Return false if transport does not implement the unsent_bytes() callback.
 */
bool vsock_wait_sent(int fd)
{
	return vsock_ioctl_int(fd, SIOCOUTQ, 0);
}

/* Create socket <type>, bind to <cid, port>.
 * Return the file descriptor, or -1 on error.
 */
int vsock_bind_try(unsigned int cid, unsigned int port, int type)
{
	struct sockaddr_vm sa = {
		.svm_family = AF_VSOCK,
		.svm_cid = cid,
		.svm_port = port,
	};
	int fd, saved_errno;

	fd = socket(AF_VSOCK, type, 0);
	if (fd < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa))) {
		saved_errno = errno;
		close(fd);
		errno = saved_errno;
		fd = -1;
	}

	return fd;
}

/* Create socket <type>, bind to <cid, port> and return the file descriptor. */
int vsock_bind(unsigned int cid, unsigned int port, int type)
{
	int fd;

	fd = vsock_bind_try(cid, port, type);
	if (fd < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}

	return fd;
}

int vsock_connect_fd(int fd, unsigned int cid, unsigned int port)
{
	struct sockaddr_vm sa = {
		.svm_family = AF_VSOCK,
		.svm_cid = cid,
		.svm_port = port,
	};
	int ret;

	timeout_begin(TIMEOUT);
	do {
		ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
		timeout_check("connect");
	} while (ret < 0 && errno == EINTR);
	timeout_end();

	return ret;
}

/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
{
	int client_fd;

	client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);

Annotation

Implementation Notes