Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 17 | #include "perfetto/ext/base/unix_socket.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 21 | #include <netdb.h> |
| 22 | #include <netinet/in.h> |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/un.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <algorithm> |
| 32 | #include <memory> |
| 33 | |
| 34 | #include "perfetto/base/build_config.h" |
| 35 | #include "perfetto/base/logging.h" |
| 36 | #include "perfetto/base/task_runner.h" |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 37 | #include "perfetto/ext/base/string_utils.h" |
Primiano Tucci | 2c5488f | 2019-06-01 03:27:28 +0100 | [diff] [blame] | 38 | #include "perfetto/ext/base/utils.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 39 | |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 40 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 41 | #include <sys/ucred.h> |
| 42 | #endif |
| 43 | |
| 44 | namespace perfetto { |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 45 | namespace base { |
| 46 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 47 | // The CMSG_* macros use NULL instead of nullptr. |
| 48 | #pragma GCC diagnostic push |
| 49 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) |
| 50 | #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" |
| 51 | #endif |
| 52 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 53 | namespace { |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 54 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 55 | // MSG_NOSIGNAL is not supported on Mac OS X, but in that case the socket is |
| 56 | // created with SO_NOSIGPIPE (See InitializeSocket()). |
| 57 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) |
| 58 | constexpr int kNoSigPipe = 0; |
| 59 | #else |
| 60 | constexpr int kNoSigPipe = MSG_NOSIGNAL; |
| 61 | #endif |
| 62 | |
| 63 | // Android takes an int instead of socklen_t for the control buffer size. |
| 64 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
| 65 | using CBufLenType = size_t; |
| 66 | #else |
| 67 | using CBufLenType = socklen_t; |
| 68 | #endif |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 69 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 70 | // A wrapper around variable-size sockaddr structs. |
| 71 | // This is solving the following problem: when calling connect() or bind(), the |
| 72 | // caller needs to take care to allocate the right struct (sockaddr_un for |
| 73 | // AF_UNIX, sockaddr_in for AF_INET). Those structs have different sizes and, |
| 74 | // more importantly, are bigger than the base struct sockaddr. |
| 75 | struct SockaddrAny { |
| 76 | SockaddrAny() : size() {} |
| 77 | SockaddrAny(const void* addr, socklen_t sz) : data(new char[sz]), size(sz) { |
| 78 | memcpy(data.get(), addr, static_cast<size_t>(size)); |
| 79 | } |
| 80 | |
| 81 | const struct sockaddr* addr() const { |
| 82 | return reinterpret_cast<const struct sockaddr*>(data.get()); |
| 83 | } |
| 84 | |
| 85 | std::unique_ptr<char[]> data; |
| 86 | socklen_t size; |
| 87 | }; |
| 88 | |
| 89 | inline int GetSockFamily(SockFamily family) { |
| 90 | switch (family) { |
| 91 | case SockFamily::kUnix: |
| 92 | return AF_UNIX; |
| 93 | case SockFamily::kInet: |
| 94 | return AF_INET; |
| 95 | } |
| 96 | PERFETTO_CHECK(false); // For GCC. |
| 97 | } |
| 98 | |
| 99 | inline int GetSockType(SockType type) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 100 | switch (type) { |
| 101 | case SockType::kStream: |
| 102 | return SOCK_STREAM; |
| 103 | case SockType::kDgram: |
| 104 | return SOCK_DGRAM; |
| 105 | case SockType::kSeqPacket: |
| 106 | return SOCK_SEQPACKET; |
| 107 | } |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 108 | PERFETTO_CHECK(false); // For GCC. |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 111 | SockaddrAny MakeSockAddr(SockFamily family, const std::string& socket_name) { |
| 112 | switch (family) { |
| 113 | case SockFamily::kUnix: { |
| 114 | struct sockaddr_un saddr {}; |
| 115 | const size_t name_len = socket_name.size(); |
| 116 | if (name_len >= sizeof(saddr.sun_path)) { |
| 117 | errno = ENAMETOOLONG; |
| 118 | return SockaddrAny(); |
| 119 | } |
| 120 | memcpy(saddr.sun_path, socket_name.data(), name_len); |
| 121 | if (saddr.sun_path[0] == '@') |
| 122 | saddr.sun_path[0] = '\0'; |
| 123 | saddr.sun_family = AF_UNIX; |
| 124 | auto size = static_cast<socklen_t>( |
| 125 | __builtin_offsetof(sockaddr_un, sun_path) + name_len + 1); |
| 126 | PERFETTO_CHECK(static_cast<size_t>(size) <= sizeof(saddr)); |
| 127 | return SockaddrAny(&saddr, size); |
| 128 | } |
| 129 | case SockFamily::kInet: { |
| 130 | auto parts = SplitString(socket_name, ":"); |
| 131 | PERFETTO_CHECK(parts.size() == 2); |
| 132 | struct addrinfo* addr_info = nullptr; |
| 133 | struct addrinfo hints {}; |
| 134 | hints.ai_family = AF_INET; |
| 135 | PERFETTO_CHECK(getaddrinfo(parts[0].c_str(), parts[1].c_str(), &hints, |
| 136 | &addr_info) == 0); |
| 137 | PERFETTO_CHECK(addr_info->ai_family == AF_INET); |
| 138 | SockaddrAny res(addr_info->ai_addr, addr_info->ai_addrlen); |
| 139 | freeaddrinfo(addr_info); |
| 140 | return res; |
| 141 | } |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 142 | } |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 143 | PERFETTO_CHECK(false); // For GCC. |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 144 | } |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 145 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 146 | } // namespace |
| 147 | |
| 148 | // +-----------------------+ |
| 149 | // | UnixSocketRaw methods | |
| 150 | // +-----------------------+ |
| 151 | |
| 152 | // static |
| 153 | void UnixSocketRaw::ShiftMsgHdr(size_t n, struct msghdr* msg) { |
Primiano Tucci | 45b902a | 2018-10-04 14:57:09 +0100 | [diff] [blame] | 154 | using LenType = decltype(msg->msg_iovlen); // Mac and Linux don't agree. |
| 155 | for (LenType i = 0; i < msg->msg_iovlen; ++i) { |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 156 | struct iovec* vec = &msg->msg_iov[i]; |
| 157 | if (n < vec->iov_len) { |
| 158 | // We sent a part of this iovec. |
| 159 | vec->iov_base = reinterpret_cast<char*>(vec->iov_base) + n; |
| 160 | vec->iov_len -= n; |
| 161 | msg->msg_iov = vec; |
| 162 | msg->msg_iovlen -= i; |
| 163 | return; |
| 164 | } |
| 165 | // We sent the whole iovec. |
| 166 | n -= vec->iov_len; |
| 167 | } |
| 168 | // We sent all the iovecs. |
| 169 | PERFETTO_CHECK(n == 0); |
| 170 | msg->msg_iovlen = 0; |
| 171 | msg->msg_iov = nullptr; |
| 172 | } |
| 173 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 174 | // static |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 175 | std::pair<UnixSocketRaw, UnixSocketRaw> UnixSocketRaw::CreatePair( |
| 176 | SockFamily family, |
| 177 | SockType type) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 178 | int fds[2]; |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 179 | if (socketpair(GetSockFamily(family), GetSockType(type), 0, fds) != 0) |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 180 | return std::make_pair(UnixSocketRaw(), UnixSocketRaw()); |
| 181 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 182 | return std::make_pair(UnixSocketRaw(ScopedFile(fds[0]), family, type), |
| 183 | UnixSocketRaw(ScopedFile(fds[1]), family, type)); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | UnixSocketRaw::UnixSocketRaw() = default; |
| 187 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 188 | UnixSocketRaw::UnixSocketRaw(SockFamily family, SockType type) |
| 189 | : UnixSocketRaw( |
| 190 | ScopedFile(socket(GetSockFamily(family), GetSockType(type), 0)), |
| 191 | family, |
| 192 | type) {} |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 193 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 194 | UnixSocketRaw::UnixSocketRaw(ScopedFile fd, SockFamily family, SockType type) |
| 195 | : fd_(std::move(fd)), family_(family), type_(type) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 196 | PERFETTO_CHECK(fd_); |
| 197 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) |
| 198 | const int no_sigpipe = 1; |
| 199 | setsockopt(*fd_, SOL_SOCKET, SO_NOSIGPIPE, &no_sigpipe, sizeof(no_sigpipe)); |
| 200 | #endif |
| 201 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 202 | if (family == SockFamily::kInet) { |
| 203 | int flag = 1; |
| 204 | PERFETTO_CHECK( |
| 205 | !setsockopt(*fd_, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag))); |
| 206 | } |
| 207 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 208 | // There is no reason why a socket should outlive the process in case of |
| 209 | // exec() by default, this is just working around a broken unix design. |
| 210 | int fcntl_res = fcntl(*fd_, F_SETFD, FD_CLOEXEC); |
| 211 | PERFETTO_CHECK(fcntl_res == 0); |
| 212 | } |
| 213 | |
| 214 | void UnixSocketRaw::SetBlocking(bool is_blocking) { |
| 215 | PERFETTO_DCHECK(fd_); |
| 216 | int flags = fcntl(*fd_, F_GETFL, 0); |
| 217 | if (!is_blocking) { |
| 218 | flags |= O_NONBLOCK; |
| 219 | } else { |
| 220 | flags &= ~static_cast<int>(O_NONBLOCK); |
| 221 | } |
| 222 | bool fcntl_res = fcntl(*fd_, F_SETFL, flags); |
| 223 | PERFETTO_CHECK(fcntl_res == 0); |
| 224 | } |
| 225 | |
Ryan Savitski | 14c12d6 | 2019-01-08 22:38:53 +0000 | [diff] [blame] | 226 | void UnixSocketRaw::RetainOnExec() { |
| 227 | PERFETTO_DCHECK(fd_); |
| 228 | int flags = fcntl(*fd_, F_GETFD, 0); |
| 229 | flags &= ~static_cast<int>(FD_CLOEXEC); |
| 230 | bool fcntl_res = fcntl(*fd_, F_SETFD, flags); |
| 231 | PERFETTO_CHECK(fcntl_res == 0); |
| 232 | } |
| 233 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 234 | bool UnixSocketRaw::IsBlocking() const { |
| 235 | PERFETTO_DCHECK(fd_); |
| 236 | return (fcntl(*fd_, F_GETFL, 0) & O_NONBLOCK) == 0; |
| 237 | } |
| 238 | |
| 239 | bool UnixSocketRaw::Bind(const std::string& socket_name) { |
| 240 | PERFETTO_DCHECK(fd_); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 241 | SockaddrAny addr = MakeSockAddr(family_, socket_name); |
| 242 | if (addr.size == 0) |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 243 | return false; |
| 244 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 245 | if (bind(*fd_, addr.addr(), addr.size)) { |
Primiano Tucci | 301175d | 2019-01-11 10:10:21 +0000 | [diff] [blame] | 246 | PERFETTO_DPLOG("bind(%s)", socket_name.c_str()); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 247 | return false; |
| 248 | } |
| 249 | |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool UnixSocketRaw::Listen() { |
Ryan Savitski | 499d956 | 2019-01-08 23:10:01 +0000 | [diff] [blame] | 254 | PERFETTO_DCHECK(fd_); |
| 255 | PERFETTO_DCHECK(type_ == SockType::kStream || type_ == SockType::kSeqPacket); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 256 | return listen(*fd_, SOMAXCONN) == 0; |
| 257 | } |
| 258 | |
| 259 | bool UnixSocketRaw::Connect(const std::string& socket_name) { |
| 260 | PERFETTO_DCHECK(fd_); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 261 | SockaddrAny addr = MakeSockAddr(family_, socket_name); |
| 262 | if (addr.size == 0) |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 263 | return false; |
| 264 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 265 | int res = PERFETTO_EINTR(connect(*fd_, addr.addr(), addr.size)); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 266 | if (res && errno != EINPROGRESS) |
| 267 | return false; |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | void UnixSocketRaw::Shutdown() { |
| 273 | shutdown(*fd_, SHUT_RDWR); |
| 274 | fd_.reset(); |
| 275 | } |
| 276 | |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 277 | // For the interested reader, Linux kernel dive to verify this is not only a |
| 278 | // theoretical possibility: sock_stream_sendmsg, if sock_alloc_send_pskb returns |
| 279 | // NULL [1] (which it does when it gets interrupted [2]), returns early with the |
| 280 | // amount of bytes already sent. |
| 281 | // |
| 282 | // [1]: |
| 283 | // https://elixir.bootlin.com/linux/v4.18.10/source/net/unix/af_unix.c#L1872 |
| 284 | // [2]: https://elixir.bootlin.com/linux/v4.18.10/source/net/core/sock.c#L2101 |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 285 | ssize_t UnixSocketRaw::SendMsgAll(struct msghdr* msg) { |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 286 | // This does not make sense on non-blocking sockets. |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 287 | PERFETTO_DCHECK(fd_); |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 288 | |
| 289 | ssize_t total_sent = 0; |
| 290 | while (msg->msg_iov) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 291 | ssize_t sent = PERFETTO_EINTR(sendmsg(*fd_, msg, kNoSigPipe)); |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 292 | if (sent <= 0) { |
Florian Mayer | d6bdb6f | 2019-05-03 17:53:58 +0100 | [diff] [blame] | 293 | if (sent == -1 && IsAgain(errno)) |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 294 | return total_sent; |
| 295 | return sent; |
| 296 | } |
| 297 | total_sent += sent; |
| 298 | ShiftMsgHdr(static_cast<size_t>(sent), msg); |
| 299 | // Only send the ancillary data with the first sendmsg call. |
| 300 | msg->msg_control = nullptr; |
| 301 | msg->msg_controllen = 0; |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 302 | } |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 303 | return total_sent; |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 304 | } |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 305 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 306 | ssize_t UnixSocketRaw::Send(const void* msg, |
| 307 | size_t len, |
| 308 | const int* send_fds, |
| 309 | size_t num_fds) { |
| 310 | PERFETTO_DCHECK(fd_); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 311 | msghdr msg_hdr = {}; |
| 312 | iovec iov = {const_cast<void*>(msg), len}; |
| 313 | msg_hdr.msg_iov = &iov; |
| 314 | msg_hdr.msg_iovlen = 1; |
| 315 | alignas(cmsghdr) char control_buf[256]; |
| 316 | |
| 317 | if (num_fds > 0) { |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 318 | const auto raw_ctl_data_sz = num_fds * sizeof(int); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 319 | const CBufLenType control_buf_len = |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 320 | static_cast<CBufLenType>(CMSG_SPACE(raw_ctl_data_sz)); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 321 | PERFETTO_CHECK(control_buf_len <= sizeof(control_buf)); |
| 322 | memset(control_buf, 0, sizeof(control_buf)); |
| 323 | msg_hdr.msg_control = control_buf; |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 324 | msg_hdr.msg_controllen = control_buf_len; // used by CMSG_FIRSTHDR |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 325 | struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg_hdr); |
| 326 | cmsg->cmsg_level = SOL_SOCKET; |
| 327 | cmsg->cmsg_type = SCM_RIGHTS; |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 328 | cmsg->cmsg_len = static_cast<CBufLenType>(CMSG_LEN(raw_ctl_data_sz)); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 329 | memcpy(CMSG_DATA(cmsg), send_fds, num_fds * sizeof(int)); |
Ryan Savitski | 31e4b6c | 2018-11-21 19:41:57 +0000 | [diff] [blame] | 330 | // note: if we were to send multiple cmsghdr structures, then |
| 331 | // msg_hdr.msg_controllen would need to be adjusted, see "man 3 cmsg". |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 334 | return SendMsgAll(&msg_hdr); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 337 | ssize_t UnixSocketRaw::Receive(void* msg, |
| 338 | size_t len, |
| 339 | ScopedFile* fd_vec, |
| 340 | size_t max_files) { |
| 341 | PERFETTO_DCHECK(fd_); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 342 | msghdr msg_hdr = {}; |
| 343 | iovec iov = {msg, len}; |
| 344 | msg_hdr.msg_iov = &iov; |
| 345 | msg_hdr.msg_iovlen = 1; |
| 346 | alignas(cmsghdr) char control_buf[256]; |
| 347 | |
| 348 | if (max_files > 0) { |
| 349 | msg_hdr.msg_control = control_buf; |
| 350 | msg_hdr.msg_controllen = |
| 351 | static_cast<CBufLenType>(CMSG_SPACE(max_files * sizeof(int))); |
| 352 | PERFETTO_CHECK(msg_hdr.msg_controllen <= sizeof(control_buf)); |
| 353 | } |
Ryan Savitski | b6d6591 | 2019-01-08 16:12:53 +0000 | [diff] [blame] | 354 | const ssize_t sz = PERFETTO_EINTR(recvmsg(*fd_, &msg_hdr, 0)); |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 355 | if (sz <= 0) { |
| 356 | return sz; |
| 357 | } |
| 358 | PERFETTO_CHECK(static_cast<size_t>(sz) <= len); |
| 359 | |
| 360 | int* fds = nullptr; |
| 361 | uint32_t fds_len = 0; |
| 362 | |
| 363 | if (max_files > 0) { |
| 364 | for (cmsghdr* cmsg = CMSG_FIRSTHDR(&msg_hdr); cmsg; |
| 365 | cmsg = CMSG_NXTHDR(&msg_hdr, cmsg)) { |
| 366 | const size_t payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 367 | if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 368 | PERFETTO_DCHECK(payload_len % sizeof(int) == 0u); |
| 369 | PERFETTO_CHECK(fds == nullptr); |
| 370 | fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 371 | fds_len = static_cast<uint32_t>(payload_len / sizeof(int)); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | if (msg_hdr.msg_flags & MSG_TRUNC || msg_hdr.msg_flags & MSG_CTRUNC) { |
| 377 | for (size_t i = 0; fds && i < fds_len; ++i) |
| 378 | close(fds[i]); |
| 379 | errno = EMSGSIZE; |
| 380 | return -1; |
| 381 | } |
| 382 | |
| 383 | for (size_t i = 0; fds && i < fds_len; ++i) { |
| 384 | if (i < max_files) |
| 385 | fd_vec[i].reset(fds[i]); |
| 386 | else |
| 387 | close(fds[i]); |
| 388 | } |
| 389 | |
| 390 | return sz; |
| 391 | } |
| 392 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 393 | bool UnixSocketRaw::SetTxTimeout(uint32_t timeout_ms) { |
| 394 | PERFETTO_DCHECK(fd_); |
| 395 | struct timeval timeout {}; |
| 396 | uint32_t timeout_sec = timeout_ms / 1000; |
| 397 | timeout.tv_sec = static_cast<decltype(timeout.tv_sec)>(timeout_sec); |
| 398 | timeout.tv_usec = static_cast<decltype(timeout.tv_usec)>( |
| 399 | (timeout_ms - (timeout_sec * 1000)) * 1000); |
| 400 | |
| 401 | return setsockopt(*fd_, SOL_SOCKET, SO_SNDTIMEO, |
| 402 | reinterpret_cast<const char*>(&timeout), |
| 403 | sizeof(timeout)) == 0; |
| 404 | } |
| 405 | |
Florian Mayer | dac3127 | 2019-02-26 12:07:02 +0000 | [diff] [blame] | 406 | bool UnixSocketRaw::SetRxTimeout(uint32_t timeout_ms) { |
| 407 | PERFETTO_DCHECK(fd_); |
| 408 | struct timeval timeout {}; |
| 409 | uint32_t timeout_sec = timeout_ms / 1000; |
| 410 | timeout.tv_sec = static_cast<decltype(timeout.tv_sec)>(timeout_sec); |
| 411 | timeout.tv_usec = static_cast<decltype(timeout.tv_usec)>( |
| 412 | (timeout_ms - (timeout_sec * 1000)) * 1000); |
| 413 | |
| 414 | return setsockopt(*fd_, SOL_SOCKET, SO_RCVTIMEO, |
| 415 | reinterpret_cast<const char*>(&timeout), |
| 416 | sizeof(timeout)) == 0; |
| 417 | } |
| 418 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 419 | #pragma GCC diagnostic pop |
| 420 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 421 | // +--------------------+ |
| 422 | // | UnixSocket methods | |
| 423 | // +--------------------+ |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 424 | |
| 425 | // TODO(primiano): Add ThreadChecker to methods of this class. |
| 426 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 427 | // static |
| 428 | std::unique_ptr<UnixSocket> UnixSocket::Listen(const std::string& socket_name, |
| 429 | EventListener* event_listener, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 430 | TaskRunner* task_runner, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 431 | SockFamily sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 432 | SockType sock_type) { |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 433 | auto sock_raw = UnixSocketRaw::CreateMayFail(sock_family, sock_type); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 434 | if (!sock_raw || !sock_raw.Bind(socket_name)) |
| 435 | return nullptr; |
| 436 | |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 437 | // Forward the call to the Listen() overload below. |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 438 | return Listen(sock_raw.ReleaseFd(), event_listener, task_runner, sock_family, |
| 439 | sock_type); |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // static |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 443 | std::unique_ptr<UnixSocket> UnixSocket::Listen(ScopedFile fd, |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 444 | EventListener* event_listener, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 445 | TaskRunner* task_runner, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 446 | SockFamily sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 447 | SockType sock_type) { |
| 448 | return std::unique_ptr<UnixSocket>( |
| 449 | new UnixSocket(event_listener, task_runner, std::move(fd), |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 450 | State::kListening, sock_family, sock_type)); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // static |
| 454 | std::unique_ptr<UnixSocket> UnixSocket::Connect(const std::string& socket_name, |
| 455 | EventListener* event_listener, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 456 | TaskRunner* task_runner, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 457 | SockFamily sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 458 | SockType sock_type) { |
| 459 | std::unique_ptr<UnixSocket> sock( |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 460 | new UnixSocket(event_listener, task_runner, sock_family, sock_type)); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 461 | sock->DoConnect(socket_name); |
| 462 | return sock; |
| 463 | } |
| 464 | |
Ryan Savitski | 9c1240e | 2019-01-15 22:40:46 +0000 | [diff] [blame] | 465 | // static |
| 466 | std::unique_ptr<UnixSocket> UnixSocket::AdoptConnected( |
| 467 | ScopedFile fd, |
| 468 | EventListener* event_listener, |
| 469 | TaskRunner* task_runner, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 470 | SockFamily sock_family, |
Ryan Savitski | 9c1240e | 2019-01-15 22:40:46 +0000 | [diff] [blame] | 471 | SockType sock_type) { |
| 472 | return std::unique_ptr<UnixSocket>( |
| 473 | new UnixSocket(event_listener, task_runner, std::move(fd), |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 474 | State::kConnected, sock_family, sock_type)); |
Ryan Savitski | 9c1240e | 2019-01-15 22:40:46 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 477 | UnixSocket::UnixSocket(EventListener* event_listener, |
| 478 | TaskRunner* task_runner, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 479 | SockFamily sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 480 | SockType sock_type) |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 481 | : UnixSocket(event_listener, |
| 482 | task_runner, |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 483 | ScopedFile(), |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 484 | State::kDisconnected, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 485 | sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 486 | sock_type) {} |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 487 | |
| 488 | UnixSocket::UnixSocket(EventListener* event_listener, |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 489 | TaskRunner* task_runner, |
| 490 | ScopedFile adopt_fd, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 491 | State adopt_state, |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 492 | SockFamily sock_family, |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 493 | SockType sock_type) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 494 | : event_listener_(event_listener), |
| 495 | task_runner_(task_runner), |
| 496 | weak_ptr_factory_(this) { |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 497 | state_ = State::kDisconnected; |
| 498 | if (adopt_state == State::kDisconnected) { |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 499 | PERFETTO_DCHECK(!adopt_fd); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 500 | sock_raw_ = UnixSocketRaw::CreateMayFail(sock_family, sock_type); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 501 | if (!sock_raw_) { |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 502 | last_error_ = errno; |
| 503 | return; |
| 504 | } |
| 505 | } else if (adopt_state == State::kConnected) { |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 506 | PERFETTO_DCHECK(adopt_fd); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 507 | sock_raw_ = UnixSocketRaw(std::move(adopt_fd), sock_family, sock_type); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 508 | state_ = State::kConnected; |
| 509 | ReadPeerCredentials(); |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 510 | } else if (adopt_state == State::kListening) { |
| 511 | // We get here from Listen(). |
| 512 | |
| 513 | // |adopt_fd| might genuinely be invalid if the bind() failed. |
| 514 | if (!adopt_fd) { |
| 515 | last_error_ = errno; |
| 516 | return; |
| 517 | } |
| 518 | |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 519 | sock_raw_ = UnixSocketRaw(std::move(adopt_fd), sock_family, sock_type); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 520 | if (!sock_raw_.Listen()) { |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 521 | last_error_ = errno; |
| 522 | PERFETTO_DPLOG("listen()"); |
| 523 | return; |
| 524 | } |
| 525 | state_ = State::kListening; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 526 | } else { |
Hector Dearman | bd14fc6 | 2018-12-13 16:06:14 +0000 | [diff] [blame] | 527 | PERFETTO_FATAL("Unexpected adopt_state"); // Unfeasible. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 528 | } |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 529 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 530 | PERFETTO_CHECK(sock_raw_); |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 531 | last_error_ = 0; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 532 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 533 | sock_raw_.SetBlocking(false); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 534 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 535 | WeakPtr<UnixSocket> weak_ptr = weak_ptr_factory_.GetWeakPtr(); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 536 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 537 | task_runner_->AddFileDescriptorWatch(sock_raw_.fd(), [weak_ptr] { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 538 | if (weak_ptr) |
| 539 | weak_ptr->OnEvent(); |
| 540 | }); |
| 541 | } |
| 542 | |
| 543 | UnixSocket::~UnixSocket() { |
| 544 | // The implicit dtor of |weak_ptr_factory_| will no-op pending callbacks. |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 545 | Shutdown(true); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Florian Mayer | ae2603b | 2019-02-21 15:53:26 -0800 | [diff] [blame] | 548 | UnixSocketRaw UnixSocket::ReleaseSocket() { |
| 549 | // This will invalidate any pending calls to OnEvent. |
| 550 | state_ = State::kDisconnected; |
| 551 | if (sock_raw_) |
| 552 | task_runner_->RemoveFileDescriptorWatch(sock_raw_.fd()); |
| 553 | |
| 554 | return std::move(sock_raw_); |
| 555 | } |
| 556 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 557 | // Called only by the Connect() static constructor. |
| 558 | void UnixSocket::DoConnect(const std::string& socket_name) { |
| 559 | PERFETTO_DCHECK(state_ == State::kDisconnected); |
| 560 | |
| 561 | // This is the only thing that can gracefully fail in the ctor. |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 562 | if (!sock_raw_) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 563 | return NotifyConnectionState(false); |
| 564 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 565 | if (!sock_raw_.Connect(socket_name)) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 566 | last_error_ = errno; |
| 567 | return NotifyConnectionState(false); |
| 568 | } |
| 569 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 570 | // At this point either connect() succeeded or started asynchronously |
| 571 | // (errno = EINPROGRESS). |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 572 | last_error_ = 0; |
| 573 | state_ = State::kConnecting; |
| 574 | |
| 575 | // Even if the socket is non-blocking, connecting to a UNIX socket can be |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 576 | // acknowledged straight away rather than returning EINPROGRESS. |
| 577 | // The decision here is to deal with the two cases uniformly, at the cost of |
| 578 | // delaying the straight-away-connect() case by one task, to avoid depending |
| 579 | // on implementation details of UNIX socket on the various OSes. |
| 580 | // Posting the OnEvent() below emulates a wakeup of the FD watch. OnEvent(), |
| 581 | // which knows how to deal with spurious wakeups, will poll the SO_ERROR and |
| 582 | // evolve, if necessary, the state into either kConnected or kDisconnected. |
| 583 | WeakPtr<UnixSocket> weak_ptr = weak_ptr_factory_.GetWeakPtr(); |
| 584 | task_runner_->PostTask([weak_ptr] { |
| 585 | if (weak_ptr) |
| 586 | weak_ptr->OnEvent(); |
| 587 | }); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void UnixSocket::ReadPeerCredentials() { |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 591 | // Peer credentials are supported only on AF_UNIX sockets. |
| 592 | if (sock_raw_.family() != SockFamily::kUnix) |
| 593 | return; |
| 594 | |
Oystein Eftevaag | ff72959 | 2018-02-12 14:24:06 -0800 | [diff] [blame] | 595 | #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \ |
| 596 | PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 597 | struct ucred user_cred; |
| 598 | socklen_t len = sizeof(user_cred); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 599 | int fd = sock_raw_.fd(); |
| 600 | int res = getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &user_cred, &len); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 601 | PERFETTO_CHECK(res == 0); |
| 602 | peer_uid_ = user_cred.uid; |
Florian Mayer | fa1659e | 2018-08-07 10:45:19 +0100 | [diff] [blame] | 603 | peer_pid_ = user_cred.pid; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 604 | #else |
| 605 | struct xucred user_cred; |
| 606 | socklen_t len = sizeof(user_cred); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 607 | int res = getsockopt(sock_raw_.fd(), 0, LOCAL_PEERCRED, &user_cred, &len); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 608 | PERFETTO_CHECK(res == 0 && user_cred.cr_version == XUCRED_VERSION); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 609 | peer_uid_ = static_cast<uid_t>(user_cred.cr_uid); |
Florian Mayer | fa1659e | 2018-08-07 10:45:19 +0100 | [diff] [blame] | 610 | // There is no pid in the LOCAL_PEERCREDS for MacOS / FreeBSD. |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 611 | #endif |
| 612 | } |
| 613 | |
| 614 | void UnixSocket::OnEvent() { |
| 615 | if (state_ == State::kDisconnected) |
| 616 | return; // Some spurious event, typically queued just before Shutdown(). |
| 617 | |
| 618 | if (state_ == State::kConnected) |
| 619 | return event_listener_->OnDataAvailable(this); |
| 620 | |
| 621 | if (state_ == State::kConnecting) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 622 | PERFETTO_DCHECK(sock_raw_); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 623 | int sock_err = EINVAL; |
| 624 | socklen_t err_len = sizeof(sock_err); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 625 | int res = |
| 626 | getsockopt(sock_raw_.fd(), SOL_SOCKET, SO_ERROR, &sock_err, &err_len); |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 627 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 628 | if (res == 0 && sock_err == EINPROGRESS) |
| 629 | return; // Not connected yet, just a spurious FD watch wakeup. |
| 630 | if (res == 0 && sock_err == 0) { |
| 631 | ReadPeerCredentials(); |
| 632 | state_ = State::kConnected; |
| 633 | return event_listener_->OnConnect(this, true /* connected */); |
| 634 | } |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 635 | PERFETTO_DLOG("Connection error: %s", strerror(sock_err)); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 636 | last_error_ = sock_err; |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 637 | Shutdown(false); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 638 | return event_listener_->OnConnect(this, false /* connected */); |
| 639 | } |
| 640 | |
| 641 | // New incoming connection. |
| 642 | if (state_ == State::kListening) { |
| 643 | // There could be more than one incoming connection behind each FD watch |
| 644 | // notification. Drain'em all. |
| 645 | for (;;) { |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 646 | struct sockaddr_in cli_addr {}; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 647 | socklen_t size = sizeof(cli_addr); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 648 | ScopedFile new_fd(PERFETTO_EINTR(accept( |
| 649 | sock_raw_.fd(), reinterpret_cast<sockaddr*>(&cli_addr), &size))); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 650 | if (!new_fd) |
| 651 | return; |
Primiano Tucci | d4be966 | 2019-10-22 17:45:13 -0400 | [diff] [blame] | 652 | std::unique_ptr<UnixSocket> new_sock(new UnixSocket( |
| 653 | event_listener_, task_runner_, std::move(new_fd), State::kConnected, |
| 654 | sock_raw_.family(), sock_raw_.type())); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 655 | event_listener_->OnNewIncomingConnection(this, std::move(new_sock)); |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Florian Mayer | e1cd07d | 2018-07-31 11:56:41 +0100 | [diff] [blame] | 660 | bool UnixSocket::Send(const void* msg, |
| 661 | size_t len, |
| 662 | const int* send_fds, |
| 663 | size_t num_fds, |
| 664 | BlockingMode blocking_mode) { |
Florian Mayer | f3c0ac8 | 2018-10-02 11:53:55 +0100 | [diff] [blame] | 665 | // TODO(b/117139237): Non-blocking sends are broken because we do not |
| 666 | // properly handle partial sends. |
| 667 | PERFETTO_DCHECK(blocking_mode == BlockingMode::kBlocking); |
| 668 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 669 | if (state_ != State::kConnected) { |
| 670 | errno = last_error_ = ENOTCONN; |
| 671 | return false; |
| 672 | } |
| 673 | |
Primiano Tucci | 7a265b6 | 2017-12-07 18:37:31 +0000 | [diff] [blame] | 674 | if (blocking_mode == BlockingMode::kBlocking) |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 675 | sock_raw_.SetBlocking(true); |
| 676 | const ssize_t sz = sock_raw_.Send(msg, len, send_fds, num_fds); |
| 677 | int saved_errno = errno; |
Primiano Tucci | 7a265b6 | 2017-12-07 18:37:31 +0000 | [diff] [blame] | 678 | if (blocking_mode == BlockingMode::kBlocking) |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 679 | sock_raw_.SetBlocking(false); |
Primiano Tucci | 7a265b6 | 2017-12-07 18:37:31 +0000 | [diff] [blame] | 680 | |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 681 | if (sz == static_cast<ssize_t>(len)) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 682 | last_error_ = 0; |
| 683 | return true; |
| 684 | } |
| 685 | |
Hector Dearman | 5145e50 | 2019-09-18 16:52:24 +0100 | [diff] [blame] | 686 | // If sendmsg() succeeds but the returned size is < |len| it means that the |
Primiano Tucci | 5ae66da | 2018-03-28 15:57:34 +0100 | [diff] [blame] | 687 | // endpoint disconnected in the middle of the read, and we managed to send |
| 688 | // only a portion of the buffer. In this case we should just give up. |
| 689 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 690 | if (sz < 0 && (saved_errno == EAGAIN || saved_errno == EWOULDBLOCK)) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 691 | // A genuine out-of-buffer. The client should retry or give up. |
| 692 | // Man pages specify that EAGAIN and EWOULDBLOCK have the same semantic here |
| 693 | // and clients should check for both. |
| 694 | last_error_ = EAGAIN; |
| 695 | return false; |
| 696 | } |
| 697 | |
Hector Dearman | 2442d61 | 2019-06-04 18:05:23 +0100 | [diff] [blame] | 698 | // Either the other endpoint disconnected (ECONNRESET) or some other error |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 699 | // happened. |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 700 | last_error_ = saved_errno; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 701 | PERFETTO_DPLOG("sendmsg() failed"); |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 702 | Shutdown(true); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 703 | return false; |
| 704 | } |
| 705 | |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 706 | void UnixSocket::Shutdown(bool notify) { |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 707 | WeakPtr<UnixSocket> weak_ptr = weak_ptr_factory_.GetWeakPtr(); |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 708 | if (notify) { |
| 709 | if (state_ == State::kConnected) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 710 | task_runner_->PostTask([weak_ptr] { |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 711 | if (weak_ptr) |
| 712 | weak_ptr->event_listener_->OnDisconnect(weak_ptr.get()); |
| 713 | }); |
| 714 | } else if (state_ == State::kConnecting) { |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 715 | task_runner_->PostTask([weak_ptr] { |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 716 | if (weak_ptr) |
| 717 | weak_ptr->event_listener_->OnConnect(weak_ptr.get(), false); |
| 718 | }); |
| 719 | } |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 720 | } |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 721 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 722 | if (sock_raw_) { |
| 723 | task_runner_->RemoveFileDescriptorWatch(sock_raw_.fd()); |
| 724 | sock_raw_.Shutdown(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 725 | } |
| 726 | state_ = State::kDisconnected; |
| 727 | } |
| 728 | |
Florian Mayer | e1cd07d | 2018-07-31 11:56:41 +0100 | [diff] [blame] | 729 | size_t UnixSocket::Receive(void* msg, |
| 730 | size_t len, |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 731 | ScopedFile* fd_vec, |
Florian Mayer | e1cd07d | 2018-07-31 11:56:41 +0100 | [diff] [blame] | 732 | size_t max_files) { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 733 | if (state_ != State::kConnected) { |
| 734 | last_error_ = ENOTCONN; |
| 735 | return 0; |
| 736 | } |
| 737 | |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 738 | const ssize_t sz = sock_raw_.Receive(msg, len, fd_vec, max_files); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 739 | if (sz < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { |
| 740 | last_error_ = EAGAIN; |
| 741 | return 0; |
| 742 | } |
| 743 | if (sz <= 0) { |
| 744 | last_error_ = errno; |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 745 | Shutdown(true); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | PERFETTO_CHECK(static_cast<size_t>(sz) <= len); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 749 | return static_cast<size_t>(sz); |
| 750 | } |
| 751 | |
| 752 | std::string UnixSocket::ReceiveString(size_t max_length) { |
| 753 | std::unique_ptr<char[]> buf(new char[max_length + 1]); |
| 754 | size_t rsize = Receive(buf.get(), max_length); |
| 755 | PERFETTO_CHECK(static_cast<size_t>(rsize) <= max_length); |
| 756 | buf[static_cast<size_t>(rsize)] = '\0'; |
| 757 | return std::string(buf.get()); |
| 758 | } |
| 759 | |
| 760 | void UnixSocket::NotifyConnectionState(bool success) { |
Florian Mayer | 48c139c | 2018-03-12 14:07:57 +0000 | [diff] [blame] | 761 | if (!success) |
| 762 | Shutdown(false); |
| 763 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 764 | WeakPtr<UnixSocket> weak_ptr = weak_ptr_factory_.GetWeakPtr(); |
Primiano Tucci | 43ebf4d | 2019-01-02 20:04:34 +0100 | [diff] [blame] | 765 | task_runner_->PostTask([weak_ptr, success] { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 766 | if (weak_ptr) |
| 767 | weak_ptr->event_listener_->OnConnect(weak_ptr.get(), success); |
| 768 | }); |
| 769 | } |
| 770 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 771 | UnixSocket::EventListener::~EventListener() {} |
| 772 | void UnixSocket::EventListener::OnNewIncomingConnection( |
| 773 | UnixSocket*, |
| 774 | std::unique_ptr<UnixSocket>) {} |
| 775 | void UnixSocket::EventListener::OnConnect(UnixSocket*, bool) {} |
| 776 | void UnixSocket::EventListener::OnDisconnect(UnixSocket*) {} |
| 777 | void UnixSocket::EventListener::OnDataAvailable(UnixSocket*) {} |
| 778 | |
Florian Mayer | f7f0def | 2018-09-27 13:59:24 +0100 | [diff] [blame] | 779 | } // namespace base |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 780 | } // namespace perfetto |