diff --git a/sandboxed_api/sandbox2/BUILD.bazel b/sandboxed_api/sandbox2/BUILD.bazel index fb27b8b..3f84537 100644 --- a/sandboxed_api/sandbox2/BUILD.bazel +++ b/sandboxed_api/sandbox2/BUILD.bazel @@ -291,6 +291,7 @@ cc_library( "//sandboxed_api/util:status", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/memory", + "@com_google_absl//absl/status", "@com_google_absl//absl/strings", "@com_google_absl//absl/types:span", ], @@ -408,7 +409,6 @@ cc_library( "//sandboxed_api/util:fileops", "//sandboxed_api/util:raw_logging", "//sandboxed_api/util:status", - "//sandboxed_api/util:strerror", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/status", diff --git a/sandboxed_api/sandbox2/CMakeLists.txt b/sandboxed_api/sandbox2/CMakeLists.txt index e3ab21d..74416bd 100644 --- a/sandboxed_api/sandbox2/CMakeLists.txt +++ b/sandboxed_api/sandbox2/CMakeLists.txt @@ -282,6 +282,7 @@ target_link_libraries(sandbox2_executor sapi::base sapi::status_proto PUBLIC absl::span + absl::status absl::strings glog::glog sapi::config diff --git a/sandboxed_api/sandbox2/buffer.cc b/sandboxed_api/sandbox2/buffer.cc index 3011f39..eef7198 100644 --- a/sandboxed_api/sandbox2/buffer.cc +++ b/sandboxed_api/sandbox2/buffer.cc @@ -25,7 +25,6 @@ #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" #include "sandboxed_api/sandbox2/util.h" -#include "sandboxed_api/util/os_error.h" namespace sandbox2 { @@ -35,8 +34,7 @@ absl::StatusOr> Buffer::CreateFromFd(int fd) { struct stat stat_buf; if (fstat(fd, &stat_buf) != 0) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Could not stat buffer fd")); + return absl::ErrnoToStatus(errno, "Could not stat buffer fd"); } size_t size = stat_buf.st_size; int prot = PROT_READ | PROT_WRITE; @@ -45,8 +43,7 @@ absl::StatusOr> Buffer::CreateFromFd(int fd) { buffer->buf_ = reinterpret_cast(mmap(nullptr, size, prot, flags, fd, offset)); if (buffer->buf_ == MAP_FAILED) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Could not map buffer fd")); + return absl::ErrnoToStatus(errno, "Could not map buffer fd"); } buffer->fd_ = fd; buffer->size_ = size; @@ -61,8 +58,7 @@ absl::StatusOr> Buffer::CreateWithSize(size_t size) { return absl::InternalError("Could not create buffer temp file"); } if (ftruncate(fd, size) != 0) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Could not extend buffer fd")); + return absl::ErrnoToStatus(errno, "Could not extend buffer fd"); } return CreateFromFd(fd); } diff --git a/sandboxed_api/sandbox2/examples/network_proxy/networkproxy_bin.cc b/sandboxed_api/sandbox2/examples/network_proxy/networkproxy_bin.cc index c62bad7..bf47e84 100644 --- a/sandboxed_api/sandbox2/examples/network_proxy/networkproxy_bin.cc +++ b/sandboxed_api/sandbox2/examples/network_proxy/networkproxy_bin.cc @@ -20,7 +20,6 @@ #include "sandboxed_api/sandbox2/comms.h" #include "sandboxed_api/sandbox2/network_proxy/client.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/status_macros.h" ABSL_FLAG(bool, connect_with_handler, true, "Connect using automatic mode."); @@ -64,9 +63,8 @@ absl::StatusOr CreateAddres(int port) { saddr.sin6_family = AF_INET6; saddr.sin6_port = htons(port); - int err = inet_pton(AF_INET6, "::1", &saddr.sin6_addr); - if (err <= 0) { - return absl::InternalError(sapi::OsErrorMessage(errno, "socket() failed")); + if (int err = inet_pton(AF_INET6, "::1", &saddr.sin6_addr); err <= 0) { + return absl::ErrnoToStatus(errno, "socket()"); } return saddr; } @@ -91,7 +89,7 @@ absl::StatusOr ConnectToServer(int port) { sapi::file_util::fileops::FDCloser s(socket(AF_INET6, SOCK_STREAM, 0)); if (s.get() < 0) { - return absl::InternalError(sapi::OsErrorMessage(errno, "socket() failed")); + return absl::ErrnoToStatus(errno, "socket()"); } if (absl::GetFlag(FLAGS_connect_with_handler)) { diff --git a/sandboxed_api/sandbox2/executor.cc b/sandboxed_api/sandbox2/executor.cc index 9286379..e016487 100644 --- a/sandboxed_api/sandbox2/executor.cc +++ b/sandboxed_api/sandbox2/executor.cc @@ -26,6 +26,7 @@ #include #include "absl/memory/memory.h" +#include "absl/status/status.h" #include "absl/strings/match.h" #include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" @@ -36,7 +37,6 @@ #include "sandboxed_api/sandbox2/ipc.h" #include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" namespace sandbox2 { @@ -92,10 +92,10 @@ absl::StatusOr Executor::StartSubProcess( exec_fd_ = file_util::fileops::FDCloser(open(path_.c_str(), O_PATH)); if (exec_fd_.get() < 0) { if (errno == ENOENT) { - return absl::NotFoundError(sapi::OsErrorMessage(errno, path_)); + return absl::ErrnoToStatus(errno, path_); } - return absl::InternalError( - sapi::OsErrorMessage(errno, "Could not open file ", path_)); + return absl::ErrnoToStatus(errno, + absl::StrCat("Could not open file ", path_)); } } @@ -159,8 +159,8 @@ absl::StatusOr Executor::StartSubProcess( absl::StrCat("/proc/", libunwind_sbox_for_pid_, "/ns/user"); ns_fd = file_util::fileops::FDCloser(open(ns_path.c_str(), O_RDONLY)); if (ns_fd.get() == -1) { - return absl::InternalError(sapi::OsErrorMessage( - errno, "Could not open user ns fd (", ns_path, ")")); + return absl::ErrnoToStatus( + errno, absl::StrCat("Could not open user ns fd (", ns_path, ")")); } } diff --git a/sandboxed_api/sandbox2/forkserver.cc b/sandboxed_api/sandbox2/forkserver.cc index bda5593..de64732 100644 --- a/sandboxed_api/sandbox2/forkserver.cc +++ b/sandboxed_api/sandbox2/forkserver.cc @@ -59,7 +59,6 @@ #include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util/bpf_helper.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/strerror.h" @@ -172,8 +171,7 @@ absl::Status SendPid(int signaling_fd) { // on the socket. char dummy = ' '; if (TEMP_FAILURE_RETRY(send(signaling_fd, &dummy, 1, 0)) != 1) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Sending PID: send()")); + return absl::ErrnoToStatus(errno, "Sending PID: send()"); } return absl::OkStatus(); } @@ -197,15 +195,14 @@ absl::StatusOr ReceivePid(int signaling_fd) { iov.iov_len = sizeof(char); if (TEMP_FAILURE_RETRY(recvmsg(signaling_fd, &msgh, MSG_WAITALL)) != 1) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Receiving pid failed: recvmsg")); + return absl::ErrnoToStatus(errno, "Receiving pid failed: recvmsg"); } struct cmsghdr* cmsgp = CMSG_FIRSTHDR(&msgh); if (cmsgp->cmsg_len != CMSG_LEN(sizeof(struct ucred)) || cmsgp->cmsg_level != SOL_SOCKET || cmsgp->cmsg_type != SCM_CREDENTIALS) { return absl::InternalError("Receiving pid failed"); } - struct ucred* ucredp = reinterpret_cast(CMSG_DATA(cmsgp)); + auto* ucredp = reinterpret_cast(CMSG_DATA(cmsgp)); return ucredp->pid; } diff --git a/sandboxed_api/sandbox2/global_forkclient.cc b/sandboxed_api/sandbox2/global_forkclient.cc index db7fb87..b00a7f2 100644 --- a/sandboxed_api/sandbox2/global_forkclient.cc +++ b/sandboxed_api/sandbox2/global_forkclient.cc @@ -47,7 +47,6 @@ #include "sandboxed_api/sandbox2/forkserver_bin_embed.h" #include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/status_macros.h" @@ -139,9 +138,9 @@ absl::StatusOr> StartGlobalForkServer() { if (exec_fd < 0) { // For Android we expect the forkserver_bin in the flag if constexpr (sapi::host_os::IsAndroid()) { - return absl::InternalError(sapi::OsErrorMessage( + return absl::ErrnoToStatus( errno, - "Open init binary passed via --sandbox2_forkserver_binary_path")); + "Open init binary passed via --sandbox2_forkserver_binary_path"); } // Extract the fd when it's owned by EmbedFile exec_fd = sapi::EmbedFile::instance()->GetDupFdForFileToc( @@ -156,15 +155,13 @@ absl::StatusOr> StartGlobalForkServer() { int sv[2]; if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Creating socket pair failed")); + return absl::ErrnoToStatus(errno, "Creating socket pair failed"); } // Fork the fork-server, and clean-up the resources (close remote sockets). pid_t pid = util::ForkWithFlags(SIGCHLD); if (pid == -1) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "Forking forkserver process failed")); + return absl::ErrnoToStatus(errno, "Forking forkserver process failed"); } // Child. diff --git a/sandboxed_api/sandbox2/network_proxy/client.cc b/sandboxed_api/sandbox2/network_proxy/client.cc index c4d246f..84a1001 100644 --- a/sandboxed_api/sandbox2/network_proxy/client.cc +++ b/sandboxed_api/sandbox2/network_proxy/client.cc @@ -28,7 +28,6 @@ #include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "sandboxed_api/config.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/status_macros.h" namespace sandbox2 { @@ -120,8 +119,7 @@ absl::Status NetworkProxyClient::ReceiveRemoteResult() { } if (result != 0) { errno = result; - return absl::InternalError( - sapi::OsErrorMessage(errno, "Error in network proxy server")); + return absl::ErrnoToStatus(errno, "Error in network proxy server"); } return absl::OkStatus(); } diff --git a/sandboxed_api/sandbox2/network_proxy/filtering.cc b/sandboxed_api/sandbox2/network_proxy/filtering.cc index 230b82a..ea6e461 100644 --- a/sandboxed_api/sandbox2/network_proxy/filtering.cc +++ b/sandboxed_api/sandbox2/network_proxy/filtering.cc @@ -23,7 +23,6 @@ #include "absl/strings/numbers.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/status_macros.h" namespace sandbox2 { @@ -66,15 +65,12 @@ absl::StatusOr AddrToString(const struct sockaddr* saddr) { static absl::Status IPStringToAddr(const std::string& ip, int address_family, void* addr) { - int err = inet_pton(address_family, ip.c_str(), addr); - if (err == 0) { + if (int err = inet_pton(address_family, ip.c_str(), addr); err == 0) { return absl::InvalidArgumentError(absl::StrCat("Invalid address: ", ip)); + } else if (err == -1) { + return absl::ErrnoToStatus(errno, + absl::StrCat("inet_pton() failed for ", ip)); } - if (err == -1) { - return absl::InternalError(sapi::OsErrorMessage( - errno, absl::StrCat("inet_pton() failed for ", ip))); - } - return absl::OkStatus(); } diff --git a/sandboxed_api/sandbox2/regs.cc b/sandboxed_api/sandbox2/regs.cc index f655c51..4d1eee5 100644 --- a/sandboxed_api/sandbox2/regs.cc +++ b/sandboxed_api/sandbox2/regs.cc @@ -27,7 +27,6 @@ #include "absl/status/status.h" #include "absl/strings/str_cat.h" #include "sandboxed_api/config.h" -#include "sandboxed_api/util/os_error.h" namespace sandbox2 { @@ -38,8 +37,8 @@ namespace sandbox2 { absl::Status Regs::Fetch() { #ifdef SAPI_X86_64 if (ptrace(PTRACE_GETREGS, pid_, 0, &user_regs_) == -1L) { - return absl::InternalError(sapi::OsErrorMessage( - errno, absl::StrCat("ptrace(PTRACE_GETREGS, pid=", pid_, ") failed"))); + return absl::ErrnoToStatus( + errno, absl::StrCat("ptrace(PTRACE_GETREGS, pid=", pid_, ") failed")); } #endif if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() || @@ -47,9 +46,9 @@ absl::Status Regs::Fetch() { iovec pt_iov = {&user_regs_, sizeof(user_regs_)}; if (ptrace(PTRACE_GETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) { - return absl::InternalError(sapi::OsErrorMessage( + return absl::ErrnoToStatus( errno, - absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, ") failed"))); + absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, ") failed")); } if (pt_iov.iov_len != sizeof(user_regs_)) { return absl::InternalError(absl::StrCat( @@ -63,9 +62,9 @@ absl::Status Regs::Fetch() { iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)}; if (ptrace(PTRACE_GETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) { - return absl::InternalError(sapi::OsErrorMessage( + return absl::ErrnoToStatus( errno, absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, - ", NT_ARM_SYSTEM_CALL)"))); + ", NT_ARM_SYSTEM_CALL)")); } if (sys_iov.iov_len != sizeof(syscall_number_)) { return absl::InternalError(absl::StrCat( @@ -82,8 +81,8 @@ absl::Status Regs::Fetch() { absl::Status Regs::Store() { #ifdef SAPI_X86_64 if (ptrace(PTRACE_SETREGS, pid_, 0, &user_regs_) == -1) { - return absl::InternalError(sapi::OsErrorMessage( - errno, absl::StrCat("ptrace(PTRACE_SETREGS, pid=", pid_, ")"))); + return absl::ErrnoToStatus( + errno, absl::StrCat("ptrace(PTRACE_SETREGS, pid=", pid_, ")")); } #endif if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() || @@ -91,9 +90,9 @@ absl::Status Regs::Store() { iovec pt_iov = {&user_regs_, sizeof(user_regs_)}; if (ptrace(PTRACE_SETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) { - return absl::InternalError(sapi::OsErrorMessage( + return absl::ErrnoToStatus( errno, - absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, ") failed"))); + absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, ") failed")); } // Store syscall number on AArch64. @@ -101,9 +100,9 @@ absl::Status Regs::Store() { iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)}; if (ptrace(PTRACE_SETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) { - return absl::InternalError(sapi::OsErrorMessage( + return absl::ErrnoToStatus( errno, absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, - ", NT_ARM_SYSTEM_CALL) failed"))); + ", NT_ARM_SYSTEM_CALL) failed")); } } } diff --git a/sandboxed_api/sandbox2/sanitizer.cc b/sandboxed_api/sandbox2/sanitizer.cc index bc5988d..68cca60 100644 --- a/sandboxed_api/sandbox2/sanitizer.cc +++ b/sandboxed_api/sandbox2/sanitizer.cc @@ -16,6 +16,8 @@ #include "sandboxed_api/sandbox2/sanitizer.h" +#include "absl/status/status.h" + #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \ defined(ABSL_HAVE_LEAK_SANITIZER) || \ @@ -45,10 +47,8 @@ #include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/util/file_helpers.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/status_macros.h" -#include "sandboxed_api/util/strerror.h" namespace sandbox2::sanitizer { namespace { @@ -126,12 +126,13 @@ absl::Status MarkAllFDsAsCOEExcept( int flags = fcntl(fd, F_GETFD); if (flags == -1) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "fcntl(", fd, ", F_GETFD) failed")); + return absl::ErrnoToStatus( + errno, absl::StrCat("fcntl(", fd, ", F_GETFD) failed")); } if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { - return absl::InternalError(sapi::OsErrorMessage( - errno, "fcntl(", fd, ", F_SETFD, ", flags, " | FD_CLOEXEC) failed")); + return absl::ErrnoToStatus( + errno, absl::StrCat("fcntl(", fd, ", F_SETFD, ", flags, + " | FD_CLOEXEC) failed")); } } @@ -184,8 +185,8 @@ absl::Status SanitizeCurrentProcess( // If the parent goes down, so should we. if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "prctl(PR_SET_PDEATHSIG, SIGKILL) failed")); + return absl::ErrnoToStatus(errno, + "prctl(PR_SET_PDEATHSIG, SIGKILL) failed"); } // Close or mark as close-on-exec open file descriptors. diff --git a/sandboxed_api/sandbox2/util.cc b/sandboxed_api/sandbox2/util.cc index 3ade7b5..e3cafdb 100644 --- a/sandboxed_api/sandbox2/util.cc +++ b/sandboxed_api/sandbox2/util.cc @@ -50,7 +50,6 @@ #include "sandboxed_api/config.h" #include "sandboxed_api/util/file_helpers.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/path.h" #include "sandboxed_api/util/raw_logging.h" @@ -278,7 +277,7 @@ absl::StatusOr Communicate(const std::vector& argv, posix_spawn_file_actions_t action; if (pipe(cout_pipe) == -1) { - return absl::UnknownError(sapi::OsErrorMessage(errno, "creating pipe")); + return absl::ErrnoToStatus(errno, "creating pipe"); } file_util::fileops::FDCloser cout_closer{cout_pipe[1]}; @@ -301,7 +300,7 @@ absl::StatusOr Communicate(const std::vector& argv, if (posix_spawnp(&pid, args.array()[0], &action, nullptr, const_cast(args.data()), const_cast(envp.data())) != 0) { - return absl::UnknownError(sapi::OsErrorMessage(errno, "posix_spawnp()")); + return absl::ErrnoToStatus(errno, "posix_spawnp()"); } // Close child end of the pipe. @@ -312,8 +311,7 @@ absl::StatusOr Communicate(const std::vector& argv, int bytes_read = TEMP_FAILURE_RETRY(read(cout_pipe[0], &buffer[0], buffer.length())); if (bytes_read < 0) { - return absl::InternalError( - sapi::OsErrorMessage(errno, "reading from cout pipe")); + return absl::ErrnoToStatus(errno, "reading from cout pipe"); } if (bytes_read == 0) { break; // Nothing left to read @@ -412,13 +410,12 @@ absl::StatusOr ReadCPathFromPid(pid_t pid, uintptr_t ptr) { SAPI_RAW_VLOG(4, "ReadCPathFromPid (iovec): len1: %zu, len2: %zu", len1, len2); - ssize_t sz = process_vm_readv(pid, local_iov, ABSL_ARRAYSIZE(local_iov), - remote_iov, ABSL_ARRAYSIZE(remote_iov), 0); - if (sz < 0) { - return absl::InternalError(sapi::OsErrorMessage( + if (process_vm_readv(pid, local_iov, ABSL_ARRAYSIZE(local_iov), remote_iov, + ABSL_ARRAYSIZE(remote_iov), 0) < 0) { + return absl::ErrnoToStatus( errno, absl::StrFormat("process_vm_readv() failed for PID: %d at address: %#x", - pid, reinterpret_cast(ptr)))); + pid, reinterpret_cast(ptr))); } // Check for whether there's a NUL byte in the buffer. If not, it's an diff --git a/sandboxed_api/sandbox2/util/minielf.cc b/sandboxed_api/sandbox2/util/minielf.cc index 090e7a2..bb5f49d 100644 --- a/sandboxed_api/sandbox2/util/minielf.cc +++ b/sandboxed_api/sandbox2/util/minielf.cc @@ -27,7 +27,6 @@ #include "absl/strings/str_cat.h" #include "sandboxed_api/config.h" #include "sandboxed_api/sandbox2/util.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/status_macros.h" @@ -62,8 +61,7 @@ namespace { // NOLINTNEXTLINE absl::Status CheckedFSeek(FILE* f, long offset, int whence) { if (fseek(f, offset, whence)) { - return absl::FailedPreconditionError( - sapi::OsErrorMessage(errno, "Fseek on ELF failed")); + return absl::ErrnoToStatus(errno, "Fseek on ELF failed"); } return absl::OkStatus(); } @@ -72,8 +70,7 @@ absl::Status CheckedFRead(void* dst, size_t size, size_t nmemb, FILE* f) { if (std::fread(dst, size, nmemb, f) == nmemb) { return absl::OkStatus(); } - return absl::FailedPreconditionError( - sapi::OsErrorMessage(errno, "Reading ELF data failed")); + return absl::ErrnoToStatus(errno, "Reading ELF data failed"); } absl::Status CheckedRead(std::string* s, FILE* f) { @@ -479,8 +476,8 @@ absl::StatusOr ElfParser::Parse(const std::string& filename, uint32_t features) { ElfParser parser; if (parser.elf_ = std::fopen(filename.c_str(), "r"); !parser.elf_) { - return absl::UnknownError(sapi::OsErrorMessage( - errno, absl::StrCat("cannot open file: ", filename))); + return absl::ErrnoToStatus(errno, + absl::StrCat("cannot open file: ", filename)); } // Basic sanity check. diff --git a/sandboxed_api/util/BUILD.bazel b/sandboxed_api/util/BUILD.bazel index 8b73ef4..fe2e7f1 100644 --- a/sandboxed_api/util/BUILD.bazel +++ b/sandboxed_api/util/BUILD.bazel @@ -149,7 +149,6 @@ cc_library( name = "status", srcs = ["status.cc"], hdrs = [ - "os_error.h", "status.h", "status_macros.h", ], diff --git a/sandboxed_api/util/CMakeLists.txt b/sandboxed_api/util/CMakeLists.txt index eacb5e4..96e6a92 100644 --- a/sandboxed_api/util/CMakeLists.txt +++ b/sandboxed_api/util/CMakeLists.txt @@ -105,7 +105,6 @@ target_link_libraries(sapi_util_status_proto # sandboxed_api/util:status add_library(sapi_util_status ${SAPI_LIB_TYPE} - os_error.h status.cc status.h status_macros.h diff --git a/sandboxed_api/util/os_error.h b/sandboxed_api/util/os_error.h deleted file mode 100644 index f8c3fbd..0000000 --- a/sandboxed_api/util/os_error.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2021 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 -// -// https://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. - -#ifndef SANDBOXED_API_UTIL_OS_ERROR_H_ -#define SANDBOXED_API_UTIL_OS_ERROR_H_ - -#include - -#include "absl/strings/str_cat.h" -#include "sandboxed_api/util/strerror.h" - -namespace sapi { - -// Returns a formatted message with the result of `StrError(error_number)` -// appended. -template -std::string OsErrorMessage(int error_number, const Arg&... args) { - return absl::StrCat(std::forward(args)..., ": ", - ::sapi::StrError(error_number)); -} - -} // namespace sapi - -#endif // SANDBOXED_API_UTIL_OS_ERROR_H_ diff --git a/sandboxed_api/util/temp_file.cc b/sandboxed_api/util/temp_file.cc index 12585c7..529f4dc 100644 --- a/sandboxed_api/util/temp_file.cc +++ b/sandboxed_api/util/temp_file.cc @@ -26,7 +26,6 @@ #include "absl/status/statusor.h" #include "absl/strings/str_cat.h" #include "sandboxed_api/util/fileops.h" -#include "sandboxed_api/util/os_error.h" #include "sandboxed_api/util/status_macros.h" namespace sapi { @@ -40,7 +39,7 @@ absl::StatusOr> CreateNamedTempFile( std::string name_template = absl::StrCat(prefix, kMktempSuffix); int fd = mkstemp(&name_template[0]); if (fd < 0) { - return absl::UnknownError(sapi::OsErrorMessage(errno, "mkstemp()")); + return absl::ErrnoToStatus(errno, "mkstemp()"); } return std::pair{std::move(name_template), fd}; } @@ -55,7 +54,7 @@ absl::StatusOr CreateNamedTempFileAndClose( absl::StatusOr CreateTempDir(absl::string_view prefix) { std::string name_template = absl::StrCat(prefix, kMktempSuffix); if (mkdtemp(&name_template[0]) == nullptr) { - return absl::UnknownError(sapi::OsErrorMessage(errno, "mkdtemp()")); + return absl::ErrnoToStatus(errno, "mkdtemp()"); } return name_template; } diff --git a/sandboxed_api/util/temp_file_test.cc b/sandboxed_api/util/temp_file_test.cc index 736080a..426e9bb 100644 --- a/sandboxed_api/util/temp_file_test.cc +++ b/sandboxed_api/util/temp_file_test.cc @@ -39,7 +39,7 @@ TEST(TempFileTest, CreateTempDirTest) { EXPECT_THAT(path, StartsWith(prefix)); EXPECT_THAT(file_util::fileops::Exists(path, false), IsTrue()); EXPECT_THAT(CreateTempDir("non_existing_dir/prefix"), - StatusIs(absl::StatusCode::kUnknown)); + StatusIs(absl::StatusCode::kNotFound)); } TEST(TempFileTest, MakeTempFileTest) { @@ -54,7 +54,7 @@ TEST(TempFileTest, MakeTempFileTest) { EXPECT_THAT(fcntl(fd, F_GETFD), Ne(-1)); EXPECT_THAT(close(fd), Eq(0)); EXPECT_THAT(CreateNamedTempFile("non_existing_dir/prefix"), - StatusIs(absl::StatusCode::kUnknown)); + StatusIs(absl::StatusCode::kNotFound)); } } // namespace