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 #define LOG_TAG "hidl_test_servers"
18 
19 #include <android-base/logging.h>
20 
21 #include <android/hardware/tests/foo/1.0/BnHwSimple.h>
22 #include <android/hardware/tests/foo/1.0/BsSimple.h>
23 #include <android/hardware/tests/foo/1.0/BpHwSimple.h>
24 #include <android/hardware/tests/bar/1.0/IBar.h>
25 #include <android/hardware/tests/baz/1.0/IBaz.h>
26 #include <android/hardware/tests/hash/1.0/IHash.h>
27 #include <android/hardware/tests/inheritance/1.0/IFetcher.h>
28 #include <android/hardware/tests/inheritance/1.0/IParent.h>
29 #include <android/hardware/tests/inheritance/1.0/IChild.h>
30 #include <android/hardware/tests/memory/1.0/IMemoryTest.h>
31 #include <android/hardware/tests/pointer/1.0/IGraph.h>
32 #include <android/hardware/tests/pointer/1.0/IPointer.h>
33 
34 #include <hidl/LegacySupport.h>
35 
36 #include <hwbinder/IPCThreadState.h>
37 
38 #include <sys/wait.h>
39 #include <signal.h>
40 
41 #include <string>
42 #include <utility>
43 #include <vector>
44 
45 using ::android::hardware::tests::bar::V1_0::IBar;
46 using ::android::hardware::tests::baz::V1_0::IBaz;
47 using ::android::hardware::tests::hash::V1_0::IHash;
48 using ::android::hardware::tests::inheritance::V1_0::IFetcher;
49 using ::android::hardware::tests::inheritance::V1_0::IParent;
50 using ::android::hardware::tests::inheritance::V1_0::IChild;
51 using ::android::hardware::tests::pointer::V1_0::IGraph;
52 using ::android::hardware::tests::pointer::V1_0::IPointer;
53 using ::android::hardware::tests::memory::V1_0::IMemoryTest;
54 
55 using ::android::hardware::defaultPassthroughServiceImplementation;
56 using ::android::hardware::IPCThreadState;
57 
58 static std::vector<std::pair<std::string, pid_t>> gPidList;
59 
signal_handler_server(int signal)60 void signal_handler_server(int signal) {
61     if (signal == SIGTERM) {
62         IPCThreadState::shutdown();
63         exit(0);
64     }
65 }
66 
67 template <class T>
forkServer(const std::string & serviceName)68 static void forkServer(const std::string &serviceName) {
69     pid_t pid;
70 
71     if ((pid = fork()) == 0) {
72         // in child process
73         signal(SIGTERM, signal_handler_server);
74         int status = defaultPassthroughServiceImplementation<T>(serviceName);
75         exit(status);
76     }
77 
78     gPidList.push_back({serviceName, pid});
79     return;
80 }
81 
killServer(pid_t pid,const char * serverName)82 static void killServer(pid_t pid, const char *serverName) {
83     if (kill(pid, SIGTERM)) {
84         ALOGE("Could not kill %s; errno = %d", serverName, errno);
85     } else {
86         int status;
87         ALOGE("Waiting for %s to exit...", serverName);
88         waitpid(pid, &status, 0);
89         if (status != 0) {
90             ALOGE("%s terminates abnormally with status %d", serverName, status);
91         } else {
92             ALOGE("%s killed successfully", serverName);
93         }
94         ALOGE("Continuing...");
95     }
96 }
97 
signal_handler(int signal)98 void signal_handler(int signal) {
99     if (signal == SIGTERM) {
100         for (auto p : gPidList) {
101             killServer(p.second, p.first.c_str());
102         }
103         exit(0);
104     }
105 }
106 
main(int,char * [])107 int main(int /* argc */, char* /* argv */ []) {
108     setenv("TREBLE_TESTING_OVERRIDE", "true", true);
109 
110     forkServer<IMemoryTest>("memory");
111     forkServer<IChild>("child");
112     forkServer<IParent>("parent");
113     forkServer<IFetcher>("fetcher");
114     forkServer<IBar>("foo");
115     forkServer<IHash>("default");
116     forkServer<IBaz>("dyingBaz");
117     forkServer<IGraph>("graph");
118     forkServer<IPointer>("pointer");
119 
120     signal(SIGTERM, signal_handler);
121     // Parent process should not exit before the forked child processes.
122     pause();
123 
124     return 0;
125 }
126