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