blob: 98b84a4c2ac00886ad21dfb4bcdc87f02199dbaa [file] [log] [blame]
Anton Hanssonda4972f2020-01-08 09:48:18 +00001/*
2 * Copyright (C) 2019 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#define LOG_TAG "derive_sdk"
18
Anton Hanssondddcb572020-11-24 09:32:14 +000019#include "derive_sdk.h"
20
Anton Hanssonda4972f2020-01-08 09:48:18 +000021#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/properties.h>
Anton Hanssonb1a217c2020-11-30 11:43:05 +000024#include <android-modules-utils/sdk_level.h>
Anton Hanssone4fd55c2020-12-29 17:46:30 +000025#include <dirent.h>
26#include <sys/stat.h>
27
28#include <algorithm>
29#include <iostream>
30#include <vector>
Anton Hanssonda4972f2020-01-08 09:48:18 +000031
Anton Hansson502b7752021-01-06 12:16:54 +000032#include "packages/modules/SdkExtensions/proto/sdk.pb.h"
Anton Hanssonda4972f2020-01-08 09:48:18 +000033
Anton Hanssondddcb572020-11-24 09:32:14 +000034namespace android {
35namespace derivesdk {
36
Anton Hansson16097ea2021-01-12 20:34:32 +000037static const std::unordered_map<std::string, SdkModule> kApexNameToModule = {
38 {"com.android.ipsec", SdkModule::IPSEC},
39 {"com.android.media", SdkModule::MEDIA},
40 {"com.android.mediaprovider", SdkModule::MEDIA_PROVIDER},
41 {"com.android.permission", SdkModule::PERMISSIONS},
42 {"com.android.sdkext", SdkModule::SDK_EXTENSIONS},
43 {"com.android.os.statsd", SdkModule::STATSD},
44 {"com.android.tethering", SdkModule::TETHERING},
45};
46
47static const std::unordered_set<SdkModule> kRModules = {
48 SdkModule::IPSEC, SdkModule::MEDIA,
49 SdkModule::MEDIA_PROVIDER, SdkModule::PERMISSIONS,
50 SdkModule::SDK_EXTENSIONS, SdkModule::STATSD,
51 SdkModule::TETHERING,
52};
53
54static const std::unordered_set<SdkModule> kSModules = {};
55
56bool ReadDatabase(const std::string& db_path, ExtensionDatabase& db) {
57 std::string contents;
58 if (!android::base::ReadFileToString(db_path, &contents, true)) {
59 PLOG(ERROR) << "failed to read " << db_path << ": ";
60 return false;
61 }
62 if (!db.ParseFromString(contents)) {
63 LOG(ERROR) << "failed to parse " << db_path;
64 return false;
65 }
66 return true;
67}
68
69bool VersionRequirementsMet(
70 const ExtensionVersion& ext_version,
71 const std::unordered_set<SdkModule>& relevant_modules,
72 const std::unordered_map<SdkModule, int>& module_versions) {
73 for (const auto& requirement : ext_version.requirements()) {
74 // Only requirements on modules relevant for this extension matter.
75 if (relevant_modules.find(requirement.module()) == relevant_modules.end())
76 continue;
77
78 auto version = module_versions.find(requirement.module());
79 if (version == module_versions.end()) {
80 LOG(DEBUG) << "Not version " << ext_version.version() << ": Module "
81 << requirement.module() << " is missing";
82 return false;
83 }
84 if (version->second < requirement.version().version()) {
85 LOG(DEBUG) << "Not version " << ext_version.version() << ": Module "
86 << requirement.module() << " version (" << version->second
87 << ") too low. Needed " << requirement.version().version();
88 return false;
89 }
90 }
91 return true;
92}
93
94int GetSdkLevel(const ExtensionDatabase& db,
95 const std::unordered_set<SdkModule>& relevant_modules,
96 const std::unordered_map<SdkModule, int>& module_versions) {
97 int max = 0;
98
99 for (const auto& ext_version : db.versions()) {
100 if (ext_version.version() > max &&
101 VersionRequirementsMet(ext_version, relevant_modules,
102 module_versions)) {
103 max = ext_version.version();
104 }
105 }
106 return max;
107}
108
Anton Hanssondddcb572020-11-24 09:32:14 +0000109bool SetSdkLevels(const std::string& mountpath) {
Anton Hansson16097ea2021-01-12 20:34:32 +0000110 ExtensionDatabase db;
111 if (!ReadDatabase(
112 mountpath + "/com.android.sdkext/etc/extensions_db.binarypb", db)) {
113 LOG(ERROR) << "Failed to read database";
114 return false;
115 }
Anton Hanssondddcb572020-11-24 09:32:14 +0000116 std::unique_ptr<DIR, decltype(&closedir)> apex(opendir(mountpath.c_str()),
117 closedir);
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000118 if (!apex) {
Anton Hanssondddcb572020-11-24 09:32:14 +0000119 LOG(ERROR) << "Could not read " + mountpath;
120 return false;
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000121 }
122 struct dirent* de;
Anton Hansson16097ea2021-01-12 20:34:32 +0000123 std::unordered_map<SdkModule, int> versions;
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000124 while ((de = readdir(apex.get()))) {
125 std::string name = de->d_name;
126 if (name[0] == '.' || name.find('@') != std::string::npos) {
127 // Skip <name>@<ver> dirs, as they are bind-mounted to <name>
128 continue;
Anton Hanssonda4972f2020-01-08 09:48:18 +0000129 }
Anton Hanssondddcb572020-11-24 09:32:14 +0000130 std::string path = mountpath + "/" + name + "/etc/sdkinfo.binarypb";
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000131 struct stat statbuf;
Anton Hansson16097ea2021-01-12 20:34:32 +0000132 if (stat(path.c_str(), &statbuf) != 0) {
133 continue;
Anton Hanssonda4972f2020-01-08 09:48:18 +0000134 }
Anton Hansson16097ea2021-01-12 20:34:32 +0000135 auto module_itr = kApexNameToModule.find(name);
136 if (module_itr == kApexNameToModule.end()) {
137 LOG(WARNING) << "Found sdkinfo in unexpected apex " << name;
138 continue;
139 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000140 std::string contents;
141 if (!android::base::ReadFileToString(path, &contents, true)) {
142 LOG(ERROR) << "failed to read " << path;
143 continue;
Anton Hanssonda4972f2020-01-08 09:48:18 +0000144 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000145 SdkVersion sdk_version;
146 if (!sdk_version.ParseFromString(contents)) {
147 LOG(ERROR) << "failed to parse " << path;
148 continue;
149 }
Anton Hansson16097ea2021-01-12 20:34:32 +0000150 SdkModule module = module_itr->second;
151 LOG(INFO) << "Read version " << sdk_version.version() << " from " << module;
152 versions[module] = sdk_version.version();
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000153 }
Anton Hanssonda4972f2020-01-08 09:48:18 +0000154
Anton Hansson16097ea2021-01-12 20:34:32 +0000155 int version_R = GetSdkLevel(db, kRModules, versions);
156 LOG(INFO) << "R extension version is " << version_R;
157
158 if (!android::base::SetProperty("build.version.extensions.r",
159 std::to_string(version_R))) {
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000160 LOG(ERROR) << "failed to set r sdk_info prop";
Anton Hanssondddcb572020-11-24 09:32:14 +0000161 return false;
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000162 }
163 if (android::modules::sdklevel::IsAtLeastS()) {
Anton Hansson16097ea2021-01-12 20:34:32 +0000164 int version_S = GetSdkLevel(db, kSModules, versions);
165 LOG(INFO) << "S extension version is " << version_S;
166 if (!android::base::SetProperty("build.version.extensions.s",
167 std::to_string(version_S))) {
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000168 LOG(ERROR) << "failed to set s sdk_info prop";
Anton Hanssondddcb572020-11-24 09:32:14 +0000169 return false;
Anton Hanssonda4972f2020-01-08 09:48:18 +0000170 }
Anton Hanssone4fd55c2020-12-29 17:46:30 +0000171 }
Anton Hanssonda4972f2020-01-08 09:48:18 +0000172
Anton Hanssondddcb572020-11-24 09:32:14 +0000173 return true;
Anton Hanssonda4972f2020-01-08 09:48:18 +0000174}
Anton Hanssondddcb572020-11-24 09:32:14 +0000175
176} // namespace derivesdk
177} // namespace android