blob: c869c7a8d1d4cafbb9c6acb6343bd173ad6e9c01 [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
56int Fpipe::readFd() const { return mRead.get(); }
57
Yi Jin1a11fa12018-02-22 16:44:10 -080058int Fpipe::writeFd() const { return mWrite.get(); }
59
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) {
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 Adamseadd1232018-02-05 16:45:13 -080083
Yi Jin1a11fa12018-02-22 16:44:10 -080084// ================================================================================
85const 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 Jin06ebd1a2018-02-28 11:25:58 -080098 for (int i = 1; i < numOfArgs; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -080099 const char* arg = va_arg(rest, const char*);
Yi Jin06ebd1a2018-02-28 11:25:58 -0800100 ret[i] = arg;
Yi Jin1a11fa12018-02-22 16:44:10 -0800101 }
Yi Jin06ebd1a2018-02-28 11:25:58 -0800102 ret[numOfArgs] = NULL;
Yi Jin1a11fa12018-02-22 16:44:10 -0800103 return ret;
104}
Kweku Adamseadd1232018-02-05 16:45:13 -0800105
106// ================================================================================
107const uint64_t NANOS_PER_SEC = 1000000000;
108uint64_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}