jdk.jfr.internal.SecuritySupport.SafePath Java Examples

The following examples show how to use jdk.jfr.internal.SecuritySupport.SafePath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractDCmd.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected final void reportOperationComplete(String actionPrefix, String name, SafePath file) {
    print(actionPrefix);
    print(" recording");
    if (name != null) {
        print(" \"" + name + "\"");
    }
    if (file != null) {
        print(",");
        try {
            print(" ");
            long bytes = SecuritySupport.getFileSize(file);
            printBytes(bytes);
        } catch (IOException e) {
            // Ignore, not essential
        }
        println(" written to:");
        println();
        printPath(file);
    } else {
        println(".");
    }
}
 
Example #2
Source File: AbstractDCmd.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
protected final void reportOperationComplete(String actionPrefix, Recording r, SafePath file) {
    print(actionPrefix);
    print(" recording ");
    print("\"" + r.getName() + "\"");
    if (file != null) {
        print(",");
        try {
            print(" ");
            long bytes = SecuritySupport.getFileSize(file);
            printBytes(bytes, " ");
        } catch (IOException e) {
            // Ignore, not essential
        }
        println(" written to:");
        println();
        printPath(file);
    } else {
        println(".");
    }
}
 
Example #3
Source File: DCmdDump.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Execute JFR.dump.
 *
 * @param recordingText name or id of the recording to dump, or
 *        <code>null</code>
 *
 * @param textPath file path where recording should be written.
 *
 * @return result output
 *
 * @throws DCmdException if the dump could not be completed
 */
public String execute(String recordingText, String textPath,  Boolean pathToGcRoots) throws DCmdException {
    if (textPath == null) {
        throw new DCmdException("Failed to dump %s, missing filename.", recordingText);
    }
    Recording recording = findRecording(recordingText);
    try {
        SafePath dumpFile = resolvePath(textPath, "Failed to dump %s");
        // create file for JVM
        Utils.touch(dumpFile.toPath());
        PlatformRecording r = PrivateAccess.getInstance().getPlatformRecording(recording);
        WriteableUserPath wup = new WriteableUserPath(dumpFile.toPath());

        Map<String, String> overlay = new HashMap<>();
        Utils.updateSettingPathToGcRoots(overlay, pathToGcRoots);

        r.copyTo(wup, "Dumped by user", overlay);
        reportOperationComplete("Dumped", recording, dumpFile);
    } catch (IOException | InvalidPathException e) {
        throw new DCmdException("Failed to dump %s. Could not copy recording for dump. %s", recordingText, e.getMessage());
    }
    return getResult();
}
 
Example #4
Source File: DCmdDump.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException {
    try (PlatformRecording r = newSnapShot(recorder, recording, pathToGcRoots)) {
        r.filter(beginTime, endTime, maxSize);
        if (r.getChunks().isEmpty()) {
            throw new DCmdException("Dump failed. No data found in the specified interval.");
        }
        SafePath dumpFile = resolvePath(recording, filename);

        // Needed for JVM
        Utils.touch(dumpFile.toPath());
        r.dumpStopped(new WriteableUserPath(dumpFile.toPath()));
        reportOperationComplete("Dumped", name, dumpFile);
    } catch (IOException | InvalidPathException e) {
        throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
    }
}
 
Example #5
Source File: DCmdDump.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void dump(PlatformRecorder recorder, Recording recording, String name, String filename, Long maxSize, Boolean pathToGcRoots, Instant beginTime, Instant endTime) throws DCmdException {
    try (PlatformRecording r = newSnapShot(recorder, recording, pathToGcRoots)) {
        r.filter(beginTime, endTime, maxSize);
        if (r.getChunks().isEmpty()) {
            throw new DCmdException("Dump failed. No data found in the specified interval.");
        }
        SafePath dumpFile = resolvePath(recording, filename);

        // Needed for JVM
        Utils.touch(dumpFile.toPath());
        r.dumpStopped(new WriteableUserPath(dumpFile.toPath()));
        reportOperationComplete("Dumped", name, dumpFile);
    } catch (IOException | InvalidPathException e) {
        throw new DCmdException("Dump failed. Could not copy recording data. %s", e.getMessage());
    }
}
 
Example #6
Source File: Repository.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean tryToUseAsRepository(final SafePath path) {
    Path parent = path.toPath().getParent();
    if (parent == null) {
        return false;
    }
    try {
        try {
            SecuritySupport.createDirectories(path);
        } catch (Exception e) {
            // file already existed or some other problem occurred
        }
        if (!SecuritySupport.exists(path)) {
            return false;
        }
        if (!SecuritySupport.isDirectory(path)) {
            return false;
        }
        return true;
    } catch (IOException io) {
        return false;
    }
}
 
Example #7
Source File: AbstractDCmd.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
protected final void reportOperationComplete(String actionPrefix, String name, SafePath file) {
    print(actionPrefix);
    print(" recording");
    if (name != null) {
        print(" \"" + name + "\"");
    }
    if (file != null) {
        print(",");
        try {
            print(" ");
            long bytes = SecuritySupport.getFileSize(file);
            printBytes(bytes);
        } catch (IOException e) {
            // Ignore, not essential
        }
        println(" written to:");
        println();
        printPath(file);
    } else {
        println(".");
    }
}
 
Example #8
Source File: Repository.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static boolean tryToUseAsRepository(final SafePath path) {
    Path parent = path.toPath().getParent();
    if (parent == null) {
        return false;
    }
    try {
        try {
            SecuritySupport.createDirectories(path);
        } catch (Exception e) {
            // file already existed or some other problem occurred
        }
        if (!SecuritySupport.exists(path)) {
            return false;
        }
        if (!SecuritySupport.isDirectory(path)) {
            return false;
        }
        return true;
    } catch (IOException io) {
        return false;
    }
}
 
Example #9
Source File: Repository.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean tryToUseAsRepository(final SafePath path) {
    Path parent = path.toPath().getParent();
    if (parent == null) {
        return false;
    }
    try {
        try {
            SecuritySupport.createDirectories(path);
        } catch (Exception e) {
            // file already existed or some other problem occurred
        }
        if (!SecuritySupport.exists(path)) {
            return false;
        }
        if (!SecuritySupport.isDirectory(path)) {
            return false;
        }
        return true;
    } catch (IOException io) {
        return false;
    }
}
 
Example #10
Source File: JFC.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static List<KnownConfiguration> getKnownConfigurations() {
    if (knownConfigurations == null) {
        List<KnownConfiguration> configProxies = new ArrayList<>();
        for (SafePath p : SecuritySupport.getPredefinedJFCFiles()) {
            try {
                configProxies.add(new KnownConfiguration(p));
            } catch (IOException ioe) {
                // ignore
            }
        }
        knownConfigurations = configProxies;
    }
    return knownConfigurations;
}
 
Example #11
Source File: RepositoryChunk.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
RepositoryChunk(SafePath path, Instant startTime) throws Exception {
    ZonedDateTime z = ZonedDateTime.now();
    String fileName = Repository.REPO_DATE_FORMAT.format(
            LocalDateTime.ofInstant(startTime, z.getZone()));
    this.startTime = startTime;
    this.repositoryPath = path;
    this.unFinishedFile = findFileName(repositoryPath, fileName, ".part");
    this.file = findFileName(repositoryPath, fileName, ".jfr");
    this.unFinishedRAF = SecuritySupport.createRandomAccessFile(unFinishedFile);
    SecuritySupport.touch(file);
}
 
Example #12
Source File: AbstractDCmd.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected final SafePath resolvePath(Recording recording, String filename) throws InvalidPathException {
    if (filename == null) {
        return makeGenerated(recording, Paths.get("."));
    }
    Path path = Paths.get(filename);
    if (Files.isDirectory(path)) {
        return makeGenerated(recording, path);
    }
    return new SafePath(path.toAbsolutePath().normalize());
}
 
Example #13
Source File: JFC.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static String readContent(SafePath knownPath) throws IOException {
    if (SecuritySupport.getFileSize(knownPath) > MAXIMUM_FILE_SIZE) {
        throw new IOException("Configuration with more than "
                + MAXIMUM_FILE_SIZE + " characters can't be read.");
    }
    try (InputStream r = SecuritySupport.newFileInputStream(knownPath)) {
        return JFC.readContent(r);
    }
}
 
Example #14
Source File: JFC.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String readContent(SafePath knownPath) throws IOException {
    if (SecuritySupport.getFileSize(knownPath) > MAXIMUM_FILE_SIZE) {
        throw new IOException("Configuration with more than "
                + MAXIMUM_FILE_SIZE + " characters can't be read.");
    }
    try (InputStream r = SecuritySupport.newFileInputStream(knownPath)) {
        return JFC.readContent(r);
    }
}
 
Example #15
Source File: RepositoryChunk.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static SafePath findFileName(SafePath directory, String name, String extension) throws Exception {
    Path p = directory.toPath().resolve(name + extension);
    for (int i = 1; i < MAX_CHUNK_NAMES; i++) {
        SafePath s = new SafePath(p);
        if (!SecuritySupport.exists(s)) {
            return s;
        }
        String extendedName = String.format("%s_%02d%s", name, i, extension);
        p = directory.toPath().resolve(extendedName);
    }
    p = directory.toPath().resolve(name + "_" + System.currentTimeMillis() + extension);
    return SecuritySupport.toRealPath(new SafePath(p));
}
 
Example #16
Source File: RepositoryChunk.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static long finish(SafePath unFinishedFile, SafePath file) throws IOException {
    Objects.requireNonNull(unFinishedFile);
    Objects.requireNonNull(file);
    SecuritySupport.delete(file);
    SecuritySupport.moveReplace(unFinishedFile, file);
    return SecuritySupport.getFileSize(file);
}
 
Example #17
Source File: JFC.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static List<KnownConfiguration> getKnownConfigurations() {
    if (knownConfigurations == null) {
        List<KnownConfiguration> configProxies = new ArrayList<>();
        for (SafePath p : SecuritySupport.getPredefinedJFCFiles()) {
            try {
                configProxies.add(new KnownConfiguration(p));
            } catch (IOException ioe) {
                // ignore
            }
        }
        knownConfigurations = configProxies;
    }
    return knownConfigurations;
}
 
Example #18
Source File: RepositoryChunk.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private void delete(SafePath f) {
    try {
        SecuritySupport.delete(f);
        Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Repository chunk " + f + " deleted");
    } catch (IOException e) {
        Logger.log(LogTag.JFR, LogLevel.ERROR, ()  -> "Repository chunk " + f + " could not be deleted: " + e.getMessage());
        if (f != null) {
            SecuritySupport.deleteOnExit(f);
        }
    }
}
 
Example #19
Source File: Repository.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static SafePath createRealBasePath(SafePath safePath) throws Exception {
    if (SecuritySupport.exists(safePath)) {
        if (!SecuritySupport.isWritable(safePath)) {
            throw new IOException("JFR repository directory (" + safePath.toString() + ") exists, but isn't writable");
        }
        return SecuritySupport.toRealPath(safePath);
    }
    SafePath p = SecuritySupport.createDirectories(safePath);
    return SecuritySupport.toRealPath(p);
}
 
Example #20
Source File: RepositoryChunk.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void delete(SafePath f) {
    try {
        SecuritySupport.delete(f);
        Logger.log(LogTag.JFR, LogLevel.DEBUG, () -> "Repository chunk " + f + " deleted");
    } catch (IOException e) {
        Logger.log(LogTag.JFR, LogLevel.ERROR, ()  -> "Repository chunk " + f + " could not be deleted: " + e.getMessage());
        if (f != null) {
            SecuritySupport.deleteOnExit(f);
        }
    }
}
 
Example #21
Source File: RepositoryChunk.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static long finish(SafePath unFinishedFile, SafePath file) throws IOException {
    Objects.requireNonNull(unFinishedFile);
    Objects.requireNonNull(file);
    SecuritySupport.delete(file);
    SecuritySupport.moveReplace(unFinishedFile, file);
    return SecuritySupport.getFileSize(file);
}
 
Example #22
Source File: RepositoryChunk.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static SafePath findFileName(SafePath directory, String name, String extension) throws Exception {
    Path p = directory.toPath().resolve(name + extension);
    for (int i = 1; i < MAX_CHUNK_NAMES; i++) {
        SafePath s = new SafePath(p);
        if (!SecuritySupport.exists(s)) {
            return s;
        }
        String extendedName = String.format("%s_%02d%s", name, i, extension);
        p = directory.toPath().resolve(extendedName);
    }
    p = directory.toPath().resolve(name + "_" + System.currentTimeMillis() + extension);
    return SecuritySupport.toRealPath(new SafePath(p));
}
 
Example #23
Source File: AbstractDCmd.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected final SafePath resolvePath(Recording recording, String filename) throws InvalidPathException {
    if (filename == null) {
        return makeGenerated(recording, Paths.get("."));
    }
    Path path = Paths.get(filename);
    if (Files.isDirectory(path)) {
        return makeGenerated(recording, path);
    }
    return new SafePath(path.toAbsolutePath().normalize());
}
 
Example #24
Source File: RepositoryChunk.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
RepositoryChunk(SafePath path, Instant startTime) throws Exception {
    ZonedDateTime z = ZonedDateTime.now();
    String fileName = Repository.REPO_DATE_FORMAT.format(
            LocalDateTime.ofInstant(startTime, z.getZone()));
    this.startTime = startTime;
    this.repositoryPath = path;
    this.unFinishedFile = findFileName(repositoryPath, fileName, ".part");
    this.file = findFileName(repositoryPath, fileName, ".jfr");
    this.unFinishedRAF = SecuritySupport.createRandomAccessFile(unFinishedFile);
    SecuritySupport.touch(file);
}
 
Example #25
Source File: AbstractDCmd.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected final void printPath(SafePath path) {
    if (path == null) {
        print("N/A");
        return;
    }
    try {
        printPath(SecuritySupport.getAbsolutePath(path).toPath());
    } catch (IOException ioe) {
        printPath(path.toPath());
    }
}
 
Example #26
Source File: JFC.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static List<KnownConfiguration> getKnownConfigurations() {
    if (knownConfigurations == null) {
        List<KnownConfiguration> configProxies = new ArrayList<>();
        for (SafePath p : SecuritySupport.getPredefinedJFCFiles()) {
            try {
                configProxies.add(new KnownConfiguration(p));
            } catch (IOException ioe) {
                // ignore
            }
        }
        knownConfigurations = configProxies;
    }
    return knownConfigurations;
}
 
Example #27
Source File: Repository.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void setBasePath(SafePath baseLocation) throws Exception {
    // Probe to see if repository can be created, needed for fail fast
    // during JVM startup or JFR.configure
    this.repository = createRepository(baseLocation);
    try {
        // Remove so we don't "leak" repositories, if JFR is never started
        // and shutdown hook not added.
        SecuritySupport.delete(repository);
    } catch (IOException ioe) {
        Logger.log(LogTag.JFR, LogLevel.INFO, "Could not delete disk repository " + repository);
    }
    this.baseLocation = baseLocation;
}
 
Example #28
Source File: Repository.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static SafePath createRealBasePath(SafePath safePath) throws Exception {
    if (SecuritySupport.exists(safePath)) {
        if (!SecuritySupport.isWritable(safePath)) {
            throw new IOException("JFR repository directory (" + safePath.toString() + ") exists, but isn't writable");
        }
        return SecuritySupport.toRealPath(safePath);
    }
    SafePath p = SecuritySupport.createDirectories(safePath);
    return SecuritySupport.toRealPath(p);
}
 
Example #29
Source File: Repository.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
synchronized void clear() {
    for (SafePath p : cleanupDirectories) {
        try {
            SecuritySupport.clearDirectory(p);
            Logger.log(LogTag.JFR, LogLevel.INFO, "Removed repository " + p);
        } catch (IOException e) {
            Logger.log(LogTag.JFR, LogLevel.ERROR, "Repository " + p + " could not be removed at shutdown: " + e.getMessage());
        }
    }
}
 
Example #30
Source File: JFC.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static String readContent(SafePath knownPath) throws IOException {
    if (SecuritySupport.getFileSize(knownPath) > MAXIMUM_FILE_SIZE) {
        throw new IOException("Configuration with more than "
                + MAXIMUM_FILE_SIZE + " characters can't be read.");
    }
    try (InputStream r = SecuritySupport.newFileInputStream(knownPath)) {
        return JFC.readContent(r);
    }
}