Kernel/LibC: opening standard files is done in libc

This commit is contained in:
Bananymous 2023-05-29 20:21:19 +03:00
parent 2a34391b71
commit 7151bb86a8
6 changed files with 33 additions and 24 deletions

View File

@ -13,7 +13,7 @@ _start:
pushq %rdi pushq %rdi
# Prepare signals, memory allocation, stdio and such. # Prepare signals, memory allocation, stdio and such.
#call initialize_standard_library call _init_libc
# Run the global constructors. # Run the global constructors.
call _init call _init

View File

@ -36,7 +36,6 @@ namespace Kernel
void add_thread(Thread*); void add_thread(Thread*);
void on_thread_exit(Thread&); void on_thread_exit(Thread&);
BAN::ErrorOr<void> init_stdio();
BAN::ErrorOr<void> set_termios(const termios&); BAN::ErrorOr<void> set_termios(const termios&);
pid_t pid() const { return m_pid; } pid_t pid() const { return m_pid; }

View File

@ -31,7 +31,6 @@ namespace Kernel
s_process_lock.lock(); s_process_lock.lock();
MUST(s_processes.push_back(process)); MUST(s_processes.push_back(process));
s_process_lock.unlock(); s_process_lock.unlock();
for (auto* thread : process->m_threads) for (auto* thread : process->m_threads)
MUST(Scheduler::get().add_thread(thread)); MUST(Scheduler::get().add_thread(thread));
} }
@ -57,7 +56,6 @@ namespace Kernel
auto* process = create_process(); auto* process = create_process();
MUST(process->m_working_directory.push_back('/')); MUST(process->m_working_directory.push_back('/'));
MUST(process->init_stdio());
process->m_mmu = new MMU(); process->m_mmu = new MMU();
ASSERT(process->m_mmu); ASSERT(process->m_mmu);
@ -152,6 +150,7 @@ namespace Kernel
void Process::exit() void Process::exit()
{ {
m_lock.lock(); m_lock.lock();
m_threads.clear(); m_threads.clear();
for (auto& open_fd : m_open_files) for (auto& open_fd : m_open_files)
open_fd.inode = nullptr; open_fd.inode = nullptr;
@ -180,17 +179,9 @@ namespace Kernel
Scheduler::get().set_current_process_done(); Scheduler::get().set_current_process_done();
} }
BAN::ErrorOr<void> Process::init_stdio()
{
ASSERT(m_open_files.empty());
TRY(open("/dev/tty1", O_RDONLY)); // stdin
TRY(open("/dev/tty1", O_WRONLY)); // stdout
TRY(open("/dev/tty1", O_WRONLY)); // stderr
return {};
}
BAN::ErrorOr<void> Process::set_termios(const termios& termios) BAN::ErrorOr<void> Process::set_termios(const termios& termios)
{ {
LockGuard _(m_lock);
if (m_tty == nullptr) if (m_tty == nullptr)
return BAN::Error::from_errno(ENOTTY); return BAN::Error::from_errno(ENOTTY);
m_tty->set_termios(termios); m_tty->set_termios(termios);
@ -199,18 +190,17 @@ namespace Kernel
BAN::ErrorOr<Process*> Process::fork(uintptr_t rsp, uintptr_t rip) BAN::ErrorOr<Process*> Process::fork(uintptr_t rsp, uintptr_t rip)
{ {
LockGuard _(m_lock);
Process* forked = create_process(); Process* forked = create_process();
forked->m_mmu = new MMU();
ASSERT(forked->m_mmu);
LockGuard _(m_lock);
forked->m_tty = m_tty; forked->m_tty = m_tty;
forked->m_working_directory = m_working_directory; forked->m_working_directory = m_working_directory;
forked->m_open_files = m_open_files; forked->m_open_files = m_open_files;
forked->m_mmu = new MMU();
ASSERT(forked->m_mmu);
for (auto* mapped_range : m_mapped_ranges) for (auto* mapped_range : m_mapped_ranges)
MUST(forked->m_mapped_ranges.push_back(mapped_range->clone(forked->mmu()))); MUST(forked->m_mapped_ranges.push_back(mapped_range->clone(forked->mmu())));
@ -366,8 +356,12 @@ namespace Kernel
BAN::ErrorOr<void> Process::mount(BAN::StringView source, BAN::StringView target) BAN::ErrorOr<void> Process::mount(BAN::StringView source, BAN::StringView target)
{ {
auto absolute_source = TRY(absolute_path_of(source)); BAN::String absolute_source, absolute_target;
auto absolute_target = TRY(absolute_path_of(target)); {
LockGuard _(m_lock);
absolute_source = TRY(absolute_path_of(source));
absolute_target = TRY(absolute_path_of(target));
}
TRY(VirtualFileSystem::get().mount(absolute_source, absolute_target)); TRY(VirtualFileSystem::get().mount(absolute_source, absolute_target));
return {}; return {};
} }
@ -539,6 +533,7 @@ namespace Kernel
void Process::termid(char* buffer) const void Process::termid(char* buffer) const
{ {
LockGuard _(m_lock);
if (m_tty == nullptr) if (m_tty == nullptr)
buffer[0] = '\0'; buffer[0] = '\0';
strcpy(buffer, "/dev/"); strcpy(buffer, "/dev/");

View File

@ -186,7 +186,6 @@ static void init2(void* tty1)
Process::create_kernel( Process::create_kernel(
[](void*) [](void*)
{ {
MUST(Process::current().init_stdio());
Shell* shell = new Shell(); Shell* shell = new Shell();
ASSERT(shell); ASSERT(shell);
shell->run(); shell->run();

View File

@ -27,6 +27,15 @@ FILE* stdin = &s_files[0];
FILE* stdout = &s_files[1]; FILE* stdout = &s_files[1];
FILE* stderr = &s_files[2]; FILE* stderr = &s_files[2];
extern "C" void _init_stdio()
{
char tty[L_ctermid];
ctermid(tty);
if (open(tty, O_RDONLY) != 0) _exit(1);
if (open(tty, O_WRONLY) != 1) _exit(1);
if (open(tty, O_WRONLY) != 2) _exit(1);
}
void clearerr(FILE* file) void clearerr(FILE* file)
{ {
file->eof = false; file->eof = false;
@ -155,7 +164,7 @@ FILE* fopen(const char* pathname, const char* mode)
uint8_t flags = 0; uint8_t flags = 0;
if (mode[0] == 'r') if (mode[0] == 'r')
flags |= O_RDONLY; flags |= O_RDONLY;
else if (mode[0] == 'b') else if (mode[0] == 'w')
flags |= O_WRONLY | O_CREAT | O_TRUNC; flags |= O_WRONLY | O_CREAT | O_TRUNC;
else if (mode[0] == 'a') else if (mode[0] == 'a')
flags |= O_WRONLY | O_CREAT | O_APPEND; flags |= O_WRONLY | O_CREAT | O_APPEND;

View File

@ -7,6 +7,13 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
extern "C" void _init_stdio();
extern "C" void _init_libc()
{
_init_stdio();
}
void _exit(int status) void _exit(int status)
{ {
syscall(SYS_EXIT, status); syscall(SYS_EXIT, status);