-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathunixlauncher.cpp
131 lines (110 loc) · 3.55 KB
/
unixlauncher.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include "unixlauncher.h"
#include "utilsfuncs.h"
using namespace std;
extern "C" int nailgunClientMain(int argc, char *argv[], char *env[]);
UnixLauncher::UnixLauncher()
: ArgParser()
{
}
UnixLauncher::UnixLauncher(const UnixLauncher& orig)
: ArgParser(orig)
{
}
UnixLauncher::~UnixLauncher() {
}
int UnixLauncher::run(int argc, char* argv[], char* envp[]) {
platformDir = argv[0];
if (!initPlatformDir() || !parseArgs(argc - 1, argv + 1)) {
return 255;
}
if (nailgunClient) {
progArgs.push_front("org.jruby.util.NailMain");
char ** nailArgv = convertToArgvArray(progArgs);
int nailArgc = progArgs.size();
if (printCommandLine) {
printListToConsole(progArgs);
for (int i = 0; i < nailArgc; i++) {
free(nailArgv[i]);
}
delete[] nailArgv;
return 0;
}
return nailgunClientMain(progArgs.size(), (char**)nailArgv, envp);
}
string java("");
if (getenv("JAVACMD") != NULL) {
java = getenv("JAVACMD");
if (java.find_last_of('/') == -1) {
java = findOnPath(java.c_str());
}
} else {
if (!jdkhome.empty()) {
java = jdkhome + "/bin/java";
} else if (getenv("JAVA_HOME") != NULL) {
string java_home = string(getenv("JAVA_HOME"));
jdkhome = java_home;
java_home = trimTrailingBackslashes(java_home);
java = java_home + "/bin/java";
} else {
java = findOnPath("java");
}
}
if (java.empty()) {
printToConsole("No `java' executable found on PATH.");
return 255;
}
// still no jdk home, use other means to resolve it
if (jdkhome.empty()) {
char javaHomeCommand[] = "/usr/libexec/java_home";
if (access(javaHomeCommand, R_OK | X_OK) != -1 && !checkDirectory(javaHomeCommand)) {
// try java_home command when not set (on MacOS)
FILE *fp;
char tmp[PATH_MAX + 1];
fp = popen(javaHomeCommand, "r");
if (fp != NULL) {
fgets(tmp, sizeof(tmp), fp);
tmp[strcspn(tmp, "\n")] = 0;
jdkhome = tmp;
pclose(fp);
} else {
logErr(true, false, "failed to run %s", javaHomeCommand);
}
} else {
java = resolveSymlinks(java);
int home_index = java.find_last_of('/', java.find_last_of('/') - 1);
jdkhome = java.substr(0, home_index);
}
}
prepareOptions();
list<string> commandLine;
commandLine.push_back(java);
addOptionsToCommandLine(commandLine);
logMsg("Command line:");
for (list<string>::iterator it = commandLine.begin(); it != commandLine.end(); ++it) {
logMsg("\t%s", it->c_str());
}
char** newArgv = convertToArgvArray(commandLine);
int newArgc = commandLine.size();
if (printCommandLine) {
printListToConsole(commandLine);
for (int i = 0; i < newArgc; i++) {
free(newArgv[i]);
}
delete[] newArgv;
return 0;
}
if (!fileExists(java.c_str())) {
string msg = "No `java' exists at " + java + ", please double-check JAVA_HOME.\n";
printToConsole(msg.c_str());
return 255;
}
execv(java.c_str(), newArgv);
// shouldn't get here unless something bad happened with execv
logErr(true, true, "execv failed:");
return 255;
}