From 52bd37e63373b410c009e8611508191dfbf31d30 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Mon, 7 Nov 2016 09:39:30 -0800 Subject: libcutils: move cutils/files.h to cutils/android_get_control_file.h files.[h|cpp] is bound to be abused with junk, replace with android_get_control_file.[h|cpp]. Plus some sundry cleanup. Test: gTest libcutils-tests, logd-unit-tests, liblog-unit-tests, logcat-unit-tests and init_tests Bug: 32450474 Change-Id: Ibd4a7aa4624ea19a43d1f98a3c71ac37805d36b5 --- libcutils/sockets_unix.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'libcutils/sockets_unix.cpp') diff --git a/libcutils/sockets_unix.cpp b/libcutils/sockets_unix.cpp index 3545403aa..948d09c60 100644 --- a/libcutils/sockets_unix.cpp +++ b/libcutils/sockets_unix.cpp @@ -16,13 +16,21 @@ #define LOG_TAG "socket-unix" +#include +#include +#include +#include #include +#include #include #include #include +#include #include +#include "android_get_control_env.h" + #if defined(__ANDROID__) /* For the socket trust (credentials) check */ #include @@ -80,3 +88,24 @@ ssize_t socket_send_buffers(cutils_socket_t sock, return writev(sock, iovec_buffers, num_buffers); } + +int android_get_control_socket(const char* name) { + int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name); + + if (fd < 0) return fd; + + // 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)); + 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; +} -- cgit v1.2.3