Skip to content

Commit

Permalink
Attempt to resolve user temporary folder on Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
raphw committed Apr 28, 2022
1 parent 4c470e2 commit edf5482
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ public static VirtualMachine attach(String processId) throws IOException {
* @throws IOException If an IO exception occurs during establishing the connection.
*/
public static VirtualMachine attach(String processId, int timeout, Dispatcher dispatcher) throws IOException {
File directory = new File(System.getProperty(IBM_TEMPORARY_FOLDER, dispatcher.getTemporaryFolder()), ".com_ibm_tools_attach");
File directory = new File(System.getProperty(IBM_TEMPORARY_FOLDER, dispatcher.getTemporaryFolder(processId)), ".com_ibm_tools_attach");
RandomAccessFile attachLock = new RandomAccessFile(new File(directory, "_attachlock"), "rw");
try {
FileLock attachLockLock = attachLock.getChannel().lock();
Expand Down Expand Up @@ -1919,9 +1919,10 @@ public interface Dispatcher {
/**
* Returns this machine's temporary folder.
*
* @param processId The target process's id.
* @return The temporary folder.
*/
String getTemporaryFolder();
String getTemporaryFolder(String processId);

/**
* Returns the process id of this process.
Expand Down Expand Up @@ -2024,7 +2025,13 @@ public ForJnaPosixEnvironment(int attempts, long pause, TimeUnit timeUnit) {
/**
* {@inheritDoc}
*/
public String getTemporaryFolder() {
public String getTemporaryFolder(String processId) {
if (Platform.isLinux()) {
File file = new File("/proc/" + processId + "/root/tmp");
if (file.isDirectory() && file.canRead()) {
return file.getAbsolutePath();
}
}
String temporaryFolder = System.getenv("TMPDIR");
return temporaryFolder == null ? "/tmp" : temporaryFolder;
}
Expand Down Expand Up @@ -2249,6 +2256,8 @@ protected interface PosixLibrary extends Library {
*/
int semop(int id, SemaphoreOperation operation, int flags) throws LastErrorException;

String gettmpDir();

/**
* A structure to represent a semaphore operation for {@code semop}.
*/
Expand Down Expand Up @@ -2309,7 +2318,7 @@ public ForJnaWindowsEnvironment() {
/**
* {@inheritDoc}
*/
public String getTemporaryFolder() {
public String getTemporaryFolder(String processId) {
WinDef.DWORD length = new WinDef.DWORD(WinDef.MAX_PATH);
char[] path = new char[length.intValue()];
if (Kernel32.INSTANCE.GetTempPath(length, path).intValue() == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void tearDown() throws Exception {
public void testAttachment() throws Throwable {
final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
VirtualMachine.ForOpenJ9.Dispatcher dispatcher = mock(VirtualMachine.ForOpenJ9.Dispatcher.class);
when(dispatcher.getTemporaryFolder()).thenReturn(temporaryFolder.getAbsolutePath());
when(dispatcher.getTemporaryFolder(Integer.toString(PROCESS_ID))).thenReturn(temporaryFolder.getAbsolutePath());
File targetFolder = new File(attachFolder, Integer.toString(PROCESS_ID));
assertThat(targetFolder.mkdir(), is(true));
try {
Expand Down

0 comments on commit edf5482

Please sign in to comment.