| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [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 |  | 
| Steven Moreland | 0b8e387 | 2019-06-25 08:54:34 -0700 | [diff] [blame] | 17 | #pragma once | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 18 |  | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 19 | #include <thread> | 
|  | 20 |  | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 21 | #include <android-base/macros.h> | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 22 | #include <android-base/result.h> | 
|  | 23 | #include <android-base/unique_fd.h> | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 24 | #include <utils/Errors.h> | 
|  | 25 | #include <utils/RefBase.h> | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 26 | #include <ostream> | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 27 |  | 
| Yifan Hong | 8a66247 | 2020-09-24 11:50:50 -0700 | [diff] [blame] | 28 | #include "NullableOStream.h" | 
|  | 29 |  | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 30 | namespace android { | 
|  | 31 | namespace lshal { | 
|  | 32 |  | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 33 | /** | 
|  | 34 | * Creates a pipe and spawns a thread that relays any data | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 35 | * written to the "write"-end of the pair to the specified output stream "os". | 
|  | 36 | */ | 
|  | 37 | struct PipeRelay { | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 38 | static android::base::Result<std::unique_ptr<PipeRelay>> create( | 
|  | 39 | std::ostream& os, const NullableOStream<std::ostream>& err, const std::string& fqName); | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 40 | ~PipeRelay(); | 
|  | 41 |  | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 42 | // Returns the file descriptor corresponding to the "write"-end of the | 
|  | 43 | // connection. | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 44 | android::base::borrowed_fd fd() const { return mWrite; } | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | private: | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 47 | PipeRelay() = default; | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 48 | DISALLOW_COPY_AND_ASSIGN(PipeRelay); | 
| Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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; | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | }  // namespace lshal | 
|  | 59 | }  // namespace android |