1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <time.h>
18 #include <unistd.h>
19
20 #include "init_utils.h"
21 #include "param_manager.h"
22 #include "param_persist.h"
23 #include "param_utils.h"
24 #if !(defined __LITEOS_A__ || defined __LITEOS_M__)
25 #include "trigger_manager.h"
26 #endif
27
28 // for linux, no mutex
29 static ParamMutex g_saveMutex = {};
30
LoadOnePersistParam_(const uint32_t * context,const char * name,const char * value)31 static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
32 {
33 if (strncmp(name, "persist", strlen("persist")) != 0) {
34 PARAM_LOGE("%s is not persist param, do not load", name);
35 return 0;
36 }
37 bool clearFactoryPersistParams = *(bool*)context;
38 uint32_t dataIndex = 0;
39 unsigned int mode = 0;
40 int result = 0;
41 do {
42 if (!clearFactoryPersistParams) {
43 mode |= LOAD_PARAM_PERSIST;
44 result = WriteParam(name, value, &dataIndex, mode);
45 break;
46 }
47
48 char persetValue[PARAM_VALUE_LEN_MAX] = {0};
49 uint32_t len = PARAM_VALUE_LEN_MAX;
50 int ret = SystemReadParam(name, persetValue, &len);
51 if (ret != 0) {
52 mode |= LOAD_PARAM_PERSIST;
53 result = WriteParam(name, value, &dataIndex, mode);
54 break;
55 }
56
57 if ((strcmp(persetValue, value) != 0)) {
58 PARAM_LOGI("%s value is different, preset value is:%s, persist value is:%s", name, persetValue, value);
59 mode |= LOAD_PARAM_PERSIST;
60 result = WriteParam(name, value, &dataIndex, mode);
61 }
62 } while (0);
63 #if !(defined __LITEOS_A__ || defined __LITEOS_M__)
64 if (result == 0) {
65 PostParamTrigger(EVENT_TRIGGER_PARAM_WATCH, name, value);
66 }
67 #endif
68 return result;
69 }
70
IsPublicParam(const char * param)71 static bool IsPublicParam(const char *param)
72 {
73 const char *publicPersistParams[] = {
74 "persist.accessory", "persist.account.login_name_max",
75 "persist.ace", "persist.arkui.libace.og",
76 "persist.bluetooth", "persist.bootster",
77 "persist.cota.update.opkey.version.enable", "persist.ddgr.opinctype",
78 "persist.dfx.leak.threshold", "persist.dupdate_engine.update_type",
79 "persist.edc", "persist.edm",
80 "persist.ffrt", "persist.filemanagement.param_watcher.on",
81 "persist.global", "persist.graphic.profiler",
82 "persist.hdc.jdwp", "persist.hiview",
83 "persist.kernel", "persist.location.locationhub_state",
84 "persist.mmitest.isrunning", "persist.moduleupdate.bms",
85 "persist.multimedia", "persist.nearlink.switch_enable",
86 "persist.odm", "persist.parentcontrol.enable",
87 "persist.radio", "persist.resourceschedule.memmgr.purgeable.enable",
88 "persist.ril", "persist.rosen",
89 "persist.samgr", "persist.security.jitfort.disabled",
90 "persist.super_privacy.mode", "persist.swing",
91 "persist.sys.beta.dump.shader", "persist.sys.font",
92 "persist.sys.graphic", "persist.sys.hilog",
93 "persist.sys.hiview", "persist.sys.nfc.rom.version",
94 "persist.sys.prefork.enable", "persist.sys.text.autospacing.enable",
95 "persist.sys.usb.config", "persist.sys.xlog.debug",
96 "persist.telephony", "persist.time",
97 "persist.uiAppearance.first_initialization", "persist.update",
98 "persist.wifi", "persist.init.trace.enabled",
99 };
100 int size = sizeof(publicPersistParams) / sizeof(char*);
101 for (int i = 0; i < size; i++) {
102 if (strncmp(param, publicPersistParams[i], strlen(publicPersistParams[i])) == 0) {
103 return true;
104 }
105 }
106 return false;
107 }
108
LoadOnePublicPersistParam_(const uint32_t * context,const char * name,const char * value)109 static int LoadOnePublicPersistParam_(const uint32_t *context, const char *name, const char *value)
110 {
111 if (IsPublicParam(name)) {
112 return LoadOnePersistParam_(context, name, value);
113 }
114 PARAM_LOGI("%s is private, ignore", name);
115 return 0;
116 }
117
LoadPersistParam_(const bool clearFactoryPersistParams,const char * fileName,char * buffer,uint32_t buffSize,bool isFullLoad)118 static void LoadPersistParam_(const bool clearFactoryPersistParams, const char *fileName,
119 char *buffer, uint32_t buffSize, bool isFullLoad)
120 {
121 FILE *fp = fopen(fileName, "r");
122 PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
123 int ret = 0;
124 uint32_t paramNum = 0;
125 while (fgets(buffer, buffSize, fp) != NULL) {
126 buffer[buffSize - 1] = '\0';
127 if (isFullLoad) {
128 ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, (uint32_t*)&clearFactoryPersistParams);
129 } else {
130 ret = SplitParamString(buffer, NULL, 0, LoadOnePublicPersistParam_, (uint32_t*)&clearFactoryPersistParams);
131 }
132 PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
133 paramNum++;
134 }
135 (void)fclose(fp);
136 PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
137 }
138
GetPersistFilePath(char ** path,char ** tmpPath,int fileType)139 static bool GetPersistFilePath(char **path, char **tmpPath, int fileType)
140 {
141 bool isFullLoad = true;
142 if (InUpdaterMode() == 1) {
143 *path = "/param/persist_parameters";
144 *tmpPath = "/param/tmp_persist_paramters";
145 return isFullLoad;
146 }
147 if (fileType == PUBLIC_PERSIST_FILE) {
148 isFullLoad = false;
149 if (access(PARAM_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PUBLIC_PERSIST_SAVE_PATH, F_OK) != 0) {
150 int ret = rename(PARAM_PERSIST_SAVE_PATH, PARAM_PUBLIC_PERSIST_SAVE_PATH);
151 if (ret != 0) {
152 PARAM_LOGE("rename failed %s", PARAM_PERSIST_SAVE_PATH);
153 }
154 } else {
155 CheckAndCreateDir(PARAM_PUBLIC_PERSIST_SAVE_PATH);
156 }
157 *path = PARAM_PUBLIC_PERSIST_SAVE_PATH;
158 *tmpPath = PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH;
159 return isFullLoad;
160 }
161 if (access(PARAM_OLD_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PRIVATE_PERSIST_SAVE_PATH, F_OK) != 0) {
162 int ret = rename(PARAM_OLD_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH);
163 if (ret != 0) {
164 PARAM_LOGE("rename failed %s", PARAM_OLD_PERSIST_SAVE_PATH);
165 }
166 } else {
167 CheckAndCreateDir(PARAM_PRIVATE_PERSIST_SAVE_PATH);
168 }
169 *path = PARAM_PRIVATE_PERSIST_SAVE_PATH;
170 *tmpPath = PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH;
171 return isFullLoad;
172 }
173
LoadPersistParam(int fileType)174 static int LoadPersistParam(int fileType)
175 {
176 bool clearFactoryPersistParams = false;
177 char value[PARAM_VALUE_LEN_MAX] = {0};
178 uint32_t len = PARAM_VALUE_LEN_MAX;
179 int ret = SystemReadParam("const.startup.param.version", value, &len);
180 if ((ret != 0 || strcmp(value, "1") != 0) &&
181 (access(PERSIST_PARAM_FIXED_FLAGS, F_OK) != 0)) {
182 clearFactoryPersistParams = true;
183 }
184 const uint32_t buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10; // 10 max len
185 char *buffer = calloc(1, buffSize);
186 PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
187
188 char *tmpPath = "";
189 char *path = "";
190 bool isFullLoad = GetPersistFilePath(&path, &tmpPath, fileType);
191 LoadPersistParam_(clearFactoryPersistParams, path, buffer, buffSize, isFullLoad);
192 LoadPersistParam_(clearFactoryPersistParams, tmpPath, buffer, buffSize, isFullLoad);
193 free(buffer);
194 if (clearFactoryPersistParams) {
195 FILE *fp = fopen(PERSIST_PARAM_FIXED_FLAGS, "w");
196 PARAM_CHECK(fp != NULL, return -1, "create file %s fail error %d", PERSIST_PARAM_FIXED_FLAGS, errno);
197 (void)fclose(fp);
198 }
199 return 0;
200 }
201
SavePersistParam(const char * name,const char * value)202 static int SavePersistParam(const char *name, const char *value)
203 {
204 ParamMutexPend(&g_saveMutex);
205 int ret = -1;
206 if (InUpdaterMode() == 1) {
207 char *path = "/param/persist_parameters";
208 FILE *fp = fopen(path, "a+");
209 if (fp != NULL) {
210 ret = fprintf(fp, "%s=%s\n", name, value);
211 (void)fclose(fp);
212 }
213 ParamMutexPost(&g_saveMutex);
214 return ret;
215 }
216 const char *path[PERSIST_HANDLE_MAX] = { PARAM_PUBLIC_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH };
217 for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
218 FILE *fp = fopen(path[i], "a+");
219 if (fp != NULL) {
220 ret = fprintf(fp, "%s=%s\n", name, value);
221 (void)fclose(fp);
222 }
223 }
224 ParamMutexPost(&g_saveMutex);
225 if (ret <= 0) {
226 PARAM_LOGE("Failed to save persist param %s", name);
227 }
228 return ret;
229 }
230
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)231 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
232 {
233 ParamMutexPend(&g_saveMutex);
234 if (InUpdaterMode() == 1) {
235 char *path = "/param/tmp_persist_parameters";
236 unlink(path);
237 FILE *fp = fopen(path, "w");
238 if (fp == NULL) {
239 ParamMutexPost(&g_saveMutex);
240 PARAM_LOGE("Open file %s fail error %d", path, errno);
241 return -1;
242 }
243 handle[0] = (PERSIST_SAVE_HANDLE)fp;
244 return 0;
245 }
246 const char *path[PERSIST_HANDLE_MAX] = {
247 PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
248 PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
249 };
250 for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
251 unlink(path[i]);
252 FILE *fp = fopen(path[i], "w");
253 if (fp == NULL) {
254 PARAM_LOGE("Open file %s fail error %d", path[i], errno);
255 } else {
256 handle[i] = (PERSIST_SAVE_HANDLE)fp;
257 }
258 }
259 return 0;
260 }
261
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[],const char * name,const char * value)262 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[], const char *name, const char *value)
263 {
264 int ret = 0;
265 for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
266 FILE *fp = (FILE*)handle[i];
267 if (fp != NULL) {
268 ret = fprintf(fp, "%s=%s\n", name, value);
269 PARAM_CHECK(ret > 0, return -1, "Batchsavepersistparam fail, error %d", errno);
270 }
271 }
272 return (ret > 0) ? 0 : -1;
273 }
274
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])275 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])
276 {
277 if (InUpdaterMode() == 1) {
278 FILE *fp = (FILE *)handle[0];
279 (void)fflush(fp);
280 (void)fsync(fileno(fp));
281 (void)fclose(fp);
282 unlink("/param/persist_parameters");
283 if (rename("/param/tmp_persist_parameters", "/param/persist_parameters")) {
284 PARAM_LOGW("rename file persist_parameters fail error %d", errno);
285 }
286 ParamMutexPost(&g_saveMutex);
287 return;
288 }
289 const char *tmpPath[PERSIST_HANDLE_MAX] = {
290 PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
291 PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
292 };
293 const char *path[PERSIST_HANDLE_MAX] = {
294 PARAM_PUBLIC_PERSIST_SAVE_PATH,
295 PARAM_PRIVATE_PERSIST_SAVE_PATH
296 };
297 for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
298 if (handle[i] != NULL) {
299 FILE *fp = (FILE *)handle[i];
300 (void)fflush(fp);
301 (void)fsync(fileno(fp));
302 (void)fclose(fp);
303 }
304 unlink(path[i]);
305 if (rename(tmpPath[i], path[i])) {
306 PARAM_LOGW("rename file %s fail error %d", path[i], errno);
307 }
308 }
309 ParamMutexPost(&g_saveMutex);
310 }
311
RegisterPersistParamOps(PersistParamOps * ops)312 int RegisterPersistParamOps(PersistParamOps *ops)
313 {
314 ParamMutexCreate(&g_saveMutex);
315 PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
316 ops->save = SavePersistParam;
317 ops->load = LoadPersistParam;
318 ops->batchSaveBegin = BatchSavePersistParamBegin;
319 ops->batchSave = BatchSavePersistParam;
320 ops->batchSaveEnd = BatchSavePersistParamEnd;
321 return 0;
322 }