xref: /aosp_15_r20/frameworks/native/libs/gui/tests/testserver/TestServerHost.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2024 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 #define LOG_TAG "TestServerHost"
18 
19 #include <android-base/unique_fd.h>
20 #include <binder/IInterface.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 #include <binder/Status.h>
25 #include <gui/BufferQueue.h>
26 #include <gui/IConsumerListener.h>
27 #include <gui/IGraphicBufferConsumer.h>
28 #include <gui/IGraphicBufferProducer.h>
29 #include <libgui_test_server/BnTestServer.h>
30 #include <log/log.h>
31 #include <utils/Errors.h>
32 
33 #include <memory>
34 #include <vector>
35 
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <cstddef>
39 #include <cstdlib>
40 
41 #include "TestServerCommon.h"
42 #include "TestServerHost.h"
43 
44 namespace android {
45 
46 namespace {
47 
ForkTestServer(const char * filename,char * name)48 pid_t ForkTestServer(const char* filename, char* name) {
49     pid_t childPid = fork();
50     LOG_ALWAYS_FATAL_IF(childPid == -1);
51 
52     if (childPid != 0) {
53         return childPid;
54     }
55 
56     // We forked!
57     const char* test_server_flag = "--test-server";
58     char* args[] = {
59             const_cast<char*>(filename),
60             const_cast<char*>(test_server_flag),
61             name,
62             nullptr,
63     };
64 
65     int ret = execv(filename, args);
66     ALOGE("Failed to exec libgui_test as a TestServer. ret=%d errno=%d (%s)", ret, errno,
67           strerror(errno));
68     _exit(EXIT_FAILURE);
69 }
70 
71 } // namespace
72 
TestServerHostMain(const char * filename,base::unique_fd sendPipeFd,base::unique_fd recvPipeFd)73 int TestServerHostMain(const char* filename, base::unique_fd sendPipeFd,
74                        base::unique_fd recvPipeFd) {
75     status_t status = OK;
76     LOG_ALWAYS_FATAL_IF(sizeof(status) != write(sendPipeFd.get(), &status, sizeof(status)));
77 
78     ALOGE("Launched TestServerHost");
79 
80     while (true) {
81         CreateServerRequest request = {};
82         ssize_t bytes = read(recvPipeFd.get(), &request, sizeof(request));
83         LOG_ALWAYS_FATAL_IF(bytes != sizeof(request));
84         pid_t childPid = ForkTestServer(filename, request.name);
85 
86         CreateServerResponse response = {};
87         response.pid = childPid;
88         bytes = write(sendPipeFd.get(), &response, sizeof(response));
89         LOG_ALWAYS_FATAL_IF(bytes != sizeof(response));
90     }
91 
92     return 0;
93 }
94 
95 } // namespace android