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