Files

134 lines
3.8 KiB
C++

/*
* statuscodes.h
* MontaukOS status codes
* Copyright (c) 2026 Daniel Hammer
*/
#pragma once
// Userspace status codes.
// Analogous to kernel/src/Common/StatusCodes.hpp, but positive numbers are used.
#ifdef __cplusplus
namespace montauk {
#endif
typedef enum {
Success = 0,
GenericFailure = 1,
PermissionDenied = 2,
CapabilityDenied = 3,
SystemIntegrityError = 4,
BadPointer = 5,
InvalidArgument = 6,
UnsupportedOp = 7,
NoDevice = 8,
IoFailure = 9,
BadInvocation = 10,
NoSuchPath = 11,
Busy = 12,
BufferTooSmall = 13,
OutOfMemory = 14,
TimedOut = 15,
WouldBlock = 16
} StatusCode;
// Highest defined StatusCode; update when adding a code above.
enum { MaxStatusCode = WouldBlock };
inline const char* GetStatusText(StatusCode statusCode) {
const char* statusText[] = {
"Success", // 0
"Generic failure", // 1
"Permission denied", // 2
"Capability denied", // 3
"System integrity error", // 4
"Bad pointer", // 5
"Invalid argument", // 6
"Unsupported operation", // 7
"No device", // 8
"I/O failure", // 9
"Bad invocation", // 10
"No such path", // 11
"Busy", // 12
"Buffer too small", // 13
"Out of memory", // 14
"Timed out", // 15
"Operation would block", // 16
};
if (statusCode > 0 && statusCode <= MaxStatusCode) {
return statusText[statusCode];
} else {
return "Unknown error";
}
}
// Map a raw syscall result to a StatusCode.
inline StatusCode StatusFromResult(long long rc) {
if (rc >= 0) return Success;
if (rc < -(long long)MaxStatusCode) return GenericFailure;
return (StatusCode)(-rc);
}
// Shared with C (libc.c), so no nullptr.
inline const char* GetStatusExplanationText(StatusCode statusCode) {
const char* explanationText[] = {
0, // 0
0, // 1
0, // 2
0, // 3
"The path is protected by the system and cannot be modified.", // 4
0, // 5
0, // 6
0, // 7
0, // 8
0, // 9
0, // 10
0, // 11
0, // 12
"The buffer supplied by the caller is too small.", // 13
0, // 14
0, // 15
0, // 16
};
if (statusCode > 0 && statusCode <= MaxStatusCode) {
if (explanationText[statusCode] != 0) {
return explanationText[statusCode];
}
}
return GetStatusText(statusCode);
}
// Process termination codes: what waitpid returns when the kernel ended the
// process (crash, kill, abort) instead of it calling exit. 256 + POSIX
// signal number, so normal exit codes (0..255) can never collide.
// Analogous to kernel/src/Common/TerminationCodes.hpp; same values.
typedef enum {
TerminationBase = 256,
TerminatedIllegalInstruction = TerminationBase + 4, // SIGILL
TerminatedBreakpoint = TerminationBase + 5, // SIGTRAP
TerminatedAborted = TerminationBase + 6, // SIGABRT
TerminatedArithmeticError = TerminationBase + 8, // SIGFPE
TerminatedKilled = TerminationBase + 9, // SIGKILL
TerminatedAccessViolation = TerminationBase + 11 // SIGSEGV
} TerminationCode;
// Shared with C (libc.c), so no nullptr. Returns 0 for an unknown code.
inline const char* GetTerminationText(int exitCode) {
switch (exitCode) {
case TerminatedIllegalInstruction: return "Illegal instruction";
case TerminatedBreakpoint: return "Breakpoint";
case TerminatedAborted: return "Aborted";
case TerminatedArithmeticError: return "Arithmetic error";
case TerminatedKilled: return "Killed";
case TerminatedAccessViolation: return "Access violation";
default: return 0;
}
}
#ifdef __cplusplus
}
#endif