diff --git a/kernel/include/scheduler.hpp b/kernel/include/scheduler.hpp index be960aa0..582060f9 100644 --- a/kernel/include/scheduler.hpp +++ b/kernel/include/scheduler.hpp @@ -10,6 +10,7 @@ #include #include +#include #include "process.hpp" #include "net/socket.hpp" @@ -32,7 +33,7 @@ void init(); void start() __attribute__((noreturn)); bool is_started(); -int64_t exec(const std::string& path, const std::vector& params); +std::expected exec(const std::string& path, const std::vector& params); void kill_current_process(); void await_termination(pid_t pid); diff --git a/kernel/src/scheduler.cpp b/kernel/src/scheduler.cpp index 8e7ad807..13f8d262 100644 --- a/kernel/src/scheduler.cpp +++ b/kernel/src/scheduler.cpp @@ -202,7 +202,13 @@ void init_task(){ while(true){ auto pid = scheduler::exec("/bin/tsh", params); - scheduler::await_termination(pid); + + if(!pid){ + logging::logf(logging::log_level::DEBUG, "scheduler: failed to run the shell: %s\n", std::error_message(pid.error())); + return; + } + + scheduler::await_termination(*pid); logging::log(logging::log_level::DEBUG, "scheduler: shell exited, run new one\n"); } @@ -605,7 +611,7 @@ bool scheduler::is_started(){ return started; } -int64_t scheduler::exec(const std::string& file, const std::vector& params){ +std::expected scheduler::exec(const std::string& file, const std::vector& params){ logging::log(logging::log_level::TRACE, "scheduler:exec: read_file start\n"); std::string content; @@ -613,7 +619,7 @@ int64_t scheduler::exec(const std::string& file, const std::vector& if(result < 0){ logging::logf(logging::log_level::DEBUG, "scheduler: direct_read error: %s\n", std::error_message(-result)); - return result; + return std::make_unexpected(-result); } logging::log(logging::log_level::TRACE, "scheduler:exec: read_file end\n"); @@ -621,7 +627,7 @@ int64_t scheduler::exec(const std::string& file, const std::vector& if(content.empty()){ logging::log(logging::log_level::DEBUG, "scheduler:exec: Not a file\n"); - return -std::ERROR_NOT_EXISTS; + return std::make_unexpected(std::ERROR_NOT_EXISTS); } auto buffer = content.c_str(); @@ -629,7 +635,7 @@ int64_t scheduler::exec(const std::string& file, const std::vector& if(!elf::is_valid(buffer)){ logging::log(logging::log_level::DEBUG, "scheduler:exec: Not a valid file\n"); - return -std::ERROR_NOT_EXECUTABLE; + return std::make_unexpected(std::ERROR_NOT_EXECUTABLE); } auto& process = new_process(); @@ -639,7 +645,7 @@ int64_t scheduler::exec(const std::string& file, const std::vector& if(!create_paging(buffer, process)){ logging::log(logging::log_level::DEBUG, "scheduler:exec: Impossible to create paging\n"); - return -std::ERROR_FAILED_EXECUTION; + return std::make_unexpected(std::ERROR_FAILED_EXECUTION); } process.brk_start = program_break; diff --git a/kernel/src/system_calls.cpp b/kernel/src/system_calls.cpp index bd04f645..8d7e7899 100644 --- a/kernel/src/system_calls.cpp +++ b/kernel/src/system_calls.cpp @@ -113,7 +113,8 @@ void sc_exec(interrupt::syscall_regs* regs){ params.emplace_back(argv[i]); } - regs->rax = scheduler::exec(file, params); + auto status = scheduler::exec(file, params); + regs->rax = expected_to_i64(status); } void sc_await_termination(interrupt::syscall_regs* regs){