diff --git a/kernel/src/sys/exec.cpp b/kernel/src/sys/exec.cpp index a65a503b..53c615ce 100644 --- a/kernel/src/sys/exec.cpp +++ b/kernel/src/sys/exec.cpp @@ -6,6 +6,7 @@ #include "thread/Scheduler.h" #include "thread/ThreadImage.h" #include +#include #include #include #include @@ -76,8 +77,11 @@ Result sys_exec(Registers* regs, SyscallArgs args) guard.deactivate(); - // FIXME: Close O_CLOEXEC file descriptors. - // for (int i = 0; i < FD_MAX; i++) { current->fd_table[i] = {}; } + for (int i = 0; i < FD_MAX; i++) + { + auto& descriptor = current->fd_table[i]; + if (descriptor->flags & O_CLOEXEC) descriptor = {}; + } MMU::delete_userspace_page_directory(current->directory); diff --git a/kernel/src/sys/open.cpp b/kernel/src/sys/open.cpp index 5aaaf1cf..4456bec9 100644 --- a/kernel/src/sys/open.cpp +++ b/kernel/src/sys/open.cpp @@ -7,7 +7,7 @@ #include // These flags are needed after open(), the rest only affect open(). -constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK; +constexpr int FLAGS_TO_KEEP = O_RDWR | O_APPEND | O_NONBLOCK | O_CLOEXEC; Result sys_open(Registers*, SyscallArgs args) { diff --git a/libc/include/bits/open-flags.h b/libc/include/bits/open-flags.h index e51b0547..c0f7e680 100644 --- a/libc/include/bits/open-flags.h +++ b/libc/include/bits/open-flags.h @@ -11,5 +11,6 @@ #define O_EXCL 16 #define O_TRUNC 32 #define O_NONBLOCK 64 +#define O_CLOEXEC 128 #endif