xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libuv/examples/idle-basic.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <linux/futex.h>
16 #include <syscall.h>
17 #include <uv.h>
18 
19 #include <iostream>
20 
21 #include "absl/flags/flag.h"
22 #include "absl/log/initialize.h"
23 #include "uv_sapi.sapi.h"  // NOLINT(build/include)
24 
25 namespace {
26 
27 class UVSapiIdleBasicSandbox : public uv::UVSandbox {
28  private:
ModifyPolicy(sandbox2::PolicyBuilder *)29   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
30       sandbox2::PolicyBuilder*) override {
31     return sandbox2::PolicyBuilder()
32         .AllowDynamicStartup()
33         .AllowExit()
34         .AllowFutexOp(FUTEX_WAKE_PRIVATE)
35         .AllowEpoll()
36         .AllowSyscall(__NR_eventfd2)
37         .AllowPipe()
38         .AllowWrite()
39         .BuildOrDie();
40   }
41 };
42 
IdleBasic()43 absl::Status IdleBasic() {
44   // Initialize sandbox2 and sapi
45   UVSapiIdleBasicSandbox sandbox;
46   SAPI_RETURN_IF_ERROR(sandbox.Init());
47   uv::UVApi api(&sandbox);
48 
49   // Get remote pointer to the IdleCallback method
50   void* function_ptr;
51   SAPI_RETURN_IF_ERROR(
52       sandbox.rpc_channel()->Symbol("IdleCallback", &function_ptr));
53   sapi::v::RemotePtr idle_callback(function_ptr);
54 
55   // Allocate memory for the uv_idle_t object
56   void* idle_voidptr;
57   SAPI_RETURN_IF_ERROR(
58       sandbox.rpc_channel()->Allocate(sizeof(uv_idle_t), &idle_voidptr));
59   sapi::v::RemotePtr idler(idle_voidptr);
60 
61   int return_code;
62 
63   // Get default loop
64   SAPI_ASSIGN_OR_RETURN(void* loop_voidptr, api.sapi_uv_default_loop());
65   sapi::v::RemotePtr loop(loop_voidptr);
66 
67   // Initialize idler
68   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_idle_init(&loop, &idler));
69   if (return_code != 0) {
70     return absl::UnavailableError("sapi_uv_idle_init returned error " +
71                                   return_code);
72   }
73 
74   // Start idler
75   SAPI_ASSIGN_OR_RETURN(return_code,
76                         api.sapi_uv_idle_start(&idler, &idle_callback));
77   if (return_code != 0) {
78     return absl::UnavailableError("sapi_uv_idle_start returned error " +
79                                   return_code);
80   }
81 
82   // Run loop
83   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_run(&loop, UV_RUN_DEFAULT));
84   if (return_code != 0) {
85     return absl::UnavailableError("uv_run returned error " + return_code);
86   }
87 
88   // Close idler
89   sapi::v::NullPtr null_ptr;
90   SAPI_RETURN_IF_ERROR(api.sapi_uv_close(&idler, &null_ptr));
91 
92   // Close loop
93   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_close(&loop));
94   // UV_EBUSY is accepted because it is the return code of uv_loop_close
95   // in the original example
96   if (return_code != 0 && return_code != UV_EBUSY) {
97     return absl::UnavailableError("uv_loop_close returned error " +
98                                   return_code);
99   }
100 
101   return absl::OkStatus();
102 }
103 
104 }  // namespace
105 
main(int argc,char * argv[])106 int main(int argc, char* argv[]) {
107   absl::ParseCommandLine(argc, argv);
108   absl::InitializeLog();
109 
110   if (absl::Status status = IdleBasic(); !status.ok()) {
111     LOG(ERROR) << "IdleBasic failed: " << status.ToString();
112     return EXIT_FAILURE;
113   }
114 
115   return EXIT_SUCCESS;
116 }
117