mirror of
https://gitee.com/openharmony/startup_init
synced 2025-01-31 00:34:11 +00:00
add safe persist file
Signed-off-by: cat <chenjinxiang3@huawei.com>
This commit is contained in:
parent
e9da3f93e8
commit
fd6f876f7d
@ -111,6 +111,13 @@ int LoadDefaultParams(const char *fileName, uint32_t mode);
|
||||
*/
|
||||
int LoadPersistParams(void);
|
||||
|
||||
/**
|
||||
* Init 接口
|
||||
* 加载加密目录下持久化参数。
|
||||
*
|
||||
*/
|
||||
int LoadPrivatePersistParams(void);
|
||||
|
||||
/**
|
||||
* Init 接口
|
||||
* 设置参数,主要用于其他进程使用,通过管道修改参数
|
||||
|
@ -131,6 +131,7 @@
|
||||
"mkdir /data/chipset/el1 0711 root root",
|
||||
"mkdir /data/chipset/el1/public 0711 root root",
|
||||
"mkdir /data/chipset/el2 0711 root root",
|
||||
"load_private_persist_params ",
|
||||
"init_main_user ",
|
||||
"mkdir /data/app/el1/0 0711 root root",
|
||||
"mkdir /data/app/el1/0/base 0711 root root",
|
||||
|
@ -185,6 +185,12 @@ static void DoLoadPersistParams(const struct CmdArgs *ctx)
|
||||
{
|
||||
INIT_LOGV("LoadPersistParams");
|
||||
LoadPersistParams();
|
||||
}
|
||||
|
||||
static void DoLoadPrivatePersistParams(const struct CmdArgs *ctx)
|
||||
{
|
||||
INIT_LOGV("LoadPersistParams");
|
||||
LoadPrivatePersistParams();
|
||||
HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, NULL, NULL);
|
||||
}
|
||||
|
||||
@ -575,6 +581,7 @@ static const struct CmdTable g_cmdTable[] = {
|
||||
{ "insmod ", 1, 10, 1, DoInsmod },
|
||||
{ "setparam ", 2, 2, 0, DoSetParam },
|
||||
{ "load_persist_params ", 0, 1, 0, DoLoadPersistParams },
|
||||
{ "load_private_persist_params ", 0, 1, 0, DoLoadPrivatePersistParams },
|
||||
{ "load_param ", 1, 2, 0, DoLoadDefaultParams },
|
||||
{ "load_access_token_id ", 0, 1, 0, DoLoadAccessTokenId },
|
||||
{ "ifup ", 1, 1, 1, DoIfup },
|
||||
|
@ -21,9 +21,6 @@
|
||||
#include "param_manager.h"
|
||||
#include "param_persist.h"
|
||||
#include "param_utils.h"
|
||||
#if !(defined __LITEOS_A__ || defined __LITEOS_M__)
|
||||
#include "trigger_manager.h"
|
||||
#endif
|
||||
|
||||
// for linux, no mutex
|
||||
static ParamMutex g_saveMutex = {};
|
||||
@ -56,16 +53,58 @@ static int LoadOnePersistParam_(const uint32_t *context, const char *name, const
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool IsPrivateParam(const char *param)
|
||||
{
|
||||
const char *privatePersistParams[] = {
|
||||
"persist.sys.data.dataextpath",
|
||||
"persist.sys.radio.vendorlib.path",
|
||||
"persist.sys.default_ime",
|
||||
"persist.sys.usb.config",
|
||||
"persist.hdc.daemon.cancel",
|
||||
"persist.hdc.daemon.auth_result",
|
||||
"persist.hdc.client.hostname",
|
||||
"persist.hdc.client.pubkey_sha256",
|
||||
"persist.kernel.bundle_name.clouddrive",
|
||||
"persist.kernel.bundle_name.photos",
|
||||
"persist.kernel.bundle_name.filemanager",
|
||||
};
|
||||
int size = sizeof(privatePersistParams) / sizeof(char*);
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (strcmp(param, privatePersistParams[i]) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int LoadOnePublicPersistParam_(const uint32_t *context, const char *name, const char *value)
|
||||
{
|
||||
if (IsPrivateParam(name)) {
|
||||
PARAM_LOGI("%s is private, ignore", name);
|
||||
return 0;
|
||||
}
|
||||
return LoadOnePersistParam_(context, name, value);
|
||||
}
|
||||
|
||||
static void LoadPersistParam_(const bool clearFactoryPersistParams, const char *fileName,
|
||||
char *buffer, uint32_t buffSize)
|
||||
{
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
|
||||
|
||||
bool isPublic = false;
|
||||
if (strcmp(fileName, PARAM_PUBLIC_PERSIST_SAVE_PATH) == 0 ||
|
||||
strcmp(fileName, PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH) == 0) {
|
||||
isPublic = true;
|
||||
}
|
||||
int ret = 0;
|
||||
uint32_t paramNum = 0;
|
||||
while (fgets(buffer, buffSize, fp) != NULL) {
|
||||
buffer[buffSize - 1] = '\0';
|
||||
int ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, (uint32_t*)&clearFactoryPersistParams);
|
||||
if (isPublic) {
|
||||
ret = SplitParamString(buffer, NULL, 0, LoadOnePublicPersistParam_, (uint32_t*)&clearFactoryPersistParams);
|
||||
} else {
|
||||
ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, (uint32_t*)&clearFactoryPersistParams);
|
||||
}
|
||||
PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
|
||||
paramNum++;
|
||||
}
|
||||
@ -73,7 +112,39 @@ static void LoadPersistParam_(const bool clearFactoryPersistParams, const char *
|
||||
PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
|
||||
}
|
||||
|
||||
static int LoadPersistParam(void)
|
||||
static void GetPersistFilePath(char **path, char **tmpPath, int fileType)
|
||||
{
|
||||
if (InUpdaterMode() == 1) {
|
||||
*path = "/param/persist_parameters";
|
||||
*tmpPath = "/param/tmp_persist_paramters";
|
||||
return;
|
||||
}
|
||||
if (fileType == PUBLIC_PERSIST_FILE) {
|
||||
if (access(PARAM_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PUBLIC_PERSIST_SAVE_PATH, F_OK) != 0) {
|
||||
int ret = rename(PARAM_PERSIST_SAVE_PATH, PARAM_PUBLIC_PERSIST_SAVE_PATH);
|
||||
if (ret != 0) {
|
||||
PARAM_LOGE("rename failed %s", PARAM_PERSIST_SAVE_PATH);
|
||||
}
|
||||
} else {
|
||||
CheckAndCreateDir(PARAM_PUBLIC_PERSIST_SAVE_PATH);
|
||||
}
|
||||
*path = PARAM_PUBLIC_PERSIST_SAVE_PATH;
|
||||
*tmpPath = PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH;
|
||||
} else {
|
||||
if (access(PARAM_OLD_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PRIVATE_PERSIST_SAVE_PATH, F_OK) != 0) {
|
||||
int ret = rename(PARAM_OLD_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH);
|
||||
if (ret != 0) {
|
||||
PARAM_LOGE("rename failed %s", PARAM_OLD_PERSIST_SAVE_PATH);
|
||||
}
|
||||
} else {
|
||||
CheckAndCreateDir(PARAM_PRIVATE_PERSIST_SAVE_PATH);
|
||||
}
|
||||
*path = PARAM_PRIVATE_PERSIST_SAVE_PATH;
|
||||
*tmpPath = PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH;
|
||||
}
|
||||
}
|
||||
|
||||
static int LoadPersistParam(int fileType)
|
||||
{
|
||||
CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
|
||||
bool clearFactoryPersistParams = false;
|
||||
@ -88,10 +159,10 @@ static int LoadPersistParam(void)
|
||||
char *buffer = malloc(buffSize);
|
||||
PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
|
||||
|
||||
int updaterMode = InUpdaterMode();
|
||||
char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
|
||||
LoadPersistParam_(clearFactoryPersistParams, tmpPath, buffer, buffSize);
|
||||
tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
|
||||
char *tmpPath = "";
|
||||
char *path = "";
|
||||
(void)GetPersistFilePath(&path, &tmpPath, fileType);
|
||||
LoadPersistParam_(clearFactoryPersistParams, path, buffer, buffSize);
|
||||
LoadPersistParam_(clearFactoryPersistParams, tmpPath, buffer, buffSize);
|
||||
free(buffer);
|
||||
if (clearFactoryPersistParams && access(PARAM_PERSIST_SAVE_PATH, F_OK) == 0) {
|
||||
@ -105,12 +176,24 @@ static int LoadPersistParam(void)
|
||||
static int SavePersistParam(const char *name, const char *value)
|
||||
{
|
||||
ParamMutexPend(&g_saveMutex);
|
||||
char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
|
||||
FILE *fp = fopen(path, "a+");
|
||||
int ret = -1;
|
||||
if (fp != NULL) {
|
||||
ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
(void)fclose(fp);
|
||||
if (InUpdaterMode() == 1) {
|
||||
char *path = "/param/persist_parameters";
|
||||
FILE *fp = fopen(path, "a+");
|
||||
if (fp != NULL) {
|
||||
ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
(void)fclose(fp);
|
||||
}
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
return ret;
|
||||
}
|
||||
const char *path[PERSIST_HANDLE_MAX] = { PARAM_PUBLIC_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH };
|
||||
for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
|
||||
FILE *fp = fopen(path[i], "a+");
|
||||
if (fp != NULL) {
|
||||
ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
(void)fclose(fp);
|
||||
}
|
||||
}
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
if (ret <= 0) {
|
||||
@ -122,42 +205,79 @@ static int SavePersistParam(const char *name, const char *value)
|
||||
static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
|
||||
{
|
||||
ParamMutexPend(&g_saveMutex);
|
||||
char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
|
||||
unlink(path);
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
PARAM_LOGE("Open file %s fail error %d", path, errno);
|
||||
return -1;
|
||||
if (InUpdaterMode() == 1) {
|
||||
char *path = "/param/tmp_persist_parameters";
|
||||
unlink(path);
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
PARAM_LOGE("Open file %s fail error %d", path, errno);
|
||||
return -1;
|
||||
}
|
||||
handle[0] = (PERSIST_SAVE_HANDLE)fp;
|
||||
return 0;
|
||||
}
|
||||
const char *path[PERSIST_HANDLE_MAX] = {
|
||||
PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
|
||||
PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
|
||||
};
|
||||
for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
|
||||
unlink(path[i]);
|
||||
FILE *fp = fopen(path[i], "w");
|
||||
if (fp == NULL) {
|
||||
PARAM_LOGE("Open file %s fail error %d", path[i], errno);
|
||||
} else {
|
||||
handle[i] = (PERSIST_SAVE_HANDLE)fp;
|
||||
}
|
||||
}
|
||||
*handle = (PERSIST_SAVE_HANDLE)fp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
|
||||
static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[], const char *name, const char *value)
|
||||
{
|
||||
FILE *fp = (FILE *)handle;
|
||||
int ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
|
||||
return (ret > 0) ? 0 : -1;
|
||||
int ret = 0;
|
||||
for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
|
||||
FILE *fp = (FILE*)handle[i];
|
||||
if (fp != NULL) {
|
||||
ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
PARAM_CHECK(ret > 0, return -1, "Batchsavepersistparam fail, error %d", errno);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
|
||||
static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])
|
||||
{
|
||||
int ret;
|
||||
FILE *fp = (FILE *)handle;
|
||||
(void)fflush(fp);
|
||||
(void)fsync(fileno(fp));
|
||||
(void)fclose(fp);
|
||||
if (InUpdaterMode() == 0) {
|
||||
unlink(PARAM_PERSIST_SAVE_PATH);
|
||||
ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
|
||||
} else {
|
||||
int ret = 0;
|
||||
if (InUpdaterMode() == 1) {
|
||||
FILE *fp = (FILE *)handle[0];
|
||||
(void)fflush(fp);
|
||||
(void)fsync(fileno(fp));
|
||||
(void)fclose(fp);
|
||||
unlink("/param/persist_parameters");
|
||||
ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
return;
|
||||
}
|
||||
const char *tmpPath[PERSIST_HANDLE_MAX] = {
|
||||
PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
|
||||
PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
|
||||
};
|
||||
const char *path[PERSIST_HANDLE_MAX] = {
|
||||
PARAM_PUBLIC_PERSIST_SAVE_PATH,
|
||||
PARAM_PRIVATE_PERSIST_SAVE_PATH
|
||||
};
|
||||
for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
|
||||
if (handle[i] != NULL) {
|
||||
FILE *fp = (FILE *)handle[i];
|
||||
(void)fflush(fp);
|
||||
(void)fsync(fileno(fp));
|
||||
(void)fclose(fp);
|
||||
}
|
||||
unlink(path[i]);
|
||||
ret = rename(tmpPath[i], path[i]);
|
||||
}
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
|
||||
}
|
||||
|
||||
int RegisterPersistParamOps(PersistParamOps *ops)
|
||||
|
@ -19,6 +19,8 @@
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "param_osadp.h"
|
||||
#define PUBLIC_PERSIST_FILE 0
|
||||
#define PRIVATE_PERSIST_FILE 1
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
@ -34,11 +36,18 @@ typedef struct {
|
||||
} PersistAdpContext;
|
||||
|
||||
typedef struct {
|
||||
#if defined(__LITEOS_M__) || defined(__LITEOS_A__) || defined(__LINUX__)
|
||||
int (*load)(void);
|
||||
int (*save)(const char *name, const char *value);
|
||||
int (*batchSaveBegin)(PERSIST_SAVE_HANDLE *handle);
|
||||
int (*batchSave)(PERSIST_SAVE_HANDLE handle, const char *name, const char *value);
|
||||
void (*batchSaveEnd)(PERSIST_SAVE_HANDLE handle);
|
||||
#else
|
||||
int (*load)(int fileType);
|
||||
int (*batchSaveBegin)(PERSIST_SAVE_HANDLE *handle);
|
||||
int (*batchSave)(PERSIST_SAVE_HANDLE handle[], const char *name, const char *value);
|
||||
void (*batchSaveEnd)(PERSIST_SAVE_HANDLE handle[]);
|
||||
#endif
|
||||
int (*save)(const char *name, const char *value);
|
||||
} PersistParamOps;
|
||||
|
||||
int RegisterPersistParamOps(PersistParamOps *ops);
|
||||
|
@ -81,6 +81,13 @@ typedef enum {
|
||||
#define PARAM_STORAGE_PATH STARTUP_INIT_UT_PATH "/dev/__parameters__"
|
||||
#define PARAM_PERSIST_SAVE_PATH DATA_PATH "persist_parameters"
|
||||
#define PARAM_PERSIST_SAVE_TMP_PATH DATA_PATH "tmp_persist_parameters"
|
||||
#define PRIVATE_DATA_PATH STARTUP_INIT_UT_PATH "/data/service/el1/public/startup/parameters/"
|
||||
#define PARAM_OLD_PERSIST_SAVE_PATH PRIVATE_DATA_PATH "persist_parameters"
|
||||
#define PARAM_PUBLIC_PERSIST_SAVE_PATH DATA_PATH "public_persist_parameters"
|
||||
#define PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH DATA_PATH "tmp_public_persist_parameters"
|
||||
#define PARAM_PRIVATE_PERSIST_SAVE_PATH PRIVATE_DATA_PATH "private_persist_parameters"
|
||||
#define PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH PRIVATE_DATA_PATH "tmp_private_persist_parameters"
|
||||
#define PERSIST_HANDLE_MAX 2
|
||||
|
||||
#define WORKSPACE_FLAGS_INIT 0x01
|
||||
#define WORKSPACE_FLAGS_LOADED 0x02
|
||||
|
@ -42,7 +42,6 @@ param_include_dirs = [
|
||||
]
|
||||
|
||||
param_service_sources = [
|
||||
"//base/startup/init/services/param/adapter/param_persistadp.c",
|
||||
"//base/startup/init/services/param/linux/param_message.c",
|
||||
"//base/startup/init/services/param/linux/param_msgadp.c",
|
||||
"//base/startup/init/services/param/linux/param_service.c",
|
||||
@ -69,6 +68,8 @@ if (defined(ohos_lite)) {
|
||||
defines = []
|
||||
sources = param_service_sources
|
||||
sources += param_trigger_sources
|
||||
sources +=
|
||||
[ "//base/startup/init/services/param/liteos/param_persistadp.c" ]
|
||||
include_dirs = param_include_dirs
|
||||
include_dirs += [
|
||||
"//base/security/selinux_adapter/interfaces/policycoreutils/include",
|
||||
@ -113,6 +114,8 @@ if (defined(ohos_lite)) {
|
||||
ohos_static_library("param_init") {
|
||||
sources = param_service_sources
|
||||
sources += param_trigger_sources
|
||||
sources +=
|
||||
[ "//base/startup/init/services/param/adapter/param_persistadp.c" ]
|
||||
include_dirs = param_include_dirs
|
||||
public_configs = [ ":exported_header_files" ]
|
||||
defines = [
|
||||
|
@ -91,7 +91,7 @@ static_library("param_init_lite") {
|
||||
|
||||
if (ohos_kernel_type == "liteos_a") {
|
||||
sources += [
|
||||
"//base/startup/init/services/param/adapter/param_persistadp.c",
|
||||
"//base/startup/init/services/param/liteos/param_persistadp.c",
|
||||
"//base/startup/init/services/param/liteos/param_service.c",
|
||||
"//base/startup/init/services/param/manager/param_persist.c",
|
||||
]
|
||||
@ -123,7 +123,7 @@ static_library("param_client_lite") {
|
||||
|
||||
if (ohos_kernel_type == "liteos_a") {
|
||||
sources +=
|
||||
[ "//base/startup/init/services/param/adapter/param_persistadp.c" ]
|
||||
[ "//base/startup/init/services/param/liteos/param_persistadp.c" ]
|
||||
defines += [
|
||||
"__LITEOS_A__",
|
||||
"WORKSPACE_AREA_NEED_MUTEX",
|
||||
|
135
services/param/liteos/param_persistadp.c
Normal file
135
services/param/liteos/param_persistadp.c
Normal file
@ -0,0 +1,135 @@
|
||||
/*
|
||||
* 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 <errno.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "init_utils.h"
|
||||
#include "param_manager.h"
|
||||
#include "param_persist.h"
|
||||
#include "param_utils.h"
|
||||
|
||||
// for linux, no mutex
|
||||
static ParamMutex g_saveMutex = {};
|
||||
|
||||
static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
|
||||
{
|
||||
UNUSED(context);
|
||||
uint32_t dataIndex = 0;
|
||||
return WriteParam(name, value, &dataIndex, 0);
|
||||
}
|
||||
|
||||
static void LoadPersistParam_(const char *fileName, char *buffer, uint32_t buffSize)
|
||||
{
|
||||
FILE *fp = fopen(fileName, "r");
|
||||
PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
|
||||
|
||||
uint32_t paramNum = 0;
|
||||
while (fgets(buffer, buffSize, fp) != NULL) {
|
||||
buffer[buffSize - 1] = '\0';
|
||||
int ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, NULL);
|
||||
PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
|
||||
paramNum++;
|
||||
}
|
||||
(void)fclose(fp);
|
||||
PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
|
||||
}
|
||||
|
||||
static int LoadPersistParam(void)
|
||||
{
|
||||
CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
|
||||
const uint32_t buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10; // 10 max len
|
||||
char *buffer = malloc(buffSize);
|
||||
PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
|
||||
|
||||
int updaterMode = InUpdaterMode();
|
||||
char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
|
||||
LoadPersistParam_(tmpPath, buffer, buffSize);
|
||||
tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
|
||||
LoadPersistParam_(tmpPath, buffer, buffSize);
|
||||
free(buffer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int SavePersistParam(const char *name, const char *value)
|
||||
{
|
||||
ParamMutexPend(&g_saveMutex);
|
||||
char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
|
||||
FILE *fp = fopen(path, "a+");
|
||||
int ret = -1;
|
||||
if (fp != NULL) {
|
||||
ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
(void)fclose(fp);
|
||||
}
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
if (ret <= 0) {
|
||||
PARAM_LOGE("Failed to save persist param %s", name);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
|
||||
{
|
||||
ParamMutexPend(&g_saveMutex);
|
||||
char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
|
||||
unlink(path);
|
||||
FILE *fp = fopen(path, "w");
|
||||
if (fp == NULL) {
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
PARAM_LOGE("Open file %s fail error %d", path, errno);
|
||||
return -1;
|
||||
}
|
||||
*handle = (PERSIST_SAVE_HANDLE)fp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
|
||||
{
|
||||
FILE *fp = (FILE *)handle;
|
||||
int ret = fprintf(fp, "%s=%s\n", name, value);
|
||||
PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
|
||||
return (ret > 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
|
||||
{
|
||||
int ret;
|
||||
FILE *fp = (FILE *)handle;
|
||||
(void)fflush(fp);
|
||||
(void)fsync(fileno(fp));
|
||||
(void)fclose(fp);
|
||||
if (InUpdaterMode() == 0) {
|
||||
unlink(PARAM_PERSIST_SAVE_PATH);
|
||||
ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
|
||||
} else {
|
||||
unlink("/param/persist_parameters");
|
||||
ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
|
||||
}
|
||||
ParamMutexPost(&g_saveMutex);
|
||||
PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
|
||||
}
|
||||
|
||||
int RegisterPersistParamOps(PersistParamOps *ops)
|
||||
{
|
||||
ParamMutexCreate(&g_saveMutex);
|
||||
PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
|
||||
ops->save = SavePersistParam;
|
||||
ops->load = LoadPersistParam;
|
||||
ops->batchSaveBegin = BatchSavePersistParamBegin;
|
||||
ops->batchSave = BatchSavePersistParam;
|
||||
ops->batchSaveEnd = BatchSavePersistParamEnd;
|
||||
return 0;
|
||||
}
|
@ -98,15 +98,20 @@ static int BatchSavePersistParam(void)
|
||||
g_persistWorkSpace.persistParamOps.batchSaveEnd == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(__LITEOS_M__) || defined(__LITEOS_A__) || defined(__LINUX__)
|
||||
PERSIST_SAVE_HANDLE handle;
|
||||
int ret = g_persistWorkSpace.persistParamOps.batchSaveBegin(&handle);
|
||||
PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Failed to save persist");
|
||||
#if defined(__LITEOS_M__) || defined(__LITEOS_A__)
|
||||
const char *prefix = "";
|
||||
#else
|
||||
const char *prefix = PARAM_PERSIST_PREFIX;
|
||||
#endif
|
||||
#else
|
||||
PERSIST_SAVE_HANDLE handle[2] = { 0 };
|
||||
int ret = g_persistWorkSpace.persistParamOps.batchSaveBegin(handle);
|
||||
const char *prefix = PARAM_PERSIST_PREFIX;
|
||||
#endif
|
||||
PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_NAME, "Failed to save persist");
|
||||
// walk and save persist param
|
||||
WorkSpace *workSpace = GetNextWorkSpace(NULL);
|
||||
while (workSpace != NULL) {
|
||||
@ -228,6 +233,7 @@ int LoadPersistParams(void)
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if defined(__LITEOS_M__) || defined(__LITEOS_A__) || defined(__LINUX__)
|
||||
if (g_persistWorkSpace.persistParamOps.load != NULL) {
|
||||
(void)g_persistWorkSpace.persistParamOps.load();
|
||||
PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED);
|
||||
@ -242,6 +248,39 @@ int LoadPersistParams(void)
|
||||
ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
|
||||
ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
if (g_persistWorkSpace.persistParamOps.load != NULL) {
|
||||
(void)g_persistWorkSpace.persistParamOps.load(PUBLIC_PERSIST_FILE);
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LoadPrivatePersistParams(void)
|
||||
{
|
||||
#if !(defined(__LITEOS_M__) || defined(__LITEOS_A__) || defined(__LINUX__))
|
||||
#ifndef STARTUP_INIT_TEST
|
||||
if (PARAM_TEST_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED)) {
|
||||
PARAM_LOGE("Persist param has been loaded");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
if (g_persistWorkSpace.persistParamOps.load != NULL) {
|
||||
(void)g_persistWorkSpace.persistParamOps.load(PRIVATE_PERSIST_FILE);
|
||||
PARAM_SET_FLAG(g_persistWorkSpace.flags, WORKSPACE_FLAGS_LOADED);
|
||||
}
|
||||
// save new persist param
|
||||
int ret = BatchSavePersistParam();
|
||||
PARAM_CHECK(ret == 0, return ret, "Failed to load persist param");
|
||||
// for liteos-a, start time to check in init
|
||||
#ifdef PARAM_SUPPORT_CYCLE_CHECK
|
||||
PARAM_LOGV("LoadPersistParams start check time ");
|
||||
if (g_persistWorkSpace.saveTimer == NULL) {
|
||||
ParamTimerCreate(&g_persistWorkSpace.saveTimer, TimerCallbackForSave, NULL);
|
||||
ParamTimerStart(g_persistWorkSpace.saveTimer, PARAM_MUST_SAVE_PARAM_DIFF * MS_UNIT, MS_UNIT);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
return 0;
|
||||
}
|
@ -139,10 +139,10 @@ if (defined(ohos_lite)) {
|
||||
]
|
||||
sources += init_common_sources
|
||||
sources += [
|
||||
"//base/startup/init/services/param/adapter/param_persistadp.c",
|
||||
"//base/startup/init/services/param/liteos/param_client.c",
|
||||
"//base/startup/init/services/param/liteos/param_litedac.c",
|
||||
"//base/startup/init/services/param/liteos/param_osadp.c",
|
||||
"//base/startup/init/services/param/liteos/param_persistadp.c",
|
||||
"//base/startup/init/services/param/liteos/param_service.c",
|
||||
]
|
||||
|
||||
@ -222,12 +222,12 @@ if (defined(ohos_lite)) {
|
||||
sources += init_common_sources
|
||||
sources += [
|
||||
"//base/startup/init/services/param/adapter/param_dac.c",
|
||||
"//base/startup/init/services/param/adapter/param_persistadp.c",
|
||||
"//base/startup/init/services/param/linux/param_message.c",
|
||||
"//base/startup/init/services/param/linux/param_msgadp.c",
|
||||
"//base/startup/init/services/param/linux/param_osadp.c",
|
||||
"//base/startup/init/services/param/linux/param_request.c",
|
||||
"//base/startup/init/services/param/linux/param_service.c",
|
||||
"//base/startup/init/services/param/liteos/param_persistadp.c",
|
||||
"//base/startup/init/services/param/trigger/trigger_checker.c",
|
||||
"//base/startup/init/services/param/trigger/trigger_manager.c",
|
||||
"//base/startup/init/services/param/trigger/trigger_processor.c",
|
||||
|
Loading…
x
Reference in New Issue
Block a user