xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/libuv/examples/uvcat.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 UVSapiUVCatSandbox : public uv::UVSandbox {
27*ec63e07aSXin Li  public:
UVSapiUVCatSandbox(std::string filename)28*ec63e07aSXin Li   UVSapiUVCatSandbox(std::string filename) : filename(filename) {}
29*ec63e07aSXin Li 
30*ec63e07aSXin Li  private:
ModifyPolicy(sandbox2::PolicyBuilder *)31*ec63e07aSXin Li   std::unique_ptr<sandbox2::Policy> ModifyPolicy(
32*ec63e07aSXin Li       sandbox2::PolicyBuilder*) override {
33*ec63e07aSXin Li     return sandbox2::PolicyBuilder()
34*ec63e07aSXin Li         .AddFile(filename)
35*ec63e07aSXin Li         .AllowDynamicStartup()
36*ec63e07aSXin Li         .AllowExit()
37*ec63e07aSXin Li         .AllowFork()
38*ec63e07aSXin Li         .AllowFutexOp(FUTEX_WAKE_PRIVATE)
39*ec63e07aSXin Li         .AllowFutexOp(FUTEX_WAIT_PRIVATE)
40*ec63e07aSXin Li         .AllowMmapWithoutExec()
41*ec63e07aSXin Li         .AllowOpen()
42*ec63e07aSXin Li         .AllowEpoll()
43*ec63e07aSXin Li         .AllowSyscall(__NR_eventfd2)
44*ec63e07aSXin Li         .AllowPipe()
45*ec63e07aSXin Li         .AllowSyscall(__NR_prlimit64)
46*ec63e07aSXin Li         .AllowWrite()
47*ec63e07aSXin Li         .BuildOrDie();
48*ec63e07aSXin Li   }
49*ec63e07aSXin Li 
50*ec63e07aSXin Li   std::string filename;
51*ec63e07aSXin Li };
52*ec63e07aSXin Li 
UVCat(std::string filearg)53*ec63e07aSXin Li absl::Status UVCat(std::string filearg) {
54*ec63e07aSXin Li   // Initialize sandbox2 and sapi
55*ec63e07aSXin Li   UVSapiUVCatSandbox sandbox(filearg);
56*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(sandbox.Init());
57*ec63e07aSXin Li   uv::UVApi api(&sandbox);
58*ec63e07aSXin Li 
59*ec63e07aSXin Li   // Get remote pointer to the OnOpen method
60*ec63e07aSXin Li   void* function_ptr;
61*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(sandbox.rpc_channel()->Symbol("OnOpen", &function_ptr));
62*ec63e07aSXin Li   sapi::v::RemotePtr on_open(function_ptr);
63*ec63e07aSXin Li 
64*ec63e07aSXin Li   // Get remote pointer to the open_req variable
65*ec63e07aSXin Li   void* open_req_voidptr;
66*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(
67*ec63e07aSXin Li       sandbox.rpc_channel()->Symbol("open_req", &open_req_voidptr));
68*ec63e07aSXin Li   sapi::v::RemotePtr open_req(open_req_voidptr);
69*ec63e07aSXin Li 
70*ec63e07aSXin Li   // Get default loop
71*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(void* loop_voidptr, api.sapi_uv_default_loop());
72*ec63e07aSXin Li   sapi::v::RemotePtr loop(loop_voidptr);
73*ec63e07aSXin Li 
74*ec63e07aSXin Li   int return_code;
75*ec63e07aSXin Li 
76*ec63e07aSXin Li   // Open file using the OnOpen callback (which will also read and print it)
77*ec63e07aSXin Li   sapi::v::ConstCStr filename(filearg.c_str());
78*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(
79*ec63e07aSXin Li       return_code, api.sapi_uv_fs_open(&loop, &open_req, filename.PtrBefore(),
80*ec63e07aSXin Li                                        O_RDONLY, 0, &on_open));
81*ec63e07aSXin Li   if (return_code != 0) {
82*ec63e07aSXin Li     return absl::UnavailableError("uv_fs_open returned error " + return_code);
83*ec63e07aSXin Li   }
84*ec63e07aSXin Li 
85*ec63e07aSXin Li   // Run loop
86*ec63e07aSXin Li   SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_run(&loop, UV_RUN_DEFAULT));
87*ec63e07aSXin Li   if (return_code != 0) {
88*ec63e07aSXin Li     return absl::UnavailableError("uv_run returned error " + return_code);
89*ec63e07aSXin Li   }
90*ec63e07aSXin Li 
91*ec63e07aSXin Li   // Cleanup the request
92*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(api.sapi_uv_fs_req_cleanup(&open_req));
93*ec63e07aSXin Li 
94*ec63e07aSXin Li   return absl::OkStatus();
95*ec63e07aSXin Li }
96*ec63e07aSXin Li 
97*ec63e07aSXin Li }  // namespace
98*ec63e07aSXin Li 
main(int argc,char * argv[])99*ec63e07aSXin Li int main(int argc, char* argv[]) {
100*ec63e07aSXin Li   gflags::ParseCommandLineFlags(&argc, &argv, true);
101*ec63e07aSXin Li   sapi::InitLogging(argv[0]);
102*ec63e07aSXin Li 
103*ec63e07aSXin Li   if (argc != 2) {
104*ec63e07aSXin Li     LOG(ERROR) << "wrong number of arguments (1 expected)";
105*ec63e07aSXin Li     return EXIT_FAILURE;
106*ec63e07aSXin Li   }
107*ec63e07aSXin Li 
108*ec63e07aSXin Li   if (absl::Status status = UVCat(argv[1]); !status.ok()) {
109*ec63e07aSXin Li     LOG(ERROR) << "UVCat failed: " << status.ToString();
110*ec63e07aSXin Li     return EXIT_FAILURE;
111*ec63e07aSXin Li   }
112*ec63e07aSXin Li 
113*ec63e07aSXin Li   return EXIT_SUCCESS;
114*ec63e07aSXin Li }
115