]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/setns.2
migrate_pages.2, move_pages.2: Added LICENSE_START(VERBATIM_TWO_PARA)
[thirdparty/man-pages.git] / man2 / setns.2
CommitLineData
dfa22c8b 1.\" Copyright (C) 2011, Eric Biederman <ebiederm@xmission.com>
8d41e607 2.\" and Copyright (C) 2011, 2012, Michael Kerrisk <mtk.manpages@gamil.com>
dfa22c8b
EB
3.\" Licensed under the GPLv2
4.\"
8d41e607 5.TH SETNS 2 2013-01-01 "Linux" "Linux Programmer's Manual"
dfa22c8b 6.SH NAME
7aa166c5 7setns \- reassociate thread with a namespace
dfa22c8b
EB
8.SH SYNOPSIS
9.nf
10.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
11.B #include <sched.h>
12.sp
13.BI "int setns(int " fd ", int " nstype );
14.fi
15.SH DESCRIPTION
c07ad242 16Given a file descriptor referring to a namespace,
7aa166c5 17reassociate the calling thread with that namespace.
dfa22c8b 18
92dcb220
MK
19The
20.I fd
21argument is a file descriptor referring to one of the namespace entries in a
22.I /proc/[pid]/ns/
23directory; see
24.BR proc (5)
c409c4ff 25for further information on
92dcb220 26.IR /proc/[pid]/ns/ .
7aa166c5 27The calling thread will be reassociated with the corresponding namespace,
92dcb220
MK
28subject to any constraints imposed by the
29.I nstype
30argument.
31
dfa22c8b
EB
32The
33.I nstype
c07ad242 34argument specifies which type of namespace
7aa166c5 35the calling thread may be reassociated with.
4e1e208d 36This argument can have one of the following values:
dfa22c8b
EB
37.TP
38.BR 0
92dcb220 39Allow any type of namespace to be joined.
dfa22c8b
EB
40.TP
41.BR CLONE_NEWIPC
92dcb220
MK
42.I fd
43must refer to an IPC namespace.
dfa22c8b
EB
44.TP
45.BR CLONE_NEWNET
92dcb220
MK
46.I fd
47must refer to a network namespace.
dfa22c8b
EB
48.TP
49.BR CLONE_NEWUTS
92dcb220
MK
50.I fd
51must refer to a UTS namespace.
dfa22c8b 52.PP
92dcb220
MK
53Specifying
54.I nstype
55as 0 suffices if the caller knows (or does not care)
56what type of namespace is referred to by
57.IR fd .
58Specifying a nonzero value for
59.I nstype
60is useful if the caller does not know what type of namespace is referred to by
61.IR fd
62and wants to ensure that the namespace is of a particular type.
63(The caller might not know the type of the namespace referred to by
64.IR fd
65if the file descriptor was opened by another process and, for example,
66passed to the caller via a UNIX domain socket.)
dfa22c8b 67.SH RETURN VALUE
92dcb220
MK
68On success,
69.IR setns ()
70returns 0.
dfa22c8b
EB
71On failure, \-1 is returned and
72.I errno
73is set to indicate the error.
74.SH ERRORS
75.TP
dfa22c8b 76.B EBADF
c07ad242
MK
77.I fd
78is not a valid file descriptor.
dfa22c8b
EB
79.TP
80.B EINVAL
92dcb220
MK
81.I fd
82refers to a namespace whose type does not match that specified in
7aa166c5
MK
83.IR nstype ,
84or there is problem with reassociating the
85the thread with the specified namespace.
dfa22c8b
EB
86.TP
87.B ENOMEM
88Cannot allocate sufficient memory to change the specified namespace.
dfa22c8b
EB
89.TP
90.B EPERM
c409c4ff 91The calling thread did not have the required privilege
63b6ef41
MK
92.RB ( CAP_SYS_ADMIN )
93for this operation.
dfa22c8b
EB
94.SH VERSIONS
95The
96.BR setns ()
c95b6ae1
MK
97system call first appeared in Linux in kernel 3.0;
98library support was added to glibc in version 2.14.
dfa22c8b
EB
99.SH CONFORMING TO
100The
101.BR setns ()
102system call is Linux-specific.
103.SH NOTES
7aa166c5
MK
104Not all of the attributes that can be shared when
105a new thread is created using
dfa22c8b
EB
106.BR clone (2)
107can be changed using
108.BR setns ().
8d41e607
MK
109.SH EXAMPLE
110The program below takes two or more arguments.
111The first argument specifies the pathname of a namespace file in an existing
112.I /proc/[pid]/ns/
113directory.
114The remaining arguments specify a command and its arguments.
115The program opens the namespace file, joins that namespace using
116.BR setns (),
117and executes the specified command inside that namespace.
118
119The following shell session demonstrates the use of this program
120(compiled as a binary named
a1ce9159 121.IR ns_exec )
8d41e607
MK
122in conjunction with the
123.BR CLONE_NEWUTS
124example program in the
125.BR clone (2)
126man page (complied as a binary named
127.IR newuts ).
128
129We begin by executing the example program in
130.BR clone (2)
131in the background.
132That program creates a child in a separate UTS namespace.
51ebe080 133The child changes the hostname in its namespace,
8d41e607
MK
134and then both processes display the hostnames in their UTS namespaces,
135so that we can see that they are different.
136
137.nf
138.in +4n
139$ \fBsu\fP # Need privilege for namespace operations
9f1b9726 140Password:
8d41e607
MK
141# \fB./newuts bizarro &\fP
142[1] 3549
143clone() returned 3550
144uts.nodename in child: bizarro
145uts.nodename in parent: antero
146# \fBuname -n\fP # Verify hostname in the shell
147antero
148.in
149.fi
150
151We then run the program shown below,
152using it to execute a shell.
153Inside that shell, we verify that the hostname is the one
154set by the child created by the first program:
155
156.nf
157.in +4n
a1ce9159
MK
158# \fB./ns_exec /proc/3550/ns/uts /bin/bash\fP
159# \fBuname -n\fP # Executed in shell started by ns_exec
8d41e607
MK
160bizarro
161.in
162.fi
8d41e607
MK
163.SS Program source
164.nf
165#define _GNU_SOURCE
166#include <fcntl.h>
167#include <sched.h>
168#include <unistd.h>
169#include <stdlib.h>
170#include <stdio.h>
171
172#define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
173 } while (0)
174
175int
176main(int argc, char *argv[])
177{
178 int fd;
179
180 if (argc < 3) {
181 fprintf(stderr, "%s /proc/PID/ns/FILE cmd args...\\n", argv[0]);
182 exit(EXIT_FAILURE);
183 }
184
185 fd = open(argv[1], O_RDONLY); /* Get descriptor for namespace */
186 if (fd == \-1)
187 errExit("open");
188
189 if (setns(fd, 0) == \-1) /* Join that namespace */
190 errExit("setns");
191
192 execvp(argv[2], &argv[2]); /* Execute a command in namespace */
193 errExit("execvp");
194}
195.fi
dfa22c8b
EB
196.SH SEE ALSO
197.BR clone (2),
198.BR fork (2),
b93a34b5 199.BR vfork (2),
92dcb220
MK
200.BR proc (5),
201.BR unix (7)