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