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