blob: 752c2a8d7400e8d607c9986c4e2c136b78b96354 [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
Mark Salyzynfc737fb2015-04-01 07:36:23 -070010#include <stdio.h>
11#include <stdlib.h>
12#include <sys/prctl.h>
13#include <sys/stat.h>
14#include <unistd.h>
15
Mathias Agopian07952722009-05-19 19:08:10 -070016#include <binder/IPCThreadState.h>
17#include <binder/ProcessState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <cutils/memory.h>
Narayan Kamathc41638c2014-04-07 13:56:15 +010020#include <cutils/properties.h>
Jamie Gennis6ad04522013-04-15 18:53:24 -070021#include <cutils/trace.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <android_runtime/AndroidRuntime.h>
Narayan Kamathd1e127e2014-04-25 11:43:22 +010023#include <private/android_filesystem_config.h> // for AID_SYSTEM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025namespace android {
26
Andreas Gampecfedceb2014-09-30 21:48:18 -070027static void app_usage()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028{
29 fprintf(stderr,
30 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
31}
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033class AppRuntime : public AndroidRuntime
34{
35public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000036 AppRuntime(char* argBlockStart, const size_t argBlockLength)
37 : AndroidRuntime(argBlockStart, argBlockLength)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070038 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 {
40 }
41
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010042 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]));
46 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 }
48
Elliott Hughesd195e5a2011-04-13 15:39:37 -070049 virtual void onVmCreated(JNIEnv* env)
50 {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010051 if (mClassName.isEmpty()) {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070052 return; // Zygote. Nothing to do here.
53 }
54
55 /*
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.
63 *
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
66 * non-boot classes.
67 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010068 char* slashClassName = toSlashClassName(mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070069 mClass = env->FindClass(slashClassName);
70 if (mClass == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010071 ALOGE("ERROR: could not find class '%s'\n", mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070072 }
73 free(slashClassName);
74
75 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
76 }
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 virtual void onStarted()
79 {
80 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010081 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070082 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070083
84 AndroidRuntime* ar = AndroidRuntime::getRuntime();
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010085 ar->callMain(mClassName, mClass, mArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
Jeff Brown10e89712011-07-08 18:52:57 -070087 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 }
89
90 virtual void onZygoteInit()
91 {
92 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010093 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070094 proc->startThreadPool();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
96
97 virtual void onExit(int code)
98 {
Narayan Kamath90c75cf2014-04-12 12:25:50 +010099 if (mClassName.isEmpty()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 // if zygote
Jeff Brown10e89712011-07-08 18:52:57 -0700101 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 }
103
104 AndroidRuntime::onExit(code);
105 }
106
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700107
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100108 String8 mClassName;
109 Vector<String8> mArgs;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700110 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111};
112
113}
114
115using namespace android;
116
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000117static size_t computeArgBlockSize(int argc, char* const argv[]) {
118 // TODO: This assumes that all arguments are allocated in
119 // contiguous memory. There isn't any documented guarantee
120 // that this is the case, but this is how the kernel does it
121 // (see fs/exec.c).
122 //
123 // Also note that this is a constant for "normal" android apps.
124 // Since they're forked from zygote, the size of their command line
125 // is the size of the zygote command line.
126 //
127 // We change the process name of the process by over-writing
128 // the start of the argument block (argv[0]) with the new name of
129 // the process, so we'd mysteriously start getting truncated process
130 // names if the zygote command line decreases in size.
131 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]);
132 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700133 end += strlen(argv[argc - 1]) + 1;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000134 return (end - start);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135}
136
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100137static void maybeCreateDalvikCache() {
138#if defined(__aarch64__)
139 static const char kInstructionSet[] = "arm64";
140#elif defined(__x86_64__)
141 static const char kInstructionSet[] = "x86_64";
142#elif defined(__arm__)
143 static const char kInstructionSet[] = "arm";
Narayan Kamath6eb1b262014-04-30 16:45:07 +0100144#elif defined(__i386__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100145 static const char kInstructionSet[] = "x86";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800146#elif defined (__mips__) && !defined(__LP64__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100147 static const char kInstructionSet[] = "mips";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800148#elif defined (__mips__) && defined(__LP64__)
149 static const char kInstructionSet[] = "mips64";
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100150#else
151#error "Unknown instruction set"
152#endif
153 const char* androidRoot = getenv("ANDROID_DATA");
154 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset");
155
156 char dalvikCacheDir[PATH_MAX];
157 const int numChars = snprintf(dalvikCacheDir, PATH_MAX,
158 "%s/dalvik-cache/%s", androidRoot, kInstructionSet);
159 LOG_ALWAYS_FATAL_IF((numChars >= PATH_MAX || numChars < 0),
160 "Error constructing dalvik cache : %s", strerror(errno));
161
Alex Light55471dc2014-08-27 15:39:17 -0700162 int result = mkdir(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100163 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST),
164 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno));
165
166 // We always perform these steps because the directory might
167 // already exist, with wider permissions and a different owner
168 // than we'd like.
Alex Light55471dc2014-08-27 15:39:17 -0700169 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100170 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
171
Alex Light55471dc2014-08-27 15:39:17 -0700172 result = chmod(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100173 LOG_ALWAYS_FATAL_IF((result < 0),
174 "Error changing dalvik-cache permissions : %s", strerror(errno));
175}
176
Narayan Kamathc41638c2014-04-07 13:56:15 +0100177#if defined(__LP64__)
178static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100179static const char ZYGOTE_NICE_NAME[] = "zygote64";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100180#else
181static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100182static const char ZYGOTE_NICE_NAME[] = "zygote";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100183#endif
184
Nick Kralevich8a0a9292013-03-14 13:23:52 -0700185int main(int argc, char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186{
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700187 if (!LOG_NDEBUG) {
188 String8 argv_String;
189 for (int i = 0; i < argc; ++i) {
190 argv_String.append("\"");
191 argv_String.append(argv[i]);
192 argv_String.append("\" ");
193 }
194 ALOGV("app_process main with argv: %s", argv_String.string());
195 }
196
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000197 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 // Process command line arguments
199 // ignore argv[0]
200 argc--;
201 argv++;
202
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100203 // Everything up to '--' or first non '-' arg goes to the vm.
204 //
205 // The first argument after the VM args is the "parent dir", which
206 // is currently unused.
207 //
208 // After the parent dir, we expect one or more the following internal
209 // arguments :
210 //
211 // --zygote : Start in zygote mode
212 // --start-system-server : Start the system server.
213 // --application : Start in application (stand alone, non zygote) mode.
214 // --nice-name : The nice name for this process.
215 //
216 // For non zygote starts, these arguments will be followed by
217 // the main class name. All remaining arguments are passed to
218 // the main method of this class.
219 //
220 // For zygote starts, all remaining arguments are passed to the zygote.
221 // main function.
Jeff Brown00c0cd42014-09-10 16:48:46 -0700222 //
223 // Note that we must copy argument string values since we will rewrite the
224 // entire argument block when we apply the nice name to argv0.
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700225 //
226 // As an exception to the above rule, anything in "spaced commands"
227 // goes to the vm even though it has a space in it.
228 const char* spaced_commands[] = { "-cp", "-classpath" };
229 // Allow "spaced commands" to be succeeded by exactly 1 argument (regardless of -s).
230 bool known_command = false;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100231
Jeff Brown00c0cd42014-09-10 16:48:46 -0700232 int i;
233 for (i = 0; i < argc; i++) {
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700234 if (known_command == true) {
235 runtime.addOption(strdup(argv[i]));
George Burgess IVc29844d2017-07-11 17:18:26 -0700236 // The static analyzer gets upset that we don't ever free the above
237 // string. Since the allocation is from main, leaking it doesn't seem
238 // problematic. NOLINTNEXTLINE
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700239 ALOGV("app_process main add known option '%s'", argv[i]);
240 known_command = false;
241 continue;
242 }
243
244 for (int j = 0;
245 j < static_cast<int>(sizeof(spaced_commands) / sizeof(spaced_commands[0]));
246 ++j) {
247 if (strcmp(argv[i], spaced_commands[j]) == 0) {
248 known_command = true;
249 ALOGV("app_process main found known command '%s'", argv[i]);
250 }
251 }
252
Jeff Brown00c0cd42014-09-10 16:48:46 -0700253 if (argv[i][0] != '-') {
254 break;
255 }
256 if (argv[i][1] == '-' && argv[i][2] == 0) {
257 ++i; // Skip --.
258 break;
259 }
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700260
Jeff Brown00c0cd42014-09-10 16:48:46 -0700261 runtime.addOption(strdup(argv[i]));
George Burgess IVc29844d2017-07-11 17:18:26 -0700262 // The static analyzer gets upset that we don't ever free the above
263 // string. Since the allocation is from main, leaking it doesn't seem
264 // problematic. NOLINTNEXTLINE
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700265 ALOGV("app_process main add option '%s'", argv[i]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267
Jeff Brownebed7d62011-05-16 17:08:42 -0700268 // Parse runtime arguments. Stop at first unrecognized option.
269 bool zygote = false;
270 bool startSystemServer = false;
271 bool application = false;
Jeff Brown00c0cd42014-09-10 16:48:46 -0700272 String8 niceName;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100273 String8 className;
274
275 ++i; // Skip unused "parent dir" argument.
Jeff Brownebed7d62011-05-16 17:08:42 -0700276 while (i < argc) {
277 const char* arg = argv[i++];
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100278 if (strcmp(arg, "--zygote") == 0) {
Jeff Brownebed7d62011-05-16 17:08:42 -0700279 zygote = true;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100280 niceName = ZYGOTE_NICE_NAME;
Jeff Brownebed7d62011-05-16 17:08:42 -0700281 } else if (strcmp(arg, "--start-system-server") == 0) {
282 startSystemServer = true;
283 } else if (strcmp(arg, "--application") == 0) {
284 application = true;
285 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700286 niceName.setTo(arg + 12);
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100287 } else if (strncmp(arg, "--", 2) != 0) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100288 className.setTo(arg);
Jeff Brownebed7d62011-05-16 17:08:42 -0700289 break;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100290 } else {
291 --i;
292 break;
Jeff Brownebed7d62011-05-16 17:08:42 -0700293 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100296 Vector<String8> args;
297 if (!className.isEmpty()) {
298 // We're not in zygote mode, the only argument we need to pass
299 // to RuntimeInit is the application argument.
300 //
301 // The Remainder of args get passed to startup class main(). Make
302 // copies of them before we overwrite them with the process name.
303 args.add(application ? String8("application") : String8("tool"));
304 runtime.setClassNameAndArgs(className, argc - i, argv + i);
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700305
306 if (!LOG_NDEBUG) {
307 String8 restOfArgs;
308 char* const* argv_new = argv + i;
309 int argc_new = argc - i;
310 for (int k = 0; k < argc_new; ++k) {
311 restOfArgs.append("\"");
312 restOfArgs.append(argv_new[k]);
313 restOfArgs.append("\" ");
314 }
315 ALOGV("Class name = %s, args = %s", className.string(), restOfArgs.string());
316 }
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100317 } else {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100318 // We're in zygote mode.
319 maybeCreateDalvikCache();
320
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100321 if (startSystemServer) {
322 args.add(String8("start-system-server"));
323 }
324
Narayan Kamathc41638c2014-04-07 13:56:15 +0100325 char prop[PROP_VALUE_MAX];
326 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
Elliott Hughes6bd762282014-04-23 16:54:33 -0700327 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
Narayan Kamathc41638c2014-04-07 13:56:15 +0100328 ABI_LIST_PROPERTY);
329 return 11;
330 }
331
332 String8 abiFlag("--abi-list=");
333 abiFlag.append(prop);
334 args.add(abiFlag);
335
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100336 // In zygote mode, pass all remaining arguments to the zygote
337 // main() method.
338 for (; i < argc; ++i) {
339 args.add(String8(argv[i]));
340 }
341 }
342
Jeff Brown00c0cd42014-09-10 16:48:46 -0700343 if (!niceName.isEmpty()) {
Dmitriy Filchenkof5b6e552016-07-18 16:00:35 -0700344 runtime.setArgv0(niceName.string(), true /* setProcName */);
Jeff Brownebed7d62011-05-16 17:08:42 -0700345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
Jeff Brownebed7d62011-05-16 17:08:42 -0700347 if (zygote) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200348 runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
Jeff Brownebed7d62011-05-16 17:08:42 -0700349 } else if (className) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200350 runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
353 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700354 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356}