setns(2) | System Calls Manual | setns(2) |
setns - reassociate thread with a namespace
Standard C library (libc, -lc)
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sched.h>
int setns(int fd, int nstype);
The setns() system call allows the calling thread to move into different namespaces. The fd argument is one of the following:
The nstype argument is interpreted differently in each case.
If fd refers to a /proc/pid/ns/ link, then setns() reassociates the calling thread with the namespace associated with that link, subject to any constraints imposed by the nstype argument. In this usage, each call to setns() changes just one of the caller's namespace memberships.
The nstype argument specifies which type of namespace the calling thread may be reassociated with. This argument can have one of the following values:
Specifying nstype as 0 suffices if the caller knows (or does not care) what type of namespace is referred to by fd. Specifying a nonzero value for nstype is useful if the caller does not know what type of namespace is referred to by fd and wants to ensure that the namespace is of a particular type. (The caller might not know the type of the namespace referred to by fd if the file descriptor was opened by another process and, for example, passed to the caller via a UNIX domain socket.)
Since Linux 5.8, fd may refer to a PID file descriptor obtained from pidfd_open(2) or clone(2). In this usage, setns() atomically moves the calling thread into one or more of the same namespaces as the thread referred to by fd.
The nstype argument is a bit mask specified by ORing together one or more of the CLONE_NEW* namespace constants listed above. The caller is moved into each of the target thread's namespaces that is specified in nstype; the caller's memberships in the remaining namespaces are left unchanged.
For example, the following code would move the caller into the same user, network, and UTS namespaces as PID 1234, but would leave the caller's other namespace memberships unchanged:
int fd = pidfd_open(1234, 0); setns(fd, CLONE_NEWUSER | CLONE_NEWNET | CLONE_NEWUTS);
Note the following details and restrictions when reassociating with specific namespace types:
On success, setns() returns 0. On failure, -1 is returned and errno is set to indicate the error.
Linux.
Linux 3.0, glibc 2.14.
For further information on the /proc/pid/ns/ magic links, see namespaces(7).
Not all of the attributes that can be shared when a new thread is created using clone(2) can be changed using setns().
The program below takes two or more arguments. The first argument specifies the pathname of a namespace file in an existing /proc/pid/ns/ directory. The remaining arguments specify a command and its arguments. The program opens the namespace file, joins that namespace using setns(), and executes the specified command inside that namespace.
The following shell session demonstrates the use of this program (compiled as a binary named ns_exec) in conjunction with the CLONE_NEWUTS example program in the clone(2) man page (complied as a binary named newuts).
We begin by executing the example program in clone(2) in the background. That program creates a child in a separate UTS namespace. The child changes the hostname in its namespace, and then both processes display the hostnames in their UTS namespaces, so that we can see that they are different.
$ su # Need privilege for namespace operations Password: # ./newuts bizarro & [1] 3549 clone() returned 3550 uts.nodename in child: bizarro uts.nodename in parent: antero # uname -n # Verify hostname in the shell antero
We then run the program shown below, using it to execute a shell. Inside that shell, we verify that the hostname is the one set by the child created by the first program:
# ./ns_exec /proc/3550/ns/uts /bin/bash # uname -n # Executed in shell started by ns_exec bizarro
#define _GNU_SOURCE #include <err.h> #include <fcntl.h> #include <sched.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { int fd; if (argc < 3) { fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]); exit(EXIT_FAILURE); } /* Get file descriptor for namespace; the file descriptor is opened with O_CLOEXEC so as to ensure that it is not inherited by the program that is later executed. */ fd = open(argv[1], O_RDONLY | O_CLOEXEC); if (fd == -1) err(EXIT_FAILURE, "open"); if (setns(fd, 0) == -1) /* Join that namespace */ err(EXIT_FAILURE, "setns"); execvp(argv[2], &argv[2]); /* Execute a command in namespace */ err(EXIT_FAILURE, "execvp"); }
nsenter(1), clone(2), fork(2), unshare(2), vfork(2), namespaces(7), unix(7)
2023-10-31 | Linux man-pages 6.7 |