-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
329 lines (285 loc) · 9.03 KB
/
main.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <climits>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <android-base/result.h>
#include "linkerconfig/apex.h"
#include "linkerconfig/apexconfig.h"
#include "linkerconfig/baseconfig.h"
#include "linkerconfig/context.h"
#include "linkerconfig/environment.h"
#include "linkerconfig/legacy.h"
#include "linkerconfig/log.h"
#include "linkerconfig/namespacebuilder.h"
#include "linkerconfig/recovery.h"
#include "linkerconfig/variableloader.h"
#include "linkerconfig/variables.h"
using android::base::ErrnoError;
using android::base::Error;
using android::base::Result;
using android::linkerconfig::contents::Context;
using android::linkerconfig::modules::ApexInfo;
using android::linkerconfig::modules::Configuration;
namespace {
const static struct option program_options[] = {
{"target", required_argument, 0, 't'},
{"strict", no_argument, 0, 's'},
#ifndef __ANDROID__
{"root", required_argument, 0, 'r'},
{"vndk", required_argument, 0, 'v'},
{"recovery", no_argument, 0, 'y'},
{"legacy", no_argument, 0, 'l'},
#endif
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
struct ProgramArgs {
std::string target_directory;
bool strict;
std::string root;
std::string vndk_version;
bool is_recovery;
bool is_legacy;
};
[[noreturn]] void PrintUsage(int status = EXIT_SUCCESS) {
std::cerr << "Usage : linkerconfig [--target <target_directory>]"
" [--strict]"
#ifndef __ANDROID__
" --root <root dir>"
" --vndk <vndk version>"
" --recovery"
" --legacy"
#endif
" [--help]"
<< std::endl;
exit(status);
}
std::string RealPath(std::string_view path) {
char resolved_path[PATH_MAX];
if (realpath(path.data(), resolved_path) != nullptr) {
return resolved_path;
}
PrintUsage(-1);
}
bool ParseArgs(int argc, char* argv[], ProgramArgs* args) {
int parse_result;
while ((parse_result = getopt_long(
argc, argv, "t:sr:v:hyl", program_options, NULL)) != -1) {
switch (parse_result) {
case 't':
args->target_directory = optarg;
break;
case 's':
args->strict = true;
break;
case 'r':
args->root = RealPath(optarg);
break;
case 'v':
args->vndk_version = optarg;
break;
case 'y':
args->is_recovery = true;
break;
case 'l':
args->is_legacy = true;
break;
case 'h':
PrintUsage();
default:
return false;
}
}
if (optind < argc) {
return false;
}
return true;
}
void LoadVariables(ProgramArgs args) {
#ifndef __ANDROID__
if (!args.is_recovery && (args.root == "" || args.vndk_version == "")) {
PrintUsage();
}
android::linkerconfig::modules::Variables::AddValue("ro.vndk.version",
args.vndk_version);
#endif
if (!args.is_recovery) {
android::linkerconfig::generator::LoadVariables(args.root);
}
}
Result<void> WriteConfigurationToFile(Configuration& conf,
std::string file_path) {
std::ostream* out = &std::cout;
std::ofstream file_out;
if (file_path != "") {
file_out.open(file_path);
if (file_out.fail()) {
return ErrnoError() << "Failed to open file " << file_path;
}
out = &file_out;
}
android::linkerconfig::modules::ConfigWriter config_writer;
conf.WriteConfig(config_writer);
*out << config_writer.ToString();
if (!out->good()) {
return ErrnoError() << "Failed to write content to " << file_path;
}
return {};
}
Result<void> UpdatePermission([[maybe_unused]] const std::string& file_path) {
#ifdef __ANDROID__
if (fchmodat(AT_FDCWD,
file_path.c_str(),
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH,
AT_SYMLINK_NOFOLLOW) < 0) {
return ErrnoError() << "Failed to update permission of " << file_path;
}
#endif
return {};
}
Context GetContext(ProgramArgs args) {
auto apex_list = android::linkerconfig::modules::ScanActiveApexes(args.root);
Context ctx;
for (auto const& apex_item : apex_list) {
auto apex_info = apex_item.second;
if (apex_info.has_bin || apex_info.has_lib) {
ctx.AddApexModule(std::move(apex_info));
}
}
if (args.strict) {
ctx.SetStrictMode(true);
}
android::linkerconfig::contents::RegisterApexNamespaceBuilders(ctx);
return ctx;
}
Configuration GetConfiguration(Context& ctx) {
if (android::linkerconfig::modules::IsRecoveryMode()) {
return android::linkerconfig::contents::CreateRecoveryConfiguration(ctx);
}
if (android::linkerconfig::modules::IsLegacyDevice()) {
return android::linkerconfig::contents::CreateLegacyConfiguration(ctx);
}
// Use base configuration in default
return android::linkerconfig::contents::CreateBaseConfiguration(ctx);
}
Result<void> GenerateConfiguration(Configuration config, std::string dir_path,
bool update_permission) {
std::string file_path = "";
if (dir_path != "") {
file_path = dir_path + "/ld.config.txt";
}
auto write_config = WriteConfigurationToFile(config, file_path);
if (!write_config.ok()) {
return write_config;
} else if (update_permission && file_path != "") {
return UpdatePermission(file_path);
}
return {};
}
Result<void> GenerateBaseLinkerConfiguration(Context& ctx,
const std::string& dir_path) {
return GenerateConfiguration(GetConfiguration(ctx), dir_path, true);
}
Result<void> GenerateRecoveryLinkerConfiguration(Context& ctx,
const std::string& dir_path) {
return GenerateConfiguration(
android::linkerconfig::contents::CreateRecoveryConfiguration(ctx),
dir_path,
false);
}
Result<void> GenerateLegacyLinkerConfiguration(Context& ctx,
const std::string& dir_path) {
return GenerateConfiguration(
android::linkerconfig::contents::CreateLegacyConfiguration(ctx),
dir_path,
false);
}
Result<void> GenerateApexConfiguration(
const std::string& base_dir, android::linkerconfig::contents::Context& ctx,
const android::linkerconfig::modules::ApexInfo& target_apex) {
std::string dir_path = base_dir + "/" + target_apex.name;
if (auto ret = mkdir(dir_path.c_str(), 0755); ret != 0 && errno != EEXIST) {
return ErrnoError() << "Failed to create directory " << dir_path;
}
return GenerateConfiguration(
android::linkerconfig::contents::CreateApexConfiguration(ctx, target_apex),
dir_path,
true);
}
void GenerateApexConfigurations(Context& ctx, const std::string& dir_path) {
for (auto const& apex_item : ctx.GetApexModules()) {
if (apex_item.has_bin) {
auto result = GenerateApexConfiguration(dir_path, ctx, apex_item);
if (!result.ok()) {
LOG(WARNING) << result.error();
}
}
}
}
void ExitOnFailure(Result<void> task) {
if (!task.ok()) {
LOG(FATAL) << task.error();
exit(EXIT_FAILURE);
}
}
#ifdef __ANDROID__
struct CombinedLogger {
android::base::LogdLogger logd;
void operator()(android::base::LogId id, android::base::LogSeverity severity,
const char* tag, const char* file, unsigned int line,
const char* message) {
logd(id, severity, tag, file, line, message);
KernelLogger(id, severity, tag, file, line, message);
}
};
#endif
} // namespace
int main(int argc, char* argv[]) {
android::base::InitLogging(argv
#ifdef __ANDROID__
,
CombinedLogger()
#endif
);
ProgramArgs args = {};
if (!ParseArgs(argc, argv, &args)) {
PrintUsage(EXIT_FAILURE);
}
LoadVariables(args);
Context ctx = GetContext(args);
// when exec'ed from init, this is 0x0077, which makes the subdirectories
// inaccessible for others. set umask to 0x0022 so that they can be
// accessible.
umask(0x0022);
if (args.is_recovery) {
ExitOnFailure(
GenerateRecoveryLinkerConfiguration(ctx, args.target_directory));
} else if (args.is_legacy) {
ExitOnFailure(GenerateLegacyLinkerConfiguration(ctx, args.target_directory));
} else {
ExitOnFailure(GenerateBaseLinkerConfiguration(ctx, args.target_directory));
GenerateApexConfigurations(ctx, args.target_directory);
}
return EXIT_SUCCESS;
}