blob: 6fe358c7ee54ec9687380622460549581a29b74a [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>
15#include <android_runtime/AndroidRuntime.h>
16
17#include <stdio.h>
18#include <unistd.h>
19
20namespace android {
21
22void app_usage()
23{
24 fprintf(stderr,
25 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
26}
27
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028class AppRuntime : public AndroidRuntime
29{
30public:
31 AppRuntime()
32 : mParentDir(NULL)
33 , mClassName(NULL)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070034 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 , mArgC(0)
36 , mArgV(NULL)
37 {
38 }
39
40#if 0
41 // this appears to be unused
42 const char* getParentDir() const
43 {
44 return mParentDir;
45 }
46#endif
47
48 const char* getClassName() const
49 {
50 return mClassName;
51 }
52
Elliott Hughesd195e5a2011-04-13 15:39:37 -070053 virtual void onVmCreated(JNIEnv* env)
54 {
55 if (mClassName == NULL) {
56 return; // Zygote. Nothing to do here.
57 }
58
59 /*
60 * This is a little awkward because the JNI FindClass call uses the
61 * class loader associated with the native method we're executing in.
62 * If called in onStarted (from RuntimeInit.finishInit because we're
63 * launching "am", for example), FindClass would see that we're calling
64 * from a boot class' native method, and so wouldn't look for the class
65 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
66 * because the "am" classes are not boot classes.
67 *
68 * The easiest fix is to call FindClass here, early on before we start
69 * executing boot class Java code and thereby deny ourselves access to
70 * non-boot classes.
71 */
72 char* slashClassName = toSlashClassName(mClassName);
73 mClass = env->FindClass(slashClassName);
74 if (mClass == NULL) {
Steve Block3762c312012-01-06 19:20:56 +000075 ALOGE("ERROR: could not find class '%s'\n", mClassName);
Elliott Hughesd195e5a2011-04-13 15:39:37 -070076 }
77 free(slashClassName);
78
79 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
80 }
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 virtual void onStarted()
83 {
84 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010085 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070086 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070087
88 AndroidRuntime* ar = AndroidRuntime::getRuntime();
89 ar->callMain(mClassName, mClass, mArgC, mArgV);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
Jeff Brown10e89712011-07-08 18:52:57 -070091 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 }
93
94 virtual void onZygoteInit()
95 {
96 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010097 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070098 proc->startThreadPool();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100
101 virtual void onExit(int code)
102 {
103 if (mClassName == NULL) {
104 // if zygote
Jeff Brown10e89712011-07-08 18:52:57 -0700105 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
107
108 AndroidRuntime::onExit(code);
109 }
110
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 const char* mParentDir;
113 const char* mClassName;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700114 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 int mArgC;
116 const char* const* mArgV;
117};
118
119}
120
121using namespace android;
122
123/*
124 * sets argv0 to as much of newArgv0 as will fit
125 */
126static void setArgv0(const char *argv0, const char *newArgv0)
127{
128 strlcpy(const_cast<char *>(argv0), newArgv0, strlen(argv0));
129}
130
131int main(int argc, const char* const argv[])
132{
133 // These are global variables in ProcessState.cpp
134 mArgC = argc;
135 mArgV = argv;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 mArgLen = 0;
138 for (int i=0; i<argc; i++) {
139 mArgLen += strlen(argv[i]) + 1;
140 }
141 mArgLen--;
142
143 AppRuntime runtime;
Jeff Brownebed7d62011-05-16 17:08:42 -0700144 const char* argv0 = argv[0];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 // Process command line arguments
147 // ignore argv[0]
148 argc--;
149 argv++;
150
151 // Everything up to '--' or first non '-' arg goes to the vm
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 int i = runtime.addVmArguments(argc, argv);
154
Jeff Brownebed7d62011-05-16 17:08:42 -0700155 // Parse runtime arguments. Stop at first unrecognized option.
156 bool zygote = false;
157 bool startSystemServer = false;
158 bool application = false;
159 const char* parentDir = NULL;
160 const char* niceName = NULL;
161 const char* className = NULL;
162 while (i < argc) {
163 const char* arg = argv[i++];
164 if (!parentDir) {
165 parentDir = arg;
166 } else if (strcmp(arg, "--zygote") == 0) {
167 zygote = true;
168 niceName = "zygote";
169 } else if (strcmp(arg, "--start-system-server") == 0) {
170 startSystemServer = true;
171 } else if (strcmp(arg, "--application") == 0) {
172 application = true;
173 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
174 niceName = arg + 12;
175 } else {
176 className = arg;
177 break;
178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
180
Jeff Brownebed7d62011-05-16 17:08:42 -0700181 if (niceName && *niceName) {
182 setArgv0(argv0, niceName);
183 set_process_name(niceName);
184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185
Jeff Brownebed7d62011-05-16 17:08:42 -0700186 runtime.mParentDir = parentDir;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187
Jeff Brownebed7d62011-05-16 17:08:42 -0700188 if (zygote) {
189 runtime.start("com.android.internal.os.ZygoteInit",
190 startSystemServer ? "start-system-server" : "");
191 } else if (className) {
192 // Remainder of args get passed to startup class main()
193 runtime.mClassName = className;
194 runtime.mArgC = argc - i;
195 runtime.mArgV = argv + i;
196 runtime.start("com.android.internal.os.RuntimeInit",
197 application ? "application" : "tool");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
200 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700201 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 return 10;
203 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204}