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