Skip to content
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

Make accessFilter file registration thread safe #635

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@
*/
package org.graalvm.buildtools.agent;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

public class AgentConfiguration implements Serializable {

private static final String DEFAULT_ACCESS_FILTER_FILE = "/access-filter.json";
private static final String ACCESS_FILTER_PREFIX = "access-filter";
private static final String ACCESS_FILTER_SUFFIX = ".json";
private static final String DEFAULT_ACCESS_FILTER_FILE_LOCATION = "/" + ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX;

private static final Logger logger = Logger.getGlobal();

private final Collection<String> callerFilterFiles;
private final Collection<String> accessFilterFiles;
private final Boolean builtinCallerFilter;
Expand Down Expand Up @@ -144,23 +146,36 @@ private void addDefaultAccessFilter() {

String tempDir = System.getProperty("java.io.tmpdir");
Path agentDir = Path.of(tempDir).resolve("agent-config");
Path accessFilterFile = agentDir.resolve("access-filter.json");
Path accessFilterFile = agentDir.resolve(ACCESS_FILTER_PREFIX + ACCESS_FILTER_SUFFIX);
if (Files.exists(accessFilterFile)) {
accessFilterFiles.add(accessFilterFile.toString());
return;
}

try(InputStream accessFilter = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE)) {
if (accessFilter != null) {
if (!Files.exists(agentDir)) {
Files.createDirectory(agentDir);
}
try(InputStream accessFilterData = AgentConfiguration.class.getResourceAsStream(DEFAULT_ACCESS_FILTER_FILE_LOCATION)) {
if (accessFilterData == null) {
throw new IOException("Cannot access data from: " + DEFAULT_ACCESS_FILTER_FILE_LOCATION);
}

try {
Files.createDirectory(agentDir);
} catch (FileAlreadyExistsException e) {
logger.info("Skip creation of directory " + agentDir + " (already created).");
}

long pid = ProcessHandle.current().pid();
long time = System.currentTimeMillis();
Path tmpAccessFilter = agentDir.resolve(ACCESS_FILTER_PREFIX + '_' + pid + '_' + time + '_' + ACCESS_FILTER_SUFFIX);
Files.copy(accessFilterData, tmpAccessFilter);

Files.copy(accessFilter, accessFilterFile, StandardCopyOption.REPLACE_EXISTING);
accessFilterFiles.add(accessFilterFile.toString());
} else {
throw new IOException("Cannot find access-filter.json on default location: " + DEFAULT_ACCESS_FILTER_FILE);
try {
Files.move(tmpAccessFilter, accessFilterFile, StandardCopyOption.ATOMIC_MOVE);
} catch (FileAlreadyExistsException e) {
Files.delete(tmpAccessFilter);
logger.info(accessFilterFile + " already exists. Delete " + tmpAccessFilter);
}

accessFilterFiles.add(accessFilterFile.toString());
} catch (IOException e) {
throw new RuntimeException("Cannot add default access-filter.json" ,e);
}
Expand Down
Loading