| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 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 | #ifndef LIBMEMUNREACHABLE_SCOPED_PIPE_H_ | 
 | 18 | #define LIBMEMUNREACHABLE_SCOPED_PIPE_H_ | 
 | 19 |  | 
 | 20 | #include <unistd.h> | 
 | 21 |  | 
 | 22 | #include "log.h" | 
 | 23 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 24 | namespace android { | 
 | 25 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 26 | class ScopedPipe { | 
 | 27 |  public: | 
 | 28 |   ScopedPipe() : pipefd_{-1, -1} { | 
 | 29 |     int ret = pipe2(pipefd_, O_CLOEXEC); | 
 | 30 |     if (ret < 0) { | 
| Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 31 |       MEM_LOG_ALWAYS_FATAL("failed to open pipe"); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 32 |     } | 
 | 33 |   } | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 34 |   ~ScopedPipe() { Close(); } | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 35 |  | 
 | 36 |   ScopedPipe(ScopedPipe&& other) { | 
 | 37 |     SetReceiver(other.ReleaseReceiver()); | 
 | 38 |     SetSender(other.ReleaseSender()); | 
 | 39 |   } | 
 | 40 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 41 |   ScopedPipe& operator=(ScopedPipe&& other) { | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 42 |     SetReceiver(other.ReleaseReceiver()); | 
 | 43 |     SetSender(other.ReleaseSender()); | 
 | 44 |     return *this; | 
 | 45 |   } | 
 | 46 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 47 |   void CloseReceiver() { close(ReleaseReceiver()); } | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 48 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 49 |   void CloseSender() { close(ReleaseSender()); } | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 50 |  | 
 | 51 |   void Close() { | 
 | 52 |     CloseReceiver(); | 
 | 53 |     CloseSender(); | 
 | 54 |   } | 
 | 55 |  | 
 | 56 |   int Receiver() { return pipefd_[0]; } | 
 | 57 |   int Sender() { return pipefd_[1]; } | 
 | 58 |  | 
 | 59 |   int ReleaseReceiver() { | 
 | 60 |     int ret = Receiver(); | 
 | 61 |     SetReceiver(-1); | 
 | 62 |     return ret; | 
 | 63 |   } | 
 | 64 |  | 
 | 65 |   int ReleaseSender() { | 
 | 66 |     int ret = Sender(); | 
 | 67 |     SetSender(-1); | 
 | 68 |     return ret; | 
 | 69 |   } | 
 | 70 |  | 
 | 71 |  private: | 
 | 72 |   void SetReceiver(int fd) { pipefd_[0] = fd; }; | 
 | 73 |   void SetSender(int fd) { pipefd_[1] = fd; }; | 
 | 74 |  | 
 | 75 |   int pipefd_[2]; | 
 | 76 | }; | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 77 |  | 
 | 78 | }  // namespace android | 
 | 79 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 80 | #endif |