!942 set env info transferred from ams for app

Merge pull request !942 from nianyuu/master
This commit is contained in:
openharmony_ci 2024-02-29 08:12:10 +00:00 committed by Gitee
commit 0149021814
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 217 additions and 1 deletions

View File

@ -133,6 +133,7 @@ if (!defined(ohos_lite)) {
"${appspawn_path}/interfaces/innerkits/src/appspawn_mount_permission.cpp",
"${appspawn_path}/standard/appspawn_process.c",
"${appspawn_path}/standard/appspawn_service.c",
"${appspawn_path}/util/src/env_utils.cpp",
"${appspawn_path}/util/src/json_utils.cpp",
"${appspawn_path}/util/src/sandbox_utils.cpp",
]

View File

@ -131,6 +131,12 @@ int DoStartApp(struct AppSpawnContent_ *content, AppSpawnClient *client, char *l
return ret, "Failed to setCapabilities");
}
if (content->setEnvInfo) {
ret = content->setEnvInfo(content, client);
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);
return ret, "Failed to setEnvInfo");
}
if (content->waitForDebugger) {
ret = content->waitForDebugger(client);
APPSPAWN_CHECK(ret == 0, NotifyResToParent(content, client, ret);

View File

@ -64,6 +64,7 @@ typedef struct AppSpawnContent_ {
int (*setAppSandbox)(struct AppSpawnContent_ *content, AppSpawnClient *client);
int (*setKeepCapabilities)(struct AppSpawnContent_ *content, AppSpawnClient *client);
int (*setFileDescriptors)(struct AppSpawnContent_ *content, AppSpawnClient *client);
int (*setEnvInfo)(struct AppSpawnContent_ *content, AppSpawnClient *client);
int (*setProcessName)(struct AppSpawnContent_ *content, AppSpawnClient *client,
char *longProcName, uint32_t longProcNameLen);
int (*setUidGid)(struct AppSpawnContent_ *content, AppSpawnClient *client);

View File

@ -15,6 +15,7 @@
#include "appspawn_service.h"
#include "appspawn_adapter.h"
#include "env_utils.h"
#include "param_helper.h"
#include <fcntl.h>
@ -639,6 +640,7 @@ void SetContentFunction(AppSpawnContent *content)
content->setUidGid = SetUidGid;
content->setXpmConfig = SetXpmConfig;
content->setFileDescriptors = SetFileDescriptors;
content->setEnvInfo = SetEnvInfo;
content->coldStartApp = ColdStartApp;
content->setAsanEnabledEnv = SetAsanEnabledEnv;
if (content->isNweb) {

View File

@ -139,6 +139,39 @@ HWTEST_F(AppSpawnClientTest, AppSpawn_Client_AppSpawn_3, TestSize.Level0)
ClientClose();
}
// has env data,read json from etc
static const std::string g_envJsonStr = "|AppEnv|{"
"\"test.name1\": \"test.value1\","
"\"test.name2\": \"test.value2\""
"}|AppEnv|";
HWTEST_F(AppSpawnClientTest, AppSpawn_Client_AppSpawn_3_1, TestSize.Level0)
{
APPSPAWN_LOGI("AppSpawn_Client_AppSpawn_3_1 start");
int ret = ClientCreateSocket("/dev/unix/socket/AppSpawn");
EXPECT_EQ(ret, 0);
AppParameter request = {};
memset_s((void *)(&request), sizeof(request), 0, sizeof(request));
ClientFillMsg(&request, "ohos.samples.clock", "ls -l > aaa.txt");
printf("AppSpawn_Client_AppSpawn_3_1 env %zu %s \n", g_envJsonStr.size(), g_envJsonStr.c_str());
request.extraInfo.totalLength = g_envJsonStr.size();
std::vector<char *> data(sizeof(request) + g_envJsonStr.size());
memcpy_s(data.data(), sizeof(request), &request, sizeof(request));
memcpy_s(data.data() + sizeof(request), g_envJsonStr.size(), g_envJsonStr.data(), g_envJsonStr.size());
ret = ClientSendMsg(reinterpret_cast<const uint8_t *>(data.data()), data.size());
EXPECT_EQ(ret, 0);
pid_t pid = 0;
ret = ClientRecvMsg(pid);
if (pid > 0) {
kill(pid, SIGKILL);
}
// close client
ClientClose();
APPSPAWN_LOGI("AppSpawn_Client_AppSpawn_3_1 end");
}
HWTEST_F(AppSpawnClientTest, AppSpawn_Client_AppSpawn_4, TestSize.Level0)
{
APPSPAWN_LOGI("AppSpawn_Client_AppSpawn_4");

View File

@ -101,6 +101,7 @@ if (!defined(ohos_lite)) {
"${appspawn_path}/standard/appspawn_process.c",
"${appspawn_path}/standard/appspawn_service.c",
"${appspawn_path}/util/src/command_lexer.cpp",
"${appspawn_path}/util/src/env_utils.cpp",
"${appspawn_path}/util/src/json_utils.cpp",
"${appspawn_path}/util/src/param_helper.c",
"${appspawn_path}/util/src/sandbox_utils.cpp",

View File

@ -29,6 +29,7 @@
#include "appspawn_adapter.h"
#include "appspawn_server.h"
#include "appspawn_service.h"
#include "env_utils.h"
#include "init_hashmap.h"
#include "json_utils.h"
#include "le_task.h"
@ -421,6 +422,66 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003_4, TestSize.Level0)
}
APPSPAWN_LOGI("App_Spawn_Standard_003_4 en");
}
/**
* @tc.name: App_Spawn_Standard_003_5
* @tc.desc: Verify set Arg if GetAppSpawnClient succeed, with AppEnvInfo
* @tc.type: FUNC
* @tc.require:issueI936IH
* @tc.author:
*/
HWTEST(AppSpawnStandardTest, App_Spawn_Standard_003_5, TestSize.Level0)
{
APPSPAWN_LOGI("App_Spawn_Standard_003_5 start");
AppSpawnClientExt client = {};
char arg1[] = "/system/bin/appspawn";
char arg2[] = "cold-start";
char arg3[] = "1";
{
char arg4[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char arg5[] = "200";
char arg6[] = "|AppEnv|{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}|AppEnv|";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv)/sizeof(argv[0]);
EXPECT_EQ(0, GetAppSpawnClientFromArg(argc, argv, &client));
}
{ // AppEnvInfo content is valid, but length is 0
char arg4[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char arg5[] = "0";
char arg6[] = "|AppEnv|{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}|AppEnv|";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, arg6};
int argc = sizeof(argv)/sizeof(argv[0]);
EXPECT_EQ(0, GetAppSpawnClientFromArg(argc, argv, &client));
}
{ // AppEnvInfo content is valid, but length is nullptr
char arg4[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char arg6[] = "|AppEnv|{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}|AppEnv|";
char* argv[] = {arg1, arg2, arg3, arg4, nullptr, arg6};
int argc = sizeof(argv)/sizeof(argv[0]);
EXPECT_EQ(0, GetAppSpawnClientFromArg(argc, argv, &client));
}
{ // AppEnvInfo length is valid, but content is nullptr
char arg4[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char arg5[] = "200";
char* argv[] = {arg1, arg2, arg3, arg4, arg5, nullptr};
int argc = sizeof(argv)/sizeof(argv[0]);
EXPECT_EQ(-1, GetAppSpawnClientFromArg(argc, argv, &client));
}
{ // AppEnvInfo length is valid, but argc is 5
char arg4[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char arg5[] = "200";
char* argv[] = {arg1, arg2, arg3, arg4, arg5};
int argc = sizeof(argv)/sizeof(argv[0]);
EXPECT_EQ(-1, GetAppSpawnClientFromArg(argc, argv, &client));
}
APPSPAWN_LOGI("App_Spawn_Standard_003_5 end");
}
/**
* @tc.name: App_Spawn_Standard_004
* @tc.desc: App cold start.
@ -479,6 +540,40 @@ HWTEST(AppSpawnStandardTest, App_Spawn_Standard_004, TestSize.Level0)
GTEST_LOG_(INFO) << "App_Spawn_Standard_004 end";
}
/**
* @tc.name: App_Spawn_Standard_004_1
* @tc.desc: App cold start.
* @tc.type: FUNC
* @tc.require:issueI936IH
* @tc.author:
*/
HWTEST(AppSpawnStandardTest, App_Spawn_Standard_004_1, TestSize.Level0)
{
APPSPAWN_LOGI("App_Spawn_Standard_004_1 start");
string longProcName = "App_Spawn_Standard_004_1";
int64_t longProcNameLen = longProcName.length();
int cold = 1;
AppSpawnContent *content = AppSpawnCreateContent("AppSpawn", (char*)longProcName.c_str(), longProcNameLen, cold);
EXPECT_TRUE(content);
content->loadExtendLib = LoadExtendLib;
content->runChildProcessor = RunChildProcessor;
content->setEnvInfo = SetEnvInfo;
content->runChildProcessor(content, nullptr);
char tmp0[] = "/system/bin/appspawn";
char tmp1[] = "cold-start";
char tmp2[] = "1";
{
char tmp3[] = "1:1:1:1:1:1:1:1:1:2:1000:1000:ohos.samples:ohos.samples.ecg:"
"default:671201800:system_core:default:owerid:0:671201800";
char tmp4[] = "200";
char tmp5[] = "|AppEnv|{\"test.name1\": \"test.value1\", \"test.name2\": \"test.value2\"}|AppEnv|";
char * const argv[] = {tmp0, tmp1, tmp2, tmp3, tmp4, tmp5};
AppSpawnColdRun(content, 6, argv);
}
APPSPAWN_LOGI("App_Spawn_Standard_004_1 end");
}
/**
* @tc.name: App_Spawn_Standard_005
* @tc.desc: Verify start App.

33
util/include/env_utils.h Normal file
View File

@ -0,0 +1,33 @@
/*
* 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
*
* 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.
*/
#ifndef ENV_UTILS_H
#define ENV_UTILS_H
#include <stdint.h>
#include "appspawn_server.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
int32_t SetEnvInfo(struct AppSpawnContent_ *content, AppSpawnClient *client);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // ENV_UTILS_H

View File

@ -36,6 +36,7 @@ public:
static int32_t SetAppSandboxPropertyNweb(AppSpawnClient *client);
static uint32_t GetSandboxNsFlags(bool isNweb);
static std::set<std::string> GetMountPermissionNames();
static std::string GetExtraInfoByType(const ClientSocket::AppProperty *appProperty, const std::string &type);
typedef struct {
unsigned long mountFlags;
std::string optionsPoint;

43
util/src/env_utils.cpp Normal file
View File

@ -0,0 +1,43 @@
/*
* 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
*
* 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 "env_utils.h"
#include "appspawn_service.h"
#include "sandbox_utils.h"
#include "nlohmann/json.hpp"
const std::string APPENVLIST_TYPE = "|AppEnv|";
int32_t SetEnvInfo(struct AppSpawnContent_ *content, AppSpawnClient *client)
{
int ret = 0;
OHOS::AppSpawn::ClientSocket::AppProperty *appProperty = &((AppSpawnClientExt *)client)->property;
std::string appEnvInfo = OHOS::AppSpawn::SandboxUtils::GetExtraInfoByType(appProperty, APPENVLIST_TYPE);
if (appEnvInfo.length() == 0) {
return ret;
}
nlohmann::json envs = nlohmann::json::parse(appEnvInfo.c_str(), nullptr, false);
APPSPAWN_CHECK(!envs.is_discarded(), return -1, "SetEnvInfo: json parse failed");
for (nlohmann::json::iterator it = envs.begin(); it != envs.end(); ++it) {
APPSPAWN_CHECK(it.value().is_string(), return -1, "SetEnvInfo: element type error");
std::string name = it.key();
std::string value = it.value();
ret = setenv(name.c_str(), value.c_str(), 1);
APPSPAWN_CHECK(ret == 0, return ret, "setenv failed, errno is %{public}d", errno);
}
return ret;
}

View File

@ -943,7 +943,7 @@ static inline bool CheckPath(const std::string& name)
return !name.empty() && name != "." && name != ".." && name.find("/") == std::string::npos;
}
static std::string GetExtraInfoByType(const ClientSocket::AppProperty *appProperty, const std::string &type)
std::string SandboxUtils::GetExtraInfoByType(const ClientSocket::AppProperty *appProperty, const std::string &type)
{
if (appProperty->extraInfo.totalLength == 0 || appProperty->extraInfo.data == NULL) {
return "";