Add UST namespace context change tests
[lttng-tools.git] / tests / utils / testapp / gen-ust-events-ns / gen-ust-events-ns.c
CommitLineData
135801fc
MJ
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 <stdio.h>
25#include <stdlib.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <unistd.h>
29#include <stdarg.h>
30
31#include <common/compat/tid.h>
32
33#include "utils.h"
34#include "signal-helper.h"
35
36#define TRACEPOINT_DEFINE
37#include "tp.h"
38
39#define LTTNG_PROC_NS_PATH_MAX 40
40
41static int nr_iter = 100;
42static int debug = 0;
43static char *ns_opt = NULL;
44static char *after_unshare_file_path = NULL;
45static char *before_second_event_file_path = NULL;
46
47static
48struct 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
59static
60void debug_printf(const char *format, ...) {
61 va_list args;
62 va_start(args, format);
63
64 if (debug)
65 vfprintf(stderr, format, args);
66
67 va_end(args);
68}
69
70static
71ino_t get_ns_inum(char ns[]) {
72 struct stat sb;
73 char proc_ns_path[LTTNG_PROC_NS_PATH_MAX];
74
75 /*
76 * /proc/thread-self was introduced in kernel v3.17
77 */
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) {
81 return sb.st_ino;
82 }
83 }
84
85 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
86 "/proc/self/task/%d/%s/net",
87 gettid(), ns) >= 0) {
88
89 if (stat(proc_ns_path, &sb) == 0) {
90 return sb.st_ino;
91 }
92 }
93
94 return 1;
95}
96
97static
98int do_the_needful(int ns_flag, char ns_str[]) {
99 int ret = 0, i;
100 ino_t ns1, ns2;
101
102 ns1 = get_ns_inum(ns_str);
103 debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1);
104
105 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
106
107 tracepoint(tp, tptest, ns1);
108
109 if (should_quit) {
110 break;
111 }
112 }
113
114 ret = unshare(ns_flag);
115
116 if (ret == -1) {
117 perror("unshare");
118 ret = 0;
119 }
120
121 ns2 = get_ns_inum(ns_str);
122 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
123
124 /*
125 * Signal that we emited the first event group and that the
126 * unshare call is completed.
127 */
128 if (after_unshare_file_path) {
129 ret = create_file(after_unshare_file_path);
130
131 if (ret != 0) {
132 goto end;
133 }
134 }
135
136 /*
137 * Wait on synchronization before writing second event group.
138 */
139 if (before_second_event_file_path) {
140 ret = wait_on_file(before_second_event_file_path);
141 if (ret != 0) {
142 goto end;
143 }
144 }
145
146 for (i = 0; nr_iter < 0 || i < nr_iter; i++) {
147
148 tracepoint(tp, tptest, ns2);
149
150 if (should_quit) {
151 break;
152 }
153 }
154
155end:
156 return ret;
157}
158
159// Send X events, change NS, wait for file to sync with test script, send X events in new NS
160
161
162int main(int argc, const char **argv) {
163 int opt;
164 int ret = EXIT_SUCCESS;
165 poptContext pc;
166
167 pc = poptGetContext(NULL, argc, argv, opts, 0);
168 poptReadDefaultConfig(pc, 0);
169
170 if (argc < 2) {
171 poptPrintHelp(pc, stderr, 0);
172 ret = EXIT_FAILURE;
173 goto end;
174 }
175
176 while ((opt = poptGetNextOpt(pc)) >= 0) {
177 switch(opt) {
178 default:
179 poptPrintUsage(pc, stderr, 0);
180 ret = EXIT_FAILURE;
181 goto end;
182 }
183 }
184
185 if (opt < -1) {
186 /* an error occurred during option processing */
187 poptPrintUsage(pc, stderr, 0);
188 fprintf(stderr, "%s: %s\n",
189 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
190 poptStrerror(opt));
191 ret = EXIT_FAILURE;
192 goto end;
193 }
194
195 if (ns_opt == NULL) {
196 poptPrintUsage(pc, stderr, 0);
197 ret = EXIT_FAILURE;
198 goto end;
199 }
200
201 if (set_signal_handler()) {
202 ret = EXIT_FAILURE;
203 goto end;
204 }
205
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");
221 } else {
222 printf("invalid ns id\n");
223 ret = EXIT_FAILURE;
224 }
225
226end:
227 poptFreeContext(pc);
228 return ret;
229}
This page took 0.031772 seconds and 5 git commands to generate.