blob: a54993fed42d5c504e2bd1a61250c008a46dd730 [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 Jin6cacbcb2018-03-30 14:04:52 -070029namespace android {
30namespace os {
31namespace incidentd {
32
Yi Jinb592e3b2018-02-01 15:17:04 -080033using namespace android::base;
34
Yi Jin1a11fa12018-02-22 16:44:10 -080035/**
36 * Looks up Privacy of a section in the auto-gen PRIVACY_POLICY_LIST;
37 */
Yi Jinb592e3b2018-02-01 15:17:04 -080038const Privacy* get_privacy_of_section(int id);
Yi Jin99c248f2017-08-25 18:11:58 -070039
Yi Jin1a11fa12018-02-22 16:44:10 -080040/**
41 * This class wraps android::base::Pipe.
42 */
Yi Jin99c248f2017-08-25 18:11:58 -070043class Fpipe {
44public:
45 Fpipe();
46 ~Fpipe();
47
48 bool init();
49 bool close();
Yi Jin6355d2f2018-03-14 15:18:02 -070050 unique_fd& readFd();
51 unique_fd& writeFd();
Yi Jin99c248f2017-08-25 18:11:58 -070052
53private:
Yi Jinb592e3b2018-02-01 15:17:04 -080054 unique_fd mRead;
55 unique_fd mWrite;
Yi Jin99c248f2017-08-25 18:11:58 -070056};
57
Yi Jin1a11fa12018-02-22 16:44:10 -080058/**
Mike Mab6f7c472020-03-03 17:58:35 -080059 * Forks and exec a command with two pipes and returns the pid of the child, or -1 when it fails.
60 *
61 * input connects stdin for input. output connects stdout for output. input can be nullptr to
62 * indicate that child process doesn't read stdin. This function will close in and out fds upon
63 * success. If status is not NULL, the status information will be stored in the int to which it
64 * points.
Yi Jin1a11fa12018-02-22 16:44:10 -080065 */
Mike Mab6f7c472020-03-03 17:58:35 -080066pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output, int* status = nullptr);
67
68/**
69 * Forks and exec a command that reads from in fd and writes to out fd and returns the pid of the
70 * child, or -1 when it fails.
71 *
72 * in can be -1 to indicate that child process doesn't read stdin. This function will close in and
73 * out fds upon success. If status is not NULL, the status information will be stored in the int
74 * to which it points.
75 */
76pid_t fork_execute_cmd(char* const argv[], int in, int out, int* status = nullptr);
Yi Jin1a11fa12018-02-22 16:44:10 -080077
78/**
79 * Grabs varargs from stack and stores them in heap with NULL-terminated array.
80 */
81const char** varargs(const char* first, va_list rest);
82
Kweku Adamseadd1232018-02-05 16:45:13 -080083/**
84 * Returns the current monotonic clock time in nanoseconds.
85 */
86uint64_t Nanotime();
87
Yi Jinc36e91d2018-03-08 11:25:58 -080088/**
89 * Methods to wait or kill child process, return exit status code.
90 */
91status_t kill_child(pid_t pid);
Mike Mab6f7c472020-03-03 17:58:35 -080092status_t wait_child(pid_t pid, int timeout_ms = 1000);
Yi Jinc36e91d2018-03-08 11:25:58 -080093
Joe Onorato99598ee2019-02-11 15:55:13 +000094status_t start_detached_thread(const function<void ()>& func);
95
Yi Jin6cacbcb2018-03-30 14:04:52 -070096} // namespace incidentd
97} // namespace os
98} // namespace android
99
Kweku Adamseadd1232018-02-05 16:45:13 -0800100#endif // INCIDENTD_UTIL_H