blob: d69ffbfc98ef4a8b5ee2c8cc8926faf3fec15b38 [file] [log] [blame]
Felipe Lemebda15a02016-11-16 17:48:25 -08001/*
Felipe Leme6f674ae2016-11-18 17:10:33 -08002 * Copyright (C) 2016 The Android Open Source Project
Felipe Lemebda15a02016-11-16 17:48:25 -08003 *
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 */
Felipe Leme47e9be22016-12-21 15:37:07 -080016#ifndef ANDROID_OS_DUMPSTATE_UTIL_H_
17#define ANDROID_OS_DUMPSTATE_UTIL_H_
Felipe Lemebda15a02016-11-16 17:48:25 -080018
Felipe Lemef0292972016-11-22 13:57:05 -080019#include <cstdint>
20#include <string>
21
Vishnu Nair6921f802017-11-22 09:17:23 -080022/*
23 * Converts seconds to milliseconds.
24 */
25#define SEC_TO_MSEC(second) (second * 1000)
26
27/*
28 * Converts milliseconds to seconds.
29 */
30#define MSEC_TO_SEC(millisecond) (millisecond / 1000)
31
Felipe Leme47e9be22016-12-21 15:37:07 -080032namespace android {
33namespace os {
34namespace dumpstate {
Felipe Leme6f674ae2016-11-18 17:10:33 -080035
Felipe Lemebda15a02016-11-16 17:48:25 -080036/*
Felipe Leme46b85da2016-11-21 17:40:45 -080037 * Defines the Linux account that should be executing a command.
Felipe Lemebda15a02016-11-16 17:48:25 -080038 */
Felipe Leme46b85da2016-11-21 17:40:45 -080039enum PrivilegeMode {
Felipe Lemebda15a02016-11-16 17:48:25 -080040 /* Explicitly change the `uid` and `gid` to be `shell`.*/
41 DROP_ROOT,
42 /* Don't change the `uid` and `gid`. */
43 DONT_DROP_ROOT,
44 /* Prefix the command with `/PATH/TO/su root`. Won't work non user builds. */
45 SU_ROOT
46};
47
48/*
Felipe Leme46b85da2016-11-21 17:40:45 -080049 * Defines what should happen with the main output stream (`stdout` or fd) of a command.
Felipe Lemebda15a02016-11-16 17:48:25 -080050 */
Felipe Leme46b85da2016-11-21 17:40:45 -080051enum OutputMode {
52 /* Don't change main output. */
53 NORMAL_OUTPUT,
54 /* Redirect main output to `stderr`. */
Felipe Lemebda15a02016-11-16 17:48:25 -080055 REDIRECT_TO_STDERR
56};
57
58/*
59 * Value object used to set command options.
60 *
61 * Typically constructed using a builder with chained setters. Examples:
62 *
63 * CommandOptions::WithTimeout(20).AsRoot().Build();
64 * CommandOptions::WithTimeout(10).Always().RedirectStderr().Build();
65 *
66 * Although the builder could be used to dynamically set values. Example:
67 *
68 * CommandOptions::CommandOptionsBuilder options =
69 * CommandOptions::WithTimeout(10);
70 * if (!is_user_build()) {
71 * options.AsRoot();
72 * }
73 * RunCommand("command", {"args"}, options.Build());
74 */
75class CommandOptions {
76 private:
77 class CommandOptionsValues {
78 private:
Vishnu Nair6921f802017-11-22 09:17:23 -080079 CommandOptionsValues(int64_t timeout_ms);
Felipe Lemebda15a02016-11-16 17:48:25 -080080
Vishnu Nair6921f802017-11-22 09:17:23 -080081 int64_t timeout_ms_;
Felipe Lemebda15a02016-11-16 17:48:25 -080082 bool always_;
Felipe Leme46b85da2016-11-21 17:40:45 -080083 PrivilegeMode account_mode_;
84 OutputMode output_mode_;
Felipe Lemebda15a02016-11-16 17:48:25 -080085 std::string logging_message_;
86
87 friend class CommandOptions;
88 friend class CommandOptionsBuilder;
89 };
90
91 CommandOptions(const CommandOptionsValues& values);
92
93 const CommandOptionsValues values;
94
95 public:
96 class CommandOptionsBuilder {
97 public:
98 /* Sets the command to always run, even on `dry-run` mode. */
99 CommandOptionsBuilder& Always();
Nandana Dutt4b392be2018-11-02 16:17:05 +0000100 /*
101 * Sets the command's PrivilegeMode as `SU_ROOT` unless overridden by system property
102 * 'dumpstate.unroot'.
103 */
Felipe Lemebda15a02016-11-16 17:48:25 -0800104 CommandOptionsBuilder& AsRoot();
Nandana Dutt4b392be2018-11-02 16:17:05 +0000105 /*
106 * Runs AsRoot() on userdebug builds. No-op on user builds since 'su' is
107 * not available. This is used for commands that return some useful information even
108 * when run as shell.
109 */
Yifan Hong48e83a12017-10-03 14:10:07 -0700110 CommandOptionsBuilder& AsRootIfAvailable();
Felipe Leme46b85da2016-11-21 17:40:45 -0800111 /* Sets the command's PrivilegeMode as `DROP_ROOT` */
Felipe Lemebda15a02016-11-16 17:48:25 -0800112 CommandOptionsBuilder& DropRoot();
Felipe Leme46b85da2016-11-21 17:40:45 -0800113 /* Sets the command's OutputMode as `REDIRECT_TO_STDERR` */
Felipe Lemebda15a02016-11-16 17:48:25 -0800114 CommandOptionsBuilder& RedirectStderr();
115 /* When not empty, logs a message before executing the command.
116 * Must contain a `%s`, which will be replaced by the full command line, and end on `\n`. */
117 CommandOptionsBuilder& Log(const std::string& message);
118 /* Builds the command options. */
119 CommandOptions Build();
120
121 private:
Vishnu Nair6921f802017-11-22 09:17:23 -0800122 CommandOptionsBuilder(int64_t timeout_ms);
Felipe Lemebda15a02016-11-16 17:48:25 -0800123 CommandOptionsValues values;
124 friend class CommandOptions;
125 };
126
Vishnu Nair6921f802017-11-22 09:17:23 -0800127 /** Gets the command timeout in seconds. */
Felipe Lemebda15a02016-11-16 17:48:25 -0800128 int64_t Timeout() const;
Vishnu Nair6921f802017-11-22 09:17:23 -0800129 /** Gets the command timeout in milliseconds. */
130 int64_t TimeoutInMs() const;
Felipe Lemebda15a02016-11-16 17:48:25 -0800131 /* Checks whether the command should always be run, even on dry-run mode. */
132 bool Always() const;
Felipe Leme46b85da2016-11-21 17:40:45 -0800133 /** Gets the PrivilegeMode of the command. */
134 PrivilegeMode PrivilegeMode() const;
135 /** Gets the OutputMode of the command. */
136 OutputMode OutputMode() const;
Felipe Lemebda15a02016-11-16 17:48:25 -0800137 /** Gets the logging message header, it any. */
138 std::string LoggingMessage() const;
139
Vishnu Nair6921f802017-11-22 09:17:23 -0800140 /** Creates a builder with the requied timeout in seconds. */
141 static CommandOptionsBuilder WithTimeout(int64_t timeout_sec);
142
143 /** Creates a builder with the requied timeout in milliseconds. */
144 static CommandOptionsBuilder WithTimeoutInMs(int64_t timeout_ms);
Felipe Lemebda15a02016-11-16 17:48:25 -0800145
146 // Common options.
147 static CommandOptions DEFAULT;
Felipe Lemef0292972016-11-22 13:57:05 -0800148 static CommandOptions AS_ROOT;
Felipe Lemef0292972016-11-22 13:57:05 -0800149};
150
151/*
152 * System properties helper.
153 */
154class PropertiesHelper {
155 friend class DumpstateBaseTest;
156
157 public:
158 /*
159 * Gets whether device is running a `user` build.
160 */
161 static bool IsUserBuild();
162
163 /*
164 * When running in dry-run mode, skips the real dumps and just print the section headers.
165 *
166 * Useful when debugging dumpstate or other bugreport-related activities.
167 *
168 * Dry-run mode is enabled by setting the system property `dumpstate.dry_run` to true.
169 */
170 static bool IsDryRun();
171
Nandana Dutt4b392be2018-11-02 16:17:05 +0000172 /**
173 * Checks whether root availability should be overridden.
174 *
175 * Useful to verify how dumpstate would work in a device with an user build.
176 */
177 static bool IsUnroot();
178
Felipe Lemef0292972016-11-22 13:57:05 -0800179 private:
180 static std::string build_type_;
181 static int dry_run_;
Nandana Dutt4b392be2018-11-02 16:17:05 +0000182 static int unroot_;
Felipe Lemebda15a02016-11-16 17:48:25 -0800183};
184
185/*
186 * Forks a command, waits for it to finish, and returns its status.
187 *
188 * |fd| file descriptor that receives the command's 'stdout'.
Felipe Lemef0292972016-11-22 13:57:05 -0800189 * |title| description of the command printed on `stdout` (or empty to skip
190 * description).
Felipe Lemebda15a02016-11-16 17:48:25 -0800191 * |full_command| array containing the command (first entry) and its arguments.
Felipe Leme46b85da2016-11-21 17:40:45 -0800192 * Must contain at least one element.
Felipe Lemebda15a02016-11-16 17:48:25 -0800193 * |options| optional argument defining the command's behavior.
Felipe Lemebda15a02016-11-16 17:48:25 -0800194 */
Felipe Lemef0292972016-11-22 13:57:05 -0800195int RunCommandToFd(int fd, const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme46b85da2016-11-21 17:40:45 -0800196 const CommandOptions& options = CommandOptions::DEFAULT);
Felipe Lemebda15a02016-11-16 17:48:25 -0800197
198/*
199 * Dumps the contents of a file into a file descriptor.
200 *
201 * |fd| file descriptor where the file is dumped into.
Felipe Lemef0292972016-11-22 13:57:05 -0800202 * |title| description of the command printed on `stdout` (or empty to skip
203 * description).
Felipe Lemebda15a02016-11-16 17:48:25 -0800204 * |path| location of the file to be dumped.
205 */
Felipe Lemef0292972016-11-22 13:57:05 -0800206int DumpFileToFd(int fd, const std::string& title, const std::string& path);
Felipe Lemebda15a02016-11-16 17:48:25 -0800207
Ecco Park61ffcf72016-10-27 15:46:26 -0700208/*
209 * Finds the process id by process name.
210 * |ps_name| the process name we want to search for
211 */
212int GetPidByName(const std::string& ps_name);
213
Felipe Leme47e9be22016-12-21 15:37:07 -0800214} // namespace dumpstate
215} // namespace os
216} // namespace android
217
218#endif // ANDROID_OS_DUMPSTATE_UTIL_H_