17ec681f3Smrg/* 27ec681f3Smrg * Copyright (C) 2006 The Android Open Source Project 37ec681f3Smrg * 47ec681f3Smrg * Licensed under the Apache License, Version 2.0 (the "License"); 57ec681f3Smrg * you may not use this file except in compliance with the License. 67ec681f3Smrg * You may obtain a copy of the License at 77ec681f3Smrg * 87ec681f3Smrg * http://www.apache.org/licenses/LICENSE-2.0 97ec681f3Smrg * 107ec681f3Smrg * Unless required by applicable law or agreed to in writing, software 117ec681f3Smrg * distributed under the License is distributed on an "AS IS" BASIS, 127ec681f3Smrg * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137ec681f3Smrg * See the License for the specific language governing permissions and 147ec681f3Smrg * limitations under the License. 157ec681f3Smrg */ 167ec681f3Smrg 177ec681f3Smrg#pragma once 187ec681f3Smrg 197ec681f3Smrg#include <sys/cdefs.h> 207ec681f3Smrg#include <stddef.h> 217ec681f3Smrg#include <stdint.h> 227ec681f3Smrg 237ec681f3Smrg#if __has_include(<sys/system_properties.h>) 247ec681f3Smrg#include <sys/system_properties.h> 257ec681f3Smrg#else 267ec681f3Smrg#define PROP_VALUE_MAX 92 277ec681f3Smrg#endif 287ec681f3Smrg 297ec681f3Smrg#ifdef __cplusplus 307ec681f3Smrgextern "C" { 317ec681f3Smrg#endif 327ec681f3Smrg 337ec681f3Smrg// 347ec681f3Smrg// Deprecated. 357ec681f3Smrg// 367ec681f3Smrg// See <android-base/properties.h> for better API. 377ec681f3Smrg// 387ec681f3Smrg 397ec681f3Smrg#define PROPERTY_KEY_MAX PROP_NAME_MAX 407ec681f3Smrg#define PROPERTY_VALUE_MAX PROP_VALUE_MAX 417ec681f3Smrg 427ec681f3Smrg/* property_get: returns the length of the value which will never be 437ec681f3Smrg** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. 447ec681f3Smrg** (the length does not include the terminating zero). 457ec681f3Smrg** 467ec681f3Smrg** If the property read fails or returns an empty value, the default 477ec681f3Smrg** value is used (if nonnull). 487ec681f3Smrg*/ 497ec681f3Smrgint property_get(const char* key, char* value, const char* default_value); 507ec681f3Smrg 517ec681f3Smrg/* property_get_bool: returns the value of key coerced into a 527ec681f3Smrg** boolean. If the property is not set, then the default value is returned. 537ec681f3Smrg** 547ec681f3Smrg* The following is considered to be true (1): 557ec681f3Smrg** "1", "true", "y", "yes", "on" 567ec681f3Smrg** 577ec681f3Smrg** The following is considered to be false (0): 587ec681f3Smrg** "0", "false", "n", "no", "off" 597ec681f3Smrg** 607ec681f3Smrg** The conversion is whitespace-sensitive (e.g. " off" will not be false). 617ec681f3Smrg** 627ec681f3Smrg** If no property with this key is set (or the key is NULL) or the boolean 637ec681f3Smrg** conversion fails, the default value is returned. 647ec681f3Smrg**/ 657ec681f3Smrgint8_t property_get_bool(const char *key, int8_t default_value); 667ec681f3Smrg 677ec681f3Smrg/* property_get_int64: returns the value of key truncated and coerced into a 687ec681f3Smrg** int64_t. If the property is not set, then the default value is used. 697ec681f3Smrg** 707ec681f3Smrg** The numeric conversion is identical to strtoimax with the base inferred: 717ec681f3Smrg** - All digits up to the first non-digit characters are read 727ec681f3Smrg** - The longest consecutive prefix of digits is converted to a long 737ec681f3Smrg** 747ec681f3Smrg** Valid strings of digits are: 757ec681f3Smrg** - An optional sign character + or - 767ec681f3Smrg** - An optional prefix indicating the base (otherwise base 10 is assumed) 777ec681f3Smrg** -- 0 prefix is octal 787ec681f3Smrg** -- 0x / 0X prefix is hex 797ec681f3Smrg** 807ec681f3Smrg** Leading/trailing whitespace is ignored. Overflow/underflow will cause 817ec681f3Smrg** numeric conversion to fail. 827ec681f3Smrg** 837ec681f3Smrg** If no property with this key is set (or the key is NULL) or the numeric 847ec681f3Smrg** conversion fails, the default value is returned. 857ec681f3Smrg**/ 867ec681f3Smrgint64_t property_get_int64(const char *key, int64_t default_value); 877ec681f3Smrg 887ec681f3Smrg/* property_get_int32: returns the value of key truncated and coerced into an 897ec681f3Smrg** int32_t. If the property is not set, then the default value is used. 907ec681f3Smrg** 917ec681f3Smrg** The numeric conversion is identical to strtoimax with the base inferred: 927ec681f3Smrg** - All digits up to the first non-digit characters are read 937ec681f3Smrg** - The longest consecutive prefix of digits is converted to a long 947ec681f3Smrg** 957ec681f3Smrg** Valid strings of digits are: 967ec681f3Smrg** - An optional sign character + or - 977ec681f3Smrg** - An optional prefix indicating the base (otherwise base 10 is assumed) 987ec681f3Smrg** -- 0 prefix is octal 997ec681f3Smrg** -- 0x / 0X prefix is hex 1007ec681f3Smrg** 1017ec681f3Smrg** Leading/trailing whitespace is ignored. Overflow/underflow will cause 1027ec681f3Smrg** numeric conversion to fail. 1037ec681f3Smrg** 1047ec681f3Smrg** If no property with this key is set (or the key is NULL) or the numeric 1057ec681f3Smrg** conversion fails, the default value is returned. 1067ec681f3Smrg**/ 1077ec681f3Smrgint32_t property_get_int32(const char *key, int32_t default_value); 1087ec681f3Smrg 1097ec681f3Smrg/* property_set: returns 0 on success, < 0 on failure 1107ec681f3Smrg*/ 1117ec681f3Smrgint property_set(const char *key, const char *value); 1127ec681f3Smrg 1137ec681f3Smrgint property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); 1147ec681f3Smrg 1157ec681f3Smrg#if defined(__BIONIC_FORTIFY) 1167ec681f3Smrg#define __property_get_err_str "property_get() called with too small of a buffer" 1177ec681f3Smrg 1187ec681f3Smrg#if defined(__clang__) 1197ec681f3Smrg 1207ec681f3Smrg/* Some projects use -Weverything; diagnose_if is clang-specific. */ 1217ec681f3Smrg#pragma clang diagnostic push 1227ec681f3Smrg#pragma clang diagnostic ignored "-Wgcc-compat" 1237ec681f3Smrgint property_get(const char* key, char* value, const char* default_value) 1247ec681f3Smrg __clang_error_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE && 1257ec681f3Smrg __bos(value) < PROPERTY_VALUE_MAX, 1267ec681f3Smrg __property_get_err_str); 1277ec681f3Smrg#pragma clang diagnostic pop 1287ec681f3Smrg 1297ec681f3Smrg#else /* defined(__clang__) */ 1307ec681f3Smrg 1317ec681f3Smrgextern int __property_get_real(const char *, char *, const char *) 1327ec681f3Smrg __asm__(__USER_LABEL_PREFIX__ "property_get"); 1337ec681f3Smrg__errordecl(__property_get_too_small_error, __property_get_err_str); 1347ec681f3Smrg 1357ec681f3Smrg__BIONIC_FORTIFY_INLINE 1367ec681f3Smrgint property_get(const char *key, char *value, const char *default_value) { 1377ec681f3Smrg size_t bos = __bos(value); 1387ec681f3Smrg if (bos < PROPERTY_VALUE_MAX) { 1397ec681f3Smrg __property_get_too_small_error(); 1407ec681f3Smrg } 1417ec681f3Smrg return __property_get_real(key, value, default_value); 1427ec681f3Smrg} 1437ec681f3Smrg 1447ec681f3Smrg#endif /* defined(__clang__) */ 1457ec681f3Smrg 1467ec681f3Smrg#undef __property_get_err_str 1477ec681f3Smrg#endif /* defined(__BIONIC_FORTIFY) */ 1487ec681f3Smrg 1497ec681f3Smrg#ifdef __cplusplus 1507ec681f3Smrg} 1517ec681f3Smrg#endif 152