Stay organized with collections
Save and categorize content based on your preferences.
4. Run the Sandbox
In the previous sections, you prepared the sandboxed environment, policy, and
executor and Sandboxee. The next step is to create the Sandbox2
object and run
it.
Run synchronously
The sandbox can run synchronously, thus blocking until there is a result. The
code snippet below demonstrates the instantiation of the Sandbox2
object and
its synchronous execution. For a more detailed example, see
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();
Run asynchronously
You can also run the sandbox asynchronously, thus not blocking until there is a
result. This is useful, for instance, when communicating with the Sandboxee. The
code snippet below demonstrates this use case, for more detailed examples see
crc4 and
tool.
#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();
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-04-22 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-22 UTC."],[[["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."]]],[]]