1e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry/* 2e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * Copyright (C) 2008 The Android Open Source Project 3e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * All rights reserved. 4e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * 5e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * Redistribution and use in source and binary forms, with or without 6e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * modification, are permitted provided that the following conditions 7e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * are met: 8e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * * Redistributions of source code must retain the above copyright 9e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * notice, this list of conditions and the following disclaimer. 10e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * * Redistributions in binary form must reproduce the above copyright 11e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * notice, this list of conditions and the following disclaimer in 12e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * the documentation and/or other materials provided with the 13e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * distribution. 14e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * 15e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 18e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 19e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 20e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 22e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 25e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry * SUCH DAMAGE. 27e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry */ 28e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 29cbc80ba9d839675a0c4891e2ab33f39ba51b04b2Elliott Hughes#pragma once 30e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 31e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include <stdint.h> 32ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry#include <sys/param.h> 33e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include <sys/system_properties.h> 34e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 35e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include "contexts.h" 36e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include "contexts_pre_split.h" 37e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include "contexts_serialized.h" 38e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry#include "contexts_split.h" 39e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 40e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherryconstexpr int PROP_FILENAME_MAX = 1024; 41e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 42e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherryclass SystemProperties { 43e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry public: 44ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry friend struct LocalPropertyTestState; 45ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry friend class SystemPropertiesTest; 46e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry // Note that system properties are initialized before libc calls static initializers, so 47e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry // doing any initialization in this constructor is an error. Even a Constructor that zero 48e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry // initializes this class will clobber the previous property initialization. 49e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry // We rely on the static SystemProperties in libc to be placed in .bss and zero initialized. 50ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry SystemProperties() = default; 51e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry // Special constructor for testing that also zero initializes the important members. 525cec377f49d6429b19895eb632225cd757ea611eElliott Hughes explicit SystemProperties(bool initialized) : initialized_(initialized) { 53e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry } 54e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 55e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry DISALLOW_COPY_AND_ASSIGN(SystemProperties); 56e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 57e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry bool Init(const char* filename); 58e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry bool AreaInit(const char* filename, bool* fsetxattr_failed); 59e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry uint32_t AreaSerial(); 60e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry const prop_info* Find(const char* name); 61e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry int Read(const prop_info* pi, char* name, char* value); 62e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry void ReadCallback(const prop_info* pi, 63e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry void (*callback)(void* cookie, const char* name, const char* value, 64e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry uint32_t serial), 65e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry void* cookie); 66e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry int Get(const char* name, char* value); 67e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry int Update(prop_info* pi, const char* value, unsigned int len); 68e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry int Add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen); 69e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry uint32_t Serial(const prop_info* pi); 70e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry uint32_t WaitAny(uint32_t old_serial); 71e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry bool Wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr, 72e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry const timespec* relative_timeout); 73e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry const prop_info* FindNth(unsigned n); 74e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry int Foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie); 75e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry 76e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry private: 77ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry // We don't want to use new or malloc in properties (b/31659220), and we don't want to waste a 78ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry // full page by using mmap(), so we set aside enough space to create any context of the three 79ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry // contexts. 80ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry static constexpr size_t kMaxContextsAlign = 81ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry MAX(alignof(ContextsSerialized), MAX(alignof(ContextsSplit), alignof(ContextsPreSplit))); 82ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry static constexpr size_t kMaxContextsSize = 83ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry MAX(sizeof(ContextsSerialized), MAX(sizeof(ContextsSplit), sizeof(ContextsPreSplit))); 84ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry alignas(kMaxContextsAlign) char contexts_data_[kMaxContextsSize]; 85ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry Contexts* contexts_; 86ee8e3dd67c910907115c7cef87be3d898d71a05dTom Cherry 87e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry bool initialized_; 88e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry char property_filename_[PROP_FILENAME_MAX]; 89e275d6d72db1868056a204e8483a7346a28cb8d6Tom Cherry}; 90