Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 468067a2 authored by Bart Van Assche's avatar Bart Van Assche
Browse files

init_test: Close all file descriptors before calling execv()



This change improves test reliability.

Change-Id: Ib9a6b75bbd81968eb7e2fd90ea567155bc8355f7
Signed-off-by: default avatarBart Van Assche <bvanassche@google.com>
parent 878560da
Loading
Loading
Loading
Loading
+16 −3
Original line number Original line Diff line number Diff line
@@ -646,13 +646,26 @@ TEST(init, MemLockLimit) {
    ASSERT_LE(curr_limit.rlim_max, max_limit);
    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 ForkExecvpAsync(const char* argv[]) {
    pid_t pid = fork();
    pid_t pid = fork();
    if (pid == 0) {
    if (pid == 0) {
        // Child process.
        // Child process.
        close(STDIN_FILENO);
        CloseAllFds();
        close(STDOUT_FILENO);
        close(STDERR_FILENO);


        execvp(argv[0], const_cast<char**>(argv));
        execvp(argv[0], const_cast<char**>(argv));
        PLOG(ERROR) << "exec in ForkExecvpAsync init test";
        PLOG(ERROR) << "exec in ForkExecvpAsync init test";