1 /*
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 */
16
17 #define LOG_TAG "vndksupport"
18
19 #include "linker.h"
20
21 #include <android/dlext.h>
22 #include <dlfcn.h>
23 #include <log/log.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <initializer_list>
28
29 __attribute__((weak)) extern "C" android_namespace_t* android_get_exported_namespace(const char*);
30 __attribute__((weak)) extern "C" void* android_dlopen_ext(const char*, int,
31 const android_dlextinfo*);
32
33 namespace {
34
35 struct VendorNamespace {
36 android_namespace_t* ptr = nullptr;
37 const char* name = nullptr;
38 };
39
40 } // anonymous namespace
41
get_vendor_namespace()42 static 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 }
49 }
50 }
51 return VendorNamespace{};
52 })();
53 return result;
54 }
55
android_is_in_vendor_process()56 int android_is_in_vendor_process() {
57 // 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 }
62 if (android_get_exported_namespace == nullptr) {
63 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.
69 return android_get_exported_namespace("vndk") == nullptr;
70 }
71
android_load_sphal_library(const char * name,int flag)72 void* android_load_sphal_library(const char* name, int flag) {
73 VendorNamespace vendor_namespace = get_vendor_namespace();
74 if (vendor_namespace.ptr != nullptr) {
75 const android_dlextinfo dlextinfo = {
76 .flags = ANDROID_DLEXT_USE_NAMESPACE,
77 .library_namespace = vendor_namespace.ptr,
78 };
79 void* handle = nullptr;
80 if (android_dlopen_ext != nullptr) {
81 handle = android_dlopen_ext(name, flag, &dlextinfo);
82 }
83 if (!handle) {
84 ALOGE("Could not load %s from %s namespace: %s.", name, vendor_namespace.name,
85 dlerror());
86 }
87 return handle;
88 } else {
89 ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
90 return dlopen(name, flag);
91 }
92 }
93
android_unload_sphal_library(void * handle)94 int android_unload_sphal_library(void* handle) {
95 return dlclose(handle);
96 }
97