tests: add helper call have_namespaces() to test whether Linux namespaces are available
A slighly sloppy test call for conditionalizing several tests.
This commit is contained in:
parent
795919efdf
commit
a4bc3c1d25
|
@ -1,6 +1,10 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include <sched.h>
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/wait.h>
|
||||
#include <util.h>
|
||||
|
||||
/* When we include libgen.h because we need dirname() we immediately
|
||||
|
@ -112,3 +116,36 @@ int log_tests_skipped_errno(int r, const char *message) {
|
|||
program_invocation_short_name, message);
|
||||
return EXIT_TEST_SKIP;
|
||||
}
|
||||
|
||||
bool have_namespaces(void) {
|
||||
siginfo_t si = {};
|
||||
pid_t pid;
|
||||
|
||||
/* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we
|
||||
* do so in a child process in order not to affect our own process. */
|
||||
|
||||
pid = fork();
|
||||
assert_se(pid >= 0);
|
||||
|
||||
if (pid == 0) {
|
||||
/* child */
|
||||
if (unshare(CLONE_NEWNS) < 0)
|
||||
_exit(EXIT_FAILURE);
|
||||
|
||||
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
|
||||
_exit(EXIT_FAILURE);
|
||||
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0);
|
||||
assert_se(si.si_code == CLD_EXITED);
|
||||
|
||||
if (si.si_status == EXIT_SUCCESS)
|
||||
return true;
|
||||
|
||||
if (si.si_status == EXIT_FAILURE)
|
||||
return false;
|
||||
|
||||
assert_not_reached("unexpected exit code");
|
||||
}
|
||||
|
|
|
@ -10,3 +10,5 @@ bool slow_tests_enabled(void);
|
|||
void test_setup_logging(int level);
|
||||
int log_tests_skipped(const char *message);
|
||||
int log_tests_skipped_errno(int r, const char *message);
|
||||
|
||||
bool have_namespaces(void);
|
||||
|
|
Loading…
Reference in New Issue