| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2016 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 | #define LOG_TAG "dumpstate" | 
|  | 18 |  | 
|  | 19 | #include "DumpstateUtil.h" | 
|  | 20 |  | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 21 | #include <dirent.h> | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 22 | #include <fcntl.h> | 
|  | 23 | #include <sys/prctl.h> | 
|  | 24 | #include <sys/wait.h> | 
|  | 25 | #include <unistd.h> | 
|  | 26 |  | 
|  | 27 | #include <vector> | 
|  | 28 |  | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 29 | #include <android-base/file.h> | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 30 | #include <android-base/properties.h> | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> | 
|  | 32 | #include <android-base/strings.h> | 
| Narayan Kamath | 6b9516c | 2017-10-27 11:15:51 +0100 | [diff] [blame] | 33 | #include <android-base/unique_fd.h> | 
| Jiyong Park | b22e65d | 2017-06-23 21:23:16 +0900 | [diff] [blame] | 34 | #include <log/log.h> | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | #include "DumpstateInternal.h" | 
|  | 37 |  | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 38 | namespace android { | 
|  | 39 | namespace os { | 
|  | 40 | namespace dumpstate { | 
|  | 41 |  | 
|  | 42 | namespace { | 
|  | 43 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 44 | static constexpr const char* kSuPath = "/system/xbin/su"; | 
|  | 45 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 46 | static bool waitpid_with_timeout(pid_t pid, int timeout_ms, int* status) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 47 | sigset_t child_mask, old_mask; | 
|  | 48 | sigemptyset(&child_mask); | 
|  | 49 | sigaddset(&child_mask, SIGCHLD); | 
|  | 50 |  | 
|  | 51 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { | 
|  | 52 | printf("*** sigprocmask failed: %s\n", strerror(errno)); | 
|  | 53 | return false; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | timespec ts; | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 57 | ts.tv_sec = MSEC_TO_SEC(timeout_ms); | 
|  | 58 | ts.tv_nsec = (timeout_ms % 1000) * 1000000; | 
| Nandana Dutt | 263cf38 | 2018-09-26 11:22:02 +0100 | [diff] [blame] | 59 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts)); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 60 | int saved_errno = errno; | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 61 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 62 | // Set the signals back the way they were. | 
| Nandana Dutt | 263cf38 | 2018-09-26 11:22:02 +0100 | [diff] [blame] | 63 | if (sigprocmask(SIG_SETMASK, &old_mask, nullptr) == -1) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 64 | printf("*** sigprocmask failed: %s\n", strerror(errno)); | 
|  | 65 | if (ret == 0) { | 
|  | 66 | return false; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 | if (ret == -1) { | 
|  | 70 | errno = saved_errno; | 
|  | 71 | if (errno == EAGAIN) { | 
|  | 72 | errno = ETIMEDOUT; | 
|  | 73 | } else { | 
|  | 74 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); | 
|  | 75 | } | 
|  | 76 | return false; | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | pid_t child_pid = waitpid(pid, status, WNOHANG); | 
|  | 80 | if (child_pid != pid) { | 
|  | 81 | if (child_pid != -1) { | 
|  | 82 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); | 
|  | 83 | } else { | 
|  | 84 | printf("*** waitpid failed: %s\n", strerror(errno)); | 
|  | 85 | } | 
|  | 86 | return false; | 
|  | 87 | } | 
|  | 88 | return true; | 
|  | 89 | } | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 90 | }  // unnamed namespace | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 91 |  | 
|  | 92 | CommandOptions CommandOptions::DEFAULT = CommandOptions::WithTimeout(10).Build(); | 
|  | 93 | CommandOptions CommandOptions::AS_ROOT = CommandOptions::WithTimeout(10).AsRoot().Build(); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 94 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 95 | CommandOptions::CommandOptionsBuilder::CommandOptionsBuilder(int64_t timeout_ms) : values(timeout_ms) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
|  | 98 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Always() { | 
|  | 99 | values.always_ = true; | 
|  | 100 | return *this; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRoot() { | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 104 | if (!PropertiesHelper::IsUnroot()) { | 
|  | 105 | values.account_mode_ = SU_ROOT; | 
|  | 106 | } | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 107 | return *this; | 
|  | 108 | } | 
|  | 109 |  | 
| Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 110 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRootIfAvailable() { | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 111 | if (!PropertiesHelper::IsUserBuild()) { | 
|  | 112 | return AsRoot(); | 
|  | 113 | } | 
| Yifan Hong | 48e83a1 | 2017-10-03 14:10:07 -0700 | [diff] [blame] | 114 | return *this; | 
|  | 115 | } | 
|  | 116 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 117 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::DropRoot() { | 
|  | 118 | values.account_mode_ = DROP_ROOT; | 
|  | 119 | return *this; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::RedirectStderr() { | 
|  | 123 | values.output_mode_ = REDIRECT_TO_STDERR; | 
|  | 124 | return *this; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Log( | 
|  | 128 | const std::string& message) { | 
|  | 129 | values.logging_message_ = message; | 
|  | 130 | return *this; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | CommandOptions CommandOptions::CommandOptionsBuilder::Build() { | 
|  | 134 | return CommandOptions(values); | 
|  | 135 | } | 
|  | 136 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 137 | CommandOptions::CommandOptionsValues::CommandOptionsValues(int64_t timeout_ms) | 
|  | 138 | : timeout_ms_(timeout_ms), | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 139 | always_(false), | 
|  | 140 | account_mode_(DONT_DROP_ROOT), | 
|  | 141 | output_mode_(NORMAL_OUTPUT), | 
|  | 142 | logging_message_("") { | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | CommandOptions::CommandOptions(const CommandOptionsValues& values) : values(values) { | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | int64_t CommandOptions::Timeout() const { | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 149 | return MSEC_TO_SEC(values.timeout_ms_); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | int64_t CommandOptions::TimeoutInMs() const { | 
|  | 153 | return values.timeout_ms_; | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
|  | 156 | bool CommandOptions::Always() const { | 
|  | 157 | return values.always_; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | PrivilegeMode CommandOptions::PrivilegeMode() const { | 
|  | 161 | return values.account_mode_; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | OutputMode CommandOptions::OutputMode() const { | 
|  | 165 | return values.output_mode_; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | std::string CommandOptions::LoggingMessage() const { | 
|  | 169 | return values.logging_message_; | 
|  | 170 | } | 
|  | 171 |  | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 172 | CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeout(int64_t timeout_sec) { | 
|  | 173 | return CommandOptions::CommandOptionsBuilder(SEC_TO_MSEC(timeout_sec)); | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeoutInMs(int64_t timeout_ms) { | 
|  | 177 | return CommandOptions::CommandOptionsBuilder(timeout_ms); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 178 | } | 
|  | 179 |  | 
|  | 180 | std::string PropertiesHelper::build_type_ = ""; | 
|  | 181 | int PropertiesHelper::dry_run_ = -1; | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 182 | int PropertiesHelper::unroot_ = -1; | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 183 |  | 
|  | 184 | bool PropertiesHelper::IsUserBuild() { | 
|  | 185 | if (build_type_.empty()) { | 
|  | 186 | build_type_ = android::base::GetProperty("ro.build.type", "user"); | 
|  | 187 | } | 
|  | 188 | return "user" == build_type_; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | bool PropertiesHelper::IsDryRun() { | 
|  | 192 | if (dry_run_ == -1) { | 
|  | 193 | dry_run_ = android::base::GetBoolProperty("dumpstate.dry_run", false) ? 1 : 0; | 
|  | 194 | } | 
|  | 195 | return dry_run_ == 1; | 
|  | 196 | } | 
|  | 197 |  | 
| Nandana Dutt | 4b392be | 2018-11-02 16:17:05 +0000 | [diff] [blame] | 198 | bool PropertiesHelper::IsUnroot() { | 
|  | 199 | if (unroot_ == -1) { | 
|  | 200 | unroot_ = android::base::GetBoolProperty("dumpstate.unroot", false) ? 1 : 0; | 
|  | 201 | } | 
|  | 202 | return unroot_ == 1; | 
|  | 203 | } | 
|  | 204 |  | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 205 | int DumpFileToFd(int out_fd, const std::string& title, const std::string& path) { | 
| Narayan Kamath | 6b9516c | 2017-10-27 11:15:51 +0100 | [diff] [blame] | 206 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC))); | 
|  | 207 | if (fd.get() < 0) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 208 | int err = errno; | 
|  | 209 | if (title.empty()) { | 
|  | 210 | dprintf(out_fd, "*** Error dumping %s: %s\n", path.c_str(), strerror(err)); | 
|  | 211 | } else { | 
|  | 212 | dprintf(out_fd, "*** Error dumping %s (%s): %s\n", path.c_str(), title.c_str(), | 
|  | 213 | strerror(err)); | 
|  | 214 | } | 
|  | 215 | fsync(out_fd); | 
|  | 216 | return -1; | 
|  | 217 | } | 
| Narayan Kamath | 6b9516c | 2017-10-27 11:15:51 +0100 | [diff] [blame] | 218 | return DumpFileFromFdToFd(title, path, fd.get(), out_fd, PropertiesHelper::IsDryRun()); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 219 | } | 
|  | 220 |  | 
|  | 221 | int RunCommandToFd(int fd, const std::string& title, const std::vector<std::string>& full_command, | 
|  | 222 | const CommandOptions& options) { | 
|  | 223 | if (full_command.empty()) { | 
|  | 224 | MYLOGE("No arguments on RunCommandToFd(%s)\n", title.c_str()); | 
|  | 225 | return -1; | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | int size = full_command.size() + 1;  // null terminated | 
|  | 229 | int starting_index = 0; | 
|  | 230 | if (options.PrivilegeMode() == SU_ROOT) { | 
|  | 231 | starting_index = 2;  // "su" "root" | 
|  | 232 | size += starting_index; | 
|  | 233 | } | 
|  | 234 |  | 
|  | 235 | std::vector<const char*> args; | 
|  | 236 | args.resize(size); | 
|  | 237 |  | 
|  | 238 | std::string command_string; | 
|  | 239 | if (options.PrivilegeMode() == SU_ROOT) { | 
|  | 240 | args[0] = kSuPath; | 
|  | 241 | command_string += kSuPath; | 
|  | 242 | args[1] = "root"; | 
|  | 243 | command_string += " root "; | 
|  | 244 | } | 
|  | 245 | for (size_t i = 0; i < full_command.size(); i++) { | 
|  | 246 | args[i + starting_index] = full_command[i].data(); | 
|  | 247 | command_string += args[i + starting_index]; | 
|  | 248 | if (i != full_command.size() - 1) { | 
|  | 249 | command_string += " "; | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 | args[size - 1] = nullptr; | 
|  | 253 |  | 
|  | 254 | const char* command = command_string.c_str(); | 
|  | 255 |  | 
|  | 256 | if (options.PrivilegeMode() == SU_ROOT && PropertiesHelper::IsUserBuild()) { | 
|  | 257 | dprintf(fd, "Skipping '%s' on user build.\n", command); | 
|  | 258 | return 0; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | if (!title.empty()) { | 
|  | 262 | dprintf(fd, "------ %s (%s) ------\n", title.c_str(), command); | 
|  | 263 | fsync(fd); | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | const std::string& logging_message = options.LoggingMessage(); | 
|  | 267 | if (!logging_message.empty()) { | 
|  | 268 | MYLOGI(logging_message.c_str(), command_string.c_str()); | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | bool silent = (options.OutputMode() == REDIRECT_TO_STDERR); | 
|  | 272 | bool redirecting_to_fd = STDOUT_FILENO != fd; | 
|  | 273 |  | 
|  | 274 | if (PropertiesHelper::IsDryRun() && !options.Always()) { | 
|  | 275 | if (!title.empty()) { | 
|  | 276 | dprintf(fd, "\t(skipped on dry run)\n"); | 
|  | 277 | } else if (redirecting_to_fd) { | 
|  | 278 | // There is no title, but we should still print a dry-run message | 
|  | 279 | dprintf(fd, "%s: skipped on dry run\n", command_string.c_str()); | 
|  | 280 | } | 
|  | 281 | fsync(fd); | 
|  | 282 | return 0; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | const char* path = args[0]; | 
|  | 286 |  | 
|  | 287 | uint64_t start = Nanotime(); | 
|  | 288 | pid_t pid = fork(); | 
|  | 289 |  | 
|  | 290 | /* handle error case */ | 
|  | 291 | if (pid < 0) { | 
|  | 292 | if (!silent) dprintf(fd, "*** fork: %s\n", strerror(errno)); | 
|  | 293 | MYLOGE("*** fork: %s\n", strerror(errno)); | 
|  | 294 | return pid; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | /* handle child case */ | 
|  | 298 | if (pid == 0) { | 
|  | 299 | if (options.PrivilegeMode() == DROP_ROOT && !DropRootUser()) { | 
|  | 300 | if (!silent) { | 
|  | 301 | dprintf(fd, "*** failed to drop root before running %s: %s\n", command, | 
|  | 302 | strerror(errno)); | 
|  | 303 | } | 
|  | 304 | MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno)); | 
|  | 305 | return -1; | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | if (silent) { | 
|  | 309 | // Redirects stdout to stderr | 
|  | 310 | TEMP_FAILURE_RETRY(dup2(STDERR_FILENO, STDOUT_FILENO)); | 
|  | 311 | } else if (redirecting_to_fd) { | 
|  | 312 | // Redirect stdout to fd | 
|  | 313 | TEMP_FAILURE_RETRY(dup2(fd, STDOUT_FILENO)); | 
|  | 314 | close(fd); | 
|  | 315 | } | 
|  | 316 |  | 
|  | 317 | /* make sure the child dies when dumpstate dies */ | 
|  | 318 | prctl(PR_SET_PDEATHSIG, SIGKILL); | 
|  | 319 |  | 
|  | 320 | /* just ignore SIGPIPE, will go down with parent's */ | 
|  | 321 | struct sigaction sigact; | 
|  | 322 | memset(&sigact, 0, sizeof(sigact)); | 
|  | 323 | sigact.sa_handler = SIG_IGN; | 
| Nandana Dutt | 263cf38 | 2018-09-26 11:22:02 +0100 | [diff] [blame] | 324 | sigaction(SIGPIPE, &sigact, nullptr); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 325 |  | 
|  | 326 | execvp(path, (char**)args.data()); | 
|  | 327 | // execvp's result will be handled after waitpid_with_timeout() below, but | 
|  | 328 | // if it failed, it's safer to exit dumpstate. | 
|  | 329 | MYLOGD("execvp on command '%s' failed (error: %s)\n", command, strerror(errno)); | 
|  | 330 | // Must call _exit (instead of exit), otherwise it will corrupt the zip | 
|  | 331 | // file. | 
|  | 332 | _exit(EXIT_FAILURE); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | /* handle parent case */ | 
|  | 336 | int status; | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 337 | bool ret = waitpid_with_timeout(pid, options.TimeoutInMs(), &status); | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 338 | fsync(fd); | 
|  | 339 |  | 
|  | 340 | uint64_t elapsed = Nanotime() - start; | 
|  | 341 | if (!ret) { | 
|  | 342 | if (errno == ETIMEDOUT) { | 
|  | 343 | if (!silent) | 
|  | 344 | dprintf(fd, "*** command '%s' timed out after %.3fs (killing pid %d)\n", command, | 
|  | 345 | static_cast<float>(elapsed) / NANOS_PER_SEC, pid); | 
|  | 346 | MYLOGE("*** command '%s' timed out after %.3fs (killing pid %d)\n", command, | 
|  | 347 | static_cast<float>(elapsed) / NANOS_PER_SEC, pid); | 
|  | 348 | } else { | 
|  | 349 | if (!silent) | 
|  | 350 | dprintf(fd, "*** command '%s': Error after %.4fs (killing pid %d)\n", command, | 
|  | 351 | static_cast<float>(elapsed) / NANOS_PER_SEC, pid); | 
|  | 352 | MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", command, | 
|  | 353 | static_cast<float>(elapsed) / NANOS_PER_SEC, pid); | 
|  | 354 | } | 
|  | 355 | kill(pid, SIGTERM); | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 356 | if (!waitpid_with_timeout(pid, 5000, nullptr)) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 357 | kill(pid, SIGKILL); | 
| Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 358 | if (!waitpid_with_timeout(pid, 5000, nullptr)) { | 
| Felipe Leme | f029297 | 2016-11-22 13:57:05 -0800 | [diff] [blame] | 359 | if (!silent) | 
|  | 360 | dprintf(fd, "could not kill command '%s' (pid %d) even with SIGKILL.\n", | 
|  | 361 | command, pid); | 
|  | 362 | MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); | 
|  | 363 | } | 
|  | 364 | } | 
|  | 365 | return -1; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | if (WIFSIGNALED(status)) { | 
|  | 369 | if (!silent) | 
|  | 370 | dprintf(fd, "*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status)); | 
|  | 371 | MYLOGE("*** command '%s' failed: killed by signal %d\n", command, WTERMSIG(status)); | 
|  | 372 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { | 
|  | 373 | status = WEXITSTATUS(status); | 
|  | 374 | if (!silent) dprintf(fd, "*** command '%s' failed: exit code %d\n", command, status); | 
|  | 375 | MYLOGE("*** command '%s' failed: exit code %d\n", command, status); | 
|  | 376 | } | 
|  | 377 |  | 
|  | 378 | return status; | 
|  | 379 | } | 
| Ecco Park | 61ffcf7 | 2016-10-27 15:46:26 -0700 | [diff] [blame] | 380 |  | 
|  | 381 | int GetPidByName(const std::string& ps_name) { | 
|  | 382 | DIR* proc_dir; | 
|  | 383 | struct dirent* ps; | 
|  | 384 | unsigned int pid; | 
|  | 385 | std::string cmdline; | 
|  | 386 |  | 
|  | 387 | if (!(proc_dir = opendir("/proc"))) { | 
|  | 388 | MYLOGE("Can't open /proc\n"); | 
|  | 389 | return -1; | 
|  | 390 | } | 
|  | 391 |  | 
|  | 392 | while ((ps = readdir(proc_dir))) { | 
|  | 393 | if (!(pid = atoi(ps->d_name))) { | 
|  | 394 | continue; | 
|  | 395 | } | 
|  | 396 | android::base::ReadFileToString("/proc/" + std::string(ps->d_name) + "/cmdline", &cmdline); | 
|  | 397 | if (cmdline.find(ps_name) == std::string::npos) { | 
|  | 398 | continue; | 
|  | 399 | } else { | 
|  | 400 | closedir(proc_dir); | 
|  | 401 | return pid; | 
|  | 402 | } | 
|  | 403 | } | 
|  | 404 | MYLOGE("can't find the pid\n"); | 
|  | 405 | closedir(proc_dir); | 
|  | 406 | return -1; | 
|  | 407 | } | 
| Felipe Leme | 47e9be2 | 2016-12-21 15:37:07 -0800 | [diff] [blame] | 408 |  | 
|  | 409 | }  // namespace dumpstate | 
|  | 410 | }  // namespace os | 
|  | 411 | }  // namespace android |