kernel: Support returning termination signals from waitpid
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
apio 2023-07-10 20:16:06 +02:00
parent fc3fdc2b87
commit cde467ee46
Signed by: apio
GPG Key ID: B8A7D06E42258954
6 changed files with 28 additions and 7 deletions

View File

@ -328,7 +328,14 @@ int main()
{ {
if (service.pid.has_value() && service.pid.value() == child) if (service.pid.has_value() && service.pid.value() == child)
{ {
do_log("[init] service %s exited with status %d\n", service.name.chars(), WEXITSTATUS(status)); if (WIFEXITED(status))
{
do_log("[init] service %s exited with status %d\n", service.name.chars(), WEXITSTATUS(status));
}
else
{
do_log("[init] service %s was terminated by signal %d\n", service.name.chars(), WTERMSIG(status));
}
if (service.restart) if (service.restart)
{ {

View File

@ -62,7 +62,7 @@ Result<u64> sys_kill(Registers*, SyscallArgs args)
if (target->is_kernel) return 0; if (target->is_kernel) return 0;
if (signo == 0) return 0; if (signo == 0) return 0;
target->pending_signals |= 1 << (signo - 1); target->send_signal(signo);
return 0; return 0;
} }

View File

@ -4,6 +4,7 @@
#include "thread/Scheduler.h" #include "thread/Scheduler.h"
#include <bits/atfile.h> #include <bits/atfile.h>
#include <bits/open-flags.h> #include <bits/open-flags.h>
#include <bits/waitpid.h>
#include <luna/Alloc.h> #include <luna/Alloc.h>
#include <luna/Atomic.h> #include <luna/Atomic.h>
#include <luna/PathParser.h> #include <luna/PathParser.h>
@ -70,7 +71,7 @@ Result<SharedPtr<VFS::Inode>> Thread::resolve_atfile(int dirfd, const String& pa
return VFS::resolve_path(path.chars(), this->auth, descriptor->inode, follow_last_symlink); return VFS::resolve_path(path.chars(), this->auth, descriptor->inode, follow_last_symlink);
} }
[[noreturn]] void Thread::exit_and_signal_parent(u8 _status) [[noreturn]] void Thread::exit_and_signal_parent(int _status)
{ {
if (this->id == 1) fail("the init process exited"); if (this->id == 1) fail("the init process exited");
if (is_kernel) state = ThreadState::Dying; if (is_kernel) state = ThreadState::Dying;
@ -140,7 +141,8 @@ void Thread::process_pending_signals(Registers* current_regs)
{ {
case DefaultSignalAction::Ignore: return; case DefaultSignalAction::Ignore: return;
// FIXME: Add signal exit codes. // FIXME: Add signal exit codes.
case DefaultSignalAction::Terminate: exit_and_signal_parent(255); case DefaultSignalAction::Terminate: exit_and_signal_parent(signo | _SIGBIT);
default: return;
} }
} }
// If we fail to deliver the signal (usually because there's not enough space on the stack), execute the // If we fail to deliver the signal (usually because there's not enough space on the stack), execute the
@ -151,6 +153,12 @@ void Thread::process_pending_signals(Registers* current_regs)
} }
} }
void Thread::send_signal(int signo)
{
check(signo > 0 && signo <= NSIG);
pending_signals |= 1 << (signo - 1);
}
bool FileDescriptor::should_append() bool FileDescriptor::should_append()
{ {
return flags & O_APPEND; return flags & O_APPEND;

View File

@ -91,7 +91,7 @@ struct Thread : public LinkedListNode<Thread>
bool is_kernel { true }; bool is_kernel { true };
u8 status { 0 }; int status { 0 };
mode_t umask { 0 }; mode_t umask { 0 };
@ -110,7 +110,7 @@ struct Thread : public LinkedListNode<Thread>
PageDirectory* active_directory { nullptr }; PageDirectory* active_directory { nullptr };
[[noreturn]] void exit_and_signal_parent(u8 status); [[noreturn]] void exit_and_signal_parent(int status);
bool is_idle() bool is_idle()
{ {
@ -144,6 +144,8 @@ struct Thread : public LinkedListNode<Thread>
Result<u64> push_mem_on_stack(const u8* mem, usize size); Result<u64> push_mem_on_stack(const u8* mem, usize size);
Result<u64> pop_mem_from_stack(u8* mem, usize size); Result<u64> pop_mem_from_stack(u8* mem, usize size);
void send_signal(int signo);
static void init(); static void init();
}; };

View File

@ -3,6 +3,8 @@
#ifndef _BITS_WAITPID_H #ifndef _BITS_WAITPID_H
#define _BITS_WAITPID_H #define _BITS_WAITPID_H
#define _SIGBIT 0x100
#define WNOHANG 1 #define WNOHANG 1
#endif #endif

View File

@ -6,8 +6,10 @@
#include <bits/waitpid.h> #include <bits/waitpid.h>
#include <sys/types.h> #include <sys/types.h>
#define WIFEXITED(ret) ((ret) | 0) #define WIFEXITED(ret) (((ret)&_SIGBIT) == 0)
#define WEXITSTATUS(ret) ((ret)&0xff) #define WEXITSTATUS(ret) ((ret)&0xff)
#define WIFSIGNALED(ret) (((ret)&_SIGBIT) == _SIGBIT)
#define WTERMSIG(ret) ((ret)&0xff)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"