2023-12-31 16:58:02 +01:00
|
|
|
#include "KernelContext.hpp"
|
2023-07-03 13:10:16 +02:00
|
|
|
#include "sys/sysproto.hpp"
|
2024-01-13 18:57:02 +01:00
|
|
|
#include "thread/Process.hpp"
|
|
|
|
|
#include "thread/Thread.hpp"
|
|
|
|
|
#include "ucontext.hpp"
|
2023-07-29 23:32:09 +02:00
|
|
|
#include "utils/Logs.hpp"
|
2023-12-31 16:58:02 +01:00
|
|
|
#include <csignal>
|
2023-07-03 13:10:16 +02:00
|
|
|
|
2023-07-06 18:16:25 +02:00
|
|
|
orbis::SysResult orbis::sys_sigaction(Thread *thread, sint sig,
|
2024-01-13 18:57:02 +01:00
|
|
|
ptr<SigAction> act, ptr<SigAction> oact) {
|
|
|
|
|
ORBIS_LOG_WARNING(__FUNCTION__, sig, act, oact);
|
|
|
|
|
|
|
|
|
|
auto &sigAct = thread->tproc->sigActions[sig];
|
|
|
|
|
if (oact != nullptr) {
|
|
|
|
|
if (auto errc = uwrite(oact, sigAct); errc != orbis::ErrorCode{}) {
|
|
|
|
|
return errc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (act != nullptr) {
|
|
|
|
|
if (auto errc = uread(sigAct, act); errc != ErrorCode{}) {
|
|
|
|
|
return errc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ORBIS_LOG_WARNING(__FUNCTION__, sigAct.handler, sigAct.flags,
|
|
|
|
|
sigAct.mask.bits[0], sigAct.mask.bits[1],
|
|
|
|
|
sigAct.mask.bits[2], sigAct.mask.bits[3]);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-29 23:32:09 +02:00
|
|
|
return {};
|
2023-07-06 18:16:25 +02:00
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
|
2023-07-06 18:16:25 +02:00
|
|
|
orbis::SysResult orbis::sys_sigprocmask(Thread *thread, sint how,
|
2024-01-13 18:57:02 +01:00
|
|
|
ptr<SigSet> set, ptr<SigSet> oset) {
|
2024-11-13 19:53:05 +01:00
|
|
|
std::lock_guard lock(thread->mtx);
|
|
|
|
|
|
2023-07-03 13:10:16 +02:00
|
|
|
if (oset) {
|
2024-01-13 18:57:02 +01:00
|
|
|
ORBIS_RET_ON_ERROR(uwrite(oset, thread->sigMask));
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (set) {
|
2024-01-13 18:57:02 +01:00
|
|
|
SigSet _set;
|
|
|
|
|
ORBIS_RET_ON_ERROR(uread(_set, set));
|
|
|
|
|
|
2024-11-13 19:53:05 +01:00
|
|
|
auto newSigMask = thread->sigMask;
|
|
|
|
|
auto oldSigMask = newSigMask;
|
|
|
|
|
|
2023-07-03 13:10:16 +02:00
|
|
|
switch (how) {
|
|
|
|
|
case 1: // block
|
2024-01-13 18:57:02 +01:00
|
|
|
for (std::size_t i = 0; i < 4; ++i) {
|
2024-11-13 19:53:05 +01:00
|
|
|
newSigMask.bits[i] |= _set.bits[i];
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
break;
|
2024-01-13 18:57:02 +01:00
|
|
|
|
|
|
|
|
case 2: // unblock
|
|
|
|
|
for (std::size_t i = 0; i < 4; ++i) {
|
2024-11-13 19:53:05 +01:00
|
|
|
newSigMask.bits[i] &= ~_set.bits[i];
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
|
|
|
|
break;
|
2024-01-13 18:57:02 +01:00
|
|
|
case 3: // set
|
2024-11-13 19:53:05 +01:00
|
|
|
newSigMask = _set;
|
2024-01-13 18:57:02 +01:00
|
|
|
break;
|
2023-07-03 13:10:16 +02:00
|
|
|
|
|
|
|
|
default:
|
2023-07-29 23:32:09 +02:00
|
|
|
ORBIS_LOG_ERROR("sys_sigprocmask: unimplemented how", how);
|
|
|
|
|
thread->where();
|
2024-01-13 18:57:02 +01:00
|
|
|
return ErrorCode::INVAL;
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
|
2024-11-13 19:53:05 +01:00
|
|
|
newSigMask.clear(kSigKill);
|
|
|
|
|
newSigMask.clear(kSigStop);
|
|
|
|
|
|
|
|
|
|
thread->sigMask = newSigMask;
|
|
|
|
|
|
|
|
|
|
for (std::size_t word = 0; word < std::size(newSigMask.bits); ++word) {
|
|
|
|
|
auto unblockedBits = ~oldSigMask.bits[word] & newSigMask.bits[word];
|
|
|
|
|
std::uint32_t offset = word * 32 + 1;
|
|
|
|
|
|
|
|
|
|
for (std::uint32_t i = std::countr_zero(unblockedBits); i < 32;
|
|
|
|
|
i += std::countr_zero(unblockedBits >> (i + 1)) + 1) {
|
|
|
|
|
thread->notifyUnblockedSignal(offset + i);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-03 13:10:16 +02:00
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
|
2023-07-03 13:10:16 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigwait(Thread *thread, ptr<const SigSet> set,
|
2023-07-06 18:16:25 +02:00
|
|
|
ptr<sint> sig) {
|
|
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigtimedwait(Thread *thread, ptr<const SigSet> set,
|
2023-07-06 18:16:25 +02:00
|
|
|
ptr<struct siginfo> info,
|
2023-07-12 04:22:44 +02:00
|
|
|
ptr<const timespec> timeout) {
|
2023-07-06 18:16:25 +02:00
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigwaitinfo(Thread *thread, ptr<const SigSet> set,
|
2023-07-06 18:16:25 +02:00
|
|
|
ptr<struct siginfo> info) {
|
|
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigpending(Thread *thread, ptr<SigSet> set) {
|
2023-07-06 18:16:25 +02:00
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigsuspend(Thread *thread, ptr<const SigSet> set) {
|
2023-07-06 18:16:25 +02:00
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
|
|
|
|
orbis::SysResult orbis::sys_sigaltstack(Thread *thread, ptr<struct stack_t> ss,
|
|
|
|
|
ptr<struct stack_t> oss) {
|
|
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
|
|
|
|
orbis::SysResult orbis::sys_kill(Thread *thread, sint pid, sint signum) {
|
2024-01-13 18:57:02 +01:00
|
|
|
ORBIS_LOG_WARNING(__FUNCTION__, pid, signum);
|
2023-12-31 16:58:02 +01:00
|
|
|
|
|
|
|
|
int hostPid = pid;
|
|
|
|
|
if (pid > 0) {
|
|
|
|
|
auto process = g_context.findProcessById(pid);
|
|
|
|
|
if (process == nullptr) {
|
|
|
|
|
return ErrorCode::SRCH;
|
|
|
|
|
}
|
|
|
|
|
hostPid = process->hostPid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: wrap signal
|
2024-01-13 18:57:02 +01:00
|
|
|
// int result = ::kill(hostPid, signum);
|
|
|
|
|
// if (result < 0) {
|
|
|
|
|
// return static_cast<ErrorCode>(errno);
|
|
|
|
|
// }
|
2023-12-31 16:58:02 +01:00
|
|
|
|
|
|
|
|
return {};
|
2023-07-06 18:16:25 +02:00
|
|
|
}
|
2023-12-31 16:58:02 +01:00
|
|
|
|
2023-07-06 18:16:25 +02:00
|
|
|
orbis::SysResult orbis::sys_pdkill(Thread *thread, sint fd, sint signum) {
|
|
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
|
|
|
|
orbis::SysResult orbis::sys_sigqueue(Thread *thread, pid_t pid, sint signum,
|
|
|
|
|
ptr<void> value) {
|
|
|
|
|
return ErrorCode::NOSYS;
|
|
|
|
|
}
|
2024-01-13 18:57:02 +01:00
|
|
|
orbis::SysResult orbis::sys_sigreturn(Thread *thread, ptr<UContext> sigcntxp) {
|
|
|
|
|
ORBIS_LOG_WARNING(__FUNCTION__, sigcntxp);
|
|
|
|
|
|
|
|
|
|
// auto sigRet = thread->sigReturns.front();
|
|
|
|
|
// thread->sigReturns.erase(thread->sigReturns.begin(), thread->sigReturns.begin() + 1);
|
|
|
|
|
// writeRegister(thread->context, RegisterId::rip, sigRet.rip);
|
|
|
|
|
// writeRegister(thread->context, RegisterId::rsp, sigRet.rsp);
|
|
|
|
|
// ORBIS_LOG_ERROR(__FUNCTION__, sigRet.rip, sigRet.rsp);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orbis::SysResult orbis::nosys(Thread *thread) {
|
|
|
|
|
thread->sendSignal(kSigSys);
|
|
|
|
|
return{};
|
2023-07-06 18:16:25 +02:00
|
|
|
}
|