blob: 12083b6fe20bb8d093b31e167193756472aae4f9 [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
Elliott Hughesa1194cb2018-03-23 11:27:04 -070016#include <android-base/macros.h>
Mathias Agopian07952722009-05-19 19:08:10 -070017#include <binder/IPCThreadState.h>
Martijn Coenenb69ffdb2017-04-19 16:40:49 -070018#include <hwbinder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <cutils/memory.h>
Narayan Kamathc41638c2014-04-07 13:56:15 +010021#include <cutils/properties.h>
Jamie Gennis6ad04522013-04-15 18:53:24 -070022#include <cutils/trace.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <android_runtime/AndroidRuntime.h>
Narayan Kamathd1e127e2014-04-25 11:43:22 +010024#include <private/android_filesystem_config.h> // for AID_SYSTEM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026namespace android {
27
Andreas Gampecfedceb2014-09-30 21:48:18 -070028static void app_usage()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029{
30 fprintf(stderr,
31 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
32}
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034class AppRuntime : public AndroidRuntime
35{
36public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000037 AppRuntime(char* argBlockStart, const size_t argBlockLength)
38 : AndroidRuntime(argBlockStart, argBlockLength)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070039 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 {
41 }
42
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010043 void setClassNameAndArgs(const String8& className, int argc, char * const *argv) {
44 mClassName = className;
45 for (int i = 0; i < argc; ++i) {
46 mArgs.add(String8(argv[i]));
47 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 }
49
Elliott Hughesd195e5a2011-04-13 15:39:37 -070050 virtual void onVmCreated(JNIEnv* env)
51 {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010052 if (mClassName.isEmpty()) {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070053 return; // Zygote. Nothing to do here.
54 }
55
56 /*
57 * This is a little awkward because the JNI FindClass call uses the
58 * class loader associated with the native method we're executing in.
59 * If called in onStarted (from RuntimeInit.finishInit because we're
60 * launching "am", for example), FindClass would see that we're calling
61 * from a boot class' native method, and so wouldn't look for the class
62 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
63 * because the "am" classes are not boot classes.
64 *
65 * The easiest fix is to call FindClass here, early on before we start
66 * executing boot class Java code and thereby deny ourselves access to
67 * non-boot classes.
68 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010069 char* slashClassName = toSlashClassName(mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070070 mClass = env->FindClass(slashClassName);
71 if (mClass == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010072 ALOGE("ERROR: could not find class '%s'\n", mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070073 }
74 free(slashClassName);
75
76 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
77 }
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 virtual void onStarted()
80 {
81 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010082 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070083 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070084
85 AndroidRuntime* ar = AndroidRuntime::getRuntime();
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010086 ar->callMain(mClassName, mClass, mArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087
Jeff Brown10e89712011-07-08 18:52:57 -070088 IPCThreadState::self()->stopProcess();
Martijn Coenenb69ffdb2017-04-19 16:40:49 -070089 hardware::IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91
92 virtual void onZygoteInit()
93 {
94 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();
Martijn Coenenb69ffdb2017-04-19 16:40:49 -0700104 hardware::IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
106
107 AndroidRuntime::onExit(code);
108 }
109
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700110
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100111 String8 mClassName;
112 Vector<String8> mArgs;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700113 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114};
115
116}
117
118using namespace android;
119
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000120static size_t computeArgBlockSize(int argc, char* const argv[]) {
121 // TODO: This assumes that all arguments are allocated in
122 // contiguous memory. There isn't any documented guarantee
123 // that this is the case, but this is how the kernel does it
124 // (see fs/exec.c).
125 //
126 // Also note that this is a constant for "normal" android apps.
127 // Since they're forked from zygote, the size of their command line
128 // is the size of the zygote command line.
129 //
130 // We change the process name of the process by over-writing
131 // the start of the argument block (argv[0]) with the new name of
132 // the process, so we'd mysteriously start getting truncated process
133 // names if the zygote command line decreases in size.
134 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]);
135 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700136 end += strlen(argv[argc - 1]) + 1;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000137 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() {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100141 const char* androidRoot = getenv("ANDROID_DATA");
142 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset");
143
144 char dalvikCacheDir[PATH_MAX];
145 const int numChars = snprintf(dalvikCacheDir, PATH_MAX,
Elliott Hughesa1194cb2018-03-23 11:27:04 -0700146 "%s/dalvik-cache/" ABI_STRING, androidRoot);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100147 LOG_ALWAYS_FATAL_IF((numChars >= PATH_MAX || numChars < 0),
148 "Error constructing dalvik cache : %s", strerror(errno));
149
Alex Light55471dc2014-08-27 15:39:17 -0700150 int result = mkdir(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100151 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST),
152 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno));
153
154 // We always perform these steps because the directory might
155 // already exist, with wider permissions and a different owner
156 // than we'd like.
Alex Light55471dc2014-08-27 15:39:17 -0700157 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100158 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
159
Alex Light55471dc2014-08-27 15:39:17 -0700160 result = chmod(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100161 LOG_ALWAYS_FATAL_IF((result < 0),
162 "Error changing dalvik-cache permissions : %s", strerror(errno));
163}
164
Narayan Kamathc41638c2014-04-07 13:56:15 +0100165#if defined(__LP64__)
166static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100167static const char ZYGOTE_NICE_NAME[] = "zygote64";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100168#else
169static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100170static const char ZYGOTE_NICE_NAME[] = "zygote";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100171#endif
172
Nick Kralevich8a0a9292013-03-14 13:23:52 -0700173int main(int argc, char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174{
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700175 if (!LOG_NDEBUG) {
176 String8 argv_String;
177 for (int i = 0; i < argc; ++i) {
178 argv_String.append("\"");
179 argv_String.append(argv[i]);
180 argv_String.append("\" ");
181 }
182 ALOGV("app_process main with argv: %s", argv_String.string());
183 }
184
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000185 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 // Process command line arguments
187 // ignore argv[0]
188 argc--;
189 argv++;
190
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100191 // Everything up to '--' or first non '-' arg goes to the vm.
192 //
193 // The first argument after the VM args is the "parent dir", which
194 // is currently unused.
195 //
196 // After the parent dir, we expect one or more the following internal
197 // arguments :
198 //
199 // --zygote : Start in zygote mode
200 // --start-system-server : Start the system server.
201 // --application : Start in application (stand alone, non zygote) mode.
202 // --nice-name : The nice name for this process.
203 //
204 // For non zygote starts, these arguments will be followed by
205 // the main class name. All remaining arguments are passed to
206 // the main method of this class.
207 //
208 // For zygote starts, all remaining arguments are passed to the zygote.
209 // main function.
Jeff Brown00c0cd42014-09-10 16:48:46 -0700210 //
211 // Note that we must copy argument string values since we will rewrite the
212 // entire argument block when we apply the nice name to argv0.
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700213 //
214 // As an exception to the above rule, anything in "spaced commands"
215 // goes to the vm even though it has a space in it.
216 const char* spaced_commands[] = { "-cp", "-classpath" };
217 // Allow "spaced commands" to be succeeded by exactly 1 argument (regardless of -s).
218 bool known_command = false;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100219
Jeff Brown00c0cd42014-09-10 16:48:46 -0700220 int i;
221 for (i = 0; i < argc; i++) {
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700222 if (known_command == true) {
223 runtime.addOption(strdup(argv[i]));
George Burgess IVc29844d2017-07-11 17:18:26 -0700224 // The static analyzer gets upset that we don't ever free the above
225 // string. Since the allocation is from main, leaking it doesn't seem
226 // problematic. NOLINTNEXTLINE
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700227 ALOGV("app_process main add known option '%s'", argv[i]);
228 known_command = false;
229 continue;
230 }
231
232 for (int j = 0;
233 j < static_cast<int>(sizeof(spaced_commands) / sizeof(spaced_commands[0]));
234 ++j) {
235 if (strcmp(argv[i], spaced_commands[j]) == 0) {
236 known_command = true;
237 ALOGV("app_process main found known command '%s'", argv[i]);
238 }
239 }
240
Jeff Brown00c0cd42014-09-10 16:48:46 -0700241 if (argv[i][0] != '-') {
242 break;
243 }
244 if (argv[i][1] == '-' && argv[i][2] == 0) {
245 ++i; // Skip --.
246 break;
247 }
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700248
Jeff Brown00c0cd42014-09-10 16:48:46 -0700249 runtime.addOption(strdup(argv[i]));
George Burgess IVc29844d2017-07-11 17:18:26 -0700250 // The static analyzer gets upset that we don't ever free the above
251 // string. Since the allocation is from main, leaking it doesn't seem
252 // problematic. NOLINTNEXTLINE
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700253 ALOGV("app_process main add option '%s'", argv[i]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255
Jeff Brownebed7d62011-05-16 17:08:42 -0700256 // Parse runtime arguments. Stop at first unrecognized option.
257 bool zygote = false;
258 bool startSystemServer = false;
259 bool application = false;
Jeff Brown00c0cd42014-09-10 16:48:46 -0700260 String8 niceName;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100261 String8 className;
262
263 ++i; // Skip unused "parent dir" argument.
Jeff Brownebed7d62011-05-16 17:08:42 -0700264 while (i < argc) {
265 const char* arg = argv[i++];
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100266 if (strcmp(arg, "--zygote") == 0) {
Jeff Brownebed7d62011-05-16 17:08:42 -0700267 zygote = true;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100268 niceName = ZYGOTE_NICE_NAME;
Jeff Brownebed7d62011-05-16 17:08:42 -0700269 } else if (strcmp(arg, "--start-system-server") == 0) {
270 startSystemServer = true;
271 } else if (strcmp(arg, "--application") == 0) {
272 application = true;
273 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700274 niceName.setTo(arg + 12);
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100275 } else if (strncmp(arg, "--", 2) != 0) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100276 className.setTo(arg);
Jeff Brownebed7d62011-05-16 17:08:42 -0700277 break;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100278 } else {
279 --i;
280 break;
Jeff Brownebed7d62011-05-16 17:08:42 -0700281 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100284 Vector<String8> args;
285 if (!className.isEmpty()) {
286 // We're not in zygote mode, the only argument we need to pass
287 // to RuntimeInit is the application argument.
288 //
289 // The Remainder of args get passed to startup class main(). Make
290 // copies of them before we overwrite them with the process name.
291 args.add(application ? String8("application") : String8("tool"));
292 runtime.setClassNameAndArgs(className, argc - i, argv + i);
Igor Murashkin4f66cb32016-09-29 15:19:58 -0700293
294 if (!LOG_NDEBUG) {
295 String8 restOfArgs;
296 char* const* argv_new = argv + i;
297 int argc_new = argc - i;
298 for (int k = 0; k < argc_new; ++k) {
299 restOfArgs.append("\"");
300 restOfArgs.append(argv_new[k]);
301 restOfArgs.append("\" ");
302 }
303 ALOGV("Class name = %s, args = %s", className.string(), restOfArgs.string());
304 }
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100305 } else {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100306 // We're in zygote mode.
307 maybeCreateDalvikCache();
308
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100309 if (startSystemServer) {
310 args.add(String8("start-system-server"));
311 }
312
Narayan Kamathc41638c2014-04-07 13:56:15 +0100313 char prop[PROP_VALUE_MAX];
314 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
Elliott Hughes6bd762282014-04-23 16:54:33 -0700315 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
Narayan Kamathc41638c2014-04-07 13:56:15 +0100316 ABI_LIST_PROPERTY);
317 return 11;
318 }
319
320 String8 abiFlag("--abi-list=");
321 abiFlag.append(prop);
322 args.add(abiFlag);
323
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100324 // In zygote mode, pass all remaining arguments to the zygote
325 // main() method.
326 for (; i < argc; ++i) {
327 args.add(String8(argv[i]));
328 }
329 }
330
Jeff Brown00c0cd42014-09-10 16:48:46 -0700331 if (!niceName.isEmpty()) {
Dmitriy Filchenkof5b6e552016-07-18 16:00:35 -0700332 runtime.setArgv0(niceName.string(), true /* setProcName */);
Jeff Brownebed7d62011-05-16 17:08:42 -0700333 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
Jeff Brownebed7d62011-05-16 17:08:42 -0700335 if (zygote) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200336 runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
Jeff Brownebed7d62011-05-16 17:08:42 -0700337 } else if (className) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200338 runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
341 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700342 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344}