blob: 6b74e10cde32d959099e8709c8fff056cc412a94 [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 <string.h>
32
33#include "private/bionic_macros.h"
34
35#include "prop_info.h"
36
37#ifndef SYSTEM_PROPERTIES_PROP_AREA_H
38#define SYSTEM_PROPERTIES_PROP_AREA_H
39
40// Properties are stored in a hybrid trie/binary tree structure.
41// Each property's name is delimited at '.' characters, and the tokens are put
42// into a trie structure. Siblings at each level of the trie are stored in a
43// binary tree. For instance, "ro.secure"="1" could be stored as follows:
44//
45// +-----+ children +----+ children +--------+
46// | |-------------->| ro |-------------->| secure |
47// +-----+ +----+ +--------+
48// / \ / |
49// left / \ right left / | prop +===========+
50// v v v +-------->| ro.secure |
51// +-----+ +-----+ +-----+ +-----------+
52// | net | | sys | | com | | 1 |
53// +-----+ +-----+ +-----+ +===========+
54
55// Represents a node in the trie.
56struct prop_bt {
57 uint32_t namelen;
58
59 // The property trie is updated only by the init process (single threaded) which provides
60 // property service. And it can be read by multiple threads at the same time.
61 // As the property trie is not protected by locks, we use atomic_uint_least32_t types for the
62 // left, right, children "pointers" in the trie node. To make sure readers who see the
63 // change of "pointers" can also notice the change of prop_bt structure contents pointed by
64 // the "pointers", we always use release-consume ordering pair when accessing these "pointers".
65
66 // prop "points" to prop_info structure if there is a propery associated with the trie node.
67 // Its situation is similar to the left, right, children "pointers". So we use
68 // atomic_uint_least32_t and release-consume ordering to protect it as well.
69
70 // We should also avoid rereading these fields redundantly, since not
71 // all processor implementations ensure that multiple loads from the
72 // same field are carried out in the right order.
73 atomic_uint_least32_t prop;
74
75 atomic_uint_least32_t left;
76 atomic_uint_least32_t right;
77
78 atomic_uint_least32_t children;
79
80 char name[0];
81
82 prop_bt(const char* name, const uint32_t name_length) {
83 this->namelen = name_length;
84 memcpy(this->name, name, name_length);
85 this->name[name_length] = '\0';
86 }
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(prop_bt);
90};
91
92class prop_area {
93 public:
94 static prop_area* map_prop_area_rw(const char* filename, const char* context,
95 bool* fsetxattr_failed);
96 static prop_area* map_prop_area(const char* filename);
97
98 prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) {
99 atomic_init(&serial_, 0);
100 memset(reserved_, 0, sizeof(reserved_));
101 // Allocate enough space for the root node.
102 bytes_used_ = sizeof(prop_bt);
103 }
104
105 const prop_info* find(const char* name);
106 bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
107
108 bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
109
110 atomic_uint_least32_t* serial() {
111 return &serial_;
112 }
113 uint32_t magic() const {
114 return magic_;
115 }
116 uint32_t version() const {
117 return version_;
118 }
119
120 private:
121 static prop_area* map_fd_ro(const int fd);
122
123 void* allocate_obj(const size_t size, uint_least32_t* const off);
124 prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off);
125 prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen,
126 uint_least32_t* const off);
127 void* to_prop_obj(uint_least32_t off);
128 prop_bt* to_prop_bt(atomic_uint_least32_t* off_p);
129 prop_info* to_prop_info(atomic_uint_least32_t* off_p);
130
131 prop_bt* root_node();
132
133 prop_bt* find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen, bool alloc_if_needed);
134
135 const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen,
136 const char* value, uint32_t valuelen, bool alloc_if_needed);
137
138 bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie),
139 void* cookie);
140
141 uint32_t bytes_used_;
142 atomic_uint_least32_t serial_;
143 uint32_t magic_;
144 uint32_t version_;
145 uint32_t reserved_[28];
146 char data_[0];
147
148 DISALLOW_COPY_AND_ASSIGN(prop_area);
149};
150
151#endif