blob: 8bcbf51cc8572fcc8f3aea34833a8d31a46f8669 [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/process_name.h>
21#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>
Nicolas Geoffray573895b2016-02-26 13:16:50 +000024#include <nativeloader/native_loader.h>
Narayan Kamathd1e127e2014-04-25 11:43:22 +010025#include <private/android_filesystem_config.h> // for AID_SYSTEM
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027namespace android {
28
Andreas Gampecfedceb2014-09-30 21:48:18 -070029static void app_usage()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030{
31 fprintf(stderr,
32 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n");
33}
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035class AppRuntime : public AndroidRuntime
36{
37public:
Narayan Kamatha23fcd72014-03-28 13:39:21 +000038 AppRuntime(char* argBlockStart, const size_t argBlockLength)
39 : AndroidRuntime(argBlockStart, argBlockLength)
Elliott Hughesd195e5a2011-04-13 15:39:37 -070040 , mClass(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 {
42 }
43
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010044 void setClassNameAndArgs(const String8& className, int argc, char * const *argv) {
45 mClassName = className;
46 for (int i = 0; i < argc; ++i) {
47 mArgs.add(String8(argv[i]));
48 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 }
50
Elliott Hughesd195e5a2011-04-13 15:39:37 -070051 virtual void onVmCreated(JNIEnv* env)
52 {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010053 if (mClassName.isEmpty()) {
Elliott Hughesd195e5a2011-04-13 15:39:37 -070054 return; // Zygote. Nothing to do here.
55 }
56
57 /*
58 * This is a little awkward because the JNI FindClass call uses the
59 * class loader associated with the native method we're executing in.
60 * If called in onStarted (from RuntimeInit.finishInit because we're
61 * launching "am", for example), FindClass would see that we're calling
62 * from a boot class' native method, and so wouldn't look for the class
63 * we're trying to look up in CLASSPATH. Unfortunately it needs to,
64 * because the "am" classes are not boot classes.
65 *
66 * The easiest fix is to call FindClass here, early on before we start
67 * executing boot class Java code and thereby deny ourselves access to
68 * non-boot classes.
69 */
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010070 char* slashClassName = toSlashClassName(mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070071 mClass = env->FindClass(slashClassName);
72 if (mClass == NULL) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010073 ALOGE("ERROR: could not find class '%s'\n", mClassName.string());
Elliott Hughesd195e5a2011-04-13 15:39:37 -070074 }
75 free(slashClassName);
76
77 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass));
78 }
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 virtual void onStarted()
81 {
82 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010083 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070084 proc->startThreadPool();
Elliott Hughesd195e5a2011-04-13 15:39:37 -070085
86 AndroidRuntime* ar = AndroidRuntime::getRuntime();
Narayan Kamath22ec1ee2014-04-07 12:44:58 +010087 ar->callMain(mClassName, mClass, mArgs);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Jeff Brown10e89712011-07-08 18:52:57 -070089 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();
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]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700135 end += strlen(argv[argc - 1]) + 1;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000136 return (end - start);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137}
138
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100139static 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";
Narayan Kamath6eb1b262014-04-30 16:45:07 +0100146#elif defined(__i386__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100147 static const char kInstructionSet[] = "x86";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800148#elif defined (__mips__) && !defined(__LP64__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100149 static const char kInstructionSet[] = "mips";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800150#elif defined (__mips__) && defined(__LP64__)
151 static const char kInstructionSet[] = "mips64";
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100152#else
153#error "Unknown instruction set"
154#endif
155 const char* androidRoot = getenv("ANDROID_DATA");
156 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset");
157
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));
163
Alex Light55471dc2014-08-27 15:39:17 -0700164 int result = mkdir(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100165 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST),
166 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno));
167
168 // We always perform these steps because the directory might
169 // already exist, with wider permissions and a different owner
170 // than we'd like.
Alex Light55471dc2014-08-27 15:39:17 -0700171 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100172 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
173
Alex Light55471dc2014-08-27 15:39:17 -0700174 result = chmod(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100175 LOG_ALWAYS_FATAL_IF((result < 0),
176 "Error changing dalvik-cache permissions : %s", strerror(errno));
177}
178
Narayan Kamathc41638c2014-04-07 13:56:15 +0100179#if defined(__LP64__)
180static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100181static const char ZYGOTE_NICE_NAME[] = "zygote64";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100182#else
183static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100184static const char ZYGOTE_NICE_NAME[] = "zygote";
Narayan Kamathc41638c2014-04-07 13:56:15 +0100185#endif
186
Nick Kralevich8a0a9292013-03-14 13:23:52 -0700187int main(int argc, char* const argv[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188{
Nick Kralevich195c73c2014-04-25 15:01:24 -0700189 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
190 // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
191 // EINVAL. Don't die on such kernels.
192 if (errno != EINVAL) {
193 LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
194 return 12;
195 }
196 }
197
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000198 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 // Process command line arguments
200 // ignore argv[0]
201 argc--;
202 argv++;
203
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100204 // Everything up to '--' or first non '-' arg goes to the vm.
205 //
206 // The first argument after the VM args is the "parent dir", which
207 // is currently unused.
208 //
209 // After the parent dir, we expect one or more the following internal
210 // arguments :
211 //
212 // --zygote : Start in zygote mode
213 // --start-system-server : Start the system server.
214 // --application : Start in application (stand alone, non zygote) mode.
215 // --nice-name : The nice name for this process.
216 //
217 // For non zygote starts, these arguments will be followed by
218 // the main class name. All remaining arguments are passed to
219 // the main method of this class.
220 //
221 // For zygote starts, all remaining arguments are passed to the zygote.
222 // main function.
Jeff Brown00c0cd42014-09-10 16:48:46 -0700223 //
224 // Note that we must copy argument string values since we will rewrite the
225 // entire argument block when we apply the nice name to argv0.
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100226
Jeff Brown00c0cd42014-09-10 16:48:46 -0700227 int i;
228 for (i = 0; i < argc; i++) {
229 if (argv[i][0] != '-') {
230 break;
231 }
232 if (argv[i][1] == '-' && argv[i][2] == 0) {
233 ++i; // Skip --.
234 break;
235 }
236 runtime.addOption(strdup(argv[i]));
237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
Jeff Brownebed7d62011-05-16 17:08:42 -0700239 // Parse runtime arguments. Stop at first unrecognized option.
240 bool zygote = false;
241 bool startSystemServer = false;
242 bool application = false;
Jeff Brown00c0cd42014-09-10 16:48:46 -0700243 String8 niceName;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100244 String8 className;
245
246 ++i; // Skip unused "parent dir" argument.
Jeff Brownebed7d62011-05-16 17:08:42 -0700247 while (i < argc) {
248 const char* arg = argv[i++];
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100249 if (strcmp(arg, "--zygote") == 0) {
Jeff Brownebed7d62011-05-16 17:08:42 -0700250 zygote = true;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100251 niceName = ZYGOTE_NICE_NAME;
Jeff Brownebed7d62011-05-16 17:08:42 -0700252 } else if (strcmp(arg, "--start-system-server") == 0) {
253 startSystemServer = true;
254 } else if (strcmp(arg, "--application") == 0) {
255 application = true;
256 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700257 niceName.setTo(arg + 12);
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100258 } else if (strncmp(arg, "--", 2) != 0) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100259 className.setTo(arg);
Jeff Brownebed7d62011-05-16 17:08:42 -0700260 break;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100261 } else {
262 --i;
263 break;
Jeff Brownebed7d62011-05-16 17:08:42 -0700264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 }
266
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100267 Vector<String8> args;
268 if (!className.isEmpty()) {
269 // We're not in zygote mode, the only argument we need to pass
270 // to RuntimeInit is the application argument.
271 //
272 // The Remainder of args get passed to startup class main(). Make
273 // copies of them before we overwrite them with the process name.
274 args.add(application ? String8("application") : String8("tool"));
275 runtime.setClassNameAndArgs(className, argc - i, argv + i);
276 } else {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100277 // We're in zygote mode.
278 maybeCreateDalvikCache();
279
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100280 if (startSystemServer) {
281 args.add(String8("start-system-server"));
282 }
283
Narayan Kamathc41638c2014-04-07 13:56:15 +0100284 char prop[PROP_VALUE_MAX];
285 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
Elliott Hughes6bd762282014-04-23 16:54:33 -0700286 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
Narayan Kamathc41638c2014-04-07 13:56:15 +0100287 ABI_LIST_PROPERTY);
288 return 11;
289 }
290
291 String8 abiFlag("--abi-list=");
292 abiFlag.append(prop);
293 args.add(abiFlag);
294
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100295 // In zygote mode, pass all remaining arguments to the zygote
296 // main() method.
297 for (; i < argc; ++i) {
298 args.add(String8(argv[i]));
299 }
300 }
301
Jeff Brown00c0cd42014-09-10 16:48:46 -0700302 if (!niceName.isEmpty()) {
303 runtime.setArgv0(niceName.string());
304 set_process_name(niceName.string());
Jeff Brownebed7d62011-05-16 17:08:42 -0700305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
Jeff Brownebed7d62011-05-16 17:08:42 -0700307 if (zygote) {
Dimitry Ivanovb0824972016-02-22 11:22:11 -0800308 PreloadPublicNativeLibraries();
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200309 runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
Jeff Brownebed7d62011-05-16 17:08:42 -0700310 } else if (className) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200311 runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
314 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700315 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 return 10;
317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318}