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