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