Skip to content

Commit 34782b6

Browse files
committed
Make TempLocationManager USER aware
1 parent efdcea8 commit 34782b6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

dd-java-agent/agent-profiling/profiling-controller/src/main/java/com/datadog/profiling/controller/TempLocationManager.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ private TempLocationManager() {
295295

296296
String pid = PidHelper.getPid();
297297

298-
baseTempDir = configuredTempDir.resolve("ddprof");
298+
baseTempDir = configuredTempDir.resolve(getBaseTempDirName());
299299
baseTempDir.toFile().deleteOnExit();
300300

301301
tempDir = baseTempDir.resolve(TEMPDIR_PREFIX + pid);
@@ -319,6 +319,17 @@ private TempLocationManager() {
319319
Runtime.getRuntime().addShutdownHook(selfCleanup);
320320
}
321321

322+
// @VisibleForTesting
323+
static String getBaseTempDirName() {
324+
String userName = System.getProperty("user.name");
325+
// unlikely, but fall-back to system env based user name
326+
userName = userName == null ? System.getenv("USER") : userName;
327+
// make sure we do not have any illegal characters in the user name
328+
userName =
329+
userName != null ? userName.replace('.', '_').replace('/', '_').replace(' ', '_') : null;
330+
return "ddprof" + (userName != null ? "_" + userName : "");
331+
}
332+
322333
/**
323334
* Get the temporary directory for the current process. The directory will be removed at JVM exit.
324335
*
@@ -396,7 +407,7 @@ boolean cleanup(boolean cleanSelf) {
396407
boolean cleanup(boolean cleanSelf, long timeout, TimeUnit unit) {
397408
try {
398409
if (!Files.exists(baseTempDir)) {
399-
// not event the main temp location exists; nothing to clean up
410+
// not even the main temp location exists; nothing to clean up
400411
return true;
401412
}
402413
try (Stream<Path> paths = Files.walk(baseTempDir)) {

dd-java-agent/agent-profiling/profiling-controller/src/test/java/com/datadog/profiling/controller/TempLocationManagerTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void testCleanup(String subPath) throws Exception {
104104

105105
// fake temp location
106106
Path fakeTempDir = tempDir.getParent();
107-
while (fakeTempDir != null && !fakeTempDir.endsWith("ddprof")) {
107+
while (fakeTempDir != null && !fakeTempDir.getFileName().toString().contains("ddprof")) {
108108
fakeTempDir = fakeTempDir.getParent();
109109
}
110110
fakeTempDir = fakeTempDir.resolve("pid_0000");
@@ -133,7 +133,8 @@ void testConcurrentCleanup(String section) throws Exception {
133133
"ddprof-test-",
134134
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
135135

136-
Path fakeTempDir = baseDir.resolve("ddprof/pid_1234/scratch");
136+
Path fakeTempDir =
137+
baseDir.resolve(TempLocationManager.getBaseTempDirName() + "/pid_1234/scratch");
137138
Files.createDirectories(fakeTempDir);
138139
Path fakeTempFile = fakeTempDir.resolve("libxxx.so");
139140
Files.createFile(fakeTempFile);

0 commit comments

Comments
 (0)