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 #ifndef ART_RUNTIME_EXEC_UTILS_H_
18 #define ART_RUNTIME_EXEC_UTILS_H_
19
20 #include <time.h>
21
22 #include <functional>
23 #include <string>
24 #include <vector>
25
26 #include "android-base/unique_fd.h"
27
28 namespace art {
29
30 struct ProcessStat {
31 // The total wall time, in milliseconds, that the process spent, or 0 if failed to get the value.
32 int wall_time_ms = 0;
33 // The total CPU time, in milliseconds, that the process and any waited-for children spent, or 0
34 // if failed to get the value.
35 int cpu_time_ms = 0;
36 };
37
38 struct ExecCallbacks {
39 // Called in the parent process as soon as the child process is forked.
40 std::function<void(pid_t pid)> on_start = [](pid_t) {};
41 // Called in the parent process after the child process exits while still in a waitable state, no
42 // matter the child process succeeds or not.
43 std::function<void(pid_t pid)> on_end = [](pid_t) {};
44 };
45
46 struct ExecResult {
47 // This struct needs to be in sync with the ExecResultStatus enum contained within the
48 // OdrefreshReported atom in frameworks/proto_logging/atoms/art/odrefresh_extension_atoms.proto.
49 enum Status {
50 // Unable to get the status.
51 kUnknown = 0,
52 // Process exited normally with an exit code.
53 kExited = 1,
54 // Process terminated by a signal.
55 kSignaled = 2,
56 // Process timed out and killed.
57 kTimedOut = 3,
58 // Failed to start the process.
59 kStartFailed = 4,
60 kLast = kStartFailed
61 };
62
63 Status status = kUnknown;
64
65 // The process exit code, if `status` is `kExited`, or -1.
66 int exit_code = -1;
67
68 // The signal that terminated the process, if `status` is `kSignaled`, or 0.
69 int signal = 0;
70 };
71
72 // Wrapper on fork/execv to run a command in a subprocess.
73 // These spawn child processes using the environment as it was set when the single instance
74 // of the runtime (Runtime::Current()) was started. If no instance of the runtime was started, it
75 // will use the current environment settings.
76 class ExecUtils {
77 public:
78 virtual ~ExecUtils() = default;
79
80 virtual bool Exec(const std::vector<std::string>& arg_vector,
81 /*out*/ std::string* error_msg) const;
82
83 virtual int ExecAndReturnCode(const std::vector<std::string>& arg_vector,
84 /*out*/ std::string* error_msg) const;
85
86 // Executes the command specified in `arg_vector` in a subprocess with a timeout.
87 // If `timeout_sec` is negative, blocks until the subprocess exits.
88 // Returns a structured result. If the status is not `kExited`, also returns a non-empty
89 // `error_msg`.
90 virtual ExecResult ExecAndReturnResult(const std::vector<std::string>& arg_vector,
91 int timeout_sec,
92 /*out*/ std::string* error_msg) const;
93
94 // Same as above, but also collects stat of the process and calls callbacks. The stat is collected
95 // no matter the child process succeeds or not.
96 virtual ExecResult ExecAndReturnResult(const std::vector<std::string>& arg_vector,
97 int timeout_sec,
98 const ExecCallbacks& callbacks,
99 /*out*/ ProcessStat* stat,
100 /*out*/ std::string* error_msg) const;
101
102 protected:
103 virtual android::base::unique_fd PidfdOpen(pid_t pid) const;
104
105 // Returns the content of `/proc/<pid>/stat`, or an empty string if failed.
106 virtual std::string GetProcStat(pid_t pid) const;
107
108 virtual int64_t GetUptimeMs() const;
109
110 virtual int64_t GetTicksPerSec() const;
111
112 private:
113 bool GetStat(pid_t pid, /*out*/ ProcessStat* stat, /*out*/ std::string* error_msg) const;
114 };
115
Exec(const std::vector<std::string> & arg_vector,std::string * error_msg)116 inline bool Exec(const std::vector<std::string>& arg_vector, /*out*/ std::string* error_msg) {
117 return ExecUtils().Exec(arg_vector, error_msg);
118 }
119
ExecAndReturnCode(const std::vector<std::string> & arg_vector,std::string * error_msg)120 inline int ExecAndReturnCode(const std::vector<std::string>& arg_vector,
121 /*out*/ std::string* error_msg) {
122 return ExecUtils().ExecAndReturnCode(arg_vector, error_msg);
123 }
124
125 } // namespace art
126
127 #endif // ART_RUNTIME_EXEC_UTILS_H_
128