Commit | Line | Data |
---|---|---|
5f0d4e78 MJ |
1 | /* |
2 | * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com> | |
3 | * | |
9d16b343 | 4 | * SPDX-License-Identifier: LGPL-2.1-only |
5f0d4e78 | 5 | * |
5f0d4e78 MJ |
6 | */ |
7 | ||
8 | #ifndef _GNU_SOURCE | |
9 | #define _GNU_SOURCE | |
10 | #endif | |
11 | ||
28ab034a JG |
12 | #include "signal-helper.hpp" |
13 | #include "utils.h" | |
14 | ||
15 | #include <common/compat/tid.hpp> | |
16 | #include <common/macros.hpp> | |
17 | ||
18 | #include <inttypes.h> | |
5f0d4e78 MJ |
19 | #include <popt.h> |
20 | #include <sched.h> | |
21 | #include <stdarg.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/types.h> | |
27 | #include <unistd.h> | |
5f0d4e78 MJ |
28 | |
29 | #define LTTNG_PROC_NS_PATH_MAX 40 | |
30 | ||
93d0d1f7 JG |
31 | /* |
32 | * The runner of this test validates that the kernel supports the | |
33 | * namespace for which it is invoked. However, these defines are added | |
34 | * to allow tests to run on systems that support a given namespace, | |
35 | * but that use a libc that doesn't define its associated clone flag. | |
36 | */ | |
37 | #ifndef CLONE_NEWNS | |
28ab034a | 38 | #define CLONE_NEWNS 0x00020000 |
93d0d1f7 JG |
39 | #endif |
40 | #ifndef CLONE_NEWCGROUP | |
41 | #define CLONE_NEWCGROUP 0x02000000 | |
42 | #endif | |
43 | #ifndef CLONE_NEWUTS | |
28ab034a | 44 | #define CLONE_NEWUTS 0x04000000 |
93d0d1f7 JG |
45 | #endif |
46 | #ifndef CLONE_NEWIPC | |
28ab034a | 47 | #define CLONE_NEWIPC 0x08000000 |
93d0d1f7 JG |
48 | #endif |
49 | #ifndef CLONE_NEWUSER | |
28ab034a | 50 | #define CLONE_NEWUSER 0x10000000 |
93d0d1f7 JG |
51 | #endif |
52 | #ifndef CLONE_NEWPID | |
28ab034a | 53 | #define CLONE_NEWPID 0x20000000 |
93d0d1f7 JG |
54 | #endif |
55 | #ifndef CLONE_NEWNET | |
28ab034a | 56 | #define CLONE_NEWNET 0x40000000 |
93d0d1f7 | 57 | #endif |
d37ac3cd | 58 | #ifndef CLONE_NEWTIME |
28ab034a | 59 | #define CLONE_NEWTIME 0x00000080 |
d37ac3cd | 60 | #endif |
93d0d1f7 | 61 | |
5f0d4e78 | 62 | static int debug = 0; |
cd9adb8b JG |
63 | static char *ns_opt = nullptr; |
64 | static char *before_unshare_wait_file_path = nullptr; | |
65 | static char *after_unshare_wait_file_path = nullptr; | |
66 | static char *after_unshare_signal_file_path = nullptr; | |
5f0d4e78 MJ |
67 | |
68 | static struct poptOption opts[] = { | |
69 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
cd9adb8b JG |
70 | { "debug", 'd', POPT_ARG_NONE, &debug, 0, "Enable debug output", nullptr }, |
71 | { "ns", 'n', POPT_ARG_STRING, &ns_opt, 0, "Namespace short identifier", nullptr }, | |
28ab034a JG |
72 | { "before", |
73 | 'b', | |
74 | POPT_ARG_STRING, | |
75 | &before_unshare_wait_file_path, | |
76 | 0, | |
77 | "Wait for file before unshare", | |
cd9adb8b | 78 | nullptr }, |
28ab034a JG |
79 | { "after", |
80 | 'a', | |
81 | POPT_ARG_STRING, | |
82 | &after_unshare_wait_file_path, | |
83 | 0, | |
84 | "Wait for file after unshare", | |
cd9adb8b | 85 | nullptr }, |
28ab034a JG |
86 | { "signal", |
87 | 's', | |
88 | POPT_ARG_STRING, | |
89 | &after_unshare_signal_file_path, | |
90 | 0, | |
91 | "Create signal file after unshare", | |
cd9adb8b JG |
92 | nullptr }, |
93 | POPT_AUTOHELP{ nullptr, 0, 0, nullptr, 0, nullptr, nullptr } | |
5f0d4e78 MJ |
94 | }; |
95 | ||
28ab034a | 96 | static ATTR_FORMAT_PRINTF(1, 2) void debug_printf(const char *format, ...) |
5f0d4e78 MJ |
97 | { |
98 | va_list args; | |
99 | va_start(args, format); | |
100 | ||
101 | if (debug) { | |
102 | vfprintf(stderr, format, args); | |
103 | } | |
104 | ||
105 | va_end(args); | |
106 | } | |
107 | ||
108 | static int get_ns_inum(const char *ns, ino_t *ns_inum) | |
109 | { | |
110 | int ret = 0; | |
111 | struct stat sb; | |
112 | char proc_ns_path[LTTNG_PROC_NS_PATH_MAX]; | |
113 | ||
114 | /* | |
115 | * /proc/thread-self was introduced in kernel v3.17 | |
116 | */ | |
28ab034a | 117 | if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX, "/proc/thread-self/ns/%s", ns) >= 0) { |
5f0d4e78 MJ |
118 | if (stat(proc_ns_path, &sb) == 0) { |
119 | *ns_inum = sb.st_ino; | |
120 | } else { | |
121 | ret = -1; | |
122 | } | |
123 | goto end; | |
124 | } | |
125 | ||
28ab034a JG |
126 | if (snprintf(proc_ns_path, |
127 | LTTNG_PROC_NS_PATH_MAX, | |
128 | "/proc/self/task/%d/%s/net", | |
129 | lttng_gettid(), | |
130 | ns) >= 0) { | |
5f0d4e78 MJ |
131 | if (stat(proc_ns_path, &sb) == 0) { |
132 | *ns_inum = sb.st_ino; | |
133 | } else { | |
134 | ret = -1; | |
135 | } | |
136 | goto end; | |
137 | } | |
138 | end: | |
139 | return ret; | |
140 | } | |
141 | ||
142 | static int do_the_needful(int ns_flag, const char *ns_str) | |
143 | { | |
144 | int ret = 0; | |
145 | ino_t ns1, ns2; | |
146 | ||
147 | ret = get_ns_inum(ns_str, &ns1); | |
148 | if (ret) { | |
28ab034a | 149 | debug_printf("Failed to get ns inode number for namespace %s", ns_str); |
5f0d4e78 MJ |
150 | ret = -1; |
151 | goto end; | |
152 | } | |
dfd6e9d7 | 153 | debug_printf("Initial %s ns inode number: %" PRIuMAX "\n", ns_str, (uintmax_t) ns1); |
5f0d4e78 MJ |
154 | |
155 | /* Wait on synchronization before unshare. */ | |
156 | if (before_unshare_wait_file_path) { | |
157 | ret = wait_on_file(before_unshare_wait_file_path); | |
158 | if (ret != 0) { | |
159 | goto end; | |
160 | } | |
161 | } | |
162 | ||
163 | ret = unshare(ns_flag); | |
164 | if (ret == -1) { | |
165 | perror("Failed to unshare namespace"); | |
166 | goto end; | |
167 | } | |
168 | ||
169 | ret = get_ns_inum(ns_str, &ns2); | |
170 | if (ret) { | |
28ab034a | 171 | debug_printf("Failed to get ns inode number for namespace %s", ns_str); |
5f0d4e78 MJ |
172 | ret = -1; |
173 | goto end; | |
174 | } | |
dfd6e9d7 | 175 | debug_printf("Post unshare %s ns inode number: %" PRIuMAX "\n", ns_str, (uintmax_t) ns2); |
5f0d4e78 MJ |
176 | |
177 | /* Signal that the unshare call is completed. */ | |
178 | if (after_unshare_signal_file_path) { | |
179 | ret = create_file(after_unshare_signal_file_path); | |
180 | if (ret != 0) { | |
181 | goto end; | |
182 | } | |
183 | } | |
184 | ||
185 | /* Wait on synchronization after unshare. */ | |
186 | if (after_unshare_wait_file_path) { | |
187 | ret = wait_on_file(after_unshare_wait_file_path); | |
188 | if (ret != 0) { | |
189 | goto end; | |
190 | } | |
191 | } | |
192 | ||
193 | end: | |
194 | return ret; | |
195 | } | |
196 | ||
197 | int main(int argc, const char **argv) | |
198 | { | |
199 | int opt; | |
200 | int ret = EXIT_SUCCESS; | |
201 | poptContext pc; | |
202 | ||
cd9adb8b | 203 | pc = poptGetContext(nullptr, argc, argv, opts, 0); |
5f0d4e78 MJ |
204 | poptReadDefaultConfig(pc, 0); |
205 | ||
206 | if (argc < 2) { | |
207 | poptPrintHelp(pc, stderr, 0); | |
208 | ret = EXIT_FAILURE; | |
209 | goto end; | |
210 | } | |
211 | ||
212 | while ((opt = poptGetNextOpt(pc)) >= 0) { | |
213 | switch (opt) { | |
214 | default: | |
215 | poptPrintUsage(pc, stderr, 0); | |
216 | ret = EXIT_FAILURE; | |
217 | goto end; | |
218 | } | |
219 | } | |
220 | ||
221 | if (opt < -1) { | |
222 | /* an error occurred during option processing */ | |
223 | poptPrintUsage(pc, stderr, 0); | |
28ab034a JG |
224 | fprintf(stderr, |
225 | "%s: %s\n", | |
5f0d4e78 MJ |
226 | poptBadOption(pc, POPT_BADOPTION_NOALIAS), |
227 | poptStrerror(opt)); | |
228 | ret = EXIT_FAILURE; | |
229 | goto end; | |
230 | } | |
231 | ||
cd9adb8b | 232 | if (ns_opt == nullptr) { |
5f0d4e78 MJ |
233 | poptPrintUsage(pc, stderr, 0); |
234 | ret = EXIT_FAILURE; | |
235 | goto end; | |
236 | } | |
237 | ||
238 | if (set_signal_handler()) { | |
239 | ret = EXIT_FAILURE; | |
240 | goto end; | |
241 | } | |
242 | ||
d37ac3cd | 243 | if (strncmp(ns_opt, "cgroup", 6) == 0) { |
5f0d4e78 MJ |
244 | ret = do_the_needful(CLONE_NEWCGROUP, "cgroup"); |
245 | } else if (strncmp(ns_opt, "ipc", 3) == 0) { | |
246 | ret = do_the_needful(CLONE_NEWIPC, "ipc"); | |
247 | } else if (strncmp(ns_opt, "mnt", 3) == 0) { | |
248 | ret = do_the_needful(CLONE_NEWNS, "mnt"); | |
249 | } else if (strncmp(ns_opt, "net", 3) == 0) { | |
250 | ret = do_the_needful(CLONE_NEWNET, "net"); | |
251 | } else if (strncmp(ns_opt, "pid", 3) == 0) { | |
252 | ret = do_the_needful(CLONE_NEWPID, "pid"); | |
d37ac3cd MJ |
253 | } else if (strncmp(ns_opt, "time", 4) == 0) { |
254 | ret = do_the_needful(CLONE_NEWTIME, "time"); | |
255 | } else if (strncmp(ns_opt, "user", 4) == 0) { | |
5f0d4e78 MJ |
256 | /* |
257 | * Will always fail, requires a single threaded application, | |
258 | * which can't happen with UST. | |
259 | */ | |
260 | ret = do_the_needful(CLONE_NEWUSER, "user"); | |
261 | } else if (strncmp(ns_opt, "uts", 3) == 0) { | |
262 | ret = do_the_needful(CLONE_NEWUTS, "uts"); | |
263 | } else { | |
264 | printf("invalid ns id\n"); | |
265 | ret = EXIT_FAILURE; | |
266 | goto end; | |
267 | } | |
268 | ret = ret ? EXIT_FAILURE : EXIT_SUCCESS; | |
269 | end: | |
270 | poptFreeContext(pc); | |
271 | return ret; | |
272 | } |