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