提交 02b4a78f 编写于 作者: C cheng_jinsong

read param 优化

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
......@@ -33,9 +33,13 @@ declare_args() {
# boot_kernel_extended_cmdline for extend cmdline
startup_init_feature_decode_group_file = true
startup_init_feature_decode_group_file = false
startup_init_test_performance = true
startup_init_with_param_base = false
if (!use_musl) {
startup_init_with_param_base = true
startup_init_feature_decode_group_file = true
}
}
......@@ -160,6 +160,9 @@ if (defined(ohos_lite)) {
]
}
if (startup_init_test_performance) {
defines += [ "PARAM_TEST_PERFORMANCE" ]
}
install_images = [ "system" ]
install_enable = true
......
......@@ -358,7 +358,7 @@ static int32_t BShellParamCmdWait(BShellHandle shell, int32_t argc, char *argv[]
static int32_t BShellParamCmdDump(BShellHandle shell, int32_t argc, char *argv[])
{
BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
if (argc >= 2) {
if (argc >= 2) { // 2 min parameter
if (strcmp(argv[1], "verbose") == 0) {
SystemDumpParameters(1, -1, printf);
return 0;
......
......@@ -119,8 +119,26 @@ if (defined(ohos_lite)) {
]
part_name = "init"
subsystem_name = "startup"
extra_paras = []
if (target_cpu == "arm64") {
extra_paras = [ "const.product.cpu.abilist=arm64-v8a" ]
extra_paras += [ "const.product.cpu.abilist=arm64-v8a" ]
}
if (build_variant == "user") {
extra_paras += [
"const.secure=1",
"const.debuggable=0",
]
} else if (build_variant == "root") {
extra_paras += [
"const.secure=0",
"const.debuggable=1",
]
}
if (device_type != "default") {
extra_paras += [
"const.product.devicetype=${device_type}",
"const.build.characteristics=${device_type}",
]
}
module_install_dir = "etc/param"
}
......
......@@ -46,7 +46,7 @@ ddms:x:3012:
access_token:x:3020:
blue_host:x:3021:
sample_host:x:3022:
usb_host:x:3023:
usb_host:x:3023:shell
usbfnMaster_host:x:3024:
power_host:x:3025:
wifi_host:x:3026:
......@@ -119,6 +119,7 @@ memmgr:x:1111:
ispserver:x:3821:
dfs_share:x:3822:
appspawn:x:4000:
print:x:3823:
security_guard:x:3520:
tee:x:6668:
dsserver:x:3068:
......@@ -13,7 +13,7 @@
const.actionable_compatible_property.enabled=false
const.postinstall.fstab.prefix=/system
const.secure=1
const.secure=0
security.perf_harden=1
const.allow.mock.location=0
const.debuggable=1
......@@ -29,7 +29,7 @@ const.build.product=default
const.product.hardwareversion=default
const.product.bootloader.version=bootloader
const.product.cpu.abilist=default
const.product.software.version=OpenHarmony 3.2.9.1
const.product.software.version=OpenHarmony 4.0.1.1
const.product.incremental.version=default
const.product.firstapiversion=1
const.product.build.type=default
......
......@@ -12,5 +12,5 @@
# limitations under the License.
const.ohos.version.security_patch=2022-03-30
const.ohos.releasetype=Canary1
const.ohos.apiversion=9
const.ohos.fullname=OpenHarmony-3.2.0.0
const.ohos.apiversion=10
const.ohos.fullname=OpenHarmony-4.0.0.0
......@@ -115,6 +115,7 @@ motion_host:x:3065:3065:::/bin/false
memmgr:x:1111:1111:::/bin/false
ispserver:x:3821:3821:::/bin/false
dfs_share:x:3822:3822:::/bin/false
print:x:3823:3823:::/bin/false
security_guard:x:3520:3520:::/bin/false
tee:x:6668:6668:::/bin/false
dsserver:x:3068:3068:::/bin/false
......@@ -11,7 +11,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
const.ohos.releasetype=Beta3
const.ohos.apiversion=9
const.ohos.releasetype=Canary1
const.ohos.apiversion=10
const.ohos.version.security_patch=2022-09-01
const.ohos.fullname=OpenHarmony-3.2.9.1
\ No newline at end of file
const.ohos.fullname=OpenHarmony-4.0.1.1
\ No newline at end of file
......@@ -24,7 +24,7 @@ extern "C" {
#endif
typedef uint32_t ParamHandle;
typedef void * CachedHandle;
typedef void *CachedHandle;
typedef struct {
uint8_t updaterMode;
......
......@@ -94,6 +94,10 @@ static int FdHolderSockInit(void)
void SystemInit(void)
{
SignalInit();
// Set up a session keyring that all processes will have access to.
KeyCtrlGetKeyringId(KEY_SPEC_SESSION_KEYRING, 1);
// umask call always succeeds and return the previous mask value which is not needed here
(void)umask(DEFAULT_UMASK_INIT);
MakeDirRecursive("/dev/unix/socket", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
......@@ -199,9 +203,6 @@ static void StartInitSecondStage(void)
// It will panic if close stdio before execv("/bin/sh", NULL)
CloseStdio();
// Set up a session keyring that all processes will have access to.
KeyCtrlGetKeyringId(KEY_SPEC_SESSION_KEYRING, 1);
#ifndef DISABLE_INIT_TWO_STAGES
INIT_LOGI("Start init second stage.");
SwitchRoot("/usr");
......
......@@ -19,6 +19,7 @@
#include "init_hook.h"
#include "init_module_engine.h"
#include "plugin_adapter.h"
#include "securec.h"
#include <policycoreutils.h>
#include <selinux/selinux.h>
......@@ -30,8 +31,13 @@ enum {
CMD_RESTORE_INDEX = 3,
};
extern char *__progname;
static int LoadSelinuxPolicy(int id, const char *name, int argc, const char **argv)
{
int ret;
char process_context[MAX_SECON_LEN];
UNUSED(id);
UNUSED(name);
UNUSED(argc);
......@@ -44,7 +50,12 @@ static int LoadSelinuxPolicy(int id, const char *name, int argc, const char **ar
PLUGIN_LOGI("main, load_policy success.");
}
setcon("u:r:init:s0");
ret = snprintf_s(process_context, sizeof(process_context), sizeof(process_context) - 1, "u:r:%s:s0", __progname);
if (ret == -1) {
setcon("u:r:init:s0");
} else {
setcon(process_context);
}
(void)RestoreconRecurse("/dev");
return 0;
}
......
......@@ -48,7 +48,11 @@ static void GetUserIdByName(uid_t *uid, const char *name)
static void GetGroupIdByName(gid_t *gid, const char *name)
{
*gid = -1;
struct group *data = NULL;
struct group *data = getgrnam(name);
if (data != NULL) {
*gid = data->gr_gid;
return;
}
while ((data = getgrent()) != NULL) {
if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) {
*gid = data->gr_gid;
......
......@@ -251,7 +251,7 @@ static int OpenPermissionWorkSpace(const char *path)
ret = SelinuxGetAllLabel(1, HandleSelinuxLabel);
} else if (strcmp(path, "open") == 0) {
if (loadLabels == 0) {
ret = SelinuxGetAllLabel(2, HandleSelinuxLabel);
ret = SelinuxGetAllLabel(2, HandleSelinuxLabel); // 2 mean need to open workspace
}
loadLabels = 1;
}
......
......@@ -115,6 +115,10 @@ if (defined(ohos_lite)) {
if (use_musl) {
defines += [ "__MUSL__" ]
}
if (startup_init_test_performance) {
defines += [ "PARAM_TEST_PERFORMANCE" ]
}
if (!startup_init_with_param_base) {
ldflags = [ "-nostdlib" ]
configs -= inherited_configs
......
......@@ -38,28 +38,6 @@ STATIC_INLINE ParamTrieNode *BaseFindTrieNode(WorkSpace *workSpace,
const char *key, uint32_t keyLen, uint32_t *matchLabel);
STATIC_INLINE int ReadParamValue_(ParamNode *entry, uint32_t *commitId, char *value, uint32_t *length);
STATIC_INLINE int IsWorkSpaceReady(WorkSpace *workSpace)
{
if (workSpace == NULL) {
return -1;
}
int ret = -1;
WORKSPACE_RD_LOCK(workSpace);
if (workSpace->area != NULL) {
if ((g_paramWorkSpace.flags & WORKSPACE_FLAGS_NEED_ACCESS) == WORKSPACE_FLAGS_NEED_ACCESS) {
char buffer[FILENAME_LEN_MAX] = {0};
int size = ParamSprintf(buffer, sizeof(buffer), "%s/%s", PARAM_STORAGE_PATH, workSpace->fileName);
if (size > 0 && access(buffer, R_OK) == 0) {
ret = 0;
}
} else {
ret = 0;
}
}
WORKSPACE_RW_UNLOCK(workSpace);
return ret;
}
// return workspace no check valid
STATIC_INLINE WorkSpace *GetWorkSpaceByName(const char *name)
{
......@@ -241,7 +219,6 @@ INIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void)
int SystemReadParam(const char *name, char *value, uint32_t *len)
{
InitParameterClient();
PARAM_WORKSPACE_CHECK(&g_paramWorkSpace, return -1, "Param workspace has not init.");
PARAM_CHECK(name != NULL && len != NULL, return -1, "The name or value is null");
ParamTrieNode *node = NULL;
......@@ -410,7 +387,7 @@ INIT_LOCAL_API int OpenWorkSpace(uint32_t index, int readOnly)
workSpace = paramSpace->workSpace[index];
}
if (workSpace == NULL) {
return -1;
return 0;
}
int ret = 0;
WORKSPACE_RW_LOCK(workSpace);
......@@ -534,7 +511,8 @@ STATIC_INLINE int DacCheckParamPermission(const ParamLabelIndex *labelIndex,
if ((node->mode & localMode) != 0) {
ret = DAC_RESULT_PERMISSION;
} else {
PARAM_LOGW("Param '%s' label gid:%d uid:%d mode 0%o", name, srcLabel->cred.gid, srcLabel->cred.uid, localMode);
PARAM_LOGW("Param '%s' label gid:%d uid:%d mode 0%o",
name, srcLabel->cred.gid, srcLabel->cred.uid, localMode);
PARAM_LOGW("Cfg label %u gid:%d uid:%d mode 0%o ", index, node->gid, node->uid, node->mode);
#ifndef __MUSL__
#ifndef STARTUP_INIT_TEST
......@@ -547,6 +525,28 @@ STATIC_INLINE int DacCheckParamPermission(const ParamLabelIndex *labelIndex,
}
#ifdef PARAM_SUPPORT_SELINUX
STATIC_INLINE int IsWorkSpaceReady(WorkSpace *workSpace)
{
if (workSpace == NULL) {
return -1;
}
int ret = -1;
WORKSPACE_RD_LOCK(workSpace);
if (workSpace->area != NULL) {
if ((g_paramWorkSpace.flags & WORKSPACE_FLAGS_NEED_ACCESS) == WORKSPACE_FLAGS_NEED_ACCESS) {
char buffer[FILENAME_LEN_MAX] = {0};
int size = ParamSprintf(buffer, sizeof(buffer), "%s/%s", PARAM_STORAGE_PATH, workSpace->fileName);
if (size > 0 && access(buffer, R_OK) == 0) {
ret = 0;
}
} else {
ret = 0;
}
}
WORKSPACE_RW_UNLOCK(workSpace);
return ret;
}
STATIC_INLINE const char *GetSelinuxContent(const char *name)
{
SelinuxSpace *selinuxSpace = &g_paramWorkSpace.selinuxSpace;
......@@ -639,8 +639,7 @@ STATIC_INLINE ParamTrieNode *BaseFindTrieNode(WorkSpace *workSpace,
PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid key ");
uint32_t tmpMatchLen = 0;
ParamTrieNode *node = NULL;
node = FindTrieNode_(workSpace, key, keyLen, &tmpMatchLen);
ParamTrieNode *node = FindTrieNode_(workSpace, key, keyLen, &tmpMatchLen);
if (matchLabel != NULL) {
*matchLabel = tmpMatchLen;
}
......@@ -672,7 +671,8 @@ CachedHandle CachedParameterCreate(const char *name, const char *defValue)
int ret = ReadParamWithCheck(workspace, name, DAC_READ, &node);
PARAM_CHECK(ret == 0, return NULL, "Forbid to access parameter %s", name);
CachedParameter *param = malloc(sizeof(CachedParameter) + PARAM_ALIGN(nameLen) + 1 + PARAM_VALUE_LEN_MAX);
CachedParameter *param = (CachedParameter *)malloc(
sizeof(CachedParameter) + PARAM_ALIGN(nameLen) + 1 + PARAM_VALUE_LEN_MAX);
PARAM_CHECK(param != NULL, return NULL, "Failed to create CachedParameter for %s", name);
ret = ParamStrCpy(param->data, nameLen + 1, name);
PARAM_CHECK(ret == 0, free(param);
......@@ -699,6 +699,7 @@ CachedHandle CachedParameterCreate(const char *name, const char *defValue)
return NULL, "Failed to copy name %s", name);
}
param->spaceCommitId = ATOMIC_LOAD_EXPLICIT(&workspace->area->commitId, memory_order_acquire);
PARAM_LOGV("CachedParameterCreate %u %u %lld \n", param->dataIndex, param->dataCommitId, param->spaceCommitId);
return (CachedHandle)param;
}
......@@ -747,215 +748,107 @@ void CachedParameterDestroy(CachedHandle handle)
}
}
#if 1 // def PARAM_BASE
#ifdef PARAM_TEST_PERFORMANCE
#define MAX_TEST 10000
STATIC_INLINE long long DiffLocalTime(struct timespec *startTime)
{
struct timespec endTime;
clock_gettime(CLOCK_MONOTONIC, &(endTime));
long long diff = (long long)((endTime.tv_sec - startTime->tv_sec) * 1000000); // 1000000 1000ms
if (endTime.tv_nsec > startTime->tv_nsec) {
diff += (endTime.tv_nsec - startTime->tv_nsec) / 1000;
diff += (endTime.tv_nsec - startTime->tv_nsec) / 1000; // 1000 1ms
} else {
diff -= (startTime->tv_nsec - endTime.tv_nsec) / 1000;
diff -= (startTime->tv_nsec - endTime.tv_nsec) / 1000; // 1000 1ms
}
return diff;
}
void TestReader()
static void TestPermissionCheck(const char *testParamName)
{
struct timespec startTime;
const int max = 10000;
const char *testParamName = "startup.appspawn.randrom.read";
(void)srand((unsigned)time(NULL));
const uint32_t buffSize = 1024;
char buffer[1024] = {0};
uint32_t size = buffSize;
int ret = SystemGetParameter(testParamName, buffer, &size);
int count = 0;
uint32_t spaceIndex = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
clock_gettime(CLOCK_MONOTONIC, &(startTime));
printf("TestReader total time %lld us %s \n", DiffLocalTime(&startTime), __FILE__);
printf("TestReader total time %lld us %s \n", DiffLocalTime(&startTime), testParamName);
ParamSecurityLabel *label = &(GetParamWorkSpace()->securityLabel);
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
size = buffSize;
ret = SystemReadParam(testParamName, buffer, &size);
count++;
}
printf("SystemReadParam total time %lld us \n", DiffLocalTime(&startTime));
printf("SystemReadParam total buffer %s \n", buffer);
count = 0;
WorkSpace *workspace = GetWorkSpaceByName(testParamName);
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
ParamTrieNode *entry = NULL;
ReadParamWithCheck(workspace, testParamName, DAC_READ, &entry);
count++;
}
printf("ReadParamWithCheck total time %lld us \n", DiffLocalTime(&startTime));
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
size = buffSize;
ret = SystemReadParam(testParamName, buffer, &size);
count++;
}
printf("SystemReadParam total time %lld us \n", DiffLocalTime(&startTime));
printf("SystemReadParam total buffer %s \n", buffer);
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
ParamTrieNode *entry = NULL;
ReadParamWithCheck(workspace, testParamName, DAC_READ, &entry);
count++;
}
printf("ReadParamWithCheck total time %lld us \n", DiffLocalTime(&startTime));
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
size = buffSize;
ret = SystemReadParam(testParamName, buffer, &size);
count++;
}
printf("SystemReadParam total time %lld us \n", DiffLocalTime(&startTime));
printf("SystemReadParam total buffer %s \n", buffer);
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
ParamTrieNode *entry = NULL;
ReadParamWithCheck(workspace, testParamName, DAC_READ, &entry);
count++;
}
printf("ReadParamWithCheck total time %lld us \n", DiffLocalTime(&startTime));
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
GetWorkSpaceByName(testParamName);
count++;
}
printf("GetWorkSpaceByName total time %lld us \n", DiffLocalTime(&startTime));
count = 0;
ParamLabelIndex labelIndex = {0};
labelIndex.workspace = GetWorkSpaceByName(testParamName);
labelIndex.selinuxLabelIndex = labelIndex.workspace->spaceIndex;
BaseFindTrieNode(labelIndex.workspace,
testParamName, strlen(testParamName), &labelIndex.dacLabelIndex);
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
for (int i = 0; i < MAX_TEST; ++i) {
CheckParamPermission_(&labelIndex, label, testParamName, DAC_READ);
count++;
}
printf("CheckParamPermission total time %lld us \n", DiffLocalTime(&startTime));
count = 0;
spaceIndex = labelIndex.selinuxLabelIndex;
label = &(GetParamWorkSpace()->securityLabel);
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
for (int i = 0; i < MAX_TEST; ++i) {
DacCheckParamPermission(&labelIndex, label, testParamName, DAC_READ);
count++;
}
printf("DacCheckParamPermission DAC total time %lld us \n", DiffLocalTime(&startTime));
#ifdef PARAM_SUPPORT_SELINUX
count = 0;
spaceIndex = GetWorkSpaceIndex(testParamName);
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
for (int i = 0; i < MAX_TEST; ++i) {
SelinuxCheckParamPermission(&labelIndex, label, testParamName, DAC_READ);
count++;
}
printf("CheckParamPermission selinux total time %lld us \n", DiffLocalTime(&startTime));
#endif
#if 1
count = 0;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
GetWorkSpaceByName(testParamName);
count++;
}
printf("GetWorkSpaceByName total time %lld us \n", DiffLocalTime(&startTime));
#endif
#if 1
count = 0;
ParamHandle handle = -1;
uint32_t index = 0;
WorkSpace *space = labelIndex.workspace;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
ParamTrieNode *node = BaseFindTrieNode(space, testParamName, strlen(testParamName), &index);
for (int i = 0; i < MAX_TEST; ++i) {
ParamTrieNode *node = BaseFindTrieNode(labelIndex.workspace, testParamName, strlen(testParamName), &index);
if (node != NULL && node->dataIndex != 0) {
handle = PARAM_HANDLE(space, node->dataIndex);
handle = PARAM_HANDLE(labelIndex.workspace, node->dataIndex);
}
count++;
}
printf("BaseFindTrieNode total time %lld us spaceIndex %u %s\n", DiffLocalTime(&startTime), spaceIndex, space->fileName);
printf("BaseFindTrieNode handle %x \n", handle);
#endif
printf("BaseFindTrieNode total time %lld us handle %x \n", DiffLocalTime(&startTime), handle);
#if 1
count = 0;
CachedHandle cacheHandle2 = CachedParameterCreate(testParamName, "true");
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
index = 0;
// get dac label
space = g_paramWorkSpace.workSpace[0];
(void)BaseFindTrieNode(space, testParamName, strlen(testParamName), &index);
GetTrieNode(space, index);
count++;
}
printf("BaseFindTrieNode dac time %lld us \n", DiffLocalTime(&startTime));
printf("BaseFindTrieNode index %u \n", index);
#endif
static CachedHandle cacheHandle = NULL;
if (cacheHandle == NULL) {
cacheHandle = CachedParameterCreate(testParamName, "true");
const char *value = NULL;
for (int i = 0; i < MAX_TEST; ++i) {
value = CachedParameterGet(cacheHandle2);
}
int result = 0;
count = 0;
CachedParameterDestroy(cacheHandle2);
printf("CachedParameterGet time %lld us value %s \n", DiffLocalTime(&startTime), value);
return;
}
void TestParameterReaderPerformance(void)
{
struct timespec startTime;
const char *testParamName = "persist.appspawn.randrom.read";
const uint32_t buffSize = 1024;
char buffer[1024] = {0};
uint32_t size = buffSize;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
while (count < max) {
CachedParameter *param = (CachedParameter *)cacheHandle;
if (param != NULL) {
long long spaceCommitId = ATOMIC_LOAD_EXPLICIT(&param->workspace->area->commitId, memory_order_acquire);
result = spaceCommitId == param->spaceCommitId;
if (param->dataIndex != 0) {
ParamNode *entry = (ParamNode *)GetTrieNode(param->workspace, param->dataIndex);
uint32_t dataCommitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, memory_order_acquire);
dataCommitId &= PARAM_FLAGS_COMMITID;
result = param->dataCommitId == dataCommitId;
}
printf("TestReader total time %lld us %s \n", DiffLocalTime(&startTime), testParamName);
for (int j = 0; j < 5; ++j) { // retry 5
clock_gettime(CLOCK_MONOTONIC, &(startTime));
for (int i = 0; i < MAX_TEST; ++i) {
size = buffSize;
SystemReadParam(testParamName, buffer, &size);
}
printf("SystemReadParam total time %lld us \n", DiffLocalTime(&startTime));
printf("SystemReadParam result %s \n", buffer);
WorkSpace *workspace = GetWorkSpaceByName(testParamName);
clock_gettime(CLOCK_MONOTONIC, &(startTime));
for (int i = 0; i < MAX_TEST; ++i) {
ParamTrieNode *entry = NULL;
ReadParamWithCheck(workspace, testParamName, DAC_READ, &entry);
}
count++;
printf("ReadParamWithCheck total time %lld us \n", DiffLocalTime(&startTime));
}
printf("CachedParameterGet time %lld us %d \n", DiffLocalTime(&startTime), result);
count = 0;
static CachedHandle cacheHandle2 = NULL;
if (cacheHandle2 == NULL) {
cacheHandle2 = CachedParameterCreate(testParamName, "true");
}
clock_gettime(CLOCK_MONOTONIC, &(startTime));
const char *value = NULL;
while (count < max) {
value = CachedParameterGet(cacheHandle2);
count++;
for (int i = 0; i < MAX_TEST; ++i) {
GetWorkSpaceByName(testParamName);
}
printf("CachedParameterGet time %lld us \n", DiffLocalTime(&startTime));
printf("CachedParameterGet index %s \n", value);
printf("GetWorkSpaceByName total time %lld us \n", DiffLocalTime(&startTime));
TestPermissionCheck(testParamName);
return;
}
#endif
\ No newline at end of file
......@@ -44,7 +44,9 @@ typedef struct CachedParameter_ {
char *paramValue;
char data[0];
} CachedParameter;
#ifdef PARAM_TEST_PERFORMANCE
void TestParameterReaderPerformance(void);
#endif
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -42,21 +42,27 @@ STATIC_INLINE int CompareParamTrieNode(const ParamTrieNode *node, const char *ke
} else if (node->length < keyLen) {
return 1;
}
return strncmp(node->key, key, keyLen);
return memcmp(node->key, key, keyLen);
}
STATIC_INLINE ParamTrieNode *FindSubTrie(const WorkSpace *workSpace,
ParamTrieNode *current, const char *key, uint32_t keyLen, uint32_t *matchLabel)
{
ParamTrieNode *subTrie = current;
int ret = 0;
while (subTrie != NULL) {
int ret = CompareParamTrieNode(subTrie, key, keyLen);
if (ret == 0) {
if (subTrie->labelIndex != 0) {
*matchLabel = subTrie->labelIndex;
if (subTrie->length > keyLen) {
ret = -1;
} else if (subTrie->length < keyLen) {
ret = 1;
} else {
ret = memcmp(subTrie->key, key, keyLen);
if (ret == 0) {
*matchLabel = (subTrie->labelIndex != 0) ? subTrie->labelIndex : *matchLabel;
return subTrie;
}
return subTrie;
}
uint32_t offset = 0;
if (ret < 0) {
offset = subTrie->left;
......
......@@ -286,7 +286,7 @@ INIT_LOCAL_API void SaveIndex(uint32_t *index, uint32_t offset)
INIT_LOCAL_API ParamTrieNode *FindTrieNode(WorkSpace *workSpace,
const char *key, uint32_t keyLen, uint32_t *matchLabel)
{
PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid key ");
PARAM_ONLY_CHECK(key != NULL && keyLen > 0, return NULL);
uint32_t tmpMatchLen = 0;
ParamTrieNode *node = NULL;
......
......@@ -103,7 +103,7 @@ typedef struct {
char *prefix;
} ParamTraversalContext;
#define PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24) | (index);
#define PARAM_HANDLE(workSpace, index) (ParamHandle)((workSpace)->spaceIndex << 24 | (index))
#define PARAM_GET_HANDLE_INFO(handle, label, index) \
do { \
(label) = (((handle) >> 24) & 0x000000ff); \
......
......@@ -60,32 +60,39 @@ extern "C" {
data size
strlen(xxxx.xxxx.xxxx.xxxx) + 96
*/
#if (defined __LITEOS_A__ || defined __LITEOS_M__)
#define DAC_DEFAULT_MODE 0777
#define DAC_DEFAULT_GROUP 0
#define DAC_DEFAULT_USER 0
#ifdef STARTUP_INIT_TEST
#define DAC_DEFAULT_MODE 0777
#define PARAM_WORKSPACE_DEF (1024 * 50)
#define PARAM_WORKSPACE_MAX (1024 * 50)
#define PARAM_WORKSPACE_SMALL PARAM_WORKSPACE_MAX
#define PARAM_WORKSPACE_DEF PARAM_WORKSPACE_MAX
#else
#ifdef __LITEOS_M__
#define DAC_DEFAULT_MODE 0777
#ifndef PARAM_WORKSPACE_MAX
#define PARAM_WORKSPACE_MAX (1024 * 5)
#endif
#endif
#define PARAM_WORKSPACE_SMALL PARAM_WORKSPACE_MAX
#define PARAM_WORKSPACE_DEF PARAM_WORKSPACE_MAX
#define DAC_DEFAULT_GROUP 0
#define DAC_DEFAULT_USER 0
#else
#define PARAM_WORKSPACE_MAX (80 * 1024)
#define PARAM_WORKSPACE_SMALL (1024 * 10)
#ifdef STARTUP_INIT_TEST
#else // __LITEOS_M__
#ifdef __LITEOS_A__
#define DAC_DEFAULT_MODE 0777
#define PARAM_WORKSPACE_DEF (1024 * 50)
#else
#define PARAM_WORKSPACE_MAX (1024 * 10)
#define PARAM_WORKSPACE_SMALL PARAM_WORKSPACE_MAX
#define PARAM_WORKSPACE_DEF PARAM_WORKSPACE_MAX
#else // __LITEOS_A__
#define DAC_DEFAULT_MODE 0774
#define PARAM_WORKSPACE_MAX (80 * 1024)
#define PARAM_WORKSPACE_SMALL (1024 * 10)
#define PARAM_WORKSPACE_DEF (1024 * 30)
#endif
#define DAC_DEFAULT_GROUP 0
#define DAC_DEFAULT_USER 0
#endif
#endif // __LITEOS_A__
#endif // __LITEOS_M__
#endif // STARTUP_INIT_TEST
// support futex
#ifndef __NR_futex
......
......@@ -95,7 +95,7 @@ INIT_LOCAL_API int InitWorkSpace(WorkSpace *workSpace, int onlyRead, uint32_t sp
INIT_LOCAL_API void CloseWorkSpace(WorkSpace *workSpace);
#define GetTrieNode(workSpace, offset) \
(ParamTrieNode *)((offset == 0 || offset > (workSpace)->area->dataSize) ? NULL : (workSpace)->area->data + offset)
(ParamTrieNode *)(((offset) == 0 || (offset) > (workSpace)->area->dataSize) ? NULL : (workSpace)->area->data + (offset))
#define GetTrieRoot(workSpace) \
(ParamTrieNode *)(((workSpace)->area == NULL) ? NULL : (workSpace)->area->data + (workSpace)->area->firstNode)
......
......@@ -39,12 +39,10 @@ __attribute__((constructor)) static void ParameterInit(void)
if (getpid() == 1) {
return;
}
EnableInitLog(INIT_INFO);
EnableInitLog(INIT_ERROR);
PARAM_WORKSPACE_OPS ops = {0};
ops.updaterMode = 0;
#ifdef PARAM_BASE_LOG
ops.logFunc = InitLog;
#endif
#ifdef PARAM_SUPPORT_SELINUX
ops.setfilecon = NULL;
#endif
......
......@@ -115,10 +115,6 @@ static_library("param_client_lite") {
defines = param_build_defines
public_configs = [ ":exported_header_files" ]
if (use_musl) {
defines += [ "PARAM_BASE" ]
}
if (ohos_kernel_type == "liteos_a") {
sources +=
[ "//base/startup/init/services/param/adapter/param_persistadp.c" ]
......
......@@ -52,7 +52,7 @@ static int CheckFilePermission(const ParamSecurityLabel *localLabel, const char
return 0;
}
INIT_LOCAL_API int DacCheckParamPermission(uint32_t index,
static int LiteDacCheckParamPermission(uint32_t index,
const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
{
UNUSED(index);
......@@ -74,7 +74,7 @@ INIT_LOCAL_API int RegisterSecurityDacOps(ParamSecurityOps *ops, int isInit)
ops->securityGetLabel = NULL;
ops->securityInitLabel = InitLocalSecurityLabel;
ops->securityCheckFilePermission = CheckFilePermission;
ops->securityCheckParamPermission = DacCheckParamPermission;
ops->securityCheckParamPermission = LiteDacCheckParamPermission;
ops->securityFreeLabel = FreeLocalSecurityLabel;
if (isInit) {
ops->securityGetLabel = DacGetParamSecurityLabel;
......
......@@ -121,87 +121,20 @@ int SystemWriteParam(const char *name, const char *value)
#define OS_DELAY 1000 // * 30 // 30s
#define STACK_SIZE 1024
typedef struct SysParaInfoItem_ {
char *infoName;
const char *(*getInfoValue)(void);
}SysParaInfoItem;
static const SysParaInfoItem SYSPARA_LIST[] = {
{(char *)"DeviceType", GetDeviceType},
{(char *)"Manufacture", GetManufacture},
{(char *)"Brand", GetBrand},
{(char *)"MarketName", GetMarketName},
{(char *)"ProductSeries", GetProductSeries},
{(char *)"ProductModel", GetProductModel},
{(char *)"SoftwareModel", GetSoftwareModel},
{(char *)"HardwareModel", GetHardwareModel},
{(char *)"Serial", GetSerial},
{(char *)"OSFullName", GetOSFullName},
{(char *)"DisplayVersion", GetDisplayVersion},
{(char *)"BootloaderVersion", GetBootloaderVersion},
{(char *)"GetSecurityPatchTag", GetSecurityPatchTag},
{(char *)"AbiList", GetAbiList},
{(char *)"IncrementalVersion", GetIncrementalVersion},
{(char *)"VersionId", GetVersionId},
{(char *)"BuildType", GetBuildType},
{(char *)"BuildUser", GetBuildUser},
{(char *)"BuildHost", GetBuildHost},
{(char *)"BuildTime", GetBuildTime},
{(char *)"BuildRootHash", GetBuildRootHash},
{(char *)"GetOsReleaseType", GetOsReleaseType},
{(char *)"GetHardwareProfile", GetHardwareProfile},
};
int32_t SysParaApiDumpCmd()
{
int index = 0;
int dumpInfoItemNum = (sizeof(SYSPARA_LIST) / sizeof(SYSPARA_LIST[0]));
const char *temp = NULL;
printf("Begin dump syspara\r\n");
printf("=======================\r\n");
while (index < dumpInfoItemNum) {
temp = SYSPARA_LIST[index].getInfoValue();
printf("%s:%s\r\n", SYSPARA_LIST[index].infoName, temp);
index++;
}
printf("FirstApiVersion:%d\r\n", GetFirstApiVersion());
printf("GetSerial:%s\r\n", GetSerial());
char udid[65] = {0};
GetDevUdid(udid, sizeof(udid));
printf("GetDevUdid:%s\r\n", udid);
printf("Version:%d.%d.%d.%d\r\n",
GetMajorVersion(), GetSeniorVersion(), GetFeatureVersion(), GetBuildVersion());
printf("GetSdkApiVersion:%d\r\n", GetSdkApiVersion());
printf("GetSystemCommitId:%lld\r\n", GetSystemCommitId());
printf("=======================\r\n");
printf("End dump syspara\r\n");
return 0;
}
static void ParamServiceTask(int *arg)
{
(void)arg;
PARAM_LOGI("ParamServiceTask start");
SysParaApiDumpCmd();
while (1) {
CheckAndSavePersistParam();
PARAM_LOGI("CheckAndSavePersistParam");
printf("CheckAndSavePersistParam \n");
osDelay(OS_DELAY);
}
}
void LiteParamService(void)
{
static init = 0;
if (init) {
printf("LiteParamService has been init \n");
return;
}
init = 1;
EnableInitLog(INIT_INFO);
printf("LiteParamService \n");
PARAM_LOGI("LiteParamService");
InitParamService();
// get persist param
LoadPersistParams();
......
......@@ -107,7 +107,10 @@ gid_t DecodeGid(const char *name)
return -1;
}
}
struct group *data = NULL;
struct group *data = getgrnam(name);
if (data != NULL) {
return data->gr_gid;
}
while ((data = getgrent()) != NULL) {
if ((data->gr_name != NULL) && (strcmp(data->gr_name, name) == 0)) {
gid = data->gr_gid;
......
......@@ -24,7 +24,7 @@ namespace OHOS {
CloseStdout();
std::string str(reinterpret_cast<const char*>(data), size);
int verbose = atoi(str.c_str());
SystemDumpParameters(verbose, nullptr);
SystemDumpParameters(verbose, -1, nullptr);
return true;
}
}
......
......@@ -25,6 +25,7 @@
#include "plugin_test.h"
#include "service_watcher.h"
#include "parameter.h"
#include "param_base.h"
#define MAX_THREAD_NUMBER 100
#define MAX_NUMBER 10
......@@ -41,7 +42,6 @@ static char *GetLocalBuffer(uint32_t *buffSize)
}
int g_stop = 0;
extern void TestReader();
void *CmdReader(void *args)
{
(void)srand((unsigned)time(NULL));
......@@ -63,11 +63,15 @@ void *CmdReader(void *args)
static int32_t BShellParamCmdRead(BShellHandle shell, int32_t argc, char *argv[])
{
TestReader();
PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter");
if (argc == 1) {
#ifdef PARAM_TEST_PERFORMANCE
TestParameterReaderPerformance();
#endif
return 0;
}
static pthread_t thread = 0;
PLUGIN_LOGV("BShellParamCmdWatch %s, threadId %d", argv[1], thread);
#if 0
if (strcmp(argv[1], "start") == 0) {
if (thread != 0) {
return 0;
......@@ -83,7 +87,6 @@ static int32_t BShellParamCmdRead(BShellHandle shell, int32_t argc, char *argv[]
pthread_join(thread, NULL);
thread = 0;
}
#endif
return 0;
}
......
......@@ -212,7 +212,7 @@ HWTEST_F(ClientUnitTest, TestClient_03, TestSize.Level0)
{
// 3 Traversal test
TestParamTraversal();
SystemDumpParameters(1, NULL);
SystemDumpParameters(1, -1, NULL);
}
HWTEST_F(ClientUnitTest, TestClient_04, TestSize.Level0)
......
......@@ -79,7 +79,7 @@ static const char *TestGetParamLabel(const char *paraName)
{
BEGET_LOGI("TestGetParamLabel %s", paraName);
if (paraName == nullptr) {
return 0;
return nullptr;
}
for (size_t i = 0; i < ARRAY_LENGTH(selinuxLabels); i++) {
if (strncmp(selinuxLabels[i][0], paraName, strlen(selinuxLabels[i][0])) == 0) {
......@@ -503,16 +503,13 @@ void PrepareInitUnitTestEnv(void)
InitAddPreCfgLoadHook(0, TestHook);
InitAddPostCfgLoadHook(0, TestHook);
InitAddPostPersistParamLoadHook(0, TestHook);
// ini system
SystemInit();
SystemPrepare();
SystemConfig();
#else
LoadSpecialParam();
#endif
// read default parameter from system
LoadDefaultParams("/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
LoadDefaultParams("/vendor/etc/param", LOAD_PARAM_NORMAL);
LoadDefaultParams("/system/etc/param", LOAD_PARAM_ONLY_ADD);
#endif
// read ut parameters
LoadDefaultParams(STARTUP_INIT_UT_PATH "/system/etc/param/ohos_const", LOAD_PARAM_NORMAL);
LoadDefaultParams(STARTUP_INIT_UT_PATH "/vendor/etc/param", LOAD_PARAM_NORMAL);
......@@ -716,15 +713,22 @@ int setfilecon(const char *name, const char *content)
ParamLabelIndex *TestGetParamLabelIndex(const char *name)
{
if (GetParamWorkSpace() == NULL && GetParamWorkSpace()->selinuxSpace.getParamLabelIndex == NULL) {
return NULL;
static ParamLabelIndex labelIndex = {0};
uint32_t index = 0;
ParamWorkSpace *paramWorkspace = GetParamWorkSpace();
if (paramWorkspace == nullptr) {
return nullptr;
}
uint32_t index = (uint32_t)GetParamWorkSpace()->selinuxSpace.getParamLabelIndex(name);
if (index >= GetParamWorkSpace()->maxLabelIndex) {
return NULL;
#ifdef PARAM_SUPPORT_SELINUX
if (paramWorkspace->selinuxSpace.getParamLabelIndex == nullptr) {
return nullptr;
}
static ParamLabelIndex labelIndex = {0};
WorkSpace *workspace = GetParamWorkSpace()->workSpace[index];
index = (uint32_t)paramWorkspace->selinuxSpace.getParamLabelIndex(name) + WORKSPACE_INDEX_BASE;
if (index >= paramWorkspace->maxLabelIndex) {
return nullptr;
}
#endif
WorkSpace *workspace = paramWorkspace->workSpace[index];
labelIndex.workspace = workspace;
PARAM_CHECK(labelIndex.workspace != NULL, return NULL, "Invalid workSpace");
labelIndex.selinuxLabelIndex = labelIndex.workspace->spaceIndex;
......
......@@ -298,7 +298,7 @@ public:
int TestDumpParamMemory()
{
SystemDumpParameters(1, NULL);
SystemDumpParameters(1, -1, NULL);
return 0;
}
};
......@@ -646,6 +646,7 @@ HWTEST_F(ParamUnitTest, TestParamCache, TestSize.Level0)
EXPECT_EQ(strcmp(value, "2222222"), 0);
CachedParameterDestroy(cacheHandle3);
}
#ifdef PARAM_SUPPORT_SELINUX
HWTEST_F(ParamUnitTest, TestInitParameterClient, TestSize.Level0)
{
InitParameterClient();
......@@ -659,4 +660,5 @@ HWTEST_F(ParamUnitTest, TestInitParameterClient, TestSize.Level0)
EXPECT_EQ(ret, 0);
}
#endif
#endif
}
......@@ -550,9 +550,11 @@ HWTEST_F(ParamServiceUnitTest, TestServiceCtrl, TestSize.Level0)
ParamServiceUnitTest test;
int ret = test.TestServiceCtrl("server1", 0770);
EXPECT_NE(ret, 0);
#ifdef PARAM_SUPPORT_SELINUX
// selinux forbid
ret = test.TestServiceCtrl("server2", 0772);
EXPECT_NE(ret, 0);
#endif
ret = 0;
}
......@@ -562,24 +564,32 @@ HWTEST_F(ParamServiceUnitTest, TestPowerCtrl, TestSize.Level0)
int ret = test.TestPowerCtrl("reboot,shutdown", 0770);
EXPECT_NE(ret, 0);
ret = test.TestPowerCtrl("reboot,shutdown", 0772);
#ifdef PARAM_SUPPORT_SELINUX
// selinux forbid
EXPECT_NE(ret, 0);
#endif
ret = test.TestPowerCtrl("reboot,updater", 0770);
EXPECT_NE(ret, 0);
ret = test.TestPowerCtrl("reboot,updater", 0772);
#ifdef PARAM_SUPPORT_SELINUX
// selinux forbid
EXPECT_NE(ret, 0);
#endif
ret = test.TestPowerCtrl("reboot,flashd", 0770);
EXPECT_NE(ret, 0);
ret = test.TestPowerCtrl("reboot,flashd", 0772);
#ifdef PARAM_SUPPORT_SELINUX
// selinux forbid
EXPECT_NE(ret, 0);
#endif
ret = test.TestPowerCtrl("reboot", 0770);
EXPECT_NE(ret, 0);
ret = test.TestPowerCtrl("reboot", 0772);
#ifdef PARAM_SUPPORT_SELINUX
// selinux forbid
EXPECT_NE(ret, 0);
#endif
ParamServiceStop();
}
......
......@@ -113,7 +113,7 @@ static void SetDeviceLable(const char *path)
continue;
}
if (gap < 0) { // end with '/'
break;
return;
}
if (memcpy_s(buffer, PATH_MAX, path, p - path - 1) != EOK) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册