Path: blob/master/src/java.base/linux/classes/jdk/internal/platform/CgroupUtil.java
40948 views
/*1* Copyright (c) 2020, Red Hat Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.platform;2627import java.io.BufferedReader;28import java.io.IOException;29import java.io.UncheckedIOException;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.security.AccessController;34import java.security.PrivilegedActionException;35import java.security.PrivilegedExceptionAction;36import java.util.List;37import java.util.stream.Stream;3839public final class CgroupUtil {4041@SuppressWarnings("removal")42public static Stream<String> readFilePrivileged(Path path) throws IOException {43try {44PrivilegedExceptionAction<Stream<String>> pea = () -> Files.lines(path);45return AccessController.doPrivileged(pea);46} catch (PrivilegedActionException e) {47unwrapIOExceptionAndRethrow(e);48throw new InternalError(e.getCause());49} catch (UncheckedIOException e) {50throw e.getCause();51}52}5354static void unwrapIOExceptionAndRethrow(PrivilegedActionException pae) throws IOException {55Throwable x = pae.getCause();56if (x instanceof IOException)57throw (IOException) x;58if (x instanceof RuntimeException)59throw (RuntimeException) x;60if (x instanceof Error)61throw (Error) x;62}6364static String readStringValue(CgroupSubsystemController controller, String param) throws IOException {65PrivilegedExceptionAction<BufferedReader> pea = () ->66Files.newBufferedReader(Paths.get(controller.path(), param));67try (@SuppressWarnings("removal") BufferedReader bufferedReader =68AccessController.doPrivileged(pea)) {69String line = bufferedReader.readLine();70return line;71} catch (PrivilegedActionException e) {72unwrapIOExceptionAndRethrow(e);73throw new InternalError(e.getCause());74} catch (UncheckedIOException e) {75throw e.getCause();76}77}7879@SuppressWarnings("removal")80public static List<String> readAllLinesPrivileged(Path path) throws IOException {81try {82PrivilegedExceptionAction<List<String>> pea = () -> Files.readAllLines(path);83return AccessController.doPrivileged(pea);84} catch (PrivilegedActionException e) {85unwrapIOExceptionAndRethrow(e);86throw new InternalError(e.getCause());87} catch (UncheckedIOException e) {88throw e.getCause();89}90}91}929394