blob: 381f8f7d95234a39e22a7da24ee20d9eb8a1d70f [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
56 int modifiers = env->CallIntMethod(method.get(), get_modifiers);
57 if ((modifiers & PUBLIC) == 0) {
58 return false;
59 }
60 return true;
61}
62
Elliott Hughesf2682d52011-08-15 16:37:04 -070063static bool InvokeMain(JNIEnv* env, int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -070064 // We want to call main() with a String array with our arguments in
65 // it. Create an array and populate it. Note argv[0] is not
66 // included.
Elliott Hughes0a96fe02011-08-19 10:22:04 -070067 ScopedLocalRef<jobjectArray> args(env, toStringArray(env, argv + 1));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070068 if (args.get() == NULL) {
69 return false;
70 }
71
72 // Find [class].main(String[]).
73
74 // Convert "com.android.Blah" to "com/android/Blah".
75 std::string class_name = argv[0];
76 std::replace(class_name.begin(), class_name.end(), '.', '/');
77
Elliott Hughes0dab4ec2011-08-11 12:19:32 -070078 ScopedLocalRef<jclass> klass(env, env->FindClass(class_name.c_str()));
Carl Shapiro2ed144c2011-07-26 16:52:08 -070079 if (klass.get() == NULL) {
80 fprintf(stderr, "Unable to locate class '%s'\n", class_name.c_str());
81 return false;
82 }
83
84 jmethodID method = env->GetStaticMethodID(klass.get(),
85 "main",
86 "([Ljava/lang/String;)V");
87 if (method == NULL) {
88 fprintf(stderr, "Unable to find static main(String[]) in '%s'\n",
89 class_name.c_str());
90 return false;
91 }
92
93 // Make sure the method is public. JNI doesn't prevent us from
94 // calling a private method, so we have to check it explicitly.
95 if (!IsMethodPublic(env, klass.get(), method)) {
96 fprintf(stderr, "Sorry, main() is not public\n");
97 return false;
98 }
99
100 // Invoke main().
101
102 env->CallStaticVoidMethod(klass.get(), method, args.get());
103 if (env->ExceptionCheck()) {
104 return false;
105 } else {
106 return true;
107 }
108}
109
110// Parse arguments. Most of it just gets passed through to the VM.
111// The JNI spec defines a handful of standard arguments.
Carl Shapiro7b216702011-06-17 15:09:26 -0700112int main(int argc, char** argv) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700113 setvbuf(stdout, NULL, _IONBF, 0);
114
115 // Skip over argv[0].
116 argv++;
117 argc--;
118
119 // If we're adding any additional stuff, e.g. function hook specifiers,
120 // add them to the count here.
121 //
122 // We're over-allocating, because this includes the options to the VM
123 // plus the options to the program.
124 int option_count = argc;
125 scoped_array<JavaVMOption> options(new JavaVMOption[option_count]());
126
127 // Copy options over. Everything up to the name of the class starts
128 // with a '-' (the function hook stuff is strictly internal).
129 //
130 // [Do we need to catch & handle "-jar" here?]
131 bool need_extra = false;
132 int curr_opt, arg_idx;
133 for (curr_opt = arg_idx = 0; arg_idx < argc; arg_idx++) {
134 if (argv[arg_idx][0] != '-' && !need_extra) {
135 break;
136 }
137 options[curr_opt++].optionString = argv[arg_idx];
138
139 // Some options require an additional argument.
140 need_extra = false;
141 if (strcmp(argv[arg_idx], "-classpath") == 0 ||
142 strcmp(argv[arg_idx], "-cp") == 0) {
143 // others?
144 need_extra = true;
145 }
146 }
147
148 if (need_extra) {
149 fprintf(stderr, "VM requires value after last option flag\n");
150 return EXIT_FAILURE;
151 }
152
153 // Make sure they provided a class name. We do this after VM init
154 // so that things like "-Xrunjdwp:help" have the opportunity to emit
155 // a usage statement.
156 if (arg_idx == argc) {
157 fprintf(stderr, "Class name required\n");
158 return EXIT_FAILURE;
159 }
160
161 // insert additional internal options here
162
163 DCHECK_LE(curr_opt, option_count);
164
165 JavaVMInitArgs init_args;
Elliott Hughesf2682d52011-08-15 16:37:04 -0700166 init_args.version = JNI_VERSION_1_6;
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700167 init_args.options = options.get();
168 init_args.nOptions = curr_opt;
169 init_args.ignoreUnrecognized = JNI_FALSE;
170
171 BlockSigpipe();
172
173 // Start VM. The current thread becomes the main thread of the VM.
174 JavaVM* vm = NULL;
175 JNIEnv* env = NULL;
176 if (JNI_CreateJavaVM(&vm, &env, &init_args) != JNI_OK) {
177 fprintf(stderr, "VM init failed (check log file)\n");
178 return EXIT_FAILURE;
179 }
180
Elliott Hughesf2682d52011-08-15 16:37:04 -0700181 bool success = InvokeMain(env, argc - arg_idx, &argv[arg_idx]);
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700182
Elliott Hughesf2682d52011-08-15 16:37:04 -0700183 if (vm->DetachCurrentThread() != JNI_OK) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700184 fprintf(stderr, "Warning: unable to detach main thread\n");
185 success = false;
186 }
187
Elliott Hughesf2682d52011-08-15 16:37:04 -0700188 if (vm->DestroyJavaVM() != 0) {
Carl Shapiro2ed144c2011-07-26 16:52:08 -0700189 fprintf(stderr, "Warning: VM did not shut down cleanly\n");
190 success = false;
191 }
192
Elliott Hughesf2682d52011-08-15 16:37:04 -0700193 return success ? EXIT_SUCCESS : EXIT_FAILURE;
Carl Shapiro7b216702011-06-17 15:09:26 -0700194}