1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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 sun.tools.common;
27 
28 import java.io.IOException;
29 import java.io.UncheckedIOException;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.util.stream.Stream;
33 
34 /**
35  * A helper class that retrieves the main class name for
36  * a running Java process using the proc filesystem (procfs)
37  */
38 final class ProcessHelper {
39 
40     private static final String CMD_PREFIX = "cmd:";
41 
42     /**
43      * Gets the main class name for the given Java process by parsing the
44      * process command line. If the application was started with the <em>-jar</em>
45      * option this method returns the name of the jar file. If the application
46      * was started with <em>-m</em> or <em>--module</em> option, the method returns
47      * the module name and the main class name.
48      * @param pid - process ID (pid)
49      * @return the main class name or null if the process no longer exists or
50      * was started with a native launcher (e.g. jcmd etc)
51      */
getMainClass(String pid)52     static String getMainClass(String pid) {
53         String cmdLine = getCommandLine(pid);
54         if (cmdLine == null) {
55             return null;
56         }
57         if (cmdLine.startsWith(CMD_PREFIX)) {
58             cmdLine = cmdLine.substring(CMD_PREFIX.length());
59         }
60         String[] parts = cmdLine.split("\0");
61         String mainClass = null;
62 
63         if (parts.length == 0) {
64             return null;
65         }
66 
67         // Check the executable
68         String[] executablePath = parts[0].split("/");
69         if (executablePath.length > 0) {
70             String binaryName = executablePath[executablePath.length - 1];
71             if (!"java".equals(binaryName)) {
72                 // Skip the process if it is not started with java launcher
73                 return null;
74             }
75         }
76 
77         // To be consistent with the behavior on other platforms, if -jar, -m, or --module
78         // options are used then just return the value (the path to the jar file or module
79         // name with a main class). Otherwise, the main class name is the first part that
80         // is not a Java option (doesn't start with '-' and is not a classpath or a module
81         // whitespace option).
82 
83         for (int i = 1; i < parts.length && mainClass == null; i++) {
84             if (i < parts.length - 1) {
85                 if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) {
86                     return parts[i + 1];
87                 }
88             }
89 
90             if (parts[i].startsWith("--module=")) {
91                 return parts[i].substring("--module=".length());
92             }
93 
94             // If this is a classpath or a module whitespace option then skip the next part
95             // (the classpath or the option value itself)
96             if (parts[i].equals("-cp") || parts[i].equals("-classpath") ||  parts[i].equals("--class-path") ||
97                     isModuleWhiteSpaceOption(parts[i])) {
98                 i++;
99                 continue;
100             }
101             // Skip all other Java options
102             if (parts[i].startsWith("-")) {
103                 continue;
104             }
105 
106             // If it is a source-file mode then return null
107             if (parts[i].endsWith(".java")) {
108                 return null;
109             }
110 
111             mainClass = parts[i];
112         }
113         return mainClass;
114     }
115 
getCommandLine(String pid)116     private static String getCommandLine(String pid) {
117         try (Stream<String> lines =
118                      Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
119             return lines.findFirst().orElse(null);
120         } catch (IOException | UncheckedIOException e) {
121             return null;
122         }
123     }
124 
isModuleWhiteSpaceOption(String option)125     private static boolean isModuleWhiteSpaceOption(String option) {
126         return option.equals("-p") ||
127                option.equals("--module-path") ||
128                option.equals("--upgrade-module-path") ||
129                option.equals("--add-modules") ||
130                option.equals("--limit-modules") ||
131                option.equals("--add-exports") ||
132                option.equals("--add-opens") ||
133                option.equals("--add-reads") ||
134                option.equals("--patch-module");
135     }
136 }
137