58a2066322ac79ed3695cc4382bc62bcac3fd478
[lttng-tools.git] / tests / utils / testapp / gen-ust-events-ns / gen-ust-events-ns.c
1 /*
2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18 #ifndef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #endif
21
22 #include <popt.h>
23 #include <sched.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30
31 #include <common/compat/tid.h>
32
33 #include "signal-helper.h"
34 #include "utils.h"
35
36 #define TRACEPOINT_DEFINE
37 #include "tp.h"
38
39 #define LTTNG_PROC_NS_PATH_MAX 40
40
41 static int nr_iter = 100;
42 static int debug = 0;
43 static char *ns_opt = NULL;
44 static char *after_unshare_file_path = NULL;
45 static char *before_second_event_file_path = NULL;
46
47 static
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 },
55 POPT_AUTOHELP
56 { NULL, 0, 0, NULL, 0 }
57 };
58
59 static void debug_printf(const char *format, ...)
60 {
61 va_list args;
62 va_start(args, format);
63
64 if (debug) {
65 vfprintf(stderr, format, args);
66 }
67
68 va_end(args);
69 }
70
71 static int get_ns_inum(const char *ns, ino_t *ns_inum)
72 {
73 int ret = 0;
74 struct stat sb;
75 char proc_ns_path[LTTNG_PROC_NS_PATH_MAX];
76
77 /*
78 * /proc/thread-self was introduced in kernel v3.17
79 */
80 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
81 "/proc/thread-self/ns/%s", ns) >= 0) {
82 if (stat(proc_ns_path, &sb) == 0) {
83 *ns_inum = sb.st_ino;
84 } else {
85 ret = -1;
86 }
87 goto end;
88 }
89
90 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
91 "/proc/self/task/%d/%s/net", lttng_gettid(), ns) >= 0) {
92 if (stat(proc_ns_path, &sb) == 0) {
93 *ns_inum = sb.st_ino;
94 } else {
95 ret = -1;
96 }
97 goto end;
98 }
99 end:
100 return ret;
101 }
102
103 static int do_the_needful(int ns_flag, const char *ns_str)
104 {
105 int ret = 0, i;
106 ino_t ns1, ns2;
107
108 ret = get_ns_inum(ns_str, &ns1);
109 if (ret) {
110 debug_printf("Failed to get ns inode number for namespace %s",
111 ns_str);
112 ret = -1;
113 goto end;
114 }
115 debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1);
116
117 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
118 tracepoint(tp, tptest, ns1);
119 if (should_quit) {
120 break;
121 }
122 }
123
124 ret = unshare(ns_flag);
125 if (ret == -1) {
126 perror("Failed to unshare namespace");
127 goto end;
128 }
129
130 ret = get_ns_inum(ns_str, &ns2);
131 if (ret) {
132 debug_printf("Failed to get ns inode number for namespace %s",
133 ns_str);
134 ret = -1;
135 goto end;
136 }
137 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
138
139 /*
140 * Signal that we emited the first event group and that the
141 * unshare call is completed.
142 */
143 if (after_unshare_file_path) {
144 ret = create_file(after_unshare_file_path);
145 if (ret != 0) {
146 goto end;
147 }
148 }
149
150 /* Wait on synchronization before writing second event group. */
151 if (before_second_event_file_path) {
152 ret = wait_on_file(before_second_event_file_path);
153 if (ret != 0) {
154 goto end;
155 }
156 }
157
158 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
159 tracepoint(tp, tptest, ns2);
160 if (should_quit) {
161 break;
162 }
163 }
164
165 end:
166 return ret;
167 }
168
169 /*
170 * Send X events, change NS, wait for file to sync with test script, send X
171 * events in new NS
172 */
173 int main(int argc, const char **argv)
174 {
175 int opt;
176 int ret = EXIT_SUCCESS;
177 poptContext pc;
178
179 pc = poptGetContext(NULL, argc, argv, opts, 0);
180 poptReadDefaultConfig(pc, 0);
181
182 if (argc < 2) {
183 poptPrintHelp(pc, stderr, 0);
184 ret = EXIT_FAILURE;
185 goto end;
186 }
187
188 while ((opt = poptGetNextOpt(pc)) >= 0) {
189 switch (opt) {
190 default:
191 poptPrintUsage(pc, stderr, 0);
192 ret = EXIT_FAILURE;
193 goto end;
194 }
195 }
196
197 if (opt < -1) {
198 /* An error occurred during option processing. */
199 poptPrintUsage(pc, stderr, 0);
200 fprintf(stderr, "%s: %s\n",
201 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
202 poptStrerror(opt));
203 ret = EXIT_FAILURE;
204 goto end;
205 }
206
207 if (ns_opt == NULL) {
208 poptPrintUsage(pc, stderr, 0);
209 ret = EXIT_FAILURE;
210 goto end;
211 }
212
213 if (set_signal_handler()) {
214 ret = EXIT_FAILURE;
215 goto end;
216 }
217
218 if (strncmp(ns_opt, "cgroup", 3) == 0) {
219 ret = do_the_needful(CLONE_NEWCGROUP, "cgroup");
220 } else if (strncmp(ns_opt, "ipc", 3) == 0) {
221 ret = do_the_needful(CLONE_NEWIPC, "ipc");
222 } else if (strncmp(ns_opt, "mnt", 3) == 0) {
223 ret = do_the_needful(CLONE_NEWNS, "mnt");
224 } else if (strncmp(ns_opt, "net", 3) == 0) {
225 ret = do_the_needful(CLONE_NEWNET, "net");
226 } else if (strncmp(ns_opt, "pid", 3) == 0) {
227 ret = do_the_needful(CLONE_NEWPID, "pid");
228 } else if (strncmp(ns_opt, "user", 3) == 0) {
229 /*
230 * Will always fail, requires a single threaded application,
231 * which can't happen with UST.
232 */
233 ret = do_the_needful(CLONE_NEWUSER, "user");
234 } else if (strncmp(ns_opt, "uts", 3) == 0) {
235 ret = do_the_needful(CLONE_NEWUTS, "uts");
236 } else {
237 printf("invalid ns id\n");
238 ret = EXIT_FAILURE;
239 goto end;
240 }
241 ret = ret ? EXIT_FAILURE : EXIT_SUCCESS;
242 end:
243 poptFreeContext(pc);
244 return ret;
245 }
This page took 0.049882 seconds and 3 git commands to generate.