[Security] Fix PrivacyPrivilegeManager.RequestPermissions crash issue (#1651)
[platform/core/csapi/tizenfx.git] / internals / src / EflSharp / EflSharp / efl / NativeModule_Unix.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace Efl
5 {
6
7 namespace Eo
8 {
9
10 public partial class NativeModule
11 {
12     private const int RTLD_NOW = 0x002;
13     // Currently we are using GLOBAL due to issues
14     // with the way evas modules are built.
15     private const int RTLD_GLOBAL = 0x100;
16
17     [DllImport(efl.Libs.Libdl)]
18     private static extern IntPtr dlopen(string fileName, int flag);
19     [DllImport(efl.Libs.Libdl)]
20     private static extern int dlclose(IntPtr handle);
21
22     ///<summary>Closes the library handle.</summary>
23     ///<param name="handle">The handle to the library.</param>
24     public static void UnloadLibrary(IntPtr handle)
25     {
26         dlclose(handle);
27     }
28
29     ///<summary>Loads the given library.
30     ///
31     ///It attempts to load using the following list of names based on the <c>filename</c>
32     ///parameter:
33     ///
34     ///<list type="bullet">
35     ///<item>
36     ///<description><c>filename</c></description>
37     ///</item>
38     ///<item>
39     ///<description><c>libfilename</c></description>
40     ///</item>
41     ///<item>
42     ///<description><c>filename.so</c></description>
43     ///</item>
44     ///<item>
45     ///<description><c>libfilename.so</c></description>
46     ///</item>
47     ///</list>
48     ///</summary>
49     ///<param name="filename">The name to search for.</param>
50     ///<returns>The loaded library handle or <see cref="System.IntPtr.Zero"/> on failure.</returns>
51     public static IntPtr LoadLibrary(string filename)
52     {
53         Eina.Log.Debug($"Loading library {filename}");
54         var r = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
55         if (r == IntPtr.Zero)
56         {
57             r = dlopen("lib" + filename, RTLD_NOW | RTLD_GLOBAL);
58             if (r == IntPtr.Zero)
59             {
60                 r = dlopen(filename + ".so", RTLD_NOW | RTLD_GLOBAL);
61                 if (r == IntPtr.Zero)
62                 {
63                     r = dlopen("lib" + filename + ".so", RTLD_NOW | RTLD_GLOBAL);
64                 }
65             }
66         }
67
68         return r;
69     }
70 }
71
72 }
73
74 }