Roland Levillain | 94b4180 | 2019-01-18 11:56:50 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ** Copyright 2019, 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 | #include "otapreopt_utils.h" |
| 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <android-base/strings.h> |
| 26 | |
| 27 | using android::base::Join; |
| 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | namespace android { |
| 31 | namespace installd { |
| 32 | |
| 33 | bool Exec(const std::vector<std::string>& arg_vector, std::string* error_msg) { |
| 34 | const std::string command_line = Join(arg_vector, ' '); |
| 35 | |
| 36 | CHECK_GE(arg_vector.size(), 1U) << command_line; |
| 37 | |
| 38 | // Convert the args to char pointers. |
| 39 | const char* program = arg_vector[0].c_str(); |
| 40 | std::vector<char*> args; |
| 41 | for (size_t i = 0; i < arg_vector.size(); ++i) { |
| 42 | const std::string& arg = arg_vector[i]; |
| 43 | char* arg_str = const_cast<char*>(arg.c_str()); |
| 44 | CHECK(arg_str != nullptr) << i; |
| 45 | args.push_back(arg_str); |
| 46 | } |
| 47 | args.push_back(nullptr); |
| 48 | |
| 49 | // Fork and exec. |
| 50 | pid_t pid = fork(); |
| 51 | if (pid == 0) { |
| 52 | // No allocation allowed between fork and exec. |
| 53 | |
| 54 | // Change process groups, so we don't get reaped by ProcessManager. |
| 55 | setpgid(0, 0); |
| 56 | |
| 57 | execv(program, &args[0]); |
| 58 | |
| 59 | PLOG(ERROR) << "Failed to execv(" << command_line << ")"; |
| 60 | // _exit to avoid atexit handlers in child. |
| 61 | _exit(1); |
| 62 | } else { |
| 63 | if (pid == -1) { |
| 64 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", |
| 65 | command_line.c_str(), strerror(errno)); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // wait for subprocess to finish |
| 70 | int status; |
| 71 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 72 | if (got_pid != pid) { |
| 73 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " |
| 74 | "wanted %d, got %d: %s", |
| 75 | command_line.c_str(), pid, got_pid, strerror(errno)); |
| 76 | return false; |
| 77 | } |
| 78 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 79 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", |
| 80 | command_line.c_str()); |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | } // namespace installd |
| 88 | } // namespace android |