Files
ostest_wukong/shell_command/src/wukong_main.cpp
T
caixincen 999590347a wukong二进制文件瘦身
Signed-off-by: caixincen <caixincen1@h-partners.com>
2026-05-13 10:22:12 +08:00

174 lines
5.3 KiB
C++

/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
#include <sys/stat.h>
#include "app_manager.h"
#include "component_manager.h"
#include "exception_manager.h"
#include "multimode_manager.h"
#include "report.h"
#include "scene_delegate.h"
#include "string_ex.h"
#include "tree_manager.h"
#include "wukong_define.h"
#include "wukong_logger.h"
#include "wukong_shell_command.h"
#include "wukong_util.h"
#include "parameters.h"
#include "hisysevent.h"
#include "directory_ex.h"
using namespace OHOS::WuKong;
static bool FreeSingtion()
{
AppManager::DestroyInstance();
ComponentManager::DestroyInstance();
ExceptionManager::DestroyInstance();
MultimodeManager::DestroyInstance();
Report::DestroyInstance();
SceneDelegate::DestroyInstance();
TreeManager::DestroyInstance();
WuKongUtil::DestroyInstance();
return true;
}
static void WuKongMutexFile()
{
int fileExist = access("/dev/shm", F_OK);
if (fileExist == 0) {
DEBUG_LOG("File exist. Now create wukong test mutex.");
} else {
const int wuKongGm = mkdir("/dev/shm", 0777);
DEBUG_LOG("File create. Now create wukong test mutex.");
if (wuKongGm == -1) {
DEBUG_LOG("Error creating directory!");
}
}
}
static void InitSemaphore(NamedSemaphore& sem, const int count)
{
bool res = sem.Open();
if (!res) {
WuKongMutexFile();
res = sem.Create();
}
if (res) {
DEBUG_LOG("Open Semaphore success");
int value = sem.GetValue();
if (value > count) {
DEBUG_LOG_STR("the semaphore value is invalid (%d), and reopen Semaphore", value);
res = sem.Create();
if (!res) {
ERROR_LOG("create sem failed");
return;
}
} else {
DEBUG_LOG_STR("Semaphore Value: (%d)", value);
}
}
sem.Close();
}
static bool IsRunning(NamedSemaphore& sem)
{
bool result = false;
sem.Open();
const int bufferSize = 32;
int value = sem.GetValue();
TRACK_LOG_STR("Semaphore Is Open: (%d)", value);
if (value <= 0) {
FILE* fp = nullptr;
fp = popen("pidof wukong", "r");
TRACK_LOG("Run pidof wukong");
if (fp == nullptr) {
ERROR_LOG("popen function failed");
return true;
}
char pid[bufferSize] = {0};
if (fgets(pid, bufferSize - 1, fp) != nullptr) {
std::string pidStr(pid);
pidStr = OHOS::ReplaceStr(pidStr, "\n", " ");
TRACK_LOG_STR("Wukong Pid: (%s)", pidStr.c_str());
std::vector<std::string> strs;
OHOS::SplitStr(pidStr, " ", strs);
for (auto i : strs) {
DEBUG_LOG_STR("Pid: (%s)", i.c_str());
}
if (strs.size() >= NUMBER_TWO) {
result = true;
} else {
sem.Create();
result = false;
}
} else {
result = true;
}
pclose(fp);
}
return result;
}
int main(int argc, char *argv[])
{
if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
std::cout << "Not a development mode state, please check device mode." << std::endl;
return 0;
}
const uint64_t dataPathSize = OHOS::GetFolderSize(DATA_PATH);
const uint64_t wukongPathSize = OHOS::GetFolderSize(WUKONG_PATH);
HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT,
"USER_DATA_SIZE", OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "COMPONENT_NAME",
"wukong", "PARTITION_NAME", DATA_PATH, "REMAIN_PARTITION_SIZE", dataPathSize,
"FILE_OR_FOLDER_PATH", WUKONG_PATH, "FILE_OR_FOLDER_SIZE", wukongPathSize);
std::shared_ptr<WuKongLogger> WuKonglogger = WuKongLogger::GetInstance();
// first start logger
WuKonglogger->SetLevel(LOG_LEVEL_INFO);
for (int index = argc - 1; index >= 1; index--) {
std::string arg = argv[index];
if (arg == "--track") {
argv[index][0] = '\0';
WuKonglogger->SetLevel(LOG_LEVEL_TRACK);
}
if (arg == "--debug") {
argv[index][0] = '\0';
WuKonglogger->SetLevel(LOG_LEVEL_DEBUG);
}
}
if (!WuKonglogger->Start()) {
return 1;
}
NamedSemaphore semRun(SEMPHORE_RUN_NAME, 1);
InitSemaphore(semRun, 1);
NamedSemaphore semStop(SEMPHORE_STOP_NAME, 1);
InitSemaphore(semStop, 1);
WuKongShellCommand cmd(argc, argv);
if (IsRunning(semRun)) {
ERROR_LOG("error: wukong has running, allow one program run.");
} else {
semRun.Open();
semRun.Wait();
std::cout << cmd.ExecCommand();
semRun.Post();
semRun.Close();
}
FreeSingtion();
WuKonglogger->Stop();
std::cout << "exit main" << std::endl;
return 0;
}