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