blob: 821940ac44320b13f4048ef3fcb28dcb3f1572d5 [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 */
16#include "linker.h"
17
18#include <android/dlext.h>
19#include <dlfcn.h>
20
21#define LOG_TAG "vndksupport"
22#include <log/log.h>
23
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080024__attribute__((weak)) extern struct android_namespace_t* android_get_exported_namespace(const char*);
25__attribute__((weak)) extern void* android_dlopen_ext(const char*, int, const android_dlextinfo*);
Jiyong Park7130e132017-05-11 02:15:24 +090026
Justin Yun090b5932017-07-11 18:58:51 +090027static const char* namespace_name = NULL;
28
29static struct android_namespace_t* get_vendor_namespace() {
30 const char* namespace_names[] = {"sphal", "default", NULL};
31 static struct android_namespace_t* vendor_namespace = NULL;
32 if (vendor_namespace == NULL) {
33 int name_idx = 0;
34 while (namespace_names[name_idx] != NULL) {
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080035 if (android_get_exported_namespace != NULL) {
36 vendor_namespace = android_get_exported_namespace(namespace_names[name_idx]);
37 }
Justin Yun090b5932017-07-11 18:58:51 +090038 if (vendor_namespace != NULL) {
39 namespace_name = namespace_names[name_idx];
40 break;
41 }
42 name_idx++;
43 }
44 }
45 return vendor_namespace;
46}
47
Vic Yanga117f252019-01-09 12:38:24 -080048int android_is_in_vendor_process() {
49 if (android_get_exported_namespace == NULL) {
50 ALOGD("android_get_exported_namespace() not available. Assuming system process.");
51 return 0;
52 }
53
54 // In vendor process, 'vndk' namespace is not visible, whereas in system
55 // process, it is.
56 return android_get_exported_namespace("vndk") == NULL;
57}
58
Jiyong Park7130e132017-05-11 02:15:24 +090059void* android_load_sphal_library(const char* name, int flag) {
Justin Yun090b5932017-07-11 18:58:51 +090060 struct android_namespace_t* vendor_namespace = get_vendor_namespace();
61 if (vendor_namespace != NULL) {
Jiyong Park7130e132017-05-11 02:15:24 +090062 const android_dlextinfo dlextinfo = {
Justin Yun090b5932017-07-11 18:58:51 +090063 .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = vendor_namespace,
Jiyong Park7130e132017-05-11 02:15:24 +090064 };
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080065 void* handle = NULL;
66 if (android_dlopen_ext != NULL) {
67 handle = android_dlopen_ext(name, flag, &dlextinfo);
68 }
Justin Yun17baed12017-05-22 14:31:56 +090069 if (!handle) {
Justin Yun090b5932017-07-11 18:58:51 +090070 ALOGE("Could not load %s from %s namespace: %s.", name, namespace_name, dlerror());
Jiyong Park7130e132017-05-11 02:15:24 +090071 }
Justin Yun17baed12017-05-22 14:31:56 +090072 return handle;
Jiyong Park7130e132017-05-11 02:15:24 +090073 } else {
Justin Yun090b5932017-07-11 18:58:51 +090074 ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
Justin Yun17baed12017-05-22 14:31:56 +090075 return dlopen(name, flag);
Jiyong Park7130e132017-05-11 02:15:24 +090076 }
Jiyong Park7130e132017-05-11 02:15:24 +090077}
78
79int android_unload_sphal_library(void* handle) {
80 return dlclose(handle);
81}