1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 18:57:42 +00:00

Kernel: Use alloc_fd() more instead of walking fd list manually.

This commit is contained in:
Andreas Kling 2019-04-06 14:54:32 +02:00
parent a58d7fd8bb
commit ac6c7d3e19
2 changed files with 24 additions and 52 deletions

View file

@ -930,15 +930,9 @@ int Process::sys$fcntl(int fd, int cmd, dword arg)
int arg_fd = (int)arg; int arg_fd = (int)arg;
if (arg_fd < 0) if (arg_fd < 0)
return -EINVAL; return -EINVAL;
int new_fd = -1; int new_fd = alloc_fd(arg_fd);
for (int i = arg_fd; i < (int)m_max_open_file_descriptors; ++i) { if (new_fd < 0)
if (!m_fds[i]) { return new_fd;
new_fd = i;
break;
}
}
if (new_fd == -1)
return -EMFILE;
m_fds[new_fd].set(*descriptor); m_fds[new_fd].set(*descriptor);
break; break;
} }
@ -1054,9 +1048,9 @@ int Process::sys$open(const char* path, int options, mode_t mode)
#endif #endif
if (!validate_read_str(path)) if (!validate_read_str(path))
return -EFAULT; return -EFAULT;
if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) int fd = alloc_fd();
return -EMFILE; if (fd < 0)
return fd;
auto result = VFS::the().open(path, options, mode & ~umask(), cwd_inode()); auto result = VFS::the().open(path, options, mode & ~umask(), cwd_inode());
if (result.is_error()) if (result.is_error())
return result.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. return -ENOTDIR; // FIXME: This should be handled by VFS::open.
if (options & O_NONBLOCK) if (options & O_NONBLOCK)
descriptor->set_blocking(false); 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; dword flags = (options & O_CLOEXEC) ? FD_CLOEXEC : 0;
m_fds[fd].set(move(descriptor), flags); m_fds[fd].set(move(descriptor), flags);
return fd; return fd;
} }
int Process::alloc_fd() int Process::alloc_fd(int first_candidate_fd)
{ {
int fd = -1; int fd = -EMFILE;
for (int i = 0; i < (int)m_max_open_file_descriptors; ++i) { for (int i = first_candidate_fd; i < (int)m_max_open_file_descriptors; ++i) {
if (!m_fds[i]) { if (!m_fds[i]) {
fd = i; fd = i;
break; break;
@ -1543,13 +1531,9 @@ int Process::sys$dup(int old_fd)
auto* descriptor = file_descriptor(old_fd); auto* descriptor = file_descriptor(old_fd);
if (!descriptor) if (!descriptor)
return -EBADF; return -EBADF;
if (number_of_open_file_descriptors() == m_max_open_file_descriptors) int new_fd = alloc_fd(0);
return -EMFILE; if (new_fd < 0)
int new_fd = 0; return new_fd;
for (; new_fd < (int)m_max_open_file_descriptors; ++new_fd) {
if (!m_fds[new_fd])
break;
}
m_fds[new_fd].set(*descriptor); m_fds[new_fd].set(*descriptor);
return new_fd; return new_fd;
} }
@ -1559,8 +1543,8 @@ int Process::sys$dup2(int old_fd, int new_fd)
auto* descriptor = file_descriptor(old_fd); auto* descriptor = file_descriptor(old_fd);
if (!descriptor) if (!descriptor)
return -EBADF; return -EBADF;
if (number_of_open_file_descriptors() == m_max_open_file_descriptors) if (new_fd < 0 || new_fd >= m_max_open_file_descriptors)
return -EMFILE; return -EINVAL;
m_fds[new_fd].set(*descriptor); m_fds[new_fd].set(*descriptor);
return new_fd; return new_fd;
} }
@ -1963,13 +1947,9 @@ size_t Process::amount_shared() const
int Process::sys$socket(int domain, int type, int protocol) int Process::sys$socket(int domain, int type, int protocol)
{ {
if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) int fd = alloc_fd();
return -EMFILE; if (fd < 0)
int fd = 0; return fd;
for (; fd < (int)m_max_open_file_descriptors; ++fd) {
if (!m_fds[fd])
break;
}
auto result = Socket::create(domain, type, protocol); auto result = Socket::create(domain, type, protocol);
if (result.is_error()) if (result.is_error())
return result.error(); return result.error();
@ -2017,13 +1997,9 @@ int Process::sys$accept(int accepting_socket_fd, sockaddr* address, socklen_t* a
return -EFAULT; return -EFAULT;
if (!validate_write(address, *address_size)) if (!validate_write(address, *address_size))
return -EFAULT; return -EFAULT;
if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) int accepted_socket_fd = alloc_fd();
return -EMFILE; if (accepted_socket_fd < 0)
int accepted_socket_fd = 0; return accepted_socket_fd;
for (; accepted_socket_fd < (int)m_max_open_file_descriptors; ++accepted_socket_fd) {
if (!m_fds[accepted_socket_fd])
break;
}
auto* accepting_socket_descriptor = file_descriptor(accepting_socket_fd); auto* accepting_socket_descriptor = file_descriptor(accepting_socket_fd);
if (!accepting_socket_descriptor) if (!accepting_socket_descriptor)
return -EBADF; return -EBADF;
@ -2050,13 +2026,9 @@ int Process::sys$connect(int sockfd, const sockaddr* address, socklen_t address_
{ {
if (!validate_read(address, address_size)) if (!validate_read(address, address_size))
return -EFAULT; return -EFAULT;
if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) int fd = alloc_fd();
return -EMFILE; if (fd < 0)
int fd = 0; return fd;
for (; fd < (int)m_max_open_file_descriptors; ++fd) {
if (!m_fds[fd])
break;
}
auto* descriptor = file_descriptor(sockfd); auto* descriptor = file_descriptor(sockfd);
if (!descriptor) if (!descriptor)
return -EBADF; return -EBADF;

View file

@ -252,7 +252,7 @@ private:
int do_exec(String path, Vector<String> arguments, Vector<String> environment); 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 disown_all_shared_buffers();
void create_signal_trampolines_if_needed(); void create_signal_trampolines_if_needed();