tests: Move to kernel style SPDX license identifiers
[lttng-tools.git] / tests / utils / testapp / gen-ns-events / gen-ns-events.c
1 /*
2 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE
10 #endif
11
12 #include <popt.h>
13 #include <sched.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include <common/compat/tid.h>
23
24 #include "signal-helper.h"
25 #include "utils.h"
26
27 #define LTTNG_PROC_NS_PATH_MAX 40
28
29 /*
30 * The runner of this test validates that the kernel supports the
31 * namespace for which it is invoked. However, these defines are added
32 * to allow tests to run on systems that support a given namespace,
33 * but that use a libc that doesn't define its associated clone flag.
34 */
35 #ifndef CLONE_NEWNS
36 #define CLONE_NEWNS 0x00020000
37 #endif
38 #ifndef CLONE_NEWCGROUP
39 #define CLONE_NEWCGROUP 0x02000000
40 #endif
41 #ifndef CLONE_NEWUTS
42 #define CLONE_NEWUTS 0x04000000
43 #endif
44 #ifndef CLONE_NEWIPC
45 #define CLONE_NEWIPC 0x08000000
46 #endif
47 #ifndef CLONE_NEWUSER
48 #define CLONE_NEWUSER 0x10000000
49 #endif
50 #ifndef CLONE_NEWPID
51 #define CLONE_NEWPID 0x20000000
52 #endif
53 #ifndef CLONE_NEWNET
54 #define CLONE_NEWNET 0x40000000
55 #endif
56
57 static int debug = 0;
58 static char *ns_opt = NULL;
59 static char *before_unshare_wait_file_path = NULL;
60 static char *after_unshare_wait_file_path = NULL;
61 static char *after_unshare_signal_file_path = NULL;
62
63 static struct poptOption opts[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
65 { "debug", 'd', POPT_ARG_NONE, &debug, 0, "Enable debug output", NULL },
66 { "ns", 'n', POPT_ARG_STRING, &ns_opt, 0, "Namespace short identifier", NULL },
67 { "before", 'b', POPT_ARG_STRING, &before_unshare_wait_file_path, 0, "Wait for file before unshare", NULL },
68 { "after", 'a', POPT_ARG_STRING, &after_unshare_wait_file_path, 0, "Wait for file after unshare", NULL },
69 { "signal", 's', POPT_ARG_STRING, &after_unshare_signal_file_path, 0, "Create signal file after unshare", NULL },
70 POPT_AUTOHELP
71 { NULL, 0, 0, NULL, 0 }
72 };
73
74 static void debug_printf(const char *format, ...)
75 {
76 va_list args;
77 va_start(args, format);
78
79 if (debug) {
80 vfprintf(stderr, format, args);
81 }
82
83 va_end(args);
84 }
85
86 static int get_ns_inum(const char *ns, ino_t *ns_inum)
87 {
88 int ret = 0;
89 struct stat sb;
90 char proc_ns_path[LTTNG_PROC_NS_PATH_MAX];
91
92 /*
93 * /proc/thread-self was introduced in kernel v3.17
94 */
95 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
96 "/proc/thread-self/ns/%s", ns) >= 0) {
97 if (stat(proc_ns_path, &sb) == 0) {
98 *ns_inum = sb.st_ino;
99 } else {
100 ret = -1;
101 }
102 goto end;
103 }
104
105 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
106 "/proc/self/task/%d/%s/net", lttng_gettid(), ns) >= 0) {
107 if (stat(proc_ns_path, &sb) == 0) {
108 *ns_inum = sb.st_ino;
109 } else {
110 ret = -1;
111 }
112 goto end;
113 }
114 end:
115 return ret;
116 }
117
118 static int do_the_needful(int ns_flag, const char *ns_str)
119 {
120 int ret = 0;
121 ino_t ns1, ns2;
122
123 ret = get_ns_inum(ns_str, &ns1);
124 if (ret) {
125 debug_printf("Failed to get ns inode number for namespace %s",
126 ns_str);
127 ret = -1;
128 goto end;
129 }
130 debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1);
131
132 /* Wait on synchronization before unshare. */
133 if (before_unshare_wait_file_path) {
134 ret = wait_on_file(before_unshare_wait_file_path);
135 if (ret != 0) {
136 goto end;
137 }
138 }
139
140 ret = unshare(ns_flag);
141 if (ret == -1) {
142 perror("Failed to unshare namespace");
143 goto end;
144 }
145
146 ret = get_ns_inum(ns_str, &ns2);
147 if (ret) {
148 debug_printf("Failed to get ns inode number for namespace %s",
149 ns_str);
150 ret = -1;
151 goto end;
152 }
153 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
154
155 /* Signal that the unshare call is completed. */
156 if (after_unshare_signal_file_path) {
157 ret = create_file(after_unshare_signal_file_path);
158 if (ret != 0) {
159 goto end;
160 }
161 }
162
163 /* Wait on synchronization after unshare. */
164 if (after_unshare_wait_file_path) {
165 ret = wait_on_file(after_unshare_wait_file_path);
166 if (ret != 0) {
167 goto end;
168 }
169 }
170
171 end:
172 return ret;
173 }
174
175 int main(int argc, const char **argv)
176 {
177 int opt;
178 int ret = EXIT_SUCCESS;
179 poptContext pc;
180
181 pc = poptGetContext(NULL, argc, argv, opts, 0);
182 poptReadDefaultConfig(pc, 0);
183
184 if (argc < 2) {
185 poptPrintHelp(pc, stderr, 0);
186 ret = EXIT_FAILURE;
187 goto end;
188 }
189
190 while ((opt = poptGetNextOpt(pc)) >= 0) {
191 switch (opt) {
192 default:
193 poptPrintUsage(pc, stderr, 0);
194 ret = EXIT_FAILURE;
195 goto end;
196 }
197 }
198
199 if (opt < -1) {
200 /* an error occurred during option processing */
201 poptPrintUsage(pc, stderr, 0);
202 fprintf(stderr, "%s: %s\n",
203 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
204 poptStrerror(opt));
205 ret = EXIT_FAILURE;
206 goto end;
207 }
208
209 if (ns_opt == NULL) {
210 poptPrintUsage(pc, stderr, 0);
211 ret = EXIT_FAILURE;
212 goto end;
213 }
214
215 if (set_signal_handler()) {
216 ret = EXIT_FAILURE;
217 goto end;
218 }
219
220 if (strncmp(ns_opt, "cgroup", 3) == 0) {
221 ret = do_the_needful(CLONE_NEWCGROUP, "cgroup");
222 } else if (strncmp(ns_opt, "ipc", 3) == 0) {
223 ret = do_the_needful(CLONE_NEWIPC, "ipc");
224 } else if (strncmp(ns_opt, "mnt", 3) == 0) {
225 ret = do_the_needful(CLONE_NEWNS, "mnt");
226 } else if (strncmp(ns_opt, "net", 3) == 0) {
227 ret = do_the_needful(CLONE_NEWNET, "net");
228 } else if (strncmp(ns_opt, "pid", 3) == 0) {
229 ret = do_the_needful(CLONE_NEWPID, "pid");
230 } else if (strncmp(ns_opt, "user", 3) == 0) {
231 /*
232 * Will always fail, requires a single threaded application,
233 * which can't happen with UST.
234 */
235 ret = do_the_needful(CLONE_NEWUSER, "user");
236 } else if (strncmp(ns_opt, "uts", 3) == 0) {
237 ret = do_the_needful(CLONE_NEWUTS, "uts");
238 } else {
239 printf("invalid ns id\n");
240 ret = EXIT_FAILURE;
241 goto end;
242 }
243 ret = ret ? EXIT_FAILURE : EXIT_SUCCESS;
244 end:
245 poptFreeContext(pc);
246 return ret;
247 }
This page took 0.034391 seconds and 5 git commands to generate.