1 .\" Copyright (c) 2017 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH IOCTL_NS 2 2022-09-17 "Linux man-pages (unreleased)"
8 ioctl_ns \- ioctl() operations for Linux namespaces
10 .\" ============================================================
12 .SS Discovering namespace relationships
15 operations are provided to allow discovery of namespace relationships (see
16 .BR user_namespaces (7)
18 .BR pid_namespaces (7)).
19 The form of the calls is:
23 new_fd = ioctl(fd, request);
32 Both operations return a new file descriptor on success.
34 .BR NS_GET_USERNS " (since Linux 4.9)"
35 .\" commit bcac25a58bfc6bd79191ac5d7afb49bea96da8c9
36 .\" commit 6786741dbf99e44fb0c0ed85a37582b8a26f1c3b
37 Returns a file descriptor that refers to the owning user namespace
38 for the namespace referred to by
41 .BR NS_GET_PARENT " (since Linux 4.9)"
42 .\" commit a7306ed8d94af729ecef8b6e37506a1c6fc14788
43 Returns a file descriptor that refers to the parent namespace of
44 the namespace referred to by
46 This operation is valid only for hierarchical namespaces
47 (i.e., PID and user namespaces).
53 The new file descriptor returned by these operations is opened with the
63 to the returned file descriptor, one obtains a
69 (inode number) fields together identify the owning/parent namespace.
70 This inode number can be matched with the inode number of another
71 .IR /proc/ pid /ns/ { pid , user }
72 file to determine whether that is the owning/parent namespace.
76 operations can fail with the following errors:
79 The requested namespace is outside of the caller's namespace scope.
80 This error can occur if, for example, the owning user namespace is an
81 ancestor of the caller's current user namespace.
82 It can also occur on attempts to obtain the parent of the initial
83 user or PID namespace.
86 The operation is not supported by this kernel version.
90 operation can fail with the following error:
94 refers to a nonhierarchical namespace.
96 See the EXAMPLES section for an example of the use of these operations.
97 .\" ============================================================
99 .SS Discovering the namespace type
102 .\" commit e5ff5ce6e20ee22511398bb31fb912466cf82a36
103 operation (available since Linux 4.11) can be used to discover
104 the type of namespace referred to by the file descriptor
109 nstype = ioctl(fd, NS_GET_NSTYPE);
118 The return value is one of the
120 values that can be specified to
124 in order to create a namespace.
125 .\" ============================================================
127 .SS Discovering the owner of a user namespace
130 .\" commit 015bb305b8ebe8d601a238ab70ebdc394c7a19ba
131 operation (available since Linux 4.11) can be used to discover
132 the owner user ID of a user namespace (i.e., the effective user ID
133 of the process that created the user namespace).
134 The form of the call is:
139 ioctl(fd, NS_GET_OWNER_UID, &uid);
145 .IR /proc/ pid /ns/user
148 The owner user ID is returned in the
150 pointed to by the third argument.
152 This operation can fail with the following error:
156 does not refer to a user namespace.
160 operations can return the following errors:
168 Namespaces and the operations described on this page are a Linux-specific.
170 The example shown below uses the
172 operations described above to perform simple
173 discovery of namespace relationships.
174 The following shell sessions show various examples of the use
177 Trying to get the parent of the initial user namespace fails,
178 since it has no parent:
182 $ \fB./ns_show /proc/self/ns/user p\fP
183 The parent namespace is outside your namespace scope
187 Create a process running
189 that resides in new user and UTS namespaces,
190 and show that the new UTS namespace is associated with the new user namespace:
194 $ \fBunshare \-Uu sleep 1000 &\fP
196 $ \fB./ns_show /proc/23235/ns/uts u\fP
197 Device/Inode of owning user namespace is: [0,3] / 4026532448
198 $ \fBreadlink /proc/23235/ns/user\fP
203 Then show that the parent of the new user namespace in the preceding
204 example is the initial user namespace:
208 $ \fBreadlink /proc/self/ns/user\fP
210 $ \fB./ns_show /proc/23235/ns/user p\fP
211 Device/Inode of parent namespace is: [0,3] / 4026531837
215 Start a shell in a new user namespace, and show that from within
216 this shell, the parent user namespace can't be discovered.
217 Similarly, the UTS namespace
218 (which is associated with the initial user namespace)
223 $ \fBPS1="sh2$ " unshare \-U bash\fP
224 sh2$ \fB./ns_show /proc/self/ns/user p\fP
225 The parent namespace is outside your namespace scope
226 sh2$ \fB./ns_show /proc/self/ns/uts u\fP
227 The owning user namespace is outside your namespace scope
232 .\" SRC BEGIN (ns_show.c)
236 Licensed under the GNU General Public License v2 or later.
240 #include <linux/nsfs.h>
245 #include <sys/ioctl.h>
246 #include <sys/stat.h>
247 #include <sys/sysmacros.h>
251 main(int argc, char *argv[])
253 int fd, userns_fd, parent_fd;
257 fprintf(stderr, "Usage: %s /proc/[pid]/ns/[file] [p|u]\en",
259 fprintf(stderr, "\enDisplay the result of one or both "
260 "of NS_GET_USERNS (u) or NS_GET_PARENT (p)\en"
261 "for the specified /proc/[pid]/ns/[file]. If neither "
262 "\(aqp\(aq nor \(aqu\(aq is specified,\en"
263 "NS_GET_USERNS is the default.\en");
267 /* Obtain a file descriptor for the \(aqns\(aq file specified
270 fd = open(argv[1], O_RDONLY);
276 /* Obtain a file descriptor for the owning user namespace and
277 then obtain and display the inode number of that namespace. */
279 if (argc < 3 || strchr(argv[2], \(aqu\(aq)) {
280 userns_fd = ioctl(fd, NS_GET_USERNS);
282 if (userns_fd == \-1) {
284 printf("The owning user namespace is outside "
285 "your namespace scope\en");
287 perror("ioctl\-NS_GET_USERNS");
291 if (fstat(userns_fd, &sb) == \-1) {
292 perror("fstat\-userns");
295 printf("Device/Inode of owning user namespace is: "
299 (uintmax_t) sb.st_ino);
304 /* Obtain a file descriptor for the parent namespace and
305 then obtain and display the inode number of that namespace. */
307 if (argc > 2 && strchr(argv[2], \(aqp\(aq)) {
308 parent_fd = ioctl(fd, NS_GET_PARENT);
310 if (parent_fd == \-1) {
312 printf("Can\(aq get parent namespace of a "
313 "nonhierarchical namespace\en");
314 else if (errno == EPERM)
315 printf("The parent namespace is outside "
316 "your namespace scope\en");
318 perror("ioctl\-NS_GET_PARENT");
322 if (fstat(parent_fd, &sb) == \-1) {
323 perror("fstat\-parentns");
326 printf("Device/Inode of parent namespace is: [%x,%x] / %ju\en",
329 (uintmax_t) sb.st_ino);