blob: 03f04961cd1ce005384a8366757d23371daa6fd2 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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 Hughes8e9aeb92017-11-10 10:22:07 -080017#include <cutils/properties.h>
18
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070019#include <errno.h>
20#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <stdlib.h>
22#include <string.h>
23#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053025#include <android-base/properties.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053027int8_t property_get_bool(const char* key, int8_t default_value) {
28 if (!key) return default_value;
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070029
30 int8_t result = default_value;
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053031 char buf[PROPERTY_VALUE_MAX] = {};
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070032
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 Watson22c09622016-12-20 06:47:36 -080042 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070043 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 Prasad100b08a2020-05-08 11:45:45 +053052template <typename T>
53static 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 Murashkind8f2a8d2014-04-08 11:22:42 -070068 }
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053069 errno = saved_errno;
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070070 return result;
71}
72
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053073int64_t property_get_int64(const char* key, int64_t default_value) {
74 return property_get_int<int64_t>(key, default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070075}
76
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053077int32_t property_get_int32(const char* key, int32_t default_value) {
78 return property_get_int<int32_t>(key, default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070079}
80
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053081int property_set(const char* key, const char* value) {
Brad Fitzpatrickeb1f0c62011-03-07 13:52:21 -080082 return __system_property_set(key, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083}
84
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053085int property_get(const char* key, char* value, const char* default_value) {
Elliott Hughes4eacd702017-01-26 17:31:40 -080086 int len = __system_property_get(key, value);
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053087 if (len < 1 && default_value) {
88 snprintf(value, PROPERTY_VALUE_MAX, "%s", default_value);
89 return strlen(value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090 }
91 return len;
92}
93
S Vasudev Prasad100b08a2020-05-08 11:45:45 +053094#if __has_include(<sys/system_properties.h>)
95
96#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
97#include <sys/_system_properties.h>
98
Elliott Hughes4eacd702017-01-26 17:31:40 -080099struct callback_data {
100 void (*callback)(const char* name, const char* value, void* cookie);
101 void* cookie;
Greg Hackmann69679352013-02-12 14:41:59 -0800102};
103
Elliott Hughesb30769a2017-02-10 19:02:51 -0800104static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) {
Elliott Hughes4eacd702017-01-26 17:31:40 -0800105 callback_data* data = reinterpret_cast<callback_data*>(raw_data);
106 data->callback(name, value, data->cookie);
Greg Hackmann69679352013-02-12 14:41:59 -0800107}
108
Elliott Hughes4eacd702017-01-26 17:31:40 -0800109static void property_list_callback(const prop_info* pi, void* data) {
110 __system_property_read_callback(pi, trampoline, data);
111}
112
113int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
S Vasudev Prasad100b08a2020-05-08 11:45:45 +0530114 callback_data data = {fn, cookie};
Greg Hackmann69679352013-02-12 14:41:59 -0800115 return __system_property_foreach(property_list_callback, &data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116}
S Vasudev Prasad100b08a2020-05-08 11:45:45 +0530117
118#endif