tests: compile some tools/tests as C++
[lttng-tools.git] / tests / utils / testapp / gen-ns-events / gen-ns-events.cpp
CommitLineData
5f0d4e78
MJ
1/*
2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
3 *
9d16b343 4 * SPDX-License-Identifier: LGPL-2.1-only
5f0d4e78 5 *
5f0d4e78
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 <string.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20#include <unistd.h>
21
22#include <common/compat/tid.h>
23
24#include "signal-helper.h"
25#include "utils.h"
26
27#define LTTNG_PROC_NS_PATH_MAX 40
28
93d0d1f7
JG
29/*
30 * The runner of this test validates that the kernel supports the
31 * namespace for which it is invoked. However, these defines are added
32 * to allow tests to run on systems that support a given namespace,
33 * but that use a libc that doesn't define its associated clone flag.
34 */
35#ifndef CLONE_NEWNS
36#define CLONE_NEWNS 0x00020000
37#endif
38#ifndef CLONE_NEWCGROUP
39#define CLONE_NEWCGROUP 0x02000000
40#endif
41#ifndef CLONE_NEWUTS
42#define CLONE_NEWUTS 0x04000000
43#endif
44#ifndef CLONE_NEWIPC
45#define CLONE_NEWIPC 0x08000000
46#endif
47#ifndef CLONE_NEWUSER
48#define CLONE_NEWUSER 0x10000000
49#endif
50#ifndef CLONE_NEWPID
51#define CLONE_NEWPID 0x20000000
52#endif
53#ifndef CLONE_NEWNET
54#define CLONE_NEWNET 0x40000000
55#endif
d37ac3cd
MJ
56#ifndef CLONE_NEWTIME
57#define CLONE_NEWTIME 0x00000080
58#endif
93d0d1f7 59
5f0d4e78
MJ
60static int debug = 0;
61static char *ns_opt = NULL;
62static char *before_unshare_wait_file_path = NULL;
63static char *after_unshare_wait_file_path = NULL;
64static char *after_unshare_signal_file_path = NULL;
65
66static struct 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 { "before", 'b', POPT_ARG_STRING, &before_unshare_wait_file_path, 0, "Wait for file before unshare", NULL },
71 { "after", 'a', POPT_ARG_STRING, &after_unshare_wait_file_path, 0, "Wait for file after unshare", NULL },
72 { "signal", 's', POPT_ARG_STRING, &after_unshare_signal_file_path, 0, "Create signal file after unshare", 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{
91 int ret = 0;
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;
102 } else {
103 ret = -1;
104 }
105 goto end;
106 }
107
108 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
109 "/proc/self/task/%d/%s/net", lttng_gettid(), 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 }
117end:
118 return ret;
119}
120
121static int do_the_needful(int ns_flag, const char *ns_str)
122{
123 int ret = 0;
124 ino_t ns1, ns2;
125
126 ret = get_ns_inum(ns_str, &ns1);
127 if (ret) {
128 debug_printf("Failed to get ns inode number for namespace %s",
129 ns_str);
130 ret = -1;
131 goto end;
132 }
133 debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1);
134
135 /* Wait on synchronization before unshare. */
136 if (before_unshare_wait_file_path) {
137 ret = wait_on_file(before_unshare_wait_file_path);
138 if (ret != 0) {
139 goto end;
140 }
141 }
142
143 ret = unshare(ns_flag);
144 if (ret == -1) {
145 perror("Failed to unshare namespace");
146 goto end;
147 }
148
149 ret = get_ns_inum(ns_str, &ns2);
150 if (ret) {
151 debug_printf("Failed to get ns inode number for namespace %s",
152 ns_str);
153 ret = -1;
154 goto end;
155 }
156 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
157
158 /* Signal that the unshare call is completed. */
159 if (after_unshare_signal_file_path) {
160 ret = create_file(after_unshare_signal_file_path);
161 if (ret != 0) {
162 goto end;
163 }
164 }
165
166 /* Wait on synchronization after unshare. */
167 if (after_unshare_wait_file_path) {
168 ret = wait_on_file(after_unshare_wait_file_path);
169 if (ret != 0) {
170 goto end;
171 }
172 }
173
174end:
175 return ret;
176}
177
178int main(int argc, const char **argv)
179{
180 int opt;
181 int ret = EXIT_SUCCESS;
182 poptContext pc;
183
184 pc = poptGetContext(NULL, argc, argv, opts, 0);
185 poptReadDefaultConfig(pc, 0);
186
187 if (argc < 2) {
188 poptPrintHelp(pc, stderr, 0);
189 ret = EXIT_FAILURE;
190 goto end;
191 }
192
193 while ((opt = poptGetNextOpt(pc)) >= 0) {
194 switch (opt) {
195 default:
196 poptPrintUsage(pc, stderr, 0);
197 ret = EXIT_FAILURE;
198 goto end;
199 }
200 }
201
202 if (opt < -1) {
203 /* an error occurred during option processing */
204 poptPrintUsage(pc, stderr, 0);
205 fprintf(stderr, "%s: %s\n",
206 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
207 poptStrerror(opt));
208 ret = EXIT_FAILURE;
209 goto end;
210 }
211
212 if (ns_opt == NULL) {
213 poptPrintUsage(pc, stderr, 0);
214 ret = EXIT_FAILURE;
215 goto end;
216 }
217
218 if (set_signal_handler()) {
219 ret = EXIT_FAILURE;
220 goto end;
221 }
222
d37ac3cd 223 if (strncmp(ns_opt, "cgroup", 6) == 0) {
5f0d4e78
MJ
224 ret = do_the_needful(CLONE_NEWCGROUP, "cgroup");
225 } else if (strncmp(ns_opt, "ipc", 3) == 0) {
226 ret = do_the_needful(CLONE_NEWIPC, "ipc");
227 } else if (strncmp(ns_opt, "mnt", 3) == 0) {
228 ret = do_the_needful(CLONE_NEWNS, "mnt");
229 } else if (strncmp(ns_opt, "net", 3) == 0) {
230 ret = do_the_needful(CLONE_NEWNET, "net");
231 } else if (strncmp(ns_opt, "pid", 3) == 0) {
232 ret = do_the_needful(CLONE_NEWPID, "pid");
d37ac3cd
MJ
233 } else if (strncmp(ns_opt, "time", 4) == 0) {
234 ret = do_the_needful(CLONE_NEWTIME, "time");
235 } else if (strncmp(ns_opt, "user", 4) == 0) {
5f0d4e78
MJ
236 /*
237 * Will always fail, requires a single threaded application,
238 * which can't happen with UST.
239 */
240 ret = do_the_needful(CLONE_NEWUSER, "user");
241 } else if (strncmp(ns_opt, "uts", 3) == 0) {
242 ret = do_the_needful(CLONE_NEWUTS, "uts");
243 } else {
244 printf("invalid ns id\n");
245 ret = EXIT_FAILURE;
246 goto end;
247 }
248 ret = ret ? EXIT_FAILURE : EXIT_SUCCESS;
249end:
250 poptFreeContext(pc);
251 return ret;
252}
This page took 0.037039 seconds and 4 git commands to generate.