diff options
author | Mark Salyzyn <salyzyn@google.com> | 2016-11-17 15:22:13 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2016-11-17 15:22:14 +0000 |
commit | 8c41e791ed726449bd51a35f03dd6269274668c0 (patch) | |
tree | 0f98b5a1fd8ca8fe185e26a54a6ef1ef0a49e24e /libcutils/sockets_unix.cpp | |
parent | 5368723cf3979586605117b13593fa83da57cdc9 (diff) | |
parent | 52bd37e63373b410c009e8611508191dfbf31d30 (diff) |
Merge "libcutils: move cutils/files.h to cutils/android_get_control_file.h"
Diffstat (limited to 'libcutils/sockets_unix.cpp')
-rw-r--r-- | libcutils/sockets_unix.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> #include <sys/uio.h> +#include <sys/un.h> #include <time.h> #include <unistd.h> #include <android/log.h> +#include <cutils/android_get_control_file.h> #include <cutils/sockets.h> +#include "android_get_control_env.h" + #if defined(__ANDROID__) /* For the socket trust (credentials) check */ #include <private/android_filesystem_config.h> @@ -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; +} |