kernel: Add locking to BinaryFormat and DeviceRegistry
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-10-23 22:48:04 +02:00
parent 9c65dba412
commit cea1b030ff
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 15 additions and 2 deletions

View File

@ -1,6 +1,7 @@
#include "binfmt/BinaryFormat.h" #include "binfmt/BinaryFormat.h"
#include "binfmt/ELF.h" #include "binfmt/ELF.h"
#include "binfmt/Script.h" #include "binfmt/Script.h"
#include "lib/Mutex.h"
struct BinaryFormatDescriptor struct BinaryFormatDescriptor
{ {
@ -10,6 +11,8 @@ struct BinaryFormatDescriptor
Vector<BinaryFormatDescriptor> g_binary_formats; Vector<BinaryFormatDescriptor> g_binary_formats;
static Mutex g_lock;
Result<void> BinaryFormat::init() Result<void> BinaryFormat::init()
{ {
TRY(register_binary_format(ELFLoader::create, nullptr)); TRY(register_binary_format(ELFLoader::create, nullptr));
@ -20,6 +23,7 @@ Result<void> BinaryFormat::init()
Result<void> BinaryFormat::register_binary_format(binfmt_loader_creator_t creator, void* arg) Result<void> BinaryFormat::register_binary_format(binfmt_loader_creator_t creator, void* arg)
{ {
ScopedMutexLock lock(g_lock);
return g_binary_formats.try_append({ creator, arg }); return g_binary_formats.try_append({ creator, arg });
} }
@ -27,6 +31,8 @@ Result<SharedPtr<BinaryFormatLoader>> BinaryFormat::create_loader(SharedPtr<VFS:
{ {
if (recursion_level >= 8) return err(ELOOP); if (recursion_level >= 8) return err(ELOOP);
ScopedMutexLock lock(g_lock);
for (const auto& format : g_binary_formats) for (const auto& format : g_binary_formats)
{ {
auto loader = TRY(format.creator(inode, format.arg, recursion_level)); auto loader = TRY(format.creator(inode, format.arg, recursion_level));

View File

@ -12,6 +12,7 @@
#include "fs/devices/UARTDevice.h" #include "fs/devices/UARTDevice.h"
#include "fs/devices/ZeroDevice.h" #include "fs/devices/ZeroDevice.h"
#include "fs/tmpfs/FileSystem.h" #include "fs/tmpfs/FileSystem.h"
#include "lib/Mutex.h"
#include "thread/Thread.h" #include "thread/Thread.h"
#include <luna/Vector.h> #include <luna/Vector.h>
@ -26,10 +27,14 @@ struct DeviceDescriptor
Vector<DeviceDescriptor> g_available_devices = {}; Vector<DeviceDescriptor> g_available_devices = {};
static Mutex g_lock;
namespace DeviceRegistry namespace DeviceRegistry
{ {
Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor) Result<SharedPtr<Device>> fetch_special_device(u32 major, u32 minor)
{ {
ScopedMutexLock lock(g_lock);
for (const auto& descriptor : g_available_devices) for (const auto& descriptor : g_available_devices)
{ {
if (descriptor.major == major && descriptor.minor == minor) return descriptor.device; if (descriptor.major == major && descriptor.minor == minor) return descriptor.device;
@ -38,7 +43,7 @@ namespace DeviceRegistry
return err(ENODEV); return err(ENODEV);
} }
Result<void> create_special_device_inode(SharedPtr<VFS::FileSystem> fs, const DeviceDescriptor& descriptor) static Result<void> create_special_device_inode(SharedPtr<VFS::FileSystem> fs, const DeviceDescriptor& descriptor)
{ {
auto inode = TRY(fs->create_device_inode(descriptor.major, descriptor.minor, descriptor.mode)); auto inode = TRY(fs->create_device_inode(descriptor.major, descriptor.minor, descriptor.mode));
TRY(fs->root_inode()->add_entry(inode, descriptor.name)); TRY(fs->root_inode()->add_entry(inode, descriptor.name));
@ -48,6 +53,8 @@ namespace DeviceRegistry
Result<void> register_special_device(u32 major, u32 minor, SharedPtr<Device> device, mode_t mode) Result<void> register_special_device(u32 major, u32 minor, SharedPtr<Device> device, mode_t mode)
{ {
ScopedMutexLock lock(g_lock);
for (const auto& descriptor : g_available_devices) for (const auto& descriptor : g_available_devices)
{ {
if (descriptor.major == major && descriptor.minor == minor) return err(EEXIST); if (descriptor.major == major && descriptor.minor == minor) return err(EEXIST);
@ -82,7 +89,7 @@ namespace DeviceRegistry
dev_t next_null_device_id() dev_t next_null_device_id()
{ {
static u32 next_minor = 0; static Atomic<u32> next_minor = 0;
return luna_dev_makedev(0, next_minor++); return luna_dev_makedev(0, next_minor++);
} }