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