diff --git a/lib/mammoth/file/file.cpp b/lib/mammoth/file/file.cpp index ed6ce66..0dfa605 100644 --- a/lib/mammoth/file/file.cpp +++ b/lib/mammoth/file/file.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "util/debug.h" @@ -14,7 +15,11 @@ VFSClient* gVfsClient = nullptr; void GetVfsClientIfNeeded() { if (gVfsClient == nullptr) { - YellowstoneClient client(gInitEndpointCap); + // TODO: Add an unowned client so we don't have to duplicate this cap every + // time. + uint64_t dup_cap; + check(ZCapDuplicate(gInitEndpointCap, kZionPerm_All, &dup_cap)); + YellowstoneClient client(dup_cap); GetEndpointRequest yreq; yreq.set_endpoint_name("victoriafalls"); diff --git a/lib/mammoth/input/keyboard.cpp b/lib/mammoth/input/keyboard.cpp index 377cc22..19d0100 100644 --- a/lib/mammoth/input/keyboard.cpp +++ b/lib/mammoth/input/keyboard.cpp @@ -24,7 +24,9 @@ KeyboardListenerBase::KeyboardListenerBase() { } void KeyboardListenerBase::Register() { - YellowstoneClient client(gInitEndpointCap); + uint64_t dup_cap; + check(ZCapDuplicate(gInitEndpointCap, kZionPerm_All, &dup_cap)); + YellowstoneClient client(dup_cap); GetEndpointRequest req; req.set_endpoint_name("voyageurs"); diff --git a/sys/denali/lib/denali/denali.yunq.client.cpp b/sys/denali/lib/denali/denali.yunq.client.cpp index a8c29f0..2020331 100644 --- a/sys/denali/lib/denali/denali.yunq.client.cpp +++ b/sys/denali/lib/denali/denali.yunq.client.cpp @@ -3,10 +3,18 @@ #include #include +#include #include +DenaliClient::~DenaliClient() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + + glcr::ErrorCode DenaliClient::Read(const ReadRequest& request, ReadResponse& response) { diff --git a/sys/denali/lib/denali/denali.yunq.client.h b/sys/denali/lib/denali/denali.yunq.client.h index c0e2d42..ee80aca 100644 --- a/sys/denali/lib/denali/denali.yunq.client.h +++ b/sys/denali/lib/denali/denali.yunq.client.h @@ -13,6 +13,7 @@ class DenaliClient { DenaliClient(z_cap_t Denali_cap) : endpoint_(Denali_cap) {} DenaliClient(const DenaliClient&) = delete; DenaliClient(DenaliClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;}; + ~DenaliClient(); z_cap_t Capability() { return endpoint_; } diff --git a/sys/denali/lib/denali/denali.yunq.server.cpp b/sys/denali/lib/denali/denali.yunq.server.cpp index 1fda418..9b1d83f 100644 --- a/sys/denali/lib/denali/denali.yunq.server.cpp +++ b/sys/denali/lib/denali/denali.yunq.server.cpp @@ -35,6 +35,12 @@ DenaliServerBase::~DenaliServerBase() { } } +glcr::ErrorOr DenaliServerBase::CreateClientCap() { + uint64_t client_cap; + RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); + return client_cap; +} + glcr::ErrorOr DenaliServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/denali/lib/denali/denali.yunq.server.h b/sys/denali/lib/denali/denali.yunq.server.h index b10e187..41083ed 100644 --- a/sys/denali/lib/denali/denali.yunq.server.h +++ b/sys/denali/lib/denali/denali.yunq.server.h @@ -17,6 +17,7 @@ class DenaliServerBase { DenaliServerBase(DenaliServerBase&&) = delete; virtual ~DenaliServerBase(); + glcr::ErrorOr CreateClientCap(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp index 142625a..cbd3c3a 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.cpp @@ -3,10 +3,18 @@ #include #include +#include #include +VFSClient::~VFSClient() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + + glcr::ErrorCode VFSClient::OpenFile(const OpenFileRequest& request, OpenFileResponse& response) { diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h index d0ffcdc..ba62714 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.client.h @@ -13,6 +13,7 @@ class VFSClient { VFSClient(z_cap_t VFS_cap) : endpoint_(VFS_cap) {} VFSClient(const VFSClient&) = delete; VFSClient(VFSClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;}; + ~VFSClient(); z_cap_t Capability() { return endpoint_; } diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp index 995f8cb..64aa20a 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.cpp @@ -35,6 +35,12 @@ VFSServerBase::~VFSServerBase() { } } +glcr::ErrorOr VFSServerBase::CreateClientCap() { + uint64_t client_cap; + RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); + return client_cap; +} + glcr::ErrorOr VFSServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h index d7cabe0..1be2e9f 100644 --- a/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h +++ b/sys/victoriafalls/lib/victoriafalls/victoriafalls.yunq.server.h @@ -17,6 +17,7 @@ class VFSServerBase { VFSServerBase(VFSServerBase&&) = delete; virtual ~VFSServerBase(); + glcr::ErrorOr CreateClientCap(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.cpp index 65f4d26..df1c323 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.cpp @@ -3,10 +3,18 @@ #include #include +#include #include +VoyageursClient::~VoyageursClient() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + + glcr::ErrorCode VoyageursClient::RegisterKeyboardListener(const KeyboardListener& request) { diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.h b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.h index ecf97fe..48b784c 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.h +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.client.h @@ -13,6 +13,7 @@ class VoyageursClient { VoyageursClient(z_cap_t Voyageurs_cap) : endpoint_(Voyageurs_cap) {} VoyageursClient(const VoyageursClient&) = delete; VoyageursClient(VoyageursClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;}; + ~VoyageursClient(); z_cap_t Capability() { return endpoint_; } diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp index a062b02..bd90ab2 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.cpp @@ -35,6 +35,12 @@ VoyageursServerBase::~VoyageursServerBase() { } } +glcr::ErrorOr VoyageursServerBase::CreateClientCap() { + uint64_t client_cap; + RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); + return client_cap; +} + glcr::ErrorOr VoyageursServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h index 7c85b40..f349e3b 100644 --- a/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h +++ b/sys/voyageurs/lib/voyageurs/voyageurs.yunq.server.h @@ -17,6 +17,7 @@ class VoyageursServerBase { VoyageursServerBase(VoyageursServerBase&&) = delete; virtual ~VoyageursServerBase(); + glcr::ErrorOr CreateClientCap(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.cpp index 2a016c5..4dd4d8b 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.cpp @@ -3,10 +3,18 @@ #include #include +#include #include +YellowstoneClient::~YellowstoneClient() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + + glcr::ErrorCode YellowstoneClient::RegisterEndpoint(const RegisterEndpointRequest& request) { diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.h b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.h index 1d8dfaa..93371e8 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.h +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.client.h @@ -13,6 +13,7 @@ class YellowstoneClient { YellowstoneClient(z_cap_t Yellowstone_cap) : endpoint_(Yellowstone_cap) {} YellowstoneClient(const YellowstoneClient&) = delete; YellowstoneClient(YellowstoneClient&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;}; + ~YellowstoneClient(); z_cap_t Capability() { return endpoint_; } diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp index 88afc31..e05cda0 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.cpp @@ -35,6 +35,12 @@ YellowstoneServerBase::~YellowstoneServerBase() { } } +glcr::ErrorOr YellowstoneServerBase::CreateClientCap() { + uint64_t client_cap; + RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); + return client_cap; +} + glcr::ErrorOr YellowstoneServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h index 316a8d3..1b829ef 100644 --- a/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h +++ b/sys/yellowstone/lib/yellowstone/yellowstone.yunq.server.h @@ -17,6 +17,7 @@ class YellowstoneServerBase { YellowstoneServerBase(YellowstoneServerBase&&) = delete; virtual ~YellowstoneServerBase(); + glcr::ErrorOr CreateClientCap(); glcr::ErrorOr CreateClient(); [[nodiscard]] Thread RunServer(); diff --git a/sys/yellowstone/yellowstone.cpp b/sys/yellowstone/yellowstone.cpp index 7e575a0..b4f16ae 100644 --- a/sys/yellowstone/yellowstone.cpp +++ b/sys/yellowstone/yellowstone.cpp @@ -25,13 +25,13 @@ uint64_t main(uint64_t port_cap) { ASSIGN_OR_RETURN(auto server, YellowstoneServer::Create()); Thread server_thread = server->RunServer(); - ASSIGN_OR_RETURN(YellowstoneClient client1, server->CreateClient()); - check(SpawnProcess(gBootDenaliVmmoCap, client1.Capability())); + ASSIGN_OR_RETURN(uint64_t client_cap, server->CreateClientCap()); + check(SpawnProcess(gBootDenaliVmmoCap, client_cap)); server->WaitDenaliRegistered(); - ASSIGN_OR_RETURN(YellowstoneClient client2, server->CreateClient()); - check(SpawnProcess(gBootVictoriaFallsVmmoCap, client2.Capability())); + ASSIGN_OR_RETURN(client_cap, server->CreateClientCap()); + check(SpawnProcess(gBootVictoriaFallsVmmoCap, client_cap)); server->WaitVictoriaFallsRegistered(); @@ -47,9 +47,9 @@ uint64_t main(uint64_t port_cap) { mmth::File binary = mmth::File::Open(glcr::StrFormat("/bin/{}", files[i])); - ASSIGN_OR_RETURN(YellowstoneClient client3, server->CreateClient()); + ASSIGN_OR_RETURN(client_cap, server->CreateClientCap()); check(mmth::SpawnProcessFromElfRegion((uint64_t)binary.raw_ptr(), - client3.Capability())); + client_cap)); } } diff --git a/sys/yellowstone/yellowstone_server.cpp b/sys/yellowstone/yellowstone_server.cpp index ed7ccd1..f22b36e 100644 --- a/sys/yellowstone/yellowstone_server.cpp +++ b/sys/yellowstone/yellowstone_server.cpp @@ -87,7 +87,9 @@ glcr::ErrorCode YellowstoneServer::HandleRegisterEndpoint( dbgln("Registering {}.", req.endpoint_name().view()); check(endpoint_map_.Insert(req.endpoint_name(), req.endpoint_capability())); if (req.endpoint_name() == "denali") { - auto part_info_or = HandleDenaliRegistration(req.endpoint_capability()); + z_cap_t dup_cap; + check(ZCapDuplicate(req.endpoint_capability(), kZionPerm_All, &dup_cap)); + auto part_info_or = HandleDenaliRegistration(dup_cap); if (!part_info_or.ok()) { check(part_info_or.error()); } diff --git a/yunq/client.cpp.jinja b/yunq/client.cpp.jinja index 7a88c90..e17b64b 100644 --- a/yunq/client.cpp.jinja +++ b/yunq/client.cpp.jinja @@ -3,9 +3,17 @@ #include #include +#include #include {% for interface in interfaces %} + +{{interface.name}}Client::~{{interface.name}}Client() { + if (endpoint_ != 0) { + check(ZCapRelease(endpoint_)); + } +} + {% for method in interface.methods %} {% if method.request == None %} diff --git a/yunq/client.h.jinja b/yunq/client.h.jinja index a1c2936..5684d5f 100644 --- a/yunq/client.h.jinja +++ b/yunq/client.h.jinja @@ -14,6 +14,7 @@ class {{interface.name}}Client { {{interface.name}}Client(z_cap_t {{interface.name}}_cap) : endpoint_({{interface.name}}_cap) {} {{interface.name}}Client(const {{interface.name}}Client&) = delete; {{interface.name}}Client({{interface.name}}Client&& other) : endpoint_(other.endpoint_) {other.endpoint_ = 0;}; + ~{{interface.name}}Client(); z_cap_t Capability() { return endpoint_; } diff --git a/yunq/server.cpp.jinja b/yunq/server.cpp.jinja index 595b278..1695ffb 100644 --- a/yunq/server.cpp.jinja +++ b/yunq/server.cpp.jinja @@ -35,6 +35,12 @@ void {{interface.name}}ServerBaseThreadBootstrap(void* server_base) { } } +glcr::ErrorOr {{interface.name}}ServerBase::CreateClientCap() { + uint64_t client_cap; + RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); + return client_cap; +} + glcr::ErrorOr<{{interface.name}}Client> {{interface.name}}ServerBase::CreateClient() { uint64_t client_cap; RET_ERR(ZCapDuplicate(endpoint_, ~(kZionPerm_Read), &client_cap)); diff --git a/yunq/server.h.jinja b/yunq/server.h.jinja index 5b487ac..a053d63 100644 --- a/yunq/server.h.jinja +++ b/yunq/server.h.jinja @@ -17,6 +17,7 @@ class {{interface.name}}ServerBase { {{interface.name}}ServerBase({{interface.name}}ServerBase&&) = delete; virtual ~{{interface.name}}ServerBase(); + glcr::ErrorOr CreateClientCap(); glcr::ErrorOr<{{interface.name}}Client> CreateClient(); [[nodiscard]] Thread RunServer();