1SETNS(2)                   Linux Programmer's Manual                  SETNS(2)
2
3
4

NAME

6       setns - reassociate thread with a namespace
7

SYNOPSIS

9       #define _GNU_SOURCE             /* See feature_test_macros(7) */
10       #include <sched.h>
11
12       int setns(int fd, int nstype);
13

DESCRIPTION

15       Given a file descriptor referring to a namespace, reassociate the call‐
16       ing thread with that namespace.
17
18       The fd argument is a file descriptor referring to one of the  namespace
19       entries  in  a /proc/[pid]/ns/ directory; see namespaces(7) for further
20       information on /proc/[pid]/ns/.  The calling thread will  be  reassoci‐
21       ated  with  the  corresponding  namespace,  subject  to any constraints
22       imposed by the nstype argument.
23
24       The nstype argument specifies  which  type  of  namespace  the  calling
25       thread  may  be  reassociated  with.  This argument can have one of the
26       following values:
27
28       0      Allow any type of namespace to be joined.
29
30       CLONE_NEWCGROUP (since Linux 4.6)
31              fd must refer to a cgroup namespace.
32
33       CLONE_NEWIPC (since Linux 3.0)
34              fd must refer to an IPC namespace.
35
36       CLONE_NEWNET (since Linux 3.0)
37              fd must refer to a network namespace.
38
39       CLONE_NEWNS (since Linux 3.8)
40              fd must refer to a mount namespace.
41
42       CLONE_NEWPID (since Linux 3.8)
43              fd must refer to a descendant PID namespace.
44
45       CLONE_NEWUSER (since Linux 3.8)
46              fd must refer to a user namespace.
47
48       CLONE_NEWUTS (since Linux 3.0)
49              fd must refer to a UTS namespace.
50
51       Specifying nstype as 0 suffices if the caller knows (or does not  care)
52       what  type  of  namespace  is  referred to by fd.  Specifying a nonzero
53       value for nstype is useful if the caller does not  know  what  type  of
54       namespace  is  referred to by fd and wants to ensure that the namespace
55       is of a particular type.  (The caller might not know the  type  of  the
56       namespace  referred  to  by  fd  if  the  file descriptor was opened by
57       another process and, for example, passed  to  the  caller  via  a  UNIX
58       domain socket.)
59
60       If  fd refers to a PID namespaces, the semantics are somewhat different
61       from other namespace types: reassociating the calling thread with a PID
62       namespace  changes  only  the  PID  namespace that subsequently created
63       child processes of the caller will be placed in; it does not change the
64       PID namespace of the caller itself.  Reassociating with a PID namespace
65       is allowed only if the PID namespace specified by fd  is  a  descendant
66       (child,  grandchild,  etc.)   of  the PID namespace of the caller.  For
67       further details on PID namespaces, see pid_namespaces(7).
68
69       A process reassociating itself with a  user  namespace  must  have  the
70       CAP_SYS_ADMIN  capability  in the target user namespace.  Upon success‐
71       fully joining a user namespace, a process is granted  all  capabilities
72       in  that  namespace,  regardless  of  its user and group IDs.  A multi‐
73       threaded process may not change user namespace with setns().  It is not
74       permitted  to  use  setns() to reenter the caller's current user names‐
75       pace.  This prevents  a  caller  that  has  dropped  capabilities  from
76       regaining  those capabilities via a call to setns().  For security rea‐
77       sons, a process can't join a  new  user  namespace  if  it  is  sharing
78       filesystem-related  attributes  (the  attributes  whose sharing is con‐
79       trolled by the clone(2) CLONE_FS flag) with another process.  For  fur‐
80       ther details on user namespaces, see user_namespaces(7).
81
82       A  process  may not be reassociated with a new mount namespace if it is
83       multithreaded.  Changing the mount namespace requires that  the  caller
84       possess  both  CAP_SYS_CHROOT and CAP_SYS_ADMIN capabilities in its own
85       user namespace and CAP_SYS_ADMIN in the target  mount  namespace.   See
86       user_namespaces(7)  for  details  on the interaction of user namespaces
87       and mount namespaces.
88
89       Using setns() to change the caller's cgroup namespace does  not  change
90       the caller's cgroup memberships.
91

RETURN VALUE

93       On success, setns() returns 0.  On failure, -1 is returned and errno is
94       set to indicate the error.
95

ERRORS

97       EBADF  fd is not a valid file descriptor.
98
99       EINVAL fd refers to a namespace whose type does not match  that  speci‐
100              fied in nstype.
101
102       EINVAL There  is  problem with reassociating the thread with the speci‐
103              fied namespace.
104
105       EINVAL The caller tried to join an ancestor (parent,  grandparent,  and
106              so on) PID namespace.
107
108       EINVAL The  caller  attempted to join the user namespace in which it is
109              already a member.
110
111       EINVAL The caller shares filesystem (CLONE_FS)  state  (in  particular,
112              the root directory) with other processes and tried to join a new
113              user namespace.
114
115       EINVAL The caller is multithreaded and tried to join a new user  names‐
116              pace.
117
118       ENOMEM Cannot allocate sufficient memory to change the specified names‐
119              pace.
120
121       EPERM  The calling thread did not have the required capability for this
122              operation.
123

VERSIONS

125       The  setns() system call first appeared in Linux in kernel 3.0; library
126       support was added to glibc in version 2.14.
127

CONFORMING TO

129       The setns() system call is Linux-specific.
130

NOTES

132       Not all of the attributes that can be shared when a new thread is  cre‐
133       ated using clone(2) can be changed using setns().
134

EXAMPLE

136       The  program  below  takes  two  or more arguments.  The first argument
137       specifies  the  pathname  of  a   namespace   file   in   an   existing
138       /proc/[pid]/ns/  directory.   The remaining arguments specify a command
139       and its arguments.  The program opens the namespace  file,  joins  that
140       namespace using setns(), and executes the specified command inside that
141       namespace.
142
143       The following shell session demonstrates the use of this program  (com‐
144       piled  as  a binary named ns_exec) in conjunction with the CLONE_NEWUTS
145       example program in the clone(2) man page (complied as  a  binary  named
146       newuts).
147
148       We  begin  by  executing  the  example program in clone(2) in the back‐
149       ground.  That program creates a child in a separate UTS namespace.  The
150       child  changes  the  hostname in its namespace, and then both processes
151       display the hostnames in their UTS namespaces, so that we can see  that
152       they are different.
153
154           $ su                   # Need privilege for namespace operations
155           Password:
156           # ./newuts bizarro &
157           [1] 3549
158           clone() returned 3550
159           uts.nodename in child:  bizarro
160           uts.nodename in parent: antero
161           # uname -n             # Verify hostname in the shell
162           antero
163
164       We  then  run  the  program  shown  below, using it to execute a shell.
165       Inside that shell, we verify that the hostname is the one  set  by  the
166       child created by the first program:
167
168           # ./ns_exec /proc/3550/ns/uts /bin/bash
169           # uname -n             # Executed in shell started by ns_exec
170           bizarro
171
172   Program source
173       #define _GNU_SOURCE
174       #include <fcntl.h>
175       #include <sched.h>
176       #include <unistd.h>
177       #include <stdlib.h>
178       #include <stdio.h>
179
180       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
181                               } while (0)
182
183       int
184       main(int argc, char *argv[])
185       {
186           int fd;
187
188           if (argc < 3) {
189               fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]);
190               exit(EXIT_FAILURE);
191           }
192
193           fd = open(argv[1], O_RDONLY); /* Get file descriptor for namespace */
194           if (fd == -1)
195               errExit("open");
196
197           if (setns(fd, 0) == -1)       /* Join that namespace */
198               errExit("setns");
199
200           execvp(argv[2], &argv[2]);    /* Execute a command in namespace */
201           errExit("execvp");
202       }
203

SEE ALSO

205       nsenter(1),  clone(2),  fork(2),  unshare(2),  vfork(2), namespaces(7),
206       unix(7)
207

COLOPHON

209       This page is part of release 4.16 of the Linux  man-pages  project.   A
210       description  of  the project, information about reporting bugs, and the
211       latest    version    of    this    page,    can     be     found     at
212       https://www.kernel.org/doc/man-pages/.
213
214
215
216Linux                             2017-09-15                          SETNS(2)
Impressum