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 #include "renderControl_enc.h"
19
20 #include <qemu_pipe_bp.h>
21
22 #if PLATFORM_SDK_VERSION < 26
23 #include <cutils/log.h>
24 #else
25 #include <log/log.h>
26 #endif
27 #include <pthread.h>
28 #include <errno.h>
29
30 #ifdef __Fuchsia__
31 #include <fuchsia/hardware/goldfish/llcpp/fidl.h>
32 #include <lib/zx/vmo.h>
33
34 #include "services/service_connector.h"
35
36 #define GET_STATUS_SAFE(result, member) \
37 ((result).ok() ? ((result).Unwrap()->member) : ZX_OK)
38
39 static QEMU_PIPE_HANDLE sProcDevice = 0;
40 #else // __Fuchsia__
41
42 #include "VirtioGpuPipeStream.h"
43 static VirtioGpuPipeStream* sVirtioGpuPipeStream = 0;
44
45 #endif // !__Fuchsia__
46
47 static QEMU_PIPE_HANDLE sProcPipe = 0;
48 static pthread_once_t sProcPipeOnce = PTHREAD_ONCE_INIT;
49 // sProcUID is a unique ID per process assigned by the host.
50 // It is different from getpid().
51 static uint64_t sProcUID = 0;
52 static volatile HostConnectionType sConnType = HOST_CONNECTION_VIRTIO_GPU_PIPE;
53
54 // processPipeInitOnce is used to generate a process unique ID (puid).
55 // processPipeInitOnce will only be called at most once per process.
56 // Use it with pthread_once for thread safety.
57 // The host associates resources with process unique ID (puid) for memory cleanup.
58 // It will fallback to the default path if the host does not support it.
59 // Processes are identified by acquiring a per-process 64bit unique ID from the
60 // host.
61 #ifdef __Fuchsia__
processPipeInitOnce()62 static void processPipeInitOnce() {
63 zx::channel channel(GetConnectToServiceFunction()(QEMU_PIPE_PATH));
64 if (!channel) {
65 ALOGE("%s: failed to open " QEMU_PIPE_PATH,
66 __FUNCTION__);
67 return;
68 }
69
70 llcpp::fuchsia::hardware::goldfish::PipeDevice::SyncClient device(
71 std::move(channel));
72
73 zx::channel pipe_server, pipe_client;
74 zx_status_t status = zx::channel::create(0, &pipe_server, &pipe_client);
75 if (status != ZX_OK) {
76 ALOGE("%s: zx_channel_create failed: %d", __FUNCTION__, status);
77 return;
78 }
79
80 llcpp::fuchsia::hardware::goldfish::Pipe::SyncClient pipe(
81 std::move(pipe_client));
82 device.OpenPipe(std::move(pipe_server));
83
84 zx::vmo vmo;
85 {
86 auto result = pipe.GetBuffer();
87 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
88 ALOGE("%s: failed to get buffer: %d:%d", __FUNCTION__,
89 result.status(), GET_STATUS_SAFE(result, res));
90 return;
91 }
92 vmo = std::move(result.Unwrap()->vmo);
93 }
94
95 size_t len = strlen("pipe:GLProcessPipe");
96 status = vmo.write("pipe:GLProcessPipe", 0, len + 1);
97 if (status != ZX_OK) {
98 ALOGE("%s: failed write pipe name", __FUNCTION__);
99 return;
100 }
101
102 {
103 auto result = pipe.Write(len + 1, 0);
104 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
105 ALOGD("%s: connecting to pipe service failed: %d:%d", __FUNCTION__,
106 result.status(), GET_STATUS_SAFE(result, res));
107 return;
108 }
109 }
110
111 // Send a confirmation int to the host and get per-process unique ID back
112 int32_t confirmInt = 100;
113 status = vmo.write(&confirmInt, 0, sizeof(confirmInt));
114 if (status != ZX_OK) {
115 ALOGE("%s: failed write confirm int", __FUNCTION__);
116 return;
117 }
118
119 {
120 auto result = pipe.DoCall(sizeof(confirmInt), 0, sizeof(sProcUID), 0);
121 if (!result.ok() || result.Unwrap()->res != ZX_OK) {
122 ALOGD("%s: failed to get per-process ID: %d:%d", __FUNCTION__,
123 result.status(), GET_STATUS_SAFE(result, res));
124 return;
125 }
126 }
127
128 status = vmo.read(&sProcUID, 0, sizeof(sProcUID));
129 if (status != ZX_OK) {
130 ALOGE("%s: failed read per-process ID: %d", __FUNCTION__, status);
131 return;
132 }
133 sProcDevice = device.mutable_channel()->release();
134 sProcPipe = pipe.mutable_channel()->release();
135 }
136 #else // __Fuchsia__
137
sQemuPipeInit()138 static void sQemuPipeInit() {
139 sProcPipe = qemu_pipe_open("GLProcessPipe");
140 if (!qemu_pipe_valid(sProcPipe)) {
141 sProcPipe = 0;
142 ALOGW("Process pipe failed");
143 return;
144 }
145 // Send a confirmation int to the host
146 int32_t confirmInt = 100;
147 ssize_t stat = 0;
148 do {
149 stat =
150 qemu_pipe_write(sProcPipe, (const char*)&confirmInt,
151 sizeof(confirmInt));
152 } while (stat < 0 && errno == EINTR);
153
154 if (stat != sizeof(confirmInt)) { // failed
155 qemu_pipe_close(sProcPipe);
156 sProcPipe = 0;
157 ALOGW("Process pipe failed");
158 return;
159 }
160
161 // Ask the host for per-process unique ID
162 do {
163 stat =
164 qemu_pipe_read(sProcPipe, (char*)&sProcUID,
165 sizeof(sProcUID));
166 } while (stat < 0 && (errno == EINTR || errno == EAGAIN));
167
168 if (stat != sizeof(sProcUID)) {
169 qemu_pipe_close(sProcPipe);
170 sProcPipe = 0;
171 sProcUID = 0;
172 ALOGW("Process pipe failed");
173 return;
174 }
175 }
176
processPipeInitOnce()177 static void processPipeInitOnce() {
178 #if defined(HOST_BUILD) || !defined(GOLDFISH_VULKAN)
179 sQemuPipeInit();
180 #else // HOST_BUILD
181 switch (sConnType) {
182 // TODO: Move those over too
183 case HOST_CONNECTION_QEMU_PIPE:
184 case HOST_CONNECTION_ADDRESS_SPACE:
185 case HOST_CONNECTION_TCP:
186 case HOST_CONNECTION_VIRTIO_GPU:
187 sQemuPipeInit();
188 break;
189 case HOST_CONNECTION_VIRTIO_GPU_PIPE:
190 case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
191 sVirtioGpuPipeStream = new VirtioGpuPipeStream(4096);
192 sProcUID = sVirtioGpuPipeStream->initProcessPipe();
193 break;
194 }
195 }
196 #endif // !HOST_BUILD
197 }
198 #endif // !__Fuchsia__
199
processPipeInit(HostConnectionType connType,renderControl_encoder_context_t * rcEnc)200 bool processPipeInit(HostConnectionType connType, renderControl_encoder_context_t *rcEnc) {
201 sConnType = connType;
202 pthread_once(&sProcPipeOnce, processPipeInitOnce);
203 bool pipeHandleInvalid = !sProcPipe;
204 #ifndef __Fuchsia__
205 pipeHandleInvalid = pipeHandleInvalid && !sVirtioGpuPipeStream;
206 #endif // !__Fuchsia__
207 if (pipeHandleInvalid) return false;
208 rcEnc->rcSetPuid(rcEnc, sProcUID);
209 return true;
210 }
211