Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | #pragma once |
| 18 | |
| 19 | #include <binder/unique_fd.h> |
| 20 | |
| 21 | #if defined(_WIN32) || defined(__TRUSTY__) |
| 22 | // Pipe and Socketpair are missing there |
| 23 | #elif !defined(BINDER_NO_LIBBASE) |
| 24 | |
| 25 | namespace android::binder { |
| 26 | using android::base::Pipe; |
| 27 | using android::base::Socketpair; |
| 28 | } // namespace android::binder |
| 29 | |
| 30 | #else // BINDER_NO_LIBBASE |
| 31 | |
| 32 | #include <sys/socket.h> |
| 33 | |
| 34 | namespace android::binder { |
| 35 | |
| 36 | // Inline functions, so that they can be used header-only. |
| 37 | |
| 38 | // See pipe(2). |
| 39 | // This helper hides the details of converting to unique_fd, and also hides the |
| 40 | // fact that macOS doesn't support O_CLOEXEC or O_NONBLOCK directly. |
| 41 | inline bool Pipe(unique_fd* read, unique_fd* write, int flags = O_CLOEXEC) { |
| 42 | int pipefd[2]; |
| 43 | |
| 44 | #if defined(__APPLE__) |
| 45 | if (flags & ~(O_CLOEXEC | O_NONBLOCK)) { |
| 46 | return false; |
| 47 | } |
| 48 | if (pipe(pipefd) != 0) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if (flags & O_CLOEXEC) { |
| 53 | if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || |
| 54 | fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) { |
| 55 | close(pipefd[0]); |
| 56 | close(pipefd[1]); |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | if (flags & O_NONBLOCK) { |
| 61 | if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0 || |
| 62 | fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) { |
| 63 | close(pipefd[0]); |
| 64 | close(pipefd[1]); |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | #else |
| 69 | if (pipe2(pipefd, flags) != 0) { |
| 70 | return false; |
| 71 | } |
| 72 | #endif |
| 73 | |
| 74 | read->reset(pipefd[0]); |
| 75 | write->reset(pipefd[1]); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | // See socketpair(2). |
| 80 | // This helper hides the details of converting to unique_fd. |
| 81 | inline bool Socketpair(int domain, int type, int protocol, unique_fd* left, unique_fd* right) { |
| 82 | int sockfd[2]; |
| 83 | if (socketpair(domain, type, protocol, sockfd) != 0) { |
| 84 | return false; |
| 85 | } |
| 86 | left->reset(sockfd[0]); |
| 87 | right->reset(sockfd[1]); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | // See socketpair(2). |
| 92 | // This helper hides the details of converting to unique_fd. |
| 93 | inline bool Socketpair(int type, unique_fd* left, unique_fd* right) { |
| 94 | return Socketpair(AF_UNIX, type, 0, left, right); |
| 95 | } |
| 96 | |
| 97 | } // namespace android::binder |
| 98 | |
| 99 | #endif // BINDER_NO_LIBBASE |