Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
L
linux
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
linux
Commits
a5161eee
Commit
a5161eee
authored
Jun 03, 2020
by
Christian Brauner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: add CLOSE_RANGE_UNSHARE tests
Signed-off-by:
Christian Brauner
<
christian.brauner@ubuntu.com
>
parent
60997c3d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
137 additions
and
0 deletions
+137
-0
tools/testing/selftests/core/close_range_test.c
tools/testing/selftests/core/close_range_test.c
+137
-0
No files found.
tools/testing/selftests/core/close_range_test.c
View file @
a5161eee
...
@@ -13,11 +13,16 @@
...
@@ -13,11 +13,16 @@
#include <unistd.h>
#include <unistd.h>
#include "../kselftest_harness.h"
#include "../kselftest_harness.h"
#include "../clone3/clone3_selftests.h"
#ifndef __NR_close_range
#ifndef __NR_close_range
#define __NR_close_range -1
#define __NR_close_range -1
#endif
#endif
#ifndef CLOSE_RANGE_UNSHARE
#define CLOSE_RANGE_UNSHARE (1U << 1)
#endif
static
inline
int
sys_close_range
(
unsigned
int
fd
,
unsigned
int
max_fd
,
static
inline
int
sys_close_range
(
unsigned
int
fd
,
unsigned
int
max_fd
,
unsigned
int
flags
)
unsigned
int
flags
)
{
{
...
@@ -87,4 +92,136 @@ TEST(close_range)
...
@@ -87,4 +92,136 @@ TEST(close_range)
EXPECT_EQ
(
-
1
,
fcntl
(
open_fds
[
100
],
F_GETFL
));
EXPECT_EQ
(
-
1
,
fcntl
(
open_fds
[
100
],
F_GETFL
));
}
}
TEST
(
close_range_unshare
)
{
int
i
,
ret
,
status
;
pid_t
pid
;
int
open_fds
[
101
];
struct
clone_args
args
=
{
.
flags
=
CLONE_FILES
,
.
exit_signal
=
SIGCHLD
,
};
for
(
i
=
0
;
i
<
ARRAY_SIZE
(
open_fds
);
i
++
)
{
int
fd
;
fd
=
open
(
"/dev/null"
,
O_RDONLY
|
O_CLOEXEC
);
ASSERT_GE
(
fd
,
0
)
{
if
(
errno
==
ENOENT
)
XFAIL
(
return
,
"Skipping test since /dev/null does not exist"
);
}
open_fds
[
i
]
=
fd
;
}
pid
=
sys_clone3
(
&
args
,
sizeof
(
args
));
ASSERT_GE
(
pid
,
0
);
if
(
pid
==
0
)
{
ret
=
sys_close_range
(
open_fds
[
0
],
open_fds
[
50
],
CLOSE_RANGE_UNSHARE
);
if
(
ret
)
exit
(
EXIT_FAILURE
);
for
(
i
=
0
;
i
<=
50
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
!=
-
1
)
exit
(
EXIT_FAILURE
);
for
(
i
=
51
;
i
<=
100
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
==
-
1
)
exit
(
EXIT_FAILURE
);
/* create a couple of gaps */
close
(
57
);
close
(
78
);
close
(
81
);
close
(
82
);
close
(
84
);
close
(
90
);
ret
=
sys_close_range
(
open_fds
[
51
],
open_fds
[
92
],
CLOSE_RANGE_UNSHARE
);
if
(
ret
)
exit
(
EXIT_FAILURE
);
for
(
i
=
51
;
i
<=
92
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
!=
-
1
)
exit
(
EXIT_FAILURE
);
for
(
i
=
93
;
i
<=
100
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
==
-
1
)
exit
(
EXIT_FAILURE
);
/* test that the kernel caps and still closes all fds */
ret
=
sys_close_range
(
open_fds
[
93
],
open_fds
[
99
],
CLOSE_RANGE_UNSHARE
);
if
(
ret
)
exit
(
EXIT_FAILURE
);
for
(
i
=
93
;
i
<=
99
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
!=
-
1
)
exit
(
EXIT_FAILURE
);
if
(
fcntl
(
open_fds
[
100
],
F_GETFL
)
==
-
1
)
exit
(
EXIT_FAILURE
);
ret
=
sys_close_range
(
open_fds
[
100
],
open_fds
[
100
],
CLOSE_RANGE_UNSHARE
);
if
(
ret
)
exit
(
EXIT_FAILURE
);
if
(
fcntl
(
open_fds
[
100
],
F_GETFL
)
!=
-
1
)
exit
(
EXIT_FAILURE
);
exit
(
EXIT_SUCCESS
);
}
EXPECT_EQ
(
waitpid
(
pid
,
&
status
,
0
),
pid
);
EXPECT_EQ
(
true
,
WIFEXITED
(
status
));
EXPECT_EQ
(
0
,
WEXITSTATUS
(
status
));
}
TEST
(
close_range_unshare_capped
)
{
int
i
,
ret
,
status
;
pid_t
pid
;
int
open_fds
[
101
];
struct
clone_args
args
=
{
.
flags
=
CLONE_FILES
,
.
exit_signal
=
SIGCHLD
,
};
for
(
i
=
0
;
i
<
ARRAY_SIZE
(
open_fds
);
i
++
)
{
int
fd
;
fd
=
open
(
"/dev/null"
,
O_RDONLY
|
O_CLOEXEC
);
ASSERT_GE
(
fd
,
0
)
{
if
(
errno
==
ENOENT
)
XFAIL
(
return
,
"Skipping test since /dev/null does not exist"
);
}
open_fds
[
i
]
=
fd
;
}
pid
=
sys_clone3
(
&
args
,
sizeof
(
args
));
ASSERT_GE
(
pid
,
0
);
if
(
pid
==
0
)
{
ret
=
sys_close_range
(
open_fds
[
0
],
UINT_MAX
,
CLOSE_RANGE_UNSHARE
);
if
(
ret
)
exit
(
EXIT_FAILURE
);
for
(
i
=
0
;
i
<=
100
;
i
++
)
if
(
fcntl
(
open_fds
[
i
],
F_GETFL
)
!=
-
1
)
exit
(
EXIT_FAILURE
);
exit
(
EXIT_SUCCESS
);
}
EXPECT_EQ
(
waitpid
(
pid
,
&
status
,
0
),
pid
);
EXPECT_EQ
(
true
,
WIFEXITED
(
status
));
EXPECT_EQ
(
0
,
WEXITSTATUS
(
status
));
}
TEST_HARNESS_MAIN
TEST_HARNESS_MAIN
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment