From 631afe0e5d253dd65598598d5c6721e847a4b91d Mon Sep 17 00:00:00 2001 From: xlei1030 Date: Tue, 1 Mar 2022 14:28:15 +0800 Subject: [PATCH 01/14] =?UTF-8?q?ClientSocket=E7=B1=BB=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=88=90=E5=91=98=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xlei1030 --- interfaces/innerkits/include/client_socket.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interfaces/innerkits/include/client_socket.h b/interfaces/innerkits/include/client_socket.h index 7be38b87..dc459bc7 100644 --- a/interfaces/innerkits/include/client_socket.h +++ b/interfaces/innerkits/include/client_socket.h @@ -90,6 +90,7 @@ public: static constexpr int MAX_GIDS = 64; static constexpr int APL_MAX_LEN = 32; static constexpr int RENDER_CMD_MAX_LEN = 1024; + static constexpr int APPSPAWN_COLD_BOOT = 0x01; struct AppProperty { uint32_t uid; // the UNIX uid that the child process setuid() to after fork() @@ -102,6 +103,7 @@ public: uint32_t accessTokenId; char apl[APL_MAX_LEN]; char renderCmd[RENDER_CMD_MAX_LEN]; + uint32_t flags; }; private: -- Gitee From 335ed9525e08d60b7dbf8b16afa13a8cde35eff1 Mon Sep 17 00:00:00 2001 From: xlei1030 Date: Tue, 1 Mar 2022 14:59:48 +0800 Subject: [PATCH 02/14] =?UTF-8?q?=E6=B8=85=E7=90=86=E4=BF=A1=E5=8F=B7?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xlei1030 --- src/appspawn_server.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index e5679e6a..def2a98f 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -246,6 +246,17 @@ void AppSpawnServer::LoadAceLib() #endif } + +static void ClearEnvironment(void) +{ + sigset_t mask; + sigemptyset(&mask); + sigaddset(&mask, SIGCHLD); + sigaddset(&mask, SIGTERM); + sigprocmask(SIG_UNBLOCK, &mask, NULL); + return; +} + int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, ClientSocket::AppProperty *appProperty, int connectFd, pid_t &pid) { @@ -274,6 +285,7 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, socket_->CloseServerMonitor(); } close(fd[0]); // close read fd + ClearEnvironment(); UninstallSigHandler(); SetAppProcProperty(appProperty, longProcName, longProcNameLen, fd); _exit(0); @@ -794,14 +806,16 @@ int32_t AppSpawnServer::SetAppSandboxProperty(const ClientSocket::AppProperty *a void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appProperty) { int32_t ret = SetSelfTokenID(appProperty->accessTokenId); - if (ret != 0) { - HiLog::Error(LABEL, "AppSpawnServer::Failed to set access token id, errno = %{public}d", errno); - } + HiLog::Info(LABEL, "AppSpawnServer::set access token id = %{public}d, ret = %{public}d", + appProperty->accessTokenId, ret); + #ifdef WITH_SELINUX HapContext hapContext; ret = hapContext.HapDomainSetcontext(appProperty->apl, appProperty->processName); if (ret != 0) { HiLog::Error(LABEL, "AppSpawnServer::Failed to hap domain set context, errno = %{public}d", errno); + } else { + HiLog::Info(LABEL, "AppSpawnServer::Success to hap domain set context, ret = %{public}d", ret); } #endif } -- Gitee From 342cf8841ddc581f3a68a16f6d47899f4ab354fb Mon Sep 17 00:00:00 2001 From: xlei1030 Date: Tue, 1 Mar 2022 15:14:19 +0800 Subject: [PATCH 03/14] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xlei1030 --- src/appspawn_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index def2a98f..10bbd2f5 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -253,7 +253,7 @@ static void ClearEnvironment(void) sigemptyset(&mask); sigaddset(&mask, SIGCHLD); sigaddset(&mask, SIGTERM); - sigprocmask(SIG_UNBLOCK, &mask, NULL); + sigprocmask(SIG_UNBLOCK, &mask, nullptr); return; } -- Gitee From 0e6424ce4ceafb1c2b14c83a175a347de2b07d00 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 15:27:41 +0800 Subject: [PATCH 04/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- BUILD.gn | 14 +++++++ bundle.json | 1 + src/appspawn_server.cpp | 79 ++++++++++++++++++++++++++++++----- src/include/appspawn_server.h | 6 ++- tools/appspawn_start_app.cpp | 57 +++++++++++++++++++++++++ 5 files changed, 145 insertions(+), 12 deletions(-) create mode 100755 tools/appspawn_start_app.cpp diff --git a/BUILD.gn b/BUILD.gn index 9f0e9b9b..1a1a33e1 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -95,6 +95,20 @@ ohos_static_library("appspawn_server") { part_name = "${part_name}" } +ohos_executable("appspawntools") { + sources = [ "${appspawn_path}/tools/appspawn_start_app.cpp" ] + configs = [ ":appspawn_config" ] + deps = [ + "${appspawn_path}:appspawn_server", + "//base/security/access_token/interfaces/innerkits/token_setproc:libtoken_setproc", + ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + install_enable = true + subsystem_name = "${subsystem_name}" + part_name = "${part_name}" +} + ohos_prebuilt_etc("appspawn.rc") { source = "appspawn.cfg" relative_install_dir = "init" diff --git a/bundle.json b/bundle.json index 502b4725..b9972a5b 100644 --- a/bundle.json +++ b/bundle.json @@ -37,6 +37,7 @@ "//base/startup/appspawn_standard:appspawn", "//base/startup/appspawn_standard:appspawn.rc", "//base/startup/appspawn_standard:appspawn_server", + "//base/startup/appspawn_standard:appspawntools", "//base/startup/appspawn_standard:webview", "//base/startup/appspawn_standard/interfaces/innerkits:appspawn_socket_client" ], diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 10bbd2f5..31cf2728 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -246,7 +246,6 @@ void AppSpawnServer::LoadAceLib() #endif } - static void ClearEnvironment(void) { sigset_t mask; @@ -257,6 +256,40 @@ static void ClearEnvironment(void) return; } +int AppSpawnServer::DoColdStartApp(ClientSocket::AppProperty *appProperty, int fd) +{ + APPSPAWN_LOGI("DoColdStartApp::appName %s", appProperty->processName); + std::vector extractedCmds; + extractedCmds.push_back(const_cast("/system/bin/appspawntools")); + std::string fdStr = std::to_string(fd); + APPSPAWN_LOGI("DoColdStartApp::fd %d %s", fd, fdStr.c_str()); + extractedCmds.push_back(const_cast(fdStr.c_str())); + std::string uidStr = std::to_string(appProperty->uid); + APPSPAWN_LOGI("DoColdStartApp::uid %d %s", appProperty->uid, uidStr.c_str()); + extractedCmds.push_back(const_cast(uidStr.c_str())); + extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gid)).c_str())); + extractedCmds.push_back(const_cast(appProperty->processName)); + extractedCmds.push_back(const_cast(appProperty->bundleName)); + extractedCmds.push_back(const_cast(appProperty->soPath)); + std::string access = std::to_string(appProperty->accessTokenId); + APPSPAWN_LOGI("DoColdStartApp::accessTokenId %d %s", appProperty->accessTokenId, access.c_str()); + extractedCmds.push_back(const_cast(access.c_str())); + extractedCmds.push_back(const_cast(appProperty->apl)); + extractedCmds.push_back(const_cast(appProperty->renderCmd)); + extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->flags)).c_str())); + extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidCount)).c_str())); + for (uint32_t i = 0; i < appProperty->gidCount; i++) { + extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidTable[i])).c_str())); + } + extractedCmds.push_back(nullptr); + + int ret = execv(extractedCmds[0], extractedCmds.data()); + if (ret != 0) { + HiLog::Error(LABEL, "Failed to execv, errno = %{public}d", errno); + } + return 0; +} + int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, ClientSocket::AppProperty *appProperty, int connectFd, pid_t &pid) { @@ -287,7 +320,12 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, close(fd[0]); // close read fd ClearEnvironment(); UninstallSigHandler(); - SetAppProcProperty(appProperty, longProcName, longProcNameLen, fd); + appProperty->flags = 1; + if (appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) { + DoColdStartApp(appProperty, fd[1]); + } else { + SetAppProcProperty(appProperty, longProcName, longProcNameLen, fd[1]); + } _exit(0); } read(fd[0], &buff, sizeof(buff)); // wait child process resutl @@ -339,7 +377,8 @@ bool AppSpawnServer::ServerMain(char *longProcName, int64_t longProcNameLen) appMap_[pid] = appProperty->processName; } socket_->CloseConnection(connectFd); // close socket connection - APPSPAWN_LOGI("AppSpawnServer::parent process create app finish, pid = %d %s", pid, appProperty->processName); + APPSPAWN_LOGI("AppSpawnServer::parent process create app finish, pid = %d uid %d %s %s", + pid, appProperty->uid, appProperty->processName, appProperty->bundleName); } while (appMap_.size() > 0) { @@ -821,21 +860,27 @@ void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appPrope } bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProperty, char *longProcName, - int64_t longProcNameLen, const int32_t fd[FDLEN2]) + int64_t longProcNameLen, const int32_t fd) { + APPSPAWN_LOGI("SetAppProcProperty::bundleName %s", appProperty->bundleName); + APPSPAWN_LOGI("SetAppProcProperty::apl %s", appProperty->apl); + APPSPAWN_LOGI("SetAppProcProperty::uid %d", appProperty->uid); + APPSPAWN_LOGI("SetAppProcProperty::gidCound %d", appProperty->gidCount); + APPSPAWN_LOGI("SetAppProcProperty::accessTokenId %d", appProperty->accessTokenId); + pid_t newPid = getpid(); HiLog::Debug(LABEL, "AppSpawnServer::Success to fork new process, pid = %{public}d", newPid); int32_t ret = ERR_OK; ret = SetAppSandboxProperty(appProperty); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } ret = SetKeepCapabilities(appProperty->uid); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } @@ -843,31 +888,31 @@ bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProp ret = SetProcessName(longProcName, longProcNameLen, appProperty->processName, strlen(appProperty->processName) + 1); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } #ifdef GRAPHIC_PERMISSION_CHECK ret = SetUidGid(appProperty->uid, appProperty->gid, appProperty->gidTable, appProperty->gidCount); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } #endif ret = SetFileDescriptors(); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } ret = SetCapabilities(); if (FAILED(ret)) { - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); return false; } // notify success to father process and start app process - NotifyResToParentProc(fd[1], ret); + NotifyResToParentProc(fd, ret); #ifdef WEBVIEW_SPAWN using FuncType = void (*)(const char *cmd); @@ -941,5 +986,17 @@ bool AppSpawnServer::CheckAppProperty(const ClientSocket::AppProperty *appProper return true; } + +int AppSpawnServer::AppColdStart(char *longProcName, + int64_t longProcNameLen, const ClientSocket::AppProperty *appProperty, int fd) +{ + APPSPAWN_LOGI("AppColdStart appName %s", appProperty->bundleName); + LoadAceLib(); + if (!SetAppProcProperty(appProperty, longProcName, longProcNameLen, fd)) { + return -1; + } + APPSPAWN_LOGI("AppColdStart appName %s success", appProperty->bundleName); + return 0; +} } // namespace AppSpawn } // namespace OHOS diff --git a/src/include/appspawn_server.h b/src/include/appspawn_server.h index 6e2192a8..694b3e43 100644 --- a/src/include/appspawn_server.h +++ b/src/include/appspawn_server.h @@ -70,7 +70,11 @@ public: */ void SetServerSocket(const std::shared_ptr &serverSocket); + int AppColdStart(char *longProcName, + int64_t longProcNameLen, const ClientSocket::AppProperty *appProperty, int fd); private: + int DoColdStartApp(ClientSocket::AppProperty *appProperty, int fd); + static constexpr uint8_t BITLEN32 = 32; static constexpr uint8_t FDLEN2 = 2; static constexpr uint8_t FD_INIT_VALUE = 0; @@ -162,7 +166,7 @@ private: * Sets app process property. */ bool SetAppProcProperty(const ClientSocket::AppProperty *appProperty, char *longProcName, - int64_t longProcNameLen, const int32_t fd[FDLEN2]); + int64_t longProcNameLen, const int32_t fd); /** * Notify diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp new file mode 100755 index 00000000..80d8ce8a --- /dev/null +++ b/tools/appspawn_start_app.cpp @@ -0,0 +1,57 @@ +#include + +#include "appspawn_server.h" +#include "client_socket.h" +#include "hilog/log.h" +#include "securec.h" + +static const int DECIMAL = 10; + +int main(int argc, char *const argv[]) +{ + if (argc <= 11) { // 11 min argc + printf("appspawntools xxxx \n"); + return 0; + } + + // calculate child process long name size + uintptr_t start = reinterpret_cast(argv[0]); + uintptr_t end = reinterpret_cast(strchr(argv[argc - 1], 0)); + uintptr_t argvSize = end - start; + if (end == 0) { + return 0; + } + + auto appProperty = std::make_unique(); + if (appProperty == nullptr) { + return -1; + } + int index = 1; + int fd = strtoul(argv[index++], nullptr, DECIMAL); + appProperty->uid = strtoul(argv[index++], nullptr, DECIMAL); + appProperty->gid = strtoul(argv[index++], nullptr, DECIMAL); + (void)strcpy_s(appProperty->processName, sizeof(appProperty->processName), argv[index++]); + (void)strcpy_s(appProperty->bundleName, sizeof(appProperty->bundleName), argv[index++]); + (void)strcpy_s(appProperty->soPath, sizeof(appProperty->soPath), argv[index++]); + appProperty->accessTokenId = strtoul(argv[index++], nullptr, DECIMAL); + (void)strcpy_s(appProperty->apl, sizeof(appProperty->apl), argv[index++]); + (void)strcpy_s(appProperty->renderCmd, sizeof(appProperty->renderCmd), argv[index++]); + appProperty->flags = strtoul(argv[index++], nullptr, DECIMAL); + + appProperty->gidCount = strtoul(argv[index++], nullptr, DECIMAL); + uint32_t i = 0; + while (i < appProperty->gidCount && i < sizeof(appProperty->gidTable) / sizeof(sizeof(appProperty->gidTable[0]))) { + if (argv[index] == nullptr) { + break; + } + appProperty->gidTable[i] = strtoul(argv[index++], nullptr, DECIMAL); + } + auto appspawnServer = std::make_shared("AppSpawn"); + if (appspawnServer != nullptr) { + int ret = appspawnServer->AppColdStart(argv[0], argvSize, appProperty.get(), fd); + if (ret != 0) { + printf("Cold start %s fail \n", appProperty->bundleName); + } + } + return 0; +} -- Gitee From 4694a15acf319c6dbc384f5b88621d6f8388e5ef Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 16:30:31 +0800 Subject: [PATCH 05/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 14 ++++--------- tools/appspawn_start_app.cpp | 38 ++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 31cf2728..bb66a485 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -862,14 +862,9 @@ void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appPrope bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProperty, char *longProcName, int64_t longProcNameLen, const int32_t fd) { - APPSPAWN_LOGI("SetAppProcProperty::bundleName %s", appProperty->bundleName); - APPSPAWN_LOGI("SetAppProcProperty::apl %s", appProperty->apl); - APPSPAWN_LOGI("SetAppProcProperty::uid %d", appProperty->uid); - APPSPAWN_LOGI("SetAppProcProperty::gidCound %d", appProperty->gidCount); - APPSPAWN_LOGI("SetAppProcProperty::accessTokenId %d", appProperty->accessTokenId); - - pid_t newPid = getpid(); - HiLog::Debug(LABEL, "AppSpawnServer::Success to fork new process, pid = %{public}d", newPid); + APPSPAWN_LOGI("SetAppProcProperty::bundleName %s accessTokenId %d", + appProperty->bundleName, appProperty->accessTokenId); + HiLog::Debug(LABEL, "AppSpawnServer::Success to fork new process, pid = %{public}d", getpid()); int32_t ret = ERR_OK; ret = SetAppSandboxProperty(appProperty); @@ -885,7 +880,6 @@ bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProp } SetAppAccessToken(appProperty); - ret = SetProcessName(longProcName, longProcNameLen, appProperty->processName, strlen(appProperty->processName) + 1); if (FAILED(ret)) { NotifyResToParentProc(fd, ret); @@ -926,7 +920,7 @@ bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProp AppExecFwk::MainThread::Start(); #endif - HiLog::Error(LABEL, "Failed to start process, pid = %{public}d", newPid); + HiLog::Error(LABEL, "Failed to start process, pid = %{public}d", getpid()); return false; } diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index 80d8ce8a..3ae8606d 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -1,3 +1,17 @@ +/* + * 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 #include "appspawn_server.h" @@ -5,25 +19,29 @@ #include "hilog/log.h" #include "securec.h" +using namespace OHOS; +using namespace OHOS::HiviewDFX; +static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "AppSpawnServer"}; static const int DECIMAL = 10; int main(int argc, char *const argv[]) { if (argc <= 11) { // 11 min argc - printf("appspawntools xxxx \n"); - return 0; + HiLog::Error(LABEL, "Invalid argc %{public}d", argc); + return -1; } // calculate child process long name size uintptr_t start = reinterpret_cast(argv[0]); uintptr_t end = reinterpret_cast(strchr(argv[argc - 1], 0)); - uintptr_t argvSize = end - start; if (end == 0) { - return 0; + return -1; } + uintptr_t argvSize = end - start; auto appProperty = std::make_unique(); if (appProperty == nullptr) { + HiLog::Error(LABEL, "Failed to create app property %{public}s", argv[2]); return -1; } int index = 1; @@ -37,10 +55,18 @@ int main(int argc, char *const argv[]) (void)strcpy_s(appProperty->apl, sizeof(appProperty->apl), argv[index++]); (void)strcpy_s(appProperty->renderCmd, sizeof(appProperty->renderCmd), argv[index++]); appProperty->flags = strtoul(argv[index++], nullptr, DECIMAL); - + if (index >= argc) { + HiLog::Error(LABEL, "Invalid arg %{public}d %{public}d", index, argc); + return -1; + } appProperty->gidCount = strtoul(argv[index++], nullptr, DECIMAL); + uint32_t i = 0; while (i < appProperty->gidCount && i < sizeof(appProperty->gidTable) / sizeof(sizeof(appProperty->gidTable[0]))) { + if (index >= argc) { + HiLog::Error(LABEL, "Invalid arg %{public}d %{public}d", index, argc); + return -1; + } if (argv[index] == nullptr) { break; } @@ -50,7 +76,7 @@ int main(int argc, char *const argv[]) if (appspawnServer != nullptr) { int ret = appspawnServer->AppColdStart(argv[0], argvSize, appProperty.get(), fd); if (ret != 0) { - printf("Cold start %s fail \n", appProperty->bundleName); + HiLog::Error(LABEL, "Cold start %{public}s fail.", appProperty->bundleName); } } return 0; -- Gitee From 904ff6d25e9276df99d0e3ba71e3c328042e2f92 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 16:34:21 +0800 Subject: [PATCH 06/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- tools/appspawn_start_app.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index 3ae8606d..1ca7fd6d 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -62,7 +62,7 @@ int main(int argc, char *const argv[]) appProperty->gidCount = strtoul(argv[index++], nullptr, DECIMAL); uint32_t i = 0; - while (i < appProperty->gidCount && i < sizeof(appProperty->gidTable) / sizeof(sizeof(appProperty->gidTable[0]))) { + while ((i < appProperty->gidCount) && (i < sizeof(appProperty->gidTable) / sizeof(appProperty->gidTable[0]))) { if (index >= argc) { HiLog::Error(LABEL, "Invalid arg %{public}d %{public}d", index, argc); return -1; -- Gitee From 1b5286a33f5f233379242c6b9f19e8fa8616318a Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 16:43:10 +0800 Subject: [PATCH 07/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index bb66a485..532f17a4 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -320,7 +320,7 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, close(fd[0]); // close read fd ClearEnvironment(); UninstallSigHandler(); - appProperty->flags = 1; + appProperty->flags = 0; if (appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) { DoColdStartApp(appProperty, fd[1]); } else { -- Gitee From 7a8ce5ac55bd66bfadc17663cd46ac08617b8fbd Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 16:45:51 +0800 Subject: [PATCH 08/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 6 +----- tools/appspawn_start_app.cpp | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 532f17a4..15799b0e 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -862,12 +862,8 @@ void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appPrope bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProperty, char *longProcName, int64_t longProcNameLen, const int32_t fd) { - APPSPAWN_LOGI("SetAppProcProperty::bundleName %s accessTokenId %d", - appProperty->bundleName, appProperty->accessTokenId); HiLog::Debug(LABEL, "AppSpawnServer::Success to fork new process, pid = %{public}d", getpid()); - int32_t ret = ERR_OK; - - ret = SetAppSandboxProperty(appProperty); + int32_t ret = SetAppSandboxProperty(appProperty); if (FAILED(ret)) { NotifyResToParentProc(fd, ret); return false; diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index 1ca7fd6d..3ca42a5d 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -41,7 +41,7 @@ int main(int argc, char *const argv[]) auto appProperty = std::make_unique(); if (appProperty == nullptr) { - HiLog::Error(LABEL, "Failed to create app property %{public}s", argv[2]); + HiLog::Error(LABEL, "Failed to create app property %{public}s", argv[2]); // 2 name index return -1; } int index = 1; -- Gitee From 260559f7df1a0261627e537c0c18236cc5e229d7 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 17:36:49 +0800 Subject: [PATCH 09/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 15799b0e..71f241d6 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -320,7 +320,7 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, close(fd[0]); // close read fd ClearEnvironment(); UninstallSigHandler(); - appProperty->flags = 0; + appProperty->flags = 1; if (appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) { DoColdStartApp(appProperty, fd[1]); } else { -- Gitee From 882cb105f025792d564ebe7ba95945840d5c89b7 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 19:58:11 +0800 Subject: [PATCH 10/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 13 ++++++++++--- tools/appspawn_start_app.cpp | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 71f241d6..3f03a42a 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -275,17 +275,23 @@ int AppSpawnServer::DoColdStartApp(ClientSocket::AppProperty *appProperty, int f APPSPAWN_LOGI("DoColdStartApp::accessTokenId %d %s", appProperty->accessTokenId, access.c_str()); extractedCmds.push_back(const_cast(access.c_str())); extractedCmds.push_back(const_cast(appProperty->apl)); - extractedCmds.push_back(const_cast(appProperty->renderCmd)); + APPSPAWN_LOGI("DoColdStartApp renderCmd %s", appProperty->renderCmd); + if (strlen(appProperty->renderCmd) != 0) { + extractedCmds.push_back(const_cast(appProperty->renderCmd)); + } else { + extractedCmds.push_back(const_cast("")); + } extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->flags)).c_str())); extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidCount)).c_str())); for (uint32_t i = 0; i < appProperty->gidCount; i++) { extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidTable[i])).c_str())); } extractedCmds.push_back(nullptr); - + APPSPAWN_LOGI("DoColdStartApp extractedCmds %d", extractedCmds.size()); int ret = execv(extractedCmds[0], extractedCmds.data()); if (ret != 0) { HiLog::Error(LABEL, "Failed to execv, errno = %{public}d", errno); + NotifyResToParentProc(fd, -1); } return 0; } @@ -852,7 +858,8 @@ void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appPrope HapContext hapContext; ret = hapContext.HapDomainSetcontext(appProperty->apl, appProperty->processName); if (ret != 0) { - HiLog::Error(LABEL, "AppSpawnServer::Failed to hap domain set context, errno = %{public}d", errno); + HiLog::Error(LABEL, "AppSpawnServer::Failed to hap domain set context, errno = %{public}d %{public}s", + errno, appProperty->apl); } else { HiLog::Info(LABEL, "AppSpawnServer::Success to hap domain set context, ret = %{public}d", ret); } diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index 3ca42a5d..c77231f4 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -30,7 +30,7 @@ int main(int argc, char *const argv[]) HiLog::Error(LABEL, "Invalid argc %{public}d", argc); return -1; } - + HiLog::Debug(LABEL, "AppSpawnServer argc %{public}d app:%{public}s", argc, argv[4]); // 4 name index // calculate child process long name size uintptr_t start = reinterpret_cast(argv[0]); uintptr_t end = reinterpret_cast(strchr(argv[argc - 1], 0)); @@ -41,7 +41,7 @@ int main(int argc, char *const argv[]) auto appProperty = std::make_unique(); if (appProperty == nullptr) { - HiLog::Error(LABEL, "Failed to create app property %{public}s", argv[2]); // 2 name index + HiLog::Error(LABEL, "Failed to create app property %{public}s", argv[4]); // 4 name index return -1; } int index = 1; -- Gitee From 70330845ab10ced06b3520855caec5fd9a88f76e Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 21:20:36 +0800 Subject: [PATCH 11/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- tools/appspawn_start_app.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index c77231f4..29194df4 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -55,11 +55,8 @@ int main(int argc, char *const argv[]) (void)strcpy_s(appProperty->apl, sizeof(appProperty->apl), argv[index++]); (void)strcpy_s(appProperty->renderCmd, sizeof(appProperty->renderCmd), argv[index++]); appProperty->flags = strtoul(argv[index++], nullptr, DECIMAL); - if (index >= argc) { - HiLog::Error(LABEL, "Invalid arg %{public}d %{public}d", index, argc); - return -1; - } appProperty->gidCount = strtoul(argv[index++], nullptr, DECIMAL); + HiLog::Error(LABEL, "gidCount %{public}d %{public}d", index, appProperty->gidCount); uint32_t i = 0; while ((i < appProperty->gidCount) && (i < sizeof(appProperty->gidTable) / sizeof(appProperty->gidTable[0]))) { @@ -67,10 +64,7 @@ int main(int argc, char *const argv[]) HiLog::Error(LABEL, "Invalid arg %{public}d %{public}d", index, argc); return -1; } - if (argv[index] == nullptr) { - break; - } - appProperty->gidTable[i] = strtoul(argv[index++], nullptr, DECIMAL); + appProperty->gidTable[i++] = strtoul(argv[index++], nullptr, DECIMAL); } auto appspawnServer = std::make_shared("AppSpawn"); if (appspawnServer != nullptr) { -- Gitee From 41dc5915e984d6511b34624ec26f4171290aaa0b Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 22:12:29 +0800 Subject: [PATCH 12/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 3f03a42a..6ff954a0 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -261,28 +261,28 @@ int AppSpawnServer::DoColdStartApp(ClientSocket::AppProperty *appProperty, int f APPSPAWN_LOGI("DoColdStartApp::appName %s", appProperty->processName); std::vector extractedCmds; extractedCmds.push_back(const_cast("/system/bin/appspawntools")); - std::string fdStr = std::to_string(fd); - APPSPAWN_LOGI("DoColdStartApp::fd %d %s", fd, fdStr.c_str()); - extractedCmds.push_back(const_cast(fdStr.c_str())); - std::string uidStr = std::to_string(appProperty->uid); - APPSPAWN_LOGI("DoColdStartApp::uid %d %s", appProperty->uid, uidStr.c_str()); - extractedCmds.push_back(const_cast(uidStr.c_str())); - extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gid)).c_str())); + std::string tmp = std::to_string(fd); + APPSPAWN_LOGI("DoColdStartApp::fd %d %s", fd, tmp.c_str()); + extractedCmds.push_back(const_cast(tmp.c_str())); + std::string uid = std::to_string(appProperty->uid); + APPSPAWN_LOGI("DoColdStartApp::uid %d gid %d %s", appProperty->uid, appProperty->gid, uid.c_str()); + extractedCmds.push_back(const_cast(uid.c_str())); + std::string gid = std::to_string(appProperty->gid); + extractedCmds.push_back(const_cast(gid.c_str())); extractedCmds.push_back(const_cast(appProperty->processName)); extractedCmds.push_back(const_cast(appProperty->bundleName)); extractedCmds.push_back(const_cast(appProperty->soPath)); - std::string access = std::to_string(appProperty->accessTokenId); - APPSPAWN_LOGI("DoColdStartApp::accessTokenId %d %s", appProperty->accessTokenId, access.c_str()); - extractedCmds.push_back(const_cast(access.c_str())); + std::string accessTokenId = std::to_string(appProperty->accessTokenId); + APPSPAWN_LOGI("DoColdStartApp::accessTokenId %d %s", appProperty->accessTokenId, accessTokenId.c_str()); + extractedCmds.push_back(const_cast(accessTokenId.c_str())); extractedCmds.push_back(const_cast(appProperty->apl)); APPSPAWN_LOGI("DoColdStartApp renderCmd %s", appProperty->renderCmd); - if (strlen(appProperty->renderCmd) != 0) { - extractedCmds.push_back(const_cast(appProperty->renderCmd)); - } else { - extractedCmds.push_back(const_cast("")); - } - extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->flags)).c_str())); - extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidCount)).c_str())); + extractedCmds.push_back(const_cast(appProperty->renderCmd)); + std::string flags = std::to_string(appProperty->flags); + extractedCmds.push_back(const_cast(flags.c_str())); + std::string gidCount = std::to_string(appProperty->gidCount); + APPSPAWN_LOGI("DoColdStartApp gidCount %d %s", appProperty->gidCount, gidCount.c_str()); + extractedCmds.push_back(const_cast(gidCount.c_str())); for (uint32_t i = 0; i < appProperty->gidCount; i++) { extractedCmds.push_back(const_cast(std::string(std::to_string(appProperty->gidTable[i])).c_str())); } -- Gitee From 404d0e3f0268a795bf85f10103dd15306f9b8181 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 23:03:45 +0800 Subject: [PATCH 13/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 6 +++--- tools/appspawn_start_app.cpp | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 6ff954a0..59de9544 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -326,6 +326,7 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, close(fd[0]); // close read fd ClearEnvironment(); UninstallSigHandler(); + SetAppAccessToken(appProperty); appProperty->flags = 1; if (appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) { DoColdStartApp(appProperty, fd[1]); @@ -851,8 +852,8 @@ int32_t AppSpawnServer::SetAppSandboxProperty(const ClientSocket::AppProperty *a void AppSpawnServer::SetAppAccessToken(const ClientSocket::AppProperty *appProperty) { int32_t ret = SetSelfTokenID(appProperty->accessTokenId); - HiLog::Info(LABEL, "AppSpawnServer::set access token id = %{public}d, ret = %{public}d", - appProperty->accessTokenId, ret); + HiLog::Info(LABEL, "AppSpawnServer::set access token id = %{public}d, ret = %{public}d %{public}d", + appProperty->accessTokenId, ret, getuid()); #ifdef WITH_SELINUX HapContext hapContext; @@ -882,7 +883,6 @@ bool AppSpawnServer::SetAppProcProperty(const ClientSocket::AppProperty *appProp return false; } - SetAppAccessToken(appProperty); ret = SetProcessName(longProcName, longProcNameLen, appProperty->processName, strlen(appProperty->processName) + 1); if (FAILED(ret)) { NotifyResToParentProc(fd, ret); diff --git a/tools/appspawn_start_app.cpp b/tools/appspawn_start_app.cpp index 29194df4..072e69f9 100755 --- a/tools/appspawn_start_app.cpp +++ b/tools/appspawn_start_app.cpp @@ -56,8 +56,6 @@ int main(int argc, char *const argv[]) (void)strcpy_s(appProperty->renderCmd, sizeof(appProperty->renderCmd), argv[index++]); appProperty->flags = strtoul(argv[index++], nullptr, DECIMAL); appProperty->gidCount = strtoul(argv[index++], nullptr, DECIMAL); - HiLog::Error(LABEL, "gidCount %{public}d %{public}d", index, appProperty->gidCount); - uint32_t i = 0; while ((i < appProperty->gidCount) && (i < sizeof(appProperty->gidTable) / sizeof(appProperty->gidTable[0]))) { if (index >= argc) { -- Gitee From 0dcfbd0a281a2d5e49d6817009ca5e915e60fd51 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Tue, 1 Mar 2022 23:59:48 +0800 Subject: [PATCH 14/14] =?UTF-8?q?feature:=20=E6=94=AF=E6=8C=81=E5=86=B7?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xionglei6 --- src/appspawn_server.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/appspawn_server.cpp b/src/appspawn_server.cpp index 59de9544..4f99f948 100644 --- a/src/appspawn_server.cpp +++ b/src/appspawn_server.cpp @@ -40,6 +40,7 @@ #include "system_ability_definition.h" #include "token_setproc.h" #include "parameter.h" +#include "parameters.h" #include "beget_ext.h" #ifdef WITH_SELINUX #include "hap_restorecon.h" @@ -327,8 +328,8 @@ int AppSpawnServer::StartApp(char *longProcName, int64_t longProcNameLen, ClearEnvironment(); UninstallSigHandler(); SetAppAccessToken(appProperty); - appProperty->flags = 1; - if (appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) { + if ((appProperty->flags == ClientSocket::APPSPAWN_COLD_BOOT) && + OHOS::system::GetBoolParameter("appspawn.cold.boot", false)) { DoColdStartApp(appProperty, fd[1]); } else { SetAppProcProperty(appProperty, longProcName, longProcNameLen, fd[1]); -- Gitee