2020-07-31 00:38:15 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-09-09 05:37:15 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <AK/Time.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.h>
|
2020-07-31 00:38:15 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-01 15:49:16 +03:00
|
|
|
KResultOr<int> Process::sys$select(const Syscall::SC_select_params* user_params)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-09-12 06:11:07 +03:00
|
|
|
Syscall::SC_select_params params;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
|
|
|
|
if (params.nfds < 0)
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINVAL;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
Thread::BlockTimeout timeout;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (params.timeout) {
|
|
|
|
timespec timeout_copy;
|
|
|
|
if (!copy_from_user(&timeout_copy, params.timeout))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-15 21:58:19 +03:00
|
|
|
timeout = Thread::BlockTimeout(false, &timeout_copy);
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto current_thread = Thread::current();
|
2020-09-09 05:37:15 +03:00
|
|
|
|
|
|
|
u32 previous_signal_mask = 0;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (params.sigmask) {
|
|
|
|
sigset_t sigmask_copy;
|
|
|
|
if (!copy_from_user(&sigmask_copy, params.sigmask))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
previous_signal_mask = current_thread->update_signal_mask(sigmask_copy);
|
|
|
|
}
|
2020-09-09 05:37:15 +03:00
|
|
|
ScopeGuard rollback_signal_mask([&]() {
|
2020-09-12 06:11:07 +03:00
|
|
|
if (params.sigmask)
|
2020-09-09 05:37:15 +03:00
|
|
|
current_thread->update_signal_mask(previous_signal_mask);
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
fd_set fds_read, fds_write, fds_except;
|
|
|
|
if (params.readfds && !copy_from_user(&fds_read, params.readfds))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.writefds && !copy_from_user(&fds_write, params.writefds))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.exceptfds && !copy_from_user(&fds_except, params.exceptfds))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-30 02:05:27 +03:00
|
|
|
|
|
|
|
Thread::SelectBlocker::FDVector fds_info;
|
|
|
|
Vector<int> fds;
|
|
|
|
for (int fd = 0; fd < params.nfds; fd++) {
|
|
|
|
u32 block_flags = (u32)Thread::FileBlocker::BlockFlags::None;
|
|
|
|
if (params.readfds && FD_ISSET(fd, &fds_read))
|
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
|
|
|
|
if (params.writefds && FD_ISSET(fd, &fds_write))
|
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
|
|
|
|
if (params.exceptfds && FD_ISSET(fd, &fds_except))
|
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Exception;
|
|
|
|
if (block_flags == (u32)Thread::FileBlocker::BlockFlags::None)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto description = file_description(fd);
|
|
|
|
if (!description) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("sys$select: Bad fd number {}", fd);
|
2021-03-01 15:49:16 +03:00
|
|
|
return EBADF;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
fds_info.append({ description.release_nonnull(), (Thread::FileBlocker::BlockFlags)block_flags });
|
|
|
|
fds.append(fd);
|
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (IO_DEBUG || POLL_SELECT_DEBUG)
|
2021-01-15 02:18:55 +03:00
|
|
|
dbgln("selecting on {} fds, timeout={}", fds_info.size(), params.timeout);
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted()) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(POLL_SELECT_DEBUG, "select was interrupted");
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINTR;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.readfds)
|
|
|
|
FD_ZERO(&fds_read);
|
|
|
|
if (params.writefds)
|
|
|
|
FD_ZERO(&fds_write);
|
|
|
|
if (params.exceptfds)
|
|
|
|
FD_ZERO(&fds_except);
|
|
|
|
|
2020-07-31 00:38:15 +03:00
|
|
|
int marked_fd_count = 0;
|
2020-11-30 02:05:27 +03:00
|
|
|
for (size_t i = 0; i < fds_info.size(); i++) {
|
|
|
|
auto& fd_entry = fds_info[i];
|
|
|
|
if (fd_entry.unblocked_flags == Thread::FileBlocker::BlockFlags::None)
|
|
|
|
continue;
|
|
|
|
if (params.readfds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Read)) {
|
|
|
|
FD_SET(fds[i], &fds_read);
|
|
|
|
marked_fd_count++;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.writefds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Write)) {
|
|
|
|
FD_SET(fds[i], &fds_write);
|
|
|
|
marked_fd_count++;
|
|
|
|
}
|
|
|
|
if (params.exceptfds && ((u32)fd_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Exception)) {
|
|
|
|
FD_SET(fds[i], &fds_except);
|
|
|
|
marked_fd_count++;
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.readfds && !copy_to_user(params.readfds, &fds_read))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.writefds && !copy_to_user(params.writefds, &fds_write))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-30 02:05:27 +03:00
|
|
|
if (params.exceptfds && !copy_to_user(params.exceptfds, &fds_except))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
return marked_fd_count;
|
|
|
|
}
|
|
|
|
|
2021-03-01 15:49:16 +03:00
|
|
|
KResultOr<int> Process::sys$poll(Userspace<const Syscall::SC_poll_params*> user_params)
|
2020-07-31 00:38:15 +03:00
|
|
|
{
|
|
|
|
REQUIRE_PROMISE(stdio);
|
2020-08-05 09:15:42 +03:00
|
|
|
|
2020-07-31 00:38:15 +03:00
|
|
|
// FIXME: Return -EINVAL if timeout is invalid.
|
2020-08-05 09:15:42 +03:00
|
|
|
Syscall::SC_poll_params params;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(¶ms, user_params))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-02-13 12:27:01 +03:00
|
|
|
if (params.nfds >= m_max_open_file_descriptors)
|
2021-03-01 15:49:16 +03:00
|
|
|
return ENOBUFS;
|
2021-02-13 02:58:33 +03:00
|
|
|
|
2020-11-15 21:58:19 +03:00
|
|
|
Thread::BlockTimeout timeout;
|
|
|
|
if (params.timeout) {
|
|
|
|
timespec timeout_copy;
|
|
|
|
if (!copy_from_user(&timeout_copy, params.timeout))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-11-15 21:58:19 +03:00
|
|
|
timeout = Thread::BlockTimeout(false, &timeout_copy);
|
|
|
|
}
|
2020-08-05 09:15:42 +03:00
|
|
|
|
|
|
|
sigset_t sigmask = {};
|
2020-09-12 06:11:07 +03:00
|
|
|
if (params.sigmask && !copy_from_user(&sigmask, params.sigmask))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
Vector<pollfd> fds_copy;
|
|
|
|
if (params.nfds > 0) {
|
|
|
|
Checked nfds_checked = sizeof(pollfd);
|
|
|
|
nfds_checked *= params.nfds;
|
|
|
|
if (nfds_checked.has_overflow())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
fds_copy.resize(params.nfds);
|
2021-02-21 19:03:03 +03:00
|
|
|
if (!copy_from_user(fds_copy.data(), ¶ms.fds[0], nfds_checked.value()))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
}
|
2020-09-18 10:49:51 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
Thread::SelectBlocker::FDVector fds_info;
|
|
|
|
for (size_t i = 0; i < params.nfds; i++) {
|
2020-09-12 06:11:07 +03:00
|
|
|
auto& pfd = fds_copy[i];
|
2020-11-30 02:05:27 +03:00
|
|
|
auto description = file_description(pfd.fd);
|
|
|
|
if (!description) {
|
2021-01-10 18:21:56 +03:00
|
|
|
dbgln("sys$poll: Bad fd number {}", pfd.fd);
|
2021-03-01 15:49:16 +03:00
|
|
|
return EBADF;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
u32 block_flags = (u32)Thread::FileBlocker::BlockFlags::Exception; // always want POLLERR, POLLHUP, POLLNVAL
|
2020-09-12 06:11:07 +03:00
|
|
|
if (pfd.events & POLLIN)
|
2020-11-30 02:05:27 +03:00
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Read;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (pfd.events & POLLOUT)
|
2020-11-30 02:05:27 +03:00
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::Write;
|
|
|
|
if (pfd.events & POLLPRI)
|
|
|
|
block_flags |= (u32)Thread::FileBlocker::BlockFlags::ReadPriority;
|
|
|
|
fds_info.append({ description.release_nonnull(), (Thread::FileBlocker::BlockFlags)block_flags });
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto current_thread = Thread::current();
|
2020-09-09 05:37:15 +03:00
|
|
|
|
|
|
|
u32 previous_signal_mask = 0;
|
2020-08-05 09:15:42 +03:00
|
|
|
if (params.sigmask)
|
2020-09-12 06:11:07 +03:00
|
|
|
previous_signal_mask = current_thread->update_signal_mask(sigmask);
|
2020-09-09 05:37:15 +03:00
|
|
|
ScopeGuard rollback_signal_mask([&]() {
|
|
|
|
if (params.sigmask)
|
|
|
|
current_thread->update_signal_mask(previous_signal_mask);
|
|
|
|
});
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (IO_DEBUG || POLL_SELECT_DEBUG)
|
2021-01-15 02:18:55 +03:00
|
|
|
dbgln("polling on {} fds, timeout={}", fds_info.size(), params.timeout);
|
2020-07-31 00:38:15 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
if (current_thread->block<Thread::SelectBlocker>(timeout, fds_info).was_interrupted())
|
2021-03-01 15:49:16 +03:00
|
|
|
return EINTR;
|
2020-07-31 00:38:15 +03:00
|
|
|
|
|
|
|
int fds_with_revents = 0;
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
for (unsigned i = 0; i < params.nfds; ++i) {
|
|
|
|
auto& pfd = fds_copy[i];
|
2020-11-30 02:05:27 +03:00
|
|
|
auto& fds_entry = fds_info[i];
|
|
|
|
|
|
|
|
pfd.revents = 0;
|
|
|
|
if (fds_entry.unblocked_flags == Thread::FileBlocker::BlockFlags::None)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Exception) {
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::ReadHangUp)
|
|
|
|
pfd.revents |= POLLRDHUP;
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::WriteError)
|
|
|
|
pfd.revents |= POLLERR;
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::WriteHangUp)
|
|
|
|
pfd.revents |= POLLNVAL;
|
2020-09-12 06:11:07 +03:00
|
|
|
} else {
|
2020-11-30 02:05:27 +03:00
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Read) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(pfd.events & POLLIN);
|
2020-09-12 06:11:07 +03:00
|
|
|
pfd.revents |= POLLIN;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::ReadPriority) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(pfd.events & POLLPRI);
|
2020-11-30 02:05:27 +03:00
|
|
|
pfd.revents |= POLLPRI;
|
|
|
|
}
|
|
|
|
if ((u32)fds_entry.unblocked_flags & (u32)Thread::FileBlocker::BlockFlags::Write) {
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(pfd.events & POLLOUT);
|
2020-09-12 06:11:07 +03:00
|
|
|
pfd.revents |= POLLOUT;
|
2020-11-30 02:05:27 +03:00
|
|
|
}
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
if (pfd.revents)
|
|
|
|
fds_with_revents++;
|
2020-07-31 00:38:15 +03:00
|
|
|
}
|
|
|
|
|
2021-02-21 19:03:03 +03:00
|
|
|
if (params.nfds > 0 && !copy_to_user(¶ms.fds[0], fds_copy.data(), params.nfds * sizeof(pollfd)))
|
2021-03-01 15:49:16 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
|
2020-07-31 00:38:15 +03:00
|
|
|
return fds_with_revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|