blob: b4eb15165d4c9c3b6c8f071fa17cce48a8378515 [file] [log] [blame]
Carl Shapiro6b6b5f02011-06-21 15:05:09 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Carl Shapiro2ed144c2011-07-26 16:52:08 -07003#include <cstring>
4#include <cstdio>
5#include <signal.h>
6#include <string>
Carl Shapiro7b216702011-06-17 15:09:26 -07007
Carl Shapiro2ed144c2011-07-26 16:52:08 -07008#include "jni.h"
9#include "logging.h"
10#include "scoped_ptr.h"
Elliott Hughes0a96fe02011-08-19 10:22:04 -070011#include "toStringArray.h"
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070012#include "ScopedLocalRef.h"
Carl Shapiro2ed144c2011-07-26 16:52:08 -070013
14// TODO: move this into the runtime.
15static void BlockSigpipe() {
16 sigset_t sigset;
17 if (sigemptyset(&sigset) == -1) {
18 PLOG(ERROR) << "sigemptyset failed";
19 return;
20 }
21 if (sigaddset(&sigset, SIGPIPE) == -1) {
22 PLOG(ERROR) << "sigaddset failed";
23 return;
24 }
25 if (sigprocmask(SIG_BLOCK, &sigset, NULL) == -1) {
26 PLOG(ERROR) << "sigprocmask failed";
27 }
28}
29
Carl Shapiro2ed144c2011-07-26 16:52:08 -070030// Determine whether or not the specified method is public.
31//
32// Returns JNI_TRUE on success, JNI_FALSE on failure.
33static bool IsMethodPublic(JNIEnv* env, jclass clazz, jmethodID method_id) {
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070034 ScopedLocalRef<jobject> reflected(env, env->ToReflectedMethod(clazz,
35 method_id, JNI_FALSE));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070036 if (reflected.get() == NULL) {
37 fprintf(stderr, "Unable to get reflected method\n");
38 return false;
39 }
40 // We now have a Method instance. We need to call its
41 // getModifiers() method.
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070042 ScopedLocalRef<jclass> method(env,
43 env->FindClass("java/lang/reflect/Method"));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070044 if (method.get() == NULL) {
45 fprintf(stderr, "Unable to find class Method\n");
46 return false;
47 }
48 jmethodID get_modifiers = env->GetMethodID(method.get(),
49 "getModifiers",
50 "()I");
51 if (get_modifiers == NULL) {
52 fprintf(stderr, "Unable to find reflect.Method.getModifiers\n");
53 return false;
54 }
55 static const int PUBLIC = 0x0001; // java.lang.reflect.Modifiers.PUBLIC
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070056#if 0 // CallIntMethod not yet implemented
Carl Shapiro2ed144c2011-07-26 16:52:08 -070057 int modifiers = env->CallIntMethod(method.get(), get_modifiers);
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070058#else
59 int modifiers = PUBLIC;
60 UNIMPLEMENTED(WARNING) << "assuming main is public...";
61#endif
Carl Shapiro2ed144c2011-07-26 16:52:08 -070062 if ((modifiers & PUBLIC) == 0) {
63 return false;
64 }
65 return true;
66}
67
Elliott Hughesf2682d52011-08-15 16:37:04 -070068static bool InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070069 // We want to call main() with a String array with our arguments in
70 // it. Create an array and populate it. Note argv[0] is not
71 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070072 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070073 if (args.get() == NULL) {
74 return false;
75 }
76
77 // Find [class].main(String[]).
78
79 // Convert "com.android.Blah" to "com/android/Blah".
80 std::string class_name = argv[0];
81 std::replace(class_name.begin(), class_name.end(), '.', '/');
82
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070083 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070084 if (klass.get() == NULL) {
85 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
86 return false;
87 }
88
89 jmethodID method = env->GetStaticMethodID(klass.get(),
90 "main",
91 "([Ljava/lang/String;)V");
92 if (method == NULL) {
93 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
94 class_name.c_str());
95 return false;
96 }
97
98 // Make sure the method is public. JNI doesn't prevent us from
99 // calling a private method, so we have to check it explicitly.
100 if (!IsMethodPublic(env, klass.get(), method)) {
101 fprintf(stderr, "Sorry, main() is not public\n");
102 return false;
103 }
104
105 // Invoke main().
106
107 env->CallStaticVoidMethod(klass.get(), method, args.get());
108 if (env->ExceptionCheck()) {
109 return false;
110 } else {
111 return true;
112 }
113}
114
115// Parse arguments. Most of it just gets passed through to the VM.
116// The JNI spec defines a handful of standard arguments.
Carl Shapiro7b216702011-06-17 15:09:26 -0700117int main(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700118 setvbuf(stdout, NULL, _IONBF, 0);
119
120 // Skip over argv[0].
121 argv++;
122 argc--;
123
124 // If we're adding any additional stuff, e.g. function hook specifiers,
125 // add them to the count here.
126 //
127 // We're over-allocating, because this includes the options to the VM
128 // plus the options to the program.
129 int option_count = argc;
130 scoped_array<JavaVMOption> options(new JavaVMOption[option_count]());
131
132 // Copy options over. Everything up to the name of the class starts
133 // with a '-' (the function hook stuff is strictly internal).
134 //
135 // [Do we need to catch & handle "-jar" here?]
136 bool need_extra = false;
137 int curr_opt, arg_idx;
138 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
139 if (argv[arg_idx][0] != '-' && !need_extra) {
140 break;
141 }
142 options[curr_opt++].optionString = argv[arg_idx];
143
144 // Some options require an additional argument.
145 need_extra = false;
146 if (strcmp(argv[arg_idx], "-classpath") == 0 ||
147 strcmp(argv[arg_idx], "-cp") == 0) {
148 // others?
149 need_extra = true;
150 }
151 }
152
153 if (need_extra) {
154 fprintf(stderr, "VM requires value after last option flag\n");
155 return EXIT_FAILURE;
156 }
157
158 // Make sure they provided a class name. We do this after VM init
159 // so that things like "-Xrunjdwp:help" have the opportunity to emit
160 // a usage statement.
161 if (arg_idx == argc) {
162 fprintf(stderr, "Class name required\n");
163 return EXIT_FAILURE;
164 }
165
166 // insert additional internal options here
167
168 DCHECK_LE(curr_opt, option_count);
169
170 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700171 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700172 init_args.options = options.get();
173 init_args.nOptions = curr_opt;
174 init_args.ignoreUnrecognized = JNI_FALSE;
175
176 BlockSigpipe();
177
178 // Start VM. The current thread becomes the main thread of the VM.
179 JavaVM* vm = NULL;
180 JNIEnv* env = NULL;
181 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
182 fprintf(stderr, "VM init failed (check log file)\n");
183 return EXIT_FAILURE;
184 }
185
Elliott Hughesf2682d52011-08-15 16:37:04 -0700186 bool success = InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700187
Elliott Hughesf2682d52011-08-15 16:37:04 -0700188 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700189 fprintf(stderr, "Warning: unable to detach main thread\n");
190 success = false;
191 }
192
Elliott Hughesf2682d52011-08-15 16:37:04 -0700193 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700194 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
195 success = false;
196 }
197
Elliott Hughesf2682d52011-08-15 16:37:04 -0700198 return success ? EXIT_SUCCESS : EXIT_FAILURE;
Carl Shapiro7b216702011-06-17 15:09:26 -0700199}