diff --git a/device/cmds/src/main.cpp b/device/cmds/src/main.cpp index 76d8f0f1f..9a1f62e83 100644 --- a/device/cmds/src/main.cpp +++ b/device/cmds/src/main.cpp @@ -54,9 +54,6 @@ const std::string HIPROFILER_PLUGINS_NAME("hiprofiler_plugins"); const std::string NATIVE_DAEMON_NAME("native_daemon"); uint32_t g_sampleDuration = 0; -int g_hiprofilerdPid = -1; -int g_hiprofilerPluginsPid = -1; -int g_nativeDaemonPid = -1; std::condition_variable g_sessionCv; std::condition_variable g_keepSessionCv; bool g_exitProcessFlag = false; @@ -396,45 +393,16 @@ bool StartDependentProcess() constexpr int waitProcMills = 300; OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0"); std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - if (getuid() == 0) { - if (!COMMON::IsProcessExist(HIPROFILERD_NAME, g_hiprofilerdPid)) { - // need start hiprofilerd - std::vector argvVec; - argvVec.push_back(const_cast(HIPROFILERD_NAME.c_str())); - g_hiprofilerdPid = COMMON::StartProcess("/system/bin/hiprofilerd", argvVec); - // Wait for the hiprofilerd to start - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - } + OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0"); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0"); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0"); - if (!COMMON::IsProcessExist(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid)) { - // need start hiprofiler_plugins - std::vector argvVec; - argvVec.push_back(const_cast(HIPROFILER_PLUGINS_NAME.c_str())); - g_hiprofilerPluginsPid = COMMON::StartProcess("/system/bin/hiprofiler_plugins", argvVec); - // Wait for the hiprofiler_plugins add preset plugin - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - } - - if (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid)) { - // need start native_daemon - std::vector argvVec; - argvVec.push_back(const_cast(NATIVE_DAEMON_NAME.c_str())); - g_nativeDaemonPid = COMMON::StartProcess("/system/bin/native_daemon", argvVec); - // Wait for the native_daemon to start - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - } - } else { - OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0"); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0"); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0"); - - OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1"); - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "1"); - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "1"); - std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - } + OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "1"); + std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "1"); + std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "1"); + std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); std::string content = ""; GetCapabilities(content, true); @@ -446,60 +414,14 @@ bool StartDependentProcess() return true; } -bool CheckProcessExit(const std::string& processName, int& pid) -{ - int nCount = 5; // 5: try 5 times - constexpr int waitExitMills = 1000; - while (nCount > 0) { - if (COMMON::IsProcessExist(processName, pid)) { - nCount--; - std::this_thread::sleep_for(std::chrono::milliseconds(waitExitMills)); - } else { - break; - } - } - return nCount > 0; -} - void KillDependentProcess() { constexpr int waitProcMills = 300; OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0"); std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); - if (getuid() == 0) { - // if pid is equal to -1, need to get pid first. - if (g_nativeDaemonPid == -1) { - COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid); - } - if (g_hiprofilerPluginsPid == -1) { - COMMON::IsProcessExist(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid); - } - if (g_hiprofilerdPid == -1) { - COMMON::IsProcessExist(HIPROFILERD_NAME, g_hiprofilerdPid); - } - COMMON::KillProcess(g_nativeDaemonPid); - if (CheckProcessExit(NATIVE_DAEMON_NAME, g_nativeDaemonPid)) { - printf("process native_daemon exits successfully\n"); - } else { - printf("process native_daemon exits failed\n"); - } - COMMON::KillProcess(g_hiprofilerPluginsPid); - if (CheckProcessExit(HIPROFILER_PLUGINS_NAME, g_hiprofilerPluginsPid)) { - printf("process hiprofiler_plugins exits successfully\n"); - } else { - printf("process hiprofiler_plugins exits failed\n"); - } - COMMON::KillProcess(g_hiprofilerdPid); - if (CheckProcessExit(HIPROFILERD_NAME, g_hiprofilerdPid)) { - printf("process hiprofilerd exits successfully\n"); - } else { - printf("process hiprofilerd exits failed\n"); - } - } else { - OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0"); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0"); - OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0"); - } + OHOS::system::SetParameter("hiviewdfx.hiprofiler.profilerd.start", "0"); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.plugins.start", "0"); + OHOS::system::SetParameter("hiviewdfx.hiprofiler.native_memoryd.start", "0"); } bool ParseConfig(const std::string& configFile, std::string& config) diff --git a/device/plugins/ffrt_profiler/service/src/ffrt_profiler_socket_service.cpp b/device/plugins/ffrt_profiler/service/src/ffrt_profiler_socket_service.cpp index ca4a15b1a..a36853d1c 100644 --- a/device/plugins/ffrt_profiler/service/src/ffrt_profiler_socket_service.cpp +++ b/device/plugins/ffrt_profiler/service/src/ffrt_profiler_socket_service.cpp @@ -27,11 +27,7 @@ FfrtProfilerSocketService::FfrtProfilerSocketService(std::shared_ptr(a) / static_cast(b) * FULL_PERCENTAGE; } -bool IsRoot(); bool PowerOfTwo(int n); #define INDENT_ONE_LEVEL (indent + 1) diff --git a/device/plugins/native_daemon/src/hook_service.cpp b/device/plugins/native_daemon/src/hook_service.cpp index d089e8449..2e6229923 100644 --- a/device/plugins/native_daemon/src/hook_service.cpp +++ b/device/plugins/native_daemon/src/hook_service.cpp @@ -29,11 +29,7 @@ HookService::HookService(const ClientConfig& clientConfig, std::shared_ptr #include +#include "common.h" #include "dfx_ark.h" #include "dfx_extractor_utils.h" #include "dfx_symbols.h" @@ -520,7 +521,7 @@ public: bool LoadKernelSyms() { - if (!IsRoot()) { + if (COMMON::IsUserMode()) { HLOGE("only root mode can access kernel symbols"); return false; } diff --git a/device/plugins/native_daemon/src/utilities.cpp b/device/plugins/native_daemon/src/utilities.cpp index fc96c3c71..b776f27d0 100644 --- a/device/plugins/native_daemon/src/utilities.cpp +++ b/device/plugins/native_daemon/src/utilities.cpp @@ -286,16 +286,6 @@ bool WriteStringToFile(const std::string &fileName, const std::string &value) return output.good(); } -bool IsRoot() -{ -#if is_linux || is_ohos - static bool isRoot = (getuid() == 0); - return isRoot; -#else - return true; -#endif -} - bool PowerOfTwo(int n) { return n && (!(n & (n - 1))); diff --git a/device/plugins/native_daemon/test/unittest/common/native/symbols_file_test.cpp b/device/plugins/native_daemon/test/unittest/common/native/symbols_file_test.cpp index ee2a57a02..2408c862b 100644 --- a/device/plugins/native_daemon/test/unittest/common/native/symbols_file_test.cpp +++ b/device/plugins/native_daemon/test/unittest/common/native/symbols_file_test.cpp @@ -17,6 +17,7 @@ #include #include #include +#include "common.h" using namespace testing::ext; using namespace std; @@ -391,7 +392,7 @@ HWTEST_F(SymbolsFileTest, GetSymbolWithVaddr, TestSize.Level1) { auto symbols = SymbolsFile::CreateSymbolsFile(SYMBOL_KERNEL_FILE); - if ((getuid() == 0)) { + if (!COMMON::IsUserMode()) { HLOGD("in root mode"); EXPECT_EQ(symbols->LoadSymbols(), true); CheckSymbols(symbols); diff --git a/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp b/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp index e0b4c957a..e33914467 100644 --- a/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp +++ b/device/plugins/native_daemon/test/unittest/common/native/utilities_test.cpp @@ -596,20 +596,6 @@ HWTEST_F(UtilitiesTest, Percentage, TestSize.Level1) EXPECT_EQ(Percentage(99, 100), 99); } -/** - * @tc.name: IsRoot - * @tc.desc: - * @tc.type: FUNC - */ -HWTEST_F(UtilitiesTest, IsRoot, TestSize.Level1) -{ - bool isRoot = true; -#if is_linux || is_ohos - isRoot = (getuid() == 0); -#endif - EXPECT_EQ(IsRoot(), isRoot); -} - /** * @tc.name: PowerOfTwo * @tc.desc: diff --git a/device/plugins/network_profiler/service/src/network_profiler_socket_service.cpp b/device/plugins/network_profiler/service/src/network_profiler_socket_service.cpp index fa62a2b24..58c64660f 100644 --- a/device/plugins/network_profiler/service/src/network_profiler_socket_service.cpp +++ b/device/plugins/network_profiler/service/src/network_profiler_socket_service.cpp @@ -27,11 +27,7 @@ NetworkProfilerSocketService::NetworkProfilerSocketService(std::shared_ptr pipe(popen(chmodCmd.c_str(), "r"), pclose); - } else { - sock = GetControlSocket(addrname.c_str()); - CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock); - } + int sock = GetControlSocket(addrname.c_str()); + CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock); CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0, "StartServer FAIL listen ERR : %d", errno); diff --git a/device/services/plugin_service/src/plugin_service.cpp b/device/services/plugin_service/src/plugin_service.cpp index 042bf7527..24e9fdc6a 100644 --- a/device/services/plugin_service/src/plugin_service.cpp +++ b/device/services/plugin_service/src/plugin_service.cpp @@ -39,11 +39,7 @@ constexpr uint32_t STOP_BASELINE = (1U << 22); // need to stop take data size wi PluginService::PluginService() { pluginIdCounter_ = 0; - if (getuid() == 0) { - StartService(DEFAULT_UNIX_SOCKET_FULL_PATH); - } else { - StartService(DEFAULT_UNIX_SOCKET_PATH); - } + StartService(DEFAULT_UNIX_SOCKET_PATH); pluginCommandBuilder_ = std::make_shared();