sandboxed-api/oss-internship-2020/libuv/examples/helloworld.cc

91 lines
2.6 KiB
C++
Raw Normal View History

2020-09-22 22:41:06 +08:00
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <linux/futex.h>
#include <syscall.h>
#include <uv.h>
#include <iostream>
#include "sandboxed_api/util/flag.h"
#include "uv_sapi.sapi.h" // NOLINT(build/include)
2020-09-22 22:41:06 +08:00
2020-09-29 02:41:37 +08:00
namespace {
2020-09-25 23:06:24 +08:00
class UVSapiHelloworldSandbox : public uv::UVSandbox {
2020-09-22 22:41:06 +08:00
private:
std::unique_ptr<sandbox2::Policy> ModifyPolicy(
sandbox2::PolicyBuilder*) override {
return sandbox2::PolicyBuilder()
.AllowDynamicStartup()
.AllowExit()
.AllowFutexOp(FUTEX_WAKE_PRIVATE)
.AllowSyscalls({__NR_epoll_create1, __NR_eventfd2, __NR_pipe2})
.AllowWrite()
.BuildOrDie();
}
};
2020-09-25 23:06:24 +08:00
absl::Status HelloWorld() {
2020-09-22 22:41:06 +08:00
// Initialize sandbox2 and sapi
UVSapiHelloworldSandbox sandbox;
2020-09-25 23:06:24 +08:00
SAPI_RETURN_IF_ERROR(sandbox.Init());
uv::UVApi api(&sandbox);
2020-09-22 22:41:06 +08:00
// Allocate memory for the uv_loop_t object
void* loop_voidptr;
2020-09-25 23:06:24 +08:00
SAPI_RETURN_IF_ERROR(
sandbox.rpc_channel()->Allocate(sizeof(uv_loop_t), &loop_voidptr));
2020-09-22 22:41:06 +08:00
sapi::v::RemotePtr loop(loop_voidptr);
2020-09-25 23:06:24 +08:00
int return_code;
2020-09-22 22:41:06 +08:00
// Initialize loop
2020-09-25 23:06:24 +08:00
SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_init(&loop));
if (return_code != 0) {
return absl::UnavailableError("uv_loop_init returned error " + return_code);
2020-09-22 22:41:06 +08:00
}
std::cout << "The loop is about to quit" << std::endl;
// Run loop
2020-09-25 23:06:24 +08:00
SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_run(&loop, UV_RUN_DEFAULT));
if (return_code != 0) {
return absl::UnavailableError("uv_run returned error " + return_code);
2020-09-22 22:41:06 +08:00
}
// Close loop
2020-09-25 23:06:24 +08:00
SAPI_ASSIGN_OR_RETURN(return_code, api.sapi_uv_loop_close(&loop));
if (return_code != 0) {
return absl::UnavailableError("uv_loop_close returned error " +
return_code);
2020-09-22 22:41:06 +08:00
}
2020-09-25 23:06:24 +08:00
return absl::OkStatus();
}
2020-09-29 02:41:37 +08:00
} // namespace
2020-09-25 23:06:24 +08:00
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
google::InitGoogleLogging(argv[0]);
if (absl::Status status = HelloWorld(); !status.ok()) {
LOG(ERROR) << "HelloWorld failed: " << status.ToString();
return EXIT_FAILURE;
2020-09-22 22:41:06 +08:00
}
return EXIT_SUCCESS;
}