1 .\" Copyright (c) 2017 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH IOCTL_NS 2 2021-03-22 "Linux" "Linux Programmer's Manual"
28 ioctl_ns \- ioctl() operations for Linux namespaces
30 .\" ============================================================
32 .SS Discovering namespace relationships
35 operations are provided to allow discovery of namespace relationships (see
36 .BR user_namespaces (7)
38 .BR pid_namespaces (7)).
39 The form of the calls is:
43 new_fd = ioctl(fd, request);
52 Both operations return a new file descriptor on success.
54 .BR NS_GET_USERNS " (since Linux 4.9)"
55 .\" commit bcac25a58bfc6bd79191ac5d7afb49bea96da8c9
56 .\" commit 6786741dbf99e44fb0c0ed85a37582b8a26f1c3b
57 Returns a file descriptor that refers to the owning user namespace
58 for the namespace referred to by
61 .BR NS_GET_PARENT " (since Linux 4.9)"
62 .\" commit a7306ed8d94af729ecef8b6e37506a1c6fc14788
63 Returns a file descriptor that refers to the parent namespace of
64 the namespace referred to by
66 This operation is valid only for hierarchical namespaces
67 (i.e., PID and user namespaces).
73 The new file descriptor returned by these operations is opened with the
83 to the returned file descriptor, one obtains a
89 (inode number) fields together identify the owning/parent namespace.
90 This inode number can be matched with the inode number of another
91 .IR /proc/[pid]/ns/{pid,user}
92 file to determine whether that is the owning/parent namespace.
96 operations can fail with the following errors:
99 The requested namespace is outside of the caller's namespace scope.
100 This error can occur if, for example, the owning user namespace is an
101 ancestor of the caller's current user namespace.
102 It can also occur on attempts to obtain the parent of the initial
103 user or PID namespace.
106 The operation is not supported by this kernel version.
110 operation can fail with the following error:
114 refers to a nonhierarchical namespace.
116 See the EXAMPLES section for an example of the use of these operations.
117 .\" ============================================================
119 .SS Discovering the namespace type
122 .\" commit e5ff5ce6e20ee22511398bb31fb912466cf82a36
123 operation (available since Linux 4.11) can be used to discover
124 the type of namespace referred to by the file descriptor
129 nstype = ioctl(fd, NS_GET_NSTYPE);
138 The return value is one of the
140 values that can be specified to
144 in order to create a namespace.
145 .\" ============================================================
147 .SS Discovering the owner of a user namespace
150 .\" commit 015bb305b8ebe8d601a238ab70ebdc394c7a19ba
151 operation (available since Linux 4.11) can be used to discover
152 the owner user ID of a user namespace (i.e., the effective user ID
153 of the process that created the user namespace).
154 The form of the call is:
159 ioctl(fd, NS_GET_OWNER_UID, &uid);
165 .IR /proc/[pid]/ns/user
168 The owner user ID is returned in the
170 pointed to by the third argument.
172 This operation can fail with the following error:
176 does not refer to a user namespace.
180 operations can return the following errors:
188 Namespaces and the operations described on this page are a Linux-specific.
190 The example shown below uses the
192 operations described above to perform simple
193 discovery of namespace relationships.
194 The following shell sessions show various examples of the use
197 Trying to get the parent of the initial user namespace fails,
198 since it has no parent:
202 $ \fB./ns_show /proc/self/ns/user p\fP
203 The parent namespace is outside your namespace scope
207 Create a process running
209 that resides in new user and UTS namespaces,
210 and show that the new UTS namespace is associated with the new user namespace:
214 $ \fBunshare \-Uu sleep 1000 &\fP
216 $ \fB./ns_show /proc/23235/ns/uts u\fP
217 Device/Inode of owning user namespace is: [0,3] / 4026532448
218 $ \fBreadlink /proc/23235/ns/user\fP
223 Then show that the parent of the new user namespace in the preceding
224 example is the initial user namespace:
228 $ \fBreadlink /proc/self/ns/user\fP
230 $ \fB./ns_show /proc/23235/ns/user p\fP
231 Device/Inode of parent namespace is: [0,3] / 4026531837
235 Start a shell in a new user namespace, and show that from within
236 this shell, the parent user namespace can't be discovered.
237 Similarly, the UTS namespace
238 (which is associated with the initial user namespace)
243 $ \fBPS1="sh2$ " unshare \-U bash\fP
244 sh2$ \fB./ns_show /proc/self/ns/user p\fP
245 The parent namespace is outside your namespace scope
246 sh2$ \fB./ns_show /proc/self/ns/uts u\fP
247 The owning user namespace is outside your namespace scope
255 Licensed under the GNU General Public License v2 or later.
263 #include <sys/stat.h>
264 #include <sys/ioctl.h>
266 #include <sys/sysmacros.h>
268 #ifndef NS_GET_USERNS
270 #define NS_GET_USERNS _IO(NSIO, 0x1)
271 #define NS_GET_PARENT _IO(NSIO, 0x2)
275 main(int argc, char *argv[])
277 int fd, userns_fd, parent_fd;
281 fprintf(stderr, "Usage: %s /proc/[pid]/ns/[file] [p|u]\en",
283 fprintf(stderr, "\enDisplay the result of one or both "
284 "of NS_GET_USERNS (u) or NS_GET_PARENT (p)\en"
285 "for the specified /proc/[pid]/ns/[file]. If neither "
286 "\(aqp\(aq nor \(aqu\(aq is specified,\en"
287 "NS_GET_USERNS is the default.\en");
291 /* Obtain a file descriptor for the \(aqns\(aq file specified
294 fd = open(argv[1], O_RDONLY);
300 /* Obtain a file descriptor for the owning user namespace and
301 then obtain and display the inode number of that namespace. */
303 if (argc < 3 || strchr(argv[2], \(aqu\(aq)) {
304 userns_fd = ioctl(fd, NS_GET_USERNS);
306 if (userns_fd == \-1) {
308 printf("The owning user namespace is outside "
309 "your namespace scope\en");
311 perror("ioctl\-NS_GET_USERNS");
315 if (fstat(userns_fd, &sb) == \-1) {
316 perror("fstat\-userns");
319 printf("Device/Inode of owning user namespace is: "
320 "[%jx,%jx] / %ju\en",
321 (uintmax_t) major(sb.st_dev),
322 (uintmax_t) minor(sb.st_dev),
323 (uintmax_t) sb.st_ino);
328 /* Obtain a file descriptor for the parent namespace and
329 then obtain and display the inode number of that namespace. */
331 if (argc > 2 && strchr(argv[2], \(aqp\(aq)) {
332 parent_fd = ioctl(fd, NS_GET_PARENT);
334 if (parent_fd == \-1) {
336 printf("Can\(aq get parent namespace of a "
337 "nonhierarchical namespace\en");
338 else if (errno == EPERM)
339 printf("The parent namespace is outside "
340 "your namespace scope\en");
342 perror("ioctl\-NS_GET_PARENT");
346 if (fstat(parent_fd, &sb) == \-1) {
347 perror("fstat\-parentns");
350 printf("Device/Inode of parent namespace is: [%jx,%jx] / %ju\en",
351 (uintmax_t) major(sb.st_dev),
352 (uintmax_t) minor(sb.st_dev),
353 (uintmax_t) sb.st_ino);