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.
This commit is contained in:
Liav A 2023-02-05 12:27:38 +02:00 committed by Linus Groh
parent df72c9327b
commit ab27fce86f
Notes: sideshowbarker 2024-07-17 20:33:50 +09:00
4 changed files with 34 additions and 0 deletions

View file

@ -4,6 +4,7 @@ set(AK_SOURCES
CircularBuffer.cpp
DeprecatedFlyString.cpp
DeprecatedString.cpp
Error.cpp
FloatingPointStringConversions.cpp
FlyString.cpp
Format.cpp

25
AK/Error.cpp Normal file
View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Error.h>
#ifdef KERNEL
# include <AK/Format.h>
#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
}
}

View file

@ -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); }

View file

@ -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