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>
26 #include <sys/types.h>
29 #define LTTNG_PROC_NS_PATH_MAX 40
32 * The runner of this test validates that the kernel supports the
33 * namespace for which it is invoked. However, these defines are added
34 * to allow tests to run on systems that support a given namespace,
35 * but that use a libc that doesn't define its associated clone flag.
38 #define CLONE_NEWNS 0x00020000
40 #ifndef CLONE_NEWCGROUP
41 #define CLONE_NEWCGROUP 0x02000000
44 #define CLONE_NEWUTS 0x04000000
47 #define CLONE_NEWIPC 0x08000000
50 #define CLONE_NEWUSER 0x10000000
53 #define CLONE_NEWPID 0x20000000
56 #define CLONE_NEWNET 0x40000000
59 #define CLONE_NEWTIME 0x00000080
63 static char *ns_opt
= NULL
;
64 static char *before_unshare_wait_file_path
= NULL
;
65 static char *after_unshare_wait_file_path
= NULL
;
66 static char *after_unshare_signal_file_path
= NULL
;
68 static struct poptOption opts
[] = {
69 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
70 { "debug", 'd', POPT_ARG_NONE
, &debug
, 0, "Enable debug output", NULL
},
71 { "ns", 'n', POPT_ARG_STRING
, &ns_opt
, 0, "Namespace short identifier", NULL
},
75 &before_unshare_wait_file_path
,
77 "Wait for file before unshare",
82 &after_unshare_wait_file_path
,
84 "Wait for file after unshare",
89 &after_unshare_signal_file_path
,
91 "Create signal file after unshare",
93 POPT_AUTOHELP
{ NULL
, 0, 0, NULL
, 0, NULL
, NULL
}
96 static ATTR_FORMAT_PRINTF(1, 2) void debug_printf(const char *format
, ...)
99 va_start(args
, format
);
102 vfprintf(stderr
, format
, args
);
108 static int get_ns_inum(const char *ns
, ino_t
*ns_inum
)
112 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
115 * /proc/thread-self was introduced in kernel v3.17
117 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
, "/proc/thread-self/ns/%s", ns
) >= 0) {
118 if (stat(proc_ns_path
, &sb
) == 0) {
119 *ns_inum
= sb
.st_ino
;
126 if (snprintf(proc_ns_path
,
127 LTTNG_PROC_NS_PATH_MAX
,
128 "/proc/self/task/%d/%s/net",
131 if (stat(proc_ns_path
, &sb
) == 0) {
132 *ns_inum
= sb
.st_ino
;
142 static int do_the_needful(int ns_flag
, const char *ns_str
)
147 ret
= get_ns_inum(ns_str
, &ns1
);
149 debug_printf("Failed to get ns inode number for namespace %s", ns_str
);
153 debug_printf("Initial %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns1
);
155 /* Wait on synchronization before unshare. */
156 if (before_unshare_wait_file_path
) {
157 ret
= wait_on_file(before_unshare_wait_file_path
);
163 ret
= unshare(ns_flag
);
165 perror("Failed to unshare namespace");
169 ret
= get_ns_inum(ns_str
, &ns2
);
171 debug_printf("Failed to get ns inode number for namespace %s", ns_str
);
175 debug_printf("Post unshare %s ns inode number: %" PRIuMAX
"\n", ns_str
, (uintmax_t) ns2
);
177 /* Signal that the unshare call is completed. */
178 if (after_unshare_signal_file_path
) {
179 ret
= create_file(after_unshare_signal_file_path
);
185 /* Wait on synchronization after unshare. */
186 if (after_unshare_wait_file_path
) {
187 ret
= wait_on_file(after_unshare_wait_file_path
);
197 int main(int argc
, const char **argv
)
200 int ret
= EXIT_SUCCESS
;
203 pc
= poptGetContext(NULL
, argc
, argv
, opts
, 0);
204 poptReadDefaultConfig(pc
, 0);
207 poptPrintHelp(pc
, stderr
, 0);
212 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
215 poptPrintUsage(pc
, stderr
, 0);
222 /* an error occurred during option processing */
223 poptPrintUsage(pc
, stderr
, 0);
226 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
232 if (ns_opt
== NULL
) {
233 poptPrintUsage(pc
, stderr
, 0);
238 if (set_signal_handler()) {
243 if (strncmp(ns_opt
, "cgroup", 6) == 0) {
244 ret
= do_the_needful(CLONE_NEWCGROUP
, "cgroup");
245 } else if (strncmp(ns_opt
, "ipc", 3) == 0) {
246 ret
= do_the_needful(CLONE_NEWIPC
, "ipc");
247 } else if (strncmp(ns_opt
, "mnt", 3) == 0) {
248 ret
= do_the_needful(CLONE_NEWNS
, "mnt");
249 } else if (strncmp(ns_opt
, "net", 3) == 0) {
250 ret
= do_the_needful(CLONE_NEWNET
, "net");
251 } else if (strncmp(ns_opt
, "pid", 3) == 0) {
252 ret
= do_the_needful(CLONE_NEWPID
, "pid");
253 } else if (strncmp(ns_opt
, "time", 4) == 0) {
254 ret
= do_the_needful(CLONE_NEWTIME
, "time");
255 } else if (strncmp(ns_opt
, "user", 4) == 0) {
257 * Will always fail, requires a single threaded application,
258 * which can't happen with UST.
260 ret
= do_the_needful(CLONE_NEWUSER
, "user");
261 } else if (strncmp(ns_opt
, "uts", 3) == 0) {
262 ret
= do_the_needful(CLONE_NEWUTS
, "uts");
264 printf("invalid ns id\n");
268 ret
= ret
? EXIT_FAILURE
: EXIT_SUCCESS
;