blob: c2117a7cc72ef9e5ed79c1f227026635ed6e6208 [file] [log] [blame]
Hongyi Zhang50a4e012018-09-26 21:20:03 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.am;
18
19import android.content.ContentResolver;
20import android.database.ContentObserver;
21import android.net.Uri;
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080022import android.os.AsyncTask;
Hongyi Zhang50a4e012018-09-26 21:20:03 -070023import android.os.Build;
24import android.os.SystemProperties;
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080025import android.provider.DeviceConfig;
Hongyi Zhang50a4e012018-09-26 21:20:03 -070026import android.provider.Settings;
27import android.text.TextUtils;
28import android.util.Slog;
29
30import com.android.internal.annotations.VisibleForTesting;
31
32import java.io.BufferedReader;
33import java.io.File;
34import java.io.FileReader;
35import java.io.IOException;
36import java.util.HashSet;
37
38/**
39 * Maps system settings to system properties.
40 * <p>The properties are dynamically updated when settings change.
41 */
42class SettingsToPropertiesMapper {
43
44 private static final String TAG = "SettingsToPropertiesMapper";
45
46 private static final String SYSTEM_PROPERTY_PREFIX = "persist.device_config.";
47
48 private static final String RESET_PERFORMED_PROPERTY = "device_config.reset_performed";
49
50 private static final String RESET_RECORD_FILE_PATH =
51 "/data/server_configurable_flags/reset_flags";
52
53 private static final String SYSTEM_PROPERTY_VALID_CHARACTERS_REGEX = "^[\\w\\.\\-@:]*$";
54
55 private static final String SYSTEM_PROPERTY_INVALID_SUBSTRING = "..";
56
57 private static final int SYSTEM_PROPERTY_MAX_LENGTH = 92;
58
59 // experiment flags added to Global.Settings(before new "Config" provider table is available)
60 // will be added under this category.
61 private static final String GLOBAL_SETTINGS_CATEGORY = "global_settings";
62
63 // Add the global setting you want to push to native level as experiment flag into this list.
64 //
65 // NOTE: please grant write permission system property prefix
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080066 // with format persist.device_config.global_settings.[flag_name] in system_server.te and grant
67 // read permission in the corresponding .te file your feature belongs to.
Hongyi Zhang50a4e012018-09-26 21:20:03 -070068 @VisibleForTesting
69 static final String[] sGlobalSettings = new String[] {
Hongyi Zhanga02118d2018-11-15 20:15:38 -080070 Settings.Global.NATIVE_FLAGS_HEALTH_CHECK_ENABLED,
Hongyi Zhang50a4e012018-09-26 21:20:03 -070071 };
72
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080073 // All the flags under the listed DeviceConfig scopes will be synced to native level.
74 //
75 // NOTE: please grant write permission system property prefix
76 // with format persist.device_config.[device_config_scope]. in system_server.te and grant read
77 // permission in the corresponding .te file your feature belongs to.
Hongyi Zhang50a4e012018-09-26 21:20:03 -070078 @VisibleForTesting
79 static final String[] sDeviceConfigScopes = new String[] {
Siarhei Vishniakou39a1a982019-01-11 09:22:32 -080080 DeviceConfig.NAMESPACE_INPUT_NATIVE_BOOT,
Hongyi Zhang50a4e012018-09-26 21:20:03 -070081 };
82
83 private final String[] mGlobalSettings;
84
85 private final String[] mDeviceConfigScopes;
86
87 private final ContentResolver mContentResolver;
88
89 @VisibleForTesting
90 protected SettingsToPropertiesMapper(ContentResolver contentResolver,
91 String[] globalSettings,
92 String[] deviceConfigScopes) {
93 mContentResolver = contentResolver;
94 mGlobalSettings = globalSettings;
95 mDeviceConfigScopes = deviceConfigScopes;
96 }
97
98 @VisibleForTesting
99 void updatePropertiesFromSettings() {
100 for (String globalSetting : mGlobalSettings) {
101 Uri settingUri = Settings.Global.getUriFor(globalSetting);
102 String propName = makePropertyName(GLOBAL_SETTINGS_CATEGORY, globalSetting);
103 if (settingUri == null) {
104 log("setting uri is null for globalSetting " + globalSetting);
105 continue;
106 }
107 if (propName == null) {
108 log("invalid prop name for globalSetting " + globalSetting);
109 continue;
110 }
111
112 ContentObserver co = new ContentObserver(null) {
113 @Override
114 public void onChange(boolean selfChange) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800115 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700116 }
117 };
118
119 // only updating on starting up when no native flags reset is performed during current
120 // booting.
121 if (!isNativeFlagsResetPerformed()) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800122 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700123 }
124 mContentResolver.registerContentObserver(settingUri, false, co);
125 }
126
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800127 for (String deviceConfigScope : mDeviceConfigScopes) {
128 DeviceConfig.addOnPropertyChangedListener(
129 deviceConfigScope,
130 AsyncTask.THREAD_POOL_EXECUTOR,
131 (String scope, String name, String value) -> {
132 String propertyName = makePropertyName(scope, name);
133 if (propertyName == null) {
134 log("unable to construct system property for " + scope + "/" + name);
135 return;
136 }
137 setProperty(propertyName, value);
138 });
139 }
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700140 }
141
142 public static SettingsToPropertiesMapper start(ContentResolver contentResolver) {
143 SettingsToPropertiesMapper mapper = new SettingsToPropertiesMapper(
144 contentResolver, sGlobalSettings, sDeviceConfigScopes);
145 mapper.updatePropertiesFromSettings();
146 return mapper;
147 }
148
149 /**
150 * If native level flags reset has been performed as an attempt to recover from a crash loop
151 * during current device booting.
152 * @return
153 */
154 public boolean isNativeFlagsResetPerformed() {
155 String value = systemPropertiesGet(RESET_PERFORMED_PROPERTY);
156 return "true".equals(value);
157 }
158
159 /**
160 * return an array of native flag categories under which flags got reset during current device
161 * booting.
162 * @return
163 */
164 public String[] getResetNativeCategories() {
165 if (!isNativeFlagsResetPerformed()) {
166 return new String[0];
167 }
168
169 String content = getResetFlagsFileContent();
170 if (TextUtils.isEmpty(content)) {
171 return new String[0];
172 }
173
174 String[] property_names = content.split(";");
175 HashSet<String> categories = new HashSet<>();
176 for (String property_name : property_names) {
177 String[] segments = property_name.split("\\.");
178 if (segments.length < 3) {
179 log("failed to extract category name from property " + property_name);
180 continue;
181 }
182 categories.add(segments[2]);
183 }
184 return categories.toArray(new String[0]);
185 }
186
187 /**
188 * system property name constructing rule: "persist.device_config.[category_name].[flag_name]".
189 * If the name contains invalid characters or substrings for system property name,
190 * will return null.
191 * @param categoryName
192 * @param flagName
193 * @return
194 */
195 @VisibleForTesting
196 static String makePropertyName(String categoryName, String flagName) {
197 String propertyName = SYSTEM_PROPERTY_PREFIX + categoryName + "." + flagName;
198
199 if (!propertyName.matches(SYSTEM_PROPERTY_VALID_CHARACTERS_REGEX)
200 || propertyName.contains(SYSTEM_PROPERTY_INVALID_SUBSTRING)) {
201 return null;
202 }
203
204 return propertyName;
205 }
206
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700207 private void setProperty(String key, String value) {
208 // Check if need to clear the property
209 if (value == null) {
210 // It's impossible to remove system property, therefore we check previous value to
211 // avoid setting an empty string if the property wasn't set.
212 if (TextUtils.isEmpty(systemPropertiesGet(key))) {
213 return;
214 }
215 value = "";
216 } else if (value.length() > SYSTEM_PROPERTY_MAX_LENGTH) {
217 log(value + " exceeds system property max length.");
218 return;
219 }
220
221 try {
222 systemPropertiesSet(key, value);
223 } catch (Exception e) {
224 // Failure to set a property can be caused by SELinux denial. This usually indicates
225 // that the property wasn't whitelisted in sepolicy.
226 // No need to report it on all user devices, only on debug builds.
227 log("Unable to set property " + key + " value '" + value + "'", e);
228 }
229 }
230
231 private static void log(String msg, Exception e) {
232 if (Build.IS_DEBUGGABLE) {
233 Slog.wtf(TAG, msg, e);
234 } else {
235 Slog.e(TAG, msg, e);
236 }
237 }
238
239 private static void log(String msg) {
240 if (Build.IS_DEBUGGABLE) {
241 Slog.wtf(TAG, msg);
242 } else {
243 Slog.e(TAG, msg);
244 }
245 }
246
247 @VisibleForTesting
248 protected String systemPropertiesGet(String key) {
249 return SystemProperties.get(key);
250 }
251
252 @VisibleForTesting
253 protected void systemPropertiesSet(String key, String value) {
254 SystemProperties.set(key, value);
255 }
256
257 @VisibleForTesting
258 protected String getResetFlagsFileContent() {
259 String content = null;
260 try {
261 File reset_flag_file = new File(RESET_RECORD_FILE_PATH);
262 BufferedReader br = new BufferedReader(new FileReader(reset_flag_file));
263 content = br.readLine();
264
265 br.close();
266 } catch (IOException ioe) {
267 log("failed to read file " + RESET_RECORD_FILE_PATH, ioe);
268 }
269 return content;
270 }
271
272 @VisibleForTesting
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800273 void updatePropertyFromSetting(String settingName, String propName) {
274 String settingValue = Settings.Global.getString(mContentResolver, settingName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700275 setProperty(propName, settingValue);
276 }
277}