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