Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 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 | |
Steven Moreland | 7b680eb | 2017-04-19 16:34:26 -0700 | [diff] [blame] | 19 | #include "hidl_test.h" |
| 20 | |
Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 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> |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 28 | #include <android/hardware/tests/hash/1.0/IHash.h> |
Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 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 | |
| 47 | using ::android::hardware::tests::bar::V1_0::IBar; |
| 48 | using ::android::hardware::tests::baz::V1_0::IBaz; |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 49 | using ::android::hardware::tests::hash::V1_0::IHash; |
Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 50 | using ::android::hardware::tests::inheritance::V1_0::IFetcher; |
| 51 | using ::android::hardware::tests::inheritance::V1_0::IParent; |
| 52 | using ::android::hardware::tests::inheritance::V1_0::IChild; |
| 53 | using ::android::hardware::tests::pointer::V1_0::IGraph; |
| 54 | using ::android::hardware::tests::pointer::V1_0::IPointer; |
| 55 | using ::android::hardware::tests::memory::V1_0::IMemoryTest; |
| 56 | |
| 57 | using ::android::hardware::defaultPassthroughServiceImplementation; |
| 58 | using ::android::hardware::IPCThreadState; |
| 59 | |
| 60 | static std::vector<std::pair<std::string, pid_t>> gPidList; |
| 61 | |
| 62 | void signal_handler_server(int signal) { |
| 63 | if (signal == SIGTERM) { |
| 64 | IPCThreadState::shutdown(); |
| 65 | exit(0); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | template <class T> |
| 70 | static 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 | |
| 84 | static 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 | |
| 100 | void 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 | |
| 109 | int main(int /* argc */, char* /* argv */ []) { |
Steven Moreland | f9cf33b | 2017-05-18 13:58:54 -0700 | [diff] [blame] | 110 | setenv("TREBLE_TESTING_OVERRIDE", "true", true); |
| 111 | |
Steven Moreland | 7b680eb | 2017-04-19 16:34:26 -0700 | [diff] [blame] | 112 | EACH_SERVER(forkServer); |
| 113 | |
Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 114 | forkServer<IBaz>("dyingBaz"); |
Hridya Valsaraju | 179379a | 2017-02-09 16:38:12 -0800 | [diff] [blame] | 115 | |
| 116 | signal(SIGTERM, signal_handler); |
| 117 | // Parent process should not exit before the forked child processes. |
| 118 | pause(); |
| 119 | |
| 120 | return 0; |
| 121 | } |