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 #include "ProcessPipe.h"
18 
19 #include <errno.h>
20 #include <log/log.h>
21 #include <pthread.h>
22 #include <qemu_pipe_bp.h>
23 
24 #include "HostConnection.h"
25 
26 #ifndef __Fuchsia__
27 
28 #include "QemuPipeStream.h"
29 #include "VirtioGpuPipeStream.h"
30 
31 static QemuPipeStream* sQemuPipeStream = nullptr;
32 static VirtioGpuPipeStream* sVirtioGpuPipeStream = nullptr;
33 static int sStreamHandle = -1;
34 
35 #endif  // !__Fuchsia__
36 // sProcUID is a unique ID per process assigned by the host.
37 // It is different from getpid().
38 static uint64_t           sProcUID = 0;
39 static HostConnectionType sConnType = HOST_CONNECTION_VIRTIO_GPU_PIPE;
40 
41 namespace {
42 
43 static std::mutex sNeedInitMutex;
44 static bool sNeedInit = true;
45 
46 }  // namespace
47 
processPipeDoInit(uint32_t noRenderControlEnc)48 static void processPipeDoInit(uint32_t noRenderControlEnc) {
49     // No need to setup auxiliary pipe stream in this case
50     if (noRenderControlEnc) return;
51 
52 #if defined(__Fuchsia__)
53     // Note: sProcUID is not initialized.
54     ALOGE("Fuchsia: requires noRenderControlEnc");
55     abort();
56 #else
57     switch (sConnType) {
58         // TODO: Move those over too
59         case HOST_CONNECTION_QEMU_PIPE:
60         case HOST_CONNECTION_ADDRESS_SPACE:
61             sQemuPipeStream = new QemuPipeStream();
62             sProcUID = sQemuPipeStream->processPipeInit();
63             break;
64         case HOST_CONNECTION_VIRTIO_GPU_PIPE:
65         case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
66             sVirtioGpuPipeStream = new VirtioGpuPipeStream(4096, sStreamHandle);
67             sProcUID = sVirtioGpuPipeStream->processPipeInit();
68             break;
69         }
70     }
71 #endif
72 }
73 
processPipeInit(int streamHandle,HostConnectionType connType,uint32_t noRenderControlEnc)74 bool processPipeInit(int streamHandle, HostConnectionType connType, uint32_t noRenderControlEnc) {
75     sConnType = connType;
76 #ifndef __Fuchsia__
77     sStreamHandle = streamHandle;
78 #endif // !__Fuchsia
79 
80     {
81         std::lock_guard<std::mutex> lock(sNeedInitMutex);
82 
83         if (sNeedInit) {
84             sNeedInit = false;
85             processPipeDoInit(noRenderControlEnc);
86 
87             if (noRenderControlEnc) {
88                 return true;
89             }
90 
91 #ifndef __Fuchsia__
92             if (!sQemuPipeStream && !sVirtioGpuPipeStream) {
93                 return false;
94             }
95 #endif
96         }
97     }
98 
99     return true;
100 }
101 
getPuid()102 uint64_t getPuid() { return sProcUID; }
103