From fcf0b69aefeed3ce4f697aa01457ae5b9244969f Mon Sep 17 00:00:00 2001 From: Matt Staveley-Taylor Date: Wed, 13 Nov 2024 23:39:47 +0100 Subject: [PATCH] meta: add clang-format and reformat managarm sysdeps --- .clang-format | 34 + .github/workflows/pre-commit.yml | 21 + .pre-commit-config.yaml | 17 + README.md | 4 + sysdeps/aero/.clang-format | 2 + sysdeps/astral/.clang-format | 2 + sysdeps/dripos/.clang-format | 2 + sysdeps/ironclad/.clang-format | 2 + sysdeps/keyronex/.clang-format | 2 + sysdeps/lemon/.clang-format | 2 + sysdeps/lyre/.clang-format | 2 + sysdeps/managarm/aarch64/thread.cpp | 39 +- sysdeps/managarm/generic/drm.cpp | 2219 +++++++++-------- sysdeps/managarm/generic/ensure.cpp | 47 +- sysdeps/managarm/generic/entry.cpp | 68 +- sysdeps/managarm/generic/file.cpp | 1344 +++++----- sysdeps/managarm/generic/fork-exec.cpp | 355 ++- sysdeps/managarm/generic/ioctl.cpp | 1243 ++++----- sysdeps/managarm/generic/memory.cpp | 7 +- sysdeps/managarm/generic/mount.cpp | 29 +- sysdeps/managarm/generic/net.cpp | 12 +- sysdeps/managarm/generic/sched.cpp | 55 +- sysdeps/managarm/generic/signals.cpp | 86 +- sysdeps/managarm/generic/socket.cpp | 431 ++-- sysdeps/managarm/generic/time.cpp | 32 +- sysdeps/managarm/include/mlibc/posix-pipe.hpp | 130 +- .../managarm/include/mlibc/thread-entry.hpp | 2 +- sysdeps/managarm/riscv64/thread.cpp | 8 +- sysdeps/managarm/rtld-generic/support.cpp | 113 +- sysdeps/managarm/x86_64/thread.cpp | 39 +- sysdeps/vinix/.clang-format | 2 + 31 files changed, 3292 insertions(+), 3059 deletions(-) create mode 100644 .clang-format create mode 100644 .github/workflows/pre-commit.yml create mode 100644 .pre-commit-config.yaml create mode 100644 sysdeps/aero/.clang-format create mode 100644 sysdeps/astral/.clang-format create mode 100644 sysdeps/dripos/.clang-format create mode 100644 sysdeps/ironclad/.clang-format create mode 100644 sysdeps/keyronex/.clang-format create mode 100644 sysdeps/lemon/.clang-format create mode 100644 sysdeps/lyre/.clang-format create mode 100644 sysdeps/vinix/.clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..f218b5925f --- /dev/null +++ b/.clang-format @@ -0,0 +1,34 @@ +--- +BasedOnStyle: LLVM +Language: Cpp + +ColumnLimit: 100 +IndentWidth: 4 +TabWidth: 4 +UseTab: ForIndentation + +ContinuationIndentWidth: 4 +ConstructorInitializerIndentWidth: 0 +IndentCaseLabels: true +# Do not indent "public", "private", etc. +AccessModifierOffset: -4 + +AlignAfterOpenBracket: BlockIndent + +# Put template<> on its own line. +BreakTemplateDeclarations: Yes +# Put long return types on their own line. +BreakAfterReturnType: Automatic + +# Put all initializers onto their own lines. +PackConstructorInitializers: CurrentLine + +# Put all function arguments on their own lines. +BinPackArguments: false +BinPackParameters: false + +# Format braces as {1, 2, 3}, not as { 1, 2, 3 }. +Cpp11BracedListStyle: true + +# Force \n at EOF. +InsertNewlineAtEOF: true diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000000..d8acac2eac --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,21 @@ +name: pre-commit + +on: [merge_group, pull_request] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + # Check .clang-format is up to date + - uses: actions/checkout@v4 + with: + repository: 'managarm/common-config' + path: common-config + - run: 'cmp .clang-format common-config/.clang-format' + name: 'Check .clang-format is up to date' + + # Check formatting + - uses: actions/setup-python@v3 + - uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..f370cea005 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,17 @@ +fail_fast: false +repos: + - repo: https://github.com/pocc/pre-commit-hooks + rev: 336fdd7c3cab698ead0b1c95157b9e74d3906b62 + hooks: + - id: clang-format + additional_dependencies: [clang-format==19.1.3] + args: ["-i"] + exclude: > + (?x)^( + options/ansi/musl-generic-math/.*| + options/ansi/generic/complex/.*| + options/posix/musl-generic-regex/.* + ) + # Only enable on Managarm sysdeps for now. + files: + sysdeps/managarm/ diff --git a/README.md b/README.md index 12db9505bc..c459f9fa03 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,10 @@ The type of library to be built (static, shared, or both) is controlled by meson We also support building with `-Db_sanitize=undefined` to use UBSan inside mlibc. Note that this does not enable UBSan for external applications which link against `libc.so`, but it can be useful during development to detect internal bugs (e.g when adding new sysdeps). +## Running pre-commit hooks + +To format your code before submitting a PR, you should install [`pre-commit`](https://pre-commit.com/). Then do `pre-commit install` to install the git hook which runs each time you commit. Alternatively, you can do `pre-commit run -a` to manually format all files. + ## Running Tests The `mlibc` test suite can be run under a Linux host. To do this, first install a set of kernel headers (as described [here](https://docs.kernel.org/kbuild/headers_install.html)), then run from the project root: diff --git a/sysdeps/aero/.clang-format b/sysdeps/aero/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/aero/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/astral/.clang-format b/sysdeps/astral/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/astral/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/dripos/.clang-format b/sysdeps/dripos/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/dripos/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/ironclad/.clang-format b/sysdeps/ironclad/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/ironclad/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/keyronex/.clang-format b/sysdeps/keyronex/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/keyronex/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/lemon/.clang-format b/sysdeps/lemon/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/lemon/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/lyre/.clang-format b/sysdeps/lyre/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/lyre/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true diff --git a/sysdeps/managarm/aarch64/thread.cpp b/sysdeps/managarm/aarch64/thread.cpp index 88f1bf3588..129267d3e7 100644 --- a/sysdeps/managarm/aarch64/thread.cpp +++ b/sysdeps/managarm/aarch64/thread.cpp @@ -1,18 +1,18 @@ -#include -#include -#include #include -#include #include -#include +#include +#include +#include #include +#include +#include extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) { // Wait until our parent sets up the TID. - while(!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED)) + while (!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED)) mlibc::sys_futex_wait(&tcb->tid, 0, nullptr); - if(mlibc::sys_tcb_set(tcb)) + if (mlibc::sys_tcb_set(tcb)) __ensure(!"sys_tcb_set() failed"); tcb->invokeThreadFunc(entry, user_arg); @@ -29,7 +29,15 @@ namespace mlibc { static constexpr size_t default_stacksize = 0x200000; -int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) { +int sys_prepare_stack( + void **stack, + void *entry, + void *user_arg, + void *tcb, + size_t *stack_size, + size_t *guard_size, + void **stack_base +) { if (!*stack_size) *stack_size = default_stacksize; *guard_size = 0; @@ -37,20 +45,21 @@ int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size if (*stack) { *stack_base = *stack; } else { - *stack_base = mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if(*stack_base == MAP_FAILED) { + *stack_base = + mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (*stack_base == MAP_FAILED) { return errno; } } - - uintptr_t *sp = reinterpret_cast(reinterpret_cast(*stack_base) + *stack_size); + + uintptr_t *sp = + reinterpret_cast(reinterpret_cast(*stack_base) + *stack_size); *--sp = reinterpret_cast(tcb); *--sp = reinterpret_cast(user_arg); *--sp = reinterpret_cast(entry); - *stack = reinterpret_cast(sp); + *stack = reinterpret_cast(sp); return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/drm.cpp b/sysdeps/managarm/generic/drm.cpp index 805c366b7b..ab24ae185e 100644 --- a/sysdeps/managarm/generic/drm.cpp +++ b/sysdeps/managarm/generic/drm.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include @@ -14,1163 +14,1234 @@ namespace mlibc { int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle handle) { managarm::fs::IoctlRequest ioctl_req(getSysdepsAllocator()); - switch(request) { - case DRM_IOCTL_VERSION: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->version_major = resp.drm_version_major(); - param->version_minor = resp.drm_version_minor(); - param->version_patchlevel = resp.drm_version_patchlevel(); - - if(param->name) - memcpy(param->name, resp.drm_driver_name().data(), frg::min(param->name_len, - resp.drm_driver_name().size())); - if(param->date) - memcpy(param->date, resp.drm_driver_date().data(), frg::min(param->date_len, - resp.drm_driver_date().size())); - if(param->desc) - memcpy(param->desc, resp.drm_driver_desc().data(), frg::min(param->desc_len, - resp.drm_driver_desc().size())); - - param->name_len = resp.drm_driver_name().size(); - param->date_len = resp.drm_driver_date().size(); - param->desc_len = resp.drm_driver_desc().size(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_GET_CAP: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_capability(param->capability); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { - return EINVAL; - }else{ + switch (request) { + case DRM_IOCTL_VERSION: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->value = resp.drm_value(); + param->version_major = resp.drm_version_major(); + param->version_minor = resp.drm_version_minor(); + param->version_patchlevel = resp.drm_version_patchlevel(); + + if (param->name) + memcpy( + param->name, + resp.drm_driver_name().data(), + frg::min(param->name_len, resp.drm_driver_name().size()) + ); + if (param->date) + memcpy( + param->date, + resp.drm_driver_date().data(), + frg::min(param->date_len, resp.drm_driver_date().size()) + ); + if (param->desc) + memcpy( + param->desc, + resp.drm_driver_desc().data(), + frg::min(param->desc_len, resp.drm_driver_desc().size()) + ); + + param->name_len = resp.drm_driver_name().size(); + param->date_len = resp.drm_driver_date().size(); + param->desc_len = resp.drm_driver_desc().size(); + *result = resp.result(); return 0; } - } - case DRM_IOCTL_SET_CLIENT_CAP: { - auto param = reinterpret_cast(arg); - mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_SET_CLIENT_CAP(" << param->capability << ") ignores its value\e[39m" << frg::endlog; - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_capability(param->capability); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { - return EINVAL; - }else{ + case DRM_IOCTL_GET_CAP: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_capability(param->capability); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } else { + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->value = resp.drm_value(); + *result = resp.result(); + return 0; + } + } + case DRM_IOCTL_SET_CLIENT_CAP: { + auto param = reinterpret_cast(arg); + mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_SET_CLIENT_CAP(" << param->capability + << ") ignores its value\e[39m" << frg::endlog; + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_capability(param->capability); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } else { + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->value = resp.drm_value(); + *result = resp.result(); + return 0; + } + } + case DRM_IOCTL_GET_MAGIC: { + auto param = reinterpret_cast(arg); + mlibc::infoLogger( + ) << "\e[31mmlibc: DRM_IOCTL_GET_MAGIC is not implemented correctly\e[39m" + << frg::endlog; + param->magic = 1; + *result = 0; + return 0; + } + case DRM_IOCTL_AUTH_MAGIC: { + mlibc::infoLogger( + ) << "\e[31mmlibc: DRM_IOCTL_AUTH_MAGIC is not implemented correctly\e[39m" + << frg::endlog; + *result = 0; + return 0; + } + case DRM_IOCTL_SET_MASTER: { + mlibc::infoLogger( + ) << "\e[31mmlibc: DRM_IOCTL_SET_MASTER is not implemented correctly\e[39m" + << frg::endlog; + *result = 0; + return 0; + } + case DRM_IOCTL_DROP_MASTER: { + mlibc::infoLogger( + ) << "\e[31mmlibc: DRM_IOCTL_DROP_MASTER is not implemented correctly\e[39m" + << frg::endlog; + *result = 0; + return 0; + } + case DRM_IOCTL_MODE_GETRESOURCES: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + if (recv_resp.error() == kHelErrDismissed) { + return EINVAL; + } + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->value = resp.drm_value(); + for (size_t i = 0; i < resp.drm_fb_ids_size(); i++) { + if (i >= param->count_fbs) + break; + auto dest = reinterpret_cast(param->fb_id_ptr); + dest[i] = resp.drm_fb_ids(i); + } + param->count_fbs = resp.drm_fb_ids_size(); + + for (size_t i = 0; i < resp.drm_crtc_ids_size(); i++) { + if (i >= param->count_crtcs) + break; + auto dest = reinterpret_cast(param->crtc_id_ptr); + dest[i] = resp.drm_crtc_ids(i); + } + param->count_crtcs = resp.drm_crtc_ids_size(); + + for (size_t i = 0; i < resp.drm_connector_ids_size(); i++) { + if (i >= param->count_connectors) + break; + auto dest = reinterpret_cast(param->connector_id_ptr); + dest[i] = resp.drm_connector_ids(i); + } + param->count_connectors = resp.drm_connector_ids_size(); + + for (size_t i = 0; i < resp.drm_encoder_ids_size(); i++) { + if (i >= param->count_encoders) + continue; + auto dest = reinterpret_cast(param->encoder_id_ptr); + dest[i] = resp.drm_encoder_ids(i); + } + param->count_encoders = resp.drm_encoder_ids_size(); + + param->min_width = resp.drm_min_width(); + param->max_width = resp.drm_max_width(); + param->min_height = resp.drm_min_height(); + param->max_height = resp.drm_max_height(); + *result = resp.result(); return 0; } - } - case DRM_IOCTL_GET_MAGIC: { - auto param = reinterpret_cast(arg); - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_GET_MAGIC is not implemented correctly\e[39m" - << frg::endlog; - param->magic = 1; - *result = 0; - return 0; - } - case DRM_IOCTL_AUTH_MAGIC: { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_AUTH_MAGIC is not implemented correctly\e[39m" - << frg::endlog; - *result = 0; - return 0; - } - case DRM_IOCTL_SET_MASTER: { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_SET_MASTER is not implemented correctly\e[39m" - << frg::endlog; - *result = 0; - return 0; - } - case DRM_IOCTL_DROP_MASTER: { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_DROP_MASTER is not implemented correctly\e[39m" - << frg::endlog; - *result = 0; - return 0; - } - case DRM_IOCTL_MODE_GETRESOURCES: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - if(recv_resp.error() == kHelErrDismissed) { - return EINVAL; - } + case DRM_IOCTL_MODE_GETCONNECTOR: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_connector_id(param->connector_id); + req.set_drm_max_modes(param->count_modes); + + auto [offer, send_ioctl_req, send_req, recv_resp, recv_list] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer( + reinterpret_cast(param->modes_ptr), + param->count_modes * sizeof(drm_mode_modeinfo) + ) + ) + ); + + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + if (recv_resp.error() == kHelErrDismissed) + return EINVAL; + + HEL_CHECK(recv_resp.error()); + HEL_CHECK(recv_list.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + for (size_t i = 0; i < resp.drm_encoders_size(); i++) { + if (i >= param->count_encoders) + continue; + auto dest = reinterpret_cast(param->encoders_ptr); + dest[i] = resp.drm_encoders(i); + } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + param->encoder_id = resp.drm_encoder_id(); + param->connector_type = resp.drm_connector_type(); + param->connector_type_id = resp.drm_connector_type_id(); + param->connection = resp.drm_connection(); + param->mm_width = resp.drm_mm_width(); + param->mm_height = resp.drm_mm_height(); + param->subpixel = resp.drm_subpixel(); + param->pad = 0; + param->count_encoders = resp.drm_encoders_size(); + param->count_modes = resp.drm_num_modes(); + + if (param->props_ptr) { + auto id_ptr = reinterpret_cast(param->props_ptr); + auto val_ptr = reinterpret_cast(param->prop_values_ptr); + + for (size_t i = 0; i < frg::min( + static_cast(param->count_props), + resp.drm_obj_property_ids_size() + ); + i++) { + id_ptr[i] = resp.drm_obj_property_ids(i); + val_ptr[i] = resp.drm_obj_property_values(i); + } + } - for(size_t i = 0; i < resp.drm_fb_ids_size(); i++) { - if(i >= param->count_fbs) - break; - auto dest = reinterpret_cast(param->fb_id_ptr); - dest[i] = resp.drm_fb_ids(i); - } - param->count_fbs = resp.drm_fb_ids_size(); + param->count_props = resp.drm_obj_property_ids_size(); - for(size_t i = 0; i < resp.drm_crtc_ids_size(); i++) { - if(i >= param->count_crtcs) - break; - auto dest = reinterpret_cast(param->crtc_id_ptr); - dest[i] = resp.drm_crtc_ids(i); + *result = resp.result(); + return 0; } - param->count_crtcs = resp.drm_crtc_ids_size(); + case DRM_IOCTL_MODE_GETPROPERTY: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_property_id(param->prop_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() != managarm::fs::Errors::SUCCESS) { + mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_GETPROPERTY(" << param->prop_id + << ") error " << (int)resp.error() << "\e[39m" << frg::endlog; + *result = 0; + return EINVAL; + } + + memcpy(param->name, resp.drm_property_name().data(), resp.drm_property_name().size()); + param->count_values = resp.drm_property_vals_size(); + param->flags = resp.drm_property_flags(); + + for (size_t i = 0; + i < param->count_values && i < resp.drm_property_vals_size() && param->values_ptr; + i++) { + auto dest = reinterpret_cast(param->values_ptr); + dest[i] = resp.drm_property_vals(i); + } + + __ensure(resp.drm_enum_name_size() == resp.drm_enum_value_size()); + + for (size_t i = 0; i < param->count_enum_blobs && i < resp.drm_enum_name_size() && + i < resp.drm_enum_value_size(); + i++) { + auto dest = reinterpret_cast(param->enum_blob_ptr); + dest[i].value = resp.drm_enum_value(i); + strncpy(dest[i].name, resp.drm_enum_name(i).data(), DRM_PROP_NAME_LEN); + } - for(size_t i = 0; i < resp.drm_connector_ids_size(); i++) { - if(i >= param->count_connectors) - break; - auto dest = reinterpret_cast(param->connector_id_ptr); - dest[i] = resp.drm_connector_ids(i); + param->count_enum_blobs = resp.drm_enum_name_size(); + + *result = 0; + return 0; } - param->count_connectors = resp.drm_connector_ids_size(); + case DRM_IOCTL_MODE_SETPROPERTY: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_property_id(param->prop_id); + req.set_drm_property_value(param->value); + req.set_drm_obj_id(param->connector_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() != managarm::fs::Errors::SUCCESS) { + mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_SETPROPERTY(" << param->prop_id + << ") error " << (int)resp.error() << "\e[39m" << frg::endlog; + *result = 0; + return EINVAL; + } - for(size_t i = 0; i < resp.drm_encoder_ids_size(); i++) { - if(i >= param->count_encoders) - continue; - auto dest = reinterpret_cast(param->encoder_id_ptr); - dest[i] = resp.drm_encoder_ids(i); + *result = resp.result(); + return 0; } - param->count_encoders = resp.drm_encoder_ids_size(); + case DRM_IOCTL_MODE_GETPROPBLOB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_blob_id(param->blob_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() != managarm::fs::Errors::SUCCESS) { + mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_GETPROPBLOB(" << param->blob_id + << ") error " << (int)resp.error() << "\e[39m" << frg::endlog; + *result = 0; + return EINVAL; + } - param->min_width = resp.drm_min_width(); - param->max_width = resp.drm_max_width(); - param->min_height = resp.drm_min_height(); - param->max_height = resp.drm_max_height(); + uint8_t *dest = reinterpret_cast(param->data); + for (size_t i = 0; i < resp.drm_property_blob_size(); i++) { + if (i >= param->length) { + continue; + } - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETCONNECTOR: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_connector_id(param->connector_id); - req.set_drm_max_modes(param->count_modes); - - auto [offer, send_ioctl_req, send_req, recv_resp, recv_list] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(reinterpret_cast(param->modes_ptr), param->count_modes * sizeof(drm_mode_modeinfo)) - ) - ); - - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - if(recv_resp.error() == kHelErrDismissed) - return EINVAL; + dest[i] = resp.drm_property_blob(i); + } + + param->length = resp.drm_property_blob_size(); + + *result = 0; + return 0; + } + case DRM_IOCTL_MODE_GETPLANE: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_plane_id(param->plane_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - HEL_CHECK(recv_resp.error()); - HEL_CHECK(recv_list.error()); + param->crtc_id = resp.drm_crtc_id(); + param->fb_id = resp.drm_fb_id(); + param->possible_crtcs = resp.drm_possible_crtcs(); + param->gamma_size = resp.drm_gamma_size(); + + // FIXME: this should be passed as a buffer with helix, but this has no bounded max + // size? + for (size_t i = 0; i < resp.drm_format_type_size(); i++) { + if (i >= param->count_format_types) { + break; + } + auto dest = reinterpret_cast(param->format_type_ptr); + dest[i] = resp.drm_format_type(i); + } - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + param->count_format_types = resp.drm_format_type_size(); - for(size_t i = 0; i < resp.drm_encoders_size(); i++) { - if(i >= param->count_encoders) - continue; - auto dest = reinterpret_cast(param->encoders_ptr); - dest[i] = resp.drm_encoders(i); + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_GETPLANERESOURCES: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + frg::string ser(getSysdepsAllocator()); + req.SerializeToString(&ser); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->encoder_id = resp.drm_encoder_id(); - param->connector_type = resp.drm_connector_type(); - param->connector_type_id = resp.drm_connector_type_id(); - param->connection = resp.drm_connection(); - param->mm_width = resp.drm_mm_width(); - param->mm_height = resp.drm_mm_height(); - param->subpixel = resp.drm_subpixel(); - param->pad = 0; - param->count_encoders = resp.drm_encoders_size(); - param->count_modes = resp.drm_num_modes(); - - if(param->props_ptr) { - auto id_ptr = reinterpret_cast(param->props_ptr); - auto val_ptr = reinterpret_cast(param->prop_values_ptr); - - for(size_t i = 0; i < frg::min(static_cast(param->count_props), resp.drm_obj_property_ids_size()); i++) { - id_ptr[i] = resp.drm_obj_property_ids(i); - val_ptr[i] = resp.drm_obj_property_values(i); + // FIXME: send this via a helix_ng buffer + for (size_t i = 0; i < resp.drm_plane_res_size(); i++) { + if (i >= param->count_planes) { + continue; + } + auto dest = reinterpret_cast(param->plane_id_ptr); + dest[i] = resp.drm_plane_res(i); } + + param->count_planes = resp.drm_plane_res_size(); + + *result = resp.result(); + + return 0; } + case DRM_IOCTL_MODE_GETENCODER: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_encoder_id(param->encoder_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->count_props = resp.drm_obj_property_ids_size(); + param->encoder_type = resp.drm_encoder_type(); + param->crtc_id = resp.drm_crtc_id(); + param->possible_crtcs = resp.drm_possible_crtcs(); + param->possible_clones = resp.drm_possible_clones(); - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETPROPERTY: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_property_id(param->prop_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() != managarm::fs::Errors::SUCCESS) { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_GETPROPERTY(" << param->prop_id << ") error " << (int) resp.error() << "\e[39m" - << frg::endlog; - *result = 0; - return EINVAL; + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_CREATE_DUMB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_width(param->width); + req.set_drm_height(param->height); + req.set_drm_bpp(param->bpp); + req.set_drm_flags(param->flags); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - memcpy(param->name, resp.drm_property_name().data(), resp.drm_property_name().size()); - param->count_values = resp.drm_property_vals_size(); - param->flags = resp.drm_property_flags(); + param->handle = resp.drm_handle(); + param->pitch = resp.drm_pitch(); + param->size = resp.drm_size(); - for(size_t i = 0; i < param->count_values && i < resp.drm_property_vals_size() && param->values_ptr; i++) { - auto dest = reinterpret_cast(param->values_ptr); - dest[i] = resp.drm_property_vals(i); + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_ADDFB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); - __ensure(resp.drm_enum_name_size() == resp.drm_enum_value_size()); + req.set_drm_width(param->width); + req.set_drm_height(param->height); + req.set_drm_pitch(param->pitch); + req.set_drm_bpp(param->bpp); + req.set_drm_depth(param->depth); + req.set_drm_handle(param->handle); - for(size_t i = 0; i < param->count_enum_blobs && i < resp.drm_enum_name_size() && i < resp.drm_enum_value_size(); i++) { - auto dest = reinterpret_cast(param->enum_blob_ptr); - dest[i].value = resp.drm_enum_value(i); - strncpy(dest[i].name, resp.drm_enum_name(i).data(), DRM_PROP_NAME_LEN); + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->fb_id = resp.drm_fb_id(); + + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_GETFB2: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(DRM_IOCTL_MODE_GETFB2); + req.set_drm_fb_id(param->fb_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->count_enum_blobs = resp.drm_enum_name_size(); + param->width = resp.drm_width(); + param->height = resp.drm_height(); + param->pixel_format = resp.pixel_format(); + param->modifier[0] = resp.modifier(); + memcpy( + param->handles, + resp.drm_handles().data(), + sizeof(uint32_t) * resp.drm_handles_size() + ); + memcpy( + param->pitches, + resp.drm_pitches().data(), + sizeof(uint32_t) * resp.drm_pitches_size() + ); + memcpy( + param->offsets, + resp.drm_offsets().data(), + sizeof(uint32_t) * resp.drm_offsets_size() + ); - *result = 0; - return 0; - } - case DRM_IOCTL_MODE_SETPROPERTY: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_property_id(param->prop_id); - req.set_drm_property_value(param->value); - req.set_drm_obj_id(param->connector_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() != managarm::fs::Errors::SUCCESS) { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_SETPROPERTY(" << param->prop_id << ") error " << (int) resp.error() << "\e[39m" - << frg::endlog; - *result = 0; - return EINVAL; + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_ADDFB2: { + auto param = reinterpret_cast(arg); - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETPROPBLOB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_blob_id(param->blob_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() != managarm::fs::Errors::SUCCESS) { - mlibc::infoLogger() << "\e[31mmlibc: DRM_IOCTL_MODE_GETPROPBLOB(" << param->blob_id << ") error " << (int) resp.error() << "\e[39m" - << frg::endlog; - *result = 0; - return EINVAL; + __ensure(!param->flags || param->flags == DRM_MODE_FB_MODIFIERS); + __ensure(!param->modifier[0] || param->modifier[0] == DRM_FORMAT_MOD_INVALID); + __ensure(!param->offsets[0]); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(DRM_IOCTL_MODE_ADDFB2); + + req.set_drm_width(param->width); + req.set_drm_height(param->height); + req.set_drm_pitch(param->pitches[0]); + req.set_drm_fourcc(param->pixel_format); + req.set_drm_handle(param->handles[0]); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->fb_id = resp.drm_fb_id(); + + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_RMFB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_fb_id(*param); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - uint8_t *dest = reinterpret_cast(param->data); - for(size_t i = 0; i < resp.drm_property_blob_size(); i++) { - if(i >= param->length) { - continue; - } + *result = resp.result(); + return 0; + } + case DRM_IOCTL_MODE_MAP_DUMB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_handle(param->handle); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->offset = resp.drm_offset(); - dest[i] = resp.drm_property_blob(i); + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_GETCRTC: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_crtc_id(param->crtc_id); + + auto [offer, send_ioctl_req, send_req, recv_resp, recv_data] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(¶m->mode, sizeof(drm_mode_modeinfo)) + ) + ); + + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + HEL_CHECK(recv_data.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->length = resp.drm_property_blob_size(); + param->fb_id = resp.drm_fb_id(); + param->x = resp.drm_x(); + param->y = resp.drm_y(); + param->gamma_size = resp.drm_gamma_size(); + param->mode_valid = resp.drm_mode_valid(); - *result = 0; - return 0; - } - case DRM_IOCTL_MODE_GETPLANE: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_plane_id(param->plane_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->crtc_id = resp.drm_crtc_id(); - param->fb_id = resp.drm_fb_id(); - param->possible_crtcs = resp.drm_possible_crtcs(); - param->gamma_size = resp.drm_gamma_size(); - - // FIXME: this should be passed as a buffer with helix, but this has no bounded max size? - for(size_t i = 0; i < resp.drm_format_type_size(); i++) { - if(i >= param->count_format_types) { - break; - } - auto dest = reinterpret_cast(param->format_type_ptr); - dest[i] = resp.drm_format_type(i); + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_SETCRTC: { + auto param = reinterpret_cast(arg); - param->count_format_types = resp.drm_format_type_size(); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETPLANERESOURCES: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - frg::string ser(getSysdepsAllocator()); - req.SerializeToString(&ser); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - // FIXME: send this via a helix_ng buffer - for(size_t i = 0; i < resp.drm_plane_res_size(); i++) { - if(i >= param->count_planes) { - continue; + for (size_t i = 0; i < param->count_connectors; i++) { + auto dest = reinterpret_cast(param->set_connectors_ptr); + req.add_drm_connector_ids(dest[i]); } - auto dest = reinterpret_cast(param->plane_id_ptr); - dest[i] = resp.drm_plane_res(i); + req.set_drm_x(param->x); + req.set_drm_y(param->y); + req.set_drm_crtc_id(param->crtc_id); + req.set_drm_fb_id(param->fb_id); + req.set_drm_mode_valid(param->mode_valid); + + auto [offer, send_ioctl_req, send_req, send_mode, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::sendBuffer(¶m->mode, sizeof(drm_mode_modeinfo)), + helix_ng::recvInline() + ) + ); + + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(send_mode.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_OBJ_GETPROPERTIES: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_count_props(param->count_props); + req.set_drm_obj_id(param->obj_id); + req.set_drm_obj_type(param->obj_type); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - param->count_planes = resp.drm_plane_res_size(); + auto props = reinterpret_cast(param->props_ptr); + auto prop_vals = reinterpret_cast(param->prop_values_ptr); - *result = resp.result(); + for (size_t i = 0; i < resp.drm_obj_property_ids_size(); i++) { + if (i >= param->count_props) { + break; + } + props[i] = resp.drm_obj_property_ids(i); + prop_vals[i] = resp.drm_obj_property_values(i); + } - return 0; - } - case DRM_IOCTL_MODE_GETENCODER: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_encoder_id(param->encoder_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->encoder_type = resp.drm_encoder_type(); - param->crtc_id = resp.drm_crtc_id(); - param->possible_crtcs = resp.drm_possible_crtcs(); - param->possible_clones = resp.drm_possible_clones(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_CREATE_DUMB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_width(param->width); - req.set_drm_height(param->height); - req.set_drm_bpp(param->bpp); - req.set_drm_flags(param->flags); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->handle = resp.drm_handle(); - param->pitch = resp.drm_pitch(); - param->size = resp.drm_size(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_ADDFB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_width(param->width); - req.set_drm_height(param->height); - req.set_drm_pitch(param->pitch); - req.set_drm_bpp(param->bpp); - req.set_drm_depth(param->depth); - req.set_drm_handle(param->handle); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->fb_id = resp.drm_fb_id(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETFB2: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(DRM_IOCTL_MODE_GETFB2); - req.set_drm_fb_id(param->fb_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->width = resp.drm_width(); - param->height = resp.drm_height(); - param->pixel_format = resp.pixel_format(); - param->modifier[0] = resp.modifier(); - memcpy(param->handles, resp.drm_handles().data(), sizeof(uint32_t) * resp.drm_handles_size()); - memcpy(param->pitches, resp.drm_pitches().data(), sizeof(uint32_t) * resp.drm_pitches_size()); - memcpy(param->offsets, resp.drm_offsets().data(), sizeof(uint32_t) * resp.drm_offsets_size()); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_ADDFB2: { - auto param = reinterpret_cast(arg); - - __ensure(!param->flags || param->flags == DRM_MODE_FB_MODIFIERS); - __ensure(!param->modifier[0] || param->modifier[0] == DRM_FORMAT_MOD_INVALID); - __ensure(!param->offsets[0]); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(DRM_IOCTL_MODE_ADDFB2); - - req.set_drm_width(param->width); - req.set_drm_height(param->height); - req.set_drm_pitch(param->pitches[0]); - req.set_drm_fourcc(param->pixel_format); - req.set_drm_handle(param->handles[0]); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->fb_id = resp.drm_fb_id(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_RMFB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_fb_id(*param); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_MAP_DUMB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_handle(param->handle); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->offset = resp.drm_offset(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_GETCRTC: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_crtc_id(param->crtc_id); - - auto [offer, send_ioctl_req, send_req, recv_resp, recv_data] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(¶m->mode, sizeof(drm_mode_modeinfo))) - ); - - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - HEL_CHECK(recv_data.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->fb_id = resp.drm_fb_id(); - param->x = resp.drm_x(); - param->y = resp.drm_y(); - param->gamma_size = resp.drm_gamma_size(); - param->mode_valid = resp.drm_mode_valid(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_SETCRTC: { - auto param = reinterpret_cast(arg); + param->count_props = resp.drm_obj_property_ids_size(); - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); + *result = resp.result(); + return 0; + } + case DRM_IOCTL_MODE_PAGE_FLIP: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + __ensure(!(param->flags & ~DRM_MODE_PAGE_FLIP_EVENT)); + req.set_drm_crtc_id(param->crtc_id); + req.set_drm_fb_id(param->fb_id); + req.set_drm_cookie(param->user_data); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - for(size_t i = 0; i < param->count_connectors; i++) { - auto dest = reinterpret_cast(param->set_connectors_ptr); - req.add_drm_connector_ids(dest[i]); + *result = resp.result(); + return 0; } - req.set_drm_x(param->x); - req.set_drm_y(param->y); - req.set_drm_crtc_id(param->crtc_id); - req.set_drm_fb_id(param->fb_id); - req.set_drm_mode_valid(param->mode_valid); - - auto [offer, send_ioctl_req, send_req, send_mode, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::sendBuffer(¶m->mode, sizeof(drm_mode_modeinfo)), - helix_ng::recvInline()) - ); - - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(send_mode.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_OBJ_GETPROPERTIES: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_count_props(param->count_props); - req.set_drm_obj_id(param->obj_id); - req.set_drm_obj_type(param->obj_type); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - auto props = reinterpret_cast(param->props_ptr); - auto prop_vals = reinterpret_cast(param->prop_values_ptr); - - for(size_t i = 0; i < resp.drm_obj_property_ids_size(); i++) { - if(i >= param->count_props) { - break; + case DRM_IOCTL_MODE_DIRTYFB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_fb_id(param->fb_id); + req.set_drm_flags(param->flags); + req.set_drm_color(param->color); + for (size_t i = 0; i < param->num_clips; i++) { + auto dest = reinterpret_cast(param->clips_ptr); + managarm::fs::Rect clip(getSysdepsAllocator()); + clip.set_x1(dest->x1); + clip.set_y1(dest->y1); + clip.set_x2(dest->x2); + clip.set_y2(dest->y2); + req.add_drm_clips(std::move(clip)); + } + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } else { + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + return 0; } - props[i] = resp.drm_obj_property_ids(i); - prop_vals[i] = resp.drm_obj_property_values(i); } + case DRM_IOCTL_MODE_CURSOR: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_flags(param->flags); + req.set_drm_crtc_id(param->crtc_id); + + if (param->flags == DRM_MODE_CURSOR_MOVE) { + req.set_drm_x(param->x); + req.set_drm_y(param->y); + } else if (param->flags == DRM_MODE_CURSOR_BO) { + req.set_drm_width(param->width); + req.set_drm_height(param->height); + req.set_drm_handle(param->handle); + } else { + mlibc::infoLogger() + << "\e[35mmlibc: invalid flags in DRM_IOCTL_MODE_CURSOR\e[39m" << frg::endlog; + return EINVAL; + } - param->count_props = resp.drm_obj_property_ids_size(); + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + if (resp.error() == managarm::fs::Errors::NO_BACKING_DEVICE) { + return ENXIO; + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } else { + *result = resp.result(); + return 0; + } + } + case DRM_IOCTL_MODE_DESTROY_DUMB: { + auto param = reinterpret_cast(arg); - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_PAGE_FLIP: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - __ensure(!(param->flags & ~DRM_MODE_PAGE_FLIP_EVENT)); - req.set_drm_crtc_id(param->crtc_id); - req.set_drm_fb_id(param->fb_id); - req.set_drm_cookie(param->user_data); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_DIRTYFB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_fb_id(param->fb_id); - req.set_drm_flags(param->flags); - req.set_drm_color(param->color); - for(size_t i = 0; i < param->num_clips; i++) { - auto dest = reinterpret_cast(param->clips_ptr); - managarm::fs::Rect clip(getSysdepsAllocator()); - clip.set_x1(dest->x1); - clip.set_y1(dest->y1); - clip.set_x2(dest->x2); - clip.set_y2(dest->y2); - req.add_drm_clips(std::move(clip)); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + req.set_drm_handle(param->handle); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + + *result = resp.result(); + return 0; } + case DRM_IOCTL_MODE_CREATEPROPBLOB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_blob_size(param->length); + + auto [offer, send_ioctl_req, send_req, blob_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::sendBuffer(reinterpret_cast(param->data), param->length), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(blob_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { - return EINVAL; - }else{ + param->blob_id = resp.drm_blob_id(); + + *result = resp.result(); + return 0; + } + case DRM_IOCTL_MODE_DESTROYPROPBLOB: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_blob_id(param->blob_id); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); return 0; } - } - case DRM_IOCTL_MODE_CURSOR: { - auto param = reinterpret_cast(arg); + case DRM_IOCTL_MODE_ATOMIC: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_flags(param->flags); + req.set_drm_cookie(param->user_data); + + size_t prop_count = 0; + auto objs_ptr = reinterpret_cast(param->objs_ptr); + auto count_props_ptr = reinterpret_cast(param->count_props_ptr); + auto props_ptr = reinterpret_cast(param->props_ptr); + auto prop_values_ptr = reinterpret_cast(param->prop_values_ptr); + + for (size_t i = 0; i < param->count_objs; i++) { + /* list of modeobjs and their property count */ + req.add_drm_obj_ids(objs_ptr[i]); + req.add_drm_prop_counts(count_props_ptr[i]); + prop_count += count_props_ptr[i]; + } - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); + for (size_t i = 0; i < prop_count; i++) { + /* array of property IDs */ + req.add_drm_props(props_ptr[i]); + /* array of property values */ + req.add_drm_prop_values(prop_values_ptr[i]); + } - req.set_drm_flags(param->flags); - req.set_drm_crtc_id(param->crtc_id); + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if (param->flags == DRM_MODE_CURSOR_MOVE) { - req.set_drm_x(param->x); - req.set_drm_y(param->y); - } else if (param->flags == DRM_MODE_CURSOR_BO) { - req.set_drm_width(param->width); - req.set_drm_height(param->height); - req.set_drm_handle(param->handle); - } else { - mlibc::infoLogger() << "\e[35mmlibc: invalid flags in DRM_IOCTL_MODE_CURSOR\e[39m" << frg::endlog; - return EINVAL; + *result = resp.result(); + return 0; } - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if (resp.error() == managarm::fs::Errors::NO_BACKING_DEVICE) { - return ENXIO; - }else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + case DRM_IOCTL_MODE_LIST_LESSEES: { + mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_LIST_LESSEES" + " is not implemented correctly\e[39m" + << frg::endlog; return EINVAL; - }else{ - *result = resp.result(); + } + case DRM_IOCTL_MODE_SETGAMMA: { + mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_SETGAMMA" + " is not implemented correctly\e[39m" + << frg::endlog; return 0; } - } - case DRM_IOCTL_MODE_DESTROY_DUMB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - req.set_drm_handle(param->handle); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_CREATEPROPBLOB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_blob_size(param->length); - - auto [offer, send_ioctl_req, send_req, blob_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::sendBuffer(reinterpret_cast(param->data), param->length), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(blob_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->blob_id = resp.drm_blob_id(); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_DESTROYPROPBLOB: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_blob_id(param->blob_id); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_ATOMIC: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_flags(param->flags); - req.set_drm_cookie(param->user_data); - - size_t prop_count = 0; - auto objs_ptr = reinterpret_cast(param->objs_ptr); - auto count_props_ptr = reinterpret_cast(param->count_props_ptr); - auto props_ptr = reinterpret_cast(param->props_ptr); - auto prop_values_ptr = reinterpret_cast(param->prop_values_ptr); - - for(size_t i = 0; i < param->count_objs; i++) { - /* list of modeobjs and their property count */ - req.add_drm_obj_ids(objs_ptr[i]); - req.add_drm_prop_counts(count_props_ptr[i]); - prop_count += count_props_ptr[i]; + case DRM_IOCTL_MODE_CREATE_LEASE: { + auto param = reinterpret_cast(arg); + + mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_CREATE_LEASE" + " is a noop\e[39m" + << frg::endlog; + param->lessee_id = 1; + param->fd = fd; + *result = 0; + return 0; } - - for(size_t i = 0; i < prop_count; i++) { - /* array of property IDs */ - req.add_drm_props(props_ptr[i]); - /* array of property values */ - req.add_drm_prop_values(prop_values_ptr[i]); + case DRM_IOCTL_GEM_CLOSE: { + mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_GEM_CLOSE" + " is a noop\e[39m" + << frg::endlog; + return 0; } - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - *result = resp.result(); - return 0; - } - case DRM_IOCTL_MODE_LIST_LESSEES: { - mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_LIST_LESSEES" - " is not implemented correctly\e[39m" << frg::endlog; - return EINVAL; - } - case DRM_IOCTL_MODE_SETGAMMA: { - mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_SETGAMMA" - " is not implemented correctly\e[39m" << frg::endlog; - return 0; - } - case DRM_IOCTL_MODE_CREATE_LEASE: { - auto param = reinterpret_cast(arg); - - mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_MODE_CREATE_LEASE" - " is a noop\e[39m" << frg::endlog; - param->lessee_id = 1; - param->fd = fd; - *result = 0; - return 0; - } - case DRM_IOCTL_GEM_CLOSE: { - mlibc::infoLogger() << "\e[35mmlibc: DRM_IOCTL_GEM_CLOSE" - " is a noop\e[39m" << frg::endlog; - return 0; - } - case DRM_IOCTL_PRIME_HANDLE_TO_FD: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_prime_handle(param->handle); - req.set_drm_flags(param->flags); - - auto [offer, send_ioctl_req, send_req, send_creds, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(send_creds.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - - param->fd = resp.drm_prime_fd(); - *result = resp.result(); - return 0; - } - case DRM_IOCTL_PRIME_FD_TO_HANDLE: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_drm_flags(param->flags); - - auto [offer, send_ioctl_req, send_req, send_creds, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(getHandleForFd(param->fd)), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(send_creds.error()); - HEL_CHECK(recv_resp.error()); - - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { - return EBADF; - } else { + case DRM_IOCTL_PRIME_HANDLE_TO_FD: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_prime_handle(param->handle); + req.set_drm_flags(param->flags); + + auto [offer, send_ioctl_req, send_req, send_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(send_creds.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + + param->fd = resp.drm_prime_fd(); + *result = resp.result(); + return 0; } + case DRM_IOCTL_PRIME_FD_TO_HANDLE: { + auto param = reinterpret_cast(arg); + + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_drm_flags(param->flags); + + auto [offer, send_ioctl_req, send_req, send_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(getHandleForFd(param->fd)), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(send_creds.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { + return EBADF; + } else { + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + } - param->handle = resp.drm_prime_handle(); - *result = resp.result(); - return 0; - } + param->handle = resp.drm_prime_handle(); + *result = resp.result(); + return 0; + } } mlibc::infoLogger() << "mlibc: Unexpected DRM ioctl with" - << ", number: 0x" << frg::hex_fmt(_IOC_NR(request)) - << " (raw request: " << frg::hex_fmt(request) << ")" << frg::endlog; + << ", number: 0x" << frg::hex_fmt(_IOC_NR(request)) + << " (raw request: " << frg::hex_fmt(request) << ")" << frg::endlog; __ensure(!"Illegal ioctl request"); __builtin_unreachable(); } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/ensure.cpp b/sysdeps/managarm/generic/ensure.cpp index c9ba35d411..79316e0290 100644 --- a/sysdeps/managarm/generic/ensure.cpp +++ b/sysdeps/managarm/generic/ensure.cpp @@ -1,38 +1,37 @@ +#include #include #include -#include #include -#include #include +#include -#include #include +#include -void __frigg_assert_fail(const char *assertion, const char *file, unsigned int line, - const char *function) { - mlibc::panicLogger() << "In function " << function - << ", file " << file << ":" << line << "\n" - << "__ensure(" << assertion << ") failed" << frg::endlog; +void __frigg_assert_fail( + const char *assertion, const char *file, unsigned int line, const char *function +) { + mlibc::panicLogger() << "In function " << function << ", file " << file << ":" << line << "\n" + << "__ensure(" << assertion << ") failed" << frg::endlog; } namespace mlibc { - void sys_libc_log(const char *message) { - // This implementation is inherently signal-safe. - size_t n = 0; - while(message[n]) - n++; - HEL_CHECK(helLog(kHelLogSeverityInfo, message, n)); - } - - void sys_libc_panic() { - // This implementation is inherently signal-safe. - const char *message = "mlibc: Panic!"; - size_t n = 0; - while(message[n]) - n++; - helPanic(message, n); - } +void sys_libc_log(const char *message) { + // This implementation is inherently signal-safe. + size_t n = 0; + while (message[n]) + n++; + HEL_CHECK(helLog(kHelLogSeverityInfo, message, n)); } +void sys_libc_panic() { + // This implementation is inherently signal-safe. + const char *message = "mlibc: Panic!"; + size_t n = 0; + while (message[n]) + n++; + helPanic(message, n); +} +} // namespace mlibc diff --git a/sysdeps/managarm/generic/entry.cpp b/sysdeps/managarm/generic/entry.cpp index 3ff28d2cb3..6fdda27fa1 100644 --- a/sysdeps/managarm/generic/entry.cpp +++ b/sysdeps/managarm/generic/entry.cpp @@ -5,11 +5,11 @@ #include #include +#include #include #include -#include -#include #include +#include #include #include @@ -28,49 +28,51 @@ thread_local HelHandle __mlibc_posix_lane; thread_local void *__mlibc_clk_tracker_page; namespace { - thread_local unsigned __mlibc_gsf_nesting; - thread_local void *__mlibc_cached_thread_page; - thread_local HelHandle *cachedFileTable; - - // This construction is a bit weird: Even though the variables above - // are thread_local we still protect their initialization with a pthread_once_t - // (instead of using a C++ constructor). - // We do this in order to able to clear the pthread_once_t after a fork. - thread_local pthread_once_t has_cached_infos = PTHREAD_ONCE_INIT; - - void actuallyCacheInfos() { - posix::ManagarmProcessData data; - HEL_CHECK(helSyscall1(kHelCallSuper + posix::superGetProcessData, reinterpret_cast(&data))); - - __mlibc_posix_lane = data.posixLane; - __mlibc_cached_thread_page = data.threadPage; - cachedFileTable = data.fileTable; - __mlibc_clk_tracker_page = data.clockTrackerPage; - } +thread_local unsigned __mlibc_gsf_nesting; +thread_local void *__mlibc_cached_thread_page; +thread_local HelHandle *cachedFileTable; + +// This construction is a bit weird: Even though the variables above +// are thread_local we still protect their initialization with a pthread_once_t +// (instead of using a C++ constructor). +// We do this in order to able to clear the pthread_once_t after a fork. +thread_local pthread_once_t has_cached_infos = PTHREAD_ONCE_INIT; + +void actuallyCacheInfos() { + posix::ManagarmProcessData data; + HEL_CHECK( + helSyscall1(kHelCallSuper + posix::superGetProcessData, reinterpret_cast(&data)) + ); + + __mlibc_posix_lane = data.posixLane; + __mlibc_cached_thread_page = data.threadPage; + cachedFileTable = data.fileTable; + __mlibc_clk_tracker_page = data.clockTrackerPage; } +} // namespace SignalGuard::SignalGuard() { pthread_once(&has_cached_infos, &actuallyCacheInfos); - if(!__mlibc_cached_thread_page) + if (!__mlibc_cached_thread_page) return; auto p = reinterpret_cast(__mlibc_cached_thread_page); - if(!__mlibc_gsf_nesting) + if (!__mlibc_gsf_nesting) __atomic_store_n(p, 1, __ATOMIC_RELAXED); __mlibc_gsf_nesting++; } SignalGuard::~SignalGuard() { pthread_once(&has_cached_infos, &actuallyCacheInfos); - if(!__mlibc_cached_thread_page) + if (!__mlibc_cached_thread_page) return; auto p = reinterpret_cast(__mlibc_cached_thread_page); __ensure(__mlibc_gsf_nesting > 0); __mlibc_gsf_nesting--; - if(!__mlibc_gsf_nesting) { + if (!__mlibc_gsf_nesting) { unsigned int result = __atomic_exchange_n(p, 0, __ATOMIC_RELAXED); - if(result == 2) { + if (result == 2) { HEL_CHECK(helSyscall0(kHelCallSuper + posix::superSigRaise)); - }else{ + } else { __ensure(result == 1); } } @@ -103,9 +105,7 @@ HelHandle getHandleForFd(int fd) { return cacheFileTable()[fd]; } -void clearCachedInfos() { - has_cached_infos = PTHREAD_ONCE_INIT; -} +void clearCachedInfos() { has_cached_infos = PTHREAD_ONCE_INIT; } struct LibraryGuard { LibraryGuard(); @@ -121,11 +121,13 @@ LibraryGuard::LibraryGuard() { // Parse the exec() stack. mlibc::parse_exec_stack(__dlapi_entrystack(), &__mlibc_stack_data); - mlibc::set_startup_data(__mlibc_stack_data.argc, __mlibc_stack_data.argv, - __mlibc_stack_data.envp); + mlibc::set_startup_data( + __mlibc_stack_data.argc, __mlibc_stack_data.argv, __mlibc_stack_data.envp + ); } -extern "C" void __mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) { +extern "C" void +__mlibc_entry(uintptr_t *entry_stack, int (*main_fn)(int argc, char *argv[], char *env[])) { __dlapi_enter(entry_stack); auto result = main_fn(__mlibc_stack_data.argc, __mlibc_stack_data.argv, environ); exit(result); diff --git a/sysdeps/managarm/generic/file.cpp b/sysdeps/managarm/generic/file.cpp index c2bf08a7df..e7b2754f47 100644 --- a/sysdeps/managarm/generic/file.cpp +++ b/sysdeps/managarm/generic/file.cpp @@ -32,11 +32,11 @@ int sys_chdir(const char *path) { req.set_request_type(managarm::posix::CntReqType::CHDIR); req.set_path(frg::string(getSysdepsAllocator(), path)); - auto [offer, send_req, recv_resp] = exchangeMsgsSync(getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_req, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -45,9 +45,9 @@ int sys_chdir(const char *path) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -61,10 +61,10 @@ int sys_fchdir(int fd) { req.set_fd(fd); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -85,10 +85,10 @@ int sys_chroot(const char *path) { req.set_path(frg::string(getSysdepsAllocator(), path)); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -101,9 +101,7 @@ int sys_chroot(const char *path) { return 0; } -int sys_mkdir(const char *path, mode_t mode) { - return sys_mkdirat(AT_FDCWD, path, mode); -} +int sys_mkdir(const char *path, mode_t mode) { return sys_mkdirat(AT_FDCWD, path, mode); } int sys_mkdirat(int dirfd, const char *path, mode_t mode) { (void)mode; @@ -113,14 +111,12 @@ int sys_mkdirat(int dirfd, const char *path, mode_t mode) { req.set_fd(dirfd); req.set_path(frg::string(getSysdepsAllocator(), path)); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -129,17 +125,17 @@ int sys_mkdirat(int dirfd, const char *path, mode_t mode) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - } else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + } else if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -157,14 +153,12 @@ int sys_symlinkat(const char *target_path, int dirfd, const char *link_path) { req.set_path(frg::string(getSysdepsAllocator(), link_path)); req.set_target_path(frg::string(getSysdepsAllocator(), target_path)); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -173,17 +167,17 @@ int sys_symlinkat(const char *target_path, int dirfd, const char *link_path) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -203,14 +197,12 @@ int sys_linkat(int olddirfd, const char *old_path, int newdirfd, const char *new req.set_newfd(newdirfd); req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -219,13 +211,13 @@ int sys_linkat(int olddirfd, const char *old_path, int newdirfd, const char *new managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -244,14 +236,12 @@ int sys_renameat(int olddirfd, const char *old_path, int newdirfd, const char *n req.set_fd(olddirfd); req.set_newfd(newdirfd); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -260,42 +250,42 @@ int sys_renameat(int olddirfd, const char *old_path, int newdirfd, const char *n managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } } -} //namespace mlibc +} // namespace mlibc namespace mlibc { int sys_fcntl(int fd, int request, va_list args, int *result) { SignalGuard sguard; - if(request == F_DUPFD) { + if (request == F_DUPFD) { int newfd; - if(int e = sys_dup(fd, 0, &newfd); e) + if (int e = sys_dup(fd, 0, &newfd); e) return e; *result = newfd; return 0; - }else if(request == F_DUPFD_CLOEXEC) { + } else if (request == F_DUPFD_CLOEXEC) { int newfd; - if(int e = sys_dup(fd, O_CLOEXEC, &newfd); e) + if (int e = sys_dup(fd, O_CLOEXEC, &newfd); e) return e; *result = newfd; return 0; - }else if(request == F_GETFD) { + } else if (request == F_GETFD) { managarm::posix::CntRequest req(getSysdepsAllocator()); req.set_request_type(managarm::posix::CntReqType::FD_GET_FLAGS); req.set_fd(fd); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -303,22 +293,22 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) + if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) return EBADF; __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *result = resp.flags(); return 0; - }else if(request == F_SETFD) { + } else if (request == F_SETFD) { managarm::posix::CntRequest req(getSysdepsAllocator()); req.set_request_type(managarm::posix::CntReqType::FD_SET_FLAGS); req.set_fd(fd); req.set_flags(va_arg(args, int)); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -326,14 +316,14 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) + if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) return EBADF; - else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) + else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) return EINVAL; __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *result = static_cast(resp.error()); return 0; - }else if(request == F_GETFL) { + } else if (request == F_GETFL) { SignalGuard sguard; auto handle = getHandleForFd(fd); @@ -345,10 +335,10 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { req.set_fd(fd); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -356,14 +346,15 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { - mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_GETFL) unimplemented for this file\e[39m" << frg::endlog; + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_GETFL) unimplemented for this file\e[39m" + << frg::endlog; return EINVAL; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = resp.flags(); return 0; - }else if(request == F_SETFL) { + } else if (request == F_SETFL) { SignalGuard sguard; auto handle = getHandleForFd(fd); @@ -376,10 +367,10 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { req.set_flags(va_arg(args, int)); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -387,25 +378,26 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { - mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_SETFL) unimplemented for this file\e[39m" << frg::endlog; + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_SETFL) unimplemented for this file\e[39m" + << frg::endlog; return EINVAL; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = 0; return 0; - }else if(request == F_SETLK) { + } else if (request == F_SETLK) { mlibc::infoLogger() << "\e[31mmlibc: F_SETLK\e[39m" << frg::endlog; return 0; - }else if(request == F_SETLKW) { + } else if (request == F_SETLKW) { mlibc::infoLogger() << "\e[31mmlibc: F_SETLKW\e[39m" << frg::endlog; return 0; - }else if(request == F_GETLK) { + } else if (request == F_GETLK) { struct flock *lock = va_arg(args, struct flock *); lock->l_type = F_UNLCK; mlibc::infoLogger() << "\e[31mmlibc: F_GETLK is stubbed!\e[39m" << frg::endlog; return 0; - }else if(request == F_ADD_SEALS) { + } else if (request == F_ADD_SEALS) { auto seals = va_arg(args, int); auto handle = getHandleForFd(fd); if (!handle) @@ -416,11 +408,12 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { req.set_fd(fd); req.set_seals(seals); - auto [offer, send_req, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::RecvInline() - )); + auto [offer, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::RecvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -428,17 +421,19 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { - mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_ADD_SEALS) unimplemented for this file\e[39m" << frg::endlog; + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + mlibc::infoLogger( + ) << "\e[31mmlibc: fcntl(F_ADD_SEALS) unimplemented for this file\e[39m" + << frg::endlog; return EINVAL; - } else if(resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { + } else if (resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { return EPERM; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = resp.seals(); return 0; - }else if(request == F_GET_SEALS) { + } else if (request == F_GET_SEALS) { auto handle = getHandleForFd(fd); if (!handle) return EBADF; @@ -447,11 +442,12 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { req.set_req_type(managarm::fs::CntReqType::PT_GET_SEALS); req.set_fd(fd); - auto [offer, send_req, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::RecvInline() - )); + auto [offer, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::RecvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -459,23 +455,23 @@ int sys_fcntl(int fd, int request, va_list args, int *result) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { - mlibc::infoLogger() << "\e[31mmlibc: fcntl(F_GET_SEALS) unimplemented for this file\e[39m" << frg::endlog; + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + mlibc::infoLogger( + ) << "\e[31mmlibc: fcntl(F_GET_SEALS) unimplemented for this file\e[39m" + << frg::endlog; return EINVAL; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = resp.seals(); return 0; - }else{ - mlibc::infoLogger() << "\e[31mmlibc: Unexpected fcntl() request: " - << request << "\e[39m" << frg::endlog; + } else { + mlibc::infoLogger() << "\e[31mmlibc: Unexpected fcntl() request: " << request << "\e[39m" + << frg::endlog; return EINVAL; } } -int sys_open_dir(const char *path, int *handle) { - return sys_open(path, 0, 0, handle); -} +int sys_open_dir(const char *path, int *handle) { return sys_open(path, 0, 0, handle); } int sys_read_entries(int fd, void *buffer, size_t max_size, size_t *bytes_read) { SignalGuard sguard; @@ -487,10 +483,10 @@ int sys_read_entries(int fd, void *buffer, size_t max_size, size_t *bytes_read) req.set_req_type(managarm::fs::CntReqType::PT_READ_ENTRIES); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -498,10 +494,10 @@ int sys_read_entries(int fd, void *buffer, size_t max_size, size_t *bytes_read) managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::END_OF_FILE) { + if (resp.error() == managarm::fs::Errors::END_OF_FILE) { *bytes_read = 0; return 0; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); __ensure(max_size > sizeof(struct dirent)); auto ent = new (buffer) struct dirent; @@ -521,10 +517,10 @@ int sys_ttyname(int fd, char *buf, size_t size) { req.set_fd(fd); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -533,11 +529,11 @@ int sys_ttyname(int fd, char *buf, size_t size) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::NOT_A_TTY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_TTY) { return ENOTTY; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); __ensure(size >= resp.path().size() + 1); memcpy(buf, resp.path().data(), size); @@ -547,8 +543,7 @@ int sys_ttyname(int fd, char *buf, size_t size) { } int sys_fdatasync(int) { - mlibc::infoLogger() << "\e[35mmlibc: fdatasync() is a no-op\e[39m" - << frg::endlog; + mlibc::infoLogger() << "\e[35mmlibc: fdatasync() is a no-op\e[39m" << frg::endlog; return 0; } @@ -560,11 +555,12 @@ int sys_getcwd(char *buffer, size_t size) { req.set_size(size); auto [offer, send_req, recv_resp, recv_path] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(buffer, size)) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(buffer, size) + ) ); HEL_CHECK(offer.error()); @@ -575,7 +571,7 @@ int sys_getcwd(char *buffer, size_t size) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); __ensure(resp.error() == managarm::posix::Errors::SUCCESS); - if(static_cast(resp.size()) >= size) + if (static_cast(resp.size()) >= size) return ERANGE; return 0; } @@ -592,11 +588,10 @@ int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offse req.set_rel_offset(offset); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -605,13 +600,13 @@ int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offse managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - }else if(resp.error() == managarm::posix::Errors::NO_MEMORY) { + } else if (resp.error() == managarm::posix::Errors::NO_MEMORY) { return EFAULT; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else { + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *window = reinterpret_cast(resp.offset()); } @@ -629,10 +624,10 @@ int sys_vm_remap(void *pointer, size_t size, size_t new_size, void **window) { req.set_new_size(new_size); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -656,10 +651,10 @@ int sys_vm_protect(void *pointer, size_t size, int prot) { req.set_mode(prot); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -681,10 +676,10 @@ int sys_vm_unmap(void *pointer, size_t size) { req.set_size(size); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -704,10 +699,10 @@ int sys_setsid(pid_t *sid) { req.set_request_type(managarm::posix::CntReqType::SETSID); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -716,7 +711,7 @@ int sys_setsid(pid_t *sid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { *sid = -1; return EPERM; } @@ -727,16 +722,16 @@ int sys_setsid(pid_t *sid) { int sys_tcgetattr(int fd, struct termios *attr) { int result; - if(int e = sys_ioctl(fd, TCGETS, attr, &result); e) + if (int e = sys_ioctl(fd, TCGETS, attr, &result); e) return e; return 0; } int sys_tcsetattr(int fd, int when, const struct termios *attr) { - if(when < TCSANOW || when > TCSAFLUSH) + if (when < TCSANOW || when > TCSAFLUSH) return EINVAL; - if(int e = sys_ioctl(fd, TCSETS, const_cast(attr), nullptr); e) + if (int e = sys_ioctl(fd, TCSETS, const_cast(attr), nullptr); e) return e; return 0; } @@ -760,11 +755,10 @@ int sys_socket(int domain, int type_and_flags, int proto, int *fd) { req.set_flags(type_and_flags & flags_mask); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -773,7 +767,7 @@ int sys_socket(int domain, int type_and_flags, int proto, int *fd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EAFNOSUPPORT; } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -790,10 +784,10 @@ int sys_pipe(int *fds, int flags) { req.set_flags(flags); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -823,11 +817,10 @@ int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) { req.set_flags(type_and_flags & flags_mask); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -836,7 +829,7 @@ int sys_socketpair(int domain, int type_and_flags, int proto, int *fds) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::PROTOCOL_NOT_SUPPORTED) { + if (resp.error() == managarm::posix::Errors::PROTOCOL_NOT_SUPPORTED) { return EPROTONOSUPPORT; } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -854,10 +847,10 @@ int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *lengt return EBADF; size_t overall_size = 0; - for(size_t i = 0; i < hdr->msg_iovlen; i++) { + for (size_t i = 0; i < hdr->msg_iovlen; i++) { HelSgItem item{ - .buffer = hdr->msg_iov[i].iov_base, - .length = hdr->msg_iov[i].iov_len, + .buffer = hdr->msg_iov[i].iov_base, + .length = hdr->msg_iov[i].iov_len, }; sglist.push_back(item); overall_size += hdr->msg_iov[i].iov_len; @@ -871,10 +864,10 @@ int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *lengt req.set_has_cmsg_creds(false); req.set_has_cmsg_rights(false); - for(auto cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) { + for (auto cmsg = CMSG_FIRSTHDR(hdr); cmsg; cmsg = CMSG_NXTHDR(hdr, cmsg)) { __ensure(cmsg->cmsg_level == SOL_SOCKET); __ensure(cmsg->cmsg_len >= sizeof(struct cmsghdr)); - if(cmsg->cmsg_type == SCM_CREDENTIALS) { + if (cmsg->cmsg_type == SCM_CREDENTIALS) { req.set_has_cmsg_creds(true); size_t size = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); __ensure(size == sizeof(struct ucred)); @@ -883,30 +876,34 @@ int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *lengt req.set_creds_pid(creds.pid); req.set_creds_uid(creds.uid); req.set_creds_gid(creds.gid); - } else if(cmsg->cmsg_type == SCM_RIGHTS) { + } else if (cmsg->cmsg_type == SCM_RIGHTS) { req.set_has_cmsg_rights(true); size_t size = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr)); __ensure(!(size % sizeof(int))); - for(size_t off = 0; off < size; off += sizeof(int)) { + for (size_t off = 0; off < size; off += sizeof(int)) { int fd; memcpy(&fd, CMSG_DATA(cmsg) + off, sizeof(int)); req.add_fds(fd); } } else { - mlibc::infoLogger() << "mlibc: sys_msg_send only supports SCM_RIGHTS or SCM_CREDENTIALS, got: " << cmsg->cmsg_type << "!" << frg::endlog; + mlibc::infoLogger( + ) << "mlibc: sys_msg_send only supports SCM_RIGHTS or SCM_CREDENTIALS, got: " + << cmsg->cmsg_type << "!" << frg::endlog; return EINVAL; } } - auto [offer, send_head, send_tail, send_data, imbue_creds, send_addr, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::sendBufferSg(sglist.data(), hdr->msg_iovlen), - helix_ng::imbueCredentials(), - helix_ng::sendBuffer(hdr->msg_name, hdr->msg_namelen), - helix_ng::recvInline()) - ); + auto [offer, send_head, send_tail, send_data, imbue_creds, send_addr, recv_resp] = + exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), + helix_ng::sendBufferSg(sglist.data(), hdr->msg_iovlen), + helix_ng::imbueCredentials(), + helix_ng::sendBuffer(hdr->msg_name, hdr->msg_namelen), + helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -919,31 +916,31 @@ int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *lengt managarm::fs::SendMsgReply resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::BROKEN_PIPE) { + if (resp.error() == managarm::fs::Errors::BROKEN_PIPE) { return EPIPE; - }else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + } else if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { return ENOTCONN; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::HOST_UNREACHABLE) { + } else if (resp.error() == managarm::fs::Errors::HOST_UNREACHABLE) { return EHOSTUNREACH; - }else if(resp.error() == managarm::fs::Errors::ACCESS_DENIED) { + } else if (resp.error() == managarm::fs::Errors::ACCESS_DENIED) { return EACCES; - }else if(resp.error() == managarm::fs::Errors::NETWORK_UNREACHABLE) { + } else if (resp.error() == managarm::fs::Errors::NETWORK_UNREACHABLE) { return ENETUNREACH; - }else if(resp.error() == managarm::fs::Errors::DESTINATION_ADDRESS_REQUIRED) { + } else if (resp.error() == managarm::fs::Errors::DESTINATION_ADDRESS_REQUIRED) { return EDESTADDRREQ; - }else if(resp.error() == managarm::fs::Errors::ADDRESS_NOT_AVAILABLE) { + } else if (resp.error() == managarm::fs::Errors::ADDRESS_NOT_AVAILABLE) { return EADDRNOTAVAIL; - }else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - }else if(resp.error() == managarm::fs::Errors::AF_NOT_SUPPORTED) { + } else if (resp.error() == managarm::fs::Errors::AF_NOT_SUPPORTED) { return EAFNOSUPPORT; - }else if(resp.error() == managarm::fs::Errors::MESSAGE_TOO_LARGE) { + } else if (resp.error() == managarm::fs::Errors::MESSAGE_TOO_LARGE) { return EMSGSIZE; - }else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { return EOPNOTSUPP; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *length = resp.size(); return 0; @@ -951,7 +948,7 @@ int sys_msg_send(int sockfd, const struct msghdr *hdr, int flags, ssize_t *lengt } int sys_msg_recv(int sockfd, struct msghdr *hdr, int flags, ssize_t *length) { - if(!hdr->msg_iovlen) { + if (!hdr->msg_iovlen) { return EMSGSIZE; } @@ -967,16 +964,18 @@ int sys_msg_recv(int sockfd, struct msghdr *hdr, int flags, ssize_t *length) { req.set_addr_size(hdr->msg_namelen); req.set_ctrl_size(hdr->msg_controllen); - auto [offer, send_req, imbue_creds, recv_resp, recv_addr, recv_data, recv_ctrl] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::recvInline(), - helix_ng::recvBuffer(hdr->msg_name, hdr->msg_namelen), - helix_ng::recvBuffer(hdr->msg_iov[0].iov_base, hdr->msg_iov[0].iov_len), - helix_ng::recvBuffer(hdr->msg_control, hdr->msg_controllen)) - ); + auto [offer, send_req, imbue_creds, recv_resp, recv_addr, recv_data, recv_ctrl] = + exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::recvInline(), + helix_ng::recvBuffer(hdr->msg_name, hdr->msg_namelen), + helix_ng::recvBuffer(hdr->msg_iov[0].iov_base, hdr->msg_iov[0].iov_len), + helix_ng::recvBuffer(hdr->msg_control, hdr->msg_controllen) + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -986,9 +985,9 @@ int sys_msg_recv(int sockfd, struct msghdr *hdr, int flags, ssize_t *length) { managarm::fs::RecvMsgReply resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); HEL_CHECK(recv_addr.error()); HEL_CHECK(recv_data.error()); @@ -1002,37 +1001,44 @@ int sys_msg_recv(int sockfd, struct msghdr *hdr, int flags, ssize_t *length) { } } -int sys_pselect(int, fd_set *read_set, fd_set *write_set, - fd_set *except_set, const struct timespec *timeout, - const sigset_t *sigmask, int *num_events) { +int sys_pselect( + int, + fd_set *read_set, + fd_set *write_set, + fd_set *except_set, + const struct timespec *timeout, + const sigset_t *sigmask, + int *num_events +) { // TODO: Do not keep errors from epoll (?). int fd = epoll_create1(0); - if(fd == -1) + if (fd == -1) return -1; - for(int k = 0; k < FD_SETSIZE; k++) { + for (int k = 0; k < FD_SETSIZE; k++) { struct epoll_event ev; memset(&ev, 0, sizeof(struct epoll_event)); - if(read_set && FD_ISSET(k, read_set)) + if (read_set && FD_ISSET(k, read_set)) ev.events |= EPOLLIN; // TODO: Additional events. - if(write_set && FD_ISSET(k, write_set)) + if (write_set && FD_ISSET(k, write_set)) ev.events |= EPOLLOUT; // TODO: Additional events. - if(except_set && FD_ISSET(k, except_set)) + if (except_set && FD_ISSET(k, except_set)) ev.events |= EPOLLPRI; - if(!ev.events) + if (!ev.events) continue; ev.data.u32 = k; - if(epoll_ctl(fd, EPOLL_CTL_ADD, k, &ev)) + if (epoll_ctl(fd, EPOLL_CTL_ADD, k, &ev)) return -1; } struct epoll_event evnts[16]; - int n = epoll_pwait(fd, evnts, 16, - timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 100) : -1, sigmask); - if(n == -1) + int n = epoll_pwait( + fd, evnts, 16, timeout ? (timeout->tv_sec * 1000 + timeout->tv_nsec / 100) : -1, sigmask + ); + if (n == -1) return -1; fd_set res_read_set; @@ -1043,36 +1049,35 @@ int sys_pselect(int, fd_set *read_set, fd_set *write_set, FD_ZERO(&res_except_set); int m = 0; - for(int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { int k = evnts[i].data.u32; - if(read_set && FD_ISSET(k, read_set) - && evnts[i].events & (EPOLLIN | EPOLLERR | EPOLLHUP)) { + if (read_set && FD_ISSET(k, read_set) && + evnts[i].events & (EPOLLIN | EPOLLERR | EPOLLHUP)) { FD_SET(k, &res_read_set); m++; } - if(write_set && FD_ISSET(k, write_set) - && evnts[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) { + if (write_set && FD_ISSET(k, write_set) && + evnts[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) { FD_SET(k, &res_write_set); m++; } - if(except_set && FD_ISSET(k, except_set) - && evnts[i].events & EPOLLPRI) { + if (except_set && FD_ISSET(k, except_set) && evnts[i].events & EPOLLPRI) { FD_SET(k, &res_except_set); m++; } } - if(close(fd)) + if (close(fd)) __ensure("close() failed on epoll file"); - if(read_set) + if (read_set) memcpy(read_set, &res_read_set, sizeof(fd_set)); - if(write_set) + if (write_set) memcpy(write_set, &res_write_set, sizeof(fd_set)); - if(except_set) + if (except_set) memcpy(except_set, &res_except_set, sizeof(fd_set)); *num_events = m; @@ -1088,16 +1093,16 @@ int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) { req.set_request_type(managarm::posix::CntReqType::EPOLL_CALL); req.set_timeout(timeout > 0 ? int64_t{timeout} * 1000000 : timeout); - for(nfds_t i = 0; i < count; i++) { + for (nfds_t i = 0; i < count; i++) { req.add_fds(fds[i].fd); req.add_events(fds[i].events); } auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1106,15 +1111,15 @@ int sys_poll(struct pollfd *fds, nfds_t count, int timeout, int *num_events) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); __ensure(resp.events_size() == count); int m = 0; - for(nfds_t i = 0; i < count; i++) { - if(resp.events(i)) + for (nfds_t i = 0; i < count; i++) { + if (resp.events(i)) m++; fds[i].revents = resp.events(i); } @@ -1132,7 +1137,7 @@ int sys_epoll_create(int flags, int *fd) { SignalGuard sguard; uint32_t proto_flags = 0; - if(flags & EPOLL_CLOEXEC || flags & O_CLOEXEC) + if (flags & EPOLL_CLOEXEC || flags & O_CLOEXEC) proto_flags |= managarm::posix::OpenFlags::OF_CLOEXEC; managarm::posix::CntRequest req(getSysdepsAllocator()); @@ -1140,10 +1145,10 @@ int sys_epoll_create(int flags, int *fd) { req.set_flags(proto_flags); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1161,29 +1166,29 @@ int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) { SignalGuard sguard; managarm::posix::CntRequest req(getSysdepsAllocator()); - if(mode == EPOLL_CTL_ADD) { + if (mode == EPOLL_CTL_ADD) { __ensure(ev); req.set_request_type(managarm::posix::CntReqType::EPOLL_ADD); req.set_flags(ev->events); req.set_cookie(ev->data.u64); - }else if(mode == EPOLL_CTL_MOD) { + } else if (mode == EPOLL_CTL_MOD) { __ensure(ev); req.set_request_type(managarm::posix::CntReqType::EPOLL_MODIFY); req.set_flags(ev->events); req.set_cookie(ev->data.u64); - }else if(mode == EPOLL_CTL_DEL) { + } else if (mode == EPOLL_CTL_DEL) { req.set_request_type(managarm::posix::CntReqType::EPOLL_DELETE); - }else{ + } else { mlibc::panicLogger() << "\e[31mmlibc: Illegal epoll_ctl() mode\e[39m" << frg::endlog; } req.set_fd(epfd); req.set_newfd(fd); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1192,11 +1197,11 @@ int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::BAD_FD) { + if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - } else if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - } else if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + } else if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -1205,8 +1210,9 @@ int sys_epoll_ctl(int epfd, int mode, int fd, struct epoll_event *ev) { return 0; } -int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, - int timeout, const sigset_t *sigmask, int *raised) { +int sys_epoll_pwait( + int epfd, struct epoll_event *ev, int n, int timeout, const sigset_t *sigmask, int *raised +) { __ensure(timeout >= 0 || timeout == -1); // TODO: Report errors correctly. SignalGuard sguard; @@ -1216,7 +1222,7 @@ int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, req.set_fd(epfd); req.set_size(n); req.set_timeout(timeout > 0 ? int64_t{timeout} * 1000000 : timeout); - if(sigmask != NULL) { + if (sigmask != NULL) { req.set_sigmask(*reinterpret_cast(sigmask)); req.set_sigmask_needed(true); } else { @@ -1224,11 +1230,12 @@ int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, } auto [offer, send_req, recv_resp, recv_data] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(ev, n * sizeof(struct epoll_event))) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(ev, n * sizeof(struct epoll_event)) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -1237,7 +1244,7 @@ int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::BAD_FD) { + if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; } __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -1247,7 +1254,7 @@ int sys_epoll_pwait(int epfd, struct epoll_event *ev, int n, } int sys_timerfd_create(int clockid, int flags, int *fd) { - (void) clockid; + (void)clockid; SignalGuard sguard; managarm::posix::CntRequest req(getSysdepsAllocator()); @@ -1255,10 +1262,10 @@ int sys_timerfd_create(int clockid, int flags, int *fd) { req.set_flags(flags); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1272,8 +1279,7 @@ int sys_timerfd_create(int clockid, int flags, int *fd) { return 0; } -int sys_timerfd_settime(int fd, int, - const struct itimerspec *value, struct itimerspec *oldvalue) { +int sys_timerfd_settime(int fd, int, const struct itimerspec *value, struct itimerspec *oldvalue) { __ensure(!oldvalue); SignalGuard sguard; @@ -1286,10 +1292,10 @@ int sys_timerfd_settime(int fd, int, req.set_interval_nanos(value->it_interval.tv_nsec); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1302,13 +1308,13 @@ int sys_timerfd_settime(int fd, int, return 0; } -int sys_signalfd_create(const sigset_t *masks, int flags, int *fd) { +int sys_signalfd_create(const sigset_t *masks, int flags, int *fd) { __ensure(!(flags & ~(SFD_CLOEXEC | SFD_NONBLOCK))); uint32_t proto_flags = 0; - if(flags & SFD_CLOEXEC) + if (flags & SFD_CLOEXEC) proto_flags |= managarm::posix::OpenFlags::OF_CLOEXEC; - if(flags & SFD_NONBLOCK) + if (flags & SFD_NONBLOCK) proto_flags |= managarm::posix::OpenFlags::OF_NONBLOCK; SignalGuard sguard; @@ -1319,10 +1325,10 @@ int sys_signalfd_create(const sigset_t *masks, int flags, int *fd) { req.set_sigset(*reinterpret_cast(masks)); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1337,8 +1343,10 @@ int sys_signalfd_create(const sigset_t *masks, int flags, int *fd) { } int sys_reboot(int command) { - if(command != RB_POWER_OFF && command != RB_AUTOBOOT) { - mlibc::infoLogger() << "mlibc: Anything other than power off or reboot is not supported yet!" << frg::endlog; + if (command != RB_POWER_OFF && command != RB_AUTOBOOT) { + mlibc::infoLogger( + ) << "mlibc: Anything other than power off or reboot is not supported yet!" + << frg::endlog; return EINVAL; } @@ -1348,11 +1356,10 @@ int sys_reboot(int command) { req.set_cmd(command); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1361,7 +1368,7 @@ int sys_reboot(int command) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::INSUFFICIENT_PERMISSION) + if (resp.error() == managarm::posix::Errors::INSUFFICIENT_PERMISSION) return EPERM; __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; @@ -1373,20 +1380,19 @@ int sys_inotify_create(int flags, int *fd) { SignalGuard sguard; uint32_t proto_flags = 0; - if(flags & IN_CLOEXEC) + if (flags & IN_CLOEXEC) proto_flags |= managarm::posix::OpenFlags::OF_CLOEXEC; - if(flags & IN_NONBLOCK) + if (flags & IN_NONBLOCK) proto_flags |= managarm::posix::OpenFlags::OF_NONBLOCK; managarm::posix::InotifyCreateRequest req(getSysdepsAllocator()); req.set_flags(proto_flags); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1408,14 +1414,12 @@ int sys_inotify_add_watch(int ifd, const char *path, uint32_t mask, int *wd) { req.set_path(frg::string(getSysdepsAllocator(), path)); req.set_flags(mask); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -1424,11 +1428,11 @@ int sys_inotify_add_watch(int ifd, const char *path, uint32_t mask, int *wd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *wd = resp.wd(); return 0; @@ -1439,20 +1443,22 @@ int sys_eventfd_create(unsigned int initval, int flags, int *fd) { SignalGuard sguard; uint32_t proto_flags = 0; - if (flags & EFD_NONBLOCK) proto_flags |= managarm::posix::EventFdFlags::NONBLOCK; - if (flags & EFD_CLOEXEC) proto_flags |= managarm::posix::EventFdFlags::CLOEXEC; - if (flags & EFD_SEMAPHORE) proto_flags |= managarm::posix::EventFdFlags::SEMAPHORE; + if (flags & EFD_NONBLOCK) + proto_flags |= managarm::posix::EventFdFlags::NONBLOCK; + if (flags & EFD_CLOEXEC) + proto_flags |= managarm::posix::EventFdFlags::CLOEXEC; + if (flags & EFD_SEMAPHORE) + proto_flags |= managarm::posix::EventFdFlags::SEMAPHORE; managarm::posix::EventfdCreateRequest req(getSysdepsAllocator()); req.set_flags(proto_flags); req.set_initval(initval); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1474,33 +1480,33 @@ int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) { SignalGuard sguard; // We do not support O_TMPFILE. - if(flags & O_TMPFILE) + if (flags & O_TMPFILE) return EOPNOTSUPP; uint32_t proto_flags = 0; - if(flags & O_APPEND) + if (flags & O_APPEND) proto_flags |= managarm::posix::OpenFlags::OF_APPEND; - if(flags & O_CREAT) + if (flags & O_CREAT) proto_flags |= managarm::posix::OpenFlags::OF_CREATE; - if(flags & O_EXCL) + if (flags & O_EXCL) proto_flags |= managarm::posix::OpenFlags::OF_EXCLUSIVE; - if(flags & O_NONBLOCK) + if (flags & O_NONBLOCK) proto_flags |= managarm::posix::OpenFlags::OF_NONBLOCK; - if(flags & O_TRUNC) + if (flags & O_TRUNC) proto_flags |= managarm::posix::OpenFlags::OF_TRUNC; - if(flags & O_CLOEXEC) + if (flags & O_CLOEXEC) proto_flags |= managarm::posix::OpenFlags::OF_CLOEXEC; - if(flags & O_NOCTTY) + if (flags & O_NOCTTY) proto_flags |= managarm::posix::OpenFlags::OF_NOCTTY; - if(flags & O_PATH) + if (flags & O_PATH) proto_flags |= managarm::posix::OpenFlags::OF_PATH; - else if((flags & O_ACCMODE) == O_RDONLY) + else if ((flags & O_ACCMODE) == O_RDONLY) proto_flags |= managarm::posix::OpenFlags::OF_RDONLY; - else if((flags & O_ACCMODE) == O_WRONLY) + else if ((flags & O_ACCMODE) == O_WRONLY) proto_flags |= managarm::posix::OpenFlags::OF_WRONLY; - else if((flags & O_ACCMODE) == O_RDWR) + else if ((flags & O_ACCMODE) == O_RDWR) proto_flags |= managarm::posix::OpenFlags::OF_RDWR; managarm::posix::OpenAtRequest req(getSysdepsAllocator()); @@ -1510,11 +1516,10 @@ int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) { req.set_mode(mode); auto [offer, sendHead, sendTail, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1524,22 +1529,23 @@ int sys_openat(int dirfd, const char *path, int flags, mode_t mode, int *fd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_OPERATION_TARGET) { - mlibc::infoLogger() << "\e[31mmlibc: openat unimplemented for this file " << path << "\e[39m" << frg::endlog; + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_OPERATION_TARGET) { + mlibc::infoLogger() << "\e[31mmlibc: openat unimplemented for this file " << path + << "\e[39m" << frg::endlog; return EINVAL; - }else if(resp.error() == managarm::posix::Errors::NO_BACKING_DEVICE) { + } else if (resp.error() == managarm::posix::Errors::NO_BACKING_DEVICE) { return ENXIO; - }else if(resp.error() == managarm::posix::Errors::IS_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::IS_DIRECTORY) { return EISDIR; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *fd = resp.fd(); return 0; @@ -1554,14 +1560,12 @@ int sys_mkfifoat(int dirfd, const char *path, mode_t mode) { req.set_path(frg::string(getSysdepsAllocator(), path)); req.set_mode(mode); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -1570,17 +1574,17 @@ int sys_mkfifoat(int dirfd, const char *path, mode_t mode) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::INTERNAL_ERROR) { + } else if (resp.error() == managarm::posix::Errors::INTERNAL_ERROR) { return EIEIO; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -1595,14 +1599,12 @@ int sys_mknodat(int dirfd, const char *path, int mode, int dev) { req.set_mode(mode); req.set_device(dev); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -1611,15 +1613,15 @@ int sys_mknodat(int dirfd, const char *path, int mode, int dev) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::posix::Errors::ALREADY_EXISTS) { return EEXIST; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -1640,16 +1642,15 @@ int sys_read(int fd, void *data, size_t max_size, ssize_t *bytes_read) { frg::string ser(getSysdepsAllocator()); req.SerializeToString(&ser); - auto [offer, send_req, imbue_creds, recv_resp, recv_data] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::imbueCredentials(), - helix_ng::recvInline(), - helix_ng::recvBuffer(data, max_size) - ) - ); + auto [offer, send_req, imbue_creds, recv_resp, recv_data] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::imbueCredentials(), + helix_ng::recvInline(), + helix_ng::recvBuffer(data, max_size) + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -1658,21 +1659,21 @@ int sys_read(int fd, void *data, size_t max_size, ssize_t *bytes_read) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); -/* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { - return EBADF; - }else*/ - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + /* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { + return EBADF; + }else*/ + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::IS_DIRECTORY) { + } else if (resp.error() == managarm::fs::Errors::IS_DIRECTORY) { return EISDIR; - }else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + } else if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { return ENOTCONN; - }else if(resp.error() == managarm::fs::Errors::END_OF_FILE) { + } else if (resp.error() == managarm::fs::Errors::END_OF_FILE) { *bytes_read = 0; return 0; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); HEL_CHECK(recv_data.error()); *bytes_read = recv_data.actualLength(); @@ -1681,12 +1682,12 @@ int sys_read(int fd, void *data, size_t max_size, ssize_t *bytes_read) { } int sys_readv(int fd, const struct iovec *iovs, int iovc, ssize_t *bytes_read) { - for(int i = 0; i < iovc; i++) { + for (int i = 0; i < iovc; i++) { ssize_t intermed = 0; - if(int e = sys_read(fd, iovs[i].iov_base, iovs[i].iov_len, &intermed); e) + if (int e = sys_read(fd, iovs[i].iov_base, iovs[i].iov_len, &intermed); e) return e; - else if(intermed == 0) + else if (intermed == 0) break; *bytes_read += intermed; @@ -1708,12 +1709,13 @@ int sys_write(int fd, const void *data, size_t size, ssize_t *bytes_written) { req.set_size(size); auto [offer, send_req, imbue_creds, send_data, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::sendBuffer(data, size), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::sendBuffer(data, size), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -1725,19 +1727,20 @@ int sys_write(int fd, const void *data, size_t size, ssize_t *bytes_written) { resp.ParseFromArray(recv_resp.data(), recv_resp.length()); // TODO: implement NO_SUCH_FD -/* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { - return EBADF; - }else*/ if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + /* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { + return EBADF; + }else*/ + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { return EINVAL; // FD does not support writes. - }else if(resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { + } else if (resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { return ENOSPC; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + } else if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { return ENOTCONN; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - if(bytes_written) { + if (bytes_written) { *bytes_written = resp.size(); } return 0; @@ -1748,10 +1751,10 @@ int sys_writev(int fd, const struct iovec *iovs, int iovc, ssize_t *bytes_writte frg::small_vector sglist{getSysdepsAllocator()}; size_t overall_size = 0; - for(int i = 0; i < iovc; i++) { + for (int i = 0; i < iovc; i++) { HelSgItem item{ - .buffer = iovs[i].iov_base, - .length = iovs[i].iov_len, + .buffer = iovs[i].iov_base, + .length = iovs[i].iov_len, }; sglist.push_back(item); overall_size += iovs[i].iov_len; @@ -1769,12 +1772,13 @@ int sys_writev(int fd, const struct iovec *iovs, int iovc, ssize_t *bytes_writte req.set_size(overall_size); auto [offer, send_req, imbue_creds, send_data, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::sendBufferSg(sglist.data(), iovc), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::sendBufferSg(sglist.data(), iovc), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -1785,17 +1789,17 @@ int sys_writev(int fd, const struct iovec *iovs, int iovc, ssize_t *bytes_writte 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::ILLEGAL_OPERATION_TARGET) { return EINVAL; // FD does not support writes. - }else if(resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { + } else if (resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { return ENOSPC; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + } else if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { return ENOTCONN; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - if(bytes_written) + if (bytes_written) *bytes_written = resp.size(); return 0; @@ -1816,12 +1820,13 @@ int sys_pread(int fd, void *buf, size_t n, off_t off, ssize_t *bytes_read) { req.set_offset(off); auto [offer, send_req, imbue_creds, recv_resp, recv_data] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::recvInline(), - helix_ng::recvBuffer(buf, n)) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::recvInline(), + helix_ng::recvBuffer(buf, n) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -1830,17 +1835,17 @@ int sys_pread(int fd, void *buf, size_t n, off_t off, ssize_t *bytes_read) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); -/* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { - return EBADF; - }else*/ - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + /* if(resp.error() == managarm::fs::Errors::NO_SUCH_FD) { + return EBADF; + }else*/ + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::END_OF_FILE) { + } else if (resp.error() == managarm::fs::Errors::END_OF_FILE) { *bytes_read = 0; return 0; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); HEL_CHECK(recv_data.error()); *bytes_read = recv_data.actualLength(); @@ -1865,13 +1870,13 @@ int sys_pwrite(int fd, const void *buf, size_t n, off_t off, ssize_t *bytes_writ req.SerializeToString(&ser); auto [offer, send_head, imbue_creds, to_write, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::sendBuffer(buf, n), - helix_ng::recvInline() - ) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::sendBuffer(buf, n), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1882,17 +1887,17 @@ int sys_pwrite(int fd, const void *buf, size_t n, off_t off, ssize_t *bytes_writ managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - }else if(resp.error() == managarm::fs::Errors::WOULD_BLOCK) { + } else if (resp.error() == managarm::fs::Errors::WOULD_BLOCK) { return EAGAIN; - }else if(resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { + } else if (resp.error() == managarm::fs::Errors::NO_SPACE_LEFT) { return ENOSPC; - }else if(resp.error() == managarm::fs::Errors::SEEK_ON_PIPE) { + } else if (resp.error() == managarm::fs::Errors::SEEK_ON_PIPE) { return ESPIPE; - }else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *bytes_written = n; return 0; @@ -1903,20 +1908,20 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) { SignalGuard sguard; auto handle = getHandleForFd(fd); - if(!handle) + if (!handle) return EBADF; managarm::fs::CntRequest req(getSysdepsAllocator()); req.set_fd(fd); req.set_rel_offset(offset); - if(whence == SEEK_SET) { + if (whence == SEEK_SET) { req.set_req_type(managarm::fs::CntReqType::SEEK_ABS); - }else if(whence == SEEK_CUR) { + } else if (whence == SEEK_CUR) { req.set_req_type(managarm::fs::CntReqType::SEEK_REL); - }else if(whence == SEEK_END) { + } else if (whence == SEEK_END) { req.set_req_type(managarm::fs::CntReqType::SEEK_EOF); - }else{ + } else { return EINVAL; } @@ -1924,11 +1929,8 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) { req.SerializeToString(&ser); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::recvInline() - ) + handle, + helix_ng::offer(helix_ng::sendBuffer(ser.data(), ser.size()), helix_ng::recvInline()) ); HEL_CHECK(offer.error()); @@ -1937,9 +1939,9 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::SEEK_ON_PIPE) { + if (resp.error() == managarm::fs::Errors::SEEK_ON_PIPE) { return ESPIPE; - } else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -1948,7 +1950,6 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) { } } - int sys_close(int fd) { SignalGuard sguard; @@ -1956,11 +1957,10 @@ int sys_close(int fd) { req.set_fd(fd); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1970,11 +1970,11 @@ int sys_close(int fd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::SUCCESS) { + } else if (resp.error() == managarm::posix::Errors::SUCCESS) { return 0; - }else{ + } else { __ensure(!"Unexpected error"); __builtin_unreachable(); } @@ -1986,7 +1986,7 @@ int sys_dup(int fd, int flags, int *newfd) { __ensure(!(flags & ~(O_CLOEXEC))); uint32_t proto_flags = 0; - if(flags & O_CLOEXEC) + if (flags & O_CLOEXEC) proto_flags |= managarm::posix::OpenFlags::OF_CLOEXEC; managarm::posix::CntRequest req(getSysdepsAllocator()); @@ -1995,10 +1995,10 @@ int sys_dup(int fd, int flags, int *newfd) { req.set_flags(proto_flags); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -2029,10 +2029,10 @@ int sys_dup2(int fd, int flags, int newfd) { req.set_flags(flags); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -2079,14 +2079,12 @@ int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2095,33 +2093,40 @@ int sys_stat(fsfd_target fsfdt, int fd, const char *path, int flags, struct stat managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); memset(result, 0, sizeof(struct stat)); - switch(resp.file_type()) { - case managarm::posix::FileType::FT_REGULAR: - result->st_mode = S_IFREG; break; - case managarm::posix::FileType::FT_DIRECTORY: - result->st_mode = S_IFDIR; break; - case managarm::posix::FileType::FT_SYMLINK: - result->st_mode = S_IFLNK; break; - case managarm::posix::FileType::FT_CHAR_DEVICE: - result->st_mode = S_IFCHR; break; - case managarm::posix::FileType::FT_BLOCK_DEVICE: - result->st_mode = S_IFBLK; break; - case managarm::posix::FileType::FT_SOCKET: - result->st_mode = S_IFSOCK; break; - case managarm::posix::FileType::FT_FIFO: - result->st_mode = S_IFIFO; break; - default: - __ensure(!resp.file_type()); + switch (resp.file_type()) { + case managarm::posix::FileType::FT_REGULAR: + result->st_mode = S_IFREG; + break; + case managarm::posix::FileType::FT_DIRECTORY: + result->st_mode = S_IFDIR; + break; + case managarm::posix::FileType::FT_SYMLINK: + result->st_mode = S_IFLNK; + break; + case managarm::posix::FileType::FT_CHAR_DEVICE: + result->st_mode = S_IFCHR; + break; + case managarm::posix::FileType::FT_BLOCK_DEVICE: + result->st_mode = S_IFBLK; + break; + case managarm::posix::FileType::FT_SOCKET: + result->st_mode = S_IFSOCK; + break; + case managarm::posix::FileType::FT_FIFO: + result->st_mode = S_IFIFO; + break; + default: + __ensure(!resp.file_type()); } result->st_dev = 1; @@ -2156,11 +2161,12 @@ int sys_readlinkat(int dirfd, const char *path, void *data, size_t max_size, ssi req.set_path(frg::string(getSysdepsAllocator(), path)); auto [offer, send_head, send_tail, recv_resp, recv_data] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(data, max_size)) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(data, max_size) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2169,11 +2175,11 @@ int sys_readlinkat(int dirfd, const char *path, void *data, size_t max_size, ssi managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *length = recv_data.actualLength(); return 0; @@ -2186,14 +2192,12 @@ int sys_rmdir(const char *path) { managarm::posix::RmdirRequest req(getSysdepsAllocator()); req.set_path(frg::string(getSysdepsAllocator(), path)); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2202,15 +2206,15 @@ int sys_rmdir(const char *path) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::DIRECTORY_NOT_EMPTY) { + } else if (resp.error() == managarm::posix::Errors::DIRECTORY_NOT_EMPTY) { return ENOTEMPTY; - }else if(resp.error() == managarm::posix::Errors::INTERNAL_ERROR) { + } else if (resp.error() == managarm::posix::Errors::INTERNAL_ERROR) { return EIEIO; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -2228,10 +2232,10 @@ int sys_ftruncate(int fd, size_t size) { req.set_size(size); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -2239,7 +2243,7 @@ int sys_ftruncate(int fd, size_t size) { 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::ILLEGAL_OPERATION_TARGET) { return EINVAL; } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -2260,10 +2264,10 @@ int sys_fallocate(int fd, off_t offset, size_t size) { req.set_size(size); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -2271,13 +2275,13 @@ int sys_fallocate(int fd, off_t offset, size_t size) { 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::ILLEGAL_OPERATION_TARGET) { return EINVAL; - }else if(resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { + } else if (resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { return EPERM; - }else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); return 0; } @@ -2292,14 +2296,12 @@ int sys_unlinkat(int fd, const char *path, int flags) { req.set_path(frg::string(getSysdepsAllocator(), path)); req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2308,29 +2310,27 @@ int sys_unlinkat(int fd, const char *path, int flags) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::RESOURCE_IN_USE) { + } else if (resp.error() == managarm::posix::Errors::RESOURCE_IN_USE) { return EBUSY; - }else if(resp.error() == managarm::posix::Errors::IS_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::IS_DIRECTORY) { return EISDIR; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::BAD_FD) { + } else if (resp.error() == managarm::posix::Errors::BAD_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { + } else if (resp.error() == managarm::posix::Errors::NOT_A_DIRECTORY) { return ENOTDIR; - }else if(resp.error() == managarm::posix::Errors::DIRECTORY_NOT_EMPTY) { + } else if (resp.error() == managarm::posix::Errors::DIRECTORY_NOT_EMPTY) { return ENOTEMPTY; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } } -int sys_access(const char *path, int mode) { - return sys_faccessat(AT_FDCWD, path, mode, 0); -} +int sys_access(const char *path, int mode) { return sys_faccessat(AT_FDCWD, path, mode, 0); } int sys_faccessat(int dirfd, const char *pathname, int, int flags) { SignalGuard sguard; @@ -2340,14 +2340,12 @@ int sys_faccessat(int dirfd, const char *pathname, int, int flags) { req.set_fd(dirfd); req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2356,13 +2354,13 @@ int sys_faccessat(int dirfd, const char *pathname, int, int flags) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + } else if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -2376,15 +2374,15 @@ int sys_flock(int fd, int opts) { req.set_fd(fd); req.set_flock_flags(opts); auto handle = getHandleForFd(fd); - if(!handle) { + if (!handle) { return EBADF; } auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -2392,9 +2390,9 @@ int sys_flock(int fd, int opts) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::WOULD_BLOCK) { + if (resp.error() == managarm::posix::Errors::WOULD_BLOCK) { return EWOULDBLOCK; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -2409,11 +2407,10 @@ int sys_isatty(int fd) { req.set_fd(fd); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -2422,11 +2419,11 @@ int sys_isatty(int fd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); - if(resp.mode()) + if (resp.mode()) return 0; return ENOTTY; } @@ -2436,9 +2433,7 @@ int sys_chmod(const char *pathname, mode_t mode) { return sys_fchmodat(AT_FDCWD, pathname, mode, 0); } -int sys_fchmod(int fd, mode_t mode) { - return sys_fchmodat(fd, "", mode, AT_EMPTY_PATH); -} +int sys_fchmod(int fd, mode_t mode) { return sys_fchmodat(fd, "", mode, AT_EMPTY_PATH); } int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) { SignalGuard sguard; @@ -2449,14 +2444,12 @@ int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) { req.set_mode(mode); req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2465,15 +2458,15 @@ int sys_fchmodat(int fd, const char *pathname, mode_t mode, int flags) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + } else if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::NOT_SUPPORTED) { + } else if (resp.error() == managarm::posix::Errors::NOT_SUPPORTED) { return ENOTSUP; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -2501,9 +2494,9 @@ int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2 managarm::posix::UtimensAtRequest req(getSysdepsAllocator()); req.set_fd(dirfd); - if(pathname != nullptr) + if (pathname != nullptr) req.set_path(frg::string(getSysdepsAllocator(), pathname)); - if(times) { + if (times) { req.set_atimeSec(times[0].tv_sec); req.set_atimeNsec(times[0].tv_nsec); req.set_mtimeSec(times[1].tv_sec); @@ -2516,14 +2509,12 @@ int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2 } req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2532,15 +2523,15 @@ int sys_utimensat(int dirfd, const char *pathname, const struct timespec times[2 managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) { return ENOENT; - }else if(resp.error() == managarm::posix::Errors::NO_SUCH_FD) { + } else if (resp.error() == managarm::posix::Errors::NO_SUCH_FD) { return EBADF; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else if(resp.error() == managarm::posix::Errors::NOT_SUPPORTED) { + } else if (resp.error() == managarm::posix::Errors::NOT_SUPPORTED) { return ENOTSUP; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -2551,10 +2542,10 @@ int sys_getentropy(void *buffer, size_t length) { auto p = reinterpret_cast(buffer); size_t n = 0; - while(n < length) { + while (n < length) { size_t chunk; HEL_CHECK(helGetRandomBytes(p + n, length - n, &chunk)); - n+= chunk; + n += chunk; } return 0; @@ -2564,7 +2555,7 @@ int sys_gethostname(char *buffer, size_t bufsize) { SignalGuard sguard; mlibc::infoLogger() << "mlibc: gethostname always returns managarm" << frg::endlog; char name[10] = "managarm\0"; - if(bufsize < 10) + if (bufsize < 10) return ENAMETOOLONG; strncpy(buffer, name, 10); return 0; @@ -2582,14 +2573,12 @@ int sys_memfd_create(const char *name, int flags, int *fd) { req.set_name(frg::string(getSysdepsAllocator(), name)); req.set_flags(flags); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -2598,7 +2587,7 @@ int sys_memfd_create(const char *name, int flags, int *fd) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } @@ -2617,12 +2606,12 @@ int sys_uname(struct utsname *buf) { strcpy(buf->version, "Managarm is not Managram"); #if defined(__x86_64__) strcpy(buf->machine, "x86_64"); -#elif defined (__aarch64__) +#elif defined(__aarch64__) strcpy(buf->machine, "aarch64"); -#elif defined (__riscv) +#elif defined(__riscv) strcpy(buf->machine, "riscv64"); #else -# error Unknown architecture +#error Unknown architecture #endif return 0; @@ -2635,9 +2624,9 @@ int sys_madvise(void *, size_t, int) { int sys_ptsname(int fd, char *buffer, size_t length) { int index; - if(int e = sys_ioctl(fd, TIOCGPTN, &index, NULL); e) + if (int e = sys_ioctl(fd, TIOCGPTN, &index, NULL); e) return e; - if((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) { + if ((size_t)snprintf(buffer, length, "/dev/pts/%d", index) >= length) { return ERANGE; } return 0; @@ -2646,14 +2635,14 @@ int sys_ptsname(int fd, char *buffer, size_t length) { int sys_unlockpt(int fd) { int unlock = 0; - if(int e = sys_ioctl(fd, TIOCSPTLCK, &unlock, NULL); e) + if (int e = sys_ioctl(fd, TIOCSPTLCK, &unlock, NULL); e) return e; return 0; } int sys_getrlimit(int resource, struct rlimit *limit) { - switch(resource) { + switch (resource) { case RLIMIT_NOFILE: /* TODO: change this once we support more than 512 */ limit->rlim_cur = 512; @@ -2665,10 +2654,10 @@ int sys_getrlimit(int resource, struct rlimit *limit) { } int sys_sysconf(int num, long *ret) { - switch(num) { + switch (num) { case _SC_OPEN_MAX: { struct rlimit ru; - if(int e = sys_getrlimit(RLIMIT_NOFILE, &ru); e) { + if (int e = sys_getrlimit(RLIMIT_NOFILE, &ru); e) { return e; } *ret = (ru.rlim_cur == RLIM_INFINITY) ? -1 : ru.rlim_cur; @@ -2680,11 +2669,11 @@ int sys_sysconf(int num, long *ret) { managarm::posix::SysconfRequest req(getSysdepsAllocator()); req.set_num(num); - auto [offer, send_req, recv_resp] = exchangeMsgsSync(getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_req, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -2693,7 +2682,7 @@ int sys_sysconf(int num, long *ret) { managarm::posix::SysconfResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } @@ -2712,11 +2701,11 @@ int sys_sysinfo(struct sysinfo *info) { managarm::posix::GetMemoryInformationRequest req(getSysdepsAllocator()); - auto [offer, send_req, recv_resp] = exchangeMsgsSync(getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_req, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -2745,5 +2734,4 @@ int sys_sysinfo(struct sysinfo *info) { return 0; } -} //namespace mlibc - +} // namespace mlibc diff --git a/sysdeps/managarm/generic/fork-exec.cpp b/sysdeps/managarm/generic/fork-exec.cpp index ee1bcd43a6..858b9dbedb 100644 --- a/sysdeps/managarm/generic/fork-exec.cpp +++ b/sysdeps/managarm/generic/fork-exec.cpp @@ -2,8 +2,8 @@ // for _Exit() #include -#include #include +#include // for fork() and execve() #include @@ -13,15 +13,15 @@ // for getrusage() #include // for waitpid() -#include #include +#include #include +#include #include #include #include #include -#include #include #include @@ -29,27 +29,26 @@ namespace mlibc { int sys_futex_tid() { HelWord tid = 0; - HEL_CHECK(helSyscall0_1(kHelCallSuper + posix::superGetTid, - &tid)); + HEL_CHECK(helSyscall0_1(kHelCallSuper + posix::superGetTid, &tid)); return tid; } int sys_futex_wait(int *pointer, int expected, const struct timespec *time) { // This implementation is inherently signal-safe. - if(time) { - if(helFutexWait(pointer, expected, time->tv_nsec + time->tv_sec * 1000000000)) + if (time) { + if (helFutexWait(pointer, expected, time->tv_nsec + time->tv_sec * 1000000000)) return -1; return 0; } - if(helFutexWait(pointer, expected, -1)) + if (helFutexWait(pointer, expected, -1)) return -1; return 0; } int sys_futex_wake(int *pointer) { // This implementation is inherently signal-safe. - if(helFutexWake(pointer)) + if (helFutexWake(pointer)) return -1; return 0; } @@ -62,14 +61,12 @@ int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru, pid_t *ret req.set_pid(pid); req.set_flags(flags); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -77,15 +74,15 @@ int sys_waitpid(pid_t pid, int *status, int flags, struct rusage *ru, pid_t *ret managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } __ensure(resp.error() == managarm::posix::Errors::SUCCESS); - if(status) + if (status) *status = resp.mode(); *ret_pid = resp.pid(); - if(ru != nullptr) { + if (ru != nullptr) { ru->ru_utime.tv_sec = resp.ru_user_time() / 1'000'000'000; ru->ru_utime.tv_usec = (resp.ru_user_time() % 1'000'000'000) / 1'000; } @@ -102,14 +99,12 @@ int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) { req.set_id(id); req.set_flags(options); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -117,7 +112,7 @@ int sys_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) { managarm::posix::WaitIdResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } __ensure(resp.error() == managarm::posix::Errors::SUCCESS); @@ -148,8 +143,9 @@ int sys_sleep(time_t *secs, long *nanos) { HEL_CHECK(helGetClock(&now)); uint64_t async_id; - HEL_CHECK(helSubmitAwaitClock(now + uint64_t(*secs) * 1000000000 + uint64_t(*nanos), - globalQueue.getQueue(), 0, &async_id)); + HEL_CHECK(helSubmitAwaitClock( + now + uint64_t(*secs) * 1000000000 + uint64_t(*nanos), globalQueue.getQueue(), 0, &async_id + )); auto element = globalQueue.dequeueSingle(); auto result = parseSimple(element); @@ -177,7 +173,7 @@ int sys_fork(pid_t *child) { HEL_CHECK(helSyscall0_1(kHelCallSuper + posix::superFork, &out)); *child = out; - if(!out) { + if (!out) { clearCachedInfos(); globalQueue.recreateQueue(); } @@ -191,23 +187,25 @@ int sys_fork(pid_t *child) { int sys_execve(const char *path, char *const argv[], char *const envp[]) { // TODO: Make this function signal-safe! frg::string args_area(getSysdepsAllocator()); - for(auto it = argv; *it; ++it) + for (auto it = argv; *it; ++it) args_area += frg::string_view{*it, strlen(*it) + 1}; frg::string env_area(getSysdepsAllocator()); - for(auto it = envp; *it; ++it) + for (auto it = envp; *it; ++it) env_area += frg::string_view{*it, strlen(*it) + 1}; uintptr_t out; - HEL_CHECK(helSyscall6_1(kHelCallSuper + posix::superExecve, - reinterpret_cast(path), - strlen(path), - reinterpret_cast(args_area.data()), - args_area.size(), - reinterpret_cast(env_area.data()), - env_area.size(), - &out)); + HEL_CHECK(helSyscall6_1( + kHelCallSuper + posix::superExecve, + reinterpret_cast(path), + strlen(path), + reinterpret_cast(args_area.data()), + args_area.size(), + reinterpret_cast(env_area.data()), + env_area.size(), + &out + )); return out; } @@ -217,14 +215,12 @@ gid_t sys_getgid() { managarm::posix::GetGidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -243,14 +239,12 @@ int sys_setgid(gid_t gid) { req.set_uid(gid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -258,11 +252,11 @@ int sys_setgid(gid_t gid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { return EPERM; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -273,14 +267,12 @@ gid_t sys_getegid() { managarm::posix::GetEgidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -299,14 +291,12 @@ int sys_setegid(gid_t egid) { req.set_uid(egid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -314,11 +304,11 @@ int sys_setegid(gid_t egid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { return EPERM; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -329,14 +319,12 @@ uid_t sys_getuid() { managarm::posix::GetUidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -355,14 +343,12 @@ int sys_setuid(uid_t uid) { req.set_uid(uid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -370,11 +356,11 @@ int sys_setuid(uid_t uid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { return EPERM; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -385,14 +371,12 @@ uid_t sys_geteuid() { managarm::posix::GetEuidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -411,14 +395,12 @@ int sys_seteuid(uid_t euid) { req.set_uid(euid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -426,11 +408,11 @@ int sys_seteuid(uid_t euid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { return EPERM; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -446,14 +428,12 @@ pid_t sys_getpid() { managarm::posix::GetPidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -470,14 +450,12 @@ pid_t sys_getppid() { managarm::posix::GetPpidRequest req(getSysdepsAllocator()); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -495,14 +473,12 @@ int sys_getsid(pid_t pid, pid_t *sid) { managarm::posix::GetSidRequest req(getSysdepsAllocator()); req.set_pid(pid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -510,7 +486,7 @@ int sys_getsid(pid_t pid, pid_t *sid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { + if (resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { *sid = 0; return ESRCH; } else { @@ -526,14 +502,12 @@ int sys_getpgid(pid_t pid, pid_t *pgid) { managarm::posix::GetPgidRequest req(getSysdepsAllocator()); req.set_pid(pid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -541,7 +515,7 @@ int sys_getpgid(pid_t pid, pid_t *pgid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { + if (resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { *pgid = 0; return ESRCH; } else { @@ -559,14 +533,12 @@ int sys_setpgid(pid_t pid, pid_t pgid) { req.set_pid(pid); req.set_pgid(pgid); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -574,13 +546,13 @@ int sys_setpgid(pid_t pid, pid_t pgid) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::INSUFFICIENT_PERMISSION) { + if (resp.error() == managarm::posix::Errors::INSUFFICIENT_PERMISSION) { return EPERM; - }else if(resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { + } else if (resp.error() == managarm::posix::Errors::NO_SUCH_RESOURCE) { return ESRCH; - }else if(resp.error() == managarm::posix::Errors::ACCESS_DENIED) { + } else if (resp.error() == managarm::posix::Errors::ACCESS_DENIED) { return EACCES; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } @@ -595,14 +567,12 @@ int sys_getrusage(int scope, struct rusage *usage) { req.set_request_type(managarm::posix::CntReqType::GET_RESOURCE_USAGE); req.set_mode(scope); - auto [offer, send_head, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -619,25 +589,26 @@ int sys_getrusage(int scope, struct rusage *usage) { } int sys_getschedparam(void *tcb, int *policy, struct sched_param *param) { - if(tcb != mlibc::get_current_tcb()) { + if (tcb != mlibc::get_current_tcb()) { return ESRCH; } *policy = SCHED_OTHER; int prio = 0; // TODO(no92): use helGetPriority(kHelThisThread) here - mlibc::infoLogger() << "\e[31mlibc: sys_getschedparam always returns priority 0\e[39m" << frg::endlog; + mlibc::infoLogger() << "\e[31mlibc: sys_getschedparam always returns priority 0\e[39m" + << frg::endlog; param->sched_priority = prio; return 0; } int sys_setschedparam(void *tcb, int policy, const struct sched_param *param) { - if(tcb != mlibc::get_current_tcb()) { + if (tcb != mlibc::get_current_tcb()) { return ESRCH; } - if(policy != SCHED_OTHER) { + if (policy != SCHED_OTHER) { return EINVAL; } @@ -648,10 +619,12 @@ int sys_setschedparam(void *tcb, int policy, const struct sched_param *param) { int sys_clone(void *tcb, pid_t *pid_out, void *stack) { HelWord pid = 0; - HEL_CHECK(helSyscall2_1(kHelCallSuper + posix::superClone, - reinterpret_cast(__mlibc_start_thread), - reinterpret_cast(stack), - &pid)); + HEL_CHECK(helSyscall2_1( + kHelCallSuper + posix::superClone, + reinterpret_cast(__mlibc_start_thread), + reinterpret_cast(stack), + &pid + )); if (pid_out) *pid_out = pid; @@ -663,7 +636,7 @@ int sys_tcb_set(void *pointer) { #if defined(__aarch64__) uintptr_t addr = reinterpret_cast(pointer); addr += sizeof(Tcb) - 0x10; - asm volatile ("msr tpidr_el0, %0" :: "r"(addr)); + asm volatile("msr tpidr_el0, %0" ::"r"(addr)); #else HEL_CHECK(helWriteFsBase(pointer)); #endif @@ -677,7 +650,7 @@ void sys_thread_exit() { } int sys_thread_setname(void *tcb, const char *name) { - if(strlen(name) > 15) { + if (strlen(name) > 15) { return ERANGE; } @@ -685,18 +658,18 @@ int sys_thread_setname(void *tcb, const char *name) { char *path; int cs = 0; - if(asprintf(&path, "/proc/self/task/%d/comm", t->tid) < 0) { + if (asprintf(&path, "/proc/self/task/%d/comm", t->tid) < 0) { return ENOMEM; } pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); int fd; - if(int e = sys_open(path, O_WRONLY, 0, &fd); e) { + if (int e = sys_open(path, O_WRONLY, 0, &fd); e) { return e; } - if(int e = sys_write(fd, name, strlen(name) + 1, NULL)) { + if (int e = sys_write(fd, name, strlen(name) + 1, NULL)) { return e; } @@ -713,18 +686,18 @@ int sys_thread_getname(void *tcb, char *name, size_t size) { int cs = 0; ssize_t real_size = 0; - if(asprintf(&path, "/proc/self/task/%d/comm", t->tid) < 0) { + if (asprintf(&path, "/proc/self/task/%d/comm", t->tid) < 0) { return ENOMEM; } pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); int fd; - if(int e = sys_open(path, O_RDONLY | O_CLOEXEC, 0, &fd); e) { + if (int e = sys_open(path, O_RDONLY | O_CLOEXEC, 0, &fd); e) { return e; } - if(int e = sys_read(fd, name, size, &real_size)) { + if (int e = sys_read(fd, name, size, &real_size)) { return e; } @@ -733,13 +706,11 @@ int sys_thread_getname(void *tcb, char *name, size_t size) { pthread_setcancelstate(cs, 0); - if(static_cast(size) <= real_size) { + if (static_cast(size) <= real_size) { return ERANGE; } return 0; } - -} //namespace mlibc - +} // namespace mlibc diff --git a/sysdeps/managarm/generic/ioctl.cpp b/sysdeps/managarm/generic/ioctl.cpp index b0925e6929..5d316dce46 100644 --- a/sysdeps/managarm/generic/ioctl.cpp +++ b/sysdeps/managarm/generic/ioctl.cpp @@ -3,12 +3,12 @@ #include #include #include -#include #include -#include +#include #include #include #include +#include #include #include @@ -28,26 +28,28 @@ static constexpr bool logIoctls = false; int ioctl_drm(int fd, unsigned long request, void *arg, int *result, HelHandle handle); int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { - if(logIoctls) + if (logIoctls) mlibc::infoLogger() << "mlibc: ioctl with" - << " type: 0x" << frg::hex_fmt(_IOC_TYPE(request)) - << ", number: 0x" << frg::hex_fmt(_IOC_NR(request)) - << " (raw request: " << frg::hex_fmt(request) << ")" - << " on fd " << fd << frg::endlog; + << " type: 0x" << frg::hex_fmt(_IOC_TYPE(request)) << ", number: 0x" + << frg::hex_fmt(_IOC_NR(request)) + << " (raw request: " << frg::hex_fmt(request) << ")" + << " on fd " << fd << frg::endlog; SignalGuard sguard; auto handle = getHandleForFd(fd); - if(!handle) + if (!handle) return EBADF; - if(_IOC_TYPE(request) == 'd') { + if (_IOC_TYPE(request) == 'd') { return ioctl_drm(fd, request, arg, result, handle); } - auto handle_siocgif = [&arg, &request, &result] - (void (*req_setup)(managarm::fs::IfreqRequest &req, struct ifreq *ifr), - int (*resp_parse)(managarm::fs::IfreqReply &resp, struct ifreq *ifr)) -> int { - if(!arg) + auto handle_siocgif = + [&arg, &request, &result]( + void (*req_setup)(managarm::fs::IfreqRequest &req, struct ifreq *ifr), + int (*resp_parse)(managarm::fs::IfreqReply &resp, struct ifreq *ifr) + ) -> int { + if (!arg) return EFAULT; auto ifr = reinterpret_cast(arg); @@ -58,14 +60,13 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { req_setup(req, ifr); - auto [offer, send_token_req, send_req, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(token_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_token_req, send_req, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(token_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -78,452 +79,452 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { int ret = resp_parse(resp, ifr); - if(result) + if (result) *result = 0; return ret; }; managarm::fs::IoctlRequest ioctl_req(getSysdepsAllocator()); - switch(request) { - case FIONBIO: { - auto mode = reinterpret_cast(arg); - int flags = fcntl(fd, F_GETFL, 0); - if(*mode) { - fcntl(fd, F_SETFL, flags | O_NONBLOCK); - }else{ - fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); + switch (request) { + case FIONBIO: { + auto mode = reinterpret_cast(arg); + int flags = fcntl(fd, F_GETFL, 0); + if (*mode) { + fcntl(fd, F_SETFL, flags | O_NONBLOCK); + } else { + fcntl(fd, F_SETFL, flags & ~O_NONBLOCK); + } + return 0; } - return 0; - } - case FIONREAD: { - auto argp = reinterpret_cast(arg); + case FIONREAD: { + auto argp = reinterpret_cast(arg); - auto handle = getHandleForFd(fd); - if (!handle) - return EBADF; + auto handle = getHandleForFd(fd); + if (!handle) + return EBADF; - if(!argp) - return EINVAL; + if (!argp) + return EINVAL; - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(FIONREAD); - - auto [offer, send_ioctl_req, send_req, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(FIONREAD); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { - return ENOTCONN; - } else { - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + return ENOTCONN; + } else { + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *argp = resp.fionread_count(); + *argp = resp.fionread_count(); - return 0; + return 0; + } } - } - case FIOCLEX: { - managarm::posix::IoctlFioclexRequest req(getSysdepsAllocator()); - req.set_fd(fd); - - auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); - - HEL_CHECK(offer.error()); - HEL_CHECK(sendReq.error()); - if(recvResp.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(recvResp.error()); - - managarm::posix::SvrResponse resp(getSysdepsAllocator()); - resp.ParseFromArray(recvResp.data(), recvResp.length()); - __ensure(resp.error() == managarm::posix::Errors::SUCCESS); - return 0; - } - case TCGETS: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - auto [offer, send_ioctl_req, send_req, recv_resp, recv_attrs] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(param, sizeof(struct termios)) - ) - ); - - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); - HEL_CHECK(recv_attrs.error()); + case FIOCLEX: { + managarm::posix::IoctlFioclexRequest req(getSysdepsAllocator()); + req.set_fd(fd); + + auto [offer, sendReq, recvResp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - __ensure(recv_attrs.actualLength() == sizeof(struct termios)); - *result = resp.result(); - return 0; - } - case TCSETS: { - auto param = reinterpret_cast(arg); + HEL_CHECK(offer.error()); + HEL_CHECK(sendReq.error()); + if (recvResp.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(recvResp.error()); - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); + managarm::posix::SvrResponse resp(getSysdepsAllocator()); + resp.ParseFromArray(recvResp.data(), recvResp.length()); + __ensure(resp.error() == managarm::posix::Errors::SUCCESS); + return 0; + } + case TCGETS: { + auto param = reinterpret_cast(arg); - auto [offer, send_ioctl_req, send_req, send_attrs, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::sendBuffer(param, sizeof(struct termios)), - helix_ng::recvInline() - ) - ); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, recv_resp, recv_attrs] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(param, sizeof(struct termios)) + ) + ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(send_attrs.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); + HEL_CHECK(recv_attrs.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - if(result) + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + __ensure(recv_attrs.actualLength() == sizeof(struct termios)); *result = resp.result(); - return 0; - } - case TIOCSCTTY: { - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::recvInline()) - ); + return 0; + } + case TCSETS: { + auto param = reinterpret_cast(arg); - HEL_CHECK(offer.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_ioctl_req.error()); - HEL_CHECK(imbue_creds.error()); - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, send_attrs, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::sendBuffer(param, sizeof(struct termios)), + helix_ng::recvInline() + ) + ); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(send_attrs.error()); + HEL_CHECK(recv_resp.error()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { - return EINVAL; - }else if(resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { - return EPERM; + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + if (result) + *result = resp.result(); + return 0; } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - return 0; - } - case TIOCGWINSZ: { - auto param = reinterpret_cast(arg); - - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + case TIOCSCTTY: { + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::recvInline() + ) + ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - if(recv_resp.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_ioctl_req.error()); + HEL_CHECK(imbue_creds.error()); + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) - return EINVAL; - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - *result = resp.result(); - param->ws_col = resp.pts_width(); - param->ws_row = resp.pts_height(); - param->ws_xpixel = resp.pts_pixel_width(); - param->ws_ypixel = resp.pts_pixel_height(); - return 0; - } - case TIOCSWINSZ: { - auto param = reinterpret_cast(arg); + if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } else if (resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { + return EPERM; + } + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + return 0; + } + case TIOCGWINSZ: { + auto param = reinterpret_cast(arg); - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_pts_width(param->ws_col); - req.set_pts_height(param->ws_row); - req.set_pts_pixel_width(param->ws_xpixel); - req.set_pts_pixel_height(param->ws_ypixel); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + if (recv_resp.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) + return EINVAL; + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - return 0; - } - case TIOCGPTN: { - auto param = reinterpret_cast(arg); + *result = resp.result(); + param->ws_col = resp.pts_width(); + param->ws_row = resp.pts_height(); + param->ws_xpixel = resp.pts_pixel_width(); + param->ws_ypixel = resp.pts_pixel_height(); + return 0; + } + case TIOCSWINSZ: { + auto param = reinterpret_cast(arg); - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_pts_width(param->ws_col); + req.set_pts_height(param->ws_row); + req.set_pts_pixel_width(param->ws_xpixel); + req.set_pts_pixel_height(param->ws_ypixel); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) + ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) - ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *param = resp.pts_index(); - if(result) *result = resp.result(); - return 0; - } - case TIOCGPGRP: { - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); + return 0; + } + case TIOCGPTN: { + auto param = reinterpret_cast(arg); - frg::string ser(getSysdepsAllocator()); - req.SerializeToString(&ser); - - auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::imbueCredentials(), - helix_ng::recvInline() - ) + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error()) - return EINVAL; - HEL_CHECK(send_req.error()); - if(imbue_creds.error()) { - infoLogger() << "mlibc: TIOCGPGRP used on unexpected socket, returning EINVAL (FIXME)" << frg::endlog; - return EINVAL; + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *param = resp.pts_index(); + if (result) + *result = resp.result(); + return 0; } - HEL_CHECK(imbue_creds.error()); - HEL_CHECK(recv_resp.error()); + case TIOCGPGRP: { + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + frg::string ser(getSysdepsAllocator()); + req.SerializeToString(&ser); + + auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::imbueCredentials(), + helix_ng::recvInline() + ) + ); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { - return ENOTTY; + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error()) + return EINVAL; + HEL_CHECK(send_req.error()); + if (imbue_creds.error()) { + infoLogger( + ) << "mlibc: TIOCGPGRP used on unexpected socket, returning EINVAL (FIXME)" + << frg::endlog; + return EINVAL; + } + HEL_CHECK(imbue_creds.error()); + HEL_CHECK(recv_resp.error()); + + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { + return ENOTTY; + } + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + *static_cast(arg) = resp.pid(); + return 0; } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - *static_cast(arg) = resp.pid(); - return 0; - } - case TIOCSPGRP: { - auto param = reinterpret_cast(arg); + case TIOCSPGRP: { + auto param = reinterpret_cast(arg); - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - req.set_pgid(*param); - - frg::string ser(getSysdepsAllocator()); - req.SerializeToString(&ser); - - auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::imbueCredentials(), - helix_ng::recvInline() - ) + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + req.set_pgid(*param); + + frg::string ser(getSysdepsAllocator()); + req.SerializeToString(&ser); + + auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::imbueCredentials(), + helix_ng::recvInline() + ) ); - HEL_CHECK(offer.error()); - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(imbue_creds.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(imbue_creds.error()); + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { - return EPERM; - } else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { - return EINVAL; + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::INSUFFICIENT_PERMISSIONS) { + return EPERM; + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + return EINVAL; + } + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + return 0; } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - return 0; - } - case TIOCGSID: { - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - frg::string ser(getSysdepsAllocator()); - req.SerializeToString(&ser); - - auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::imbueCredentials(), - helix_ng::recvInline() - ) + case TIOCGSID: { + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + frg::string ser(getSysdepsAllocator()); + req.SerializeToString(&ser); + + auto [offer, send_ioctl_req, send_req, imbue_creds, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::imbueCredentials(), + helix_ng::recvInline() + ) ); - HEL_CHECK(offer.error()); - if(send_ioctl_req.error()) - return EINVAL; - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error()) - return EINVAL; - HEL_CHECK(send_req.error()); - if(imbue_creds.error() == kHelErrDismissed) - return EINVAL; - HEL_CHECK(imbue_creds.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + if (send_ioctl_req.error()) + return EINVAL; + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error()) + return EINVAL; + HEL_CHECK(send_req.error()); + if (imbue_creds.error() == kHelErrDismissed) + return EINVAL; + HEL_CHECK(imbue_creds.error()); + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { - return ENOTTY; + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { + return ENOTTY; + } + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + *static_cast(arg) = resp.pid(); + return 0; } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - *static_cast(arg) = resp.pid(); - return 0; - } - case CDROM_GET_CAPABILITY: { - managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); - req.set_command(request); - - frg::string ser(getSysdepsAllocator()); - req.SerializeToString(&ser); - - auto [offer, send_ioctl_req, send_req, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::recvInline() - ) + case CDROM_GET_CAPABILITY: { + managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); + req.set_command(request); + + frg::string ser(getSysdepsAllocator()); + req.SerializeToString(&ser); + + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::recvInline() + ) ); - HEL_CHECK(offer.error()); - if(send_ioctl_req.error()) - return EINVAL; - HEL_CHECK(send_ioctl_req.error()); - if(send_req.error()) - return EINVAL; - HEL_CHECK(send_req.error()); - HEL_CHECK(recv_resp.error()); + HEL_CHECK(offer.error()); + if (send_ioctl_req.error()) + return EINVAL; + HEL_CHECK(send_ioctl_req.error()); + if (send_req.error()) + return EINVAL; + HEL_CHECK(send_req.error()); + HEL_CHECK(recv_resp.error()); - managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); - resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { - return ENOTTY; + managarm::fs::GenericIoctlReply resp(getSysdepsAllocator()); + resp.ParseFromArray(recv_resp.data(), recv_resp.length()); + if (resp.error() == managarm::fs::Errors::NOT_A_TERMINAL) { + return ENOTTY; + } + __ensure(resp.error() == managarm::fs::Errors::SUCCESS); + *result = resp.result(); + return 0; } - __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - *result = resp.result(); - return 0; - } } // end of switch() - - if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGVERSION)) { + if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGVERSION)) { *reinterpret_cast(arg) = EV_VERSION; *result = 0; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGID)) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGID)) { memset(arg, 0, sizeof(struct input_id)); auto param = reinterpret_cast(arg); managarm::fs::EvioGetIdRequest req(getSysdepsAllocator()); auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::want_lane, - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::want_lane, + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); auto conversation = offer.descriptor(); @@ -531,7 +532,8 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { HEL_CHECK(send_req.error()); HEL_CHECK(recv_resp.error()); - auto resp = *bragi::parse_head_only(recv_resp, getSysdepsAllocator()); + auto resp = + *bragi::parse_head_only(recv_resp, getSysdepsAllocator()); recv_resp.reset(); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -542,17 +544,17 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { *result = 0; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGNAME(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGNAME(0))) { managarm::fs::EvioGetNameRequest req(getSysdepsAllocator()); auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::want_lane, - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::want_lane, + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); auto conversation = offer.descriptor(); @@ -566,13 +568,14 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { frg::vector tailBuffer{getSysdepsAllocator()}; tailBuffer.resize(preamble.tail_size()); auto [recv_tail] = exchangeMsgsSync( - conversation.getHandle(), - helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) + conversation.getHandle(), helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) ); HEL_CHECK(recv_tail.error()); - auto resp = *bragi::parse_head_tail(recv_resp, tailBuffer, getSysdepsAllocator()); + auto resp = *bragi::parse_head_tail( + recv_resp, tailBuffer, getSysdepsAllocator() + ); recv_resp.reset(); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -580,43 +583,37 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { memcpy(arg, resp.name().data(), chunk); *result = chunk; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGRAB)) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGRAB)) { mlibc::infoLogger() << "mlibc: EVIOCGRAB is a no-op" << frg::endlog; *result = 0; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGPHYS(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGPHYS(0))) { // Returns the sysfs path of the device. const char *s = "input0"; auto chunk = frg::min(_IOC_SIZE(request), strlen(s) + 1); memcpy(arg, s, chunk); *result = chunk; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGUNIQ(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGUNIQ(0))) { // Returns a unique ID for the device. const char *s = "0"; auto chunk = frg::min(_IOC_SIZE(request), strlen(s) + 1); memcpy(arg, s, chunk); *result = chunk; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGPROP(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGPROP(0))) { // Returns a bitmask of properties of the device. auto size = _IOC_SIZE(request); memset(arg, 0, size); *result = size; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGKEY(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGKEY(0))) { // Returns the current key state. auto size = _IOC_SIZE(request); memset(arg, 0, size); *result = size; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGMTSLOTS(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGMTSLOTS(0))) { // this ioctl is completely, utterly undocumented // the _IOC_SIZE is a buffer size in bytes, which should be a multiple of int32_t // bytes should be at least sizeof(int32_t) large. @@ -627,7 +624,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { // the length argument is the buffer size, in bytes auto bytes = _IOC_SIZE(request); // the length argument should be a multiple of int32_t - if(!bytes || bytes % sizeof(int32_t)) + if (!bytes || bytes % sizeof(int32_t)) return EINVAL; // the number of entries the buffer can hold @@ -637,12 +634,13 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { req.set_code(*reinterpret_cast(arg)); auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::want_lane, - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::want_lane, + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); auto conversation = offer.descriptor(); @@ -656,47 +654,45 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { frg::vector tailBuffer{getSysdepsAllocator()}; tailBuffer.resize(preamble.tail_size()); auto [recv_tail] = exchangeMsgsSync( - conversation.getHandle(), - helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) + conversation.getHandle(), helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) ); HEL_CHECK(recv_tail.error()); - auto resp = *bragi::parse_head_tail(recv_resp, tailBuffer, getSysdepsAllocator()); + auto resp = *bragi::parse_head_tail( + recv_resp, tailBuffer, getSysdepsAllocator() + ); recv_resp.reset(); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); auto param = reinterpret_cast(arg); - for(size_t i = 0; i < resp.values_size() && i < entries; i++) { + for (size_t i = 0; i < resp.values_size() && i < entries; i++) { param[i + 1] = resp.values(i); } param[0] = resp.values_size(); return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGLED(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGLED(0))) { // Returns the current LED state. auto size = _IOC_SIZE(request); memset(arg, 0, size); *result = size; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCGSW(0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCGSW(0))) { auto size = _IOC_SIZE(request); memset(arg, 0, size); *result = size; return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) >= _IOC_NR(EVIOCGBIT(0, 0)) - && _IOC_NR(request) <= _IOC_NR(EVIOCGBIT(EV_MAX, 0))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) >= _IOC_NR(EVIOCGBIT(0, 0)) && + _IOC_NR(request) <= _IOC_NR(EVIOCGBIT(EV_MAX, 0))) { // Returns a bitmask of capabilities of the device. // If type is zero, return a mask of supported types. // As EV_SYN is zero, this implies that it is impossible // to get the mask of supported synthetic events. auto type = _IOC_NR(request) - _IOC_NR(EVIOCGBIT(0, 0)); - if(!type) { + if (!type) { // TODO: Check with the Linux ABI if we have to do this. memset(arg, 0, _IOC_SIZE(request)); @@ -705,17 +701,18 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { req.set_size(_IOC_SIZE(request)); auto [offer, send_ioctl_req, send_req, recv_resp, recv_data] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(arg, _IOC_SIZE(request))) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(arg, _IOC_SIZE(request)) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) + if (send_req.error() == kHelErrDismissed) return EINVAL; HEL_CHECK(send_req.error()); HEL_CHECK(recv_resp.error()); @@ -726,7 +723,7 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = recv_data.actualLength(); return 0; - }else{ + } else { // TODO: Check with the Linux ABI if we have to do this. memset(arg, 0, _IOC_SIZE(request)); @@ -736,17 +733,18 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { req.set_size(_IOC_SIZE(request)); auto [offer, send_ioctl_req, send_req, recv_resp, recv_data] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(arg, _IOC_SIZE(request))) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(arg, _IOC_SIZE(request)) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) + if (send_req.error() == kHelErrDismissed) return EINVAL; HEL_CHECK(send_req.error()); HEL_CHECK(recv_resp.error()); @@ -758,25 +756,25 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { *result = recv_data.actualLength(); return 0; } - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) == _IOC_NR(EVIOCSCLOCKID)) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) == _IOC_NR(EVIOCSCLOCKID)) { auto param = reinterpret_cast(arg); managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); req.set_command(request); req.set_input_clock(*param); - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) + if (send_req.error() == kHelErrDismissed) return EINVAL; HEL_CHECK(send_req.error()); HEL_CHECK(recv_resp.error()); @@ -786,9 +784,8 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *result = resp.result(); return 0; - }else if(_IOC_TYPE(request) == 'E' - && _IOC_NR(request) >= _IOC_NR(EVIOCGABS(0)) - && _IOC_NR(request) <= _IOC_NR(EVIOCGABS(ABS_MAX))) { + } else if (_IOC_TYPE(request) == 'E' && _IOC_NR(request) >= _IOC_NR(EVIOCGABS(0)) && + _IOC_NR(request) <= _IOC_NR(EVIOCGABS(ABS_MAX))) { auto param = reinterpret_cast(arg); auto type = _IOC_NR(request) - _IOC_NR(EVIOCGABS(0)); @@ -796,17 +793,18 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { req.set_command(EVIOCGABS(0)); req.set_input_type(type); - auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync(handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_ioctl_req.error()); - if(send_req.error() == kHelErrDismissed) + if (send_req.error() == kHelErrDismissed) return EINVAL; HEL_CHECK(send_req.error()); HEL_CHECK(recv_resp.error()); @@ -824,32 +822,34 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { *result = resp.result(); return 0; - }else if(request == KDSETMODE) { + } else if (request == KDSETMODE) { auto param = reinterpret_cast(arg); - mlibc::infoLogger() << "\e[35mmlibc: KD_SETMODE(" << frg::hex_fmt(param) << ") is a no-op" << frg::endlog; + mlibc::infoLogger() << "\e[35mmlibc: KD_SETMODE(" << frg::hex_fmt(param) << ") is a no-op" + << frg::endlog; *result = 0; return 0; - }else if(request == KDGETMODE) { + } else if (request == KDGETMODE) { auto param = reinterpret_cast(arg); mlibc::infoLogger() << "\e[35mmlibc: KD_GETMODE is a no-op" << frg::endlog; *param = 0; *result = 0; return 0; - }else if(request == KDSKBMODE) { + } else if (request == KDSKBMODE) { auto param = reinterpret_cast(arg); - mlibc::infoLogger() << "\e[35mmlibc: KD_SKBMODE(" << frg::hex_fmt(param) << ") is a no-op" << frg::endlog; + mlibc::infoLogger() << "\e[35mmlibc: KD_SKBMODE(" << frg::hex_fmt(param) << ") is a no-op" + << frg::endlog; *result = 0; return 0; - }else if(request == VT_SETMODE) { + } else if (request == VT_SETMODE) { // auto param = reinterpret_cast(arg); mlibc::infoLogger() << "\e[35mmlibc: VT_SETMODE is a no-op" << frg::endlog; *result = 0; return 0; - }else if(request == VT_GETSTATE) { + } else if (request == VT_GETSTATE) { auto param = reinterpret_cast(arg); param->v_active = 0; @@ -860,26 +860,27 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { *result = 0; return 0; - }else if(request == VT_ACTIVATE || request == VT_WAITACTIVE) { + } else if (request == VT_ACTIVATE || request == VT_WAITACTIVE) { mlibc::infoLogger() << "\e[35mmlibc: VT_ACTIVATE/VT_WAITACTIVE are no-ops" << frg::endlog; *result = 0; return 0; - }else if(request == TIOCSPTLCK) { + } else if (request == TIOCSPTLCK) { mlibc::infoLogger() << "\e[35mmlibc: TIOCSPTLCK is a no-op" << frg::endlog; - if(result) + if (result) *result = 0; return 0; - } else if(request == SIOCGIFNAME) { - return handle_siocgif([](auto req, auto ifr) { - req.set_index(ifr->ifr_ifindex); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - strncpy(ifr->ifr_name, resp.name().data(), IFNAMSIZ); - return 0; - }); - }else if(request == SIOCGIFCONF) { - if(!arg) + } else if (request == SIOCGIFNAME) { + return handle_siocgif( + [](auto req, auto ifr) { req.set_index(ifr->ifr_ifindex); }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + strncpy(ifr->ifr_name, resp.name().data(), IFNAMSIZ); + return 0; + } + ); + } else if (request == SIOCGIFCONF) { + if (!arg) return EFAULT; auto ifc = reinterpret_cast(arg); @@ -888,15 +889,14 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { managarm::fs::IfreqRequest req(getSysdepsAllocator()); req.set_command(request); - auto [offer, send_token_req, send_req, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::want_lane, - helix_ng::sendBragiHeadOnly(token_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + auto [offer, send_token_req, send_req, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::want_lane, + helix_ng::sendBragiHeadOnly(token_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); auto conversation = offer.descriptor(); @@ -912,25 +912,27 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { frg::vector tailBuffer{getSysdepsAllocator()}; tailBuffer.resize(preamble.tail_size()); auto [recv_tail] = exchangeMsgsSync( - conversation.getHandle(), - helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) + conversation.getHandle(), helix_ng::recvBuffer(tailBuffer.data(), tailBuffer.size()) ); HEL_CHECK(recv_tail.error()); - auto resp = *bragi::parse_head_tail(recv_resp, tailBuffer, getSysdepsAllocator()); + auto resp = *bragi::parse_head_tail( + recv_resp, tailBuffer, getSysdepsAllocator() + ); recv_resp.reset(); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - if(ifc->ifc_buf == nullptr) { + if (ifc->ifc_buf == nullptr) { ifc->ifc_len = int(resp.ifconf_size() * sizeof(struct ifreq)); return 0; } ifc->ifc_len = frg::min(int(resp.ifconf_size() * sizeof(struct ifreq)), ifc->ifc_len); - for(size_t i = 0; i < frg::min(resp.ifconf_size() , ifc->ifc_len / sizeof(struct ifreq)); ++i) { + for (size_t i = 0; i < frg::min(resp.ifconf_size(), ifc->ifc_len / sizeof(struct ifreq)); + ++i) { auto &conf = resp.ifconf()[i]; sockaddr_in addr{}; @@ -942,107 +944,128 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { memcpy(&req->ifr_addr, &addr, sizeof(addr)); } - if(result) + if (result) *result = 0; return 0; - }else if(request == SIOCGIFNETMASK) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_addr = { htonl(resp.ip4_netmask()) }; - memcpy(&ifr->ifr_netmask, &addr, sizeof(addr)); - - return 0; - }); - }else if(request == SIOCGIFINDEX) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - ifr->ifr_ifindex = resp.index(); - return 0; - }); - }else if(request == SIOCGIFFLAGS) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - ifr->ifr_flags = resp.flags(); - return 0; - }); - }else if(request == SIOCGIFADDR) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_addr = { htonl(resp.ip4_addr()) }; - memcpy(&ifr->ifr_addr, &addr, sizeof(addr)); - - return 0; - }); - }else if(request == SIOCGIFMTU) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - - ifr->ifr_mtu = resp.mtu(); - - return 0; - }); - }else if(request == SIOCGIFBRDADDR) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - - sockaddr_in addr{}; - addr.sin_family = AF_INET; - addr.sin_addr = { htonl(resp.ip4_broadcast_addr()) }; - memcpy(&ifr->ifr_broadaddr, &addr, sizeof(addr)); - - return 0; - }); - } else if(request == SIOCGIFHWADDR) { - return handle_siocgif([](auto req, auto ifr) { - req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); - }, [](auto resp, auto ifr) { - if(resp.error() != managarm::fs::Errors::SUCCESS) - return EINVAL; - - sockaddr addr{}; - addr.sa_family = ARPHRD_ETHER; - memcpy(addr.sa_data, resp.mac().data(), 6); - memcpy(&ifr->ifr_hwaddr, &addr, sizeof(addr)); - - return 0; - }); - } else if(request == IOCTL_WDM_MAX_COMMAND) { + } else if (request == SIOCGIFNETMASK) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_addr = {htonl(resp.ip4_netmask())}; + memcpy(&ifr->ifr_netmask, &addr, sizeof(addr)); + + return 0; + } + ); + } else if (request == SIOCGIFINDEX) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + ifr->ifr_ifindex = resp.index(); + return 0; + } + ); + } else if (request == SIOCGIFFLAGS) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + ifr->ifr_flags = resp.flags(); + return 0; + } + ); + } else if (request == SIOCGIFADDR) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_addr = {htonl(resp.ip4_addr())}; + memcpy(&ifr->ifr_addr, &addr, sizeof(addr)); + + return 0; + } + ); + } else if (request == SIOCGIFMTU) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + + ifr->ifr_mtu = resp.mtu(); + + return 0; + } + ); + } else if (request == SIOCGIFBRDADDR) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + + sockaddr_in addr{}; + addr.sin_family = AF_INET; + addr.sin_addr = {htonl(resp.ip4_broadcast_addr())}; + memcpy(&ifr->ifr_broadaddr, &addr, sizeof(addr)); + + return 0; + } + ); + } else if (request == SIOCGIFHWADDR) { + return handle_siocgif( + [](auto req, auto ifr) { + req.set_name(frg::string{ifr->ifr_name, getSysdepsAllocator()}); + }, + [](auto resp, auto ifr) { + if (resp.error() != managarm::fs::Errors::SUCCESS) + return EINVAL; + + sockaddr addr{}; + addr.sa_family = ARPHRD_ETHER; + memcpy(addr.sa_data, resp.mac().data(), 6); + memcpy(&ifr->ifr_hwaddr, &addr, sizeof(addr)); + + return 0; + } + ); + } else if (request == IOCTL_WDM_MAX_COMMAND) { auto param = reinterpret_cast(arg); managarm::fs::GenericIoctlRequest req(getSysdepsAllocator()); req.set_command(request); auto [offer, send_ioctl_req, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(ioctl_req, getSysdepsAllocator()), + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -1059,11 +1082,11 @@ int sys_ioctl(int fd, unsigned long request, void *arg, int *result) { } mlibc::infoLogger() << "mlibc: Unexpected ioctl with" - << " type: 0x" << frg::hex_fmt(_IOC_TYPE(request)) - << ", number: 0x" << frg::hex_fmt(_IOC_NR(request)) - << " (raw request: " << frg::hex_fmt(request) << ")" << frg::endlog; + << " type: 0x" << frg::hex_fmt(_IOC_TYPE(request)) << ", number: 0x" + << frg::hex_fmt(_IOC_NR(request)) + << " (raw request: " << frg::hex_fmt(request) << ")" << frg::endlog; __ensure(!"Illegal ioctl request"); __builtin_unreachable(); } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/memory.cpp b/sysdeps/managarm/generic/memory.cpp index 91e47af3e9..5695f0d9b5 100644 --- a/sysdeps/managarm/generic/memory.cpp +++ b/sysdeps/managarm/generic/memory.cpp @@ -2,12 +2,12 @@ #include #include -#include #include +#include #include -#include #include +#include namespace mlibc { @@ -26,5 +26,4 @@ int sys_anon_free(void *pointer, size_t size) { return 0; } -} //namespace mlibc - +} // namespace mlibc diff --git a/sysdeps/managarm/generic/mount.cpp b/sysdeps/managarm/generic/mount.cpp index 5677b20a70..b50b4fa703 100644 --- a/sysdeps/managarm/generic/mount.cpp +++ b/sysdeps/managarm/generic/mount.cpp @@ -3,16 +3,16 @@ #include #include +#include +#include #include #include -#include #include -#include namespace mlibc { -int sys_mount(const char *source, const char *target, - const char *fstype, unsigned long, const void *) { +int +sys_mount(const char *source, const char *target, const char *fstype, unsigned long, const void *) { SignalGuard sguard; managarm::posix::MountRequest req(getSysdepsAllocator()); @@ -20,25 +20,24 @@ int sys_mount(const char *source, const char *target, req.set_target_path(frg::string(getSysdepsAllocator(), target)); req.set_fs_type(frg::string(getSysdepsAllocator(), fstype)); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); HEL_CHECK(send_tail.error()); HEL_CHECK(recv_resp.error()); - auto resp = *bragi::parse_head_only(recv_resp, getSysdepsAllocator()); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) + auto resp = + *bragi::parse_head_only(recv_resp, getSysdepsAllocator()); + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) return ENOENT; __ensure(resp.error() == managarm::posix::Errors::SUCCESS); return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/net.cpp b/sysdeps/managarm/generic/net.cpp index 5435f09496..010ff59308 100644 --- a/sysdeps/managarm/generic/net.cpp +++ b/sysdeps/managarm/generic/net.cpp @@ -11,7 +11,7 @@ int sys_if_indextoname(unsigned int index, char *name) { int fd = 0; int r = sys_socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd); - if(r) + if (r) return r; struct ifreq ifr; @@ -21,8 +21,8 @@ int sys_if_indextoname(unsigned int index, char *name) { int ret = sys_ioctl(fd, SIOCGIFNAME, &ifr, &res); close(fd); - if(ret) { - if(ret == ENODEV) + if (ret) { + if (ret == ENODEV) return ENXIO; return ret; } @@ -36,7 +36,7 @@ int sys_if_nametoindex(const char *name, unsigned int *ret) { int fd = 0; int r = sys_socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, AF_UNSPEC, &fd); - if(r) + if (r) return r; struct ifreq ifr; @@ -46,7 +46,7 @@ int sys_if_nametoindex(const char *name, unsigned int *ret) { r = sys_ioctl(fd, SIOCGIFINDEX, &ifr, &res); close(fd); - if(r) + if (r) return r; *ret = ifr.ifr_ifindex; @@ -66,4 +66,4 @@ int sys_getifaddrs(struct ifaddrs **out) { return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/sched.cpp b/sysdeps/managarm/generic/sched.cpp index bce8db8601..f5f3ee5c5e 100644 --- a/sysdeps/managarm/generic/sched.cpp +++ b/sysdeps/managarm/generic/sched.cpp @@ -1,10 +1,10 @@ #include #include -#include #include -#include +#include #include +#include #include #include @@ -24,15 +24,14 @@ int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask) { req.set_pid(tid); req.set_size(cpusetsize); - auto [offer, send_head, recv_resp, recv_data] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(mask, cpusetsize) - ) - ); + auto [offer, send_head, recv_resp, recv_data] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(mask, cpusetsize) + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -40,11 +39,12 @@ int sys_getthreadaffinity(pid_t tid, size_t cpusetsize, cpu_set_t *mask) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - } else if(resp.error() != managarm::posix::Errors::SUCCESS) { - mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog; + } else if (resp.error() != managarm::posix::Errors::SUCCESS) { + mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" + << frg::endlog; return EIEIO; } HEL_CHECK(recv_data.error()); @@ -67,14 +67,12 @@ int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask) { req.set_pid(tid); req.set_mask(affinity_mask); - auto [offer, send_head, send_tail, recv_resp] = - exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) - ); + auto [offer, send_head, send_tail, recv_resp] = exchangeMsgsSync( + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadTail(req, getSysdepsAllocator()), helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_head.error()); @@ -83,11 +81,12 @@ int sys_setthreadaffinity(pid_t tid, size_t cpusetsize, const cpu_set_t *mask) { managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - - if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + + if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; - } else if(resp.error() != managarm::posix::Errors::SUCCESS) { - mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" << frg::endlog; + } else if (resp.error() != managarm::posix::Errors::SUCCESS) { + mlibc::infoLogger() << "mlibc: got unexpected error from posix in sys_getaffinity!" + << frg::endlog; return EIEIO; } @@ -99,4 +98,4 @@ int sys_getcpu(int *cpu) { return 0; } -} +} // namespace mlibc diff --git a/sysdeps/managarm/generic/signals.cpp b/sysdeps/managarm/generic/signals.cpp index 634f9917e6..f313887115 100644 --- a/sysdeps/managarm/generic/signals.cpp +++ b/sysdeps/managarm/generic/signals.cpp @@ -1,17 +1,17 @@ #include +#include #include #include #include #include -#include -#include #include +#include -#include +#include #include +#include #include -#include #include #include @@ -26,18 +26,26 @@ namespace mlibc { int sys_sigprocmask(int how, const sigset_t *set, sigset_t *retrieve) { // This implementation is inherently signal-safe. uint64_t former, unused; - if(set) { - HEL_CHECK(helSyscall2_2(kHelObserveSuperCall + posix::superSigMask, how, *reinterpret_cast(set), &former, &unused)); - }else{ - HEL_CHECK(helSyscall2_2(kHelObserveSuperCall + posix::superSigMask, 0, 0, &former, &unused)); + if (set) { + HEL_CHECK(helSyscall2_2( + kHelObserveSuperCall + posix::superSigMask, + how, + *reinterpret_cast(set), + &former, + &unused + )); + } else { + HEL_CHECK(helSyscall2_2(kHelObserveSuperCall + posix::superSigMask, 0, 0, &former, &unused) + ); } - if(retrieve) + if (retrieve) *reinterpret_cast(retrieve) = former; return 0; } -int sys_sigaction(int number, const struct sigaction *__restrict action, - struct sigaction *__restrict saved_action) { +int sys_sigaction( + int number, const struct sigaction *__restrict action, struct sigaction *__restrict saved_action +) { SignalGuard sguard; // TODO: Respect restorer. __ensure(!(action->sa_flags & SA_RESTORER)); @@ -45,13 +53,13 @@ int sys_sigaction(int number, const struct sigaction *__restrict action, managarm::posix::CntRequest req(getSysdepsAllocator()); req.set_request_type(managarm::posix::CntReqType::SIG_ACTION); req.set_sig_number(number); - if(action) { + if (action) { req.set_mode(1); req.set_flags(action->sa_flags); req.set_sig_mask(*reinterpret_cast(&action->sa_mask)); - if(action->sa_flags & SA_SIGINFO) { + if (action->sa_flags & SA_SIGINFO) { req.set_sig_handler(reinterpret_cast(action->sa_sigaction)); - }else{ + } else { req.set_sig_handler(reinterpret_cast(action->sa_handler)); } req.set_sig_restorer(reinterpret_cast(&__mlibc_signal_restore)); @@ -60,10 +68,10 @@ int sys_sigaction(int number, const struct sigaction *__restrict action, } auto [offer, send_req, recv_resp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -72,21 +80,21 @@ int sys_sigaction(int number, const struct sigaction *__restrict action, managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::posix::Errors::ILLEGAL_REQUEST) { + if (resp.error() == managarm::posix::Errors::ILLEGAL_REQUEST) { // This is only returned for servers, not for normal userspace. return ENOSYS; - }else if(resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { + } else if (resp.error() == managarm::posix::Errors::ILLEGAL_ARGUMENTS) { return EINVAL; } __ensure(resp.error() == managarm::posix::Errors::SUCCESS); - if(saved_action) { + if (saved_action) { saved_action->sa_flags = resp.flags(); *reinterpret_cast(&saved_action->sa_mask) = resp.sig_mask(); - if(resp.flags() & SA_SIGINFO) { + if (resp.flags() & SA_SIGINFO) { saved_action->sa_sigaction = - reinterpret_cast(resp.sig_handler()); - }else{ + reinterpret_cast(resp.sig_handler()); + } else { saved_action->sa_handler = reinterpret_cast(resp.sig_handler()); } // TODO: saved_action->sa_restorer = resp.sig_restorer; @@ -100,29 +108,37 @@ int sys_kill(int pid, int number) { return 0; } -int sys_tgkill(int, int tid, int number) { - return sys_kill(tid, number); -} +int sys_tgkill(int, int tid, int number) { return sys_kill(tid, number); } int sys_sigaltstack(const stack_t *ss, stack_t *oss) { HelWord out; // This implementation is inherently signal-safe. - HEL_CHECK(helSyscall2_1(kHelObserveSuperCall + posix::superSigAltStack, - reinterpret_cast(ss), - reinterpret_cast(oss), - &out)); + HEL_CHECK(helSyscall2_1( + kHelObserveSuperCall + posix::superSigAltStack, + reinterpret_cast(ss), + reinterpret_cast(oss), + &out + )); return out; } int sys_sigsuspend(const sigset_t *set) { - //SignalGuard sguard; + // SignalGuard sguard; uint64_t former, seq, unused; - HEL_CHECK(helSyscall2_2(kHelObserveSuperCall + posix::superSigMask, SIG_SETMASK, *reinterpret_cast(set), &former, &seq)); + HEL_CHECK(helSyscall2_2( + kHelObserveSuperCall + posix::superSigMask, + SIG_SETMASK, + *reinterpret_cast(set), + &former, + &seq + )); HEL_CHECK(helSyscall1(kHelObserveSuperCall + posix::superSigSuspend, seq)); - HEL_CHECK(helSyscall2_2(kHelObserveSuperCall + posix::superSigMask, SIG_SETMASK, former, &unused, &unused)); + HEL_CHECK(helSyscall2_2( + kHelObserveSuperCall + posix::superSigMask, SIG_SETMASK, former, &unused, &unused + )); return EINTR; } @@ -136,4 +152,4 @@ int sys_sigpending(sigset_t *set) { return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/socket.cpp b/sysdeps/managarm/generic/socket.cpp index 4121bd5b99..ea93fa830c 100644 --- a/sysdeps/managarm/generic/socket.cpp +++ b/sysdeps/managarm/generic/socket.cpp @@ -1,18 +1,18 @@ #include #include #include -#include #include -#include #include -#include +#include #include +#include +#include +#include +#include #include #include #include -#include -#include #include namespace { @@ -20,7 +20,7 @@ namespace { int fcntl_helper(int fd, int request, int *result, ...) { va_list args; va_start(args, result); - if(!mlibc::sys_fcntl) { + if (!mlibc::sys_fcntl) { return ENOSYS; } int ret = mlibc::sys_fcntl(fd, request, args, result); @@ -28,7 +28,7 @@ int fcntl_helper(int fd, int request, int *result, ...) { return ret; } -} +} // namespace namespace mlibc { @@ -39,11 +39,10 @@ int sys_accept(int fd, int *newfd, struct sockaddr *addr_ptr, socklen_t *addr_le req.set_fd(fd); auto [offer, sendReq, recvResp] = exchangeMsgsSync( - getPosixLane(), - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline() - ) + getPosixLane(), + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); @@ -52,27 +51,27 @@ int sys_accept(int fd, int *newfd, struct sockaddr *addr_ptr, socklen_t *addr_le managarm::posix::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recvResp.data(), recvResp.length()); - if(resp.error() == managarm::posix::Errors::WOULD_BLOCK) { + if (resp.error() == managarm::posix::Errors::WOULD_BLOCK) { return EWOULDBLOCK; - }else{ + } else { __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *newfd = resp.fd(); } - if(addr_ptr && addr_length) { - if(int e = mlibc::sys_peername(*newfd, addr_ptr, *addr_length, addr_length); e) { + if (addr_ptr && addr_length) { + if (int e = mlibc::sys_peername(*newfd, addr_ptr, *addr_length, addr_length); e) { errno = e; return -1; } } - if(flags & SOCK_NONBLOCK) { + if (flags & SOCK_NONBLOCK) { int fcntl_ret = 0; fcntl_helper(*newfd, F_GETFL, &fcntl_ret); fcntl_helper(*newfd, F_SETFL, &fcntl_ret, fcntl_ret | O_NONBLOCK); } - if(flags & SOCK_CLOEXEC) { + if (flags & SOCK_CLOEXEC) { int fcntl_ret = 0; fcntl_helper(*newfd, F_GETFD, &fcntl_ret); fcntl_helper(*newfd, F_SETFD, &fcntl_ret, fcntl_ret | FD_CLOEXEC); @@ -91,12 +90,13 @@ int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) { req.set_req_type(managarm::fs::CntReqType::PT_BIND); auto [offer, send_req, send_creds, send_buf, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::imbueCredentials(), - helix_ng::sendBuffer(addr_ptr, addr_length), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::imbueCredentials(), + helix_ng::sendBuffer(addr_ptr, addr_length), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -106,19 +106,19 @@ int sys_bind(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) { managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { return ENOENT; - } else if(resp.error() == managarm::fs::Errors::ADDRESS_IN_USE) { + } else if (resp.error() == managarm::fs::Errors::ADDRESS_IN_USE) { return EADDRINUSE; - } else if(resp.error() == managarm::fs::Errors::ALREADY_EXISTS) { + } else if (resp.error() == managarm::fs::Errors::ALREADY_EXISTS) { return EINVAL; - } else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) { return EINVAL; - } else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - } else if(resp.error() == managarm::fs::Errors::ACCESS_DENIED) { + } else if (resp.error() == managarm::fs::Errors::ACCESS_DENIED) { return EACCES; - } else if(resp.error() == managarm::fs::Errors::ADDRESS_NOT_AVAILABLE) { + } else if (resp.error() == managarm::fs::Errors::ADDRESS_NOT_AVAILABLE) { return EADDRNOTAVAIL; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -138,16 +138,15 @@ int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) frg::string ser(getSysdepsAllocator()); req.SerializeToString(&ser); - auto [offer, send_req, imbue_creds, send_addr, recv_resp] = - exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::imbueCredentials(), - helix_ng::sendBuffer(const_cast(addr_ptr), addr_length), - helix_ng::recvInline() - ) - ); + auto [offer, send_req, imbue_creds, send_addr, recv_resp] = exchangeMsgsSync( + handle, + helix_ng::offer( + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::imbueCredentials(), + helix_ng::sendBuffer(const_cast(addr_ptr), addr_length), + helix_ng::recvInline() + ) + ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -157,11 +156,11 @@ int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { + if (resp.error() == managarm::fs::Errors::FILE_NOT_FOUND) { return ENOENT; - } else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { + } else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) { return EINVAL; - } else if(resp.error() == managarm::fs::Errors::CONNECTION_REFUSED) { + } else if (resp.error() == managarm::fs::Errors::CONNECTION_REFUSED) { return ECONNREFUSED; } @@ -169,8 +168,9 @@ int sys_connect(int fd, const struct sockaddr *addr_ptr, socklen_t addr_length) return 0; } -int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, - socklen_t *actual_length) { +int sys_sockname( + int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length +) { SignalGuard sguard; auto handle = getHandleForFd(fd); @@ -183,11 +183,12 @@ int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, req.set_size(max_addr_length); auto [offer, send_req, recv_resp, recv_addr] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline(), - helix_ng::recvBuffer(addr_ptr, max_addr_length)) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::recvInline(), + helix_ng::recvBuffer(addr_ptr, max_addr_length) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -195,7 +196,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::ILLEGAL_OPERATION_TARGET) { return ENOTSOCK; } __ensure(resp.error() == managarm::fs::Errors::SUCCESS); @@ -204,8 +205,9 @@ int sys_sockname(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, return 0; } -int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, - socklen_t *actual_length) { +int sys_peername( + int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, socklen_t *actual_length +) { SignalGuard sguard; auto handle = getHandleForFd(fd); @@ -221,40 +223,40 @@ int sys_peername(int fd, struct sockaddr *addr_ptr, socklen_t max_addr_length, req.SerializeToString(&ser); auto [offer, sendReq, recvResp, recvData] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::recvInline(), - helix_ng::recvBuffer(addr_ptr, max_addr_length) - ) + handle, + helix_ng::offer( + helix_ng::sendBuffer(ser.data(), ser.size()), + helix_ng::recvInline(), + helix_ng::recvBuffer(addr_ptr, max_addr_length) + ) ); HEL_CHECK(offer.error()); HEL_CHECK(sendReq.error()); - if(recvResp.error() == kHelErrDismissed) + if (recvResp.error() == kHelErrDismissed) return ENOTSOCK; HEL_CHECK(recvResp.error()); HEL_CHECK(recvData.error()); 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::ILLEGAL_OPERATION_TARGET) { return ENOTSOCK; - }else if(resp.error() == managarm::fs::Errors::NOT_CONNECTED) { + } else if (resp.error() == managarm::fs::Errors::NOT_CONNECTED) { return ENOTCONN; - }else{ + } else { __ensure(resp.error() == managarm::fs::Errors::SUCCESS); *actual_length = resp.file_size(); return 0; } } -int sys_getsockopt(int fd, int layer, int number, - void *__restrict buffer, socklen_t *__restrict size) { +int +sys_getsockopt(int fd, int layer, int number, void *__restrict buffer, socklen_t *__restrict size) { SignalGuard sguard; - if(layer == SOL_SOCKET && number == SO_PEERCRED) { - if(*size != sizeof(struct ucred)) + if (layer == SOL_SOCKET && number == SO_PEERCRED) { + if (*size != sizeof(struct ucred)) return EINVAL; auto handle = getHandleForFd(fd); @@ -266,10 +268,10 @@ int sys_getsockopt(int fd, int layer, int number, req.set_command(SO_PEERCRED); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -285,44 +287,63 @@ int sys_getsockopt(int fd, int layer, int number, creds.gid = resp.gid(); memcpy(buffer, &creds, sizeof(struct ucred)); return 0; - }else if(layer == SOL_SOCKET && number == SO_SNDBUF) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_SNDBUF is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_SNDBUF) { + mlibc::infoLogger( + ) << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_SNDBUF is unimplemented\e[39m" + << frg::endlog; *(int *)buffer = 4096; return 0; - }else if(layer == SOL_SOCKET && number == SO_RCVBUF) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_RCVBUF is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_RCVBUF) { + mlibc::infoLogger( + ) << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_RCVBUF is unimplemented\e[39m" + << frg::endlog; *(int *)buffer = 4096; return 0; - }else if(layer == SOL_SOCKET && number == SO_TYPE) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_TYPE is unimplemented, hardcoding SOCK_STREAM\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_TYPE) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_TYPE is " + "unimplemented, hardcoding SOCK_STREAM\e[39m" + << frg::endlog; *(int *)buffer = SOCK_STREAM; return 0; - }else if(layer == SOL_SOCKET && number == SO_ERROR) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ERROR is unimplemented, hardcoding 0\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_ERROR) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ERROR is " + "unimplemented, hardcoding 0\e[39m" + << frg::endlog; *(int *)buffer = 0; return 0; - }else if(layer == SOL_SOCKET && number == SO_KEEPALIVE) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_KEEPALIVE is unimplemented, hardcoding 0\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_KEEPALIVE is " + "unimplemented, hardcoding 0\e[39m" + << frg::endlog; *(int *)buffer = 0; return 0; - }else if(layer == SOL_SOCKET && number == SO_LINGER) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_LINGER is unimplemented, hardcoding 0\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_LINGER) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_LINGER is " + "unimplemented, hardcoding 0\e[39m" + << frg::endlog; *(int *)buffer = 0; return 0; - }else if(layer == SOL_SOCKET && number == SO_PEERSEC) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_PEERSEC is unimplemented, hardcoding 0\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_PEERSEC) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_PEERSEC is " + "unimplemented, hardcoding 0\e[39m" + << frg::endlog; *(int *)buffer = 0; return 0; - }else if(layer == SOL_SOCKET && number == SO_PEERGROUPS) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_PEERGROUPS is unimplemented, hardcoding 0\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_PEERGROUPS) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_PEERGROUPS " + "is unimplemented, hardcoding 0\e[39m" + << frg::endlog; *(int *)buffer = 0; return 0; - }else if(layer == SOL_SOCKET && number == SO_ACCEPTCONN) { - mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ACCEPTCONN is unimplemented, hardcoding 1\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_ACCEPTCONN) { + mlibc::infoLogger() << "\e[31mmlibc: getsockopt() call with SOL_SOCKET and SO_ACCEPTCONN " + "is unimplemented, hardcoding 1\e[39m" + << frg::endlog; *(int *)buffer = 1; return 0; - }else{ - mlibc::panicLogger() << "\e[31mmlibc: Unexpected getsockopt() call, layer: " << layer << " number: " << number << "\e[39m" << frg::endlog; + } else { + mlibc::panicLogger() << "\e[31mmlibc: Unexpected getsockopt() call, layer: " << layer + << " number: " << number << "\e[39m" << frg::endlog; __builtin_unreachable(); } } @@ -330,32 +351,31 @@ int sys_getsockopt(int fd, int layer, int number, namespace { std::array, 5> setsockopt_readonly = {{ - { SOL_SOCKET, SO_ACCEPTCONN }, - { SOL_SOCKET, SO_DOMAIN }, - { SOL_SOCKET, SO_ERROR }, - { SOL_SOCKET, SO_PROTOCOL }, - { SOL_SOCKET, SO_TYPE }, + {SOL_SOCKET, SO_ACCEPTCONN}, + {SOL_SOCKET, SO_DOMAIN}, + {SOL_SOCKET, SO_ERROR}, + {SOL_SOCKET, SO_PROTOCOL}, + {SOL_SOCKET, SO_TYPE}, }}; std::array, 5> setsockopt_passthrough = {{ - { SOL_PACKET, PACKET_AUXDATA }, - { SOL_SOCKET, SO_LOCK_FILTER }, - { SOL_SOCKET, SO_BINDTODEVICE }, - { SOL_IP, IP_PKTINFO }, - { SOL_NETLINK, NETLINK_ADD_MEMBERSHIP }, + {SOL_PACKET, PACKET_AUXDATA}, + {SOL_SOCKET, SO_LOCK_FILTER}, + {SOL_SOCKET, SO_BINDTODEVICE}, + {SOL_IP, IP_PKTINFO}, + {SOL_NETLINK, NETLINK_ADD_MEMBERSHIP}, }}; std::array, 2> setsockopt_passthrough_noopt = {{ - { SOL_SOCKET, SO_DETACH_FILTER }, + {SOL_SOCKET, SO_DETACH_FILTER}, }}; -} +} // namespace -int sys_setsockopt(int fd, int layer, int number, - const void *buffer, socklen_t size) { +int sys_setsockopt(int fd, int layer, int number, const void *buffer, socklen_t size) { SignalGuard sguard; - if(layer == SOL_SOCKET && number == SO_PASSCRED) { + if (layer == SOL_SOCKET && number == SO_PASSCRED) { int value; __ensure(size == sizeof(int)); memcpy(&value, buffer, sizeof(int)); @@ -370,10 +390,10 @@ int sys_setsockopt(int fd, int layer, int number, req.set_value(value); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -383,9 +403,13 @@ int sys_setsockopt(int fd, int layer, int number, resp.ParseFromArray(recv_resp.data(), recv_resp.length()); __ensure(resp.error() == managarm::fs::Errors::SUCCESS); return 0; - }else if(std::find(setsockopt_passthrough.begin(), setsockopt_passthrough.end(), std::pair{layer, number}) != setsockopt_passthrough.end()) { + } else if (std::find( + setsockopt_passthrough.begin(), + setsockopt_passthrough.end(), + std::pair{layer, number} + ) != setsockopt_passthrough.end()) { auto handle = getHandleForFd(fd); - if(!handle) + if (!handle) return EBADF; managarm::fs::SetSockOpt req(getSysdepsAllocator()); @@ -394,11 +418,12 @@ int sys_setsockopt(int fd, int layer, int number, req.set_optlen(size); auto [offer, send_req, send_buf, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::sendBuffer(buffer, size), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::sendBuffer(buffer, size), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -407,19 +432,23 @@ int sys_setsockopt(int fd, int layer, int number, managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::SUCCESS) + if (resp.error() == managarm::fs::Errors::SUCCESS) return 0; - else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) + else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) return EINVAL; - else if(resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) + else if (resp.error() == managarm::fs::Errors::ILLEGAL_ARGUMENT) return EINVAL; - else if(resp.error() == managarm::fs::Errors::INVALID_PROTOCOL_OPTION) + else if (resp.error() == managarm::fs::Errors::INVALID_PROTOCOL_OPTION) return ENOPROTOOPT; else __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - }else if(std::find(setsockopt_passthrough_noopt.begin(), setsockopt_passthrough_noopt.end(), std::pair{layer, number}) != setsockopt_passthrough_noopt.end()) { + } else if (std::find( + setsockopt_passthrough_noopt.begin(), + setsockopt_passthrough_noopt.end(), + std::pair{layer, number} + ) != setsockopt_passthrough_noopt.end()) { auto handle = getHandleForFd(fd); - if(!handle) + if (!handle) return EBADF; managarm::fs::SetSockOpt req(getSysdepsAllocator()); @@ -428,10 +457,10 @@ int sys_setsockopt(int fd, int layer, int number, req.set_optlen(0); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -439,21 +468,25 @@ int sys_setsockopt(int fd, int layer, int number, managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::SUCCESS) + if (resp.error() == managarm::fs::Errors::SUCCESS) return 0; - else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) + else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) return EINVAL; else __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - }else if(std::find(setsockopt_readonly.begin(), setsockopt_readonly.end(), std::pair{layer, number}) != setsockopt_readonly.end()) { + } else if (std::find( + setsockopt_readonly.begin(), + setsockopt_readonly.end(), + std::pair{layer, number} + ) != setsockopt_readonly.end()) { // this is purely read-only return ENOPROTOOPT; - }else if(layer == SOL_SOCKET && number == SO_ATTACH_FILTER) { + } else if (layer == SOL_SOCKET && number == SO_ATTACH_FILTER) { auto handle = getHandleForFd(fd); - if(!handle) + if (!handle) return EBADF; - if(size != sizeof(sock_fprog)) + if (size != sizeof(sock_fprog)) return EINVAL; auto fprog = reinterpret_cast(buffer); @@ -464,11 +497,12 @@ int sys_setsockopt(int fd, int layer, int number, req.set_optlen(fprog->len * sizeof(*fprog->filter)); auto [offer, send_req, send_buf, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), - helix_ng::sendBuffer(fprog->filter, req.optlen()), - helix_ng::recvInline()) + handle, + helix_ng::offer( + helix_ng::sendBragiHeadOnly(req, getSysdepsAllocator()), + helix_ng::sendBuffer(fprog->filter, req.optlen()), + helix_ng::recvInline() + ) ); HEL_CHECK(offer.error()); HEL_CHECK(send_req.error()); @@ -477,66 +511,100 @@ int sys_setsockopt(int fd, int layer, int number, managarm::fs::SvrResponse resp(getSysdepsAllocator()); resp.ParseFromArray(recv_resp.data(), recv_resp.length()); - if(resp.error() == managarm::fs::Errors::SUCCESS) + if (resp.error() == managarm::fs::Errors::SUCCESS) return 0; - else if(resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) + else if (resp.error() == managarm::fs::Errors::ILLEGAL_OPERATION_TARGET) return EINVAL; else __ensure(resp.error() == managarm::fs::Errors::SUCCESS); - }else if(layer == SOL_SOCKET && number == SO_RCVBUFFORCE) { + } else if (layer == SOL_SOCKET && number == SO_RCVBUFFORCE) { mlibc::infoLogger() << "\e[31mmlibc: setsockopt(SO_RCVBUFFORCE) is not implemented" - " correctly\e[39m" << frg::endlog; + " correctly\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_SNDBUF) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDBUF is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_SNDBUF) { + mlibc::infoLogger( + ) << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDBUF is unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_SNDBUFFORCE) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDBUFFORCE is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_SNDBUFFORCE) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDBUFFORCE " + "is unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_KEEPALIVE) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_KEEPALIVE is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_KEEPALIVE) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_KEEPALIVE is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_REUSEADDR) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_REUSEADDR is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_REUSEADDR) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_REUSEADDR is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_REUSEPORT) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_REUSEPORT is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_REUSEPORT) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_REUSEPORT is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_RCVBUF) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_RCVBUF is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_RCVBUF) { + mlibc::infoLogger( + ) << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_RCVBUF is unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == IPPROTO_TCP && number == TCP_NODELAY) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_NODELAY is unimplemented\e[39m" << frg::endlog; + } else if (layer == IPPROTO_TCP && number == TCP_NODELAY) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_NODELAY is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }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; + } 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; + } 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; + } 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; + } 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; 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; + } 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; return 0; - }else if(layer == IPPROTO_TCP && number == TCP_KEEPCNT) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPCNT is unimplemented\e[39m" << frg::endlog; + } else if (layer == IPPROTO_TCP && number == TCP_KEEPCNT) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with IPPROTO_TCP and TCP_KEEPCNT is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_SNDTIMEO) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDTIMEO is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_SNDTIMEO) { + mlibc::infoLogger( + ) << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_SNDTIMEO is unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_OOBINLINE) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_OOBINLINE is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_OOBINLINE) { + mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_OOBINLINE is " + "unimplemented\e[39m" + << frg::endlog; return 0; - }else if(layer == SOL_SOCKET && number == SO_PRIORITY) { - mlibc::infoLogger() << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_PRIORITY is unimplemented\e[39m" << frg::endlog; + } else if (layer == SOL_SOCKET && number == SO_PRIORITY) { + mlibc::infoLogger( + ) << "\e[31mmlibc: setsockopt() call with SOL_SOCKET and SO_PRIORITY is unimplemented\e[39m" + << frg::endlog; return 0; - }else{ - mlibc::panicLogger() << "\e[31mmlibc: Unexpected setsockopt() call, layer: " << layer << " number: " << number << "\e[39m" << frg::endlog; + } else { + mlibc::panicLogger() << "\e[31mmlibc: Unexpected setsockopt() call, layer: " << layer + << " number: " << number << "\e[39m" << frg::endlog; __builtin_unreachable(); } } @@ -555,11 +623,8 @@ int sys_listen(int fd, int) { req.SerializeToString(&ser); auto [offer, send_req, recv_resp] = exchangeMsgsSync( - handle, - helix_ng::offer( - helix_ng::sendBuffer(ser.data(), ser.size()), - helix_ng::recvInline() - ) + handle, + helix_ng::offer(helix_ng::sendBuffer(ser.data(), ser.size()), helix_ng::recvInline()) ); HEL_CHECK(offer.error()); @@ -572,4 +637,4 @@ int sys_listen(int fd, int) { return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/managarm/generic/time.cpp b/sysdeps/managarm/generic/time.cpp index 468a738b9b..6727189c14 100644 --- a/sysdeps/managarm/generic/time.cpp +++ b/sysdeps/managarm/generic/time.cpp @@ -1,15 +1,15 @@ #include -#include #include #include +#include -#include #include -#include +#include +#include #include +#include #include -#include struct TrackerPage { uint64_t seqlock; @@ -25,12 +25,13 @@ namespace mlibc { int sys_clock_get(int clock, time_t *secs, long *nanos) { // This implementation is inherently signal-safe. - if(clock == CLOCK_MONOTONIC || clock == CLOCK_MONOTONIC_RAW || clock == CLOCK_MONOTONIC_COARSE) { + if (clock == CLOCK_MONOTONIC || clock == CLOCK_MONOTONIC_RAW || + clock == CLOCK_MONOTONIC_COARSE) { uint64_t tick; HEL_CHECK(helGetClock(&tick)); *secs = tick / 1000000000; *nanos = tick % 1000000000; - }else if(clock == CLOCK_REALTIME) { + } else if (clock == CLOCK_REALTIME) { cacheFileTable(); // Start the seqlock read. @@ -48,22 +49,26 @@ int sys_clock_get(int clock, time_t *secs, long *nanos) { // Calculate the current time. uint64_t tick; HEL_CHECK(helGetClock(&tick)); - __ensure(tick >= (uint64_t)__mlibc_clk_tracker_page->refClock); // TODO: Respect the seqlock! + __ensure( + tick >= (uint64_t)__mlibc_clk_tracker_page->refClock + ); // TODO: Respect the seqlock! tick -= ref; tick += base; *secs = tick / 1000000000; *nanos = tick % 1000000000; - }else if(clock == CLOCK_PROCESS_CPUTIME_ID) { + } else if (clock == CLOCK_PROCESS_CPUTIME_ID) { mlibc::infoLogger() << "\e[31mmlibc: clock_gettime does not support the CPU time clocks" - "\e[39m" << frg::endlog; + "\e[39m" + << frg::endlog; *secs = 0; *nanos = 0; - }else if(clock == CLOCK_BOOTTIME) { + } else if (clock == CLOCK_BOOTTIME) { mlibc::infoLogger() << "\e[31mmlibc: clock_gettime does not support CLOCK_BOOTTIME" - "\e[39m" << frg::endlog; + "\e[39m" + << frg::endlog; *secs = 0; *nanos = 0; - }else{ + } else { mlibc::panicLogger() << "mlibc: Unexpected clock " << clock << frg::endlog; } return 0; @@ -77,5 +82,4 @@ int sys_clock_getres(int clock, time_t *secs, long *nanos) { return 0; } -} //namespace mlibc - +} // namespace mlibc diff --git a/sysdeps/managarm/include/mlibc/posix-pipe.hpp b/sysdeps/managarm/include/mlibc/posix-pipe.hpp index 4bcf1cba2c..120e7388d5 100644 --- a/sysdeps/managarm/include/mlibc/posix-pipe.hpp +++ b/sysdeps/managarm/include/mlibc/posix-pipe.hpp @@ -1,13 +1,13 @@ #ifndef MLIBC_POSIX_PIPE #define MLIBC_POSIX_PIPE +#include #include #include #include -#include -#include #include +#include struct SignalGuard { SignalGuard(); @@ -16,7 +16,7 @@ struct SignalGuard { ~SignalGuard(); - SignalGuard &operator= (const SignalGuard &) = delete; + SignalGuard &operator=(const SignalGuard &) = delete; }; // We need an allocator for message structs in sysdeps functions; the "normal" mlibc @@ -33,33 +33,24 @@ struct ElementHandle { swap(u._data, v._data); } - ElementHandle() - : _queue{nullptr}, _n{-1}, _data{nullptr} { } + ElementHandle() : _queue{nullptr}, _n{-1}, _data{nullptr} {} - ElementHandle(Queue *queue, int n, void *data) - : _queue{queue}, _n{n}, _data{data} { } + ElementHandle(Queue *queue, int n, void *data) : _queue{queue}, _n{n}, _data{data} {} ElementHandle(const ElementHandle &other); - ElementHandle(ElementHandle &&other) - : ElementHandle{} { - swap(*this, other); - } + ElementHandle(ElementHandle &&other) : ElementHandle{} { swap(*this, other); } ~ElementHandle(); - ElementHandle &operator= (ElementHandle other) { + ElementHandle &operator=(ElementHandle other) { swap(*this, other); return *this; } - void *data() { - return _data; - } + void *data() { return _data; } - void advance(size_t size) { - _data = reinterpret_cast(_data) + size; - } + void advance(size_t size) { _data = reinterpret_cast(_data) + size; } private: Queue *_queue; @@ -68,19 +59,20 @@ struct ElementHandle { }; struct Queue { - Queue() - : _handle{kHelNullHandle} { + Queue() : _handle{kHelNullHandle} { // We do not need to protect those allocations against signals as this constructor // is only called during library initialization. - _chunks[0] = reinterpret_cast(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096)); - _chunks[1] = reinterpret_cast(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096)); + _chunks[0] = + reinterpret_cast(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096)); + _chunks[1] = + reinterpret_cast(getSysdepsAllocator().allocate(sizeof(HelChunk) + 4096)); recreateQueue(); } Queue(const Queue &) = delete; - Queue &operator= (const Queue &) = delete; + Queue &operator=(const Queue &) = delete; void recreateQueue() { // Reset the internal queue state. @@ -89,12 +81,7 @@ struct Queue { _lastProgress = 0; // Setup the queue header. - HelQueueParameters params { - .flags = 0, - .ringShift = 1, - .numChunks = 2, - .chunkSize = 4096 - }; + HelQueueParameters params{.flags = 0, .ringShift = 1, .numChunks = 2, .chunkSize = 4096}; HEL_CHECK(helCreateQueue(¶ms, &_handle)); auto chunksOffset = (sizeof(HelQueue) + (sizeof(int) << 1) + 63) & ~size_t(63); @@ -102,13 +89,19 @@ struct Queue { auto overallSize = chunksOffset + params.numChunks * reservedPerChunk; void *mapping; - HEL_CHECK(helMapMemory(_handle, kHelNullHandle, nullptr, - 0, (overallSize + 0xFFF) & ~size_t(0xFFF), - kHelMapProtRead | kHelMapProtWrite, &mapping)); + HEL_CHECK(helMapMemory( + _handle, + kHelNullHandle, + nullptr, + 0, + (overallSize + 0xFFF) & ~size_t(0xFFF), + kHelMapProtRead | kHelMapProtWrite, + &mapping + )); _queue = reinterpret_cast(mapping); auto chunksPtr = reinterpret_cast(mapping) + chunksOffset; - for(unsigned int i = 0; i < 2; ++i) + for (unsigned int i = 0; i < 2; ++i) _chunks[i] = reinterpret_cast(chunksPtr + i * reservedPerChunk); // Reset and enqueue the chunks. @@ -124,14 +117,12 @@ struct Queue { _wakeHeadFutex(); } - HelHandle getQueue() { - return _handle; - } + HelHandle getQueue() { return _handle; } + + void trim() {} - void trim() { } - ElementHandle dequeueSingle() { - while(true) { + while (true) { __ensure(_retrieveIndex != _nextIndex); bool done; @@ -140,7 +131,7 @@ struct Queue { auto n = _numberOf(_retrieveIndex); __ensure(_refCount[n]); - if(done) { + if (done) { retire(n); _lastProgress = 0; @@ -159,7 +150,7 @@ struct Queue { void retire(int n) { __ensure(_refCount[n]); - if(_refCount[n]-- > 1) + if (_refCount[n]-- > 1) return; // Reset and enqueue the chunk again. @@ -171,46 +162,46 @@ struct Queue { _wakeHeadFutex(); } - void reference(int n) { - _refCount[n]++; - } + void reference(int n) { _refCount[n]++; } private: - int _numberOf(int index) { - return _queue->indexQueue[index & 1]; - } + int _numberOf(int index) { return _queue->indexQueue[index & 1]; } - HelChunk *_retrieveChunk() { - return _chunks[_numberOf(_retrieveIndex)]; - } + HelChunk *_retrieveChunk() { return _chunks[_numberOf(_retrieveIndex)]; } void _wakeHeadFutex() { auto futex = __atomic_exchange_n(&_queue->headFutex, _nextIndex, __ATOMIC_RELEASE); - if(futex & kHelHeadWaiters) + if (futex & kHelHeadWaiters) HEL_CHECK(helFutexWake(&_queue->headFutex)); } void _waitProgressFutex(bool *done) { - while(true) { + while (true) { auto futex = __atomic_load_n(&_retrieveChunk()->progressFutex, __ATOMIC_ACQUIRE); __ensure(!(futex & ~(kHelProgressMask | kHelProgressWaiters | kHelProgressDone))); do { - if(_lastProgress != (futex & kHelProgressMask)) { + if (_lastProgress != (futex & kHelProgressMask)) { *done = false; return; - }else if(futex & kHelProgressDone) { + } else if (futex & kHelProgressDone) { *done = true; return; } - if(futex & kHelProgressWaiters) + if (futex & kHelProgressWaiters) break; // Waiters bit is already set (in a previous iteration). - } while(!__atomic_compare_exchange_n(&_retrieveChunk()->progressFutex, &futex, - _lastProgress | kHelProgressWaiters, - false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)); - - HEL_CHECK(helFutexWait(&_retrieveChunk()->progressFutex, - _lastProgress | kHelProgressWaiters, -1)); + } while (!__atomic_compare_exchange_n( + &_retrieveChunk()->progressFutex, + &futex, + _lastProgress | kHelProgressWaiters, + false, + __ATOMIC_ACQUIRE, + __ATOMIC_ACQUIRE + )); + + HEL_CHECK(helFutexWait( + &_retrieveChunk()->progressFutex, _lastProgress | kHelProgressWaiters, -1 + )); } } @@ -218,7 +209,7 @@ struct Queue { HelHandle _handle; HelQueue *_queue; HelChunk *_chunks[2]; - + // Index of the chunk that we are currently retrieving/inserting next. int _retrieveIndex; int _nextIndex; @@ -231,7 +222,7 @@ struct Queue { }; inline ElementHandle::~ElementHandle() { - if(_queue) + if (_queue) _queue->retire(_n); } @@ -243,7 +234,6 @@ inline ElementHandle::ElementHandle(const ElementHandle &other) { _queue->reference(_n); } - inline HelSimpleResult *parseSimple(ElementHandle &element) { auto result = reinterpret_cast(element.data()); element.advance(sizeof(HelSimpleResult)); @@ -278,23 +268,23 @@ extern thread_local Queue globalQueue; // This include is here because it needs ElementHandle to be declared #include -template +template auto exchangeMsgsSync(HelHandle descriptor, Args &&...args) { auto results = helix_ng::createResultsTuple(args...); auto actions = helix_ng::chainActionArrays(args...); - HEL_CHECK(helSubmitAsync(descriptor, actions.data(), - actions.size(), globalQueue.getQueue(), 0, 0)); + HEL_CHECK( + helSubmitAsync(descriptor, actions.data(), actions.size(), globalQueue.getQueue(), 0, 0) + ); auto element = globalQueue.dequeueSingle(); void *ptr = element.data(); - [&](std::index_sequence) { + [&](std::index_sequence) { (results.template get

().parse(ptr, element), ...); - } (std::make_index_sequence>{}); + }(std::make_index_sequence>{}); return results; } - #endif // MLIBC_POSIX_PIPE diff --git a/sysdeps/managarm/include/mlibc/thread-entry.hpp b/sysdeps/managarm/include/mlibc/thread-entry.hpp index 2dd88a6461..0154307c4a 100644 --- a/sysdeps/managarm/include/mlibc/thread-entry.hpp +++ b/sysdeps/managarm/include/mlibc/thread-entry.hpp @@ -6,5 +6,5 @@ extern "C" void __mlibc_start_thread(void); extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb); namespace mlibc { - void *prepare_stack(void *entry, void *user_arg, void *tcb); +void *prepare_stack(void *entry, void *user_arg, void *tcb); } diff --git a/sysdeps/managarm/riscv64/thread.cpp b/sysdeps/managarm/riscv64/thread.cpp index e2ffc2a74c..9f006ac426 100644 --- a/sysdeps/managarm/riscv64/thread.cpp +++ b/sysdeps/managarm/riscv64/thread.cpp @@ -1,9 +1,5 @@ #include -extern "C" void __mlibc_start_thread() { - __ensure(!"unimplemented"); -} +extern "C" void __mlibc_start_thread() { __ensure(!"unimplemented"); } -extern "C" void __mlibc_signal_restore() { - __ensure(!"unimplemented"); -} +extern "C" void __mlibc_signal_restore() { __ensure(!"unimplemented"); } diff --git a/sysdeps/managarm/rtld-generic/support.cpp b/sysdeps/managarm/rtld-generic/support.cpp index ce383074bb..cda3dfc172 100644 --- a/sysdeps/managarm/rtld-generic/support.cpp +++ b/sysdeps/managarm/rtld-generic/support.cpp @@ -1,17 +1,17 @@ +#include #include #include #include -#include #include #include -#include #include +#include -#include +#include #include +#include #include -#include #include #include @@ -24,16 +24,18 @@ HelHandle posixLane; HelHandle *fileTable; void cacheFileTable() { - if(fileTable) + if (fileTable) return; posix::ManagarmProcessData data; - HEL_CHECK(helSyscall1(kHelCallSuper + posix::superGetProcessData, reinterpret_cast(&data))); + HEL_CHECK( + helSyscall1(kHelCallSuper + posix::superGetProcessData, reinterpret_cast(&data)) + ); posixLane = data.posixLane; fileTable = data.fileTable; } -template +template T load(void *ptr) { T result; memcpy(&result, ptr, sizeof(T)); @@ -43,14 +45,8 @@ T load(void *ptr) { // This Queue implementation is more simplistic than the ones in mlibc and helix. // In fact, we only manage a single chunk; this minimizes the memory usage of the queue. struct Queue { - Queue() - : _handle{kHelNullHandle}, _lastProgress(0) { - HelQueueParameters params { - .flags = 0, - .ringShift = 0, - .numChunks = 1, - .chunkSize = 4096 - }; + Queue() : _handle{kHelNullHandle}, _lastProgress(0) { + HelQueueParameters params{.flags = 0, .ringShift = 0, .numChunks = 1, .chunkSize = 4096}; HEL_CHECK(helCreateQueue(¶ms, &_handle)); auto chunksOffset = (sizeof(HelQueue) + (sizeof(int) << 0) + 63) & ~size_t(63); @@ -58,13 +54,19 @@ struct Queue { auto overallSize = chunksOffset + params.numChunks * reservedPerChunk; void *mapping; - HEL_CHECK(helMapMemory(_handle, kHelNullHandle, nullptr, - 0, (overallSize + 0xFFF) & ~size_t(0xFFF), - kHelMapProtRead | kHelMapProtWrite, &mapping)); + HEL_CHECK(helMapMemory( + _handle, + kHelNullHandle, + nullptr, + 0, + (overallSize + 0xFFF) & ~size_t(0xFFF), + kHelMapProtRead | kHelMapProtWrite, + &mapping + )); _queue = reinterpret_cast(mapping); - _chunk = reinterpret_cast( - reinterpret_cast(mapping) + chunksOffset); + _chunk = + reinterpret_cast(reinterpret_cast(mapping) + chunksOffset); // Reset and enqueue the first chunk. _chunk->progressFutex = 0; @@ -77,17 +79,15 @@ struct Queue { Queue(const Queue &) = delete; - Queue &operator= (const Queue &) = delete; + Queue &operator=(const Queue &) = delete; - HelHandle getHandle() { - return _handle; - } + HelHandle getHandle() { return _handle; } void *dequeueSingle() { - while(true) { + while (true) { bool done; _waitProgressFutex(&done); - if(done) { + if (done) { // Reset and enqueue the chunk again. _chunk->progressFutex = 0; @@ -110,31 +110,36 @@ struct Queue { private: void _wakeHeadFutex() { auto futex = __atomic_exchange_n(&_queue->headFutex, _nextIndex, __ATOMIC_RELEASE); - if(futex & kHelHeadWaiters) + if (futex & kHelHeadWaiters) HEL_CHECK(helFutexWake(&_queue->headFutex)); } void _waitProgressFutex(bool *done) { - while(true) { + while (true) { auto futex = __atomic_load_n(&_chunk->progressFutex, __ATOMIC_ACQUIRE); __ensure(!(futex & ~(kHelProgressMask | kHelProgressWaiters | kHelProgressDone))); do { - if(_lastProgress != (futex & kHelProgressMask)) { + if (_lastProgress != (futex & kHelProgressMask)) { *done = false; return; - }else if(futex & kHelProgressDone) { + } else if (futex & kHelProgressDone) { *done = true; return; } - if(futex & kHelProgressWaiters) + if (futex & kHelProgressWaiters) break; // Waiters bit is already set (in a previous iteration). - } while(!__atomic_compare_exchange_n(&_chunk->progressFutex, &futex, - _lastProgress | kHelProgressWaiters, - false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE)); - - HEL_CHECK(helFutexWait(&_chunk->progressFutex, - _lastProgress | kHelProgressWaiters, -1)); + } while (!__atomic_compare_exchange_n( + &_chunk->progressFutex, + &futex, + _lastProgress | kHelProgressWaiters, + false, + __ATOMIC_ACQUIRE, + __ATOMIC_ACQUIRE + )); + + HEL_CHECK(helFutexWait(&_chunk->progressFutex, _lastProgress | kHelProgressWaiters, -1) + ); } } @@ -156,8 +161,7 @@ HelSimpleResult *parseSimple(void *&element) { HelInlineResult *parseInline(void *&element) { auto result = reinterpret_cast(element); - element = (char *)element + sizeof(HelInlineResult) - + ((result->length + 7) & ~size_t(7)); + element = (char *)element + sizeof(HelInlineResult) + ((result->length + 7) & ~size_t(7)); return result; } @@ -181,10 +185,10 @@ int sys_tcb_set(void *pointer) { #elif defined(__aarch64__) uintptr_t addr = reinterpret_cast(pointer); addr += sizeof(Tcb) - 0x10; - asm volatile ("msr tpidr_el0, %0" :: "r"(addr)); + asm volatile("msr tpidr_el0, %0" ::"r"(addr)); #elif defined(__riscv) && __riscv_xlen == 64 uintptr_t tp = reinterpret_cast(pointer) + sizeof(Tcb); - asm volatile ("mv tp, %0" : : "r"(tp) : "memory"); + asm volatile("mv tp, %0" : : "r"(tp) : "memory"); #else #error Unknown architecture #endif @@ -201,7 +205,7 @@ int sys_open(const char *path, int flags, mode_t mode, int *fd) { req.set_mode(mode); req.set_path(frg::string(getAllocator(), path)); - if(!globalQueue.valid()) + if (!globalQueue.valid()) globalQueue.initialize(); frg::string head(getAllocator()); @@ -242,7 +246,7 @@ int sys_open(const char *path, int flags, mode_t mode, int *fd) { managarm::posix::SvrResponse resp(getAllocator()); resp.ParseFromArray(recv_resp->data, recv_resp->length); - if(resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) + if (resp.error() == managarm::posix::Errors::FILE_NOT_FOUND) return -1; __ensure(resp.error() == managarm::posix::Errors::SUCCESS); *fd = resp.fd(); @@ -260,7 +264,7 @@ int sys_seek(int fd, off_t offset, int whence, off_t *new_offset) { req.set_req_type(managarm::fs::CntReqType::SEEK_ABS); req.set_rel_offset(offset); - if(!globalQueue.valid()) + if (!globalQueue.valid()) globalQueue.initialize(); frg::string ser(getAllocator()); @@ -299,7 +303,7 @@ int sys_read(int fd, void *data, size_t length, ssize_t *bytes_read) { req.set_req_type(managarm::fs::CntReqType::READ); req.set_size(length); - if(!globalQueue.valid()) + if (!globalQueue.valid()) globalQueue.initialize(); frg::string ser(getAllocator()); @@ -352,7 +356,7 @@ int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offse req.set_fd(fd); req.set_rel_offset(offset); - if(!globalQueue.valid()) + if (!globalQueue.valid()) globalQueue.initialize(); frg::string ser(getAllocator()); @@ -365,8 +369,7 @@ int sys_vm_map(void *hint, size_t size, int prot, int flags, int fd, off_t offse actions[1].length = ser.size(); actions[2].type = kHelActionRecvInline; actions[2].flags = 0; - HEL_CHECK(helSubmitAsync(posixLane, actions, 3, - globalQueue->getHandle(), 0, 0)); + HEL_CHECK(helSubmitAsync(posixLane, actions, 3, globalQueue->getHandle(), 0, 0)); auto element = globalQueue->dequeueSingle(); auto offer = parseHandle(element); @@ -391,7 +394,7 @@ int sys_close(int fd) { managarm::posix::CloseRequest req(getAllocator()); req.set_fd(fd); - if(!globalQueue.valid()) + if (!globalQueue.valid()) globalQueue.initialize(); frg::string ser(getAllocator()); @@ -422,30 +425,28 @@ int sys_close(int fd) { int sys_futex_tid() { HelWord tid = 0; - HEL_CHECK(helSyscall0_1(kHelCallSuper + posix::superGetTid, - &tid)); + HEL_CHECK(helSyscall0_1(kHelCallSuper + posix::superGetTid, &tid)); return tid; } int sys_futex_wait(int *pointer, int expected, const struct timespec *time) { // This implementation is inherently signal-safe. - if(time) { - if(helFutexWait(pointer, expected, time->tv_nsec + time->tv_sec * 1000000000)) + if (time) { + if (helFutexWait(pointer, expected, time->tv_nsec + time->tv_sec * 1000000000)) return -1; return 0; } - if(helFutexWait(pointer, expected, -1)) + if (helFutexWait(pointer, expected, -1)) return -1; return 0; } int sys_futex_wake(int *pointer) { // This implementation is inherently signal-safe. - if(helFutexWake(pointer)) + if (helFutexWake(pointer)) return -1; return 0; } - } // namespace mlibc diff --git a/sysdeps/managarm/x86_64/thread.cpp b/sysdeps/managarm/x86_64/thread.cpp index 88f1bf3588..129267d3e7 100644 --- a/sysdeps/managarm/x86_64/thread.cpp +++ b/sysdeps/managarm/x86_64/thread.cpp @@ -1,18 +1,18 @@ -#include -#include -#include #include -#include #include -#include +#include +#include +#include #include +#include +#include extern "C" void __mlibc_enter_thread(void *entry, void *user_arg, Tcb *tcb) { // Wait until our parent sets up the TID. - while(!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED)) + while (!__atomic_load_n(&tcb->tid, __ATOMIC_RELAXED)) mlibc::sys_futex_wait(&tcb->tid, 0, nullptr); - if(mlibc::sys_tcb_set(tcb)) + if (mlibc::sys_tcb_set(tcb)) __ensure(!"sys_tcb_set() failed"); tcb->invokeThreadFunc(entry, user_arg); @@ -29,7 +29,15 @@ namespace mlibc { static constexpr size_t default_stacksize = 0x200000; -int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size_t *stack_size, size_t *guard_size, void **stack_base) { +int sys_prepare_stack( + void **stack, + void *entry, + void *user_arg, + void *tcb, + size_t *stack_size, + size_t *guard_size, + void **stack_base +) { if (!*stack_size) *stack_size = default_stacksize; *guard_size = 0; @@ -37,20 +45,21 @@ int sys_prepare_stack(void **stack, void *entry, void *user_arg, void *tcb, size if (*stack) { *stack_base = *stack; } else { - *stack_base = mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if(*stack_base == MAP_FAILED) { + *stack_base = + mmap(nullptr, *stack_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (*stack_base == MAP_FAILED) { return errno; } } - - uintptr_t *sp = reinterpret_cast(reinterpret_cast(*stack_base) + *stack_size); + + uintptr_t *sp = + reinterpret_cast(reinterpret_cast(*stack_base) + *stack_size); *--sp = reinterpret_cast(tcb); *--sp = reinterpret_cast(user_arg); *--sp = reinterpret_cast(entry); - *stack = reinterpret_cast(sp); + *stack = reinterpret_cast(sp); return 0; } -} //namespace mlibc +} // namespace mlibc diff --git a/sysdeps/vinix/.clang-format b/sysdeps/vinix/.clang-format new file mode 100644 index 0000000000..46626920cf --- /dev/null +++ b/sysdeps/vinix/.clang-format @@ -0,0 +1,2 @@ +--- +DisableFormat: true