Skip to content

Commit ac4607e

Browse files
slovdahlkevinjwalls
authored andcommitted
8226919: attach in linux hangs due to permission denied accessing /proc/pid/root
Reviewed-by: sgehwolf, kevinw
1 parent b42b888 commit ac4607e

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src/jdk.attach/linux/classes/sun/tools/attach/VirtualMachineImpl.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -210,11 +210,8 @@ protected void close(long fd) throws IOException {
210210
}
211211

212212
// Return the socket file for the given process.
213-
private File findSocketFile(int pid, int ns_pid) {
214-
// A process may not exist in the same mount namespace as the caller.
215-
// Instead, attach relative to the target root filesystem as exposed by
216-
// procfs regardless of namespaces.
217-
String root = "/proc/" + pid + "/root/" + tmpdir;
213+
private File findSocketFile(int pid, int ns_pid) throws IOException {
214+
String root = findTargetProcessTmpDirectory(pid, ns_pid);
218215
return new File(root, ".java_pid" + ns_pid);
219216
}
220217

@@ -230,21 +227,34 @@ private File createAttachFile(int pid, int ns_pid) throws IOException {
230227
// Do not canonicalize the file path, or we will fail to attach to a VM in a container.
231228
f.createNewFile();
232229
} catch (IOException x) {
233-
String root;
234-
if (pid != ns_pid) {
235-
// A process may not exist in the same mount namespace as the caller.
236-
// Instead, attach relative to the target root filesystem as exposed by
237-
// procfs regardless of namespaces.
238-
root = "/proc/" + pid + "/root/" + tmpdir;
239-
} else {
240-
root = tmpdir;
241-
}
230+
String root = findTargetProcessTmpDirectory(pid, ns_pid);
242231
f = new File(root, fn);
243232
f.createNewFile();
244233
}
245234
return f;
246235
}
247236

237+
private String findTargetProcessTmpDirectory(int pid, int ns_pid) throws IOException {
238+
String root;
239+
if (pid != ns_pid) {
240+
// A process may not exist in the same mount namespace as the caller, e.g.
241+
// if we are trying to attach to a JVM process inside a container.
242+
// Instead, attach relative to the target root filesystem as exposed by
243+
// procfs regardless of namespaces.
244+
String procRootDirectory = "/proc/" + pid + "/root";
245+
if (!Files.isReadable(Path.of(procRootDirectory))) {
246+
throw new IOException(
247+
String.format("Unable to access root directory %s " +
248+
"of target process %d", procRootDirectory, pid));
249+
}
250+
251+
root = procRootDirectory + "/" + tmpdir;
252+
} else {
253+
root = tmpdir;
254+
}
255+
return root;
256+
}
257+
248258
/*
249259
* Write/sends the given to the target VM. String is transmitted in
250260
* UTF-8 encoding.

0 commit comments

Comments
 (0)