[Teton] Wait on spawned processes to exit.

This commit is contained in:
Drew Galbraith 2023-12-02 13:26:42 -08:00
parent 8c5dd00443
commit 0b9f83b321
4 changed files with 34 additions and 11 deletions

View File

@ -103,8 +103,8 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
} // namespace
glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
z_cap_t yellowstone_client) {
glcr::ErrorOr<z_cap_t> SpawnProcessFromElfRegion(uint64_t program,
z_cap_t yellowstone_client) {
uint64_t proc_cap;
uint64_t as_cap;
uint64_t foreign_port_id;
@ -130,7 +130,9 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
uint64_t thread_cap;
RET_ERR(ZThreadCreate(proc_cap, &thread_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
uint64_t dup_proc_cap;
RET_ERR(ZCapDuplicate(proc_cap, kZionPerm_All, &dup_proc_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, dup_proc_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, yellowstone_client));
@ -139,7 +141,7 @@ glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
#endif
RET_ERR(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
return glcr::OK;
return proc_cap;
}
} // namespace mmth

View File

@ -1,12 +1,12 @@
#pragma once
#include <glacier/status/error.h>
#include <glacier/status/error_or.h>
#include <stdint.h>
#include <ztypes.h>
namespace mmth {
glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
z_cap_t yellowstone_client);
glcr::ErrorOr<z_cap_t> SpawnProcessFromElfRegion(uint64_t program,
z_cap_t yellowstone_client);
} // namespace mmth

View File

@ -4,6 +4,7 @@
#include <glacier/string/str_split.h>
#include <mammoth/file/file.h>
#include <mammoth/proc/process.h>
#include <mammoth/util/debug.h>
#include <mammoth/util/init.h>
void Terminal::HandleCharacter(char c) {
@ -68,7 +69,19 @@ void Terminal::ExecuteCommand(const glcr::String& command) {
auto file = mmth::File::Open(tokens[1]);
// TODO: Wait until the process exits.
mmth::SpawnProcessFromElfRegion((uint64_t)file.raw_ptr(), gInitEndpointCap);
auto error_or_cap = mmth::SpawnProcessFromElfRegion(
(uint64_t)file.raw_ptr(), gInitEndpointCap);
if (!error_or_cap.ok()) {
console_.WriteString(
glcr::StrFormat("Error: {}\n", error_or_cap.error()));
return;
}
uint64_t err_code;
check(ZProcessWait(error_or_cap.value(), &err_code));
if (err_code != 0) {
console_.WriteString(glcr::StrFormat(
"Process Error: {}\n", static_cast<glcr::ErrorCode>(err_code)));
}
} else {
console_.WriteString("Unknown command: ");

View File

@ -15,7 +15,12 @@
glcr::ErrorCode SpawnProcess(z_cap_t vmmo_cap, z_cap_t yellowstone_cap) {
mmth::OwnedMemoryRegion region =
mmth::OwnedMemoryRegion::FromCapability(vmmo_cap);
return mmth::SpawnProcessFromElfRegion(region.vaddr(), yellowstone_cap);
auto error_or =
mmth::SpawnProcessFromElfRegion(region.vaddr(), yellowstone_cap);
if (error_or.ok()) {
return glcr::OK;
}
return error_or.error();
}
uint64_t main(uint64_t port_cap) {
@ -48,8 +53,11 @@ uint64_t main(uint64_t port_cap) {
mmth::File::Open(glcr::StrFormat("/bin/{}", files[i]));
ASSIGN_OR_RETURN(client_cap, server->CreateClientCap());
check(mmth::SpawnProcessFromElfRegion((uint64_t)binary.raw_ptr(),
client_cap));
auto error_or = mmth::SpawnProcessFromElfRegion(
(uint64_t)binary.raw_ptr(), client_cap);
if (!error_or.ok()) {
check(error_or.error());
}
}
}