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