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
26extern "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*/
46int property_get(const char *key, char *value, const char *default_value);
47
48/* property_get_bool: returns the value of key coerced into a
49** boolean. If the property is not set, then the default value is returned.
50**
51* The following is considered to be true (1):
52**   "1", "true", "y", "yes", "on"
53**
54** The following is considered to be false (0):
55**   "0", "false", "n", "no", "off"
56**
57** The conversion is whitespace-sensitive (e.g. " off" will not be false).
58**
59** If no property with this key is set (or the key is NULL) or the boolean
60** conversion fails, the default value is returned.
61**/
62int8_t property_get_bool(const char *key, int8_t default_value);
63
64/* property_get_int64: returns the value of key truncated and coerced into a
65** int64_t. If the property is not set, then the default value is used.
66**
67** The numeric conversion is identical to strtoimax with the base inferred:
68** - All digits up to the first non-digit characters are read
69** - The longest consecutive prefix of digits is converted to a long
70**
71** Valid strings of digits are:
72** - An optional sign character + or -
73** - An optional prefix indicating the base (otherwise base 10 is assumed)
74**   -- 0 prefix is octal
75**   -- 0x / 0X prefix is hex
76**
77** Leading/trailing whitespace is ignored. Overflow/underflow will cause
78** numeric conversion to fail.
79**
80** If no property with this key is set (or the key is NULL) or the numeric
81** conversion fails, the default value is returned.
82**/
83int64_t property_get_int64(const char *key, int64_t default_value);
84
85/* property_get_int32: returns the value of key truncated and coerced into an
86** int32_t. If the property is not set, then the default value is used.
87**
88** The numeric conversion is identical to strtoimax with the base inferred:
89** - All digits up to the first non-digit characters are read
90** - The longest consecutive prefix of digits is converted to a long
91**
92** Valid strings of digits are:
93** - An optional sign character + or -
94** - An optional prefix indicating the base (otherwise base 10 is assumed)
95**   -- 0 prefix is octal
96**   -- 0x / 0X prefix is hex
97**
98** Leading/trailing whitespace is ignored. Overflow/underflow will cause
99** numeric conversion to fail.
100**
101** If no property with this key is set (or the key is NULL) or the numeric
102** conversion fails, the default value is returned.
103**/
104int32_t property_get_int32(const char *key, int32_t default_value);
105
106/* property_set: returns 0 on success, < 0 on failure
107*/
108int property_set(const char *key, const char *value);
109
110int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
111
112#if defined(__BIONIC_FORTIFY)
113
114extern int __property_get_real(const char *, char *, const char *)
115    __asm__(__USER_LABEL_PREFIX__ "property_get");
116__errordecl(__property_get_too_small_error, "property_get() called with too small of a buffer");
117
118__BIONIC_FORTIFY_INLINE
119int property_get(const char *key, char *value, const char *default_value) {
120    size_t bos = __bos(value);
121    if (bos < PROPERTY_VALUE_MAX) {
122        __property_get_too_small_error();
123    }
124    return __property_get_real(key, value, default_value);
125}
126
127#endif
128
129#ifdef HAVE_SYSTEM_PROPERTY_SERVER
130/*
131 * We have an external property server instead of built-in libc support.
132 * Used by the simulator.
133 */
134#define SYSTEM_PROPERTY_PIPE_NAME       "/tmp/android-sysprop"
135
136enum {
137    kSystemPropertyUnknown = 0,
138    kSystemPropertyGet,
139    kSystemPropertySet,
140    kSystemPropertyList
141};
142#endif /*HAVE_SYSTEM_PROPERTY_SERVER*/
143
144
145#ifdef __cplusplus
146}
147#endif
148
149#endif
150