tools/testing/selftests/bpf/netlink_helpers.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/netlink_helpers.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/netlink_helpers.c- Extension
.c- Size
- 8324 bytes
- Lines
- 359
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
stdio.hstdlib.hunistd.herrno.htime.hsys/socket.hnetlink_helpers.h
Detected Declarations
function rtnl_closefunction rtnl_open_byprotofunction rtnl_openfunction __rtnl_recvmsgfunction rtnl_recvmsgfunction rtnl_talk_errorfunction __rtnl_talk_iovfunction __rtnl_talkfunction rtnl_talkfunction addattrfunction addattr8function addattr16function addattr32function addattr64function addattrstrzfunction addattr_lfunction addraw_lfunction addattr_nest_end
Annotated Snippet
sizeof(rth->local)) < 0) {
perror("Cannot bind netlink socket");
goto err;
}
addr_len = sizeof(rth->local);
if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
&addr_len) < 0) {
perror("Cannot getsockname");
goto err;
}
if (addr_len != sizeof(rth->local)) {
fprintf(stderr, "Wrong address length %d\n", addr_len);
goto err;
}
if (rth->local.nl_family != AF_NETLINK) {
fprintf(stderr, "Wrong address family %d\n",
rth->local.nl_family);
goto err;
}
rth->seq = time(NULL);
return 0;
err:
rtnl_close(rth);
return -1;
}
int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
{
return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
}
static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
{
int len;
do {
len = recvmsg(fd, msg, flags);
} while (len < 0 && (errno == EINTR || errno == EAGAIN));
if (len < 0) {
fprintf(stderr, "netlink receive error %s (%d)\n",
strerror(errno), errno);
return -errno;
}
if (len == 0) {
fprintf(stderr, "EOF on netlink\n");
return -ENODATA;
}
return len;
}
static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
{
struct iovec *iov = msg->msg_iov;
char *buf;
int len;
iov->iov_base = NULL;
iov->iov_len = 0;
len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
if (len < 0)
return len;
if (len < 32768)
len = 32768;
buf = malloc(len);
if (!buf) {
fprintf(stderr, "malloc error: not enough buffer\n");
return -ENOMEM;
}
iov->iov_base = buf;
iov->iov_len = len;
len = __rtnl_recvmsg(fd, msg, 0);
if (len < 0) {
free(buf);
return len;
}
if (answer)
*answer = buf;
else
free(buf);
return len;
}
static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
nl_ext_ack_fn_t errfn)
{
fprintf(stderr, "RTNETLINK answers: %s\n",
strerror(-err->error));
}
Annotation
- Immediate include surface: `stdio.h`, `stdlib.h`, `unistd.h`, `errno.h`, `time.h`, `sys/socket.h`, `netlink_helpers.h`.
- Detected declarations: `function rtnl_close`, `function rtnl_open_byproto`, `function rtnl_open`, `function __rtnl_recvmsg`, `function rtnl_recvmsg`, `function rtnl_talk_error`, `function __rtnl_talk_iov`, `function __rtnl_talk`, `function rtnl_talk`, `function addattr`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.