From fb17af4844cbfb770456f094831f007f9c3d3e6c Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 29 May 2023 20:21:19 +0300 Subject: [PATCH] Kernel/LibC: opening standard files is done in libc --- kernel/arch/x86_64/crt0.S | 2 +- kernel/include/kernel/Process.h | 1 - kernel/kernel/Process.cpp | 33 ++++++++++++++------------------- kernel/kernel/kernel.cpp | 1 - libc/stdio.cpp | 11 ++++++++++- libc/unistd.cpp | 9 ++++++++- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/kernel/arch/x86_64/crt0.S b/kernel/arch/x86_64/crt0.S index 889e498328..1f0832603d 100644 --- a/kernel/arch/x86_64/crt0.S +++ b/kernel/arch/x86_64/crt0.S @@ -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 diff --git a/kernel/include/kernel/Process.h b/kernel/include/kernel/Process.h index 49fd8708f6..ac2bce1b44 100644 --- a/kernel/include/kernel/Process.h +++ b/kernel/include/kernel/Process.h @@ -36,7 +36,6 @@ namespace Kernel void add_thread(Thread*); void on_thread_exit(Thread&); - BAN::ErrorOr init_stdio(); BAN::ErrorOr set_termios(const termios&); pid_t pid() const { return m_pid; } diff --git a/kernel/kernel/Process.cpp b/kernel/kernel/Process.cpp index 5acac2de0b..3440809307 100644 --- a/kernel/kernel/Process.cpp +++ b/kernel/kernel/Process.cpp @@ -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 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 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::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 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/"); diff --git a/kernel/kernel/kernel.cpp b/kernel/kernel/kernel.cpp index 906c367152..19cf6ef292 100644 --- a/kernel/kernel/kernel.cpp +++ b/kernel/kernel/kernel.cpp @@ -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(); diff --git a/libc/stdio.cpp b/libc/stdio.cpp index c705f3afda..6e9da2e8e2 100644 --- a/libc/stdio.cpp +++ b/libc/stdio.cpp @@ -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; diff --git a/libc/unistd.cpp b/libc/unistd.cpp index 9e6bc6fab5..b1d3eec643 100644 --- a/libc/unistd.cpp +++ b/libc/unistd.cpp @@ -7,6 +7,13 @@ #include #include +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); -} \ No newline at end of file +}