tools/testing/selftests/namespaces/siocgskns_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/namespaces/siocgskns_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/namespaces/siocgskns_test.c- Extension
.c- Size
- 45254 bytes
- Lines
- 1825
- 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
errno.hfcntl.hlimits.hsched.hstdio.hstdlib.hstring.hsys/ioctl.hsys/socket.hsys/stat.hsys/types.hsys/wait.hunistd.hlinux/if.hlinux/sockios.hlinux/nsfs.harpa/inet.h../kselftest_harness.h../filesystems/utils.hwrappers.h
Detected Declarations
function typesfunction listnsfunction listnsfunction open_by_handle_at
Annotated Snippet
if (unshare(CLONE_NEWNET) < 0) {
TH_LOG("unshare(CLONE_NEWNET) failed: %s", strerror(errno));
close(ipc_sockets[1]);
exit(1);
}
/* Create a socket in the new network namespace */
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0) {
TH_LOG("socket() failed: %s", strerror(errno));
close(ipc_sockets[1]);
exit(1);
}
/* Send socket FD to parent via SCM_RIGHTS */
struct msghdr msg = {0};
struct iovec iov = {0};
char buf[1] = {'X'};
char cmsg_buf[CMSG_SPACE(sizeof(int))];
iov.iov_base = buf;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
memcpy(CMSG_DATA(cmsg), &sock_fd, sizeof(int));
if (sendmsg(ipc_sockets[1], &msg, 0) < 0) {
close(sock_fd);
close(ipc_sockets[1]);
exit(1);
}
close(sock_fd);
close(ipc_sockets[1]);
exit(0);
}
/* Parent: receive socket FD */
close(ipc_sockets[1]);
struct msghdr msg = {0};
struct iovec iov = {0};
char buf[1];
char cmsg_buf[CMSG_SPACE(sizeof(int))];
iov.iov_base = buf;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
ssize_t n = recvmsg(ipc_sockets[0], &msg, 0);
close(ipc_sockets[0]);
ASSERT_EQ(n, 1);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
ASSERT_NE(cmsg, NULL);
ASSERT_EQ(cmsg->cmsg_type, SCM_RIGHTS);
memcpy(&sock_fd, CMSG_DATA(cmsg), sizeof(int));
/* Wait for child to exit */
waitpid(pid, &status, 0);
ASSERT_TRUE(WIFEXITED(status));
ASSERT_EQ(WEXITSTATUS(status), 0);
/* Get network namespace from socket */
netns_fd = ioctl(sock_fd, SIOCGSKNS);
if (netns_fd < 0) {
close(sock_fd);
if (errno == ENOTTY || errno == EINVAL)
SKIP(return, "SIOCGSKNS not supported");
ASSERT_GE(netns_fd, 0);
}
ASSERT_EQ(fstat(netns_fd, &st), 0);
/*
* Namespace should still be active because socket FD keeps it alive.
* Try to access it via /proc/self/fd/<fd>.
*/
char path[64];
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `limits.h`, `sched.h`, `stdio.h`, `stdlib.h`, `string.h`, `sys/ioctl.h`.
- Detected declarations: `function types`, `function listns`, `function listns`, `function open_by_handle_at`.
- 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.