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