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
27 #include <sys/types.h>
31 #include <common/compat/tid.h>
34 #include "signal-helper.h"
36 #define TRACEPOINT_DEFINE
39 #define LTTNG_PROC_NS_PATH_MAX 40
41 static int nr_iter
= 100;
43 static char *ns_opt
= NULL
;
44 static char *after_unshare_file_path
= NULL
;
45 static char *before_second_event_file_path
= NULL
;
48 struct poptOption opts
[] = {
49 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
50 { "debug", 'd', POPT_ARG_NONE
, &debug
, 0, "Enable debug output", NULL
},
51 { "ns", 'n', POPT_ARG_STRING
, &ns_opt
, 0, "Namespace short identifier", NULL
},
52 { "iter", 'i', POPT_ARG_INT
, &nr_iter
, 0, "Number of tracepoint iterations", NULL
},
53 { "after", 'a', POPT_ARG_STRING
, &after_unshare_file_path
, 0, "after_unshare_file_path,", NULL
},
54 { "before", 'b', POPT_ARG_STRING
, &before_second_event_file_path
, 0, "before_second_event_file_path,", NULL
},
56 { NULL
, 0, 0, NULL
, 0 }
60 void debug_printf(const char *format
, ...) {
62 va_start(args
, format
);
65 vfprintf(stderr
, format
, args
);
71 ino_t
get_ns_inum(char ns
[]) {
73 char proc_ns_path
[LTTNG_PROC_NS_PATH_MAX
];
76 * /proc/thread-self was introduced in kernel v3.17
78 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
79 "/proc/thread-self/ns/%s", ns
) >= 0) {
80 if (stat(proc_ns_path
, &sb
) == 0) {
85 if (snprintf(proc_ns_path
, LTTNG_PROC_NS_PATH_MAX
,
86 "/proc/self/task/%d/%s/net",
89 if (stat(proc_ns_path
, &sb
) == 0) {
98 int do_the_needful(int ns_flag
, char ns_str
[]) {
102 ns1
= get_ns_inum(ns_str
);
103 debug_printf("Initial %s ns inode number: %lu\n", ns_str
, ns1
);
105 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
107 tracepoint(tp
, tptest
, ns1
);
114 ret
= unshare(ns_flag
);
121 ns2
= get_ns_inum(ns_str
);
122 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str
, ns2
);
125 * Signal that we emited the first event group and that the
126 * unshare call is completed.
128 if (after_unshare_file_path
) {
129 ret
= create_file(after_unshare_file_path
);
137 * Wait on synchronization before writing second event group.
139 if (before_second_event_file_path
) {
140 ret
= wait_on_file(before_second_event_file_path
);
146 for (i
= 0; nr_iter
< 0 || i
< nr_iter
; i
++) {
148 tracepoint(tp
, tptest
, ns2
);
159 // Send X events, change NS, wait for file to sync with test script, send X events in new NS
162 int main(int argc
, const char **argv
) {
164 int ret
= EXIT_SUCCESS
;
167 pc
= poptGetContext(NULL
, argc
, argv
, opts
, 0);
168 poptReadDefaultConfig(pc
, 0);
171 poptPrintHelp(pc
, stderr
, 0);
176 while ((opt
= poptGetNextOpt(pc
)) >= 0) {
179 poptPrintUsage(pc
, stderr
, 0);
186 /* an error occurred during option processing */
187 poptPrintUsage(pc
, stderr
, 0);
188 fprintf(stderr
, "%s: %s\n",
189 poptBadOption(pc
, POPT_BADOPTION_NOALIAS
),
195 if (ns_opt
== NULL
) {
196 poptPrintUsage(pc
, stderr
, 0);
201 if (set_signal_handler()) {
206 if (strncmp(ns_opt
, "cgroup", 3) == 0) {
207 do_the_needful(CLONE_NEWCGROUP
, "cgroup");
208 } else if (strncmp(ns_opt
, "ipc", 3) == 0) {
209 do_the_needful(CLONE_NEWIPC
, "ipc");
210 } else if (strncmp(ns_opt
, "mnt", 3) == 0) {
211 do_the_needful(CLONE_NEWNS
, "mnt");
212 } else if (strncmp(ns_opt
, "net", 3) == 0) {
213 do_the_needful(CLONE_NEWNET
, "net");
214 } else if (strncmp(ns_opt
, "pid", 3) == 0) {
215 do_the_needful(CLONE_NEWPID
, "pid");
216 } else if (strncmp(ns_opt
, "user", 3) == 0) {
217 // Will always fail, requires a single threaded application, which can't happen with UST.
218 do_the_needful(CLONE_NEWUSER
, "user");
219 } else if (strncmp(ns_opt
, "uts", 3) == 0) {
220 do_the_needful(CLONE_NEWUTS
, "uts");
222 printf("invalid ns id\n");