2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
19 #include <sys/types.h>
22 #include <common/compat/tid.h>
23 #include <common/macros.h>
25 #include "signal-helper.h"
28 #define LTTNG_PROC_NS_PATH_MAX 40
31 * The runner of this test validates that the kernel supports the
32 * namespace for which it is invoked. However, these defines are added
33 * to allow tests to run on systems that support a given namespace,
34 * but that use a libc that doesn't define its associated clone flag.
37 #define CLONE_NEWNS 0x00020000
39 #ifndef CLONE_NEWCGROUP
40 #define CLONE_NEWCGROUP 0x02000000
43 #define CLONE_NEWUTS 0x04000000
46 #define CLONE_NEWIPC 0x08000000
49 #define CLONE_NEWUSER 0x10000000
52 #define CLONE_NEWPID 0x20000000
55 #define CLONE_NEWNET 0x40000000
58 #define CLONE_NEWTIME 0x00000080
62 static char *ns_opt
= NULL
;
63 static char *before_unshare_wait_file_path
= NULL
;
64 static char *after_unshare_wait_file_path
= NULL
;
65 static char *after_unshare_signal_file_path
= NULL
;
67 static struct poptOption opts
[] = {
68 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
69 { "debug", 'd', POPT_ARG_NONE
, &debug
, 0, "Enable debug output", NULL
},
70 { "ns", 'n', POPT_ARG_STRING
, &ns_opt
, 0, "Namespace short identifier", NULL
},
71 { "before", 'b', POPT_ARG_STRING
, &before_unshare_wait_file_path
, 0, "Wait for file before unshare", NULL
},
72 { "after", 'a', POPT_ARG_STRING
, &after_unshare_wait_file_path
, 0, "Wait for file after unshare", NULL
},
73 { "signal", 's', POPT_ARG_STRING
, &after_unshare_signal_file_path
, 0, "Create signal file after unshare", NULL
},
75 { NULL
, 0, 0, NULL
, 0 }
78 static ATTR_FORMAT_PRINTF(1, 2)
79 void debug_printf(const char *format
, ...)
82 va_start(args
, format
);
85 vfprintf(stderr
, format
, args
);
91 static int get_ns_inum(const char *ns
, ino_t
*ns_inum
)
95 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
98 * /proc/thread-self was introduced in kernel v3.17
100 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
101 "/proc/thread-self/ns/%s", ns
) >= 0) {
102 if (stat(proc_ns_path
, &sb
) == 0) {
103 *ns_inum
= sb
.st_ino
;
110 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
111 "/proc/self/task/%d/%s/net", lttng_gettid(), ns
) >= 0) {
112 if (stat(proc_ns_path
, &sb
) == 0) {
113 *ns_inum
= sb
.st_ino
;
123 static int do_the_needful(int ns_flag
, const char *ns_str
)
128 ret
= get_ns_inum(ns_str
, &ns1
);
130 debug_printf("Failed to get ns inode number for namespace %s",
135 debug_printf("Initial %s ns inode number: %lu\n", ns_str
, ns1
);
137 /* Wait on synchronization before unshare. */
138 if (before_unshare_wait_file_path
) {
139 ret
= wait_on_file(before_unshare_wait_file_path
);
145 ret
= unshare(ns_flag
);
147 perror("Failed to unshare namespace");
151 ret
= get_ns_inum(ns_str
, &ns2
);
153 debug_printf("Failed to get ns inode number for namespace %s",
158 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str
, ns2
);
160 /* Signal that the unshare call is completed. */
161 if (after_unshare_signal_file_path
) {
162 ret
= create_file(after_unshare_signal_file_path
);
168 /* Wait on synchronization after unshare. */
169 if (after_unshare_wait_file_path
) {
170 ret
= wait_on_file(after_unshare_wait_file_path
);
180 int main(int argc
, const char **argv
)
183 int ret
= EXIT_SUCCESS
;
186 pc
= poptGetContext(NULL
, argc
, argv
, opts
, 0);
187 poptReadDefaultConfig(pc
, 0);
190 poptPrintHelp(pc
, stderr
, 0);
195 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
198 poptPrintUsage(pc
, stderr
, 0);
205 /* an error occurred during option processing */
206 poptPrintUsage(pc
, stderr
, 0);
207 fprintf(stderr
, "%s: %s\n",
208 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
214 if (ns_opt
== NULL
) {
215 poptPrintUsage(pc
, stderr
, 0);
220 if (set_signal_handler()) {
225 if (strncmp(ns_opt
, "cgroup", 6) == 0) {
226 ret
= do_the_needful(CLONE_NEWCGROUP
, "cgroup");
227 } else if (strncmp(ns_opt
, "ipc", 3) == 0) {
228 ret
= do_the_needful(CLONE_NEWIPC
, "ipc");
229 } else if (strncmp(ns_opt
, "mnt", 3) == 0) {
230 ret
= do_the_needful(CLONE_NEWNS
, "mnt");
231 } else if (strncmp(ns_opt
, "net", 3) == 0) {
232 ret
= do_the_needful(CLONE_NEWNET
, "net");
233 } else if (strncmp(ns_opt
, "pid", 3) == 0) {
234 ret
= do_the_needful(CLONE_NEWPID
, "pid");
235 } else if (strncmp(ns_opt
, "time", 4) == 0) {
236 ret
= do_the_needful(CLONE_NEWTIME
, "time");
237 } else if (strncmp(ns_opt
, "user", 4) == 0) {
239 * Will always fail, requires a single threaded application,
240 * which can't happen with UST.
242 ret
= do_the_needful(CLONE_NEWUSER
, "user");
243 } else if (strncmp(ns_opt
, "uts", 3) == 0) {
244 ret
= do_the_needful(CLONE_NEWUTS
, "uts");
246 printf("invalid ns id\n");
250 ret
= ret
? EXIT_FAILURE
: EXIT_SUCCESS
;