init_test: Close all file descriptors before calling execv()
This change improves test reliability.
Change-Id: Ib9a6b75bbd81968eb7e2fd90ea567155bc8355f7
Signed-off-by: Bart Van Assche <bvanassche@google.com>
diff --git a/init/init_test.cpp b/init/init_test.cpp
index 4a7bac2..305bf95 100644
--- a/init/init_test.cpp
+++ b/init/init_test.cpp
@@ -646,13 +646,26 @@
ASSERT_LE(curr_limit.rlim_max, max_limit);
}
+void CloseAllFds() {
+ DIR* dir;
+ struct dirent* ent;
+ int fd;
+
+ if ((dir = opendir("/proc/self/fd"))) {
+ while ((ent = readdir(dir))) {
+ if (sscanf(ent->d_name, "%d", &fd) == 1) {
+ close(fd);
+ }
+ }
+ closedir(dir);
+ }
+}
+
pid_t ForkExecvpAsync(const char* argv[]) {
pid_t pid = fork();
if (pid == 0) {
// Child process.
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- close(STDERR_FILENO);
+ CloseAllFds();
execvp(argv[0], const_cast<char**>(argv));
PLOG(ERROR) << "exec in ForkExecvpAsync init test";