From 166856573b1b94c4645a362a9033e0a2bb0de1f3 Mon Sep 17 00:00:00 2001 From: duke Date: Mon, 30 Jun 2025 15:30:58 +0000 Subject: [PATCH] Backport ac4607ed81eb75f43e7d1062e38506972738d086 --- .../sun/tools/attach/VirtualMachineImpl.java | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java b/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java index 77ac7c85d60..235fe1c69d3 100644 --- a/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java +++ b/src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -275,11 +275,8 @@ public synchronized void close() throws IOException { } // Return the socket file for the given process. - private File findSocketFile(int pid, int ns_pid) { - // A process may not exist in the same mount namespace as the caller. - // Instead, attach relative to the target root filesystem as exposed by - // procfs regardless of namespaces. - String root = "/proc/" + pid + "/root/" + tmpdir; + private File findSocketFile(int pid, int ns_pid) throws IOException { + String root = findTargetProcessTmpDirectory(pid, ns_pid); return new File(root, ".java_pid" + ns_pid); } @@ -295,21 +292,34 @@ private File createAttachFile(int pid, int ns_pid) throws IOException { // Do not canonicalize the file path, or we will fail to attach to a VM in a container. f.createNewFile(); } catch (IOException x) { - String root; - if (pid != ns_pid) { - // A process may not exist in the same mount namespace as the caller. - // Instead, attach relative to the target root filesystem as exposed by - // procfs regardless of namespaces. - root = "/proc/" + pid + "/root/" + tmpdir; - } else { - root = tmpdir; - } + String root = findTargetProcessTmpDirectory(pid, ns_pid); f = new File(root, fn); f.createNewFile(); } return f; } + private String findTargetProcessTmpDirectory(int pid, int ns_pid) throws IOException { + String root; + if (pid != ns_pid) { + // A process may not exist in the same mount namespace as the caller, e.g. + // if we are trying to attach to a JVM process inside a container. + // Instead, attach relative to the target root filesystem as exposed by + // procfs regardless of namespaces. + String procRootDirectory = "/proc/" + pid + "/root"; + if (!Files.isReadable(Path.of(procRootDirectory))) { + throw new IOException( + String.format("Unable to access root directory %s " + + "of target process %d", procRootDirectory, pid)); + } + + root = procRootDirectory + "/" + tmpdir; + } else { + root = tmpdir; + } + return root; + } + /* * Write/sends the given to the target VM. String is transmitted in * UTF-8 encoding.