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