tools/testing/selftests/net/so_netns_cookie.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/so_netns_cookie.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/so_netns_cookie.c- Extension
.c- Size
- 1427 bytes
- Lines
- 62
- 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.
Dependency Surface
sched.hunistd.hstdio.herrno.hstring.hstdlib.hstdint.hsys/types.hsys/socket.h
Detected Declarations
function main
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <sched.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef SO_NETNS_COOKIE
#define SO_NETNS_COOKIE 71
#endif
#define pr_err(fmt, ...) \
({ \
fprintf(stderr, "%s:%d:" fmt ": %m\n", \
__func__, __LINE__, ##__VA_ARGS__); \
1; \
})
int main(int argc, char *argvp[])
{
uint64_t cookie1, cookie2;
socklen_t vallen;
int sock1, sock2;
sock1 = socket(AF_INET, SOCK_STREAM, 0);
if (sock1 < 0)
return pr_err("Unable to create TCP socket");
vallen = sizeof(cookie1);
if (getsockopt(sock1, SOL_SOCKET, SO_NETNS_COOKIE, &cookie1, &vallen) != 0)
return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)");
if (!cookie1)
return pr_err("SO_NETNS_COOKIE returned zero cookie");
if (unshare(CLONE_NEWNET))
return pr_err("unshare");
sock2 = socket(AF_INET, SOCK_STREAM, 0);
if (sock2 < 0)
return pr_err("Unable to create TCP socket");
vallen = sizeof(cookie2);
if (getsockopt(sock2, SOL_SOCKET, SO_NETNS_COOKIE, &cookie2, &vallen) != 0)
return pr_err("getsockopt(SOL_SOCKET, SO_NETNS_COOKIE)");
if (!cookie2)
return pr_err("SO_NETNS_COOKIE returned zero cookie");
if (cookie1 == cookie2)
return pr_err("SO_NETNS_COOKIE returned identical cookies for distinct ns");
close(sock1);
close(sock2);
return 0;
}
Annotation
- Immediate include surface: `sched.h`, `unistd.h`, `stdio.h`, `errno.h`, `string.h`, `stdlib.h`, `stdint.h`, `sys/types.h`.
- Detected declarations: `function main`.
- 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.