tools/testing/selftests/filesystems/xattr/xattr_socket_types_test.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/filesystems/xattr/xattr_socket_types_test.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/filesystems/xattr/xattr_socket_types_test.c
Extension
.c
Size
4180 bytes
Lines
178
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

// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2026 Christian Brauner <brauner@kernel.org>
/*
 * Test user.* xattrs on various socket families.
 *
 * All socket types use sockfs for their inodes, so user.* xattrs should
 * work on any socket regardless of address family. This tests AF_INET,
 * AF_INET6, AF_NETLINK, AF_PACKET, and abstract namespace AF_UNIX sockets.
 */

#define _GNU_SOURCE
#include <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/xattr.h>
#include <linux/netlink.h>
#include <unistd.h>

#include "../../kselftest_harness.h"

#define TEST_XATTR_NAME		"user.testattr"
#define TEST_XATTR_VALUE	"testvalue"

FIXTURE(xattr_socket_types)
{
	int sockfd;
};

FIXTURE_VARIANT(xattr_socket_types)
{
	int family;
	int type;
	int protocol;
};

FIXTURE_VARIANT_ADD(xattr_socket_types, inet) {
	.family = AF_INET,
	.type = SOCK_STREAM,
	.protocol = 0,
};

FIXTURE_VARIANT_ADD(xattr_socket_types, inet6) {
	.family = AF_INET6,
	.type = SOCK_STREAM,
	.protocol = 0,
};

FIXTURE_VARIANT_ADD(xattr_socket_types, netlink) {
	.family = AF_NETLINK,
	.type = SOCK_RAW,
	.protocol = NETLINK_USERSOCK,
};

FIXTURE_VARIANT_ADD(xattr_socket_types, packet) {
	.family = AF_PACKET,
	.type = SOCK_DGRAM,
	.protocol = 0,
};

FIXTURE_SETUP(xattr_socket_types)
{
	self->sockfd = socket(variant->family, variant->type,
			      variant->protocol);
	if (self->sockfd < 0 &&
	    (errno == EAFNOSUPPORT || errno == EPERM || errno == EACCES))
		SKIP(return, "socket(%d, %d, %d) not available: %s",
		     variant->family, variant->type, variant->protocol,
		     strerror(errno));
	ASSERT_GE(self->sockfd, 0) {
		TH_LOG("Failed to create socket(%d, %d, %d): %s",
		       variant->family, variant->type, variant->protocol,
		       strerror(errno));
	}
}

FIXTURE_TEARDOWN(xattr_socket_types)
{
	if (self->sockfd >= 0)
		close(self->sockfd);
}

TEST_F(xattr_socket_types, set_get_list_remove)
{
	char buf[256], list[4096], *ptr;
	ssize_t ret;

Annotation

Implementation Notes