From 9dbe284f9f02341ddf71bd01c3482048efeb1cf2 Mon Sep 17 00:00:00 2001 From: changcheng-weng Date: Thu, 17 Sep 2020 21:32:04 +0800 Subject: [PATCH 1/3] update BUILD.gn. --- BUILD.gn | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index 2cbd4c6..5def570 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -11,6 +11,7 @@ # See the License for the specific language governing permissions and # limitations under the License. import("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/subsystem/aafwk/config.gni") lite_component("appspawn_lite") { features = [ @@ -28,6 +29,10 @@ executable("appspawn") { "src/appspawn_adapter.c", ] + ldflags = [ + "-lstdc++", + ] + cflags = [ "-Wall", "-Wno-format", @@ -39,17 +44,29 @@ executable("appspawn") { "//utils/native/lite/include", "//foundation/distributedschedule/interfaces/kits/samgr_lite/samgr", "//foundation/distributedschedule/interfaces/kits/samgr_lite/registry", + "//foundation/aafwk/interfaces/innerkits/abilitymgr_lite", "//third_party/bounds_checking_function/include/", "//third_party/cJSON", ] deps = [ "//third_party/bounds_checking_function:libsec_shared", + "//foundation/aafwk/frameworks/ability_lite:aafwk_abilitykit_lite", "//foundation/distributedschedule/services/samgr_lite/samgr:samgr", "//foundation/communication/frameworks/ipc_lite:liteipc_adapter", "//third_party/cJSON:cjson_shared", + "//utils/native/lite/kv_store:kv_store", ] + if (enable_ohos_appexecfwk_feature_ability == true) { + deps += [ + "//foundation/ace/frameworks/lite:ace_lite", + "//foundation/graphic/lite/frameworks/ui:ui", + "//foundation/graphic/lite/utils:graphic_utils", + "//foundation/graphic/lite/frameworks/surface", + ] + } + if (ohos_kernel_type == "liteos_a") { include_dirs += [ "//kernel/liteos_a/kernel/include", @@ -61,7 +78,6 @@ executable("appspawn") { } if (ohos_kernel_type == "linux") { - ldflags = ["-lstdc++"] include_dirs += [ ] } -- Gitee From ba65620e011b55fc7046bf67e9481f5b4dff58c1 Mon Sep 17 00:00:00 2001 From: changcheng-weng Date: Thu, 17 Sep 2020 21:33:28 +0800 Subject: [PATCH 2/3] update src/appspawn_process.c. --- src/appspawn_process.c | 46 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) diff --git a/src/appspawn_process.c b/src/appspawn_process.c index 31f0312..fcd50a6 100755 --- a/src/appspawn_process.c +++ b/src/appspawn_process.c @@ -19,6 +19,7 @@ #include #include #include +#include "ability_main.h" #include "appspawn_adapter.h" #include "log.h" #include "securec.h" @@ -31,17 +32,12 @@ extern "C" { #define DEFAULT_UMASK 022 #define CAP_NUM 2 -#define ABILITY_EXE_FILE_FULL_PATH "/bin/abilityMain" -#define ABILITY_EXE_FILE_NAME "abilityMain" #define ENV_TITLE "LD_LIBRARY_PATH=" #define UPPER_BOUND_GID 999 #define LOWER_BOUND_GID 100 #define GRP_NUM 2 #define DEVMGR_GRP 99 -static const unsigned int MAX_IDENTITY_ID_LENGTH = 25; -static const unsigned int MAX_PROCESS_NAME_LENGTH = 130; - static int SetPerms(uid_t uID, gid_t gID) { gid_t groups[GRP_NUM]; @@ -118,35 +114,11 @@ static char* GetEnvStrs(const MessageSt* msgSt) pid_t CreateProcess(const MessageSt* msgSt) { - char identityIDStr[MAX_IDENTITY_ID_LENGTH]; - char processNameStr[MAX_PROCESS_NAME_LENGTH]; - if (memset_s(identityIDStr, MAX_IDENTITY_ID_LENGTH, '\0', MAX_IDENTITY_ID_LENGTH) != EOK || - memset_s(processNameStr, MAX_PROCESS_NAME_LENGTH, '\0', MAX_PROCESS_NAME_LENGTH) != EOK) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] create service, memset_s failed."); - return -1; - } - - if (sprintf_s(identityIDStr, MAX_IDENTITY_ID_LENGTH, "%s", msgSt->identityID) <= 0 || - sprintf_s(processNameStr, MAX_PROCESS_NAME_LENGTH, "%s", msgSt->bundleName) <= 0) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sprintf_s failed. id %{public}s, name %{public}s.",\ - msgSt->identityID, msgSt->bundleName); - return -1; - } - char* envStr = GetEnvStrs(msgSt); if (envStr == NULL) { return -1; } - // check if the exe file exists - struct stat pathStat = {0}; - if (stat(ABILITY_EXE_FILE_FULL_PATH, &pathStat) != 0) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] stat %{public}s failed, err %{public}d.",\ - ABILITY_EXE_FILE_FULL_PATH, errno); - free(envStr); - return -1; - } - pid_t newPID = fork(); if (newPID < 0) { HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] create process, fork failed! err %{public}d.", errno); @@ -158,18 +130,18 @@ pid_t CreateProcess(const MessageSt* msgSt) if (newPID == 0) { // set permissions if (SetPerms(msgSt->uID, msgSt->gID) != 0) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] process %{public}s exit!", processNameStr); - _exit(0x7f); // 0x7f: user specified + HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] process %{public}s exit!", msgSt->bundleName); + exit(0x7f); // 0x7f: user specified } - char* argv[] = {ABILITY_EXE_FILE_NAME, identityIDStr, processNameStr, NULL}; - char* env[] = {envStr, NULL}; - if (execve(ABILITY_EXE_FILE_FULL_PATH, argv, env) != 0) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] execve %{public}s failed! err %{public}d.",\ - ABILITY_EXE_FILE_FULL_PATH, errno); + (void) prctl(PR_SET_NAME, msgSt->bundleName); + if (AbilityMain(msgSt->identityID) != 0) { + HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] AbilityMain execute failed, pid %{public}d.", getpid()); + exit(0x7f); // 0x7f: user specified } + HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sub-process exit, pid %{public}d.", getpid()); - _exit(0x7f); // 0x7f: user specified + exit(0x7f); // 0x7f: user specified } free(envStr); -- Gitee From 8279dffcaac6c1786ecf086baf760332a640c209 Mon Sep 17 00:00:00 2001 From: changcheng-weng Date: Wed, 30 Sep 2020 11:15:06 +0800 Subject: [PATCH 3/3] share library support --- include/appspawn_message.h | 1 - src/appspawn_message.c | 15 --------------- src/appspawn_process.c | 31 ------------------------------- src/appspawn_service.c | 4 ++-- 4 files changed, 2 insertions(+), 49 deletions(-) diff --git a/include/appspawn_message.h b/include/appspawn_message.h index 8fb547f..8f7fab7 100755 --- a/include/appspawn_message.h +++ b/include/appspawn_message.h @@ -23,7 +23,6 @@ extern "C" { typedef struct { char* bundleName; - char* sharedLibPaths; char* identityID; int uID; int gID; diff --git a/src/appspawn_message.c b/src/appspawn_message.c index d06c980..213d58a 100755 --- a/src/appspawn_message.c +++ b/src/appspawn_message.c @@ -30,8 +30,6 @@ extern "C" { static const size_t MAX_BUNDLE_NAME_LEN = 127; static const size_t MIN_BUNDLE_NAME_LEN = 7; -static const size_t MAX_SHARED_LIB_PATH_LEN = 2048; -static const size_t MIN_SHARED_LIB_PATH_LEN = 0; static const size_t MAX_IDENTITY_ID_LEN = 24; static const size_t MIN_IDENTITY_ID_LEN = 1; @@ -43,11 +41,6 @@ void FreeMessageSt(MessageSt* targetSt) targetSt->bundleName = NULL; } - if (targetSt->sharedLibPaths != NULL) { - free(targetSt->sharedLibPaths); - targetSt->sharedLibPaths = NULL; - } - if (targetSt->identityID != NULL) { free(targetSt->identityID); targetSt->identityID = NULL; @@ -117,14 +110,6 @@ int SplitMessage(const char* msg, unsigned int msgLen, MessageSt* msgSt) return ret; } - cJSON* libPathsItem = cJSON_GetObjectItem(rootJ, "sharedLibPaths"); - ret = ReadStringItem(libPathsItem, &(msgSt->sharedLibPaths), MAX_SHARED_LIB_PATH_LEN, MIN_SHARED_LIB_PATH_LEN); - if (ret != EC_SUCCESS) { - FreeMessageSt(msgSt); - cJSON_Delete(rootJ); - return ret; - } - cJSON* identityIDItem = cJSON_GetObjectItem(rootJ, "identityID"); ret = ReadStringItem(identityIDItem, &(msgSt->identityID), MAX_IDENTITY_ID_LEN, MIN_IDENTITY_ID_LEN); if (ret != EC_SUCCESS) { diff --git a/src/appspawn_process.c b/src/appspawn_process.c index fcd50a6..de3d099 100755 --- a/src/appspawn_process.c +++ b/src/appspawn_process.c @@ -88,41 +88,11 @@ static int SetPerms(uid_t uID, gid_t gID) return 0; } -static char* GetEnvStrs(const MessageSt* msgSt) -{ - size_t totalLen = strlen(ENV_TITLE) + strlen(msgSt->sharedLibPaths); - char* envStr = (char*)malloc(totalLen + 1); - if (envStr == NULL) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] malloc for env failed! len %{public}u.", totalLen); - return NULL; - } - - if (memset_s(envStr, totalLen + 1, '\0', totalLen + 1) != EOK) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] memset_s for env failed."); - free(envStr); - return NULL; - } - - if (sprintf_s(envStr, totalLen + 1, "%s%s", ENV_TITLE, msgSt->sharedLibPaths) <= 0) { - HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] sprintf_s for env failed. libPath %{public}s",\ - msgSt->sharedLibPaths); - free(envStr); - return NULL; - } - return envStr; -} - pid_t CreateProcess(const MessageSt* msgSt) { - char* envStr = GetEnvStrs(msgSt); - if (envStr == NULL) { - return -1; - } - pid_t newPID = fork(); if (newPID < 0) { HILOG_ERROR(HILOG_MODULE_HIVIEW, "[appspawn] create process, fork failed! err %{public}d.", errno); - free(envStr); return -1; } @@ -144,7 +114,6 @@ pid_t CreateProcess(const MessageSt* msgSt) exit(0x7f); // 0x7f: user specified } - free(envStr); return newPID; } diff --git a/src/appspawn_service.c b/src/appspawn_service.c index 3415d9a..1899730 100755 --- a/src/appspawn_service.c +++ b/src/appspawn_service.c @@ -119,8 +119,8 @@ static int Invoke(IServerProxy* iProxy, int funcId, void* origin, IpcIo* req, Ip return EC_FAILURE; } - HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] msg<%{public}s,%{public}s,%{public}s,%{public}d,%{public}d>",\ - msgSt.bundleName, msgSt.sharedLibPaths, msgSt.identityID, msgSt.uID, msgSt.gID); + HILOG_INFO(HILOG_MODULE_HIVIEW, "[appspawn] msg<%{public}s,%{public}s,%{public}d,%{public}d>",\ + msgSt.bundleName, msgSt.identityID, msgSt.uID, msgSt.gID); pid_t newPid = CreateProcess(&msgSt); FreeMessageSt(&msgSt); -- Gitee