diff --git a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java index 8b7aeeefbd..4fcf557798 100644 --- a/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java +++ b/core/src/main/java/jenkins/slaves/restarter/UnixSlaveRestarter.java @@ -6,14 +6,13 @@ import static hudson.util.jna.GNUCLibrary.F_SETFD; import static hudson.util.jna.GNUCLibrary.LIBC; import static java.util.logging.Level.FINE; -import com.sun.jna.Memory; import com.sun.jna.Native; -import com.sun.jna.NativeLong; import com.sun.jna.StringArray; import hudson.Extension; import java.io.File; import java.io.IOException; -import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; import java.util.List; import java.util.logging.Logger; import jenkins.util.JavaVMArguments; @@ -73,11 +72,8 @@ public class UnixSlaveRestarter extends SlaveRestarter { File exe = new File(name); if (exe.exists()) { try { - String path = resolveSymlink(exe); - if (path != null) { - return path; - } - } catch (IOException e) { + return Files.readSymbolicLink(exe.toPath()).toString(); + } catch (IOException | InvalidPathException | UnsupportedOperationException e) { LOGGER.log(FINE, "Failed to resolve symlink " + exe, e); } return name; @@ -87,33 +83,6 @@ public class UnixSlaveRestarter extends SlaveRestarter { return System.getProperty("java.home") + "/bin/java"; } - private static String resolveSymlink(File link) throws IOException { - String filename = link.getAbsolutePath(); - - for (int sz = 512; sz < 65536; sz *= 2) { - Memory m = new Memory(sz); - int r = LIBC.readlink(filename, m, new NativeLong(sz)); - if (r < 0) { - int err = Native.getLastError(); - if (err == 22 /*EINVAL --- but is this really portable?*/) { - return null; // this means it's not a symlink - } - throw new IOException( - "Failed to readlink " + link + " error=" + err + " " + LIBC.strerror(err)); - } - - if (r == sz) { - continue; // buffer too small - } - - byte[] buf = new byte[r]; - m.read(0, buf, 0, r); - return new String(buf, StandardCharsets.UTF_8); - } - - throw new IOException("Failed to readlink " + link); - } - private static final Logger LOGGER = Logger.getLogger(UnixSlaveRestarter.class.getName()); private static final long serialVersionUID = 1L;