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
# Prepare signals, memory allocation, stdio and such.
#call initialize_standard_library
call _init_libc
# Run the global constructors.
call _init

View File

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

View File

@ -31,7 +31,6 @@ namespace Kernel
s_process_lock.lock();
MUST(s_processes.push_back(process));
s_process_lock.unlock();
for (auto* thread : process->m_threads)
MUST(Scheduler::get().add_thread(thread));
}
@ -57,10 +56,9 @@ namespace Kernel
auto* process = create_process();
MUST(process->m_working_directory.push_back('/'));
MUST(process->init_stdio());
process->m_mmu = new MMU();
ASSERT(process->m_mmu);
auto& elf_file_header = elf->file_header_native();
for (size_t i = 0; i < elf_file_header.e_phnum; i++)
{
@ -152,6 +150,7 @@ namespace Kernel
void Process::exit()
{
m_lock.lock();
m_threads.clear();
for (auto& open_fd : m_open_files)
open_fd.inode = nullptr;
@ -180,17 +179,9 @@ namespace Kernel
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)
{
LockGuard _(m_lock);
if (m_tty == nullptr)
return BAN::Error::from_errno(ENOTTY);
m_tty->set_termios(termios);
@ -199,18 +190,17 @@ namespace Kernel
BAN::ErrorOr<Process*> Process::fork(uintptr_t rsp, uintptr_t rip)
{
LockGuard _(m_lock);
Process* forked = create_process();
forked->m_mmu = new MMU();
ASSERT(forked->m_mmu);
LockGuard _(m_lock);
forked->m_tty = m_tty;
forked->m_working_directory = m_working_directory;
forked->m_open_files = m_open_files;
forked->m_mmu = new MMU();
ASSERT(forked->m_mmu);
for (auto* mapped_range : m_mapped_ranges)
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)
{
auto absolute_source = TRY(absolute_path_of(source));
auto absolute_target = TRY(absolute_path_of(target));
BAN::String absolute_source, absolute_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));
return {};
}
@ -539,6 +533,7 @@ namespace Kernel
void Process::termid(char* buffer) const
{
LockGuard _(m_lock);
if (m_tty == nullptr)
buffer[0] = '\0';
strcpy(buffer, "/dev/");

View File

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

View File

@ -27,6 +27,15 @@ FILE* stdin = &s_files[0];
FILE* stdout = &s_files[1];
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)
{
file->eof = false;
@ -155,7 +164,7 @@ FILE* fopen(const char* pathname, const char* mode)
uint8_t flags = 0;
if (mode[0] == 'r')
flags |= O_RDONLY;
else if (mode[0] == 'b')
else if (mode[0] == 'w')
flags |= O_WRONLY | O_CREAT | O_TRUNC;
else if (mode[0] == 'a')
flags |= O_WRONLY | O_CREAT | O_APPEND;

View File

@ -7,6 +7,13 @@
#include <sys/syscall.h>
#include <unistd.h>
extern "C" void _init_stdio();
extern "C" void _init_libc()
{
_init_stdio();
}
void _exit(int status)
{
syscall(SYS_EXIT, status);
@ -139,4 +146,4 @@ pid_t fork(void)
unsigned int sleep(unsigned int seconds)
{
return syscall(SYS_SLEEP, seconds);
}
}