diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-06 14:54:32 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-06 14:54:32 +0200 |
commit | ac6c7d3e1906d68074577cb4d15ad42b80fbc341 (patch) | |
tree | f5590434d90d8ddf36223682ab86652672c31e31 /Kernel | |
parent | a58d7fd8bb040c16929330e93ea1491cf9649642 (diff) | |
download | serenity-ac6c7d3e1906d68074577cb4d15ad42b80fbc341.zip |
Kernel: Use alloc_fd() more instead of walking fd list manually.
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/Process.cpp | 74 | ||||
-rw-r--r-- | Kernel/Process.h | 2 |
2 files changed, 24 insertions, 52 deletions
diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 24a4531488..b700f54275 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -930,15 +930,9 @@ int Process::sys$fcntl(int fd, int cmd, dword arg) int arg_fd = (int)arg; if (arg_fd < 0) return -EINVAL; - int new_fd = -1; - for (int i = arg_fd; i < (int)m_max_open_file_descriptors; ++i) { - if (!m_fds[i]) { - new_fd = i; - break; - } - } - if (new_fd == -1) - return -EMFILE; + int new_fd = alloc_fd(arg_fd); + if (new_fd < 0) + return new_fd; m_fds[new_fd].set(*descriptor); break; } @@ -1054,9 +1048,9 @@ int Process::sys$open(const char* path, int options, mode_t mode) #endif if (!validate_read_str(path)) return -EFAULT; - if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) - return -EMFILE; - + int fd = alloc_fd(); + if (fd < 0) + return fd; auto result = VFS::the().open(path, options, mode & ~umask(), cwd_inode()); if (result.is_error()) return result.error(); @@ -1065,21 +1059,15 @@ int Process::sys$open(const char* path, int options, mode_t mode) return -ENOTDIR; // FIXME: This should be handled by VFS::open. if (options & O_NONBLOCK) descriptor->set_blocking(false); - - int fd = 0; - for (; fd < (int)m_max_open_file_descriptors; ++fd) { - if (!m_fds[fd]) - break; - } dword flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0; m_fds[fd].set(move(descriptor), flags); return fd; } -int Process::alloc_fd() +int Process::alloc_fd(int first_candidate_fd) { - int fd = -1; - for (int i = 0; i < (int)m_max_open_file_descriptors; ++i) { + int fd = -EMFILE; + for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) { if (!m_fds[i]) { fd = i; break; @@ -1543,13 +1531,9 @@ int Process::sys$dup(int old_fd) auto* descriptor = file_descriptor(old_fd); if (!descriptor) return -EBADF; - if (number_of_open_file_descriptors() == m_max_open_file_descriptors) - return -EMFILE; - int new_fd = 0; - for (; new_fd < (int)m_max_open_file_descriptors; ++new_fd) { - if (!m_fds[new_fd]) - break; - } + int new_fd = alloc_fd(0); + if (new_fd < 0) + return new_fd; m_fds[new_fd].set(*descriptor); return new_fd; } @@ -1559,8 +1543,8 @@ int Process::sys$dup2(int old_fd, int new_fd) auto* descriptor = file_descriptor(old_fd); if (!descriptor) return -EBADF; - if (number_of_open_file_descriptors() == m_max_open_file_descriptors) - return -EMFILE; + if (new_fd < 0 || new_fd >= m_max_open_file_descriptors) + return -EINVAL; m_fds[new_fd].set(*descriptor); return new_fd; } @@ -1963,13 +1947,9 @@ size_t Process::amount_shared() const int Process::sys$socket(int domain, int type, int protocol) { - if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) - return -EMFILE; - int fd = 0; - for (; fd < (int)m_max_open_file_descriptors; ++fd) { - if (!m_fds[fd]) - break; - } + int fd = alloc_fd(); + if (fd < 0) + return fd; auto result = Socket::create(domain, type, protocol); if (result.is_error()) return result.error(); @@ -2017,13 +1997,9 @@ int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* a return -EFAULT; if (!validate_write(address, *address_size)) return -EFAULT; - if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) - return -EMFILE; - int accepted_socket_fd = 0; - for (; accepted_socket_fd < (int)m_max_open_file_descriptors; ++accepted_socket_fd) { - if (!m_fds[accepted_socket_fd]) - break; - } + int accepted_socket_fd = alloc_fd(); + if (accepted_socket_fd < 0) + return accepted_socket_fd; auto* accepting_socket_descriptor = file_descriptor(accepting_socket_fd); if (!accepting_socket_descriptor) return -EBADF; @@ -2050,13 +2026,9 @@ int Process::sys$connect(int sockfd, const sockaddr* address, socklen_t address_ { if (!validate_read(address, address_size)) return -EFAULT; - if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) - return -EMFILE; - int fd = 0; - for (; fd < (int)m_max_open_file_descriptors; ++fd) { - if (!m_fds[fd]) - break; - } + int fd = alloc_fd(); + if (fd < 0) + return fd; auto* descriptor = file_descriptor(sockfd); if (!descriptor) return -EBADF; diff --git a/Kernel/Process.h b/Kernel/Process.h index b6dd1df8ed..2516abfd06 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -252,7 +252,7 @@ private: int do_exec(String path, Vector<String> arguments, Vector<String> environment); - int alloc_fd(); + int alloc_fd(int first_candidate_fd = 0); void disown_all_shared_buffers(); void create_signal_trampolines_if_needed(); |