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
CommitLineData
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
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>
8a558304
MJ
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>
8a558304
MJ
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
28ab034a 40#define CLONE_NEWNS 0x00020000
93d0d1f7
JG
41#endif
42#ifndef CLONE_NEWCGROUP
43#define CLONE_NEWCGROUP 0x02000000
44#endif
45#ifndef CLONE_NEWUTS
28ab034a 46#define CLONE_NEWUTS 0x04000000
93d0d1f7
JG
47#endif
48#ifndef CLONE_NEWIPC
28ab034a 49#define CLONE_NEWIPC 0x08000000
93d0d1f7
JG
50#endif
51#ifndef CLONE_NEWUSER
28ab034a 52#define CLONE_NEWUSER 0x10000000
93d0d1f7
JG
53#endif
54#ifndef CLONE_NEWPID
28ab034a 55#define CLONE_NEWPID 0x20000000
93d0d1f7
JG
56#endif
57#ifndef CLONE_NEWNET
28ab034a 58#define CLONE_NEWNET 0x40000000
93d0d1f7 59#endif
d37ac3cd 60#ifndef CLONE_NEWTIME
28ab034a 61#define CLONE_NEWTIME 0x00000080
d37ac3cd 62#endif
93d0d1f7 63
8a558304
MJ
64static int nr_iter = 100;
65static int debug = 0;
cd9adb8b
JG
66static char *ns_opt = nullptr;
67static char *after_unshare_file_path = nullptr;
68static char *before_second_event_file_path = nullptr;
8a558304 69
28ab034a 70static struct poptOption opts[] = {
8a558304 71 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
cd9adb8b
JG
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 },
28ab034a
JG
75 { "after",
76 'a',
77 POPT_ARG_STRING,
78 &after_unshare_file_path,
79 0,
80 "after_unshare_file_path,",
cd9adb8b 81 nullptr },
28ab034a
JG
82 { "before",
83 'b',
84 POPT_ARG_STRING,
85 &before_second_event_file_path,
86 0,
87 "before_second_event_file_path,",
cd9adb8b
JG
88 nullptr },
89 POPT_AUTOHELP{ nullptr, 0, 0, nullptr, 0 }
8a558304
MJ
90};
91
28ab034a 92static ATTR_FORMAT_PRINTF(1, 2) void debug_printf(const char *format, ...)
8a558304
MJ
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
104static int get_ns_inum(const char *ns, ino_t *ns_inum)
105{
3ad7f5fe 106 int ret = -1;
8a558304
MJ
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 */
28ab034a 113 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX, "/proc/thread-self/ns/%s", ns) >= 0) {
8a558304
MJ
114 if (stat(proc_ns_path, &sb) == 0) {
115 *ns_inum = sb.st_ino;
3ad7f5fe 116 ret = 0;
8a558304
MJ
117 }
118 goto end;
119 }
120
28ab034a
JG
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) {
8a558304
MJ
126 if (stat(proc_ns_path, &sb) == 0) {
127 *ns_inum = sb.st_ino;
3ad7f5fe 128 ret = 0;
8a558304
MJ
129 }
130 goto end;
131 }
132end:
133 return ret;
134}
135
136static 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) {
28ab034a 143 debug_printf("Failed to get ns inode number for namespace %s", ns_str);
8a558304
MJ
144 ret = -1;
145 goto end;
146 }
dfd6e9d7 147 debug_printf("Initial %s ns inode number: %" PRIuMAX "\n", ns_str, (uintmax_t) ns1);
8a558304
MJ
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) {
28ab034a 164 debug_printf("Failed to get ns inode number for namespace %s", ns_str);
8a558304
MJ
165 ret = -1;
166 goto end;
167 }
dfd6e9d7 168 debug_printf("Post unshare %s ns inode number: %" PRIuMAX "\n", ns_str, (uintmax_t) ns2);
8a558304
MJ
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
196end:
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 */
204int main(int argc, const char **argv)
205{
206 int opt;
207 int ret = EXIT_SUCCESS;
208 poptContext pc;
209
cd9adb8b 210 pc = poptGetContext(nullptr, argc, argv, opts, 0);
8a558304
MJ
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);
28ab034a
JG
231 fprintf(stderr,
232 "%s: %s\n",
233 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
234 poptStrerror(opt));
8a558304
MJ
235 ret = EXIT_FAILURE;
236 goto end;
237 }
238
cd9adb8b 239 if (ns_opt == nullptr) {
8a558304
MJ
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
d37ac3cd 250 if (strncmp(ns_opt, "cgroup", 6) == 0) {
8a558304
MJ
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");
d37ac3cd
MJ
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) {
8a558304
MJ
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;
276end:
277 poptFreeContext(pc);
278 return ret;
279}
This page took 0.05925 seconds and 4 git commands to generate.