hidl_test_servers.cpp revision 7b680ebdc6b150e808dc85ff5fae6344b20a206b
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 "hidl_test.h"
20
21#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>
28#include <android/hardware/tests/hash/1.0/IHash.h>
29#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;
49using ::android::hardware::tests::hash::V1_0::IHash;
50using ::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 */ []) {
110    EACH_SERVER(forkServer);
111
112    forkServer<IBaz>("dyingBaz");
113
114    signal(SIGTERM, signal_handler);
115    // Parent process should not exit before the forked child processes.
116    pause();
117
118    return 0;
119}
120