blob: b5f6e21a997630344fbf547d294445c43071ad10 [file] [log] [blame]
Yi Jin99c248f2017-08-25 18:11:58 -07001/*
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 Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Yi Jin99c248f2017-08-25 18:11:58 -070017
Yi Jinb592e3b2018-02-01 15:17:04 -080018#ifndef INCIDENTD_UTIL_H
19#define INCIDENTD_UTIL_H
Yi Jin99c248f2017-08-25 18:11:58 -070020
Yi Jin1a11fa12018-02-22 16:44:10 -080021#include <stdarg.h>
Yi Jinc36e91d2018-03-08 11:25:58 -080022#include <unistd.h>
23
24#include <android-base/unique_fd.h>
25#include <utils/Errors.h>
Yi Jin1a11fa12018-02-22 16:44:10 -080026
Yi Jinb592e3b2018-02-01 15:17:04 -080027#include "Privacy.h"
Yi Jin99c248f2017-08-25 18:11:58 -070028
Yi Jinc36e91d2018-03-08 11:25:58 -080029using namespace android;
Yi Jinb592e3b2018-02-01 15:17:04 -080030using namespace android::base;
31
Yi Jin1a11fa12018-02-22 16:44:10 -080032/**
33 * Looks up Privacy of a section in the auto-gen PRIVACY_POLICY_LIST;
34 */
Yi Jinb592e3b2018-02-01 15:17:04 -080035const Privacy* get_privacy_of_section(int id);
Yi Jin99c248f2017-08-25 18:11:58 -070036
Yi Jin1a11fa12018-02-22 16:44:10 -080037/**
38 * This class wraps android::base::Pipe.
39 */
Yi Jin99c248f2017-08-25 18:11:58 -070040class Fpipe {
41public:
42 Fpipe();
43 ~Fpipe();
44
45 bool init();
46 bool close();
Yi Jin6355d2f2018-03-14 15:18:02 -070047 unique_fd& readFd();
48 unique_fd& writeFd();
Yi Jin99c248f2017-08-25 18:11:58 -070049
50private:
Yi Jinb592e3b2018-02-01 15:17:04 -080051 unique_fd mRead;
52 unique_fd mWrite;
Yi Jin99c248f2017-08-25 18:11:58 -070053};
54
Yi Jin1a11fa12018-02-22 16:44:10 -080055/**
56 * Forks and exec a command with two pipes, one connects stdin for input,
57 * one connects stdout for output. It returns the pid of the child.
Yi Jinc36e91d2018-03-08 11:25:58 -080058 * Input pipe can be NULL to indicate child process doesn't read stdin.
Yi Jin1a11fa12018-02-22 16:44:10 -080059 */
Yi Jinc36e91d2018-03-08 11:25:58 -080060pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output);
Yi Jin1a11fa12018-02-22 16:44:10 -080061
62/**
63 * Grabs varargs from stack and stores them in heap with NULL-terminated array.
64 */
65const char** varargs(const char* first, va_list rest);
66
Kweku Adamseadd1232018-02-05 16:45:13 -080067/**
68 * Returns the current monotonic clock time in nanoseconds.
69 */
70uint64_t Nanotime();
71
Yi Jinc36e91d2018-03-08 11:25:58 -080072/**
73 * Methods to wait or kill child process, return exit status code.
74 */
75status_t kill_child(pid_t pid);
76status_t wait_child(pid_t pid);
77
Kweku Adamseadd1232018-02-05 16:45:13 -080078#endif // INCIDENTD_UTIL_H