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