blob: 99bcaafbe210ed9af7ffa35a140568eec7c3fb52 [file] [log] [blame]
Tom Cherryfd44b9f2017-11-08 14:01:00 -08001/*
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#include <stdatomic.h>
30#include <stdint.h>
31#include <sys/system_properties.h>
32
33#include "private/bionic_macros.h"
34
35#ifndef SYSTEM_PROPERTIES_PROP_INFO_H
36#define SYSTEM_PROPERTIES_PROP_INFO_H
37
38// The C11 standard doesn't allow atomic loads from const fields,
39// though C++11 does. Fudge it until standards get straightened out.
40static inline uint_least32_t load_const_atomic(const atomic_uint_least32_t* s, memory_order mo) {
41 atomic_uint_least32_t* non_const_s = const_cast<atomic_uint_least32_t*>(s);
42 return atomic_load_explicit(non_const_s, mo);
43}
44
45struct prop_info {
46 // Read only properties will not set anything but the bottom most bit of serial and the top byte.
47 // We borrow the 2nd from the top byte for extra flags, and use the bottom most bit of that for
48 // our first user, kLongFlag.
49 constexpr static uint32_t kLongFlag = 1 << 16;
50
51 // The error message fits in part of a union with the previous 92 char property value so there
52 // must be room left over after the error message for the offset to the new longer property value
53 // and future expansion fields if needed. Note that this value cannot ever increase. The offset
54 // to the new longer property value appears immediately after it, so an increase of this size will
55 // break compatibility.
56 constexpr static size_t kLongLegacyErrorBufferSize = 56;
57
58 public:
59 atomic_uint_least32_t serial;
60 // we need to keep this buffer around because the property
61 // value can be modified whereas name is constant.
62 union {
63 char value[PROP_VALUE_MAX];
64 struct {
65 char error_message[kLongLegacyErrorBufferSize];
66 uint32_t offset;
67 } long_property;
68 };
69 char name[0];
70
71 bool is_long() const {
72 return (load_const_atomic(&serial, memory_order_relaxed) & kLongFlag) != 0;
73 }
74
75 const char* long_value() const {
76 // We can't store pointers here since this is shared memory that will have different absolute
77 // pointers in different processes. We don't have data_ from prop_area, but since we know
78 // `this` is data_ + some offset and long_value is data_ + some other offset, we calculate the
79 // offset from `this` to long_value and store it as long_property.offset.
80 return reinterpret_cast<const char*>(this) + long_property.offset;
81 }
82
83 prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen);
84 prop_info(const char* name, uint32_t namelen, uint32_t long_offset);
85
86 private:
87 DISALLOW_IMPLICIT_CONSTRUCTORS(prop_info);
88};
89
90static_assert(sizeof(prop_info) == 96, "sizeof struct prop_info must be 96 bytes");
91
92#endif