blob: cdaac86b506d787fa845b7640ac04759c24c5f62 [file] [log] [blame]
Bart Van Assche1693f422022-10-19 16:30:15 -07001/*
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#pragma once
18
19#include <array>
20
21#include <android-base/result.h>
22
23namespace android {
24namespace init {
25
26// A FIFO for inter-process communication that uses a Unix pipe internally.
27class InterprocessFifo {
28 public:
29 template <typename T>
30 using Result = ::android::base::Result<T>;
31
32 InterprocessFifo() noexcept;
33 InterprocessFifo(const InterprocessFifo& orig) noexcept = delete;
34 InterprocessFifo(InterprocessFifo&& orig) noexcept;
35 InterprocessFifo& operator=(const InterprocessFifo& orig) noexcept = delete;
36 InterprocessFifo& operator=(InterprocessFifo&& orig) noexcept = delete;
37 ~InterprocessFifo() noexcept;
38 void CloseReadFd() noexcept;
39 void CloseWriteFd() noexcept;
40 void Close() noexcept;
41 Result<void> Initialize() noexcept;
42 Result<void> Write(uint8_t byte) noexcept;
43 Result<uint8_t> Read() noexcept;
44
45 private:
46 static void CloseFd(int& fd) noexcept;
47
48 std::array<int, 2> fds_;
49};
50
51} // namespace init
52} // namespace android