rjw | 2e8229f | 2022-02-15 21:08:12 +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 | |
| 17 | #define LOG_TAG "properties" |
| 18 | // #define LOG_NDEBUG 0 |
| 19 | #define TRACE_TAG TRACE_ADB |
| 20 | |
| 21 | |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <ctype.h> |
| 25 | #include <unistd.h> |
| 26 | #include <cutils/sockets.h> |
| 27 | #include <errno.h> |
| 28 | #include <assert.h> |
| 29 | |
| 30 | #include <cutils/properties.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <inttypes.h> |
| 33 | #include "loghack.h" |
| 34 | |
| 35 | int8_t property_get_bool(const char *key, int8_t default_value) { |
| 36 | if (!key) { |
| 37 | return default_value; |
| 38 | } |
| 39 | |
| 40 | int8_t result = default_value; |
| 41 | char buf[PROPERTY_VALUE_MAX] = {'\0',}; |
| 42 | |
| 43 | int len = property_get(key, buf, ""); |
| 44 | if (len == 1) { |
| 45 | char ch = buf[0]; |
| 46 | if (ch == '0' || ch == 'n') { |
| 47 | result = false; |
| 48 | } else if (ch == '1' || ch == 'y') { |
| 49 | result = true; |
| 50 | } |
| 51 | } else if (len > 1) { |
| 52 | if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) { |
| 53 | result = false; |
| 54 | } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) { |
| 55 | result = true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return result; |
| 60 | } |
| 61 | |
| 62 | // Convert string property to int (default if fails); return default value if out of bounds |
| 63 | static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound, |
| 64 | intmax_t default_value) { |
| 65 | if (!key) { |
| 66 | return default_value; |
| 67 | } |
| 68 | |
| 69 | intmax_t result = default_value; |
| 70 | char buf[PROPERTY_VALUE_MAX] = {'\0',}; |
| 71 | char *end = NULL; |
| 72 | |
| 73 | int len = property_get(key, buf, ""); |
| 74 | if (len > 0) { |
| 75 | int tmp = errno; |
| 76 | errno = 0; |
| 77 | |
| 78 | // Infer base automatically |
| 79 | result = strtoimax(buf, &end, /*base*/0); |
| 80 | if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) { |
| 81 | // Over or underflow |
| 82 | result = default_value; |
| 83 | ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value); |
| 84 | } else if (result < lower_bound || result > upper_bound) { |
| 85 | // Out of range of requested bounds |
| 86 | result = default_value; |
| 87 | ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value); |
| 88 | } else if (end == buf) { |
| 89 | // Numeric conversion failed |
| 90 | result = default_value; |
| 91 | ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", |
| 92 | __FUNCTION__, key, default_value); |
| 93 | } |
| 94 | |
| 95 | errno = tmp; |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | int64_t property_get_int64(const char *key, int64_t default_value) { |
| 102 | return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value); |
| 103 | } |
| 104 | |
| 105 | int32_t property_get_int32(const char *key, int32_t default_value) { |
| 106 | return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value); |
| 107 | } |
| 108 | |
| 109 | #ifdef HAVE_LIBC_SYSTEM_PROPERTIES |
| 110 | |
| 111 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 112 | #include <sys/_system_properties.h> |
| 113 | |
| 114 | int property_set(const char *key, const char *value) |
| 115 | { |
| 116 | return __system_property_set(key, value); |
| 117 | } |
| 118 | |
| 119 | int property_get(const char *key, char *value, const char *default_value) |
| 120 | { |
| 121 | int len; |
| 122 | |
| 123 | len = __system_property_get(key, value); |
| 124 | if(len > 0) { |
| 125 | return len; |
| 126 | } |
| 127 | if(default_value) { |
| 128 | len = strlen(default_value); |
| 129 | if (len >= PROPERTY_VALUE_MAX) { |
| 130 | len = PROPERTY_VALUE_MAX - 1; |
| 131 | } |
| 132 | memcpy(value, default_value, len); |
| 133 | value[len] = '\0'; |
| 134 | } |
| 135 | return len; |
| 136 | } |
| 137 | |
| 138 | struct property_list_callback_data |
| 139 | { |
| 140 | void (*propfn)(const char *key, const char *value, void *cookie); |
| 141 | void *cookie; |
| 142 | }; |
| 143 | |
| 144 | static void property_list_callback(const prop_info *pi, void *cookie) |
| 145 | { |
| 146 | char name[PROP_NAME_MAX]; |
| 147 | char value[PROP_VALUE_MAX]; |
| 148 | struct property_list_callback_data *data = cookie; |
| 149 | |
| 150 | __system_property_read(pi, name, value); |
| 151 | data->propfn(name, value, data->cookie); |
| 152 | } |
| 153 | |
| 154 | int property_list( |
| 155 | void (*propfn)(const char *key, const char *value, void *cookie), |
| 156 | void *cookie) |
| 157 | { |
| 158 | struct property_list_callback_data data = { propfn, cookie }; |
| 159 | return __system_property_foreach(property_list_callback, &data); |
| 160 | } |
| 161 | |
| 162 | #elif defined(HAVE_SYSTEM_PROPERTY_SERVER) |
| 163 | |
| 164 | /* |
| 165 | * The Linux simulator provides a "system property server" that uses IPC |
| 166 | * to set/get/list properties. The file descriptor is shared by all |
| 167 | * threads in the process, so we use a mutex to ensure that requests |
| 168 | * from multiple threads don't get interleaved. |
| 169 | */ |
| 170 | #include <stdio.h> |
| 171 | #include <sys/types.h> |
| 172 | #include <sys/socket.h> |
| 173 | #include <sys/un.h> |
| 174 | #include <pthread.h> |
| 175 | |
| 176 | static pthread_once_t gInitOnce = PTHREAD_ONCE_INIT; |
| 177 | static pthread_mutex_t gPropertyFdLock = PTHREAD_MUTEX_INITIALIZER; |
| 178 | static int gPropFd = -1; |
| 179 | |
| 180 | /* |
| 181 | * Connect to the properties server. |
| 182 | * |
| 183 | * Returns the socket descriptor on success. |
| 184 | */ |
| 185 | static int connectToServer(const char* fileName) |
| 186 | { |
| 187 | int sock = -1; |
| 188 | int cc; |
| 189 | |
| 190 | struct sockaddr_un addr; |
| 191 | |
| 192 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 193 | if (sock < 0) { |
| 194 | ALOGW("UNIX domain socket create failed (errno=%d)\n", errno); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | /* connect to socket; fails if file doesn't exist */ |
| 199 | strcpy(addr.sun_path, fileName); // max 108 bytes |
| 200 | addr.sun_family = AF_UNIX; |
| 201 | cc = connect(sock, (struct sockaddr*) &addr, SUN_LEN(&addr)); |
| 202 | if (cc < 0) { |
| 203 | // ENOENT means socket file doesn't exist |
| 204 | // ECONNREFUSED means socket exists but nobody is listening |
| 205 | //ALOGW("AF_UNIX connect failed for '%s': %s\n", |
| 206 | // fileName, strerror(errno)); |
| 207 | close(sock); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | return sock; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Perform one-time initialization. |
| 216 | */ |
| 217 | static void init(void) |
| 218 | { |
| 219 | assert(gPropFd == -1); |
| 220 | |
| 221 | gPropFd = connectToServer(SYSTEM_PROPERTY_PIPE_NAME); |
| 222 | if (gPropFd < 0) { |
| 223 | //ALOGW("not connected to system property server\n"); |
| 224 | } else { |
| 225 | //ALOGV("Connected to system property server\n"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | int property_get(const char *key, char *value, const char *default_value) |
| 230 | { |
| 231 | char sendBuf[1+PROPERTY_KEY_MAX]; |
| 232 | char recvBuf[1+PROPERTY_VALUE_MAX]; |
| 233 | int len = -1; |
| 234 | |
| 235 | //ALOGV("PROPERTY GET [%s]\n", key); |
| 236 | |
| 237 | pthread_once(&gInitOnce, init); |
| 238 | if (gPropFd < 0) { |
| 239 | /* this mimics the behavior of the device implementation */ |
| 240 | if (default_value != NULL) { |
| 241 | strcpy(value, default_value); |
| 242 | len = strlen(value); |
| 243 | } |
| 244 | return len; |
| 245 | } |
| 246 | |
| 247 | if (strlen(key) >= PROPERTY_KEY_MAX) return -1; |
| 248 | |
| 249 | memset(sendBuf, 0xdd, sizeof(sendBuf)); // placate valgrind |
| 250 | |
| 251 | sendBuf[0] = (char) kSystemPropertyGet; |
| 252 | strcpy(sendBuf+1, key); |
| 253 | |
| 254 | pthread_mutex_lock(&gPropertyFdLock); |
| 255 | if (write(gPropFd, sendBuf, sizeof(sendBuf)) != sizeof(sendBuf)) { |
| 256 | pthread_mutex_unlock(&gPropertyFdLock); |
| 257 | return -1; |
| 258 | } |
| 259 | if (read(gPropFd, recvBuf, sizeof(recvBuf)) != sizeof(recvBuf)) { |
| 260 | pthread_mutex_unlock(&gPropertyFdLock); |
| 261 | return -1; |
| 262 | } |
| 263 | pthread_mutex_unlock(&gPropertyFdLock); |
| 264 | |
| 265 | /* first byte is 0 if value not defined, 1 if found */ |
| 266 | if (recvBuf[0] == 0) { |
| 267 | if (default_value != NULL) { |
| 268 | strcpy(value, default_value); |
| 269 | len = strlen(value); |
| 270 | } else { |
| 271 | /* |
| 272 | * If the value isn't defined, hand back an empty string and |
| 273 | * a zero length, rather than a failure. This seems wrong, |
| 274 | * since you can't tell the difference between "undefined" and |
| 275 | * "defined but empty", but it's what the device does. |
| 276 | */ |
| 277 | value[0] = '\0'; |
| 278 | len = 0; |
| 279 | } |
| 280 | } else if (recvBuf[0] == 1) { |
| 281 | strcpy(value, recvBuf+1); |
| 282 | len = strlen(value); |
| 283 | } else { |
| 284 | ALOGE("Got strange response to property_get request (%d)\n", |
| 285 | recvBuf[0]); |
| 286 | assert(0); |
| 287 | return -1; |
| 288 | } |
| 289 | //ALOGV("PROP [found=%d def='%s'] (%d) [%s]: [%s]\n", |
| 290 | // recvBuf[0], default_value, len, key, value); |
| 291 | |
| 292 | return len; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | int 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 | |
| 333 | int 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 | #elif defined(ADB_OVER_PCIE) |
| 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 | |
| 352 | int 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 | } |
| 384 | |
| 385 | |
| 386 | int property_set(const char *key, const char *value) |
| 387 | { |
| 388 | char ename[PROPERTY_KEY_MAX + 6]; |
| 389 | char *p; |
| 390 | int len; |
| 391 | int r; |
| 392 | |
| 393 | if(strlen(value) >= PROPERTY_VALUE_MAX) return -1; |
| 394 | |
| 395 | len = strlen(key); |
| 396 | if(len >= PROPERTY_KEY_MAX) return -1; |
| 397 | memcpy(ename, "PROP_", 5); |
| 398 | memcpy(ename + 5, key, len + 1); |
| 399 | |
| 400 | mutex_lock(&env_lock); |
| 401 | #ifdef HAVE_MS_C_RUNTIME |
| 402 | { |
| 403 | char temp[256]; |
| 404 | snprintf( temp, sizeof(temp), "%s=%s", ename, value); |
| 405 | putenv(temp); |
| 406 | r = 0; |
| 407 | } |
| 408 | #else |
| 409 | r = setenv(ename, value, 1); |
| 410 | #endif |
| 411 | mutex_unlock(&env_lock); |
| 412 | |
| 413 | return r; |
| 414 | } |
| 415 | |
| 416 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), |
| 417 | void *cookie) |
| 418 | { |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | #else |
| 423 | |
| 424 | /* UCI configuration system implementation for OpenWrt */ |
| 425 | #include <uci.h> |
| 426 | #include "adb_trace.h" |
| 427 | |
| 428 | #define UCI_CONFIG_FILE "/etc/config/usb" |
| 429 | |
| 430 | int property_get(const char *key, char *value, const char *default_value) { |
| 431 | if (NULL == key || NULL == value) { |
| 432 | return 0; |
| 433 | } |
| 434 | int retValue = 0; |
| 435 | struct uci_context *uciCtx = uci_alloc_context(); |
| 436 | const char *pValueData = NULL; |
| 437 | struct uci_package *pkg = NULL; |
| 438 | struct uci_element *e; |
| 439 | |
| 440 | // replace '.' with '_' to avoid uci r/w error |
| 441 | char tmp_string[PROPERTY_KEY_MAX]; |
| 442 | int str_i = 0; |
| 443 | strncpy(tmp_string, key, PROPERTY_KEY_MAX - 1); |
| 444 | for (str_i = 0; str_i < PROPERTY_KEY_MAX; str_i++) { |
| 445 | if (tmp_string[str_i] == '.') { |
| 446 | tmp_string[str_i] = '_'; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
| 451 | if (UCI_OK != uci_load(uciCtx, UCI_CONFIG_FILE, &pkg)) { |
| 452 | if (default_value) { |
| 453 | int len = strlen(default_value); |
| 454 | memcpy(value, default_value, len); |
| 455 | value[len] = '\0'; |
| 456 | retValue = 1; |
| 457 | } |
| 458 | D("%s(), uci load fail, file: %s\n", __FUNCTION__, UCI_CONFIG_FILE); |
| 459 | goto cleanup; |
| 460 | } |
| 461 | |
| 462 | uci_foreach_element(&pkg->sections, e) { |
| 463 | struct uci_section *s = uci_to_section(e); |
| 464 | if (NULL != (pValueData = uci_lookup_option_string(uciCtx, s, tmp_string))) { |
| 465 | if (!strncmp(pValueData, ":empty", strlen(":empty"))) { |
| 466 | value[0] = '\0'; |
| 467 | } else { |
| 468 | strncpy(value, pValueData, strlen(pValueData)); |
| 469 | } |
| 470 | retValue = 1; |
| 471 | D("property_get, %s: %s\n", key, value); |
| 472 | } |
| 473 | } |
| 474 | if (!retValue) { |
| 475 | if (default_value) { |
| 476 | int len = strlen(default_value); |
| 477 | memcpy(value, default_value, len); |
| 478 | value[len] = '\0'; |
| 479 | retValue = 1; |
| 480 | D("property_get use default value, %s: %s\n", key, value); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | uci_unload(uciCtx, pkg); |
| 485 | |
| 486 | cleanup: |
| 487 | uci_free_context(uciCtx); |
| 488 | uciCtx = NULL; |
| 489 | return retValue; |
| 490 | } |
| 491 | |
| 492 | int property_set(const char *key, const char *new_value) { |
| 493 | |
| 494 | if (NULL == key || NULL == new_value) { |
| 495 | return 0; |
| 496 | } |
| 497 | int retValue = 0; |
| 498 | int value = 0; |
| 499 | struct uci_context *_ctx = uci_alloc_context(); |
| 500 | struct uci_ptr ptr; |
| 501 | struct uci_package *pkg = NULL; |
| 502 | memset(&ptr, 0, sizeof(ptr)); |
| 503 | |
| 504 | // replace '.' with '_' to avoid uci r/w error |
| 505 | char tmp_string[PROPERTY_KEY_MAX]; |
| 506 | int str_i = 0; |
| 507 | strncpy(tmp_string, key, PROPERTY_KEY_MAX - 1); |
| 508 | for (str_i = 0; str_i < PROPERTY_KEY_MAX; str_i++) { |
| 509 | if (tmp_string[str_i] == '.') { |
| 510 | tmp_string[str_i] = '_'; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | ptr.package = "usb"; |
| 515 | ptr.section = "adb"; |
| 516 | ptr.option = tmp_string; |
| 517 | if (strlen(new_value) == 0) { |
| 518 | ptr.value = ":empty"; |
| 519 | } else { |
| 520 | ptr.value = new_value; |
| 521 | } |
| 522 | value = uci_set(_ctx, &ptr); |
| 523 | if (value == 0) { |
| 524 | value = uci_commit(_ctx, &ptr.p, false); |
| 525 | if (value == 0) { |
| 526 | retValue = 1; |
| 527 | D("property_set ok. %s=%s return %d\n", key, new_value, value); |
| 528 | } else { |
| 529 | D("property_set uci_commit fail %s=%s return %d\n", key, new_value, value); |
| 530 | } |
| 531 | } else { |
| 532 | D("uci_set fail %s=%s return %d\n", key, new_value, value); |
| 533 | } |
| 534 | uci_unload(_ctx, ptr.p); |
| 535 | uci_free_context(_ctx); |
| 536 | _ctx = NULL; |
| 537 | return retValue; |
| 538 | } |
| 539 | |
| 540 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), |
| 541 | void *cookie) |
| 542 | { |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | #endif |