byte-buddy/no-unixsocket.patch
2018-12-04 11:04:29 +00:00

290 lines
12 KiB
Diff

--- byte-buddy-agent/pom.xml.orig 2018-11-30 11:43:29.016481386 +0000
+++ byte-buddy-agent/pom.xml 2018-11-30 11:43:42.676439662 +0000
@@ -15,7 +15,6 @@
<bytebuddy.agent>net.bytebuddy.agent.Installer</bytebuddy.agent>
<attach.package.sun>com.sun.tools.attach</attach.package.sun>
<attach.package.ibm>com.ibm.tools.attach</attach.package.ibm>
- <version.unixsocket>2.0.4</version.unixsocket>
<packages.list>net.bytebuddy.agent</packages.list>
</properties>
@@ -30,12 +29,6 @@
<dependencies>
<dependency>
- <groupId>com.kohlschutter.junixsocket</groupId>
- <artifactId>junixsocket-native-common</artifactId>
- <version>${version.unixsocket}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
--- byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java.orig 2018-11-30 11:22:23.274443432 +0000
+++ byte-buddy-agent/src/main/java/net/bytebuddy/agent/ByteBuddyAgent.java 2018-11-30 11:42:22.236685381 +0000
@@ -541,8 +541,7 @@
ForStandardToolsJarVm.JVM_ROOT,
ForStandardToolsJarVm.JDK_ROOT,
ForStandardToolsJarVm.MACINTOSH,
- ForUserDefinedToolsJar.INSTANCE,
- ForUnixHotSpotVm.INSTANCE);
+ ForUserDefinedToolsJar.INSTANCE);
/**
* Attempts the creation of an accessor for a specific JVM's attachment API.
@@ -922,28 +921,6 @@
}
}
}
-
- /**
- * An attachment provider using a custom protocol implementation for HotSpot on Unix.
- */
- enum ForUnixHotSpotVm implements AttachmentProvider {
-
- /**
- * The singleton instance.
- */
- INSTANCE;
-
- /**
- * {@inheritDoc}
- */
- public Accessor attempt() {
- try {
- return new Accessor.Simple.WithoutExternalAttachment(VirtualMachine.ForHotSpot.OnUnix.assertAvailability());
- } catch (Throwable ignored) {
- return Accessor.Unavailable.INSTANCE;
- }
- }
- }
/**
* A compound attachment provider that attempts the attachment by delegation to other providers. If
--- byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java.orig 2018-11-30 11:22:55.830341642 +0000
+++ byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java 2018-11-30 11:38:21.698438915 +0000
@@ -16,8 +16,6 @@
package net.bytebuddy.agent;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import org.newsclub.net.unix.AFUNIXSocket;
-import org.newsclub.net.unix.AFUNIXSocketAddress;
import java.io.File;
import java.io.IOException;
@@ -172,213 +170,5 @@
*/
protected abstract void write(byte[] buffer) throws IOException;
- /**
- * A virtual machine implementation for a HotSpot VM running on Unix.
- */
- public static class OnUnix extends ForHotSpot {
-
- /**
- * The default amount of attempts to connect.
- */
- private static final int DEFAULT_ATTEMPTS = 10;
-
- /**
- * The default pause between two attempts.
- */
- private static final long DEFAULT_PAUSE = 200;
-
- /**
- * The default socket timeout.
- */
- private static final long DEFAULT_TIMEOUT = 5000;
-
- /**
- * The temporary directory on Unix systems.
- */
- private static final String TEMPORARY_DIRECTORY = "/tmp";
-
- /**
- * The name prefix for a socket.
- */
- private static final String SOCKET_FILE_PREFIX = ".java_pid";
-
- /**
- * The name prefix for an attachment file indicator.
- */
- private static final String ATTACH_FILE_PREFIX = ".attach_pid";
-
- /**
- * The Unix socket to use for communication. The containing object is supposed to be an instance
- * of {@link AFUNIXSocket} which is however not set to avoid eager loading
- */
- private final Object socket;
-
- /**
- * The number of attempts to connect.
- */
- private final int attempts;
-
- /**
- * The time to pause between attempts.
- */
- private final long pause;
-
- /**
- * The socket timeout.
- */
- private final long timeout;
-
- /**
- * The time unit of the pause time.
- */
- private final TimeUnit timeUnit;
-
- /**
- * Creates a new VM implementation for a HotSpot VM running on Unix.
- *
- * @param processId The process id of the target VM.
- * @param socket The Unix socket to use for communication.
- * @param attempts The number of attempts to connect.
- * @param pause The pause time between two VMs.
- * @param timeout The socket timeout.
- * @param timeUnit The time unit of the pause time.
- */
- public OnUnix(String processId, Object socket, int attempts, long pause, long timeout, TimeUnit timeUnit) {
- super(processId);
- this.socket = socket;
- this.attempts = attempts;
- this.pause = pause;
- this.timeout = timeout;
- this.timeUnit = timeUnit;
- }
-
- /**
- * Asserts the availability of this virtual machine implementation. If the Unix socket library is missing or
- * if this VM does not support Unix socket communication, a {@link Throwable} is thrown.
- *
- * @return This virtual machine type.
- * @throws Throwable If this attachment method is not available.
- */
- public static Class<?> assertAvailability() throws Throwable {
- try {
- Class<?> moduleType = Class.forName("java.lang.Module");
- Method getModule = Class.class.getMethod("getModule"), canRead = moduleType.getMethod("canRead", moduleType);
- Object thisModule = getModule.invoke(OnUnix.class), otherModule = getModule.invoke(AFUNIXSocket.class);
- if (!(Boolean) canRead.invoke(thisModule, otherModule)) {
- moduleType.getMethod("addReads", moduleType).invoke(thisModule, otherModule);
- }
- return doAssertAvailability();
- } catch (ClassNotFoundException ignored) {
- return doAssertAvailability();
- }
- }
-
- /**
- * Asserts the availability of this virtual machine implementation.
- *
- * @return This virtual machine type.
- */
- private static Class<?> doAssertAvailability() {
- if (!AFUNIXSocket.isSupported()) {
- throw new IllegalStateException("POSIX sockets are not supported on the current system");
- } else if (!System.getProperty("java.vm.name").toLowerCase(Locale.US).contains("hotspot")) {
- throw new IllegalStateException("Cannot apply attachment on non-Hotspot compatible VM");
- } else {
- return OnUnix.class;
- }
- }
-
- /**
- * Attaches to the supplied VM process.
- *
- * @param processId The process id of the target VM.
- * @return An appropriate virtual machine implementation.
- * @throws IOException If an I/O exception occurs.
- */
- public static VirtualMachine attach(String processId) throws IOException {
- return new OnUnix(processId, AFUNIXSocket.newInstance(), DEFAULT_ATTEMPTS, DEFAULT_PAUSE, DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
- }
-
- /**
- * {@inheritDoc}
- */
- @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME", justification = "This is a Unix-specific implementation")
- protected void connect() throws IOException {
- File socketFile = new File(TEMPORARY_DIRECTORY, SOCKET_FILE_PREFIX + processId);
- if (!socketFile.exists()) {
- String target = ATTACH_FILE_PREFIX + processId, path = "/proc/" + processId + "/cwd/" + target;
- File attachFile = new File(path);
- try {
- if (!attachFile.createNewFile() && !attachFile.isFile()) {
- throw new IllegalStateException("Could not create attach file: " + attachFile);
- }
- } catch (IOException ignored) {
- attachFile = new File(TEMPORARY_DIRECTORY, target);
- if (!attachFile.createNewFile() && !attachFile.isFile()) {
- throw new IllegalStateException("Could not create attach file: " + attachFile);
- }
- }
- try {
- // The HotSpot attachment API attempts to send the signal to all children of a process
- Process process = Runtime.getRuntime().exec("kill -3 " + processId);
- int attempts = this.attempts;
- boolean killed = false;
- do {
- try {
- if (process.exitValue() != 0) {
- throw new IllegalStateException("Error while sending signal to target VM: " + processId);
- }
- killed = true;
- break;
- } catch (IllegalThreadStateException ignored) {
- attempts -= 1;
- Thread.sleep(timeUnit.toMillis(pause));
- }
- } while (attempts > 0);
- if (!killed) {
- throw new IllegalStateException("Target VM did not respond to signal: " + processId);
- }
- attempts = this.attempts;
- while (attempts-- > 0 && !socketFile.exists()) {
- Thread.sleep(timeUnit.toMillis(pause));
- }
- if (!socketFile.exists()) {
- throw new IllegalStateException("Target VM did not respond: " + processId);
- }
- } catch (InterruptedException exception) {
- throw new IllegalStateException("Interrupted during wait for process", exception);
- } finally {
- if (!attachFile.delete()) {
- attachFile.deleteOnExit();
- }
- }
- }
- if (timeout != 0) {
- ((AFUNIXSocket) socket).setSoTimeout((int) timeUnit.toMillis(timeout));
- }
- ((AFUNIXSocket) socket).connect(new AFUNIXSocketAddress(socketFile));
- }
-
- /**
- * {@inheritDoc}
- */
- public int read(byte[] buffer) throws IOException {
- return ((AFUNIXSocket) this.socket).getInputStream().read(buffer);
- }
-
- /**
- * {@inheritDoc}
- */
- public void write(byte[] buffer) throws IOException {
- ((AFUNIXSocket) this.socket).getOutputStream().write(buffer);
- }
-
- /**
- * {@inheritDoc}
- */
- public void detach() throws IOException {
- ((AFUNIXSocket) this.socket).close();
- }
- }
}
}