feat: make userspace StatusCodes positive; print status text along with code in shell exec; make init return BadInvocation on faulty launch

This commit is contained in:
daniel committed 2026-09-30 16:21:32 +02:00
1 parent 8a3ffb2be1
commit 5e4e8902c2
6 files changed
+95 -40

No files matched your search

+1 -1
View File
@@ -12,4 +12,4 @@
#pragma once #pragma once
#define MONTAUK_BUILD_NUMBER 254 #define MONTAUK_BUILD_NUMBER 264
+2 -3
View File
@@ -6,9 +6,8 @@
#pragma once #pragma once
// Syscall return status. Mirrored for userspace in // Syscall return status.
// programs/include/montauk/statuscodes.h and, as MTK_STATUS_*, in // Mirrored in userspace with positive values at programs/include/montauk/statuscodes.h.
// programs/include/libc/montauk.h; scripts/montauk-syscalls.py checks all three.
enum StatusCodes { enum StatusCodes {
Success = 0, Success = 0,
GenericFailure = -1, GenericFailure = -1,
+71 -17
View File
@@ -6,30 +6,84 @@
#pragma once #pragma once
// Userspace mirror of kernel/src/Common/StatusCodes.hpp. Also usable from C // Userspace status codes.
// (the libc includes it), where the enumerators are unqualified. // Analogous to kernel/src/Common/StatusCodes.hpp, but positive numbers are used.
#ifdef __cplusplus #ifdef __cplusplus
namespace montauk { namespace montauk {
#endif #endif
enum StatusCodes { typedef enum {
Success = 0, Success = 0,
GenericFailure = -1, GenericFailure = 1,
PermissionDenied = -2, PermissionDenied = 2,
CapabilityDenied = -3, CapabilityDenied = 3,
SystemIntegrityError = -4, SystemIntegrityError = 4,
BadPointer = -5, BadPointer = 5,
InvalidArgument = -6, InvalidArgument = 6,
UnsupportedOp = -7, UnsupportedOp = 7,
NoDevice = -8, NoDevice = 8,
IoFailure = -9, IoFailure = 9,
BadInvocation = -10, BadInvocation = 10,
NoSuchPath = -11, NoSuchPath = 11,
Busy = -12, Busy = 12,
BufferTooSmall = -13, BufferTooSmall = 13,
OutOfMemory = -14 OutOfMemory = 14
} StatusCodes;
inline const char* GetStatusText(StatusCodes 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
}; };
if (statusCode > 0 && statusCode <= 14) {
return statusText[statusCode];
} else {
return "Unknown error";
}
}
inline const char* GetStatusExplanationText(StatusCodes statusCode) {
const char* explanationText[] = {
nullptr, // 0
nullptr, // 1
nullptr, // 2
nullptr, // 3
"The path is protected by the system and can't be modified.", // 4
nullptr, // 5
nullptr, // 6
nullptr, // 7
nullptr, // 8
nullptr, // 9
nullptr, // 10
nullptr, // 11
nullptr, // 12
"The buffer supplied by the caller is too small.", // 13
nullptr, // 14
};
if (statusCode > 0 && statusCode <= 14) {
if (explanationText[statusCode] != nullptr) {
return explanationText[statusCode];
}
}
return GetStatusText(statusCode);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
+2 -1
View File
@@ -8,6 +8,7 @@
#include <montauk/syscall.h> #include <montauk/syscall.h>
#include <montauk/config.h> #include <montauk/config.h>
#include <montauk/capabilities.h> #include <montauk/capabilities.h>
#include <montauk/statuscodes.h>
#include <libc/stdio.h> #include <libc/stdio.h>
// ---- ANSI color codes ---- // ---- ANSI color codes ----
@@ -204,7 +205,7 @@ const char* getReleaseString(char* buffer, int n) {
extern "C" void _start() { extern "C" void _start() {
if (system_already_initialized()) { if (system_already_initialized()) {
montauk::exit(1); montauk::exit(montauk::StatusCodes::BadInvocation);
} }
char releaseString[128] = ""; char releaseString[128] = "";
+13 -1
View File
@@ -17,7 +17,9 @@ static bool file_exists(const char* path) {
// ---- Try to spawn an ELF at the given path ---- // ---- Try to spawn an ELF at the given path ----
static void print_exit_code(int code) { static void print_exit_code(const int exitCode) {
int code = exitCode;
if (code == 0) return; if (code == 0) return;
char buf[16]; char buf[16];
int n = 0; int n = 0;
@@ -36,6 +38,16 @@ static void print_exit_code(int code) {
char c[2] = { buf[--n], 0 }; char c[2] = { buf[--n], 0 };
montauk::print(c); montauk::print(c);
} }
if (exitCode > 0 && exitCode <= 14) { // 0-14 - current range of status codes within StatusCodes enum
const char* statusText = montauk::GetStatusText((montauk::StatusCodes)exitCode);
if (statusText != nullptr) {
montauk::print(" - ");
montauk::print(statusText);
}
}
montauk::print("]\n"); montauk::print("]\n");
} }
+6 -17
View File
@@ -4,7 +4,6 @@
# #
# Source of truth : programs/include/Api/Syscall.hpp (montauk::abi::SYS_*) # Source of truth : programs/include/Api/Syscall.hpp (montauk::abi::SYS_*)
# kernel/src/Common/StatusCodes.hpp (enum StatusCodes) # kernel/src/Common/StatusCodes.hpp (enum StatusCodes)
# Mirrors : programs/include/montauk/statuscodes.h (must match exactly)
# Generated blocks: programs/include/libc/montauk.h (#define MTK_SYS_*, # Generated blocks: programs/include/libc/montauk.h (#define MTK_SYS_*,
# #define MTK_STATUS_*) # #define MTK_STATUS_*)
# #
@@ -16,13 +15,11 @@
# #
# Usage: # Usage:
# montauk-syscalls.py check Verify every SYS_* has a matching MTK_SYS_* with # montauk-syscalls.py check Verify every SYS_* has a matching MTK_SYS_* with
# the same number, and that both status code # the same number, and that MTK_STATUS_* matches
# mirrors match the kernel enum. Exits non-zero on # the kernel enum. Exits non-zero on any drift.
# any drift. Run from the build so stale headers # Run from the build so stale headers fail loudly.
# fail loudly.
# montauk-syscalls.py gen Rewrite the MTK_SYS_* and MTK_STATUS_* blocks in # montauk-syscalls.py gen Rewrite the MTK_SYS_* and MTK_STATUS_* blocks in
# montauk.h (numbers only; wrappers untouched). # montauk.h (numbers only; wrappers untouched).
# statuscodes.h is not generated; fix it by hand.
import os import os
import re import re
@@ -33,7 +30,6 @@ REPO_ROOT = os.path.dirname(SCRIPT_DIR)
ABI_HDR = os.path.join(REPO_ROOT, "programs/include/Api/Syscall.hpp") ABI_HDR = os.path.join(REPO_ROOT, "programs/include/Api/Syscall.hpp")
C_HDR = os.path.join(REPO_ROOT, "programs/include/libc/montauk.h") C_HDR = os.path.join(REPO_ROOT, "programs/include/libc/montauk.h")
STATUS_HDR = os.path.join(REPO_ROOT, "kernel/src/Common/StatusCodes.hpp") STATUS_HDR = os.path.join(REPO_ROOT, "kernel/src/Common/StatusCodes.hpp")
STATUS_MIRROR = os.path.join(REPO_ROOT, "programs/include/montauk/statuscodes.h")
BEGIN_MARKER = "/* @SYSCALLS-BEGIN" BEGIN_MARKER = "/* @SYSCALLS-BEGIN"
END_MARKER = "/* @SYSCALLS-END */" END_MARKER = "/* @SYSCALLS-END */"
@@ -91,16 +87,9 @@ def status_macro(name):
def check_status(c_text): def check_status(c_text):
"""Print drift between the kernel enum and its two mirrors; return count.""" """Print drift between the kernel enum and MTK_STATUS_*; return count."""
canon = parse_status(STATUS_HDR) canon = parse_status(STATUS_HDR)
problems = 0 problems = 0
mirror = parse_status(STATUS_MIRROR)
if mirror != canon:
problems += 1
print("montauk-syscalls: DRIFT between Common/StatusCodes.hpp and "
"montauk/statuscodes.h", file=sys.stderr)
print(" kernel: %s" % canon, file=sys.stderr)
print(" mirror: %s" % mirror, file=sys.stderr)
want = {status_macro(n): v for n, v in canon} want = {status_macro(n): v for n, v in canon}
have = {n: int(v) for n, v in STATUS_DEF_RE.findall(c_text)} have = {n: int(v) for n, v in STATUS_DEF_RE.findall(c_text)}
if want != have: if want != have:
@@ -132,8 +121,8 @@ def cmd_check():
status_problems = check_status(c_text) status_problems = check_status(c_text)
if not (missing or mismatched or extra): if not (missing or mismatched or extra):
if status_problems: if status_problems:
print("\n Run 'make gen-syscalls' to regenerate MTK_STATUS_*; " print("\n Run 'make gen-syscalls' to regenerate MTK_STATUS_*.",
"montauk/statuscodes.h is fixed by hand.", file=sys.stderr) file=sys.stderr)
return 1 return 1
print("montauk-syscalls: OK (%d syscalls, %d status codes in sync)" print("montauk-syscalls: OK (%d syscalls, %d status codes in sync)"
% (len(abi), len(parse_status(STATUS_HDR)))) % (len(abi), len(parse_status(STATUS_HDR))))