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