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 
  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 
  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(" ");
  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         // path).
  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             // If this is a classpath or a module path option then skip the next part
  99             // (the classpath or the module path itself)
 100             if (parts[i].equals("-cp") || parts[i].equals("-classpath") ||  parts[i].equals("--class-path") ||
 101                     parts[i].equals("-p") || parts[i].equals("--module-path")) {
 102                 i++;
 103                 continue;
 104             }
 105             // Skip all other Java options
 106             if (parts[i].startsWith("-")) {
 107                 continue;
 108             }
 109             mainClass = parts[i];
 110         }
 111         return mainClass;
 112 
 113     }
 114 
 115     private static String getCommandLine(String pid) {
 116         try (Stream<String> lines =
 117                      Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
 118             return lines.map(x -> x.replaceAll("\0", " ")).findFirst().orElse(null);
 119         } catch (IOException | UncheckedIOException e) {
 120             return null;
 121         }
 122     }
 123 }
 124 
 125