-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8319589: Attach from root to a user java process not supported in Mac #25824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
9fd3781
f35e7e6
779a1b3
de855d3
9ca4aaf
4c96cca
75dd6fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,13 @@ | |
import com.sun.tools.attach.AttachNotSupportedException; | ||
import com.sun.tools.attach.spi.AttachProvider; | ||
|
||
import sun.jvmstat.PlatformSupport; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is convenient but I'm not sure it is appropriate. Need the serviceability folk to comment. |
||
|
||
import java.io.InputStream; | ||
import java.io.IOException; | ||
import java.io.File; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
|
@@ -39,14 +43,17 @@ | |
*/ | ||
@SuppressWarnings("restricted") | ||
public class VirtualMachineImpl extends HotSpotVirtualMachine { | ||
// "tmpdir" is used as a global well-known location for the files | ||
// .java_pid<pid>. and .attach_pid<pid>. It is important that this | ||
// location is the same for all processes, otherwise the tools | ||
// will not be able to find all Hotspot processes. | ||
// This is intentionally not the same as java.io.tmpdir, since | ||
// the latter can be changed by the user. | ||
// Any changes to this needs to be synchronized with HotSpot. | ||
private static final String tmpdir; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we can't cache this any more? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks David for pointing this out. A different instance with a different PID may have a different temp path, so the path should be specific to the instance. Since we don't need the path outside the constructor, I think we don't need to cache it. On the other hand I see that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would save it in a local in the constructor and pass it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
/** | ||
* HotSpot PerfData file prefix | ||
*/ | ||
private static final String HSPERFDATA_PREFIX = "hsperfdata_"; | ||
|
||
/** | ||
* Use platform specific methods for looking up temporary directories. | ||
*/ | ||
private static final PlatformSupport platformSupport = PlatformSupport.getInstance(); | ||
|
||
String socket_path; | ||
private OperationProperties props = new OperationProperties(VERSION_1); // updated in ctor | ||
|
||
|
@@ -67,7 +74,8 @@ public class VirtualMachineImpl extends HotSpotVirtualMachine { | |
// Find the socket file. If not found then we attempt to start the | ||
// attach mechanism in the target VM by sending it a QUIT signal. | ||
// Then we attempt to find the socket file again. | ||
File socket_file = new File(tmpdir, ".java_pid" + pid); | ||
// In macOS the socket file is located in per-user temp directory. | ||
File socket_file = new File(getTempDirFromPid(pid), ".java_pid" + pid); | ||
socket_path = socket_file.getPath(); | ||
if (!socket_file.exists()) { | ||
File f = createAttachFile(pid); | ||
|
@@ -212,11 +220,34 @@ protected void close(long fd) throws IOException { | |
} | ||
|
||
private File createAttachFile(int pid) throws IOException { | ||
File f = new File(tmpdir, ".attach_pid" + pid); | ||
// In macOS the attach file is created in the target user temp directory | ||
File f = new File(getTempDirFromPid(pid), ".attach_pid" + pid); | ||
createAttachFile0(f.getPath()); | ||
return f; | ||
} | ||
|
||
/* | ||
* Returns a platform-specific temporary directory for a given process. | ||
* In VMs running as unprivileged user it returns the default platform-specific | ||
* temporary directory. In VMs running as root it searches over the list of | ||
* temporary directories for one containing HotSpot PerfData directory. | ||
*/ | ||
private String getTempDirFromPid(int pid) { | ||
ProcessHandle ph = ProcessHandle.of(pid).orElse(null); | ||
if (ph != null) { | ||
String user = ph.info().user().orElse(null); | ||
if (user != null) { | ||
for (String dir : platformSupport.getTemporaryDirectories(pid)) { | ||
Path fullPath = Path.of(dir, HSPERFDATA_PREFIX + user, String.valueOf(pid)); | ||
if(Files.exists(fullPath)) { | ||
sercher marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return dir; | ||
} | ||
} | ||
} | ||
} | ||
return PlatformSupport.getTemporaryDirectory(); | ||
} | ||
|
||
//-- native methods | ||
|
||
static native boolean checkCatchesAndSendQuitTo(int pid, boolean throwIfNotReady) throws IOException, AttachNotSupportedException; | ||
|
@@ -239,6 +270,5 @@ private File createAttachFile(int pid) throws IOException { | |
|
||
static { | ||
System.loadLibrary("attach"); | ||
tmpdir = getTempDir(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2025, BELLSOFT. 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 | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. Oracle designates this | ||
* particular file as subject to the "Classpath" exception as provided | ||
* by Oracle in the LICENSE file that accompanied this code. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
package sun.jvmstat; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/* | ||
* macOS specific implementation of the PlatformSupport routines | ||
* providing temporary directory support. | ||
*/ | ||
public class PlatformSupportImpl extends PlatformSupport { | ||
|
||
private static final String VAR_FOLDERS_PATH = "/var/folders"; | ||
private static final String USER_NAME_SYSTEM_PROPERTY = "user.name"; | ||
private static final String USER_NAME_ROOT = "root"; | ||
private static final String DIRHELPER_TEMP_STR = "T"; | ||
|
||
private static final boolean isCurrentUserRoot = | ||
System.getProperty(USER_NAME_SYSTEM_PROPERTY).equals(USER_NAME_ROOT); | ||
|
||
public PlatformSupportImpl() { | ||
super(); | ||
} | ||
|
||
/* | ||
* Return a list of the temporary directories that the VM uses | ||
* for the attach and perf data files. | ||
* | ||
* This function returns the traditional temp directory. Additionally, | ||
* when called by root, it returns other temporary directories of non-root | ||
* users. | ||
* | ||
* macOS per-user temp directories are located under /var/folders | ||
* and have the form /var/folders/<BUCKET>/<ENCODED_UUID_UID>/T | ||
*/ | ||
@Override | ||
public List<String> getTemporaryDirectories(int pid) { | ||
if (!isCurrentUserRoot) { | ||
// early exit for non-root | ||
return List.of(PlatformSupport.getTemporaryDirectory()); | ||
} | ||
List<String> result = new ArrayList<>(); | ||
try (DirectoryStream<Path> bs = Files.newDirectoryStream(Path.of(VAR_FOLDERS_PATH))) { | ||
for (Path bucket : bs) { | ||
try (DirectoryStream<Path> encUuids = Files.newDirectoryStream(bucket)) { | ||
for (Path encUuid : encUuids) { | ||
try { | ||
Path tempDir = encUuid.resolve(DIRHELPER_TEMP_STR); | ||
if (Files.isDirectory(tempDir) && Files.isReadable(tempDir)) { | ||
result.add(tempDir.toString()); | ||
} | ||
} catch (Exception ignore) { // ignored unreadable bucket/encUuid, continue | ||
} | ||
} | ||
} catch (IOException ignore) { // IOException ignored, continue to the next bucket | ||
} | ||
} | ||
} catch (Exception ignore) { // var/folders directory is inaccessible / other errors | ||
} | ||
return result.isEmpty() ? List.of(PlatformSupport.getTemporaryDirectory()) : result; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit too much non-posix code in the posix file IMO. I'd rather see a
MACOS_ONLY
call later on to something defined inos_bsd.cpp
for macOS.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @dholmes-ora , I moved the function to
os_bsd.cpp