The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Main entry of app process. |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 3 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4 | * Starts the interpreted runtime, then starts up the application. |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 5 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #define LOG_TAG "appproc" |
| 9 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 10 | #include <binder/IPCThreadState.h> |
| 11 | #include <binder/ProcessState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 12 | #include <utils/Log.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 13 | #include <cutils/memory.h> |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 14 | #include <cutils/process_name.h> |
| 15 | #include <cutils/properties.h> |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 16 | #include <cutils/trace.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | #include <android_runtime/AndroidRuntime.h> |
Narayan Kamath | 973cdee | 2014-04-25 11:43:22 +0100 | [diff] [blame] | 18 | #include <private/android_filesystem_config.h> // for AID_SYSTEM |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | |
Nick Kralevich | 1fe21bd | 2013-03-15 11:38:29 -0700 | [diff] [blame] | 20 | #include <stdlib.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <unistd.h> |
Nick Kralevich | 195c73c | 2014-04-25 15:01:24 -0700 | [diff] [blame] | 23 | #include <sys/prctl.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | void app_usage() |
| 28 | { |
| 29 | fprintf(stderr, |
| 30 | "Usage: app_process [java-options] cmd-dir start-class-name [options]\n"); |
| 31 | } |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | class AppRuntime : public AndroidRuntime |
| 34 | { |
| 35 | public: |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame] | 36 | AppRuntime(char* argBlockStart, const size_t argBlockLength) |
| 37 | : AndroidRuntime(argBlockStart, argBlockLength) |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 38 | , mClass(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 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])); |
| 46 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 49 | virtual void onVmCreated(JNIEnv* env) |
| 50 | { |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 51 | if (mClassName.isEmpty()) { |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 52 | 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 Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 68 | char* slashClassName = toSlashClassName(mClassName.string()); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 69 | mClass = env->FindClass(slashClassName); |
| 70 | if (mClass == NULL) { |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 71 | ALOGE("ERROR: could not find class '%s'\n", mClassName.string()); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 72 | } |
| 73 | free(slashClassName); |
| 74 | |
| 75 | mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass)); |
| 76 | } |
| 77 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | virtual void onStarted() |
| 79 | { |
| 80 | sp<ProcessState> proc = ProcessState::self(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 81 | ALOGV("App process: starting thread pool.\n"); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 82 | proc->startThreadPool(); |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 83 | |
| 84 | AndroidRuntime* ar = AndroidRuntime::getRuntime(); |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 85 | ar->callMain(mClassName, mClass, mArgs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 87 | IPCThreadState::self()->stopProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | virtual void onZygoteInit() |
| 91 | { |
Jamie Gennis | 6ad0452 | 2013-04-15 18:53:24 -0700 | [diff] [blame] | 92 | // Re-enable tracing now that we're no longer in Zygote. |
| 93 | atrace_set_tracing_enabled(true); |
| 94 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | sp<ProcessState> proc = ProcessState::self(); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 96 | ALOGV("App process: starting thread pool.\n"); |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 97 | proc->startThreadPool(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | virtual void onExit(int code) |
| 101 | { |
Narayan Kamath | 90c75cf | 2014-04-12 12:25:50 +0100 | [diff] [blame] | 102 | if (mClassName.isEmpty()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | // if zygote |
Jeff Brown | 10e8971 | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 104 | IPCThreadState::self()->stopProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | AndroidRuntime::onExit(code); |
| 108 | } |
| 109 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 110 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 111 | String8 mClassName; |
| 112 | Vector<String8> mArgs; |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 113 | jclass mClass; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } |
| 117 | |
| 118 | using namespace android; |
| 119 | |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame] | 120 | static 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]); |
| 136 | end += strlen(argv[argc - 1]); |
| 137 | |
| 138 | return (end - start); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Narayan Kamath | 973cdee | 2014-04-25 11:43:22 +0100 | [diff] [blame] | 141 | static void maybeCreateDalvikCache() { |
| 142 | #if defined(__aarch64__) |
| 143 | static const char kInstructionSet[] = "arm64"; |
| 144 | #elif defined(__x86_64__) |
| 145 | static const char kInstructionSet[] = "x86_64"; |
| 146 | #elif defined(__arm__) |
| 147 | static const char kInstructionSet[] = "arm"; |
Narayan Kamath | d618320 | 2014-04-30 16:45:07 +0100 | [diff] [blame] | 148 | #elif defined(__i386__) |
Narayan Kamath | 973cdee | 2014-04-25 11:43:22 +0100 | [diff] [blame] | 149 | static const char kInstructionSet[] = "x86"; |
| 150 | #elif defined (__mips__) |
| 151 | static const char kInstructionSet[] = "mips"; |
| 152 | #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 | |
| 164 | int result = mkdir(dalvikCacheDir, 0771); |
| 165 | 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. |
| 171 | result = chown(dalvikCacheDir, AID_SYSTEM, AID_SYSTEM); |
| 172 | LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno)); |
| 173 | |
| 174 | result = chmod(dalvikCacheDir, 0771); |
| 175 | LOG_ALWAYS_FATAL_IF((result < 0), |
| 176 | "Error changing dalvik-cache permissions : %s", strerror(errno)); |
| 177 | } |
| 178 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 179 | #if defined(__LP64__) |
| 180 | static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64"; |
Narayan Kamath | d35d3e5 | 2014-04-10 12:13:06 +0100 | [diff] [blame] | 181 | static const char ZYGOTE_NICE_NAME[] = "zygote64"; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 182 | #else |
| 183 | static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32"; |
Narayan Kamath | d35d3e5 | 2014-04-10 12:13:06 +0100 | [diff] [blame] | 184 | static const char ZYGOTE_NICE_NAME[] = "zygote"; |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 185 | #endif |
| 186 | |
Nick Kralevich | 8a0a929 | 2013-03-14 13:23:52 -0700 | [diff] [blame] | 187 | int main(int argc, char* const argv[]) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | { |
Nick Kralevich | 195c73c | 2014-04-25 15:01:24 -0700 | [diff] [blame] | 189 | 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 Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame] | 198 | AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | // Process command line arguments |
| 200 | // ignore argv[0] |
| 201 | argc--; |
| 202 | argv++; |
| 203 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 204 | // 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. |
| 223 | |
Elliott Hughes | d195e5a | 2011-04-13 15:39:37 -0700 | [diff] [blame] | 224 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | int i = runtime.addVmArguments(argc, argv); |
| 226 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 227 | // Parse runtime arguments. Stop at first unrecognized option. |
| 228 | bool zygote = false; |
| 229 | bool startSystemServer = false; |
| 230 | bool application = false; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 231 | const char* niceName = NULL; |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 232 | String8 className; |
| 233 | |
| 234 | ++i; // Skip unused "parent dir" argument. |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 235 | while (i < argc) { |
| 236 | const char* arg = argv[i++]; |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 237 | if (strcmp(arg, "--zygote") == 0) { |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 238 | zygote = true; |
Narayan Kamath | d35d3e5 | 2014-04-10 12:13:06 +0100 | [diff] [blame] | 239 | niceName = ZYGOTE_NICE_NAME; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 240 | } else if (strcmp(arg, "--start-system-server") == 0) { |
| 241 | startSystemServer = true; |
| 242 | } else if (strcmp(arg, "--application") == 0) { |
| 243 | application = true; |
| 244 | } else if (strncmp(arg, "--nice-name=", 12) == 0) { |
| 245 | niceName = arg + 12; |
Narayan Kamath | d35d3e5 | 2014-04-10 12:13:06 +0100 | [diff] [blame] | 246 | } else if (strncmp(arg, "--", 2) != 0) { |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 247 | className.setTo(arg); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 248 | break; |
Narayan Kamath | d35d3e5 | 2014-04-10 12:13:06 +0100 | [diff] [blame] | 249 | } else { |
| 250 | --i; |
| 251 | break; |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 252 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 255 | Vector<String8> args; |
| 256 | if (!className.isEmpty()) { |
| 257 | // We're not in zygote mode, the only argument we need to pass |
| 258 | // to RuntimeInit is the application argument. |
| 259 | // |
| 260 | // The Remainder of args get passed to startup class main(). Make |
| 261 | // copies of them before we overwrite them with the process name. |
| 262 | args.add(application ? String8("application") : String8("tool")); |
| 263 | runtime.setClassNameAndArgs(className, argc - i, argv + i); |
| 264 | } else { |
Narayan Kamath | 973cdee | 2014-04-25 11:43:22 +0100 | [diff] [blame] | 265 | // We're in zygote mode. |
| 266 | maybeCreateDalvikCache(); |
| 267 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 268 | if (startSystemServer) { |
| 269 | args.add(String8("start-system-server")); |
| 270 | } |
| 271 | |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 272 | char prop[PROP_VALUE_MAX]; |
| 273 | if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) { |
Elliott Hughes | 6bd76228 | 2014-04-23 16:54:33 -0700 | [diff] [blame] | 274 | LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.", |
Narayan Kamath | c41638c | 2014-04-07 13:56:15 +0100 | [diff] [blame] | 275 | ABI_LIST_PROPERTY); |
| 276 | return 11; |
| 277 | } |
| 278 | |
| 279 | String8 abiFlag("--abi-list="); |
| 280 | abiFlag.append(prop); |
| 281 | args.add(abiFlag); |
| 282 | |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 283 | // In zygote mode, pass all remaining arguments to the zygote |
| 284 | // main() method. |
| 285 | for (; i < argc; ++i) { |
| 286 | args.add(String8(argv[i])); |
| 287 | } |
| 288 | } |
| 289 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 290 | if (niceName && *niceName) { |
Narayan Kamath | a23fcd7 | 2014-03-28 13:39:21 +0000 | [diff] [blame] | 291 | runtime.setArgv0(niceName); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 292 | set_process_name(niceName); |
| 293 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 295 | if (zygote) { |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 296 | runtime.start("com.android.internal.os.ZygoteInit", args); |
Jeff Brown | ebed7d6 | 2011-05-16 17:08:42 -0700 | [diff] [blame] | 297 | } else if (className) { |
Narayan Kamath | 22ec1ee | 2014-04-07 12:44:58 +0100 | [diff] [blame] | 298 | runtime.start("com.android.internal.os.RuntimeInit", args); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | fprintf(stderr, "Error: no class name or --zygote supplied.\n"); |
| 301 | app_usage(); |
Brian Carlstrom | de6d1d8 | 2010-10-07 16:02:11 -0700 | [diff] [blame] | 302 | LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 303 | return 10; |
| 304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |