Kernel: Process::working_directory() is now thread safe

I realized you cannot return a stirng view and it to be thread safe
This commit is contained in:
Bananymous 2023-03-24 01:43:53 +02:00
parent 6f334756c5
commit d67de70126
3 changed files with 10 additions and 4 deletions

View File

@ -32,7 +32,7 @@ namespace Kernel
BAN::ErrorOr<size_t> read(int, void*, size_t);
BAN::ErrorOr<void> creat(BAN::StringView, mode_t);
BAN::StringView working_directory() const { return m_working_directory; }
BAN::String working_directory() const;
BAN::ErrorOr<void> set_working_directory(BAN::StringView);
Inode& inode_for_fd(int);
@ -63,7 +63,7 @@ namespace Kernel
mutable RecursiveSpinLock m_lock;
pid_t m_pid = 0;
const pid_t m_pid = 0;
BAN::String m_working_directory;
BAN::Vector<BAN::RefPtr<Thread>> m_threads;

View File

@ -21,7 +21,7 @@ namespace Kernel
BAN::ErrorOr<void> Process::add_thread(entry_t entry, void* data)
{
LockGuard _(m_lock);
auto thread = TRY(Thread::create(entry, data, this));
TRY(m_threads.push_back(thread));
if (auto res = Scheduler::get().add_thread(thread); res.is_error())
@ -104,6 +104,12 @@ namespace Kernel
return *open_file_description(fd).inode;
}
BAN::String Process::working_directory() const
{
LockGuard _(m_lock);
return m_working_directory;
}
BAN::ErrorOr<void> Process::set_working_directory(BAN::StringView path)
{
LockGuard _(m_lock);

View File

@ -356,7 +356,7 @@ argument_done:
if (arguments.size() > 2)
return BAN::Error::from_c_string("usage: 'ls [path]'");
BAN::StringView path = (arguments.size() == 2) ? arguments[1].sv() : Process::current()->working_directory();
BAN::String path = (arguments.size() == 2) ? arguments[1] : Process::current()->working_directory();
int fd = TRY(Process::current()->open(path, O_RDONLY));
BAN::ScopeGuard _([fd] { MUST(Process::current()->close(fd)); });