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