David Sehr | 97c381e | 2017-02-01 15:09:58 -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 | #ifndef ART_RUNTIME_EXEC_UTILS_H_ |
| 18 | #define ART_RUNTIME_EXEC_UTILS_H_ |
| 19 | |
Colin Cross | 073885c | 2021-09-14 14:35:15 -0700 | [diff] [blame] | 20 | #include <time.h> |
| 21 | |
Jiakai Zhang | 4c9498e | 2022-09-09 11:58:56 +0100 | [diff] [blame] | 22 | #include <functional> |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 26 | #include "android-base/unique_fd.h" |
| 27 | |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 28 | namespace art { |
| 29 | |
Jiakai Zhang | 5494695 | 2022-09-06 21:02:09 +0100 | [diff] [blame] | 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 | |
Jiakai Zhang | 4c9498e | 2022-09-09 11:58:56 +0100 | [diff] [blame] | 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 | |
Jiakai Zhang | 1a8cc46 | 2022-09-13 14:14:16 +0100 | [diff] [blame] | 46 | struct ExecResult { |
Stefano Cianciulli | 822d8ac | 2023-04-25 08:56:54 +0000 | [diff] [blame] | 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. |
Jiakai Zhang | 1a8cc46 | 2022-09-13 14:14:16 +0100 | [diff] [blame] | 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, |
Stefano Cianciulli | 6eb814c | 2022-10-03 14:09:05 +0000 | [diff] [blame] | 60 | kLast = kStartFailed |
Jiakai Zhang | 1a8cc46 | 2022-09-13 14:14:16 +0100 | [diff] [blame] | 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 | |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 72 | // Wrapper on fork/execv to run a command in a subprocess. |
Orion Hodson | dd732cc | 2021-01-15 16:31:30 +0000 | [diff] [blame] | 73 | // These spawn child processes using the environment as it was set when the single instance |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 74 | // of the runtime (Runtime::Current()) was started. If no instance of the runtime was started, it |
| 75 | // will use the current environment settings. |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 76 | class ExecUtils { |
| 77 | public: |
| 78 | virtual ~ExecUtils() = default; |
| 79 | |
Jiakai Zhang | a9f7727 | 2022-07-07 20:36:59 +0000 | [diff] [blame] | 80 | virtual bool Exec(const std::vector<std::string>& arg_vector, |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 81 | /*out*/ std::string* error_msg) const; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 82 | |
Jiakai Zhang | a9f7727 | 2022-07-07 20:36:59 +0000 | [diff] [blame] | 83 | virtual int ExecAndReturnCode(const std::vector<std::string>& arg_vector, |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 84 | /*out*/ std::string* error_msg) const; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 85 | |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 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. |
Jiakai Zhang | 1a8cc46 | 2022-09-13 14:14:16 +0100 | [diff] [blame] | 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; |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 93 | |
Jiakai Zhang | 4c9498e | 2022-09-09 11:58:56 +0100 | [diff] [blame] | 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. |
Jiakai Zhang | 1a8cc46 | 2022-09-13 14:14:16 +0100 | [diff] [blame] | 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; |
Jiakai Zhang | 5494695 | 2022-09-06 21:02:09 +0100 | [diff] [blame] | 101 | |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 102 | protected: |
| 103 | virtual android::base::unique_fd PidfdOpen(pid_t pid) const; |
Jiakai Zhang | 5494695 | 2022-09-06 21:02:09 +0100 | [diff] [blame] | 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; |
Jiakai Zhang | ccbcfb1 | 2021-08-23 15:10:35 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Jiakai Zhang | 42adb66 | 2022-07-05 16:09:05 +0100 | [diff] [blame] | 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 | |
| 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 | |
David Sehr | 97c381e | 2017-02-01 15:09:58 -0800 | [diff] [blame] | 125 | } // namespace art |
| 126 | |
| 127 | #endif // ART_RUNTIME_EXEC_UTILS_H_ |