| 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 |  | 
|  | 17 | #include "PipeRelay.h" | 
|  | 18 |  | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 19 | #include <utils/Thread.h> | 
|  | 20 |  | 
|  | 21 | namespace android { | 
|  | 22 | namespace lshal { | 
|  | 23 |  | 
|  | 24 | struct PipeRelay::RelayThread : public Thread { | 
|  | 25 | explicit RelayThread(int fd, std::ostream &os); | 
|  | 26 |  | 
|  | 27 | bool threadLoop() override; | 
|  | 28 |  | 
|  | 29 | private: | 
|  | 30 | int mFd; | 
|  | 31 | std::ostream &mOutStream; | 
|  | 32 |  | 
|  | 33 | DISALLOW_COPY_AND_ASSIGN(RelayThread); | 
|  | 34 | }; | 
|  | 35 |  | 
|  | 36 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 37 |  | 
|  | 38 | PipeRelay::RelayThread::RelayThread(int fd, std::ostream &os) | 
|  | 39 | : mFd(fd), | 
|  | 40 | mOutStream(os) { | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | bool PipeRelay::RelayThread::threadLoop() { | 
|  | 44 | char buffer[1024]; | 
|  | 45 | ssize_t n = read(mFd, buffer, sizeof(buffer)); | 
|  | 46 |  | 
|  | 47 | if (n <= 0) { | 
|  | 48 | return false; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | mOutStream.write(buffer, n); | 
|  | 52 |  | 
|  | 53 | return true; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | //////////////////////////////////////////////////////////////////////////////// | 
|  | 57 |  | 
|  | 58 | PipeRelay::PipeRelay(std::ostream &os) | 
| Chih-Hung Hsieh | 734e378 | 2017-10-05 13:44:13 -0700 | [diff] [blame] | 59 | : mInitCheck(NO_INIT) { | 
| Andreas Huber | 7dfac44 | 2018-05-22 12:56:38 -0700 | [diff] [blame] | 60 | int res = pipe(mFds); | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 61 |  | 
|  | 62 | if (res < 0) { | 
|  | 63 | mInitCheck = -errno; | 
|  | 64 | return; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | mThread = new RelayThread(mFds[0], os); | 
|  | 68 | mInitCheck = mThread->run("RelayThread"); | 
|  | 69 | } | 
|  | 70 |  | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 71 | void PipeRelay::CloseFd(int *fd) { | 
|  | 72 | if (*fd >= 0) { | 
|  | 73 | close(*fd); | 
|  | 74 | *fd = -1; | 
|  | 75 | } | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | PipeRelay::~PipeRelay() { | 
| Andreas Huber | 7dfac44 | 2018-05-22 12:56:38 -0700 | [diff] [blame] | 79 | CloseFd(&mFds[1]); | 
| Yifan Hong | b9d19c8 | 2018-08-02 14:09:55 -0700 | [diff] [blame] | 80 | CloseFd(&mFds[0]); | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 81 |  | 
| Yi Kong | 19d5c00 | 2018-07-20 13:39:55 -0700 | [diff] [blame] | 82 | if (mThread != nullptr) { | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 83 | mThread->join(); | 
|  | 84 | mThread.clear(); | 
|  | 85 | } | 
| Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | status_t PipeRelay::initCheck() const { | 
|  | 89 | return mInitCheck; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | int PipeRelay::fd() const { | 
|  | 93 | return mFds[1]; | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | }  // namespace lshal | 
|  | 97 | }  // namespace android |