From df39049e19b942747bc32d68b6b1f874e0e7a104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=BE=BE?= Date: Wed, 6 Nov 2024 10:18:12 +0800 Subject: [PATCH] =?UTF-8?q?devicedebug=E4=BB=A3=E7=A0=81=E5=B7=AE=E5=BC=82?= =?UTF-8?q?=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王达 --- .../devicedebug/kill/src/devicedebug_kill.c | 36 +++++++++++-------- standard/appspawn_service.c | 18 +++++----- .../app_spawn_service_test.cpp | 15 ++++---- test/unittest/devicedebug_test/BUILD.gn | 2 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/service/devicedebug/kill/src/devicedebug_kill.c b/service/devicedebug/kill/src/devicedebug_kill.c index 5bb0a1b7..f36c177f 100644 --- a/service/devicedebug/kill/src/devicedebug_kill.c +++ b/service/devicedebug/kill/src/devicedebug_kill.c @@ -43,6 +43,7 @@ APPSPAWN_STATIC char* DeviceDebugJsonStringGeneral(int pid, const char *op, cJSO { cJSON *root = cJSON_CreateObject(); if (root == NULL) { + cJSON_Delete(args); DEVICEDEBUG_LOGE("devicedebug json write create root object unsuccess"); return NULL; } @@ -58,20 +59,6 @@ APPSPAWN_STATIC char* DeviceDebugJsonStringGeneral(int pid, const char *op, cJSO APPSPAWN_STATIC int DeviceDebugKill(int pid, int signal) { - AppSpawnClientHandle clientHandle; - int ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); - if (ret != 0) { - DEVICEDEBUG_LOGE("devicedebug appspawn client init unsuccess, ret=%{public}d", ret); - return ret; - } - - AppSpawnReqMsgHandle reqHandle; - ret = AppSpawnReqMsgCreate(MSG_DEVICE_DEBUG, "devicedebug", &reqHandle); - if (ret != 0) { - DEVICEDEBUG_LOGE("devicedebug appspawn message create unsuccess, ret=%{public}d", ret); - return ret; - } - cJSON *args = cJSON_CreateObject(); if (args == NULL) { DEVICEDEBUG_LOGE("devicedebug json write create args object unsuccess"); @@ -80,20 +67,39 @@ APPSPAWN_STATIC int DeviceDebugKill(int pid, int signal) cJSON_AddNumberToObject(args, "signal", signal); char *jsonString = DeviceDebugJsonStringGeneral(pid, "kill", args); if (jsonString == NULL) { - cJSON_Delete(args); return DEVICEDEBUG_ERRNO_JSON_CREATED_FAILED; } + AppSpawnReqMsgHandle reqHandle; + int ret = AppSpawnReqMsgCreate(MSG_DEVICE_DEBUG, "devicedebug", &reqHandle); + if (ret != 0) { + free(jsonString); + DEVICEDEBUG_LOGE("devicedebug appspawn message create unsuccess, ret=%{public}d", ret); + return ret; + } + ret = AppSpawnReqMsgAddExtInfo(reqHandle, "devicedebug", (uint8_t *)jsonString, strlen(jsonString) + 1); if (ret != 0) { DEVICEDEBUG_LOGE("devicedebug appspawn message add devicedebug[%{public}s] unsuccess, ret=%{public}d", jsonString, ret); + free(jsonString); + AppSpawnReqMsgFree(reqHandle); + return ret; + } + + AppSpawnClientHandle clientHandle; + ret = AppSpawnClientInit(APPSPAWN_SERVER_NAME, &clientHandle); + if (ret != 0) { + free(jsonString); + AppSpawnReqMsgFree(reqHandle); + DEVICEDEBUG_LOGE("devicedebug appspawn client init unsuccess, ret=%{public}d", ret); return ret; } AppSpawnResult result = {0}; ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result); AppSpawnClientDestroy(clientHandle); + free(jsonString); if (ret != 0) { DEVICEDEBUG_LOGE("devicedebug appspawn send msg unsuccess, ret=%{public}d", ret); return ret; diff --git a/standard/appspawn_service.c b/standard/appspawn_service.c index 2848096c..81a2f106 100644 --- a/standard/appspawn_service.c +++ b/standard/appspawn_service.c @@ -458,7 +458,6 @@ static void OnReceiveRequest(const TaskHandle taskHandle, const uint8_t *buffer, APPSPAWN_CHECK(ret == 0, LE_CloseStreamTask(LE_GetDefaultLoop(), taskHandle); return, "Failed to create time for connection"); } - return; } static char *GetMapMem(uint32_t clientId, const char *processName, uint32_t size, bool readOnly, bool isNweb) @@ -928,7 +927,7 @@ static void WaitChildTimeout(const TimerHandle taskHandle, void *context) DeleteAppSpawningCtx(property); } -static int ProcessChildFdCheck(int fd, AppSpawningCtx *property, int *pResult) +static int ProcessChildFdCheck(int fd, AppSpawningCtx *property) { int result = 0; (void)read(fd, &result, sizeof(result)); @@ -941,7 +940,6 @@ static int ProcessChildFdCheck(int fd, AppSpawningCtx *property, int *pResult) DeleteAppSpawningCtx(property); return -1; } - *pResult = result; return 0; } @@ -952,13 +950,12 @@ static void ProcessChildResponse(const WatcherHandle taskHandle, int fd, uint32_ property->forkCtx.watcherHandle = NULL; // delete watcher LE_RemoveWatcher(LE_GetDefaultLoop(), (WatcherHandle)taskHandle); - int result = 0; - if (ProcessChildFdCheck(fd, property, &result) != 0) { + if (ProcessChildFdCheck(fd, property) != 0) { return; } // success - bool isDebuggable = CheckAppMsgFlagsSet(property, APP_FLAGS_DEBUGGABLE) == 1 ? true : false; + bool isDebuggable = CheckAppMsgFlagsSet(property, APP_FLAGS_DEBUGGABLE); AppSpawnedProcess *appInfo = AddSpawnedProcess(property->pid, GetBundleName(property), isDebuggable); if (appInfo) { AppSpawnMsgDacInfo *dacInfo = GetAppProperty(property, TLV_DAC_INFO); @@ -977,7 +974,7 @@ static void ProcessChildResponse(const WatcherHandle taskHandle, int fd, uint32_ ProcessMgrHookExecute(STAGE_SERVER_APP_ADD, GetAppSpawnContent(), appInfo); // response AppSpawnHookExecute(STAGE_PARENT_PRE_RELY, 0, GetAppSpawnContent(), &property->client); - SendResponse(property->message->connection, &property->message->msgHeader, result, property->pid); + SendResponse(property->message->connection, &property->message->msgHeader, 0, property->pid); AppSpawnHookExecute(STAGE_PARENT_POST_RELY, 0, GetAppSpawnContent(), &property->client); #ifdef DEBUG_BEGETCTL_BOOT if (IsDeveloperModeOpen()) { @@ -1538,22 +1535,27 @@ APPSPAWN_STATIC int ProcessAppSpawnDeviceDebugMsg(AppSpawnMsgNode *message) cJSON *app = cJSON_GetObjectItem(json, "app"); if (!cJSON_IsNumber(app)) { APPSPAWN_LOGE("appspawn devicedebug json get app fail"); + cJSON_Delete(json); return -1; } cJSON *op = cJSON_GetObjectItem(json, "op"); if (!cJSON_IsString(op) || op->valuestring == NULL) { APPSPAWN_LOGE("appspawn devicedebug json get op fail"); + cJSON_Delete(json); return -1; } cJSON *args = cJSON_GetObjectItem(json, "args"); if (!cJSON_IsObject(args)) { APPSPAWN_LOGE("appspawn devicedebug json get args fail"); + cJSON_Delete(json); return -1; } - return AppspawnDevicedebugDeal(op->valuestring, app->valueint, args); + int result = AppspawnDevicedebugDeal(op->valuestring, app->valueint, args); + cJSON_Delete(json); + return result; } static void ProcessRecvMsg(AppSpawnConnection *connection, AppSpawnMsgNode *message) diff --git a/test/unittest/app_spawn_standard_test/app_spawn_service_test.cpp b/test/unittest/app_spawn_standard_test/app_spawn_service_test.cpp index 910ed625..31b0d807 100644 --- a/test/unittest/app_spawn_standard_test/app_spawn_service_test.cpp +++ b/test/unittest/app_spawn_standard_test/app_spawn_service_test.cpp @@ -567,20 +567,20 @@ HWTEST_F(AppSpawnServiceTest, App_Spawn_Msg_009, TestSize.Level0) APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret); AppSpawnedProcess *app = GetSpawnedProcessByName(testServer->GetDefaultTestAppBundleName()); - EXPECT_NE(app, nullptr); + ASSERT_NE(app, nullptr); AppSpawnReqMsgHandle reqHandle2; ret = AppSpawnReqMsgCreate(MSG_DEVICE_DEBUG, "devicedebug", &reqHandle2); - EXPECT_GT(sprintf_s(pid, 16, "%d", app->pid), 0); + ASSERT_GT(sprintf_s(pid, 16, "%d", app->pid), 0); AppSpawnReqMsgAddStringInfo(reqHandle2, "signal", "-9"); AppSpawnReqMsgAddStringInfo(reqHandle2, "pid", pid); ret = AppSpawnClientSendMsg(clientHandle, reqHandle2, &result); + AppSpawnClientDestroy(clientHandle); APPSPAWN_CHECK(ret == 0 && result.result == 0, break, "Failed to send msg ret:%{public}d, result:%{public}d", ret, result.result); ASSERT_EQ(kill(app->pid, SIGKILL), 0); } while (0); - AppSpawnClientDestroy(clientHandle); ASSERT_EQ(ret, 0); ASSERT_EQ(result.result, -1); } @@ -604,15 +604,15 @@ HWTEST_F(AppSpawnServiceTest, App_Spawn_Msg_010, TestSize.Level0) APPSPAWN_CHECK(ret == 0, break, "Failed to send msg %{public}d", ret); AppSpawnedProcess *app = GetSpawnedProcessByName(testServer->GetDefaultTestAppBundleName()); - EXPECT_NE(app, nullptr); + ASSERT_NE(app, nullptr); AppSpawnReqMsgHandle reqHandle2; ret = AppSpawnReqMsgCreate(MSG_DEVICE_DEBUG, "devicedebug", &reqHandle2); cJSON *args = cJSON_CreateObject(); - EXPECT_NE(args, nullptr); + ASSERT_NE(args, nullptr); cJSON_AddNumberToObject(args, "signal", 9); cJSON *root = cJSON_CreateObject(); - EXPECT_NE(root, nullptr); + ASSERT_NE(root, nullptr); cJSON_AddNumberToObject(root, "app", app->pid); cJSON_AddStringToObject(root, "op", "kill"); cJSON_AddItemToObject(root, "args", args); @@ -621,11 +621,12 @@ HWTEST_F(AppSpawnServiceTest, App_Spawn_Msg_010, TestSize.Level0) ret = AppSpawnReqMsgAddExtInfo(reqHandle2, "devicedebug", (uint8_t *)jsonString, strlen(jsonString) + 1); ASSERT_EQ(ret, 0); ret = AppSpawnClientSendMsg(clientHandle, reqHandle2, &result); + AppSpawnClientDestroy(clientHandle); + free(jsonString); APPSPAWN_CHECK(ret == 0 && result.result == 0, break, "Failed to send msg ret:%{public}d, result:%{public}d", ret, result.result); } while (0); - AppSpawnClientDestroy(clientHandle); ASSERT_EQ(ret, 0); ASSERT_EQ(result.result, 0); } diff --git a/test/unittest/devicedebug_test/BUILD.gn b/test/unittest/devicedebug_test/BUILD.gn index b075d6c8..f50f29fa 100644 --- a/test/unittest/devicedebug_test/BUILD.gn +++ b/test/unittest/devicedebug_test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2024 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