Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 79d8f07a authored by David Pursell's avatar David Pursell Committed by Gerrit Code Review
Browse files

Merge "base: add SystemErrorCodeToString() function."

parents d18aea99 5f787ed2
Loading
Loading
Loading
Loading
+10 −9
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
#include <string>
#include <vector>

#include <android-base/errors.h>
#include <android-base/logging.h>
#include <android-base/macros.h>
#include <android-base/parsenetaddress.h>
@@ -473,7 +474,7 @@ static bool _make_handle_noninheritable(HANDLE h) {
        // Show the handle value to give us a clue in case we have problems
        // with pseudo-handle values.
        fprintf(stderr, "Cannot make handle 0x%p non-inheritable: %s\n",
                h, SystemErrorCodeToString(GetLastError()).c_str());
                h, android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return false;
    }

@@ -489,7 +490,7 @@ static bool _create_anonymous_pipe(unique_handle* pipe_read_out,
    HANDLE pipe_write_raw = NULL;
    if (!CreatePipe(&pipe_read_raw, &pipe_write_raw, sa, 0)) {
        fprintf(stderr, "Cannot create pipe: %s\n",
                SystemErrorCodeToString(GetLastError()).c_str());
                android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return false;
    }

@@ -528,7 +529,7 @@ static unsigned _redirect_pipe_thread(HANDLE h, DWORD nStdHandle) {
                return EXIT_SUCCESS;
            } else {
                fprintf(stderr, "Failed to read from %s: %s\n", output_name,
                        SystemErrorCodeToString(err).c_str());
                        android::base::SystemErrorCodeToString(err).c_str());
                return EXIT_FAILURE;
            }
        }
@@ -540,7 +541,7 @@ static unsigned _redirect_pipe_thread(HANDLE h, DWORD nStdHandle) {
            if (!WriteFile(write_handle, buf, bytes_read, &bytes_written,
                           NULL)) {
                fprintf(stderr, "Failed to write to %s: %s\n", output_name,
                        SystemErrorCodeToString(GetLastError()).c_str());
                        android::base::SystemErrorCodeToString(GetLastError()).c_str());
                return EXIT_FAILURE;
            }

@@ -591,7 +592,7 @@ int launch_server(int server_port)
            FILE_ATTRIBUTE_NORMAL, NULL));
    if (nul_read.get() == INVALID_HANDLE_VALUE) {
        fprintf(stderr, "Cannot open 'nul': %s\n",
                SystemErrorCodeToString(GetLastError()).c_str());
                android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return -1;
    }

@@ -661,7 +662,7 @@ int launch_server(int server_port)
    if ((module_result >= arraysize(program_path)) || (module_result == 0)) {
        // String truncation or some other error.
        fprintf(stderr, "Cannot get executable path: %s\n",
                SystemErrorCodeToString(GetLastError()).c_str());
                android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return -1;
    }

@@ -687,7 +688,7 @@ int launch_server(int server_port)
            &startup,                 /* startup info, i.e. std handles */
            &pinfo )) {
        fprintf(stderr, "Cannot create process: %s\n",
                SystemErrorCodeToString(GetLastError()).c_str());
                android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return -1;
    }

@@ -753,7 +754,7 @@ int launch_server(int server_port)
            fprintf(stderr, "could not read ok from ADB Server%s\n",
                    err == ERROR_BROKEN_PIPE ? "" :
                    android::base::StringPrintf(": %s",
                            SystemErrorCodeToString(err).c_str()).c_str());
                            android::base::SystemErrorCodeToString(err).c_str()).c_str());
        }
    }

@@ -784,7 +785,7 @@ int launch_server(int server_port)

    if (wait_result != WAIT_OBJECT_0) {
        fprintf(stderr, "Unexpected result waiting for threads: %lu: %s\n",
                wait_result, SystemErrorCodeToString(GetLastError()).c_str());
                wait_result, android::base::SystemErrorCodeToString(GetLastError()).c_str());
        return -1;
    }

+2 −2
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include "mincrypt/rsa.h"
#undef RSA_verify

#include <android-base/errors.h>
#include <android-base/strings.h>
#include <cutils/list.h>

@@ -307,8 +308,7 @@ static int get_user_keyfilepath(char *filename, size_t len)
        WCHAR path[MAX_PATH];
        const HRESULT hr = SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path);
        if (FAILED(hr)) {
            D("SHGetFolderPathW failed: %s",
              SystemErrorCodeToString(hr).c_str());
            D("SHGetFolderPathW failed: %s", android::base::SystemErrorCodeToString(hr).c_str());
            return -1;
        }
        if (!android::base::WideToUTF8(path, &home_str)) {
+3 −2
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <sched.h>
#endif

#include <android-base/errors.h>
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
@@ -54,7 +55,7 @@ static std::string GetLogFilePath() {
    if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
        // If string truncation or some other error.
        fatal("cannot retrieve temporary file path: %s\n",
              SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
    }

    std::string temp_path_utf8;
@@ -134,7 +135,7 @@ int adb_server_main(int is_daemon, int server_port, int ack_reply_fd) {
        DWORD written = 0;
        if (!WriteFile(ack_reply_handle, ack, bytes_to_write, &written, NULL)) {
            fatal("adb: cannot write ACK to handle 0x%p: %s", ack_reply_handle,
                  SystemErrorCodeToString(GetLastError()).c_str());
                  android::base::SystemErrorCodeToString(GetLastError()).c_str());
        }
        if (written != bytes_to_write) {
            fatal("adb: cannot write %lu bytes of ACK: only wrote %lu bytes",
+0 −3
Original line number Diff line number Diff line
@@ -257,9 +257,6 @@ static __inline__ int adb_is_absolute_host_path(const char* path) {
    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
}

// Like strerror(), but for Win32 error codes.
std::string SystemErrorCodeToString(DWORD error_code);

// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
//   struct stat s;
//   stat(filename, &s);
+28 −60
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@

#include <cutils/sockets.h>

#include <android-base/errors.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
@@ -93,36 +94,6 @@ static const FHClassRec _fh_socket_class = {
        if (!(cond)) fatal("assertion failed '%s' on %s:%d\n", #cond, __FILE__, __LINE__); \
    } while (0)

std::string SystemErrorCodeToString(const DWORD error_code) {
  const int kErrorMessageBufferSize = 256;
  WCHAR msgbuf[kErrorMessageBufferSize];
  DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
  DWORD len = FormatMessageW(flags, nullptr, error_code, 0, msgbuf,
                             arraysize(msgbuf), nullptr);
  if (len == 0) {
    return android::base::StringPrintf(
        "Error (%lu) while retrieving error. (%lu)", GetLastError(),
        error_code);
  }

  // Convert UTF-16 to UTF-8.
  std::string msg;
  if (!android::base::WideToUTF8(msgbuf, &msg)) {
      return android::base::StringPrintf(
          "Error (%d) converting from UTF-16 to UTF-8 while retrieving error. (%lu)", errno,
          error_code);
  }

  // Messages returned by the system end with line breaks.
  msg = android::base::Trim(msg);
  // There are many Windows error messages compared to POSIX, so include the
  // numeric error code for easier, quicker, accurate identification. Use
  // decimal instead of hex because there are decimal ranges like 10000-11999
  // for Winsock.
  android::base::StringAppendF(&msg, " (%lu)", error_code);
  return msg;
}

void handle_deleter::operator()(HANDLE h) {
    // CreateFile() is documented to return INVALID_HANDLE_FILE on error,
    // implying that NULL is a valid handle, but this is probably impossible.
@@ -134,7 +105,7 @@ void handle_deleter::operator()(HANDLE h) {
    if (h != INVALID_HANDLE_VALUE) {
        if (!CloseHandle(h)) {
            D("CloseHandle(%p) failed: %s", h,
              SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
        }
    }
}
@@ -470,8 +441,7 @@ int adb_open(const char* path, int options)
                return -1;

            default:
                D( "unknown error: %s",
                   SystemErrorCodeToString( err ).c_str() );
                D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
                errno = ENOENT;
                return -1;
        }
@@ -517,8 +487,7 @@ int adb_creat(const char* path, int mode)
                return -1;

            default:
                D( "unknown error: %s",
                   SystemErrorCodeToString( err ).c_str() );
                D("unknown error: %s", android::base::SystemErrorCodeToString(err).c_str());
                errno = ENOENT;
                return -1;
        }
@@ -708,7 +677,7 @@ static void _fh_socket_init( FH f ) {
    f->event     = WSACreateEvent();
    if (f->event == WSA_INVALID_EVENT) {
        D("WSACreateEvent failed: %s",
          SystemErrorCodeToString(WSAGetLastError()).c_str());
          android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());

        // _event_socket_start assumes that this field is INVALID_HANDLE_VALUE
        // on failure, instead of NULL which is what Windows really returns on
@@ -727,19 +696,19 @@ static int _fh_socket_close( FH f ) {
            // minimize logging spam, so don't log these errors for now.
#if 0
            D("socket shutdown failed: %s",
              SystemErrorCodeToString(WSAGetLastError()).c_str());
              android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
#endif
        }
        if (closesocket(f->fh_socket) == SOCKET_ERROR) {
            D("closesocket failed: %s",
              SystemErrorCodeToString(WSAGetLastError()).c_str());
              android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        }
        f->fh_socket = INVALID_SOCKET;
    }
    if (f->event != NULL) {
        if (!CloseHandle(f->event)) {
            D("CloseHandle failed: %s",
              SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
        }
        f->event = NULL;
    }
@@ -760,7 +729,7 @@ static int _fh_socket_read(FH f, void* buf, int len) {
        // that to reduce spam and confusion.
        if (err != WSAEWOULDBLOCK) {
            D("recv fd %d failed: %s", _fh_to_int(f),
              SystemErrorCodeToString(err).c_str());
              android::base::SystemErrorCodeToString(err).c_str());
        }
        _socket_set_errno(err);
        result = -1;
@@ -776,7 +745,7 @@ static int _fh_socket_write(FH f, const void* buf, int len) {
        // that to reduce spam and confusion.
        if (err != WSAEWOULDBLOCK) {
            D("send fd %d failed: %s", _fh_to_int(f),
              SystemErrorCodeToString(err).c_str());
              android::base::SystemErrorCodeToString(err).c_str());
        }
        _socket_set_errno(err);
        result = -1;
@@ -812,7 +781,7 @@ _init_winsock( void )
        int      rc = WSAStartup( MAKEWORD(2,2), &wsaData);
        if (rc != 0) {
            fatal("adb: could not initialize Winsock: %s",
                   SystemErrorCodeToString( rc ).c_str());
                  android::base::SystemErrorCodeToString(rc).c_str());
        }
        _winsock_init = 1;

@@ -870,7 +839,7 @@ int network_loopback_client(int port, int type, std::string* error) {
    s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
    if(s == INVALID_SOCKET) {
        *error = android::base::StringPrintf("cannot create socket: %s",
                SystemErrorCodeToString(WSAGetLastError()).c_str());
                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("%s", error->c_str());
        return -1;
    }
@@ -881,7 +850,7 @@ int network_loopback_client(int port, int type, std::string* error) {
        const DWORD err = WSAGetLastError();
        *error = android::base::StringPrintf("cannot connect to %s:%u: %s",
                inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
                SystemErrorCodeToString(err).c_str());
                android::base::SystemErrorCodeToString(err).c_str());
        D("could not connect to %s:%d: %s",
          type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
        return -1;
@@ -924,7 +893,7 @@ static int _network_server(int port, int type, u_long interface_address,
    s = socket(AF_INET, type, GetSocketProtocolFromSocketType(type));
    if (s == INVALID_SOCKET) {
        *error = android::base::StringPrintf("cannot create socket: %s",
                SystemErrorCodeToString(WSAGetLastError()).c_str());
                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("%s", error->c_str());
        return -1;
    }
@@ -938,7 +907,7 @@ static int _network_server(int port, int type, u_long interface_address,
                   sizeof(n)) == SOCKET_ERROR) {
        *error = android::base::StringPrintf(
                "cannot set socket option SO_EXCLUSIVEADDRUSE: %s",
                SystemErrorCodeToString(WSAGetLastError()).c_str());
                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("%s", error->c_str());
        return -1;
    }
@@ -948,7 +917,7 @@ static int _network_server(int port, int type, u_long interface_address,
        const DWORD err = WSAGetLastError();
        *error = android::base::StringPrintf("cannot bind to %s:%u: %s",
                inet_ntoa(addr.sin_addr), ntohs(addr.sin_port),
                SystemErrorCodeToString(err).c_str());
                android::base::SystemErrorCodeToString(err).c_str());
        D("could not bind to %s:%d: %s",
          type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
        return -1;
@@ -956,7 +925,7 @@ static int _network_server(int port, int type, u_long interface_address,
    if (type == SOCK_STREAM) {
        if (listen(s, LISTEN_BACKLOG) == SOCKET_ERROR) {
            *error = android::base::StringPrintf("cannot listen on socket: %s",
                    SystemErrorCodeToString(WSAGetLastError()).c_str());
                    android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
            D("could not listen on %s:%d: %s",
              type != SOCK_STREAM ? "udp" : "tcp", port, error->c_str());
            return -1;
@@ -1010,7 +979,7 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
    if (getaddrinfo(host.c_str(), port_str, &hints, &addrinfo_ptr) != 0) {
        *error = android::base::StringPrintf(
                "cannot resolve host '%s' and port %s: %s", host.c_str(),
                port_str, SystemErrorCodeToString(WSAGetLastError()).c_str());
                port_str, android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("%s", error->c_str());
        return -1;
    }
@@ -1025,7 +994,7 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
                      addrinfo->ai_protocol);
    if(s == INVALID_SOCKET) {
        *error = android::base::StringPrintf("cannot create socket: %s",
                SystemErrorCodeToString(WSAGetLastError()).c_str());
                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("%s", error->c_str());
        return -1;
    }
@@ -1037,7 +1006,7 @@ int network_connect(const std::string& host, int port, int type, int timeout, st
        // TODO: Use WSAAddressToString or inet_ntop on address.
        *error = android::base::StringPrintf("cannot connect to %s:%s: %s",
                host.c_str(), port_str,
                SystemErrorCodeToString(WSAGetLastError()).c_str());
                android::base::SystemErrorCodeToString(WSAGetLastError()).c_str());
        D("could not connect to %s:%s:%s: %s",
          type != SOCK_STREAM ? "udp" : "tcp", host.c_str(), port_str,
          error->c_str());
@@ -1075,7 +1044,7 @@ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrle
    if (fh->fh_socket == INVALID_SOCKET) {
        const DWORD err = WSAGetLastError();
        LOG(ERROR) << "adb_socket_accept: accept on fd " << serverfd <<
                      " failed: " + SystemErrorCodeToString(err);
                      " failed: " + android::base::SystemErrorCodeToString(err);
        _socket_set_errno( err );
        return -1;
    }
@@ -1106,9 +1075,8 @@ int adb_setsockopt( int fd, int level, int optname, const void* optval, soc
                             reinterpret_cast<const char*>(optval), optlen );
    if ( result == SOCKET_ERROR ) {
        const DWORD err = WSAGetLastError();
        D( "adb_setsockopt: setsockopt on fd %d level %d optname %d "
           "failed: %s\n", fd, level, optname,
           SystemErrorCodeToString(err).c_str() );
        D("adb_setsockopt: setsockopt on fd %d level %d optname %d failed: %s\n",
          fd, level, optname, android::base::SystemErrorCodeToString(err).c_str());
        _socket_set_errno( err );
        result = -1;
    }
@@ -1130,7 +1098,7 @@ int adb_shutdown(int fd)
    if (shutdown(f->fh_socket, SD_BOTH) == SOCKET_ERROR) {
        const DWORD err = WSAGetLastError();
        D("socket shutdown fd %d failed: %s", fd,
          SystemErrorCodeToString(err).c_str());
          android::base::SystemErrorCodeToString(err).c_str());
        _socket_set_errno(err);
        return -1;
    }
@@ -2575,7 +2543,7 @@ static bool _get_key_event_record(const HANDLE console, INPUT_RECORD* const inpu
        memset(input_record, 0, sizeof(*input_record));
        if (!ReadConsoleInputA(console, input_record, 1, &read_count)) {
            D("_get_key_event_record: ReadConsoleInputA() failed: %s\n",
              SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
            errno = EIO;
            return false;
        }
@@ -3344,7 +3312,7 @@ void stdin_raw_init() {
    if (!SetConsoleMode(in, new_console_mode)) {
        // This really should not fail.
        D("stdin_raw_init: SetConsoleMode() failed: %s",
          SystemErrorCodeToString(GetLastError()).c_str());
          android::base::SystemErrorCodeToString(GetLastError()).c_str());
    }

    // Once this is set, it means that stdin has been configured for
@@ -3364,7 +3332,7 @@ void stdin_raw_restore() {
        if (!SetConsoleMode(in, _old_console_mode)) {
            // This really should not fail.
            D("stdin_raw_restore: SetConsoleMode() failed: %s",
              SystemErrorCodeToString(GetLastError()).c_str());
              android::base::SystemErrorCodeToString(GetLastError()).c_str());
        }
    }
}
Loading