xj | 123f7cc | 2022-06-06 11:35:21 +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 | #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 | #ifndef PROP_NAME_MAX |
| 30 | #define PROP_NAME_MAX 32 |
| 31 | #endif |
| 32 | #ifndef PROP_VALUE_MAX |
| 33 | #define PROP_VALUE_MAX 92 |
| 34 | #endif |
| 35 | |
| 36 | /* System properties are *small* name value pairs managed by the |
| 37 | ** property service. If your data doesn't fit in the provided |
| 38 | ** space it is not appropriate for a system property. |
| 39 | ** |
| 40 | ** WARNING: system/bionic/include/sys/system_properties.h also defines |
| 41 | ** these, but with different names. (TODO: fix that) |
| 42 | */ |
| 43 | #define PROPERTY_KEY_MAX PROP_NAME_MAX |
| 44 | #define PROPERTY_VALUE_MAX PROP_VALUE_MAX |
| 45 | |
| 46 | /* property_get: returns the length of the value which will never be |
| 47 | ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. |
| 48 | ** (the length does not include the terminating zero). |
| 49 | ** |
| 50 | ** If the property read fails or returns an empty value, the default |
| 51 | ** value is used (if nonnull). |
| 52 | */ |
| 53 | int property_get(const char *key, char *value, const char *default_value); |
| 54 | |
| 55 | /* property_get_bool: returns the value of key coerced into a |
| 56 | ** boolean. If the property is not set, then the default value is returned. |
| 57 | ** |
| 58 | * The following is considered to be true (1): |
| 59 | ** "1", "true", "y", "yes", "on" |
| 60 | ** |
| 61 | ** The following is considered to be false (0): |
| 62 | ** "0", "false", "n", "no", "off" |
| 63 | ** |
| 64 | ** The conversion is whitespace-sensitive (e.g. " off" will not be false). |
| 65 | ** |
| 66 | ** If no property with this key is set (or the key is NULL) or the boolean |
| 67 | ** conversion fails, the default value is returned. |
| 68 | **/ |
| 69 | int8_t property_get_bool(const char *key, int8_t default_value); |
| 70 | |
| 71 | /* property_get_int64: returns the value of key truncated and coerced into a |
| 72 | ** int64_t. If the property is not set, then the default value is used. |
| 73 | ** |
| 74 | ** The numeric conversion is identical to strtoimax with the base inferred: |
| 75 | ** - All digits up to the first non-digit characters are read |
| 76 | ** - The longest consecutive prefix of digits is converted to a long |
| 77 | ** |
| 78 | ** Valid strings of digits are: |
| 79 | ** - An optional sign character + or - |
| 80 | ** - An optional prefix indicating the base (otherwise base 10 is assumed) |
| 81 | ** -- 0 prefix is octal |
| 82 | ** -- 0x / 0X prefix is hex |
| 83 | ** |
| 84 | ** Leading/trailing whitespace is ignored. Overflow/underflow will cause |
| 85 | ** numeric conversion to fail. |
| 86 | ** |
| 87 | ** If no property with this key is set (or the key is NULL) or the numeric |
| 88 | ** conversion fails, the default value is returned. |
| 89 | **/ |
| 90 | int64_t property_get_int64(const char *key, int64_t default_value); |
| 91 | |
| 92 | /* property_get_int32: returns the value of key truncated and coerced into an |
| 93 | ** int32_t. If the property is not set, then the default value is used. |
| 94 | ** |
| 95 | ** The numeric conversion is identical to strtoimax with the base inferred: |
| 96 | ** - All digits up to the first non-digit characters are read |
| 97 | ** - The longest consecutive prefix of digits is converted to a long |
| 98 | ** |
| 99 | ** Valid strings of digits are: |
| 100 | ** - An optional sign character + or - |
| 101 | ** - An optional prefix indicating the base (otherwise base 10 is assumed) |
| 102 | ** -- 0 prefix is octal |
| 103 | ** -- 0x / 0X prefix is hex |
| 104 | ** |
| 105 | ** Leading/trailing whitespace is ignored. Overflow/underflow will cause |
| 106 | ** numeric conversion to fail. |
| 107 | ** |
| 108 | ** If no property with this key is set (or the key is NULL) or the numeric |
| 109 | ** conversion fails, the default value is returned. |
| 110 | **/ |
| 111 | int32_t property_get_int32(const char *key, int32_t default_value); |
| 112 | |
| 113 | /* property_set: returns 0 on success, < 0 on failure |
| 114 | */ |
| 115 | int property_set(const char *key, const char *value); |
| 116 | |
| 117 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); |
| 118 | |
| 119 | #if defined(__BIONIC_FORTIFY) |
| 120 | |
| 121 | extern int __property_get_real(const char *, char *, const char *) |
| 122 | __asm__(__USER_LABEL_PREFIX__ "property_get"); |
| 123 | __errordecl(__property_get_too_small_error, "property_get() called with too small of a buffer"); |
| 124 | |
| 125 | __BIONIC_FORTIFY_INLINE |
| 126 | int property_get(const char *key, char *value, const char *default_value) { |
| 127 | size_t bos = __bos(value); |
| 128 | if (bos < PROPERTY_VALUE_MAX) { |
| 129 | __property_get_too_small_error(); |
| 130 | } |
| 131 | return __property_get_real(key, value, default_value); |
| 132 | } |
| 133 | |
| 134 | #endif |
| 135 | |
| 136 | #ifdef HAVE_SYSTEM_PROPERTY_SERVER |
| 137 | /* |
| 138 | * We have an external property server instead of built-in libc support. |
| 139 | * Used by the simulator. |
| 140 | */ |
| 141 | #define SYSTEM_PROPERTY_PIPE_NAME "/tmp/android-sysprop" |
| 142 | |
| 143 | enum { |
| 144 | kSystemPropertyUnknown = 0, |
| 145 | kSystemPropertyGet, |
| 146 | kSystemPropertySet, |
| 147 | kSystemPropertyList |
| 148 | }; |
| 149 | #endif /*HAVE_SYSTEM_PROPERTY_SERVER*/ |
| 150 | |
| 151 | |
| 152 | #ifdef __cplusplus |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | #endif |