diff --git a/sysdeps/managarm/generic/errors.cpp b/sysdeps/managarm/generic/errors.cpp new file mode 100644 index 0000000000..fcafc18fc3 --- /dev/null +++ b/sysdeps/managarm/generic/errors.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include +#include + +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(); + } +} diff --git a/sysdeps/managarm/generic/socket.cpp b/sysdeps/managarm/generic/socket.cpp index 86a9373b4b..51781b8f87 100644 --- a/sysdeps/managarm/generic/socket.cpp +++ b/sysdeps/managarm/generic/socket.cpp @@ -191,7 +191,7 @@ int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, managarm::fs::SvrResponse 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); @@ -234,7 +234,7 @@ int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, managarm::fs::SvrResponse 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; @@ -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; diff --git a/sysdeps/managarm/include/mlibc/errors.hpp b/sysdeps/managarm/include/mlibc/errors.hpp new file mode 100644 index 0000000000..3b93205027 --- /dev/null +++ b/sysdeps/managarm/include/mlibc/errors.hpp @@ -0,0 +1,32 @@ +#ifndef MLIBC_ERRORS +#define MLIBC_ERRORS + +#include + +#include +#include + +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 diff --git a/sysdeps/managarm/meson.build b/sysdeps/managarm/meson.build index 34da8d77c5..584b244d31 100644 --- a/sysdeps/managarm/meson.build +++ b/sysdeps/managarm/meson.build @@ -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',