1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.internal.nicl;
  24 
  25 import java.io.File;
  26 import java.lang.reflect.Field;
  27 import java.nicl.Library;
  28 import java.nicl.NativeScope;
  29 import java.nicl.Scope;
  30 import java.nicl.types.Pointer;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import static sun.security.action.GetPropertyAction.privilegedGetProperty;
  34 import static jdk.internal.nicl.UnixDynamicLibraries.*;
  35 
  36 public class LdLoader extends LibraryLoader {
  37     private static final boolean DEBUG = Boolean.parseBoolean(
  38         privilegedGetProperty("jdk.internal.nicl.LdLoader.DEBUG"));
  39 
  40     private final String[] usr_paths;
  41 
  42     // preloaded/built-in library
  43     private final UnixLibrary defaultLibrary;
  44 
  45     private String[] getUserClassPath() {
  46         return AccessController.doPrivileged((PrivilegedAction<String[]>)() -> {
  47             try {
  48                 Field f = ClassLoader.class.getDeclaredField("usr_paths");
  49                 f.setAccessible(true);
  50                 return (String[])f.get(null);
  51             } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
  52                 throw new RuntimeException(e);
  53             }
  54         });
  55     }
  56 
  57     public LdLoader() {
  58         this.usr_paths = getUserClassPath();
  59         this.defaultLibrary = new UnixLibrary(null);
  60     }
  61 
  62     @Override
  63     public Library getDefaultLibrary() {
  64         return defaultLibrary;
  65     }
  66 
  67     private Library tryLoadLibrary(String libPath) {
  68         if (DEBUG) {
  69             System.err.println("Trying " + libPath);
  70         }
  71 
  72         try (Scope scope = new NativeScope()) {
  73             Pointer<Byte> cname = scope.toCString(libPath);
  74 
  75             try {
  76                 Pointer<Void> handle = UnixDynamicLibraries.getInstance().dlopen(cname, RTLD_LAZY);
  77                 if (handle != null) {
  78                     return new UnixLibrary(handle);
  79                 }
  80 
  81                 if (DEBUG) {
  82                     Pointer<Byte> err = UnixDynamicLibraries.getInstance().dlerror();
  83                     if (err != null) {
  84                         System.err.println(Pointer.toString(err));
  85                     }
  86                 }
  87 
  88                 throw new UnsatisfiedLinkError("Can't load library: " + libPath);
  89             } catch (Throwable t) {
  90                 throw new RuntimeException(t);
  91             }
  92         }
  93     }
  94 
  95     @Override
  96     public Library load(String name, boolean isAbsolute) {
  97         if (DEBUG) {
  98             System.err.println("Library.load(U, " + name + ", " + isAbsolute + ")");
  99         }
 100 
 101         if (isAbsolute) {
 102             return tryLoadLibrary(name);
 103         }
 104 
 105         for (String usr_path : usr_paths) {
 106             String libPath = usr_path + File.separator + name;
 107             try {
 108                 return tryLoadLibrary(libPath);
 109             } catch (UnsatisfiedLinkError e) {
 110                 // ignore and try next path
 111             }
 112         }
 113 
 114         throw new UnsatisfiedLinkError(name);
 115     }
 116 }