Skip to content

Commit bb81fba

Browse files
committed
fs: implement FD_CLOEXEC flag
1 parent 9d3afae commit bb81fba

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

include/kernel/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ enum file_flags {
3333
FD_WRITE = _FWRITE,
3434
FD_RW = FD_READ | FD_WRITE,
3535
FD_APPEND = _FAPPEND,
36+
FD_NOINHERIT = _FNOINHERIT, /* FD_CLOEXEC is already defined by fcntl(). */
3637
};
3738

3839
/** Opened file description.

kernel/fs/vfs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ static inline error_t file_compute_flags(int oflags, int *flags)
342342
{
343343
*flags = 0;
344344

345-
if (oflags & (O_TRUNC | O_SYNC | O_NONBLOCK | O_NOCTTY) ||
346-
oflags & (O_CLOEXEC | O_NOFOLLOW))
345+
if (oflags & (O_TRUNC | O_SYNC | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW))
347346
return -E_INVAL;
348347

349348
if (O_READABLE(oflags))
@@ -353,6 +352,8 @@ static inline error_t file_compute_flags(int oflags, int *flags)
353352

354353
if (oflags & O_APPEND)
355354
*flags |= FD_APPEND;
355+
if (oflags & O_CLOEXEC)
356+
*flags |= FD_NOINHERIT;
356357

357358
return E_SUCCESS;
358359
}

kernel/misc/exec.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ static NO_RETURN void execfmt_execute_executable(struct executable *executable,
6060
void *stack_pointer,
6161
void *base_pointer)
6262
{
63+
struct process *process = current->process;
64+
65+
/*
66+
* Close all files marked as CLOEXEC.
67+
*/
68+
locked_scope(&current->process->files_lock) {
69+
for (size_t i = 0; i < PROCESS_FD_COUNT; ++i) {
70+
if (process->files[i] && process->files[i]->flags & FD_NOINHERIT) {
71+
file_put(process->files[i]);
72+
process->files[i] = NULL;
73+
}
74+
}
75+
}
76+
6377
thread_jump_to_userland(stack_pointer, base_pointer, executable->entrypoint,
6478
NULL);
6579
}

0 commit comments

Comments
 (0)