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 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <utils/Thread.h> |
| 21 | |
| 22 | namespace android { |
| 23 | namespace lshal { |
| 24 | |
| 25 | struct PipeRelay::RelayThread : public Thread { |
| 26 | explicit RelayThread(int fd, std::ostream &os); |
| 27 | |
| 28 | bool threadLoop() override; |
| 29 | |
| 30 | private: |
| 31 | int mFd; |
| 32 | std::ostream &mOutStream; |
| 33 | |
| 34 | DISALLOW_COPY_AND_ASSIGN(RelayThread); |
| 35 | }; |
| 36 | |
| 37 | //////////////////////////////////////////////////////////////////////////////// |
| 38 | |
| 39 | PipeRelay::RelayThread::RelayThread(int fd, std::ostream &os) |
| 40 | : mFd(fd), |
| 41 | mOutStream(os) { |
| 42 | } |
| 43 | |
| 44 | bool PipeRelay::RelayThread::threadLoop() { |
| 45 | char buffer[1024]; |
| 46 | ssize_t n = read(mFd, buffer, sizeof(buffer)); |
| 47 | |
| 48 | if (n <= 0) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | mOutStream.write(buffer, n); |
| 53 | |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | //////////////////////////////////////////////////////////////////////////////// |
| 58 | |
| 59 | PipeRelay::PipeRelay(std::ostream &os) |
| 60 | : mOutStream(os), |
| 61 | mInitCheck(NO_INIT) { |
| 62 | int res = socketpair(AF_UNIX, SOCK_STREAM, 0 /* protocol */, mFds); |
| 63 | |
| 64 | if (res < 0) { |
| 65 | mInitCheck = -errno; |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | mThread = new RelayThread(mFds[0], os); |
| 70 | mInitCheck = mThread->run("RelayThread"); |
| 71 | } |
| 72 | |
| 73 | // static |
| 74 | void PipeRelay::CloseFd(int *fd) { |
| 75 | if (*fd >= 0) { |
| 76 | close(*fd); |
| 77 | *fd = -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | PipeRelay::~PipeRelay() { |
| 82 | if (mFds[1] >= 0) { |
| 83 | shutdown(mFds[1], SHUT_WR); |
| 84 | } |
| 85 | |
| 86 | if (mFds[0] >= 0) { |
| 87 | shutdown(mFds[0], SHUT_RD); |
| 88 | } |
| 89 | |
| 90 | if (mThread != NULL) { |
| 91 | mThread->join(); |
| 92 | mThread.clear(); |
| 93 | } |
| 94 | |
| 95 | CloseFd(&mFds[1]); |
| 96 | CloseFd(&mFds[0]); |
| 97 | } |
| 98 | |
| 99 | status_t PipeRelay::initCheck() const { |
| 100 | return mInitCheck; |
| 101 | } |
| 102 | |
| 103 | int PipeRelay::fd() const { |
| 104 | return mFds[1]; |
| 105 | } |
| 106 | |
| 107 | } // namespace lshal |
| 108 | } // namespace android |