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 17 #pragma once 18 19 #include <thread> 20 21 #include <android-base/macros.h> 22 #include <android-base/result.h> 23 #include <android-base/unique_fd.h> 24 #include <utils/Errors.h> 25 #include <utils/RefBase.h> 26 #include <ostream> 27 28 #include "NullableOStream.h" 29 30 namespace android { 31 namespace lshal { 32 33 /** 34 * Creates a pipe and spawns a thread that relays any data 35 * written to the "write"-end of the pair to the specified output stream "os". 36 */ 37 struct PipeRelay { 38 static android::base::Result<std::unique_ptr<PipeRelay>> create( 39 std::ostream& os, const NullableOStream<std::ostream>& err, const std::string& fqName); 40 ~PipeRelay(); 41 42 // Returns the file descriptor corresponding to the "write"-end of the 43 // connection. fdPipeRelay44 android::base::borrowed_fd fd() const { return mWrite; } 45 46 private: 47 PipeRelay() = default; 48 DISALLOW_COPY_AND_ASSIGN(PipeRelay); 49 static void thread(android::base::unique_fd rfd, android::base::unique_fd rfdTrigger, 50 std::ostream* out, const NullableOStream<std::ostream>* err, 51 std::string fqName); 52 53 android::base::unique_fd mWrite; 54 android::base::unique_fd mWriteTrigger; 55 std::unique_ptr<std::thread> mThread; 56 }; 57 58 } // namespace lshal 59 } // namespace android 60