blob: 22488178f1e8f80521b5d81dd4163590933e4360 [file] [log] [blame]
jeffhao2b8f76c2011-05-05 14:25:36 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/sockets.h>
18
Mark Salyzyncfd5b082016-10-17 14:28:00 -070019#define LOG_TAG "socket-unix"
20
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/socket.h>
David Pursellb34e4a02016-02-01 09:42:09 -080025#include <sys/uio.h>
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080026#include <sys/un.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070027#include <time.h>
28#include <unistd.h>
David Pursellb34e4a02016-02-01 09:42:09 -080029
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080030#include <cutils/android_get_control_file.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080031#include <log/log.h>
jeffhao2b8f76c2011-05-05 14:25:36 -070032
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080033#include "android_get_control_env.h"
34
David Pursell0eb8e1b2016-01-14 17:18:27 -080035int socket_close(int sock) {
David Pursell8385fb22016-01-29 13:49:25 -080036 return close(sock);
David Pursell0eb8e1b2016-01-14 17:18:27 -080037}
David Pursell572bce22016-01-15 14:19:56 -080038
39int socket_set_receive_timeout(cutils_socket_t sock, int timeout_ms) {
David Pursellb34e4a02016-02-01 09:42:09 -080040 timeval tv;
David Pursell8385fb22016-01-29 13:49:25 -080041 tv.tv_sec = timeout_ms / 1000;
42 tv.tv_usec = (timeout_ms % 1000) * 1000;
43 return setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
44}
45
David Pursell8385fb22016-01-29 13:49:25 -080046ssize_t socket_send_buffers(cutils_socket_t sock,
David Pursellb34e4a02016-02-01 09:42:09 -080047 const cutils_socket_buffer_t* buffers,
David Pursell8385fb22016-01-29 13:49:25 -080048 size_t num_buffers) {
David Pursellb34e4a02016-02-01 09:42:09 -080049 if (num_buffers > SOCKET_SEND_BUFFERS_MAX_BUFFERS) {
50 return -1;
51 }
52
53 iovec iovec_buffers[SOCKET_SEND_BUFFERS_MAX_BUFFERS];
54 for (size_t i = 0; i < num_buffers; ++i) {
55 // It's safe to cast away const here; iovec declares non-const
56 // void* because it's used for both send and receive, but since
57 // we're only sending, the data won't be modified.
58 iovec_buffers[i].iov_base = const_cast<void*>(buffers[i].data);
59 iovec_buffers[i].iov_len = buffers[i].length;
60 }
61
62 return writev(sock, iovec_buffers, num_buffers);
David Pursell572bce22016-01-15 14:19:56 -080063}
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080064
65int android_get_control_socket(const char* name) {
66 int fd = __android_get_control_from_env(ANDROID_SOCKET_ENV_PREFIX, name);
67
68 if (fd < 0) return fd;
69
70 // Compare to UNIX domain socket name, must match!
71 struct sockaddr_un addr;
72 socklen_t addrlen = sizeof(addr);
Bernie Innocenti4351bb02018-05-28 14:56:57 +090073 int ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080074 if (ret < 0) return -1;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080075
Bernie Innocenti4351bb02018-05-28 14:56:57 +090076 constexpr char prefix[] = ANDROID_SOCKET_DIR "/";
77 constexpr size_t prefix_size = sizeof(prefix) - sizeof('\0');
78 if ((strncmp(addr.sun_path, prefix, prefix_size) == 0) &&
79 (strcmp(addr.sun_path + prefix_size, name) == 0)) {
80 // It is what we think it is
81 return fd;
82 }
83 return -1;
Mark Salyzyn52bd37e2016-11-07 09:39:30 -080084}