blob: 8d2b739fc30113eb1d765d78c01f64efa723c72a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Main entry of app process.
Elliott Hughesd195e5a2011-04-13 15:39:37 -07003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004 * Starts the interpreted runtime, then starts up the application.
Elliott Hughesd195e5a2011-04-13 15:39:37 -07005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006 */
7
8#define LOG_TAG "appproc"
9
Mathias Agopian07952722009-05-19 19:08:10 -070010#include <binder/IPCThreadState.h>
11#include <binder/ProcessState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012#include <utils/Log.h>
13#include <cutils/process_name.h>
14#include <cutils/memory.h>
Jamie Gennis6ad04522013-04-15 18:53:24 -070015#include <cutils/trace.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016#include <android_runtime/AndroidRuntime.h>
17
Nick Kralevich1fe21bd2013-03-15 11:38:29 -070018#include <stdlib.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <stdio.h>
20#include <unistd.h>
21
22namespace android {
23
24void app_usage()
25{
26 fprintf(stderr,
27 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
28}
29
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030class AppRuntime : public AndroidRuntime
31{
32public:
33 AppRuntime()
34 : mParentDir(NULL)
35 , mClassName(NULL)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070036 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037 , mArgC(0)
38 , mArgV(NULL)
39 {
40 }
41
42#if 0
43 // this appears to be unused
44 const char* getParentDir() const
45 {
46 return mParentDir;
47 }
48#endif
49
50 const char* getClassName() const
51 {
52 return mClassName;
53 }
54
Elliott Hughesd195e5a2011-04-13 15:39:37 -070055 virtual void onVmCreated(JNIEnv* env)
56 {
57 if (mClassName == NULL) {
58 return; // Zygote. Nothing to do here.
59 }
60
61 /*
62 * This is a little awkward because the JNI FindClass call uses the
63 * class loader associated with the native method we're executing in.
64 * If called in onStarted (from RuntimeInit.finishInit because we're
65 * launching "am", for example), FindClass would see that we're calling
66 * from a boot class' native method, and so wouldn't look for the class
67 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
68 * because the "am" classes are not boot classes.
69 *
70 * The easiest fix is to call FindClass here, early on before we start
71 * executing boot class Java code and thereby deny ourselves access to
72 * non-boot classes.
73 */
74 char* slashClassName = toSlashClassName(mClassName);
75 mClass = env->FindClass(slashClassName);
76 if (mClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000077 ALOGE("ERROR: could not find class '%s'\n", mClassName);
Elliott Hughesd195e5a2011-04-13 15:39:37 -070078 }
79 free(slashClassName);
80
81 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
82 }
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 virtual void onStarted()
85 {
86 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010087 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070088 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070089
90 AndroidRuntime* ar = AndroidRuntime::getRuntime();
91 ar->callMain(mClassName, mClass, mArgC, mArgV);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Jeff Brown10e89712011-07-08 18:52:57 -070093 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
95
96 virtual void onZygoteInit()
97 {
Jamie Gennis6ad04522013-04-15 18:53:24 -070098 // Re-enable tracing now that we're no longer in Zygote.
99 atrace_set_tracing_enabled(true);
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +0100102 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -0700103 proc->startThreadPool();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 virtual void onExit(int code)
107 {
108 if (mClassName == NULL) {
109 // if zygote
Jeff Brown10e89712011-07-08 18:52:57 -0700110 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 }
112
113 AndroidRuntime::onExit(code);
114 }
115
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 const char* mParentDir;
118 const char* mClassName;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700119 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 int mArgC;
121 const char* const* mArgV;
122};
123
124}
125
126using namespace android;
127
128/*
129 * sets argv0 to as much of newArgv0 as will fit
130 */
131static void setArgv0(const char *argv0, const char *newArgv0)
132{
133 strlcpy(const_cast<char *>(argv0), newArgv0, strlen(argv0));
134}
135
Nick Kralevich8a0a9292013-03-14 13:23:52 -0700136int main(int argc, char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137{
138 // These are global variables in ProcessState.cpp
139 mArgC = argc;
140 mArgV = argv;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 mArgLen = 0;
143 for (int i=0; i<argc; i++) {
144 mArgLen += strlen(argv[i]) + 1;
145 }
146 mArgLen--;
147
148 AppRuntime runtime;
Jeff Brownebed7d62011-05-16 17:08:42 -0700149 const char* argv0 = argv[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
151 // Process command line arguments
152 // ignore argv[0]
153 argc--;
154 argv++;
155
156 // Everything up to '--' or first non '-' arg goes to the vm
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 int i = runtime.addVmArguments(argc, argv);
159
Jeff Brownebed7d62011-05-16 17:08:42 -0700160 // Parse runtime arguments. Stop at first unrecognized option.
161 bool zygote = false;
162 bool startSystemServer = false;
163 bool application = false;
164 const char* parentDir = NULL;
165 const char* niceName = NULL;
166 const char* className = NULL;
167 while (i < argc) {
168 const char* arg = argv[i++];
169 if (!parentDir) {
170 parentDir = arg;
171 } else if (strcmp(arg, "--zygote") == 0) {
172 zygote = true;
173 niceName = "zygote";
174 } else if (strcmp(arg, "--start-system-server") == 0) {
175 startSystemServer = true;
176 } else if (strcmp(arg, "--application") == 0) {
177 application = true;
178 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
179 niceName = arg + 12;
180 } else {
181 className = arg;
182 break;
183 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185
Jeff Brownebed7d62011-05-16 17:08:42 -0700186 if (niceName && *niceName) {
187 setArgv0(argv0, niceName);
188 set_process_name(niceName);
189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190
Jeff Brownebed7d62011-05-16 17:08:42 -0700191 runtime.mParentDir = parentDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
Jeff Brownebed7d62011-05-16 17:08:42 -0700193 if (zygote) {
194 runtime.start("com.android.internal.os.ZygoteInit",
195 startSystemServer ? "start-system-server" : "");
196 } else if (className) {
197 // Remainder of args get passed to startup class main()
198 runtime.mClassName = className;
199 runtime.mArgC = argc - i;
200 runtime.mArgV = argv + i;
201 runtime.start("com.android.internal.os.RuntimeInit",
202 application ? "application" : "tool");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
205 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700206 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 return 10;
208 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209}