]> granicus.if.org Git - strace/blob - tests/ioctl_nsfs.c
tests: use fixed timestamps in utime related tests
[strace] / tests / ioctl_nsfs.c
1 /*
2  * Check decoding of NS_* commands of ioctl syscall.
3  *
4  * Copyright (c) 2017 Nikolay Marchuk <marchuk.nikolay.a@gmail.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #include <fcntl.h>
33 #include <sched.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <sys/ioctl.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39 #include "nsfs.h"
40
41 #ifndef CLONE_NEWUSER
42 # define CLONE_NEWUSER 0x10000000
43 #endif
44
45 static void
46 test_no_namespace(void)
47 {
48         ioctl(-1, NS_GET_USERNS);
49         printf("ioctl(-1, NS_GET_USERNS) = -1 EBADF (%m)\n");
50         ioctl(-1, NS_GET_PARENT);
51         printf("ioctl(-1, NS_GET_PARENT) = -1 EBADF (%m)\n");
52         ioctl(-1, NS_GET_NSTYPE);
53         printf("ioctl(-1, NS_GET_NSTYPE) = -1 EBADF (%m)\n");
54         ioctl(-1, NS_GET_OWNER_UID, NULL);
55         printf("ioctl(-1, NS_GET_OWNER_UID, NULL) = -1 EBADF (%m)\n");
56 }
57
58 static void
59 test_clone(pid_t pid)
60 {
61         char path[sizeof("/proc/%d/ns/user") + sizeof(int)*3];
62         snprintf(path, sizeof(path), "/proc/%d/ns/user", pid);
63
64         int ns_fd = open(path, O_RDONLY);
65         if (ns_fd == -1)
66                 perror_msg_and_skip("open: %s", path);
67
68         int userns_fd = ioctl(ns_fd, NS_GET_USERNS);
69         printf("ioctl(%d, NS_GET_USERNS) = %s\n", ns_fd, sprintrc(userns_fd));
70
71         int parent_ns_fd = ioctl(userns_fd, NS_GET_PARENT);
72         printf("ioctl(%d, NS_GET_PARENT) = %s\n",
73                userns_fd, sprintrc(parent_ns_fd));
74
75         int nstype = ioctl(userns_fd, NS_GET_NSTYPE);
76         if (nstype == -1) {
77                 printf("ioctl(%d, NS_GET_NSTYPE) = %s\n",
78                        userns_fd, sprintrc(nstype));
79         } else {
80                 printf("ioctl(%d, NS_GET_NSTYPE) = %d (CLONE_NEWUSER)\n",
81                        userns_fd, nstype);
82         }
83
84         TAIL_ALLOC_OBJECT_CONST_PTR(unsigned int, uid);
85         int rc = ioctl(userns_fd, NS_GET_OWNER_UID, uid);
86         if (rc == -1) {
87                 printf("ioctl(%d, NS_GET_OWNER_UID, %p) = %s\n",
88                        userns_fd, uid, sprintrc(rc));
89         } else {
90                 printf("ioctl(%d, NS_GET_OWNER_UID, [%u]) = %d\n",
91                        userns_fd, *uid, rc);
92         }
93 }
94
95 static int
96 child(void *arg)
97 {
98         int *pipefd = (int *) arg;
99         close(pipefd[1]);
100         /* Wait for EOF from pipe. */
101         if (read(pipefd[0], &pipefd[1], 1))
102                 perror_msg_and_fail("read");
103         return 0;
104 }
105
106 #ifdef IA64
107 extern int __clone2(int (*)(void *), void *, size_t, int, void *, ...);
108 # define clone(fn, child_stack, flags, arg)     \
109                 __clone2(fn, child_stack, get_page_size() / 2, flags, arg)
110 #endif
111
112 static void
113 test_user_namespace(void)
114 {
115         pid_t pid;
116         int pipefd[2];
117         int status;
118
119         if (pipe(pipefd))
120                 perror_msg_and_fail("pipe");
121
122         pid = clone(child, tail_alloc(get_page_size() / 2),
123                     CLONE_NEWUSER | CLONE_UNTRACED | SIGCHLD, pipefd);
124         if (pid == -1) {
125                 perror("clone");
126                 return;
127         }
128         close(pipefd[0]);
129         test_clone(pid);
130         close(pipefd[1]);
131         if (wait(&status) != pid) {
132                 perror_msg_and_fail("wait");
133         } else if (status != 0) {
134                 error_msg_and_fail("unexpected child exit status %d", status);
135         }
136 }
137
138 int
139 main(void)
140 {
141         test_no_namespace();
142         test_user_namespace();
143         puts("+++ exited with 0 +++");
144         return 0;
145 }