mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 01:10:28 +03:00
AK+LibSystem+LibMain: Add Error::from_syscall() for syscall failures
This creates an error that contains the name of the syscall that failed. This allows error handlers to print out the name of the call if they want to. :^)
This commit is contained in:
parent
d3cf68a540
commit
4e530135d5
Notes:
sideshowbarker
2024-07-18 00:52:47 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/4e530135d55
10
AK/Error.h
10
AK/Error.h
@ -21,9 +21,11 @@ namespace AK {
|
||||
class Error {
|
||||
public:
|
||||
static Error from_errno(int code) { return Error(code); }
|
||||
static Error from_syscall(StringView syscall_name, int rc) { return Error(syscall_name, rc); }
|
||||
static Error from_string_literal(StringView string_literal) { return Error(string_literal); }
|
||||
|
||||
bool is_errno() const { return m_code != 0; }
|
||||
bool is_syscall() const { return m_syscall; }
|
||||
|
||||
int code() const { return m_code; }
|
||||
StringView string_literal() const { return m_string_literal; }
|
||||
@ -40,8 +42,16 @@ private:
|
||||
{
|
||||
}
|
||||
|
||||
Error(StringView syscall_name, int rc)
|
||||
: m_code(-rc)
|
||||
, m_string_literal(syscall_name)
|
||||
, m_syscall(true)
|
||||
{
|
||||
}
|
||||
|
||||
int m_code { 0 };
|
||||
StringView m_string_literal;
|
||||
bool m_syscall { false };
|
||||
};
|
||||
|
||||
template<typename T, typename ErrorType>
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
@ -22,7 +23,13 @@ int main(int argc, char** argv)
|
||||
.arguments = arguments.span(),
|
||||
});
|
||||
if (result.is_error()) {
|
||||
warnln("Runtime error: {}", result.error());
|
||||
auto error = result.release_error();
|
||||
if (error.is_syscall())
|
||||
warnln("Runtime error: {}: {} (errno={})", error.string_literal(), strerror(error.code()), error.code());
|
||||
else if (error.is_errno())
|
||||
warnln("Runtime error: {} (errno={})", strerror(error.code()), error.code());
|
||||
else
|
||||
warnln("Runtime error: {}", error.string_literal());
|
||||
return 1;
|
||||
}
|
||||
return result.value();
|
||||
|
@ -7,6 +7,12 @@
|
||||
#include <LibSystem/Wrappers.h>
|
||||
#include <LibSystem/syscall.h>
|
||||
|
||||
#define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc) \
|
||||
if ((rc) < 0) { \
|
||||
return Error::from_syscall(syscall_name, rc); \
|
||||
} \
|
||||
return {};
|
||||
|
||||
namespace System {
|
||||
|
||||
ErrorOr<void> pledge(StringView promises, StringView execpromises)
|
||||
@ -16,9 +22,7 @@ ErrorOr<void> pledge(StringView promises, StringView execpromises)
|
||||
{ execpromises.characters_without_null_termination(), execpromises.length() },
|
||||
};
|
||||
int rc = syscall(SC_pledge, ¶ms);
|
||||
if (rc < 0)
|
||||
return Error::from_errno(-rc);
|
||||
return {};
|
||||
HANDLE_SYSCALL_RETURN_VALUE("pledge"sv, rc);
|
||||
}
|
||||
|
||||
ErrorOr<void> unveil(StringView path, StringView permissions)
|
||||
@ -28,9 +32,7 @@ ErrorOr<void> unveil(StringView path, StringView permissions)
|
||||
{ permissions.characters_without_null_termination(), permissions.length() },
|
||||
};
|
||||
int rc = syscall(SC_unveil, ¶ms);
|
||||
if (rc < 0)
|
||||
return Error::from_errno(-rc);
|
||||
return {};
|
||||
HANDLE_SYSCALL_RETURN_VALUE("unveil"sv, rc);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user