1 /*
2  * Copyright (C) 2015 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 #include <dlfcn.h>
18 
19 static const char* g_local_string = "This string is local to root library";
20 extern "C" const char* g_private_extern_string;
21 extern "C" const char* g_public_extern_string;
22 
23 bool g_dlopened = false;
24 
ns_get_local_string()25 extern "C" const char* ns_get_local_string() {
26   return g_local_string;
27 }
28 
ns_get_private_extern_string()29 extern "C" const char* ns_get_private_extern_string() {
30   return g_private_extern_string;
31 }
32 
ns_get_public_extern_string()33 extern "C" const char* ns_get_public_extern_string() {
34   return g_public_extern_string;
35 }
36 
ns_get_dlopened_string()37 extern "C" const char* ns_get_dlopened_string() {
38   void* handle = dlopen("libnstest_dlopened.so", RTLD_NOW | RTLD_GLOBAL);
39   if (handle == nullptr) {
40     return nullptr;
41   }
42 
43   const char** result = static_cast<const char**>(dlsym(handle, "g_private_dlopened_string"));
44   if (result == nullptr) {
45     return nullptr;
46   } else {
47     g_dlopened = true;
48   }
49 
50   return *result;
51 }
52