blob: 057e5b197da6c0880894f211cc3c44d3f2c068c1 [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>
Jayant Chowdharya229d3e2019-03-11 08:26:22 -070023#include <sys/types.h>
24#include <unistd.h>
Jiyong Park7130e132017-05-11 02:15:24 +090025
Ryan Prichard807d9d62019-07-15 13:35:31 -070026__attribute__((weak)) extern "C" struct android_namespace_t* android_get_exported_namespace(
27 const char*);
28__attribute__((weak)) extern "C" void* android_dlopen_ext(const char*, int,
29 const android_dlextinfo*);
Jiyong Park7130e132017-05-11 02:15:24 +090030
Justin Yun090b5932017-07-11 18:58:51 +090031static const char* namespace_name = NULL;
32
33static struct android_namespace_t* get_vendor_namespace() {
34 const char* namespace_names[] = {"sphal", "default", NULL};
35 static struct android_namespace_t* vendor_namespace = NULL;
36 if (vendor_namespace == NULL) {
37 int name_idx = 0;
38 while (namespace_names[name_idx] != NULL) {
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080039 if (android_get_exported_namespace != NULL) {
40 vendor_namespace = android_get_exported_namespace(namespace_names[name_idx]);
41 }
Justin Yun090b5932017-07-11 18:58:51 +090042 if (vendor_namespace != NULL) {
43 namespace_name = namespace_names[name_idx];
44 break;
45 }
46 name_idx++;
47 }
48 }
49 return vendor_namespace;
50}
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 if (android_get_exported_namespace == NULL) {
59 ALOGD("android_get_exported_namespace() not available. Assuming system process.");
60 return 0;
61 }
62
63 // In vendor process, 'vndk' namespace is not visible, whereas in system
64 // process, it is.
65 return android_get_exported_namespace("vndk") == NULL;
66}
67
Jiyong Park7130e132017-05-11 02:15:24 +090068void* android_load_sphal_library(const char* name, int flag) {
Justin Yun090b5932017-07-11 18:58:51 +090069 struct android_namespace_t* vendor_namespace = get_vendor_namespace();
70 if (vendor_namespace != NULL) {
Jiyong Park7130e132017-05-11 02:15:24 +090071 const android_dlextinfo dlextinfo = {
Ryan Prichard807d9d62019-07-15 13:35:31 -070072 .flags = ANDROID_DLEXT_USE_NAMESPACE,
73 .library_namespace = vendor_namespace,
Jiyong Park7130e132017-05-11 02:15:24 +090074 };
Yifan Hong1ccdb7d2017-11-09 13:08:18 -080075 void* handle = NULL;
76 if (android_dlopen_ext != NULL) {
77 handle = android_dlopen_ext(name, flag, &dlextinfo);
78 }
Justin Yun17baed12017-05-22 14:31:56 +090079 if (!handle) {
Justin Yun090b5932017-07-11 18:58:51 +090080 ALOGE("Could not load %s from %s namespace: %s.", name, namespace_name, dlerror());
Jiyong Park7130e132017-05-11 02:15:24 +090081 }
Justin Yun17baed12017-05-22 14:31:56 +090082 return handle;
Jiyong Park7130e132017-05-11 02:15:24 +090083 } else {
Justin Yun090b5932017-07-11 18:58:51 +090084 ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
Justin Yun17baed12017-05-22 14:31:56 +090085 return dlopen(name, flag);
Jiyong Park7130e132017-05-11 02:15:24 +090086 }
Jiyong Park7130e132017-05-11 02:15:24 +090087}
88
89int android_unload_sphal_library(void* handle) {
90 return dlclose(handle);
91}