1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00
This commit is contained in:
Benoit Jacob 2020-11-16 22:46:48 -05:00
parent 460c2b15d2
commit d1e226b2eb

View File

@ -825,7 +825,7 @@ static void SetupSampling( int64_t& samplingPeriod )
// for instance from https://github.com/topjohnwu/Magisk. // for instance from https://github.com/topjohnwu/Magisk.
// We have to handle both flavors of `su` commands. // We have to handle both flavors of `su` commands.
// Internal implementation helper for GetHowToRunAsRoot. // Internal implementation helper for GetRootMethod.
// //
// Checks if `su <flag> command` succeeds running the specified command as root. // Checks if `su <flag> command` succeeds running the specified command as root.
// //
@ -868,13 +868,13 @@ static bool TrySuCommandFlag(const char* flag) {
} }
// Enum identifying a method for running a command as root. // Enum identifying a method for running a command as root.
enum class HowToRunAsRoot { enum class RootMethod {
// Our process is already root (getuid()==0). Nothing else is needed. // Our process is already root (getuid()==0). Nothing else is needed.
// This scenario happens when running a command-line program // This scenario happens when running a command-line program
// via `adb shell` while adbd is running as root, that is, // via `adb shell` while adbd is running as root, that is,
// $ adb root // $ adb root
// $ adb shell /data/local/tmp/some_program // $ adb shell /data/local/tmp/some_program
AlreadyRunningAsRoot, AlreadyRoot,
// The way to run a command as root is: `su -c 'command'`. // The way to run a command as root is: `su -c 'command'`.
// In this case, `command` is interpreted by a shell (not just exec'd). // In this case, `command` is interpreted by a shell (not just exec'd).
SuDashC, SuDashC,
@ -884,23 +884,23 @@ enum class HowToRunAsRoot {
// `su root sh -c 'command'`. // `su root sh -c 'command'`.
SuRoot, SuRoot,
// We don't know how to run a command as root on this device. // We don't know how to run a command as root on this device.
DontKnow None
}; };
// Internal implementation helper for GetHowToRunAsRoot. // Internal implementation helper for GetRootMethod.
// //
// Functionally equivalent to it, but much more expensive (no caching). // Functionally equivalent to it, but much more expensive (no caching).
static HowToRunAsRoot ExpensivelyDetermineHowToRunAsRoot() { static RootMethod EvalRootMethod() {
if (getuid() == 0) { if (getuid() == 0) {
return HowToRunAsRoot::AlreadyRunningAsRoot; return RootMethod::AlreadyRoot;
} }
if (TrySuCommandFlag("-c")) { if (TrySuCommandFlag("-c")) {
return HowToRunAsRoot::SuDashC; return RootMethod::SuDashC;
} }
if (TrySuCommandFlag("root")) { if (TrySuCommandFlag("root")) {
return HowToRunAsRoot::SuRoot; return RootMethod::SuRoot;
} }
return HowToRunAsRoot::DontKnow; return RootMethod::None;
} }
// Internal implementation helper for ExeclpAsRoot and SystemAsRoot. // Internal implementation helper for ExeclpAsRoot and SystemAsRoot.
@ -908,8 +908,8 @@ static HowToRunAsRoot ExpensivelyDetermineHowToRunAsRoot() {
// Returns how to run a command as root. Determines that once, then // Returns how to run a command as root. Determines that once, then
// caches the result. Reentrant thanks to C++11 specifying the // caches the result. Reentrant thanks to C++11 specifying the
// initialization of static locals as reentrant. // initialization of static locals as reentrant.
static HowToRunAsRoot GetHowToRunAsRoot() { static RootMethod GetRootMethod() {
static const HowToRunAsRoot value = ExpensivelyDetermineHowToRunAsRoot(); static const RootMethod value = EvalRootMethod();
return value; return value;
} }
@ -919,18 +919,18 @@ static int ExeclpAsRoot( char* argv0, ... ) {
static constexpr int maxargs = 16; static constexpr int maxargs = 16;
char* args[maxargs] = { nullptr }; char* args[maxargs] = { nullptr };
int args_count = 0; int args_count = 0;
switch(GetHowToRunAsRoot()) { switch(GetRootMethod()) {
case HowToRunAsRoot::AlreadyRunningAsRoot: case RootMethod::AlreadyRoot:
break; // no need to prepend any args. break; // no need to prepend any args.
case HowToRunAsRoot::SuDashC: case RootMethod::SuDashC:
args[args_count++] = "su"; args[args_count++] = "su";
args[args_count++] = "-c"; args[args_count++] = "-c";
break; break;
case HowToRunAsRoot::SuRoot: case RootMethod::SuRoot:
args[args_count++] = "su"; args[args_count++] = "su";
args[args_count++] = "root"; args[args_count++] = "root";
break; break;
case HowToRunAsRoot::DontKnow: case RootMethod::None:
break; // just cross fingers! break; // just cross fingers!
} }
va_list l; va_list l;
@ -951,17 +951,17 @@ static int ExeclpAsRoot( char* argv0, ... ) {
// `sh -c` in the command. // `sh -c` in the command.
static int SystemAsRoot(const char* command) { static int SystemAsRoot(const char* command) {
const char* command_format = ""; const char* command_format = "";
switch(GetHowToRunAsRoot()) { switch(GetRootMethod()) {
case HowToRunAsRoot::AlreadyRunningAsRoot: case RootMethod::AlreadyRoot:
command_format = "%s"; // no need to prepend any args. command_format = "%s"; // no need to prepend any args.
break; break;
case HowToRunAsRoot::SuDashC: case RootMethod::SuDashC:
command_format = "su -c '%s'"; command_format = "su -c '%s'";
break; break;
case HowToRunAsRoot::SuRoot: case RootMethod::SuRoot:
command_format = "su root sh -c '%s'"; command_format = "su root sh -c '%s'";
break; break;
case HowToRunAsRoot::DontKnow: case RootMethod::None:
command_format = "%s"; // just cross fingers! command_format = "%s"; // just cross fingers!
break; break;
} }
@ -1049,7 +1049,7 @@ bool SysTraceStart( int64_t& samplingPeriod )
return false; return false;
#endif #endif
if (GetHowToRunAsRoot() == HowToRunAsRoot::DontKnow) { if (GetRootMethod() == RootMethod::None) {
return false; return false;
} }