diff options
author | Bernie Innocenti <codewiz@google.com> | 2018-05-28 14:56:57 +0900 |
---|---|---|
committer | Bernie Innocenti <codewiz@google.com> | 2018-05-28 15:00:50 +0900 |
commit | 4351bb05ad9fd5580aba7cd6a3dc2bc8c101c3a3 (patch) | |
tree | 6e73f7d146873ec44ead1d897f3b35e49187a59e /libcutils/sockets_unix.cpp | |
parent | e82401e592c6c45eca854525c91530ef8422db40 (diff) |
libcutils: Simplify android_get_control_socket()
1. TEMP_FAILURE_RETRY() isn't necessary with getsockname() because
it's a synchronous syscall.
2. There's no need to allocate and free a temporary buffer for the
full pathname of the socket.
Test: Manually, as follows:
- added temporary ALOG output
- flashed and booted a device
- checked expected output from all system daemons
- removed log statements
Change-Id: I4550a2d67fdffe6aff3c1050f8eeeaca0f985fb3
Diffstat (limited to 'libcutils/sockets_unix.cpp')
-rw-r--r-- | libcutils/sockets_unix.cpp | 24 |
1 files changed, 10 insertions, 14 deletions
diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp index 2849aa886..0cb8a4dc5 100644 --- a/libcutils/sockets_unix.cpp +++ b/libcutils/sockets_unix.cpp @@ -32,10 +32,6 @@ #include "android_get_control_env.h" -#ifndef TEMP_FAILURE_RETRY -#define TEMP_FAILURE_RETRY(exp) (exp) // KISS implementation -#endif - #if defined(__ANDROID__) /* For the socket trust (credentials) check */ #include <private/android_filesystem_config.h> @@ -102,15 +98,15 @@ int android_get_control_socket(const char* name) { // Compare to UNIX domain socket name, must match! struct sockaddr_un addr; socklen_t addrlen = sizeof(addr); - int ret = TEMP_FAILURE_RETRY(getsockname(fd, (struct sockaddr *)&addr, &addrlen)); + int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen); if (ret < 0) return -1; - char *path = NULL; - if (asprintf(&path, ANDROID_SOCKET_DIR "/%s", name) < 0) return -1; - if (!path) return -1; - int cmp = strcmp(addr.sun_path, path); - free(path); - if (cmp != 0) return -1; - - // It is what we think it is - return fd; + + constexpr char prefix[] = ANDROID_SOCKET_DIR "/"; + constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0'); + if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) && + (strcmp(addr.sun_path + prefix_size, name) == 0)) { + // It is what we think it is + return fd; + } + return -1; } |