• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 extern "C" android_namespace_t* android_get_exported_namespace(const char*);
30 
31 namespace {
32 
33 struct VendorNamespace {
34     android_namespace_t* ptr = nullptr;
35     const char* name = nullptr;
36 };
37 
38 }  // anonymous namespace
39 
get_vendor_namespace()40 static VendorNamespace get_vendor_namespace() {
41     static VendorNamespace result = ([] {
42         for (const char* name : {"sphal", "default"}) {
43             if (android_namespace_t* ns = android_get_exported_namespace(name)) {
44                 return VendorNamespace{ns, name};
45             }
46         }
47         return VendorNamespace{};
48     })();
49     return result;
50 }
51 
android_is_in_vendor_process()52 int android_is_in_vendor_process() {
53     // 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     }
58 
59     // In vendor process, 'vndk' namespace is not visible, whereas in system
60     // process, it is.
61     return android_get_exported_namespace("vndk") == nullptr;
62 }
63 
android_load_sphal_library(const char * name,int flag)64 void* android_load_sphal_library(const char* name, int flag) {
65     VendorNamespace vendor_namespace = get_vendor_namespace();
66     if (vendor_namespace.ptr != nullptr) {
67         const android_dlextinfo dlextinfo = {
68                 .flags = ANDROID_DLEXT_USE_NAMESPACE,
69                 .library_namespace = vendor_namespace.ptr,
70         };
71         void* handle = android_dlopen_ext(name, flag, &dlextinfo);
72         if (!handle) {
73             ALOGE("Could not load %s from %s namespace: %s.", name, vendor_namespace.name,
74                   dlerror());
75         }
76         return handle;
77     } else {
78         ALOGD("Loading %s from current namespace instead of sphal namespace.", name);
79         return dlopen(name, flag);
80     }
81 }
82 
android_unload_sphal_library(void * handle)83 int android_unload_sphal_library(void* handle) {
84     return dlclose(handle);
85 }
86