Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 16 | #pragma once |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 17 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 18 | #ifndef INCIDENTD_UTIL_H |
| 19 | #define INCIDENTD_UTIL_H |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 20 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 21 | #include <stdarg.h> |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 22 | #include <utils/Errors.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 23 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 24 | #include "Privacy.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 25 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 26 | namespace android { |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace util { |
| 29 | class EncodedBuffer; |
| 30 | } |
| 31 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 32 | namespace os { |
| 33 | namespace incidentd { |
| 34 | |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 35 | using android::base::unique_fd; |
| 36 | using android::util::EncodedBuffer; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 37 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 38 | /** |
| 39 | * Looks up Privacy of a section in the auto-gen PRIVACY_POLICY_LIST; |
| 40 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 41 | const Privacy* get_privacy_of_section(int id); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 42 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 43 | /** |
Mike Ma | 892ccd9 | 2020-03-20 16:30:37 -0700 | [diff] [blame] | 44 | * Get an EncodedBuffer from an internal pool, or create and return a new one if the pool is empty. |
| 45 | * The EncodedBuffer should be returned after use. |
| 46 | * Thread safe. |
| 47 | */ |
| 48 | sp<EncodedBuffer> get_buffer_from_pool(); |
| 49 | |
| 50 | /** |
| 51 | * Return the EncodedBuffer back to the pool for reuse. |
| 52 | * Thread safe. |
| 53 | */ |
| 54 | void return_buffer_to_pool(sp<EncodedBuffer> buffer); |
| 55 | |
| 56 | /** |
| 57 | * Clear the buffer pool to free memory, after taking an incident report. |
| 58 | * Thread safe. |
| 59 | */ |
| 60 | void clear_buffer_pool(); |
| 61 | |
| 62 | /** |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 63 | * This class wraps android::base::Pipe. |
| 64 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 65 | class Fpipe { |
| 66 | public: |
| 67 | Fpipe(); |
| 68 | ~Fpipe(); |
| 69 | |
| 70 | bool init(); |
| 71 | bool close(); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 72 | unique_fd& readFd(); |
| 73 | unique_fd& writeFd(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 74 | |
| 75 | private: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 76 | unique_fd mRead; |
| 77 | unique_fd mWrite; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 80 | /** |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 81 | * Forks and exec a command with two pipes and returns the pid of the child, or -1 when it fails. |
| 82 | * |
| 83 | * input connects stdin for input. output connects stdout for output. input can be nullptr to |
| 84 | * indicate that child process doesn't read stdin. This function will close in and out fds upon |
| 85 | * success. If status is not NULL, the status information will be stored in the int to which it |
| 86 | * points. |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 87 | */ |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 88 | pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output, int* status = nullptr); |
| 89 | |
| 90 | /** |
| 91 | * Forks and exec a command that reads from in fd and writes to out fd and returns the pid of the |
| 92 | * child, or -1 when it fails. |
| 93 | * |
| 94 | * in can be -1 to indicate that child process doesn't read stdin. This function will close in and |
| 95 | * out fds upon success. If status is not NULL, the status information will be stored in the int |
| 96 | * to which it points. |
| 97 | */ |
| 98 | pid_t fork_execute_cmd(char* const argv[], int in, int out, int* status = nullptr); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Grabs varargs from stack and stores them in heap with NULL-terminated array. |
| 102 | */ |
| 103 | const char** varargs(const char* first, va_list rest); |
| 104 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 105 | /** |
| 106 | * Returns the current monotonic clock time in nanoseconds. |
| 107 | */ |
| 108 | uint64_t Nanotime(); |
| 109 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 110 | /** |
| 111 | * Methods to wait or kill child process, return exit status code. |
| 112 | */ |
| 113 | status_t kill_child(pid_t pid); |
Mike Ma | b6f7c47 | 2020-03-03 17:58:35 -0800 | [diff] [blame] | 114 | status_t wait_child(pid_t pid, int timeout_ms = 1000); |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 115 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame] | 116 | status_t start_detached_thread(const function<void ()>& func); |
| 117 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 118 | } // namespace incidentd |
| 119 | } // namespace os |
| 120 | } // namespace android |
| 121 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 122 | #endif // INCIDENTD_UTIL_H |