blob: 9b811d1c95357891587111999ce5137a194c73ed [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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013#include <cutils/memory.h>
Narayan Kamathc41638c2014-04-07 13:56:15 +010014#include <cutils/process_name.h>
15#include <cutils/properties.h>
Jamie Gennis6ad04522013-04-15 18:53:24 -070016#include <cutils/trace.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <android_runtime/AndroidRuntime.h>
Narayan Kamathd1e127e2014-04-25 11:43:22 +010018#include <private/android_filesystem_config.h> // for AID_SYSTEM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
Nick Kralevich1fe21bd2013-03-15 11:38:29 -070020#include <stdlib.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include <stdio.h>
22#include <unistd.h>
23
24namespace android {
25
26void app_usage()
27{
28 fprintf(stderr,
29 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
30}
31
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032class AppRuntime : public AndroidRuntime
33{
34public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000035 AppRuntime(char* argBlockStart, const size_t argBlockLength)
36 : AndroidRuntime(argBlockStart, argBlockLength)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070037 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 {
39 }
40
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010041 void setClassNameAndArgs(const String8& className, int argc, char * const *argv) {
42 mClassName = className;
43 for (int i = 0; i < argc; ++i) {
44 mArgs.add(String8(argv[i]));
45 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 }
47
Elliott Hughesd195e5a2011-04-13 15:39:37 -070048 virtual void onVmCreated(JNIEnv* env)
49 {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010050 if (mClassName.isEmpty()) {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070051 return; // Zygote. Nothing to do here.
52 }
53
54 /*
55 * This is a little awkward because the JNI FindClass call uses the
56 * class loader associated with the native method we're executing in.
57 * If called in onStarted (from RuntimeInit.finishInit because we're
58 * launching "am", for example), FindClass would see that we're calling
59 * from a boot class' native method, and so wouldn't look for the class
60 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
61 * because the "am" classes are not boot classes.
62 *
63 * The easiest fix is to call FindClass here, early on before we start
64 * executing boot class Java code and thereby deny ourselves access to
65 * non-boot classes.
66 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010067 char* slashClassName = toSlashClassName(mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070068 mClass = env->FindClass(slashClassName);
69 if (mClass == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010070 ALOGE("ERROR: could not find class '%s'\n", mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070071 }
72 free(slashClassName);
73
74 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
75 }
76
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 virtual void onStarted()
78 {
79 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010080 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070081 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070082
83 AndroidRuntime* ar = AndroidRuntime::getRuntime();
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010084 ar->callMain(mClassName, mClass, mArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
Jeff Brown10e89712011-07-08 18:52:57 -070086 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 }
88
89 virtual void onZygoteInit()
90 {
Jamie Gennis6ad04522013-04-15 18:53:24 -070091 // Re-enable tracing now that we're no longer in Zygote.
92 atrace_set_tracing_enabled(true);
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010095 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070096 proc->startThreadPool();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 }
98
99 virtual void onExit(int code)
100 {
Narayan Kamath90c75cf2014-04-12 12:25:50 +0100101 if (mClassName.isEmpty()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 // if zygote
Jeff Brown10e89712011-07-08 18:52:57 -0700103 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 }
105
106 AndroidRuntime::onExit(code);
107 }
108
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700109
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100110 String8 mClassName;
111 Vector<String8> mArgs;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700112 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113};
114
115}
116
117using namespace android;
118
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000119static size_t computeArgBlockSize(int argc, char* const argv[]) {
120 // TODO: This assumes that all arguments are allocated in
121 // contiguous memory. There isn't any documented guarantee
122 // that this is the case, but this is how the kernel does it
123 // (see fs/exec.c).
124 //
125 // Also note that this is a constant for "normal" android apps.
126 // Since they're forked from zygote, the size of their command line
127 // is the size of the zygote command line.
128 //
129 // We change the process name of the process by over-writing
130 // the start of the argument block (argv[0]) with the new name of
131 // the process, so we'd mysteriously start getting truncated process
132 // names if the zygote command line decreases in size.
133 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]);
134 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]);
135 end += strlen(argv[argc - 1]);
136
137 return (end - start);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100140static void maybeCreateDalvikCache() {
141#if defined(__aarch64__)
142 static const char kInstructionSet[] = "arm64";
143#elif defined(__x86_64__)
144 static const char kInstructionSet[] = "x86_64";
145#elif defined(__arm__)
146 static const char kInstructionSet[] = "arm";
147#elif defined(__x86__)
148 static const char kInstructionSet[] = "x86";
149#elif defined (__mips__)
150 static const char kInstructionSet[] = "mips";
151#else
152#error "Unknown instruction set"
153#endif
154 const char* androidRoot = getenv("ANDROID_DATA");
155 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset");
156
157 char dalvikCacheDir[PATH_MAX];
158 const int numChars = snprintf(dalvikCacheDir, PATH_MAX,
159 "%s/dalvik-cache/%s", androidRoot, kInstructionSet);
160 LOG_ALWAYS_FATAL_IF((numChars >= PATH_MAX || numChars < 0),
161 "Error constructing dalvik cache : %s", strerror(errno));
162
163 int result = mkdir(dalvikCacheDir, 0771);
164 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST),
165 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno));
166
167 // We always perform these steps because the directory might
168 // already exist, with wider permissions and a different owner
169 // than we'd like.
170 result = chown(dalvikCacheDir, AID_SYSTEM, AID_SYSTEM);
171 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
172
173 result = chmod(dalvikCacheDir, 0771);
174 LOG_ALWAYS_FATAL_IF((result < 0),
175 "Error changing dalvik-cache permissions : %s", strerror(errno));
176}
177
Narayan Kamathc41638c2014-04-07 13:56:15 +0100178#if defined(__LP64__)
179static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100180static const char ZYGOTE_NICE_NAME[] = "zygote64";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100181#else
182static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100183static const char ZYGOTE_NICE_NAME[] = "zygote";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100184#endif
185
Nick Kralevich8a0a9292013-03-14 13:23:52 -0700186int main(int argc, char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187{
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000188 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 // Process command line arguments
190 // ignore argv[0]
191 argc--;
192 argv++;
193
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100194 // Everything up to '--' or first non '-' arg goes to the vm.
195 //
196 // The first argument after the VM args is the "parent dir", which
197 // is currently unused.
198 //
199 // After the parent dir, we expect one or more the following internal
200 // arguments :
201 //
202 // --zygote : Start in zygote mode
203 // --start-system-server : Start the system server.
204 // --application : Start in application (stand alone, non zygote) mode.
205 // --nice-name : The nice name for this process.
206 //
207 // For non zygote starts, these arguments will be followed by
208 // the main class name. All remaining arguments are passed to
209 // the main method of this class.
210 //
211 // For zygote starts, all remaining arguments are passed to the zygote.
212 // main function.
213
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 int i = runtime.addVmArguments(argc, argv);
216
Jeff Brownebed7d62011-05-16 17:08:42 -0700217 // Parse runtime arguments. Stop at first unrecognized option.
218 bool zygote = false;
219 bool startSystemServer = false;
220 bool application = false;
Jeff Brownebed7d62011-05-16 17:08:42 -0700221 const char* niceName = NULL;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100222 String8 className;
223
224 ++i; // Skip unused "parent dir" argument.
Jeff Brownebed7d62011-05-16 17:08:42 -0700225 while (i < argc) {
226 const char* arg = argv[i++];
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100227 if (strcmp(arg, "--zygote") == 0) {
Jeff Brownebed7d62011-05-16 17:08:42 -0700228 zygote = true;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100229 niceName = ZYGOTE_NICE_NAME;
Jeff Brownebed7d62011-05-16 17:08:42 -0700230 } else if (strcmp(arg, "--start-system-server") == 0) {
231 startSystemServer = true;
232 } else if (strcmp(arg, "--application") == 0) {
233 application = true;
234 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
235 niceName = arg + 12;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100236 } else if (strncmp(arg, "--", 2) != 0) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100237 className.setTo(arg);
Jeff Brownebed7d62011-05-16 17:08:42 -0700238 break;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100239 } else {
240 --i;
241 break;
Jeff Brownebed7d62011-05-16 17:08:42 -0700242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 }
244
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100245 Vector<String8> args;
246 if (!className.isEmpty()) {
247 // We're not in zygote mode, the only argument we need to pass
248 // to RuntimeInit is the application argument.
249 //
250 // The Remainder of args get passed to startup class main(). Make
251 // copies of them before we overwrite them with the process name.
252 args.add(application ? String8("application") : String8("tool"));
253 runtime.setClassNameAndArgs(className, argc - i, argv + i);
254 } else {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100255 // We're in zygote mode.
256 maybeCreateDalvikCache();
257
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100258 if (startSystemServer) {
259 args.add(String8("start-system-server"));
260 }
261
Narayan Kamathc41638c2014-04-07 13:56:15 +0100262 char prop[PROP_VALUE_MAX];
263 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
Elliott Hughes6bd762282014-04-23 16:54:33 -0700264 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
Narayan Kamathc41638c2014-04-07 13:56:15 +0100265 ABI_LIST_PROPERTY);
266 return 11;
267 }
268
269 String8 abiFlag("--abi-list=");
270 abiFlag.append(prop);
271 args.add(abiFlag);
272
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100273 // In zygote mode, pass all remaining arguments to the zygote
274 // main() method.
275 for (; i < argc; ++i) {
276 args.add(String8(argv[i]));
277 }
278 }
279
Jeff Brownebed7d62011-05-16 17:08:42 -0700280 if (niceName && *niceName) {
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000281 runtime.setArgv0(niceName);
Jeff Brownebed7d62011-05-16 17:08:42 -0700282 set_process_name(niceName);
283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284
Jeff Brownebed7d62011-05-16 17:08:42 -0700285 if (zygote) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100286 runtime.start("com.android.internal.os.ZygoteInit", args);
Jeff Brownebed7d62011-05-16 17:08:42 -0700287 } else if (className) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100288 runtime.start("com.android.internal.os.RuntimeInit", args);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
291 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700292 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 return 10;
294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295}