使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
4. 运行沙盒
在前面的部分中,您介绍了沙盒化环境、政策、执行器和 Sandboxee。下一步是创建并运行 Sandbox2
对象。
同步运行
沙盒可以同步运行,因此会阻塞,直到得出结果。以下代码段演示了 Sandbox2
对象的实例化及其同步执行。如需查看更详细的示例,请参阅 static。
#include "sandboxed_api/sandbox2/sandbox2.h"
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
sandbox2::Result result = s2.Run(); // Synchronous
LOG(INFO) << "Result of sandbox execution: " << result.ToString();
异步运行
您也可以异步运行沙盒,因此在获得结果之前不会阻塞。例如,在与 Sandboxee 通信时,这非常有用。以下代码段演示了此用例,如需更详细的示例,请参阅 crc4 和工具。
#include "sandboxed_api/sandbox2/sandbox2.h"
sandbox2::Sandbox2 s2(std::move(executor), std::move(policy));
if (s2.RunAsync()) {
// Communicate with sandboxee, use s2.Kill() to kill it if needed
// ...
}
Sandbox2::Result result = s2.AwaitResult();
LOG(INFO) << "Final execution status: " << result.ToString();
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-12-06。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2023-12-06。"],[[["Sandbox2 can be executed synchronously, blocking until a result is obtained, or asynchronously, allowing for concurrent operations and communication with the sandboxed process."],["Running synchronously involves creating a `Sandbox2` object and directly calling `Run()` to execute it and retrieve the result."],["Asynchronous execution utilizes `RunAsync()` to initiate the sandboxed process and `AwaitResult()` to obtain the final outcome, enabling communication with the sandboxee during execution."],["Example code snippets and links to more detailed examples are provided for both synchronous and asynchronous approaches to running the sandbox."]]],[]]