blob: af685d8adeb889e404b403e4e66dd65aa6377ad8 [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
Yi Jin6cacbcb2018-03-30 14:04:52 -070026namespace android {
27namespace os {
28namespace incidentd {
29
30using namespace android::base;
31
Yi Jinb592e3b2018-02-01 15:17:04 -080032const 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// ================================================================================
51Fpipe::Fpipe() : mRead(), mWrite() {}
52
53Fpipe::~Fpipe() { close(); }
54
55bool Fpipe::close() {
56 mRead.reset();
57 mWrite.reset();
58 return true;
59}
60
61bool Fpipe::init() { return Pipe(&mRead, &mWrite); }
62
Yi Jin6355d2f2018-03-14 15:18:02 -070063unique_fd& Fpipe::readFd() { return mRead; }
Yi Jinb592e3b2018-02-01 15:17:04 -080064
Yi Jin6355d2f2018-03-14 15:18:02 -070065unique_fd& Fpipe::writeFd() { return mWrite; }
Yi Jin1a11fa12018-02-22 16:44:10 -080066
Yi Jinc36e91d2018-03-08 11:25:58 -080067pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output) {
Yi Jin1a11fa12018-02-22 16:44:10 -080068 // fork used in multithreaded environment, avoid adding unnecessary code in child process
69 pid_t pid = fork();
70 if (pid == 0) {
Yi Jinc36e91d2018-03-08 11:25:58 -080071 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 Jin1a11fa12018-02-22 16:44:10 -080075 _exit(EXIT_FAILURE);
76 }
Yi Jinc36e91d2018-03-08 11:25:58 -080077 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 Jin1a11fa12018-02-22 16:44:10 -080082 /* make sure the child dies when incidentd dies */
83 prctl(PR_SET_PDEATHSIG, SIGKILL);
Yi Jinc36e91d2018-03-08 11:25:58 -080084 execvp(argv[0], argv);
85 _exit(errno); // always exits with failure if any
Yi Jin1a11fa12018-02-22 16:44:10 -080086 }
87 // close the fds used in child process.
Yi Jinc36e91d2018-03-08 11:25:58 -080088 if (input != NULL) input->readFd().reset();
Yi Jin6355d2f2018-03-14 15:18:02 -070089 output->writeFd().reset();
Yi Jin1a11fa12018-02-22 16:44:10 -080090 return pid;
91}
Kweku Adamseadd1232018-02-05 16:45:13 -080092
Yi Jin1a11fa12018-02-22 16:44:10 -080093// ================================================================================
94const 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 Jin06ebd1a2018-02-28 11:25:58 -0800107 for (int i = 1; i < numOfArgs; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800108 const char* arg = va_arg(rest, const char*);
Yi Jin06ebd1a2018-02-28 11:25:58 -0800109 ret[i] = arg;
Yi Jin1a11fa12018-02-22 16:44:10 -0800110 }
Yi Jin06ebd1a2018-02-28 11:25:58 -0800111 ret[numOfArgs] = NULL;
Yi Jin1a11fa12018-02-22 16:44:10 -0800112 return ret;
113}
Kweku Adamseadd1232018-02-05 16:45:13 -0800114
115// ================================================================================
116const uint64_t NANOS_PER_SEC = 1000000000;
117uint64_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 Jinc36e91d2018-03-08 11:25:58 -0800122
123// ================================================================================
124const int WAIT_MAX = 5;
125const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
126
127static 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
138status_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
146status_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 Jin6cacbcb2018-03-30 14:04:52 -0700158
159} // namespace incidentd
160} // namespace os
161} // namespace android