Add kernel namespace context change tests
[lttng-tools.git] / tests / utils / testapp / gen-ns-events / gen-ns-events.c
CommitLineData
e26422e8
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#include <string.h>
31
32#include <common/compat/tid.h>
33
34#include "utils.h"
35#include "signal-helper.h"
36
37#define LTTNG_PROC_NS_PATH_MAX 40
38
39static int debug = 0;
40static char *ns_opt = NULL;
41static char *before_unshare_wait_file_path = NULL;
42static char *after_unshare_wait_file_path = NULL;
43static char *after_unshare_signal_file_path = NULL;
44
45static
46struct poptOption opts[] = {
47 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
48 { "debug", 'd', POPT_ARG_NONE, &debug, 0, "Enable debug output", NULL },
49 { "ns", 'n', POPT_ARG_STRING, &ns_opt, 0, "Namespace short identifier", NULL },
50 { "before", 'b', POPT_ARG_STRING, &before_unshare_wait_file_path, 0, "Wait for file before unshare", NULL },
51 { "after", 'a', POPT_ARG_STRING, &after_unshare_wait_file_path, 0, "Wait for file after unshare", NULL },
52 { "signal", 's', POPT_ARG_STRING, &after_unshare_signal_file_path, 0, "Create signal file after unshare", NULL },
53 POPT_AUTOHELP
54 { NULL, 0, 0, NULL, 0 }
55};
56
57static
58void debug_printf(const char *format, ...) {
59 va_list args;
60 va_start(args, format);
61
62 if (debug)
63 vfprintf(stderr, format, args);
64
65 va_end(args);
66}
67
68static
69ino_t get_ns_inum(char ns[]) {
70 struct stat sb;
71 char proc_ns_path[LTTNG_PROC_NS_PATH_MAX];
72
73 /*
74 * /proc/thread-self was introduced in kernel v3.17
75 */
76 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
77 "/proc/thread-self/ns/%s", ns) >= 0) {
78 if (stat(proc_ns_path, &sb) == 0) {
79 return sb.st_ino;
80 }
81 }
82
83 if (snprintf(proc_ns_path, LTTNG_PROC_NS_PATH_MAX,
84 "/proc/self/task/%d/%s/net",
85 gettid(), ns) >= 0) {
86
87 if (stat(proc_ns_path, &sb) == 0) {
88 return sb.st_ino;
89 }
90 }
91
92 return 1;
93}
94
95static
96int do_the_needful(int ns_flag, char ns_str[]) {
97 int ret = 0;
98 ino_t ns1, ns2;
99
100 ns1 = get_ns_inum(ns_str);
101 debug_printf("Initial %s ns inode number: %lu\n", ns_str, ns1);
102
103 /*
104 * Wait on synchronization before unshare
105 */
106 if (before_unshare_wait_file_path) {
107 ret = wait_on_file(before_unshare_wait_file_path);
108 if (ret != 0) {
109 goto end;
110 }
111 }
112
113 ret = unshare(ns_flag);
114
115 if (ret == -1) {
116 perror("unshare");
117 ret = 0;
118 }
119
120 ns2 = get_ns_inum(ns_str);
121 debug_printf("Post unshare %s ns inode number: %lu\n", ns_str, ns2);
122
123 /*
124 * Signal that the unshare call is completed.
125 */
126 if (after_unshare_signal_file_path) {
127 ret = create_file(after_unshare_signal_file_path);
128
129 if (ret != 0) {
130 goto end;
131 }
132 }
133
134 /*
135 * Wait on synchronization after unshare
136 */
137 if (after_unshare_wait_file_path) {
138 ret = wait_on_file(after_unshare_wait_file_path);
139 if (ret != 0) {
140 goto end;
141 }
142 }
143
144end:
145 return ret;
146}
147
148int main(int argc, const char **argv) {
149 int opt;
150 int ret = EXIT_SUCCESS;
151 poptContext pc;
152
153 pc = poptGetContext(NULL, argc, argv, opts, 0);
154 poptReadDefaultConfig(pc, 0);
155
156 if (argc < 2) {
157 poptPrintHelp(pc, stderr, 0);
158 ret = EXIT_FAILURE;
159 goto end;
160 }
161
162 while ((opt = poptGetNextOpt(pc)) >= 0) {
163 switch(opt) {
164 default:
165 poptPrintUsage(pc, stderr, 0);
166 ret = EXIT_FAILURE;
167 goto end;
168 }
169 }
170
171 if (opt < -1) {
172 /* an error occurred during option processing */
173 poptPrintUsage(pc, stderr, 0);
174 fprintf(stderr, "%s: %s\n",
175 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
176 poptStrerror(opt));
177 ret = EXIT_FAILURE;
178 goto end;
179 }
180
181 if (ns_opt == NULL) {
182 poptPrintUsage(pc, stderr, 0);
183 ret = EXIT_FAILURE;
184 goto end;
185 }
186
187 if (set_signal_handler()) {
188 ret = EXIT_FAILURE;
189 goto end;
190 }
191
192 if (strncmp(ns_opt, "cgroup", 3) == 0) {
193 do_the_needful(CLONE_NEWCGROUP, "cgroup");
194 } else if (strncmp(ns_opt, "ipc", 3) == 0) {
195 do_the_needful(CLONE_NEWIPC, "ipc");
196 } else if (strncmp(ns_opt, "mnt", 3) == 0) {
197 do_the_needful(CLONE_NEWNS, "mnt");
198 } else if (strncmp(ns_opt, "net", 3) == 0) {
199 do_the_needful(CLONE_NEWNET, "net");
200 } else if (strncmp(ns_opt, "pid", 3) == 0) {
201 do_the_needful(CLONE_NEWPID, "pid");
202 } else if (strncmp(ns_opt, "user", 3) == 0) {
203 // Will always fail, requires a single threaded application, which can't happen with UST.
204 do_the_needful(CLONE_NEWUSER, "user");
205 } else if (strncmp(ns_opt, "uts", 3) == 0) {
206 do_the_needful(CLONE_NEWUTS, "uts");
207 } else {
208 printf("invalid ns id\n");
209 ret = EXIT_FAILURE;
210 }
211
212end:
213 poptFreeContext(pc);
214 return ret;
215}
This page took 0.032157 seconds and 5 git commands to generate.