jruby-launcher 1.1.1-java → 1.1.2-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/COPYING +826 -826
- data/Makefile +122 -122
- data/README.md +64 -64
- data/Rakefile +51 -51
- data/argnames.h +35 -35
- data/argparser.cpp +668 -667
- data/argparser.h +78 -78
- data/extconf.rb +7 -7
- data/inc/Makefile-conf.mk +76 -76
- data/inc/Makefile-impl.mk +107 -107
- data/inc/Makefile-rules.mk +40 -40
- data/jruby.cpp +77 -77
- data/jrubyexe.cpp +84 -84
- data/jvmlauncher.cpp +459 -454
- data/jvmlauncher.h +143 -143
- data/lib/jruby-launcher.rb +3 -3
- data/lib/rubygems/defaults/jruby_native.rb +4 -4
- data/nbexecloader.h +43 -43
- data/ng.c +730 -730
- data/platformlauncher.cpp +252 -252
- data/platformlauncher.h +73 -73
- data/rb_w32_cmdvector.h +287 -287
- data/resources/jruby.rc +25 -25
- data/spec/launcher_spec.rb +262 -258
- data/spec/spec_helper.rb +81 -81
- data/strlcpy.c +72 -72
- data/unixlauncher.cpp +100 -100
- data/unixlauncher.h +22 -22
- data/utilsfuncs.cpp +279 -279
- data/utilsfuncs.h +85 -85
- data/utilsfuncswin.cpp +229 -229
- data/version.h +1 -1
- metadata +3 -3
data/utilsfuncs.h
CHANGED
@@ -1,85 +1,85 @@
|
|
1
|
-
/*
|
2
|
-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3
|
-
*
|
4
|
-
* Copyright 2009-2010 JRuby Team (www.jruby.org).
|
5
|
-
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
|
6
|
-
*
|
7
|
-
* The contents of this file are subject to the terms of either the GNU
|
8
|
-
* General Public License Version 2 only ("GPL") or the Common
|
9
|
-
* Development and Distribution License("CDDL") (collectively, the
|
10
|
-
* "License"). You may not use this file except in compliance with the
|
11
|
-
* License. You can obtain a copy of the License at
|
12
|
-
* http://www.netbeans.org/cddl-gplv2.html
|
13
|
-
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
14
|
-
* specific language governing permissions and limitations under the
|
15
|
-
* License. When distributing the software, include this License Header
|
16
|
-
* Notice in each file and include the License file at
|
17
|
-
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
18
|
-
* particular file as subject to the "Classpath" exception as provided
|
19
|
-
* by Sun in the GPL Version 2 section of the License file that
|
20
|
-
* accompanied this code. If applicable, add the following below the
|
21
|
-
* License Header, with the fields enclosed by brackets [] replaced by
|
22
|
-
* your own identifying information:
|
23
|
-
* "Portions Copyrighted [year] [name of copyright owner]"
|
24
|
-
*
|
25
|
-
* Contributor(s):
|
26
|
-
*
|
27
|
-
* The Original Software is NetBeans. The Initial Developer of the Original
|
28
|
-
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
|
29
|
-
* Microsystems, Inc. All Rights Reserved.
|
30
|
-
*
|
31
|
-
* If you wish your version of this file to be governed by only the CDDL
|
32
|
-
* or only the GPL Version 2, indicate your decision by adding
|
33
|
-
* "[Contributor] elects to include this software in this distribution
|
34
|
-
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
35
|
-
* single choice of license, a recipient has the option to distribute
|
36
|
-
* your version of this file under either the CDDL, the GPL Version 2 or
|
37
|
-
* to extend the choice of license to its licensees as provided above.
|
38
|
-
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
39
|
-
* Version 2 license, then the option applies only if the new code is
|
40
|
-
* made subject to such option by the copyright holder.
|
41
|
-
*
|
42
|
-
* Author: Tomas Holy
|
43
|
-
*/
|
44
|
-
|
45
|
-
#ifndef _UTILSFUNCS_H
|
46
|
-
#define _UTILSFUNCS_H
|
47
|
-
|
48
|
-
#include <string>
|
49
|
-
#include <list>
|
50
|
-
|
51
|
-
bool dirExists(const char *path);
|
52
|
-
bool fileExists(const char *path);
|
53
|
-
void logMsg(const char *format, ...);
|
54
|
-
void logErr(bool appendSysError, bool showMsgBox, const char *format, ...);
|
55
|
-
bool checkLoggingArg(int argc, char *argv[], bool delFile);
|
56
|
-
bool printToConsole(const char *msg);
|
57
|
-
char** convertToArgvArray(std::list<std::string> args);
|
58
|
-
void addToArgList(std::list<std::string> & args, int argc, char ** argv);
|
59
|
-
std::string findOnPath(const char* name);
|
60
|
-
bool checkDirectory(const char* path);
|
61
|
-
void printListToConsole(std::list<std::string> l);
|
62
|
-
std::string trimTrailingBackslashes(std::string orig);
|
63
|
-
|
64
|
-
#ifndef WIN32
|
65
|
-
#define FILE_SEP '/'
|
66
|
-
#define PATH_SEP ':'
|
67
|
-
#else // WIN32
|
68
|
-
#include <windows.h>
|
69
|
-
#define FILE_SEP '\\'
|
70
|
-
#define PATH_SEP ';'
|
71
|
-
|
72
|
-
#ifdef JRUBYW
|
73
|
-
bool setupProcess(int &argc, char *argv[], DWORD &parentProcID, const char *attachMsg = 0);
|
74
|
-
bool getParentProcessID(DWORD &id);
|
75
|
-
#endif /* JRUBYW */
|
76
|
-
|
77
|
-
bool normalizePath(char *path, int len);
|
78
|
-
bool disableFolderVirtualization(HANDLE hProcess);
|
79
|
-
char * getCurrentModulePath(char *path, int pathLen);
|
80
|
-
bool getStringFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, std::string &value);
|
81
|
-
bool getDwordFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, DWORD &value);
|
82
|
-
bool isConsoleAttached();
|
83
|
-
#endif // WIN32
|
84
|
-
|
85
|
-
#endif /* _UTILSFUNCS_H */
|
1
|
+
/*
|
2
|
+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3
|
+
*
|
4
|
+
* Copyright 2009-2010 JRuby Team (www.jruby.org).
|
5
|
+
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
|
6
|
+
*
|
7
|
+
* The contents of this file are subject to the terms of either the GNU
|
8
|
+
* General Public License Version 2 only ("GPL") or the Common
|
9
|
+
* Development and Distribution License("CDDL") (collectively, the
|
10
|
+
* "License"). You may not use this file except in compliance with the
|
11
|
+
* License. You can obtain a copy of the License at
|
12
|
+
* http://www.netbeans.org/cddl-gplv2.html
|
13
|
+
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
14
|
+
* specific language governing permissions and limitations under the
|
15
|
+
* License. When distributing the software, include this License Header
|
16
|
+
* Notice in each file and include the License file at
|
17
|
+
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
|
18
|
+
* particular file as subject to the "Classpath" exception as provided
|
19
|
+
* by Sun in the GPL Version 2 section of the License file that
|
20
|
+
* accompanied this code. If applicable, add the following below the
|
21
|
+
* License Header, with the fields enclosed by brackets [] replaced by
|
22
|
+
* your own identifying information:
|
23
|
+
* "Portions Copyrighted [year] [name of copyright owner]"
|
24
|
+
*
|
25
|
+
* Contributor(s):
|
26
|
+
*
|
27
|
+
* The Original Software is NetBeans. The Initial Developer of the Original
|
28
|
+
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
|
29
|
+
* Microsystems, Inc. All Rights Reserved.
|
30
|
+
*
|
31
|
+
* If you wish your version of this file to be governed by only the CDDL
|
32
|
+
* or only the GPL Version 2, indicate your decision by adding
|
33
|
+
* "[Contributor] elects to include this software in this distribution
|
34
|
+
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
35
|
+
* single choice of license, a recipient has the option to distribute
|
36
|
+
* your version of this file under either the CDDL, the GPL Version 2 or
|
37
|
+
* to extend the choice of license to its licensees as provided above.
|
38
|
+
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
39
|
+
* Version 2 license, then the option applies only if the new code is
|
40
|
+
* made subject to such option by the copyright holder.
|
41
|
+
*
|
42
|
+
* Author: Tomas Holy
|
43
|
+
*/
|
44
|
+
|
45
|
+
#ifndef _UTILSFUNCS_H
|
46
|
+
#define _UTILSFUNCS_H
|
47
|
+
|
48
|
+
#include <string>
|
49
|
+
#include <list>
|
50
|
+
|
51
|
+
bool dirExists(const char *path);
|
52
|
+
bool fileExists(const char *path);
|
53
|
+
void logMsg(const char *format, ...);
|
54
|
+
void logErr(bool appendSysError, bool showMsgBox, const char *format, ...);
|
55
|
+
bool checkLoggingArg(int argc, char *argv[], bool delFile);
|
56
|
+
bool printToConsole(const char *msg);
|
57
|
+
char** convertToArgvArray(std::list<std::string> args);
|
58
|
+
void addToArgList(std::list<std::string> & args, int argc, char ** argv);
|
59
|
+
std::string findOnPath(const char* name);
|
60
|
+
bool checkDirectory(const char* path);
|
61
|
+
void printListToConsole(std::list<std::string> l);
|
62
|
+
std::string trimTrailingBackslashes(std::string orig);
|
63
|
+
|
64
|
+
#ifndef WIN32
|
65
|
+
#define FILE_SEP '/'
|
66
|
+
#define PATH_SEP ':'
|
67
|
+
#else // WIN32
|
68
|
+
#include <windows.h>
|
69
|
+
#define FILE_SEP '\\'
|
70
|
+
#define PATH_SEP ';'
|
71
|
+
|
72
|
+
#ifdef JRUBYW
|
73
|
+
bool setupProcess(int &argc, char *argv[], DWORD &parentProcID, const char *attachMsg = 0);
|
74
|
+
bool getParentProcessID(DWORD &id);
|
75
|
+
#endif /* JRUBYW */
|
76
|
+
|
77
|
+
bool normalizePath(char *path, int len);
|
78
|
+
bool disableFolderVirtualization(HANDLE hProcess);
|
79
|
+
char * getCurrentModulePath(char *path, int pathLen);
|
80
|
+
bool getStringFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, std::string &value);
|
81
|
+
bool getDwordFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, DWORD &value);
|
82
|
+
bool isConsoleAttached();
|
83
|
+
#endif // WIN32
|
84
|
+
|
85
|
+
#endif /* _UTILSFUNCS_H */
|
data/utilsfuncswin.cpp
CHANGED
@@ -1,229 +1,229 @@
|
|
1
|
-
#include "utilsfuncs.h"
|
2
|
-
#include "argnames.h"
|
3
|
-
#include <tlhelp32.h>
|
4
|
-
|
5
|
-
using namespace std;
|
6
|
-
|
7
|
-
bool normalizePath(char *path, int len) {
|
8
|
-
char tmp[MAX_PATH] = "";
|
9
|
-
int i = 0;
|
10
|
-
while (path[i] && i < MAX_PATH - 1) {
|
11
|
-
tmp[i] = path[i] == '/' ? '\\' : path[i];
|
12
|
-
i++;
|
13
|
-
}
|
14
|
-
tmp[i] = '\0';
|
15
|
-
return _fullpath(path, tmp, len) != NULL;
|
16
|
-
}
|
17
|
-
|
18
|
-
bool disableFolderVirtualization(HANDLE hProcess) {
|
19
|
-
OSVERSIONINFO osvi = {0};
|
20
|
-
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
|
21
|
-
if (GetVersionEx(&osvi) && osvi.dwMajorVersion == 6) // check it is Win VISTA
|
22
|
-
{
|
23
|
-
HANDLE hToken;
|
24
|
-
if (OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken)) {
|
25
|
-
DWORD tokenInfoVal = 0;
|
26
|
-
if (!SetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS) 24, &tokenInfoVal, sizeof (DWORD))) {
|
27
|
-
// invalid token information class (24) is OK, it means there is no folder virtualization on current system
|
28
|
-
if (GetLastError() != ERROR_INVALID_PARAMETER) {
|
29
|
-
logErr(true, true, "Failed to set token information.");
|
30
|
-
return false;
|
31
|
-
}
|
32
|
-
}
|
33
|
-
CloseHandle(hToken);
|
34
|
-
} else {
|
35
|
-
logErr(true, true, "Failed to open process token.");
|
36
|
-
return false;
|
37
|
-
}
|
38
|
-
}
|
39
|
-
return true;
|
40
|
-
}
|
41
|
-
|
42
|
-
bool getStringFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, string &value) {
|
43
|
-
logMsg("getStringFromRegistry()\n\tkeyName: %s\n\tvalueName: %s", keyName, valueName);
|
44
|
-
HKEY hKey = 0;
|
45
|
-
if (RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
|
46
|
-
DWORD valSize = 4096;
|
47
|
-
DWORD type = 0;
|
48
|
-
char val[4096] = "";
|
49
|
-
if (RegQueryValueEx(hKey, valueName, 0, &type, (BYTE *) val, &valSize) == ERROR_SUCCESS
|
50
|
-
&& type == REG_SZ) {
|
51
|
-
logMsg("%s: %s", valueName, val);
|
52
|
-
RegCloseKey(hKey);
|
53
|
-
value = val;
|
54
|
-
return true;
|
55
|
-
} else {
|
56
|
-
logErr(true, false, "RegQueryValueEx() failed.");
|
57
|
-
}
|
58
|
-
RegCloseKey(hKey);
|
59
|
-
} else {
|
60
|
-
logErr(true, false, "RegOpenKeyEx() failed.");
|
61
|
-
}
|
62
|
-
return false;
|
63
|
-
}
|
64
|
-
|
65
|
-
bool getDwordFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, DWORD &value) {
|
66
|
-
logMsg("getDwordFromRegistry()\n\tkeyName: %s\n\tvalueName: %s", keyName, valueName);
|
67
|
-
HKEY hKey = 0;
|
68
|
-
if (RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
|
69
|
-
DWORD valSize = sizeof(DWORD);
|
70
|
-
DWORD type = 0;
|
71
|
-
if (RegQueryValueEx(hKey, valueName, 0, &type, (BYTE *) &value, &valSize) == ERROR_SUCCESS
|
72
|
-
&& type == REG_DWORD) {
|
73
|
-
logMsg("%s: %u", valueName, value);
|
74
|
-
RegCloseKey(hKey);
|
75
|
-
return true;
|
76
|
-
} else {
|
77
|
-
logErr(true, false, "RegQueryValueEx() failed.");
|
78
|
-
}
|
79
|
-
RegCloseKey(hKey);
|
80
|
-
} else {
|
81
|
-
logErr(true, false, "RegOpenKeyEx() failed.");
|
82
|
-
}
|
83
|
-
return false;
|
84
|
-
}
|
85
|
-
|
86
|
-
char * getCurrentModulePath(char *path, int pathLen) {
|
87
|
-
MEMORY_BASIC_INFORMATION mbi;
|
88
|
-
static int dummy;
|
89
|
-
VirtualQuery(&dummy, &mbi, sizeof (mbi));
|
90
|
-
HMODULE hModule = (HMODULE) mbi.AllocationBase;
|
91
|
-
GetModuleFileName(hModule, path, pathLen);
|
92
|
-
return path;
|
93
|
-
}
|
94
|
-
|
95
|
-
#ifdef JRUBYW
|
96
|
-
bool setupProcess(int &argc, char *argv[], DWORD &parentProcID, const char *attachMsg) {
|
97
|
-
#define CHECK_ARG \
|
98
|
-
if (i+1 == argc) {\
|
99
|
-
logErr(false, true, "Argument is missing for \"%s\" option.", argv[i]);\
|
100
|
-
return false;\
|
101
|
-
}
|
102
|
-
|
103
|
-
parentProcID = 0;
|
104
|
-
DWORD cmdLineArgPPID = 0;
|
105
|
-
for (int i = 0; i < argc; i++) {
|
106
|
-
// break arg parsing, once "--" is found
|
107
|
-
if (strcmp("--", argv[i]) == 0) {
|
108
|
-
break;
|
109
|
-
}
|
110
|
-
if (strcmp(ARG_NAME_CONSOLE, argv[i]) == 0) {
|
111
|
-
CHECK_ARG;
|
112
|
-
if (strcmp("new", argv[i + 1]) == 0){
|
113
|
-
logMsg("Allocating new console...");
|
114
|
-
AllocConsole();
|
115
|
-
} else if (strcmp("suppress", argv[i + 1]) == 0) {
|
116
|
-
logMsg("Suppressing the attachment to console...");
|
117
|
-
// nothing, no console should be attached
|
118
|
-
} else if (strcmp("attach", argv[i + 1]) == 0) {
|
119
|
-
logMsg("Trying to attach to the existing console...");
|
120
|
-
// attach to parent process console if exists
|
121
|
-
// AttachConsole exists since WinXP, so be nice and do it dynamically
|
122
|
-
typedef BOOL(WINAPI * LPFAC)(DWORD dwProcessId);
|
123
|
-
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
124
|
-
if (hKernel32) {
|
125
|
-
LPFAC attachConsole = (LPFAC) GetProcAddress(hKernel32, "AttachConsole");
|
126
|
-
if (attachConsole) {
|
127
|
-
if (cmdLineArgPPID) {
|
128
|
-
if (!attachConsole(cmdLineArgPPID)) {
|
129
|
-
logErr(true, false, "AttachConsole of PPID: %u failed.", cmdLineArgPPID);
|
130
|
-
}
|
131
|
-
} else {
|
132
|
-
if (!attachConsole((DWORD) - 1)) {
|
133
|
-
logErr(true, true, "AttachConsole of PP failed.");
|
134
|
-
} else {
|
135
|
-
getParentProcessID(parentProcID);
|
136
|
-
if (attachMsg) {
|
137
|
-
printToConsole(attachMsg);
|
138
|
-
}
|
139
|
-
}
|
140
|
-
}
|
141
|
-
} else {
|
142
|
-
logErr(true, false, "GetProcAddress() for AttachConsole failed.");
|
143
|
-
}
|
144
|
-
}
|
145
|
-
} else {
|
146
|
-
logErr(false, true, "Invalid argument for \"%s\" option.", argv[i]);
|
147
|
-
return false;
|
148
|
-
}
|
149
|
-
// remove options
|
150
|
-
for (int k = i + 2; k < argc; k++) {
|
151
|
-
argv[k-2] = argv[k];
|
152
|
-
}
|
153
|
-
argc -= 2;
|
154
|
-
return true;
|
155
|
-
}
|
156
|
-
}
|
157
|
-
#undef CHECK_ARG
|
158
|
-
|
159
|
-
return true;
|
160
|
-
}
|
161
|
-
#endif /* JRUBYW */
|
162
|
-
|
163
|
-
bool isConsoleAttached() {
|
164
|
-
typedef HWND (WINAPI *GetConsoleWindowT)();
|
165
|
-
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
166
|
-
if (hKernel32) {
|
167
|
-
GetConsoleWindowT getConsoleWindow = (GetConsoleWindowT) GetProcAddress(hKernel32, "GetConsoleWindow");
|
168
|
-
if (getConsoleWindow) {
|
169
|
-
if (getConsoleWindow() != NULL) {
|
170
|
-
logMsg("Console is attached.");
|
171
|
-
return true;
|
172
|
-
}
|
173
|
-
} else {
|
174
|
-
logErr(true, false, "GetProcAddress() for GetConsoleWindow failed.");
|
175
|
-
}
|
176
|
-
}
|
177
|
-
return false;
|
178
|
-
}
|
179
|
-
|
180
|
-
#ifdef JRUBYW
|
181
|
-
bool getParentProcessID(DWORD &id) {
|
182
|
-
typedef HANDLE (WINAPI * CreateToolhelp32SnapshotT)(DWORD, DWORD);
|
183
|
-
typedef BOOL (WINAPI * Process32FirstT)(HANDLE, LPPROCESSENTRY32);
|
184
|
-
typedef BOOL (WINAPI * Process32NextT)(HANDLE, LPPROCESSENTRY32);
|
185
|
-
|
186
|
-
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
187
|
-
if (!hKernel32) {
|
188
|
-
return false;
|
189
|
-
}
|
190
|
-
|
191
|
-
CreateToolhelp32SnapshotT createToolhelp32Snapshot = (CreateToolhelp32SnapshotT) GetProcAddress(hKernel32, "CreateToolhelp32Snapshot");
|
192
|
-
Process32FirstT process32First = (Process32FirstT) GetProcAddress(hKernel32, "Process32First");
|
193
|
-
Process32NextT process32Next = (Process32NextT) GetProcAddress(hKernel32, "Process32Next");
|
194
|
-
|
195
|
-
if (createToolhelp32Snapshot == NULL || process32First == NULL || process32Next == NULL) {
|
196
|
-
logErr(true, false, "Failed to obtain Toolhelp32 functions.");
|
197
|
-
return false;
|
198
|
-
}
|
199
|
-
|
200
|
-
HANDLE hSnapshot = createToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
201
|
-
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
202
|
-
logErr(true, false, "Failed to obtain process snapshot.");
|
203
|
-
return false;
|
204
|
-
}
|
205
|
-
|
206
|
-
PROCESSENTRY32 entry = {0};
|
207
|
-
entry.dwSize = sizeof (PROCESSENTRY32);
|
208
|
-
if (!process32First(hSnapshot, &entry)) {
|
209
|
-
CloseHandle(hSnapshot);
|
210
|
-
return false;
|
211
|
-
}
|
212
|
-
|
213
|
-
DWORD curID = GetCurrentProcessId();
|
214
|
-
logMsg("Current process ID: %u", curID);
|
215
|
-
|
216
|
-
do {
|
217
|
-
if (entry.th32ProcessID == curID) {
|
218
|
-
id = entry.th32ParentProcessID;
|
219
|
-
logMsg("Parent process ID: %u", id);
|
220
|
-
CloseHandle(hSnapshot);
|
221
|
-
return true;
|
222
|
-
}
|
223
|
-
} while (process32Next(hSnapshot, &entry));
|
224
|
-
|
225
|
-
CloseHandle(hSnapshot);
|
226
|
-
return false;
|
227
|
-
}
|
228
|
-
#endif /* JRUBYW */
|
229
|
-
|
1
|
+
#include "utilsfuncs.h"
|
2
|
+
#include "argnames.h"
|
3
|
+
#include <tlhelp32.h>
|
4
|
+
|
5
|
+
using namespace std;
|
6
|
+
|
7
|
+
bool normalizePath(char *path, int len) {
|
8
|
+
char tmp[MAX_PATH] = "";
|
9
|
+
int i = 0;
|
10
|
+
while (path[i] && i < MAX_PATH - 1) {
|
11
|
+
tmp[i] = path[i] == '/' ? '\\' : path[i];
|
12
|
+
i++;
|
13
|
+
}
|
14
|
+
tmp[i] = '\0';
|
15
|
+
return _fullpath(path, tmp, len) != NULL;
|
16
|
+
}
|
17
|
+
|
18
|
+
bool disableFolderVirtualization(HANDLE hProcess) {
|
19
|
+
OSVERSIONINFO osvi = {0};
|
20
|
+
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
|
21
|
+
if (GetVersionEx(&osvi) && osvi.dwMajorVersion == 6) // check it is Win VISTA
|
22
|
+
{
|
23
|
+
HANDLE hToken;
|
24
|
+
if (OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken)) {
|
25
|
+
DWORD tokenInfoVal = 0;
|
26
|
+
if (!SetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS) 24, &tokenInfoVal, sizeof (DWORD))) {
|
27
|
+
// invalid token information class (24) is OK, it means there is no folder virtualization on current system
|
28
|
+
if (GetLastError() != ERROR_INVALID_PARAMETER) {
|
29
|
+
logErr(true, true, "Failed to set token information.");
|
30
|
+
return false;
|
31
|
+
}
|
32
|
+
}
|
33
|
+
CloseHandle(hToken);
|
34
|
+
} else {
|
35
|
+
logErr(true, true, "Failed to open process token.");
|
36
|
+
return false;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
return true;
|
40
|
+
}
|
41
|
+
|
42
|
+
bool getStringFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, string &value) {
|
43
|
+
logMsg("getStringFromRegistry()\n\tkeyName: %s\n\tvalueName: %s", keyName, valueName);
|
44
|
+
HKEY hKey = 0;
|
45
|
+
if (RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
|
46
|
+
DWORD valSize = 4096;
|
47
|
+
DWORD type = 0;
|
48
|
+
char val[4096] = "";
|
49
|
+
if (RegQueryValueEx(hKey, valueName, 0, &type, (BYTE *) val, &valSize) == ERROR_SUCCESS
|
50
|
+
&& type == REG_SZ) {
|
51
|
+
logMsg("%s: %s", valueName, val);
|
52
|
+
RegCloseKey(hKey);
|
53
|
+
value = val;
|
54
|
+
return true;
|
55
|
+
} else {
|
56
|
+
logErr(true, false, "RegQueryValueEx() failed.");
|
57
|
+
}
|
58
|
+
RegCloseKey(hKey);
|
59
|
+
} else {
|
60
|
+
logErr(true, false, "RegOpenKeyEx() failed.");
|
61
|
+
}
|
62
|
+
return false;
|
63
|
+
}
|
64
|
+
|
65
|
+
bool getDwordFromRegistry(HKEY rootKey, const char *keyName, const char *valueName, DWORD &value) {
|
66
|
+
logMsg("getDwordFromRegistry()\n\tkeyName: %s\n\tvalueName: %s", keyName, valueName);
|
67
|
+
HKEY hKey = 0;
|
68
|
+
if (RegOpenKeyEx(rootKey, keyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
|
69
|
+
DWORD valSize = sizeof(DWORD);
|
70
|
+
DWORD type = 0;
|
71
|
+
if (RegQueryValueEx(hKey, valueName, 0, &type, (BYTE *) &value, &valSize) == ERROR_SUCCESS
|
72
|
+
&& type == REG_DWORD) {
|
73
|
+
logMsg("%s: %u", valueName, value);
|
74
|
+
RegCloseKey(hKey);
|
75
|
+
return true;
|
76
|
+
} else {
|
77
|
+
logErr(true, false, "RegQueryValueEx() failed.");
|
78
|
+
}
|
79
|
+
RegCloseKey(hKey);
|
80
|
+
} else {
|
81
|
+
logErr(true, false, "RegOpenKeyEx() failed.");
|
82
|
+
}
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
|
86
|
+
char * getCurrentModulePath(char *path, int pathLen) {
|
87
|
+
MEMORY_BASIC_INFORMATION mbi;
|
88
|
+
static int dummy;
|
89
|
+
VirtualQuery(&dummy, &mbi, sizeof (mbi));
|
90
|
+
HMODULE hModule = (HMODULE) mbi.AllocationBase;
|
91
|
+
GetModuleFileName(hModule, path, pathLen);
|
92
|
+
return path;
|
93
|
+
}
|
94
|
+
|
95
|
+
#ifdef JRUBYW
|
96
|
+
bool setupProcess(int &argc, char *argv[], DWORD &parentProcID, const char *attachMsg) {
|
97
|
+
#define CHECK_ARG \
|
98
|
+
if (i+1 == argc) {\
|
99
|
+
logErr(false, true, "Argument is missing for \"%s\" option.", argv[i]);\
|
100
|
+
return false;\
|
101
|
+
}
|
102
|
+
|
103
|
+
parentProcID = 0;
|
104
|
+
DWORD cmdLineArgPPID = 0;
|
105
|
+
for (int i = 0; i < argc; i++) {
|
106
|
+
// break arg parsing, once "--" is found
|
107
|
+
if (strcmp("--", argv[i]) == 0) {
|
108
|
+
break;
|
109
|
+
}
|
110
|
+
if (strcmp(ARG_NAME_CONSOLE, argv[i]) == 0) {
|
111
|
+
CHECK_ARG;
|
112
|
+
if (strcmp("new", argv[i + 1]) == 0){
|
113
|
+
logMsg("Allocating new console...");
|
114
|
+
AllocConsole();
|
115
|
+
} else if (strcmp("suppress", argv[i + 1]) == 0) {
|
116
|
+
logMsg("Suppressing the attachment to console...");
|
117
|
+
// nothing, no console should be attached
|
118
|
+
} else if (strcmp("attach", argv[i + 1]) == 0) {
|
119
|
+
logMsg("Trying to attach to the existing console...");
|
120
|
+
// attach to parent process console if exists
|
121
|
+
// AttachConsole exists since WinXP, so be nice and do it dynamically
|
122
|
+
typedef BOOL(WINAPI * LPFAC)(DWORD dwProcessId);
|
123
|
+
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
124
|
+
if (hKernel32) {
|
125
|
+
LPFAC attachConsole = (LPFAC) GetProcAddress(hKernel32, "AttachConsole");
|
126
|
+
if (attachConsole) {
|
127
|
+
if (cmdLineArgPPID) {
|
128
|
+
if (!attachConsole(cmdLineArgPPID)) {
|
129
|
+
logErr(true, false, "AttachConsole of PPID: %u failed.", cmdLineArgPPID);
|
130
|
+
}
|
131
|
+
} else {
|
132
|
+
if (!attachConsole((DWORD) - 1)) {
|
133
|
+
logErr(true, true, "AttachConsole of PP failed.");
|
134
|
+
} else {
|
135
|
+
getParentProcessID(parentProcID);
|
136
|
+
if (attachMsg) {
|
137
|
+
printToConsole(attachMsg);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
} else {
|
142
|
+
logErr(true, false, "GetProcAddress() for AttachConsole failed.");
|
143
|
+
}
|
144
|
+
}
|
145
|
+
} else {
|
146
|
+
logErr(false, true, "Invalid argument for \"%s\" option.", argv[i]);
|
147
|
+
return false;
|
148
|
+
}
|
149
|
+
// remove options
|
150
|
+
for (int k = i + 2; k < argc; k++) {
|
151
|
+
argv[k-2] = argv[k];
|
152
|
+
}
|
153
|
+
argc -= 2;
|
154
|
+
return true;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
#undef CHECK_ARG
|
158
|
+
|
159
|
+
return true;
|
160
|
+
}
|
161
|
+
#endif /* JRUBYW */
|
162
|
+
|
163
|
+
bool isConsoleAttached() {
|
164
|
+
typedef HWND (WINAPI *GetConsoleWindowT)();
|
165
|
+
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
166
|
+
if (hKernel32) {
|
167
|
+
GetConsoleWindowT getConsoleWindow = (GetConsoleWindowT) GetProcAddress(hKernel32, "GetConsoleWindow");
|
168
|
+
if (getConsoleWindow) {
|
169
|
+
if (getConsoleWindow() != NULL) {
|
170
|
+
logMsg("Console is attached.");
|
171
|
+
return true;
|
172
|
+
}
|
173
|
+
} else {
|
174
|
+
logErr(true, false, "GetProcAddress() for GetConsoleWindow failed.");
|
175
|
+
}
|
176
|
+
}
|
177
|
+
return false;
|
178
|
+
}
|
179
|
+
|
180
|
+
#ifdef JRUBYW
|
181
|
+
bool getParentProcessID(DWORD &id) {
|
182
|
+
typedef HANDLE (WINAPI * CreateToolhelp32SnapshotT)(DWORD, DWORD);
|
183
|
+
typedef BOOL (WINAPI * Process32FirstT)(HANDLE, LPPROCESSENTRY32);
|
184
|
+
typedef BOOL (WINAPI * Process32NextT)(HANDLE, LPPROCESSENTRY32);
|
185
|
+
|
186
|
+
HINSTANCE hKernel32 = GetModuleHandle("kernel32");
|
187
|
+
if (!hKernel32) {
|
188
|
+
return false;
|
189
|
+
}
|
190
|
+
|
191
|
+
CreateToolhelp32SnapshotT createToolhelp32Snapshot = (CreateToolhelp32SnapshotT) GetProcAddress(hKernel32, "CreateToolhelp32Snapshot");
|
192
|
+
Process32FirstT process32First = (Process32FirstT) GetProcAddress(hKernel32, "Process32First");
|
193
|
+
Process32NextT process32Next = (Process32NextT) GetProcAddress(hKernel32, "Process32Next");
|
194
|
+
|
195
|
+
if (createToolhelp32Snapshot == NULL || process32First == NULL || process32Next == NULL) {
|
196
|
+
logErr(true, false, "Failed to obtain Toolhelp32 functions.");
|
197
|
+
return false;
|
198
|
+
}
|
199
|
+
|
200
|
+
HANDLE hSnapshot = createToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
201
|
+
if (hSnapshot == INVALID_HANDLE_VALUE) {
|
202
|
+
logErr(true, false, "Failed to obtain process snapshot.");
|
203
|
+
return false;
|
204
|
+
}
|
205
|
+
|
206
|
+
PROCESSENTRY32 entry = {0};
|
207
|
+
entry.dwSize = sizeof (PROCESSENTRY32);
|
208
|
+
if (!process32First(hSnapshot, &entry)) {
|
209
|
+
CloseHandle(hSnapshot);
|
210
|
+
return false;
|
211
|
+
}
|
212
|
+
|
213
|
+
DWORD curID = GetCurrentProcessId();
|
214
|
+
logMsg("Current process ID: %u", curID);
|
215
|
+
|
216
|
+
do {
|
217
|
+
if (entry.th32ProcessID == curID) {
|
218
|
+
id = entry.th32ParentProcessID;
|
219
|
+
logMsg("Parent process ID: %u", id);
|
220
|
+
CloseHandle(hSnapshot);
|
221
|
+
return true;
|
222
|
+
}
|
223
|
+
} while (process32Next(hSnapshot, &entry));
|
224
|
+
|
225
|
+
CloseHandle(hSnapshot);
|
226
|
+
return false;
|
227
|
+
}
|
228
|
+
#endif /* JRUBYW */
|
229
|
+
|