blob: 1190ab74a2fdc9d23291fa43330745d6d97c3185 [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
20#include <stdlib.h>
21#include <string.h>
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070022#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023#include <unistd.h>
24#include <cutils/sockets.h>
25#include <errno.h>
26#include <assert.h>
27
28#include <cutils/properties.h>
Igor Murashkind8f2a8d2014-04-08 11:22:42 -070029#include <stdbool.h>
30#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031#include "loghack.h"
32
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;
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
61static 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;
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;
Igor Murashkin1f7f70a2014-04-15 18:06:39 -070089 ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed",
90 __FUNCTION__, key, 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
Elliott Hughesae521652015-01-12 13:56:34 -0800107#ifdef __BIONIC__
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108
109#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
110#include <sys/_system_properties.h>
111
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112int property_set(const char *key, const char *value)
113{
Brad Fitzpatrickeb1f0c62011-03-07 13:52:21 -0800114 return __system_property_set(key, value);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115}
116
117int property_get(const char *key, char *value, const char *default_value)
118{
119 int len;
120
121 len = __system_property_get(key, value);
122 if(len > 0) {
123 return len;
124 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 if(default_value) {
126 len = strlen(default_value);
Igor Murashkind8f2a8d2014-04-08 11:22:42 -0700127 if (len >= PROPERTY_VALUE_MAX) {
128 len = PROPERTY_VALUE_MAX - 1;
129 }
130 memcpy(value, default_value, len);
131 value[len] = '\0';
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132 }
133 return len;
134}
135
Greg Hackmann69679352013-02-12 14:41:59 -0800136struct property_list_callback_data
137{
138 void (*propfn)(const char *key, const char *value, void *cookie);
139 void *cookie;
140};
141
142static void property_list_callback(const prop_info *pi, void *cookie)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143{
144 char name[PROP_NAME_MAX];
145 char value[PROP_VALUE_MAX];
Greg Hackmann69679352013-02-12 14:41:59 -0800146 struct property_list_callback_data *data = cookie;
147
148 __system_property_read(pi, name, value);
149 data->propfn(name, value, data->cookie);
150}
151
152int property_list(
153 void (*propfn)(const char *key, const char *value, void *cookie),
154 void *cookie)
155{
156 struct property_list_callback_data data = { propfn, cookie };
157 return __system_property_foreach(property_list_callback, &data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158}
159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160#else
161
Elliott Hughesae521652015-01-12 13:56:34 -0800162/* SUPER-cheesy place-holder implementation for glibc/Mac OS/Windows. */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163
164#include <cutils/threads.h>
165
166static mutex_t env_lock = MUTEX_INITIALIZER;
167
168int property_get(const char *key, char *value, const char *default_value)
169{
170 char ename[PROPERTY_KEY_MAX + 6];
171 char *p;
172 int len;
173
174 len = strlen(key);
175 if(len >= PROPERTY_KEY_MAX) return -1;
176 memcpy(ename, "PROP_", 5);
177 memcpy(ename + 5, key, len + 1);
178
179 mutex_lock(&env_lock);
180
181 p = getenv(ename);
182 if(p == 0) p = "";
183 len = strlen(p);
184 if(len >= PROPERTY_VALUE_MAX) {
185 len = PROPERTY_VALUE_MAX - 1;
186 }
187
188 if((len == 0) && default_value) {
189 len = strlen(default_value);
190 memcpy(value, default_value, len + 1);
191 } else {
192 memcpy(value, p, len);
193 value[len] = 0;
194 }
195
196 mutex_unlock(&env_lock);
197
198 return len;
199}
200
201
202int property_set(const char *key, const char *value)
203{
204 char ename[PROPERTY_KEY_MAX + 6];
205 char *p;
206 int len;
207 int r;
208
209 if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
210
211 len = strlen(key);
212 if(len >= PROPERTY_KEY_MAX) return -1;
213 memcpy(ename, "PROP_", 5);
214 memcpy(ename + 5, key, len + 1);
215
216 mutex_lock(&env_lock);
217#ifdef HAVE_MS_C_RUNTIME
218 {
219 char temp[256];
220 snprintf( temp, sizeof(temp), "%s=%s", ename, value);
221 putenv(temp);
222 r = 0;
223 }
224#else
225 r = setenv(ename, value, 1);
226#endif
227 mutex_unlock(&env_lock);
228
229 return r;
230}
231
232int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
233 void *cookie)
234{
235 return 0;
236}
237
238#endif