Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 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 | #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 Chowdhary | a229d3e | 2019-03-11 08:26:22 -0700 | [diff] [blame^] | 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 25 | |
Yifan Hong | 1ccdb7d | 2017-11-09 13:08:18 -0800 | [diff] [blame] | 26 | __attribute__((weak)) extern struct android_namespace_t* android_get_exported_namespace(const char*); |
| 27 | __attribute__((weak)) extern void* android_dlopen_ext(const char*, int, const android_dlextinfo*); |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 28 | |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 29 | static const char* namespace_name = NULL; |
| 30 | |
| 31 | static struct android_namespace_t* get_vendor_namespace() { |
| 32 | const char* namespace_names[] = {"sphal", "default", NULL}; |
| 33 | static struct android_namespace_t* vendor_namespace = NULL; |
| 34 | if (vendor_namespace == NULL) { |
| 35 | int name_idx = 0; |
| 36 | while (namespace_names[name_idx] != NULL) { |
Yifan Hong | 1ccdb7d | 2017-11-09 13:08:18 -0800 | [diff] [blame] | 37 | if (android_get_exported_namespace != NULL) { |
| 38 | vendor_namespace = android_get_exported_namespace(namespace_names[name_idx]); |
| 39 | } |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 40 | if (vendor_namespace != NULL) { |
| 41 | namespace_name = namespace_names[name_idx]; |
| 42 | break; |
| 43 | } |
| 44 | name_idx++; |
| 45 | } |
| 46 | } |
| 47 | return vendor_namespace; |
| 48 | } |
| 49 | |
Vic Yang | a117f25 | 2019-01-09 12:38:24 -0800 | [diff] [blame] | 50 | int android_is_in_vendor_process() { |
Jayant Chowdhary | a229d3e | 2019-03-11 08:26:22 -0700 | [diff] [blame^] | 51 | // Special case init, since when init runs, ld.config.<ver>.txt hasn't been |
| 52 | // loaded (sysprop service isn't up for init to know <ver>). |
| 53 | if (getpid() == 1) { |
| 54 | return 0; |
| 55 | } |
Vic Yang | a117f25 | 2019-01-09 12:38:24 -0800 | [diff] [blame] | 56 | if (android_get_exported_namespace == NULL) { |
| 57 | ALOGD("android_get_exported_namespace() not available. Assuming system process."); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | // In vendor process, 'vndk' namespace is not visible, whereas in system |
| 62 | // process, it is. |
| 63 | return android_get_exported_namespace("vndk") == NULL; |
| 64 | } |
| 65 | |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 66 | void* android_load_sphal_library(const char* name, int flag) { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 67 | struct android_namespace_t* vendor_namespace = get_vendor_namespace(); |
| 68 | if (vendor_namespace != NULL) { |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 69 | const android_dlextinfo dlextinfo = { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 70 | .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = vendor_namespace, |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 71 | }; |
Yifan Hong | 1ccdb7d | 2017-11-09 13:08:18 -0800 | [diff] [blame] | 72 | void* handle = NULL; |
| 73 | if (android_dlopen_ext != NULL) { |
| 74 | handle = android_dlopen_ext(name, flag, &dlextinfo); |
| 75 | } |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 76 | if (!handle) { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 77 | ALOGE("Could not load %s from %s namespace: %s.", name, namespace_name, dlerror()); |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 78 | } |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 79 | return handle; |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 80 | } else { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame] | 81 | ALOGD("Loading %s from current namespace instead of sphal namespace.", name); |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 82 | return dlopen(name, flag); |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 83 | } |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | int android_unload_sphal_library(void* handle) { |
| 87 | return dlclose(handle); |
| 88 | } |