1 /*
2 * Copyright (C) 2024 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 #ifndef ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
18 #define ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
19
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include <functional>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31
32 #include "android-base/logging.h"
33 #include "android-base/scopeguard.h"
34 #include "android-base/strings.h"
35 #include "base/file_utils.h"
36 #include "base/globals.h"
37 #include "base/macros.h"
38
39 namespace art {
40 namespace tools {
41
42 using ::android::base::make_scope_guard;
43 using ::android::base::ScopeGuard;
44 using ::android::base::Split;
45
GetArtBin(const std::string & name)46 [[maybe_unused]] static std::string GetArtBin(const std::string& name) {
47 CHECK(kIsTargetAndroid);
48 return ART_FORMAT("{}/bin/{}", GetArtRoot(), name);
49 }
50
GetBin(const std::string & name)51 [[maybe_unused]] static std::string GetBin(const std::string& name) {
52 for (const std::string& path : Split(getenv("PATH"), ":")) {
53 if (std::filesystem::exists(path + "/" + name)) {
54 return path + "/" + name;
55 }
56 }
57 LOG(FATAL) << ART_FORMAT("Binary '{}' not found", name);
58 UNREACHABLE();
59 }
60
61 // Executes the command. If `wait` is true, waits for the process to finish and keeps it in a
62 // waitable state; otherwise, returns immediately after fork. When the current scope exits, destroys
63 // the process.
64 [[maybe_unused]] static std::pair<pid_t, std::unique_ptr<ScopeGuard<std::function<void()>>>>
ScopedExec(std::vector<std::string> & args,bool wait)65 ScopedExec(std::vector<std::string>& args, bool wait) {
66 std::vector<char*> execv_args;
67 execv_args.reserve(args.size() + 1);
68 for (std::string& arg : args) {
69 execv_args.push_back(arg.data());
70 }
71 execv_args.push_back(nullptr);
72
73 pid_t pid = fork();
74 if (pid == 0) {
75 execv(execv_args[0], execv_args.data());
76 PLOG(FATAL) << "Failed to call execv";
77 UNREACHABLE();
78 } else if (pid > 0) {
79 if (wait) {
80 siginfo_t info;
81 CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED | WNOWAIT)), 0);
82 CHECK_EQ(info.si_code, CLD_EXITED);
83 CHECK_EQ(info.si_status, 0);
84 }
85 std::function<void()> cleanup([=] {
86 siginfo_t info;
87 if (!wait) {
88 CHECK_EQ(kill(pid, SIGKILL), 0);
89 }
90 CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED)), 0);
91 });
92 return std::make_pair(
93 pid,
94 std::make_unique<ScopeGuard<std::function<void()>>>(make_scope_guard(std::move(cleanup))));
95 } else {
96 PLOG(FATAL) << "Failed to call fork";
97 UNREACHABLE();
98 }
99 }
100
101 } // namespace tools
102 } // namespace art
103
104 #endif // ART_LIBARTTOOLS_INCLUDE_TESTING_TOOLS_TESTING_H_
105