blob: e731f345b70488f60942d5821dca609461a55615 [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[] {
Ng Zhi Ane1bff3f2019-01-17 12:38:17 -080080 DeviceConfig.ActivityManagerNativeBoot.NAMESPACE,
Siarhei Vishniakou39a1a982019-01-11 09:22:32 -080081 DeviceConfig.NAMESPACE_INPUT_NATIVE_BOOT,
chenbrucedb279e82018-12-27 17:12:31 +080082 DeviceConfig.NAMESPACE_NETD_NATIVE,
Mathieu Chartier7f11eb92019-01-25 16:08:21 -080083 DeviceConfig.RuntimeNative.NAMESPACE,
Hongyi Zhang50a4e012018-09-26 21:20:03 -070084 };
85
86 private final String[] mGlobalSettings;
87
88 private final String[] mDeviceConfigScopes;
89
90 private final ContentResolver mContentResolver;
91
92 @VisibleForTesting
93 protected SettingsToPropertiesMapper(ContentResolver contentResolver,
94 String[] globalSettings,
95 String[] deviceConfigScopes) {
96 mContentResolver = contentResolver;
97 mGlobalSettings = globalSettings;
98 mDeviceConfigScopes = deviceConfigScopes;
99 }
100
101 @VisibleForTesting
102 void updatePropertiesFromSettings() {
103 for (String globalSetting : mGlobalSettings) {
104 Uri settingUri = Settings.Global.getUriFor(globalSetting);
105 String propName = makePropertyName(GLOBAL_SETTINGS_CATEGORY, globalSetting);
106 if (settingUri == null) {
107 log("setting uri is null for globalSetting " + globalSetting);
108 continue;
109 }
110 if (propName == null) {
111 log("invalid prop name for globalSetting " + globalSetting);
112 continue;
113 }
114
115 ContentObserver co = new ContentObserver(null) {
116 @Override
117 public void onChange(boolean selfChange) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800118 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700119 }
120 };
121
122 // only updating on starting up when no native flags reset is performed during current
123 // booting.
124 if (!isNativeFlagsResetPerformed()) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800125 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700126 }
127 mContentResolver.registerContentObserver(settingUri, false, co);
128 }
129
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800130 for (String deviceConfigScope : mDeviceConfigScopes) {
131 DeviceConfig.addOnPropertyChangedListener(
132 deviceConfigScope,
133 AsyncTask.THREAD_POOL_EXECUTOR,
134 (String scope, String name, String value) -> {
135 String propertyName = makePropertyName(scope, name);
136 if (propertyName == null) {
137 log("unable to construct system property for " + scope + "/" + name);
138 return;
139 }
140 setProperty(propertyName, value);
141 });
142 }
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700143 }
144
145 public static SettingsToPropertiesMapper start(ContentResolver contentResolver) {
146 SettingsToPropertiesMapper mapper = new SettingsToPropertiesMapper(
147 contentResolver, sGlobalSettings, sDeviceConfigScopes);
148 mapper.updatePropertiesFromSettings();
149 return mapper;
150 }
151
152 /**
153 * If native level flags reset has been performed as an attempt to recover from a crash loop
154 * during current device booting.
155 * @return
156 */
157 public boolean isNativeFlagsResetPerformed() {
158 String value = systemPropertiesGet(RESET_PERFORMED_PROPERTY);
159 return "true".equals(value);
160 }
161
162 /**
163 * return an array of native flag categories under which flags got reset during current device
164 * booting.
165 * @return
166 */
167 public String[] getResetNativeCategories() {
168 if (!isNativeFlagsResetPerformed()) {
169 return new String[0];
170 }
171
172 String content = getResetFlagsFileContent();
173 if (TextUtils.isEmpty(content)) {
174 return new String[0];
175 }
176
177 String[] property_names = content.split(";");
178 HashSet<String> categories = new HashSet<>();
179 for (String property_name : property_names) {
180 String[] segments = property_name.split("\\.");
181 if (segments.length < 3) {
182 log("failed to extract category name from property " + property_name);
183 continue;
184 }
185 categories.add(segments[2]);
186 }
187 return categories.toArray(new String[0]);
188 }
189
190 /**
191 * system property name constructing rule: "persist.device_config.[category_name].[flag_name]".
192 * If the name contains invalid characters or substrings for system property name,
193 * will return null.
194 * @param categoryName
195 * @param flagName
196 * @return
197 */
198 @VisibleForTesting
199 static String makePropertyName(String categoryName, String flagName) {
200 String propertyName = SYSTEM_PROPERTY_PREFIX + categoryName + "." + flagName;
201
202 if (!propertyName.matches(SYSTEM_PROPERTY_VALID_CHARACTERS_REGEX)
203 || propertyName.contains(SYSTEM_PROPERTY_INVALID_SUBSTRING)) {
204 return null;
205 }
206
207 return propertyName;
208 }
209
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700210 private void setProperty(String key, String value) {
211 // Check if need to clear the property
212 if (value == null) {
213 // It's impossible to remove system property, therefore we check previous value to
214 // avoid setting an empty string if the property wasn't set.
215 if (TextUtils.isEmpty(systemPropertiesGet(key))) {
216 return;
217 }
218 value = "";
219 } else if (value.length() > SYSTEM_PROPERTY_MAX_LENGTH) {
220 log(value + " exceeds system property max length.");
221 return;
222 }
223
224 try {
225 systemPropertiesSet(key, value);
226 } catch (Exception e) {
227 // Failure to set a property can be caused by SELinux denial. This usually indicates
228 // that the property wasn't whitelisted in sepolicy.
229 // No need to report it on all user devices, only on debug builds.
230 log("Unable to set property " + key + " value '" + value + "'", e);
231 }
232 }
233
234 private static void log(String msg, Exception e) {
235 if (Build.IS_DEBUGGABLE) {
236 Slog.wtf(TAG, msg, e);
237 } else {
238 Slog.e(TAG, msg, e);
239 }
240 }
241
242 private static void log(String msg) {
243 if (Build.IS_DEBUGGABLE) {
244 Slog.wtf(TAG, msg);
245 } else {
246 Slog.e(TAG, msg);
247 }
248 }
249
250 @VisibleForTesting
251 protected String systemPropertiesGet(String key) {
252 return SystemProperties.get(key);
253 }
254
255 @VisibleForTesting
256 protected void systemPropertiesSet(String key, String value) {
257 SystemProperties.set(key, value);
258 }
259
260 @VisibleForTesting
261 protected String getResetFlagsFileContent() {
262 String content = null;
263 try {
264 File reset_flag_file = new File(RESET_RECORD_FILE_PATH);
265 BufferedReader br = new BufferedReader(new FileReader(reset_flag_file));
266 content = br.readLine();
267
268 br.close();
269 } catch (IOException ioe) {
270 log("failed to read file " + RESET_RECORD_FILE_PATH, ioe);
271 }
272 return content;
273 }
274
275 @VisibleForTesting
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800276 void updatePropertyFromSetting(String settingName, String propName) {
277 String settingValue = Settings.Global.getString(mContentResolver, settingName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700278 setProperty(propName, settingValue);
279 }
280}