From ab27fce86f18341d463bc921a2071aefc0071a93 Mon Sep 17 00:00:00 2001 From: Liav A Date: Sun, 5 Feb 2023 12:27:38 +0200 Subject: [PATCH] AK: Add a new method to propagate errno while printing errors in Kernel This new method is meant to be used in both userspace and kernel code. The idea is to allow printing of a verbose message and then returning an errno code which is the proper mechanism for kernel code because we should almost always assume that such error will be propagated back to userspace in some way, so the userspace code could reasonably decode it. For userspace code however, this new method is meant to be a simple wrapper for Error::from_string_view, because for most invocations, it's much more useful to have a verbose & literal error than a errno code, so we simply ignore that errno code completely in such context. --- AK/CMakeLists.txt | 1 + AK/Error.cpp | 25 +++++++++++++++++++++++++ AK/Error.h | 6 ++++++ Kernel/CMakeLists.txt | 2 ++ 4 files changed, 34 insertions(+) create mode 100644 AK/Error.cpp diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 2988ada7ff..8ce72a78c9 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -4,6 +4,7 @@ set(AK_SOURCES CircularBuffer.cpp DeprecatedFlyString.cpp DeprecatedString.cpp + Error.cpp FloatingPointStringConversions.cpp FlyString.cpp Format.cpp diff --git a/AK/Error.cpp b/AK/Error.cpp new file mode 100644 index 0000000000..df2e9a1986 --- /dev/null +++ b/AK/Error.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023, Liav A. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#ifdef KERNEL +# include +#endif + +namespace AK { + +Error Error::from_string_view_or_print_error_and_return_errno(StringView string_literal, [[maybe_unused]] int code) +{ +#ifdef KERNEL + dmesgln("{}", string_literal); + return Error::from_errno(code); +#else + return Error::from_string_view(string_literal); +#endif +} + +} diff --git a/AK/Error.h b/AK/Error.h index b1c29b74d5..ad21cf6075 100644 --- a/AK/Error.h +++ b/AK/Error.h @@ -25,6 +25,12 @@ public: ALWAYS_INLINE Error& operator=(Error&&) = default; [[nodiscard]] static Error from_errno(int code) { return Error(code); } + + // NOTE: For calling this method from within kernel code, we will simply print + // the error message and return the errno code. + // For calling this method from userspace programs, we will simply return from + // the Error::from_string_view method! + [[nodiscard]] static Error from_string_view_or_print_error_and_return_errno(StringView string_literal, int code); [[nodiscard]] static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); } [[nodiscard]] static Error from_string_view(StringView string_literal) { return Error(string_literal); } diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index f3cc888a7f..0bf86a0d6d 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -489,6 +489,7 @@ set(AK_SOURCES ../AK/StringUtils.cpp ../AK/StringView.cpp ../AK/Time.cpp + ../AK/Error.cpp ../AK/Format.cpp ../AK/UUID.cpp ) @@ -626,6 +627,7 @@ if (ENABLE_KERNEL_COVERAGE_COLLECTION) set(KCOV_EXCLUDED_SOURCES # Make sure we don't instrument any code called from __sanitizer_cov_trace_pc # otherwise we'll end up with recursive calls to that function. + ../AK/Error.cpp ../AK/Format.cpp ../AK/StringBuilder.cpp ../Kernel/Arch/x86_64/Processor.cpp