blob: 864e2e41bf777fa1f6f3bb799a3212ea884d2d24 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +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"
18// #define LOG_NDEBUG 0
19
20#include <stdlib.h>
21#include <string.h>
22#include <ctype.h>
23#include <unistd.h>
24#include <cutils/sockets.h>
25#include <errno.h>
26#include <assert.h>
27
28#include <inttypes.h>
29#include <cutils/properties.h>
30#include <stdbool.h>
31#include <inttypes.h>
32#include "loghack.h"
33
34int8_t property_get_bool(const char *key, int8_t default_value) {
35 if (!key) {
36 return default_value;
37 }
38
39 int8_t result = default_value;
40 char buf[PROPERTY_VALUE_MAX] = {'\0',};
41
42 int len = property_get(key, buf, "");
43 if (len == 1) {
44 char ch = buf[0];
45 if (ch == '0' || ch == 'n') {
46 result = false;
47 } else if (ch == '1' || ch == 'y') {
48 result = true;
49 }
50 } else if (len > 1) {
51 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
52 result = false;
53 } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
54 result = true;
55 }
56 }
57
58 return result;
59}
60
61// Convert string property to int (default if fails); return default value if out of bounds
62static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
63 intmax_t default_value) {
64 if (!key) {
65 return default_value;
66 }
67
68 intmax_t result = default_value;
69 char buf[PROPERTY_VALUE_MAX] = {'\0',};
70 char *end = NULL;
71
72 int len = property_get(key, buf, "");
73 if (len > 0) {
74 int tmp = errno;
75 errno = 0;
76
77 // Infer base automatically
78 result = strtoimax(buf, &end, /*base*/0);
79 if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
80 // Over or underflow
81 result = default_value;
82 ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
83 } else if (result < lower_bound || result > upper_bound) {
84 // Out of range of requested bounds
85 result = default_value;
86 ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
87 } else if (end == buf) {
88 // Numeric conversion failed
89 result = default_value;
90 ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed",
91 __FUNCTION__, key, default_value);
92 }
93
94 errno = tmp;
95 }
96
97 return result;
98}
99
100int64_t property_get_int64(const char *key, int64_t default_value) {
101 return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
102}
103
104int32_t property_get_int32(const char *key, int32_t default_value) {
105 return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
106}
107
108#ifdef HAVE_LIBC_SYSTEM_PROPERTIES
109
110#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
111#include <sys/_system_properties.h>
112
113int property_set(const char *key, const char *value)
114{
115 return __system_property_set(key, value);
116}
117
118int property_get(const char *key, char *value, const char *default_value)
119{
120 int len;
121
122 len = __system_property_get(key, value);
123 if(len > 0) {
124 return len;
125 }
126 if(default_value) {
127 len = strlen(default_value);
128 if (len >= PROPERTY_VALUE_MAX) {
129 len = PROPERTY_VALUE_MAX - 1;
130 }
131 memcpy(value, default_value, len);
132 value[len] = '\0';
133 }
134 return len;
135}
136
137struct property_list_callback_data
138{
139 void (*propfn)(const char *key, const char *value, void *cookie);
140 void *cookie;
141};
142
143static void property_list_callback(const prop_info *pi, void *cookie)
144{
145 char name[PROP_NAME_MAX];
146 char value[PROP_VALUE_MAX];
147 struct property_list_callback_data *data = cookie;
148
149 __system_property_read(pi, name, value);
150 data->propfn(name, value, data->cookie);
151}
152
153int property_list(
154 void (*propfn)(const char *key, const char *value, void *cookie),
155 void *cookie)
156{
157 struct property_list_callback_data data = { propfn, cookie };
158 return __system_property_foreach(property_list_callback, &data);
159}
160
161#elif defined(HAVE_SYSTEM_PROPERTY_SERVER)
162
163/*
164 * The Linux simulator provides a "system property server" that uses IPC
165 * to set/get/list properties. The file descriptor is shared by all
166 * threads in the process, so we use a mutex to ensure that requests
167 * from multiple threads don't get interleaved.
168 */
169#include <stdio.h>
170#include <sys/types.h>
171#include <sys/socket.h>
172#include <sys/un.h>
173#include <pthread.h>
174
175static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT;
176static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER;
177static int gPropFd = -1;
178
179/*
180 * Connect to the properties server.
181 *
182 * Returns the socket descriptor on success.
183 */
184static int connectToServer(const char* fileName)
185{
186 int sock = -1;
187 int cc;
188
189 struct sockaddr_un addr;
190
191 sock = socket(AF_UNIX, SOCK_STREAM, 0);
192 if (sock < 0) {
193 ALOGW("UNIX domain socket create failed (errno=%d)\n", errno);
194 return -1;
195 }
196
197 /* connect to socket; fails if file doesn't exist */
198 strcpy(addr.sun_path, fileName); // max 108 bytes
199 addr.sun_family = AF_UNIX;
200 cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr));
201 if (cc < 0) {
202 // ENOENT means socket file doesn't exist
203 // ECONNREFUSED means socket exists but nobody is listening
204 //ALOGW("AF_UNIX connect failed for '%s': %s\n",
205 // fileName, strerror(errno));
206 close(sock);
207 return -1;
208 }
209
210 return sock;
211}
212
213/*
214 * Perform one-time initialization.
215 */
216static void init(void)
217{
218 assert(gPropFd == -1);
219
220 gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME);
221 if (gPropFd < 0) {
222 //ALOGW("not connected to system property server\n");
223 } else {
224 //ALOGV("Connected to system property server\n");
225 }
226}
227
228int property_get(const char *key, char *value, const char *default_value)
229{
230 //char sendBuf[1+PROPERTY_KEY_MAX];
231 //char recvBuf[1+PROPERTY_VALUE_MAX];
232 //int len = -1;
233//
234 ////ALOGV("PROPERTY GET [%s]\n", key);
235//
236 //pthread_once(&gInitOnce, init);
237 //if (gPropFd < 0) {
238 // /* this mimics the behavior of the device implementation */
239 // if (default_value != NULL) {
240 // strcpy(value, default_value);
241 // len = strlen(value);
242 // }
243 // return len;
244 //}
245//
246 //if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
247//
248 //memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
249//
250 //sendBuf[0] = (char) kSystemPropertyGet;
251 //strcpy(sendBuf+1, key);
252//
253 //pthread_mutex_lock(&gPropertyFdLock);
254 //if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
255 // pthread_mutex_unlock(&gPropertyFdLock);
256 // return -1;
257 //}
258 //if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
259 // pthread_mutex_unlock(&gPropertyFdLock);
260 // return -1;
261 //}
262 //pthread_mutex_unlock(&gPropertyFdLock);
263//
264 ///* first byte is 0 if value not defined, 1 if found */
265 //if (recvBuf[0] == 0) {
266 // if (default_value != NULL) {
267 // strcpy(value, default_value);
268 // len = strlen(value);
269 // } else {
270 // /*
271 // * If the value isn't defined, hand back an empty string and
272 // * a zero length, rather than a failure. This seems wrong,
273 // * since you can't tell the difference between "undefined" and
274 // * "defined but empty", but it's what the device does.
275 // */
276 // value[0] = '\0';
277 // len = 0;
278 // }
279 //} else if (recvBuf[0] == 1) {
280 // strcpy(value, recvBuf+1);
281 // len = strlen(value);
282 //} else {
283 // ALOGE("Got strange response to property_get request (%d)\n",
284 // recvBuf[0]);
285 // assert(0);
286 // return -1;
287 //}
288 ////ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n",
289 //// recvBuf[0], default_value, len, key, value);
290//
291 //return len;
292 return 0;
293}
294
295
296int property_set(const char *key, const char *value)
297{
298 //char sendBuf[1+PROPERTY_KEY_MAX+PROPERTY_VALUE_MAX];
299 //char recvBuf[1];
300 //int result = -1;
301//
302 ////ALOGV("PROPERTY SET [%s]: [%s]\n", key, value);
303//
304 //pthread_once(&gInitOnce, init);
305 //if (gPropFd < 0)
306 // return -1;
307//
308 //if (strlen(key) >= PROPERTY_KEY_MAX) return -1;
309 //if (strlen(value) >= PROPERTY_VALUE_MAX) return -1;
310//
311 //memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind
312//
313 //sendBuf[0] = (char) kSystemPropertySet;
314 //strcpy(sendBuf+1, key);
315 //strcpy(sendBuf+1+PROPERTY_KEY_MAX, value);
316//
317 //pthread_mutex_lock(&gPropertyFdLock);
318 //if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) {
319 // pthread_mutex_unlock(&gPropertyFdLock);
320 // return -1;
321 //}
322 //if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) {
323 // pthread_mutex_unlock(&gPropertyFdLock);
324 // return -1;
325 //}
326 //pthread_mutex_unlock(&gPropertyFdLock);
327//
328 //if (recvBuf[0] != 1)
329 // return -1;
330 return 0;
331}
332
333int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
334 void *cookie)
335{
336 //ALOGV("PROPERTY LIST\n");
337 //pthread_once(&gInitOnce, init);
338 //if (gPropFd < 0)
339 // return -1;
340
341 return 0;
342}
343
344#else
345
346/* SUPER-cheesy place-holder implementation for Win32 */
347
348//#include <cutils/threads.h>
349
350//static mutex_t env_lock = MUTEX_INITIALIZER;
351
352int property_get(const char *key, char *value, const char *default_value)
353{
354 //char ename[PROPERTY_KEY_MAX + 6];
355 //char *p;
356 //int len;
357 //
358 //len = strlen(key);
359 //if(len >= PROPERTY_KEY_MAX) return -1;
360 //memcpy(ename, "PROP_", 5);
361 //memcpy(ename + 5, key, len + 1);
362 //
363 //mutex_lock(&env_lock);
364//
365 //p = getenv(ename);
366 //if(p == 0) p = "";
367 //len = strlen(p);
368 //if(len >= PROPERTY_VALUE_MAX) {
369 // len = PROPERTY_VALUE_MAX - 1;
370 //}
371 //
372 //if((len == 0) && default_value) {
373 // len = strlen(default_value);
374 // memcpy(value, default_value, len + 1);
375 //} else {
376 // memcpy(value, p, len);
377 // value[len] = 0;
378 //}
379//
380 //mutex_unlock(&env_lock);
381
382 //return len;
383 return 0;
384}
385
386
387int property_set(const char *key, const char *value)
388{
389// char ename[PROPERTY_KEY_MAX + 6];
390// char *p;
391// int len;
392// int r;
393//
394// if(strlen(value) >= PROPERTY_VALUE_MAX) return -1;
395//
396// len = strlen(key);
397// if(len >= PROPERTY_KEY_MAX) return -1;
398// memcpy(ename, "PROP_", 5);
399// memcpy(ename + 5, key, len + 1);
400//
401// mutex_lock(&env_lock);
402//#ifdef HAVE_MS_C_RUNTIME
403// {
404// char temp[256];
405// snprintf( temp, sizeof(temp), "%s=%s", ename, value);
406// putenv(temp);
407// r = 0;
408// }
409//#else
410// r = setenv(ename, value, 1);
411//#endif
412// mutex_unlock(&env_lock);
413//
414// return r;
415 return 0;
416}
417
418int property_list(void (*propfn)(const char *key, const char *value, void *cookie),
419 void *cookie)
420{
421 return 0;
422}
423
424#endif