Yi Jin | b592e3b | 2018-02-01 15:17:04 -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 | */ |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 16 | #define DEBUG false |
| 17 | #include "Log.h" |
| 18 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 19 | #include "incidentd_util.h" |
| 20 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 21 | #include <sys/prctl.h> |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 22 | #include <wait.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 23 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 24 | #include "section_list.h" |
| 25 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace os { |
| 28 | namespace incidentd { |
| 29 | |
| 30 | using namespace android::base; |
| 31 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 32 | const Privacy* get_privacy_of_section(int id) { |
| 33 | int l = 0; |
| 34 | int r = PRIVACY_POLICY_COUNT - 1; |
| 35 | while (l <= r) { |
| 36 | int mid = (l + r) >> 1; |
| 37 | const Privacy* p = PRIVACY_POLICY_LIST[mid]; |
| 38 | |
| 39 | if (p->field_id < (uint32_t)id) { |
| 40 | l = mid + 1; |
| 41 | } else if (p->field_id > (uint32_t)id) { |
| 42 | r = mid - 1; |
| 43 | } else { |
| 44 | return p; |
| 45 | } |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | // ================================================================================ |
| 51 | Fpipe::Fpipe() : mRead(), mWrite() {} |
| 52 | |
| 53 | Fpipe::~Fpipe() { close(); } |
| 54 | |
| 55 | bool Fpipe::close() { |
| 56 | mRead.reset(); |
| 57 | mWrite.reset(); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | bool Fpipe::init() { return Pipe(&mRead, &mWrite); } |
| 62 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 63 | unique_fd& Fpipe::readFd() { return mRead; } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 64 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 65 | unique_fd& Fpipe::writeFd() { return mWrite; } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 66 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 67 | pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 68 | // fork used in multithreaded environment, avoid adding unnecessary code in child process |
| 69 | pid_t pid = fork(); |
| 70 | if (pid == 0) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 71 | VLOG("[In child]cmd %s", argv[0]); |
| 72 | if (input != NULL && (TEMP_FAILURE_RETRY(dup2(input->readFd().get(), STDIN_FILENO)) < 0 || |
| 73 | !input->close())) { |
| 74 | ALOGW("Failed to dup2 stdin."); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 75 | _exit(EXIT_FAILURE); |
| 76 | } |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 77 | if (TEMP_FAILURE_RETRY(dup2(output->writeFd().get(), STDOUT_FILENO)) < 0 || |
| 78 | !output->close()) { |
| 79 | ALOGW("Failed to dup2 stdout."); |
| 80 | _exit(EXIT_FAILURE); |
| 81 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 82 | /* make sure the child dies when incidentd dies */ |
| 83 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 84 | execvp(argv[0], argv); |
| 85 | _exit(errno); // always exits with failure if any |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 86 | } |
| 87 | // close the fds used in child process. |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 88 | if (input != NULL) input->readFd().reset(); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 89 | output->writeFd().reset(); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 90 | return pid; |
| 91 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 92 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 93 | // ================================================================================ |
| 94 | const char** varargs(const char* first, va_list rest) { |
| 95 | va_list copied_rest; |
| 96 | int numOfArgs = 1; // first is already count. |
| 97 | |
| 98 | va_copy(copied_rest, rest); |
| 99 | while (va_arg(copied_rest, const char*) != NULL) { |
| 100 | numOfArgs++; |
| 101 | } |
| 102 | va_end(copied_rest); |
| 103 | |
| 104 | // allocate extra 1 for NULL terminator |
| 105 | const char** ret = (const char**)malloc(sizeof(const char*) * (numOfArgs + 1)); |
| 106 | ret[0] = first; |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 107 | for (int i = 1; i < numOfArgs; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 108 | const char* arg = va_arg(rest, const char*); |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 109 | ret[i] = arg; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 110 | } |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 111 | ret[numOfArgs] = NULL; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 112 | return ret; |
| 113 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 114 | |
| 115 | // ================================================================================ |
| 116 | const uint64_t NANOS_PER_SEC = 1000000000; |
| 117 | uint64_t Nanotime() { |
| 118 | timespec ts; |
| 119 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 120 | return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec); |
| 121 | } |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 122 | |
| 123 | // ================================================================================ |
| 124 | const int WAIT_MAX = 5; |
| 125 | const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000}; |
| 126 | |
| 127 | static status_t statusCode(int status) { |
| 128 | if (WIFSIGNALED(status)) { |
| 129 | VLOG("return by signal: %s", strerror(WTERMSIG(status))); |
| 130 | return -WTERMSIG(status); |
| 131 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 132 | VLOG("return by exit: %s", strerror(WEXITSTATUS(status))); |
| 133 | return -WEXITSTATUS(status); |
| 134 | } |
| 135 | return NO_ERROR; |
| 136 | } |
| 137 | |
| 138 | status_t kill_child(pid_t pid) { |
| 139 | int status; |
| 140 | VLOG("try to kill child process %d", pid); |
| 141 | kill(pid, SIGKILL); |
| 142 | if (waitpid(pid, &status, 0) == -1) return -1; |
| 143 | return statusCode(status); |
| 144 | } |
| 145 | |
| 146 | status_t wait_child(pid_t pid) { |
| 147 | int status; |
| 148 | bool died = false; |
| 149 | // wait for child to report status up to 1 seconds |
| 150 | for (int loop = 0; !died && loop < WAIT_MAX; loop++) { |
| 151 | if (waitpid(pid, &status, WNOHANG) == pid) died = true; |
| 152 | // sleep for 0.2 second |
| 153 | nanosleep(&WAIT_INTERVAL_NS, NULL); |
| 154 | } |
| 155 | if (!died) return kill_child(pid); |
| 156 | return statusCode(status); |
| 157 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 158 | |
| 159 | } // namespace incidentd |
| 160 | } // namespace os |
| 161 | } // namespace android |