2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
18 #include <sys/types.h>
22 #include <common/compat/tid.h>
23 #include <common/macros.h>
25 #include "signal-helper.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
= NULL
;
67 static char *after_unshare_file_path
= NULL
;
68 static char *before_second_event_file_path
= NULL
;
71 struct poptOption opts
[] = {
72 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
73 { "debug", 'd', POPT_ARG_NONE
, &debug
, 0, "Enable debug output", NULL
},
74 { "ns", 'n', POPT_ARG_STRING
, &ns_opt
, 0, "Namespace short identifier", NULL
},
75 { "iter", 'i', POPT_ARG_INT
, &nr_iter
, 0, "Number of tracepoint iterations", NULL
},
76 { "after", 'a', POPT_ARG_STRING
, &after_unshare_file_path
, 0, "after_unshare_file_path,", NULL
},
77 { "before", 'b', POPT_ARG_STRING
, &before_second_event_file_path
, 0, "before_second_event_file_path,", NULL
},
79 { NULL
, 0, 0, NULL
, 0 }
82 static ATTR_FORMAT_PRINTF(1, 2)
83 void debug_printf(const char *format
, ...)
86 va_start(args
, format
);
89 vfprintf(stderr
, format
, args
);
95 static int get_ns_inum(const char *ns
, ino_t
*ns_inum
)
99 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
102 * /proc/thread-self was introduced in kernel v3.17
104 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
105 "/proc/thread-self/ns/%s", ns
) >= 0) {
106 if (stat(proc_ns_path
, &sb
) == 0) {
107 *ns_inum
= sb
.st_ino
;
113 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
114 "/proc/self/task/%d/%s/net", lttng_gettid(), ns
) >= 0) {
115 if (stat(proc_ns_path
, &sb
) == 0) {
116 *ns_inum
= sb
.st_ino
;
125 static int do_the_needful(int ns_flag
, const char *ns_str
)
130 ret
= get_ns_inum(ns_str
, &ns1
);
132 debug_printf("Failed to get ns inode number for namespace %s",
137 debug_printf("Initial %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns1
);
139 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
140 tracepoint(tp
, tptest
, ns1
);
146 ret
= unshare(ns_flag
);
148 perror("Failed to unshare namespace");
152 ret
= get_ns_inum(ns_str
, &ns2
);
154 debug_printf("Failed to get ns inode number for namespace %s",
159 debug_printf("Post unshare %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns2
);
162 * Signal that we emited the first event group and that the
163 * unshare call is completed.
165 if (after_unshare_file_path
) {
166 ret
= create_file(after_unshare_file_path
);
172 /* Wait on synchronization before writing second event group. */
173 if (before_second_event_file_path
) {
174 ret
= wait_on_file(before_second_event_file_path
);
180 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
181 tracepoint(tp
, tptest
, ns2
);
192 * Send X events, change NS, wait for file to sync with test script, send X
195 int main(int argc
, const char **argv
)
198 int ret
= EXIT_SUCCESS
;
201 pc
= poptGetContext(NULL
, argc
, argv
, opts
, 0);
202 poptReadDefaultConfig(pc
, 0);
205 poptPrintHelp(pc
, stderr
, 0);
210 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
213 poptPrintUsage(pc
, stderr
, 0);
220 /* An error occurred during option processing. */
221 poptPrintUsage(pc
, stderr
, 0);
222 fprintf(stderr
, "%s: %s\n",
223 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
229 if (ns_opt
== NULL
) {
230 poptPrintUsage(pc
, stderr
, 0);
235 if (set_signal_handler()) {
240 if (strncmp(ns_opt
, "cgroup", 6) == 0) {
241 ret
= do_the_needful(CLONE_NEWCGROUP
, "cgroup");
242 } else if (strncmp(ns_opt
, "ipc", 3) == 0) {
243 ret
= do_the_needful(CLONE_NEWIPC
, "ipc");
244 } else if (strncmp(ns_opt
, "mnt", 3) == 0) {
245 ret
= do_the_needful(CLONE_NEWNS
, "mnt");
246 } else if (strncmp(ns_opt
, "net", 3) == 0) {
247 ret
= do_the_needful(CLONE_NEWNET
, "net");
248 } else if (strncmp(ns_opt
, "pid", 3) == 0) {
249 ret
= do_the_needful(CLONE_NEWPID
, "pid");
250 } else if (strncmp(ns_opt
, "time", 4) == 0) {
251 ret
= do_the_needful(CLONE_NEWTIME
, "time");
252 } else if (strncmp(ns_opt
, "user", 4) == 0) {
254 * Will always fail, requires a single threaded application,
255 * which can't happen with UST.
257 ret
= do_the_needful(CLONE_NEWUSER
, "user");
258 } else if (strncmp(ns_opt
, "uts", 3) == 0) {
259 ret
= do_the_needful(CLONE_NEWUTS
, "uts");
261 printf("invalid ns id\n");
265 ret
= ret
? EXIT_FAILURE
: EXIT_SUCCESS
;