2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
12 #include "signal-helper.hpp"
15 #include <common/compat/tid.hpp>
16 #include <common/macros.hpp>
25 #include <sys/types.h>
28 #define TRACEPOINT_DEFINE
31 #define LTTNG_PROC_NS_PATH_MAX 40
34 * The runner of this test validates that the kernel supports the
35 * namespace for which it is invoked. However, these defines are added
36 * to allow tests to run on systems that support a given namespace,
37 * but that use a libc that doesn't define its associated clone flag.
40 #define CLONE_NEWNS 0x00020000
42 #ifndef CLONE_NEWCGROUP
43 #define CLONE_NEWCGROUP 0x02000000
46 #define CLONE_NEWUTS 0x04000000
49 #define CLONE_NEWIPC 0x08000000
52 #define CLONE_NEWUSER 0x10000000
55 #define CLONE_NEWPID 0x20000000
58 #define CLONE_NEWNET 0x40000000
61 #define CLONE_NEWTIME 0x00000080
64 static int nr_iter
= 100;
66 static char *ns_opt
= nullptr;
67 static char *after_unshare_file_path
= nullptr;
68 static char *before_second_event_file_path
= nullptr;
70 static struct poptOption opts
[] = {
71 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
72 { "debug", 'd', POPT_ARG_NONE
, &debug
, 0, "Enable debug output", nullptr },
73 { "ns", 'n', POPT_ARG_STRING
, &ns_opt
, 0, "Namespace short identifier", nullptr },
74 { "iter", 'i', POPT_ARG_INT
, &nr_iter
, 0, "Number of tracepoint iterations", nullptr },
78 &after_unshare_file_path
,
80 "after_unshare_file_path,",
85 &before_second_event_file_path
,
87 "before_second_event_file_path,",
89 POPT_AUTOHELP
{ nullptr, 0, 0, nullptr, 0 }
92 static ATTR_FORMAT_PRINTF(1, 2) void debug_printf(const char *format
, ...)
95 va_start(args
, format
);
98 vfprintf(stderr
, format
, args
);
104 static int get_ns_inum(const char *ns
, ino_t
*ns_inum
)
108 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
111 * /proc/thread-self was introduced in kernel v3.17
113 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
, "/proc/thread-self/ns/%s", ns
) >= 0) {
114 if (stat(proc_ns_path
, &sb
) == 0) {
115 *ns_inum
= sb
.st_ino
;
121 if (snprintf(proc_ns_path
,
122 LTTNG_PROC_NS_PATH_MAX
,
123 "/proc/self/task/%d/%s/net",
126 if (stat(proc_ns_path
, &sb
) == 0) {
127 *ns_inum
= sb
.st_ino
;
136 static int do_the_needful(int ns_flag
, const char *ns_str
)
141 ret
= get_ns_inum(ns_str
, &ns1
);
143 debug_printf("Failed to get ns inode number for namespace %s", ns_str
);
147 debug_printf("Initial %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns1
);
149 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
150 tracepoint(tp
, tptest
, ns1
);
156 ret
= unshare(ns_flag
);
158 perror("Failed to unshare namespace");
162 ret
= get_ns_inum(ns_str
, &ns2
);
164 debug_printf("Failed to get ns inode number for namespace %s", ns_str
);
168 debug_printf("Post unshare %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns2
);
171 * Signal that we emited the first event group and that the
172 * unshare call is completed.
174 if (after_unshare_file_path
) {
175 ret
= create_file(after_unshare_file_path
);
181 /* Wait on synchronization before writing second event group. */
182 if (before_second_event_file_path
) {
183 ret
= wait_on_file(before_second_event_file_path
);
189 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
190 tracepoint(tp
, tptest
, ns2
);
201 * Send X events, change NS, wait for file to sync with test script, send X
204 int main(int argc
, const char **argv
)
207 int ret
= EXIT_SUCCESS
;
210 pc
= poptGetContext(nullptr, argc
, argv
, opts
, 0);
211 poptReadDefaultConfig(pc
, 0);
214 poptPrintHelp(pc
, stderr
, 0);
219 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
222 poptPrintUsage(pc
, stderr
, 0);
229 /* An error occurred during option processing. */
230 poptPrintUsage(pc
, stderr
, 0);
233 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
239 if (ns_opt
== nullptr) {
240 poptPrintUsage(pc
, stderr
, 0);
245 if (set_signal_handler()) {
250 if (strncmp(ns_opt
, "cgroup", 6) == 0) {
251 ret
= do_the_needful(CLONE_NEWCGROUP
, "cgroup");
252 } else if (strncmp(ns_opt
, "ipc", 3) == 0) {
253 ret
= do_the_needful(CLONE_NEWIPC
, "ipc");
254 } else if (strncmp(ns_opt
, "mnt", 3) == 0) {
255 ret
= do_the_needful(CLONE_NEWNS
, "mnt");
256 } else if (strncmp(ns_opt
, "net", 3) == 0) {
257 ret
= do_the_needful(CLONE_NEWNET
, "net");
258 } else if (strncmp(ns_opt
, "pid", 3) == 0) {
259 ret
= do_the_needful(CLONE_NEWPID
, "pid");
260 } else if (strncmp(ns_opt
, "time", 4) == 0) {
261 ret
= do_the_needful(CLONE_NEWTIME
, "time");
262 } else if (strncmp(ns_opt
, "user", 4) == 0) {
264 * Will always fail, requires a single threaded application,
265 * which can't happen with UST.
267 ret
= do_the_needful(CLONE_NEWUSER
, "user");
268 } else if (strncmp(ns_opt
, "uts", 3) == 0) {
269 ret
= do_the_needful(CLONE_NEWUTS
, "uts");
271 printf("invalid ns id\n");
275 ret
= ret
? EXIT_FAILURE
: EXIT_SUCCESS
;