blob: d2645e6408d326af65ead1b7b4083d254f44071c [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
17#define LOG_TAG "properties"
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070018// #define LOG_NDEBUG 0
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070020#include <assert.h>
21#include <ctype.h>
22#include <errno.h>
23#include <inttypes.h>
24#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028
29#include <cutils/properties.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070030#include <cutils/sockets.h>
Mark Salyzyn30f991f2017-01-10 13:19:54 -080031#include <log/log.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070033int8_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;
Myles Watson22c09622016-12-20 06:47:36 -080039 char buf[PROPERTY_VALUE_MAX] = {'\0'};
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070040
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) {
Myles Watson22c09622016-12-20 06:47:36 -080050 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070051 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
61static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
Myles Watson22c09622016-12-20 06:47:36 -080062 intmax_t default_value) {
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070063 if (!key) {
64 return default_value;
65 }
66
67 intmax_t result = default_value;
Myles Watson22c09622016-12-20 06:47:36 -080068 char buf[PROPERTY_VALUE_MAX] = {'\0'};
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070069 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
Myles Watson22c09622016-12-20 06:47:36 -080077 result = strtoimax(buf, &end, /*base*/ 0);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070078 if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
79 // Over or underflow
80 result = default_value;
Igor Murashkin1f7f70a2014-04-15 18:06:39 -070081 ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070082 } else if (result < lower_bound || result > upper_bound) {
83 // Out of range of requested bounds
84 result = default_value;
Igor Murashkin1f7f70a2014-04-15 18:06:39 -070085 ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070086 } else if (end == buf) {
87 // Numeric conversion failed
88 result = default_value;
Myles Watson22c09622016-12-20 06:47:36 -080089 ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", __FUNCTION__, key,
90 default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070091 }
92
93 errno = tmp;
94 }
95
96 return result;
97}
98
99int64_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
103int32_t property_get_int32(const char *key, int32_t default_value) {
104 return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
105}
106
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
108#include <sys/_system_properties.h>
109
Myles Watson22c09622016-12-20 06:47:36 -0800110int property_set(const char *key, const char *value) {
Brad Fitzpatrickeb1f0c62011-03-07 13:52:21 -0800111 return __system_property_set(key, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112}
113
Myles Watson22c09622016-12-20 06:47:36 -0800114int property_get(const char *key, char *value, const char *default_value) {
Elliott Hughes4eacd702017-01-26 17:31:40 -0800115 int len = __system_property_get(key, value);
Myles Watson22c09622016-12-20 06:47:36 -0800116 if (len > 0) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117 return len;
118 }
Myles Watson22c09622016-12-20 06:47:36 -0800119 if (default_value) {
Myles Watsone67abec2016-12-22 09:05:18 -0800120 len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -0700121 memcpy(value, default_value, len);
122 value[len] = '\0';
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123 }
124 return len;
125}
126
Elliott Hughes4eacd702017-01-26 17:31:40 -0800127struct callback_data {
128 void (*callback)(const char* name, const char* value, void* cookie);
129 void* cookie;
Greg Hackmann69679352013-02-12 14:41:59 -0800130};
131
Elliott Hughesb30769a2017-02-10 19:02:51 -0800132static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) {
Elliott Hughes4eacd702017-01-26 17:31:40 -0800133 callback_data* data = reinterpret_cast<callback_data*>(raw_data);
134 data->callback(name, value, data->cookie);
Greg Hackmann69679352013-02-12 14:41:59 -0800135}
136
Elliott Hughes4eacd702017-01-26 17:31:40 -0800137static void property_list_callback(const prop_info* pi, void* data) {
138 __system_property_read_callback(pi, trampoline, data);
139}
140
141int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
142 callback_data data = { fn, cookie };
Greg Hackmann69679352013-02-12 14:41:59 -0800143 return __system_property_foreach(property_list_callback, &data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144}