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 27INIT_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 61INIT_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 69INIT_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 95static PropertyValueProcessor g_propertyGetProcessor = NULL; 96 97INIT_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 121INIT_LOCAL_API PropertyValueProcessor SetPropertyGetProcessor(PropertyValueProcessor processor) 122{ 123 PropertyValueProcessor prev = g_propertyGetProcessor; 124 g_propertyGetProcessor = processor; 125 return prev; 126} 127 128INIT_LOCAL_API const char *GetProductModel_(void) 129{ 130 static const char *productModel = NULL; 131 return GetProperty("const.product.model", &productModel); 132} 133 134INIT_LOCAL_API const char *GetManufacture_(void) 135{ 136 static const char *productManufacture = NULL; 137 return GetProperty("const.product.manufacturer", &productManufacture); 138} 139 140INIT_LOCAL_API const char *GetFullName_(void) 141{ 142 static const char *fillname = NULL; 143 return GetProperty("const.ohos.fullname", &fillname); 144} 145