Skip to content

Commit

Permalink
[WIP] sysdeps/managarm: move posix, fs error to c error conversion out
Browse files Browse the repository at this point in the history
This should reduce code size a bit, along with generally cleaning up
code.
  • Loading branch information
ElectrodeYT committed Dec 22, 2023
1 parent 3bccc2e commit 9cf0b15
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
85 changes: 85 additions & 0 deletions sysdeps/managarm/generic/errors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <asm-generic/errno.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

#include <bits/ensure.h>
#include <mlibc/debug.hpp>
#include <mlibc/all-sysdeps.hpp>
#include <mlibc/errors.hpp>

#include <hel.h>
#include <hel-syscalls.h>

#include <posix.frigg_bragi.hpp>
#include <fs.frigg_bragi.hpp>

int posixErrorToError(managarm::posix::Errors posixError) {
using enum managarm::posix::Errors;

switch(posixError) {
case SUCCESS: return 0;
case DEAD_FORK: mlibc::panicLogger() << "mlibc: posix error DEAD_FORK must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_REQUEST: mlibc::panicLogger() << "mlibc: posix error ILLEGAL_REQUEST must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_ARGUMENTS: return EINVAL;
case FILE_NOT_FOUND: return ENOENT;
case ACCESS_DENIED: return EPERM;
case ALREADY_EXISTS: return EEXIST;
case NO_SUCH_FD: return EBADFD;
case END_OF_FILE: mlibc::panicLogger() << "mlibc: posix error END_OF_FILE must be handled manually" << frg::endlog; __builtin_unreachable();
case BAD_FD: return EBADFD;
case WOULD_BLOCK: return EWOULDBLOCK;
case BROKEN_PIPE: return EPIPE;
case NOT_SUPPORTED: return ENOTSUP;
case RESOURCE_IN_USE: return EBUSY;
case ILLEGAL_OPERATION_TARGET: return EINVAL;
case NOT_A_DIRECTORY: return ENOTDIR;
case NO_BACKING_DEVICE: return ENXIO;

// TODO: the name of this posix error implies that it might return other errors, since "ESRCH" is "No such process"
// Maybe we should rename this?
case NO_SUCH_RESOURCE: return ESRCH;
case INSUFFICIENT_PERMISSION: return EPERM;
case IS_DIRECTORY: return EISDIR;
case NOT_A_TTY: return ENOTTY;
case PROTOCOL_NOT_SUPPORTED: return EPROTONOSUPPORT;
case ADDRESS_FAMILY_NOT_SUPPORTED: return EAFNOSUPPORT;
case NO_MEMORY: return ENOMEM;
case INTERNAL_ERROR: return EIEIO;
default: mlibc::panicLogger() << "mlibc: invalid posix error: " << (int)posixError << frg::endlog; __builtin_unreachable();
}
}

int fsErrorToError(managarm::fs::Errors fsError) {
using enum managarm::fs::Errors;

switch(fsError) {
case SUCCESS: return 0;
case FILE_NOT_FOUND: return ENOENT;
case END_OF_FILE: mlibc::panicLogger() << "mlibc: posix error END_OF_FILE must be handled manually" << frg::endlog; __builtin_unreachable();
case ILLEGAL_ARGUMENT: return EINVAL;
case WOULD_BLOCK: return EWOULDBLOCK;
case SEEK_ON_PIPE: return ESPIPE;
case BROKEN_PIPE: return EPIPE;
case ACCESS_DENIED: return EPERM;
case AF_NOT_SUPPORTED: return EAFNOSUPPORT;
case DESTINATION_ADDRESS_REQUIRED: return EDESTADDRREQ;
case NETWORK_UNREACHABLE: return ENETUNREACH;
case MESSAGE_TOO_LARGE: return EMSGSIZE;
case HOST_UNREACHABLE: return EHOSTUNREACH;
case INSUFFICIENT_PERMISSIONS: return EPERM;
case ADDRESS_IN_USE: return EADDRINUSE;
case ADDRESS_NOT_AVAILABLE: return EADDRNOTAVAIL;
case NOT_CONNECTED: return ENOTCONN;
case ALREADY_EXISTS: return EEXIST;
case ILLEGAL_OPERATION_TARGET: return EINVAL;
case NOT_DIRECTORY: return ENOTDIR;
case NO_SPACE_LEFT: return ENOSPC;
case NOT_A_TERMINAL: return ENOTTY;
case NO_BACKING_DEVICE: return ENXIO;
case IS_DIRECTORY: return EISDIR;
case NOT_A_SOCKET: return ENOTSOCK;
default: mlibc::panicLogger() << "mlibc: invalid fs error: " << (int)posixError << frg::endlog; __builtin_unreachable();
}
}
9 changes: 6 additions & 3 deletions sysdeps/managarm/generic/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length,

managarm::fs::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recv_resp.data(), recv_resp.length());
if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) {
if(resp.error() == managarm::fs::Errors::NOT_A_SOCKET) {
return ENOTSOCK;
}
__ensure(resp.error() == managarm::fs::Errors::SUCCESS);
Expand Down Expand Up @@ -234,7 +234,7 @@ int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length,

managarm::fs::SvrResponse<MemoryAllocator> resp(getSysdepsAllocator());
resp.ParseFromArray(recvResp.data(), recvResp.length());
if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) {
if(resp.error() == managarm::fs::Errors::NOT_A_SOCKET) {
return ENOTSOCK;
}else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) {
return ENOTCONN;
Expand Down Expand Up @@ -364,11 +364,14 @@ int sys_setsockopt(int fd, int layer, int number,
}else if(layer == IPPROTO_TCP && number == TCP_KEEPIDLE) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPIDLE is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_BROADCAST_ERROR) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_BROADCAST_ERROR is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_EXT_ACK) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_EXT_ACK is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == SOL_NETLINK && number == NETLINK_GET_STRICT_CHK) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_EXT_ACK is unimplemented\e[39m" << frg::endlog;
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_NETLINK and NETLINK_GET_STRICT_CHK is unimplemented\e[39m" << frg::endlog;
return 0;
}else if(layer == IPPROTO_TCP && number == TCP_KEEPINTVL) {
mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPINTVL is unimplemented\e[39m" << frg::endlog;
Expand Down
32 changes: 32 additions & 0 deletions sysdeps/managarm/include/mlibc/errors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef MLIBC_ERRORS
#define MLIBC_ERRORS

#include <mlibc/debug.hpp>

#include <posix.frigg_bragi.hpp>
#include <fs.frigg_bragi.hpp>

constexpr bool logErrors = false;

int posixErrorToError(managarm::posix::Errors posixError);
int fsErrorToError(managarm::fs::Errors posixError);

#define POSIX_SUCCESS_OR_RETURN_ERROR(error) \
if(int __error = posixErrorToError(error) != 0) { \
if(logErrors) { \
mlibc::infoLogger() << "mlibc: posix returned error " << (int)error << " (c error " << __error << ")\n" \
" In file " __FILE__ " on line " HEL_STRINGIFY(__LINE__) "\n"; \
} \
return __error; \
}

#define FS_SUCCESS_OR_RETURN_ERROR(error) \
if(int __error = fsErrorToError(error) != 0) { \
if(logErrors) { \
mlibc::infoLogger() << "mlibc: fs protocol returned error " << (int)error << " (c error " << __error << ")\n" \
" In file " __FILE__ " on line " HEL_STRINGIFY(__LINE__) "\n"; \
} \
return __error; \
}

#endif
1 change: 1 addition & 0 deletions sysdeps/managarm/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ libc_sources += files(
'generic/drm.cpp',
'generic/ensure.cpp',
'generic/entry.cpp',
'generic/errors.cpp',
'generic/file.cpp',
'generic/fork-exec.cpp',
'generic/ioctl.cpp',
Expand Down

0 comments on commit 9cf0b15

Please sign in to comment.