changeset 4965:ab842d66b05a

7141739: [osx] Local attach fails if java.io.tmpdir is set Summary: Make sure Hotspot and jdk looks for well-known files in the same locations Reviewed-by: dcubed
author sla
date Fri, 03 Feb 2012 11:58:10 +0100
parents d56728a282c6
children 228092b90efb
files src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java src/solaris/native/sun/tools/attach/BsdVirtualMachine.c
diffstat 2 files changed, 42 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java	Thu Feb 02 11:38:26 2012 -0800
+++ b/src/solaris/classes/sun/tools/attach/BsdVirtualMachine.java	Fri Feb 03 11:58:10 2012 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2012, 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
@@ -37,8 +37,14 @@
  * Bsd implementation of HotSpotVirtualMachine
  */
 public class BsdVirtualMachine extends HotSpotVirtualMachine {
-    // temp directory for socket file
-    private static final String tmpdir = System.getProperty("java.io.tmpdir");
+    // "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;
 
     // The patch to the socket file created by the target VM
     String path;
@@ -243,11 +249,8 @@
     }
 
     // Return the socket file for the given process.
-    // Checks working directory of process for .java_pid<pid>. If not
-    // found it looks in temp directory.
+    // Checks temp directory for .java_pid<pid>.
     private String findSocketFile(int pid) {
-        // First check for a .java_pid<pid> file in the working directory
-        // of the target process
         String fn = ".java_pid" + pid;
         File f = new File(tmpdir, fn);
         return f.exists() ? f.getPath() : null;
@@ -291,7 +294,10 @@
 
     static native void createAttachFile(String path);
 
+    static native String getTempDir();
+
     static {
         System.loadLibrary("attach");
+        tmpdir = getTempDir();
     }
 }
--- a/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c	Thu Feb 02 11:38:26 2012 -0800
+++ b/src/solaris/native/sun/tools/attach/BsdVirtualMachine.c	Fri Feb 03 11:58:10 2012 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2005, 2012, 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
@@ -37,6 +37,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/syslimits.h>
 #include <sys/un.h>
 #include <fcntl.h>
 
@@ -277,3 +278,30 @@
         JNU_ReleaseStringPlatformChars(env, path, _path);
     }
 }
+
+/*
+ * Class:     sun_tools_attach_BSDVirtualMachine
+ * Method:    getTempDir
+ * Signature: (V)Ljava.lang.String;
+ */
+JNIEXPORT jstring JNICALL Java_sun_tools_attach_BsdVirtualMachine_getTempDir(JNIEnv *env, jclass cls)
+{
+    // This must be hard coded because it's the system's temporary
+    // directory not the java application's temp directory, ala java.io.tmpdir.
+
+#ifdef __APPLE__
+    // macosx has a secure per-user temporary directory
+    static char *temp_path = NULL;
+    char temp_path_storage[PATH_MAX];
+    if (temp_path == NULL) {
+        int pathSize = confstr(_CS_DARWIN_USER_TEMP_DIR, temp_path_storage, PATH_MAX);
+        if (pathSize == 0 || pathSize > PATH_MAX) {
+            strlcpy(temp_path_storage, "/tmp", sizeof(temp_path_storage));
+        }
+        temp_path = temp_path_storage;
+    }
+    return JNU_NewStringPlatform(env, temp_path);
+#else /* __APPLE__ */
+    return (*env)->NewStringUTF(env, "/tmp");
+#endif /* __APPLE__ */
+}