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 // copy of ClassLoader.usr_paths field 41 private final String[] usr_paths; 42 43 // preloaded/built-in library 44 private final UnixLibrary defaultLibrary; 45 46 private String[] getUserLibraryPaths() { 47 // ClassLoader.usr_paths is initialized from "java.library.path" System property. 48 // FIXME: we should define and use a private API rather than reading a 49 // private static non-final lazily initialized field from ClassLoader. 50 return AccessController.doPrivileged((PrivilegedAction<String[]>)() -> { 51 try { 52 Field f = ClassLoader.class.getDeclaredField("usr_paths"); 53 f.setAccessible(true); 54 return (String[])f.get(null); 55 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 56 throw new RuntimeException(e); 57 } 58 }); 59 } 60 61 public LdLoader() { 62 this.usr_paths = getUserLibraryPaths(); 63 this.defaultLibrary = new UnixLibrary(null); 64 } 65 66 @Override 67 public Library getDefaultLibrary() { 68 return defaultLibrary; 69 } 70 71 private Library tryLoadLibrary(String libPath) { 72 if (DEBUG) { 73 System.err.println("Trying " + libPath); 74 } 75 76 try (Scope scope = new NativeScope()) { 77 Pointer<Byte> cname = scope.toCString(libPath); 78 79 try { 80 Pointer<Void> handle = UnixDynamicLibraries.getInstance().dlopen(cname, RTLD_LAZY); 81 if (handle != null) { 82 return new UnixLibrary(handle); 83 } 84 85 if (DEBUG) { 86 Pointer<Byte> err = UnixDynamicLibraries.getInstance().dlerror(); 87 if (err != null) { 88 System.err.println(Pointer.toString(err)); 89 } 90 } 91 92 throw new UnsatisfiedLinkError("Can't load library: " + libPath); 93 } catch (RuntimeException re) { 94 throw re; 95 } catch (Exception e) { 96 throw new RuntimeException(e); 97 } 98 } 99 } 100 101 @Override 102 public Library load(String name, boolean isAbsolute) { 103 if (DEBUG) { 104 System.err.println("Library.load(U, " + name + ", " + isAbsolute + ")"); 105 } 106 107 if (isAbsolute) { 108 return tryLoadLibrary(name); 109 } 110 111 for (String usr_path : usr_paths) { 112 String libPath = usr_path + File.separator + name; 113 try { 114 return tryLoadLibrary(libPath); 115 } catch (RuntimeException | UnsatisfiedLinkError e) { 116 if (DEBUG) { 117 System.err.println(e); 118 } 119 // ignore and try next path 120 } 121 } 122 123 throw new UnsatisfiedLinkError(name); 124 } 125 }