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 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 22 | #include <sys/prctl.h> |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 23 | #include <wait.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 24 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 25 | #include "section_list.h" |
| 26 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 27 | namespace android { |
| 28 | namespace os { |
| 29 | namespace incidentd { |
| 30 | |
| 31 | using namespace android::base; |
| 32 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 33 | const Privacy* get_privacy_of_section(int id) { |
| 34 | int l = 0; |
| 35 | int r = PRIVACY_POLICY_COUNT - 1; |
| 36 | while (l <= r) { |
| 37 | int mid = (l + r) >> 1; |
| 38 | const Privacy* p = PRIVACY_POLICY_LIST[mid]; |
| 39 | |
| 40 | if (p->field_id < (uint32_t)id) { |
| 41 | l = mid + 1; |
| 42 | } else if (p->field_id > (uint32_t)id) { |
| 43 | r = mid - 1; |
| 44 | } else { |
| 45 | return p; |
| 46 | } |
| 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | // ================================================================================ |
| 52 | Fpipe::Fpipe() : mRead(), mWrite() {} |
| 53 | |
| 54 | Fpipe::~Fpipe() { close(); } |
| 55 | |
| 56 | bool Fpipe::close() { |
| 57 | mRead.reset(); |
| 58 | mWrite.reset(); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool Fpipe::init() { return Pipe(&mRead, &mWrite); } |
| 63 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 64 | unique_fd& Fpipe::readFd() { return mRead; } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 65 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 66 | unique_fd& Fpipe::writeFd() { return mWrite; } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 67 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 68 | pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output, int* status) { |
| 69 | int in = -1; |
| 70 | if (input != nullptr) { |
| 71 | in = input->readFd().release(); |
| 72 | // Auto close write end of the input pipe on exec to prevent leaking fd in child process |
| 73 | fcntl(input->writeFd().get(), F_SETFD, FD_CLOEXEC); |
| 74 | } |
| 75 | int out = output->writeFd().release(); |
| 76 | // Auto close read end of the output pipe on exec |
| 77 | fcntl(output->readFd().get(), F_SETFD, FD_CLOEXEC); |
| 78 | return fork_execute_cmd(argv, in, out, status); |
| 79 | } |
| 80 | |
| 81 | pid_t fork_execute_cmd(char* const argv[], int in, int out, int* status) { |
| 82 | int dummy_status = 0; |
| 83 | if (status == nullptr) { |
| 84 | status = &dummy_status; |
| 85 | } |
| 86 | *status = 0; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 87 | pid_t pid = fork(); |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 88 | if (pid < 0) { |
| 89 | *status = -errno; |
| 90 | return -1; |
| 91 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 92 | if (pid == 0) { |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 93 | // In child |
| 94 | if (in >= 0 && (TEMP_FAILURE_RETRY(dup2(in, STDIN_FILENO)) < 0 || close(in))) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 95 | ALOGW("Failed to dup2 stdin."); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 96 | _exit(EXIT_FAILURE); |
| 97 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 98 | if (TEMP_FAILURE_RETRY(dup2(out, STDOUT_FILENO)) < 0 || close(out)) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 99 | ALOGW("Failed to dup2 stdout."); |
| 100 | _exit(EXIT_FAILURE); |
| 101 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 102 | // Make sure the child dies when incidentd dies |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 103 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 104 | execvp(argv[0], argv); |
| 105 | _exit(errno); // always exits with failure if any |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 106 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 107 | // In parent |
| 108 | if ((in >= 0 && close(in) < 0) || close(out) < 0) { |
| 109 | ALOGW("Failed to close pd. Killing child process"); |
| 110 | *status = -errno; |
| 111 | kill_child(pid); |
| 112 | return -1; |
| 113 | } |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 114 | return pid; |
| 115 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 116 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 117 | // ================================================================================ |
| 118 | const char** varargs(const char* first, va_list rest) { |
| 119 | va_list copied_rest; |
| 120 | int numOfArgs = 1; // first is already count. |
| 121 | |
| 122 | va_copy(copied_rest, rest); |
| 123 | while (va_arg(copied_rest, const char*) != NULL) { |
| 124 | numOfArgs++; |
| 125 | } |
| 126 | va_end(copied_rest); |
| 127 | |
| 128 | // allocate extra 1 for NULL terminator |
| 129 | const char** ret = (const char**)malloc(sizeof(const char*) * (numOfArgs + 1)); |
| 130 | ret[0] = first; |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 131 | for (int i = 1; i < numOfArgs; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 132 | const char* arg = va_arg(rest, const char*); |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 133 | ret[i] = arg; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 134 | } |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 135 | ret[numOfArgs] = NULL; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 136 | return ret; |
| 137 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 138 | |
| 139 | // ================================================================================ |
| 140 | const uint64_t NANOS_PER_SEC = 1000000000; |
| 141 | uint64_t Nanotime() { |
| 142 | timespec ts; |
| 143 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 144 | return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec); |
| 145 | } |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 146 | |
| 147 | // ================================================================================ |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 148 | static status_t statusCode(int status) { |
| 149 | if (WIFSIGNALED(status)) { |
| 150 | VLOG("return by signal: %s", strerror(WTERMSIG(status))); |
| 151 | return -WTERMSIG(status); |
| 152 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
| 153 | VLOG("return by exit: %s", strerror(WEXITSTATUS(status))); |
| 154 | return -WEXITSTATUS(status); |
| 155 | } |
| 156 | return NO_ERROR; |
| 157 | } |
| 158 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 159 | static bool waitpid_with_timeout(pid_t pid, int timeout_ms, int* status) { |
| 160 | sigset_t child_mask, old_mask; |
| 161 | sigemptyset(&child_mask); |
| 162 | sigaddset(&child_mask, SIGCHLD); |
| 163 | |
| 164 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 165 | ALOGW("sigprocmask failed: %s", strerror(errno)); |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | timespec ts; |
| 170 | ts.tv_sec = timeout_ms / 1000; |
| 171 | ts.tv_nsec = (timeout_ms % 1000) * 1000000; |
| 172 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts)); |
| 173 | int saved_errno = errno; |
| 174 | |
| 175 | // Set the signals back the way they were. |
| 176 | if (sigprocmask(SIG_SETMASK, &old_mask, nullptr) == -1) { |
| 177 | ALOGW("sigprocmask failed: %s", strerror(errno)); |
| 178 | if (ret == 0) { |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 | if (ret == -1) { |
| 183 | errno = saved_errno; |
| 184 | if (errno == EAGAIN) { |
| 185 | errno = ETIMEDOUT; |
| 186 | } else { |
| 187 | ALOGW("sigtimedwait failed: %s", strerror(errno)); |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 193 | if (child_pid == pid) { |
| 194 | return true; |
| 195 | } |
| 196 | if (child_pid == -1) { |
| 197 | ALOGW("waitpid failed: %s", strerror(errno)); |
| 198 | } else { |
| 199 | ALOGW("Waiting for pid %d, got pid %d instead", pid, child_pid); |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 204 | status_t kill_child(pid_t pid) { |
| 205 | int status; |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 206 | kill(pid, SIGKILL); |
| 207 | if (waitpid(pid, &status, 0) == -1) return -1; |
| 208 | return statusCode(status); |
| 209 | } |
| 210 | |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 211 | status_t wait_child(pid_t pid, int timeout_ms) { |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 212 | int status; |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 213 | if (waitpid_with_timeout(pid, timeout_ms, &status)) { |
| 214 | return statusCode(status); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 215 | } |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 216 | return kill_child(pid); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 217 | } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 218 | |
| 219 | } // namespace incidentd |
| 220 | } // namespace os |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 221 | } // namespace android |