blob: d7995133c7223c9c6649a8b8b2af731dc561b048 [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>
22
Yi Jinb592e3b2018-02-01 15:17:04 -080023#include "section_list.h"
24
25const 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// ================================================================================
44Fpipe::Fpipe() : mRead(), mWrite() {}
45
46Fpipe::~Fpipe() { close(); }
47
48bool Fpipe::close() {
49 mRead.reset();
50 mWrite.reset();
51 return true;
52}
53
54bool Fpipe::init() { return Pipe(&mRead, &mWrite); }
55
Yi Jin6355d2f2018-03-14 15:18:02 -070056unique_fd& Fpipe::readFd() { return mRead; }
Yi Jinb592e3b2018-02-01 15:17:04 -080057
Yi Jin6355d2f2018-03-14 15:18:02 -070058unique_fd& Fpipe::writeFd() { return mWrite; }
Yi Jin1a11fa12018-02-22 16:44:10 -080059
60pid_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) {
Yi Jin6355d2f2018-03-14 15:18:02 -070064 if (TEMP_FAILURE_RETRY(dup2(input->readFd().get(), STDIN_FILENO)) < 0 || !input->close() ||
65 TEMP_FAILURE_RETRY(dup2(output->writeFd().get(), STDOUT_FILENO)) < 0 ||
66 !output->close()) {
Yi Jin1a11fa12018-02-22 16:44:10 -080067 ALOGW("Can't setup stdin and stdout for command %s", cmd);
68 _exit(EXIT_FAILURE);
69 }
70
71 /* make sure the child dies when incidentd dies */
72 prctl(PR_SET_PDEATHSIG, SIGKILL);
73
74 execv(cmd, argv);
75
76 ALOGW("%s failed in the child process: %s", cmd, strerror(errno));
77 _exit(EXIT_FAILURE); // always exits with failure if any
78 }
79 // close the fds used in child process.
Yi Jin6355d2f2018-03-14 15:18:02 -070080 input->readFd().reset();
81 output->writeFd().reset();
Yi Jin1a11fa12018-02-22 16:44:10 -080082 return pid;
83}
Kweku Adamseadd1232018-02-05 16:45:13 -080084
Yi Jin1a11fa12018-02-22 16:44:10 -080085// ================================================================================
86const char** varargs(const char* first, va_list rest) {
87 va_list copied_rest;
88 int numOfArgs = 1; // first is already count.
89
90 va_copy(copied_rest, rest);
91 while (va_arg(copied_rest, const char*) != NULL) {
92 numOfArgs++;
93 }
94 va_end(copied_rest);
95
96 // allocate extra 1 for NULL terminator
97 const char** ret = (const char**)malloc(sizeof(const char*) * (numOfArgs + 1));
98 ret[0] = first;
Yi Jin06ebd1a2018-02-28 11:25:58 -080099 for (int i = 1; i < numOfArgs; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800100 const char* arg = va_arg(rest, const char*);
Yi Jin06ebd1a2018-02-28 11:25:58 -0800101 ret[i] = arg;
Yi Jin1a11fa12018-02-22 16:44:10 -0800102 }
Yi Jin06ebd1a2018-02-28 11:25:58 -0800103 ret[numOfArgs] = NULL;
Yi Jin1a11fa12018-02-22 16:44:10 -0800104 return ret;
105}
Kweku Adamseadd1232018-02-05 16:45:13 -0800106
107// ================================================================================
108const uint64_t NANOS_PER_SEC = 1000000000;
109uint64_t Nanotime() {
110 timespec ts;
111 clock_gettime(CLOCK_MONOTONIC, &ts);
112 return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
113}