2022-12-07 14:02:46 +00:00
|
|
|
#include "thread/Thread.h"
|
2023-07-10 17:46:57 +00:00
|
|
|
#include "Log.h"
|
2023-07-21 12:09:37 +00:00
|
|
|
#include "arch/CPU.h"
|
2023-04-15 18:26:15 +00:00
|
|
|
#include "memory/MemoryManager.h"
|
2023-06-19 10:33:25 +00:00
|
|
|
#include "thread/Scheduler.h"
|
2023-04-15 18:26:15 +00:00
|
|
|
#include <bits/atfile.h>
|
2023-03-12 13:43:58 +00:00
|
|
|
#include <bits/open-flags.h>
|
2023-07-10 18:30:37 +00:00
|
|
|
#include <bits/signal.h>
|
2023-07-10 18:16:06 +00:00
|
|
|
#include <bits/waitpid.h>
|
2022-12-07 14:02:46 +00:00
|
|
|
#include <luna/Alloc.h>
|
2022-12-17 09:50:49 +00:00
|
|
|
#include <luna/Atomic.h>
|
2023-04-15 18:26:15 +00:00
|
|
|
#include <luna/PathParser.h>
|
2022-12-07 14:02:46 +00:00
|
|
|
|
2023-07-26 19:32:00 +00:00
|
|
|
static Atomic<pid_t> g_next_id;
|
2022-12-07 14:02:46 +00:00
|
|
|
|
2022-12-19 11:43:23 +00:00
|
|
|
LinkedList<Thread> g_threads;
|
2022-12-07 14:02:46 +00:00
|
|
|
|
2022-12-17 09:50:49 +00:00
|
|
|
void Thread::init()
|
|
|
|
{
|
2023-04-28 11:23:07 +00:00
|
|
|
g_next_id = 2;
|
2022-12-17 09:50:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 14:02:46 +00:00
|
|
|
Result<Thread*> new_thread()
|
|
|
|
{
|
2023-01-10 18:31:41 +00:00
|
|
|
Thread* const thread = TRY(make<Thread>());
|
2022-12-07 14:02:46 +00:00
|
|
|
|
|
|
|
thread->id = g_next_id++;
|
|
|
|
|
|
|
|
return thread;
|
2023-01-02 12:07:29 +00:00
|
|
|
}
|
2023-03-11 16:45:20 +00:00
|
|
|
|
2023-07-26 19:32:00 +00:00
|
|
|
pid_t next_thread_id()
|
2023-07-12 17:23:06 +00:00
|
|
|
{
|
|
|
|
return g_next_id.load();
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:45:20 +00:00
|
|
|
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.
|
2023-03-12 12:57:38 +00:00
|
|
|
if (!fd_table[i].has_value()) { return i; }
|
2023-03-11 16:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err(EMFILE);
|
|
|
|
}
|
2023-03-12 12:57:38 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2023-03-12 13:43:58 +00:00
|
|
|
|
2023-04-15 18:26:15 +00:00
|
|
|
Result<SharedPtr<VFS::Inode>> Thread::resolve_atfile(int dirfd, const String& path, bool allow_empty_path,
|
2023-05-20 19:46:31 +00:00
|
|
|
bool follow_last_symlink, SharedPtr<VFS::Inode>* parent_inode)
|
2023-04-15 18:26:15 +00:00
|
|
|
{
|
|
|
|
if (parent_inode) *parent_inode = this->current_directory;
|
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
if (PathParser::is_absolute(path.view()))
|
2023-11-22 17:49:40 +00:00
|
|
|
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, {}, follow_last_symlink);
|
2023-04-15 18:26:15 +00:00
|
|
|
|
2023-05-20 19:46:31 +00:00
|
|
|
if (dirfd == AT_FDCWD)
|
2023-11-22 17:49:40 +00:00
|
|
|
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, this->current_directory,
|
|
|
|
follow_last_symlink);
|
2023-04-15 18:26:15 +00:00
|
|
|
|
|
|
|
auto descriptor = TRY(resolve_fd(dirfd));
|
|
|
|
|
2023-07-27 17:21:23 +00:00
|
|
|
if (parent_inode) *parent_inode = descriptor->inode();
|
2023-04-15 18:26:15 +00:00
|
|
|
|
2023-07-27 17:21:23 +00:00
|
|
|
if (path.is_empty() && allow_empty_path) return descriptor->inode();
|
2023-04-15 18:26:15 +00:00
|
|
|
|
2023-11-22 17:49:40 +00:00
|
|
|
return VFS::resolve_path(path.chars(), this->auth, &this->extra_groups, descriptor->inode(), follow_last_symlink);
|
2023-04-15 18:26:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 18:16:06 +00:00
|
|
|
[[noreturn]] void Thread::exit_and_signal_parent(int _status)
|
2023-06-19 10:33:25 +00:00
|
|
|
{
|
2023-07-21 12:09:37 +00:00
|
|
|
#ifndef MOON_ENABLE_TESTING_FEATURES
|
2023-07-10 11:04:47 +00:00
|
|
|
if (this->id == 1) fail("the init process exited");
|
2023-07-21 12:09:37 +00:00
|
|
|
#else
|
|
|
|
if (this->id == 1) CPU::magic_exit(_status);
|
|
|
|
#endif
|
2023-06-19 10:33:25 +00:00
|
|
|
if (is_kernel) state = ThreadState::Dying;
|
|
|
|
else
|
|
|
|
{
|
2023-06-19 10:35:31 +00:00
|
|
|
Scheduler::for_each_child(this, [](Thread* child) {
|
|
|
|
child->parent = Scheduler::init_thread();
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2023-10-14 18:41:34 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-10 18:49:22 +00:00
|
|
|
if (parent)
|
2023-06-19 10:33:25 +00:00
|
|
|
{
|
2023-07-10 18:49:22 +00:00
|
|
|
if (parent->state == ThreadState::Waiting)
|
2023-06-19 10:33:25 +00:00
|
|
|
{
|
2023-07-10 18:49:22 +00:00
|
|
|
auto child = *parent->child_being_waited_for;
|
2023-07-26 19:32:00 +00:00
|
|
|
if (child == -1 || child == id)
|
2023-07-10 18:49:22 +00:00
|
|
|
{
|
2023-07-26 19:32:00 +00:00
|
|
|
parent->child_being_waited_for = id;
|
2023-07-10 18:49:22 +00:00
|
|
|
parent->wake_up();
|
|
|
|
}
|
2023-06-19 10:33:25 +00:00
|
|
|
}
|
2023-10-14 18:41:34 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while (parent->pending_signals.get(SIGCHLD - 1)) kernel_yield();
|
|
|
|
parent->send_signal(SIGCHLD);
|
|
|
|
}
|
2023-06-19 10:33:25 +00:00
|
|
|
}
|
2023-06-19 10:35:31 +00:00
|
|
|
|
2023-06-19 10:33:25 +00:00
|
|
|
state = ThreadState::Exited;
|
|
|
|
}
|
|
|
|
status = _status;
|
|
|
|
kernel_yield();
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:59:01 +00:00
|
|
|
enum class DefaultSignalAction
|
|
|
|
{
|
|
|
|
Ignore,
|
|
|
|
Terminate,
|
2023-10-28 13:15:32 +00:00
|
|
|
Stop,
|
2023-07-10 17:59:01 +00:00
|
|
|
};
|
|
|
|
|
2023-07-12 11:45:36 +00:00
|
|
|
// FIXME: Implement coredumps for some signals.
|
2023-07-10 17:59:01 +00:00
|
|
|
static constexpr DefaultSignalAction default_actions[] = {
|
2023-07-10 18:30:37 +00:00
|
|
|
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
|
2023-10-28 13:15:32 +00:00
|
|
|
DefaultSignalAction::Stop, // SIGSTOP
|
2023-07-10 18:30:37 +00:00
|
|
|
DefaultSignalAction::Terminate, // SIGSEGV (dump core)
|
2023-10-28 13:15:32 +00:00
|
|
|
DefaultSignalAction::Ignore, // SIGCONT (Handled separately)
|
2023-07-10 18:30:37 +00:00
|
|
|
DefaultSignalAction::Terminate, // SIGPIPE
|
|
|
|
DefaultSignalAction::Terminate, // SIGALRM
|
|
|
|
DefaultSignalAction::Terminate, // SIGTERM
|
2023-07-12 11:45:36 +00:00
|
|
|
DefaultSignalAction::Terminate, // SIGTTIN
|
|
|
|
DefaultSignalAction::Terminate, // SIGTTOU
|
2023-07-27 12:00:30 +00:00
|
|
|
DefaultSignalAction::Ignore, // SIGWINCH
|
2023-07-10 17:59:01 +00:00
|
|
|
};
|
|
|
|
|
2023-07-10 17:46:57 +00:00
|
|
|
void Thread::process_pending_signals(Registers* current_regs)
|
|
|
|
{
|
2023-07-12 11:48:43 +00:00
|
|
|
interrupted = false;
|
2023-07-10 17:46:57 +00:00
|
|
|
for (int i = 0; i < NSIG; i++)
|
|
|
|
{
|
2023-07-10 18:30:37 +00:00
|
|
|
int signo = i + 1;
|
2023-08-23 08:51:02 +00:00
|
|
|
if (signo != SIGKILL && signo != SIGSTOP && signal_mask.get(i)) continue;
|
|
|
|
if (pending_signals.get(i))
|
2023-07-10 17:46:57 +00:00
|
|
|
{
|
2023-08-23 08:51:02 +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;
|
2023-07-10 18:30:37 +00:00
|
|
|
if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
|
2023-07-10 17:46:57 +00:00
|
|
|
{
|
|
|
|
default_signal:
|
2023-07-10 17:48:46 +00:00
|
|
|
if (id == 1)
|
|
|
|
{
|
|
|
|
kwarnln("signal: init got a signal it has no handler for, ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-10 17:59:01 +00:00
|
|
|
auto action = default_actions[i];
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case DefaultSignalAction::Ignore: return;
|
2023-07-10 18:16:06 +00:00
|
|
|
case DefaultSignalAction::Terminate: exit_and_signal_parent(signo | _SIGBIT);
|
2023-10-28 13:15:32 +00:00
|
|
|
case DefaultSignalAction::Stop: stop();
|
2023-07-10 18:16:06 +00:00
|
|
|
default: return;
|
2023-07-10 17:59:01 +00:00
|
|
|
}
|
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.
|
|
|
|
if (!deliver_signal(signo, current_regs)) goto default_signal;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 08:41:46 +00:00
|
|
|
bool Thread::will_ignore_pending_signal()
|
2023-07-12 11:48:43 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < NSIG; i++)
|
|
|
|
{
|
2023-08-23 08:51:02 +00:00
|
|
|
if (pending_signals.get(i))
|
2023-07-12 11:48:43 +00:00
|
|
|
{
|
|
|
|
int signo = i + 1;
|
|
|
|
if (signo == SIGKILL || signo == SIGSTOP) return false;
|
2023-08-23 08:51:02 +00:00
|
|
|
if (signal_mask.get(i)) continue;
|
2023-08-08 08:41:46 +00:00
|
|
|
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;
|
2023-07-12 11:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-10 18:16:06 +00:00
|
|
|
void Thread::send_signal(int signo)
|
|
|
|
{
|
2023-08-22 13:25:05 +00:00
|
|
|
if (is_kernel) return;
|
2023-09-22 20:40:24 +00:00
|
|
|
if (state == ThreadState::Exited || state == ThreadState::Dying) return;
|
2023-08-22 13:25:05 +00:00
|
|
|
|
2023-10-28 13:15:32 +00:00
|
|
|
if (state == ThreadState::Stopped && signo == SIGCONT)
|
|
|
|
{
|
|
|
|
wake_up();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-10 18:16:06 +00:00
|
|
|
check(signo > 0 && signo <= NSIG);
|
2023-08-23 08:51:02 +00:00
|
|
|
pending_signals.set(signo - 1, true);
|
2023-07-12 11:48:43 +00:00
|
|
|
|
2023-09-18 05:19:29 +00:00
|
|
|
if (state == ThreadState::Waiting || state == ThreadState::Sleeping || is_in_kernel(®s))
|
2023-07-12 11:48:43 +00:00
|
|
|
{
|
2023-10-28 13:15:32 +00:00
|
|
|
if (state == ThreadState::Stopped && signo != SIGKILL) return;
|
2023-07-12 11:48:43 +00:00
|
|
|
interrupted = true;
|
|
|
|
wake_up();
|
|
|
|
}
|
2023-07-10 18:16:06 +00:00
|
|
|
}
|
2023-10-28 13:15:32 +00:00
|
|
|
|
|
|
|
void Thread::stop()
|
|
|
|
{
|
|
|
|
state = ThreadState::Stopped;
|
|
|
|
kernel_yield();
|
|
|
|
}
|