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 "absl/log/initialize.h"
23*ec63e07aSXin Li #include "uv_sapi.sapi.h" // NOLINT(build/include)
24*ec63e07aSXin Li
25*ec63e07aSXin Li namespace {
26*ec63e07aSXin Li
27*ec63e07aSXin Li class UVSapiIdleBasicSandbox : public uv::UVSandbox {
28*ec63e07aSXin Li private:
ModifyPolicy(sandbox2::PolicyBuilder *)29*ec63e07aSXin Li std::unique_ptr<sandbox2::Policy> ModifyPolicy(
30*ec63e07aSXin Li sandbox2::PolicyBuilder*) override {
31*ec63e07aSXin Li return sandbox2::PolicyBuilder()
32*ec63e07aSXin Li .AllowDynamicStartup()
33*ec63e07aSXin Li .AllowExit()
34*ec63e07aSXin Li .AllowFutexOp(FUTEX_WAKE_PRIVATE)
35*ec63e07aSXin Li .AllowEpoll()
36*ec63e07aSXin Li .AllowSyscall(__NR_eventfd2)
37*ec63e07aSXin Li .AllowPipe()
38*ec63e07aSXin Li .AllowWrite()
39*ec63e07aSXin Li .BuildOrDie();
40*ec63e07aSXin Li }
41*ec63e07aSXin Li };
42*ec63e07aSXin Li
IdleBasic()43*ec63e07aSXin Li absl::Status IdleBasic() {
44*ec63e07aSXin Li // Initialize sandbox2 and sapi
45*ec63e07aSXin Li UVSapiIdleBasicSandbox sandbox;
46*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(sandbox.Init());
47*ec63e07aSXin Li uv::UVApi api(&sandbox);
48*ec63e07aSXin Li
49*ec63e07aSXin Li // Get remote pointer to the IdleCallback method
50*ec63e07aSXin Li void* function_ptr;
51*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(
52*ec63e07aSXin Li sandbox.rpc_channel()->Symbol("IdleCallback", &function_ptr));
53*ec63e07aSXin Li sapi::v::RemotePtr idle_callback(function_ptr);
54*ec63e07aSXin Li
55*ec63e07aSXin Li // Allocate memory for the uv_idle_t object
56*ec63e07aSXin Li void* idle_voidptr;
57*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(
58*ec63e07aSXin Li sandbox.rpc_channel()->Allocate(sizeof(uv_idle_t), &idle_voidptr));
59*ec63e07aSXin Li sapi::v::RemotePtr idler(idle_voidptr);
60*ec63e07aSXin Li
61*ec63e07aSXin Li int return_code;
62*ec63e07aSXin Li
63*ec63e07aSXin Li // Get default loop
64*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(void* loop_voidptr, api.sapi_uv_default_loop());
65*ec63e07aSXin Li sapi::v::RemotePtr loop(loop_voidptr);
66*ec63e07aSXin Li
67*ec63e07aSXin Li // Initialize idler
68*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_idle_init(&loop, &idler));
69*ec63e07aSXin Li if (return_code != 0) {
70*ec63e07aSXin Li return absl::UnavailableError("sapi_uv_idle_init returned error " +
71*ec63e07aSXin Li return_code);
72*ec63e07aSXin Li }
73*ec63e07aSXin Li
74*ec63e07aSXin Li // Start idler
75*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(return_code,
76*ec63e07aSXin Li api.sapi_uv_idle_start(&idler, &idle_callback));
77*ec63e07aSXin Li if (return_code != 0) {
78*ec63e07aSXin Li return absl::UnavailableError("sapi_uv_idle_start returned error " +
79*ec63e07aSXin Li return_code);
80*ec63e07aSXin Li }
81*ec63e07aSXin Li
82*ec63e07aSXin Li // Run loop
83*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_run(&loop, UV_RUN_DEFAULT));
84*ec63e07aSXin Li if (return_code != 0) {
85*ec63e07aSXin Li return absl::UnavailableError("uv_run returned error " + return_code);
86*ec63e07aSXin Li }
87*ec63e07aSXin Li
88*ec63e07aSXin Li // Close idler
89*ec63e07aSXin Li sapi::v::NullPtr null_ptr;
90*ec63e07aSXin Li SAPI_RETURN_IF_ERROR(api.sapi_uv_close(&idler, &null_ptr));
91*ec63e07aSXin Li
92*ec63e07aSXin Li // Close loop
93*ec63e07aSXin Li SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_close(&loop));
94*ec63e07aSXin Li // UV_EBUSY is accepted because it is the return code of uv_loop_close
95*ec63e07aSXin Li // in the original example
96*ec63e07aSXin Li if (return_code != 0 && return_code != UV_EBUSY) {
97*ec63e07aSXin Li return absl::UnavailableError("uv_loop_close returned error " +
98*ec63e07aSXin Li return_code);
99*ec63e07aSXin Li }
100*ec63e07aSXin Li
101*ec63e07aSXin Li return absl::OkStatus();
102*ec63e07aSXin Li }
103*ec63e07aSXin Li
104*ec63e07aSXin Li } // namespace
105*ec63e07aSXin Li
main(int argc,char * argv[])106*ec63e07aSXin Li int main(int argc, char* argv[]) {
107*ec63e07aSXin Li absl::ParseCommandLine(argc, argv);
108*ec63e07aSXin Li absl::InitializeLog();
109*ec63e07aSXin Li
110*ec63e07aSXin Li if (absl::Status status = IdleBasic(); !status.ok()) {
111*ec63e07aSXin Li LOG(ERROR) << "IdleBasic failed: " << status.ToString();
112*ec63e07aSXin Li return EXIT_FAILURE;
113*ec63e07aSXin Li }
114*ec63e07aSXin Li
115*ec63e07aSXin Li return EXIT_SUCCESS;
116*ec63e07aSXin Li }
117