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 <sys/param.h>
32 #include <sys/system_properties.h>
33 
34 #include "contexts.h"
35 #include "contexts_pre_split.h"
36 #include "contexts_serialized.h"
37 #include "contexts_split.h"
38 
39 class SystemProperties {
40  public:
41   friend struct LocalPropertyTestState;
42   friend class SystemPropertiesTest;
43   // Note that system properties are initialized before libc calls static initializers, so
44   // doing any initialization in this constructor is an error.  Even a Constructor that zero
45   // initializes this class will clobber the previous property initialization.
46   // We rely on the static SystemProperties in libc to be placed in .bss and zero initialized.
47   SystemProperties() = default;
48   // Special constructor for testing that also zero initializes the important members.
SystemProperties(bool initialized)49   explicit SystemProperties(bool initialized) : initialized_(initialized) {
50   }
51 
52   BIONIC_DISALLOW_COPY_AND_ASSIGN(SystemProperties);
53 
54   bool Init(const char* filename);
55   bool Reload(bool load_default_path);
56   bool AreaInit(const char* filename, bool* fsetxattr_failed);
57   bool AreaInit(const char* filename, bool* fsetxattr_failed, bool load_default_path);
58   uint32_t AreaSerial();
59   const prop_info* Find(const char* name);
60   int Read(const prop_info* pi, char* name, char* value);
61   void ReadCallback(const prop_info* pi,
62                     void (*callback)(void* cookie, const char* name, const char* value,
63                                      uint32_t serial),
64                     void* cookie);
65   int Get(const char* name, char* value);
66   int Update(prop_info* pi, const char* value, unsigned int len);
67   int Add(const char* name, unsigned int namelen, const char* value, unsigned int valuelen);
68   uint32_t WaitAny(uint32_t old_serial);
69   bool Wait(const prop_info* pi, uint32_t old_serial, uint32_t* new_serial_ptr,
70             const timespec* relative_timeout);
71   const prop_info* FindNth(unsigned n);
72   int Foreach(void (*propfn)(const prop_info* pi, void* cookie), void* cookie);
73 
74  private:
75   uint32_t ReadMutablePropertyValue(const prop_info* pi, char* value);
76 
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   alignas(kMaxContextsAlign) char appcompat_override_contexts_data_[kMaxContextsSize];
86   Contexts* contexts_;
87   // See http://b/291816546#comment#3 for more explanation of appcompat_override
88   Contexts* appcompat_override_contexts_;
89 
90   bool InitContexts(bool load_default_path);
91 
92   bool initialized_;
93   PropertiesFilename properties_filename_;
94   PropertiesFilename appcompat_filename_;
95 };
96