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 #pragma once
18 
19 #include <android-base/macros.h>
20 #include <ostream>
21 #include <utils/Errors.h>
22 #include <utils/RefBase.h>
23 
24 #include "NullableOStream.h"
25 
26 namespace android {
27 namespace lshal {
28 
29 /* Creates an AF_UNIX socketpair and spawns a thread that relays any data
30  * written to the "write"-end of the pair to the specified output stream "os".
31  */
32 struct PipeRelay {
33     explicit PipeRelay(std::ostream& os,
34                        const NullableOStream<std::ostream>& err,
35                        const std::string& interfaceName,
36                        const std::string& instanceName);
37     ~PipeRelay();
38 
39     status_t initCheck() const;
40 
41     // Returns the file descriptor corresponding to the "write"-end of the
42     // connection.
43     int fd() const;
44 
45 private:
46     struct RelayThread;
47 
48     status_t mInitCheck;
49     int mFds[2];
50     sp<RelayThread> mThread;
51 
52     static void CloseFd(int *fd);
53 
54     DISALLOW_COPY_AND_ASSIGN(PipeRelay);
55 };
56 
57 }  // namespace lshal
58 }  // namespace android
59