1 /* 2 * Copyright 2020 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 #include <unistd.h> 19 /** 20 * A pipe class for use when testing or fuzzing Looper 21 */ 22 class Pipe { 23 public: 24 int sendFd; 25 int receiveFd; 26 Pipe()27 Pipe() { 28 int fds[2]; 29 ::pipe(fds); 30 31 receiveFd = fds[0]; 32 sendFd = fds[1]; 33 } 34 ~Pipe()35 ~Pipe() { 36 if (sendFd != -1) { 37 ::close(sendFd); 38 } 39 40 if (receiveFd != -1) { 41 ::close(receiveFd); 42 } 43 } 44 writeSignal()45 android::status_t writeSignal() { 46 ssize_t nWritten = ::write(sendFd, "*", 1); 47 return nWritten == 1 ? 0 : -errno; 48 } 49 readSignal()50 android::status_t readSignal() { 51 char buf[1]; 52 ssize_t nRead = ::read(receiveFd, buf, 1); 53 return nRead == 1 ? 0 : nRead == 0 ? -EPIPE : -errno; 54 } 55 }; 56