1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_EXEC_UTILS_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_EXEC_UTILS_H_
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include <time.h>
21*795d594fSAndroid Build Coastguard Worker
22*795d594fSAndroid Build Coastguard Worker #include <cstdint>
23*795d594fSAndroid Build Coastguard Worker #include <functional>
24*795d594fSAndroid Build Coastguard Worker #include <optional>
25*795d594fSAndroid Build Coastguard Worker #include <string>
26*795d594fSAndroid Build Coastguard Worker #include <vector>
27*795d594fSAndroid Build Coastguard Worker
28*795d594fSAndroid Build Coastguard Worker #include "android-base/unique_fd.h"
29*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
32*795d594fSAndroid Build Coastguard Worker
33*795d594fSAndroid Build Coastguard Worker struct ProcessStat {
34*795d594fSAndroid Build Coastguard Worker // The total wall time, in milliseconds, that the process spent, or 0 if failed to get the value.
35*795d594fSAndroid Build Coastguard Worker int64_t wall_time_ms = 0;
36*795d594fSAndroid Build Coastguard Worker // The total CPU time, in milliseconds, that the process and any waited-for children spent, or 0
37*795d594fSAndroid Build Coastguard Worker // if failed to get the value.
38*795d594fSAndroid Build Coastguard Worker int64_t cpu_time_ms = 0;
39*795d594fSAndroid Build Coastguard Worker };
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Worker struct ExecCallbacks {
42*795d594fSAndroid Build Coastguard Worker // Called in the parent process as soon as the child process is forked.
43*795d594fSAndroid Build Coastguard Worker std::function<void(pid_t pid)> on_start = [](pid_t) {};
44*795d594fSAndroid Build Coastguard Worker // Called in the parent process after the child process exits while still in a waitable state, no
45*795d594fSAndroid Build Coastguard Worker // matter the child process succeeds or not.
46*795d594fSAndroid Build Coastguard Worker std::function<void(pid_t pid)> on_end = [](pid_t) {};
47*795d594fSAndroid Build Coastguard Worker };
48*795d594fSAndroid Build Coastguard Worker
49*795d594fSAndroid Build Coastguard Worker struct ExecResult {
50*795d594fSAndroid Build Coastguard Worker // This struct needs to be in sync with the ExecResultStatus enum contained within the
51*795d594fSAndroid Build Coastguard Worker // OdrefreshReported atom in frameworks/proto_logging/stats/enums/art/common_enums.proto.
52*795d594fSAndroid Build Coastguard Worker enum Status {
53*795d594fSAndroid Build Coastguard Worker // Unable to get the status.
54*795d594fSAndroid Build Coastguard Worker kUnknown = 0,
55*795d594fSAndroid Build Coastguard Worker // Process exited normally with an exit code.
56*795d594fSAndroid Build Coastguard Worker kExited = 1,
57*795d594fSAndroid Build Coastguard Worker // Process terminated by a signal.
58*795d594fSAndroid Build Coastguard Worker kSignaled = 2,
59*795d594fSAndroid Build Coastguard Worker // Process timed out and killed.
60*795d594fSAndroid Build Coastguard Worker kTimedOut = 3,
61*795d594fSAndroid Build Coastguard Worker // Failed to start the process.
62*795d594fSAndroid Build Coastguard Worker kStartFailed = 4,
63*795d594fSAndroid Build Coastguard Worker kLast = kStartFailed
64*795d594fSAndroid Build Coastguard Worker };
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker Status status = kUnknown;
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker // The process exit code, if `status` is `kExited`, or -1.
69*795d594fSAndroid Build Coastguard Worker int exit_code = -1;
70*795d594fSAndroid Build Coastguard Worker
71*795d594fSAndroid Build Coastguard Worker // The signal that terminated the process, if `status` is `kSignaled`, or 0.
72*795d594fSAndroid Build Coastguard Worker int signal = 0;
73*795d594fSAndroid Build Coastguard Worker };
74*795d594fSAndroid Build Coastguard Worker
75*795d594fSAndroid Build Coastguard Worker // Wrapper on fork/execv to run a command in a subprocess.
76*795d594fSAndroid Build Coastguard Worker // These spawn child processes using the environment as it was set when the single instance
77*795d594fSAndroid Build Coastguard Worker // of the runtime (Runtime::Current()) was started. If no instance of the runtime was started, it
78*795d594fSAndroid Build Coastguard Worker // will use the current environment settings.
79*795d594fSAndroid Build Coastguard Worker class EXPORT ExecUtils {
80*795d594fSAndroid Build Coastguard Worker public:
81*795d594fSAndroid Build Coastguard Worker virtual ~ExecUtils() = default;
82*795d594fSAndroid Build Coastguard Worker
83*795d594fSAndroid Build Coastguard Worker virtual bool Exec(const std::vector<std::string>& arg_vector,
84*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) const;
85*795d594fSAndroid Build Coastguard Worker
86*795d594fSAndroid Build Coastguard Worker virtual int ExecAndReturnCode(const std::vector<std::string>& arg_vector,
87*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) const;
88*795d594fSAndroid Build Coastguard Worker
89*795d594fSAndroid Build Coastguard Worker // Executes the command specified in `arg_vector` in a subprocess with a timeout.
90*795d594fSAndroid Build Coastguard Worker // If `timeout_sec` is negative, blocks until the subprocess exits.
91*795d594fSAndroid Build Coastguard Worker // Returns a structured result. If the status is not `kExited`, also returns a non-empty
92*795d594fSAndroid Build Coastguard Worker // `error_msg`.
93*795d594fSAndroid Build Coastguard Worker virtual ExecResult ExecAndReturnResult(const std::vector<std::string>& arg_vector,
94*795d594fSAndroid Build Coastguard Worker int timeout_sec,
95*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) const;
96*795d594fSAndroid Build Coastguard Worker
97*795d594fSAndroid Build Coastguard Worker // Same as above, but also collects stat of the process, calls callbacks, and supports creating a
98*795d594fSAndroid Build Coastguard Worker // new process group. The stat is collected no matter the child process succeeds or not.
99*795d594fSAndroid Build Coastguard Worker //
100*795d594fSAndroid Build Coastguard Worker // Programs used by artd (odrefresh, dex2oat, profman) should NOT set `new_process_group` to true
101*795d594fSAndroid Build Coastguard Worker // when spawning child processes because artd needs to kill an entire process subtree by killing
102*795d594fSAndroid Build Coastguard Worker // the process group.
103*795d594fSAndroid Build Coastguard Worker virtual ExecResult ExecAndReturnResult(const std::vector<std::string>& arg_vector,
104*795d594fSAndroid Build Coastguard Worker int timeout_sec,
105*795d594fSAndroid Build Coastguard Worker const ExecCallbacks& callbacks,
106*795d594fSAndroid Build Coastguard Worker bool new_process_group,
107*795d594fSAndroid Build Coastguard Worker /*out*/ ProcessStat* stat,
108*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) const;
109*795d594fSAndroid Build Coastguard Worker
110*795d594fSAndroid Build Coastguard Worker protected:
111*795d594fSAndroid Build Coastguard Worker virtual android::base::unique_fd PidfdOpen(pid_t pid) const;
112*795d594fSAndroid Build Coastguard Worker
113*795d594fSAndroid Build Coastguard Worker // Returns the content of `/proc/<pid>/stat`, or an empty string if failed.
114*795d594fSAndroid Build Coastguard Worker virtual std::string GetProcStat(pid_t pid) const;
115*795d594fSAndroid Build Coastguard Worker
116*795d594fSAndroid Build Coastguard Worker virtual std::optional<int64_t> GetUptimeMs(std::string* error_msg) const;
117*795d594fSAndroid Build Coastguard Worker
118*795d594fSAndroid Build Coastguard Worker virtual int64_t GetTicksPerSec() const;
119*795d594fSAndroid Build Coastguard Worker
120*795d594fSAndroid Build Coastguard Worker private:
121*795d594fSAndroid Build Coastguard Worker bool GetStat(pid_t pid,
122*795d594fSAndroid Build Coastguard Worker int64_t start_time,
123*795d594fSAndroid Build Coastguard Worker /*out*/ ProcessStat* stat,
124*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) const;
125*795d594fSAndroid Build Coastguard Worker };
126*795d594fSAndroid Build Coastguard Worker
Exec(const std::vector<std::string> & arg_vector,std::string * error_msg)127*795d594fSAndroid Build Coastguard Worker inline bool Exec(const std::vector<std::string>& arg_vector, /*out*/ std::string* error_msg) {
128*795d594fSAndroid Build Coastguard Worker return ExecUtils().Exec(arg_vector, error_msg);
129*795d594fSAndroid Build Coastguard Worker }
130*795d594fSAndroid Build Coastguard Worker
ExecAndReturnCode(const std::vector<std::string> & arg_vector,std::string * error_msg)131*795d594fSAndroid Build Coastguard Worker inline int ExecAndReturnCode(const std::vector<std::string>& arg_vector,
132*795d594fSAndroid Build Coastguard Worker /*out*/ std::string* error_msg) {
133*795d594fSAndroid Build Coastguard Worker return ExecUtils().ExecAndReturnCode(arg_vector, error_msg);
134*795d594fSAndroid Build Coastguard Worker }
135*795d594fSAndroid Build Coastguard Worker
136*795d594fSAndroid Build Coastguard Worker } // namespace art
137*795d594fSAndroid Build Coastguard Worker
138*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_EXEC_UTILS_H_
139