Skip to content

Commit 608d2b3

Browse files
committed
tests: verify the auxstr of unshare(2) when --namespace=switch is given
Signed-off-by: Masatake YAMATO <[email protected]>
1 parent dd0682b commit 608d2b3

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

tests/gen_tests.in

+1
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,7 @@ umovestr_cached_adjacent +umovestr_cached.test 3
11731173
unlink -a24
11741174
unlinkat -a35
11751175
unshare -a11
1176+
unshare-report-ns-id -a11 -e trace=unshare -e namespace=switch
11761177
userfaultfd -a15
11771178
ustat -a33
11781179
utime -a16

tests/pure_executables.list

+1
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ uname
835835
unlink
836836
unlinkat
837837
unshare
838+
unshare-report-ns-id
838839
userfaultfd
839840
ustat
840841
utime

tests/unshare-report-ns-id.c

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Check appending auxstr of unshare(2) when --namespace=switch is given
3+
*
4+
* This is derrived from unshare.c.
5+
*
6+
* Copyright (c) 2024 The strace developers.
7+
* All rights reserved.
8+
*
9+
* SPDX-License-Identifier: GPL-2.0-or-later
10+
*/
11+
12+
#include "tests.h"
13+
#include "scno.h"
14+
15+
#include <limits.h>
16+
#include <stdio.h>
17+
#include <unistd.h>
18+
19+
int
20+
main(void)
21+
{
22+
static const kernel_ulong_t bogus_flags =
23+
(kernel_ulong_t) 0xbadc0ded0000000fULL;
24+
25+
static struct {
26+
kernel_ulong_t val;
27+
const char *str;
28+
const char *fnames[8];
29+
} unshare_flags[] = {
30+
{ ARG_STR(0), { NULL } },
31+
{ 0x10000000, "CLONE_NEWUSER", { "user", NULL } },
32+
{ 0x00020000, "CLONE_NEWNS", { "mnt", NULL } },
33+
{ 0x00000080|0x00020000|0x02000000|0x04000000|0x08000000|0x20000000|0x40000000,
34+
"CLONE_NEWTIME|CLONE_NEWNS|CLONE_NEWCGROUP"
35+
"|CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWPID"
36+
"|CLONE_NEWNET", { "cgroup", "ipc", "mnt", "net", "pid", "time", "uts", NULL } },
37+
};
38+
39+
long rc = syscall(__NR_unshare, bogus_flags);
40+
printf("unshare(%#llx /* CLONE_??? */) = %s\n",
41+
(unsigned long long) bogus_flags, sprintrc(rc));
42+
43+
if (chdir("/proc/self/ns") != 0)
44+
perror_msg_and_skip("chdir");
45+
46+
for (unsigned int i = 0; i < ARRAY_SIZE(unshare_flags); ++i) {
47+
rc = syscall(__NR_unshare, unshare_flags[i].val);
48+
printf("unshare(%s) = %s%s",
49+
unshare_flags[i].str, sprintrc(rc), rc == 0? "": "\n");
50+
51+
if (rc == 0) {
52+
unsigned int j;
53+
for (j = 0; unshare_flags[i].fnames[j]; j++) {
54+
char buf[PATH_MAX + 1];
55+
int n = readlink(unshare_flags[i].fnames[j], buf, sizeof(buf));
56+
if (n < 0 || (size_t)n >= sizeof(buf))
57+
perror_msg_and_skip("readlink");
58+
buf[n] = '\0';
59+
if (j == 0)
60+
printf(" (%s", buf);
61+
else
62+
printf(", %s", buf);
63+
}
64+
printf("%s", j == 0? "\n": ")\n");
65+
}
66+
}
67+
68+
puts("+++ exited with 0 +++");
69+
70+
return 0;
71+
}

0 commit comments

Comments
 (0)