diff --git a/kernel/kernel/OpenFileDescriptorSet.cpp b/kernel/kernel/OpenFileDescriptorSet.cpp index 43c1584695..123d572840 100644 --- a/kernel/kernel/OpenFileDescriptorSet.cpp +++ b/kernel/kernel/OpenFileDescriptorSet.cpp @@ -202,20 +202,23 @@ namespace Kernel { TRY(validate_fd(fd)); - constexpr int creation_flags = O_CLOEXEC | O_CREAT | O_DIRECTORY | O_EXCL | O_NOCTTY | O_NOFOLLOW | O_TRUNC | O_TTY_INIT; + dprintln("fcntl({} ('{}'), {}, {H})", fd, m_open_files[fd]->path(), cmd, extra); switch (cmd) { case F_GETFD: - return m_open_files[fd]->flags; + return m_open_files[fd]->flags & FD_CLOEXEC; case F_SETFD: - // FIXME: validate permissions to set access flags - m_open_files[fd]->flags = extra; + m_open_files[fd]->flags &= ~FD_CLOEXEC; + m_open_files[fd]->flags |= extra & FD_CLOEXEC; + dprintln(" set CLOEXEC to {}", !!(m_open_files[fd]->flags & FD_CLOEXEC)); return 0; case F_GETFL: - return m_open_files[fd]->flags & ~creation_flags; + return m_open_files[fd]->flags & ~(O_APPEND | O_DSYNC | O_NONBLOCK | O_RSYNC | O_SYNC | O_ACCMODE); case F_SETFL: - m_open_files[fd]->flags |= extra & ~(O_ACCMODE | creation_flags); + m_open_files[fd]->flags &= ~O_NONBLOCK; + m_open_files[fd]->flags |= extra & O_NONBLOCK; + dprintln(" set NONBLOCK to {}", !!(m_open_files[fd]->flags & O_NONBLOCK)); return 0; default: break; diff --git a/userspace/libraries/LibC/include/fcntl.h b/userspace/libraries/LibC/include/fcntl.h index 356cedb6e2..2e7d6ad655 100644 --- a/userspace/libraries/LibC/include/fcntl.h +++ b/userspace/libraries/LibC/include/fcntl.h @@ -27,8 +27,6 @@ __BEGIN_DECLS #define F_GETOWN 10 #define F_SETOWN 11 -#define FD_CLOEXEC 1 - #define F_RDLCK 1 #define F_UNLCK 2 #define F_WRLCK 3 @@ -38,6 +36,14 @@ __BEGIN_DECLS #define SEEK_CUR 2 #define SEEK_END 3 +/* bits 0-3 */ +#define O_RDONLY 0x00001 +#define O_WRONLY 0x00002 +#define O_RDWR (O_RDONLY | O_WRONLY) +#define O_SEARCH 0x00004 +#define O_EXEC 0x00008 +#define O_ACCMODE 0x0000F + /* bits 4-11 */ #define O_CLOEXEC 0x00010 #define O_CREAT 0x00020 @@ -47,6 +53,7 @@ __BEGIN_DECLS #define O_NOFOLLOW 0x00200 #define O_TRUNC 0x00400 #define O_TTY_INIT 0x00800 +#define FD_CLOEXEC O_CLOEXEC /* bits 12-16 */ #define O_APPEND 0x01000 @@ -55,14 +62,6 @@ __BEGIN_DECLS #define O_RSYNC 0x08000 #define O_SYNC 0x10000 -/* bits 0-3 */ -#define O_RDONLY 0x00001 -#define O_WRONLY 0x00002 -#define O_RDWR (O_RDONLY | O_WRONLY) -#define O_SEARCH 0x00004 -#define O_EXEC 0x00008 -#define O_ACCMODE 0x0000F - /* bit 17 */ #define AT_FDCWD 0x20000 diff --git a/userspace/libraries/LibGUI/Window.cpp b/userspace/libraries/LibGUI/Window.cpp index 524a35be5c..bd9e330a84 100644 --- a/userspace/libraries/LibGUI/Window.cpp +++ b/userspace/libraries/LibGUI/Window.cpp @@ -69,7 +69,7 @@ namespace LibGUI return BAN::Error::from_errno(errno); BAN::ScopeGuard server_closer([server_fd] { close(server_fd); }); - if (fcntl(server_fd, F_SETFL, fcntl(server_fd, F_GETFL) | O_CLOEXEC) == -1) + if (fcntl(server_fd, F_SETFD, fcntl(server_fd, F_GETFD) | FD_CLOEXEC) == -1) return BAN::Error::from_errno(errno); timespec start_time; diff --git a/userspace/programs/snake/main.cpp b/userspace/programs/snake/main.cpp index 1cef6e317f..705467de3f 100644 --- a/userspace/programs/snake/main.cpp +++ b/userspace/programs/snake/main.cpp @@ -202,7 +202,7 @@ void update() int main() { // Make stdin non blocking - if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK)) + if (fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK)) { perror("fcntl"); return 1;