tools/testing/selftests/net/sk_so_peek_off.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/sk_so_peek_off.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/net/sk_so_peek_off.c
Extension
.c
Size
4627 bytes
Lines
203
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 (recv_sock <= 0) {
			ksft_perror("Temporary socket accept() failed\n");
			goto out;
		}
	}

	/* Some basic tests of getting/setting offset */
	offset = sk_peek_offset_get(recv_sock);
	if (offset != -1) {
		ksft_perror("Initial value of socket offset not -1\n");
		goto out;
	}
	sk_peek_offset_set(recv_sock, 0);
	offset = sk_peek_offset_get(recv_sock);
	if (offset != 0) {
		ksft_perror("Failed to set socket offset to 0\n");
		goto out;
	}

	/* Transfer a message */
	if (send(s[1], (char *)("ab"), 2, 0) != 2) {
		ksft_perror("Temporary probe socket send() failed\n");
		goto out;
	}
	/* Read first byte */
	len = recv(recv_sock, buf, 1, MSG_PEEK);
	if (len != 1 || buf[0] != 'a') {
		ksft_perror("Failed to read first byte of message\n");
		goto out;
	}
	offset = sk_peek_offset_get(recv_sock);
	if (offset != 1) {
		ksft_perror("Offset not forwarded correctly at first byte\n");
		goto out;
	}
	/* Try to read beyond last byte */
	len = recv(recv_sock, buf, 2, MSG_PEEK);
	if (len != 1 || buf[0] != 'b') {
		ksft_perror("Failed to read last byte of message\n");
		goto out;
	}
	offset = sk_peek_offset_get(recv_sock);
	if (offset != 2) {
		ksft_perror("Offset not forwarded correctly at last byte\n");
		goto out;
	}
	/* Flush message */
	len = recv(recv_sock, buf, 2, MSG_TRUNC);
	if (len != 2) {
		ksft_perror("Failed to flush message\n");
		goto out;
	}
	offset = sk_peek_offset_get(recv_sock);
	if (offset != 0) {
		ksft_perror("Offset not reverted correctly after flush\n");
		goto out;
	}

	printf("%s with MSG_PEEK_OFF works correctly\n", afstr(af, proto));
	res = 1;
out:
	if (proto == IPPROTO_TCP && recv_sock >= 0)
		close(recv_sock);
	if (s[1] >= 0)
		close(s[1]);
	if (s[0] >= 0)
		close(s[0]);
	return res;
}

static int do_test(int proto)
{
	int res4, res6;

	res4 = sk_peek_offset_probe(AF_INET, proto);
	res6 = sk_peek_offset_probe(AF_INET6, proto);

	if (!res4 && !res6)
		return KSFT_SKIP;

	if (res4)
		res4 = sk_peek_offset_test(AF_INET, proto);

	if (res6)
		res6 = sk_peek_offset_test(AF_INET6, proto);

	if (!res4 || !res6)
		return KSFT_FAIL;

	return KSFT_PASS;

Annotation

Implementation Notes