app_main.cpp revision fc737fb76cc889d0c19bd8bf74d2b5f1fc4fbe6e
1/* 2 * Main entry of app process. 3 * 4 * Starts the interpreted runtime, then starts up the application. 5 * 6 */ 7 8#define LOG_TAG "appproc" 9 10#include <stdio.h> 11#include <stdlib.h> 12#include <sys/prctl.h> 13#include <sys/stat.h> 14#include <unistd.h> 15 16#include <binder/IPCThreadState.h> 17#include <binder/ProcessState.h> 18#include <utils/Log.h> 19#include <cutils/memory.h> 20#include <cutils/process_name.h> 21#include <cutils/properties.h> 22#include <cutils/trace.h> 23#include <android_runtime/AndroidRuntime.h> 24#include <private/android_filesystem_config.h> // for AID_SYSTEM 25 26namespace android { 27 28static void app_usage() 29{ 30 fprintf(stderr, 31 "Usage: app_process [java-options] cmd-dir start-class-name [options]\n"); 32} 33 34class AppRuntime : public AndroidRuntime 35{ 36public: 37 AppRuntime(char* argBlockStart, const size_t argBlockLength) 38 : AndroidRuntime(argBlockStart, argBlockLength) 39 , mClass(NULL) 40 { 41 } 42 43 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 } 48 } 49 50 virtual void onVmCreated(JNIEnv* env) 51 { 52 if (mClassName.isEmpty()) { 53 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 */ 69 char* slashClassName = toSlashClassName(mClassName.string()); 70 mClass = env->FindClass(slashClassName); 71 if (mClass == NULL) { 72 ALOGE("ERROR: could not find class '%s'\n", mClassName.string()); 73 } 74 free(slashClassName); 75 76 mClass = reinterpret_cast<jclass>(env->NewGlobalRef(mClass)); 77 } 78 79 virtual void onStarted() 80 { 81 sp<ProcessState> proc = ProcessState::self(); 82 ALOGV("App process: starting thread pool.\n"); 83 proc->startThreadPool(); 84 85 AndroidRuntime* ar = AndroidRuntime::getRuntime(); 86 ar->callMain(mClassName, mClass, mArgs); 87 88 IPCThreadState::self()->stopProcess(); 89 } 90 91 virtual void onZygoteInit() 92 { 93 // Re-enable tracing now that we're no longer in Zygote. 94 atrace_set_tracing_enabled(true); 95 96 sp<ProcessState> proc = ProcessState::self(); 97 ALOGV("App process: starting thread pool.\n"); 98 proc->startThreadPool(); 99 } 100 101 virtual void onExit(int code) 102 { 103 if (mClassName.isEmpty()) { 104 // if zygote 105 IPCThreadState::self()->stopProcess(); 106 } 107 108 AndroidRuntime::onExit(code); 109 } 110 111 112 String8 mClassName; 113 Vector<String8> mArgs; 114 jclass mClass; 115}; 116 117} 118 119using namespace android; 120 121static size_t computeArgBlockSize(int argc, char* const argv[]) { 122 // TODO: This assumes that all arguments are allocated in 123 // contiguous memory. There isn't any documented guarantee 124 // that this is the case, but this is how the kernel does it 125 // (see fs/exec.c). 126 // 127 // Also note that this is a constant for "normal" android apps. 128 // Since they're forked from zygote, the size of their command line 129 // is the size of the zygote command line. 130 // 131 // We change the process name of the process by over-writing 132 // the start of the argument block (argv[0]) with the new name of 133 // the process, so we'd mysteriously start getting truncated process 134 // names if the zygote command line decreases in size. 135 uintptr_t start = reinterpret_cast<uintptr_t>(argv[0]); 136 uintptr_t end = reinterpret_cast<uintptr_t>(argv[argc - 1]); 137 end += strlen(argv[argc - 1]) + 1; 138 return (end - start); 139} 140 141static 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"; 148#elif defined(__i386__) 149 static const char kInstructionSet[] = "x86"; 150#elif defined (__mips__) && !defined(__LP64__) 151 static const char kInstructionSet[] = "mips"; 152#elif defined (__mips__) && defined(__LP64__) 153 static const char kInstructionSet[] = "mips64"; 154#else 155#error "Unknown instruction set" 156#endif 157 const char* androidRoot = getenv("ANDROID_DATA"); 158 LOG_ALWAYS_FATAL_IF(androidRoot == NULL, "ANDROID_DATA environment variable unset"); 159 160 char dalvikCacheDir[PATH_MAX]; 161 const int numChars = snprintf(dalvikCacheDir, PATH_MAX, 162 "%s/dalvik-cache/%s", androidRoot, kInstructionSet); 163 LOG_ALWAYS_FATAL_IF((numChars >= PATH_MAX || numChars < 0), 164 "Error constructing dalvik cache : %s", strerror(errno)); 165 166 int result = mkdir(dalvikCacheDir, 0711); 167 LOG_ALWAYS_FATAL_IF((result < 0 && errno != EEXIST), 168 "Error creating cache dir %s : %s", dalvikCacheDir, strerror(errno)); 169 170 // We always perform these steps because the directory might 171 // already exist, with wider permissions and a different owner 172 // than we'd like. 173 result = chown(dalvikCacheDir, AID_ROOT, AID_ROOT); 174 LOG_ALWAYS_FATAL_IF((result < 0), "Error changing dalvik-cache ownership : %s", strerror(errno)); 175 176 result = chmod(dalvikCacheDir, 0711); 177 LOG_ALWAYS_FATAL_IF((result < 0), 178 "Error changing dalvik-cache permissions : %s", strerror(errno)); 179} 180 181#if defined(__LP64__) 182static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64"; 183static const char ZYGOTE_NICE_NAME[] = "zygote64"; 184#else 185static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32"; 186static const char ZYGOTE_NICE_NAME[] = "zygote"; 187#endif 188 189int main(int argc, char* const argv[]) 190{ 191 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) { 192 // Older kernels don't understand PR_SET_NO_NEW_PRIVS and return 193 // EINVAL. Don't die on such kernels. 194 if (errno != EINVAL) { 195 LOG_ALWAYS_FATAL("PR_SET_NO_NEW_PRIVS failed: %s", strerror(errno)); 196 return 12; 197 } 198 } 199 200 AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv)); 201 // Process command line arguments 202 // ignore argv[0] 203 argc--; 204 argv++; 205 206 // Everything up to '--' or first non '-' arg goes to the vm. 207 // 208 // The first argument after the VM args is the "parent dir", which 209 // is currently unused. 210 // 211 // After the parent dir, we expect one or more the following internal 212 // arguments : 213 // 214 // --zygote : Start in zygote mode 215 // --start-system-server : Start the system server. 216 // --application : Start in application (stand alone, non zygote) mode. 217 // --nice-name : The nice name for this process. 218 // 219 // For non zygote starts, these arguments will be followed by 220 // the main class name. All remaining arguments are passed to 221 // the main method of this class. 222 // 223 // For zygote starts, all remaining arguments are passed to the zygote. 224 // main function. 225 // 226 // Note that we must copy argument string values since we will rewrite the 227 // entire argument block when we apply the nice name to argv0. 228 229 int i; 230 for (i = 0; i < argc; i++) { 231 if (argv[i][0] != '-') { 232 break; 233 } 234 if (argv[i][1] == '-' && argv[i][2] == 0) { 235 ++i; // Skip --. 236 break; 237 } 238 runtime.addOption(strdup(argv[i])); 239 } 240 241 // Parse runtime arguments. Stop at first unrecognized option. 242 bool zygote = false; 243 bool startSystemServer = false; 244 bool application = false; 245 String8 niceName; 246 String8 className; 247 248 ++i; // Skip unused "parent dir" argument. 249 while (i < argc) { 250 const char* arg = argv[i++]; 251 if (strcmp(arg, "--zygote") == 0) { 252 zygote = true; 253 niceName = ZYGOTE_NICE_NAME; 254 } else if (strcmp(arg, "--start-system-server") == 0) { 255 startSystemServer = true; 256 } else if (strcmp(arg, "--application") == 0) { 257 application = true; 258 } else if (strncmp(arg, "--nice-name=", 12) == 0) { 259 niceName.setTo(arg + 12); 260 } else if (strncmp(arg, "--", 2) != 0) { 261 className.setTo(arg); 262 break; 263 } else { 264 --i; 265 break; 266 } 267 } 268 269 Vector<String8> args; 270 if (!className.isEmpty()) { 271 // We're not in zygote mode, the only argument we need to pass 272 // to RuntimeInit is the application argument. 273 // 274 // The Remainder of args get passed to startup class main(). Make 275 // copies of them before we overwrite them with the process name. 276 args.add(application ? String8("application") : String8("tool")); 277 runtime.setClassNameAndArgs(className, argc - i, argv + i); 278 } else { 279 // We're in zygote mode. 280 maybeCreateDalvikCache(); 281 282 if (startSystemServer) { 283 args.add(String8("start-system-server")); 284 } 285 286 char prop[PROP_VALUE_MAX]; 287 if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) { 288 LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.", 289 ABI_LIST_PROPERTY); 290 return 11; 291 } 292 293 String8 abiFlag("--abi-list="); 294 abiFlag.append(prop); 295 args.add(abiFlag); 296 297 // In zygote mode, pass all remaining arguments to the zygote 298 // main() method. 299 for (; i < argc; ++i) { 300 args.add(String8(argv[i])); 301 } 302 } 303 304 if (!niceName.isEmpty()) { 305 runtime.setArgv0(niceName.string()); 306 set_process_name(niceName.string()); 307 } 308 309 if (zygote) { 310 runtime.start("com.android.internal.os.ZygoteInit", args); 311 } else if (className) { 312 runtime.start("com.android.internal.os.RuntimeInit", args); 313 } else { 314 fprintf(stderr, "Error: no class name or --zygote supplied.\n"); 315 app_usage(); 316 LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied."); 317 return 10; 318 } 319} 320