Luna/kernel/src/thread/Thread.cpp

273 lines
8.2 KiB
C++
Raw Normal View History

#include "thread/Thread.h"
2023-07-10 17:46:57 +00:00
#include "Log.h"
#include "arch/CPU.h"
#include "memory/MemoryManager.h"
#include "thread/Scheduler.h"
#include <bits/atfile.h>
#include <bits/open-flags.h>
#include <bits/signal.h>
#include <bits/waitpid.h>
#include <luna/Alloc.h>
2022-12-17 09:50:49 +00:00
#include <luna/Atomic.h>
#include <luna/PathParser.h>
static Atomic<pid_t> g_next_id;
2022-12-19 11:43:23 +00:00
LinkedList<Thread> g_threads;
2022-12-17 09:50:49 +00:00
void Thread::init()
{
g_next_id = 2;
2022-12-17 09:50:49 +00:00
}
Result<Thread*> new_thread()
{
2023-01-10 18:31:41 +00:00
Thread* const thread = TRY(make<Thread>());
thread->id = g_next_id++;
return thread;
2023-01-02 12:07:29 +00:00
}
pid_t next_thread_id()
{
return g_next_id.load();
}
Result<int> Thread::allocate_fd(int min)
{
if (min < 0 || min >= FD_MAX) return err(EINVAL);
for (int i = min; i < FD_MAX; i++)
{
// FIXME: Possible race condition if multiple threads share a FileDescriptorTable? Let's not worry about it for
// now, we're still a long way away from reaching that point.
if (!fd_table[i].has_value()) { return i; }
}
return err(EMFILE);
}
Result<FileDescriptor*> Thread::resolve_fd(int fd)
{
if (fd < 0 || fd >= FD_MAX) return err(EBADF);
Option<FileDescriptor>& maybe_descriptor = fd_table[fd];
if (!maybe_descriptor.has_value()) return err(EBADF);
return maybe_descriptor.value_ptr();
}
2024-01-05 21:12:58 +00:00
Result<int> Thread::allocate_timerid()
{
for (int i = 0; i < MAX_POSIX_TIMERS; i++)
{
// FIXME: Possible race condition, this should be used alongside a mutex.
if (!posix_timers[i].has_value()) { return i; }
}
return err(EMFILE);
}
Result<Timer*> Thread::resolve_timerid(int tid)
{
if (tid < 0 || tid >= MAX_POSIX_TIMERS) return err(EBADF);
Option<Timer>& maybe_timer = posix_timers[tid];
if (!maybe_timer.has_value()) return err(EINVAL);
return maybe_timer.value_ptr();
}
Result<SharedPtr<VFS::Inode>> Thread::resolve_atfile(int dirfd, const String& path, bool allow_empty_path,
bool follow_last_symlink, SharedPtr<VFS::Inode>* parent_inode)
{
if (parent_inode) *parent_inode = this->current_directory;
if (PathParser::is_absolute(path.view()))
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, {}, follow_last_symlink);
if (dirfd == AT_FDCWD)
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, this->current_directory,
follow_last_symlink);
auto descriptor = TRY(resolve_fd(dirfd));
if (parent_inode) *parent_inode = descriptor->inode();
if (path.is_empty() && allow_empty_path) return descriptor->inode();
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, descriptor->inode(), follow_last_symlink);
}
[[noreturn]] void Thread::exit_and_signal_parent(int _status)
{
#ifndef MOON_ENABLE_TESTING_FEATURES
2023-07-10 11:04:47 +00:00
if (this->id == 1) fail("the init process exited");
#else
if (this->id == 1) CPU::magic_exit(_status);
#endif
if (is_kernel) state = ThreadState::Dying;
else
{
Scheduler::for_each_child(this, [](Thread* child) {
child->parent = Scheduler::init_thread();
return true;
});
if (is_session_leader())
{
kinfoln("thread %d is exiting as a session leader, sending signals to session", id);
// FIXME: Send SIGHUP only to the foreground process group if the session has a controlling terminal.
Scheduler::for_each_in_session(sid, [this](Thread* thread) {
if (thread == this) return true;
thread->sid = 0;
thread->controlling_terminal = {};
thread->send_signal(SIGHUP);
kinfoln("reparenting and sending SIGHUP to %d", thread->id);
return true;
});
}
if (parent)
{
if (parent->state == ThreadState::Waiting)
{
auto child = *parent->child_being_waited_for;
if (child == -1 || child == id)
{
parent->child_being_waited_for = id;
parent->wake_up();
}
}
else
{
while (parent->pending_signals.get(SIGCHLD - 1)) kernel_yield();
parent->send_signal(SIGCHLD);
}
}
state = ThreadState::Exited;
}
status = _status;
kernel_yield();
unreachable();
}
enum class DefaultSignalAction
{
Ignore,
Terminate,
Stop,
};
// FIXME: Implement coredumps for some signals.
static constexpr DefaultSignalAction default_actions[] = {
DefaultSignalAction::Terminate, // SIGHUP
DefaultSignalAction::Terminate, // SIGINT
DefaultSignalAction::Terminate, // SIGQUIT (dump core)
DefaultSignalAction::Terminate, // SIGILL (dump core)
DefaultSignalAction::Terminate, // SIGTRAP (dump core)
DefaultSignalAction::Terminate, // SIGABRT (dump core)
DefaultSignalAction::Ignore, // SIGCHLD
DefaultSignalAction::Terminate, // SIGFPE (dump core)
DefaultSignalAction::Terminate, // SIGKILL
DefaultSignalAction::Stop, // SIGSTOP
DefaultSignalAction::Terminate, // SIGSEGV (dump core)
DefaultSignalAction::Ignore, // SIGCONT (Handled separately)
DefaultSignalAction::Terminate, // SIGPIPE
DefaultSignalAction::Terminate, // SIGALRM
DefaultSignalAction::Terminate, // SIGTERM
DefaultSignalAction::Terminate, // SIGTTIN
DefaultSignalAction::Terminate, // SIGTTOU
DefaultSignalAction::Ignore, // SIGWINCH
};
2023-07-10 17:46:57 +00:00
void Thread::process_pending_signals(Registers* current_regs)
{
interrupted = false;
2023-07-10 17:46:57 +00:00
for (int i = 0; i < NSIG; i++)
{
int signo = i + 1;
if (signo != SIGKILL && signo != SIGSTOP && signal_mask.get(i)) continue;
if (pending_signals.get(i))
2023-07-10 17:46:57 +00:00
{
pending_signals.set(i, false);
2023-07-10 17:46:57 +00:00
auto handler = signal_handlers[i];
2023-11-16 21:02:17 +00:00
if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN) return;
if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
2023-07-10 17:46:57 +00:00
{
default_signal:
if (id == 1)
{
kwarnln("signal: init got a signal it has no handler for, ignoring");
return;
}
auto action = default_actions[i];
switch (action)
{
case DefaultSignalAction::Ignore: return;
case DefaultSignalAction::Terminate:
kwarnln("Terminating thread %d with signal %d", id, signo);
CPU::print_stack_trace_at(current_regs);
exit_and_signal_parent(signo | _SIGBIT);
case DefaultSignalAction::Stop: stop();
default: return;
}
2023-07-10 17:46:57 +00:00
}
// If we fail to deliver the signal (usually because there's not enough space on the stack), execute the
// default action. FIXME: Should this be changed?
2023-07-10 17:46:57 +00:00
if (!deliver_signal(signo, current_regs)) goto default_signal;
return;
}
}
}
bool Thread::will_ignore_pending_signal()
{
for (int i = 0; i < NSIG; i++)
{
if (pending_signals.get(i))
{
int signo = i + 1;
if (signo == SIGKILL || signo == SIGSTOP) return false;
if (signal_mask.get(i)) continue;
auto handler = signal_handlers[i];
if (handler.sa_handler == SIG_IGN) return true;
if (handler.sa_handler == SIG_DFL && default_actions[i] == DefaultSignalAction::Ignore) return true;
return false;
}
}
return false;
}
void Thread::send_signal(int signo)
{
if (is_kernel) return;
if (state == ThreadState::Exited || state == ThreadState::Dying) return;
if (state == ThreadState::Stopped && signo == SIGCONT)
{
wake_up();
return;
}
check(signo > 0 && signo <= NSIG);
pending_signals.set(signo - 1, true);
if (state == ThreadState::Waiting || state == ThreadState::Sleeping || is_in_kernel(&regs))
{
if (state == ThreadState::Stopped && signo != SIGKILL) return;
interrupted = true;
wake_up();
}
}
void Thread::stop()
{
state = ThreadState::Stopped;
kernel_yield();
}