• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TESTING_H_
18 #define ART_LIBARTTOOLS_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 <string>
28 #include <utility>
29 #include <vector>
30 
31 #include "android-base/logging.h"
32 #include "android-base/scopeguard.h"
33 #include "base/file_utils.h"
34 #include "base/globals.h"
35 #include "base/macros.h"
36 
37 namespace art {
38 namespace tools {
39 
40 using ::android::base::make_scope_guard;
41 using ::android::base::ScopeGuard;
42 
GetArtBin(const std::string & name)43 [[maybe_unused]] static std::string GetArtBin(const std::string& name) {
44   CHECK(kIsTargetAndroid);
45   return ART_FORMAT("{}/bin/{}", GetArtRoot(), name);
46 }
47 
GetBin(const std::string & name)48 [[maybe_unused]] static std::string GetBin(const std::string& name) {
49   CHECK(kIsTargetAndroid);
50   return ART_FORMAT("{}/bin/{}", GetAndroidRoot(), name);
51 }
52 
53 // Executes the command. If the `wait` is true, waits for the process to finish and keeps it in a
54 // waitable state; otherwise, returns immediately after fork. When the current scope exits, destroys
55 // the process.
ScopedExec(std::vector<std::string> & args,bool wait)56 [[maybe_unused]] static std::pair<pid_t, ScopeGuard<std::function<void()>>> ScopedExec(
57     std::vector<std::string>& args, bool wait) {
58   std::vector<char*> execv_args;
59   execv_args.reserve(args.size() + 1);
60   for (std::string& arg : args) {
61     execv_args.push_back(arg.data());
62   }
63   execv_args.push_back(nullptr);
64 
65   pid_t pid = fork();
66   if (pid == 0) {
67     execv(execv_args[0], execv_args.data());
68     UNREACHABLE();
69   } else if (pid > 0) {
70     if (wait) {
71       siginfo_t info;
72       CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED | WNOWAIT)), 0);
73       CHECK_EQ(info.si_code, CLD_EXITED);
74       CHECK_EQ(info.si_status, 0);
75     }
76     std::function<void()> cleanup([=] {
77       siginfo_t info;
78       if (!wait) {
79         CHECK_EQ(kill(pid, SIGKILL), 0);
80       }
81       CHECK_EQ(TEMP_FAILURE_RETRY(waitid(P_PID, pid, &info, WEXITED)), 0);
82     });
83     return std::make_pair(pid, make_scope_guard(std::move(cleanup)));
84   } else {
85     LOG(FATAL) << "Failed to call fork";
86     UNREACHABLE();
87   }
88 }
89 
90 }  // namespace tools
91 }  // namespace art
92 
93 #endif  // ART_LIBARTTOOLS_TESTING_H_
94