blob: 7db1fa7d61df93df629610f13f5b1fb25ab4341a [file] [log] [blame]
Yi Jinb592e3b2018-02-01 15:17:04 -08001/*
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 Jin1a11fa12018-02-22 16:44:10 -080016#define DEBUG false
17#include "Log.h"
18
Yi Jinb592e3b2018-02-01 15:17:04 -080019#include "incidentd_util.h"
20
Yi Jin1a11fa12018-02-22 16:44:10 -080021#include <sys/prctl.h>
Yi Jinc36e91d2018-03-08 11:25:58 -080022#include <wait.h>
Yi Jin1a11fa12018-02-22 16:44:10 -080023
Yi Jinb592e3b2018-02-01 15:17:04 -080024#include "section_list.h"
25
26const 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// ================================================================================
45Fpipe::Fpipe() : mRead(), mWrite() {}
46
47Fpipe::~Fpipe() { close(); }
48
49bool Fpipe::close() {
50 mRead.reset();
51 mWrite.reset();
52 return true;
53}
54
55bool Fpipe::init() { return Pipe(&mRead, &mWrite); }
56
Yi Jin6355d2f2018-03-14 15:18:02 -070057unique_fd& Fpipe::readFd() { return mRead; }
Yi Jinb592e3b2018-02-01 15:17:04 -080058
Yi Jin6355d2f2018-03-14 15:18:02 -070059unique_fd& Fpipe::writeFd() { return mWrite; }
Yi Jin1a11fa12018-02-22 16:44:10 -080060
Yi Jinc36e91d2018-03-08 11:25:58 -080061pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output) {
Yi Jin1a11fa12018-02-22 16:44:10 -080062 // fork used in multithreaded environment, avoid adding unnecessary code in child process
63 pid_t pid = fork();
64 if (pid == 0) {
Yi Jinc36e91d2018-03-08 11:25:58 -080065 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 Jin1a11fa12018-02-22 16:44:10 -080069 _exit(EXIT_FAILURE);
70 }
Yi Jinc36e91d2018-03-08 11:25:58 -080071 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 Jin1a11fa12018-02-22 16:44:10 -080076 /* make sure the child dies when incidentd dies */
77 prctl(PR_SET_PDEATHSIG, SIGKILL);
Yi Jinc36e91d2018-03-08 11:25:58 -080078 execvp(argv[0], argv);
79 _exit(errno); // always exits with failure if any
Yi Jin1a11fa12018-02-22 16:44:10 -080080 }
81 // close the fds used in child process.
Yi Jinc36e91d2018-03-08 11:25:58 -080082 if (input != NULL) input->readFd().reset();
Yi Jin6355d2f2018-03-14 15:18:02 -070083 output->writeFd().reset();
Yi Jin1a11fa12018-02-22 16:44:10 -080084 return pid;
85}
Kweku Adamseadd1232018-02-05 16:45:13 -080086
Yi Jin1a11fa12018-02-22 16:44:10 -080087// ================================================================================
88const 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 Jin06ebd1a2018-02-28 11:25:58 -0800101 for (int i = 1; i < numOfArgs; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800102 const char* arg = va_arg(rest, const char*);
Yi Jin06ebd1a2018-02-28 11:25:58 -0800103 ret[i] = arg;
Yi Jin1a11fa12018-02-22 16:44:10 -0800104 }
Yi Jin06ebd1a2018-02-28 11:25:58 -0800105 ret[numOfArgs] = NULL;
Yi Jin1a11fa12018-02-22 16:44:10 -0800106 return ret;
107}
Kweku Adamseadd1232018-02-05 16:45:13 -0800108
109// ================================================================================
110const uint64_t NANOS_PER_SEC = 1000000000;
111uint64_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 Jinc36e91d2018-03-08 11:25:58 -0800116
117// ================================================================================
118const int WAIT_MAX = 5;
119const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
120
121static 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
132status_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
140status_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}