1/*
2 * Copyright 2016, 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
17#include "debuggerd/util.h"
18
19#include <sys/socket.h>
20
21#include <utility>
22
23#include <android-base/unique_fd.h>
24#include <cutils/sockets.h>
25#include <debuggerd/protocol.h>
26
27#include "private/libc_logging.h"
28
29using android::base::unique_fd;
30
31ssize_t send_fd(int sockfd, const void* data, size_t len, unique_fd fd) {
32  char cmsg_buf[CMSG_SPACE(sizeof(int))];
33
34  iovec iov = { .iov_base = const_cast<void*>(data), .iov_len = len };
35  msghdr msg = {
36    .msg_iov = &iov, .msg_iovlen = 1, .msg_control = cmsg_buf, .msg_controllen = sizeof(cmsg_buf),
37  };
38  auto cmsg = CMSG_FIRSTHDR(&msg);
39  cmsg->cmsg_level = SOL_SOCKET;
40  cmsg->cmsg_type = SCM_RIGHTS;
41  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
42  *reinterpret_cast<int*>(CMSG_DATA(cmsg)) = fd.get();
43
44  return TEMP_FAILURE_RETRY(sendmsg(sockfd, &msg, 0));
45}
46
47ssize_t recv_fd(int sockfd, void* _Nonnull data, size_t len, unique_fd* _Nullable out_fd) {
48  char cmsg_buf[CMSG_SPACE(sizeof(int))];
49
50  iovec iov = { .iov_base = const_cast<void*>(data), .iov_len = len };
51  msghdr msg = {
52    .msg_iov = &iov,
53    .msg_iovlen = 1,
54    .msg_control = cmsg_buf,
55    .msg_controllen = sizeof(cmsg_buf),
56    .msg_flags = 0,
57  };
58  auto cmsg = CMSG_FIRSTHDR(&msg);
59  cmsg->cmsg_level = SOL_SOCKET;
60  cmsg->cmsg_type = SCM_RIGHTS;
61  cmsg->cmsg_len = CMSG_LEN(sizeof(int));
62
63  ssize_t result = TEMP_FAILURE_RETRY(recvmsg(sockfd, &msg, 0));
64  if (result == -1) {
65    return -1;
66  }
67
68  unique_fd fd;
69  bool received_fd = msg.msg_controllen == sizeof(cmsg_buf);
70  if (received_fd) {
71    fd.reset(*reinterpret_cast<int*>(CMSG_DATA(cmsg)));
72  }
73
74  if ((msg.msg_flags & MSG_TRUNC) != 0) {
75    errno = EFBIG;
76    return -1;
77  } else if ((msg.msg_flags & MSG_CTRUNC) != 0) {
78    errno = ERANGE;
79    return -1;
80  }
81
82  if (out_fd) {
83    *out_fd = std::move(fd);
84  } else if (received_fd) {
85    errno = ERANGE;
86    return -1;
87  }
88
89  return result;
90}
91
92bool Pipe(unique_fd* read, unique_fd* write) {
93  int pipefds[2];
94  if (pipe(pipefds) != 0) {
95    return false;
96  }
97  read->reset(pipefds[0]);
98  write->reset(pipefds[1]);
99  return true;
100}
101