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 <unistd.h> |
| 23 | |
| 24 | #include <android-base/unique_fd.h> |
| 25 | #include <utils/Errors.h> |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 26 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 27 | #include "Privacy.h" |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 28 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace incidentd { |
| 32 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 33 | using namespace android::base; |
| 34 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 35 | /** |
| 36 | * Looks up Privacy of a section in the auto-gen PRIVACY_POLICY_LIST; |
| 37 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 38 | const Privacy* get_privacy_of_section(int id); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 39 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 40 | /** |
| 41 | * This class wraps android::base::Pipe. |
| 42 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 43 | class Fpipe { |
| 44 | public: |
| 45 | Fpipe(); |
| 46 | ~Fpipe(); |
| 47 | |
| 48 | bool init(); |
| 49 | bool close(); |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 50 | unique_fd& readFd(); |
| 51 | unique_fd& writeFd(); |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 54 | unique_fd mRead; |
| 55 | unique_fd mWrite; |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Forks and exec a command with two pipes, one connects stdin for input, |
| 60 | * one connects stdout for output. It returns the pid of the child. |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 61 | * Input pipe can be NULL to indicate child process doesn't read stdin. |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 62 | */ |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 63 | pid_t fork_execute_cmd(char* const argv[], Fpipe* input, Fpipe* output); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Grabs varargs from stack and stores them in heap with NULL-terminated array. |
| 67 | */ |
| 68 | const char** varargs(const char* first, va_list rest); |
| 69 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 70 | /** |
| 71 | * Returns the current monotonic clock time in nanoseconds. |
| 72 | */ |
| 73 | uint64_t Nanotime(); |
| 74 | |
Yi Jin | c36e91d | 2018-03-08 11:25:58 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Methods to wait or kill child process, return exit status code. |
| 77 | */ |
| 78 | status_t kill_child(pid_t pid); |
| 79 | status_t wait_child(pid_t pid); |
| 80 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 81 | status_t start_detached_thread(const function<void ()>& func); |
| 82 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 83 | } // namespace incidentd |
| 84 | } // namespace os |
| 85 | } // namespace android |
| 86 | |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 87 | #endif // INCIDENTD_UTIL_H |