1 /*
2 * Copyright (c) 2021-2022 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 "param_comm.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "init_param.h"
22 #include "parameter.h"
23 #include "sysparam_errno.h"
24 #include "securec.h"
25 #include "beget_ext.h"
26
GetSystemError(int err)27 INIT_LOCAL_API int GetSystemError(int err)
28 {
29 switch (err) {
30 case 0:
31 return 0;
32 case PARAM_CODE_INVALID_PARAM:
33 case PARAM_CODE_INVALID_NAME:
34 case PARAM_CODE_READ_ONLY:
35 case PARAM_WORKSPACE_NOT_INIT:
36 case PARAM_WATCHER_CALLBACK_EXIST:
37 case PARAM_WATCHER_GET_SERVICE_FAILED:
38 return EC_INVALID;
39 case PARAM_CODE_INVALID_VALUE:
40 return SYSPARAM_INVALID_VALUE;
41 case PARAM_CODE_NOT_FOUND:
42 case PARAM_CODE_NODE_EXIST:
43 return SYSPARAM_NOT_FOUND;
44 case DAC_RESULT_FORBIDED:
45 case SELINUX_RESULT_FORBIDED:
46 return SYSPARAM_PERMISSION_DENIED;
47 case PARAM_CODE_REACHED_MAX:
48 case PARAM_CODE_FAIL_CONNECT:
49 case PARAM_CODE_NOT_SUPPORT:
50 case PARAM_CODE_IPC_ERROR:
51 case PARAM_CODE_MEMORY_MAP_FAILED:
52 case PARAM_CODE_MEMORY_NOT_ENOUGH:
53 return SYSPARAM_SYSTEM_ERROR;
54 case PARAM_CODE_TIMEOUT:
55 return SYSPARAM_WAIT_TIMEOUT;
56 default:
57 return SYSPARAM_SYSTEM_ERROR;
58 }
59 }
60
IsValidParamValue(const char * value,uint32_t len)61 INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len)
62 {
63 if ((value == NULL) || (strlen(value) + 1 > len)) {
64 return 0;
65 }
66 return 1;
67 }
68
GetParameter_(const char * key,const char * def,char * value,uint32_t len)69 INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t len)
70 {
71 if ((key == NULL) || (value == NULL) || (len > (uint32_t)PARAM_BUFFER_MAX)) {
72 return EC_INVALID;
73 }
74 uint32_t size = len;
75 int ret = SystemGetParameter(key, NULL, &size);
76 if (ret != 0) {
77 if (def == NULL) {
78 return GetSystemError(ret);
79 }
80 if (strlen(def) > len) {
81 return EC_INVALID;
82 }
83 ret = strcpy_s(value, len, def);
84 return (ret == 0) ? 0 : EC_FAILURE;
85 } else if (size > len) {
86 return EC_INVALID;
87 }
88
89 size = len;
90 ret = SystemGetParameter(key, value, &size);
91 BEGET_CHECK_ONLY_ELOG(ret == 0, "GetParameter_ failed! the errNum is: %d", ret);
92 return GetSystemError(ret);
93 }
94
95 static PropertyValueProcessor g_propertyGetProcessor = NULL;
96
GetProperty(const char * key,const char ** paramHolder)97 INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder)
98 {
99 BEGET_CHECK(paramHolder != NULL, return NULL);
100 if (*paramHolder != NULL) {
101 return *paramHolder;
102 }
103 uint32_t len = 0;
104 int ret = SystemGetParameter(key, NULL, &len);
105 if (ret == 0 && len > 0) {
106 char *res = (char *)calloc(1, len + 1);
107 BEGET_CHECK(res != NULL, return NULL);
108 ret = SystemGetParameter(key, res, &len);
109 if (ret != 0) {
110 free(res);
111 return NULL;
112 }
113 if (g_propertyGetProcessor != NULL) {
114 res = g_propertyGetProcessor(key, res);
115 }
116 *paramHolder = res;
117 }
118 return *paramHolder;
119 }
120
121 #ifndef OHOS_LITE
GetPropertyAtomic(const char * key,const char ** paramHolder)122 INIT_LOCAL_API const char *GetPropertyAtomic(const char *key, const char **paramHolder)
123 {
124 BEGET_CHECK(paramHolder != NULL, return NULL);
125 const char *_Atomic *atomicParam = (const char *_Atomic *)paramHolder;
126 const char *cached = atomic_load(atomicParam);
127 if (cached != NULL) {
128 return cached;
129 }
130
131 uint32_t len = 0;
132 int ret = SystemGetParameter(key, NULL, &len);
133 if (ret == 0 && len > 0) {
134 char *res = (char *)calloc(1, len + 1);
135 BEGET_CHECK(res != NULL, return NULL);
136
137 ret = SystemGetParameter(key, res, &len);
138 if (ret != 0) {
139 free(res);
140 return NULL;
141 }
142
143 if (g_propertyGetProcessor != NULL) {
144 res = g_propertyGetProcessor(key, res);
145 }
146
147 const char *expected = NULL;
148 if (!atomic_compare_exchange_strong(atomicParam, &expected, res)) {
149 free(res);
150 }
151 }
152 return atomic_load(atomicParam);
153 }
154 #endif
155
SetPropertyGetProcessor(PropertyValueProcessor processor)156 INIT_LOCAL_API PropertyValueProcessor SetPropertyGetProcessor(PropertyValueProcessor processor)
157 {
158 PropertyValueProcessor prev = g_propertyGetProcessor;
159 g_propertyGetProcessor = processor;
160 return prev;
161 }
162
GetProductModel_(void)163 INIT_LOCAL_API const char *GetProductModel_(void)
164 {
165 static const char *productModel = NULL;
166 return GetProperty("const.product.model", &productModel);
167 }
168
GetProductModelAlias_(void)169 INIT_LOCAL_API const char *GetProductModelAlias_(void)
170 {
171 static const char *productModelAlias = NULL;
172 const char *result = GetProperty("const.product.model_alias", &productModelAlias);
173 if (result == NULL) {
174 return GetProperty("const.product.model", &productModelAlias);
175 }
176 return result;
177 }
178
GetManufacture_(void)179 INIT_LOCAL_API const char *GetManufacture_(void)
180 {
181 static const char *productManufacture = NULL;
182 return GetProperty("const.product.manufacturer", &productManufacture);
183 }
184
GetFullName_(void)185 INIT_LOCAL_API const char *GetFullName_(void)
186 {
187 static const char *fillname = NULL;
188 return GetProperty("const.ohos.fullname", &fillname);
189 }
190