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 <string.h> 34 #include <sys/mman.h> 35 36 #include "private/bionic_macros.h" 37 38 #include "prop_info.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. 56 struct 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 BIONIC_DISALLOW_COPY_AND_ASSIGN(prop_bt); 90 }; 91 92 class 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 static void unmap_prop_area(prop_area** pa) { 98 if (*pa) { 99 munmap(*pa, pa_size_); 100 *pa = nullptr; 101 } 102 } 103 104 prop_area(const uint32_t magic, const uint32_t version) : magic_(magic), version_(version) { 105 atomic_init(&serial_, 0u); 106 memset(reserved_, 0, sizeof(reserved_)); 107 // Allocate enough space for the root node. 108 bytes_used_ = sizeof(prop_bt); 109 } 110 111 const prop_info* find(const char* name); 112 bool add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen); 113 114 bool foreach (void (*propfn)(const prop_info* pi, void* cookie), void* cookie); 115 116 atomic_uint_least32_t* serial() { 117 return &serial_; 118 } 119 uint32_t magic() const { 120 return magic_; 121 } 122 uint32_t version() const { 123 return version_; 124 } 125 126 private: 127 static prop_area* map_fd_ro(const int fd); 128 129 void* allocate_obj(const size_t size, uint_least32_t* const off); 130 prop_bt* new_prop_bt(const char* name, uint32_t namelen, uint_least32_t* const off); 131 prop_info* new_prop_info(const char* name, uint32_t namelen, const char* value, uint32_t valuelen, 132 uint_least32_t* const off); 133 void* to_prop_obj(uint_least32_t off); 134 prop_bt* to_prop_bt(atomic_uint_least32_t* off_p); 135 prop_info* to_prop_info(atomic_uint_least32_t* off_p); 136 137 prop_bt* root_node(); 138 139 prop_bt* find_prop_bt(prop_bt* const bt, const char* name, uint32_t namelen, bool alloc_if_needed); 140 141 const prop_info* find_property(prop_bt* const trie, const char* name, uint32_t namelen, 142 const char* value, uint32_t valuelen, bool alloc_if_needed); 143 144 bool foreach_property(prop_bt* const trie, void (*propfn)(const prop_info* pi, void* cookie), 145 void* cookie); 146 147 // The original design doesn't include pa_size or pa_data_size in the prop_area struct itself. 148 // Since we'll need to be backwards compatible with that design, we don't gain much by adding it 149 // now, especially since we don't have any plans to make different property areas different sizes, 150 // and thus we share these two variables among all instances. 151 static size_t pa_size_; 152 static size_t pa_data_size_; 153 154 uint32_t bytes_used_; 155 atomic_uint_least32_t serial_; 156 uint32_t magic_; 157 uint32_t version_; 158 uint32_t reserved_[28]; 159 char data_[0]; 160 161 BIONIC_DISALLOW_COPY_AND_ASSIGN(prop_area); 162 }; 163