2020-07-30 21:38:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 21:38:15 +00:00
|
|
|
*/
|
|
|
|
|
2020-09-09 02:37:15 +00:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <AK/Time.h>
|
2021-01-25 15:07:10 +00:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-30 21:38:15 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-07 11:01:11 +00:00
|
|
|
using BlockFlags = Thread::FileBlocker::BlockFlags;
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$select(Userspace<const Syscall::SC_select_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2021-03-01 14:07:01 +00:00
|
|
|
Syscall::SC_select_params params {};
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
|
|
|
if (params.nfds < 0)
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINVAL;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-11-15 18:58:19 +00:00
|
|
|
Thread::BlockTimeout timeout;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (params.timeout) {
|
2021-02-21 19:28:20 +00:00
|
|
|
Optional<Time> timeout_time = copy_time_from_user(params.timeout);
|
|
|
|
if (!timeout_time.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-02-27 22:56:16 +00:00
|
|
|
timeout = Thread::BlockTimeout(false, &timeout_time.value());
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto current_thread = Thread::current();
|
2020-09-09 02:37:15 +00:00
|
|
|
|
|
|
|
u32 previous_signal_mask = 0;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (params.sigmask) {
|
|
|
|
sigset_t sigmask_copy;
|
|
|
|
if (!copy_from_user(&sigmask_copy, params.sigmask))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
previous_signal_mask = current_thread->update_signal_mask(sigmask_copy);
|
|
|
|
}
|
2020-09-09 02:37:15 +00:00
|
|
|
ScopeGuard rollback_signal_mask([&]() {
|
2020-09-12 03:11:07 +00:00
|
|
|
if (params.sigmask)
|
2020-09-09 02:37:15 +00:00
|
|
|
current_thread->update_signal_mask(previous_signal_mask);
|
|
|
|
});
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
fd_set fds_read, fds_write, fds_except;
|
2021-04-28 06:36:37 +00:00
|
|
|
|
|
|
|
size_t bytes_used = ceil_div(params.nfds, 8);
|
|
|
|
if (bytes_used > sizeof(fds_read))
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (params.readfds && !copy_from_user(&fds_read, params.readfds, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-04-28 06:36:37 +00:00
|
|
|
if (params.writefds && !copy_from_user(&fds_write, params.writefds, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-04-28 06:36:37 +00:00
|
|
|
if (params.exceptfds && !copy_from_user(&fds_except, params.exceptfds, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-11-29 23:05:27 +00:00
|
|
|
|
|
|
|
Thread::SelectBlocker::FDVector fds_info;
|
2021-06-22 18:22:17 +00:00
|
|
|
Vector<int, FD_SETSIZE> selected_fds;
|
2020-11-29 23:05:27 +00:00
|
|
|
for (int fd = 0; fd < params.nfds; fd++) {
|
2021-03-07 11:01:11 +00:00
|
|
|
auto block_flags = BlockFlags::None;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (params.readfds && FD_ISSET(fd, &fds_read))
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::Read;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (params.writefds && FD_ISSET(fd, &fds_write))
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::Write;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (params.exceptfds && FD_ISSET(fd, &fds_except))
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::Exception;
|
|
|
|
if (block_flags == BlockFlags::None)
|
2020-11-29 23:05:27 +00:00
|
|
|
continue;
|
|
|
|
|
2021-06-22 18:22:17 +00:00
|
|
|
auto description = fds().file_description(fd);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (!description) {
|
2021-01-10 15:21:56 +00:00
|
|
|
dbgln("sys$select: Bad fd number {}", fd);
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2021-04-29 09:27:38 +00:00
|
|
|
if (!fds_info.try_append({ description.release_nonnull(), block_flags }))
|
|
|
|
return ENOMEM;
|
2021-06-22 18:22:17 +00:00
|
|
|
if (!selected_fds.try_append(fd))
|
2021-04-29 09:27:38 +00:00
|
|
|
return ENOMEM;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-01-23 22:59:27 +00:00
|
|
|
if constexpr (IO_DEBUG || POLL_SELECT_DEBUG)
|
2021-01-14 23:18:55 +00:00
|
|
|
dbgln("selecting on {} fds, timeout={}", fds_info.size(), params.timeout);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted()) {
|
2021-02-07 12:03:24 +00:00
|
|
|
dbgln_if(POLL_SELECT_DEBUG, "select was interrupted");
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINTR;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
if (params.readfds)
|
|
|
|
FD_ZERO(&fds_read);
|
|
|
|
if (params.writefds)
|
|
|
|
FD_ZERO(&fds_write);
|
|
|
|
if (params.exceptfds)
|
|
|
|
FD_ZERO(&fds_except);
|
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
int marked_fd_count = 0;
|
2020-11-29 23:05:27 +00:00
|
|
|
for (size_t i = 0; i < fds_info.size(); i++) {
|
|
|
|
auto& fd_entry = fds_info[i];
|
2021-03-07 11:01:11 +00:00
|
|
|
if (fd_entry.unblocked_flags == BlockFlags::None)
|
2020-11-29 23:05:27 +00:00
|
|
|
continue;
|
2021-03-07 11:01:11 +00:00
|
|
|
if (params.readfds && has_flag(fd_entry.unblocked_flags, BlockFlags::Read)) {
|
2021-06-22 18:22:17 +00:00
|
|
|
FD_SET(selected_fds[i], &fds_read);
|
2020-11-29 23:05:27 +00:00
|
|
|
marked_fd_count++;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2021-03-07 11:01:11 +00:00
|
|
|
if (params.writefds && has_flag(fd_entry.unblocked_flags, BlockFlags::Write)) {
|
2021-06-22 18:22:17 +00:00
|
|
|
FD_SET(selected_fds[i], &fds_write);
|
2020-11-29 23:05:27 +00:00
|
|
|
marked_fd_count++;
|
|
|
|
}
|
2021-07-14 11:59:22 +00:00
|
|
|
if (params.exceptfds && has_any_flag(fd_entry.unblocked_flags, BlockFlags::Exception)) {
|
2021-06-22 18:22:17 +00:00
|
|
|
FD_SET(selected_fds[i], &fds_except);
|
2020-11-29 23:05:27 +00:00
|
|
|
marked_fd_count++;
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-04-28 06:36:37 +00:00
|
|
|
if (params.readfds && !copy_to_user(params.readfds, &fds_read, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-04-28 06:36:37 +00:00
|
|
|
if (params.writefds && !copy_to_user(params.writefds, &fds_write, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-04-28 06:36:37 +00:00
|
|
|
if (params.exceptfds && !copy_to_user(params.exceptfds, &fds_except, bytes_used))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
return marked_fd_count;
|
|
|
|
}
|
|
|
|
|
2021-06-28 18:59:35 +00:00
|
|
|
KResultOr<FlatPtr> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> user_params)
|
2020-07-30 21:38:15 +00:00
|
|
|
{
|
2021-07-18 18:20:12 +00:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 21:38:15 +00:00
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-08-05 06:15:42 +00:00
|
|
|
|
|
|
|
Syscall::SC_poll_params params;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-06-22 18:22:17 +00:00
|
|
|
if (params.nfds >= fds().max_open())
|
2021-03-01 12:49:16 +00:00
|
|
|
return ENOBUFS;
|
2021-02-12 23:58:33 +00:00
|
|
|
|
2020-11-15 18:58:19 +00:00
|
|
|
Thread::BlockTimeout timeout;
|
|
|
|
if (params.timeout) {
|
2021-02-21 19:28:20 +00:00
|
|
|
auto timeout_time = copy_time_from_user(params.timeout);
|
|
|
|
if (!timeout_time.has_value())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-02-27 22:56:16 +00:00
|
|
|
timeout = Thread::BlockTimeout(false, &timeout_time.value());
|
2020-11-15 18:58:19 +00:00
|
|
|
}
|
2020-08-05 06:15:42 +00:00
|
|
|
|
|
|
|
sigset_t sigmask = {};
|
2020-09-12 03:11:07 +00:00
|
|
|
if (params.sigmask && !copy_from_user(&sigmask, params.sigmask))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-05-14 07:39:08 +00:00
|
|
|
Vector<pollfd, FD_SETSIZE> fds_copy;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (params.nfds > 0) {
|
2021-07-04 17:23:26 +00:00
|
|
|
Checked<size_t> nfds_checked = sizeof(pollfd);
|
2020-09-12 03:11:07 +00:00
|
|
|
nfds_checked *= params.nfds;
|
|
|
|
if (nfds_checked.has_overflow())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2021-04-29 08:41:30 +00:00
|
|
|
if (!fds_copy.try_resize(params.nfds))
|
|
|
|
return ENOMEM;
|
2021-02-21 16:03:03 +00:00
|
|
|
if (!copy_from_user(fds_copy.data(), ¶ms.fds[0], nfds_checked.value()))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
}
|
2020-09-18 07:49:51 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
Thread::SelectBlocker::FDVector fds_info;
|
|
|
|
for (size_t i = 0; i < params.nfds; i++) {
|
2020-09-12 03:11:07 +00:00
|
|
|
auto& pfd = fds_copy[i];
|
2021-06-22 18:22:17 +00:00
|
|
|
auto description = fds().file_description(pfd.fd);
|
2020-11-29 23:05:27 +00:00
|
|
|
if (!description) {
|
2021-01-10 15:21:56 +00:00
|
|
|
dbgln("sys$poll: Bad fd number {}", pfd.fd);
|
2021-03-01 12:49:16 +00:00
|
|
|
return EBADF;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2021-03-07 11:01:11 +00:00
|
|
|
BlockFlags block_flags = BlockFlags::Exception; // always want POLLERR, POLLHUP, POLLNVAL
|
2020-09-12 03:11:07 +00:00
|
|
|
if (pfd.events & POLLIN)
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::Read;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (pfd.events & POLLOUT)
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::Write;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (pfd.events & POLLPRI)
|
2021-03-07 11:01:11 +00:00
|
|
|
block_flags |= BlockFlags::ReadPriority;
|
2021-04-29 08:41:30 +00:00
|
|
|
if (!fds_info.try_append({ description.release_nonnull(), block_flags }))
|
|
|
|
return ENOMEM;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto current_thread = Thread::current();
|
2020-09-09 02:37:15 +00:00
|
|
|
|
|
|
|
u32 previous_signal_mask = 0;
|
2020-08-05 06:15:42 +00:00
|
|
|
if (params.sigmask)
|
2020-09-12 03:11:07 +00:00
|
|
|
previous_signal_mask = current_thread->update_signal_mask(sigmask);
|
2020-09-09 02:37:15 +00:00
|
|
|
ScopeGuard rollback_signal_mask([&]() {
|
|
|
|
if (params.sigmask)
|
|
|
|
current_thread->update_signal_mask(previous_signal_mask);
|
|
|
|
});
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2021-01-23 22:59:27 +00:00
|
|
|
if constexpr (IO_DEBUG || POLL_SELECT_DEBUG)
|
2021-01-14 23:18:55 +00:00
|
|
|
dbgln("polling on {} fds, timeout={}", fds_info.size(), params.timeout);
|
2020-07-30 21:38:15 +00:00
|
|
|
|
2020-11-29 23:05:27 +00:00
|
|
|
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted())
|
2021-03-01 12:49:16 +00:00
|
|
|
return EINTR;
|
2020-07-30 21:38:15 +00:00
|
|
|
|
|
|
|
int fds_with_revents = 0;
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
for (unsigned i = 0; i < params.nfds; ++i) {
|
|
|
|
auto& pfd = fds_copy[i];
|
2020-11-29 23:05:27 +00:00
|
|
|
auto& fds_entry = fds_info[i];
|
|
|
|
|
|
|
|
pfd.revents = 0;
|
2021-03-07 11:01:11 +00:00
|
|
|
if (fds_entry.unblocked_flags == BlockFlags::None)
|
2020-11-29 23:05:27 +00:00
|
|
|
continue;
|
|
|
|
|
2021-07-14 11:59:22 +00:00
|
|
|
if (has_any_flag(fds_entry.unblocked_flags, BlockFlags::Exception)) {
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::ReadHangUp))
|
2020-11-29 23:05:27 +00:00
|
|
|
pfd.revents |= POLLRDHUP;
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::WriteError))
|
2020-11-29 23:05:27 +00:00
|
|
|
pfd.revents |= POLLERR;
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::WriteHangUp))
|
2020-11-29 23:05:27 +00:00
|
|
|
pfd.revents |= POLLNVAL;
|
2020-09-12 03:11:07 +00:00
|
|
|
} else {
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::Read)) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(pfd.events & POLLIN);
|
2020-09-12 03:11:07 +00:00
|
|
|
pfd.revents |= POLLIN;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::ReadPriority)) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(pfd.events & POLLPRI);
|
2020-11-29 23:05:27 +00:00
|
|
|
pfd.revents |= POLLPRI;
|
|
|
|
}
|
2021-03-07 11:01:11 +00:00
|
|
|
if (has_flag(fds_entry.unblocked_flags, BlockFlags::Write)) {
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(pfd.events & POLLOUT);
|
2020-09-12 03:11:07 +00:00
|
|
|
pfd.revents |= POLLOUT;
|
2020-11-29 23:05:27 +00:00
|
|
|
}
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
2020-11-29 23:05:27 +00:00
|
|
|
if (pfd.revents)
|
|
|
|
fds_with_revents++;
|
2020-07-30 21:38:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-21 16:03:03 +00:00
|
|
|
if (params.nfds > 0 && !copy_to_user(¶ms.fds[0], fds_copy.data(), params.nfds * sizeof(pollfd)))
|
2021-03-01 12:49:16 +00:00
|
|
|
return EFAULT;
|
2020-09-12 03:11:07 +00:00
|
|
|
|
2020-07-30 21:38:15 +00:00
|
|
|
return fds_with_revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|