System.Data.SQLite
Check-in [cedca9ac3f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Another experimental change to the native library pre-loader for POSIX.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | netStandard20
Files: files | file ages | folders
SHA1: cedca9ac3f10cd69b478a1df22cd89997b4643c6
User & Date: mistachkin 2018-04-07 10:59:09
Context
2018-04-07
13:30
More work on the native library pre-loader for POSIX. check-in: c62df132f4 user: mistachkin tags: netStandard20
10:59
Another experimental change to the native library pre-loader for POSIX. check-in: cedca9ac3f user: mistachkin tags: netStandard20
10:42
Experimental enhancement to the native library pre-loader on POSIX. check-in: f04b575ad8 user: mistachkin tags: netStandard20
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to System.Data.SQLite/UnsafeNativeMethods.cs.

   952    952         /// Ensures that the directory containing the specified file is included
   953    953         /// in the POSIX shared library search path via the environment variable
   954    954         /// LD_LIBRARY_PATH.
   955    955         /// </summary>
   956    956         /// <param name="fileName">
   957    957         /// The file name of the native library that was successfully loaded.
   958    958         /// </param>
   959         -      private static void MaybeUpdateLdLibraryPath(
          959  +      /// <returns>
          960  +      /// Non-zero if the POSIX shared library path was updated; otherwise,
          961  +      /// zero.
          962  +      /// </returns>
          963  +      private static bool MaybeUpdateLdLibraryPath(
   960    964             string fileName
   961    965             )
   962    966         {
   963    967             string directory = Path.GetDirectoryName(fileName);
   964    968   
   965    969             if (!String.IsNullOrEmpty(directory))
   966    970             {
................................................................................
   976    980                     }
   977    981                     else
   978    982                     {
   979    983                         path = directory;
   980    984                     }
   981    985   
   982    986                     Environment.SetEnvironmentVariable(LdLibraryPath, path);
          987  +                  return true;
   983    988                 }
   984    989             }
          990  +
          991  +          return false;
   985    992         }
   986    993   
   987    994         /////////////////////////////////////////////////////////////////////////
   988    995   
   989    996         /// <summary>
   990    997         /// Attempts to load the specified native library file using the POSIX
   991    998         /// API.
................................................................................
  1000   1007             string fileName
  1001   1008             )
  1002   1009         {
  1003   1010             IntPtr nativeModuleHandle = UnsafeNativeMethodsPosix.dlopen(
  1004   1011                 fileName, UnsafeNativeMethodsPosix.RTLD_DEFAULT);
  1005   1012   
  1006   1013             if (nativeModuleHandle != IntPtr.Zero)
  1007         -              MaybeUpdateLdLibraryPath(fileName);
         1014  +          {
         1015  +              if (MaybeUpdateLdLibraryPath(fileName))
         1016  +              {
         1017  +                  UnsafeNativeMethodsPosix.dlclose(nativeModuleHandle);
         1018  +                  nativeModuleHandle = IntPtr.Zero;
         1019  +              }
         1020  +          }
  1008   1021   
  1009   1022             return nativeModuleHandle;
  1010   1023         }
  1011   1024   #endif
  1012   1025         #endregion
  1013   1026   
  1014   1027         /////////////////////////////////////////////////////////////////////////
................................................................................
  1140   1153         [DllImport("__Internal",
  1141   1154   #endif
  1142   1155             EntryPoint = "dlopen",
  1143   1156             CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi,
  1144   1157             BestFitMapping = false, ThrowOnUnmappableChar = true,
  1145   1158             SetLastError = true)]
  1146   1159         internal static extern IntPtr dlopen(string fileName, int mode);
         1160  +
         1161  +      /////////////////////////////////////////////////////////////////////////
         1162  +      /// <summary>
         1163  +      /// This is the P/Invoke method that wraps the native Unix dlclose
         1164  +      /// function.  See the POSIX documentation for full details on what it
         1165  +      /// does.
         1166  +      /// </summary>
         1167  +      /// <param name="module">
         1168  +      /// The handle to the loaded native library.
         1169  +      /// </param>
         1170  +      /// <returns>
         1171  +      /// Zero upon success -OR- non-zero on failure.
         1172  +      /// </returns>
         1173  +#if NET_STANDARD_20
         1174  +      [DllImport("libdl",
         1175  +#else
         1176  +      [DllImport("__Internal",
         1177  +#endif
         1178  +          EntryPoint = "dlclose",
         1179  +          CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
         1180  +      internal static extern int dlclose(IntPtr module);
  1147   1181   
  1148   1182         /////////////////////////////////////////////////////////////////////////
  1149   1183   
  1150   1184         #region Private Constants
  1151   1185         /// <summary>
  1152   1186         /// For use with dlopen(), bind function calls lazily.
  1153   1187         /// </summary>