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