/
JavaLauncher.cpp
217 lines (185 loc) · 5.73 KB
/
JavaLauncher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
*
* Shared Memory FMI
*
* Copyright (C) 2015 - 2017 Overture
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*
* Author: Kenneth Lausdahl
*/
#include "JavaLauncher.h"
bool JavaLauncher::debug = true;
int javaLauncherInternalDebugPrint(void* sender, const char* format, ...) {
if (!JavaLauncher::debug) {
return 0;
}
va_list args;
va_start(args, format);
int ret = vfprintf(stdout, format, args);
va_end(args);
return ret;
}
JavaLauncher::debugPrintType* JavaLauncher::debugPrintPtr =
&javaLauncherInternalDebugPrint;
JavaLauncher::JavaLauncher(bool visible, const char* workingDir, char** args) {
this->m_workingDir = workingDir;
this->m_launched = false;
this->m_args = args;
this->m_visible = visible;
#ifdef _WIN32
#elif __APPLE__ || __linux
this->pid = -1;
#endif
}
JavaLauncher::~JavaLauncher() {}
#ifdef _WIN32
// Returns the last Win32 error, in string format. Returns an empty string if
// there is no error.
std::string GetLastErrorAsString() {
// Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return std::string(); // No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
// Free the buffer.
LocalFree(messageBuffer);
return message;
}
// Create a string with last error message
std::string GetLastErrorStdStr() {
DWORD error = GetLastError();
if (error) {
LPVOID lpMsgBuf;
DWORD bufLen = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
if (bufLen) {
LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
std::string result(lpMsgStr, lpMsgStr + bufLen);
LocalFree(lpMsgBuf);
return result;
}
}
return std::string();
}
#endif
int JavaLauncher::launch() {
#ifdef _WIN32
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
std::string cmd = "";
int i = 0;
while (m_args[i] != NULL) {
cmd += m_args[i];
cmd += " ";
i++;
}
int kk = cmd.length();
char* buf = new char[cmd.length()];
memcpy(buf, cmd.c_str(), cmd.length());
buf[kk - 1] = 0;
debugPrintPtr(this,
"Launching process: '%s' with arguments: '%s' in folder '%s'\n",
m_args[0], buf, m_workingDir);
// Start the child process.
if (!CreateProcess(NULL, // m_args[0], // No module name (use command line)
buf, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
(m_visible ? 0 : CREATE_NO_WINDOW), // No creation flags
NULL, // Use parent's environment block
m_workingDir, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
// printf("CreateProcess failed (%d).\n", GetLastError());
debugPrintPtr(this, "---CresteProcess failed %s\n",
GetLastErrorStdStr().c_str());
delete buf;
return 1;
}
delete buf;
debugPrintPtr(this, "Process launched continuing main thread.\n");
this->pid = pi;
this->m_launched = true;
return 0;
#elif __APPLE__ || __linux
int status;
pid_t pid2;
pid = fork();
if (pid == 0) {
/* child process B */
pid2 = fork();
if (pid2) {
exit(0);
} else if (!pid2) {
/* child process C */
debugPrintPtr(this, "Fork application %s\n", m_args[0]);
if (m_workingDir != NULL) {
if (chdir(m_workingDir) == 0) {
debugPrintPtr(this, "Changed dir to: %s\n", m_workingDir);
} else {
debugPrintPtr(
this, "server_create: failed to fork and change dir: %01d %s\n",
__LINE__, strerror(errno));
}
}
int i = 0;
while (m_args[i] != NULL) {
debugPrintPtr(this, "%s\n", m_args[i]);
i++;
}
status = execvp(m_args[0], m_args);
} else {
/* error */
}
} else if (pid == -1) {
// Parent process will return a non-zero value from fork()
debugPrintPtr(this, "server_create: failed to fork: %01d %s\n", __LINE__,
strerror(errno));
m_launched = false;
} else {
m_launched = true;
/* parent process A */
waitpid(pid, &status, 0);
}
debugPrintPtr(this, "Fork pid %d\n", pid);
return status;
#endif
}
void JavaLauncher::terminate() {
if (m_launched) {
#ifdef _WIN32
UINT uExitCode = 0;
TerminateProcess(pid.hProcess, uExitCode);
#elif __APPLE__ || __linux
// kill(pid, SIGKILL);
#endif
m_launched = false;
}
}