Tim Yao | 72bc0fb | 2019-12-03 11:04:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <cutils/properties.h> |
| 18 | |
| 19 | #define LOG_TAG "properties" |
| 20 | // #define LOG_NDEBUG 0 |
| 21 | |
| 22 | #include <assert.h> |
| 23 | #include <ctype.h> |
| 24 | #include <errno.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | //#include <cutils/sockets.h> |
| 31 | #include <cutils/log.h> |
| 32 | |
| 33 | int8_t property_get_bool(const char *key, int8_t default_value) { |
| 34 | if (!key) { |
| 35 | return default_value; |
| 36 | } |
| 37 | |
| 38 | int8_t result = default_value; |
| 39 | char buf[PROPERTY_VALUE_MAX] = {'\0'}; |
| 40 | |
| 41 | int len = property_get(key, buf, ""); |
| 42 | if (len == 1) { |
| 43 | char ch = buf[0]; |
| 44 | if (ch == '0' || ch == 'n') { |
| 45 | result = false; |
| 46 | } else if (ch == '1' || ch == 'y') { |
| 47 | result = true; |
| 48 | } |
| 49 | } else if (len > 1) { |
| 50 | if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) { |
| 51 | result = false; |
| 52 | } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) { |
| 53 | result = true; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | // Convert string property to int (default if fails); return default value if out of bounds |
| 61 | static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound, |
| 62 | intmax_t default_value) { |
| 63 | if (!key) { |
| 64 | return default_value; |
| 65 | } |
| 66 | |
| 67 | intmax_t result = default_value; |
| 68 | char buf[PROPERTY_VALUE_MAX] = {'\0'}; |
| 69 | char *end = NULL; |
| 70 | |
| 71 | int len = property_get(key, buf, ""); |
| 72 | if (len > 0) { |
| 73 | int tmp = errno; |
| 74 | errno = 0; |
| 75 | |
| 76 | // Infer base automatically |
| 77 | result = strtoimax(buf, &end, /*base*/ 0); |
| 78 | if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) { |
| 79 | // Over or underflow |
| 80 | result = default_value; |
| 81 | ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value); |
| 82 | } else if (result < lower_bound || result > upper_bound) { |
| 83 | // Out of range of requested bounds |
| 84 | result = default_value; |
| 85 | ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value); |
| 86 | } else if (end == buf) { |
| 87 | // Numeric conversion failed |
| 88 | result = default_value; |
| 89 | ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", __FUNCTION__, key, |
| 90 | default_value); |
| 91 | } |
| 92 | |
| 93 | errno = tmp; |
| 94 | } |
| 95 | |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | int64_t property_get_int64(const char *key, int64_t default_value) { |
| 100 | return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value); |
| 101 | } |
| 102 | |
| 103 | int32_t property_get_int32(const char *key, int32_t default_value) { |
yuliang.hu | 3e3ab24 | 2024-07-29 14:52:31 +0800 | [diff] [blame^] | 104 | /*coverity[integer_overflow]: getenv is under control and will not overflow*/ |
Tim Yao | 72bc0fb | 2019-12-03 11:04:19 -0800 | [diff] [blame] | 105 | return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value); |
| 106 | } |
| 107 | |
| 108 | //#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 109 | //#include <sys/_system_properties.h> |
| 110 | |
| 111 | int property_set(const char *key, const char *value) { |
| 112 | //return __system_property_set(key, value); |
| 113 | return setenv(key, value, 1); |
| 114 | } |
| 115 | |
| 116 | int property_get(const char *key, char *value, const char *default_value) { |
| 117 | #if 0 |
| 118 | int len = __system_property_get(key, value); |
| 119 | if (len > 0) { |
| 120 | return len; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | int i, len; |
| 125 | char key_buf[PROPERTY_VALUE_MAX]; |
| 126 | const char *buf; |
| 127 | |
| 128 | /* convert '.' in property name to '_' */ |
| 129 | memset(key_buf, 0, sizeof(key_buf)); |
| 130 | strncpy(key_buf, key, PROPERTY_VALUE_MAX - 1); |
| 131 | len = strlen(key_buf); |
| 132 | for (i = 0; i < len; i++) { |
| 133 | if (key_buf[i] == '.') |
| 134 | key_buf[i] = '_'; |
| 135 | } |
| 136 | |
| 137 | len = 0; |
| 138 | buf = getenv(key_buf); |
| 139 | if (buf) { |
| 140 | len = strlen(strncpy(value, buf, PROPERTY_VALUE_MAX - 1)); |
| 141 | value[len] = '\0'; |
| 142 | return len; |
| 143 | } |
| 144 | |
| 145 | if (default_value) { |
| 146 | len = strnlen(default_value, PROPERTY_VALUE_MAX - 1); |
| 147 | memcpy(value, default_value, len); |
| 148 | value[len] = '\0'; |
| 149 | } |
| 150 | return len; |
| 151 | } |
| 152 | |
| 153 | #if 0 |
| 154 | struct callback_data { |
| 155 | void (*callback)(const char* name, const char* value, void* cookie); |
| 156 | void* cookie; |
| 157 | }; |
| 158 | |
| 159 | static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) { |
| 160 | callback_data* data = reinterpret_cast<callback_data*>(raw_data); |
| 161 | data->callback(name, value, data->cookie); |
| 162 | } |
| 163 | |
| 164 | static void property_list_callback(const prop_info* pi, void* data) { |
| 165 | __system_property_read_callback(pi, trampoline, data); |
| 166 | } |
| 167 | |
| 168 | int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) { |
| 169 | callback_data data = { fn, cookie }; |
| 170 | return __system_property_foreach(property_list_callback, &data); |
| 171 | } |
| 172 | #endif |
| 173 | |