src/jdk.jpackage/windows/native/msiwrapper/Executor.cpp
author herrick
Mon, 17 Jun 2019 15:38:04 -0400
branchJDK-8200758-branch
changeset 57413 45c74e654794
child 57909 c7de06ed4b54
permissions -rw-r--r--
8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files) Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57413
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     1
/*
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     4
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    10
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    15
 * accompanied this code).
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    16
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    20
 *
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    23
 * questions.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    24
 */
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    25
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    26
#include <algorithm>
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    27
#include "Executor.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    28
#include "Log.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    29
#include "WinErrorHandling.h"
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    30
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    31
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    32
namespace {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    33
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    34
void escapeArg(std::wstring& str) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    35
    if (str.empty()) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    36
        return;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    37
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    38
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    39
    if (str.front() == L'\"' && str.back() == L'\"' && str.size() > 1) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    40
        return;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    41
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    42
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    43
    if (str.find_first_of(L" \t") != std::wstring::npos) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    44
        str = L'"' + str + L'"';
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    45
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    46
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    47
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    48
} // namespace
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    49
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    50
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    51
std::wstring Executor::args() const {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    52
    tstring_array tmpArgs;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    53
    // argv[0] is the module name.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    54
    tmpArgs.push_back(appPath);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    55
    tmpArgs.insert(tmpArgs.end(), argsArray.begin(), argsArray.end());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    56
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    57
    std::for_each(tmpArgs.begin(), tmpArgs.end(), escapeArg);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    58
    return tstrings::join(tmpArgs.begin(), tmpArgs.end(), _T(" "));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    59
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    60
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    61
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    62
int Executor::execAndWaitForExit() const {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    63
    UniqueHandle h = startProcess();
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    64
    
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    65
    const DWORD res = ::WaitForSingleObject(h.get(), INFINITE);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    66
    if (WAIT_FAILED ==  res) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    67
        JP_THROW(SysError("WaitForSingleObject() failed", WaitForSingleObject));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    68
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    69
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    70
    DWORD exitCode = 0;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    71
    if (!GetExitCodeProcess(h.get(), &exitCode)) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    72
        // Error reading process's exit code.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    73
        JP_THROW(SysError("GetExitCodeProcess() failed", GetExitCodeProcess));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    74
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    75
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    76
    const DWORD processId = GetProcessId(h.get());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    77
    if (!processId) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    78
        JP_THROW(SysError("GetProcessId() failed.", GetProcessId));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    79
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    80
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    81
    LOG_TRACE(tstrings::any() << "Process with PID=" << processId
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    82
                                << " terminated. Exit code=" << exitCode);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    83
    
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    84
    return static_cast<int>(exitCode);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    85
}
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    86
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    87
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    88
UniqueHandle Executor::startProcess() const {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    89
    const std::wstring argsStr = args();
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    90
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    91
    std::vector<TCHAR> argsBuffer(argsStr.begin(), argsStr.end());
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    92
    argsBuffer.push_back(0); // terminating '\0'
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    93
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    94
    STARTUPINFO startupInfo;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    95
    ZeroMemory(&startupInfo, sizeof(startupInfo));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    96
    startupInfo.cb = sizeof(startupInfo);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    97
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    98
    PROCESS_INFORMATION processInfo;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
    99
    ZeroMemory(&processInfo, sizeof(processInfo));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   100
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   101
    DWORD creationFlags = 0;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   102
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   103
    if (!theVisible) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   104
        // For GUI applications.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   105
        startupInfo.dwFlags |= STARTF_USESHOWWINDOW;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   106
        startupInfo.wShowWindow = SW_HIDE;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   107
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   108
        // For console applications.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   109
        creationFlags |= CREATE_NO_WINDOW;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   110
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   111
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   112
    tstrings::any msg;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   113
    msg << "CreateProcess(" << appPath << ", " << argsStr << ")";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   114
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   115
    if (!CreateProcess(appPath.c_str(), argsBuffer.data(), NULL, NULL, FALSE,
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   116
                    creationFlags, NULL, NULL, &startupInfo, &processInfo)) {
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   117
        msg << " failed";
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   118
        JP_THROW(SysError(msg, CreateProcess));
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   119
    }
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   120
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   121
    msg << " succeeded; PID=" << processInfo.dwProcessId;
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   122
    LOG_TRACE(msg);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   123
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   124
    // Close unneeded handles immediately.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   125
    UniqueHandle(processInfo.hThread);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   126
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   127
    // Return process handle.
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   128
    return UniqueHandle(processInfo.hProcess);
45c74e654794 8221333: Replace Inno Setup with custom MSI wrapper for .exe bundler (missed files)
herrick
parents:
diff changeset
   129
}