blob: cf0f6186dacd25c74e1b7a754aa3528161ce4191 [file] [log] [blame]
Jiyong Park7130e132017-05-11 02:15:24 +09001/*
2 * Copyright (C) 2017 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 */
Ryan Prichard8c733612019-07-15 13:40:05 -070016
17#define LOG_TAG "vndksupport"
18
Jiyong Park7130e132017-05-11 02:15:24 +090019#include "linker.h"
20
21#include <android/dlext.h>
22#include <dlfcn.h>
Jiyong Park7130e132017-05-11 02:15:24 +090023#include <log/log.h>
Jayant Chowdharya229d3e2019-03-11 08:26:22 -070024#include <sys/types.h>
25#include <unistd.h>
Jiyong Park7130e132017-05-11 02:15:24 +090026
Ryan Prichard8c733612019-07-15 13:40:05 -070027#include <initializer_list>
28
29__attribute__((weak)) extern "C" android_namespace_t* android_get_exported_namespace(const char*);
Ryan Prichard807d9d62019-07-15 13:35:31 -070030__attribute__((weak)) extern "C" void* android_dlopen_ext(const char*, int,
31 const android_dlextinfo*);
Jiyong Park7130e132017-05-11 02:15:24 +090032
Ryan Prichard8c733612019-07-15 13:40:05 -070033namespace {
Justin Yun090b5932017-07-11 18:58:51 +090034
Ryan Prichard8c733612019-07-15 13:40:05 -070035struct VendorNamespace {
36 android_namespace_t* ptr = nullptr;
37 const char* name = nullptr;
38};
39
40} // anonymous namespace
41
42static VendorNamespace get_vendor_namespace() {
43 static VendorNamespace result = ([] {
44 for (const char* name : {"sphal", "default"}) {
45 if (android_get_exported_namespace != nullptr) {
46 if (android_namespace_t* ns = android_get_exported_namespace(name)) {
47 return VendorNamespace{ns, name};
48 }
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080049 }
Justin Yun090b5932017-07-11 18:58:51 +090050 }
Ryan Prichard8c733612019-07-15 13:40:05 -070051 return VendorNamespace{};
52 })();
53 return result;
Justin Yun090b5932017-07-11 18:58:51 +090054}
55
Vic Yanga117f252019-01-09 12:38:24 -080056int android_is_in_vendor_process() {
Jayant Chowdharya229d3e2019-03-11 08:26:22 -070057 // Special case init, since when init runs, ld.config.<ver>.txt hasn't been
58 // loaded (sysprop service isn't up for init to know <ver>).
59 if (getpid() == 1) {
60 return 0;
61 }
Ryan Prichard8c733612019-07-15 13:40:05 -070062 if (android_get_exported_namespace == nullptr) {
Vic Yanga117f252019-01-09 12:38:24 -080063 ALOGD("android_get_exported_namespace() not available. Assuming system process.");
64 return 0;
65 }
66
67 // In vendor process, 'vndk' namespace is not visible, whereas in system
68 // process, it is.
Ryan Prichard8c733612019-07-15 13:40:05 -070069 return android_get_exported_namespace("vndk") == nullptr;
Vic Yanga117f252019-01-09 12:38:24 -080070}
71
Jiyong Park7130e132017-05-11 02:15:24 +090072void* android_load_sphal_library(const char* name, int flag) {
Ryan Prichard8c733612019-07-15 13:40:05 -070073 VendorNamespace vendor_namespace = get_vendor_namespace();
74 if (vendor_namespace.ptr != nullptr) {
Jiyong Park7130e132017-05-11 02:15:24 +090075 const android_dlextinfo dlextinfo = {
Ryan Prichard807d9d62019-07-15 13:35:31 -070076 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Ryan Prichard8c733612019-07-15 13:40:05 -070077 .library_namespace = vendor_namespace.ptr,
Jiyong Park7130e132017-05-11 02:15:24 +090078 };
Ryan Prichard8c733612019-07-15 13:40:05 -070079 void* handle = nullptr;
80 if (android_dlopen_ext != nullptr) {
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080081 handle = android_dlopen_ext(name, flag, &dlextinfo);
82 }
Justin Yun17baed12017-05-22 14:31:56 +090083 if (!handle) {
Ryan Prichard8c733612019-07-15 13:40:05 -070084 ALOGE("Could not load %s from %s namespace: %s.", name, vendor_namespace.name,
85 dlerror());
Jiyong Park7130e132017-05-11 02:15:24 +090086 }
Justin Yun17baed12017-05-22 14:31:56 +090087 return handle;
Jiyong Park7130e132017-05-11 02:15:24 +090088 } else {
Justin Yun090b5932017-07-11 18:58:51 +090089 ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
Justin Yun17baed12017-05-22 14:31:56 +090090 return dlopen(name, flag);
Jiyong Park7130e132017-05-11 02:15:24 +090091 }
Jiyong Park7130e132017-05-11 02:15:24 +090092}
93
94int android_unload_sphal_library(void* handle) {
95 return dlclose(handle);
96}