blob: 894a704c5bba8be8ecdd83058baf37ec9312f8a7 [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.
Hongyi Zhang78a58252019-01-18 15:10:18 -080041 * @hide
Hongyi Zhang50a4e012018-09-26 21:20:03 -070042 */
Hongyi Zhang78a58252019-01-18 15:10:18 -080043public class SettingsToPropertiesMapper {
Hongyi Zhang50a4e012018-09-26 21:20:03 -070044
45 private static final String TAG = "SettingsToPropertiesMapper";
46
47 private static final String SYSTEM_PROPERTY_PREFIX = "persist.device_config.";
48
49 private static final String RESET_PERFORMED_PROPERTY = "device_config.reset_performed";
50
51 private static final String RESET_RECORD_FILE_PATH =
52 "/data/server_configurable_flags/reset_flags";
53
54 private static final String SYSTEM_PROPERTY_VALID_CHARACTERS_REGEX = "^[\\w\\.\\-@:]*$";
55
56 private static final String SYSTEM_PROPERTY_INVALID_SUBSTRING = "..";
57
58 private static final int SYSTEM_PROPERTY_MAX_LENGTH = 92;
59
60 // experiment flags added to Global.Settings(before new "Config" provider table is available)
61 // will be added under this category.
62 private static final String GLOBAL_SETTINGS_CATEGORY = "global_settings";
63
64 // Add the global setting you want to push to native level as experiment flag into this list.
65 //
66 // NOTE: please grant write permission system property prefix
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080067 // with format persist.device_config.global_settings.[flag_name] in system_server.te and grant
68 // read permission in the corresponding .te file your feature belongs to.
Hongyi Zhang50a4e012018-09-26 21:20:03 -070069 @VisibleForTesting
70 static final String[] sGlobalSettings = new String[] {
Hongyi Zhanga02118d2018-11-15 20:15:38 -080071 Settings.Global.NATIVE_FLAGS_HEALTH_CHECK_ENABLED,
Hongyi Zhang50a4e012018-09-26 21:20:03 -070072 };
73
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -080074 // All the flags under the listed DeviceConfig scopes will be synced to native level.
75 //
76 // NOTE: please grant write permission system property prefix
77 // with format persist.device_config.[device_config_scope]. in system_server.te and grant read
78 // permission in the corresponding .te file your feature belongs to.
Hongyi Zhang50a4e012018-09-26 21:20:03 -070079 @VisibleForTesting
80 static final String[] sDeviceConfigScopes = new String[] {
Ng Zhi Ane1bff3f2019-01-17 12:38:17 -080081 DeviceConfig.ActivityManagerNativeBoot.NAMESPACE,
Dongwon Kang5ebb2652019-01-30 15:26:46 -080082 DeviceConfig.MediaNative.NAMESPACE,
Siarhei Vishniakou39a1a982019-01-11 09:22:32 -080083 DeviceConfig.NAMESPACE_INPUT_NATIVE_BOOT,
chenbrucedb279e82018-12-27 17:12:31 +080084 DeviceConfig.NAMESPACE_NETD_NATIVE,
Mathieu Chartierb31138f2019-02-01 16:51:12 -080085 DeviceConfig.RuntimeNativeBoot.NAMESPACE,
Mathieu Chartier7f11eb92019-01-25 16:08:21 -080086 DeviceConfig.RuntimeNative.NAMESPACE,
Hongyi Zhang50a4e012018-09-26 21:20:03 -070087 };
88
89 private final String[] mGlobalSettings;
90
91 private final String[] mDeviceConfigScopes;
92
93 private final ContentResolver mContentResolver;
94
95 @VisibleForTesting
96 protected SettingsToPropertiesMapper(ContentResolver contentResolver,
97 String[] globalSettings,
98 String[] deviceConfigScopes) {
99 mContentResolver = contentResolver;
100 mGlobalSettings = globalSettings;
101 mDeviceConfigScopes = deviceConfigScopes;
102 }
103
104 @VisibleForTesting
105 void updatePropertiesFromSettings() {
106 for (String globalSetting : mGlobalSettings) {
107 Uri settingUri = Settings.Global.getUriFor(globalSetting);
108 String propName = makePropertyName(GLOBAL_SETTINGS_CATEGORY, globalSetting);
109 if (settingUri == null) {
110 log("setting uri is null for globalSetting " + globalSetting);
111 continue;
112 }
113 if (propName == null) {
114 log("invalid prop name for globalSetting " + globalSetting);
115 continue;
116 }
117
118 ContentObserver co = new ContentObserver(null) {
119 @Override
120 public void onChange(boolean selfChange) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800121 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700122 }
123 };
124
125 // only updating on starting up when no native flags reset is performed during current
126 // booting.
127 if (!isNativeFlagsResetPerformed()) {
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800128 updatePropertyFromSetting(globalSetting, propName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700129 }
130 mContentResolver.registerContentObserver(settingUri, false, co);
131 }
132
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800133 for (String deviceConfigScope : mDeviceConfigScopes) {
134 DeviceConfig.addOnPropertyChangedListener(
135 deviceConfigScope,
136 AsyncTask.THREAD_POOL_EXECUTOR,
137 (String scope, String name, String value) -> {
138 String propertyName = makePropertyName(scope, name);
139 if (propertyName == null) {
140 log("unable to construct system property for " + scope + "/" + name);
141 return;
142 }
143 setProperty(propertyName, value);
144 });
145 }
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700146 }
147
148 public static SettingsToPropertiesMapper start(ContentResolver contentResolver) {
149 SettingsToPropertiesMapper mapper = new SettingsToPropertiesMapper(
150 contentResolver, sGlobalSettings, sDeviceConfigScopes);
151 mapper.updatePropertiesFromSettings();
152 return mapper;
153 }
154
155 /**
156 * If native level flags reset has been performed as an attempt to recover from a crash loop
157 * during current device booting.
158 * @return
159 */
Hongyi Zhang78a58252019-01-18 15:10:18 -0800160 public static boolean isNativeFlagsResetPerformed() {
161 String value = SystemProperties.get(RESET_PERFORMED_PROPERTY);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700162 return "true".equals(value);
163 }
164
165 /**
166 * return an array of native flag categories under which flags got reset during current device
167 * booting.
168 * @return
169 */
Hongyi Zhang78a58252019-01-18 15:10:18 -0800170 public static String[] getResetNativeCategories() {
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700171 if (!isNativeFlagsResetPerformed()) {
172 return new String[0];
173 }
174
175 String content = getResetFlagsFileContent();
176 if (TextUtils.isEmpty(content)) {
177 return new String[0];
178 }
179
180 String[] property_names = content.split(";");
181 HashSet<String> categories = new HashSet<>();
182 for (String property_name : property_names) {
183 String[] segments = property_name.split("\\.");
184 if (segments.length < 3) {
185 log("failed to extract category name from property " + property_name);
186 continue;
187 }
188 categories.add(segments[2]);
189 }
190 return categories.toArray(new String[0]);
191 }
192
193 /**
194 * system property name constructing rule: "persist.device_config.[category_name].[flag_name]".
195 * If the name contains invalid characters or substrings for system property name,
196 * will return null.
197 * @param categoryName
198 * @param flagName
199 * @return
200 */
201 @VisibleForTesting
202 static String makePropertyName(String categoryName, String flagName) {
203 String propertyName = SYSTEM_PROPERTY_PREFIX + categoryName + "." + flagName;
204
205 if (!propertyName.matches(SYSTEM_PROPERTY_VALID_CHARACTERS_REGEX)
206 || propertyName.contains(SYSTEM_PROPERTY_INVALID_SUBSTRING)) {
207 return null;
208 }
209
210 return propertyName;
211 }
212
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700213 private void setProperty(String key, String value) {
214 // Check if need to clear the property
215 if (value == null) {
216 // It's impossible to remove system property, therefore we check previous value to
217 // avoid setting an empty string if the property wasn't set.
Hongyi Zhang78a58252019-01-18 15:10:18 -0800218 if (TextUtils.isEmpty(SystemProperties.get(key))) {
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700219 return;
220 }
221 value = "";
222 } else if (value.length() > SYSTEM_PROPERTY_MAX_LENGTH) {
223 log(value + " exceeds system property max length.");
224 return;
225 }
226
227 try {
Hongyi Zhang78a58252019-01-18 15:10:18 -0800228 SystemProperties.set(key, value);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700229 } catch (Exception e) {
230 // Failure to set a property can be caused by SELinux denial. This usually indicates
231 // that the property wasn't whitelisted in sepolicy.
232 // No need to report it on all user devices, only on debug builds.
233 log("Unable to set property " + key + " value '" + value + "'", e);
234 }
235 }
236
237 private static void log(String msg, Exception e) {
238 if (Build.IS_DEBUGGABLE) {
239 Slog.wtf(TAG, msg, e);
240 } else {
241 Slog.e(TAG, msg, e);
242 }
243 }
244
245 private static void log(String msg) {
246 if (Build.IS_DEBUGGABLE) {
247 Slog.wtf(TAG, msg);
248 } else {
249 Slog.e(TAG, msg);
250 }
251 }
252
253 @VisibleForTesting
Hongyi Zhang78a58252019-01-18 15:10:18 -0800254 static String getResetFlagsFileContent() {
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700255 String content = null;
256 try {
257 File reset_flag_file = new File(RESET_RECORD_FILE_PATH);
258 BufferedReader br = new BufferedReader(new FileReader(reset_flag_file));
259 content = br.readLine();
260
261 br.close();
262 } catch (IOException ioe) {
263 log("failed to read file " + RESET_RECORD_FILE_PATH, ioe);
264 }
265 return content;
266 }
267
268 @VisibleForTesting
Hongyi Zhangc4aad0e2018-12-11 14:36:06 -0800269 void updatePropertyFromSetting(String settingName, String propName) {
270 String settingValue = Settings.Global.getString(mContentResolver, settingName);
Hongyi Zhang50a4e012018-09-26 21:20:03 -0700271 setProperty(propName, settingValue);
272 }
Hongyi Zhang78a58252019-01-18 15:10:18 -0800273}