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 
IsPrivateParam(const char * param)71 static bool IsPrivateParam(const char *param)
72 {
73     const char *privatePersistParams[] = {
74         "persist.sys.data.dataextpath",
75         "persist.sys.radio.vendorlib.path",
76         "persist.sys.default_ime",
77         "persist.hdc.daemon.cancel",
78         "persist.hdc.daemon.auth_result",
79         "persist.hdc.client.hostname",
80         "persist.hdc.client.pubkey_sha256",
81         "persist.kernel.bundle_name.clouddrive",
82         "persist.kernel.bundle_name.photos",
83         "persist.kernel.bundle_name.filemanager",
84     };
85     int size = sizeof(privatePersistParams) / sizeof(char*);
86     for (int i = 0; i < size; i++) {
87         if (strcmp(param, privatePersistParams[i]) == 0) {
88             return true;
89         }
90     }
91     return false;
92 }
93 
LoadOnePublicPersistParam_(const uint32_t * context,const char * name,const char * value)94 static int LoadOnePublicPersistParam_(const uint32_t *context, const char *name, const char *value)
95 {
96     if (IsPrivateParam(name)) {
97         PARAM_LOGI("%s is private, ignore", name);
98         return 0;
99     }
100     return LoadOnePersistParam_(context, name, value);
101 }
102 
LoadPersistParam_(const bool clearFactoryPersistParams,const char * fileName,char * buffer,uint32_t buffSize,bool isFullLoad)103 static void LoadPersistParam_(const bool clearFactoryPersistParams, const char *fileName,
104     char *buffer, uint32_t buffSize, bool isFullLoad)
105 {
106     FILE *fp = fopen(fileName, "r");
107     PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
108     int ret = 0;
109     uint32_t paramNum = 0;
110     while (fgets(buffer, buffSize, fp) != NULL) {
111         buffer[buffSize - 1] = '\0';
112         if (isFullLoad) {
113             ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, (uint32_t*)&clearFactoryPersistParams);
114         } else {
115             ret = SplitParamString(buffer, NULL, 0, LoadOnePublicPersistParam_, (uint32_t*)&clearFactoryPersistParams);
116         }
117         PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
118         paramNum++;
119     }
120     (void)fclose(fp);
121     PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
122 }
123 
GetPersistFilePath(char ** path,char ** tmpPath,int fileType)124 static bool GetPersistFilePath(char **path, char **tmpPath, int fileType)
125 {
126     bool isFullLoad = true;
127     if (InUpdaterMode() == 1) {
128         *path = "/param/persist_parameters";
129         *tmpPath = "/param/tmp_persist_paramters";
130         return isFullLoad;
131     }
132     if (fileType == PUBLIC_PERSIST_FILE) {
133         if (access(PARAM_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PUBLIC_PERSIST_SAVE_PATH, F_OK) != 0) {
134             int ret = rename(PARAM_PERSIST_SAVE_PATH, PARAM_PUBLIC_PERSIST_SAVE_PATH);
135             if (ret != 0) {
136                 PARAM_LOGE("rename failed %s", PARAM_PERSIST_SAVE_PATH);
137             }
138             isFullLoad = false;
139         } else {
140             CheckAndCreateDir(PARAM_PUBLIC_PERSIST_SAVE_PATH);
141             isFullLoad = false;
142         }
143         *path = PARAM_PUBLIC_PERSIST_SAVE_PATH;
144         *tmpPath = PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH;
145         return isFullLoad;
146     }
147     if (access(PARAM_OLD_PERSIST_SAVE_PATH, F_OK) == 0 && access(PARAM_PRIVATE_PERSIST_SAVE_PATH, F_OK) != 0) {
148         int ret = rename(PARAM_OLD_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH);
149         if (ret != 0) {
150             PARAM_LOGE("rename failed %s", PARAM_OLD_PERSIST_SAVE_PATH);
151         }
152     } else {
153         CheckAndCreateDir(PARAM_PRIVATE_PERSIST_SAVE_PATH);
154     }
155     *path = PARAM_PRIVATE_PERSIST_SAVE_PATH;
156     *tmpPath = PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH;
157     return isFullLoad;
158 }
159 
LoadPersistParam(int fileType)160 static int LoadPersistParam(int fileType)
161 {
162     bool clearFactoryPersistParams = false;
163     char value[PARAM_VALUE_LEN_MAX] = {0};
164     uint32_t len = PARAM_VALUE_LEN_MAX;
165     int ret = SystemReadParam("const.startup.param.version", value, &len);
166     if ((ret != 0 || strcmp(value, "1") != 0) &&
167         (access(PERSIST_PARAM_FIXED_FLAGS, F_OK) != 0)) {
168         clearFactoryPersistParams = true;
169     }
170     const uint32_t buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
171     char *buffer = calloc(1, buffSize);
172     PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
173 
174     char *tmpPath = "";
175     char *path = "";
176     bool isFullLoad = GetPersistFilePath(&path, &tmpPath, fileType);
177     LoadPersistParam_(clearFactoryPersistParams, path, buffer, buffSize, isFullLoad);
178     LoadPersistParam_(clearFactoryPersistParams, tmpPath, buffer, buffSize, isFullLoad);
179     free(buffer);
180     if (clearFactoryPersistParams) {
181         FILE *fp = fopen(PERSIST_PARAM_FIXED_FLAGS, "w");
182         PARAM_CHECK(fp != NULL, return -1, "create file %s fail error %d", PERSIST_PARAM_FIXED_FLAGS, errno);
183         (void)fclose(fp);
184     }
185     return 0;
186 }
187 
SavePersistParam(const char * name,const char * value)188 static int SavePersistParam(const char *name, const char *value)
189 {
190     ParamMutexPend(&g_saveMutex);
191     int ret = -1;
192     if (InUpdaterMode() == 1) {
193         char *path = "/param/persist_parameters";
194         FILE *fp = fopen(path, "a+");
195         if (fp != NULL) {
196             ret = fprintf(fp, "%s=%s\n", name, value);
197             (void)fclose(fp);
198         }
199         ParamMutexPost(&g_saveMutex);
200         return ret;
201     }
202     const char *path[PERSIST_HANDLE_MAX] = { PARAM_PUBLIC_PERSIST_SAVE_PATH, PARAM_PRIVATE_PERSIST_SAVE_PATH };
203     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
204         FILE *fp = fopen(path[i], "a+");
205         if (fp != NULL) {
206             ret = fprintf(fp, "%s=%s\n", name, value);
207             (void)fclose(fp);
208         }
209     }
210     ParamMutexPost(&g_saveMutex);
211     if (ret <= 0) {
212         PARAM_LOGE("Failed to save persist param %s", name);
213     }
214     return ret;
215 }
216 
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)217 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
218 {
219     ParamMutexPend(&g_saveMutex);
220     if (InUpdaterMode() == 1) {
221         char *path = "/param/tmp_persist_parameters";
222         unlink(path);
223         FILE *fp = fopen(path, "w");
224         if (fp == NULL) {
225             ParamMutexPost(&g_saveMutex);
226             PARAM_LOGE("Open file %s fail error %d", path, errno);
227             return -1;
228         }
229         handle[0] = (PERSIST_SAVE_HANDLE)fp;
230         return 0;
231     }
232     const char *path[PERSIST_HANDLE_MAX] = {
233         PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
234         PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
235     };
236     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
237         unlink(path[i]);
238         FILE *fp = fopen(path[i], "w");
239         if (fp == NULL) {
240             PARAM_LOGE("Open file %s fail error %d", path[i], errno);
241         } else {
242             handle[i] = (PERSIST_SAVE_HANDLE)fp;
243         }
244     }
245     return 0;
246 }
247 
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[],const char * name,const char * value)248 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle[], const char *name, const char *value)
249 {
250     int ret = 0;
251     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
252         FILE *fp = (FILE*)handle[i];
253         if (fp != NULL) {
254             ret = fprintf(fp, "%s=%s\n", name, value);
255             PARAM_CHECK(ret > 0, return -1, "Batchsavepersistparam fail, error %d", errno);
256         }
257     }
258     return (ret > 0) ? 0 : -1;
259 }
260 
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])261 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle[])
262 {
263     int ret = 0;
264     if (InUpdaterMode() == 1) {
265         FILE *fp = (FILE *)handle[0];
266         (void)fflush(fp);
267         (void)fsync(fileno(fp));
268         (void)fclose(fp);
269         unlink("/param/persist_parameters");
270         ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
271         ParamMutexPost(&g_saveMutex);
272         return;
273     }
274     const char *tmpPath[PERSIST_HANDLE_MAX] = {
275         PARAM_PUBLIC_PERSIST_SAVE_TMP_PATH,
276         PARAM_PRIVATE_PERSIST_SAVE_TMP_PATH
277     };
278     const char *path[PERSIST_HANDLE_MAX] = {
279         PARAM_PUBLIC_PERSIST_SAVE_PATH,
280         PARAM_PRIVATE_PERSIST_SAVE_PATH
281     };
282     for (int i = 0; i < PERSIST_HANDLE_MAX; i++) {
283         if (handle[i] != NULL) {
284             FILE *fp = (FILE *)handle[i];
285             (void)fflush(fp);
286             (void)fsync(fileno(fp));
287             (void)fclose(fp);
288         }
289         unlink(path[i]);
290         ret = rename(tmpPath[i], path[i]);
291     }
292     ParamMutexPost(&g_saveMutex);
293 }
294 
RegisterPersistParamOps(PersistParamOps * ops)295 int RegisterPersistParamOps(PersistParamOps *ops)
296 {
297     ParamMutexCreate(&g_saveMutex);
298     PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
299     ops->save = SavePersistParam;
300     ops->load = LoadPersistParam;
301     ops->batchSaveBegin = BatchSavePersistParamBegin;
302     ops->batchSave = BatchSavePersistParam;
303     ops->batchSaveEnd = BatchSavePersistParamEnd;
304     return 0;
305 }