Kernel: Process can now initialize stdio

This allows the use of the fds STD{IN,OUT,ERR}_FILENO
This commit is contained in:
Bananymous 2023-04-05 01:10:25 +03:00
parent fe10ea85db
commit db076058b9
3 changed files with 19 additions and 5 deletions

View File

@ -27,6 +27,8 @@ namespace Kernel
BAN::ErrorOr<void> add_thread(entry_t, void*);
void on_thread_exit(Thread&);
BAN::ErrorOr<void> init_stdio();
pid_t pid() const { return m_pid; }
BAN::ErrorOr<int> open(BAN::StringView, int);

View File

@ -18,6 +18,16 @@ namespace Kernel
return process;
}
BAN::ErrorOr<void> Process::init_stdio()
{
if (!m_open_files.empty())
return BAN::Error::from_c_string("Could not init stdio, process already has open files");
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::add_thread(entry_t entry, void* data)
{
Thread* thread = TRY(Thread::create(entry, data, this));
@ -43,7 +53,7 @@ namespace Kernel
BAN::ErrorOr<int> Process::open(BAN::StringView path, int flags)
{
if (flags != O_RDONLY)
if (flags & ~O_RDWR)
return BAN::Error::from_errno(ENOTSUP);
BAN::String absolute_path = TRY(absolute_path_of(path));
@ -97,7 +107,7 @@ namespace Kernel
auto open_fd_copy = open_file_description(fd);
m_lock.unlock();
if (!(open_fd_copy.flags & O_RDONLY))
if (!(open_fd_copy.flags & O_WRONLY))
return BAN::Error::from_errno(EBADF);
size_t n_written = TRY(open_fd_copy.inode->write(open_fd_copy.offset, buffer, count));
open_fd_copy.offset += n_written;

View File

@ -22,6 +22,8 @@
#include <kernel/Terminal/TTY.h>
#include <kernel/Terminal/VesaTerminalDriver.h>
#include <unistd.h>
extern "C" const char g_kernel_cmdline[];
struct ParsedCommandLine
@ -223,12 +225,12 @@ static void init2(void* terminal_driver)
MUST(Process::create_kernel(
[](void*)
{
int fd = MUST(Process::current()->open("/dev/tty1", 1));
MUST(Process::current()->init_stdio());
while (true)
{
char buffer[1024];
int n_read = MUST(Process::current()->read(fd, buffer, sizeof(buffer)));
MUST(Process::current()->write(fd, buffer, n_read));
int n_read = MUST(Process::current()->read(STDIN_FILENO, buffer, sizeof(buffer)));
MUST(Process::current()->write(STDOUT_FILENO, buffer, n_read));
dprintln("{} bytes", n_read);
}
}, nullptr