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 proc(5) for further infor‐
20       mation on /proc/[pid]/ns/.  The calling  thread  will  be  reassociated
21       with the corresponding namespace, subject to any constraints imposed by
22       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_NEWIPC
31              fd must refer to an IPC namespace.
32
33       CLONE_NEWNET
34              fd must refer to a network namespace.
35
36       CLONE_NEWUTS
37              fd must refer to a UTS namespace.
38
39       Specifying nstype as 0 suffices if the caller knows (or does not  care)
40       what  type  of  namespace  is  referred to by fd.  Specifying a nonzero
41       value for nstype is useful if the caller does not  know  what  type  of
42       namespace  is  referred to by fd and wants to ensure that the namespace
43       is of a particular type.  (The caller might not know the  type  of  the
44       namespace  referred  to  by  fd  if  the  file descriptor was opened by
45       another process and, for example, passed  to  the  caller  via  a  UNIX
46       domain socket.)
47

RETURN VALUE

49       On success, setns() returns 0.  On failure, -1 is returned and errno is
50       set to indicate the error.
51

ERRORS

53       EBADF  fd is not a valid file descriptor.
54
55       EINVAL fd refers to a namespace whose type does not match  that  speci‐
56              fied  in  nstype, or there is problem with reassociating the the
57              thread with the specified namespace.
58
59       ENOMEM Cannot allocate sufficient memory to change the specified names‐
60              pace.
61
62       EPERM  The   calling   thread  did  not  have  the  required  privilege
63              (CAP_SYS_ADMIN) for this operation.
64

VERSIONS

66       The setns() system call first appeared in Linux in kernel 3.0;  library
67       support was added to glibc in version 2.14.
68

CONFORMING TO

70       The setns() system call is Linux-specific.
71

NOTES

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

EXAMPLE

77       The program below takes two or  more  arguments.   The  first  argument
78       specifies   the   pathname   of   a   namespace  file  in  an  existing
79       /proc/[pid]/ns/ directory.  The remaining arguments specify  a  command
80       and  its  arguments.   The program opens the namespace file, joins that
81       namespace using setns(), and executes the specified command inside that
82       namespace.
83
84       The  following shell session demonstrates the use of this program (com‐
85       piled as a binary named ns_exec) in conjunction with  the  CLONE_NEWUTS
86       example  program  in  the clone(2) man page (complied as a binary named
87       newuts).
88
89       We begin by executing the example program  in  clone(2)  in  the  back‐
90       ground.  That program creates a child in a separate UTS namespace.  The
91       child changes the hostname in its namespace, and  then  both  processes
92       display  the hostnames in their UTS namespaces, so that we can see that
93       they are different.
94
95           $ su                   # Need privilege for namespace operations
96           Password:
97           # ./newuts bizarro &
98           [1] 3549
99           clone() returned 3550
100           uts.nodename in child:  bizarro
101           uts.nodename in parent: antero
102           # uname -n             # Verify hostname in the shell
103           antero
104
105       We then run the program shown below,  using  it  to  execute  a  shell.
106       Inside  that  shell,  we verify that the hostname is the one set by the
107       child created by the first program:
108
109           # ./ns_exec /proc/3550/ns/uts /bin/bash
110           # uname -n             # Executed in shell started by ns_exec
111           bizarro
112
113   Program source
114       #define _GNU_SOURCE
115       #include <fcntl.h>
116       #include <sched.h>
117       #include <unistd.h>
118       #include <stdlib.h>
119       #include <stdio.h>
120
121       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
122                               } while (0)
123
124       int
125       main(int argc, char *argv[])
126       {
127           int fd;
128
129           if (argc < 3) {
130               fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\n", argv[0]);
131               exit(EXIT_FAILURE);
132           }
133
134           fd = open(argv[1], O_RDONLY);   /* Get descriptor for namespace */
135           if (fd == -1)
136               errExit("open");
137
138           if (setns(fd, 0) == -1)         /* Join that namespace */
139               errExit("setns");
140
141           execvp(argv[2], &argv[2]);      /* Execute a command in namespace */
142           errExit("execvp");
143       }
144

SEE ALSO

146       clone(2), fork(2), vfork(2), proc(5), unix(7)
147

COLOPHON

149       This page is part of release 3.53 of the Linux  man-pages  project.   A
150       description  of  the project, and information about reporting bugs, can
151       be found at http://www.kernel.org/doc/man-pages/.
152
153
154
155Linux                             2013-01-01                          SETNS(2)
Impressum