Luna/kernel/src/binfmt/BinaryFormat.cpp
apio 4c87d72b44
All checks were successful
continuous-integration/drone/push Build is passing
kernel/binfmt: Add documentation + support script interpreters being scripts themselves
2023-07-31 20:41:18 +02:00

43 lines
1.1 KiB
C++

#include "binfmt/BinaryFormat.h"
#include "binfmt/ELF.h"
#include "binfmt/Script.h"
struct BinaryFormatDescriptor
{
binfmt_loader_creator_t creator;
void* arg;
};
Vector<BinaryFormatDescriptor> g_binary_formats;
Result<void> BinaryFormat::init()
{
TRY(register_binary_format(ELFLoader::create, nullptr));
TRY(register_binary_format(ScriptLoader::create, nullptr));
return {};
}
Result<void> BinaryFormat::register_binary_format(binfmt_loader_creator_t creator, void* arg)
{
return g_binary_formats.try_append({ creator, arg });
}
Result<SharedPtr<BinaryFormatLoader>> BinaryFormat::create_loader(SharedPtr<VFS::Inode> inode, int recursion_level)
{
if (recursion_level >= 8) return err(ELOOP);
for (const auto& format : g_binary_formats)
{
auto loader = TRY(format.creator(inode, format.arg, recursion_level));
if (TRY(loader->sniff())) return loader;
}
return err(ENOEXEC);
}
BinaryFormatLoader::BinaryFormatLoader(SharedPtr<VFS::Inode> inode, int recursion_level)
: m_inode(inode), m_recursion_level(recursion_level)
{
}