Skip to content

Commit 55d9ad9

Browse files
author
Christian Brauner
committed
tests: add CLONE_NEWTIME setns tests
Now that pidfds support CLONE_NEWTIME as well enable testing them in the setns() testuite. Signed-off-by: Christian Brauner <[email protected]> Cc: Serge Hallyn <[email protected]> Cc: Michael Kerrisk <[email protected]> Cc: Dmitry Safonov <[email protected]> Cc: Andrei Vagin <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 76c1288 commit 55d9ad9

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

tools/testing/selftests/pidfd/pidfd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#define P_PIDFD 3
2323
#endif
2424

25+
#ifndef CLONE_NEWTIME
26+
#define CLONE_NEWTIME 0x00000080
27+
#endif
28+
2529
#ifndef CLONE_PIDFD
2630
#define CLONE_PIDFD 0x00001000
2731
#endif

tools/testing/selftests/pidfd/pidfd_setns_test.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum {
3232
PIDFD_NS_NET,
3333
PIDFD_NS_CGROUP,
3434
PIDFD_NS_PIDCLD,
35+
PIDFD_NS_TIME,
3536
PIDFD_NS_MAX
3637
};
3738

@@ -47,6 +48,7 @@ const struct ns_info {
4748
[PIDFD_NS_NET] = { "net", CLONE_NEWNET, },
4849
[PIDFD_NS_CGROUP] = { "cgroup", CLONE_NEWCGROUP, },
4950
[PIDFD_NS_PIDCLD] = { "pid_for_children", 0, },
51+
[PIDFD_NS_TIME] = { "time", CLONE_NEWTIME, },
5052
};
5153

5254
FIXTURE(current_nsset)
@@ -83,9 +85,49 @@ pid_t create_child(int *pidfd, unsigned flags)
8385
return sys_clone3(&args, sizeof(struct clone_args));
8486
}
8587

88+
static bool switch_timens(void)
89+
{
90+
int fd, ret;
91+
92+
if (unshare(CLONE_NEWTIME))
93+
return false;
94+
95+
fd = open("/proc/self/ns/time_for_children", O_RDONLY | O_CLOEXEC);
96+
if (fd < 0)
97+
return false;
98+
99+
ret = setns(fd, CLONE_NEWTIME);
100+
close(fd);
101+
return ret == 0;
102+
}
103+
104+
static ssize_t read_nointr(int fd, void *buf, size_t count)
105+
{
106+
ssize_t ret;
107+
108+
do {
109+
ret = read(fd, buf, count);
110+
} while (ret < 0 && errno == EINTR);
111+
112+
return ret;
113+
}
114+
115+
static ssize_t write_nointr(int fd, const void *buf, size_t count)
116+
{
117+
ssize_t ret;
118+
119+
do {
120+
ret = write(fd, buf, count);
121+
} while (ret < 0 && errno == EINTR);
122+
123+
return ret;
124+
}
125+
86126
FIXTURE_SETUP(current_nsset)
87127
{
88128
int i, proc_fd, ret;
129+
int ipc_sockets[2];
130+
char c;
89131

90132
for (i = 0; i < PIDFD_NS_MAX; i++) {
91133
self->nsfds[i] = -EBADF;
@@ -130,6 +172,9 @@ FIXTURE_SETUP(current_nsset)
130172
TH_LOG("%m - Failed to open pidfd for process %d", self->pid);
131173
}
132174

175+
ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
176+
EXPECT_EQ(ret, 0);
177+
133178
/* Create tasks that will be stopped. */
134179
self->child_pid1 = create_child(&self->child_pidfd1,
135180
CLONE_NEWUSER | CLONE_NEWNS |
@@ -139,10 +184,27 @@ FIXTURE_SETUP(current_nsset)
139184
EXPECT_GE(self->child_pid1, 0);
140185

141186
if (self->child_pid1 == 0) {
187+
close(ipc_sockets[0]);
188+
189+
if (!switch_timens())
190+
_exit(EXIT_FAILURE);
191+
192+
if (write_nointr(ipc_sockets[1], "1", 1) < 0)
193+
_exit(EXIT_FAILURE);
194+
195+
close(ipc_sockets[1]);
196+
142197
pause();
143198
_exit(EXIT_SUCCESS);
144199
}
145200

201+
close(ipc_sockets[1]);
202+
ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
203+
close(ipc_sockets[0]);
204+
205+
ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
206+
EXPECT_EQ(ret, 0);
207+
146208
self->child_pid2 = create_child(&self->child_pidfd2,
147209
CLONE_NEWUSER | CLONE_NEWNS |
148210
CLONE_NEWCGROUP | CLONE_NEWIPC |
@@ -151,10 +213,24 @@ FIXTURE_SETUP(current_nsset)
151213
EXPECT_GE(self->child_pid2, 0);
152214

153215
if (self->child_pid2 == 0) {
216+
close(ipc_sockets[0]);
217+
218+
if (!switch_timens())
219+
_exit(EXIT_FAILURE);
220+
221+
if (write_nointr(ipc_sockets[1], "1", 1) < 0)
222+
_exit(EXIT_FAILURE);
223+
224+
close(ipc_sockets[1]);
225+
154226
pause();
155227
_exit(EXIT_SUCCESS);
156228
}
157229

230+
close(ipc_sockets[1]);
231+
ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
232+
close(ipc_sockets[0]);
233+
158234
for (i = 0; i < PIDFD_NS_MAX; i++) {
159235
char p[100];
160236

0 commit comments

Comments
 (0)