• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
17 // This binary is run on boot as a oneshot service. It should not be run at any
18 // other point.
19 
20 #include <string>
21 
22 #include "android-base/logging.h"
23 #include "android-base/properties.h"
24 
SetPropertyAndLog(const std::string & key,const std::string & value,const std::string & message="")25 static void SetPropertyAndLog(const std::string& key,
26                               const std::string& value,
27                               const std::string& message = "") {
28   if (android::base::SetProperty(key, value)) {
29     LOG(INFO) << "Set property " << key << " to " << value << " " << message;
30   } else {
31     LOG(ERROR) << "Failed to set property " << key << " to " << value << " " << message;
32   }
33 }
34 
35 // Copies the value of one system property to another if it isn't empty and
36 // passes the predicate test_fn.
CopyPropertyIf(const char * src,const char * dst,bool (* test_fn)(const std::string &))37 static void CopyPropertyIf(const char* src, const char* dst, bool (*test_fn)(const std::string&)) {
38   std::string prop = android::base::GetProperty(src, "");
39   if (prop.empty()) {
40     LOG(INFO) << "Property " << src << " not set";
41   } else if (!test_fn(prop)) {
42     LOG(INFO) << "Property " << src << " has ignored value " << prop;
43   } else {
44     SetPropertyAndLog(dst, prop, std::string("from ") + src);
45   }
46 }
47 
main(int,char ** argv)48 int main(int, char** argv) {
49   android::base::InitLogging(argv);
50 
51   // Copy properties that must only be set at boot and not change value later.
52   // Note that P/H can change the properties in the experiment namespaces at any
53   // time.
54   CopyPropertyIf("persist.device_config.runtime_native_boot.useartservice",
55                  "dalvik.vm.useartservice",
56                  // If an OEM has set dalvik.vm.useartservice to false we
57                  // shouldn't override it to true from the P/H property.
58                  [](const std::string& prop) { return prop == "false"; });
59 
60   return 0;
61 }
62