The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -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 | |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 17 | #include <cutils/properties.h> |
| 18 | |
Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <inttypes.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 25 | #include <android-base/properties.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 27 | int8_t property_get_bool(const char* key, int8_t default_value) { |
| 28 | if (!key) return default_value; |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 29 | |
| 30 | int8_t result = default_value; |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 31 | char buf[PROPERTY_VALUE_MAX] = {}; |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 32 | |
| 33 | int len = property_get(key, buf, ""); |
| 34 | if (len == 1) { |
| 35 | char ch = buf[0]; |
| 36 | if (ch == '0' || ch == 'n') { |
| 37 | result = false; |
| 38 | } else if (ch == '1' || ch == 'y') { |
| 39 | result = true; |
| 40 | } |
| 41 | } else if (len > 1) { |
Myles Watson | 22c0962 | 2016-12-20 06:47:36 -0800 | [diff] [blame] | 42 | if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) { |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 43 | result = false; |
| 44 | } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) { |
| 45 | result = true; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return result; |
| 50 | } |
| 51 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 52 | template <typename T> |
| 53 | static T property_get_int(const char* key, T default_value) { |
| 54 | if (!key) return default_value; |
| 55 | |
| 56 | char value[PROPERTY_VALUE_MAX] = {}; |
| 57 | if (property_get(key, value, "") < 1) return default_value; |
| 58 | |
| 59 | // libcutils unwisely allows octal, which libbase doesn't. |
| 60 | T result = default_value; |
| 61 | int saved_errno = errno; |
| 62 | errno = 0; |
| 63 | char* end = nullptr; |
| 64 | intmax_t v = strtoimax(value, &end, 0); |
| 65 | if (errno != ERANGE && end != value && v >= std::numeric_limits<T>::min() && |
| 66 | v <= std::numeric_limits<T>::max()) { |
| 67 | result = v; |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 68 | } |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 69 | errno = saved_errno; |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 70 | return result; |
| 71 | } |
| 72 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 73 | int64_t property_get_int64(const char* key, int64_t default_value) { |
| 74 | return property_get_int<int64_t>(key, default_value); |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 75 | } |
| 76 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 77 | int32_t property_get_int32(const char* key, int32_t default_value) { |
| 78 | return property_get_int<int32_t>(key, default_value); |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 81 | int property_set(const char* key, const char* value) { |
Brad Fitzpatrick | eb1f0c6 | 2011-03-07 13:52:21 -0800 | [diff] [blame] | 82 | return __system_property_set(key, value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 83 | } |
| 84 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 85 | int property_get(const char* key, char* value, const char* default_value) { |
Elliott Hughes | 4eacd70 | 2017-01-26 17:31:40 -0800 | [diff] [blame] | 86 | int len = __system_property_get(key, value); |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 87 | if (len < 1 && default_value) { |
| 88 | snprintf(value, PROPERTY_VALUE_MAX, "%s", default_value); |
| 89 | return strlen(value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } |
| 91 | return len; |
| 92 | } |
| 93 | |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 94 | #if __has_include(<sys/system_properties.h>) |
| 95 | |
| 96 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 97 | #include <sys/_system_properties.h> |
| 98 | |
Elliott Hughes | 4eacd70 | 2017-01-26 17:31:40 -0800 | [diff] [blame] | 99 | struct callback_data { |
| 100 | void (*callback)(const char* name, const char* value, void* cookie); |
| 101 | void* cookie; |
Greg Hackmann | 6967935 | 2013-02-12 14:41:59 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Elliott Hughes | b30769a | 2017-02-10 19:02:51 -0800 | [diff] [blame] | 104 | static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) { |
Elliott Hughes | 4eacd70 | 2017-01-26 17:31:40 -0800 | [diff] [blame] | 105 | callback_data* data = reinterpret_cast<callback_data*>(raw_data); |
| 106 | data->callback(name, value, data->cookie); |
Greg Hackmann | 6967935 | 2013-02-12 14:41:59 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Elliott Hughes | 4eacd70 | 2017-01-26 17:31:40 -0800 | [diff] [blame] | 109 | static void property_list_callback(const prop_info* pi, void* data) { |
| 110 | __system_property_read_callback(pi, trampoline, data); |
| 111 | } |
| 112 | |
| 113 | int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) { |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 114 | callback_data data = {fn, cookie}; |
Greg Hackmann | 6967935 | 2013-02-12 14:41:59 -0800 | [diff] [blame] | 115 | return __system_property_foreach(property_list_callback, &data); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | } |
S Vasudev Prasad | 100b08a | 2020-05-08 11:45:45 +0530 | [diff] [blame] | 117 | |
| 118 | #endif |