src/jdk.jcmd/linux/classes/sun/tools/common/ProcessHelper.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58265 577e17cab93f
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     1
/*
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     4
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    10
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    15
 * accompanied this code).
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    16
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    20
 *
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    23
 * questions.
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    24
 */
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    25
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
    26
package sun.tools.common;
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    27
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    28
import java.io.IOException;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    29
import java.io.UncheckedIOException;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    30
import java.nio.file.Files;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    31
import java.nio.file.Paths;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    32
import java.util.stream.Stream;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    33
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    34
/**
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    35
 * A helper class that retrieves the main class name for
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    36
 * a running Java process using the proc filesystem (procfs)
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    37
 */
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
    38
final class ProcessHelper {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    39
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    40
    private static final String CMD_PREFIX = "cmd:";
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    41
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    42
    /**
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    43
     * Gets the main class name for the given Java process by parsing the
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    44
     * process command line. If the application was started with the <em>-jar</em>
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    45
     * option this method returns the name of the jar file. If the application
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    46
     * was started with <em>-m</em> or <em>--module</em> option, the method returns
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    47
     * the module name and the main class name.
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    48
     * @param pid - process ID (pid)
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    49
     * @return the main class name or null if the process no longer exists or
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    50
     * was started with a native launcher (e.g. jcmd etc)
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    51
     */
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
    52
    static String getMainClass(String pid) {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    53
        String cmdLine = getCommandLine(pid);
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    54
        if (cmdLine == null) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    55
            return null;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    56
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    57
        if (cmdLine.startsWith(CMD_PREFIX)) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    58
            cmdLine = cmdLine.substring(CMD_PREFIX.length());
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    59
        }
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    60
        String[] parts = cmdLine.split("\0");
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    61
        String mainClass = null;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    62
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
    63
        if (parts.length == 0) {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    64
            return null;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    65
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    66
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    67
        // Check the executable
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    68
        String[] executablePath = parts[0].split("/");
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    69
        if (executablePath.length > 0) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    70
            String binaryName = executablePath[executablePath.length - 1];
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    71
            if (!"java".equals(binaryName)) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    72
                // Skip the process if it is not started with java launcher
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    73
                return null;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    74
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    75
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    76
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    77
        // To be consistent with the behavior on other platforms, if -jar, -m, or --module
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    78
        // options are used then just return the value (the path to the jar file or module
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    79
        // name with a main class). Otherwise, the main class name is the first part that
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    80
        // is not a Java option (doesn't start with '-' and is not a classpath or a module
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    81
        // whitespace option).
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    82
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    83
        for (int i = 1; i < parts.length && mainClass == null; i++) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    84
            if (i < parts.length - 1) {
54460
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    85
                if (parts[i].equals("-m") || parts[i].equals("--module") || parts[i].equals("-jar")) {
6733a9176cce 8221730: jcmd process name matching broken
dtitov
parents: 53707
diff changeset
    86
                    return parts[i + 1];
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    87
                }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    88
            }
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    89
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    90
            if (parts[i].startsWith("--module=")) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    91
                return parts[i].substring("--module=".length());
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    92
            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    93
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    94
            // If this is a classpath or a module whitespace option then skip the next part
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    95
            // (the classpath or the option value itself)
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    96
            if (parts[i].equals("-cp") || parts[i].equals("-classpath") ||  parts[i].equals("--class-path") ||
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
    97
                    isModuleWhiteSpaceOption(parts[i])) {
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    98
                i++;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
    99
                continue;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   100
            }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   101
            // Skip all other Java options
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   102
            if (parts[i].startsWith("-")) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   103
                continue;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   104
            }
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   105
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   106
            // If it is a source-file mode then return null
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   107
            if (parts[i].endsWith(".java")) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   108
                return null;
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   109
            }
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   110
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   111
            mainClass = parts[i];
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   112
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   113
        return mainClass;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   114
    }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   115
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   116
    private static String getCommandLine(String pid) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   117
        try (Stream<String> lines =
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   118
                     Files.lines(Paths.get("/proc/" + pid + "/cmdline"))) {
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   119
            return lines.findFirst().orElse(null);
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   120
        } catch (IOException | UncheckedIOException e) {
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   121
            return null;
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   122
        }
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   123
    }
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   124
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   125
    private static boolean isModuleWhiteSpaceOption(String option) {
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   126
        return option.equals("-p") ||
58265
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   127
               option.equals("--module-path") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   128
               option.equals("--upgrade-module-path") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   129
               option.equals("--add-modules") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   130
               option.equals("--limit-modules") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   131
               option.equals("--add-exports") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   132
               option.equals("--add-opens") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   133
               option.equals("--add-reads") ||
577e17cab93f 8230857: Avoid reflection in sun.tools.common.ProcessHelper
clanger
parents: 55386
diff changeset
   134
               option.equals("--patch-module");
55386
2f4e214781a1 8225543: Jcmd fails to attach to the Java process on Linux using the main class name if whitespace options were used to launch the process
dtitov
parents: 54460
diff changeset
   135
    }
53707
67537bbafd7f 8205654: serviceability/dcmd/framework/HelpTest.java timed out
dtitov
parents:
diff changeset
   136
}