2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
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.
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
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
28 #include <sys/types.h>
31 #include <common/compat/tid.h>
33 #include "signal-helper.h"
36 #define TRACEPOINT_DEFINE
39 #define LTTNG_PROC_NS_PATH_MAX 40
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.
48 #define CLONE_NEWNS 0x00020000
50 #ifndef CLONE_NEWCGROUP
51 #define CLONE_NEWCGROUP 0x02000000
54 #define CLONE_NEWUTS 0x04000000
57 #define CLONE_NEWIPC 0x08000000
60 #define CLONE_NEWUSER 0x10000000
63 #define CLONE_NEWPID 0x20000000
66 #define CLONE_NEWNET 0x40000000
69 static int nr_iter
= 100;
71 static char *ns_opt
= NULL
;
72 static char *after_unshare_file_path
= NULL
;
73 static char *before_second_event_file_path
= NULL
;
76 struct 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
},
84 { NULL
, 0, 0, NULL
, 0 }
87 static void debug_printf(const char *format
, ...)
90 va_start(args
, format
);
93 vfprintf(stderr
, format
, args
);
99 static int get_ns_inum(const char *ns
, ino_t
*ns_inum
)
103 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
106 * /proc/thread-self was introduced in kernel v3.17
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
;
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
;
131 static int do_the_needful(int ns_flag
, const char *ns_str
)
136 ret
= get_ns_inum(ns_str
, &ns1
);
138 debug_printf("Failed to get ns inode number for namespace %s",
143 debug_printf("Initial %s ns inode number: %lu\n", ns_str
, ns1
);
145 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
146 tracepoint(tp
, tptest
, ns1
);
152 ret
= unshare(ns_flag
);
154 perror("Failed to unshare namespace");
158 ret
= get_ns_inum(ns_str
, &ns2
);
160 debug_printf("Failed to get ns inode number for namespace %s",
165 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str
, ns2
);
168 * Signal that we emited the first event group and that the
169 * unshare call is completed.
171 if (after_unshare_file_path
) {
172 ret
= create_file(after_unshare_file_path
);
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
);
186 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
187 tracepoint(tp
, tptest
, ns2
);
198 * Send X events, change NS, wait for file to sync with test script, send X
201 int main(int argc
, const char **argv
)
204 int ret
= EXIT_SUCCESS
;
207 pc
= poptGetContext(NULL
, argc
, argv
, opts
, 0);
208 poptReadDefaultConfig(pc
, 0);
211 poptPrintHelp(pc
, stderr
, 0);
216 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
219 poptPrintUsage(pc
, stderr
, 0);
226 /* An error occurred during option processing. */
227 poptPrintUsage(pc
, stderr
, 0);
228 fprintf(stderr
, "%s: %s\n",
229 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
235 if (ns_opt
== NULL
) {
236 poptPrintUsage(pc
, stderr
, 0);
241 if (set_signal_handler()) {
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) {
258 * Will always fail, requires a single threaded application,
259 * which can't happen with UST.
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");
265 printf("invalid ns id\n");
269 ret
= ret
? EXIT_FAILURE
: EXIT_SUCCESS
;