diff --git a/dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java b/dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java index e77cf9d9648..bfdd5c698ec 100644 --- a/dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java +++ b/dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java @@ -295,7 +295,7 @@ private TempLocationManager() { String pid = PidHelper.getPid(); - baseTempDir = configuredTempDir.resolve("ddprof"); + baseTempDir = configuredTempDir.resolve(getBaseTempDirName()); baseTempDir.toFile().deleteOnExit(); tempDir = baseTempDir.resolve(TEMPDIR_PREFIX + pid); @@ -319,6 +319,17 @@ private TempLocationManager() { Runtime.getRuntime().addShutdownHook(selfCleanup); } + // @VisibleForTesting + static String getBaseTempDirName() { + String userName = System.getProperty("user.name"); + // unlikely, but fall-back to system env based user name + userName = userName == null ? System.getenv("USER") : userName; + // make sure we do not have any illegal characters in the user name + userName = + userName != null ? userName.replace('.', '_').replace('/', '_').replace(' ', '_') : null; + return "ddprof" + (userName != null ? "_" + userName : ""); + } + /** * Get the temporary directory for the current process. The directory will be removed at JVM exit. * @@ -396,7 +407,7 @@ boolean cleanup(boolean cleanSelf) { boolean cleanup(boolean cleanSelf, long timeout, TimeUnit unit) { try { if (!Files.exists(baseTempDir)) { - // not event the main temp location exists; nothing to clean up + // not even the main temp location exists; nothing to clean up return true; } try (Stream paths = Files.walk(baseTempDir)) { diff --git a/dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java b/dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java index 6d7b32c9fa6..21043fb6d5d 100644 --- a/dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java +++ b/dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java @@ -104,7 +104,7 @@ void testCleanup(String subPath) throws Exception { // fake temp location Path fakeTempDir = tempDir.getParent(); - while (fakeTempDir != null && !fakeTempDir.endsWith("ddprof")) { + while (fakeTempDir != null && !fakeTempDir.getFileName().toString().contains("ddprof")) { fakeTempDir = fakeTempDir.getParent(); } fakeTempDir = fakeTempDir.resolve("pid_0000"); @@ -112,10 +112,10 @@ void testCleanup(String subPath) throws Exception { Path tmpFile = Files.createFile(fakeTempDir.resolve("test.txt")); tmpFile.toFile().deleteOnExit(); // make sure this is deleted at exit fakeTempDir.toFile().deleteOnExit(); // also this one - tempLocationManager.cleanup(false); + boolean rslt = tempLocationManager.cleanup(false); // fake temp location should be deleted // real temp location should be kept - assertFalse(Files.exists(fakeTempDir)); + assertFalse(rslt && Files.exists(fakeTempDir)); assertTrue(Files.exists(tempDir)); } @@ -133,7 +133,8 @@ void testConcurrentCleanup(String section) throws Exception { "ddprof-test-", PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"))); - Path fakeTempDir = baseDir.resolve("ddprof/pid_1234/scratch"); + Path fakeTempDir = + baseDir.resolve(TempLocationManager.getBaseTempDirName() + "/pid_1234/scratch"); Files.createDirectories(fakeTempDir); Path fakeTempFile = fakeTempDir.resolve("libxxx.so"); Files.createFile(fakeTempFile);