1 /*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #pragma once
30
31 #include <stdatomic.h>
32 #include <stdint.h>
33 #include <sys/system_properties.h>
34
35 #include "platform/bionic/macros.h"
36
37 // The C11 standard doesn't allow atomic loads from const fields,
38 // though C++11 does. Fudge it until standards get straightened out.
load_const_atomic(const atomic_uint_least32_t * s,memory_order mo)39 static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
40 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
41 return atomic_load_explicit(non_const_s, mo);
42 }
43
44 struct prop_info {
45 // Read only properties will not set anything but the bottom most bit of serial and the top byte.
46 // We borrow the 2nd from the top byte for extra flags, and use the bottom most bit of that for
47 // our first user, kLongFlag.
48 static constexpr uint32_t kLongFlag = 1 << 16;
49
50 // The error message fits in part of a union with the previous 92 char property value so there
51 // must be room left over after the error message for the offset to the new longer property value
52 // and future expansion fields if needed. Note that this value cannot ever increase. The offset
53 // to the new longer property value appears immediately after it, so an increase of this size will
54 // break compatibility.
55 static constexpr size_t kLongLegacyErrorBufferSize = 56;
56
57 public:
58 atomic_uint_least32_t serial;
59 // we need to keep this buffer around because the property
60 // value can be modified whereas name is constant.
61 union {
62 char value[PROP_VALUE_MAX];
63 struct {
64 char error_message[kLongLegacyErrorBufferSize];
65 uint32_t offset;
66 } long_property;
67 };
68 char name[0];
69
is_longprop_info70 bool is_long() const {
71 return (load_const_atomic(&serial, memory_order_relaxed) & kLongFlag) != 0;
72 }
73
long_valueprop_info74 const char* long_value() const {
75 // We can't store pointers here since this is shared memory that will have different absolute
76 // pointers in different processes. We don't have data_ from prop_area, but since we know
77 // `this` is data_ + some offset and long_value is data_ + some other offset, we calculate the
78 // offset from `this` to long_value and store it as long_property.offset.
79 return reinterpret_cast<const char*>(this) + long_property.offset;
80 }
81
82 prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen);
83 prop_info(const char* name, uint32_t namelen, uint32_t long_offset);
84
85 private:
86 BIONIC_DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
87 };
88
89 static_assert(sizeof(prop_info) == 96, "sizeof struct prop_info must be 96 bytes");
90