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> |
| 22 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 23 | #include "section_list.h" |
| 24 | |
| 25 | const Privacy* get_privacy_of_section(int id) { |
| 26 | int l = 0; |
| 27 | int r = PRIVACY_POLICY_COUNT - 1; |
| 28 | while (l <= r) { |
| 29 | int mid = (l + r) >> 1; |
| 30 | const Privacy* p = PRIVACY_POLICY_LIST[mid]; |
| 31 | |
| 32 | if (p->field_id < (uint32_t)id) { |
| 33 | l = mid + 1; |
| 34 | } else if (p->field_id > (uint32_t)id) { |
| 35 | r = mid - 1; |
| 36 | } else { |
| 37 | return p; |
| 38 | } |
| 39 | } |
| 40 | return NULL; |
| 41 | } |
| 42 | |
| 43 | // ================================================================================ |
| 44 | Fpipe::Fpipe() : mRead(), mWrite() {} |
| 45 | |
| 46 | Fpipe::~Fpipe() { close(); } |
| 47 | |
| 48 | bool Fpipe::close() { |
| 49 | mRead.reset(); |
| 50 | mWrite.reset(); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool Fpipe::init() { return Pipe(&mRead, &mWrite); } |
| 55 | |
| 56 | int Fpipe::readFd() const { return mRead.get(); } |
| 57 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 58 | int Fpipe::writeFd() const { return mWrite.get(); } |
| 59 | |
| 60 | pid_t fork_execute_cmd(const char* cmd, char* const argv[], Fpipe* input, Fpipe* output) { |
| 61 | // fork used in multithreaded environment, avoid adding unnecessary code in child process |
| 62 | pid_t pid = fork(); |
| 63 | if (pid == 0) { |
| 64 | if (TEMP_FAILURE_RETRY(dup2(input->readFd(), STDIN_FILENO)) < 0 || !input->close() || |
| 65 | TEMP_FAILURE_RETRY(dup2(output->writeFd(), STDOUT_FILENO)) < 0 || !output->close()) { |
| 66 | ALOGW("Can't setup stdin and stdout for command %s", cmd); |
| 67 | _exit(EXIT_FAILURE); |
| 68 | } |
| 69 | |
| 70 | /* make sure the child dies when incidentd dies */ |
| 71 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 72 | |
| 73 | execv(cmd, argv); |
| 74 | |
| 75 | ALOGW("%s failed in the child process: %s", cmd, strerror(errno)); |
| 76 | _exit(EXIT_FAILURE); // always exits with failure if any |
| 77 | } |
| 78 | // close the fds used in child process. |
| 79 | close(input->readFd()); |
| 80 | close(output->writeFd()); |
| 81 | return pid; |
| 82 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame^] | 83 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 84 | // ================================================================================ |
| 85 | const char** varargs(const char* first, va_list rest) { |
| 86 | va_list copied_rest; |
| 87 | int numOfArgs = 1; // first is already count. |
| 88 | |
| 89 | va_copy(copied_rest, rest); |
| 90 | while (va_arg(copied_rest, const char*) != NULL) { |
| 91 | numOfArgs++; |
| 92 | } |
| 93 | va_end(copied_rest); |
| 94 | |
| 95 | // allocate extra 1 for NULL terminator |
| 96 | const char** ret = (const char**)malloc(sizeof(const char*) * (numOfArgs + 1)); |
| 97 | ret[0] = first; |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 98 | for (int i = 1; i < numOfArgs; i++) { |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 99 | const char* arg = va_arg(rest, const char*); |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 100 | ret[i] = arg; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 101 | } |
Yi Jin | 06ebd1a | 2018-02-28 11:25:58 -0800 | [diff] [blame] | 102 | ret[numOfArgs] = NULL; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 103 | return ret; |
| 104 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame^] | 105 | |
| 106 | // ================================================================================ |
| 107 | const uint64_t NANOS_PER_SEC = 1000000000; |
| 108 | uint64_t Nanotime() { |
| 109 | timespec ts; |
| 110 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 111 | return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec); |
| 112 | } |