Skip to content

Commit

Permalink
[orbis-kernel] implement sys_socket and sys_socketpair
Browse files Browse the repository at this point in the history
stub sys_nmount, sys_wait4, sys_getsid, sys_settimeofday, sys_bind, sys_listen, sys_accept, sys_setsockopt, sys_getsockopt
Reduce log spam of sys__umtx_op
  • Loading branch information
DHrpcs3 committed Oct 31, 2023
1 parent 5fa1da7 commit 87baff1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 15 deletions.
8 changes: 7 additions & 1 deletion orbis-kernel/src/sys/sys_exit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "sys/sysproto.hpp"
#include "utils/Logs.hpp"
#include <chrono>
#include <thread>

orbis::SysResult orbis::sys_exit(Thread *thread, sint status) {
if (auto exit = thread->tproc->ops->exit) {
Expand All @@ -13,5 +16,8 @@ orbis::SysResult orbis::sys_abort2(Thread *thread, ptr<const char> why,
}
orbis::SysResult orbis::sys_wait4(Thread *thread, sint pid, ptr<sint> status,
sint options, ptr<struct rusage> rusage) {
return ErrorCode::NOSYS;
// TODO
ORBIS_LOG_ERROR(__FUNCTION__, pid, status, options, rusage);
std::this_thread::sleep_for(std::chrono::days(1));
return {};
}
2 changes: 1 addition & 1 deletion orbis-kernel/src/sys/sys_prot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ orbis::SysResult orbis::sys_getpgid(Thread *thread, pid_t pid) {
return ErrorCode::NOSYS;
}
orbis::SysResult orbis::sys_getsid(Thread *thread, pid_t pid) {
return ErrorCode::NOSYS;
return {};
}
orbis::SysResult orbis::sys_getuid(Thread *thread) { return ErrorCode::NOSYS; }
orbis::SysResult orbis::sys_geteuid(Thread *thread) { return ErrorCode::NOSYS; }
Expand Down
2 changes: 1 addition & 1 deletion orbis-kernel/src/sys/sys_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ orbis::SysResult orbis::sys_gettimeofday(Thread *thread, ptr<orbis::timeval> tp,
}
orbis::SysResult orbis::sys_settimeofday(Thread *thread, ptr<struct timeval> tp,
ptr<orbis::timezone> tzp) {
return ErrorCode::NOSYS;
return {};
}
orbis::SysResult orbis::sys_getitimer(Thread *thread, uint which,
ptr<struct itimerval> itv) {
Expand Down
47 changes: 40 additions & 7 deletions orbis-kernel/src/sys/sys_uipc.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,64 @@
#include "pipe.hpp"
#include "sys/sysproto.hpp"
#include "utils/Logs.hpp"
#include <chrono>
#include <sys/socket.h>
#include <thread>

orbis::SysResult orbis::sys_socket(Thread *thread, sint domain, sint type,
sint protocol) {
ORBIS_LOG_TODO(__FUNCTION__, domain, type, protocol);
if (auto socket = thread->tproc->ops->socket) {
Ref<File> file;
auto result = socket(thread, nullptr, domain, type, protocol, &file);

if (result.isError()) {
return result;
}

auto fd = thread->tproc->fileDescriptors.insert(file);
ORBIS_LOG_WARNING("Socket opened", fd);
thread->retval[0] = fd;
return {};
}
return ErrorCode::NOSYS;
}
orbis::SysResult orbis::sys_bind(Thread *thread, sint s, caddr_t name,
sint namelen) {
return ErrorCode::NOSYS;
ORBIS_LOG_ERROR(__FUNCTION__, s, name, namelen);
return {};
}
orbis::SysResult orbis::sys_listen(Thread *thread, sint s, sint backlog) {
return ErrorCode::NOSYS;
ORBIS_LOG_ERROR(__FUNCTION__, s, backlog);
return {};
}
orbis::SysResult orbis::sys_accept(Thread *thread, sint s,
ptr<struct sockaddr> from,
ptr<uint32_t> fromlenaddr) {
return ErrorCode::NOSYS;
ORBIS_LOG_ERROR(__FUNCTION__, s, from, fromlenaddr);
std::this_thread::sleep_for(std::chrono::days(1));
return SysResult::notAnError(ErrorCode::WOULDBLOCK);
}
orbis::SysResult orbis::sys_connect(Thread *thread, sint s, caddr_t name,
sint namelen) {
return ErrorCode::NOSYS;
}
orbis::SysResult orbis::sys_socketpair(Thread *thread, sint domain, sint type,
sint protocol, ptr<sint> rsv) {
return ErrorCode::NOSYS;
ORBIS_LOG_ERROR(__FUNCTION__, domain, type, protocol, rsv);

auto pipe = createPipe();
auto a = thread->tproc->fileDescriptors.insert(pipe);
auto b = thread->tproc->fileDescriptors.insert(pipe);
if (auto errc = uwrite(rsv, a); errc != ErrorCode{}) {
return errc;
}
return uwrite(rsv + 1, b);
}
orbis::SysResult orbis::sys_sendto(Thread *thread, sint s, caddr_t buf,
size_t len, sint flags, caddr_t to,
sint tolen) {
return ErrorCode::NOSYS;
return {};
}
orbis::SysResult orbis::sys_sendmsg(Thread *thread, sint s,
ptr<struct msghdr> msg, sint flags) {
Expand All @@ -48,12 +79,14 @@ orbis::SysResult orbis::sys_shutdown(Thread *thread, sint s, sint how) {
}
orbis::SysResult orbis::sys_setsockopt(Thread *thread, sint s, sint level,
sint name, caddr_t val, sint valsize) {
return ErrorCode::NOSYS;
ORBIS_LOG_TODO(__FUNCTION__, s, level, name, val, valsize);
return {};
}
orbis::SysResult orbis::sys_getsockopt(Thread *thread, sint s, sint level,
sint name, caddr_t val,
ptr<sint> avalsize) {
return ErrorCode::NOSYS;
ORBIS_LOG_TODO(__FUNCTION__, s, level, name, val, avalsize);
return {};
}
orbis::SysResult orbis::sys_getsockname(Thread *thread, sint fdes,
ptr<struct sockaddr> asa,
Expand Down
15 changes: 11 additions & 4 deletions orbis-kernel/src/sys/sys_umtx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ orbis::SysResult orbis::sys__umtx_op(Thread *thread, ptr<void> obj, sint op,
ORBIS_LOG_TRACE(__FUNCTION__, obj, op, val, uaddr1, uaddr2);
if (reinterpret_cast<std::uintptr_t>(obj) - 0x10000 > 0xff'fffe'ffff)
return ErrorCode::FAULT;
auto with_timeout = [&](auto op, bool loop = true) -> orbis::ErrorCode {
auto with_timeout = [&](auto op, bool loop = true) -> SysResult {
timespec *ts = nullptr;
timespec timeout{};
if (uaddr2 != nullptr) {
Expand All @@ -59,8 +59,15 @@ orbis::SysResult orbis::sys__umtx_op(Thread *thread, ptr<void> obj, sint op,
usec += (timeout.nsec + 999) / 1000;
if (usec >= UINT64_MAX)
usec = -2;
if (!loop)
return op(usec);

if (!loop) {
if (auto result = op(usec); result != ErrorCode::TIMEDOUT) {
return result;
}

return SysResult::notAnError(ErrorCode::TIMEDOUT);
}

auto start = std::chrono::steady_clock::now();
std::uint64_t udiff = 0;
while (true) {
Expand All @@ -70,7 +77,7 @@ orbis::SysResult orbis::sys__umtx_op(Thread *thread, ptr<void> obj, sint op,
std::chrono::steady_clock::now() - start)
.count();
if (udiff >= usec)
return ErrorCode::TIMEDOUT;
return SysResult::notAnError(ErrorCode::TIMEDOUT);
}
}
};
Expand Down
16 changes: 15 additions & 1 deletion orbis-kernel/src/sys/sys_vfs_mount.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "sys/sysproto.hpp"
#include "uio.hpp"
#include "utils/Logs.hpp"

orbis::SysResult orbis::sys_mount(Thread *thread, ptr<char> type,
ptr<char> path, sint flags, caddr_t data) {
Expand All @@ -10,5 +12,17 @@ orbis::SysResult orbis::sys_unmount(Thread *thread, ptr<char> path,
}
orbis::SysResult orbis::sys_nmount(Thread *thread, ptr<IoVec> iovp, uint iovcnt,
sint flags) {
return ErrorCode::NOSYS;
ORBIS_LOG_ERROR(__FUNCTION__, iovp, iovcnt, flags);

for (auto it = iovp; it < iovp + iovcnt; it += 2) {
IoVec a{}, b{};
uread(a, it);
uread(b, it + 1);

std::string aSv((char *)a.base, a.len);
std::string bSv((char *)b.base, b.len);

std::fprintf(stderr, "%s: '%s':'%s'\n", __FUNCTION__, aSv.c_str(), bSv.c_str());
}
return {};
}

0 comments on commit 87baff1

Please sign in to comment.