blob: 2e023825a219a91e2907d164cba95afe0c4ccd76 [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>
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();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
90
91 virtual void onZygoteInit()
92 {
93 sp<ProcessState> proc = ProcessState::self();
Steve Block71f2cf12011-10-20 11:56:00 +010094 ALOGV("App process: starting thread pool.\n");
Jeff Brown10e89712011-07-08 18:52:57 -070095 proc->startThreadPool();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 }
97
98 virtual void onExit(int code)
99 {
Narayan Kamath90c75cf2014-04-12 12:25:50 +0100100 if (mClassName.isEmpty()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 // if zygote
Jeff Brown10e89712011-07-08 18:52:57 -0700102 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 }
104
105 AndroidRuntime::onExit(code);
106 }
107
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700108
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100109 String8 mClassName;
110 Vector<String8> mArgs;
Elliott Hughesd195e5a2011-04-13 15:39:37 -0700111 jclass mClass;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112};
113
114}
115
116using namespace android;
117
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000118static size_t computeArgBlockSize(int argc, char* const argv[]) {
119 // TODO: This assumes that all arguments are allocated in
120 // contiguous memory. There isn't any documented guarantee
121 // that this is the case, but this is how the kernel does it
122 // (see fs/exec.c).
123 //
124 // Also note that this is a constant for "normal" android apps.
125 // Since they're forked from zygote, the size of their command line
126 // is the size of the zygote command line.
127 //
128 // We change the process name of the process by over-writing
129 // the start of the argument block (argv[0]) with the new name of
130 // the process, so we'd mysteriously start getting truncated process
131 // names if the zygote command line decreases in size.
132 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]);
133 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]);
Jeff Brown00c0cd42014-09-10 16:48:46 -0700134 end += strlen(argv[argc - 1]) + 1;
Narayan Kamatha23fcd72014-03-28 13:39:21 +0000135 return (end - start);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100138static void maybeCreateDalvikCache() {
139#if defined(__aarch64__)
140 static const char kInstructionSet[] = "arm64";
141#elif defined(__x86_64__)
142 static const char kInstructionSet[] = "x86_64";
143#elif defined(__arm__)
144 static const char kInstructionSet[] = "arm";
Narayan Kamath6eb1b262014-04-30 16:45:07 +0100145#elif defined(__i386__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100146 static const char kInstructionSet[] = "x86";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800147#elif defined (__mips__) && !defined(__LP64__)
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100148 static const char kInstructionSet[] = "mips";
Douglas Leung7e7c6032014-12-17 20:25:20 -0800149#elif defined (__mips__) && defined(__LP64__)
150 static const char kInstructionSet[] = "mips64";
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100151#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
Alex Light55471dc2014-08-27 15:39:17 -0700163 int result = mkdir(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100164 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.
Alex Light55471dc2014-08-27 15:39:17 -0700170 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100171 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno));
172
Alex Light55471dc2014-08-27 15:39:17 -0700173 result = chmod(dalvikCacheDir, 0711);
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100174 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{
Nick Kralevich195c73c2014-04-25 15:01:24 -0700188 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
189 // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return
190 // EINVAL. Don't die on such kernels.
191 if (errno != EINVAL) {
192 LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno));
193 return 12;
194 }
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.
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100225
Jeff Brown00c0cd42014-09-10 16:48:46 -0700226 int i;
227 for (i = 0; i < argc; i++) {
228 if (argv[i][0] != '-') {
229 break;
230 }
231 if (argv[i][1] == '-' && argv[i][2] == 0) {
232 ++i; // Skip --.
233 break;
234 }
235 runtime.addOption(strdup(argv[i]));
236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237
Jeff Brownebed7d62011-05-16 17:08:42 -0700238 // Parse runtime arguments. Stop at first unrecognized option.
239 bool zygote = false;
240 bool startSystemServer = false;
241 bool application = false;
Jeff Brown00c0cd42014-09-10 16:48:46 -0700242 String8 niceName;
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100243 String8 className;
244
245 ++i; // Skip unused "parent dir" argument.
Jeff Brownebed7d62011-05-16 17:08:42 -0700246 while (i < argc) {
247 const char* arg = argv[i++];
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100248 if (strcmp(arg, "--zygote") == 0) {
Jeff Brownebed7d62011-05-16 17:08:42 -0700249 zygote = true;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100250 niceName = ZYGOTE_NICE_NAME;
Jeff Brownebed7d62011-05-16 17:08:42 -0700251 } else if (strcmp(arg, "--start-system-server") == 0) {
252 startSystemServer = true;
253 } else if (strcmp(arg, "--application") == 0) {
254 application = true;
255 } else if (strncmp(arg, "--nice-name=", 12) == 0) {
Jeff Brown00c0cd42014-09-10 16:48:46 -0700256 niceName.setTo(arg + 12);
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100257 } else if (strncmp(arg, "--", 2) != 0) {
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100258 className.setTo(arg);
Jeff Brownebed7d62011-05-16 17:08:42 -0700259 break;
Narayan Kamathd35d3e52014-04-10 12:13:06 +0100260 } else {
261 --i;
262 break;
Jeff Brownebed7d62011-05-16 17:08:42 -0700263 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 }
265
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100266 Vector<String8> args;
267 if (!className.isEmpty()) {
268 // We're not in zygote mode, the only argument we need to pass
269 // to RuntimeInit is the application argument.
270 //
271 // The Remainder of args get passed to startup class main(). Make
272 // copies of them before we overwrite them with the process name.
273 args.add(application ? String8("application") : String8("tool"));
274 runtime.setClassNameAndArgs(className, argc - i, argv + i);
275 } else {
Narayan Kamathd1e127e2014-04-25 11:43:22 +0100276 // We're in zygote mode.
277 maybeCreateDalvikCache();
278
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100279 if (startSystemServer) {
280 args.add(String8("start-system-server"));
281 }
282
Narayan Kamathc41638c2014-04-07 13:56:15 +0100283 char prop[PROP_VALUE_MAX];
284 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
Elliott Hughes6bd762282014-04-23 16:54:33 -0700285 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
Narayan Kamathc41638c2014-04-07 13:56:15 +0100286 ABI_LIST_PROPERTY);
287 return 11;
288 }
289
290 String8 abiFlag("--abi-list=");
291 abiFlag.append(prop);
292 args.add(abiFlag);
293
Narayan Kamath22ec1ee2014-04-07 12:44:58 +0100294 // In zygote mode, pass all remaining arguments to the zygote
295 // main() method.
296 for (; i < argc; ++i) {
297 args.add(String8(argv[i]));
298 }
299 }
300
Jeff Brown00c0cd42014-09-10 16:48:46 -0700301 if (!niceName.isEmpty()) {
302 runtime.setArgv0(niceName.string());
303 set_process_name(niceName.string());
Jeff Brownebed7d62011-05-16 17:08:42 -0700304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305
Jeff Brownebed7d62011-05-16 17:08:42 -0700306 if (zygote) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200307 runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
Jeff Brownebed7d62011-05-16 17:08:42 -0700308 } else if (className) {
Sebastien Hertz7a09b832015-08-10 18:55:34 +0200309 runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 } else {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 fprintf(stderr, "Error: no class name or --zygote supplied.\n");
312 app_usage();
Brian Carlstromde6d1d82010-10-07 16:02:11 -0700313 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 return 10;
315 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316}