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