• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 __LITEOFS_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 __LITEOFS_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.init", "persist.hmos_fusion_mgr.ctl.support_hmos",
84         "persist.kernel", "persist.location.locationhub_state",
85         "persist.mmitest.isrunning", "persist.moduleupdate.bms",
86         "persist.multimedia", "persist.nearlink.switch_enable",
87         "persist.odm", "persist.parentcontrol.enable",
88         "persist.radio", "persist.resourceschedule.memmgr.purgeable.enable",
89         "persist.ril", "persist.rosen",
90         "persist.samgr", "persist.security.jitfort.disabled",
91         "persist.super_privacy.mode", "persist.swing",
92         "persist.sys.beta.dump.shader", "persist.sys.font",
93         "persist.sys.graphic", "persist.sys.hilog",
94         "persist.sys.hiview", "persist.sys.nfc.rom.version",
95         "persist.sys.prefork.enable", "persist.sys.text.autospacing.enable",
96         "persist.sys.usb.config", "persist.sys.xlog.debug",
97         "persist.telephony", "persist.time",
98         "persist.uiAppearance.first_initialization", "persist.update",
99         "persist.wifi", "persist.init.trace.enabled",
100     };
101     int size = sizeof(publicPersistParams) / sizeof(char*);
102     for (int i = 0; i < size; i++) {
103         if (strncmp(param, publicPersistParams[i], strlen(publicPersistParams[i])) == 0) {
104             return true;
105         }
106     }
107     return false;
108 }
109 
LoadOnePublicPersistParam_(const uint32_t * context,const char * name,const char * value)110 static int LoadOnePublicPersistParam_(const uint32_t *context, const char *name, const char *value)
111 {
112     if (IsPublicParam(name)) {
113         return LoadOnePersistParam_(context, name, value);
114     }
115     PARAM_LOGI("%s is private, ignore", name);
116     return 0;
117 }
118 
LoadPersistParam_(const bool clearFactoryPersistParams,const char * fileName,char * buffer,uint32_t buffSize,bool isFullLoad)119 static void LoadPersistParam_(const bool clearFactoryPersistParams, const char *fileName,
120     char *buffer, uint32_t buffSize, bool isFullLoad)
121 {
122     FILE *fp = fopen(fileName, "r");
123     PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
124     int ret = 0;
125     uint32_t paramNum = 0;
126     while (fgets(buffer, buffSize, fp) != NULL) {
127         buffer[buffSize - 1] = '\0';
128         if (isFullLoad) {
129             ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, (uint32_t*)&clearFactoryPersistParams);
130         } else {
131             ret = SplitParamString(buffer, NULL, 0, LoadOnePublicPersistParam_, (uint32_t*)&clearFactoryPersistParams);
132         }
133         PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
134         paramNum++;
135     }
136     (void)fclose(fp);
137     PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
138 }
139 
GetPersistFilePath(char ** path,char ** tmpPath,int fileType)140 static bool GetPersistFilePath(char **path, char **tmpPath, int fileType)
141 {
142     bool isFullLoad = true;
143     if (InUpdaterMode() == 1) {
144         *path = "/param/persist_parameters";
145         *tmpPath = "/param/tmp_persist_paramters";
146         return isFullLoad;
147     }
148     if (fileType == PUBLIC_PERSIST_FILE) {
149         isFullLoad = false;
150         if (access(PARAM_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PUBLIC_PERSIST_SAVE_PATH, F_OK) != 0) {
151             int ret = rename(PARAM_PERSIST_SAVE_PATH, PARAM_PUBLIC_PERSIST_SAVE_PATH);
152             if (ret != 0) {
153                 PARAM_LOGE("rename failed %s", PARAM_PERSIST_SAVE_PATH);
154             }
155         } else {
156             CheckAndCreateDir(PARAM_PUBLIC_PERSIST_SAVE_PATH);
157         }
158         *path = PARAM_PUBLIC_PERSIST_SAVE_PATH;
159         *tmpPath = PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH;
160         return isFullLoad;
161     }
162     if (access(PARAM_OLD_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PRIVATE_PERSIST_SAVE_PATH, F_OK) != 0) {
163         int ret = rename(PARAM_OLD_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH);
164         if (ret != 0) {
165             PARAM_LOGE("rename failed %s", PARAM_OLD_PERSIST_SAVE_PATH);
166         }
167     } else {
168         CheckAndCreateDir(PARAM_PRIVATE_PERSIST_SAVE_PATH);
169     }
170     *path = PARAM_PRIVATE_PERSIST_SAVE_PATH;
171     *tmpPath = PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH;
172     return isFullLoad;
173 }
174 
LoadPersistParam(int fileType)175 static int LoadPersistParam(int fileType)
176 {
177     bool clearFactoryPersistParams = false;
178     char value[PARAM_VALUE_LEN_MAX] = {0};
179     uint32_t len = PARAM_VALUE_LEN_MAX;
180     int ret = SystemReadParam("const.startup.param.version", value, &len);
181     if ((ret != 0 || strcmp(value, "1") != 0) &&
182         (access(PERSIST_PARAM_FIXED_FLAGS, F_OK) != 0)) {
183         clearFactoryPersistParams = true;
184     }
185     const uint32_t buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
186     char *buffer = calloc(1, buffSize);
187     PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
188 
189     char *tmpPath = "";
190     char *path = "";
191     bool isFullLoad = GetPersistFilePath(&path, &tmpPath, fileType);
192     LoadPersistParam_(clearFactoryPersistParams, path, buffer, buffSize, isFullLoad);
193     LoadPersistParam_(clearFactoryPersistParams, tmpPath, buffer, buffSize, isFullLoad);
194     free(buffer);
195     if (clearFactoryPersistParams) {
196         FILE *fp = fopen(PERSIST_PARAM_FIXED_FLAGS, "w");
197         PARAM_CHECK(fp != NULL, return -1, "create file %s fail error %d", PERSIST_PARAM_FIXED_FLAGS, errno);
198         (void)fclose(fp);
199     }
200     return 0;
201 }
202 
SavePersistParam(const char * name,const char * value)203 static int SavePersistParam(const char *name, const char *value)
204 {
205     ParamMutexPend(&g_saveMutex);
206     int ret = -1;
207     if (InUpdaterMode() == 1) {
208         char *path = "/param/persist_parameters";
209         FILE *fp = fopen(path, "a+");
210         if (fp != NULL) {
211             ret = fprintf(fp, "%s=%s\n", name, value);
212             (void)fclose(fp);
213         }
214         ParamMutexPost(&g_saveMutex);
215         return ret;
216     }
217     const char *path[PERSIST_HANDLE_MAX] = { PARAM_PUBLIC_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH };
218     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
219         FILE *fp = fopen(path[i], "a+");
220         if (fp != NULL) {
221             ret = fprintf(fp, "%s=%s\n", name, value);
222             (void)fclose(fp);
223         }
224     }
225     ParamMutexPost(&g_saveMutex);
226     if (ret <= 0) {
227         PARAM_LOGE("Failed to save persist param %s", name);
228     }
229     return ret;
230 }
231 
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)232 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
233 {
234     ParamMutexPend(&g_saveMutex);
235     if (InUpdaterMode() == 1) {
236         char *path = "/param/tmp_persist_parameters";
237         unlink(path);
238         FILE *fp = fopen(path, "w");
239         if (fp == NULL) {
240             ParamMutexPost(&g_saveMutex);
241             PARAM_LOGE("Open file %s fail error %d", path, errno);
242             return -1;
243         }
244         handle[0] = (PERSIST_SAVE_HANDLE)fp;
245         return 0;
246     }
247     const char *path[PERSIST_HANDLE_MAX] = {
248         PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
249         PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
250     };
251     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
252         unlink(path[i]);
253         FILE *fp = fopen(path[i], "w");
254         if (fp == NULL) {
255             PARAM_LOGE("Open file %s fail error %d", path[i], errno);
256         } else {
257             handle[i] = (PERSIST_SAVE_HANDLE)fp;
258         }
259     }
260     return 0;
261 }
262 
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[],const char * name,const char * value)263 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[], const char *name, const char *value)
264 {
265     int ret = 0;
266     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
267         FILE *fp = (FILE*)handle[i];
268         if (fp != NULL) {
269             ret = fprintf(fp, "%s=%s\n", name, value);
270             PARAM_CHECK(ret > 0, return -1, "Batchsavepersistparam fail, error %d", errno);
271         }
272     }
273     return (ret > 0) ? 0 : -1;
274 }
275 
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])276 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])
277 {
278     int ret = 0;
279     if (InUpdaterMode() == 1) {
280         FILE *fp = (FILE *)handle[0];
281         (void)fflush(fp);
282         (void)fsync(fileno(fp));
283         (void)fclose(fp);
284         unlink("/param/persist_parameters");
285         ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
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         ret = rename(tmpPath[i], path[i]);
306     }
307     ParamMutexPost(&g_saveMutex);
308 }
309 
RegisterPersistParamOps(PersistParamOps * ops)310 int RegisterPersistParamOps(PersistParamOps *ops)
311 {
312     ParamMutexCreate(&g_saveMutex);
313     PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
314     ops->save = SavePersistParam;
315     ops->load = LoadPersistParam;
316     ops->batchSaveBegin = BatchSavePersistParamBegin;
317     ops->batchSave = BatchSavePersistParam;
318     ops->batchSaveEnd = BatchSavePersistParamEnd;
319     return 0;
320 }