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