Home | History | Annotate | Download | only in cutils
      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 #ifndef __CUTILS_PROPERTIES_H
     18 #define __CUTILS_PROPERTIES_H
     19 
     20 #include <sys/cdefs.h>
     21 #include <stddef.h>
     22 #include <sys/system_properties.h>
     23 #include <stdint.h>
     24 
     25 #ifdef __cplusplus
     26 extern "C" {
     27 #endif
     28 
     29 /* System properties are *small* name value pairs managed by the
     30 ** property service.  If your data doesn't fit in the provided
     31 ** space it is not appropriate for a system property.
     32 **
     33 ** WARNING: system/bionic/include/sys/system_properties.h also defines
     34 **          these, but with different names.  (TODO: fix that)
     35 */
     36 #define PROPERTY_KEY_MAX   PROP_NAME_MAX
     37 #define PROPERTY_VALUE_MAX  PROP_VALUE_MAX
     38 
     39 /* property_get: returns the length of the value which will never be
     40 ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
     41 ** (the length does not include the terminating zero).
     42 **
     43 ** If the property read fails or returns an empty value, the default
     44 ** value is used (if nonnull).
     45 */
     46 int property_get(const char *key, char *value, const char *default_value)
     47 /* Sometimes we use not-Bionic with this, so we need this check. */
     48 #if defined(__BIONIC_FORTIFY)
     49         __overloadable __RENAME_CLANG(property_get)
     50 #endif
     51         ;
     52 
     53 /* property_get_bool: returns the value of key coerced into a
     54 ** boolean. If the property is not set, then the default value is returned.
     55 **
     56 * The following is considered to be true (1):
     57 **   "1", "true", "y", "yes", "on"
     58 **
     59 ** The following is considered to be false (0):
     60 **   "0", "false", "n", "no", "off"
     61 **
     62 ** The conversion is whitespace-sensitive (e.g. " off" will not be false).
     63 **
     64 ** If no property with this key is set (or the key is NULL) or the boolean
     65 ** conversion fails, the default value is returned.
     66 **/
     67 int8_t property_get_bool(const char *key, int8_t default_value);
     68 
     69 /* property_get_int64: returns the value of key truncated and coerced into a
     70 ** int64_t. If the property is not set, then the default value is used.
     71 **
     72 ** The numeric conversion is identical to strtoimax with the base inferred:
     73 ** - All digits up to the first non-digit characters are read
     74 ** - The longest consecutive prefix of digits is converted to a long
     75 **
     76 ** Valid strings of digits are:
     77 ** - An optional sign character + or -
     78 ** - An optional prefix indicating the base (otherwise base 10 is assumed)
     79 **   -- 0 prefix is octal
     80 **   -- 0x / 0X prefix is hex
     81 **
     82 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
     83 ** numeric conversion to fail.
     84 **
     85 ** If no property with this key is set (or the key is NULL) or the numeric
     86 ** conversion fails, the default value is returned.
     87 **/
     88 int64_t property_get_int64(const char *key, int64_t default_value);
     89 
     90 /* property_get_int32: returns the value of key truncated and coerced into an
     91 ** int32_t. If the property is not set, then the default value is used.
     92 **
     93 ** The numeric conversion is identical to strtoimax with the base inferred:
     94 ** - All digits up to the first non-digit characters are read
     95 ** - The longest consecutive prefix of digits is converted to a long
     96 **
     97 ** Valid strings of digits are:
     98 ** - An optional sign character + or -
     99 ** - An optional prefix indicating the base (otherwise base 10 is assumed)
    100 **   -- 0 prefix is octal
    101 **   -- 0x / 0X prefix is hex
    102 **
    103 ** Leading/trailing whitespace is ignored. Overflow/underflow will cause
    104 ** numeric conversion to fail.
    105 **
    106 ** If no property with this key is set (or the key is NULL) or the numeric
    107 ** conversion fails, the default value is returned.
    108 **/
    109 int32_t property_get_int32(const char *key, int32_t default_value);
    110 
    111 /* property_set: returns 0 on success, < 0 on failure
    112 */
    113 int property_set(const char *key, const char *value);
    114 
    115 int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
    116 
    117 #if defined(__BIONIC_FORTIFY)
    118 #define __property_get_err_str "property_get() called with too small of a buffer"
    119 
    120 #if defined(__clang__)
    121 
    122 /* Some projects use -Weverything; enable_if is clang-specific.
    123 ** FIXME: This is marked used because we'll otherwise get complaints about an
    124 ** unused static function. This is more robust than marking it unused, since
    125 ** -Wused-but-marked-unused is a thing that will complain if this function is
    126 ** actually used, thus making FORTIFY noisier when an error happens. It's going
    127 ** to go away anyway during our FORTIFY cleanup.
    128 **/
    129 #pragma clang diagnostic push
    130 #pragma clang diagnostic ignored "-Wgcc-compat"
    131 __BIONIC_ERROR_FUNCTION_VISIBILITY
    132 int property_get(const char *key, char *value, const char *default_value)
    133         __overloadable
    134         __enable_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
    135                     __bos(value) < PROPERTY_VALUE_MAX, __property_get_err_str)
    136         __errorattr(__property_get_err_str)
    137         __attribute__((used));
    138 #pragma clang diagnostic pop
    139 
    140 /* No object size? No FORTIFY.
    141 */
    142 
    143 #else /* defined(__clang__) */
    144 
    145 extern int __property_get_real(const char *, char *, const char *)
    146     __asm__(__USER_LABEL_PREFIX__ "property_get");
    147 __errordecl(__property_get_too_small_error, __property_get_err_str);
    148 
    149 __BIONIC_FORTIFY_INLINE
    150 int property_get(const char *key, char *value, const char *default_value) {
    151     size_t bos = __bos(value);
    152     if (bos < PROPERTY_VALUE_MAX) {
    153         __property_get_too_small_error();
    154     }
    155     return __property_get_real(key, value, default_value);
    156 }
    157 
    158 #endif /* defined(__clang__) */
    159 
    160 #undef __property_get_err_str
    161 #endif /* defined(__BIONIC_FORTIFY) */
    162 
    163 #ifdef __cplusplus
    164 }
    165 #endif
    166 
    167 #endif
    168