| Bart Van Assche | 1693f42 | 2022-10-19 16:30:15 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2022 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 "interprocess_fifo.h" | 
|  | 18 |  | 
|  | 19 | #include <android-base/logging.h> | 
|  | 20 |  | 
|  | 21 | #include <unistd.h> | 
|  | 22 |  | 
|  | 23 | using ::android::base::ErrnoError; | 
|  | 24 | using ::android::base::Error; | 
|  | 25 | using ::android::base::Result; | 
|  | 26 |  | 
|  | 27 | namespace android { | 
|  | 28 | namespace init { | 
|  | 29 |  | 
|  | 30 | InterprocessFifo::InterprocessFifo() noexcept : fds_({-1, -1}) {} | 
|  | 31 |  | 
|  | 32 | InterprocessFifo::InterprocessFifo(InterprocessFifo&& orig) noexcept : fds_({-1, -1}) { | 
|  | 33 | std::swap(fds_, orig.fds_); | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | InterprocessFifo::~InterprocessFifo() noexcept { | 
|  | 37 | Close(); | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | void InterprocessFifo::CloseFd(int& fd) noexcept { | 
|  | 41 | if (fd >= 0) { | 
|  | 42 | close(fd); | 
|  | 43 | fd = -1; | 
|  | 44 | } | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | void InterprocessFifo::CloseReadFd() noexcept { | 
|  | 48 | CloseFd(fds_[0]); | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | void InterprocessFifo::CloseWriteFd() noexcept { | 
|  | 52 | CloseFd(fds_[1]); | 
|  | 53 | } | 
|  | 54 |  | 
|  | 55 | void InterprocessFifo::Close() noexcept { | 
|  | 56 | CloseReadFd(); | 
|  | 57 | CloseWriteFd(); | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | Result<void> InterprocessFifo::Initialize() noexcept { | 
|  | 61 | if (fds_[0] >= 0) { | 
|  | 62 | return Error() << "already initialized"; | 
|  | 63 | } | 
|  | 64 | if (pipe(fds_.data()) < 0) {  // NOLINT(android-cloexec-pipe) | 
|  | 65 | return ErrnoError() << "pipe()"; | 
|  | 66 | } | 
|  | 67 | return {}; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | Result<uint8_t> InterprocessFifo::Read() noexcept { | 
|  | 71 | uint8_t byte; | 
|  | 72 | ssize_t count = read(fds_[0], &byte, 1); | 
|  | 73 | if (count < 0) { | 
|  | 74 | return ErrnoError() << "read()"; | 
|  | 75 | } | 
|  | 76 | if (count == 0) { | 
|  | 77 | return Error() << "read() EOF"; | 
|  | 78 | } | 
|  | 79 | DCHECK_EQ(count, 1); | 
|  | 80 | return byte; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | Result<void> InterprocessFifo::Write(uint8_t byte) noexcept { | 
|  | 84 | ssize_t written = write(fds_[1], &byte, 1); | 
|  | 85 | if (written < 0) { | 
|  | 86 | return ErrnoError() << "write()"; | 
|  | 87 | } | 
|  | 88 | if (written == 0) { | 
|  | 89 | return Error() << "write() EOF"; | 
|  | 90 | } | 
|  | 91 | DCHECK_EQ(written, 1); | 
|  | 92 | return {}; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | }  // namespace init | 
|  | 96 | }  // namespace android |