xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libuv/examples/helloworld.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2020 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include <linux/futex.h>
16*ec63e07aSXin Li #include <syscall.h>
17*ec63e07aSXin Li #include <uv.h>
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include <iostream>
20*ec63e07aSXin Li 
21*ec63e07aSXin Li #include "absl/flags/flag.h"
22*ec63e07aSXin Li #include "uv_sapi.sapi.h"  // NOLINT(build/include)
23*ec63e07aSXin Li 
24*ec63e07aSXin Li namespace {
25*ec63e07aSXin Li 
26*ec63e07aSXin Li class UVSapiHelloworldSandbox : public uv::UVSandbox {
27*ec63e07aSXin Li  private:
ModifyPolicy(sandbox2::PolicyBuilder *)28*ec63e07aSXin Li   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
29*ec63e07aSXin Li       sandbox2::PolicyBuilder*) override {
30*ec63e07aSXin Li     return sandbox2::PolicyBuilder()
31*ec63e07aSXin Li         .AllowDynamicStartup()
32*ec63e07aSXin Li         .AllowExit()
33*ec63e07aSXin Li         .AllowFutexOp(FUTEX_WAKE_PRIVATE)
34*ec63e07aSXin Li         .AllowSyscalls({__NR_epoll_create1, __NR_eventfd2, __NR_pipe2})
35*ec63e07aSXin Li         .AllowWrite()
36*ec63e07aSXin Li         .BuildOrDie();
37*ec63e07aSXin Li   }
38*ec63e07aSXin Li };
39*ec63e07aSXin Li 
HelloWorld()40*ec63e07aSXin Li absl::Status HelloWorld() {
41*ec63e07aSXin Li   // Initialize sandbox2 and sapi
42*ec63e07aSXin Li   UVSapiHelloworldSandbox sandbox;
43*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(sandbox.Init());
44*ec63e07aSXin Li   uv::UVApi api(&sandbox);
45*ec63e07aSXin Li 
46*ec63e07aSXin Li   // Allocate memory for the uv_loop_t object
47*ec63e07aSXin Li   void* loop_voidptr;
48*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(
49*ec63e07aSXin Li       sandbox.rpc_channel()->Allocate(sizeof(uv_loop_t), &loop_voidptr));
50*ec63e07aSXin Li   sapi::v::RemotePtr loop(loop_voidptr);
51*ec63e07aSXin Li 
52*ec63e07aSXin Li   int return_code;
53*ec63e07aSXin Li 
54*ec63e07aSXin Li   // Initialize loop
55*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_init(&loop));
56*ec63e07aSXin Li   if (return_code != 0) {
57*ec63e07aSXin Li     return absl::UnavailableError("uv_loop_init returned error " + return_code);
58*ec63e07aSXin Li   }
59*ec63e07aSXin Li 
60*ec63e07aSXin Li   std::cout << "The loop is about to quit" << std::endl;
61*ec63e07aSXin Li 
62*ec63e07aSXin Li   // Run loop
63*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_run(&loop, UV_RUN_DEFAULT));
64*ec63e07aSXin Li   if (return_code != 0) {
65*ec63e07aSXin Li     return absl::UnavailableError("uv_run returned error " + return_code);
66*ec63e07aSXin Li   }
67*ec63e07aSXin Li 
68*ec63e07aSXin Li   // Close loop
69*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_close(&loop));
70*ec63e07aSXin Li   if (return_code != 0) {
71*ec63e07aSXin Li     return absl::UnavailableError("uv_loop_close returned error " +
72*ec63e07aSXin Li                                   return_code);
73*ec63e07aSXin Li   }
74*ec63e07aSXin Li 
75*ec63e07aSXin Li   return absl::OkStatus();
76*ec63e07aSXin Li }
77*ec63e07aSXin Li 
78*ec63e07aSXin Li }  // namespace
79*ec63e07aSXin Li 
main(int argc,char * argv[])80*ec63e07aSXin Li int main(int argc, char* argv[]) {
81*ec63e07aSXin Li   gflags::ParseCommandLineFlags(&argc, &argv, true);
82*ec63e07aSXin Li   sapi::InitLogging(argv[0]);
83*ec63e07aSXin Li 
84*ec63e07aSXin Li   if (absl::Status status = HelloWorld(); !status.ok()) {
85*ec63e07aSXin Li     LOG(ERROR) << "HelloWorld failed: " << status.ToString();
86*ec63e07aSXin Li     return EXIT_FAILURE;
87*ec63e07aSXin Li   }
88*ec63e07aSXin Li 
89*ec63e07aSXin Li   return EXIT_SUCCESS;
90*ec63e07aSXin Li }
91