2 * Main entry of app process.
4 * Starts the interpreted runtime, then starts up the application.
8 #define LOG_TAG "appproc"
12 #include <sys/prctl.h>
16 #include <binder/IPCThreadState.h>
17 #include <hwbinder/IPCThreadState.h>
18 #include <utils/Log.h>
19 #include <cutils/memory.h>
20 #include <cutils/properties.h>
21 #include <cutils/trace.h>
22 #include <android_runtime/AndroidRuntime.h>
23 #include <private/android_filesystem_config.h> // for AID_SYSTEM
27 static void app_usage()
30 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
33 class AppRuntime : public AndroidRuntime
36 AppRuntime(char* argBlockStart, const size_t argBlockLength)
37 : AndroidRuntime(argBlockStart, argBlockLength)
42 void setClassNameAndArgs(const String8& className, int argc, char * const *argv) {
43 mClassName = className;
44 for (int i = 0; i < argc; ++i) {
45 mArgs.add(String8(argv[i]));
49 virtual void onVmCreated(JNIEnv* env)
51 if (mClassName.isEmpty()) {
52 return; // Zygote. Nothing to do here.
56 * This is a little awkward because the JNI FindClass call uses the
57 * class loader associated with the native method we're executing in.
58 * If called in onStarted (from RuntimeInit.finishInit because we're
59 * launching "am", for example), FindClass would see that we're calling
60 * from a boot class' native method, and so wouldn't look for the class
61 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
62 * because the "am" classes are not boot classes.
64 * The easiest fix is to call FindClass here, early on before we start
65 * executing boot class Java code and thereby deny ourselves access to
68 char* slashClassName = toSlashClassName(mClassName.string());
69 mClass = env->FindClass(slashClassName);
71 ALOGE("ERROR: could not find class '%s'\n", mClassName.string());
75 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
78 virtual void onStarted()
80 sp<ProcessState> proc = ProcessState::self();
81 ALOGV("App process: starting thread pool.\n");
82 proc->startThreadPool();
84 AndroidRuntime* ar = AndroidRuntime::getRuntime();
85 ar->callMain(mClassName, mClass, mArgs);
87 IPCThreadState::self()->stopProcess();
88 hardware::IPCThreadState::self()->stopProcess();
91 virtual void onZygoteInit()
93 sp<ProcessState> proc = ProcessState::self();
94 ALOGV("App process: starting thread pool.\n");
95 proc->startThreadPool();
98 virtual void onExit(int code)
100 if (mClassName.isEmpty()) {
102 IPCThreadState::self()->stopProcess();
103 hardware::IPCThreadState::self()->stopProcess();
106 AndroidRuntime::onExit(code);
111 Vector<String8> mArgs;
117 using namespace android;
119 static 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
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.
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]) + 1;
136 return (end - start);
139 static void maybeCreateDalvikCache() {
140 #if defined(__aarch64__)
141 static const char kInstructionSet[] = "arm64";
142 #elif defined(__x86_64__)
143 static const char kInstructionSet[] = "x86_64";
144 #elif defined(__arm__)
145 static const char kInstructionSet[] = "arm";
146 #elif defined(__i386__)
147 static const char kInstructionSet[] = "x86";
148 #elif defined (__mips__) && !defined(__LP64__)
149 static const char kInstructionSet[] = "mips";
150 #elif defined (__mips__) && defined(__LP64__)
151 static const char kInstructionSet[] = "mips64";
153 #error "Unknown instruction set"
155 const char* androidRoot = getenv("ANDROID_DATA");
156 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset");
158 char dalvikCacheDir[PATH_MAX];
159 const int numChars = snprintf(dalvikCacheDir, PATH_MAX,
160 "%s/dalvik-cache/%s", androidRoot, kInstructionSet);
161 LOG_ALWAYS_FATAL_IF((numChars >= PATH_MAX || numChars < 0),
162 "Error constructing dalvik cache : %s", strerror(errno));
164 int result = mkdir(dalvikCacheDir, 0711);
165 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST),
166 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno));
168 // We always perform these steps because the directory might
169 // already exist, with wider permissions and a different owner
171 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT);
172 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
174 result = chmod(dalvikCacheDir, 0711);
175 LOG_ALWAYS_FATAL_IF((result < 0),
176 "Error changing dalvik-cache permissions : %s", strerror(errno));
179 #if defined(__LP64__)
180 static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
181 static const char ZYGOTE_NICE_NAME[] = "zygote64";
183 static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
184 static const char ZYGOTE_NICE_NAME[] = "zygote";
187 int main(int argc, char* const argv[])
191 for (int i = 0; i < argc; ++i) {
192 argv_String.append("\"");
193 argv_String.append(argv[i]);
194 argv_String.append("\" ");
196 ALOGV("app_process main with argv: %s", argv_String.string());
199 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
200 // Process command line arguments
205 // Everything up to '--' or first non '-' arg goes to the vm.
207 // The first argument after the VM args is the "parent dir", which
208 // is currently unused.
210 // After the parent dir, we expect one or more the following internal
213 // --zygote : Start in zygote mode
214 // --start-system-server : Start the system server.
215 // --application : Start in application (stand alone, non zygote) mode.
216 // --nice-name : The nice name for this process.
218 // For non zygote starts, these arguments will be followed by
219 // the main class name. All remaining arguments are passed to
220 // the main method of this class.
222 // For zygote starts, all remaining arguments are passed to the zygote.
225 // Note that we must copy argument string values since we will rewrite the
226 // entire argument block when we apply the nice name to argv0.
228 // As an exception to the above rule, anything in "spaced commands"
229 // goes to the vm even though it has a space in it.
230 const char* spaced_commands[] = { "-cp", "-classpath" };
231 // Allow "spaced commands" to be succeeded by exactly 1 argument (regardless of -s).
232 bool known_command = false;
235 for (i = 0; i < argc; i++) {
236 if (known_command == true) {
237 runtime.addOption(strdup(argv[i]));
238 // The static analyzer gets upset that we don't ever free the above
239 // string. Since the allocation is from main, leaking it doesn't seem
240 // problematic. NOLINTNEXTLINE
241 ALOGV("app_process main add known option '%s'", argv[i]);
242 known_command = false;
247 j < static_cast<int>(sizeof(spaced_commands) / sizeof(spaced_commands[0]));
249 if (strcmp(argv[i], spaced_commands[j]) == 0) {
250 known_command = true;
251 ALOGV("app_process main found known command '%s'", argv[i]);
255 if (argv[i][0] != '-') {
258 if (argv[i][1] == '-' && argv[i][2] == 0) {
263 runtime.addOption(strdup(argv[i]));
264 // The static analyzer gets upset that we don't ever free the above
265 // string. Since the allocation is from main, leaking it doesn't seem
266 // problematic. NOLINTNEXTLINE
267 ALOGV("app_process main add option '%s'", argv[i]);
270 // Parse runtime arguments. Stop at first unrecognized option.
272 bool startSystemServer = false;
273 bool application = false;
277 ++i; // Skip unused "parent dir" argument.
279 const char* arg = argv[i++];
280 if (strcmp(arg, "--zygote") == 0) {
282 niceName = ZYGOTE_NICE_NAME;
283 } else if (strcmp(arg, "--start-system-server") == 0) {
284 startSystemServer = true;
285 } else if (strcmp(arg, "--application") == 0) {
287 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
288 niceName.setTo(arg + 12);
289 } else if (strncmp(arg, "--", 2) != 0) {
290 className.setTo(arg);
298 Vector<String8> args;
299 if (!className.isEmpty()) {
300 // We're not in zygote mode, the only argument we need to pass
301 // to RuntimeInit is the application argument.
303 // The Remainder of args get passed to startup class main(). Make
304 // copies of them before we overwrite them with the process name.
305 args.add(application ? String8("application") : String8("tool"));
306 runtime.setClassNameAndArgs(className, argc - i, argv + i);
310 char* const* argv_new = argv + i;
311 int argc_new = argc - i;
312 for (int k = 0; k < argc_new; ++k) {
313 restOfArgs.append("\"");
314 restOfArgs.append(argv_new[k]);
315 restOfArgs.append("\" ");
317 ALOGV("Class name = %s, args = %s", className.string(), restOfArgs.string());
320 // We're in zygote mode.
321 maybeCreateDalvikCache();
323 if (startSystemServer) {
324 args.add(String8("start-system-server"));
327 char prop[PROP_VALUE_MAX];
328 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
329 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
334 String8 abiFlag("--abi-list=");
335 abiFlag.append(prop);
338 // In zygote mode, pass all remaining arguments to the zygote
340 for (; i < argc; ++i) {
341 args.add(String8(argv[i]));
345 if (!niceName.isEmpty()) {
346 runtime.setArgv0(niceName.string(), true /* setProcName */);
350 runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
351 } else if (className) {
352 runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
354 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
356 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");