properties.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef __CUTILS_PROPERTIES_H
  17. #define __CUTILS_PROPERTIES_H
  18. #include <sys/cdefs.h>
  19. #include <stddef.h>
  20. #include <sys/system_properties.h>
  21. #include <stdint.h>
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* System properties are *small* name value pairs managed by the
  26. ** property service. If your data doesn't fit in the provided
  27. ** space it is not appropriate for a system property.
  28. **
  29. ** WARNING: system/bionic/include/sys/system_properties.h also defines
  30. ** these, but with different names. (TODO: fix that)
  31. */
  32. #define PROPERTY_KEY_MAX PROP_NAME_MAX
  33. #define PROPERTY_VALUE_MAX PROP_VALUE_MAX
  34. /* property_get: returns the length of the value which will never be
  35. ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
  36. ** (the length does not include the terminating zero).
  37. **
  38. ** If the property read fails or returns an empty value, the default
  39. ** value is used (if nonnull).
  40. */
  41. int property_get(const char* key, char* value, const char* default_value);
  42. /* property_get_bool: returns the value of key coerced into a
  43. ** boolean. If the property is not set, then the default value is returned.
  44. **
  45. * The following is considered to be true (1):
  46. ** "1", "true", "y", "yes", "on"
  47. **
  48. ** The following is considered to be false (0):
  49. ** "0", "false", "n", "no", "off"
  50. **
  51. ** The conversion is whitespace-sensitive (e.g. " off" will not be false).
  52. **
  53. ** If no property with this key is set (or the key is NULL) or the boolean
  54. ** conversion fails, the default value is returned.
  55. **/
  56. int8_t property_get_bool(const char *key, int8_t default_value);
  57. /* property_get_int64: returns the value of key truncated and coerced into a
  58. ** int64_t. If the property is not set, then the default value is used.
  59. **
  60. ** The numeric conversion is identical to strtoimax with the base inferred:
  61. ** - All digits up to the first non-digit characters are read
  62. ** - The longest consecutive prefix of digits is converted to a long
  63. **
  64. ** Valid strings of digits are:
  65. ** - An optional sign character + or -
  66. ** - An optional prefix indicating the base (otherwise base 10 is assumed)
  67. ** -- 0 prefix is octal
  68. ** -- 0x / 0X prefix is hex
  69. **
  70. ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
  71. ** numeric conversion to fail.
  72. **
  73. ** If no property with this key is set (or the key is NULL) or the numeric
  74. ** conversion fails, the default value is returned.
  75. **/
  76. int64_t property_get_int64(const char *key, int64_t default_value);
  77. /* property_get_int32: returns the value of key truncated and coerced into an
  78. ** int32_t. If the property is not set, then the default value is used.
  79. **
  80. ** The numeric conversion is identical to strtoimax with the base inferred:
  81. ** - All digits up to the first non-digit characters are read
  82. ** - The longest consecutive prefix of digits is converted to a long
  83. **
  84. ** Valid strings of digits are:
  85. ** - An optional sign character + or -
  86. ** - An optional prefix indicating the base (otherwise base 10 is assumed)
  87. ** -- 0 prefix is octal
  88. ** -- 0x / 0X prefix is hex
  89. **
  90. ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
  91. ** numeric conversion to fail.
  92. **
  93. ** If no property with this key is set (or the key is NULL) or the numeric
  94. ** conversion fails, the default value is returned.
  95. **/
  96. int32_t property_get_int32(const char *key, int32_t default_value);
  97. /* property_set: returns 0 on success, < 0 on failure
  98. */
  99. int property_set(const char *key, const char *value);
  100. int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
  101. #if defined(__BIONIC_FORTIFY)
  102. #define __property_get_err_str "property_get() called with too small of a buffer"
  103. #if defined(__clang__)
  104. /* Some projects use -Weverything; diagnose_if is clang-specific. */
  105. #pragma clang diagnostic push
  106. #pragma clang diagnostic ignored "-Wgcc-compat"
  107. int property_get(const char* key, char* value, const char* default_value)
  108. __clang_error_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
  109. __bos(value) < PROPERTY_VALUE_MAX,
  110. __property_get_err_str);
  111. #pragma clang diagnostic pop
  112. #else /* defined(__clang__) */
  113. extern int __property_get_real(const char *, char *, const char *)
  114. __asm__(__USER_LABEL_PREFIX__ "property_get");
  115. __errordecl(__property_get_too_small_error, __property_get_err_str);
  116. __BIONIC_FORTIFY_INLINE
  117. int property_get(const char *key, char *value, const char *default_value) {
  118. size_t bos = __bos(value);
  119. if (bos < PROPERTY_VALUE_MAX) {
  120. __property_get_too_small_error();
  121. }
  122. return __property_get_real(key, value, default_value);
  123. }
  124. #endif /* defined(__clang__) */
  125. #undef __property_get_err_str
  126. #endif /* defined(__BIONIC_FORTIFY) */
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #endif