Home | History | Annotate | Download | only in system_properties
      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 <stdint.h>
     32 #include <sys/param.h>
     33 #include <sys/system_properties.h>
     34 
     35 #include "contexts.h"
     36 #include "contexts_pre_split.h"
     37 #include "contexts_serialized.h"
     38 #include "contexts_split.h"
     39 
     40 constexpr int PROP_FILENAME_MAX = 1024;
     41 
     42 class SystemProperties {
     43  public:
     44   friend struct LocalPropertyTestState;
     45   friend class SystemPropertiesTest;
     46   // Note that system properties are initialized before libc calls static initializers, so
     47   // doing any initialization in this constructor is an error.  Even a Constructor that zero
     48   // initializes this class will clobber the previous property initialization.
     49   // We rely on the static SystemProperties in libc to be placed in .bss and zero initialized.
     50   SystemProperties() = default;
     51   // Special constructor for testing that also zero initializes the important members.
     52   explicit SystemProperties(bool initialized) : initialized_(initialized) {
     53   }
     54 
     55   DISALLOW_COPY_AND_ASSIGN(SystemProperties);
     56 
     57   bool Init(const char* filename);
     58   bool AreaInit(const char* filename, bool* fsetxattr_failed);
     59   uint32_t AreaSerial();
     60   const prop_info* Find(const char* name);
     61   int Read(const prop_info* pi, char* name, char* value);
     62   void ReadCallback(const prop_info* pi,
     63                     void (*callback)(void* cookie, const char* name, const char* value,
     64                                      uint32_t serial),
     65                     void* cookie);
     66   int Get(const char* name, char* value);
     67   int Update(prop_info* pi, const char* value, unsigned int len);
     68   int Add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
     69   uint32_t Serial(const prop_info* pi);
     70   uint32_t WaitAny(uint32_t old_serial);
     71   bool Wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,
     72             const timespec* relative_timeout);
     73   const prop_info* FindNth(unsigned n);
     74   int Foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
     75 
     76  private:
     77   // We don't want to use new or malloc in properties (b/31659220), and we don't want to waste a
     78   // full page by using mmap(), so we set aside enough space to create any context of the three
     79   // contexts.
     80   static constexpr size_t kMaxContextsAlign =
     81       MAX(alignof(ContextsSerialized), MAX(alignof(ContextsSplit), alignof(ContextsPreSplit)));
     82   static constexpr size_t kMaxContextsSize =
     83       MAX(sizeof(ContextsSerialized), MAX(sizeof(ContextsSplit), sizeof(ContextsPreSplit)));
     84   alignas(kMaxContextsAlign) char contexts_data_[kMaxContextsSize];
     85   Contexts* contexts_;
     86 
     87   bool initialized_;
     88   char property_filename_[PROP_FILENAME_MAX];
     89 };
     90