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