blob: 30b9c2e28fbedb848c1b256ec44c6437d2e921e6 [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
Peter Collingbourne68319222019-12-17 15:24:00 -080029extern "C" android_namespace_t* android_get_exported_namespace(const char*);
Jiyong Park7130e132017-05-11 02:15:24 +090030
Ryan Prichard8c733612019-07-15 13:40:05 -070031namespace {
Justin Yun090b5932017-07-11 18:58:51 +090032
Ryan Prichard8c733612019-07-15 13:40:05 -070033struct VendorNamespace {
34 android_namespace_t* ptr = nullptr;
35 const char* name = nullptr;
36};
37
38} // anonymous namespace
39
40static VendorNamespace get_vendor_namespace() {
41 static VendorNamespace result = ([] {
42 for (const char* name : {"sphal", "default"}) {
Peter Collingbourne68319222019-12-17 15:24:00 -080043 if (android_namespace_t* ns = android_get_exported_namespace(name)) {
44 return VendorNamespace{ns, name};
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080045 }
Justin Yun090b5932017-07-11 18:58:51 +090046 }
Ryan Prichard8c733612019-07-15 13:40:05 -070047 return VendorNamespace{};
48 })();
49 return result;
Justin Yun090b5932017-07-11 18:58:51 +090050}
51
Vic Yanga117f252019-01-09 12:38:24 -080052int android_is_in_vendor_process() {
Jayant Chowdharya229d3e2019-03-11 08:26:22 -070053 // Special case init, since when init runs, ld.config.<ver>.txt hasn't been
54 // loaded (sysprop service isn't up for init to know <ver>).
55 if (getpid() == 1) {
56 return 0;
57 }
Vic Yanga117f252019-01-09 12:38:24 -080058
59 // In vendor process, 'vndk' namespace is not visible, whereas in system
60 // process, it is.
Ryan Prichard8c733612019-07-15 13:40:05 -070061 return android_get_exported_namespace("vndk") == nullptr;
Vic Yanga117f252019-01-09 12:38:24 -080062}
63
Jiyong Park7130e132017-05-11 02:15:24 +090064void* android_load_sphal_library(const char* name, int flag) {
Ryan Prichard8c733612019-07-15 13:40:05 -070065 VendorNamespace vendor_namespace = get_vendor_namespace();
66 if (vendor_namespace.ptr != nullptr) {
Jiyong Park7130e132017-05-11 02:15:24 +090067 const android_dlextinfo dlextinfo = {
Ryan Prichard807d9d62019-07-15 13:35:31 -070068 .flags = ANDROID_DLEXT_USE_NAMESPACE,
Ryan Prichard8c733612019-07-15 13:40:05 -070069 .library_namespace = vendor_namespace.ptr,
Jiyong Park7130e132017-05-11 02:15:24 +090070 };
Peter Collingbourne68319222019-12-17 15:24:00 -080071 void* handle = android_dlopen_ext(name, flag, &dlextinfo);
Justin Yun17baed12017-05-22 14:31:56 +090072 if (!handle) {
Ryan Prichard8c733612019-07-15 13:40:05 -070073 ALOGE("Could not load %s from %s namespace: %s.", name, vendor_namespace.name,
74 dlerror());
Jiyong Park7130e132017-05-11 02:15:24 +090075 }
Justin Yun17baed12017-05-22 14:31:56 +090076 return handle;
Jiyong Park7130e132017-05-11 02:15:24 +090077 } else {
Justin Yun090b5932017-07-11 18:58:51 +090078 ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
Justin Yun17baed12017-05-22 14:31:56 +090079 return dlopen(name, flag);
Jiyong Park7130e132017-05-11 02:15:24 +090080 }
Jiyong Park7130e132017-05-11 02:15:24 +090081}
82
83int android_unload_sphal_library(void* handle) {
84 return dlclose(handle);
85}